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" |
| 16 | #include "clang/AST/Decl.h" |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
| 18 | #include "llvm/ADT/FoldingSet.h" |
| 19 | #include "llvm/Bitcode/Serialize.h" |
| 20 | #include "llvm/Bitcode/Deserialize.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | namespace clang { |
| 24 | /// CXXSpecialName - Records the type associated with one of the |
| 25 | /// "special" kinds of declaration names in C++, e.g., constructors, |
| 26 | /// destructors, and conversion functions. |
| 27 | class CXXSpecialName |
| 28 | : public DeclarationNameExtra, public llvm::FoldingSetNode { |
| 29 | public: |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 30 | /// Type - The type associated with this declaration name. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 31 | QualType Type; |
| 32 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 33 | /// FETokenInfo - Extra information associated with this declaration |
| 34 | /// name that can be used by the front end. |
| 35 | void *FETokenInfo; |
| 36 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 37 | void Profile(llvm::FoldingSetNodeID &ID) { |
| 38 | ID.AddInteger(ExtraKindOrNumArgs); |
| 39 | ID.AddPointer(Type.getAsOpaquePtr()); |
| 40 | } |
| 41 | }; |
| 42 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 43 | /// CXXOperatorIdName - Contains extra information for the name of an |
| 44 | /// overloaded operator in C++, such as "operator+. |
| 45 | class CXXOperatorIdName : public DeclarationNameExtra { |
| 46 | public: |
| 47 | /// FETokenInfo - Extra information associated with this operator |
| 48 | /// name that can be used by the front end. |
| 49 | void *FETokenInfo; |
| 50 | }; |
| 51 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 52 | bool operator<(DeclarationName LHS, DeclarationName RHS) { |
| 53 | if (IdentifierInfo *LhsId = LHS.getAsIdentifierInfo()) |
| 54 | if (IdentifierInfo *RhsId = RHS.getAsIdentifierInfo()) |
| 55 | return strcmp(LhsId->getName(), RhsId->getName()) < 0; |
| 56 | |
| 57 | return LHS.getAsOpaqueInteger() < RHS.getAsOpaqueInteger(); |
| 58 | } |
| 59 | |
| 60 | } // end namespace clang |
| 61 | |
| 62 | DeclarationName::DeclarationName(Selector Sel) { |
| 63 | switch (Sel.getNumArgs()) { |
| 64 | case 0: |
| 65 | Ptr = reinterpret_cast<uintptr_t>(Sel.getAsIdentifierInfo()); |
| 66 | Ptr |= StoredObjCZeroArgSelector; |
| 67 | break; |
| 68 | |
| 69 | case 1: |
| 70 | Ptr = reinterpret_cast<uintptr_t>(Sel.getAsIdentifierInfo()); |
| 71 | Ptr |= StoredObjCOneArgSelector; |
| 72 | break; |
| 73 | |
| 74 | default: |
| 75 | Ptr = Sel.InfoPtr & ~Selector::ArgFlags; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 76 | Ptr |= StoredDeclarationNameExtra; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | DeclarationName::NameKind DeclarationName::getNameKind() const { |
| 82 | switch (getStoredNameKind()) { |
| 83 | case StoredIdentifier: return Identifier; |
| 84 | case StoredObjCZeroArgSelector: return ObjCZeroArgSelector; |
| 85 | case StoredObjCOneArgSelector: return ObjCOneArgSelector; |
| 86 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 87 | case StoredDeclarationNameExtra: |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 88 | switch (getExtra()->ExtraKindOrNumArgs) { |
| 89 | case DeclarationNameExtra::CXXConstructor: |
| 90 | return CXXConstructorName; |
| 91 | |
| 92 | case DeclarationNameExtra::CXXDestructor: |
| 93 | return CXXDestructorName; |
| 94 | |
| 95 | case DeclarationNameExtra::CXXConversionFunction: |
| 96 | return CXXConversionFunctionName; |
| 97 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 98 | case DeclarationNameExtra::CXXUsingDirective: |
| 99 | return CXXUsingDirective; |
| 100 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 101 | default: |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 102 | // Check if we have one of the CXXOperator* enumeration values. |
| 103 | if (getExtra()->ExtraKindOrNumArgs < |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 104 | DeclarationNameExtra::CXXUsingDirective) |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 105 | return CXXOperatorName; |
| 106 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 107 | return ObjCMultiArgSelector; |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | // Can't actually get here. |
| 113 | return Identifier; |
| 114 | } |
| 115 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 116 | std::string DeclarationName::getAsString() const { |
| 117 | switch (getNameKind()) { |
| 118 | case Identifier: |
| 119 | if (const IdentifierInfo *II = getAsIdentifierInfo()) |
| 120 | return II->getName(); |
| 121 | return ""; |
| 122 | |
| 123 | case ObjCZeroArgSelector: |
| 124 | case ObjCOneArgSelector: |
| 125 | case ObjCMultiArgSelector: |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 126 | return getObjCSelector().getAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 127 | |
| 128 | case CXXConstructorName: { |
| 129 | QualType ClassType = getCXXNameType(); |
| 130 | if (const RecordType *ClassRec = ClassType->getAsRecordType()) |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 131 | return ClassRec->getDecl()->getNameAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 132 | return ClassType.getAsString(); |
| 133 | } |
| 134 | |
| 135 | case CXXDestructorName: { |
| 136 | std::string Result = "~"; |
| 137 | QualType Type = getCXXNameType(); |
| 138 | if (const RecordType *Rec = Type->getAsRecordType()) |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 139 | Result += Rec->getDecl()->getNameAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 140 | else |
| 141 | Result += Type.getAsString(); |
| 142 | return Result; |
| 143 | } |
| 144 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 145 | case CXXOperatorName: { |
| 146 | static const char *OperatorNames[NUM_OVERLOADED_OPERATORS] = { |
| 147 | 0, |
| 148 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 149 | Spelling, |
| 150 | #include "clang/Basic/OperatorKinds.def" |
| 151 | }; |
| 152 | const char *OpName = OperatorNames[getCXXOverloadedOperator()]; |
| 153 | assert(OpName && "not an overloaded operator"); |
| 154 | |
| 155 | std::string Result = "operator"; |
| 156 | if (OpName[0] >= 'a' && OpName[0] <= 'z') |
| 157 | Result += ' '; |
| 158 | Result += OpName; |
| 159 | return Result; |
| 160 | } |
| 161 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 162 | case CXXConversionFunctionName: { |
| 163 | std::string Result = "operator "; |
| 164 | QualType Type = getCXXNameType(); |
| 165 | if (const RecordType *Rec = Type->getAsRecordType()) |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 166 | Result += Rec->getDecl()->getNameAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 167 | else |
| 168 | Result += Type.getAsString(); |
| 169 | return Result; |
| 170 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 171 | case CXXUsingDirective: |
| 172 | return "<using-directive>"; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | assert(false && "Unexpected declaration name kind"); |
| 176 | return ""; |
| 177 | } |
| 178 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 179 | QualType DeclarationName::getCXXNameType() const { |
| 180 | if (CXXSpecialName *CXXName = getAsCXXSpecialName()) |
| 181 | return CXXName->Type; |
| 182 | else |
| 183 | return QualType(); |
| 184 | } |
| 185 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 186 | OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const { |
| 187 | if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) { |
| 188 | unsigned value |
| 189 | = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction; |
| 190 | return static_cast<OverloadedOperatorKind>(value); |
| 191 | } else { |
| 192 | return OO_None; |
| 193 | } |
| 194 | } |
| 195 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 196 | Selector DeclarationName::getObjCSelector() const { |
| 197 | switch (getNameKind()) { |
| 198 | case ObjCZeroArgSelector: |
| 199 | return Selector(reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask), 0); |
| 200 | |
| 201 | case ObjCOneArgSelector: |
| 202 | return Selector(reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask), 1); |
| 203 | |
| 204 | case ObjCMultiArgSelector: |
| 205 | return Selector(reinterpret_cast<MultiKeywordSelector *>(Ptr & ~PtrMask)); |
| 206 | |
| 207 | default: |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | return Selector(); |
| 212 | } |
| 213 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 214 | void *DeclarationName::getFETokenInfoAsVoid() const { |
| 215 | switch (getNameKind()) { |
| 216 | case Identifier: |
| 217 | return getAsIdentifierInfo()->getFETokenInfo<void>(); |
| 218 | |
| 219 | case CXXConstructorName: |
| 220 | case CXXDestructorName: |
| 221 | case CXXConversionFunctionName: |
| 222 | return getAsCXXSpecialName()->FETokenInfo; |
| 223 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 224 | case CXXOperatorName: |
| 225 | return getAsCXXOperatorIdName()->FETokenInfo; |
| 226 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 227 | default: |
| 228 | assert(false && "Declaration name has no FETokenInfo"); |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | void DeclarationName::setFETokenInfo(void *T) { |
| 234 | switch (getNameKind()) { |
| 235 | case Identifier: |
| 236 | getAsIdentifierInfo()->setFETokenInfo(T); |
| 237 | break; |
| 238 | |
| 239 | case CXXConstructorName: |
| 240 | case CXXDestructorName: |
| 241 | case CXXConversionFunctionName: |
| 242 | getAsCXXSpecialName()->FETokenInfo = T; |
| 243 | break; |
| 244 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 245 | case CXXOperatorName: |
| 246 | getAsCXXOperatorIdName()->FETokenInfo = T; |
| 247 | break; |
| 248 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 249 | default: |
| 250 | assert(false && "Declaration name has no FETokenInfo"); |
| 251 | } |
| 252 | } |
| 253 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 254 | DeclarationName DeclarationName::getUsingDirectiveName() { |
| 255 | // Single instance of DeclarationNameExtra for using-directive |
| 256 | static DeclarationNameExtra UDirExtra = |
| 257 | { DeclarationNameExtra::CXXUsingDirective }; |
| 258 | |
| 259 | uintptr_t Ptr = reinterpret_cast<uintptr_t>(&UDirExtra); |
| 260 | Ptr |= StoredDeclarationNameExtra; |
| 261 | |
| 262 | return DeclarationName(Ptr); |
| 263 | } |
| 264 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 265 | DeclarationNameTable::DeclarationNameTable() { |
| 266 | CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 267 | |
| 268 | // Initialize the overloaded operator names. |
| 269 | CXXOperatorNames = new CXXOperatorIdName[NUM_OVERLOADED_OPERATORS]; |
| 270 | for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) { |
| 271 | CXXOperatorNames[Op].ExtraKindOrNumArgs |
| 272 | = Op + DeclarationNameExtra::CXXConversionFunction; |
| 273 | CXXOperatorNames[Op].FETokenInfo = 0; |
| 274 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | DeclarationNameTable::~DeclarationNameTable() { |
Nuno Lopes | 6d34ae5 | 2008-12-14 17:27:25 +0000 | [diff] [blame] | 278 | llvm::FoldingSet<CXXSpecialName> *set = |
| 279 | static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl); |
Nuno Lopes | f9d1e4b | 2008-12-14 21:53:25 +0000 | [diff] [blame] | 280 | llvm::FoldingSetIterator<CXXSpecialName> I = set->begin(), E = set->end(); |
Nuno Lopes | 6d34ae5 | 2008-12-14 17:27:25 +0000 | [diff] [blame] | 281 | |
Nuno Lopes | f9d1e4b | 2008-12-14 21:53:25 +0000 | [diff] [blame] | 282 | while (I != E) { |
| 283 | CXXSpecialName *n = &*I++; |
| 284 | delete n; |
Nuno Lopes | 6d34ae5 | 2008-12-14 17:27:25 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | delete set; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 288 | delete [] CXXOperatorNames; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | DeclarationName |
| 292 | DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind, |
| 293 | QualType Ty) { |
| 294 | assert(Kind >= DeclarationName::CXXConstructorName && |
| 295 | Kind <= DeclarationName::CXXConversionFunctionName && |
| 296 | "Kind must be a C++ special name kind"); |
| 297 | |
| 298 | llvm::FoldingSet<CXXSpecialName> *SpecialNames |
| 299 | = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl); |
| 300 | |
| 301 | DeclarationNameExtra::ExtraKind EKind; |
| 302 | switch (Kind) { |
| 303 | case DeclarationName::CXXConstructorName: |
| 304 | EKind = DeclarationNameExtra::CXXConstructor; |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame^] | 305 | assert(Ty.getCVRQualifiers() == 0 &&"Constructor type must be unqualified"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 306 | break; |
| 307 | case DeclarationName::CXXDestructorName: |
| 308 | EKind = DeclarationNameExtra::CXXDestructor; |
Douglas Gregor | e63ef48 | 2009-01-13 00:11:19 +0000 | [diff] [blame] | 309 | assert(Ty.getCVRQualifiers() == 0 && "Destructor type must be unqualified"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 310 | break; |
| 311 | case DeclarationName::CXXConversionFunctionName: |
| 312 | EKind = DeclarationNameExtra::CXXConversionFunction; |
| 313 | break; |
| 314 | default: |
| 315 | return DeclarationName(); |
| 316 | } |
| 317 | |
| 318 | // Unique selector, to guarantee there is one per name. |
| 319 | llvm::FoldingSetNodeID ID; |
| 320 | ID.AddInteger(EKind); |
| 321 | ID.AddPointer(Ty.getAsOpaquePtr()); |
| 322 | |
| 323 | void *InsertPos = 0; |
| 324 | if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos)) |
| 325 | return DeclarationName(Name); |
| 326 | |
| 327 | CXXSpecialName *SpecialName = new CXXSpecialName; |
| 328 | SpecialName->ExtraKindOrNumArgs = EKind; |
| 329 | SpecialName->Type = Ty; |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 330 | SpecialName->FETokenInfo = 0; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 331 | |
| 332 | SpecialNames->InsertNode(SpecialName, InsertPos); |
| 333 | return DeclarationName(SpecialName); |
| 334 | } |
| 335 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 336 | DeclarationName |
| 337 | DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) { |
| 338 | return DeclarationName(&CXXOperatorNames[(unsigned)Op]); |
| 339 | } |
| 340 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 341 | unsigned |
| 342 | llvm::DenseMapInfo<clang::DeclarationName>:: |
| 343 | getHashValue(clang::DeclarationName N) { |
| 344 | return DenseMapInfo<void*>::getHashValue(N.getAsOpaquePtr()); |
| 345 | } |
| 346 | |