Esempio n. 1
0
        public void GenerateName(bool isStatic)
        {
            MethodAttributes attr = getMethodAttributes(isStatic);

            if (FnName.IsConstructor(this.ClassName) != -1)
            {
                List <Type>        argTypes           = FnName.ProcDesc.GetArgTypes(1);
                var                ClassSymbol        = MethodContext.ClassContext.ClassSymbol;
                ConstructorBuilder constructorBuilder = ClassSymbol.ClassBuilder.DefineConstructor(attr, CallingConventions.Standard, argTypes.ToArray());
                MethodContext.EmitContext.SetBuilder(constructorBuilder);
                MethodContext.EmitContext.ClassContext = this.ClassContext.EmitContext;
                if (FnName.IsMinConstructor())
                {
                    MethodContext.ClassContext.ZeroConstructor = constructorBuilder;
                }
                else
                {
                    List <TKTProcArg> normalArgs = this.MethodContext.ProcDesc.GetSpecialArgs(1);
                    int start_i = isStatic ? 0 : 1;
                    for (var i = 0; i < normalArgs.Count; i++)
                    {
                        constructorBuilder.DefineParameter(i + start_i, ParameterAttributes.None, normalArgs[i].ArgName);
                    }
                }
                MethodContext.ClassContext.ConstructorBuilderList.Add(constructorBuilder);
            }
            else
            {
                List <Type> argTypes    = FnName.ProcDesc.GetArgTypes(1);
                var         ClassSymbol = MethodContext.ClassContext.ClassSymbol;
                MethodName = getMethodName(isStatic);
                MethodBuilder methodBuilder = ClassSymbol.ClassBuilder.DefineMethod(MethodName, attr, MethodContext.RetType, argTypes.ToArray());
                if (MethodName == "启动")
                {
                    Type                   myType           = typeof(STAThreadAttribute);
                    ConstructorInfo        infoConstructor  = myType.GetConstructor(new Type[] { });
                    CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { });
                    methodBuilder.SetCustomAttribute(attributeBuilder);
                }
                else
                {
                    setAttrMappingCode(methodBuilder);
                }
                MethodContext.EmitContext.SetBuilder(methodBuilder);
                MethodContext.EmitContext.ClassContext = this.ClassContext.EmitContext;
                List <TKTProcArg> genericArgs = this.MethodContext.ProcDesc.GetSpecialArgs(2);
                if (genericArgs.Count > 0)
                {
                    string[] names = genericArgs.Select(p => p.ArgName).ToArray();
                    methodBuilder.DefineGenericParameters(names);
                }

                List <TKTProcArg> normalArgs = this.MethodContext.ProcDesc.GetSpecialArgs(1);
                int start_i = isStatic ? 0 : 1;
                for (var i = 0; i < normalArgs.Count; i++)
                {
                    methodBuilder.DefineParameter(i + start_i, ParameterAttributes.None, normalArgs[i].ArgName);
                }
            }
        }
Esempio n. 2
0
        public void Generate(TypeBuilder classBuilder, bool isStatic)
        {
            var             symbols     = MethodContext.Symbols;
            EmitStmtContext stmtContext = new EmitStmtContext(MethodContext.EmitContext);
            var             il          = MethodContext.EmitContext.ILout;

            for (int i = 0; i < MethodContext.LoacalVarList.Count; i++)
            {
                string    ident     = MethodContext.LoacalVarList[i];
                SymbolVar varSymbol = symbols.Get(ident) as SymbolVar;
                varSymbol.VarBuilder = il.DeclareLocal(varSymbol.DimType);
                varSymbol.VarBuilder.SetLocalSymInfo(varSymbol.SymbolName);
            }
            if (MethodContext.RetType != typeof(void))
            {
                if (MethodContext.RetType.IsValueType)
                {
                    EmitHelper.LoadInt(il, 0);
                }
                else
                {
                    il.Emit(OpCodes.Ldnull);
                }
                EmitHelper.StormVar(il, (symbols.Get(MethodContext.LoacalVarList[0]) as SymbolVar).VarBuilder);
            }

            if (FnName.IsConstructor(this.ClassName) != -1)
            {
                if (!isStatic)
                {
                    callBaseDefault();
                    if (ClassContext.InitMemberValueMethod != null)
                    {
                        il.Emit(OpCodes.Ldarg_0);
                        EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod, true);//EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod);
                    }
                }
                else
                {
                    if (ClassContext.InitMemberValueMethod != null)
                    {
                        EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod, true);//EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod);
                    }
                }
            }
            //var MethodName = getMethodName(isStatic);
            //if(MethodName=="启动")
            //{
            //    //EmitHelper.CallDynamic(il, typeof(DebugHelper).GetMethod("PrintBaseDirectory"));
            //}
            FnBody.Generate(stmtContext);

            if (MethodContext.RetType != typeof(void))
            {
                il.Emit(OpCodes.Ldloc, retResult.VarBuilder);
                EmitHelper.EmitConv(il, MethodContext.RetType, retResult.DimType);
            }
            il.Emit(OpCodes.Ret);
        }
Esempio n. 3
0
 MethodAttributes getMethodAttributes(bool isStatic)
 {
     if (isStatic)
     {
         return(MethodAttributes.Public | MethodAttributes.Static);
     }
     else
     {
         if (FnName.IsConstructor(this.ClassName) != -1)
         {
             return(MethodAttributes.Public);
         }
         else
         {
             return(MethodAttributes.Public | MethodAttributes.Virtual);
         }
     }
 }