private static void CheckTypeForGenericParams( IType type, HashSet <string> genericsSet, BooClassBuilder builder, GeneratorTypeReplacer mapper) { if (type is IGenericParameter) { if (!genericsSet.Contains(type.Name)) { builder.AddGenericParameter(type.Name); genericsSet.Add(type.Name); } if (!mapper.ContainsType(type)) { mapper.Replace( type, (IType)builder.ClassDefinition.GenericParameters .First(gp => gp.Name.Equals(type.Name)).Entity); } } else { var genType = type as IConstructedTypeInfo; if (genType != null) { foreach (var arg in genType.GenericArguments) { CheckTypeForGenericParams(arg, genericsSet, builder, mapper); } } } }
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; } }
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; } }
public GeneratorSkeleton(BooClassBuilder generatorBuilder, BooMethodBuilder getEnumeratorBuilder, IType generatorItemType, GeneratorTypeReplacer generatorClassTypeReplacer) { GeneratorClassBuilder = generatorBuilder; GeneratorItemType = generatorItemType; GetEnumeratorBuilder = getEnumeratorBuilder; GeneratorClassTypeReplacer = generatorClassTypeReplacer; }
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); }
BooClassBuilder CreateClosureClass(ForeignReferenceCollector collector, InternalMethod closure) { Method method = closure.Method; TypeDefinition parent = method.DeclaringType; parent.Members.Remove(method); BooClassBuilder builder = collector.CreateSkeletonClass(closure.Name, method.LexicalInfo); parent.Members.Add(builder.ClassDefinition); builder.ClassDefinition.Members.Add(method); method.Name = "Invoke"; if (method.IsStatic) { // need to adjust paremeter indexes (parameter 0 is now self) foreach (ParameterDeclaration parameter in method.Parameters) { ((InternalParameter)parameter.Entity).Index += 1; } } method.Modifiers = TypeMemberModifiers.Public; var coll = new GenericTypeCollector(CodeBuilder); coll.Process(builder.ClassDefinition); return(builder); }
BooMethodBuilder CreateConstructor(BooClassBuilder builder) { BooMethodBuilder constructor = builder.AddConstructor(); constructor.Body.Add(CodeBuilder.CreateSuperConstructorInvocation(builder.Entity.BaseType)); return(constructor); }
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); } } }
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); }
public void DeclareFieldsAndConstructor(BooClassBuilder builder) { var keys = _referencedEntities.Keys.Cast <ITypedEntity>().ToArray(); foreach (var entity in keys) { _collector.Visit(entity.Type); } if (_collector.Matches.Any()) { BuildTypeMap(builder.ClassDefinition); } // referenced entities turn into fields foreach (var entity in keys) { Field field = builder.AddInternalField(GetUniqueName(entity.Name), _mapper.MapType(entity.Type)); _referencedEntities[entity] = (InternalField)field.Entity; } // single constructor taking all referenced entities BooMethodBuilder constructor = builder.AddConstructor(); constructor.Modifiers = TypeMemberModifiers.Public; constructor.Body.Add(CodeBuilder.CreateSuperConstructorInvocation(builder.Entity.BaseType)); foreach (var entity in _referencedEntities.Keys) { InternalField field = _referencedEntities[entity]; ParameterDeclaration parameter = constructor.AddParameter(field.Name, ((ITypedEntity)entity).Type); constructor.Body.Add( CodeBuilder.CreateAssignment(CodeBuilder.CreateReference(field), CodeBuilder.CreateReference(parameter))); } }
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; } }
private BooMethodBuilder SetUpGetEnumeratorMethodBuilder(Node sourceNode, BooClassBuilder builder, IType generatorItemType) { var getEnumeratorBuilder = builder.AddVirtualMethod( "GetEnumerator", TypeSystemServices.IEnumeratorGenericType.GenericInfo.ConstructType(generatorItemType)); getEnumeratorBuilder.Method.LexicalInfo = sourceNode.LexicalInfo; return getEnumeratorBuilder; }
public BooClassBuilder CreateSkeletonClass(string name) { BooClassBuilder builder = CodeBuilder.CreateClass(name); builder.AddBaseType(CodeBuilder.TypeSystemServices.ObjectType); DeclareFieldsAndConstructor(builder); return(builder); }
private BooMethodBuilder SetUpGetEnumeratorMethodBuilder(Node sourceNode, BooClassBuilder builder, IType generatorItemType) { BooMethodBuilder getEnumeratorBuilder = builder.AddVirtualMethod( "GetEnumerator", TypeSystemServices.IEnumeratorGenericType.GenericInfo.ConstructType(generatorItemType)); getEnumeratorBuilder.Method.LexicalInfo = sourceNode.LexicalInfo; return(getEnumeratorBuilder); }
override public void Run() { _generatorItemType = (IType)_generator.Method["GeneratorItemType"]; _enumerable = (BooClassBuilder)_generator.Method["GeneratorClassBuilder"]; if (null == _enumerable) { Errors.Add(CompilerErrorFactory.InternalError(_generator.Method, null)); } CreateEnumerableConstructor(); CreateEnumerator(); MethodInvocationExpression enumerableConstructorInvocation = CodeBuilder.CreateConstructorInvocation(_enumerable.ClassDefinition); MethodInvocationExpression enumeratorConstructorInvocation = CodeBuilder.CreateConstructorInvocation(_enumerator.ClassDefinition); // propagate the necessary parameters from // the enumerable to the enumerator foreach (ParameterDeclaration parameter in _generator.Method.Parameters) { InternalParameter entity = (InternalParameter)parameter.Entity; if (entity.IsUsed) { enumerableConstructorInvocation.Arguments.Add( CodeBuilder.CreateReference(parameter)); PropagateFromEnumerableToEnumerator(enumeratorConstructorInvocation, entity.Name, entity.Type); } } // propagate the external self reference if necessary if (null != _externalEnumeratorSelf) { IType type = (IType)_externalEnumeratorSelf.Type.Entity; enumerableConstructorInvocation.Arguments.Add( CodeBuilder.CreateSelfReference(type)); PropagateFromEnumerableToEnumerator(enumeratorConstructorInvocation, "self_", type); } CreateGetEnumerator(enumeratorConstructorInvocation); Block body = _generator.Method.Body; body.Clear(); body.Add( new ReturnStatement( GeneratorReturnsIEnumerator() ? CreateGetEnumeratorInvocation(enumerableConstructorInvocation) : enumerableConstructorInvocation)); }
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); } } }
Field DeclareFieldInitializedFromConstructorParameter(BooClassBuilder type, BooMethodBuilder constructor, string parameterName, IType parameterType) { Field field = type.AddInternalField(UniqueName(parameterName), parameterType); InitializeFieldFromConstructorParameter(constructor, field, parameterName, parameterType); return(field); }
GeneratorSkeleton CreateGeneratorSkeleton(Node sourceNode, Method enclosingMethod, IType generatorItemType) { // create the class skeleton for type inference to work BooClassBuilder builder = SetUpEnumerableClassBuilder(sourceNode, enclosingMethod, generatorItemType); BooMethodBuilder getEnumeratorBuilder = SetUpGetEnumeratorMethodBuilder(sourceNode, builder, generatorItemType); enclosingMethod.DeclaringType.Members.Add(builder.ClassDefinition); return(new GeneratorSkeleton(builder, generatorItemType, getEnumeratorBuilder)); }
Field DeclareFieldInitializedFromConstructorParameter(BooClassBuilder type, BooMethodBuilder constructor, string parameterName, IType parameterType) { Field field = type.AddInternalField("$" + parameterName + _context.AllocIndex(), parameterType); InitializeFieldFromConstructorParameter(constructor, field, parameterName, parameterType); return(field); }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) : base(context, method) { var skeleton = My <GeneratorSkeletonBuilder> .Instance.SkeletonFor(method); _generatorItemType = skeleton.GeneratorItemType; _enumerable = skeleton.GeneratorClassBuilder; _getEnumeratorBuilder = skeleton.GetEnumeratorBuilder; _methodToEnumerableMapper = skeleton.GeneratorClassTypeReplacer; }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) { _labels = new List(); _mapping = new Hashtable(); _generator = method; _generatorItemType = (IType)_generator.Method["GeneratorItemType"]; _enumerable = (BooClassBuilder)_generator.Method["GeneratorClassBuilder"]; Debug.Assert(null != _generatorItemType); Debug.Assert(null != _enumerable); Initialize(context); }
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); }
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); }
protected Field DeclareFieldInitializedFromConstructorParameter(BooClassBuilder type, BooMethodBuilder constructor, string parameterName, IType parameterType, TypeReplacer replacer) { var internalFieldType = replacer.MapType(parameterType); Field field = type.AddInternalField(UniqueName(parameterName), internalFieldType); InitializeFieldFromConstructorParameter(constructor, field, parameterName, internalFieldType); return(field); }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) { _labels = new List(); _mapping = new Hashtable(); _generator = method; GeneratorSkeleton skeleton = context.Provide<GeneratorSkeletonBuilder>().SkeletonFor(method); _generatorItemType = skeleton.GeneratorItemType; _enumerable = skeleton.GeneratorClassBuilder; _getEnumeratorBuilder = skeleton.GetEnumeratorBuilder; Initialize(context); }
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); }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) { _labels = new List<LabelStatement>(); _mapping = new Hashtable(); _generator = method; var skeleton = My<GeneratorSkeletonBuilder>.Instance.SkeletonFor(method); _generatorItemType = skeleton.GeneratorItemType; _enumerable = skeleton.GeneratorClassBuilder; _getEnumeratorBuilder = skeleton.GetEnumeratorBuilder; Initialize(context); }
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); }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) { _labels = new List <LabelStatement>(); _mapping = new Hashtable(); _generator = method; GeneratorSkeleton skeleton = My <GeneratorSkeletonBuilder> .Instance.SkeletonFor(method); _generatorItemType = skeleton.GeneratorItemType; _enumerable = skeleton.GeneratorClassBuilder; _getEnumeratorBuilder = skeleton.GetEnumeratorBuilder; Initialize(context); }
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); }
private void MapMethodGenerics(BooClassBuilder builder, Method method) { Debug.Assert(_mapper != null); var classParams = builder.ClassDefinition.GenericParameters; foreach (var genParam in method.GenericParameters) { var replacement = classParams.FirstOrDefault(p => p.Name.Equals(genParam.Name)); if (replacement != null && genParam != replacement.Entity) { _mapper.Replace((IType)genParam.Entity, (IType)replacement.Entity); } } method.GenericParameters.Clear(); method.Entity = new InternalMethod(Environments.My <InternalTypeSystemProvider> .Instance, method); }
protected virtual void CreateStateMachine() { _stateMachineClass = CodeBuilder.CreateClass(StateMachineClassName); _stateMachineClass.AddAttribute(CodeBuilder.CreateAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute))); _stateMachineClass.LexicalInfo = this.LexicalInfo; foreach (var param in _genericParams) { var replacement = _stateMachineClass.AddGenericParameter(param); _methodToStateMachineMapper.Replace((IType)param.Entity, (IType)replacement.Entity); } SetupStateMachine(); CreateStateMachineConstructor(); SaveStateMachineClass(_stateMachineClass.ClassDefinition); CreateMoveNext(); }
public void ChainConstructorsFromBaseType(BooClassBuilder builder) { foreach (IConstructor constructor in TypeSystemExtensions.GetConstructors(builder.get_Entity().get_BaseType())) { ExpressionStatement statement = this.get_CodeBuilder().CreateSuperConstructorInvocation(constructor); MethodInvocationExpression expression = statement.get_Expression() as MethodInvocationExpression; BooMethodBuilder builder2 = builder.AddConstructor(); int index = 0; IParameter[] parameters = constructor.GetParameters(); int length = parameters.Length; while (index < length) { ParameterDeclaration declaration = builder2.AddParameter(parameters[index].get_Name(), parameters[index].get_Type()); expression.get_Arguments().Add(this.get_CodeBuilder().CreateReference(declaration)); index++; } builder2.get_Body().Add(statement); } }
public void AddLocalVariablesAsFields(BooClassBuilder builder) { Field field; foreach (Local local in this.CurrentMethodNode.get_Locals()) { InternalLocal entity = this.GetEntity(local); if (!entity.get_IsPrivateScope()) { field = builder.AddPublicField(entity.get_Name(), entity.get_Type()); this.SetEvaluationContextField(local, (InternalField) this.GetEntity(field)); } } foreach (ParameterDeclaration declaration in this.CurrentMethodNode.get_Parameters()) { InternalParameter parameter = this.GetEntity(declaration); field = builder.AddPublicField(parameter.get_Name(), parameter.get_Type()); this.SetEvaluationContextField(declaration, (InternalField) this.GetEntity(field)); } }
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); }
BooMethodBuilder CreateConstructor(BooClassBuilder builder) { BooMethodBuilder constructor = builder.AddConstructor(); constructor.Body.Add(CodeBuilder.CreateSuperConstructorInvocation(builder.Entity.BaseType)); return constructor; }
Field DeclareFieldInitializedFromConstructorParameter(BooClassBuilder type, BooMethodBuilder constructor, string parameterName, IType parameterType) { Field field = type.AddInternalField(UniqueName(parameterName), parameterType); InitializeFieldFromConstructorParameter(constructor, field, parameterName, parameterType); return field; }
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); }
public void DeclareFieldsAndConstructor(BooClassBuilder builder) { // referenced entities turn into fields foreach (ITypedEntity entity in Builtins.array(_referencedEntities.Keys)) { Field field = builder.AddInternalField(GetUniqueName(entity.Name), entity.Type); _referencedEntities[entity] = field.Entity; } // single constructor taking all referenced entities BooMethodBuilder constructor = builder.AddConstructor(); constructor.Modifiers = TypeMemberModifiers.Public; 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))); } }
public GeneratorSkeleton(BooClassBuilder generatorBuilder, BooMethodBuilder getEnumeratorBuilder, IType generatorItemType) { GeneratorClassBuilder = generatorBuilder; GeneratorItemType = generatorItemType; GetEnumeratorBuilder = getEnumeratorBuilder; }
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); }