Ejemplo n.º 1
0
        public override void GenerateCode(Emit.MethodBodyGenerator generator)
        {
            // Generate code for the start of the statement.
            var statementLocals = new StatementLocals()
            {
                NonDefaultBreakStatementBehavior = true, NonDefaultSourceSpanBehavior = true
            };

            GenerateStartOfStatement(generator, statementLocals);
            // Set up some labels.
            var continueTarget = generator.CreateLabel();
            var breakTarget    = generator.CreateLabel();

            // Emit the initialization statement.
            if (Initialization != null)
            {
                Initialization.GenerateCode(generator);
            }

            // The inner loop starts here.
            var startOfLoop      = generator.CreateLabel();
            var startOfCondition = generator.CreateLabel();

            if (CheckConditionAtEnd == false)
            {
                generator.Branch(startOfCondition);
            }
            //start of loop

            generator.DefineLabelPosition(startOfLoop);
            // Emit the loop body.
            generator.PushBreakOrContinueInfo(Labels, breakTarget, continueTarget, false);
            Body.GenerateCode(generator);
            generator.PopBreakOrContinueInfo();

            // The continue statement jumps here.
            generator.DefineLabelPosition(continueTarget);

            // Increment the loop variable.
            if (Increments != null)
            {
                Increments.ForEach(e => e.Accept(generator).GenerateCode(generator));
            }

            generator.DefineLabelPosition(startOfCondition);
            // Check the condition and jump to the end if it is false.
            if (Condition != null)
            {
                // generator.MarkSequencePoint(ConditionStatement.Span);
                Condition.Accept(generator).GenerateCode(generator);
                generator.CallStatic(Utils.ReflectionHelpers.BoooleanToBool);
                generator.BranchIfTrue(startOfLoop);
            }

            // Define the end of the loop (actually just after).
            generator.DefineLabelPosition(breakTarget);

            // Generate code for the end of the statement.
            GenerateEndOfStatement(generator, statementLocals);
        }
        /// <inheritdoc/>
        public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption options)
        {
            Type = VariableType != null?VariableType.ResolveType(generator.Context) : TypeProvider.AnyType;

            if (Value != null)
            {
                var defValue = Value.Accept(generator);
                defValue.GenerateCode(generator, AssignOption);
                if (VariableType == null)
                {
                    Type = defValue.Type;
                }
                else if (!TypeUtils.AreReferenceAssignable(Type, defValue.Type) && defValue.Type.TryImplicitConvert(Type, out System.Reflection.MethodInfo opConvert))
                {
                    // When converting value type to Any, must do Box
                    if (defValue.Type.IsValueType && opConvert.GetParameters()[0].ParameterType.IsValueType == false)
                    {
                        generator.Box(defValue.Type);
                    }
                    generator.CallStatic(opConvert);
                }
                else if (defValue.Type.IsValueType && !Type.IsValueType)
                {
                    generator.Box(defValue.Type);
                }
            }
            //initialize
            var variable = generator.DeclareVariable(Type, Name);

            generator.StoreVariable(variable);
            return;
        }
Ejemplo n.º 3
0
 public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption option)
 {
     Target.GenerateCode(generator);
     if (TypeSyntax is null || (TypeSyntax is RefTypeSyntax refType && refType.Name.Equals(LiteralExpression.NullString)))
     {
         generator.LoadNull();
         generator.CompareEqual();
     }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        public override void GenerateCode(Emit.MethodBodyGenerator generator)
        {
            // Generate code for the start of the statement.
            var statementLocals = new StatementLocals();

            GenerateStartOfStatement(generator, statementLocals);
            var condition = Condition.Accept(generator);

            // Generate code for condition convert to System.Boolean
            condition.GenerateCode(generator, Emit.MethodCompileOption.Return);
            if (condition.Type != typeof(bool))
            {
                if (condition.Type == TypeProvider.BooleanType)
                {
                    generator.CallStatic(Utils.ReflectionHelpers.BoooleanToBool);
                }
                else if (condition.Type.TryImplicitConvert(TypeProvider.BooleanType, out MethodInfo op_Implicit))
                {
                    generator.CallStatic(op_Implicit);
                    generator.CallStatic(Utils.ReflectionHelpers.BoooleanToBool);
                }
                else
                {
                    throw new System.InvalidCastException($"Unable to cast object of type {condition.Type} to {TypeProvider.BooleanType}");
                }
            }
            // We will need a label at the end of the if statement.
            var endOfEverything = generator.CreateLabel();

            if (Else == null)
            {
                //jump to end of if clause
                generator.BranchIfFalse(endOfEverything);
                //generate code for then clause
                Then.GenerateCode(generator);
            }
            else
            {
                //branch to else clause if false
                var startOfElseClause = generator.CreateLabel();
                generator.BranchIfFalse(startOfElseClause);

                //generate code for then clause
                Then.GenerateCode(generator);

                //brach to end of everything
                generator.Branch(endOfEverything);

                //generate code of else clause
                generator.DefineLabelPosition(startOfElseClause);
                Else.GenerateCode(generator);
            }

            //define label at end of statement
            generator.DefineLabelPosition(endOfEverything);
            //generate code for end of statement
            GenerateEndOfStatement(generator, statementLocals);
        }
Ejemplo n.º 5
0
 public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption options)
 {
     Target.GenerateCode(generator);
     if (Method != null)
     {
         // implicit conversion
         generator.CallStatic(Method);
     }
     else
     {
         generator.CastClass(Type);
     }
 }
Ejemplo n.º 6
0
 public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption options)
 {
     if (generator.Method is Generators.DynamicMethodGenerator)
     {
         // for annonymous type
         // if this is used in anonymous function or objects
         var variable = generator.GetLocalVariable("__value");
         generator.LoadVariable(variable);
     }
     else
     {
         generator.LoadArgument(0);
     }
 }
Ejemplo n.º 7
0
 public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption option)
 {
     generator.NewObject(ReflectionHelpers.AnonymousObj);
     for (int i = 0; i < Members.Count; i++)
     {
         var member = Members[i];
         generator.Duplicate();
         generator.LoadString(member.Name);
         member.Expression.GenerateCode(generator, AssignOption);
         if (member.Expression.Type.IsValueType)
         {
             generator.Box(member.Expression.Type);
         }
         generator.CallVirtual(ReflectionHelpers.AnonymousObj_SetItem);
     }
     generator.NewObject(ReflectionHelpers.Any_New);
 }
        public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption option)
        {
            Left.GenerateCode(generator);
            generator.Duplicate();
            var end = generator.CreateLabel();

            if (Left.Type.IsValueType)
            {
                if (Left.Type.TryImplicitConvert(TypeProvider.BooleanType, out System.Reflection.MethodInfo op_Implicit))
                {
                    generator.CallStatic(op_Implicit);
                    generator.CallStatic(Utils.ReflectionHelpers.BoooleanToBool);
                }
                else
                {
                    throw new System.InvalidCastException($"Unable to cast object of type {Left.Type} to {TypeProvider.BooleanType}");
                }
            }
            generator.BranchIfTrue(end);
            generator.Pop();
            Right.GenerateCode(generator, Emit.MethodCompileOption.Dupplicate);
            generator.DefineLabelPosition(end);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates IL code
 /// </summary>
 /// <param name="generator">The generator to output the CIL to.</param>
 public virtual void GenerateCode(Emit.MethodBodyGenerator generator)
 {
     generator.NoOperation();
 }
Ejemplo n.º 10
0
 public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption option)
 {
     CustomGeneration(this, generator);
 }
Ejemplo n.º 11
0
 public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption option = 0)
 {
     generator.EmitArguments(Arguments, Conversions);
     generator.NewObject(Constructor);
 }
        /// <inheritdoc/>
        public override void GenerateCode(Emit.MethodBodyGenerator generator, Emit.MethodCompileOption options)
        {
            //todo unsigned to signed
            switch (Value)
            {
            case sbyte b:
                generator.LoadByte(b);
                break;

            case byte b:
                generator.LoadByte(b);
                break;

            case short s:
                generator.LoadInt16(s);
                break;

            case ushort s:
                generator.LoadInt16(s);
                break;

            case int i:
                generator.LoadInt32(i);
                break;

            case uint i:
                generator.LoadInt32(i);
                break;

            case long l:
                generator.LoadInt64(l);
                break;

            case ulong l:
                generator.LoadInt64(l);
                break;

            case float f:
                generator.LoadSingle(f);
                break;

            case double d:
                generator.LoadDouble(d);
                break;

            case bool b:
                generator.LoadBool(b);
                break;

            case char c:
                generator.LoadChar(c);
                break;

            case string s:
                generator.LoadString(s);
                break;

            case null:
                generator.LoadNull();
                break;
            }
        }
Ejemplo n.º 13
0
 public override void GenerateCode(Emit.MethodBodyGenerator generator)
 {
     Expression.Accept(generator).GenerateCode(generator);
 }