Ejemplo n.º 1
0
        private void CreateEnumerator()
        {
            _enumerator = CodeBuilder.CreateClass("$Enumerator");
            _enumerator.AddAttribute(CodeBuilder.CreateAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)));
            _enumerator.Modifiers  |= _enumerable.Modifiers;
            _enumerator.LexicalInfo = this.LexicalInfo;
            foreach (var param in _genericParams)
            {
                var replacement = _enumerator.AddGenericParameter(param.Name);
                _methodToEnumeratorMapper.Replace((IType)param.Entity, (IType)replacement.Entity);
            }
            var abstractEnumeratorType =
                TypeSystemServices.Map(typeof(GenericGeneratorEnumerator <>)).
                GenericInfo.ConstructType(_methodToEnumeratorMapper.MapType(_generatorItemType));

            _state        = NameResolutionService.ResolveField(abstractEnumeratorType, "_state");
            _yield        = NameResolutionService.ResolveMethod(abstractEnumeratorType, "Yield");
            _yieldDefault = NameResolutionService.ResolveMethod(abstractEnumeratorType, "YieldDefault");
            _enumerator.AddBaseType(abstractEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.IEnumeratorType);

            CreateEnumeratorConstructor();
            CreateMoveNext();

            _enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
        }
Ejemplo n.º 2
0
        void CreateAnonymousGeneratorType()
        {
            _enumerable = (BooClassBuilder)_generator["GeneratorClassBuilder"];

            _enumerator = _collector.CreateSkeletonClass("Enumerator");
            _enumerator.AddBaseType(TypeSystemServices.IEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.Map(typeof(ICloneable)));

            _enumeratorField = _enumerator.AddField("____enumerator",
                                                    TypeSystemServices.IEnumeratorType);
            _current = _enumerator.AddField("____current",
                                            TypeSystemServices.ObjectType);

            CreateReset();
            CreateCurrent();
            CreateMoveNext();
            CreateClone();
            EnumeratorConstructorMustCallReset();

            _collector.AdjustReferences();

            _collector.DeclareFieldsAndConstructor(_enumerable);

            CreateGetEnumerator();
            _enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
        }
Ejemplo n.º 3
0
        void CreateAnonymousGeneratorType()
        {
            _enumerable = (BooClassBuilder)_generator["GeneratorClassBuilder"];

            _sourceItemType       = TypeSystemServices.ObjectType;
            _sourceEnumeratorType = TypeSystemServices.IEnumeratorType;
            _sourceEnumerableType = TypeSystemServices.IEnumerableType;

            _resultItemType       = (IType)_generator["GeneratorItemType"];
            _resultEnumeratorType = TypeSystemServices.IEnumeratorType;

            //_enumerator = _collector.CreateSkeletonClass(_enumerable.ClassDefinition.Name + "Enumerator");
            _enumerator = _collector.CreateSkeletonClass("Enumerator");

#if NET_2_0
            // If source item type isn't object, use a generic enumerator for the source type
            _sourceItemType = TypeSystemServices.GetGenericEnumerableItemType(_generator.Iterator.ExpressionType);

            if (_sourceItemType != null && _sourceItemType != TypeSystemServices.ObjectType)
            {
                _sourceEnumerableType = TypeSystemServices.IEnumerableGenericType.GenericTypeDefinitionInfo.MakeGenericType(_sourceItemType);
                _sourceEnumeratorType = TypeSystemServices.IEnumeratorGenericType.GenericTypeDefinitionInfo.MakeGenericType(_sourceItemType);
            }
            else
            {
                _sourceItemType = TypeSystemServices.ObjectType;
            }

            // Expose a generic enumerator
            _resultEnumeratorType = TypeSystemServices.IEnumeratorGenericType.GenericTypeDefinitionInfo.MakeGenericType(_resultItemType);
#endif

            _enumerator.AddBaseType(_resultEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.Map(typeof(ICloneable)));
            _enumerator.AddBaseType(TypeSystemServices.Map(typeof(IDisposable)));

            _enumeratorField = _enumerator.AddField("____enumerator", _sourceEnumeratorType);
            _current         = _enumerator.AddField("____current", _resultItemType);

            CreateReset();
            CreateCurrent();
            CreateMoveNext();
            CreateClone();
            CreateDispose();
            EnumeratorConstructorMustCallReset();

            _collector.AdjustReferences();

            _collector.DeclareFieldsAndConstructor(_enumerable);

            CreateGetEnumerator();
            _enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
            //TypeSystemServices.AddCompilerGeneratedType(_enumerator.ClassDefinition);
        }
Ejemplo n.º 4
0
        void CreateAnonymousGeneratorType()
        {
            _enumerable = (BooClassBuilder)_generator["GeneratorClassBuilder"];

            // Set up some important types
            _sourceItemType       = TypeSystemServices.ObjectType;
            _sourceEnumeratorType = TypeSystemServices.IEnumeratorType;
            _sourceEnumerableType = TypeSystemServices.IEnumerableType;

            _resultItemType       = (IType)_generator["GeneratorItemType"];
            _resultEnumeratorType = TypeSystemServices.IEnumeratorGenericType.GenericInfo.ConstructType(_resultItemType);

            _enumerator = _collector.CreateSkeletonClass("Enumerator");

            // use a generic enumerator for the source type if possible
            _sourceItemType = TypeSystemServices.GetGenericEnumerableItemType(_generator.Iterator.ExpressionType);
            if (_sourceItemType != null && _sourceItemType != TypeSystemServices.ObjectType)
            {
                _sourceEnumerableType = TypeSystemServices.IEnumerableGenericType.GenericInfo.ConstructType(_sourceItemType);
                _sourceEnumeratorType = TypeSystemServices.IEnumeratorGenericType.GenericInfo.ConstructType(_sourceItemType);
            }
            else
            {
                _sourceItemType = TypeSystemServices.ObjectType;
            }

            // Add base types
            _enumerator.AddBaseType(_resultEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.Map(typeof(ICloneable)));
            _enumerator.AddBaseType(TypeSystemServices.Map(typeof(IDisposable)));

            // Add fields
            _enumeratorField = _enumerator.AddField("$$enumerator", _sourceEnumeratorType);
            _current         = _enumerator.AddField("$$current", _resultItemType);

            // Add methods
            CreateReset();
            CreateCurrent();
            CreateMoveNext();
            CreateClone();
            CreateDispose();

            EnumeratorConstructorMustCallReset();

            _collector.AdjustReferences();

            _collector.DeclareFieldsAndConstructor(_enumerable);

            CreateGetEnumerator();
            _enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
        }
Ejemplo n.º 5
0
        private void CreateSharedLocalsClass()
        {
            _shared.Clear();

            CollectSharedLocalEntities(_currentMethod.Locals);
            CollectSharedLocalEntities(_currentMethod.Parameters);

            if (_shared.Count > 0)
            {
                BooClassBuilder builder = CodeBuilder.CreateClass(Context.GetUniqueName(_currentMethod.Name, "locals"));
                builder.Modifiers |= TypeMemberModifiers.Internal;
                builder.AddBaseType(TypeSystemServices.ObjectType);

                var genericsSet = new HashSet <string>();
                var replacer    = new GeneratorTypeReplacer();
                foreach (ILocalEntity local in _shared)
                {
                    CheckTypeForGenericParams(local.Type, genericsSet, builder, replacer);
                    Field field = builder.AddInternalField(
                        string.Format("${0}", local.Name),
                        replacer.MapType(local.Type));

                    _mappings[local] = (IField)field.Entity;
                }

                builder.AddConstructor().Body.Add(
                    CodeBuilder.CreateSuperConstructorInvocation(TypeSystemServices.ObjectType));

                _sharedLocalsClass = builder.ClassDefinition;
            }
        }
Ejemplo n.º 6
0
        void CreateSharedLocalsClass()
        {
            _shared.Clear();

            CollectSharedLocalEntities(_currentMethod.Locals);
            CollectSharedLocalEntities(_currentMethod.Parameters);

            if (_shared.Count > 0)
            {
                BooClassBuilder builder = CodeBuilder.CreateClass(Context.GetUniqueName(_currentMethod.Name, "locals"));
                builder.Modifiers |= TypeMemberModifiers.Internal;
                builder.AddBaseType(TypeSystemServices.ObjectType);

                var genericsSet = new System.Collections.Generic.HashSet <string>();
                foreach (ILocalEntity local in _shared)
                {
                    Field field = builder.AddInternalField(
                        string.Format("${0}", local.Name),
                        local.Type);
                    if (local.Type is IGenericParameter && !genericsSet.Contains(local.Type.Name))
                    {
                        builder.AddGenericParameter(local.Type.Name);
                        genericsSet.Add(local.Type.Name);
                    }

                    _mappings[local] = field.Entity;
                }

                builder.AddConstructor().Body.Add(
                    CodeBuilder.CreateSuperConstructorInvocation(TypeSystemServices.ObjectType));

                _sharedLocalsClass = builder.ClassDefinition;
            }
        }
Ejemplo n.º 7
0
        void CreateSharedLocalsClass()
        {
            _shared.Clear();

            CollectSharedLocalEntities(_currentMethod.Locals);
            CollectSharedLocalEntities(_currentMethod.Parameters);

            if (_shared.Count > 0)
            {
                BooClassBuilder builder = CodeBuilder.CreateClass(
                    string.Format("___locals{0}", _context.AllocIndex()));
                builder.AddBaseType(TypeSystemServices.ObjectType);

                int i = 0;
                foreach (ILocalEntity local in _shared)
                {
                    Field field = builder.AddField(
                        string.Format("___{0}_{1}", local.Name, i),
                        local.Type);
                    field.Modifiers = TypeMemberModifiers.Internal;
                    ++i;

                    _mappings[local] = field.Entity;
                }

                builder.AddConstructor().Body.Add(
                    CodeBuilder.CreateSuperConstructorInvocation(TypeSystemServices.ObjectType));

                _sharedLocalsClass = builder.ClassDefinition;
            }
        }
Ejemplo n.º 8
0
        void CreateEnumerator()
        {
            IType abstractEnumeratorType = TypeSystemServices.Map(typeof(Boo.Lang.AbstractGeneratorEnumerator));

            _state = NameResolutionService.ResolveField(abstractEnumeratorType, "_state");
            _yield = NameResolutionService.ResolveMethod(abstractEnumeratorType, "Yield");

            _enumerator = CodeBuilder.CreateClass("Enumerator");
            _enumerator.AddBaseType(abstractEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.IEnumeratorType);

            CreateEnumeratorConstructor();
            CreateMoveNext();

            _enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
        }
Ejemplo n.º 9
0
        public BooClassBuilder CreateSkeletonClass(string name)
        {
            BooClassBuilder builder = CodeBuilder.CreateClass(name);

            builder.AddBaseType(CodeBuilder.TypeSystemServices.ObjectType);
            DeclareFieldsAndConstructor(builder);
            return(builder);
        }
Ejemplo n.º 10
0
        void CreateEnumerator()
        {
            IType abstractEnumeratorType =
                TypeSystemServices.Map(typeof(Boo.Lang.GenericGeneratorEnumerator <>)).
                GenericInfo.ConstructType(new IType[] { _generatorItemType });

            _state = NameResolutionService.ResolveField(abstractEnumeratorType, "_state");
            _yield = NameResolutionService.ResolveMethod(abstractEnumeratorType, "Yield");

            _enumerator             = CodeBuilder.CreateClass("$");
            _enumerator.LexicalInfo = this.LexicalInfo;
            _enumerator.AddBaseType(abstractEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.IEnumeratorType);

            CreateEnumeratorConstructor();
            CreateMoveNext();

            _enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
        }
        public IType DefineEvaluationContext()
        {
            string[]        components = new string[] { "EvaluationContext" };
            BooClassBuilder builder    = this.CodeBuilder.CreateClass(this.Context.GetUniqueName(components), TypeMemberModifiers.Public);

            builder.AddBaseType(this.Map(typeof(EvaluationContext)));
            this.ChainConstructorsFromBaseType(builder);
            this.AddLocalVariablesAsFields(builder);
            this.CurrentTypeNode.Members.Add(builder.ClassDefinition);
            return(builder.Entity);
        }
Ejemplo n.º 12
0
        private BooClassBuilder SetUpEnumerableClassBuilder(Node sourceNode, Method enclosingMethod, IType generatorItemType)
        {
            BooClassBuilder builder = CodeBuilder.CreateClass(
                Context.GetUniqueName(enclosingMethod.Name),
                TypeMemberModifiers.Internal | TypeMemberModifiers.Final);

            builder.LexicalInfo = new LexicalInfo(sourceNode.LexicalInfo);
            builder.AddBaseType(
                TypeSystemServices.Map(typeof(GenericGenerator <>)).GenericInfo.ConstructType(generatorItemType));

            builder.AddAttribute(CodeBuilder.CreateAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)));
            return(builder);
        }
Ejemplo n.º 13
0
        void CreateEnumerator()
        {
            var abstractEnumeratorType =
                TypeSystemServices.Map(typeof(GenericGeneratorEnumerator <>)).
                GenericInfo.ConstructType(new IType[] { _generatorItemType });

            _state        = NameResolutionService.ResolveField(abstractEnumeratorType, "_state");
            _yield        = NameResolutionService.ResolveMethod(abstractEnumeratorType, "Yield");
            _yieldDefault = NameResolutionService.ResolveMethod(abstractEnumeratorType, "YieldDefault");

            _enumerator = CodeBuilder.CreateClass("$");
            _enumerator.AddAttribute(CodeBuilder.CreateAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)));
            _enumerator.Modifiers  |= _enumerable.Modifiers;
            _enumerator.LexicalInfo = this.LexicalInfo;
            _enumerator.AddBaseType(abstractEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.IEnumeratorType);

            CreateEnumeratorConstructor();
            CreateMoveNext();

            _enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
        }
Ejemplo n.º 14
0
        void CreateEnumerator()
        {
#if NET_2_0
            IType abstractEnumeratorType =
                TypeSystemServices.Map(typeof(Boo.Lang.GenericGeneratorEnumerator <>)).
                GenericTypeDefinitionInfo.MakeGenericType(new IType[] { _generatorItemType });
#else
            IType abstractEnumeratorType = TypeSystemServices.Map(typeof(Boo.Lang.AbstractGeneratorEnumerator));
#endif

            _state = NameResolutionService.ResolveField(abstractEnumeratorType, "_state");
            _yield = NameResolutionService.ResolveMethod(abstractEnumeratorType, "Yield");

            _enumerator             = CodeBuilder.CreateClass(_enumerable.ClassDefinition.Name + "_Enumerator");
            _enumerator.LexicalInfo = this.LexicalInfo;
            _enumerator.AddBaseType(abstractEnumeratorType);
            _enumerator.AddBaseType(TypeSystemServices.IEnumeratorType);

            CreateEnumeratorConstructor();
            CreateMoveNext();

            //_enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
            TypeSystemServices.AddCompilerGeneratedType(_enumerator.ClassDefinition);
        }
Ejemplo n.º 15
0
		void CreateEnumerator()
		{
			var abstractEnumeratorType =
				TypeSystemServices.Map(typeof(GenericGeneratorEnumerator<>)).
					GenericInfo.ConstructType(new IType[] {_generatorItemType});
			
			_state = NameResolutionService.ResolveField(abstractEnumeratorType, "_state");
			_yield = NameResolutionService.ResolveMethod(abstractEnumeratorType, "Yield");
			_yieldDefault = NameResolutionService.ResolveMethod(abstractEnumeratorType, "YieldDefault");
			
			_enumerator = CodeBuilder.CreateClass("$");
			_enumerator.AddAttribute(CodeBuilder.CreateAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)));
			_enumerator.Modifiers |= _enumerable.Modifiers;
			_enumerator.LexicalInfo = this.LexicalInfo;
			_enumerator.AddBaseType(abstractEnumeratorType);
			_enumerator.AddBaseType(TypeSystemServices.IEnumeratorType);

			CreateEnumeratorConstructor();
			CreateMoveNext();
			
			_enumerable.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
		}
Ejemplo n.º 16
0
        ClassDefinition CreateAdaptor(ICallableType to, ICallableType from)
        {
            BooClassBuilder adaptor = CodeBuilder.CreateClass("$adaptor$" + from.Name + "$" + to.Name + "$" + _adaptors.Count);

            adaptor.AddBaseType(TypeSystemServices.ObjectType);
            adaptor.Modifiers = TypeMemberModifiers.Final | TypeMemberModifiers.Internal;

            Field callable = adaptor.AddField("$from", from);

            BooMethodBuilder     constructor = adaptor.AddConstructor();
            ParameterDeclaration param       = constructor.AddParameter("from", from);

            constructor.Body.Add(
                CodeBuilder.CreateSuperConstructorInvocation(TypeSystemServices.ObjectType));
            constructor.Body.Add(
                CodeBuilder.CreateAssignment(
                    CodeBuilder.CreateReference(callable),
                    CodeBuilder.CreateReference(param)));

            CallableSignature signature = to.GetSignature();
            BooMethodBuilder  invoke    = adaptor.AddMethod("Invoke", signature.ReturnType);

            foreach (IParameter parameter in signature.Parameters)
            {
                invoke.AddParameter(parameter.Name, parameter.Type, parameter.IsByRef);
            }
            MethodInvocationExpression mie = CodeBuilder.CreateMethodInvocation(
                CodeBuilder.CreateReference(callable),
                GetInvokeMethod(from));
            int fromParameterCount = from.GetSignature().Parameters.Length;

            for (int i = 0; i < fromParameterCount; ++i)
            {
                mie.Arguments.Add(
                    CodeBuilder.CreateReference(invoke.Parameters[i]));
            }
            if (signature.ReturnType != TypeSystemServices.VoidType &&
                from.GetSignature().ReturnType != TypeSystemServices.VoidType)
            {
                invoke.Body.Add(new ReturnStatement(mie));
            }
            else
            {
                invoke.Body.Add(mie);
            }

            BooMethodBuilder adapt = adaptor.AddMethod("Adapt", to);

            adapt.Modifiers = TypeMemberModifiers.Static | TypeMemberModifiers.Public;
            param           = adapt.AddParameter("from", from);
            adapt.Body.Add(
                new ReturnStatement(
                    CodeBuilder.CreateConstructorInvocation(
                        to.GetConstructors().First(),
                        CodeBuilder.CreateConstructorInvocation(
                            (IConstructor)constructor.Entity,
                            CodeBuilder.CreateReference(param)),
                        CodeBuilder.CreateAddressOfExpression(invoke.Entity))));

            RegisterAdaptor(to, from, adaptor.ClassDefinition);

            return(adaptor.ClassDefinition);
        }
Ejemplo n.º 17
0
		void CreateAnonymousGeneratorType()
		{
			// Set up some important types
			_sourceItemType = TypeSystemServices.ObjectType;
			_sourceEnumeratorType = TypeSystemServices.IEnumeratorType;
			_sourceEnumerableType = TypeSystemServices.IEnumerableType;
			
			_resultEnumeratorType = TypeSystemServices.IEnumeratorGenericType.GenericInfo.ConstructType(_skeleton.GeneratorItemType);
			
			_enumerator = _collector.CreateSkeletonClass("Enumerator",_generator.LexicalInfo);

			// use a generic enumerator for the source type if possible
			_sourceItemType = TypeSystemServices.GetGenericEnumerableItemType(_generator.Iterator.ExpressionType);
			if (_sourceItemType != null && _sourceItemType != TypeSystemServices.ObjectType)
			{
				_sourceEnumerableType = TypeSystemServices.IEnumerableGenericType.GenericInfo.ConstructType(_sourceItemType);
				_sourceEnumeratorType = TypeSystemServices.IEnumeratorGenericType.GenericInfo.ConstructType(_sourceItemType);
			}
			else
			{
				_sourceItemType = TypeSystemServices.ObjectType;
			}
			
			// Add base types
			_enumerator.AddBaseType(_resultEnumeratorType);
			_enumerator.AddBaseType(TypeSystemServices.Map(typeof(ICloneable)));
			_enumerator.AddBaseType(TypeSystemServices.IDisposableType);
			
			// Add fields
			_enumeratorField = _enumerator.AddField("$$enumerator", _sourceEnumeratorType);
			_current = _enumerator.AddField("$$current", _skeleton.GeneratorItemType);
			
			// Add methods 
			CreateReset();
			CreateCurrent();
			CreateMoveNext();
			CreateClone();
			CreateDispose();
			
			EnumeratorConstructorMustCallReset();
			
			_collector.AdjustReferences();

			BooClassBuilder generatorClassBuilder = _skeleton.GeneratorClassBuilder;
			_collector.DeclareFieldsAndConstructor(generatorClassBuilder);
			
			CreateGetEnumerator();
			generatorClassBuilder.ClassDefinition.Members.Add(_enumerator.ClassDefinition);
		}