blob: f6d045b2dab20262bd779524b10f08105f06eda7 [file] [log] [blame]
Douglas Gregor77324f32008-11-17 14:58:09 +00001//===-- 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 Kremenek6aead3a2010-05-10 20:40:08 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Douglas Gregor77324f32008-11-17 14:58:09 +000016#include "clang/AST/DeclarationName.h"
Douglas Gregor92751d42008-11-17 22:58:34 +000017#include "clang/AST/Type.h"
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregorb082bab2009-11-04 22:24:30 +000019#include "clang/AST/TypeOrdering.h"
Douglas Gregor77324f32008-11-17 14:58:09 +000020#include "clang/Basic/IdentifierTable.h"
Douglas Gregor48db39d2009-04-22 21:45:53 +000021#include "llvm/ADT/DenseMap.h"
Douglas Gregor77324f32008-11-17 14:58:09 +000022#include "llvm/ADT/FoldingSet.h"
Chandler Carruth2b59fbe2010-12-15 07:29:18 +000023#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer95c7c422010-04-17 09:56:45 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregor77324f32008-11-17 14:58:09 +000025using namespace clang;
26
27namespace 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 Stump11289f42009-09-09 15:08:12 +000031class CXXSpecialName
Douglas Gregor77324f32008-11-17 14:58:09 +000032 : public DeclarationNameExtra, public llvm::FoldingSetNode {
33public:
Douglas Gregorae2fbad2008-11-17 20:34:05 +000034 /// Type - The type associated with this declaration name.
Douglas Gregor77324f32008-11-17 14:58:09 +000035 QualType Type;
36
Douglas Gregorae2fbad2008-11-17 20:34:05 +000037 /// FETokenInfo - Extra information associated with this declaration
38 /// name that can be used by the front end.
39 void *FETokenInfo;
40
Douglas Gregor77324f32008-11-17 14:58:09 +000041 void Profile(llvm::FoldingSetNodeID &ID) {
42 ID.AddInteger(ExtraKindOrNumArgs);
43 ID.AddPointer(Type.getAsOpaquePtr());
44 }
45};
46
Douglas Gregor163c5852008-11-18 14:39:36 +000047/// CXXOperatorIdName - Contains extra information for the name of an
Mike Stump11289f42009-09-09 15:08:12 +000048/// overloaded operator in C++, such as "operator+.
Douglas Gregor163c5852008-11-18 14:39:36 +000049class CXXOperatorIdName : public DeclarationNameExtra {
50public:
51 /// FETokenInfo - Extra information associated with this operator
52 /// name that can be used by the front end.
53 void *FETokenInfo;
54};
55
Richard Smithc1b05652012-03-09 08:37:16 +000056/// CXXLiteralOperatorName - Contains the actual identifier that makes up the
Alexis Hunt3d221f22009-11-29 07:34:05 +000057/// 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.
Alexis Huntc88db062010-01-13 09:01:02 +000062class CXXLiteralOperatorIdName
63 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Alexis Hunt3d221f22009-11-29 07:34:05 +000064public:
65 IdentifierInfo *ID;
Alexis Huntc88db062010-01-13 09:01:02 +000066
Richard Smithc1b05652012-03-09 08:37:16 +000067 /// FETokenInfo - Extra information associated with this operator
68 /// name that can be used by the front end.
69 void *FETokenInfo;
70
Alexis Huntc88db062010-01-13 09:01:02 +000071 void Profile(llvm::FoldingSetNodeID &FSID) {
72 FSID.AddPointer(ID);
73 }
Alexis Hunt3d221f22009-11-29 07:34:05 +000074};
75
John McCallef3057c2010-02-13 01:04:05 +000076static int compareInt(unsigned A, unsigned B) {
77 return (A < B ? -1 : (A > B ? 1 : 0));
78}
79
80int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) {
Douglas Gregorb082bab2009-11-04 22:24:30 +000081 if (LHS.getNameKind() != RHS.getNameKind())
John McCallef3057c2010-02-13 01:04:05 +000082 return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1);
Douglas Gregorb082bab2009-11-04 22:24:30 +000083
84 switch (LHS.getNameKind()) {
John McCallef3057c2010-02-13 01:04:05 +000085 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 Gregor77324f32008-11-17 14:58:09 +000093
Douglas Gregorb082bab2009-11-04 22:24:30 +000094 case DeclarationName::ObjCZeroArgSelector:
95 case DeclarationName::ObjCOneArgSelector:
96 case DeclarationName::ObjCMultiArgSelector: {
97 Selector LHSSelector = LHS.getObjCSelector();
98 Selector RHSSelector = RHS.getObjCSelector();
John McCallef3057c2010-02-13 01:04:05 +000099 unsigned LN = LHSSelector.getNumArgs(), RN = RHSSelector.getNumArgs();
100 for (unsigned I = 0, N = std::min(LN, RN); I != N; ++I) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000101 switch (LHSSelector.getNameForSlot(I).compare(
102 RHSSelector.getNameForSlot(I))) {
Douglas Gregorb082bab2009-11-04 22:24:30 +0000103 case -1: return true;
104 case 1: return false;
105 default: break;
106 }
107 }
John McCallef3057c2010-02-13 01:04:05 +0000108
109 return compareInt(LN, RN);
Douglas Gregorb082bab2009-11-04 22:24:30 +0000110 }
111
112 case DeclarationName::CXXConstructorName:
113 case DeclarationName::CXXDestructorName:
114 case DeclarationName::CXXConversionFunctionName:
John McCallef3057c2010-02-13 01:04:05 +0000115 if (QualTypeOrdering()(LHS.getCXXNameType(), RHS.getCXXNameType()))
116 return -1;
117 if (QualTypeOrdering()(RHS.getCXXNameType(), LHS.getCXXNameType()))
118 return 1;
119 return 0;
Douglas Gregorb082bab2009-11-04 22:24:30 +0000120
121 case DeclarationName::CXXOperatorName:
John McCallef3057c2010-02-13 01:04:05 +0000122 return compareInt(LHS.getCXXOverloadedOperator(),
123 RHS.getCXXOverloadedOperator());
Alexis Hunt3d221f22009-11-29 07:34:05 +0000124
125 case DeclarationName::CXXLiteralOperatorName:
John McCallef3057c2010-02-13 01:04:05 +0000126 return LHS.getCXXLiteralIdentifier()->getName().compare(
127 RHS.getCXXLiteralIdentifier()->getName());
Douglas Gregorb082bab2009-11-04 22:24:30 +0000128
129 case DeclarationName::CXXUsingDirective:
John McCallef3057c2010-02-13 01:04:05 +0000130 return 0;
Douglas Gregorb082bab2009-11-04 22:24:30 +0000131 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000132
133 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor77324f32008-11-17 14:58:09 +0000134}
135
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000136static void printCXXConstructorDestructorName(QualType ClassType,
137 raw_ostream &OS,
138 const PrintingPolicy &Policy) {
139 if (const RecordType *ClassRec = ClassType->getAs<RecordType>()) {
140 OS << *ClassRec->getDecl();
141 return;
142 }
143 if (!Policy.LangOpts.CPlusPlus) {
144 // Passed policy is the default one from operator <<, use a C++ policy.
145 LangOptions LO;
146 LO.CPlusPlus = true;
147 ClassType.print(OS, PrintingPolicy(LO));
148 } else {
149 ClassType.print(OS, Policy);
150 }
151}
152
153void DeclarationName::print(raw_ostream &OS, const PrintingPolicy &Policy) {
154 DeclarationName &N = *this;
David Blaikied4da8722013-05-14 21:04:00 +0000155 switch (N.getNameKind()) {
156 case DeclarationName::Identifier:
157 if (const IdentifierInfo *II = N.getAsIdentifierInfo())
158 OS << II->getName();
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000159 return;
David Blaikied4da8722013-05-14 21:04:00 +0000160
161 case DeclarationName::ObjCZeroArgSelector:
162 case DeclarationName::ObjCOneArgSelector:
163 case DeclarationName::ObjCMultiArgSelector:
Aaron Ballmanb190f972014-01-03 17:59:55 +0000164 N.getObjCSelector().print(OS);
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000165 return;
David Blaikied4da8722013-05-14 21:04:00 +0000166
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000167 case DeclarationName::CXXConstructorName:
168 return printCXXConstructorDestructorName(N.getCXXNameType(), OS, Policy);
David Blaikied4da8722013-05-14 21:04:00 +0000169
170 case DeclarationName::CXXDestructorName: {
171 OS << '~';
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000172 return printCXXConstructorDestructorName(N.getCXXNameType(), OS, Policy);
David Blaikied4da8722013-05-14 21:04:00 +0000173 }
174
175 case DeclarationName::CXXOperatorName: {
176 static const char* const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
Craig Topper36250ad2014-05-12 05:36:57 +0000177 nullptr,
David Blaikied4da8722013-05-14 21:04:00 +0000178#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
179 Spelling,
180#include "clang/Basic/OperatorKinds.def"
181 };
182 const char *OpName = OperatorNames[N.getCXXOverloadedOperator()];
183 assert(OpName && "not an overloaded operator");
184
185 OS << "operator";
186 if (OpName[0] >= 'a' && OpName[0] <= 'z')
187 OS << ' ';
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000188 OS << OpName;
189 return;
David Blaikied4da8722013-05-14 21:04:00 +0000190 }
191
192 case DeclarationName::CXXLiteralOperatorName:
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000193 OS << "operator\"\"" << N.getCXXLiteralIdentifier()->getName();
194 return;
David Blaikied4da8722013-05-14 21:04:00 +0000195
196 case DeclarationName::CXXConversionFunctionName: {
197 OS << "operator ";
198 QualType Type = N.getCXXNameType();
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000199 if (const RecordType *Rec = Type->getAs<RecordType>()) {
200 OS << *Rec->getDecl();
201 return;
202 }
203 if (!Policy.LangOpts.CPlusPlus) {
204 // Passed policy is the default one from operator <<, use a C++ policy.
205 LangOptions LO;
206 LO.CPlusPlus = true;
207 LO.Bool = true;
208 Type.print(OS, PrintingPolicy(LO));
209 } else {
210 Type.print(OS, Policy);
211 }
212 return;
David Blaikied4da8722013-05-14 21:04:00 +0000213 }
214 case DeclarationName::CXXUsingDirective:
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000215 OS << "<using-directive>";
216 return;
David Blaikied4da8722013-05-14 21:04:00 +0000217 }
218
219 llvm_unreachable("Unexpected declaration name kind");
220}
221
Argyrios Kyrtzidise91793c2016-02-13 21:46:50 +0000222raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) {
223 LangOptions LO;
224 N.print(OS, PrintingPolicy(LO));
225 return OS;
226}
227
Douglas Gregor77324f32008-11-17 14:58:09 +0000228} // end namespace clang
229
Douglas Gregor77324f32008-11-17 14:58:09 +0000230DeclarationName::NameKind DeclarationName::getNameKind() const {
231 switch (getStoredNameKind()) {
232 case StoredIdentifier: return Identifier;
233 case StoredObjCZeroArgSelector: return ObjCZeroArgSelector;
234 case StoredObjCOneArgSelector: return ObjCOneArgSelector;
235
Douglas Gregor163c5852008-11-18 14:39:36 +0000236 case StoredDeclarationNameExtra:
Douglas Gregor77324f32008-11-17 14:58:09 +0000237 switch (getExtra()->ExtraKindOrNumArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000238 case DeclarationNameExtra::CXXConstructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000239 return CXXConstructorName;
240
Mike Stump11289f42009-09-09 15:08:12 +0000241 case DeclarationNameExtra::CXXDestructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000242 return CXXDestructorName;
243
Mike Stump11289f42009-09-09 15:08:12 +0000244 case DeclarationNameExtra::CXXConversionFunction:
Douglas Gregor77324f32008-11-17 14:58:09 +0000245 return CXXConversionFunctionName;
246
Alexis Hunt3d221f22009-11-29 07:34:05 +0000247 case DeclarationNameExtra::CXXLiteralOperator:
248 return CXXLiteralOperatorName;
249
Douglas Gregor889ceb72009-02-03 19:21:40 +0000250 case DeclarationNameExtra::CXXUsingDirective:
251 return CXXUsingDirective;
252
Douglas Gregor77324f32008-11-17 14:58:09 +0000253 default:
Douglas Gregor163c5852008-11-18 14:39:36 +0000254 // Check if we have one of the CXXOperator* enumeration values.
Mike Stump11289f42009-09-09 15:08:12 +0000255 if (getExtra()->ExtraKindOrNumArgs <
Douglas Gregor889ceb72009-02-03 19:21:40 +0000256 DeclarationNameExtra::CXXUsingDirective)
Douglas Gregor163c5852008-11-18 14:39:36 +0000257 return CXXOperatorName;
258
Douglas Gregor77324f32008-11-17 14:58:09 +0000259 return ObjCMultiArgSelector;
260 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000261 }
262
263 // Can't actually get here.
David Blaikie83d382b2011-09-23 05:06:16 +0000264 llvm_unreachable("This should be unreachable!");
Douglas Gregor77324f32008-11-17 14:58:09 +0000265}
266
Douglas Gregorea0a0a92010-01-11 18:40:55 +0000267bool DeclarationName::isDependentName() const {
268 QualType T = getCXXNameType();
269 return !T.isNull() && T->isDependentType();
270}
271
Douglas Gregor92751d42008-11-17 22:58:34 +0000272std::string DeclarationName::getAsString() const {
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000273 std::string Result;
274 llvm::raw_string_ostream OS(Result);
David Blaikied4da8722013-05-14 21:04:00 +0000275 OS << *this;
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000276 return OS.str();
277}
278
Douglas Gregor77324f32008-11-17 14:58:09 +0000279QualType DeclarationName::getCXXNameType() const {
280 if (CXXSpecialName *CXXName = getAsCXXSpecialName())
281 return CXXName->Type;
282 else
283 return QualType();
284}
285
Douglas Gregor163c5852008-11-18 14:39:36 +0000286OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const {
287 if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) {
Mike Stump11289f42009-09-09 15:08:12 +0000288 unsigned value
Douglas Gregor163c5852008-11-18 14:39:36 +0000289 = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction;
290 return static_cast<OverloadedOperatorKind>(value);
291 } else {
292 return OO_None;
293 }
294}
295
Alexis Hunt3d221f22009-11-29 07:34:05 +0000296IdentifierInfo *DeclarationName::getCXXLiteralIdentifier() const {
297 if (CXXLiteralOperatorIdName *CXXLit = getAsCXXLiteralOperatorIdName())
298 return CXXLit->ID;
299 else
Craig Topper36250ad2014-05-12 05:36:57 +0000300 return nullptr;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000301}
302
Douglas Gregor0da53052012-05-03 23:18:44 +0000303void *DeclarationName::getFETokenInfoAsVoidSlow() const {
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000304 switch (getNameKind()) {
305 case Identifier:
Benjamin Kramer1458c1c2012-05-19 16:03:58 +0000306 llvm_unreachable("Handled by getFETokenInfo()");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000307
308 case CXXConstructorName:
309 case CXXDestructorName:
310 case CXXConversionFunctionName:
311 return getAsCXXSpecialName()->FETokenInfo;
312
Douglas Gregor163c5852008-11-18 14:39:36 +0000313 case CXXOperatorName:
314 return getAsCXXOperatorIdName()->FETokenInfo;
315
Alexis Hunt3d221f22009-11-29 07:34:05 +0000316 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000317 return getAsCXXLiteralOperatorIdName()->FETokenInfo;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000318
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000319 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000320 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000321 }
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000322}
323
324void DeclarationName::setFETokenInfo(void *T) {
325 switch (getNameKind()) {
326 case Identifier:
327 getAsIdentifierInfo()->setFETokenInfo(T);
328 break;
329
330 case CXXConstructorName:
331 case CXXDestructorName:
332 case CXXConversionFunctionName:
333 getAsCXXSpecialName()->FETokenInfo = T;
334 break;
335
Douglas Gregor163c5852008-11-18 14:39:36 +0000336 case CXXOperatorName:
337 getAsCXXOperatorIdName()->FETokenInfo = T;
338 break;
339
Alexis Hunt3d221f22009-11-29 07:34:05 +0000340 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000341 getAsCXXLiteralOperatorIdName()->FETokenInfo = T;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000342 break;
343
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000344 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000345 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000346 }
347}
348
Douglas Gregor889ceb72009-02-03 19:21:40 +0000349DeclarationName DeclarationName::getUsingDirectiveName() {
350 // Single instance of DeclarationNameExtra for using-directive
Nuno Lopes221c1fd2009-12-10 00:07:02 +0000351 static const DeclarationNameExtra UDirExtra =
Douglas Gregor889ceb72009-02-03 19:21:40 +0000352 { DeclarationNameExtra::CXXUsingDirective };
353
354 uintptr_t Ptr = reinterpret_cast<uintptr_t>(&UDirExtra);
355 Ptr |= StoredDeclarationNameExtra;
356
357 return DeclarationName(Ptr);
358}
359
Yaron Kerencdae9412016-01-29 19:38:18 +0000360LLVM_DUMP_METHOD void DeclarationName::dump() const {
David Blaikied4da8722013-05-14 21:04:00 +0000361 llvm::errs() << *this << '\n';
Anders Carlsson1b69be22009-11-15 22:30:43 +0000362}
363
Jay Foad39c79802011-01-12 09:06:06 +0000364DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000365 CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>;
Alexis Huntc88db062010-01-13 09:01:02 +0000366 CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>;
Douglas Gregor163c5852008-11-18 14:39:36 +0000367
368 // Initialize the overloaded operator names.
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000369 CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS];
Douglas Gregor163c5852008-11-18 14:39:36 +0000370 for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) {
Mike Stump11289f42009-09-09 15:08:12 +0000371 CXXOperatorNames[Op].ExtraKindOrNumArgs
Douglas Gregor163c5852008-11-18 14:39:36 +0000372 = Op + DeclarationNameExtra::CXXConversionFunction;
Craig Topper36250ad2014-05-12 05:36:57 +0000373 CXXOperatorNames[Op].FETokenInfo = nullptr;
Douglas Gregor163c5852008-11-18 14:39:36 +0000374 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000375}
376
377DeclarationNameTable::~DeclarationNameTable() {
Alexis Huntc88db062010-01-13 09:01:02 +0000378 llvm::FoldingSet<CXXSpecialName> *SpecialNames =
Nuno Lopes127adb42008-12-14 17:27:25 +0000379 static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
Alexis Huntc88db062010-01-13 09:01:02 +0000380 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
381 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000382 (CXXLiteralOperatorNames);
Alexis Huntc88db062010-01-13 09:01:02 +0000383
Alexis Huntc88db062010-01-13 09:01:02 +0000384 delete SpecialNames;
385 delete LiteralNames;
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000386}
387
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +0000388DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
389 return getCXXSpecialName(DeclarationName::CXXConstructorName,
390 Ty.getUnqualifiedType());
391}
392
393DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) {
394 return getCXXSpecialName(DeclarationName::CXXDestructorName,
395 Ty.getUnqualifiedType());
396}
397
398DeclarationName
399DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) {
400 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
401}
402
Mike Stump11289f42009-09-09 15:08:12 +0000403DeclarationName
404DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
Douglas Gregor2211d342009-08-05 05:36:45 +0000405 CanQualType Ty) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000406 assert(Kind >= DeclarationName::CXXConstructorName &&
407 Kind <= DeclarationName::CXXConversionFunctionName &&
408 "Kind must be a C++ special name kind");
Mike Stump11289f42009-09-09 15:08:12 +0000409 llvm::FoldingSet<CXXSpecialName> *SpecialNames
Douglas Gregor77324f32008-11-17 14:58:09 +0000410 = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
411
412 DeclarationNameExtra::ExtraKind EKind;
413 switch (Kind) {
Mike Stump11289f42009-09-09 15:08:12 +0000414 case DeclarationName::CXXConstructorName:
Douglas Gregor77324f32008-11-17 14:58:09 +0000415 EKind = DeclarationNameExtra::CXXConstructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000416 assert(!Ty.hasQualifiers() &&"Constructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000417 break;
418 case DeclarationName::CXXDestructorName:
419 EKind = DeclarationNameExtra::CXXDestructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000420 assert(!Ty.hasQualifiers() && "Destructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000421 break;
422 case DeclarationName::CXXConversionFunctionName:
423 EKind = DeclarationNameExtra::CXXConversionFunction;
424 break;
425 default:
426 return DeclarationName();
427 }
428
429 // Unique selector, to guarantee there is one per name.
430 llvm::FoldingSetNodeID ID;
431 ID.AddInteger(EKind);
432 ID.AddPointer(Ty.getAsOpaquePtr());
433
Craig Topper36250ad2014-05-12 05:36:57 +0000434 void *InsertPos = nullptr;
Douglas Gregor77324f32008-11-17 14:58:09 +0000435 if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos))
436 return DeclarationName(Name);
437
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000438 CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName;
Douglas Gregor77324f32008-11-17 14:58:09 +0000439 SpecialName->ExtraKindOrNumArgs = EKind;
440 SpecialName->Type = Ty;
Craig Topper36250ad2014-05-12 05:36:57 +0000441 SpecialName->FETokenInfo = nullptr;
Douglas Gregor77324f32008-11-17 14:58:09 +0000442
443 SpecialNames->InsertNode(SpecialName, InsertPos);
444 return DeclarationName(SpecialName);
445}
446
Mike Stump11289f42009-09-09 15:08:12 +0000447DeclarationName
Douglas Gregor163c5852008-11-18 14:39:36 +0000448DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) {
449 return DeclarationName(&CXXOperatorNames[(unsigned)Op]);
450}
451
Alexis Hunt3d221f22009-11-29 07:34:05 +0000452DeclarationName
453DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
Alexis Huntc88db062010-01-13 09:01:02 +0000454 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
455 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
456 (CXXLiteralOperatorNames);
457
458 llvm::FoldingSetNodeID ID;
459 ID.AddPointer(II);
460
Craig Topper36250ad2014-05-12 05:36:57 +0000461 void *InsertPos = nullptr;
Alexis Huntc88db062010-01-13 09:01:02 +0000462 if (CXXLiteralOperatorIdName *Name =
463 LiteralNames->FindNodeOrInsertPos(ID, InsertPos))
464 return DeclarationName (Name);
465
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000466 CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000467 LiteralName->ExtraKindOrNumArgs = DeclarationNameExtra::CXXLiteralOperator;
468 LiteralName->ID = II;
Craig Topper36250ad2014-05-12 05:36:57 +0000469 LiteralName->FETokenInfo = nullptr;
Alexis Huntc88db062010-01-13 09:01:02 +0000470
471 LiteralNames->InsertNode(LiteralName, InsertPos);
Alexis Hunt3d221f22009-11-29 07:34:05 +0000472 return DeclarationName(LiteralName);
473}
474
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000475DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
476 switch (Name.getNameKind()) {
477 case DeclarationName::Identifier:
478 break;
479 case DeclarationName::CXXConstructorName:
480 case DeclarationName::CXXDestructorName:
481 case DeclarationName::CXXConversionFunctionName:
Craig Topper36250ad2014-05-12 05:36:57 +0000482 NamedType.TInfo = nullptr;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000483 break;
484 case DeclarationName::CXXOperatorName:
485 CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
486 CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
487 break;
488 case DeclarationName::CXXLiteralOperatorName:
489 CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding();
490 break;
491 case DeclarationName::ObjCZeroArgSelector:
492 case DeclarationName::ObjCOneArgSelector:
493 case DeclarationName::ObjCMultiArgSelector:
494 // FIXME: ?
495 break;
496 case DeclarationName::CXXUsingDirective:
497 break;
498 }
499}
500
Douglas Gregora6e053e2010-12-15 01:34:56 +0000501bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
502 switch (Name.getNameKind()) {
503 case DeclarationName::Identifier:
504 case DeclarationName::ObjCZeroArgSelector:
505 case DeclarationName::ObjCOneArgSelector:
506 case DeclarationName::ObjCMultiArgSelector:
507 case DeclarationName::CXXOperatorName:
508 case DeclarationName::CXXLiteralOperatorName:
509 case DeclarationName::CXXUsingDirective:
510 return false;
511
512 case DeclarationName::CXXConstructorName:
513 case DeclarationName::CXXDestructorName:
514 case DeclarationName::CXXConversionFunctionName:
515 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
516 return TInfo->getType()->containsUnexpandedParameterPack();
517
518 return Name.getCXXNameType()->containsUnexpandedParameterPack();
519 }
Chandler Carruth2b59fbe2010-12-15 07:29:18 +0000520 llvm_unreachable("All name kinds handled.");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000521}
522
Douglas Gregor678d76c2011-07-01 01:22:09 +0000523bool DeclarationNameInfo::isInstantiationDependent() const {
524 switch (Name.getNameKind()) {
525 case DeclarationName::Identifier:
526 case DeclarationName::ObjCZeroArgSelector:
527 case DeclarationName::ObjCOneArgSelector:
528 case DeclarationName::ObjCMultiArgSelector:
529 case DeclarationName::CXXOperatorName:
530 case DeclarationName::CXXLiteralOperatorName:
531 case DeclarationName::CXXUsingDirective:
532 return false;
533
534 case DeclarationName::CXXConstructorName:
535 case DeclarationName::CXXDestructorName:
536 case DeclarationName::CXXConversionFunctionName:
537 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
538 return TInfo->getType()->isInstantiationDependentType();
539
540 return Name.getCXXNameType()->isInstantiationDependentType();
541 }
542 llvm_unreachable("All name kinds handled.");
543}
544
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000545std::string DeclarationNameInfo::getAsString() const {
546 std::string Result;
547 llvm::raw_string_ostream OS(Result);
548 printName(OS);
549 return OS.str();
550}
551
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000552void DeclarationNameInfo::printName(raw_ostream &OS) const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000553 switch (Name.getNameKind()) {
554 case DeclarationName::Identifier:
555 case DeclarationName::ObjCZeroArgSelector:
556 case DeclarationName::ObjCOneArgSelector:
557 case DeclarationName::ObjCMultiArgSelector:
558 case DeclarationName::CXXOperatorName:
559 case DeclarationName::CXXLiteralOperatorName:
560 case DeclarationName::CXXUsingDirective:
David Blaikied4da8722013-05-14 21:04:00 +0000561 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000562 return;
563
564 case DeclarationName::CXXConstructorName:
565 case DeclarationName::CXXDestructorName:
566 case DeclarationName::CXXConversionFunctionName:
567 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) {
568 if (Name.getNameKind() == DeclarationName::CXXDestructorName)
569 OS << '~';
570 else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
571 OS << "operator ";
Richard Smithe81daee2014-01-22 00:27:42 +0000572 LangOptions LO;
573 LO.CPlusPlus = true;
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000574 LO.Bool = true;
Richard Smithe81daee2014-01-22 00:27:42 +0000575 OS << TInfo->getType().getAsString(PrintingPolicy(LO));
David Blaikied4da8722013-05-14 21:04:00 +0000576 } else
577 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000578 return;
579 }
David Blaikie83d382b2011-09-23 05:06:16 +0000580 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000581}
582
583SourceLocation DeclarationNameInfo::getEndLoc() const {
584 switch (Name.getNameKind()) {
585 case DeclarationName::Identifier:
586 return NameLoc;
587
588 case DeclarationName::CXXOperatorName: {
589 unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc;
590 return SourceLocation::getFromRawEncoding(raw);
591 }
592
593 case DeclarationName::CXXLiteralOperatorName: {
594 unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc;
595 return SourceLocation::getFromRawEncoding(raw);
596 }
597
598 case DeclarationName::CXXConstructorName:
599 case DeclarationName::CXXDestructorName:
600 case DeclarationName::CXXConversionFunctionName:
601 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
602 return TInfo->getTypeLoc().getEndLoc();
603 else
604 return NameLoc;
605
606 // DNInfo work in progress: FIXME.
607 case DeclarationName::ObjCZeroArgSelector:
608 case DeclarationName::ObjCOneArgSelector:
609 case DeclarationName::ObjCMultiArgSelector:
610 case DeclarationName::CXXUsingDirective:
611 return NameLoc;
612 }
David Blaikie83d382b2011-09-23 05:06:16 +0000613 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000614}