blob: 097c3ae42a8cd021624559771804f773ebbe558f [file] [log] [blame]
Eugene Zelenko21fadad2017-11-21 23:26:08 +00001//===- NestedNameSpecifier.cpp - C++ nested name specifiers ---------------===//
Douglas Gregor52537682009-03-19 00:18:19 +00002//
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 defines the NestedNameSpecifier class, which represents
11// a C++ nested-name-specifier.
12//
13//===----------------------------------------------------------------------===//
Eugene Zelenko21fadad2017-11-21 23:26:08 +000014
Douglas Gregor52537682009-03-19 00:18:19 +000015#include "clang/AST/NestedNameSpecifier.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor7b26ff92011-02-24 02:36:08 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000019#include "clang/AST/PrettyPrinter.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000020#include "clang/AST/TemplateName.h"
Douglas Gregor52537682009-03-19 00:18:19 +000021#include "clang/AST/Type.h"
Douglas Gregor869ad452011-02-24 17:54:50 +000022#include "clang/AST/TypeLoc.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000023#include "clang/Basic/LLVM.h"
24#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/SourceLocation.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/Support/Casting.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/ErrorHandling.h"
Douglas Gregor18353912009-03-19 03:51:16 +000031#include "llvm/Support/raw_ostream.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000032#include <algorithm>
Douglas Gregorf21eb492009-03-26 23:50:42 +000033#include <cassert>
Eugene Zelenko21fadad2017-11-21 23:26:08 +000034#include <cstdlib>
35#include <cstring>
Douglas Gregor18353912009-03-19 03:51:16 +000036
Douglas Gregor52537682009-03-19 00:18:19 +000037using namespace clang;
38
Douglas Gregorf21eb492009-03-26 23:50:42 +000039NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000040NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
Douglas Gregorf21eb492009-03-26 23:50:42 +000041 const NestedNameSpecifier &Mockup) {
42 llvm::FoldingSetNodeID ID;
43 Mockup.Profile(ID);
Douglas Gregor52537682009-03-19 00:18:19 +000044
Craig Topper36250ad2014-05-12 05:36:57 +000045 void *InsertPos = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000046 NestedNameSpecifier *NNS
Douglas Gregorf21eb492009-03-26 23:50:42 +000047 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
48 if (!NNS) {
Benjamin Kramerc3f89252016-10-20 14:27:22 +000049 NNS =
50 new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup);
Douglas Gregorf21eb492009-03-26 23:50:42 +000051 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
52 }
Douglas Gregor52537682009-03-19 00:18:19 +000053
Douglas Gregorf21eb492009-03-26 23:50:42 +000054 return NNS;
Douglas Gregor52537682009-03-19 00:18:19 +000055}
Douglas Gregor18353912009-03-19 03:51:16 +000056
Douglas Gregorf21eb492009-03-26 23:50:42 +000057NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000058NestedNameSpecifier::Create(const ASTContext &Context,
59 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000060 assert(II && "Identifier cannot be NULL");
Douglas Gregor308047d2009-09-09 00:23:06 +000061 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
Douglas Gregor18353912009-03-19 03:51:16 +000062
Douglas Gregorf21eb492009-03-26 23:50:42 +000063 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000064 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000065 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregordce2b622009-04-01 00:28:59 +000066 Mockup.Specifier = II;
Douglas Gregorf21eb492009-03-26 23:50:42 +000067 return FindOrInsert(Context, Mockup);
68}
Douglas Gregor18353912009-03-19 03:51:16 +000069
Douglas Gregorf21eb492009-03-26 23:50:42 +000070NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000071NestedNameSpecifier::Create(const ASTContext &Context,
Dmitri Gribenkoaeeca772013-01-23 17:06:56 +000072 NestedNameSpecifier *Prefix,
73 const NamespaceDecl *NS) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000074 assert(NS && "Namespace cannot be NULL");
Mike Stump11289f42009-09-09 15:08:12 +000075 assert((!Prefix ||
Craig Topper36250ad2014-05-12 05:36:57 +000076 (Prefix->getAsType() == nullptr &&
77 Prefix->getAsIdentifier() == nullptr)) &&
Douglas Gregorf21eb492009-03-26 23:50:42 +000078 "Broken nested name specifier");
79 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000080 Mockup.Prefix.setPointer(Prefix);
Nikola Smiljanic67860242014-09-26 00:28:20 +000081 Mockup.Prefix.setInt(StoredDecl);
Dmitri Gribenkoaeeca772013-01-23 17:06:56 +000082 Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
Douglas Gregorf21eb492009-03-26 23:50:42 +000083 return FindOrInsert(Context, Mockup);
84}
85
86NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000087NestedNameSpecifier::Create(const ASTContext &Context,
Fangrui Song6907ce22018-07-30 19:24:48 +000088 NestedNameSpecifier *Prefix,
Douglas Gregor7b26ff92011-02-24 02:36:08 +000089 NamespaceAliasDecl *Alias) {
90 assert(Alias && "Namespace alias cannot be NULL");
91 assert((!Prefix ||
Craig Topper36250ad2014-05-12 05:36:57 +000092 (Prefix->getAsType() == nullptr &&
93 Prefix->getAsIdentifier() == nullptr)) &&
Douglas Gregor7b26ff92011-02-24 02:36:08 +000094 "Broken nested name specifier");
95 NestedNameSpecifier Mockup;
96 Mockup.Prefix.setPointer(Prefix);
Nikola Smiljanic67860242014-09-26 00:28:20 +000097 Mockup.Prefix.setInt(StoredDecl);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000098 Mockup.Specifier = Alias;
99 return FindOrInsert(Context, Mockup);
100}
101
102NestedNameSpecifier *
103NestedNameSpecifier::Create(const ASTContext &Context,
Jay Foad39c79802011-01-12 09:06:06 +0000104 NestedNameSpecifier *Prefix,
John McCall424cec92011-01-19 06:33:43 +0000105 bool Template, const Type *T) {
Douglas Gregorf21eb492009-03-26 23:50:42 +0000106 assert(T && "Type cannot be NULL");
107 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +0000108 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000109 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
John McCall424cec92011-01-19 06:33:43 +0000110 Mockup.Specifier = const_cast<Type*>(T);
Douglas Gregorf21eb492009-03-26 23:50:42 +0000111 return FindOrInsert(Context, Mockup);
112}
Douglas Gregor64792e02009-09-02 23:58:38 +0000113
114NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +0000115NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
Douglas Gregor64792e02009-09-02 23:58:38 +0000116 assert(II && "Identifier cannot be NULL");
117 NestedNameSpecifier Mockup;
Craig Topper36250ad2014-05-12 05:36:57 +0000118 Mockup.Prefix.setPointer(nullptr);
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000119 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregor64792e02009-09-02 23:58:38 +0000120 Mockup.Specifier = II;
121 return FindOrInsert(Context, Mockup);
122}
123
Jay Foad39c79802011-01-12 09:06:06 +0000124NestedNameSpecifier *
125NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
Douglas Gregorf21eb492009-03-26 23:50:42 +0000126 if (!Context.GlobalNestedNameSpecifier)
Richard Smithbeb386a2012-08-15 01:41:43 +0000127 Context.GlobalNestedNameSpecifier =
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000128 new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000129 return Context.GlobalNestedNameSpecifier;
130}
131
Nikola Smiljanic67860242014-09-26 00:28:20 +0000132NestedNameSpecifier *
133NestedNameSpecifier::SuperSpecifier(const ASTContext &Context,
134 CXXRecordDecl *RD) {
135 NestedNameSpecifier Mockup;
136 Mockup.Prefix.setPointer(nullptr);
137 Mockup.Prefix.setInt(StoredDecl);
138 Mockup.Specifier = RD;
139 return FindOrInsert(Context, Mockup);
140}
141
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000142NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
Craig Topper36250ad2014-05-12 05:36:57 +0000143 if (!Specifier)
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000144 return Global;
145
146 switch (Prefix.getInt()) {
147 case StoredIdentifier:
148 return Identifier;
149
Nikola Smiljanic67860242014-09-26 00:28:20 +0000150 case StoredDecl: {
151 NamedDecl *ND = static_cast<NamedDecl *>(Specifier);
152 if (isa<CXXRecordDecl>(ND))
153 return Super;
154 return isa<NamespaceDecl>(ND) ? Namespace : NamespaceAlias;
155 }
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000156
157 case StoredTypeSpec:
158 return TypeSpec;
159
160 case StoredTypeSpecWithTemplate:
161 return TypeSpecWithTemplate;
162 }
163
David Blaikiee4d798f2012-01-20 21:50:17 +0000164 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000165}
166
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000167/// Retrieve the namespace stored in this nested name specifier.
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000168NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
Yaron Kerene0bcdd42016-10-08 06:45:10 +0000169 if (Prefix.getInt() == StoredDecl)
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000170 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
171
Craig Topper36250ad2014-05-12 05:36:57 +0000172 return nullptr;
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000173}
174
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000175/// Retrieve the namespace alias stored in this nested name specifier.
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000176NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
Yaron Kerene0bcdd42016-10-08 06:45:10 +0000177 if (Prefix.getInt() == StoredDecl)
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000178 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
179
Craig Topper36250ad2014-05-12 05:36:57 +0000180 return nullptr;
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000181}
182
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000183/// Retrieve the record declaration stored in this nested name specifier.
Nikola Smiljanic67860242014-09-26 00:28:20 +0000184CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const {
Richard Smith5179eb72016-06-28 19:03:57 +0000185 switch (Prefix.getInt()) {
186 case StoredIdentifier:
187 return nullptr;
188
189 case StoredDecl:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000190 return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier));
191
Richard Smith5179eb72016-06-28 19:03:57 +0000192 case StoredTypeSpec:
193 case StoredTypeSpecWithTemplate:
194 return getAsType()->getAsCXXRecordDecl();
195 }
196
197 llvm_unreachable("Invalid NNS Kind!");
Nikola Smiljanic67860242014-09-26 00:28:20 +0000198}
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000200/// Whether this nested name specifier refers to a dependent
Douglas Gregorf21eb492009-03-26 23:50:42 +0000201/// type or not.
202bool NestedNameSpecifier::isDependent() const {
203 switch (getKind()) {
204 case Identifier:
205 // Identifier specifiers always represent dependent types
206 return true;
207
208 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000209 case NamespaceAlias:
Douglas Gregorf21eb492009-03-26 23:50:42 +0000210 case Global:
211 return false;
212
Nikola Smiljanic67860242014-09-26 00:28:20 +0000213 case Super: {
214 CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier);
215 for (const auto &Base : RD->bases())
216 if (Base.getType()->isDependentType())
217 return true;
218
219 return false;
220 }
221
Douglas Gregorf21eb492009-03-26 23:50:42 +0000222 case TypeSpec:
223 case TypeSpecWithTemplate:
224 return getAsType()->isDependentType();
Douglas Gregor18353912009-03-19 03:51:16 +0000225 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000226
David Blaikiee4d798f2012-01-20 21:50:17 +0000227 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregorf21eb492009-03-26 23:50:42 +0000228}
229
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000230/// Whether this nested name specifier refers to a dependent
Douglas Gregor678d76c2011-07-01 01:22:09 +0000231/// type or not.
232bool NestedNameSpecifier::isInstantiationDependent() const {
233 switch (getKind()) {
234 case Identifier:
235 // Identifier specifiers always represent dependent types
236 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000237
Douglas Gregor678d76c2011-07-01 01:22:09 +0000238 case Namespace:
239 case NamespaceAlias:
240 case Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000241 case Super:
Douglas Gregor678d76c2011-07-01 01:22:09 +0000242 return false;
Nikola Smiljanic67860242014-09-26 00:28:20 +0000243
Douglas Gregor678d76c2011-07-01 01:22:09 +0000244 case TypeSpec:
245 case TypeSpecWithTemplate:
246 return getAsType()->isInstantiationDependentType();
247 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000248
249 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor678d76c2011-07-01 01:22:09 +0000250}
251
Douglas Gregor506bd562010-12-13 22:49:22 +0000252bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
253 switch (getKind()) {
254 case Identifier:
255 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
256
257 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000258 case NamespaceAlias:
Douglas Gregor506bd562010-12-13 22:49:22 +0000259 case Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000260 case Super:
Douglas Gregor506bd562010-12-13 22:49:22 +0000261 return false;
262
263 case TypeSpec:
264 case TypeSpecWithTemplate:
265 return getAsType()->containsUnexpandedParameterPack();
266 }
267
David Blaikiee4d798f2012-01-20 21:50:17 +0000268 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor506bd562010-12-13 22:49:22 +0000269}
270
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000271/// Print this nested name specifier to the given output
Douglas Gregorf21eb492009-03-26 23:50:42 +0000272/// stream.
Mike Stump11289f42009-09-09 15:08:12 +0000273void
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000274NestedNameSpecifier::print(raw_ostream &OS,
Douglas Gregor7de59662009-05-29 20:38:28 +0000275 const PrintingPolicy &Policy) const {
Douglas Gregordce2b622009-04-01 00:28:59 +0000276 if (getPrefix())
Douglas Gregor7de59662009-05-29 20:38:28 +0000277 getPrefix()->print(OS, Policy);
Douglas Gregorf21eb492009-03-26 23:50:42 +0000278
279 switch (getKind()) {
280 case Identifier:
281 OS << getAsIdentifier()->getName();
282 break;
283
284 case Namespace:
Douglas Gregor2e10cf92011-11-03 00:16:13 +0000285 if (getAsNamespace()->isAnonymousNamespace())
286 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000287
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000288 OS << getAsNamespace()->getName();
289 break;
290
291 case NamespaceAlias:
292 OS << getAsNamespaceAlias()->getName();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000293 break;
294
295 case Global:
296 break;
297
Nikola Smiljanic67860242014-09-26 00:28:20 +0000298 case Super:
299 OS << "__super";
300 break;
301
Douglas Gregorf21eb492009-03-26 23:50:42 +0000302 case TypeSpecWithTemplate:
303 OS << "template ";
304 // Fall through to print the type.
Galina Kistanovaf87496d2017-06-03 06:31:42 +0000305 LLVM_FALLTHROUGH;
Douglas Gregorf21eb492009-03-26 23:50:42 +0000306
307 case TypeSpec: {
John McCall424cec92011-01-19 06:33:43 +0000308 const Type *T = getAsType();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000309
Douglas Gregor7de59662009-05-29 20:38:28 +0000310 PrintingPolicy InnerPolicy(Policy);
John McCallb2e195a2009-09-05 06:31:47 +0000311 InnerPolicy.SuppressScope = true;
Mike Stump11289f42009-09-09 15:08:12 +0000312
Douglas Gregor053f6912009-08-26 00:04:55 +0000313 // Nested-name-specifiers are intended to contain minimally-qualified
Abramo Bagnara6150c882010-05-11 21:36:43 +0000314 // types. An actual ElaboratedType will not occur, since we'll store
Douglas Gregor053f6912009-08-26 00:04:55 +0000315 // just the type that is referred to in the nested-name-specifier (e.g.,
316 // a TypedefType, TagType, etc.). However, when we are dealing with
Mike Stump11289f42009-09-09 15:08:12 +0000317 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
Douglas Gregor053f6912009-08-26 00:04:55 +0000318 // the type requires its own nested-name-specifier for uniqueness, so we
319 // suppress that nested-name-specifier during printing.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000320 assert(!isa<ElaboratedType>(T) &&
321 "Elaborated type in nested-name-specifier");
Douglas Gregor053f6912009-08-26 00:04:55 +0000322 if (const TemplateSpecializationType *SpecType
323 = dyn_cast<TemplateSpecializationType>(T)) {
Mike Stump11289f42009-09-09 15:08:12 +0000324 // Print the template name without its corresponding
Douglas Gregor053f6912009-08-26 00:04:55 +0000325 // nested-name-specifier.
326 SpecType->getTemplateName().print(OS, InnerPolicy, true);
Mike Stump11289f42009-09-09 15:08:12 +0000327
Douglas Gregor053f6912009-08-26 00:04:55 +0000328 // Print the template argument list.
Serge Pavlov03e672c2017-11-28 16:14:14 +0000329 printTemplateArgumentList(OS, SpecType->template_arguments(),
330 InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000331 } else {
332 // Print the type normally
Benjamin Kramer9170e912013-02-22 15:46:01 +0000333 QualType(T, 0).print(OS, InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000334 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000335 break;
336 }
337 }
338
339 OS << "::";
340}
341
Stephen Kelly07dd5af2018-10-04 19:22:00 +0000342LLVM_DUMP_METHOD void NestedNameSpecifier::dump(const LangOptions &LO) const {
343 dump(llvm::errs(), LO);
Yaron Keren015e6c82015-12-27 14:34:22 +0000344}
345
Stephen Kelly07dd5af2018-10-04 19:22:00 +0000346LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { dump(llvm::errs()); }
347
348LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const {
Yaron Keren015e6c82015-12-27 14:34:22 +0000349 LangOptions LO;
Stephen Kelly07dd5af2018-10-04 19:22:00 +0000350 dump(OS, LO);
351}
352
353LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS,
354 const LangOptions &LO) const {
355 print(OS, PrintingPolicy(LO));
Douglas Gregor333489b2009-03-27 23:10:48 +0000356}
Douglas Gregor869ad452011-02-24 17:54:50 +0000357
Fangrui Song6907ce22018-07-30 19:24:48 +0000358unsigned
Douglas Gregor869ad452011-02-24 17:54:50 +0000359NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
360 assert(Qualifier && "Expected a non-NULL qualifier");
361
362 // Location of the trailing '::'.
363 unsigned Length = sizeof(unsigned);
364
365 switch (Qualifier->getKind()) {
366 case NestedNameSpecifier::Global:
367 // Nothing more to add.
368 break;
369
370 case NestedNameSpecifier::Identifier:
371 case NestedNameSpecifier::Namespace:
372 case NestedNameSpecifier::NamespaceAlias:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000373 case NestedNameSpecifier::Super:
Douglas Gregor869ad452011-02-24 17:54:50 +0000374 // The location of the identifier or namespace name.
375 Length += sizeof(unsigned);
376 break;
377
378 case NestedNameSpecifier::TypeSpecWithTemplate:
379 case NestedNameSpecifier::TypeSpec:
380 // The "void*" that points at the TypeLoc data.
381 // Note: the 'template' keyword is part of the TypeLoc.
382 Length += sizeof(void *);
383 break;
384 }
385
386 return Length;
387}
388
Fangrui Song6907ce22018-07-30 19:24:48 +0000389unsigned
Douglas Gregor869ad452011-02-24 17:54:50 +0000390NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
391 unsigned Length = 0;
392 for (; Qualifier; Qualifier = Qualifier->getPrefix())
393 Length += getLocalDataLength(Qualifier);
394 return Length;
395}
396
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000397/// Load a (possibly unaligned) source location from a given address
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000398/// and offset.
399static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
400 unsigned Raw;
401 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
402 return SourceLocation::getFromRawEncoding(Raw);
403}
Fangrui Song6907ce22018-07-30 19:24:48 +0000404
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000405/// Load a (possibly unaligned) pointer from a given address and
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000406/// offset.
407static void *LoadPointer(void *Data, unsigned Offset) {
408 void *Result;
409 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
410 return Result;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000411}
Douglas Gregor869ad452011-02-24 17:54:50 +0000412
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000413SourceRange NestedNameSpecifierLoc::getSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000414 if (!Qualifier)
415 return SourceRange();
Fangrui Song6907ce22018-07-30 19:24:48 +0000416
Douglas Gregor869ad452011-02-24 17:54:50 +0000417 NestedNameSpecifierLoc First = *this;
Douglas Gregor12441b32011-02-25 16:33:46 +0000418 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
Douglas Gregor869ad452011-02-24 17:54:50 +0000419 First = Prefix;
Fangrui Song6907ce22018-07-30 19:24:48 +0000420
421 return SourceRange(First.getLocalSourceRange().getBegin(),
Douglas Gregor869ad452011-02-24 17:54:50 +0000422 getLocalSourceRange().getEnd());
423}
424
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000425SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000426 if (!Qualifier)
427 return SourceRange();
Fangrui Song6907ce22018-07-30 19:24:48 +0000428
Douglas Gregor869ad452011-02-24 17:54:50 +0000429 unsigned Offset = getDataLength(Qualifier->getPrefix());
430 switch (Qualifier->getKind()) {
431 case NestedNameSpecifier::Global:
432 return LoadSourceLocation(Data, Offset);
433
434 case NestedNameSpecifier::Identifier:
435 case NestedNameSpecifier::Namespace:
436 case NestedNameSpecifier::NamespaceAlias:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000437 case NestedNameSpecifier::Super:
Douglas Gregor869ad452011-02-24 17:54:50 +0000438 return SourceRange(LoadSourceLocation(Data, Offset),
439 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
440
441 case NestedNameSpecifier::TypeSpecWithTemplate:
442 case NestedNameSpecifier::TypeSpec: {
443 // The "void*" that points at the TypeLoc data.
444 // Note: the 'template' keyword is part of the TypeLoc.
445 void *TypeData = LoadPointer(Data, Offset);
446 TypeLoc TL(Qualifier->getAsType(), TypeData);
447 return SourceRange(TL.getBeginLoc(),
448 LoadSourceLocation(Data, Offset + sizeof(void*)));
449 }
450 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000451
452 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor869ad452011-02-24 17:54:50 +0000453}
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000454
455TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
456 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
457 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
458 "Nested-name-specifier location is not a type");
459
460 // The "void*" that points at the TypeLoc data.
461 unsigned Offset = getDataLength(Qualifier->getPrefix());
462 void *TypeData = LoadPointer(Data, Offset);
463 return TypeLoc(Qualifier->getAsType(), TypeData);
464}
Douglas Gregor9b272512011-02-28 23:58:31 +0000465
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000466static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
Douglas Gregor9b272512011-02-28 23:58:31 +0000467 unsigned &BufferCapacity) {
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000468 if (Start == End)
469 return;
Chandler Carruthb6708d82015-08-04 03:52:56 +0000470
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000471 if (BufferSize + (End - Start) > BufferCapacity) {
472 // Reallocate the buffer.
473 unsigned NewCapacity = std::max(
474 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
475 (unsigned)(BufferSize + (End - Start)));
Serge Pavlov52525732018-02-21 02:02:39 +0000476 char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000477 if (BufferCapacity) {
478 memcpy(NewBuffer, Buffer, BufferSize);
479 free(Buffer);
Douglas Gregor9b272512011-02-28 23:58:31 +0000480 }
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000481 Buffer = NewBuffer;
482 BufferCapacity = NewCapacity;
Douglas Gregor9b272512011-02-28 23:58:31 +0000483 }
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000484
485 memcpy(Buffer + BufferSize, Start, End - Start);
486 BufferSize += End-Start;
487}
Fangrui Song6907ce22018-07-30 19:24:48 +0000488
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000489/// Save a source location to the given buffer.
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000490static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
491 unsigned &BufferSize, unsigned &BufferCapacity) {
492 unsigned Raw = Loc.getRawEncoding();
493 Append(reinterpret_cast<char *>(&Raw),
494 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
495 Buffer, BufferSize, BufferCapacity);
496}
Fangrui Song6907ce22018-07-30 19:24:48 +0000497
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000498/// Save a pointer to the given buffer.
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000499static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
500 unsigned &BufferCapacity) {
501 Append(reinterpret_cast<char *>(&Ptr),
502 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
503 Buffer, BufferSize, BufferCapacity);
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000504}
Douglas Gregor9b272512011-02-28 23:58:31 +0000505
Douglas Gregor9b272512011-02-28 23:58:31 +0000506NestedNameSpecifierLocBuilder::
Fangrui Song6907ce22018-07-30 19:24:48 +0000507NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000508 : Representation(Other.Representation) {
Douglas Gregor9b272512011-02-28 23:58:31 +0000509 if (!Other.Buffer)
510 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000511
Douglas Gregor9b272512011-02-28 23:58:31 +0000512 if (Other.BufferCapacity == 0) {
513 // Shallow copy is okay.
514 Buffer = Other.Buffer;
515 BufferSize = Other.BufferSize;
516 return;
517 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000518
Douglas Gregor9b272512011-02-28 23:58:31 +0000519 // Deep copy
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000520 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
521 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000522}
523
524NestedNameSpecifierLocBuilder &
525NestedNameSpecifierLocBuilder::
526operator=(const NestedNameSpecifierLocBuilder &Other) {
527 Representation = Other.Representation;
Fangrui Song6907ce22018-07-30 19:24:48 +0000528
Douglas Gregor9b272512011-02-28 23:58:31 +0000529 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
530 // Re-use our storage.
531 BufferSize = Other.BufferSize;
532 memcpy(Buffer, Other.Buffer, BufferSize);
533 return *this;
534 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000535
Douglas Gregor9b272512011-02-28 23:58:31 +0000536 // Free our storage, if we have any.
537 if (BufferCapacity) {
538 free(Buffer);
539 BufferCapacity = 0;
540 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000541
Douglas Gregor9b272512011-02-28 23:58:31 +0000542 if (!Other.Buffer) {
543 // Empty.
Craig Topper36250ad2014-05-12 05:36:57 +0000544 Buffer = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000545 BufferSize = 0;
546 return *this;
547 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000548
Douglas Gregor9b272512011-02-28 23:58:31 +0000549 if (Other.BufferCapacity == 0) {
550 // Shallow copy is okay.
551 Buffer = Other.Buffer;
552 BufferSize = Other.BufferSize;
553 return *this;
554 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000555
Douglas Gregor9b272512011-02-28 23:58:31 +0000556 // Deep copy.
Erik Pilkingtonc5871642018-09-10 21:54:04 +0000557 BufferSize = 0;
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000558 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
559 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000560 return *this;
561}
562
Fangrui Song6907ce22018-07-30 19:24:48 +0000563void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
564 SourceLocation TemplateKWLoc,
565 TypeLoc TL,
Douglas Gregor9b272512011-02-28 23:58:31 +0000566 SourceLocation ColonColonLoc) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000567 Representation = NestedNameSpecifier::Create(Context, Representation,
568 TemplateKWLoc.isValid(),
Douglas Gregor9b272512011-02-28 23:58:31 +0000569 TL.getTypePtr());
Fangrui Song6907ce22018-07-30 19:24:48 +0000570
Douglas Gregor9b272512011-02-28 23:58:31 +0000571 // Push source-location info into the buffer.
572 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
573 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
574}
575
Fangrui Song6907ce22018-07-30 19:24:48 +0000576void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
Douglas Gregor9b272512011-02-28 23:58:31 +0000577 IdentifierInfo *Identifier,
Fangrui Song6907ce22018-07-30 19:24:48 +0000578 SourceLocation IdentifierLoc,
Douglas Gregor9b272512011-02-28 23:58:31 +0000579 SourceLocation ColonColonLoc) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000580 Representation = NestedNameSpecifier::Create(Context, Representation,
Douglas Gregor9b272512011-02-28 23:58:31 +0000581 Identifier);
Fangrui Song6907ce22018-07-30 19:24:48 +0000582
Douglas Gregor9b272512011-02-28 23:58:31 +0000583 // Push source-location info into the buffer.
584 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
585 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
586}
587
Fangrui Song6907ce22018-07-30 19:24:48 +0000588void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
Douglas Gregor9b272512011-02-28 23:58:31 +0000589 NamespaceDecl *Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +0000590 SourceLocation NamespaceLoc,
Douglas Gregor9b272512011-02-28 23:58:31 +0000591 SourceLocation ColonColonLoc) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000592 Representation = NestedNameSpecifier::Create(Context, Representation,
Douglas Gregor9b272512011-02-28 23:58:31 +0000593 Namespace);
Fangrui Song6907ce22018-07-30 19:24:48 +0000594
Douglas Gregor9b272512011-02-28 23:58:31 +0000595 // Push source-location info into the buffer.
596 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
597 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
598}
599
600void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
601 NamespaceAliasDecl *Alias,
Fangrui Song6907ce22018-07-30 19:24:48 +0000602 SourceLocation AliasLoc,
Douglas Gregor9b272512011-02-28 23:58:31 +0000603 SourceLocation ColonColonLoc) {
604 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
Fangrui Song6907ce22018-07-30 19:24:48 +0000605
Douglas Gregor9b272512011-02-28 23:58:31 +0000606 // Push source-location info into the buffer.
607 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
608 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
609}
610
Fangrui Song6907ce22018-07-30 19:24:48 +0000611void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
Douglas Gregor9b272512011-02-28 23:58:31 +0000612 SourceLocation ColonColonLoc) {
613 assert(!Representation && "Already have a nested-name-specifier!?");
614 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
Fangrui Song6907ce22018-07-30 19:24:48 +0000615
Douglas Gregor9b272512011-02-28 23:58:31 +0000616 // Push source-location info into the buffer.
617 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
618}
619
Nikola Smiljanic67860242014-09-26 00:28:20 +0000620void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
621 CXXRecordDecl *RD,
622 SourceLocation SuperLoc,
623 SourceLocation ColonColonLoc) {
624 Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
625
626 // Push source-location info into the buffer.
627 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
628 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
629}
630
Fangrui Song6907ce22018-07-30 19:24:48 +0000631void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
632 NestedNameSpecifier *Qualifier,
Douglas Gregor9b272512011-02-28 23:58:31 +0000633 SourceRange R) {
634 Representation = Qualifier;
Fangrui Song6907ce22018-07-30 19:24:48 +0000635
636 // Construct bogus (but well-formed) source information for the
Douglas Gregor9b272512011-02-28 23:58:31 +0000637 // nested-name-specifier.
638 BufferSize = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000639 SmallVector<NestedNameSpecifier *, 4> Stack;
Douglas Gregor9b272512011-02-28 23:58:31 +0000640 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
641 Stack.push_back(NNS);
642 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000643 NestedNameSpecifier *NNS = Stack.pop_back_val();
Douglas Gregor9b272512011-02-28 23:58:31 +0000644 switch (NNS->getKind()) {
645 case NestedNameSpecifier::Identifier:
646 case NestedNameSpecifier::Namespace:
647 case NestedNameSpecifier::NamespaceAlias:
648 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
649 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000650
Douglas Gregor9b272512011-02-28 23:58:31 +0000651 case NestedNameSpecifier::TypeSpec:
652 case NestedNameSpecifier::TypeSpecWithTemplate: {
653 TypeSourceInfo *TSInfo
654 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
655 R.getBegin());
Fangrui Song6907ce22018-07-30 19:24:48 +0000656 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
Douglas Gregor9b272512011-02-28 23:58:31 +0000657 BufferCapacity);
658 break;
659 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000660
Douglas Gregor9b272512011-02-28 23:58:31 +0000661 case NestedNameSpecifier::Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000662 case NestedNameSpecifier::Super:
Douglas Gregor9b272512011-02-28 23:58:31 +0000663 break;
664 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000665
Douglas Gregor9b272512011-02-28 23:58:31 +0000666 // Save the location of the '::'.
Fangrui Song6907ce22018-07-30 19:24:48 +0000667 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
Douglas Gregor9b272512011-02-28 23:58:31 +0000668 Buffer, BufferSize, BufferCapacity);
669 }
670}
671
672void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
673 if (BufferCapacity)
674 free(Buffer);
675
676 if (!Other) {
Craig Topper36250ad2014-05-12 05:36:57 +0000677 Representation = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000678 BufferSize = 0;
679 return;
680 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000681
682 // Rather than copying the data (which is wasteful), "adopt" the
Douglas Gregor9b272512011-02-28 23:58:31 +0000683 // pointer (which points into the ASTContext) but set the capacity to zero to
684 // indicate that we don't own it.
685 Representation = Other.getNestedNameSpecifier();
686 Buffer = static_cast<char *>(Other.getOpaqueData());
687 BufferSize = Other.getDataLength();
688 BufferCapacity = 0;
689}
690
Fangrui Song6907ce22018-07-30 19:24:48 +0000691NestedNameSpecifierLoc
Douglas Gregor9b272512011-02-28 23:58:31 +0000692NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
693 if (!Representation)
694 return NestedNameSpecifierLoc();
Fangrui Song6907ce22018-07-30 19:24:48 +0000695
Douglas Gregor9b272512011-02-28 23:58:31 +0000696 // If we adopted our data pointer from elsewhere in the AST context, there's
697 // no need to copy the memory.
698 if (BufferCapacity == 0)
699 return NestedNameSpecifierLoc(Representation, Buffer);
Fangrui Song6907ce22018-07-30 19:24:48 +0000700
Douglas Gregor9b272512011-02-28 23:58:31 +0000701 // FIXME: After copying the source-location information, should we free
702 // our (temporary) buffer and adopt the ASTContext-allocated memory?
703 // Doing so would optimize repeated calls to getWithLocInContext().
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000704 void *Mem = Context.Allocate(BufferSize, alignof(void *));
Douglas Gregor9b272512011-02-28 23:58:31 +0000705 memcpy(Mem, Buffer, BufferSize);
706 return NestedNameSpecifierLoc(Representation, Mem);
707}