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