Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1 | //===-- DeclarationName.cpp - Declaration names implementation --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the DeclarationName and DeclarationNameTable |
| 11 | // classes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/Decl.h" |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclarationName.h" |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 17 | #include "clang/AST/Type.h" |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeOrdering.h" |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 370187c | 2009-04-22 21:45:53 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/FoldingSet.h" |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | namespace clang { |
| 27 | /// CXXSpecialName - Records the type associated with one of the |
| 28 | /// "special" kinds of declaration names in C++, e.g., constructors, |
| 29 | /// destructors, and conversion functions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | class CXXSpecialName |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 31 | : public DeclarationNameExtra, public llvm::FoldingSetNode { |
| 32 | public: |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 33 | /// Type - The type associated with this declaration name. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 34 | QualType Type; |
| 35 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 36 | /// FETokenInfo - Extra information associated with this declaration |
| 37 | /// name that can be used by the front end. |
| 38 | void *FETokenInfo; |
| 39 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 40 | void Profile(llvm::FoldingSetNodeID &ID) { |
| 41 | ID.AddInteger(ExtraKindOrNumArgs); |
| 42 | ID.AddPointer(Type.getAsOpaquePtr()); |
| 43 | } |
| 44 | }; |
| 45 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 46 | /// CXXOperatorIdName - Contains extra information for the name of an |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | /// overloaded operator in C++, such as "operator+. |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 48 | class CXXOperatorIdName : public DeclarationNameExtra { |
| 49 | public: |
| 50 | /// FETokenInfo - Extra information associated with this operator |
| 51 | /// name that can be used by the front end. |
| 52 | void *FETokenInfo; |
| 53 | }; |
| 54 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 55 | /// CXXLiberalOperatorName - Contains the actual identifier that makes up the |
| 56 | /// name. |
| 57 | /// |
| 58 | /// This identifier is stored here rather than directly in DeclarationName so as |
| 59 | /// to allow Objective-C selectors, which are about a million times more common, |
| 60 | /// to consume minimal memory. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 61 | class CXXLiteralOperatorIdName |
| 62 | : public DeclarationNameExtra, public llvm::FoldingSetNode { |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 63 | public: |
| 64 | IdentifierInfo *ID; |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 65 | |
| 66 | void Profile(llvm::FoldingSetNodeID &FSID) { |
| 67 | FSID.AddPointer(ID); |
| 68 | } |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 71 | static int compareInt(unsigned A, unsigned B) { |
| 72 | return (A < B ? -1 : (A > B ? 1 : 0)); |
| 73 | } |
| 74 | |
| 75 | int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) { |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 76 | if (LHS.getNameKind() != RHS.getNameKind()) |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 77 | return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1); |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 78 | |
| 79 | switch (LHS.getNameKind()) { |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 80 | case DeclarationName::Identifier: { |
| 81 | IdentifierInfo *LII = LHS.getAsIdentifierInfo(); |
| 82 | IdentifierInfo *RII = RHS.getAsIdentifierInfo(); |
| 83 | if (!LII) return RII ? -1 : 0; |
| 84 | if (!RII) return 1; |
| 85 | |
| 86 | return LII->getName().compare(RII->getName()); |
| 87 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 88 | |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 89 | case DeclarationName::ObjCZeroArgSelector: |
| 90 | case DeclarationName::ObjCOneArgSelector: |
| 91 | case DeclarationName::ObjCMultiArgSelector: { |
| 92 | Selector LHSSelector = LHS.getObjCSelector(); |
| 93 | Selector RHSSelector = RHS.getObjCSelector(); |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 94 | unsigned LN = LHSSelector.getNumArgs(), RN = RHSSelector.getNumArgs(); |
| 95 | for (unsigned I = 0, N = std::min(LN, RN); I != N; ++I) { |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 96 | IdentifierInfo *LHSId = LHSSelector.getIdentifierInfoForSlot(I); |
| 97 | IdentifierInfo *RHSId = RHSSelector.getIdentifierInfoForSlot(I); |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 98 | |
| 99 | switch (LHSId->getName().compare(RHSId->getName())) { |
| 100 | case -1: return true; |
| 101 | case 1: return false; |
| 102 | default: break; |
| 103 | } |
| 104 | } |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 105 | |
| 106 | return compareInt(LN, RN); |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | case DeclarationName::CXXConstructorName: |
| 110 | case DeclarationName::CXXDestructorName: |
| 111 | case DeclarationName::CXXConversionFunctionName: |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 112 | if (QualTypeOrdering()(LHS.getCXXNameType(), RHS.getCXXNameType())) |
| 113 | return -1; |
| 114 | if (QualTypeOrdering()(RHS.getCXXNameType(), LHS.getCXXNameType())) |
| 115 | return 1; |
| 116 | return 0; |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 117 | |
| 118 | case DeclarationName::CXXOperatorName: |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 119 | return compareInt(LHS.getCXXOverloadedOperator(), |
| 120 | RHS.getCXXOverloadedOperator()); |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 121 | |
| 122 | case DeclarationName::CXXLiteralOperatorName: |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 123 | return LHS.getCXXLiteralIdentifier()->getName().compare( |
| 124 | RHS.getCXXLiteralIdentifier()->getName()); |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 125 | |
| 126 | case DeclarationName::CXXUsingDirective: |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 127 | return 0; |
Douglas Gregor | d6b5f13 | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 128 | } |
| 129 | |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 130 | return 0; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } // end namespace clang |
| 134 | |
| 135 | DeclarationName::DeclarationName(Selector Sel) { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 136 | if (!Sel.getAsOpaquePtr()) { |
Douglas Gregor | 813a97b | 2009-10-17 17:25:45 +0000 | [diff] [blame] | 137 | Ptr = 0; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 141 | switch (Sel.getNumArgs()) { |
| 142 | case 0: |
| 143 | Ptr = reinterpret_cast<uintptr_t>(Sel.getAsIdentifierInfo()); |
Ted Kremenek | 3eb8dd7 | 2009-03-14 00:27:40 +0000 | [diff] [blame] | 144 | assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 145 | Ptr |= StoredObjCZeroArgSelector; |
| 146 | break; |
| 147 | |
| 148 | case 1: |
| 149 | Ptr = reinterpret_cast<uintptr_t>(Sel.getAsIdentifierInfo()); |
Ted Kremenek | 3eb8dd7 | 2009-03-14 00:27:40 +0000 | [diff] [blame] | 150 | assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 151 | Ptr |= StoredObjCOneArgSelector; |
| 152 | break; |
| 153 | |
| 154 | default: |
| 155 | Ptr = Sel.InfoPtr & ~Selector::ArgFlags; |
Ted Kremenek | 3eb8dd7 | 2009-03-14 00:27:40 +0000 | [diff] [blame] | 156 | assert((Ptr & PtrMask) == 0 && "Improperly aligned MultiKeywordSelector"); |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 157 | Ptr |= StoredDeclarationNameExtra; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | DeclarationName::NameKind DeclarationName::getNameKind() const { |
| 163 | switch (getStoredNameKind()) { |
| 164 | case StoredIdentifier: return Identifier; |
| 165 | case StoredObjCZeroArgSelector: return ObjCZeroArgSelector; |
| 166 | case StoredObjCOneArgSelector: return ObjCOneArgSelector; |
| 167 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 168 | case StoredDeclarationNameExtra: |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 169 | switch (getExtra()->ExtraKindOrNumArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | case DeclarationNameExtra::CXXConstructor: |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 171 | return CXXConstructorName; |
| 172 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | case DeclarationNameExtra::CXXDestructor: |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 174 | return CXXDestructorName; |
| 175 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | case DeclarationNameExtra::CXXConversionFunction: |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 177 | return CXXConversionFunctionName; |
| 178 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 179 | case DeclarationNameExtra::CXXLiteralOperator: |
| 180 | return CXXLiteralOperatorName; |
| 181 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 182 | case DeclarationNameExtra::CXXUsingDirective: |
| 183 | return CXXUsingDirective; |
| 184 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 185 | default: |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 186 | // Check if we have one of the CXXOperator* enumeration values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | if (getExtra()->ExtraKindOrNumArgs < |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 188 | DeclarationNameExtra::CXXUsingDirective) |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 189 | return CXXOperatorName; |
| 190 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 191 | return ObjCMultiArgSelector; |
| 192 | } |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | // Can't actually get here. |
Chris Lattner | ac8d75f | 2009-03-21 06:40:50 +0000 | [diff] [blame] | 197 | assert(0 && "This should be unreachable!"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 198 | return Identifier; |
| 199 | } |
| 200 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 201 | bool DeclarationName::isDependentName() const { |
| 202 | QualType T = getCXXNameType(); |
| 203 | return !T.isNull() && T->isDependentType(); |
| 204 | } |
| 205 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 206 | std::string DeclarationName::getAsString() const { |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 207 | std::string Result; |
| 208 | llvm::raw_string_ostream OS(Result); |
| 209 | printName(OS); |
| 210 | return OS.str(); |
| 211 | } |
| 212 | |
| 213 | void DeclarationName::printName(llvm::raw_ostream &OS) const { |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 214 | switch (getNameKind()) { |
| 215 | case Identifier: |
| 216 | if (const IdentifierInfo *II = getAsIdentifierInfo()) |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 217 | OS << II->getName(); |
| 218 | return; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 219 | |
| 220 | case ObjCZeroArgSelector: |
| 221 | case ObjCOneArgSelector: |
| 222 | case ObjCMultiArgSelector: |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 223 | OS << getObjCSelector().getAsString(); |
| 224 | return; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 225 | |
| 226 | case CXXConstructorName: { |
| 227 | QualType ClassType = getCXXNameType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 228 | if (const RecordType *ClassRec = ClassType->getAs<RecordType>()) |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 229 | OS << ClassRec->getDecl(); |
| 230 | else |
| 231 | OS << ClassType.getAsString(); |
| 232 | return; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | case CXXDestructorName: { |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 236 | OS << '~'; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 237 | QualType Type = getCXXNameType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 238 | if (const RecordType *Rec = Type->getAs<RecordType>()) |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 239 | OS << Rec->getDecl(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 240 | else |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 241 | OS << Type.getAsString(); |
| 242 | return; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 245 | case CXXOperatorName: { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 246 | static const char* const OperatorNames[NUM_OVERLOADED_OPERATORS] = { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 247 | 0, |
| 248 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 249 | Spelling, |
| 250 | #include "clang/Basic/OperatorKinds.def" |
| 251 | }; |
| 252 | const char *OpName = OperatorNames[getCXXOverloadedOperator()]; |
| 253 | assert(OpName && "not an overloaded operator"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 255 | OS << "operator"; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 256 | if (OpName[0] >= 'a' && OpName[0] <= 'z') |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 257 | OS << ' '; |
| 258 | OS << OpName; |
| 259 | return; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 262 | case CXXLiteralOperatorName: |
| 263 | OS << "operator \"\" " << getCXXLiteralIdentifier()->getName(); |
| 264 | return; |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 266 | case CXXConversionFunctionName: { |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 267 | OS << "operator "; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 268 | QualType Type = getCXXNameType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 269 | if (const RecordType *Rec = Type->getAs<RecordType>()) |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 270 | OS << Rec->getDecl(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 271 | else |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 272 | OS << Type.getAsString(); |
| 273 | return; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 274 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 275 | case CXXUsingDirective: |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 276 | OS << "<using-directive>"; |
| 277 | return; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | assert(false && "Unexpected declaration name kind"); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 283 | QualType DeclarationName::getCXXNameType() const { |
| 284 | if (CXXSpecialName *CXXName = getAsCXXSpecialName()) |
| 285 | return CXXName->Type; |
| 286 | else |
| 287 | return QualType(); |
| 288 | } |
| 289 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 290 | OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const { |
| 291 | if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | unsigned value |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 293 | = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction; |
| 294 | return static_cast<OverloadedOperatorKind>(value); |
| 295 | } else { |
| 296 | return OO_None; |
| 297 | } |
| 298 | } |
| 299 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 300 | IdentifierInfo *DeclarationName::getCXXLiteralIdentifier() const { |
| 301 | if (CXXLiteralOperatorIdName *CXXLit = getAsCXXLiteralOperatorIdName()) |
| 302 | return CXXLit->ID; |
| 303 | else |
| 304 | return 0; |
| 305 | } |
| 306 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 307 | Selector DeclarationName::getObjCSelector() const { |
| 308 | switch (getNameKind()) { |
| 309 | case ObjCZeroArgSelector: |
| 310 | return Selector(reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask), 0); |
| 311 | |
| 312 | case ObjCOneArgSelector: |
| 313 | return Selector(reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask), 1); |
| 314 | |
| 315 | case ObjCMultiArgSelector: |
| 316 | return Selector(reinterpret_cast<MultiKeywordSelector *>(Ptr & ~PtrMask)); |
| 317 | |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | return Selector(); |
| 323 | } |
| 324 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 325 | void *DeclarationName::getFETokenInfoAsVoid() const { |
| 326 | switch (getNameKind()) { |
| 327 | case Identifier: |
| 328 | return getAsIdentifierInfo()->getFETokenInfo<void>(); |
| 329 | |
| 330 | case CXXConstructorName: |
| 331 | case CXXDestructorName: |
| 332 | case CXXConversionFunctionName: |
| 333 | return getAsCXXSpecialName()->FETokenInfo; |
| 334 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 335 | case CXXOperatorName: |
| 336 | return getAsCXXOperatorIdName()->FETokenInfo; |
| 337 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 338 | case CXXLiteralOperatorName: |
| 339 | return getCXXLiteralIdentifier()->getFETokenInfo<void>(); |
| 340 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 341 | default: |
| 342 | assert(false && "Declaration name has no FETokenInfo"); |
| 343 | } |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | void DeclarationName::setFETokenInfo(void *T) { |
| 348 | switch (getNameKind()) { |
| 349 | case Identifier: |
| 350 | getAsIdentifierInfo()->setFETokenInfo(T); |
| 351 | break; |
| 352 | |
| 353 | case CXXConstructorName: |
| 354 | case CXXDestructorName: |
| 355 | case CXXConversionFunctionName: |
| 356 | getAsCXXSpecialName()->FETokenInfo = T; |
| 357 | break; |
| 358 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 359 | case CXXOperatorName: |
| 360 | getAsCXXOperatorIdName()->FETokenInfo = T; |
| 361 | break; |
| 362 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 363 | case CXXLiteralOperatorName: |
| 364 | getCXXLiteralIdentifier()->setFETokenInfo(T); |
| 365 | break; |
| 366 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 367 | default: |
| 368 | assert(false && "Declaration name has no FETokenInfo"); |
| 369 | } |
| 370 | } |
| 371 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 372 | DeclarationName DeclarationName::getUsingDirectiveName() { |
| 373 | // Single instance of DeclarationNameExtra for using-directive |
Nuno Lopes | 68f7a24 | 2009-12-10 00:07:02 +0000 | [diff] [blame] | 374 | static const DeclarationNameExtra UDirExtra = |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 375 | { DeclarationNameExtra::CXXUsingDirective }; |
| 376 | |
| 377 | uintptr_t Ptr = reinterpret_cast<uintptr_t>(&UDirExtra); |
| 378 | Ptr |= StoredDeclarationNameExtra; |
| 379 | |
| 380 | return DeclarationName(Ptr); |
| 381 | } |
| 382 | |
Anders Carlsson | 70f5bc7 | 2009-11-15 22:30:43 +0000 | [diff] [blame] | 383 | void DeclarationName::dump() const { |
Benjamin Kramer | f6cde77 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 384 | printName(llvm::errs()); |
| 385 | llvm::errs() << '\n'; |
Anders Carlsson | 70f5bc7 | 2009-11-15 22:30:43 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Ted Kremenek | 45d9c2d | 2010-05-10 20:56:10 +0000 | [diff] [blame] | 388 | DeclarationNameTable::DeclarationNameTable(ASTContext &C) : Ctx(C) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 389 | CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>; |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 390 | CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 391 | |
| 392 | // Initialize the overloaded operator names. |
Ted Kremenek | 45d9c2d | 2010-05-10 20:56:10 +0000 | [diff] [blame] | 393 | CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS]; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 394 | for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | CXXOperatorNames[Op].ExtraKindOrNumArgs |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 396 | = Op + DeclarationNameExtra::CXXConversionFunction; |
| 397 | CXXOperatorNames[Op].FETokenInfo = 0; |
| 398 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | DeclarationNameTable::~DeclarationNameTable() { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 402 | llvm::FoldingSet<CXXSpecialName> *SpecialNames = |
Nuno Lopes | 6d34ae5 | 2008-12-14 17:27:25 +0000 | [diff] [blame] | 403 | static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 404 | llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames |
| 405 | = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*> |
Ted Kremenek | 45d9c2d | 2010-05-10 20:56:10 +0000 | [diff] [blame] | 406 | (CXXLiteralOperatorNames); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 407 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 408 | delete SpecialNames; |
| 409 | delete LiteralNames; |
Ted Kremenek | ac9590e | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | DeclarationName |
| 413 | DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 414 | CanQualType Ty) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 415 | assert(Kind >= DeclarationName::CXXConstructorName && |
| 416 | Kind <= DeclarationName::CXXConversionFunctionName && |
| 417 | "Kind must be a C++ special name kind"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | llvm::FoldingSet<CXXSpecialName> *SpecialNames |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 419 | = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl); |
| 420 | |
| 421 | DeclarationNameExtra::ExtraKind EKind; |
| 422 | switch (Kind) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | case DeclarationName::CXXConstructorName: |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 424 | EKind = DeclarationNameExtra::CXXConstructor; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 425 | assert(!Ty.hasQualifiers() &&"Constructor type must be unqualified"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 426 | break; |
| 427 | case DeclarationName::CXXDestructorName: |
| 428 | EKind = DeclarationNameExtra::CXXDestructor; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 429 | assert(!Ty.hasQualifiers() && "Destructor type must be unqualified"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 430 | break; |
| 431 | case DeclarationName::CXXConversionFunctionName: |
| 432 | EKind = DeclarationNameExtra::CXXConversionFunction; |
| 433 | break; |
| 434 | default: |
| 435 | return DeclarationName(); |
| 436 | } |
| 437 | |
| 438 | // Unique selector, to guarantee there is one per name. |
| 439 | llvm::FoldingSetNodeID ID; |
| 440 | ID.AddInteger(EKind); |
| 441 | ID.AddPointer(Ty.getAsOpaquePtr()); |
| 442 | |
| 443 | void *InsertPos = 0; |
| 444 | if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos)) |
| 445 | return DeclarationName(Name); |
| 446 | |
Ted Kremenek | 45d9c2d | 2010-05-10 20:56:10 +0000 | [diff] [blame] | 447 | CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 448 | SpecialName->ExtraKindOrNumArgs = EKind; |
| 449 | SpecialName->Type = Ty; |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 450 | SpecialName->FETokenInfo = 0; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 451 | |
| 452 | SpecialNames->InsertNode(SpecialName, InsertPos); |
| 453 | return DeclarationName(SpecialName); |
| 454 | } |
| 455 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | DeclarationName |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 457 | DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) { |
| 458 | return DeclarationName(&CXXOperatorNames[(unsigned)Op]); |
| 459 | } |
| 460 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 461 | DeclarationName |
| 462 | DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 463 | llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames |
| 464 | = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*> |
| 465 | (CXXLiteralOperatorNames); |
| 466 | |
| 467 | llvm::FoldingSetNodeID ID; |
| 468 | ID.AddPointer(II); |
| 469 | |
| 470 | void *InsertPos = 0; |
| 471 | if (CXXLiteralOperatorIdName *Name = |
| 472 | LiteralNames->FindNodeOrInsertPos(ID, InsertPos)) |
| 473 | return DeclarationName (Name); |
| 474 | |
Ted Kremenek | 45d9c2d | 2010-05-10 20:56:10 +0000 | [diff] [blame] | 475 | CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName; |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 476 | LiteralName->ExtraKindOrNumArgs = DeclarationNameExtra::CXXLiteralOperator; |
| 477 | LiteralName->ID = II; |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 478 | |
| 479 | LiteralNames->InsertNode(LiteralName, InsertPos); |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 480 | return DeclarationName(LiteralName); |
| 481 | } |
| 482 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | unsigned |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 484 | llvm::DenseMapInfo<clang::DeclarationName>:: |
| 485 | getHashValue(clang::DeclarationName N) { |
| 486 | return DenseMapInfo<void*>::getHashValue(N.getAsOpaquePtr()); |
| 487 | } |
| 488 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 489 | DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) { |
| 490 | switch (Name.getNameKind()) { |
| 491 | case DeclarationName::Identifier: |
| 492 | break; |
| 493 | case DeclarationName::CXXConstructorName: |
| 494 | case DeclarationName::CXXDestructorName: |
| 495 | case DeclarationName::CXXConversionFunctionName: |
| 496 | NamedType.TInfo = 0; |
| 497 | break; |
| 498 | case DeclarationName::CXXOperatorName: |
| 499 | CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 500 | CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 501 | break; |
| 502 | case DeclarationName::CXXLiteralOperatorName: |
| 503 | CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding(); |
| 504 | break; |
| 505 | case DeclarationName::ObjCZeroArgSelector: |
| 506 | case DeclarationName::ObjCOneArgSelector: |
| 507 | case DeclarationName::ObjCMultiArgSelector: |
| 508 | // FIXME: ? |
| 509 | break; |
| 510 | case DeclarationName::CXXUsingDirective: |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame^] | 515 | bool DeclarationNameInfo::containsUnexpandedParameterPack() const { |
| 516 | switch (Name.getNameKind()) { |
| 517 | case DeclarationName::Identifier: |
| 518 | case DeclarationName::ObjCZeroArgSelector: |
| 519 | case DeclarationName::ObjCOneArgSelector: |
| 520 | case DeclarationName::ObjCMultiArgSelector: |
| 521 | case DeclarationName::CXXOperatorName: |
| 522 | case DeclarationName::CXXLiteralOperatorName: |
| 523 | case DeclarationName::CXXUsingDirective: |
| 524 | return false; |
| 525 | |
| 526 | case DeclarationName::CXXConstructorName: |
| 527 | case DeclarationName::CXXDestructorName: |
| 528 | case DeclarationName::CXXConversionFunctionName: |
| 529 | if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) |
| 530 | return TInfo->getType()->containsUnexpandedParameterPack(); |
| 531 | |
| 532 | return Name.getCXXNameType()->containsUnexpandedParameterPack(); |
| 533 | } |
| 534 | } |
| 535 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 536 | std::string DeclarationNameInfo::getAsString() const { |
| 537 | std::string Result; |
| 538 | llvm::raw_string_ostream OS(Result); |
| 539 | printName(OS); |
| 540 | return OS.str(); |
| 541 | } |
| 542 | |
| 543 | void DeclarationNameInfo::printName(llvm::raw_ostream &OS) const { |
| 544 | switch (Name.getNameKind()) { |
| 545 | case DeclarationName::Identifier: |
| 546 | case DeclarationName::ObjCZeroArgSelector: |
| 547 | case DeclarationName::ObjCOneArgSelector: |
| 548 | case DeclarationName::ObjCMultiArgSelector: |
| 549 | case DeclarationName::CXXOperatorName: |
| 550 | case DeclarationName::CXXLiteralOperatorName: |
| 551 | case DeclarationName::CXXUsingDirective: |
| 552 | Name.printName(OS); |
| 553 | return; |
| 554 | |
| 555 | case DeclarationName::CXXConstructorName: |
| 556 | case DeclarationName::CXXDestructorName: |
| 557 | case DeclarationName::CXXConversionFunctionName: |
| 558 | if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) { |
| 559 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) |
| 560 | OS << '~'; |
| 561 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
| 562 | OS << "operator "; |
| 563 | OS << TInfo->getType().getAsString(); |
| 564 | } |
| 565 | else |
| 566 | Name.printName(OS); |
| 567 | return; |
| 568 | } |
| 569 | assert(false && "Unexpected declaration name kind"); |
| 570 | } |
| 571 | |
| 572 | SourceLocation DeclarationNameInfo::getEndLoc() const { |
| 573 | switch (Name.getNameKind()) { |
| 574 | case DeclarationName::Identifier: |
| 575 | return NameLoc; |
| 576 | |
| 577 | case DeclarationName::CXXOperatorName: { |
| 578 | unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc; |
| 579 | return SourceLocation::getFromRawEncoding(raw); |
| 580 | } |
| 581 | |
| 582 | case DeclarationName::CXXLiteralOperatorName: { |
| 583 | unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc; |
| 584 | return SourceLocation::getFromRawEncoding(raw); |
| 585 | } |
| 586 | |
| 587 | case DeclarationName::CXXConstructorName: |
| 588 | case DeclarationName::CXXDestructorName: |
| 589 | case DeclarationName::CXXConversionFunctionName: |
| 590 | if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) |
| 591 | return TInfo->getTypeLoc().getEndLoc(); |
| 592 | else |
| 593 | return NameLoc; |
| 594 | |
| 595 | // DNInfo work in progress: FIXME. |
| 596 | case DeclarationName::ObjCZeroArgSelector: |
| 597 | case DeclarationName::ObjCOneArgSelector: |
| 598 | case DeclarationName::ObjCMultiArgSelector: |
| 599 | case DeclarationName::CXXUsingDirective: |
| 600 | return NameLoc; |
| 601 | } |
| 602 | assert(false && "Unexpected declaration name kind"); |
| 603 | return SourceLocation(); |
| 604 | } |