Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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));
        }