Skip to content

Commit

Permalink
we have passed EmitterTests.Enum1 again
Browse files Browse the repository at this point in the history
  • Loading branch information
HAZAMA committed Jul 13, 2018
1 parent aef300a commit eb58bc2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
74 changes: 41 additions & 33 deletions Expresso/CodeGen/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ public Type VisitObjectCreationExpression(ObjectCreationExpression creation, CSh
{
context.TargetType = null;
creation.TypePath.AcceptWalker(this, context);

if(creation.CtorType.IsNull){
// Construct an enum variant
var variant_name = ((MemberType)creation.TypePath).ChildType.Name;
Expand All @@ -1361,18 +1362,21 @@ public Type VisitObjectCreationExpression(ObjectCreationExpression creation, CSh
create_method = create_method.IsGenericMethod ? create_method.MakeGenericMethod(field_type.GenericTypeArguments) : create_method;

var ctor = context.TargetType.GetConstructors().First();
ctor.GetParameters().Select<ParameterInfo, Unit>(p => {
if(p.ParameterType != field_type){
var prev_op_type = context.OperationTypeOnIdentifier;
context.OperationTypeOnIdentifier = OperationType.Load;
foreach(var param in ctor.GetParameters()){
if(param.ParameterType != field_type){
il_generator.Emit(OpCodes.Ldnull);
EmitCast(typeof(object), p.ParameterType);
return null;
EmitCast(typeof(object), param.ParameterType);
}else{
context.RequestPropertyOrField = true;
creation.Items.Select(i => i.AcceptWalker(this, context));
foreach(var item in creation.Items)
item.AcceptWalker(this, context);

il_generator.Emit(OpCodes.Call, create_method);
return null;
}
});
}
context.OperationTypeOnIdentifier = prev_op_type;
il_generator.Emit(OpCodes.Newobj, ctor);

return context.TargetType;
Expand Down Expand Up @@ -2214,17 +2218,24 @@ public Type VisitDestructuringPattern(DestructuringPattern destructuringPattern,
destructuringPattern.TypePath.AcceptWalker(this, context);

var type = context.TargetType;
/*if(destructuringPattern.IsEnum){
var prev_temp_var = context.TemporaryVariable;
if(destructuringPattern.IsEnum){
var variant_name = ((MemberType)destructuringPattern.TypePath).ChildType.Name;
context.TemporaryExpression = CSharpExpr.Field(context.TemporaryVariable, HiddenMemberPrefix + variant_name);
}*/
var variant_type = CSharpCompilerHelpers.GetNativeType(((MemberType)destructuringPattern.TypePath).Target);
var variant_field = variant_type.GetField(HiddenMemberPrefix + variant_name);
var variant_variable = il_generator.DeclareLocal(variant_field.FieldType);

EmitLoadLocal(context.TemporaryVariable, false);
EmitLoadField(variant_field);
EmitSet(null, variant_variable, -1, null);
context.TemporaryVariable = variant_variable;
}

context.RequestPropertyOrField = true;

var block = new List<CSharpExpr>();
var block_params = new List<ExprTree.ParameterExpression>();
var prev_op_type = context.OperationTypeOnIdentifier;
context.OperationTypeOnIdentifier = OperationType.None;
int i = 1;
foreach(var pattern in destructuringPattern.Items){
var item_ast_type = pattern.AcceptWalker(item_type_inferencer);
if(item_ast_type == null)
Expand All @@ -2237,22 +2248,23 @@ public Type VisitDestructuringPattern(DestructuringPattern destructuringPattern,
//context.TemporaryVariable = tmp_param;
//CSharpExpr prev_tmp_expr = null;
/*if(destructuringPattern.IsEnum){
var field_name = "Item" + i++;
var field_access = CSharpExpr.PropertyOrField(context.TemporaryExpression, field_name);
prev_tmp_expr = context.TemporaryExpression;
context.TemporaryExpression = field_access;
var property_name = "Item" + i++;
var variant_type = context.TemporaryVariable.LocalType;
var property = variant_type.GetProperty(property_name);
}*/
context.PropertyOrField = null;
var pattern_type = pattern.AcceptWalker(this, context);
//context.TemporaryVariable = prev_tmp_variable;

if(destructuringPattern.IsEnum){
/*var param = (ExprTree.ParameterExpression)expr;
var assignment2 = CSharpExpr.Assign(param, context.TemporaryExpression);
block_params.Add(param);
block.Add(assignment2);
var property_name = "Item" + i++;
var variant_type = context.TemporaryVariable.LocalType;
var property = variant_type.GetProperty(property_name);
var param = context.CurrentTargetVariable;

context.TemporaryExpression = prev_tmp_expr;*/
EmitLoadLocal(context.TemporaryVariable, false);
EmitCall(property.GetMethod);
EmitSet(null, param, -1, null);
}else{
var field = (FieldInfo)context.PropertyOrField;
context.PropertyOrField = null;
Expand Down Expand Up @@ -2283,9 +2295,8 @@ public Type VisitDestructuringPattern(DestructuringPattern destructuringPattern,
else
block.Add(context.ContextExpression);*/

context.TemporaryVariable = prev_temp_var;
context.RequestPropertyOrField = false;
if(block.Any())
context.ContextExpression = CSharpExpr.Block(block_params, block);

return type;
}
Expand All @@ -2295,8 +2306,6 @@ public Type VisitTuplePattern(TuplePattern tuplePattern, CSharpEmitterContext co
// Tuple patterns should always be combined with value binding patterns
if(tuplePattern.Ancestors.Any(a => a is MatchStatement)){
var native_tuple_type = context.TemporaryVariable.LocalType;
var block_params = new List<ExprTree.ParameterExpression>();
var block = new List<CSharpExpr>();
int i = 1;
var prev_op_type = context.OperationTypeOnIdentifier;
context.OperationTypeOnIdentifier = OperationType.None;
Expand Down Expand Up @@ -2348,9 +2357,6 @@ public Type VisitTuplePattern(TuplePattern tuplePattern, CSharpEmitterContext co
else
block.Add(context.ContextExpression);*/

if(block.Any())
context.ContextExpression = CSharpExpr.Block(block_params, block);

return native_tuple_type;
}else{
foreach(var pattern in tuplePattern.Patterns)
Expand All @@ -2362,7 +2368,10 @@ public Type VisitTuplePattern(TuplePattern tuplePattern, CSharpEmitterContext co

public Type VisitExpressionPattern(ExpressionPattern exprPattern, CSharpEmitterContext context)
{
/*context.RequestMethod = true;
if(context.ContextAst is MatchStatement)
return null;

context.RequestMethod = true;
context.Method = null;
var type = exprPattern.Expression.AcceptWalker(this, context);
context.RequestMethod = false;
Expand All @@ -2378,8 +2387,7 @@ public Type VisitExpressionPattern(ExpressionPattern exprPattern, CSharpEmitterC
return type;
}else{
return type;
}*/
return null;
}
}

public Type VisitIgnoringRestPattern(IgnoringRestPattern restPattern, CSharpEmitterContext context)
Expand Down Expand Up @@ -2836,7 +2844,7 @@ void DefineField(FieldDeclaration fieldDecl, CSharpEmitterContext context)
}
}

List<LocalBuilder> ExpandTuple(Type tupleType, LocalBuilder builder)
/*List<LocalBuilder> ExpandTuple(Type tupleType, LocalBuilder builder)
{
if(tupleType.Name.StartsWith("Tuple", StringComparison.CurrentCulture)){
var parameters = tupleType.GenericTypeArguments.Select((t, i) => {
Expand All @@ -2854,7 +2862,7 @@ List<LocalBuilder> ExpandTuple(Type tupleType, LocalBuilder builder)
}else{
return new List<LocalBuilder>{builder};
}
}
}*/

PropertyInfo GetTupleProperty(IdentifierPattern identifierPattern, Type tupleType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ public void VisitDestructuringPattern(DestructuringPattern destructuringPattern)
if(destructuringPattern.IsEnum){
var variant_name = ((MemberType)destructuringPattern.TypePath).ChildType.Name;
var field = context.TargetType.GetField(HiddenMemberPrefix + variant_name);

generator.EmitLoadLocal(context.TemporaryVariable, false);
generator.EmitLoadField(field);
generator.il_generator.Emit(OpCodes.Ldnull);
generator.EmitBinaryOp(OperatorType.InEquality);
}else{
var native_type = CSharpCompilerHelpers.GetNativeType(destructuringPattern.TypePath);

generator.EmitLoadLocal(context.TemporaryVariable, false);
generator.il_generator.Emit(OpCodes.Isinst, native_type);
generator.il_generator.Emit(OpCodes.Ldnull);
Expand Down
2 changes: 1 addition & 1 deletion Expresso/Expresso.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
<Compile Include="Analysis\PreProcessor.cs" />
<Compile Include="Ast\Statements\DoWhileStatement.cs" />
<Compile Include="CodeGen\PortablePdbGenerator.cs" />
<Compile Include="CodeGen\MatchClauseIdentifierDefiner.cs" />
<Compile Include="CodeGen\MatchClauseConditionDefiner.cs" />
<Compile Include="Ast\GeneralScope\AttributeSection.cs" />
<Compile Include="Ast\Patterns\TypePathPattern.cs" />
<Compile Include="Utilities\Utilities.cs" />
Expand Down

0 comments on commit eb58bc2

Please sign in to comment.