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