blob: 56b757460082c4ffbc41a1cd34d665d87d07bf6d [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();
153 return OS << ClassType.getAsString();
154 }
155
156 case DeclarationName::CXXDestructorName: {
157 OS << '~';
158 QualType Type = N.getCXXNameType();
159 if (const RecordType *Rec = Type->getAs<RecordType>())
160 return OS << *Rec->getDecl();
161 return OS << Type.getAsString();
162 }
163
164 case DeclarationName::CXXOperatorName: {
165 static const char* const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
166 0,
167#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
168 Spelling,
169#include "clang/Basic/OperatorKinds.def"
170 };
171 const char *OpName = OperatorNames[N.getCXXOverloadedOperator()];
172 assert(OpName && "not an overloaded operator");
173
174 OS << "operator";
175 if (OpName[0] >= 'a' && OpName[0] <= 'z')
176 OS << ' ';
177 return OS << OpName;
178 }
179
180 case DeclarationName::CXXLiteralOperatorName:
181 return OS << "operator \"\" " << N.getCXXLiteralIdentifier()->getName();
182
183 case DeclarationName::CXXConversionFunctionName: {
184 OS << "operator ";
185 QualType Type = N.getCXXNameType();
186 if (const RecordType *Rec = Type->getAs<RecordType>())
187 return OS << *Rec->getDecl();
188 return OS << Type.getAsString();
189 }
190 case DeclarationName::CXXUsingDirective:
191 return OS << "<using-directive>";
192 }
193
194 llvm_unreachable("Unexpected declaration name kind");
195}
196
Douglas Gregor77324f32008-11-17 14:58:09 +0000197} // end namespace clang
198
Douglas Gregor77324f32008-11-17 14:58:09 +0000199DeclarationName::NameKind DeclarationName::getNameKind() const {
200 switch (getStoredNameKind()) {
201 case StoredIdentifier: return Identifier;
202 case StoredObjCZeroArgSelector: return ObjCZeroArgSelector;
203 case StoredObjCOneArgSelector: return ObjCOneArgSelector;
204
Douglas Gregor163c5852008-11-18 14:39:36 +0000205 case StoredDeclarationNameExtra:
Douglas Gregor77324f32008-11-17 14:58:09 +0000206 switch (getExtra()->ExtraKindOrNumArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000207 case DeclarationNameExtra::CXXConstructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000208 return CXXConstructorName;
209
Mike Stump11289f42009-09-09 15:08:12 +0000210 case DeclarationNameExtra::CXXDestructor:
Douglas Gregor77324f32008-11-17 14:58:09 +0000211 return CXXDestructorName;
212
Mike Stump11289f42009-09-09 15:08:12 +0000213 case DeclarationNameExtra::CXXConversionFunction:
Douglas Gregor77324f32008-11-17 14:58:09 +0000214 return CXXConversionFunctionName;
215
Alexis Hunt3d221f22009-11-29 07:34:05 +0000216 case DeclarationNameExtra::CXXLiteralOperator:
217 return CXXLiteralOperatorName;
218
Douglas Gregor889ceb72009-02-03 19:21:40 +0000219 case DeclarationNameExtra::CXXUsingDirective:
220 return CXXUsingDirective;
221
Douglas Gregor77324f32008-11-17 14:58:09 +0000222 default:
Douglas Gregor163c5852008-11-18 14:39:36 +0000223 // Check if we have one of the CXXOperator* enumeration values.
Mike Stump11289f42009-09-09 15:08:12 +0000224 if (getExtra()->ExtraKindOrNumArgs <
Douglas Gregor889ceb72009-02-03 19:21:40 +0000225 DeclarationNameExtra::CXXUsingDirective)
Douglas Gregor163c5852008-11-18 14:39:36 +0000226 return CXXOperatorName;
227
Douglas Gregor77324f32008-11-17 14:58:09 +0000228 return ObjCMultiArgSelector;
229 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000230 }
231
232 // Can't actually get here.
David Blaikie83d382b2011-09-23 05:06:16 +0000233 llvm_unreachable("This should be unreachable!");
Douglas Gregor77324f32008-11-17 14:58:09 +0000234}
235
Douglas Gregorea0a0a92010-01-11 18:40:55 +0000236bool DeclarationName::isDependentName() const {
237 QualType T = getCXXNameType();
238 return !T.isNull() && T->isDependentType();
239}
240
Douglas Gregor92751d42008-11-17 22:58:34 +0000241std::string DeclarationName::getAsString() const {
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000242 std::string Result;
243 llvm::raw_string_ostream OS(Result);
David Blaikied4da8722013-05-14 21:04:00 +0000244 OS << *this;
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000245 return OS.str();
246}
247
Douglas Gregor77324f32008-11-17 14:58:09 +0000248QualType DeclarationName::getCXXNameType() const {
249 if (CXXSpecialName *CXXName = getAsCXXSpecialName())
250 return CXXName->Type;
251 else
252 return QualType();
253}
254
Douglas Gregor163c5852008-11-18 14:39:36 +0000255OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const {
256 if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) {
Mike Stump11289f42009-09-09 15:08:12 +0000257 unsigned value
Douglas Gregor163c5852008-11-18 14:39:36 +0000258 = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction;
259 return static_cast<OverloadedOperatorKind>(value);
260 } else {
261 return OO_None;
262 }
263}
264
Alexis Hunt3d221f22009-11-29 07:34:05 +0000265IdentifierInfo *DeclarationName::getCXXLiteralIdentifier() const {
266 if (CXXLiteralOperatorIdName *CXXLit = getAsCXXLiteralOperatorIdName())
267 return CXXLit->ID;
268 else
269 return 0;
270}
271
Douglas Gregor0da53052012-05-03 23:18:44 +0000272void *DeclarationName::getFETokenInfoAsVoidSlow() const {
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000273 switch (getNameKind()) {
274 case Identifier:
Benjamin Kramer1458c1c2012-05-19 16:03:58 +0000275 llvm_unreachable("Handled by getFETokenInfo()");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000276
277 case CXXConstructorName:
278 case CXXDestructorName:
279 case CXXConversionFunctionName:
280 return getAsCXXSpecialName()->FETokenInfo;
281
Douglas Gregor163c5852008-11-18 14:39:36 +0000282 case CXXOperatorName:
283 return getAsCXXOperatorIdName()->FETokenInfo;
284
Alexis Hunt3d221f22009-11-29 07:34:05 +0000285 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000286 return getAsCXXLiteralOperatorIdName()->FETokenInfo;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000287
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000288 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000289 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000290 }
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000291}
292
293void DeclarationName::setFETokenInfo(void *T) {
294 switch (getNameKind()) {
295 case Identifier:
296 getAsIdentifierInfo()->setFETokenInfo(T);
297 break;
298
299 case CXXConstructorName:
300 case CXXDestructorName:
301 case CXXConversionFunctionName:
302 getAsCXXSpecialName()->FETokenInfo = T;
303 break;
304
Douglas Gregor163c5852008-11-18 14:39:36 +0000305 case CXXOperatorName:
306 getAsCXXOperatorIdName()->FETokenInfo = T;
307 break;
308
Alexis Hunt3d221f22009-11-29 07:34:05 +0000309 case CXXLiteralOperatorName:
Richard Smithc1b05652012-03-09 08:37:16 +0000310 getAsCXXLiteralOperatorIdName()->FETokenInfo = T;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000311 break;
312
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000313 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000314 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000315 }
316}
317
Douglas Gregor889ceb72009-02-03 19:21:40 +0000318DeclarationName DeclarationName::getUsingDirectiveName() {
319 // Single instance of DeclarationNameExtra for using-directive
Nuno Lopes221c1fd2009-12-10 00:07:02 +0000320 static const DeclarationNameExtra UDirExtra =
Douglas Gregor889ceb72009-02-03 19:21:40 +0000321 { DeclarationNameExtra::CXXUsingDirective };
322
323 uintptr_t Ptr = reinterpret_cast<uintptr_t>(&UDirExtra);
324 Ptr |= StoredDeclarationNameExtra;
325
326 return DeclarationName(Ptr);
327}
328
Anders Carlsson1b69be22009-11-15 22:30:43 +0000329void DeclarationName::dump() const {
David Blaikied4da8722013-05-14 21:04:00 +0000330 llvm::errs() << *this << '\n';
Anders Carlsson1b69be22009-11-15 22:30:43 +0000331}
332
Jay Foad39c79802011-01-12 09:06:06 +0000333DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000334 CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>;
Alexis Huntc88db062010-01-13 09:01:02 +0000335 CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>;
Douglas Gregor163c5852008-11-18 14:39:36 +0000336
337 // Initialize the overloaded operator names.
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000338 CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS];
Douglas Gregor163c5852008-11-18 14:39:36 +0000339 for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) {
Mike Stump11289f42009-09-09 15:08:12 +0000340 CXXOperatorNames[Op].ExtraKindOrNumArgs
Douglas Gregor163c5852008-11-18 14:39:36 +0000341 = Op + DeclarationNameExtra::CXXConversionFunction;
342 CXXOperatorNames[Op].FETokenInfo = 0;
343 }
Douglas Gregor77324f32008-11-17 14:58:09 +0000344}
345
346DeclarationNameTable::~DeclarationNameTable() {
Alexis Huntc88db062010-01-13 09:01:02 +0000347 llvm::FoldingSet<CXXSpecialName> *SpecialNames =
Nuno Lopes127adb42008-12-14 17:27:25 +0000348 static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
Alexis Huntc88db062010-01-13 09:01:02 +0000349 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
350 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000351 (CXXLiteralOperatorNames);
Alexis Huntc88db062010-01-13 09:01:02 +0000352
Alexis Huntc88db062010-01-13 09:01:02 +0000353 delete SpecialNames;
354 delete LiteralNames;
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000355}
356
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +0000357DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
358 return getCXXSpecialName(DeclarationName::CXXConstructorName,
359 Ty.getUnqualifiedType());
360}
361
362DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) {
363 return getCXXSpecialName(DeclarationName::CXXDestructorName,
364 Ty.getUnqualifiedType());
365}
366
367DeclarationName
368DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) {
369 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
370}
371
Mike Stump11289f42009-09-09 15:08:12 +0000372DeclarationName
373DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
Douglas Gregor2211d342009-08-05 05:36:45 +0000374 CanQualType Ty) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000375 assert(Kind >= DeclarationName::CXXConstructorName &&
376 Kind <= DeclarationName::CXXConversionFunctionName &&
377 "Kind must be a C++ special name kind");
Mike Stump11289f42009-09-09 15:08:12 +0000378 llvm::FoldingSet<CXXSpecialName> *SpecialNames
Douglas Gregor77324f32008-11-17 14:58:09 +0000379 = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
380
381 DeclarationNameExtra::ExtraKind EKind;
382 switch (Kind) {
Mike Stump11289f42009-09-09 15:08:12 +0000383 case DeclarationName::CXXConstructorName:
Douglas Gregor77324f32008-11-17 14:58:09 +0000384 EKind = DeclarationNameExtra::CXXConstructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000385 assert(!Ty.hasQualifiers() &&"Constructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000386 break;
387 case DeclarationName::CXXDestructorName:
388 EKind = DeclarationNameExtra::CXXDestructor;
John McCall8ccfcb52009-09-24 19:53:00 +0000389 assert(!Ty.hasQualifiers() && "Destructor type must be unqualified");
Douglas Gregor77324f32008-11-17 14:58:09 +0000390 break;
391 case DeclarationName::CXXConversionFunctionName:
392 EKind = DeclarationNameExtra::CXXConversionFunction;
393 break;
394 default:
395 return DeclarationName();
396 }
397
398 // Unique selector, to guarantee there is one per name.
399 llvm::FoldingSetNodeID ID;
400 ID.AddInteger(EKind);
401 ID.AddPointer(Ty.getAsOpaquePtr());
402
403 void *InsertPos = 0;
404 if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos))
405 return DeclarationName(Name);
406
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000407 CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName;
Douglas Gregor77324f32008-11-17 14:58:09 +0000408 SpecialName->ExtraKindOrNumArgs = EKind;
409 SpecialName->Type = Ty;
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000410 SpecialName->FETokenInfo = 0;
Douglas Gregor77324f32008-11-17 14:58:09 +0000411
412 SpecialNames->InsertNode(SpecialName, InsertPos);
413 return DeclarationName(SpecialName);
414}
415
Mike Stump11289f42009-09-09 15:08:12 +0000416DeclarationName
Douglas Gregor163c5852008-11-18 14:39:36 +0000417DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) {
418 return DeclarationName(&CXXOperatorNames[(unsigned)Op]);
419}
420
Alexis Hunt3d221f22009-11-29 07:34:05 +0000421DeclarationName
422DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
Alexis Huntc88db062010-01-13 09:01:02 +0000423 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
424 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
425 (CXXLiteralOperatorNames);
426
427 llvm::FoldingSetNodeID ID;
428 ID.AddPointer(II);
429
430 void *InsertPos = 0;
431 if (CXXLiteralOperatorIdName *Name =
432 LiteralNames->FindNodeOrInsertPos(ID, InsertPos))
433 return DeclarationName (Name);
434
Ted Kremenek7e550ca2010-05-10 20:56:10 +0000435 CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName;
Alexis Hunt3d221f22009-11-29 07:34:05 +0000436 LiteralName->ExtraKindOrNumArgs = DeclarationNameExtra::CXXLiteralOperator;
437 LiteralName->ID = II;
Richard Smithc1b05652012-03-09 08:37:16 +0000438 LiteralName->FETokenInfo = 0;
Alexis Huntc88db062010-01-13 09:01:02 +0000439
440 LiteralNames->InsertNode(LiteralName, InsertPos);
Alexis Hunt3d221f22009-11-29 07:34:05 +0000441 return DeclarationName(LiteralName);
442}
443
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000444DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
445 switch (Name.getNameKind()) {
446 case DeclarationName::Identifier:
447 break;
448 case DeclarationName::CXXConstructorName:
449 case DeclarationName::CXXDestructorName:
450 case DeclarationName::CXXConversionFunctionName:
451 NamedType.TInfo = 0;
452 break;
453 case DeclarationName::CXXOperatorName:
454 CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
455 CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
456 break;
457 case DeclarationName::CXXLiteralOperatorName:
458 CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding();
459 break;
460 case DeclarationName::ObjCZeroArgSelector:
461 case DeclarationName::ObjCOneArgSelector:
462 case DeclarationName::ObjCMultiArgSelector:
463 // FIXME: ?
464 break;
465 case DeclarationName::CXXUsingDirective:
466 break;
467 }
468}
469
Douglas Gregora6e053e2010-12-15 01:34:56 +0000470bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
471 switch (Name.getNameKind()) {
472 case DeclarationName::Identifier:
473 case DeclarationName::ObjCZeroArgSelector:
474 case DeclarationName::ObjCOneArgSelector:
475 case DeclarationName::ObjCMultiArgSelector:
476 case DeclarationName::CXXOperatorName:
477 case DeclarationName::CXXLiteralOperatorName:
478 case DeclarationName::CXXUsingDirective:
479 return false;
480
481 case DeclarationName::CXXConstructorName:
482 case DeclarationName::CXXDestructorName:
483 case DeclarationName::CXXConversionFunctionName:
484 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
485 return TInfo->getType()->containsUnexpandedParameterPack();
486
487 return Name.getCXXNameType()->containsUnexpandedParameterPack();
488 }
Chandler Carruth2b59fbe2010-12-15 07:29:18 +0000489 llvm_unreachable("All name kinds handled.");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000490}
491
Douglas Gregor678d76c2011-07-01 01:22:09 +0000492bool DeclarationNameInfo::isInstantiationDependent() const {
493 switch (Name.getNameKind()) {
494 case DeclarationName::Identifier:
495 case DeclarationName::ObjCZeroArgSelector:
496 case DeclarationName::ObjCOneArgSelector:
497 case DeclarationName::ObjCMultiArgSelector:
498 case DeclarationName::CXXOperatorName:
499 case DeclarationName::CXXLiteralOperatorName:
500 case DeclarationName::CXXUsingDirective:
501 return false;
502
503 case DeclarationName::CXXConstructorName:
504 case DeclarationName::CXXDestructorName:
505 case DeclarationName::CXXConversionFunctionName:
506 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
507 return TInfo->getType()->isInstantiationDependentType();
508
509 return Name.getCXXNameType()->isInstantiationDependentType();
510 }
511 llvm_unreachable("All name kinds handled.");
512}
513
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000514std::string DeclarationNameInfo::getAsString() const {
515 std::string Result;
516 llvm::raw_string_ostream OS(Result);
517 printName(OS);
518 return OS.str();
519}
520
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000521void DeclarationNameInfo::printName(raw_ostream &OS) const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000522 switch (Name.getNameKind()) {
523 case DeclarationName::Identifier:
524 case DeclarationName::ObjCZeroArgSelector:
525 case DeclarationName::ObjCOneArgSelector:
526 case DeclarationName::ObjCMultiArgSelector:
527 case DeclarationName::CXXOperatorName:
528 case DeclarationName::CXXLiteralOperatorName:
529 case DeclarationName::CXXUsingDirective:
David Blaikied4da8722013-05-14 21:04:00 +0000530 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000531 return;
532
533 case DeclarationName::CXXConstructorName:
534 case DeclarationName::CXXDestructorName:
535 case DeclarationName::CXXConversionFunctionName:
536 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) {
537 if (Name.getNameKind() == DeclarationName::CXXDestructorName)
538 OS << '~';
539 else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
540 OS << "operator ";
541 OS << TInfo->getType().getAsString();
David Blaikied4da8722013-05-14 21:04:00 +0000542 } else
543 OS << Name;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000544 return;
545 }
David Blaikie83d382b2011-09-23 05:06:16 +0000546 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000547}
548
549SourceLocation DeclarationNameInfo::getEndLoc() const {
550 switch (Name.getNameKind()) {
551 case DeclarationName::Identifier:
552 return NameLoc;
553
554 case DeclarationName::CXXOperatorName: {
555 unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc;
556 return SourceLocation::getFromRawEncoding(raw);
557 }
558
559 case DeclarationName::CXXLiteralOperatorName: {
560 unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc;
561 return SourceLocation::getFromRawEncoding(raw);
562 }
563
564 case DeclarationName::CXXConstructorName:
565 case DeclarationName::CXXDestructorName:
566 case DeclarationName::CXXConversionFunctionName:
567 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
568 return TInfo->getTypeLoc().getEndLoc();
569 else
570 return NameLoc;
571
572 // DNInfo work in progress: FIXME.
573 case DeclarationName::ObjCZeroArgSelector:
574 case DeclarationName::ObjCOneArgSelector:
575 case DeclarationName::ObjCMultiArgSelector:
576 case DeclarationName::CXXUsingDirective:
577 return NameLoc;
578 }
David Blaikie83d382b2011-09-23 05:06:16 +0000579 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000580}