blob: e5019ab8d9ff0d4f608dde480848e16c9fa273ba [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
David Blaikied4da8722013-05-14 21:04:00 +0000136raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) {
137 switch (N.getNameKind()) {
138 case DeclarationName::Identifier:
139 if (const IdentifierInfo *II = N.getAsIdentifierInfo())
140 OS << II->getName();
141 return OS;
142
143 case DeclarationName::ObjCZeroArgSelector:
144 case DeclarationName::ObjCOneArgSelector:
145 case DeclarationName::ObjCMultiArgSelector:
Aaron Ballmanb190f972014-01-03 17:59:55 +0000146 N.getObjCSelector().print(OS);
147 return OS;
David Blaikied4da8722013-05-14 21:04:00 +0000148
149 case DeclarationName::CXXConstructorName: {
150 QualType ClassType = N.getCXXNameType();
151 if (const RecordType *ClassRec = ClassType->getAs<RecordType>())
152 return OS << *ClassRec->getDecl();
Richard Smithe81daee2014-01-22 00:27:42 +0000153 LangOptions LO;
154 LO.CPlusPlus = true;
155 return OS << ClassType.getAsString(PrintingPolicy(LO));
David Blaikied4da8722013-05-14 21:04:00 +0000156 }
157
158 case DeclarationName::CXXDestructorName: {
159 OS << '~';
160 QualType Type = N.getCXXNameType();
161 if (const RecordType *Rec = Type->getAs<RecordType>())
162 return OS << *Rec->getDecl();
Richard Smithe81daee2014-01-22 00:27:42 +0000163 LangOptions LO;
164 LO.CPlusPlus = true;
165 return OS << Type.getAsString(PrintingPolicy(LO));
David Blaikied4da8722013-05-14 21:04:00 +0000166 }
167
168 case DeclarationName::CXXOperatorName: {
169 static const char* const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
170 0,
171#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
172 Spelling,
173#include "clang/Basic/OperatorKinds.def"
174 };
175 const char *OpName = OperatorNames[N.getCXXOverloadedOperator()];
176 assert(OpName && "not an overloaded operator");
177
178 OS << "operator";
179 if (OpName[0] >= 'a' && OpName[0] <= 'z')
180 OS << ' ';
181 return OS << OpName;
182 }
183
184 case DeclarationName::CXXLiteralOperatorName:
185 return OS << "operator \"\" " << N.getCXXLiteralIdentifier()->getName();
186
187 case DeclarationName::CXXConversionFunctionName: {
188 OS << "operator ";
189 QualType Type = N.getCXXNameType();
190 if (const RecordType *Rec = Type->getAs<RecordType>())
191 return OS << *Rec->getDecl();
Richard Smithe81daee2014-01-22 00:27:42 +0000192 LangOptions LO;
193 LO.CPlusPlus = true;
194 return OS << Type.getAsString(PrintingPolicy(LO));
David Blaikied4da8722013-05-14 21:04:00 +0000195 }
196 case DeclarationName::CXXUsingDirective:
197 return OS << "<using-directive>";
198 }
199
200 llvm_unreachable("Unexpected declaration name kind");
201}
202
Douglas Gregor77324f32008-11-17 14:58:09 +0000203} // end namespace clang
204
Douglas Gregor77324f32008-11-17 14:58:09 +0000205DeclarationName::NameKind DeclarationName::getNameKind() const {
206 switch (getStoredNameKind()) {
207 case StoredIdentifier: return Identifier;
208 case StoredObjCZeroArgSelector: return ObjCZeroArgSelector;
209 case StoredObjCOneArgSelector: return ObjCOneArgSelector;
210
Douglas Gregor163c5852008-11-18 14:39:36 +0000211 case StoredDeclarationNameExtra:
Douglas Gregor77324f32008-11-17 14:58:09 +0000212 switch (getExtra()->ExtraKindOrNumArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000213 case DeclarationNameExtra::CXXConstructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000214 return CXXConstructorName;
215
Mike Stump11289f42009-09-09 15:08:12 +0000216 case DeclarationNameExtra::CXXDestructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000217 return CXXDestructorName;
218
Mike Stump11289f42009-09-09 15:08:12 +0000219 case DeclarationNameExtra::CXXConversionFunction:
Douglas Gregor77324f32008-11-17 14:58:09 +0000220 return CXXConversionFunctionName;
221
Alexis Hunt3d221f22009-11-29 07:34:05 +0000222 case DeclarationNameExtra::CXXLiteralOperator:
223 return CXXLiteralOperatorName;
224
Douglas Gregor889ceb72009-02-03 19:21:40 +0000225 case DeclarationNameExtra::CXXUsingDirective:
226 return CXXUsingDirective;
227
Douglas Gregor77324f32008-11-17 14:58:09 +0000228 default:
Douglas Gregor163c5852008-11-18 14:39:36 +0000229 // Check if we have one of the CXXOperator* enumeration values.
Mike Stump11289f42009-09-09 15:08:12 +0000230 if (getExtra()->ExtraKindOrNumArgs <
Douglas Gregor889ceb72009-02-03 19:21:40 +0000231 DeclarationNameExtra::CXXUsingDirective)
Douglas Gregor163c5852008-11-18 14:39:36 +0000232 return CXXOperatorName;
233
Douglas Gregor77324f32008-11-17 14:58:09 +0000234 return ObjCMultiArgSelector;
235 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000236 }
237
238 // Can't actually get here.
David Blaikie83d382b2011-09-23 05:06:16 +0000239 llvm_unreachable("This should be unreachable!");
Douglas Gregor77324f32008-11-17 14:58:09 +0000240}
241
Douglas Gregorea0a0a92010-01-11 18:40:55 +0000242bool DeclarationName::isDependentName() const {
243 QualType T = getCXXNameType();
244 return !T.isNull() && T->isDependentType();
245}
246
Douglas Gregor92751d42008-11-17 22:58:34 +0000247std::string DeclarationName::getAsString() const {
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000248 std::string Result;
249 llvm::raw_string_ostream OS(Result);
David Blaikied4da8722013-05-14 21:04:00 +0000250 OS << *this;
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000251 return OS.str();
252}
253
Douglas Gregor77324f32008-11-17 14:58:09 +0000254QualType DeclarationName::getCXXNameType() const {
255 if (CXXSpecialName *CXXName = getAsCXXSpecialName())
256 return CXXName->Type;
257 else
258 return QualType();
259}
260
Douglas Gregor163c5852008-11-18 14:39:36 +0000261OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const {
262 if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) {
Mike Stump11289f42009-09-09 15:08:12 +0000263 unsigned value
Douglas Gregor163c5852008-11-18 14:39:36 +0000264 = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction;
265 return static_cast<OverloadedOperatorKind>(value);
266 } else {
267 return OO_None;
268 }
269}
270
Alexis Hunt3d221f22009-11-29 07:34:05 +0000271IdentifierInfo *DeclarationName::getCXXLiteralIdentifier() const {
272 if (CXXLiteralOperatorIdName *CXXLit = getAsCXXLiteralOperatorIdName())
273 return CXXLit->ID;
274 else
275 return 0;
276}
277
Douglas Gregor0da53052012-05-03 23:18:44 +0000278void *DeclarationName::getFETokenInfoAsVoidSlow() const {
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000279 switch (getNameKind()) {
280 case Identifier:
Benjamin Kramer1458c1c2012-05-19 16:03:58 +0000281 llvm_unreachable("Handled by getFETokenInfo()");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000282
283 case CXXConstructorName:
284 case CXXDestructorName:
285 case CXXConversionFunctionName:
286 return getAsCXXSpecialName()->FETokenInfo;
287
Douglas Gregor163c5852008-11-18 14:39:36 +0000288 case CXXOperatorName:
289 return getAsCXXOperatorIdName()->FETokenInfo;
290
Alexis Hunt3d221f22009-11-29 07:34:05 +0000291 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000292 return getAsCXXLiteralOperatorIdName()->FETokenInfo;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000293
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000294 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000295 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000296 }
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000297}
298
299void DeclarationName::setFETokenInfo(void *T) {
300 switch (getNameKind()) {
301 case Identifier:
302 getAsIdentifierInfo()->setFETokenInfo(T);
303 break;
304
305 case CXXConstructorName:
306 case CXXDestructorName:
307 case CXXConversionFunctionName:
308 getAsCXXSpecialName()->FETokenInfo = T;
309 break;
310
Douglas Gregor163c5852008-11-18 14:39:36 +0000311 case CXXOperatorName:
312 getAsCXXOperatorIdName()->FETokenInfo = T;
313 break;
314
Alexis Hunt3d221f22009-11-29 07:34:05 +0000315 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000316 getAsCXXLiteralOperatorIdName()->FETokenInfo = T;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000317 break;
318
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 }
322}
323
Douglas Gregor889ceb72009-02-03 19:21:40 +0000324DeclarationName DeclarationName::getUsingDirectiveName() {
325 // Single instance of DeclarationNameExtra for using-directive
Nuno Lopes221c1fd2009-12-10 00:07:02 +0000326 static const DeclarationNameExtra UDirExtra =
Douglas Gregor889ceb72009-02-03 19:21:40 +0000327 { DeclarationNameExtra::CXXUsingDirective };
328
329 uintptr_t Ptr = reinterpret_cast<uintptr_t>(&UDirExtra);
330 Ptr |= StoredDeclarationNameExtra;
331
332 return DeclarationName(Ptr);
333}
334
Anders Carlsson1b69be22009-11-15 22:30:43 +0000335void DeclarationName::dump() const {
David Blaikied4da8722013-05-14 21:04:00 +0000336 llvm::errs() << *this << '\n';
Anders Carlsson1b69be22009-11-15 22:30:43 +0000337}
338
Jay Foad39c79802011-01-12 09:06:06 +0000339DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000340 CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>;
Alexis Huntc88db062010-01-13 09:01:02 +0000341 CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>;
Douglas Gregor163c5852008-11-18 14:39:36 +0000342
343 // Initialize the overloaded operator names.
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000344 CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS];
Douglas Gregor163c5852008-11-18 14:39:36 +0000345 for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) {
Mike Stump11289f42009-09-09 15:08:12 +0000346 CXXOperatorNames[Op].ExtraKindOrNumArgs
Douglas Gregor163c5852008-11-18 14:39:36 +0000347 = Op + DeclarationNameExtra::CXXConversionFunction;
348 CXXOperatorNames[Op].FETokenInfo = 0;
349 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000350}
351
352DeclarationNameTable::~DeclarationNameTable() {
Alexis Huntc88db062010-01-13 09:01:02 +0000353 llvm::FoldingSet<CXXSpecialName> *SpecialNames =
Nuno Lopes127adb42008-12-14 17:27:25 +0000354 static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
Alexis Huntc88db062010-01-13 09:01:02 +0000355 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
356 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000357 (CXXLiteralOperatorNames);
Alexis Huntc88db062010-01-13 09:01:02 +0000358
Alexis Huntc88db062010-01-13 09:01:02 +0000359 delete SpecialNames;
360 delete LiteralNames;
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000361}
362
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +0000363DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
364 return getCXXSpecialName(DeclarationName::CXXConstructorName,
365 Ty.getUnqualifiedType());
366}
367
368DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) {
369 return getCXXSpecialName(DeclarationName::CXXDestructorName,
370 Ty.getUnqualifiedType());
371}
372
373DeclarationName
374DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) {
375 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
376}
377
Mike Stump11289f42009-09-09 15:08:12 +0000378DeclarationName
379DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
Douglas Gregor2211d342009-08-05 05:36:45 +0000380 CanQualType Ty) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000381 assert(Kind >= DeclarationName::CXXConstructorName &&
382 Kind <= DeclarationName::CXXConversionFunctionName &&
383 "Kind must be a C++ special name kind");
Mike Stump11289f42009-09-09 15:08:12 +0000384 llvm::FoldingSet<CXXSpecialName> *SpecialNames
Douglas Gregor77324f32008-11-17 14:58:09 +0000385 = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
386
387 DeclarationNameExtra::ExtraKind EKind;
388 switch (Kind) {
Mike Stump11289f42009-09-09 15:08:12 +0000389 case DeclarationName::CXXConstructorName:
Douglas Gregor77324f32008-11-17 14:58:09 +0000390 EKind = DeclarationNameExtra::CXXConstructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000391 assert(!Ty.hasQualifiers() &&"Constructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000392 break;
393 case DeclarationName::CXXDestructorName:
394 EKind = DeclarationNameExtra::CXXDestructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000395 assert(!Ty.hasQualifiers() && "Destructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000396 break;
397 case DeclarationName::CXXConversionFunctionName:
398 EKind = DeclarationNameExtra::CXXConversionFunction;
399 break;
400 default:
401 return DeclarationName();
402 }
403
404 // Unique selector, to guarantee there is one per name.
405 llvm::FoldingSetNodeID ID;
406 ID.AddInteger(EKind);
407 ID.AddPointer(Ty.getAsOpaquePtr());
408
409 void *InsertPos = 0;
410 if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos))
411 return DeclarationName(Name);
412
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000413 CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName;
Douglas Gregor77324f32008-11-17 14:58:09 +0000414 SpecialName->ExtraKindOrNumArgs = EKind;
415 SpecialName->Type = Ty;
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000416 SpecialName->FETokenInfo = 0;
Douglas Gregor77324f32008-11-17 14:58:09 +0000417
418 SpecialNames->InsertNode(SpecialName, InsertPos);
419 return DeclarationName(SpecialName);
420}
421
Mike Stump11289f42009-09-09 15:08:12 +0000422DeclarationName
Douglas Gregor163c5852008-11-18 14:39:36 +0000423DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) {
424 return DeclarationName(&CXXOperatorNames[(unsigned)Op]);
425}
426
Alexis Hunt3d221f22009-11-29 07:34:05 +0000427DeclarationName
428DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
Alexis Huntc88db062010-01-13 09:01:02 +0000429 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
430 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
431 (CXXLiteralOperatorNames);
432
433 llvm::FoldingSetNodeID ID;
434 ID.AddPointer(II);
435
436 void *InsertPos = 0;
437 if (CXXLiteralOperatorIdName *Name =
438 LiteralNames->FindNodeOrInsertPos(ID, InsertPos))
439 return DeclarationName (Name);
440
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000441 CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000442 LiteralName->ExtraKindOrNumArgs = DeclarationNameExtra::CXXLiteralOperator;
443 LiteralName->ID = II;
Richard Smithc1b05652012-03-09 08:37:16 +0000444 LiteralName->FETokenInfo = 0;
Alexis Huntc88db062010-01-13 09:01:02 +0000445
446 LiteralNames->InsertNode(LiteralName, InsertPos);
Alexis Hunt3d221f22009-11-29 07:34:05 +0000447 return DeclarationName(LiteralName);
448}
449
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000450DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
451 switch (Name.getNameKind()) {
452 case DeclarationName::Identifier:
453 break;
454 case DeclarationName::CXXConstructorName:
455 case DeclarationName::CXXDestructorName:
456 case DeclarationName::CXXConversionFunctionName:
457 NamedType.TInfo = 0;
458 break;
459 case DeclarationName::CXXOperatorName:
460 CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
461 CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
462 break;
463 case DeclarationName::CXXLiteralOperatorName:
464 CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding();
465 break;
466 case DeclarationName::ObjCZeroArgSelector:
467 case DeclarationName::ObjCOneArgSelector:
468 case DeclarationName::ObjCMultiArgSelector:
469 // FIXME: ?
470 break;
471 case DeclarationName::CXXUsingDirective:
472 break;
473 }
474}
475
Douglas Gregora6e053e2010-12-15 01:34:56 +0000476bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
477 switch (Name.getNameKind()) {
478 case DeclarationName::Identifier:
479 case DeclarationName::ObjCZeroArgSelector:
480 case DeclarationName::ObjCOneArgSelector:
481 case DeclarationName::ObjCMultiArgSelector:
482 case DeclarationName::CXXOperatorName:
483 case DeclarationName::CXXLiteralOperatorName:
484 case DeclarationName::CXXUsingDirective:
485 return false;
486
487 case DeclarationName::CXXConstructorName:
488 case DeclarationName::CXXDestructorName:
489 case DeclarationName::CXXConversionFunctionName:
490 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
491 return TInfo->getType()->containsUnexpandedParameterPack();
492
493 return Name.getCXXNameType()->containsUnexpandedParameterPack();
494 }
Chandler Carruth2b59fbe2010-12-15 07:29:18 +0000495 llvm_unreachable("All name kinds handled.");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000496}
497
Douglas Gregor678d76c2011-07-01 01:22:09 +0000498bool DeclarationNameInfo::isInstantiationDependent() const {
499 switch (Name.getNameKind()) {
500 case DeclarationName::Identifier:
501 case DeclarationName::ObjCZeroArgSelector:
502 case DeclarationName::ObjCOneArgSelector:
503 case DeclarationName::ObjCMultiArgSelector:
504 case DeclarationName::CXXOperatorName:
505 case DeclarationName::CXXLiteralOperatorName:
506 case DeclarationName::CXXUsingDirective:
507 return false;
508
509 case DeclarationName::CXXConstructorName:
510 case DeclarationName::CXXDestructorName:
511 case DeclarationName::CXXConversionFunctionName:
512 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
513 return TInfo->getType()->isInstantiationDependentType();
514
515 return Name.getCXXNameType()->isInstantiationDependentType();
516 }
517 llvm_unreachable("All name kinds handled.");
518}
519
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000520std::string DeclarationNameInfo::getAsString() const {
521 std::string Result;
522 llvm::raw_string_ostream OS(Result);
523 printName(OS);
524 return OS.str();
525}
526
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000527void DeclarationNameInfo::printName(raw_ostream &OS) const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000528 switch (Name.getNameKind()) {
529 case DeclarationName::Identifier:
530 case DeclarationName::ObjCZeroArgSelector:
531 case DeclarationName::ObjCOneArgSelector:
532 case DeclarationName::ObjCMultiArgSelector:
533 case DeclarationName::CXXOperatorName:
534 case DeclarationName::CXXLiteralOperatorName:
535 case DeclarationName::CXXUsingDirective:
David Blaikied4da8722013-05-14 21:04:00 +0000536 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000537 return;
538
539 case DeclarationName::CXXConstructorName:
540 case DeclarationName::CXXDestructorName:
541 case DeclarationName::CXXConversionFunctionName:
542 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) {
543 if (Name.getNameKind() == DeclarationName::CXXDestructorName)
544 OS << '~';
545 else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
546 OS << "operator ";
Richard Smithe81daee2014-01-22 00:27:42 +0000547 LangOptions LO;
548 LO.CPlusPlus = true;
549 OS << TInfo->getType().getAsString(PrintingPolicy(LO));
David Blaikied4da8722013-05-14 21:04:00 +0000550 } else
551 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000552 return;
553 }
David Blaikie83d382b2011-09-23 05:06:16 +0000554 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000555}
556
557SourceLocation DeclarationNameInfo::getEndLoc() const {
558 switch (Name.getNameKind()) {
559 case DeclarationName::Identifier:
560 return NameLoc;
561
562 case DeclarationName::CXXOperatorName: {
563 unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc;
564 return SourceLocation::getFromRawEncoding(raw);
565 }
566
567 case DeclarationName::CXXLiteralOperatorName: {
568 unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc;
569 return SourceLocation::getFromRawEncoding(raw);
570 }
571
572 case DeclarationName::CXXConstructorName:
573 case DeclarationName::CXXDestructorName:
574 case DeclarationName::CXXConversionFunctionName:
575 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
576 return TInfo->getTypeLoc().getEndLoc();
577 else
578 return NameLoc;
579
580 // DNInfo work in progress: FIXME.
581 case DeclarationName::ObjCZeroArgSelector:
582 case DeclarationName::ObjCOneArgSelector:
583 case DeclarationName::ObjCMultiArgSelector:
584 case DeclarationName::CXXUsingDirective:
585 return NameLoc;
586 }
David Blaikie83d382b2011-09-23 05:06:16 +0000587 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000588}