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 | |
| 98 | default: |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 99 | // Check if we have one of the CXXOperator* enumeration values. |
| 100 | if (getExtra()->ExtraKindOrNumArgs < |
| 101 | DeclarationNameExtra::NUM_EXTRA_KINDS) |
| 102 | return CXXOperatorName; |
| 103 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 104 | return ObjCMultiArgSelector; |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | // Can't actually get here. |
| 110 | return Identifier; |
| 111 | } |
| 112 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 113 | std::string DeclarationName::getAsString() const { |
| 114 | switch (getNameKind()) { |
| 115 | case Identifier: |
| 116 | if (const IdentifierInfo *II = getAsIdentifierInfo()) |
| 117 | return II->getName(); |
| 118 | return ""; |
| 119 | |
| 120 | case ObjCZeroArgSelector: |
| 121 | case ObjCOneArgSelector: |
| 122 | case ObjCMultiArgSelector: |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 123 | return getObjCSelector().getAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 124 | |
| 125 | case CXXConstructorName: { |
| 126 | QualType ClassType = getCXXNameType(); |
| 127 | if (const RecordType *ClassRec = ClassType->getAsRecordType()) |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 128 | return ClassRec->getDecl()->getNameAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 129 | return ClassType.getAsString(); |
| 130 | } |
| 131 | |
| 132 | case CXXDestructorName: { |
| 133 | std::string Result = "~"; |
| 134 | QualType Type = getCXXNameType(); |
| 135 | if (const RecordType *Rec = Type->getAsRecordType()) |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 136 | Result += Rec->getDecl()->getNameAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 137 | else |
| 138 | Result += Type.getAsString(); |
| 139 | return Result; |
| 140 | } |
| 141 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 142 | case CXXOperatorName: { |
| 143 | static const char *OperatorNames[NUM_OVERLOADED_OPERATORS] = { |
| 144 | 0, |
| 145 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 146 | Spelling, |
| 147 | #include "clang/Basic/OperatorKinds.def" |
| 148 | }; |
| 149 | const char *OpName = OperatorNames[getCXXOverloadedOperator()]; |
| 150 | assert(OpName && "not an overloaded operator"); |
| 151 | |
| 152 | std::string Result = "operator"; |
| 153 | if (OpName[0] >= 'a' && OpName[0] <= 'z') |
| 154 | Result += ' '; |
| 155 | Result += OpName; |
| 156 | return Result; |
| 157 | } |
| 158 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 159 | case CXXConversionFunctionName: { |
| 160 | std::string Result = "operator "; |
| 161 | QualType Type = getCXXNameType(); |
| 162 | if (const RecordType *Rec = Type->getAsRecordType()) |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 163 | Result += Rec->getDecl()->getNameAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 164 | else |
| 165 | Result += Type.getAsString(); |
| 166 | return Result; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | assert(false && "Unexpected declaration name kind"); |
| 171 | return ""; |
| 172 | } |
| 173 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 174 | QualType DeclarationName::getCXXNameType() const { |
| 175 | if (CXXSpecialName *CXXName = getAsCXXSpecialName()) |
| 176 | return CXXName->Type; |
| 177 | else |
| 178 | return QualType(); |
| 179 | } |
| 180 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 181 | OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const { |
| 182 | if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) { |
| 183 | unsigned value |
| 184 | = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction; |
| 185 | return static_cast<OverloadedOperatorKind>(value); |
| 186 | } else { |
| 187 | return OO_None; |
| 188 | } |
| 189 | } |
| 190 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 191 | Selector DeclarationName::getObjCSelector() const { |
| 192 | switch (getNameKind()) { |
| 193 | case ObjCZeroArgSelector: |
| 194 | return Selector(reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask), 0); |
| 195 | |
| 196 | case ObjCOneArgSelector: |
| 197 | return Selector(reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask), 1); |
| 198 | |
| 199 | case ObjCMultiArgSelector: |
| 200 | return Selector(reinterpret_cast<MultiKeywordSelector *>(Ptr & ~PtrMask)); |
| 201 | |
| 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | return Selector(); |
| 207 | } |
| 208 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 209 | void *DeclarationName::getFETokenInfoAsVoid() const { |
| 210 | switch (getNameKind()) { |
| 211 | case Identifier: |
| 212 | return getAsIdentifierInfo()->getFETokenInfo<void>(); |
| 213 | |
| 214 | case CXXConstructorName: |
| 215 | case CXXDestructorName: |
| 216 | case CXXConversionFunctionName: |
| 217 | return getAsCXXSpecialName()->FETokenInfo; |
| 218 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 219 | case CXXOperatorName: |
| 220 | return getAsCXXOperatorIdName()->FETokenInfo; |
| 221 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 222 | default: |
| 223 | assert(false && "Declaration name has no FETokenInfo"); |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | void DeclarationName::setFETokenInfo(void *T) { |
| 229 | switch (getNameKind()) { |
| 230 | case Identifier: |
| 231 | getAsIdentifierInfo()->setFETokenInfo(T); |
| 232 | break; |
| 233 | |
| 234 | case CXXConstructorName: |
| 235 | case CXXDestructorName: |
| 236 | case CXXConversionFunctionName: |
| 237 | getAsCXXSpecialName()->FETokenInfo = T; |
| 238 | break; |
| 239 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 240 | case CXXOperatorName: |
| 241 | getAsCXXOperatorIdName()->FETokenInfo = T; |
| 242 | break; |
| 243 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 244 | default: |
| 245 | assert(false && "Declaration name has no FETokenInfo"); |
| 246 | } |
| 247 | } |
| 248 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 249 | DeclarationNameTable::DeclarationNameTable() { |
| 250 | CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 251 | |
| 252 | // Initialize the overloaded operator names. |
| 253 | CXXOperatorNames = new CXXOperatorIdName[NUM_OVERLOADED_OPERATORS]; |
| 254 | for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) { |
| 255 | CXXOperatorNames[Op].ExtraKindOrNumArgs |
| 256 | = Op + DeclarationNameExtra::CXXConversionFunction; |
| 257 | CXXOperatorNames[Op].FETokenInfo = 0; |
| 258 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | DeclarationNameTable::~DeclarationNameTable() { |
Nuno Lopes | 6d34ae5 | 2008-12-14 17:27:25 +0000 | [diff] [blame^] | 262 | llvm::FoldingSet<CXXSpecialName> *set = |
| 263 | static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl); |
| 264 | llvm::FoldingSetIterator<CXXSpecialName> it = set->begin(); |
| 265 | |
| 266 | while (it != set->end()) { |
| 267 | CXXSpecialName *n = &*it++; |
| 268 | delete n; |
| 269 | } |
| 270 | |
| 271 | delete set; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 272 | delete [] CXXOperatorNames; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | DeclarationName |
| 276 | DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind, |
| 277 | QualType Ty) { |
| 278 | assert(Kind >= DeclarationName::CXXConstructorName && |
| 279 | Kind <= DeclarationName::CXXConversionFunctionName && |
| 280 | "Kind must be a C++ special name kind"); |
| 281 | |
| 282 | llvm::FoldingSet<CXXSpecialName> *SpecialNames |
| 283 | = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl); |
| 284 | |
| 285 | DeclarationNameExtra::ExtraKind EKind; |
| 286 | switch (Kind) { |
| 287 | case DeclarationName::CXXConstructorName: |
| 288 | EKind = DeclarationNameExtra::CXXConstructor; |
| 289 | break; |
| 290 | case DeclarationName::CXXDestructorName: |
| 291 | EKind = DeclarationNameExtra::CXXDestructor; |
| 292 | break; |
| 293 | case DeclarationName::CXXConversionFunctionName: |
| 294 | EKind = DeclarationNameExtra::CXXConversionFunction; |
| 295 | break; |
| 296 | default: |
| 297 | return DeclarationName(); |
| 298 | } |
| 299 | |
| 300 | // Unique selector, to guarantee there is one per name. |
| 301 | llvm::FoldingSetNodeID ID; |
| 302 | ID.AddInteger(EKind); |
| 303 | ID.AddPointer(Ty.getAsOpaquePtr()); |
| 304 | |
| 305 | void *InsertPos = 0; |
| 306 | if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos)) |
| 307 | return DeclarationName(Name); |
| 308 | |
| 309 | CXXSpecialName *SpecialName = new CXXSpecialName; |
| 310 | SpecialName->ExtraKindOrNumArgs = EKind; |
| 311 | SpecialName->Type = Ty; |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 312 | SpecialName->FETokenInfo = 0; |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 313 | |
| 314 | SpecialNames->InsertNode(SpecialName, InsertPos); |
| 315 | return DeclarationName(SpecialName); |
| 316 | } |
| 317 | |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 318 | DeclarationName |
| 319 | DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) { |
| 320 | return DeclarationName(&CXXOperatorNames[(unsigned)Op]); |
| 321 | } |
| 322 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 323 | unsigned |
| 324 | llvm::DenseMapInfo<clang::DeclarationName>:: |
| 325 | getHashValue(clang::DeclarationName N) { |
| 326 | return DenseMapInfo<void*>::getHashValue(N.getAsOpaquePtr()); |
| 327 | } |
| 328 | |