public static bool NeedCallThis(SymbolBase symbol)
 {
     if (symbol is SymbolLocalVar)
     {
         return(false);
     }
     else if (symbol is SymbolArg)
     {
         return(false);
     }
     else if (symbol is SymbolDefProperty)
     {
         SymbolDefProperty symbol2 = (symbol as SymbolDefProperty);
         return(symbol2.IsStatic == false);
     }
     else if (symbol is SymbolDefField)
     {
         SymbolDefField symbol2 = (symbol as SymbolDefField);
         return(symbol2.IsStatic == false);
     }
     else if (symbol is SymbolRefStaticMember)
     {
         return(false);
     }
     else
     {
         throw new CompileException();
     }
 }
 public static void EmitStorm(ILGenerator il, SymbolBase symbol)
 {
     if (symbol is SymbolLocalVar)
     {
         var symbolVar = symbol as SymbolLocalVar;
         EmitHelper.StormVar(il, symbolVar.VarBuilder);
     }
     else if (symbol is SymbolArg)
     {
         SymbolArg argsymbol = (symbol as SymbolArg);
         EmitHelper.StormArg(il, argsymbol.ArgIndex);
     }
     else if (symbol is SymbolDefProperty)
     {
         SymbolDefProperty symbol2   = (symbol as SymbolDefProperty);
         MethodInfo        setMethod = symbol2.Property.GetSetMethod();
         EmitHelper.CallDynamic(il, setMethod);
     }
     else if (symbol is SymbolDefField)
     {
         SymbolDefField symbol2 = (symbol as SymbolDefField);
         EmitHelper.StormField(il, symbol2.Field);
     }
     else if (symbol is SymbolRefStaticMember)
     {
         EmitStorm(il, symbol as SymbolRefStaticMember);
     }
     else
     {
         throw new CompileException();
     }
 }
Exemple #3
0
        private void AnalyFields( )
        {
            var symbols = NestedClassContext.Symbols;

            FieldSymbols = new List <SymbolDefField>();
            if (ExpContext.ClassContext.IsStaticClass == false)
            {
                TypeBuilder    builder     = ExpContext.ClassContext.EmitContext.ClassBuilder;
                FieldBuilder   field       = ClassBuilder.DefineField(OutClassFieldName, builder, FieldAttributes.Public);
                ZType          ztype       = new ZClassType(builder, builder, false);
                SymbolDefField fieldSymbol = new SymbolDefField(OutClassFieldName, ztype, false);
                fieldSymbol.Field = field;
                FieldSymbols.Add(fieldSymbol);
                symbols.Add(fieldSymbol);
                this.NestedClassContext.NestedOutFieldSymbol = fieldSymbol;
            }

            foreach (SymbolBase symbol in BodyVars)
            {
                ZType          ztype       = symbol.SymbolZType;
                FieldBuilder   field       = ClassBuilder.DefineField(symbol.SymbolName, ztype.SharpType, FieldAttributes.Public);
                SymbolDefField fieldSymbol = new SymbolDefField(symbol.SymbolName, ztype, false);
                fieldSymbol.Field = field;
                FieldSymbols.Add(fieldSymbol);
                symbols.Add(fieldSymbol);
            }

            foreach (ExpVar expVar in FieldExpVars)
            {
                SymbolDefField symbol = symbols.Get(expVar.VarName) as SymbolDefField;
                expVar.SetAsLambdaFiled(symbol);
            }
        }
        void analyFields(bool isStatic)
        {
            Fields = new Dictionary <string, SymbolDefField>();
            var symbols = NestedClassContext.Symbols;
            var names   = FieldExps.Keys.ToList();

            for (int i = 0; i < names.Count; i++)
            {
                var       name   = names[i];
                VarExp    exp    = FieldExps[name];
                SymbolVar symbol = exp.VarSymbol as SymbolVar;

                //if (symbol is InstanceSymbol)
                //{
                var            propertyType = symbol.DimType;
                FieldBuilder   field        = NestedClassContext.EmitContext.CurrentTypeBuilder.DefineField("_" + name, propertyType, FieldAttributes.Public);
                SymbolDefField fieldSymbol  = new SymbolDefField(name, propertyType, isStatic);
                fieldSymbol.SetField(field);
                symbols.AddSafe(fieldSymbol);

                //Fields.Remove(name);
                Fields.Add(name, fieldSymbol);
                exp.IsNestedField = true;
                SymbolDefField fs = new SymbolDefField(name, field.FieldType, field.IsStatic);
                fs.SetField(field);
                exp.VarSymbol = fs;
                //nestedSymbolInfos.Add(fieldSymbol);
                //}
            }
        }
Exemple #5
0
        //public static void CallDynamic(ILGenerator il, MethodInfo method)
        //{
        //il.Emit(OpCodes.Callvirt, method);

        /*if (method.IsAbstract || method.IsVirtual)
         * {
         *  il.Emit(OpCodes.Callvirt, method);
         * }
         * else
         * {
         *  il.Emit(OpCodes.Call, method);
         * }*/
        /* Console.WriteLine(string.Format("{0} IsStatic:{1}", method.Name, method.IsStatic));
         * if (method.IsStatic)
         * {
         *   il.Emit(OpCodes.Call, method);
         * }
         * else
         * {
         *   il.Emit(OpCodes.Callvirt, method);
         * }
         * }*/
        /*
         * public static void CallDynamic(ILGenerator il, MethodInfo method,Type objType)
         * {
         *  if (objType == method.DeclaringType)
         *      il.Emit(OpCodes.Call, method);
         *  else
         *      il.Emit(OpCodes.Callvirt, method);
         * }*/

        public static bool EmitSymbolGet(ILGenerator il, SymbolInfo symbol)
        {
            if (symbol is SymbolVar)
            {
                var symbolVar = symbol as SymbolVar;
                EmitHelper.LoadVar(il, symbolVar.VarBuilder);
            }
            else if (symbol is SymbolArg)
            {
                SymbolArg argsymbol = (symbol as SymbolArg);
                il.Emit(OpCodes.Ldarg, argsymbol.ArgIndex);
            }
            else if (symbol is SymbolEnumItem)
            {
                SymbolEnumItem eisymbol = (symbol as SymbolEnumItem);
                EmitHelper.LoadInt(il, (int)eisymbol.EnumValue);
            }
            else if (symbol is SymbolPropertyDirect)
            {
                SymbolPropertyDirect prsymbol  = symbol as SymbolPropertyDirect;
                MethodInfo           getMethod = prsymbol.ExProperty.Property.GetGetMethod();
                EmitHelper.CallDynamic(il, getMethod, prsymbol.ExProperty.IsSelf);
            }
            else if (symbol is SymbolFieldDirect)
            {
                SymbolFieldDirect fymbol = symbol as SymbolFieldDirect;
                EmitHelper.LoadField(il, fymbol.ExField.Field);
            }
            else if (symbol is SymbolDefProperty)
            {
                SymbolDefProperty prsymbol = (symbol as SymbolDefProperty);
                if (!prsymbol.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
                MethodInfo getMethod = prsymbol.GetProperty().GetGetMethod();
                EmitHelper.CallDynamic(il, getMethod, true);
            }
            else if (symbol is SymbolDefField)
            {
                SymbolDefField prsymbol = (symbol as SymbolDefField);
                if (!prsymbol.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
                //il.Emit(OpCodes.Ldfld, prsymbol.Builder);
                EmitHelper.LoadField(il, prsymbol.GetField());
            }
            else if (symbol is SymbolDefClass)
            {
                return(true);
            }
            else
            {
                return(false);
            }
            return(true);
        }
Exemple #6
0
        public override void Emit()
        {
            lambdaExp.Emit();
            LocalBuilder lanmbdaLocalBuilder = IL.DeclareLocal(lambdaExp.NestedClassContext.EmitContext.ClassBuilder);

            IL.Emit(OpCodes.Newobj, lambdaExp.NewBuilder);
            EmitHelper.StormVar(IL, lanmbdaLocalBuilder);
            int i = 0;

            if (this.ExpContext.ClassContext.IsStaticClass == false)
            {
                SymbolDefField fieldSymbol = lambdaExp.FieldSymbols[0];
                EmitHelper.LoadVar(IL, lanmbdaLocalBuilder);
                EmitHelper.EmitThis(IL, false);
                IL.Emit(OpCodes.Stfld, fieldSymbol.Field);
                i++;
            }
            for (; i < this.BodySymbolVars.Count; i++)
            {
                SymbolBase     thisSymbol  = this.BodySymbolVars[i];
                SymbolDefField fieldSymbol = lambdaExp.FieldSymbols[i];

                EmitHelper.LoadVar(IL, lanmbdaLocalBuilder);
                //SymbolInfo inMethodSymbol = symbols.Get(field);
                //EmitHelper.EmitSymbolGet(IL, thisSymbol);
                if (EmitSymbolHelper.NeedCallThis(thisSymbol))
                {
                    EmitHelper.Emit_LoadThis(IL);
                }
                EmitSymbolHelper.EmitLoad(IL, thisSymbol);
                IL.Emit(OpCodes.Stfld, fieldSymbol.Field);
            }

            EmitHelper.LoadVar(IL, lanmbdaLocalBuilder);
            IL.Emit(OpCodes.Ldftn, lambdaExp.ProcBuilder);//.EmitContext.CurrentMethodBuilder);
            ConstructorInfo[] constructorInfos = lambdaExp.FnRetType.SharpType.GetConstructors();
            IL.Emit(OpCodes.Newobj, constructorInfos[0]);
            base.EmitConv();
        }
Exemple #7
0
 public static bool EmitSet_Storm(ILGenerator il, SymbolInfo symbol)
 {
     if (symbol is SymbolVar)
     {
         EmitHelper.StormVar(il, (symbol as SymbolVar).VarBuilder);
     }
     else if (symbol is SymbolArg)
     {
         EmitHelper.StormArg(il, (symbol as SymbolArg).ArgIndex);
     }
     else if (symbol is SymbolPropertyDirect)
     {
         SymbolPropertyDirect prsymbol  = symbol as SymbolPropertyDirect;
         MethodInfo           setMethod = prsymbol.ExProperty.Property.GetSetMethod();
         EmitHelper.CallDynamic(il, setMethod, prsymbol.ExProperty.IsSelf);
     }
     else if (symbol is SymbolDefProperty)
     {
         SymbolDefProperty prsymbol  = (symbol as SymbolDefProperty);
         MethodInfo        setMethod = prsymbol.GetProperty().GetSetMethod();
         EmitHelper.CallDynamic(il, setMethod, true);
     }
     else if (symbol is SymbolDefField)
     {
         SymbolDefField prsymbol = (symbol as SymbolDefField);
         EmitHelper.StormField(il, prsymbol.GetField());
     }
     else if (symbol is SymbolFieldDirect)
     {
         SymbolFieldDirect fymbol = symbol as SymbolFieldDirect;
         EmitHelper.StormField(il, fymbol.ExField.Field);
     }
     else
     {
         return(false);
     }
     return(true);
 }
Exemple #8
0
 public static bool EmitSet_Load(ILGenerator il, SymbolInfo symbol)
 {
     if (symbol is SymbolDefProperty)
     {
         SymbolDefProperty prsymbol = (symbol as SymbolDefProperty);
         if (!prsymbol.IsStatic)
         {
             il.Emit(OpCodes.Ldarg_0);
         }
     }
     else if (symbol is SymbolDefField)
     {
         SymbolDefField prsymbol = (symbol as SymbolDefField);
         if (!prsymbol.IsStatic)
         {
             il.Emit(OpCodes.Ldarg_0);
         }
     }
     else
     {
         return(false);
     }
     return(true);
 }
Exemple #9
0
 public void SetAsLambdaFiled(SymbolDefField fieldSymbol)
 {
     NestedFieldSymbol = fieldSymbol;
 }
Exemple #10
0
        public override Exp Analy(AnalyExpContext context)
        {
            base.Analy(context);
            var symbols = this.AnalyExpContext.Symbols;

            VarName = VarToken.GetText();
            if (!IsNestedField)
            {
                if (VarSymbol == null)
                {
                    VarSymbol = symbols.Get(VarName);
                }

                if (VarSymbol == null)
                {
                    //VarSymbol = symbols.Get(VarName);
                    List <SymbolEnumItem> enumValues = context.ClassContext.SearchEnumItem(VarName);
                    if (enumValues.Count == 1)
                    {
                        VarSymbol = enumValues[0];
                    }
                    if (enumValues.Count > 1)
                    {
                        errorf("'{0}'有多个相同约定值", VarName);
                        return(null);
                    }
                }

                if (VarSymbol == null)
                {
                    if (context.ClassContext.ClassSymbol.BaseGcl != null)
                    {
                        ExPropertyInfo property = context.ClassContext.ClassSymbol.BaseGcl.SearchExProperty(VarName);
                        if (property != null)
                        {
                            if (ReflectionUtil.IsPublic(property.Property) ||
                                ReflectionUtil.IsProtected(property.Property))
                            {
                                SymbolDefProperty ps = new SymbolDefProperty(VarName, property.Property.PropertyType,
                                                                             ReflectionUtil.IsStatic(property.Property));
                                ps.SetProperty(property.Property);
                                VarSymbol = ps;
                            }
                        }
                    }
                }
                if (VarSymbol == null)
                {
                    if (context.ClassContext.ClassSymbol.BaseGcl != null)
                    {
                        ExFieldInfo field = context.ClassContext.ClassSymbol.BaseGcl.SearchExField(VarName);
                        if (field != null)
                        {
                            if (field.Field.IsPublic || field.Field.IsFamily)
                            {
                                SymbolDefField fs = new SymbolDefField(VarName, field.Field.FieldType,
                                                                       field.Field.IsStatic);
                                fs.SetField(field.Field);
                                VarSymbol = fs;
                            }
                        }
                    }
                }
                if (VarSymbol == null)
                {
                    if (IsAssignedBy)
                    {
                        SymbolVar varSymbol = new SymbolVar(VarName);
                        if (!varSymbol.IsInBlock)
                        {
                            varSymbol.LoacalVarIndex = context.StmtContext.MethodContext.CreateLocalVarIndex(VarName);
                            symbols.Add(varSymbol);
                        }
                        VarSymbol = varSymbol;
                    }
                    else
                    {
                        errorf("'{0}'没有赋值", VarName);
                        return(null);
                    }
                }
                RetType = ((InstanceSymbol)VarSymbol).DimType;
            }
            else
            {
            }
            return(this);
        }