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