Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 1 | //===- DeclarationName.cpp - Declaration names implementation -------------===// |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 14 | |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclarationName.h" |
Ted Kremenek | 6aead3a | 2010-05-10 20:40:08 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclBase.h" |
Argyrios Kyrtzidis | d571908 | 2016-02-15 01:32:36 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyPrinter.h" |
Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 22 | #include "clang/AST/Type.h" |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeOrdering.h" |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 25 | #include "clang/Basic/IdentifierTable.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 26 | #include "clang/Basic/LLVM.h" |
| 27 | #include "clang/Basic/LangOptions.h" |
| 28 | #include "clang/Basic/OperatorKinds.h" |
| 29 | #include "clang/Basic/SourceLocation.h" |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/FoldingSet.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Casting.h" |
| 32 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | 2b59fbe | 2010-12-15 07:29:18 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 95c7c42 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | #include <cassert> |
| 37 | #include <cstdint> |
| 38 | #include <string> |
| 39 | |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 42 | static int compareInt(unsigned A, unsigned B) { |
| 43 | return (A < B ? -1 : (A > B ? 1 : 0)); |
| 44 | } |
| 45 | |
| 46 | int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) { |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 47 | if (LHS.getNameKind() != RHS.getNameKind()) |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 48 | return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 49 | |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 50 | switch (LHS.getNameKind()) { |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 51 | case DeclarationName::Identifier: { |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 52 | IdentifierInfo *LII = LHS.castAsIdentifierInfo(); |
| 53 | IdentifierInfo *RII = RHS.castAsIdentifierInfo(); |
| 54 | if (!LII) |
| 55 | return RII ? -1 : 0; |
| 56 | if (!RII) |
| 57 | return 1; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 58 | |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 59 | return LII->getName().compare(RII->getName()); |
| 60 | } |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 62 | case DeclarationName::ObjCZeroArgSelector: |
| 63 | case DeclarationName::ObjCOneArgSelector: |
| 64 | case DeclarationName::ObjCMultiArgSelector: { |
| 65 | Selector LHSSelector = LHS.getObjCSelector(); |
| 66 | Selector RHSSelector = RHS.getObjCSelector(); |
Manman Ren | b666573 | 2016-11-17 18:41:18 +0000 | [diff] [blame] | 67 | // getNumArgs for ZeroArgSelector returns 0, but we still need to compare. |
| 68 | if (LHS.getNameKind() == DeclarationName::ObjCZeroArgSelector && |
| 69 | RHS.getNameKind() == DeclarationName::ObjCZeroArgSelector) { |
| 70 | return LHSSelector.getAsIdentifierInfo()->getName().compare( |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 71 | RHSSelector.getAsIdentifierInfo()->getName()); |
Manman Ren | b666573 | 2016-11-17 18:41:18 +0000 | [diff] [blame] | 72 | } |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 73 | unsigned LN = LHSSelector.getNumArgs(), RN = RHSSelector.getNumArgs(); |
| 74 | for (unsigned I = 0, N = std::min(LN, RN); I != N; ++I) { |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 75 | switch (LHSSelector.getNameForSlot(I).compare( |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 76 | RHSSelector.getNameForSlot(I))) { |
| 77 | case -1: |
| 78 | return -1; |
| 79 | case 1: |
| 80 | return 1; |
| 81 | default: |
| 82 | break; |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 85 | |
| 86 | return compareInt(LN, RN); |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 87 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 88 | |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 89 | case DeclarationName::CXXConstructorName: |
| 90 | case DeclarationName::CXXDestructorName: |
| 91 | case DeclarationName::CXXConversionFunctionName: |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 92 | if (QualTypeOrdering()(LHS.getCXXNameType(), RHS.getCXXNameType())) |
| 93 | return -1; |
| 94 | if (QualTypeOrdering()(RHS.getCXXNameType(), LHS.getCXXNameType())) |
| 95 | return 1; |
| 96 | return 0; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 97 | |
| 98 | case DeclarationName::CXXDeductionGuideName: |
| 99 | // We never want to compare deduction guide names for templates from |
| 100 | // different scopes, so just compare the template-name. |
| 101 | return compare(LHS.getCXXDeductionGuideTemplate()->getDeclName(), |
| 102 | RHS.getCXXDeductionGuideTemplate()->getDeclName()); |
| 103 | |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 104 | case DeclarationName::CXXOperatorName: |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 105 | return compareInt(LHS.getCXXOverloadedOperator(), |
| 106 | RHS.getCXXOverloadedOperator()); |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 107 | |
| 108 | case DeclarationName::CXXLiteralOperatorName: |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 109 | return LHS.getCXXLiteralIdentifier()->getName().compare( |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 110 | RHS.getCXXLiteralIdentifier()->getName()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 112 | case DeclarationName::CXXUsingDirective: |
John McCall | ef3057c | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 113 | return 0; |
Douglas Gregor | b082bab | 2009-11-04 22:24:30 +0000 | [diff] [blame] | 114 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 115 | |
| 116 | llvm_unreachable("Invalid DeclarationName Kind!"); |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 119 | static void printCXXConstructorDestructorName(QualType ClassType, |
| 120 | raw_ostream &OS, |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 121 | PrintingPolicy Policy) { |
| 122 | // We know we're printing C++ here. Ensure we print types properly. |
| 123 | Policy.adjustForCPlusPlus(); |
| 124 | |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 125 | if (const RecordType *ClassRec = ClassType->getAs<RecordType>()) { |
| 126 | OS << *ClassRec->getDecl(); |
| 127 | return; |
| 128 | } |
Argyrios Kyrtzidis | d571908 | 2016-02-15 01:32:36 +0000 | [diff] [blame] | 129 | if (Policy.SuppressTemplateArgsInCXXConstructors) { |
| 130 | if (auto *InjTy = ClassType->getAs<InjectedClassNameType>()) { |
| 131 | OS << *InjTy->getDecl(); |
| 132 | return; |
| 133 | } |
| 134 | } |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 135 | ClassType.print(OS, Policy); |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void DeclarationName::print(raw_ostream &OS, const PrintingPolicy &Policy) { |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 139 | switch (getNameKind()) { |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 140 | case DeclarationName::Identifier: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 141 | if (const IdentifierInfo *II = getAsIdentifierInfo()) |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 142 | OS << II->getName(); |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 143 | return; |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 144 | |
| 145 | case DeclarationName::ObjCZeroArgSelector: |
| 146 | case DeclarationName::ObjCOneArgSelector: |
| 147 | case DeclarationName::ObjCMultiArgSelector: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 148 | getObjCSelector().print(OS); |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 149 | return; |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 150 | |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 151 | case DeclarationName::CXXConstructorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 152 | return printCXXConstructorDestructorName(getCXXNameType(), OS, Policy); |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 153 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 154 | case DeclarationName::CXXDestructorName: |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 155 | OS << '~'; |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 156 | return printCXXConstructorDestructorName(getCXXNameType(), OS, Policy); |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 157 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 158 | case DeclarationName::CXXDeductionGuideName: |
Richard Smith | f283fdc | 2017-02-08 00:35:25 +0000 | [diff] [blame] | 159 | OS << "<deduction guide for "; |
| 160 | getCXXDeductionGuideTemplate()->getDeclName().print(OS, Policy); |
| 161 | OS << '>'; |
| 162 | return; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 163 | |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 164 | case DeclarationName::CXXOperatorName: { |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 165 | static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = { |
| 166 | nullptr, |
| 167 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 168 | Spelling, |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 169 | #include "clang/Basic/OperatorKinds.def" |
| 170 | }; |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 171 | const char *OpName = OperatorNames[getCXXOverloadedOperator()]; |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 172 | assert(OpName && "not an overloaded operator"); |
| 173 | |
| 174 | OS << "operator"; |
| 175 | if (OpName[0] >= 'a' && OpName[0] <= 'z') |
| 176 | OS << ' '; |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 177 | OS << OpName; |
| 178 | return; |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | case DeclarationName::CXXLiteralOperatorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 182 | OS << "operator\"\"" << getCXXLiteralIdentifier()->getName(); |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 183 | return; |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 184 | |
| 185 | case DeclarationName::CXXConversionFunctionName: { |
| 186 | OS << "operator "; |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 187 | QualType Type = getCXXNameType(); |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 188 | if (const RecordType *Rec = Type->getAs<RecordType>()) { |
| 189 | OS << *Rec->getDecl(); |
| 190 | return; |
| 191 | } |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 192 | // We know we're printing C++ here, ensure we print 'bool' properly. |
| 193 | PrintingPolicy CXXPolicy = Policy; |
| 194 | CXXPolicy.adjustForCPlusPlus(); |
| 195 | Type.print(OS, CXXPolicy); |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 196 | return; |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 197 | } |
| 198 | case DeclarationName::CXXUsingDirective: |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 199 | OS << "<using-directive>"; |
| 200 | return; |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | llvm_unreachable("Unexpected declaration name kind"); |
| 204 | } |
| 205 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 206 | namespace clang { |
| 207 | |
Argyrios Kyrtzidis | e91793c | 2016-02-13 21:46:50 +0000 | [diff] [blame] | 208 | raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) { |
| 209 | LangOptions LO; |
| 210 | N.print(OS, PrintingPolicy(LO)); |
| 211 | return OS; |
| 212 | } |
| 213 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 214 | } // namespace clang |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 215 | |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 216 | bool DeclarationName::isDependentName() const { |
| 217 | QualType T = getCXXNameType(); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 218 | if (!T.isNull() && T->isDependentType()) |
| 219 | return true; |
| 220 | |
| 221 | // A class-scope deduction guide in a dependent context has a dependent name. |
| 222 | auto *TD = getCXXDeductionGuideTemplate(); |
| 223 | if (TD && TD->getDeclContext()->isDependentContext()) |
| 224 | return true; |
| 225 | |
| 226 | return false; |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 229 | std::string DeclarationName::getAsString() const { |
Benjamin Kramer | 95c7c42 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 230 | std::string Result; |
| 231 | llvm::raw_string_ostream OS(Result); |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 232 | OS << *this; |
Benjamin Kramer | 95c7c42 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 233 | return OS.str(); |
| 234 | } |
| 235 | |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 236 | void *DeclarationName::getFETokenInfoSlow() const { |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 237 | switch (getNameKind()) { |
| 238 | case Identifier: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 239 | llvm_unreachable("case Identifier already handled by getFETokenInfo!"); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 240 | case CXXConstructorName: |
| 241 | case CXXDestructorName: |
| 242 | case CXXConversionFunctionName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 243 | return castAsCXXSpecialNameExtra()->FETokenInfo; |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 244 | case CXXOperatorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 245 | return castAsCXXOperatorIdName()->FETokenInfo; |
| 246 | case CXXDeductionGuideName: |
| 247 | return castAsCXXDeductionGuideNameExtra()->FETokenInfo; |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 248 | case CXXLiteralOperatorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 249 | return castAsCXXLiteralOperatorIdName()->FETokenInfo; |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 250 | default: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 251 | llvm_unreachable("DeclarationName has no FETokenInfo!"); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 252 | } |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 255 | void DeclarationName::setFETokenInfoSlow(void *T) { |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 256 | switch (getNameKind()) { |
| 257 | case Identifier: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 258 | llvm_unreachable("case Identifier already handled by setFETokenInfo!"); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 259 | case CXXConstructorName: |
| 260 | case CXXDestructorName: |
| 261 | case CXXConversionFunctionName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 262 | castAsCXXSpecialNameExtra()->FETokenInfo = T; |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 263 | break; |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 264 | case CXXOperatorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 265 | castAsCXXOperatorIdName()->FETokenInfo = T; |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 266 | break; |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 267 | case CXXDeductionGuideName: |
| 268 | castAsCXXDeductionGuideNameExtra()->FETokenInfo = T; |
| 269 | break; |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 270 | case CXXLiteralOperatorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 271 | castAsCXXLiteralOperatorIdName()->FETokenInfo = T; |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 272 | break; |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 273 | default: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 274 | llvm_unreachable("DeclarationName has no FETokenInfo!"); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 278 | LLVM_DUMP_METHOD void DeclarationName::dump() const { |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 279 | llvm::errs() << *this << '\n'; |
Anders Carlsson | 1b69be2 | 2009-11-15 22:30:43 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 282 | DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) { |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 283 | // Initialize the overloaded operator names. |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 284 | for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) |
| 285 | CXXOperatorNames[Op].Kind = static_cast<OverloadedOperatorKind>(Op); |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | DeclarationName |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 289 | DeclarationNameTable::getCXXDeductionGuideName(TemplateDecl *Template) { |
| 290 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 291 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 292 | llvm::FoldingSetNodeID ID; |
| 293 | ID.AddPointer(Template); |
| 294 | |
| 295 | void *InsertPos = nullptr; |
Bruno Ricci | b619883 | 2018-08-06 16:47:31 +0000 | [diff] [blame] | 296 | if (auto *Name = CXXDeductionGuideNames.FindNodeOrInsertPos(ID, InsertPos)) |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 297 | return DeclarationName(Name); |
| 298 | |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 299 | auto *Name = new (Ctx) detail::CXXDeductionGuideNameExtra(Template); |
Bruno Ricci | b619883 | 2018-08-06 16:47:31 +0000 | [diff] [blame] | 300 | CXXDeductionGuideNames.InsertNode(Name, InsertPos); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 301 | return DeclarationName(Name); |
| 302 | } |
| 303 | |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 304 | DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) { |
| 305 | // The type of constructors is unqualified. |
| 306 | Ty = Ty.getUnqualifiedType(); |
| 307 | // Do we already have this C++ constructor name ? |
| 308 | llvm::FoldingSetNodeID ID; |
| 309 | ID.AddPointer(Ty.getAsOpaquePtr()); |
| 310 | void *InsertPos = nullptr; |
| 311 | if (auto *Name = CXXConstructorNames.FindNodeOrInsertPos(ID, InsertPos)) |
| 312 | return {Name, DeclarationName::StoredCXXConstructorName}; |
| 313 | |
| 314 | // We have to create it. |
| 315 | auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty); |
| 316 | CXXConstructorNames.InsertNode(SpecialName, InsertPos); |
| 317 | return {SpecialName, DeclarationName::StoredCXXConstructorName}; |
| 318 | } |
| 319 | |
| 320 | DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) { |
| 321 | // The type of destructors is unqualified. |
| 322 | Ty = Ty.getUnqualifiedType(); |
| 323 | // Do we already have this C++ destructor name ? |
| 324 | llvm::FoldingSetNodeID ID; |
| 325 | ID.AddPointer(Ty.getAsOpaquePtr()); |
| 326 | void *InsertPos = nullptr; |
| 327 | if (auto *Name = CXXDestructorNames.FindNodeOrInsertPos(ID, InsertPos)) |
| 328 | return {Name, DeclarationName::StoredCXXDestructorName}; |
| 329 | |
| 330 | // We have to create it. |
| 331 | auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty); |
| 332 | CXXDestructorNames.InsertNode(SpecialName, InsertPos); |
| 333 | return {SpecialName, DeclarationName::StoredCXXDestructorName}; |
| 334 | } |
| 335 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 336 | DeclarationName |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 337 | DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) { |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 338 | // Do we already have this C++ conversion function name ? |
| 339 | llvm::FoldingSetNodeID ID; |
| 340 | ID.AddPointer(Ty.getAsOpaquePtr()); |
| 341 | void *InsertPos = nullptr; |
| 342 | if (auto *Name = |
| 343 | CXXConversionFunctionNames.FindNodeOrInsertPos(ID, InsertPos)) |
| 344 | return {Name, DeclarationName::StoredCXXConversionFunctionName}; |
| 345 | |
| 346 | // We have to create it. |
| 347 | auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty); |
| 348 | CXXConversionFunctionNames.InsertNode(SpecialName, InsertPos); |
| 349 | return {SpecialName, DeclarationName::StoredCXXConversionFunctionName}; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | DeclarationName |
| 353 | DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind, |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 354 | CanQualType Ty) { |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 355 | switch (Kind) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | case DeclarationName::CXXConstructorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 357 | return getCXXConstructorName(Ty); |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 358 | case DeclarationName::CXXDestructorName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 359 | return getCXXDestructorName(Ty); |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 360 | case DeclarationName::CXXConversionFunctionName: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 361 | return getCXXConversionFunctionName(Ty); |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 362 | default: |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 363 | llvm_unreachable("Invalid kind in getCXXSpecialName!"); |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 364 | } |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 367 | DeclarationName |
| 368 | DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) { |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 369 | llvm::FoldingSetNodeID ID; |
| 370 | ID.AddPointer(II); |
| 371 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 372 | void *InsertPos = nullptr; |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 373 | if (auto *Name = CXXLiteralOperatorNames.FindNodeOrInsertPos(ID, InsertPos)) |
| 374 | return DeclarationName(Name); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 375 | |
Bruno Ricci | 366ba73 | 2018-09-21 12:53:22 +0000 | [diff] [blame] | 376 | auto *LiteralName = new (Ctx) detail::CXXLiteralOperatorIdName(II); |
Bruno Ricci | b619883 | 2018-08-06 16:47:31 +0000 | [diff] [blame] | 377 | CXXLiteralOperatorNames.InsertNode(LiteralName, InsertPos); |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 378 | return DeclarationName(LiteralName); |
| 379 | } |
| 380 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 381 | DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) { |
| 382 | switch (Name.getNameKind()) { |
| 383 | case DeclarationName::Identifier: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 384 | case DeclarationName::CXXDeductionGuideName: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 385 | break; |
| 386 | case DeclarationName::CXXConstructorName: |
| 387 | case DeclarationName::CXXDestructorName: |
| 388 | case DeclarationName::CXXConversionFunctionName: |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 389 | NamedType.TInfo = nullptr; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 390 | break; |
| 391 | case DeclarationName::CXXOperatorName: |
| 392 | CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 393 | CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 394 | break; |
| 395 | case DeclarationName::CXXLiteralOperatorName: |
| 396 | CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding(); |
| 397 | break; |
| 398 | case DeclarationName::ObjCZeroArgSelector: |
| 399 | case DeclarationName::ObjCOneArgSelector: |
| 400 | case DeclarationName::ObjCMultiArgSelector: |
| 401 | // FIXME: ? |
| 402 | break; |
| 403 | case DeclarationName::CXXUsingDirective: |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 408 | bool DeclarationNameInfo::containsUnexpandedParameterPack() const { |
| 409 | switch (Name.getNameKind()) { |
| 410 | case DeclarationName::Identifier: |
| 411 | case DeclarationName::ObjCZeroArgSelector: |
| 412 | case DeclarationName::ObjCOneArgSelector: |
| 413 | case DeclarationName::ObjCMultiArgSelector: |
| 414 | case DeclarationName::CXXOperatorName: |
| 415 | case DeclarationName::CXXLiteralOperatorName: |
| 416 | case DeclarationName::CXXUsingDirective: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 417 | case DeclarationName::CXXDeductionGuideName: |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 418 | return false; |
| 419 | |
| 420 | case DeclarationName::CXXConstructorName: |
| 421 | case DeclarationName::CXXDestructorName: |
| 422 | case DeclarationName::CXXConversionFunctionName: |
| 423 | if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) |
| 424 | return TInfo->getType()->containsUnexpandedParameterPack(); |
| 425 | |
| 426 | return Name.getCXXNameType()->containsUnexpandedParameterPack(); |
| 427 | } |
Chandler Carruth | 2b59fbe | 2010-12-15 07:29:18 +0000 | [diff] [blame] | 428 | llvm_unreachable("All name kinds handled."); |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 431 | bool DeclarationNameInfo::isInstantiationDependent() const { |
| 432 | switch (Name.getNameKind()) { |
| 433 | case DeclarationName::Identifier: |
| 434 | case DeclarationName::ObjCZeroArgSelector: |
| 435 | case DeclarationName::ObjCOneArgSelector: |
| 436 | case DeclarationName::ObjCMultiArgSelector: |
| 437 | case DeclarationName::CXXOperatorName: |
| 438 | case DeclarationName::CXXLiteralOperatorName: |
| 439 | case DeclarationName::CXXUsingDirective: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 440 | case DeclarationName::CXXDeductionGuideName: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 441 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 442 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 443 | case DeclarationName::CXXConstructorName: |
| 444 | case DeclarationName::CXXDestructorName: |
| 445 | case DeclarationName::CXXConversionFunctionName: |
| 446 | if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) |
| 447 | return TInfo->getType()->isInstantiationDependentType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 449 | return Name.getCXXNameType()->isInstantiationDependentType(); |
| 450 | } |
| 451 | llvm_unreachable("All name kinds handled."); |
| 452 | } |
| 453 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 454 | std::string DeclarationNameInfo::getAsString() const { |
| 455 | std::string Result; |
| 456 | llvm::raw_string_ostream OS(Result); |
| 457 | printName(OS); |
| 458 | return OS.str(); |
| 459 | } |
| 460 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 461 | void DeclarationNameInfo::printName(raw_ostream &OS) const { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 462 | switch (Name.getNameKind()) { |
| 463 | case DeclarationName::Identifier: |
| 464 | case DeclarationName::ObjCZeroArgSelector: |
| 465 | case DeclarationName::ObjCOneArgSelector: |
| 466 | case DeclarationName::ObjCMultiArgSelector: |
| 467 | case DeclarationName::CXXOperatorName: |
| 468 | case DeclarationName::CXXLiteralOperatorName: |
| 469 | case DeclarationName::CXXUsingDirective: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 470 | case DeclarationName::CXXDeductionGuideName: |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 471 | OS << Name; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 472 | return; |
| 473 | |
| 474 | case DeclarationName::CXXConstructorName: |
| 475 | case DeclarationName::CXXDestructorName: |
| 476 | case DeclarationName::CXXConversionFunctionName: |
| 477 | if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) { |
| 478 | if (Name.getNameKind() == DeclarationName::CXXDestructorName) |
| 479 | OS << '~'; |
| 480 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
| 481 | OS << "operator "; |
Richard Smith | e81daee | 2014-01-22 00:27:42 +0000 | [diff] [blame] | 482 | LangOptions LO; |
| 483 | LO.CPlusPlus = true; |
Benjamin Kramer | 00e8a19 | 2014-02-25 18:03:55 +0000 | [diff] [blame] | 484 | LO.Bool = true; |
Alex Lorenz | a981c7d | 2017-04-11 16:46:03 +0000 | [diff] [blame] | 485 | PrintingPolicy PP(LO); |
| 486 | PP.SuppressScope = true; |
| 487 | OS << TInfo->getType().getAsString(PP); |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 488 | } else |
| 489 | OS << Name; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 490 | return; |
| 491 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 492 | llvm_unreachable("Unexpected declaration name kind"); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Stephen Kelly | f50288c | 2018-07-30 20:39:14 +0000 | [diff] [blame] | 495 | SourceLocation DeclarationNameInfo::getEndLocPrivate() const { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 496 | switch (Name.getNameKind()) { |
| 497 | case DeclarationName::Identifier: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 498 | case DeclarationName::CXXDeductionGuideName: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 499 | return NameLoc; |
| 500 | |
| 501 | case DeclarationName::CXXOperatorName: { |
| 502 | unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc; |
| 503 | return SourceLocation::getFromRawEncoding(raw); |
| 504 | } |
| 505 | |
| 506 | case DeclarationName::CXXLiteralOperatorName: { |
| 507 | unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc; |
| 508 | return SourceLocation::getFromRawEncoding(raw); |
| 509 | } |
| 510 | |
| 511 | case DeclarationName::CXXConstructorName: |
| 512 | case DeclarationName::CXXDestructorName: |
| 513 | case DeclarationName::CXXConversionFunctionName: |
| 514 | if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) |
| 515 | return TInfo->getTypeLoc().getEndLoc(); |
| 516 | else |
| 517 | return NameLoc; |
| 518 | |
| 519 | // DNInfo work in progress: FIXME. |
| 520 | case DeclarationName::ObjCZeroArgSelector: |
| 521 | case DeclarationName::ObjCOneArgSelector: |
| 522 | case DeclarationName::ObjCMultiArgSelector: |
| 523 | case DeclarationName::CXXUsingDirective: |
| 524 | return NameLoc; |
| 525 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 526 | llvm_unreachable("Unexpected declaration name kind"); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 527 | } |