/// <summary> /// Creates a new TypeSystemConvertVisitor and initializes it with a given context. /// </summary> /// <param name="parsedFile">The parsed file to which members should be added.</param> /// <param name="currentUsingScope">The current using scope.</param> /// <param name="currentTypeDefinition">The current type definition.</param> public TypeSystemConvertVisitor(CSharpParsedFile parsedFile, UsingScope currentUsingScope = null, CSharpUnresolvedTypeDefinition currentTypeDefinition = null) { if (parsedFile == null) throw new ArgumentNullException("parsedFile"); this.parsedFile = parsedFile; this.usingScope = currentUsingScope ?? parsedFile.RootUsingScope; this.currentTypeDefinition = currentTypeDefinition; }
CSharpUnresolvedTypeDefinition CreateTypeDefinition(string name) { CSharpUnresolvedTypeDefinition newType; if (currentTypeDefinition != null) { newType = new CSharpUnresolvedTypeDefinition(currentTypeDefinition, name); foreach (var typeParameter in currentTypeDefinition.TypeParameters) newType.TypeParameters.Add(typeParameter); currentTypeDefinition.NestedTypes.Add(newType); } else { newType = new CSharpUnresolvedTypeDefinition(usingScope, name); parsedFile.TopLevelTypeDefinitions.Add(newType); } newType.ParsedFile = parsedFile; newType.HasExtensionMethods = false; // gets set to true when an extension method is added return newType; }
public override IUnresolvedEntity VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) { var td = currentTypeDefinition = CreateTypeDefinition(typeDeclaration.Name); td.Region = MakeRegion(typeDeclaration); td.BodyRegion = MakeBraceRegion(typeDeclaration); ApplyModifiers(td, typeDeclaration.Modifiers); switch (typeDeclaration.ClassType) { case ClassType.Enum: td.Kind = TypeKind.Enum; break; case ClassType.Interface: td.Kind = TypeKind.Interface; td.IsAbstract = true; // interfaces are implicitly abstract break; case ClassType.Struct: td.Kind = TypeKind.Struct; td.IsSealed = true; // enums/structs are implicitly sealed break; } ConvertAttributes(td.Attributes, typeDeclaration.Attributes); ConvertTypeParameters(td.TypeParameters, typeDeclaration.TypeParameters, typeDeclaration.Constraints, EntityType.TypeDefinition); foreach (AstType baseType in typeDeclaration.BaseTypes) { td.BaseTypes.Add(baseType.ToTypeReference(SimpleNameLookupMode.BaseTypeReference)); } foreach (AttributedNode member in typeDeclaration.Members) { member.AcceptVisitor(this, data); } currentTypeDefinition = (CSharpUnresolvedTypeDefinition)currentTypeDefinition.DeclaringTypeDefinition; if (interningProvider != null) { td.ApplyInterningProvider(interningProvider); } return td; }
public override IUnresolvedEntity VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration, object data) { var td = currentTypeDefinition = CreateTypeDefinition(delegateDeclaration.Name); td.Kind = TypeKind.Delegate; td.Region = MakeRegion(delegateDeclaration); td.BaseTypes.Add(KnownTypeReference.MulticastDelegate); ApplyModifiers(td, delegateDeclaration.Modifiers); td.IsSealed = true; // delegates are implicitly sealed ConvertTypeParameters(td.TypeParameters, delegateDeclaration.TypeParameters, delegateDeclaration.Constraints, EntityType.TypeDefinition); ITypeReference returnType = delegateDeclaration.ReturnType.ToTypeReference(); List<IUnresolvedParameter> parameters = new List<IUnresolvedParameter>(); ConvertParameters(parameters, delegateDeclaration.Parameters); AddDefaultMethodsToDelegate(td, returnType, parameters); foreach (AttributeSection section in delegateDeclaration.Attributes) { if (section.AttributeTarget == "return") { List<IUnresolvedAttribute> returnTypeAttributes = new List<IUnresolvedAttribute>(); ConvertAttributes(returnTypeAttributes, section); IUnresolvedMethod invokeMethod = (IUnresolvedMethod)td.Members.Single(m => m.Name == "Invoke"); IUnresolvedMethod endInvokeMethod = (IUnresolvedMethod)td.Members.Single(m => m.Name == "EndInvoke"); foreach (IUnresolvedAttribute attr in returnTypeAttributes) { invokeMethod.ReturnTypeAttributes.Add(attr); endInvokeMethod.ReturnTypeAttributes.Add(attr); } } else { ConvertAttributes(td.Attributes, section); } } currentTypeDefinition = (CSharpUnresolvedTypeDefinition)currentTypeDefinition.DeclaringTypeDefinition; if (interningProvider != null) { td.ApplyInterningProvider(interningProvider); } return td; }
public CSharpUnresolvedTypeDefinition(CSharpUnresolvedTypeDefinition declaringTypeDefinition, string name) : base(declaringTypeDefinition, name) { this.usingScope = declaringTypeDefinition.usingScope; this.AddDefaultConstructorIfRequired = true; }