blob: f9041c043c925a579cdb802263012bc8b2924bd4 [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;
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000194 LO.Bool = true;
Richard Smithe81daee2014-01-22 00:27:42 +0000195 return OS << Type.getAsString(PrintingPolicy(LO));
David Blaikied4da8722013-05-14 21:04:00 +0000196 }
197 case DeclarationName::CXXUsingDirective:
198 return OS << "<using-directive>";
199 }
200
201 llvm_unreachable("Unexpected declaration name kind");
202}
203
Douglas Gregor77324f32008-11-17 14:58:09 +0000204} // end namespace clang
205
Douglas Gregor77324f32008-11-17 14:58:09 +0000206DeclarationName::NameKind DeclarationName::getNameKind() const {
207 switch (getStoredNameKind()) {
208 case StoredIdentifier: return Identifier;
209 case StoredObjCZeroArgSelector: return ObjCZeroArgSelector;
210 case StoredObjCOneArgSelector: return ObjCOneArgSelector;
211
Douglas Gregor163c5852008-11-18 14:39:36 +0000212 case StoredDeclarationNameExtra:
Douglas Gregor77324f32008-11-17 14:58:09 +0000213 switch (getExtra()->ExtraKindOrNumArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000214 case DeclarationNameExtra::CXXConstructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000215 return CXXConstructorName;
216
Mike Stump11289f42009-09-09 15:08:12 +0000217 case DeclarationNameExtra::CXXDestructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000218 return CXXDestructorName;
219
Mike Stump11289f42009-09-09 15:08:12 +0000220 case DeclarationNameExtra::CXXConversionFunction:
Douglas Gregor77324f32008-11-17 14:58:09 +0000221 return CXXConversionFunctionName;
222
Alexis Hunt3d221f22009-11-29 07:34:05 +0000223 case DeclarationNameExtra::CXXLiteralOperator:
224 return CXXLiteralOperatorName;
225
Douglas Gregor889ceb72009-02-03 19:21:40 +0000226 case DeclarationNameExtra::CXXUsingDirective:
227 return CXXUsingDirective;
228
Douglas Gregor77324f32008-11-17 14:58:09 +0000229 default:
Douglas Gregor163c5852008-11-18 14:39:36 +0000230 // Check if we have one of the CXXOperator* enumeration values.
Mike Stump11289f42009-09-09 15:08:12 +0000231 if (getExtra()->ExtraKindOrNumArgs <
Douglas Gregor889ceb72009-02-03 19:21:40 +0000232 DeclarationNameExtra::CXXUsingDirective)
Douglas Gregor163c5852008-11-18 14:39:36 +0000233 return CXXOperatorName;
234
Douglas Gregor77324f32008-11-17 14:58:09 +0000235 return ObjCMultiArgSelector;
236 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000237 }
238
239 // Can't actually get here.
David Blaikie83d382b2011-09-23 05:06:16 +0000240 llvm_unreachable("This should be unreachable!");
Douglas Gregor77324f32008-11-17 14:58:09 +0000241}
242
Douglas Gregorea0a0a92010-01-11 18:40:55 +0000243bool DeclarationName::isDependentName() const {
244 QualType T = getCXXNameType();
245 return !T.isNull() && T->isDependentType();
246}
247
Douglas Gregor92751d42008-11-17 22:58:34 +0000248std::string DeclarationName::getAsString() const {
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000249 std::string Result;
250 llvm::raw_string_ostream OS(Result);
David Blaikied4da8722013-05-14 21:04:00 +0000251 OS << *this;
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000252 return OS.str();
253}
254
Douglas Gregor77324f32008-11-17 14:58:09 +0000255QualType DeclarationName::getCXXNameType() const {
256 if (CXXSpecialName *CXXName = getAsCXXSpecialName())
257 return CXXName->Type;
258 else
259 return QualType();
260}
261
Douglas Gregor163c5852008-11-18 14:39:36 +0000262OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const {
263 if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) {
Mike Stump11289f42009-09-09 15:08:12 +0000264 unsigned value
Douglas Gregor163c5852008-11-18 14:39:36 +0000265 = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction;
266 return static_cast<OverloadedOperatorKind>(value);
267 } else {
268 return OO_None;
269 }
270}
271
Alexis Hunt3d221f22009-11-29 07:34:05 +0000272IdentifierInfo *DeclarationName::getCXXLiteralIdentifier() const {
273 if (CXXLiteralOperatorIdName *CXXLit = getAsCXXLiteralOperatorIdName())
274 return CXXLit->ID;
275 else
276 return 0;
277}
278
Douglas Gregor0da53052012-05-03 23:18:44 +0000279void *DeclarationName::getFETokenInfoAsVoidSlow() const {
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000280 switch (getNameKind()) {
281 case Identifier:
Benjamin Kramer1458c1c2012-05-19 16:03:58 +0000282 llvm_unreachable("Handled by getFETokenInfo()");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000283
284 case CXXConstructorName:
285 case CXXDestructorName:
286 case CXXConversionFunctionName:
287 return getAsCXXSpecialName()->FETokenInfo;
288
Douglas Gregor163c5852008-11-18 14:39:36 +0000289 case CXXOperatorName:
290 return getAsCXXOperatorIdName()->FETokenInfo;
291
Alexis Hunt3d221f22009-11-29 07:34:05 +0000292 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000293 return getAsCXXLiteralOperatorIdName()->FETokenInfo;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000294
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000295 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000296 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000297 }
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000298}
299
300void DeclarationName::setFETokenInfo(void *T) {
301 switch (getNameKind()) {
302 case Identifier:
303 getAsIdentifierInfo()->setFETokenInfo(T);
304 break;
305
306 case CXXConstructorName:
307 case CXXDestructorName:
308 case CXXConversionFunctionName:
309 getAsCXXSpecialName()->FETokenInfo = T;
310 break;
311
Douglas Gregor163c5852008-11-18 14:39:36 +0000312 case CXXOperatorName:
313 getAsCXXOperatorIdName()->FETokenInfo = T;
314 break;
315
Alexis Hunt3d221f22009-11-29 07:34:05 +0000316 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000317 getAsCXXLiteralOperatorIdName()->FETokenInfo = T;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000318 break;
319
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000320 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000321 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000322 }
323}
324
Douglas Gregor889ceb72009-02-03 19:21:40 +0000325DeclarationName DeclarationName::getUsingDirectiveName() {
326 // Single instance of DeclarationNameExtra for using-directive
Nuno Lopes221c1fd2009-12-10 00:07:02 +0000327 static const DeclarationNameExtra UDirExtra =
Douglas Gregor889ceb72009-02-03 19:21:40 +0000328 { DeclarationNameExtra::CXXUsingDirective };
329
330 uintptr_t Ptr = reinterpret_cast<uintptr_t>(&UDirExtra);
331 Ptr |= StoredDeclarationNameExtra;
332
333 return DeclarationName(Ptr);
334}
335
Anders Carlsson1b69be22009-11-15 22:30:43 +0000336void DeclarationName::dump() const {
David Blaikied4da8722013-05-14 21:04:00 +0000337 llvm::errs() << *this << '\n';
Anders Carlsson1b69be22009-11-15 22:30:43 +0000338}
339
Jay Foad39c79802011-01-12 09:06:06 +0000340DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000341 CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>;
Alexis Huntc88db062010-01-13 09:01:02 +0000342 CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>;
Douglas Gregor163c5852008-11-18 14:39:36 +0000343
344 // Initialize the overloaded operator names.
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000345 CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS];
Douglas Gregor163c5852008-11-18 14:39:36 +0000346 for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) {
Mike Stump11289f42009-09-09 15:08:12 +0000347 CXXOperatorNames[Op].ExtraKindOrNumArgs
Douglas Gregor163c5852008-11-18 14:39:36 +0000348 = Op + DeclarationNameExtra::CXXConversionFunction;
349 CXXOperatorNames[Op].FETokenInfo = 0;
350 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000351}
352
353DeclarationNameTable::~DeclarationNameTable() {
Alexis Huntc88db062010-01-13 09:01:02 +0000354 llvm::FoldingSet<CXXSpecialName> *SpecialNames =
Nuno Lopes127adb42008-12-14 17:27:25 +0000355 static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
Alexis Huntc88db062010-01-13 09:01:02 +0000356 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
357 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000358 (CXXLiteralOperatorNames);
Alexis Huntc88db062010-01-13 09:01:02 +0000359
Alexis Huntc88db062010-01-13 09:01:02 +0000360 delete SpecialNames;
361 delete LiteralNames;
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000362}
363
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +0000364DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
365 return getCXXSpecialName(DeclarationName::CXXConstructorName,
366 Ty.getUnqualifiedType());
367}
368
369DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) {
370 return getCXXSpecialName(DeclarationName::CXXDestructorName,
371 Ty.getUnqualifiedType());
372}
373
374DeclarationName
375DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) {
376 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
377}
378
Mike Stump11289f42009-09-09 15:08:12 +0000379DeclarationName
380DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
Douglas Gregor2211d342009-08-05 05:36:45 +0000381 CanQualType Ty) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000382 assert(Kind >= DeclarationName::CXXConstructorName &&
383 Kind <= DeclarationName::CXXConversionFunctionName &&
384 "Kind must be a C++ special name kind");
Mike Stump11289f42009-09-09 15:08:12 +0000385 llvm::FoldingSet<CXXSpecialName> *SpecialNames
Douglas Gregor77324f32008-11-17 14:58:09 +0000386 = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
387
388 DeclarationNameExtra::ExtraKind EKind;
389 switch (Kind) {
Mike Stump11289f42009-09-09 15:08:12 +0000390 case DeclarationName::CXXConstructorName:
Douglas Gregor77324f32008-11-17 14:58:09 +0000391 EKind = DeclarationNameExtra::CXXConstructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000392 assert(!Ty.hasQualifiers() &&"Constructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000393 break;
394 case DeclarationName::CXXDestructorName:
395 EKind = DeclarationNameExtra::CXXDestructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000396 assert(!Ty.hasQualifiers() && "Destructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000397 break;
398 case DeclarationName::CXXConversionFunctionName:
399 EKind = DeclarationNameExtra::CXXConversionFunction;
400 break;
401 default:
402 return DeclarationName();
403 }
404
405 // Unique selector, to guarantee there is one per name.
406 llvm::FoldingSetNodeID ID;
407 ID.AddInteger(EKind);
408 ID.AddPointer(Ty.getAsOpaquePtr());
409
410 void *InsertPos = 0;
411 if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos))
412 return DeclarationName(Name);
413
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000414 CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName;
Douglas Gregor77324f32008-11-17 14:58:09 +0000415 SpecialName->ExtraKindOrNumArgs = EKind;
416 SpecialName->Type = Ty;
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000417 SpecialName->FETokenInfo = 0;
Douglas Gregor77324f32008-11-17 14:58:09 +0000418
419 SpecialNames->InsertNode(SpecialName, InsertPos);
420 return DeclarationName(SpecialName);
421}
422
Mike Stump11289f42009-09-09 15:08:12 +0000423DeclarationName
Douglas Gregor163c5852008-11-18 14:39:36 +0000424DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) {
425 return DeclarationName(&CXXOperatorNames[(unsigned)Op]);
426}
427
Alexis Hunt3d221f22009-11-29 07:34:05 +0000428DeclarationName
429DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
Alexis Huntc88db062010-01-13 09:01:02 +0000430 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
431 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
432 (CXXLiteralOperatorNames);
433
434 llvm::FoldingSetNodeID ID;
435 ID.AddPointer(II);
436
437 void *InsertPos = 0;
438 if (CXXLiteralOperatorIdName *Name =
439 LiteralNames->FindNodeOrInsertPos(ID, InsertPos))
440 return DeclarationName (Name);
441
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000442 CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000443 LiteralName->ExtraKindOrNumArgs = DeclarationNameExtra::CXXLiteralOperator;
444 LiteralName->ID = II;
Richard Smithc1b05652012-03-09 08:37:16 +0000445 LiteralName->FETokenInfo = 0;
Alexis Huntc88db062010-01-13 09:01:02 +0000446
447 LiteralNames->InsertNode(LiteralName, InsertPos);
Alexis Hunt3d221f22009-11-29 07:34:05 +0000448 return DeclarationName(LiteralName);
449}
450
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000451DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
452 switch (Name.getNameKind()) {
453 case DeclarationName::Identifier:
454 break;
455 case DeclarationName::CXXConstructorName:
456 case DeclarationName::CXXDestructorName:
457 case DeclarationName::CXXConversionFunctionName:
458 NamedType.TInfo = 0;
459 break;
460 case DeclarationName::CXXOperatorName:
461 CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
462 CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
463 break;
464 case DeclarationName::CXXLiteralOperatorName:
465 CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding();
466 break;
467 case DeclarationName::ObjCZeroArgSelector:
468 case DeclarationName::ObjCOneArgSelector:
469 case DeclarationName::ObjCMultiArgSelector:
470 // FIXME: ?
471 break;
472 case DeclarationName::CXXUsingDirective:
473 break;
474 }
475}
476
Douglas Gregora6e053e2010-12-15 01:34:56 +0000477bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
478 switch (Name.getNameKind()) {
479 case DeclarationName::Identifier:
480 case DeclarationName::ObjCZeroArgSelector:
481 case DeclarationName::ObjCOneArgSelector:
482 case DeclarationName::ObjCMultiArgSelector:
483 case DeclarationName::CXXOperatorName:
484 case DeclarationName::CXXLiteralOperatorName:
485 case DeclarationName::CXXUsingDirective:
486 return false;
487
488 case DeclarationName::CXXConstructorName:
489 case DeclarationName::CXXDestructorName:
490 case DeclarationName::CXXConversionFunctionName:
491 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
492 return TInfo->getType()->containsUnexpandedParameterPack();
493
494 return Name.getCXXNameType()->containsUnexpandedParameterPack();
495 }
Chandler Carruth2b59fbe2010-12-15 07:29:18 +0000496 llvm_unreachable("All name kinds handled.");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000497}
498
Douglas Gregor678d76c2011-07-01 01:22:09 +0000499bool DeclarationNameInfo::isInstantiationDependent() const {
500 switch (Name.getNameKind()) {
501 case DeclarationName::Identifier:
502 case DeclarationName::ObjCZeroArgSelector:
503 case DeclarationName::ObjCOneArgSelector:
504 case DeclarationName::ObjCMultiArgSelector:
505 case DeclarationName::CXXOperatorName:
506 case DeclarationName::CXXLiteralOperatorName:
507 case DeclarationName::CXXUsingDirective:
508 return false;
509
510 case DeclarationName::CXXConstructorName:
511 case DeclarationName::CXXDestructorName:
512 case DeclarationName::CXXConversionFunctionName:
513 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
514 return TInfo->getType()->isInstantiationDependentType();
515
516 return Name.getCXXNameType()->isInstantiationDependentType();
517 }
518 llvm_unreachable("All name kinds handled.");
519}
520
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000521std::string DeclarationNameInfo::getAsString() const {
522 std::string Result;
523 llvm::raw_string_ostream OS(Result);
524 printName(OS);
525 return OS.str();
526}
527
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000528void DeclarationNameInfo::printName(raw_ostream &OS) const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000529 switch (Name.getNameKind()) {
530 case DeclarationName::Identifier:
531 case DeclarationName::ObjCZeroArgSelector:
532 case DeclarationName::ObjCOneArgSelector:
533 case DeclarationName::ObjCMultiArgSelector:
534 case DeclarationName::CXXOperatorName:
535 case DeclarationName::CXXLiteralOperatorName:
536 case DeclarationName::CXXUsingDirective:
David Blaikied4da8722013-05-14 21:04:00 +0000537 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000538 return;
539
540 case DeclarationName::CXXConstructorName:
541 case DeclarationName::CXXDestructorName:
542 case DeclarationName::CXXConversionFunctionName:
543 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) {
544 if (Name.getNameKind() == DeclarationName::CXXDestructorName)
545 OS << '~';
546 else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
547 OS << "operator ";
Richard Smithe81daee2014-01-22 00:27:42 +0000548 LangOptions LO;
549 LO.CPlusPlus = true;
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000550 LO.Bool = true;
Richard Smithe81daee2014-01-22 00:27:42 +0000551 OS << TInfo->getType().getAsString(PrintingPolicy(LO));
David Blaikied4da8722013-05-14 21:04:00 +0000552 } else
553 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000554 return;
555 }
David Blaikie83d382b2011-09-23 05:06:16 +0000556 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000557}
558
559SourceLocation DeclarationNameInfo::getEndLoc() const {
560 switch (Name.getNameKind()) {
561 case DeclarationName::Identifier:
562 return NameLoc;
563
564 case DeclarationName::CXXOperatorName: {
565 unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc;
566 return SourceLocation::getFromRawEncoding(raw);
567 }
568
569 case DeclarationName::CXXLiteralOperatorName: {
570 unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc;
571 return SourceLocation::getFromRawEncoding(raw);
572 }
573
574 case DeclarationName::CXXConstructorName:
575 case DeclarationName::CXXDestructorName:
576 case DeclarationName::CXXConversionFunctionName:
577 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
578 return TInfo->getTypeLoc().getEndLoc();
579 else
580 return NameLoc;
581
582 // DNInfo work in progress: FIXME.
583 case DeclarationName::ObjCZeroArgSelector:
584 case DeclarationName::ObjCOneArgSelector:
585 case DeclarationName::ObjCMultiArgSelector:
586 case DeclarationName::CXXUsingDirective:
587 return NameLoc;
588 }
David Blaikie83d382b2011-09-23 05:06:16 +0000589 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000590}