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