blob: 2f38db40d9ab77fe2aebc88d2bb5300aff0e009a [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 {
Richard Smith5179eb72016-06-28 19:03:57 +0000174 switch (Prefix.getInt()) {
175 case StoredIdentifier:
176 return nullptr;
177
178 case StoredDecl:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000179 return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier));
180
Richard Smith5179eb72016-06-28 19:03:57 +0000181 case StoredTypeSpec:
182 case StoredTypeSpecWithTemplate:
183 return getAsType()->getAsCXXRecordDecl();
184 }
185
186 llvm_unreachable("Invalid NNS Kind!");
Nikola Smiljanic67860242014-09-26 00:28:20 +0000187}
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000188
Douglas Gregorf21eb492009-03-26 23:50:42 +0000189/// \brief Whether this nested name specifier refers to a dependent
190/// type or not.
191bool NestedNameSpecifier::isDependent() const {
192 switch (getKind()) {
193 case Identifier:
194 // Identifier specifiers always represent dependent types
195 return true;
196
197 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000198 case NamespaceAlias:
Douglas Gregorf21eb492009-03-26 23:50:42 +0000199 case Global:
200 return false;
201
Nikola Smiljanic67860242014-09-26 00:28:20 +0000202 case Super: {
203 CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier);
204 for (const auto &Base : RD->bases())
205 if (Base.getType()->isDependentType())
206 return true;
207
208 return false;
209 }
210
Douglas Gregorf21eb492009-03-26 23:50:42 +0000211 case TypeSpec:
212 case TypeSpecWithTemplate:
213 return getAsType()->isDependentType();
Douglas Gregor18353912009-03-19 03:51:16 +0000214 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000215
David Blaikiee4d798f2012-01-20 21:50:17 +0000216 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregorf21eb492009-03-26 23:50:42 +0000217}
218
Douglas Gregor678d76c2011-07-01 01:22:09 +0000219/// \brief Whether this nested name specifier refers to a dependent
220/// type or not.
221bool NestedNameSpecifier::isInstantiationDependent() const {
222 switch (getKind()) {
223 case Identifier:
224 // Identifier specifiers always represent dependent types
225 return true;
226
227 case Namespace:
228 case NamespaceAlias:
229 case Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000230 case Super:
Douglas Gregor678d76c2011-07-01 01:22:09 +0000231 return false;
Nikola Smiljanic67860242014-09-26 00:28:20 +0000232
Douglas Gregor678d76c2011-07-01 01:22:09 +0000233 case TypeSpec:
234 case TypeSpecWithTemplate:
235 return getAsType()->isInstantiationDependentType();
236 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000237
238 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor678d76c2011-07-01 01:22:09 +0000239}
240
Douglas Gregor506bd562010-12-13 22:49:22 +0000241bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
242 switch (getKind()) {
243 case Identifier:
244 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
245
246 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000247 case NamespaceAlias:
Douglas Gregor506bd562010-12-13 22:49:22 +0000248 case Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000249 case Super:
Douglas Gregor506bd562010-12-13 22:49:22 +0000250 return false;
251
252 case TypeSpec:
253 case TypeSpecWithTemplate:
254 return getAsType()->containsUnexpandedParameterPack();
255 }
256
David Blaikiee4d798f2012-01-20 21:50:17 +0000257 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor506bd562010-12-13 22:49:22 +0000258}
259
Douglas Gregorf21eb492009-03-26 23:50:42 +0000260/// \brief Print this nested name specifier to the given output
261/// stream.
Mike Stump11289f42009-09-09 15:08:12 +0000262void
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000263NestedNameSpecifier::print(raw_ostream &OS,
Douglas Gregor7de59662009-05-29 20:38:28 +0000264 const PrintingPolicy &Policy) const {
Douglas Gregordce2b622009-04-01 00:28:59 +0000265 if (getPrefix())
Douglas Gregor7de59662009-05-29 20:38:28 +0000266 getPrefix()->print(OS, Policy);
Douglas Gregorf21eb492009-03-26 23:50:42 +0000267
268 switch (getKind()) {
269 case Identifier:
270 OS << getAsIdentifier()->getName();
271 break;
272
273 case Namespace:
Douglas Gregor2e10cf92011-11-03 00:16:13 +0000274 if (getAsNamespace()->isAnonymousNamespace())
275 return;
276
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000277 OS << getAsNamespace()->getName();
278 break;
279
280 case NamespaceAlias:
281 OS << getAsNamespaceAlias()->getName();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000282 break;
283
284 case Global:
285 break;
286
Nikola Smiljanic67860242014-09-26 00:28:20 +0000287 case Super:
288 OS << "__super";
289 break;
290
Douglas Gregorf21eb492009-03-26 23:50:42 +0000291 case TypeSpecWithTemplate:
292 OS << "template ";
293 // Fall through to print the type.
294
295 case TypeSpec: {
John McCall424cec92011-01-19 06:33:43 +0000296 const Type *T = getAsType();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000297
Douglas Gregor7de59662009-05-29 20:38:28 +0000298 PrintingPolicy InnerPolicy(Policy);
John McCallb2e195a2009-09-05 06:31:47 +0000299 InnerPolicy.SuppressScope = true;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Douglas Gregor053f6912009-08-26 00:04:55 +0000301 // Nested-name-specifiers are intended to contain minimally-qualified
Abramo Bagnara6150c882010-05-11 21:36:43 +0000302 // types. An actual ElaboratedType will not occur, since we'll store
Douglas Gregor053f6912009-08-26 00:04:55 +0000303 // just the type that is referred to in the nested-name-specifier (e.g.,
304 // a TypedefType, TagType, etc.). However, when we are dealing with
Mike Stump11289f42009-09-09 15:08:12 +0000305 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
Douglas Gregor053f6912009-08-26 00:04:55 +0000306 // the type requires its own nested-name-specifier for uniqueness, so we
307 // suppress that nested-name-specifier during printing.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000308 assert(!isa<ElaboratedType>(T) &&
309 "Elaborated type in nested-name-specifier");
Douglas Gregor053f6912009-08-26 00:04:55 +0000310 if (const TemplateSpecializationType *SpecType
311 = dyn_cast<TemplateSpecializationType>(T)) {
Mike Stump11289f42009-09-09 15:08:12 +0000312 // Print the template name without its corresponding
Douglas Gregor053f6912009-08-26 00:04:55 +0000313 // nested-name-specifier.
314 SpecType->getTemplateName().print(OS, InnerPolicy, true);
Mike Stump11289f42009-09-09 15:08:12 +0000315
Douglas Gregor053f6912009-08-26 00:04:55 +0000316 // Print the template argument list.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000317 TemplateSpecializationType::PrintTemplateArgumentList(
318 OS, SpecType->getArgs(), SpecType->getNumArgs(), InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000319 } else {
320 // Print the type normally
Benjamin Kramer9170e912013-02-22 15:46:01 +0000321 QualType(T, 0).print(OS, InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000322 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000323 break;
324 }
325 }
326
327 OS << "::";
328}
329
Yaron Keren015e6c82015-12-27 14:34:22 +0000330void NestedNameSpecifier::dump(const LangOptions &LO) const {
331 print(llvm::errs(), PrintingPolicy(LO));
332}
333
Yaron Kerencdae9412016-01-29 19:38:18 +0000334LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const {
Yaron Keren015e6c82015-12-27 14:34:22 +0000335 LangOptions LO;
Chris Lattnerc61089a2009-06-30 01:26:17 +0000336 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregor333489b2009-03-27 23:10:48 +0000337}
Douglas Gregor869ad452011-02-24 17:54:50 +0000338
339unsigned
340NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
341 assert(Qualifier && "Expected a non-NULL qualifier");
342
343 // Location of the trailing '::'.
344 unsigned Length = sizeof(unsigned);
345
346 switch (Qualifier->getKind()) {
347 case NestedNameSpecifier::Global:
348 // Nothing more to add.
349 break;
350
351 case NestedNameSpecifier::Identifier:
352 case NestedNameSpecifier::Namespace:
353 case NestedNameSpecifier::NamespaceAlias:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000354 case NestedNameSpecifier::Super:
Douglas Gregor869ad452011-02-24 17:54:50 +0000355 // The location of the identifier or namespace name.
356 Length += sizeof(unsigned);
357 break;
358
359 case NestedNameSpecifier::TypeSpecWithTemplate:
360 case NestedNameSpecifier::TypeSpec:
361 // The "void*" that points at the TypeLoc data.
362 // Note: the 'template' keyword is part of the TypeLoc.
363 Length += sizeof(void *);
364 break;
365 }
366
367 return Length;
368}
369
370unsigned
371NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
372 unsigned Length = 0;
373 for (; Qualifier; Qualifier = Qualifier->getPrefix())
374 Length += getLocalDataLength(Qualifier);
375 return Length;
376}
377
378namespace {
379 /// \brief Load a (possibly unaligned) source location from a given address
380 /// and offset.
381 SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
382 unsigned Raw;
383 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
384 return SourceLocation::getFromRawEncoding(Raw);
385 }
386
387 /// \brief Load a (possibly unaligned) pointer from a given address and
388 /// offset.
389 void *LoadPointer(void *Data, unsigned Offset) {
390 void *Result;
391 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
392 return Result;
393 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000394}
Douglas Gregor869ad452011-02-24 17:54:50 +0000395
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000396SourceRange NestedNameSpecifierLoc::getSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000397 if (!Qualifier)
398 return SourceRange();
399
Douglas Gregor869ad452011-02-24 17:54:50 +0000400 NestedNameSpecifierLoc First = *this;
Douglas Gregor12441b32011-02-25 16:33:46 +0000401 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
Douglas Gregor869ad452011-02-24 17:54:50 +0000402 First = Prefix;
403
404 return SourceRange(First.getLocalSourceRange().getBegin(),
405 getLocalSourceRange().getEnd());
406}
407
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000408SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000409 if (!Qualifier)
410 return SourceRange();
411
Douglas Gregor869ad452011-02-24 17:54:50 +0000412 unsigned Offset = getDataLength(Qualifier->getPrefix());
413 switch (Qualifier->getKind()) {
414 case NestedNameSpecifier::Global:
415 return LoadSourceLocation(Data, Offset);
416
417 case NestedNameSpecifier::Identifier:
418 case NestedNameSpecifier::Namespace:
419 case NestedNameSpecifier::NamespaceAlias:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000420 case NestedNameSpecifier::Super:
Douglas Gregor869ad452011-02-24 17:54:50 +0000421 return SourceRange(LoadSourceLocation(Data, Offset),
422 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
423
424 case NestedNameSpecifier::TypeSpecWithTemplate:
425 case NestedNameSpecifier::TypeSpec: {
426 // The "void*" that points at the TypeLoc data.
427 // Note: the 'template' keyword is part of the TypeLoc.
428 void *TypeData = LoadPointer(Data, Offset);
429 TypeLoc TL(Qualifier->getAsType(), TypeData);
430 return SourceRange(TL.getBeginLoc(),
431 LoadSourceLocation(Data, Offset + sizeof(void*)));
432 }
433 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000434
435 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor869ad452011-02-24 17:54:50 +0000436}
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000437
438TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
439 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
440 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
441 "Nested-name-specifier location is not a type");
442
443 // The "void*" that points at the TypeLoc data.
444 unsigned Offset = getDataLength(Qualifier->getPrefix());
445 void *TypeData = LoadPointer(Data, Offset);
446 return TypeLoc(Qualifier->getAsType(), TypeData);
447}
Douglas Gregor9b272512011-02-28 23:58:31 +0000448
449namespace {
450 void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
451 unsigned &BufferCapacity) {
Chandler Carruthb6708d82015-08-04 03:52:56 +0000452 if (Start == End)
453 return;
454
Douglas Gregor9b272512011-02-28 23:58:31 +0000455 if (BufferSize + (End - Start) > BufferCapacity) {
456 // Reallocate the buffer.
Chandler Carruthb6708d82015-08-04 03:52:56 +0000457 unsigned NewCapacity = std::max(
458 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
459 (unsigned)(BufferSize + (End - Start)));
Douglas Gregor9b272512011-02-28 23:58:31 +0000460 char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
Chandler Carruthb6708d82015-08-04 03:52:56 +0000461 if (BufferCapacity) {
462 memcpy(NewBuffer, Buffer, BufferSize);
Douglas Gregor9b272512011-02-28 23:58:31 +0000463 free(Buffer);
Chandler Carruthb6708d82015-08-04 03:52:56 +0000464 }
Douglas Gregor9b272512011-02-28 23:58:31 +0000465 Buffer = NewBuffer;
466 BufferCapacity = NewCapacity;
467 }
468
469 memcpy(Buffer + BufferSize, Start, End - Start);
470 BufferSize += End-Start;
471 }
472
473 /// \brief Save a source location to the given buffer.
474 void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
475 unsigned &BufferSize, unsigned &BufferCapacity) {
476 unsigned Raw = Loc.getRawEncoding();
477 Append(reinterpret_cast<char *>(&Raw),
478 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
479 Buffer, BufferSize, BufferCapacity);
480 }
481
482 /// \brief Save a pointer to the given buffer.
483 void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
484 unsigned &BufferCapacity) {
485 Append(reinterpret_cast<char *>(&Ptr),
486 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
487 Buffer, BufferSize, BufferCapacity);
488 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000489}
Douglas Gregor9b272512011-02-28 23:58:31 +0000490
Douglas Gregor9b272512011-02-28 23:58:31 +0000491NestedNameSpecifierLocBuilder::
492NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
Craig Topper36250ad2014-05-12 05:36:57 +0000493 : Representation(Other.Representation), Buffer(nullptr),
Douglas Gregor9b272512011-02-28 23:58:31 +0000494 BufferSize(0), BufferCapacity(0)
495{
496 if (!Other.Buffer)
497 return;
498
499 if (Other.BufferCapacity == 0) {
500 // Shallow copy is okay.
501 Buffer = Other.Buffer;
502 BufferSize = Other.BufferSize;
503 return;
504 }
505
506 // Deep copy
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000507 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
508 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000509}
510
511NestedNameSpecifierLocBuilder &
512NestedNameSpecifierLocBuilder::
513operator=(const NestedNameSpecifierLocBuilder &Other) {
514 Representation = Other.Representation;
515
516 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
517 // Re-use our storage.
518 BufferSize = Other.BufferSize;
519 memcpy(Buffer, Other.Buffer, BufferSize);
520 return *this;
521 }
522
523 // Free our storage, if we have any.
524 if (BufferCapacity) {
525 free(Buffer);
526 BufferCapacity = 0;
527 }
528
529 if (!Other.Buffer) {
530 // Empty.
Craig Topper36250ad2014-05-12 05:36:57 +0000531 Buffer = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000532 BufferSize = 0;
533 return *this;
534 }
535
536 if (Other.BufferCapacity == 0) {
537 // Shallow copy is okay.
538 Buffer = Other.Buffer;
539 BufferSize = Other.BufferSize;
540 return *this;
541 }
542
543 // Deep copy.
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000544 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
545 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000546 return *this;
547}
548
Douglas Gregor9b272512011-02-28 23:58:31 +0000549void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
550 SourceLocation TemplateKWLoc,
551 TypeLoc TL,
552 SourceLocation ColonColonLoc) {
553 Representation = NestedNameSpecifier::Create(Context, Representation,
554 TemplateKWLoc.isValid(),
555 TL.getTypePtr());
556
557 // Push source-location info into the buffer.
558 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
559 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
560}
561
562void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
563 IdentifierInfo *Identifier,
564 SourceLocation IdentifierLoc,
565 SourceLocation ColonColonLoc) {
566 Representation = NestedNameSpecifier::Create(Context, Representation,
567 Identifier);
568
569 // Push source-location info into the buffer.
570 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
571 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
572}
573
574void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
575 NamespaceDecl *Namespace,
576 SourceLocation NamespaceLoc,
577 SourceLocation ColonColonLoc) {
578 Representation = NestedNameSpecifier::Create(Context, Representation,
579 Namespace);
580
581 // Push source-location info into the buffer.
582 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
583 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
584}
585
586void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
587 NamespaceAliasDecl *Alias,
588 SourceLocation AliasLoc,
589 SourceLocation ColonColonLoc) {
590 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
591
592 // Push source-location info into the buffer.
593 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
594 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
595}
596
597void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
598 SourceLocation ColonColonLoc) {
599 assert(!Representation && "Already have a nested-name-specifier!?");
600 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
601
602 // Push source-location info into the buffer.
603 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
604}
605
Nikola Smiljanic67860242014-09-26 00:28:20 +0000606void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
607 CXXRecordDecl *RD,
608 SourceLocation SuperLoc,
609 SourceLocation ColonColonLoc) {
610 Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
611
612 // Push source-location info into the buffer.
613 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
614 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
615}
616
Douglas Gregor9b272512011-02-28 23:58:31 +0000617void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
618 NestedNameSpecifier *Qualifier,
619 SourceRange R) {
620 Representation = Qualifier;
621
622 // Construct bogus (but well-formed) source information for the
623 // nested-name-specifier.
624 BufferSize = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000625 SmallVector<NestedNameSpecifier *, 4> Stack;
Douglas Gregor9b272512011-02-28 23:58:31 +0000626 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
627 Stack.push_back(NNS);
628 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000629 NestedNameSpecifier *NNS = Stack.pop_back_val();
Douglas Gregor9b272512011-02-28 23:58:31 +0000630 switch (NNS->getKind()) {
631 case NestedNameSpecifier::Identifier:
632 case NestedNameSpecifier::Namespace:
633 case NestedNameSpecifier::NamespaceAlias:
634 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
635 break;
636
637 case NestedNameSpecifier::TypeSpec:
638 case NestedNameSpecifier::TypeSpecWithTemplate: {
639 TypeSourceInfo *TSInfo
640 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
641 R.getBegin());
642 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
643 BufferCapacity);
644 break;
645 }
646
647 case NestedNameSpecifier::Global:
Nikola Smiljanic67860242014-09-26 00:28:20 +0000648 case NestedNameSpecifier::Super:
Douglas Gregor9b272512011-02-28 23:58:31 +0000649 break;
650 }
651
652 // Save the location of the '::'.
653 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
654 Buffer, BufferSize, BufferCapacity);
655 }
656}
657
658void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
659 if (BufferCapacity)
660 free(Buffer);
661
662 if (!Other) {
Craig Topper36250ad2014-05-12 05:36:57 +0000663 Representation = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000664 BufferSize = 0;
665 return;
666 }
667
668 // Rather than copying the data (which is wasteful), "adopt" the
669 // pointer (which points into the ASTContext) but set the capacity to zero to
670 // indicate that we don't own it.
671 Representation = Other.getNestedNameSpecifier();
672 Buffer = static_cast<char *>(Other.getOpaqueData());
673 BufferSize = Other.getDataLength();
674 BufferCapacity = 0;
675}
676
677NestedNameSpecifierLoc
678NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
679 if (!Representation)
680 return NestedNameSpecifierLoc();
681
682 // If we adopted our data pointer from elsewhere in the AST context, there's
683 // no need to copy the memory.
684 if (BufferCapacity == 0)
685 return NestedNameSpecifierLoc(Representation, Buffer);
686
687 // FIXME: After copying the source-location information, should we free
688 // our (temporary) buffer and adopt the ASTContext-allocated memory?
689 // Doing so would optimize repeated calls to getWithLocInContext().
690 void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
691 memcpy(Mem, Buffer, BufferSize);
692 return NestedNameSpecifierLoc(Representation, Mem);
693}