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