Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
0
        void CreateMoveNext()
        {
            Method generator = _generator.Method;

            BooMethodBuilder mn = _enumerator.AddVirtualMethod("MoveNext", TypeSystemServices.BoolType);

            mn.Method.LexicalInfo = this.LexicalInfo;
            _moveNext             = mn.Entity;

            foreach (Local local in generator.Locals)
            {
                InternalLocal entity = (InternalLocal)local.Entity;

                Field field = _enumerator.AddField("___" + entity.Name + _context.AllocIndex(), entity.Type);
                field.Modifiers |= TypeMemberModifiers.Internal;
                _mapping[entity] = field.Entity;
            }
            generator.Locals.Clear();

            foreach (ParameterDeclaration parameter in generator.Parameters)
            {
                InternalParameter entity = (InternalParameter)parameter.Entity;
                if (entity.IsUsed)
                {
                    Field field = DeclareFieldInitializedFromConstructorParameter(_enumerator, _enumeratorConstructor,
                                                                                  entity.Name,
                                                                                  entity.Type);
                    _mapping[entity] = field.Entity;
                }
            }

            mn.Body.Add(CreateLabel(generator));
            mn.Body.Add(generator.Body);
            generator.Body.Clear();

            Visit(mn.Body);

            mn.Body.Add(CreateYieldInvocation(null));
            mn.Body.Add(CreateLabel(generator));

            mn.Body.Insert(0,
                           CodeBuilder.CreateSwitch(
                               this.LexicalInfo,
                               CodeBuilder.CreateMemberReference(_state),
                               _labels));
        }
Ejemplo n.º 6
0
        Field DeclareFieldInitializedFromConstructorParameter(BooClassBuilder type,
                                                              BooMethodBuilder constructor,
                                                              string parameterName,
                                                              IType parameterType)
        {
            Field field = type.AddField("___" + parameterName + _context.AllocIndex(), parameterType);

            InitializeFieldFromConstructorParameter(constructor, field, parameterName, parameterType);
            return(field);
        }
Ejemplo n.º 7
0
 private Expression GetDefaultYieldValue()
 {
     if (_generatorItemType.IsValueType)
     {
         if (null == _nullValueField)
         {
             _nullValueField = _enumerator.AddField("$empty", _generatorItemType);
         }
         return(CodeBuilder.CreateReference(_nullValueField));
     }
     return(new NullLiteralExpression());
 }
Ejemplo n.º 8
0
        public void DeclareFieldsAndConstructor(BooClassBuilder builder)
        {
            // referenced entities turn into fields
            foreach (ITypedEntity entity in Builtins.array(_referencedEntities.Keys))
            {
                Field field = builder.AddField("__" + entity.Name + _context.AllocIndex(), entity.Type);
                field.Modifiers             = TypeMemberModifiers.Internal;
                _referencedEntities[entity] = field.Entity;
            }

            // single constructor taking all referenced entities
            BooMethodBuilder constructor = builder.AddConstructor();

            constructor.Body.Add(CodeBuilder.CreateSuperConstructorInvocation(builder.Entity.BaseType));
            foreach (ITypedEntity entity in _referencedEntities.Keys)
            {
                InternalField        field     = (InternalField)_referencedEntities[entity];
                ParameterDeclaration parameter = constructor.AddParameter(field.Name, entity.Type);
                constructor.Body.Add(
                    CodeBuilder.CreateAssignment(CodeBuilder.CreateReference(field),
                                                 CodeBuilder.CreateReference(parameter)));
            }
        }
Ejemplo n.º 9
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.º 10
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);
		}