private void MakeInternal(NamedTypeDefinition nsType) { NestedTypeDefinition nestedTypeDef = nsType as NestedTypeDefinition; NamespaceTypeDefinition namespaceTypeDef = nsType as NamespaceTypeDefinition; if (namespaceTypeDef != null) { namespaceTypeDef.IsPublic = false; } else if (nestedTypeDef != null) { nestedTypeDef.Visibility = GetInternalVisibility(nestedTypeDef.Visibility); } }
/// <summary> /// Creates the appropriate kind of type definition (nested or not) and /// adds it to the type symbol cache. /// </summary> private NamedTypeDefinition CreateTypeDefinition(INamedTypeSymbol typeSymbol) { Contract.Requires(typeSymbol != null); NamedTypeDefinition cciType; if (typeSymbol.ContainingType == null) { cciType = new NamespaceTypeDefinition() { ContainingUnitNamespace = (IUnitNamespace)this.namespaceSymbolCache[typeSymbol.ContainingNamespace], IsPublic = typeSymbol.DeclaredAccessibility == Accessibility.Public, }; } else { cciType = new NestedTypeDefinition() { ContainingTypeDefinition = (ITypeDefinition)this.typeSymbolCache[typeSymbol.ContainingType], Visibility = TypeMemberVisibility.Private, }; } if (typeSymbol.TypeKind == TypeKind.Interface) { cciType.IsAbstract = true; cciType.IsInterface = true; } else { cciType.BaseClasses = new List <ITypeReference> { this.Map(typeSymbol.BaseType) }; cciType.IsBeforeFieldInit = true; // REVIEW: How to determine from typeSymbol? } cciType.IsClass = typeSymbol.IsReferenceType; cciType.IsValueType = typeSymbol.IsValueType; cciType.IsSealed = typeSymbol.IsSealed; cciType.InternFactory = this.host.InternFactory; cciType.Locations = Helper.WrapLocations(typeSymbol.Locations); cciType.Name = this.host.NameTable.GetNameFor(typeSymbol.Name); this.typeSymbolCache.Add(typeSymbol, cciType); return(cciType); }
/// <summary> /// Creates a new nested type definition with a default constructor and no other members and adds it to this.closureClasses. /// If this.method is generic, then the closure class is generic as well, with the same /// number of type parameters (constrained in the same way) as the generic method. /// Initializes this.currentClosure, this.currentClosureInstance and this.currentClosureSelfInstance. /// </summary> private void CreateClosureClass() { if (this.closureClasses == null) this.closureClasses = new List<ITypeDefinition>(); NestedTypeDefinition closure = new NestedTypeDefinition(); var containingType = this.method.ContainingTypeDefinition; closure.Name = this.host.NameTable.GetNameFor("<"+this.method.Name+">c__DisplayClass"+closure.GetHashCode()); closure.Attributes = new List<ICustomAttribute>(1) { this.compilerGenerated }; closure.BaseClasses = new List<ITypeReference>(1) { this.host.PlatformType.SystemObject }; closure.ContainingTypeDefinition = containingType; closure.Fields = new List<IFieldDefinition>(); closure.InternFactory = this.host.InternFactory; closure.IsBeforeFieldInit = true; closure.IsClass = true; closure.IsSealed = true; closure.Layout = LayoutKind.Auto; closure.Methods = new List<IMethodDefinition>(); closure.StringFormat = StringFormatKind.Ansi; closure.Visibility = TypeMemberVisibility.Private; this.closureClasses.Add(closure); this.currentClosureClass = closure; //generics if (this.method.IsGeneric) { Dictionary<ushort, IGenericParameterReference> genericMethodParameterMap = new Dictionary<ushort, IGenericParameterReference>(); this.genericMethodParameterMap = genericMethodParameterMap; bool foundConstraints = false; var genericTypeParameters = new List<IGenericTypeParameter>(this.method.GenericParameterCount); closure.GenericParameters = genericTypeParameters; foreach (var genericMethodParameter in this.method.GenericParameters) { var copyOfGenericMethodParameter = this.copier.Copy(genericMethodParameter); //so that we have mutable constraints to rewrite var genericTypeParameter = new GenericTypeParameter(); genericTypeParameter.Copy(copyOfGenericMethodParameter, this.host.InternFactory); genericTypeParameter.DefiningType = closure; if (genericTypeParameter.Constraints != null && genericTypeParameter.Constraints.Count > 0) foundConstraints = true; genericTypeParameters.Add(genericTypeParameter); genericMethodParameterMap.Add(copyOfGenericMethodParameter.Index, genericTypeParameter); } if (foundConstraints) { //Fix up any self references that might lurk inside constraints. closure.GenericParameters = new GenericParameterRewriter(this.host, genericMethodParameterMap).Rewrite(genericTypeParameters); } var instanceType = closure.InstanceType; var genericArguments = IteratorHelper.GetConversionEnumerable<IGenericMethodParameter, ITypeReference>(this.method.GenericParameters); this.currentClosureInstance = new Immutable.GenericTypeInstanceReference(instanceType.GenericType, genericArguments, this.host.InternFactory); this.currentClosureSelfInstance = instanceType; } else { //if any of the containing types are generic, we need an instance or a specialized nested type. this.currentClosureInstance = NestedTypeDefinition.SelfInstance(closure, this.host.InternFactory); this.currentClosureSelfInstance = this.currentClosureInstance; } //default constructor var block = new BlockStatement(); block.Statements.Add( new ExpressionStatement() { Expression = new MethodCall() { ThisArgument = new ThisReference() { Type = this.currentClosureSelfInstance }, MethodToCall = this.objectCtor, Type = this.host.PlatformType.SystemVoid } } ); var constructorBody = new SourceMethodBody(this.host, this.sourceLocationProvider) { LocalsAreZeroed = true, IsNormalized = true, Block = block }; var defaultConstructor = new MethodDefinition() { Body = constructorBody, ContainingTypeDefinition = closure, CallingConvention = CallingConvention.HasThis, InternFactory = this.host.InternFactory, IsCil = true, IsHiddenBySignature = true, IsRuntimeSpecial = true, IsSpecialName = true, Name = this.host.NameTable.Ctor, Type = this.host.PlatformType.SystemVoid, Visibility = TypeMemberVisibility.Public, }; constructorBody.MethodDefinition = defaultConstructor; closure.Methods.Add(defaultConstructor); }
public override void RewriteChildren(NestedTypeDefinition nestedTypeDefinition) { this.PruneInterfacesAndExplicitImplementationOverrides(nestedTypeDefinition); base.RewriteChildren(nestedTypeDefinition); }
/// <summary> /// Creates the appropriate kind of type definition (nested or not) and /// adds it to the type symbol cache. /// </summary> private NamedTypeDefinition CreateTypeDefinition(R.INamedTypeSymbol typeSymbol) { Contract.Requires(typeSymbol != null); NamedTypeDefinition cciType; if (typeSymbol.ContainingType == null) { cciType = new NamespaceTypeDefinition() { ContainingUnitNamespace = (IUnitNamespace)this.namespaceSymbolCache[typeSymbol.ContainingNamespace], IsPublic = typeSymbol.DeclaredAccessibility == R.CommonAccessibility.Public, }; } else { cciType = new NestedTypeDefinition() { ContainingTypeDefinition = (ITypeDefinition)this.typeSymbolCache[typeSymbol.ContainingType], Visibility = TypeMemberVisibility.Private, }; } if (typeSymbol.TypeKind == R.CommonTypeKind.Interface) { cciType.IsAbstract = true; cciType.IsInterface = true; } else { cciType.BaseClasses = new List<ITypeReference> { this.Map(typeSymbol.BaseType) }; cciType.IsBeforeFieldInit = true; // REVIEW: How to determine from typeSymbol? } cciType.IsClass = typeSymbol.IsReferenceType; cciType.IsValueType = typeSymbol.IsValueType; cciType.IsSealed = typeSymbol.IsSealed; cciType.InternFactory = this.host.InternFactory; cciType.Locations = Helper.WrapLocations(typeSymbol.Locations); cciType.Name = this.host.NameTable.GetNameFor(typeSymbol.Name); this.typeSymbolCache.Add(typeSymbol, cciType); return cciType; }
private NestedTypeDefinition CreateClosureClass(bool makeGeneric) { CustomAttribute compilerGeneratedAttribute = new CustomAttribute(); compilerGeneratedAttribute.Constructor = this.CompilerGeneratedCtor; NestedTypeDefinition result = new NestedTypeDefinition(); string signature = MemberHelper.GetMethodSignature(this.method, NameFormattingOptions.Signature | NameFormattingOptions.ReturnType | NameFormattingOptions.TypeParameters); //result.Name = this.host.NameTable.GetNameFor("closureclass"); result.Name = this.host.NameTable.GetNameFor(signature + " closure " + this.counter); this.counter++; result.Attributes = new List <ICustomAttribute>(1) { compilerGeneratedAttribute }; result.BaseClasses = new List <ITypeReference>(1) { this.host.PlatformType.SystemObject }; result.ContainingTypeDefinition = this.generatedclosureClass != null ? this.generatedclosureClass : method.ContainingTypeDefinition; result.Fields = new List <IFieldDefinition>(); if (makeGeneric) { result.GenericParameters = new List <IGenericTypeParameter>(); } result.Methods = new List <IMethodDefinition>(); result.InternFactory = this.host.InternFactory; result.IsBeforeFieldInit = true; result.IsClass = true; result.IsSealed = true; result.Layout = LayoutKind.Auto; result.StringFormat = StringFormatKind.Ansi; result.Visibility = TypeMemberVisibility.Private; //BoundField/*?*/ capturedThis; //var thisTypeReference = TypeDefinition.SelfInstance(this.method.ContainingTypeDefinition, this.host.InternFactory); //if (this.closureLocals.Count == 0 && this.FieldForCapturedLocalOrParameter.TryGetValue(thisTypeReference.InternedKey, out capturedThis)) { // result.Fields.Add(capturedThis.Field); // capturedThis.Field.ContainingTypeDefinition = result; // capturedThis.Field.Type = this.Visit(capturedThis.Field.Type); //} if (makeGeneric) { List <IGenericMethodParameter> genericMethodParameters = new List <IGenericMethodParameter>(); ushort count = 0; if (this.method.IsGeneric) { foreach (var genericMethodParameter in this.method.GenericParameters) { genericMethodParameters.Add(genericMethodParameter); GenericTypeParameter newTypeParam = new GenericTypeParameter() { Name = this.host.NameTable.GetNameFor(genericMethodParameter.Name.Value + "_"), Index = (count++), InternFactory = this.host.InternFactory, PlatformType = this.host.PlatformType, }; this.genericTypeParameterMapping[genericMethodParameter.InternedKey] = newTypeParam; newTypeParam.DefiningType = result; result.GenericParameters.Add(newTypeParam); } } this.copyTypeToClosure = new CopyTypeFromIteratorToClosure(this.host, genericTypeParameterMapping); if (this.method.IsGeneric) { // Duplicate Constraints foreach (var genericMethodParameter in genericMethodParameters) { GenericTypeParameter correspondingTypeParameter = (GenericTypeParameter)this.genericTypeParameterMapping[genericMethodParameter.InternedKey]; if (genericMethodParameter.Constraints != null) { correspondingTypeParameter.Constraints = new List <ITypeReference>(); foreach (ITypeReference t in genericMethodParameter.Constraints) { correspondingTypeParameter.Constraints.Add(copyTypeToClosure.Visit(t)); } } } } } this.generatedclosureClass = result; classList.Add(result); return(result); }
private NestedTypeDefinition CreateClosureClass(bool makeGeneric) { CustomAttribute compilerGeneratedAttribute = new CustomAttribute(); compilerGeneratedAttribute.Constructor = this.CompilerGeneratedCtor; NestedTypeDefinition result = new NestedTypeDefinition(); string signature = MemberHelper.GetMethodSignature(this.method, NameFormattingOptions.Signature | NameFormattingOptions.ReturnType | NameFormattingOptions.TypeParameters); //result.Name = this.host.NameTable.GetNameFor("closureclass"); result.Name = this.host.NameTable.GetNameFor(signature + " closure " + this.counter); this.counter++; result.Attributes = new List<ICustomAttribute>(1) { compilerGeneratedAttribute }; result.BaseClasses = new List<ITypeReference>(1) { this.host.PlatformType.SystemObject }; result.ContainingTypeDefinition = this.generatedclosureClass != null ? this.generatedclosureClass : method.ContainingTypeDefinition; result.Fields = new List<IFieldDefinition>(); if (makeGeneric) result.GenericParameters = new List<IGenericTypeParameter>(); result.Methods = new List<IMethodDefinition>(); result.InternFactory = this.host.InternFactory; result.IsBeforeFieldInit = true; result.IsClass = true; result.IsSealed = true; result.Layout = LayoutKind.Auto; result.StringFormat = StringFormatKind.Ansi; result.Visibility = TypeMemberVisibility.Private; //BoundField/*?*/ capturedThis; //var thisTypeReference = TypeDefinition.SelfInstance(this.method.ContainingTypeDefinition, this.host.InternFactory); //if (this.closureLocals.Count == 0 && this.FieldForCapturedLocalOrParameter.TryGetValue(thisTypeReference.InternedKey, out capturedThis)) { // result.Fields.Add(capturedThis.Field); // capturedThis.Field.ContainingTypeDefinition = result; // capturedThis.Field.Type = this.Visit(capturedThis.Field.Type); //} if (makeGeneric) { List<IGenericMethodParameter> genericMethodParameters = new List<IGenericMethodParameter>(); ushort count = 0; if (this.method.IsGeneric) { foreach (var genericMethodParameter in this.method.GenericParameters) { genericMethodParameters.Add(genericMethodParameter); GenericTypeParameter newTypeParam = new GenericTypeParameter() { Name = this.host.NameTable.GetNameFor(genericMethodParameter.Name.Value + "_"), Index = (count++), InternFactory = this.host.InternFactory, PlatformType = this.host.PlatformType, }; this.genericTypeParameterMapping[genericMethodParameter.InternedKey] = newTypeParam; newTypeParam.DefiningType = result; result.GenericParameters.Add(newTypeParam); } } this.copyTypeToClosure = new CopyTypeFromIteratorToClosure(this.host, genericTypeParameterMapping); if (this.method.IsGeneric) { // Duplicate Constraints foreach (var genericMethodParameter in genericMethodParameters) { GenericTypeParameter correspondingTypeParameter = (GenericTypeParameter)this.genericTypeParameterMapping[genericMethodParameter.InternedKey]; if (genericMethodParameter.Constraints != null) { correspondingTypeParameter.Constraints = new List<ITypeReference>(); foreach (ITypeReference t in genericMethodParameter.Constraints) { correspondingTypeParameter.Constraints.Add(copyTypeToClosure.Visit(t)); } } } } } this.generatedclosureClass = result; classList.Add(result); return result; }
/// <summary> /// Create the iterator closure class and add it to the private helper types list. /// </summary> private IteratorClosureInformation CreateIteratorClosure(BlockStatement blockStatement) { IteratorClosureInformation result = new IteratorClosureInformation(this.host); CustomAttribute compilerGeneratedAttribute = new CustomAttribute(); compilerGeneratedAttribute.Constructor = this.CompilerGeneratedCtor; // Create the closure class with CompilerGeneratedAttribute, the list of generic type parameters isomorphic to // those of the iterator method. NestedTypeDefinition iteratorClosureType = new NestedTypeDefinition() { ExplicitImplementationOverrides = new List<IMethodImplementation>(), Fields = new List<IFieldDefinition>(), GenericParameters = new List<IGenericTypeParameter>(), Interfaces = new List<ITypeReference>(), Methods = new List<IMethodDefinition>(), NestedTypes = new List<INestedTypeDefinition>(), }; this.privateHelperTypes.Add(iteratorClosureType); result.ClosureDefinition = iteratorClosureType; List<IGenericMethodParameter> genericMethodParameters = new List<IGenericMethodParameter>(); ushort count = 0; foreach (var genericMethodParameter in method.GenericParameters) { genericMethodParameters.Add(genericMethodParameter); GenericTypeParameter newTypeParam = new GenericTypeParameter() { Name = this.host.NameTable.GetNameFor(genericMethodParameter.Name.Value + "_"), InternFactory = this.host.InternFactory, PlatformType = this.host.PlatformType, Index = (count++), }; this.genericTypeParameterMapping[genericMethodParameter.InternedKey] = newTypeParam; newTypeParam.DefiningType = iteratorClosureType; iteratorClosureType.GenericParameters.Add(newTypeParam); } this.copyTypeToClosure = new CopyTypeFromIteratorToClosure(this.host, this.genericTypeParameterMapping); // Duplicate Constraints foreach (var genericMethodParameter in genericMethodParameters) { GenericTypeParameter correspondingTypeParameter = (GenericTypeParameter)this.genericTypeParameterMapping[genericMethodParameter.InternedKey]; if (genericMethodParameter.Constraints != null) { correspondingTypeParameter.Constraints = new List<ITypeReference>(); foreach (ITypeReference t in genericMethodParameter.Constraints) { correspondingTypeParameter.Constraints.Add(copyTypeToClosure.Visit(t)); } } } // elementTypes contains only one element, the argument type of the return type (the T in IEnumerable<T>) of the iterator method, or simply System.Object if the // iterator is not generic. IEnumerable<ITypeReference> elementTypes = GetClosureEnumeratorTypeArguments(method.Type); // elementType of the IEnumerable. ITypeReference elementType = null; foreach (ITypeReference t in elementTypes) { elementType = t; break; } result.ElementType = this.copyTypeToClosure.Visit(elementType); // Set up the iterator closure class. // TODO: name generation to follow the convention of csc. iteratorClosureType.Name = this.host.NameTable.GetNameFor("<" + this.method.Name.Value + ">" + "ic__" + this.privateHelperTypes.Count); iteratorClosureType.Attributes = new List<ICustomAttribute>(1) { compilerGeneratedAttribute }; iteratorClosureType.BaseClasses = new List<ITypeReference>(1) { this.host.PlatformType.SystemObject }; iteratorClosureType.ContainingTypeDefinition = this.method.ContainingTypeDefinition; iteratorClosureType.ExplicitImplementationOverrides = new List<IMethodImplementation>(7); iteratorClosureType.InternFactory = this.host.InternFactory; iteratorClosureType.IsBeforeFieldInit = true; iteratorClosureType.IsClass = true; iteratorClosureType.IsSealed = true; iteratorClosureType.Layout = LayoutKind.Auto; iteratorClosureType.StringFormat = StringFormatKind.Ansi; iteratorClosureType.Visibility = TypeMemberVisibility.Private; /* Interfaces. */ result.InitializeInterfaces(result.ElementType, this.isEnumerable); /* Fields, Methods, and Properties. */ CreateIteratorClosureFields(result); CreateIteratorClosureConstructor(result); CreateIteratorClosureMethods(result, blockStatement); CreateIteratorClosureProperties(result); return result; }