Beispiel #1
0
		override public void LeaveBlockExpression(BlockExpression node)
		{
			var closureEntity = GetEntity(node) as InternalMethod;
			if (closureEntity == null)
				return;

			var collector = new ForeignReferenceCollector();
			{
				collector.CurrentMethod = closureEntity.Method;
				collector.CurrentType = (IType)closureEntity.DeclaringType;
				closureEntity.Method.Body.Accept(collector);
				
				if (collector.ContainsForeignLocalReferences)
				{
					BooClassBuilder closureClass = CreateClosureClass(collector, closureEntity);
					closureClass.ClassDefinition.LexicalInfo = node.LexicalInfo;
					collector.AdjustReferences();
					
					ReplaceCurrentNode(
						CodeBuilder.CreateMemberReference(
							collector.CreateConstructorInvocationWithReferencedEntities(
								closureClass.Entity),
							closureEntity));
				}
				else
				{
					Expression expression = CodeBuilder.CreateMemberReference(closureEntity);
					expression.LexicalInfo = node.LexicalInfo;
					TypeSystemServices.GetConcreteExpressionType(expression);
					ReplaceCurrentNode(expression);
				}
			}
		}
Beispiel #2
0
        override public void LeaveBlockExpression(BlockExpression node)
        {
            InternalMethod closureEntity = (InternalMethod)GetEntity(node);

            using (ForeignReferenceCollector collector = new ForeignReferenceCollector())
            {
                collector.CurrentMethod = closureEntity.Method;
                collector.CurrentType   = (IType)closureEntity.DeclaringType;
                collector.Initialize(_context);
                collector.Visit(closureEntity.Method.Body);

                if (collector.ContainsForeignLocalReferences)
                {
                    BooClassBuilder closureClass = CreateClosureClass(collector, closureEntity);
                    collector.AdjustReferences();

                    ReplaceCurrentNode(
                        CodeBuilder.CreateMemberReference(
                            collector.CreateConstructorInvocationWithReferencedEntities(
                                closureClass.Entity),
                            closureEntity));
                }
                else
                {
                    Expression expression = CodeBuilder.CreateMemberReference(closureEntity);
                    TypeSystemServices.GetConcreteExpressionType(expression);
                    ReplaceCurrentNode(expression);
                }
            }
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public override void LeaveBlockExpression(BlockExpression node)
        {
            var closureEntity = GetEntity(node) as InternalMethod;

            if (closureEntity == null)
            {
                return;
            }

            var collector = new ForeignReferenceCollector();
            {
                collector.CurrentMethod = closureEntity.Method;
                collector.CurrentType   = closureEntity.DeclaringType;
                closureEntity.Method.Body.Accept(collector);

                if (collector.ContainsForeignLocalReferences)
                {
                    BooClassBuilder closureClass = CreateClosureClass(collector, closureEntity);
                    if (closureEntity is InternalGenericMethod)
                    {
                        closureEntity = GetEntity(closureEntity.Method) as InternalMethod;
                    }
                    closureClass.ClassDefinition.LexicalInfo = node.LexicalInfo;
                    collector.AdjustReferences();

                    if (_mapper != null)
                    {
                        closureClass.ClassDefinition.Accept(new GenericTypeMapper(_mapper));
                    }

                    ReplaceCurrentNode(
                        CodeBuilder.CreateMemberReference(
                            collector.CreateConstructorInvocationWithReferencedEntities(
                                closureClass.Entity,
                                node.GetAncestor <Method>()),
                            closureEntity));
                }
                else
                {
                    _mapper = closureEntity.Method["GenericMapper"] as GeneratorTypeReplacer;
                    if (_mapper != null)
                    {
                        closureEntity.Method.Accept(new GenericTypeMapper(_mapper));
                    }
                    IMethod entity = closureEntity;
                    if (entity.GenericInfo != null)
                    {
                        entity = MapGenericMethod(entity, node.GetAncestor <Method>().GenericParameters);
                    }
                    Expression expression = CodeBuilder.CreateMemberReference(entity);
                    expression.LexicalInfo = node.LexicalInfo;
                    TypeSystemServices.GetConcreteExpressionType(expression);
                    ReplaceCurrentNode(expression);
                }
            }
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        override public void LeaveBlockExpression(BlockExpression node)
        {
            var closureEntity = GetEntity(node) as InternalMethod;

            if (closureEntity == null)
            {
                return;
            }

            var collector = new ForeignReferenceCollector();
            {
                collector.CurrentMethod = closureEntity.Method;
                collector.CurrentType   = (IType)closureEntity.DeclaringType;
                closureEntity.Method.Body.Accept(collector);

                if (collector.ContainsForeignLocalReferences)
                {
                    BooClassBuilder closureClass = CreateClosureClass(collector, closureEntity);
                    closureClass.ClassDefinition.LexicalInfo = node.LexicalInfo;
                    collector.AdjustReferences();

                    ReplaceCurrentNode(
                        CodeBuilder.CreateMemberReference(
                            collector.CreateConstructorInvocationWithReferencedEntities(
                                closureClass.Entity),
                            closureEntity));
                }
                else
                {
                    Expression expression = CodeBuilder.CreateMemberReference(closureEntity);
                    expression.LexicalInfo = node.LexicalInfo;
                    TypeSystemServices.GetConcreteExpressionType(expression);
                    ReplaceCurrentNode(expression);
                }
            }
        }