From dc8d4cadcb7de8c14bb69d9a59ba1ba6b2b8131f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 17 Dec 2010 18:34:45 +0100 Subject: [PATCH] Use (Namespace,Name) pair instead of FullName. --- .../TypeSystem/TypeSystemTests.cs | 2 +- .../CSharp/Parser/TypeSystemConvertVisitor.cs | 6 +- .../CSharp/Resolver/CSharpResolver.cs | 11 +-- .../CSharp/Resolver/Conversions.cs | 2 +- .../CSharp/Resolver/ResolveVisitor.cs | 2 +- .../TypeSystem/ArrayType.cs | 4 +- .../TypeSystem/CecilLoader.cs | 93 ++++++++----------- .../TypeSystem/ITypeResolveContext.cs | 10 +- .../Implementation/AbstractMember.cs | 24 ++--- .../CompositeTypeResolveContext.cs | 4 +- .../TypeSystem/Implementation/DefaultField.cs | 7 +- .../Implementation/DefaultMethod.cs | 12 ++- .../Implementation/DefaultProperty.cs | 12 ++- .../Implementation/DefaultTypeDefinition.cs | 15 ++- .../Implementation/DefaultTypeParameter.cs | 4 +- .../Implementation/GetClassTypeReference.cs | 36 +++++-- .../Implementation/ProxyTypeResolveContext.cs | 4 +- .../Implementation/SimpleProjectContent.cs | 4 +- .../TypeSystem/Implementation/TypeStorage.cs | 29 +++--- .../TypeSystem/NullableType.cs | 4 +- .../TypeSystem/ReflectionHelper.cs | 83 +++++++++-------- 21 files changed, 197 insertions(+), 171 deletions(-) diff --git a/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs b/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs index b6a6f7ea4a..67b15c78c1 100644 --- a/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs +++ b/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs @@ -27,7 +27,7 @@ namespace ICSharpCode.NRefactory.TypeSystem ITypeDefinition GetClass(Type type) { - return testCasePC.GetClass(type.FullName, type.GetGenericArguments().Length, StringComparer.Ordinal); + return testCasePC.GetClass(type); } [Test] diff --git a/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs b/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs index 720a9e0f00..66f16eb368 100644 --- a/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs @@ -373,7 +373,7 @@ namespace ICSharpCode.NRefactory.CSharp dtor.BodyRegion = MakeRegion(destructorDeclaration.Body); dtor.Accessibility = Accessibility.Protected; dtor.IsOverride = true; - dtor.ReturnType = voidReference; + dtor.ReturnType = ReflectionHelper.VoidReference; ConvertAttributes(dtor.Attributes, destructorDeclaration.Attributes); @@ -498,8 +498,6 @@ namespace ICSharpCode.NRefactory.CSharp #endregion #region Types - static readonly GetClassTypeReference voidReference = new GetClassTypeReference("System.Void", 0); - ITypeReference ConvertType(DomNode node, bool isInUsingDeclaration = false) { return ConvertType(node, currentTypeDefinition, currentMethod, usingScope, isInUsingDeclaration); @@ -555,7 +553,7 @@ namespace ICSharpCode.NRefactory.CSharp case "decimal": return TypeCode.Decimal.ToTypeReference(); case "void": - return voidReference; + return ReflectionHelper.VoidReference; default: return SharedTypes.UnknownType; } diff --git a/ICSharpCode.NRefactory/CSharp/Resolver/CSharpResolver.cs b/ICSharpCode.NRefactory/CSharp/Resolver/CSharpResolver.cs index a53c44ab6b..2854b484e5 100644 --- a/ICSharpCode.NRefactory/CSharp/Resolver/CSharpResolver.cs +++ b/ICSharpCode.NRefactory/CSharp/Resolver/CSharpResolver.cs @@ -1573,9 +1573,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver } // look in current namespace definitions for (UsingScope n = this.UsingScope; n != null; n = n.Parent) { - string fullName = NamespaceDeclaration.BuildQualifiedName(n.NamespaceName, identifier); // first look for a namespace if (k == 0) { + string fullName = NamespaceDeclaration.BuildQualifiedName(n.NamespaceName, identifier); if (context.GetNamespace(fullName, StringComparer.Ordinal) != null) { if (n.HasAlias(identifier)) return new AmbiguousTypeResolveResult(SharedTypes.UnknownType); @@ -1583,7 +1583,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver } } // then look for a type - ITypeDefinition def = context.GetClass(fullName, k, StringComparer.Ordinal); + ITypeDefinition def = context.GetClass(n.NamespaceName, identifier, k, StringComparer.Ordinal); if (def != null) { IType result = def; if (k != 0) { @@ -1617,8 +1617,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver foreach (var u in n.Usings) { NamespaceResolveResult ns = u.ResolveNamespace(context); if (ns != null) { - fullName = NamespaceDeclaration.BuildQualifiedName(ns.NamespaceName, identifier); - def = context.GetClass(fullName, k, StringComparer.Ordinal); + def = context.GetClass(ns.NamespaceName, identifier, k, StringComparer.Ordinal); if (firstResult == null) { if (k == 0) firstResult = def; @@ -1674,12 +1673,12 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver NamespaceResolveResult nrr = target as NamespaceResolveResult; if (nrr != null) { - string fullName = NamespaceDeclaration.BuildQualifiedName(nrr.NamespaceName, identifier); if (typeArguments.Count == 0) { + string fullName = NamespaceDeclaration.BuildQualifiedName(nrr.NamespaceName, identifier); if (context.GetNamespace(fullName, StringComparer.Ordinal) != null) return new NamespaceResolveResult(fullName); } - ITypeDefinition def = context.GetClass(fullName, typeArguments.Count, StringComparer.Ordinal); + ITypeDefinition def = context.GetClass(nrr.NamespaceName, identifier, typeArguments.Count, StringComparer.Ordinal); if (def != null) return new TypeResolveResult(def); return ErrorResult; diff --git a/ICSharpCode.NRefactory/CSharp/Resolver/Conversions.cs b/ICSharpCode.NRefactory/CSharp/Resolver/Conversions.cs index 3609f1c208..51ee87f298 100644 --- a/ICSharpCode.NRefactory/CSharp/Resolver/Conversions.cs +++ b/ICSharpCode.NRefactory/CSharp/Resolver/Conversions.cs @@ -194,7 +194,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver || ImplicitReferenceConversion(fromArray.ElementType, toPT.TypeArguments[0]); } // conversion from any array to System.Array and the interfaces it implements: - ITypeDefinition systemArray = context.GetClass("System.Array", 0, StringComparer.Ordinal); + ITypeDefinition systemArray = context.GetClass("System", "Array", 0, StringComparer.Ordinal); return systemArray != null && (systemArray.Equals(toType) || ImplicitReferenceConversion(systemArray, toType)); } diff --git a/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs b/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs index a67fc0dd81..4f06543df4 100644 --- a/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs @@ -718,7 +718,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver return resolver.ResolveThisReference(); } - static readonly GetClassTypeReference systemType = new GetClassTypeReference("System.Type", 0); + static readonly GetClassTypeReference systemType = new GetClassTypeReference("System", "Type", 0); public override ResolveResult VisitTypeOfExpression(TypeOfExpression typeOfExpression, object data) { diff --git a/ICSharpCode.NRefactory/TypeSystem/ArrayType.cs b/ICSharpCode.NRefactory/TypeSystem/ArrayType.cs index 801cb88bca..84d7522e9f 100644 --- a/ICSharpCode.NRefactory/TypeSystem/ArrayType.cs +++ b/ICSharpCode.NRefactory/TypeSystem/ArrayType.cs @@ -46,8 +46,8 @@ namespace ICSharpCode.NRefactory.TypeSystem return a != null && elementType.Equals(a.elementType) && a.dimensions == dimensions; } - static readonly GetClassTypeReference systemArray = new GetClassTypeReference("System.Array", 0); - static readonly GetClassTypeReference listInterface = new GetClassTypeReference("System.Collections.Generic.IList", 1); + static readonly GetClassTypeReference systemArray = new GetClassTypeReference("System", "Array", 0); + static readonly GetClassTypeReference listInterface = new GetClassTypeReference("System.Collections.Generic", "IList", 1); public override IEnumerable GetBaseTypes(ITypeResolveContext context) { diff --git a/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs b/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs index 4b27679d57..4a7ce81313 100644 --- a/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs +++ b/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs @@ -260,45 +260,33 @@ namespace ICSharpCode.NRefactory.TypeSystem } return SharedTypes.UnknownType; } + } else if (type.IsNested) { + ITypeReference typeRef = CreateType(type.DeclaringType, entity, typeAttributes, ref typeIndex); + int partTypeParameterCount; + string namepart = ReflectionHelper.SplitTypeParameterCountFromReflectionName(type.Name, out partTypeParameterCount); + return new NestedTypeReference(typeRef, namepart, partTypeParameterCount); } else { - string name = type.FullName; + string ns = type.Namespace ?? string.Empty; + string name = type.Name; if (name == null) - throw new InvalidOperationException("type.FullName returned null. Type: " + type.ToString()); + throw new InvalidOperationException("type.Name returned null. Type: " + type.ToString()); - if (name.IndexOf('/') > 0) { - string[] nameparts = name.Split('/'); - ITypeReference typeRef = GetSimpleType(nameparts[0]); - for (int i = 1; i < nameparts.Length; i++) { - int partTypeParameterCount; - string namepart = ReflectionHelper.SplitTypeParameterCountFromReflectionName(nameparts[i], out partTypeParameterCount); - typeRef = new NestedTypeReference(typeRef, namepart, partTypeParameterCount); - } - return typeRef; - } else if (name == "System.Object" && HasDynamicAttribute(typeAttributes, typeIndex)) { + if (name == "Object" && ns == "System" && HasDynamicAttribute(typeAttributes, typeIndex)) { return SharedTypes.Dynamic; } else { - return GetSimpleType(name); + int typeParameterCount; + name = ReflectionHelper.SplitTypeParameterCountFromReflectionName(name, out typeParameterCount); + var earlyBindContext = this.EarlyBindContext; + if (earlyBindContext != null) { + IType c = earlyBindContext.GetClass(ns, name, typeParameterCount, StringComparer.Ordinal); + if (c != null) + return c; + } + return new GetClassTypeReference(ns, name, typeParameterCount); } } } - /// - /// Gets a type reference for a reflection name. - /// This method does not handle nested types -- it can be only used with top-level types. - /// - ITypeReference GetSimpleType(string reflectionName) - { - int typeParameterCount; - string name = ReflectionHelper.SplitTypeParameterCountFromReflectionName(reflectionName, out typeParameterCount); - var earlyBindContext = this.EarlyBindContext; - if (earlyBindContext != null) { - IType c = earlyBindContext.GetClass(name, typeParameterCount, StringComparer.Ordinal); - if (c != null) - return c; - } - return new GetClassTypeReference(name, typeParameterCount); - } - static readonly string DynamicAttributeFullName = typeof(DynamicAttribute).FullName; static bool HasDynamicAttribute(ICustomAttributeProvider attributeProvider, int typeIndex) @@ -526,6 +514,7 @@ namespace ICSharpCode.NRefactory.TypeSystem this.typeDefinition = null; Freeze(); // freeze after initialization + ApplyInterningProvider(loader.InterningProvider); } void InitNestedTypes(CecilLoader loader) @@ -650,23 +639,18 @@ namespace ICSharpCode.NRefactory.TypeSystem } } if (typeDefinition.HasProperties) { + string defaultMemberName = null; + var defaultMemberAttribute = typeDefinition.CustomAttributes.FirstOrDefault( + a => a.AttributeType.FullName == typeof(System.Reflection.DefaultMemberAttribute).FullName); + if (defaultMemberAttribute != null && defaultMemberAttribute.ConstructorArguments.Count == 1) { + defaultMemberName = defaultMemberAttribute.ConstructorArguments[0].Value as string; + } foreach (PropertyDefinition property in typeDefinition.Properties) { bool getterVisible = property.GetMethod != null && loader.IsVisible(property.GetMethod.Attributes); bool setterVisible = property.SetMethod != null && loader.IsVisible(property.SetMethod.Attributes); if (getterVisible || setterVisible) { - this.Properties.Add(loader.ReadProperty(property, this)); - } - } - var defaultMemberAttribute = typeDefinition.CustomAttributes.FirstOrDefault( - a => a.AttributeType.FullName == typeof(System.Reflection.DefaultMemberAttribute).FullName); - if (defaultMemberAttribute != null && defaultMemberAttribute.ConstructorArguments.Count == 1) { - string defaultMemberName = defaultMemberAttribute.ConstructorArguments[0].Value as string; - if (defaultMemberName != null) { - foreach (DefaultProperty p in this.Properties) { - if (p.Name == defaultMemberName) { - p.EntityType = EntityType.Indexer; - } - } + EntityType type = property.Name == defaultMemberName ? EntityType.Indexer : EntityType.Property; + this.Properties.Add(loader.ReadProperty(property, this, type)); } } } @@ -682,7 +666,7 @@ namespace ICSharpCode.NRefactory.TypeSystem #endregion #region Read Method - IMethod ReadMethod(MethodDefinition method, ITypeDefinition parentType, EntityType methodType) + public IMethod ReadMethod(MethodDefinition method, ITypeDefinition parentType, EntityType methodType = EntityType.Method) { DefaultMethod m = new DefaultMethod(parentType, method.Name); m.EntityType = methodType; @@ -719,8 +703,7 @@ namespace ICSharpCode.NRefactory.TypeSystem } } - if (this.InterningProvider != null) - m = this.InterningProvider.Intern(m); + FinishReadMember(m); return m; } @@ -841,8 +824,7 @@ namespace ICSharpCode.NRefactory.TypeSystem f.IsVolatile = true; } - if (this.InterningProvider != null) - f = this.InterningProvider.Intern(f); + FinishReadMember(f); return f; } @@ -890,13 +872,14 @@ namespace ICSharpCode.NRefactory.TypeSystem #endregion #region Read Property - public IProperty ReadProperty(PropertyDefinition property, ITypeDefinition parentType) + public IProperty ReadProperty(PropertyDefinition property, ITypeDefinition parentType, EntityType propertyType = EntityType.Property) { if (property == null) throw new ArgumentNullException("property"); if (parentType == null) throw new ArgumentNullException("parentType"); DefaultProperty p = new DefaultProperty(parentType, property.Name); + p.EntityType = propertyType; TranslateModifiers(property.GetMethod ?? property.SetMethod, p); p.ReturnType = ReadTypeReference(property.PropertyType, typeAttributes: property, entity: p); @@ -910,8 +893,7 @@ namespace ICSharpCode.NRefactory.TypeSystem } AddAttributes(property, p); - if (this.InterningProvider != null) - p = this.InterningProvider.Intern(p); + FinishReadMember(p); return p; } @@ -951,10 +933,15 @@ namespace ICSharpCode.NRefactory.TypeSystem AddAttributes(ev, e); - if (this.InterningProvider != null) - e = this.InterningProvider.Intern(e); + FinishReadMember(e); return e; } #endregion + + void FinishReadMember(AbstractMember member) + { + member.Freeze(); + member.ApplyInterningProvider(this.InterningProvider); + } } } diff --git a/ICSharpCode.NRefactory/TypeSystem/ITypeResolveContext.cs b/ICSharpCode.NRefactory/TypeSystem/ITypeResolveContext.cs index 3b133fb163..335153ee6c 100644 --- a/ICSharpCode.NRefactory/TypeSystem/ITypeResolveContext.cs +++ b/ICSharpCode.NRefactory/TypeSystem/ITypeResolveContext.cs @@ -18,12 +18,13 @@ namespace ICSharpCode.NRefactory.TypeSystem /// /// Retrieves a class. /// - /// Full name of the class + /// Namespace that contains the class + /// Name of the class /// Number of type parameters /// Language-specific rules for how class names are compared /// The type definition for the class; or null if no such class exists. /// This method never returns inner classes; it can be used only with top-level classes. - ITypeDefinition GetClass(string fullTypeName, int typeParameterCount, StringComparer nameComparer); + ITypeDefinition GetClass(string nameSpace, string name, int typeParameterCount, StringComparer nameComparer); /// /// Retrieves all top-level classes. @@ -93,9 +94,10 @@ namespace ICSharpCode.NRefactory.TypeSystem [ContractClassFor(typeof(ITypeResolveContext))] abstract class ITypeResolveContextContract : ITypeResolveContext { - ITypeDefinition ITypeResolveContext.GetClass(string fullTypeName, int typeParameterCount, StringComparer nameComparer) + ITypeDefinition ITypeResolveContext.GetClass(string nameSpace, string name, int typeParameterCount, StringComparer nameComparer) { - Contract.Requires(fullTypeName != null); + Contract.Requires(nameSpace != null); + Contract.Requires(name != null); Contract.Requires(typeParameterCount >= 0); Contract.Requires(nameComparer != null); return null; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractMember.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractMember.cs index cf4a3b48cd..a514e20a8c 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractMember.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractMember.cs @@ -10,7 +10,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation /// /// Base class for implementations. /// - public abstract class AbstractMember : AbstractFreezable, IMember, ISupportsInterning + public abstract class AbstractMember : AbstractFreezable, IMember { // possible optimizations to reduce the memory usage of AbstractMember: // - put 'bool isFrozen' into flags @@ -260,22 +260,14 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation return "[" + EntityType + " " + ReflectionName + ":" + ReturnType + "]"; } - public virtual void PrepareForInterning(IInterningProvider provider) + public virtual void ApplyInterningProvider(IInterningProvider provider) { - returnType = provider.Intern(returnType); - attributes = provider.InternList(attributes); - interfaceImplementations = provider.InternList(interfaceImplementations); - name = provider.Intern(name); - } - - int ISupportsInterning.GetHashCodeForInterning() - { - return GetHashCode(); - } - - bool ISupportsInterning.EqualsForInterning(ISupportsInterning other) - { - return this == other; + if (provider != null) { + returnType = provider.Intern(returnType); + attributes = provider.InternList(attributes); + interfaceImplementations = provider.InternList(interfaceImplementations); + name = provider.Intern(name); + } } } } diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/CompositeTypeResolveContext.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/CompositeTypeResolveContext.cs index 19acf917f4..3d0ca71a1f 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/CompositeTypeResolveContext.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/CompositeTypeResolveContext.cs @@ -52,10 +52,10 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation } /// - public ITypeDefinition GetClass(string fullTypeName, int typeParameterCount, StringComparer nameComparer) + public ITypeDefinition GetClass(string nameSpace, string name, int typeParameterCount, StringComparer nameComparer) { foreach (ITypeResolveContext context in children) { - ITypeDefinition d = context.GetClass(fullTypeName, typeParameterCount, nameComparer); + ITypeDefinition d = context.GetClass(nameSpace, name, typeParameterCount, nameComparer); if (d != null) return d; } diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultField.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultField.cs index 07e360e77d..690dba3e59 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultField.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultField.cs @@ -34,10 +34,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation this.IsVolatile = f.IsVolatile; } - public override void PrepareForInterning(IInterningProvider provider) + public override void ApplyInterningProvider(IInterningProvider provider) { - base.PrepareForInterning(provider); - constantValue = provider.Intern(constantValue); + base.ApplyInterningProvider(provider); + if (provider != null) + constantValue = provider.Intern(constantValue); } public bool IsConst { diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultMethod.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultMethod.cs index 7b47b6bc46..7ed3cab395 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultMethod.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultMethod.cs @@ -42,12 +42,14 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation this.IsExtensionMethod = method.IsExtensionMethod; } - public override void PrepareForInterning(IInterningProvider provider) + public override void ApplyInterningProvider(IInterningProvider provider) { - base.PrepareForInterning(provider); - returnTypeAttributes = provider.InternList(returnTypeAttributes); - typeParameters = provider.InternList(typeParameters); - parameters = provider.InternList(parameters); + base.ApplyInterningProvider(provider); + if (provider != null) { + returnTypeAttributes = provider.InternList(returnTypeAttributes); + typeParameters = provider.InternList(typeParameters); + parameters = provider.InternList(parameters); + } } public IList ReturnTypeAttributes { diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultProperty.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultProperty.cs index 4e1945d31f..558dc6eb35 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultProperty.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultProperty.cs @@ -34,12 +34,14 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation this.parameters = CopyList(p.Parameters); } - public override void PrepareForInterning(IInterningProvider provider) + public override void ApplyInterningProvider(IInterningProvider provider) { - base.PrepareForInterning(provider); - getter = provider.Intern(getter); - setter = provider.Intern(setter); - parameters = provider.InternList(parameters); + base.ApplyInterningProvider(provider); + if (provider != null) { + getter = provider.Intern(getter); + setter = provider.Intern(setter); + parameters = provider.InternList(parameters); + } } public bool IsIndexer { diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs index d70429b3a2..4964cacc4e 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs @@ -16,8 +16,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation readonly IProjectContent projectContent; readonly ITypeDefinition declaringTypeDefinition; - readonly string ns; - readonly string name; + string ns; + string name; IList baseTypes; IList typeParameters; @@ -103,6 +103,17 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation } } + public void ApplyInterningProvider(IInterningProvider provider) + { + if (provider != null) { + ns = provider.Intern(ns); + name = provider.Intern(name); + baseTypes = provider.InternList(baseTypes); + typeParameters = provider.InternList(typeParameters); + attributes = provider.InternList(attributes); + } + } + public IList TypeParameters { get { if (typeParameters == null) diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeParameter.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeParameter.cs index a31b79b347..cd9338213d 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeParameter.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeParameter.cs @@ -244,7 +244,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation public IEnumerable GetMethods(ITypeResolveContext context, Predicate filter = null) { // TODO: get methods from constraints - IType objectType = context.GetClass("System.Object", 0, StringComparer.Ordinal); + IType objectType = context.GetClass("System", "Object", 0, StringComparer.Ordinal); IEnumerable objectMethods; if (objectType != null) objectMethods = objectType.GetMethods(context, filter); @@ -277,7 +277,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation public IEnumerable GetBaseTypes(ITypeResolveContext context) { - IType defaultBaseType = context.GetClass(HasValueTypeConstraint ? "System.ValueType" : "System.Object", 0, StringComparer.Ordinal); + IType defaultBaseType = context.GetClass("System", HasValueTypeConstraint ? "ValueType" : "Object", 0, StringComparer.Ordinal); if (defaultBaseType != null) yield return defaultBaseType; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs index 02411fd270..8f2b704418 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs @@ -2,6 +2,7 @@ // This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; +using ICSharpCode.NRefactory.CSharp; using ICSharpCode.NRefactory.Utils; namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -11,15 +12,33 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation /// public sealed class GetClassTypeReference : ITypeReference, ISupportsInterning { - string fullTypeName; + string nameSpace, name; int typeParameterCount; //volatile CachedResult v_cachedResult; + public GetClassTypeReference(string nameSpace, string name, int typeParameterCount) + { + if (nameSpace == null) + throw new ArgumentNullException("nameSpace"); + if (name == null) + throw new ArgumentNullException("name"); + this.nameSpace = nameSpace; + this.name = name; + this.typeParameterCount = typeParameterCount; + } + public GetClassTypeReference(string fullTypeName, int typeParameterCount) { if (fullTypeName == null) throw new ArgumentNullException("fullTypeName"); - this.fullTypeName = fullTypeName; + int pos = fullTypeName.LastIndexOf('.'); + if (pos < 0) { + nameSpace = string.Empty; + name = fullTypeName; + } else { + nameSpace = fullTypeName.Substring(0, pos); + name = fullTypeName.Substring(pos + 1); + } this.typeParameterCount = typeParameterCount; } @@ -64,31 +83,32 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation IType DoResolve(ITypeResolveContext context) { */ - return context.GetClass(fullTypeName, typeParameterCount, StringComparer.Ordinal) ?? SharedTypes.UnknownType; + return context.GetClass(nameSpace, name, typeParameterCount, StringComparer.Ordinal) ?? SharedTypes.UnknownType; } public override string ToString() { if (typeParameterCount == 0) - return fullTypeName; + return NamespaceDeclaration.BuildQualifiedName(nameSpace, name); else - return fullTypeName + "`" + typeParameterCount; + return NamespaceDeclaration.BuildQualifiedName(nameSpace, name) + "`" + typeParameterCount; } void ISupportsInterning.PrepareForInterning(IInterningProvider provider) { - fullTypeName = provider.Intern(fullTypeName); + nameSpace = provider.Intern(nameSpace); + name = provider.Intern(name); } int ISupportsInterning.GetHashCodeForInterning() { - return fullTypeName.GetHashCode() ^ typeParameterCount; + return nameSpace.GetHashCode() ^ name.GetHashCode() ^ typeParameterCount; } bool ISupportsInterning.EqualsForInterning(ISupportsInterning other) { GetClassTypeReference o = other as GetClassTypeReference; - return o != null && fullTypeName == o.fullTypeName && typeParameterCount == o.typeParameterCount; + return o != null && name == o.name && nameSpace == o.nameSpace && typeParameterCount == o.typeParameterCount; } } } diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs index 7d8f5bf97c..f8677c2969 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs @@ -25,9 +25,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation } /// - public virtual ITypeDefinition GetClass(string fullTypeName, int typeParameterCount, StringComparer nameComparer) + public virtual ITypeDefinition GetClass(string nameSpace, string name, int typeParameterCount, StringComparer nameComparer) { - return target.GetClass(fullTypeName, typeParameterCount, nameComparer); + return target.GetClass(nameSpace, name, typeParameterCount, nameComparer); } /// diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs index 1e90e3c602..3a7f5caa90 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs @@ -112,11 +112,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation #endregion #region IProjectContent implementation - public ITypeDefinition GetClass(string fullTypeName, int typeParameterCount, StringComparer nameComparer) + public ITypeDefinition GetClass(string nameSpace, string name, int typeParameterCount, StringComparer nameComparer) { readerWriterLock.EnterReadLock(); try { - return types.GetClass(fullTypeName, typeParameterCount, nameComparer); + return types.GetClass(nameSpace, name, typeParameterCount, nameComparer); } finally { readerWriterLock.ExitReadLock(); } diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs index 6a43494532..aa05ee8d75 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs @@ -21,12 +21,14 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation #region FullNameAndTypeParameterCount struct FullNameAndTypeParameterCount { - public readonly string FullName; + public readonly string Namespace; + public readonly string Name; public readonly int TypeParameterCount; - public FullNameAndTypeParameterCount(string fullName, int typeParameterCount) + public FullNameAndTypeParameterCount(string nameSpace, string name, int typeParameterCount) { - this.FullName = fullName; + this.Namespace = nameSpace; + this.Name = name; this.TypeParameterCount = typeParameterCount; } } @@ -44,12 +46,14 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation public bool Equals(FullNameAndTypeParameterCount x, FullNameAndTypeParameterCount y) { - return x.TypeParameterCount == y.TypeParameterCount && NameComparer.Equals(x.FullName, y.FullName); + return x.TypeParameterCount == y.TypeParameterCount + && NameComparer.Equals(x.Name, y.Name) + && NameComparer.Equals(x.Namespace, y.Namespace); } public int GetHashCode(FullNameAndTypeParameterCount obj) { - return NameComparer.GetHashCode(obj.FullName); + return NameComparer.GetHashCode(obj.Name) ^ NameComparer.GetHashCode(obj.Namespace) ^ obj.TypeParameterCount; } } #endregion @@ -157,13 +161,16 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation #region ITypeResolveContext implementation /// - public ITypeDefinition GetClass(string fullTypeName, int typeParameterCount, StringComparer nameComparer) + public ITypeDefinition GetClass(string nameSpace, string name, int typeParameterCount, StringComparer nameComparer) { - if (fullTypeName == null) - throw new ArgumentNullException("fullTypeName"); + if (nameSpace == null) + throw new ArgumentNullException("nameSpace"); + if (name == null) + throw new ArgumentNullException("name"); if (nameComparer == null) throw new ArgumentNullException("nameComparer"); - var key = new FullNameAndTypeParameterCount(fullTypeName, typeParameterCount); + + var key = new FullNameAndTypeParameterCount(nameSpace, name, typeParameterCount); ITypeDefinition result; if (GetTypeDictionary(nameComparer).TryGetValue(key, out result)) return result; @@ -235,7 +242,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation { if (typeDefinition == null) throw new ArgumentNullException("typeDefinition"); - var key = new FullNameAndTypeParameterCount(typeDefinition.FullName, typeDefinition.TypeParameterCount); + var key = new FullNameAndTypeParameterCount(typeDefinition.Namespace, typeDefinition.Name, typeDefinition.TypeParameterCount); bool wasRemoved = false; foreach (var dict in _typeDicts) { ITypeDefinition defInDict; @@ -267,7 +274,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation { if (typeDefinition == null) throw new ArgumentNullException("typeDefinition"); - var key = new FullNameAndTypeParameterCount(typeDefinition.FullName, typeDefinition.TypeParameterCount); + var key = new FullNameAndTypeParameterCount(typeDefinition.Namespace, typeDefinition.Name, typeDefinition.TypeParameterCount); bool isNew = !_typeDicts[0].ContainsKey(key); foreach (var dict in _typeDicts) { dict[key] = typeDefinition; diff --git a/ICSharpCode.NRefactory/TypeSystem/NullableType.cs b/ICSharpCode.NRefactory/TypeSystem/NullableType.cs index 0c7df02a91..6f2fd7165a 100644 --- a/ICSharpCode.NRefactory/TypeSystem/NullableType.cs +++ b/ICSharpCode.NRefactory/TypeSystem/NullableType.cs @@ -47,14 +47,14 @@ namespace ICSharpCode.NRefactory.TypeSystem if (context == null) throw new ArgumentNullException("context"); - ITypeDefinition nullable = context.GetClass("System.Nullable", 1, StringComparer.Ordinal); + ITypeDefinition nullable = context.GetClass("System", "Nullable", 1, StringComparer.Ordinal); if (nullable != null) return new ParameterizedType(nullable, new [] { elementType }); else return SharedTypes.UnknownType; } - static readonly ITypeReference NullableReference = new GetClassTypeReference("System.Nullable", 1); + static readonly ITypeReference NullableReference = new GetClassTypeReference("System", "Nullable", 1); /// /// Creates a nullable type reference. diff --git a/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs b/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs index 7c42f8266a..41a6fb52d1 100644 --- a/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs +++ b/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs @@ -53,8 +53,8 @@ namespace ICSharpCode.NRefactory.TypeSystem return null; } else { int typeParameterCount; - string name = SplitTypeParameterCountFromReflectionName(type.FullName, out typeParameterCount); - return context.GetClass(name, typeParameterCount, StringComparer.Ordinal); + string name = SplitTypeParameterCountFromReflectionName(type.Name, out typeParameterCount); + return context.GetClass(type.Namespace, name, typeParameterCount, StringComparer.Ordinal); } } #endregion @@ -113,8 +113,8 @@ namespace ICSharpCode.NRefactory.TypeSystem return new NestedTypeReference(baseTypeRef, name, typeParameterCount); } else { int typeParameterCount; - string name = SplitTypeParameterCountFromReflectionName(type.FullName, out typeParameterCount); - return new GetClassTypeReference(name, typeParameterCount); + string name = SplitTypeParameterCountFromReflectionName(type.Name, out typeParameterCount); + return new GetClassTypeReference(type.Namespace, name, typeParameterCount); } } #endregion @@ -155,26 +155,31 @@ namespace ICSharpCode.NRefactory.TypeSystem #endregion #region TypeCode.ToTypeReference() + /// + /// Gets a type reference pointing to the void type. + /// + public static readonly ITypeReference VoidReference = new GetClassTypeReference("System", "Void", 0); + static readonly ITypeReference[] primitiveTypeReferences = { SharedTypes.UnknownType, // TypeCode.Empty - new GetClassTypeReference("System.Object", 0), - new GetClassTypeReference("System.DBNull", 0), - new GetClassTypeReference("System.Boolean", 0), - new GetClassTypeReference("System.Char", 0), - new GetClassTypeReference("System.SByte", 0), - new GetClassTypeReference("System.Byte", 0), - new GetClassTypeReference("System.Int16", 0), - new GetClassTypeReference("System.UInt16", 0), - new GetClassTypeReference("System.Int32", 0), - new GetClassTypeReference("System.UInt32", 0), - new GetClassTypeReference("System.Int64", 0), - new GetClassTypeReference("System.UInt64", 0), - new GetClassTypeReference("System.Single", 0), - new GetClassTypeReference("System.Double", 0), - new GetClassTypeReference("System.Decimal", 0), - new GetClassTypeReference("System.DateTime", 0), + new GetClassTypeReference("System", "Object", 0), + new GetClassTypeReference("System", "DBNull", 0), + new GetClassTypeReference("System", "Boolean", 0), + new GetClassTypeReference("System", "Char", 0), + new GetClassTypeReference("System", "SByte", 0), + new GetClassTypeReference("System", "Byte", 0), + new GetClassTypeReference("System", "Int16", 0), + new GetClassTypeReference("System", "UInt16", 0), + new GetClassTypeReference("System", "Int32", 0), + new GetClassTypeReference("System", "UInt32", 0), + new GetClassTypeReference("System", "Int64", 0), + new GetClassTypeReference("System", "UInt64", 0), + new GetClassTypeReference("System", "Single", 0), + new GetClassTypeReference("System", "Double", 0), + new GetClassTypeReference("System", "Decimal", 0), + new GetClassTypeReference("System", "DateTime", 0), SharedTypes.UnknownType, // (TypeCode)17 has no enum value? - new GetClassTypeReference("System.String", 0) + new GetClassTypeReference("System", "String", 0) }; /// @@ -190,23 +195,23 @@ namespace ICSharpCode.NRefactory.TypeSystem #region GetTypeCode static readonly Dictionary typeNameToCodeDict = new Dictionary { - { "System.Object", TypeCode.Object }, - { "System.DBNull", TypeCode.DBNull }, - { "System.Boolean", TypeCode.Boolean }, - { "System.Char", TypeCode.Char }, - { "System.SByte", TypeCode.SByte }, - { "System.Byte", TypeCode.Byte }, - { "System.Int16", TypeCode.Int16 }, - { "System.UInt16", TypeCode.UInt16 }, - { "System.Int32", TypeCode.Int32 }, - { "System.UInt32", TypeCode.UInt32 }, - { "System.Int64", TypeCode.Int64 }, - { "System.UInt64", TypeCode.UInt64 }, - { "System.Single", TypeCode.Single }, - { "System.Double", TypeCode.Double }, - { "System.Decimal", TypeCode.Decimal }, - { "System.DateTime", TypeCode.DateTime }, - { "System.String", TypeCode.String } + { "Object", TypeCode.Object }, + { "DBNull", TypeCode.DBNull }, + { "Boolean", TypeCode.Boolean }, + { "Char", TypeCode.Char }, + { "SByte", TypeCode.SByte }, + { "Byte", TypeCode.Byte }, + { "Int16", TypeCode.Int16 }, + { "UInt16", TypeCode.UInt16 }, + { "Int32", TypeCode.Int32 }, + { "UInt32", TypeCode.UInt32 }, + { "Int64", TypeCode.Int64 }, + { "UInt64", TypeCode.UInt64 }, + { "Single", TypeCode.Single }, + { "Double", TypeCode.Double }, + { "Decimal", TypeCode.Decimal }, + { "DateTime", TypeCode.DateTime }, + { "String", TypeCode.String } }; /// @@ -216,7 +221,7 @@ namespace ICSharpCode.NRefactory.TypeSystem { ITypeDefinition def = type as ITypeDefinition; TypeCode typeCode; - if (def != null && def.TypeParameterCount == 0 && typeNameToCodeDict.TryGetValue(def.FullName, out typeCode)) + if (def != null && def.TypeParameterCount == 0 && def.Namespace == "System" && typeNameToCodeDict.TryGetValue(def.Name, out typeCode)) return typeCode; else return TypeCode.Empty;