From 17c1eeaf2b65ac1b3f02fe5ba73b50078cbd77aa Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 8 May 2013 22:13:36 +0200 Subject: [PATCH] Merge changes from SharpDevelop repository to NRefactory. --- .gitignore | 1 + .../OutputVisitor/CSharpAmbience.cs | 5 + .../Resolver/MemberLookup.cs | 2 +- .../TypeSystem/IAmbience.cs | 1 + .../DefaultResolvedTypeDefinition.cs | 4 +- .../TypeSystem/TypeSystemExtensions.cs | 129 +++++++++++++++++- NRefactory.suo | Bin 3072 -> 0 bytes 7 files changed, 138 insertions(+), 4 deletions(-) delete mode 100644 NRefactory.suo diff --git a/.gitignore b/.gitignore index 0031dcbe29..813e7d15cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ bin obj +*.suo /lib/*.dll /ICSharpCode.NRefactory.Tests/PartCover/* _ReSharper*/* diff --git a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpAmbience.cs b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpAmbience.cs index 1cd38fc354..043461bd7c 100644 --- a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpAmbience.cs +++ b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpAmbience.cs @@ -267,6 +267,11 @@ namespace ICSharpCode.NRefactory.CSharp astType.AcceptVisitor(new CSharpOutputVisitor(formatter, formattingPolicy)); } + public string ConvertConstantValue(object constantValue) + { + return CSharpOutputVisitor.PrintPrimitiveValue(constantValue); + } + public string WrapComment(string comment) { return "// " + comment; diff --git a/ICSharpCode.NRefactory.CSharp/Resolver/MemberLookup.cs b/ICSharpCode.NRefactory.CSharp/Resolver/MemberLookup.cs index d87982b719..9d5d989696 100644 --- a/ICSharpCode.NRefactory.CSharp/Resolver/MemberLookup.cs +++ b/ICSharpCode.NRefactory.CSharp/Resolver/MemberLookup.cs @@ -83,7 +83,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver /// /// Whether protected access to instance members is allowed. /// True if the type of the reference is derived from the current class. - /// Protected static members may be accessibe even if false is passed for this parameter. + /// Protected static members may be accessible even if false is passed for this parameter. /// public bool IsAccessible(IEntity entity, bool allowProtectedAccess) { diff --git a/ICSharpCode.NRefactory/TypeSystem/IAmbience.cs b/ICSharpCode.NRefactory/TypeSystem/IAmbience.cs index 779d4c7614..1aa9d299cb 100644 --- a/ICSharpCode.NRefactory/TypeSystem/IAmbience.cs +++ b/ICSharpCode.NRefactory/TypeSystem/IAmbience.cs @@ -89,6 +89,7 @@ namespace ICSharpCode.NRefactory.TypeSystem string ConvertEntity(IEntity entity); string ConvertType(IType type); string ConvertVariable(IVariable variable); + string ConvertConstantValue(object constantValue); string WrapComment(string comment); } diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs index c60238d940..799d388920 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs @@ -395,9 +395,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation KnownTypeCode result = this.knownTypeCode; if (result == (KnownTypeCode)(-1)) { result = KnownTypeCode.None; + ICompilation compilation = this.Compilation; for (int i = 0; i < KnownTypeReference.KnownTypeCodeCount; i++) { - KnownTypeReference r = KnownTypeReference.Get((KnownTypeCode)i); - if (r != null && r.Resolve(parentContext) == this) { + if (compilation.FindType((KnownTypeCode)i) == this) { result = (KnownTypeCode)i; break; } diff --git a/ICSharpCode.NRefactory/TypeSystem/TypeSystemExtensions.cs b/ICSharpCode.NRefactory/TypeSystem/TypeSystemExtensions.cs index 03a22d2f34..0ab3f22c2b 100644 --- a/ICSharpCode.NRefactory/TypeSystem/TypeSystemExtensions.cs +++ b/ICSharpCode.NRefactory/TypeSystem/TypeSystemExtensions.cs @@ -85,11 +85,27 @@ namespace ICSharpCode.NRefactory.TypeSystem /// public static bool IsDerivedFrom(this ITypeDefinition type, ITypeDefinition baseType) { + if (type == null) + throw new ArgumentNullException("type"); + if (baseType == null) + return false; if (type.Compilation != baseType.Compilation) { throw new InvalidOperationException("Both arguments to IsDerivedFrom() must be from the same compilation."); } return type.GetAllBaseTypeDefinitions().Contains(baseType); } + + /// + /// Gets whether this type definition is derived from a given known type. + /// + public static bool IsDerivedFrom(this ITypeDefinition type, KnownTypeCode baseType) + { + if (type == null) + throw new ArgumentNullException("type"); + if (baseType == KnownTypeCode.None) + return false; + return IsDerivedFrom(type, type.Compilation.FindType(baseType).GetDefinition()); + } #endregion #region IsOpen / IsUnbound / IsKnownType @@ -535,7 +551,118 @@ namespace ICSharpCode.NRefactory.TypeSystem return reference.Resolve (compilation.TypeResolveContext); } #endregion - + + #region ITypeDefinition.GetAttribute + /// + /// Gets the attribute of the specified attribute type (or derived attribute types). + /// + /// The entity on which the attributes are declared. + /// The attribute type to look for. + /// + /// Specifies whether attributes inherited from base classes and base members (if the given in an override) + /// should be returned. The default is true. + /// + /// + /// Returns the attribute that was found; or null if none was found. + /// If inherit is true, an from the entity itself will be returned if possible; + /// and the base entity will only be searched if none exists. + /// + public static IAttribute GetAttribute(this IEntity entity, IType attributeType, bool inherit = true) + { + return GetAttributes(entity, attributeType, inherit).FirstOrDefault(); + } + + /// + /// Gets the attributes of the specified attribute type (or derived attribute types). + /// + /// The entity on which the attributes are declared. + /// The attribute type to look for. + /// + /// Specifies whether attributes inherited from base classes and base members (if the given in an override) + /// should be returned. The default is true. + /// + /// + /// Returns the list of attributes that were found. + /// If inherit is true, attributes from the entity itself are returned first; followed by attributes inherited from the base entity. + /// + public static IEnumerable GetAttributes(this IEntity entity, IType attributeType, bool inherit = true) + { + if (entity == null) + throw new ArgumentNullException("entity"); + if (attributeType == null) + throw new ArgumentNullException("attributeType"); + return GetAttributes(entity, attributeType.Equals, inherit); + } + + /// + /// Gets the attribute of the specified attribute type (or derived attribute types). + /// + /// The entity on which the attributes are declared. + /// The attribute type to look for. + /// + /// Specifies whether attributes inherited from base classes and base members (if the given in an override) + /// should be returned. The default is true. + /// + /// + /// Returns the attribute that was found; or null if none was found. + /// If inherit is true, an from the entity itself will be returned if possible; + /// and the base entity will only be searched if none exists. + /// + public static IAttribute GetAttribute(this IEntity entity, FullTypeName attributeType, bool inherit = true) + { + return GetAttributes(entity, attributeType, inherit).FirstOrDefault(); + } + + /// + /// Gets the attributes of the specified attribute type (or derived attribute types). + /// + /// The entity on which the attributes are declared. + /// The attribute type to look for. + /// + /// Specifies whether attributes inherited from base classes and base members (if the given in an override) + /// should be returned. The default is true. + /// + /// + /// Returns the list of attributes that were found. + /// If inherit is true, attributes from the entity itself are returned first; followed by attributes inherited from the base entity. + /// + public static IEnumerable GetAttributes(this IEntity entity, FullTypeName attributeType, bool inherit = true) + { + if (entity == null) + throw new ArgumentNullException("entity"); + return GetAttributes(entity, attrType => { + ITypeDefinition typeDef = attrType.GetDefinition(); + return typeDef != null && typeDef.FullTypeName == attributeType; + }, inherit); + } + + static IEnumerable GetAttributes(IEntity entity, Predicate attributeTypePredicate, bool inherit) + { + if (!inherit) { + foreach (var attr in entity.Attributes) { + if (attributeTypePredicate(attr.AttributeType)) + yield return attr; + } + yield break; + } + ITypeDefinition typeDef = entity as ITypeDefinition; + if (typeDef != null) { + foreach (var baseType in typeDef.GetNonInterfaceBaseTypes().Reverse()) { + ITypeDefinition baseTypeDef = baseType.GetDefinition(); + if (baseTypeDef == null) + continue; + foreach (var attr in baseTypeDef.Attributes) { + if (attributeTypePredicate(attr.AttributeType)) + yield return attr; + } + } + } + IMember member = entity as IMember; + if (member != null) + throw new NotImplementedException(); + throw new NotSupportedException("Unknown entity type"); + } + #endregion #region IAssembly.GetTypeDefinition(string,string,int) /// diff --git a/NRefactory.suo b/NRefactory.suo deleted file mode 100644 index cb425d482667ddc550fd81d6ade9dc6610b411db..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3072 zcmca`Uhu)fjZzO8(10BSGsD0CoD6J8;*3Bx2!nwD0|OI~0pkDr|NlQkkbwcn90fxt z1pWfu3W`4vW`V}#5OV&gQz;CAAclM(EMZV!aAn8?(nSoFu&hiJEylnK&-2U#bS!9I zz2&F#IYyAW1E9hK2m*jM<}+k5cmi!t1F~EhDj*U>l48J82ssFd)W!fTzQDkrp%m!r z9EMDw{UDsq5Dere0&yzXEhYpER3XEDP?>25El3I3Z%o*JMvzgU-~@#O0p(z6P?;_T zwI7u4L3%-P0?PlaKnxON2htos%n8I?P%)7CqhNT20QVF@*|d+0ZcQJwlUoaee*>eM zBrt=?nZb%7h9Q)p7?@*47sx9E(r!Q(rvgiY6bw@;8Ibj%yBT5!vI;$*EyWDUyH!Gr)$SH-TM|8W{-a_E zP;Np`I}8k87zZ}|LV$iQ28KQ;-jKtcx?zT}1rj!L8@VoiI(+n2`8D%v^0~VtK&1|5 N*pQSEM-`J50sw)RZxjFk