blob: e4a41b6ffb50522b64766364f4dbf8b8676699f8 [file] [log] [blame]
Douglas Gregor2e1cd422008-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 Kremenekac9590e2010-05-10 20:40:08 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Douglas Gregor2e1cd422008-11-17 14:58:09 +000016#include "clang/AST/DeclarationName.h"
Douglas Gregor10bd3682008-11-17 22:58:34 +000017#include "clang/AST/Type.h"
Abramo Bagnara25777432010-08-11 22:01:17 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregord6b5f132009-11-04 22:24:30 +000019#include "clang/AST/TypeOrdering.h"
Douglas Gregor2e1cd422008-11-17 14:58:09 +000020#include "clang/Basic/IdentifierTable.h"
Douglas Gregor370187c2009-04-22 21:45:53 +000021#include "llvm/ADT/DenseMap.h"
Douglas Gregor2e1cd422008-11-17 14:58:09 +000022#include "llvm/ADT/FoldingSet.h"
Chandler Carruthf24e54a2010-12-15 07:29:18 +000023#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerf6cde772010-04-17 09:56:45 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregor2e1cd422008-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 Stump1eb44332009-09-09 15:08:12 +000031class CXXSpecialName
Douglas Gregor2e1cd422008-11-17 14:58:09 +000032 : public DeclarationNameExtra, public llvm::FoldingSetNode {
33public:
Douglas Gregor2def4832008-11-17 20:34:05 +000034 /// Type - The type associated with this declaration name.
Douglas Gregor2e1cd422008-11-17 14:58:09 +000035 QualType Type;
36
Douglas Gregor2def4832008-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 Gregor2e1cd422008-11-17 14:58:09 +000041 void Profile(llvm::FoldingSetNodeID &ID) {
42 ID.AddInteger(ExtraKindOrNumArgs);
43 ID.AddPointer(Type.getAsOpaquePtr());
44 }
45};
46
Douglas Gregore94ca9e42008-11-18 14:39:36 +000047/// CXXOperatorIdName - Contains extra information for the name of an
Mike Stump1eb44332009-09-09 15:08:12 +000048/// overloaded operator in C++, such as "operator+.
Douglas Gregore94ca9e42008-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 Smith3a5032b2012-03-09 08:37:16 +000056/// CXXLiteralOperatorName - Contains the actual identifier that makes up the
Sean Hunt3e518bd2009-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.
Sean Hunta6c058d2010-01-13 09:01:02 +000062class CXXLiteralOperatorIdName
63 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Sean Hunt3e518bd2009-11-29 07:34:05 +000064public:
65 IdentifierInfo *ID;
Sean Hunta6c058d2010-01-13 09:01:02 +000066
Richard Smith3a5032b2012-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
Sean Hunta6c058d2010-01-13 09:01:02 +000071 void Profile(llvm::FoldingSetNodeID &FSID) {
72 FSID.AddPointer(ID);
73 }
Sean Hunt3e518bd2009-11-29 07:34:05 +000074};
75
John McCall7fe0b9e2010-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 Gregord6b5f132009-11-04 22:24:30 +000081 if (LHS.getNameKind() != RHS.getNameKind())
John McCall7fe0b9e2010-02-13 01:04:05 +000082 return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1);
Douglas Gregord6b5f132009-11-04 22:24:30 +000083
84 switch (LHS.getNameKind()) {
John McCall7fe0b9e2010-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 Gregor2e1cd422008-11-17 14:58:09 +000093
Douglas Gregord6b5f132009-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 McCall7fe0b9e2010-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 Gregor813d8342011-02-18 22:29:55 +0000101 switch (LHSSelector.getNameForSlot(I).compare(
102 RHSSelector.getNameForSlot(I))) {
Douglas Gregord6b5f132009-11-04 22:24:30 +0000103 case -1: return true;
104 case 1: return false;
105 default: break;
106 }
107 }
John McCall7fe0b9e2010-02-13 01:04:05 +0000108
109 return compareInt(LN, RN);
Douglas Gregord6b5f132009-11-04 22:24:30 +0000110 }
111
112 case DeclarationName::CXXConstructorName:
113 case DeclarationName::CXXDestructorName:
114 case DeclarationName::CXXConversionFunctionName:
John McCall7fe0b9e2010-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 Gregord6b5f132009-11-04 22:24:30 +0000120
121 case DeclarationName::CXXOperatorName:
John McCall7fe0b9e2010-02-13 01:04:05 +0000122 return compareInt(LHS.getCXXOverloadedOperator(),
123 RHS.getCXXOverloadedOperator());
Sean Hunt3e518bd2009-11-29 07:34:05 +0000124
125 case DeclarationName::CXXLiteralOperatorName:
John McCall7fe0b9e2010-02-13 01:04:05 +0000126 return LHS.getCXXLiteralIdentifier()->getName().compare(
127 RHS.getCXXLiteralIdentifier()->getName());
Douglas Gregord6b5f132009-11-04 22:24:30 +0000128
129 case DeclarationName::CXXUsingDirective:
John McCall7fe0b9e2010-02-13 01:04:05 +0000130 return 0;
Douglas Gregord6b5f132009-11-04 22:24:30 +0000131 }
David Blaikie30263482012-01-20 21:50:17 +0000132
133 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000134}
135
136} // end namespace clang
137
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000138DeclarationName::NameKind DeclarationName::getNameKind() const {
139 switch (getStoredNameKind()) {
140 case StoredIdentifier: return Identifier;
141 case StoredObjCZeroArgSelector: return ObjCZeroArgSelector;
142 case StoredObjCOneArgSelector: return ObjCOneArgSelector;
143
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000144 case StoredDeclarationNameExtra:
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000145 switch (getExtra()->ExtraKindOrNumArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000146 case DeclarationNameExtra::CXXConstructor:
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000147 return CXXConstructorName;
148
Mike Stump1eb44332009-09-09 15:08:12 +0000149 case DeclarationNameExtra::CXXDestructor:
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000150 return CXXDestructorName;
151
Mike Stump1eb44332009-09-09 15:08:12 +0000152 case DeclarationNameExtra::CXXConversionFunction:
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000153 return CXXConversionFunctionName;
154
Sean Hunt3e518bd2009-11-29 07:34:05 +0000155 case DeclarationNameExtra::CXXLiteralOperator:
156 return CXXLiteralOperatorName;
157
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000158 case DeclarationNameExtra::CXXUsingDirective:
159 return CXXUsingDirective;
160
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000161 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000162 // Check if we have one of the CXXOperator* enumeration values.
Mike Stump1eb44332009-09-09 15:08:12 +0000163 if (getExtra()->ExtraKindOrNumArgs <
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000164 DeclarationNameExtra::CXXUsingDirective)
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000165 return CXXOperatorName;
166
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000167 return ObjCMultiArgSelector;
168 }
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000169 }
170
171 // Can't actually get here.
David Blaikieb219cfc2011-09-23 05:06:16 +0000172 llvm_unreachable("This should be unreachable!");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000173}
174
Douglas Gregor48026d22010-01-11 18:40:55 +0000175bool DeclarationName::isDependentName() const {
176 QualType T = getCXXNameType();
177 return !T.isNull() && T->isDependentType();
178}
179
Douglas Gregor10bd3682008-11-17 22:58:34 +0000180std::string DeclarationName::getAsString() const {
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000181 std::string Result;
182 llvm::raw_string_ostream OS(Result);
183 printName(OS);
184 return OS.str();
185}
186
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187void DeclarationName::printName(raw_ostream &OS) const {
Douglas Gregor10bd3682008-11-17 22:58:34 +0000188 switch (getNameKind()) {
189 case Identifier:
190 if (const IdentifierInfo *II = getAsIdentifierInfo())
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000191 OS << II->getName();
192 return;
Douglas Gregor10bd3682008-11-17 22:58:34 +0000193
194 case ObjCZeroArgSelector:
195 case ObjCOneArgSelector:
196 case ObjCMultiArgSelector:
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000197 OS << getObjCSelector().getAsString();
198 return;
Douglas Gregor10bd3682008-11-17 22:58:34 +0000199
200 case CXXConstructorName: {
201 QualType ClassType = getCXXNameType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000202 if (const RecordType *ClassRec = ClassType->getAs<RecordType>())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000203 OS << *ClassRec->getDecl();
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000204 else
205 OS << ClassType.getAsString();
206 return;
Douglas Gregor10bd3682008-11-17 22:58:34 +0000207 }
208
209 case CXXDestructorName: {
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000210 OS << '~';
Douglas Gregor10bd3682008-11-17 22:58:34 +0000211 QualType Type = getCXXNameType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000212 if (const RecordType *Rec = Type->getAs<RecordType>())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000213 OS << *Rec->getDecl();
Douglas Gregor10bd3682008-11-17 22:58:34 +0000214 else
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000215 OS << Type.getAsString();
216 return;
Douglas Gregor10bd3682008-11-17 22:58:34 +0000217 }
218
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000219 case CXXOperatorName: {
Nuno Lopes2550d702009-12-23 17:49:57 +0000220 static const char* const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000221 0,
222#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
223 Spelling,
224#include "clang/Basic/OperatorKinds.def"
225 };
226 const char *OpName = OperatorNames[getCXXOverloadedOperator()];
227 assert(OpName && "not an overloaded operator");
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000229 OS << "operator";
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000230 if (OpName[0] >= 'a' && OpName[0] <= 'z')
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000231 OS << ' ';
232 OS << OpName;
233 return;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000234 }
235
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000236 case CXXLiteralOperatorName:
237 OS << "operator \"\" " << getCXXLiteralIdentifier()->getName();
238 return;
Sean Hunt3e518bd2009-11-29 07:34:05 +0000239
Douglas Gregor10bd3682008-11-17 22:58:34 +0000240 case CXXConversionFunctionName: {
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000241 OS << "operator ";
Douglas Gregor10bd3682008-11-17 22:58:34 +0000242 QualType Type = getCXXNameType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000243 if (const RecordType *Rec = Type->getAs<RecordType>())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000244 OS << *Rec->getDecl();
Douglas Gregor10bd3682008-11-17 22:58:34 +0000245 else
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000246 OS << Type.getAsString();
247 return;
Douglas Gregor10bd3682008-11-17 22:58:34 +0000248 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000249 case CXXUsingDirective:
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000250 OS << "<using-directive>";
251 return;
Douglas Gregor10bd3682008-11-17 22:58:34 +0000252 }
253
David Blaikieb219cfc2011-09-23 05:06:16 +0000254 llvm_unreachable("Unexpected declaration name kind");
Douglas Gregor10bd3682008-11-17 22:58:34 +0000255}
256
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000257QualType DeclarationName::getCXXNameType() const {
258 if (CXXSpecialName *CXXName = getAsCXXSpecialName())
259 return CXXName->Type;
260 else
261 return QualType();
262}
263
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000264OverloadedOperatorKind DeclarationName::getCXXOverloadedOperator() const {
265 if (CXXOperatorIdName *CXXOp = getAsCXXOperatorIdName()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000266 unsigned value
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000267 = CXXOp->ExtraKindOrNumArgs - DeclarationNameExtra::CXXConversionFunction;
268 return static_cast<OverloadedOperatorKind>(value);
269 } else {
270 return OO_None;
271 }
272}
273
Sean Hunt3e518bd2009-11-29 07:34:05 +0000274IdentifierInfo *DeclarationName::getCXXLiteralIdentifier() const {
275 if (CXXLiteralOperatorIdName *CXXLit = getAsCXXLiteralOperatorIdName())
276 return CXXLit->ID;
277 else
278 return 0;
279}
280
Douglas Gregor514d3b62012-05-03 23:18:44 +0000281void *DeclarationName::getFETokenInfoAsVoidSlow() const {
Douglas Gregor2def4832008-11-17 20:34:05 +0000282 switch (getNameKind()) {
283 case Identifier:
Benjamin Kramerc4704422012-05-19 16:03:58 +0000284 llvm_unreachable("Handled by getFETokenInfo()");
Douglas Gregor2def4832008-11-17 20:34:05 +0000285
286 case CXXConstructorName:
287 case CXXDestructorName:
288 case CXXConversionFunctionName:
289 return getAsCXXSpecialName()->FETokenInfo;
290
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000291 case CXXOperatorName:
292 return getAsCXXOperatorIdName()->FETokenInfo;
293
Sean Hunt3e518bd2009-11-29 07:34:05 +0000294 case CXXLiteralOperatorName:
Richard Smith3a5032b2012-03-09 08:37:16 +0000295 return getAsCXXLiteralOperatorIdName()->FETokenInfo;
Sean Hunt3e518bd2009-11-29 07:34:05 +0000296
Douglas Gregor2def4832008-11-17 20:34:05 +0000297 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000298 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregor2def4832008-11-17 20:34:05 +0000299 }
Douglas Gregor2def4832008-11-17 20:34:05 +0000300}
301
302void DeclarationName::setFETokenInfo(void *T) {
303 switch (getNameKind()) {
304 case Identifier:
305 getAsIdentifierInfo()->setFETokenInfo(T);
306 break;
307
308 case CXXConstructorName:
309 case CXXDestructorName:
310 case CXXConversionFunctionName:
311 getAsCXXSpecialName()->FETokenInfo = T;
312 break;
313
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000314 case CXXOperatorName:
315 getAsCXXOperatorIdName()->FETokenInfo = T;
316 break;
317
Sean Hunt3e518bd2009-11-29 07:34:05 +0000318 case CXXLiteralOperatorName:
Richard Smith3a5032b2012-03-09 08:37:16 +0000319 getAsCXXLiteralOperatorIdName()->FETokenInfo = T;
Sean Hunt3e518bd2009-11-29 07:34:05 +0000320 break;
321
Douglas Gregor2def4832008-11-17 20:34:05 +0000322 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000323 llvm_unreachable("Declaration name has no FETokenInfo");
Douglas Gregor2def4832008-11-17 20:34:05 +0000324 }
325}
326
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000327DeclarationName DeclarationName::getUsingDirectiveName() {
328 // Single instance of DeclarationNameExtra for using-directive
Nuno Lopes68f7a242009-12-10 00:07:02 +0000329 static const DeclarationNameExtra UDirExtra =
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000330 { DeclarationNameExtra::CXXUsingDirective };
331
332 uintptr_t Ptr = reinterpret_cast<uintptr_t>(&UDirExtra);
333 Ptr |= StoredDeclarationNameExtra;
334
335 return DeclarationName(Ptr);
336}
337
Anders Carlsson70f5bc72009-11-15 22:30:43 +0000338void DeclarationName::dump() const {
Benjamin Kramerf6cde772010-04-17 09:56:45 +0000339 printName(llvm::errs());
340 llvm::errs() << '\n';
Anders Carlsson70f5bc72009-11-15 22:30:43 +0000341}
342
Jay Foad4ba2a172011-01-12 09:06:06 +0000343DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000344 CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>;
Sean Hunta6c058d2010-01-13 09:01:02 +0000345 CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000346
347 // Initialize the overloaded operator names.
Ted Kremenek45d9c2d2010-05-10 20:56:10 +0000348 CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS];
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000349 for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) {
Mike Stump1eb44332009-09-09 15:08:12 +0000350 CXXOperatorNames[Op].ExtraKindOrNumArgs
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000351 = Op + DeclarationNameExtra::CXXConversionFunction;
352 CXXOperatorNames[Op].FETokenInfo = 0;
353 }
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000354}
355
356DeclarationNameTable::~DeclarationNameTable() {
Sean Hunta6c058d2010-01-13 09:01:02 +0000357 llvm::FoldingSet<CXXSpecialName> *SpecialNames =
Nuno Lopes6d34ae52008-12-14 17:27:25 +0000358 static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
Sean Hunta6c058d2010-01-13 09:01:02 +0000359 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
360 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
Ted Kremenek45d9c2d2010-05-10 20:56:10 +0000361 (CXXLiteralOperatorNames);
Sean Hunta6c058d2010-01-13 09:01:02 +0000362
Sean Hunta6c058d2010-01-13 09:01:02 +0000363 delete SpecialNames;
364 delete LiteralNames;
Ted Kremenekac9590e2010-05-10 20:40:08 +0000365}
366
Benjamin Kramer9852f582012-12-01 16:35:25 +0000367DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
368 return getCXXSpecialName(DeclarationName::CXXConstructorName,
369 Ty.getUnqualifiedType());
370}
371
372DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) {
373 return getCXXSpecialName(DeclarationName::CXXDestructorName,
374 Ty.getUnqualifiedType());
375}
376
377DeclarationName
378DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) {
379 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
380}
381
Mike Stump1eb44332009-09-09 15:08:12 +0000382DeclarationName
383DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
Douglas Gregor50d62d12009-08-05 05:36:45 +0000384 CanQualType Ty) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000385 assert(Kind >= DeclarationName::CXXConstructorName &&
386 Kind <= DeclarationName::CXXConversionFunctionName &&
387 "Kind must be a C++ special name kind");
Mike Stump1eb44332009-09-09 15:08:12 +0000388 llvm::FoldingSet<CXXSpecialName> *SpecialNames
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000389 = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
390
391 DeclarationNameExtra::ExtraKind EKind;
392 switch (Kind) {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 case DeclarationName::CXXConstructorName:
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000394 EKind = DeclarationNameExtra::CXXConstructor;
John McCall0953e762009-09-24 19:53:00 +0000395 assert(!Ty.hasQualifiers() &&"Constructor type must be unqualified");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000396 break;
397 case DeclarationName::CXXDestructorName:
398 EKind = DeclarationNameExtra::CXXDestructor;
John McCall0953e762009-09-24 19:53:00 +0000399 assert(!Ty.hasQualifiers() && "Destructor type must be unqualified");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000400 break;
401 case DeclarationName::CXXConversionFunctionName:
402 EKind = DeclarationNameExtra::CXXConversionFunction;
403 break;
404 default:
405 return DeclarationName();
406 }
407
408 // Unique selector, to guarantee there is one per name.
409 llvm::FoldingSetNodeID ID;
410 ID.AddInteger(EKind);
411 ID.AddPointer(Ty.getAsOpaquePtr());
412
413 void *InsertPos = 0;
414 if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos))
415 return DeclarationName(Name);
416
Ted Kremenek45d9c2d2010-05-10 20:56:10 +0000417 CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName;
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000418 SpecialName->ExtraKindOrNumArgs = EKind;
419 SpecialName->Type = Ty;
Douglas Gregor2def4832008-11-17 20:34:05 +0000420 SpecialName->FETokenInfo = 0;
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000421
422 SpecialNames->InsertNode(SpecialName, InsertPos);
423 return DeclarationName(SpecialName);
424}
425
Mike Stump1eb44332009-09-09 15:08:12 +0000426DeclarationName
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000427DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) {
428 return DeclarationName(&CXXOperatorNames[(unsigned)Op]);
429}
430
Sean Hunt3e518bd2009-11-29 07:34:05 +0000431DeclarationName
432DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
Sean Hunta6c058d2010-01-13 09:01:02 +0000433 llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
434 = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
435 (CXXLiteralOperatorNames);
436
437 llvm::FoldingSetNodeID ID;
438 ID.AddPointer(II);
439
440 void *InsertPos = 0;
441 if (CXXLiteralOperatorIdName *Name =
442 LiteralNames->FindNodeOrInsertPos(ID, InsertPos))
443 return DeclarationName (Name);
444
Ted Kremenek45d9c2d2010-05-10 20:56:10 +0000445 CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName;
Sean Hunt3e518bd2009-11-29 07:34:05 +0000446 LiteralName->ExtraKindOrNumArgs = DeclarationNameExtra::CXXLiteralOperator;
447 LiteralName->ID = II;
Richard Smith3a5032b2012-03-09 08:37:16 +0000448 LiteralName->FETokenInfo = 0;
Sean Hunta6c058d2010-01-13 09:01:02 +0000449
450 LiteralNames->InsertNode(LiteralName, InsertPos);
Sean Hunt3e518bd2009-11-29 07:34:05 +0000451 return DeclarationName(LiteralName);
452}
453
Abramo Bagnara25777432010-08-11 22:01:17 +0000454DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
455 switch (Name.getNameKind()) {
456 case DeclarationName::Identifier:
457 break;
458 case DeclarationName::CXXConstructorName:
459 case DeclarationName::CXXDestructorName:
460 case DeclarationName::CXXConversionFunctionName:
461 NamedType.TInfo = 0;
462 break;
463 case DeclarationName::CXXOperatorName:
464 CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
465 CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
466 break;
467 case DeclarationName::CXXLiteralOperatorName:
468 CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding();
469 break;
470 case DeclarationName::ObjCZeroArgSelector:
471 case DeclarationName::ObjCOneArgSelector:
472 case DeclarationName::ObjCMultiArgSelector:
473 // FIXME: ?
474 break;
475 case DeclarationName::CXXUsingDirective:
476 break;
477 }
478}
479
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000480bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
481 switch (Name.getNameKind()) {
482 case DeclarationName::Identifier:
483 case DeclarationName::ObjCZeroArgSelector:
484 case DeclarationName::ObjCOneArgSelector:
485 case DeclarationName::ObjCMultiArgSelector:
486 case DeclarationName::CXXOperatorName:
487 case DeclarationName::CXXLiteralOperatorName:
488 case DeclarationName::CXXUsingDirective:
489 return false;
490
491 case DeclarationName::CXXConstructorName:
492 case DeclarationName::CXXDestructorName:
493 case DeclarationName::CXXConversionFunctionName:
494 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
495 return TInfo->getType()->containsUnexpandedParameterPack();
496
497 return Name.getCXXNameType()->containsUnexpandedParameterPack();
498 }
Chandler Carruthf24e54a2010-12-15 07:29:18 +0000499 llvm_unreachable("All name kinds handled.");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000500}
501
Douglas Gregor561f8122011-07-01 01:22:09 +0000502bool DeclarationNameInfo::isInstantiationDependent() const {
503 switch (Name.getNameKind()) {
504 case DeclarationName::Identifier:
505 case DeclarationName::ObjCZeroArgSelector:
506 case DeclarationName::ObjCOneArgSelector:
507 case DeclarationName::ObjCMultiArgSelector:
508 case DeclarationName::CXXOperatorName:
509 case DeclarationName::CXXLiteralOperatorName:
510 case DeclarationName::CXXUsingDirective:
511 return false;
512
513 case DeclarationName::CXXConstructorName:
514 case DeclarationName::CXXDestructorName:
515 case DeclarationName::CXXConversionFunctionName:
516 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
517 return TInfo->getType()->isInstantiationDependentType();
518
519 return Name.getCXXNameType()->isInstantiationDependentType();
520 }
521 llvm_unreachable("All name kinds handled.");
522}
523
Abramo Bagnara25777432010-08-11 22:01:17 +0000524std::string DeclarationNameInfo::getAsString() const {
525 std::string Result;
526 llvm::raw_string_ostream OS(Result);
527 printName(OS);
528 return OS.str();
529}
530
Chris Lattner5f9e2722011-07-23 10:55:15 +0000531void DeclarationNameInfo::printName(raw_ostream &OS) const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000532 switch (Name.getNameKind()) {
533 case DeclarationName::Identifier:
534 case DeclarationName::ObjCZeroArgSelector:
535 case DeclarationName::ObjCOneArgSelector:
536 case DeclarationName::ObjCMultiArgSelector:
537 case DeclarationName::CXXOperatorName:
538 case DeclarationName::CXXLiteralOperatorName:
539 case DeclarationName::CXXUsingDirective:
540 Name.printName(OS);
541 return;
542
543 case DeclarationName::CXXConstructorName:
544 case DeclarationName::CXXDestructorName:
545 case DeclarationName::CXXConversionFunctionName:
546 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) {
547 if (Name.getNameKind() == DeclarationName::CXXDestructorName)
548 OS << '~';
549 else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
550 OS << "operator ";
551 OS << TInfo->getType().getAsString();
552 }
553 else
554 Name.printName(OS);
555 return;
556 }
David Blaikieb219cfc2011-09-23 05:06:16 +0000557 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnara25777432010-08-11 22:01:17 +0000558}
559
560SourceLocation DeclarationNameInfo::getEndLoc() const {
561 switch (Name.getNameKind()) {
562 case DeclarationName::Identifier:
563 return NameLoc;
564
565 case DeclarationName::CXXOperatorName: {
566 unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc;
567 return SourceLocation::getFromRawEncoding(raw);
568 }
569
570 case DeclarationName::CXXLiteralOperatorName: {
571 unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc;
572 return SourceLocation::getFromRawEncoding(raw);
573 }
574
575 case DeclarationName::CXXConstructorName:
576 case DeclarationName::CXXDestructorName:
577 case DeclarationName::CXXConversionFunctionName:
578 if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
579 return TInfo->getTypeLoc().getEndLoc();
580 else
581 return NameLoc;
582
583 // DNInfo work in progress: FIXME.
584 case DeclarationName::ObjCZeroArgSelector:
585 case DeclarationName::ObjCOneArgSelector:
586 case DeclarationName::ObjCMultiArgSelector:
587 case DeclarationName::CXXUsingDirective:
588 return NameLoc;
589 }
David Blaikieb219cfc2011-09-23 05:06:16 +0000590 llvm_unreachable("Unexpected declaration name kind");
Abramo Bagnara25777432010-08-11 22:01:17 +0000591}