blob: ede38626c8b646f76e7efc42b20b95e3afca0d07 [file] [log] [blame]
Douglas Gregor52537682009-03-19 00:18:19 +00001//===--- NestedNameSpecifier.cpp - C++ nested name specifiers -----*- 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 defines the NestedNameSpecifier class, which represents
11// a C++ nested-name-specifier.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/NestedNameSpecifier.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
Douglas Gregor7b26ff92011-02-24 02:36:08 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Douglas Gregor52537682009-03-19 00:18:19 +000019#include "clang/AST/Type.h"
Douglas Gregor869ad452011-02-24 17:54:50 +000020#include "clang/AST/TypeLoc.h"
Richard Smithbeb386a2012-08-15 01:41:43 +000021#include "llvm/Support/AlignOf.h"
Douglas Gregor18353912009-03-19 03:51:16 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregorf21eb492009-03-26 23:50:42 +000023#include <cassert>
Douglas Gregor18353912009-03-19 03:51:16 +000024
Douglas Gregor52537682009-03-19 00:18:19 +000025using namespace clang;
26
Douglas Gregorf21eb492009-03-26 23:50:42 +000027NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000028NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
Douglas Gregorf21eb492009-03-26 23:50:42 +000029 const NestedNameSpecifier &Mockup) {
30 llvm::FoldingSetNodeID ID;
31 Mockup.Profile(ID);
Douglas Gregor52537682009-03-19 00:18:19 +000032
Craig Topper36250ad2014-05-12 05:36:57 +000033 void *InsertPos = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000034 NestedNameSpecifier *NNS
Douglas Gregorf21eb492009-03-26 23:50:42 +000035 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
36 if (!NNS) {
Richard Smithbeb386a2012-08-15 01:41:43 +000037 NNS = new (Context, llvm::alignOf<NestedNameSpecifier>())
38 NestedNameSpecifier(Mockup);
Douglas Gregorf21eb492009-03-26 23:50:42 +000039 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
40 }
Douglas Gregor52537682009-03-19 00:18:19 +000041
Douglas Gregorf21eb492009-03-26 23:50:42 +000042 return NNS;
Douglas Gregor52537682009-03-19 00:18:19 +000043}
Douglas Gregor18353912009-03-19 03:51:16 +000044
Douglas Gregorf21eb492009-03-26 23:50:42 +000045NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000046NestedNameSpecifier::Create(const ASTContext &Context,
47 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000048 assert(II && "Identifier cannot be NULL");
Douglas Gregor308047d2009-09-09 00:23:06 +000049 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
Douglas Gregor18353912009-03-19 03:51:16 +000050
Douglas Gregorf21eb492009-03-26 23:50:42 +000051 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000052 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000053 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregordce2b622009-04-01 00:28:59 +000054 Mockup.Specifier = II;
Douglas Gregorf21eb492009-03-26 23:50:42 +000055 return FindOrInsert(Context, Mockup);
56}
Douglas Gregor18353912009-03-19 03:51:16 +000057
Douglas Gregorf21eb492009-03-26 23:50:42 +000058NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000059NestedNameSpecifier::Create(const ASTContext &Context,
Dmitri Gribenkoaeeca772013-01-23 17:06:56 +000060 NestedNameSpecifier *Prefix,
61 const NamespaceDecl *NS) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000062 assert(NS && "Namespace cannot be NULL");
Mike Stump11289f42009-09-09 15:08:12 +000063 assert((!Prefix ||
Craig Topper36250ad2014-05-12 05:36:57 +000064 (Prefix->getAsType() == nullptr &&
65 Prefix->getAsIdentifier() == nullptr)) &&
Douglas Gregorf21eb492009-03-26 23:50:42 +000066 "Broken nested name specifier");
67 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000068 Mockup.Prefix.setPointer(Prefix);
Nikola Smiljanic67860242014-09-26 00:28:20 +000069 Mockup.Prefix.setInt(StoredDecl);
Dmitri Gribenkoaeeca772013-01-23 17:06:56 +000070 Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
Douglas Gregorf21eb492009-03-26 23:50:42 +000071 return FindOrInsert(Context, Mockup);
72}
73
74NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000075NestedNameSpecifier::Create(const ASTContext &Context,
Douglas Gregor7b26ff92011-02-24 02:36:08 +000076 NestedNameSpecifier *Prefix,
77 NamespaceAliasDecl *Alias) {
78 assert(Alias && "Namespace alias cannot be NULL");
79 assert((!Prefix ||
Craig Topper36250ad2014-05-12 05:36:57 +000080 (Prefix->getAsType() == nullptr &&
81 Prefix->getAsIdentifier() == nullptr)) &&
Douglas Gregor7b26ff92011-02-24 02:36:08 +000082 "Broken nested name specifier");
83 NestedNameSpecifier Mockup;
84 Mockup.Prefix.setPointer(Prefix);
Nikola Smiljanic67860242014-09-26 00:28:20 +000085 Mockup.Prefix.setInt(StoredDecl);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000086 Mockup.Specifier = Alias;
87 return FindOrInsert(Context, Mockup);
88}
89
90NestedNameSpecifier *
91NestedNameSpecifier::Create(const ASTContext &Context,
Jay Foad39c79802011-01-12 09:06:06 +000092 NestedNameSpecifier *Prefix,
John McCall424cec92011-01-19 06:33:43 +000093 bool Template, const Type *T) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000094 assert(T && "Type cannot be NULL");
95 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000096 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000097 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
John McCall424cec92011-01-19 06:33:43 +000098 Mockup.Specifier = const_cast<Type*>(T);
Douglas Gregorf21eb492009-03-26 23:50:42 +000099 return FindOrInsert(Context, Mockup);
100}
Douglas Gregor64792e02009-09-02 23:58:38 +0000101
102NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +0000103NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
Douglas Gregor64792e02009-09-02 23:58:38 +0000104 assert(II && "Identifier cannot be NULL");
105 NestedNameSpecifier Mockup;
Craig Topper36250ad2014-05-12 05:36:57 +0000106 Mockup.Prefix.setPointer(nullptr);
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000107 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregor64792e02009-09-02 23:58:38 +0000108 Mockup.Specifier = II;
109 return FindOrInsert(Context, Mockup);
110}
111
Jay Foad39c79802011-01-12 09:06:06 +0000112NestedNameSpecifier *
113NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
Douglas Gregorf21eb492009-03-26 23:50:42 +0000114 if (!Context.GlobalNestedNameSpecifier)
Richard Smithbeb386a2012-08-15 01:41:43 +0000115 Context.GlobalNestedNameSpecifier =
116 new (Context, llvm::alignOf<NestedNameSpecifier>())
117 NestedNameSpecifier();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000118 return Context.GlobalNestedNameSpecifier;
119}
120
Nikola Smiljanic67860242014-09-26 00:28:20 +0000121NestedNameSpecifier *
122NestedNameSpecifier::SuperSpecifier(const ASTContext &Context,
123 CXXRecordDecl *RD) {
124 NestedNameSpecifier Mockup;
125 Mockup.Prefix.setPointer(nullptr);
126 Mockup.Prefix.setInt(StoredDecl);
127 Mockup.Specifier = RD;
128 return FindOrInsert(Context, Mockup);
129}
130
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000131NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
Craig Topper36250ad2014-05-12 05:36:57 +0000132 if (!Specifier)
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000133 return Global;
134
135 switch (Prefix.getInt()) {
136 case StoredIdentifier:
137 return Identifier;
138
Nikola Smiljanic67860242014-09-26 00:28:20 +0000139 case StoredDecl: {
140 NamedDecl *ND = static_cast<NamedDecl *>(Specifier);
141 if (isa<CXXRecordDecl>(ND))
142 return Super;
143 return isa<NamespaceDecl>(ND) ? Namespace : NamespaceAlias;
144 }
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000145
146 case StoredTypeSpec:
147 return TypeSpec;
148
149 case StoredTypeSpecWithTemplate:
150 return TypeSpecWithTemplate;
151 }
152
David Blaikiee4d798f2012-01-20 21:50:17 +0000153 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000154}
155
Nikola Smiljanic67860242014-09-26 00:28:20 +0000156/// \brief Retrieve the namespace stored in this nested name specifier.
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000157NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
Nikola Smiljanic67860242014-09-26 00:28:20 +0000158 if (Prefix.getInt() == StoredDecl)
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000159 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
160
Craig Topper36250ad2014-05-12 05:36:57 +0000161 return nullptr;
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000162}
163
Nikola Smiljanic67860242014-09-26 00:28:20 +0000164/// \brief Retrieve the namespace alias stored in this nested name specifier.
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000165NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
Nikola Smiljanic67860242014-09-26 00:28:20 +0000166 if (Prefix.getInt() == StoredDecl)
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000167 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
168
Craig Topper36250ad2014-05-12 05:36:57 +0000169 return nullptr;
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000170}
171
Nikola Smiljanic67860242014-09-26 00:28:20 +0000172/// \brief Retrieve the record declaration stored in this nested name specifier.
173CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const {
174 if (Prefix.getInt() == StoredDecl)
175 return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier));
176
177 return nullptr;
178}
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000179
Douglas Gregorf21eb492009-03-26 23:50:42 +0000180/// \brief Whether this nested name specifier refers to a dependent
181/// type or not.
182bool NestedNameSpecifier::isDependent() const {
183 switch (getKind()) {
184 case Identifier:
185 // Identifier specifiers always represent dependent types
186 return true;
187
188 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000189 case NamespaceAlias:
Douglas Gregorf21eb492009-03-26 23:50:42 +0000190 case Global:
191 return false;
192
Nikola Smiljanic67860242014-09-26 00:28:20 +0000193 case Super: {
194 CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier);
195 for (const auto &Base : RD->bases())
196 if (Base.getType()->isDependentType())
197 return true;
198
199 return false;
200 }
201
Douglas Gregorf21eb492009-03-26 23:50:42 +0000202 case TypeSpec:
203 case TypeSpecWithTemplate:
204 return getAsType()->isDependentType();
Douglas Gregor18353912009-03-19 03:51:16 +0000205 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000206
David Blaikiee4d798f2012-01-20 21:50:17 +0000207 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregorf21eb492009-03-26 23:50:42 +0000208}
209
Douglas Gregor678d76c2011-07-01 01:22:09 +0000210/// \brief Whether this nested name specifier refers to a dependent
211/// type or not.
212bool NestedNameSpecifier::isInstantiationDependent() const {
213 switch (getKind()) {
214 case Identifier:
215 // Identifier specifiers always represent dependent types
216 return true;
217
218 case Namespace:
219 case NamespaceAlias:
220 case Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000221 case Super:
Douglas Gregor678d76c2011-07-01 01:22:09 +0000222 return false;
Nikola Smiljanic67860242014-09-26 00:28:20 +0000223
Douglas Gregor678d76c2011-07-01 01:22:09 +0000224 case TypeSpec:
225 case TypeSpecWithTemplate:
226 return getAsType()->isInstantiationDependentType();
227 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000228
229 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor678d76c2011-07-01 01:22:09 +0000230}
231
Douglas Gregor506bd562010-12-13 22:49:22 +0000232bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
233 switch (getKind()) {
234 case Identifier:
235 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
236
237 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000238 case NamespaceAlias:
Douglas Gregor506bd562010-12-13 22:49:22 +0000239 case Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000240 case Super:
Douglas Gregor506bd562010-12-13 22:49:22 +0000241 return false;
242
243 case TypeSpec:
244 case TypeSpecWithTemplate:
245 return getAsType()->containsUnexpandedParameterPack();
246 }
247
David Blaikiee4d798f2012-01-20 21:50:17 +0000248 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor506bd562010-12-13 22:49:22 +0000249}
250
Douglas Gregorf21eb492009-03-26 23:50:42 +0000251/// \brief Print this nested name specifier to the given output
252/// stream.
Mike Stump11289f42009-09-09 15:08:12 +0000253void
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000254NestedNameSpecifier::print(raw_ostream &OS,
Douglas Gregor7de59662009-05-29 20:38:28 +0000255 const PrintingPolicy &Policy) const {
Douglas Gregordce2b622009-04-01 00:28:59 +0000256 if (getPrefix())
Douglas Gregor7de59662009-05-29 20:38:28 +0000257 getPrefix()->print(OS, Policy);
Douglas Gregorf21eb492009-03-26 23:50:42 +0000258
259 switch (getKind()) {
260 case Identifier:
261 OS << getAsIdentifier()->getName();
262 break;
263
264 case Namespace:
Douglas Gregor2e10cf92011-11-03 00:16:13 +0000265 if (getAsNamespace()->isAnonymousNamespace())
266 return;
267
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000268 OS << getAsNamespace()->getName();
269 break;
270
271 case NamespaceAlias:
272 OS << getAsNamespaceAlias()->getName();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000273 break;
274
275 case Global:
276 break;
277
Nikola Smiljanic67860242014-09-26 00:28:20 +0000278 case Super:
279 OS << "__super";
280 break;
281
Douglas Gregorf21eb492009-03-26 23:50:42 +0000282 case TypeSpecWithTemplate:
283 OS << "template ";
284 // Fall through to print the type.
285
286 case TypeSpec: {
John McCall424cec92011-01-19 06:33:43 +0000287 const Type *T = getAsType();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000288
Douglas Gregor7de59662009-05-29 20:38:28 +0000289 PrintingPolicy InnerPolicy(Policy);
John McCallb2e195a2009-09-05 06:31:47 +0000290 InnerPolicy.SuppressScope = true;
Mike Stump11289f42009-09-09 15:08:12 +0000291
Douglas Gregor053f6912009-08-26 00:04:55 +0000292 // Nested-name-specifiers are intended to contain minimally-qualified
Abramo Bagnara6150c882010-05-11 21:36:43 +0000293 // types. An actual ElaboratedType will not occur, since we'll store
Douglas Gregor053f6912009-08-26 00:04:55 +0000294 // just the type that is referred to in the nested-name-specifier (e.g.,
295 // a TypedefType, TagType, etc.). However, when we are dealing with
Mike Stump11289f42009-09-09 15:08:12 +0000296 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
Douglas Gregor053f6912009-08-26 00:04:55 +0000297 // the type requires its own nested-name-specifier for uniqueness, so we
298 // suppress that nested-name-specifier during printing.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000299 assert(!isa<ElaboratedType>(T) &&
300 "Elaborated type in nested-name-specifier");
Douglas Gregor053f6912009-08-26 00:04:55 +0000301 if (const TemplateSpecializationType *SpecType
302 = dyn_cast<TemplateSpecializationType>(T)) {
Mike Stump11289f42009-09-09 15:08:12 +0000303 // Print the template name without its corresponding
Douglas Gregor053f6912009-08-26 00:04:55 +0000304 // nested-name-specifier.
305 SpecType->getTemplateName().print(OS, InnerPolicy, true);
Mike Stump11289f42009-09-09 15:08:12 +0000306
Douglas Gregor053f6912009-08-26 00:04:55 +0000307 // Print the template argument list.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000308 TemplateSpecializationType::PrintTemplateArgumentList(
309 OS, SpecType->getArgs(), SpecType->getNumArgs(), InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000310 } else {
311 // Print the type normally
Benjamin Kramer9170e912013-02-22 15:46:01 +0000312 QualType(T, 0).print(OS, InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000313 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000314 break;
315 }
316 }
317
318 OS << "::";
319}
320
Yaron Keren015e6c82015-12-27 14:34:22 +0000321void NestedNameSpecifier::dump(const LangOptions &LO) const {
322 print(llvm::errs(), PrintingPolicy(LO));
323}
324
Yaron Kerencdae9412016-01-29 19:38:18 +0000325LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const {
Yaron Keren015e6c82015-12-27 14:34:22 +0000326 LangOptions LO;
Chris Lattnerc61089a2009-06-30 01:26:17 +0000327 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregor333489b2009-03-27 23:10:48 +0000328}
Douglas Gregor869ad452011-02-24 17:54:50 +0000329
330unsigned
331NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
332 assert(Qualifier && "Expected a non-NULL qualifier");
333
334 // Location of the trailing '::'.
335 unsigned Length = sizeof(unsigned);
336
337 switch (Qualifier->getKind()) {
338 case NestedNameSpecifier::Global:
339 // Nothing more to add.
340 break;
341
342 case NestedNameSpecifier::Identifier:
343 case NestedNameSpecifier::Namespace:
344 case NestedNameSpecifier::NamespaceAlias:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000345 case NestedNameSpecifier::Super:
Douglas Gregor869ad452011-02-24 17:54:50 +0000346 // The location of the identifier or namespace name.
347 Length += sizeof(unsigned);
348 break;
349
350 case NestedNameSpecifier::TypeSpecWithTemplate:
351 case NestedNameSpecifier::TypeSpec:
352 // The "void*" that points at the TypeLoc data.
353 // Note: the 'template' keyword is part of the TypeLoc.
354 Length += sizeof(void *);
355 break;
356 }
357
358 return Length;
359}
360
361unsigned
362NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
363 unsigned Length = 0;
364 for (; Qualifier; Qualifier = Qualifier->getPrefix())
365 Length += getLocalDataLength(Qualifier);
366 return Length;
367}
368
369namespace {
370 /// \brief Load a (possibly unaligned) source location from a given address
371 /// and offset.
372 SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
373 unsigned Raw;
374 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
375 return SourceLocation::getFromRawEncoding(Raw);
376 }
377
378 /// \brief Load a (possibly unaligned) pointer from a given address and
379 /// offset.
380 void *LoadPointer(void *Data, unsigned Offset) {
381 void *Result;
382 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
383 return Result;
384 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000385}
Douglas Gregor869ad452011-02-24 17:54:50 +0000386
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000387SourceRange NestedNameSpecifierLoc::getSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000388 if (!Qualifier)
389 return SourceRange();
390
Douglas Gregor869ad452011-02-24 17:54:50 +0000391 NestedNameSpecifierLoc First = *this;
Douglas Gregor12441b32011-02-25 16:33:46 +0000392 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
Douglas Gregor869ad452011-02-24 17:54:50 +0000393 First = Prefix;
394
395 return SourceRange(First.getLocalSourceRange().getBegin(),
396 getLocalSourceRange().getEnd());
397}
398
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000399SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000400 if (!Qualifier)
401 return SourceRange();
402
Douglas Gregor869ad452011-02-24 17:54:50 +0000403 unsigned Offset = getDataLength(Qualifier->getPrefix());
404 switch (Qualifier->getKind()) {
405 case NestedNameSpecifier::Global:
406 return LoadSourceLocation(Data, Offset);
407
408 case NestedNameSpecifier::Identifier:
409 case NestedNameSpecifier::Namespace:
410 case NestedNameSpecifier::NamespaceAlias:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000411 case NestedNameSpecifier::Super:
Douglas Gregor869ad452011-02-24 17:54:50 +0000412 return SourceRange(LoadSourceLocation(Data, Offset),
413 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
414
415 case NestedNameSpecifier::TypeSpecWithTemplate:
416 case NestedNameSpecifier::TypeSpec: {
417 // The "void*" that points at the TypeLoc data.
418 // Note: the 'template' keyword is part of the TypeLoc.
419 void *TypeData = LoadPointer(Data, Offset);
420 TypeLoc TL(Qualifier->getAsType(), TypeData);
421 return SourceRange(TL.getBeginLoc(),
422 LoadSourceLocation(Data, Offset + sizeof(void*)));
423 }
424 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000425
426 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor869ad452011-02-24 17:54:50 +0000427}
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000428
429TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
430 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
431 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
432 "Nested-name-specifier location is not a type");
433
434 // The "void*" that points at the TypeLoc data.
435 unsigned Offset = getDataLength(Qualifier->getPrefix());
436 void *TypeData = LoadPointer(Data, Offset);
437 return TypeLoc(Qualifier->getAsType(), TypeData);
438}
Douglas Gregor9b272512011-02-28 23:58:31 +0000439
440namespace {
441 void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
442 unsigned &BufferCapacity) {
Chandler Carruthb6708d82015-08-04 03:52:56 +0000443 if (Start == End)
444 return;
445
Douglas Gregor9b272512011-02-28 23:58:31 +0000446 if (BufferSize + (End - Start) > BufferCapacity) {
447 // Reallocate the buffer.
Chandler Carruthb6708d82015-08-04 03:52:56 +0000448 unsigned NewCapacity = std::max(
449 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
450 (unsigned)(BufferSize + (End - Start)));
Douglas Gregor9b272512011-02-28 23:58:31 +0000451 char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
Chandler Carruthb6708d82015-08-04 03:52:56 +0000452 if (BufferCapacity) {
453 memcpy(NewBuffer, Buffer, BufferSize);
Douglas Gregor9b272512011-02-28 23:58:31 +0000454 free(Buffer);
Chandler Carruthb6708d82015-08-04 03:52:56 +0000455 }
Douglas Gregor9b272512011-02-28 23:58:31 +0000456 Buffer = NewBuffer;
457 BufferCapacity = NewCapacity;
458 }
459
460 memcpy(Buffer + BufferSize, Start, End - Start);
461 BufferSize += End-Start;
462 }
463
464 /// \brief Save a source location to the given buffer.
465 void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
466 unsigned &BufferSize, unsigned &BufferCapacity) {
467 unsigned Raw = Loc.getRawEncoding();
468 Append(reinterpret_cast<char *>(&Raw),
469 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
470 Buffer, BufferSize, BufferCapacity);
471 }
472
473 /// \brief Save a pointer to the given buffer.
474 void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
475 unsigned &BufferCapacity) {
476 Append(reinterpret_cast<char *>(&Ptr),
477 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
478 Buffer, BufferSize, BufferCapacity);
479 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000480}
Douglas Gregor9b272512011-02-28 23:58:31 +0000481
Douglas Gregor9b272512011-02-28 23:58:31 +0000482NestedNameSpecifierLocBuilder::
483NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
Craig Topper36250ad2014-05-12 05:36:57 +0000484 : Representation(Other.Representation), Buffer(nullptr),
Douglas Gregor9b272512011-02-28 23:58:31 +0000485 BufferSize(0), BufferCapacity(0)
486{
487 if (!Other.Buffer)
488 return;
489
490 if (Other.BufferCapacity == 0) {
491 // Shallow copy is okay.
492 Buffer = Other.Buffer;
493 BufferSize = Other.BufferSize;
494 return;
495 }
496
497 // Deep copy
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000498 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
499 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000500}
501
502NestedNameSpecifierLocBuilder &
503NestedNameSpecifierLocBuilder::
504operator=(const NestedNameSpecifierLocBuilder &Other) {
505 Representation = Other.Representation;
506
507 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
508 // Re-use our storage.
509 BufferSize = Other.BufferSize;
510 memcpy(Buffer, Other.Buffer, BufferSize);
511 return *this;
512 }
513
514 // Free our storage, if we have any.
515 if (BufferCapacity) {
516 free(Buffer);
517 BufferCapacity = 0;
518 }
519
520 if (!Other.Buffer) {
521 // Empty.
Craig Topper36250ad2014-05-12 05:36:57 +0000522 Buffer = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000523 BufferSize = 0;
524 return *this;
525 }
526
527 if (Other.BufferCapacity == 0) {
528 // Shallow copy is okay.
529 Buffer = Other.Buffer;
530 BufferSize = Other.BufferSize;
531 return *this;
532 }
533
534 // Deep copy.
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000535 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
536 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000537 return *this;
538}
539
Douglas Gregor9b272512011-02-28 23:58:31 +0000540void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
541 SourceLocation TemplateKWLoc,
542 TypeLoc TL,
543 SourceLocation ColonColonLoc) {
544 Representation = NestedNameSpecifier::Create(Context, Representation,
545 TemplateKWLoc.isValid(),
546 TL.getTypePtr());
547
548 // Push source-location info into the buffer.
549 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
550 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
551}
552
553void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
554 IdentifierInfo *Identifier,
555 SourceLocation IdentifierLoc,
556 SourceLocation ColonColonLoc) {
557 Representation = NestedNameSpecifier::Create(Context, Representation,
558 Identifier);
559
560 // Push source-location info into the buffer.
561 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
562 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
563}
564
565void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
566 NamespaceDecl *Namespace,
567 SourceLocation NamespaceLoc,
568 SourceLocation ColonColonLoc) {
569 Representation = NestedNameSpecifier::Create(Context, Representation,
570 Namespace);
571
572 // Push source-location info into the buffer.
573 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
574 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
575}
576
577void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
578 NamespaceAliasDecl *Alias,
579 SourceLocation AliasLoc,
580 SourceLocation ColonColonLoc) {
581 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
582
583 // Push source-location info into the buffer.
584 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
585 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
586}
587
588void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
589 SourceLocation ColonColonLoc) {
590 assert(!Representation && "Already have a nested-name-specifier!?");
591 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
592
593 // Push source-location info into the buffer.
594 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
595}
596
Nikola Smiljanic67860242014-09-26 00:28:20 +0000597void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
598 CXXRecordDecl *RD,
599 SourceLocation SuperLoc,
600 SourceLocation ColonColonLoc) {
601 Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
602
603 // Push source-location info into the buffer.
604 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
605 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
606}
607
Douglas Gregor9b272512011-02-28 23:58:31 +0000608void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
609 NestedNameSpecifier *Qualifier,
610 SourceRange R) {
611 Representation = Qualifier;
612
613 // Construct bogus (but well-formed) source information for the
614 // nested-name-specifier.
615 BufferSize = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000616 SmallVector<NestedNameSpecifier *, 4> Stack;
Douglas Gregor9b272512011-02-28 23:58:31 +0000617 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
618 Stack.push_back(NNS);
619 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000620 NestedNameSpecifier *NNS = Stack.pop_back_val();
Douglas Gregor9b272512011-02-28 23:58:31 +0000621 switch (NNS->getKind()) {
622 case NestedNameSpecifier::Identifier:
623 case NestedNameSpecifier::Namespace:
624 case NestedNameSpecifier::NamespaceAlias:
625 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
626 break;
627
628 case NestedNameSpecifier::TypeSpec:
629 case NestedNameSpecifier::TypeSpecWithTemplate: {
630 TypeSourceInfo *TSInfo
631 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
632 R.getBegin());
633 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
634 BufferCapacity);
635 break;
636 }
637
638 case NestedNameSpecifier::Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000639 case NestedNameSpecifier::Super:
Douglas Gregor9b272512011-02-28 23:58:31 +0000640 break;
641 }
642
643 // Save the location of the '::'.
644 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
645 Buffer, BufferSize, BufferCapacity);
646 }
647}
648
649void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
650 if (BufferCapacity)
651 free(Buffer);
652
653 if (!Other) {
Craig Topper36250ad2014-05-12 05:36:57 +0000654 Representation = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000655 BufferSize = 0;
656 return;
657 }
658
659 // Rather than copying the data (which is wasteful), "adopt" the
660 // pointer (which points into the ASTContext) but set the capacity to zero to
661 // indicate that we don't own it.
662 Representation = Other.getNestedNameSpecifier();
663 Buffer = static_cast<char *>(Other.getOpaqueData());
664 BufferSize = Other.getDataLength();
665 BufferCapacity = 0;
666}
667
668NestedNameSpecifierLoc
669NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
670 if (!Representation)
671 return NestedNameSpecifierLoc();
672
673 // If we adopted our data pointer from elsewhere in the AST context, there's
674 // no need to copy the memory.
675 if (BufferCapacity == 0)
676 return NestedNameSpecifierLoc(Representation, Buffer);
677
678 // FIXME: After copying the source-location information, should we free
679 // our (temporary) buffer and adopt the ASTContext-allocated memory?
680 // Doing so would optimize repeated calls to getWithLocInContext().
681 void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
682 memcpy(Mem, Buffer, BufferSize);
683 return NestedNameSpecifierLoc(Representation, Mem);
684}