AddStaticField() public method

public AddStaticField ( Type fieldType, FieldAttributes attributes, string name ) : FieldBuilder
fieldType System.Type
attributes FieldAttributes
name string
return System.Reflection.Emit.FieldBuilder
        protected override Expression VisitConstant(ConstantExpression node)
        {
            object data = node.Value;
            Type   type = node.Type;

            // if the constant can be emitted into IL, nothing to do
            if (CanEmitConstant(data, type))
            {
                return(node);
            }

            type = TypeUtils.GetConstantType(type);

            int index;

            if (!_constantCache.TryGetValue(data, out index))
            {
                int          number = AddStaticData(data);
                FieldBuilder field  = TypeGen.AddStaticField(type, "#Constant" + number);
                index = _staticFields.Count;
                _staticFields.Add(field);
                _constantCache.Add(data, index);
            }

            return(Expression.Field(null, _staticFields[index]));
        }
        private ILGen MakeRawKeysMethod()
        {
            FieldBuilder rawKeysCache = TypeGen.AddStaticField(typeof(SymbolId[]), "ExtraKeysCache");
            ILGen        init         = TypeGen.TypeInitializer;

            init.EmitInt(_fields.Count);
            init.Emit(OpCodes.Newarr, typeof(SymbolId));

            int current = 0;

            foreach (GlobalVariableExpression variable in _fields.Keys)
            {
                init.Emit(OpCodes.Dup);
                init.EmitInt(current++);
                EmitSymbolId(init, SymbolTable.StringToId(variable.Name));
                init.EmitStoreElement(typeof(SymbolId));
            }

            init.Emit(OpCodes.Stsfld, rawKeysCache);

            MethodInfo baseMethod = typeof(CustomSymbolDictionary).GetMethod("GetExtraKeys", BindingFlags.Public | BindingFlags.Instance);
            ILGen      cg         = TypeGen.DefineExplicitInterfaceImplementation(baseMethod);

            cg.Emit(OpCodes.Ldsfld, rawKeysCache);
            cg.Emit(OpCodes.Ret);
            return(cg);
        }
Ejemplo n.º 3
0
        public StaticGlobalAllocator(LanguageContext/*!*/ context, string name) {
            _typeGen = Snippets.Shared.DefineType(name, typeof(object), false, false);

            _codeContextField = _typeGen.AddStaticField(typeof(CodeContext), "__global_context");
            _codeContext = CreateFieldBuilderExpression(_codeContextField);

            _scope = new Scope(new PythonDictionary(new GlobalDictionaryStorage(_globalVals)));
            _context = new CodeContext(_scope, context);
        }
Ejemplo n.º 4
0
        protected override Expression VisitExtension(Expression node)
        {
            if (node.NodeType == ExpressionType.Dynamic)
            {
                // the node was dynamic, the dynamic nodes were removed,
                // we now need to rewrite any call sites.
                return(VisitDynamic((DynamicExpression)node));
            }

            SymbolConstantExpression symbol = node as SymbolConstantExpression;

            if (symbol != null)
            {
                SymbolId id = symbol.Value;

                if (id == SymbolId.Empty)
                {
                    return(Expression.Field(null, typeof(SymbolId).GetField("Empty")));
                }

                FieldBuilderExpression value;
                if (!_indirectSymbolIds.TryGetValue(id, out value))
                {
                    // create field, emit fix-up...

                    if (_symbolGen == null)
                    {
                        _symbolGen = new TypeGen(_typeGen.AssemblyGen, ((ModuleBuilder)_typeGen.TypeBuilder.Module).DefineType("Symbols" + Interlocked.Increment(ref _id)));
#if SILVERLIGHT
                        _finishedSymbolType = new StrongBox <Type>();
#endif
                    }
                    FieldBuilder field = _symbolGen.AddStaticField(typeof(SymbolId), FieldAttributes.Public, SymbolTable.IdToString(id));
                    ILGen        init  = _symbolGen.TypeInitializer;
                    if (_indirectSymbolIds.Count == 0)
                    {
                        init.EmitType(_symbolGen.TypeBuilder);
                        init.EmitCall(typeof(ScriptingRuntimeHelpers), "InitializeSymbols");
                    }
#if SILVERLIGHT
                    _indirectSymbolIds[id] = value = new FieldBuilderExpression(field, _finishedSymbolType);
#else
                    _indirectSymbolIds[id] = value = new FieldBuilderExpression(field);
#endif
                }
                Debug.Assert(value != null);
                return(value);
            }
            return(Visit(node.Reduce()));
        }
Ejemplo n.º 5
0
        private Expression RewriteCallSite(CallSite site, TypeGen tg)
        {
            IExpressionSerializable serializer = site.Binder as IExpressionSerializable;
            if (serializer == null)
            {
                throw new ArgumentException("Generating code from non-serializable CallSiteBinder.");
            }

            Type siteType = site.GetType();

            FieldBuilder fb = tg.AddStaticField(siteType, "sf" + (_id++).ToString());
            Expression init = Expression.Call(siteType.GetMethod("Create"), serializer.CreateExpression());

            _fieldBuilders.Add(fb);
            _fieldInits.Add(init);

            Type t = init.Type;
            if (t.IsGenericType)
            {
                Type[] args = t.GetGenericArguments()[0].GetGenericArguments(); ;
                // skip the first one, it is the site.
                for (int k = 1; k < args.Length; k++)
                {
                    Type p = args[k];
                    //if (!p.Assembly.GetName().Name.Equals("mscorlib") && !p.Assembly.GetName().Name.Equals("Clojure"))
                    //    Console.WriteLine("Found {0}", p.ToString());
                }
            }

            // rewrite the node...
            return Expression.Field(null, fb);
        }