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