Exemplo n.º 1
0
 /// <summary>
 /// Evaluates the values of  argumants.
 /// </summary>
 private void GenerateArgs()
 {
     arg0.GenerateCode();
     ILTypeTranslator.GenerateImplicitCast(IL, arg0.Type, Type);
     arg1.GenerateCode();
     ILTypeTranslator.GenerateImplicitCast(IL, arg1.Type, Type);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:ILFunctionRef"/> class.
 /// This is not user defined function, it's .NET framework static method
 /// </summary>
 /// <param name="generator">The IL generator.</param>
 /// <param name="self">The new MethodInfo object of .NET Framework method</param>
 private ILFunctionRef(ILCodeGenerator generator, MethodInfo self)
 {
     system         = true;
     this.self      = self;
     this.generator = generator;
     type           = ILTypeTranslator.Translate(self.ReturnType);
 }
Exemplo n.º 3
0
        //

        /// <summary>
        /// Generates the code for function calling, before call this function must be called SetCallNode()
        /// </summary>
        public override void GenerateCode()
        {
            if (call == null)
            {
                throw new ArgumentException("You should before call SetCallNode() method");
            }

            int index = 0;

            foreach (Expression arg in call)
            {
                ILCodeObject argObject = generator.ExprEvaluator.Create(arg);
                argObject.GenerateCode();
                if (!system)
                {
                    try
                    {
                        ILTypeTranslator.GenerateImplicitCast(IL, argObject.Type, new TypeEntity(node.Args[index].Type));
                    }
                    catch (AnalizeException)
                    {
                        string message = string.Format("Function {0}, missing parameters type. Has \"{1}\" instead of \"{2}\"", node.Name, argObject.Type, new TypeEntity(node.Args[index].Type));
                        throw new AnalizeException(message, call);
                    }
                }
            }
            IL.Emit(OpCodes.Call, self);
            if (type == TypeEntity.Object && self.ReturnType.IsValueType) //post boxing
            {
                IL.Emit(OpCodes.Box, self.ReturnType);
            }
            call = null;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Generates function for some function node
        /// </summary>
        /// <param name="funcNode">The function node.</param>
        private void GenerateMethod(FunctionDefinition funcNode)
        {
            Type        returnType  = ILTypeTranslator.Translate(funcNode.ReturnType);
            List <Type> paramsTypes = new List <Type>();

            foreach (ArgumentDefinition arg in funcNode.Args)
            {
                paramsTypes.Add(ILTypeTranslator.Translate(arg.Type));
            }
            MethodBuilder currentMethod = mainClass.DefineMethod(funcNode.Name, MethodAttributes.Static | MethodAttributes.Public, returnType, paramsTypes.ToArray());

            currentFunction = new ILFunctionRef(this, funcNode, currentMethod);

            il = currentMethod.GetILGenerator();
            BlockEnter();

            int index = 1;

            foreach (ArgumentDefinition arg in funcNode.Args)
            {
                new ILArgument(new TypeEntity(arg.Type), arg.Name, this, currentMethod, index++);
            }
            statementEvaluator.GenerateCode(funcNode.Body);
            il.Emit(OpCodes.Ret);
            BlockLeave();
            entryPoint = currentMethod;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:ILGlobal"/> class.
        /// </summary>
        /// <param name="type">The type of the variable.</param>
        /// <param name="name">The name of the variable.</param>
        /// <param name="generator">The IL generator.</param>
        internal ILGlobal(TypeEntity type, string name, ILCodeGenerator generator)
            : base(type, generator)
        {
            this.type = type;
            Type clrType = ILTypeTranslator.Translate(type);

            self = generator.MainClass.DefineField(name, clrType, FieldAttributes.Public | FieldAttributes.Static);
            generator.GlobalContext.DefineObject(name, this);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:ILLocal"/> class.
        /// </summary>
        /// <param name="type">The type of the variable.</param>
        /// <param name="name">The name of the variable.</param>
        /// <param name="generator">The IL generator.</param>
        internal ILLocal(TypeEntity type, string name, ILCodeGenerator generator)
            : base(type, generator)
        {
            this.type = type;
            Type clrType = ILTypeTranslator.Translate(type);

            self = IL.DeclareLocal(clrType);
            generator.CurrentContext.DefineObject(name, this);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:ILArgument"/> class.
        /// </summary>
        /// <param name="type">The type of the argument.</param>
        /// <param name="name">The name of the argument.</param>
        /// <param name="generator">The IL generator.</param>
        /// <param name="method">The parent method.</param>
        /// <param name="index">The index of this argument in arguments array of parent method.</param>
        internal ILArgument(TypeEntity type, string name, ILCodeGenerator generator, MethodBuilder method, int index)
            : base(type, generator)
        {
            this.type   = type;
            this.method = method;
            Type clrType = ILTypeTranslator.Translate(type);

            self = method.DefineParameter(index, ParameterAttributes.None, name);
            generator.CurrentContext.DefineObject(name, this);
        }
Exemplo n.º 8
0
 public override void GenerateCode()
 {
     arg.GenerateCode();
     try
     {
         ILTypeTranslator.GenerateExplicitCast(IL, arg.Type, type);
     }
     catch (AnalizeException e)
     {
         throw new AnalizeException(e.Message, node);
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// Looks for .NET method
        /// </summary>
        /// <param name="generator">The IL generator.</param>
        /// <param name="callNode">The node which called the .NET static method</param>
        /// <returns>ILFunctionRef for called .NET static method.
        /// <c>null</c> - if there is no static method with this name exist in .NET framework</returns>
        public static ILFunctionRef LookForDotNetMethod(ILCodeGenerator generator, FunctionCall callNode)
        {
            string name  = callNode.Name;
            int    index = name.LastIndexOf('.');

            if (index == -1)
            {
                return(null);
            }
            string typeName   = name.Substring(0, index);
            string methodName = name.Substring(index + 1);

            System.Type type = System.Type.GetType(typeName);
            if (type == null)
            {
                type = System.Type.GetType("System." + typeName);
            }
            if (type == null)
            {
                return(null);
            }


            List <Type>         argTypes = new List <Type>();
            ExpressionGenerator g        = new ExpressionGenerator(generator);

            foreach (Expression arg in callNode)
            {
                argTypes.Add(ILTypeTranslator.Translate(g.Create(arg).Type));
            }

            MethodInfo method = type.GetMethod(methodName, argTypes.ToArray());

            if (method == null || !method.IsStatic)
            {
                return(null);
            }

            return(new ILFunctionRef(generator, method));
        }
Exemplo n.º 10
0
 /// <summary>
 /// Generates the IL code for <c>return</c> construction
 /// </summary>
 /// <param name="This">The [return] node.</param>
 void IStatementVisitor.Visit(ReturnStatement This)
 {
     if (This.Result == null)
     {
         if (generator.CurrentFunction.Type != TypeEntity.Void)
         {
             throw new AnalizeException("This function must return a value", This);
         }
     }
     else
     {
         try
         {
             ILCodeObject result = generator.ExprEvaluator.Create(This.Result);
             result.GenerateCode();
             ILTypeTranslator.GenerateImplicitCast(IL, result.Type, generator.CurrentFunction.Type);
         }
         catch (AnalizeException e)
         {
             throw new AnalizeException(e.Message, This);
         }
     }
     IL.Emit(OpCodes.Ret);
 }
Exemplo n.º 11
0
 public override void GenerateStore(CodeObject value)
 {
     value.GenerateCode();
     ILTypeTranslator.GenerateImplicitCast(IL, value.Type, Type);
     GenerateStore();
 }