Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1 | //===--- 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 Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/Type.h" |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLoc.h" |
Richard Smith | beb386a | 2012-08-15 01:41:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/AlignOf.h" |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 23 | #include <cassert> |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 24 | |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 27 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 28 | NestedNameSpecifier::FindOrInsert(const ASTContext &Context, |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 29 | const NestedNameSpecifier &Mockup) { |
| 30 | llvm::FoldingSetNodeID ID; |
| 31 | Mockup.Profile(ID); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 32 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 33 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | NestedNameSpecifier *NNS |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 35 | = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos); |
| 36 | if (!NNS) { |
Richard Smith | beb386a | 2012-08-15 01:41:43 +0000 | [diff] [blame] | 37 | NNS = new (Context, llvm::alignOf<NestedNameSpecifier>()) |
| 38 | NestedNameSpecifier(Mockup); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 39 | Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos); |
| 40 | } |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 42 | return NNS; |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 43 | } |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 45 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 46 | NestedNameSpecifier::Create(const ASTContext &Context, |
| 47 | NestedNameSpecifier *Prefix, IdentifierInfo *II) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 48 | assert(II && "Identifier cannot be NULL"); |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 49 | assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent"); |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 51 | NestedNameSpecifier Mockup; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 52 | Mockup.Prefix.setPointer(Prefix); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 53 | Mockup.Prefix.setInt(StoredIdentifier); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 54 | Mockup.Specifier = II; |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 55 | return FindOrInsert(Context, Mockup); |
| 56 | } |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 58 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 59 | NestedNameSpecifier::Create(const ASTContext &Context, |
Dmitri Gribenko | aeeca77 | 2013-01-23 17:06:56 +0000 | [diff] [blame] | 60 | NestedNameSpecifier *Prefix, |
| 61 | const NamespaceDecl *NS) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 62 | assert(NS && "Namespace cannot be NULL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | assert((!Prefix || |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 64 | (Prefix->getAsType() == nullptr && |
| 65 | Prefix->getAsIdentifier() == nullptr)) && |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 66 | "Broken nested name specifier"); |
| 67 | NestedNameSpecifier Mockup; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 68 | Mockup.Prefix.setPointer(Prefix); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 69 | Mockup.Prefix.setInt(StoredDecl); |
Dmitri Gribenko | aeeca77 | 2013-01-23 17:06:56 +0000 | [diff] [blame] | 70 | Mockup.Specifier = const_cast<NamespaceDecl *>(NS); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 71 | return FindOrInsert(Context, Mockup); |
| 72 | } |
| 73 | |
| 74 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 75 | NestedNameSpecifier::Create(const ASTContext &Context, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 76 | NestedNameSpecifier *Prefix, |
| 77 | NamespaceAliasDecl *Alias) { |
| 78 | assert(Alias && "Namespace alias cannot be NULL"); |
| 79 | assert((!Prefix || |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 80 | (Prefix->getAsType() == nullptr && |
| 81 | Prefix->getAsIdentifier() == nullptr)) && |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 82 | "Broken nested name specifier"); |
| 83 | NestedNameSpecifier Mockup; |
| 84 | Mockup.Prefix.setPointer(Prefix); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 85 | Mockup.Prefix.setInt(StoredDecl); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 86 | Mockup.Specifier = Alias; |
| 87 | return FindOrInsert(Context, Mockup); |
| 88 | } |
| 89 | |
| 90 | NestedNameSpecifier * |
| 91 | NestedNameSpecifier::Create(const ASTContext &Context, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 92 | NestedNameSpecifier *Prefix, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 93 | bool Template, const Type *T) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 94 | assert(T && "Type cannot be NULL"); |
| 95 | NestedNameSpecifier Mockup; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 96 | Mockup.Prefix.setPointer(Prefix); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 97 | Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 98 | Mockup.Specifier = const_cast<Type*>(T); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 99 | return FindOrInsert(Context, Mockup); |
| 100 | } |
Douglas Gregor | 64792e0 | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 101 | |
| 102 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 103 | NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) { |
Douglas Gregor | 64792e0 | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 104 | assert(II && "Identifier cannot be NULL"); |
| 105 | NestedNameSpecifier Mockup; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 106 | Mockup.Prefix.setPointer(nullptr); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 107 | Mockup.Prefix.setInt(StoredIdentifier); |
Douglas Gregor | 64792e0 | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 108 | Mockup.Specifier = II; |
| 109 | return FindOrInsert(Context, Mockup); |
| 110 | } |
| 111 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 112 | NestedNameSpecifier * |
| 113 | NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 114 | if (!Context.GlobalNestedNameSpecifier) |
Richard Smith | beb386a | 2012-08-15 01:41:43 +0000 | [diff] [blame] | 115 | Context.GlobalNestedNameSpecifier = |
| 116 | new (Context, llvm::alignOf<NestedNameSpecifier>()) |
| 117 | NestedNameSpecifier(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 118 | return Context.GlobalNestedNameSpecifier; |
| 119 | } |
| 120 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 121 | NestedNameSpecifier * |
| 122 | NestedNameSpecifier::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 Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 131 | NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 132 | if (!Specifier) |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 133 | return Global; |
| 134 | |
| 135 | switch (Prefix.getInt()) { |
| 136 | case StoredIdentifier: |
| 137 | return Identifier; |
| 138 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 139 | 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 Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 145 | |
| 146 | case StoredTypeSpec: |
| 147 | return TypeSpec; |
| 148 | |
| 149 | case StoredTypeSpecWithTemplate: |
| 150 | return TypeSpecWithTemplate; |
| 151 | } |
| 152 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 153 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 156 | /// \brief Retrieve the namespace stored in this nested name specifier. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 157 | NamespaceDecl *NestedNameSpecifier::getAsNamespace() const { |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 158 | if (Prefix.getInt() == StoredDecl) |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 159 | return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier)); |
| 160 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 161 | return nullptr; |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 164 | /// \brief Retrieve the namespace alias stored in this nested name specifier. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 165 | NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const { |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 166 | if (Prefix.getInt() == StoredDecl) |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 167 | return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier)); |
| 168 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 169 | return nullptr; |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 172 | /// \brief Retrieve the record declaration stored in this nested name specifier. |
| 173 | CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const { |
| 174 | if (Prefix.getInt() == StoredDecl) |
| 175 | return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier)); |
| 176 | |
| 177 | return nullptr; |
| 178 | } |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 180 | /// \brief Whether this nested name specifier refers to a dependent |
| 181 | /// type or not. |
| 182 | bool NestedNameSpecifier::isDependent() const { |
| 183 | switch (getKind()) { |
| 184 | case Identifier: |
| 185 | // Identifier specifiers always represent dependent types |
| 186 | return true; |
| 187 | |
| 188 | case Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 189 | case NamespaceAlias: |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 190 | case Global: |
| 191 | return false; |
| 192 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 193 | 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 Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 202 | case TypeSpec: |
| 203 | case TypeSpecWithTemplate: |
| 204 | return getAsType()->isDependentType(); |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 205 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 206 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 207 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 210 | /// \brief Whether this nested name specifier refers to a dependent |
| 211 | /// type or not. |
| 212 | bool 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 221 | case Super: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 222 | return false; |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 224 | case TypeSpec: |
| 225 | case TypeSpecWithTemplate: |
| 226 | return getAsType()->isInstantiationDependentType(); |
| 227 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 228 | |
| 229 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 232 | bool NestedNameSpecifier::containsUnexpandedParameterPack() const { |
| 233 | switch (getKind()) { |
| 234 | case Identifier: |
| 235 | return getPrefix() && getPrefix()->containsUnexpandedParameterPack(); |
| 236 | |
| 237 | case Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 238 | case NamespaceAlias: |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 239 | case Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 240 | case Super: |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 241 | return false; |
| 242 | |
| 243 | case TypeSpec: |
| 244 | case TypeSpecWithTemplate: |
| 245 | return getAsType()->containsUnexpandedParameterPack(); |
| 246 | } |
| 247 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 248 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 251 | /// \brief Print this nested name specifier to the given output |
| 252 | /// stream. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | void |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 254 | NestedNameSpecifier::print(raw_ostream &OS, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 255 | const PrintingPolicy &Policy) const { |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 256 | if (getPrefix()) |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 257 | getPrefix()->print(OS, Policy); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 258 | |
| 259 | switch (getKind()) { |
| 260 | case Identifier: |
| 261 | OS << getAsIdentifier()->getName(); |
| 262 | break; |
| 263 | |
| 264 | case Namespace: |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 265 | if (getAsNamespace()->isAnonymousNamespace()) |
| 266 | return; |
| 267 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 268 | OS << getAsNamespace()->getName(); |
| 269 | break; |
| 270 | |
| 271 | case NamespaceAlias: |
| 272 | OS << getAsNamespaceAlias()->getName(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 273 | break; |
| 274 | |
| 275 | case Global: |
| 276 | break; |
| 277 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 278 | case Super: |
| 279 | OS << "__super"; |
| 280 | break; |
| 281 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 282 | case TypeSpecWithTemplate: |
| 283 | OS << "template "; |
| 284 | // Fall through to print the type. |
| 285 | |
| 286 | case TypeSpec: { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 287 | const Type *T = getAsType(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 289 | PrintingPolicy InnerPolicy(Policy); |
John McCall | b2e195a | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 290 | InnerPolicy.SuppressScope = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 292 | // Nested-name-specifiers are intended to contain minimally-qualified |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 293 | // types. An actual ElaboratedType will not occur, since we'll store |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 294 | // 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 Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | // dependent template-id types (e.g., Outer<T>::template Inner<U>), |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 297 | // the type requires its own nested-name-specifier for uniqueness, so we |
| 298 | // suppress that nested-name-specifier during printing. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 299 | assert(!isa<ElaboratedType>(T) && |
| 300 | "Elaborated type in nested-name-specifier"); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 301 | if (const TemplateSpecializationType *SpecType |
| 302 | = dyn_cast<TemplateSpecializationType>(T)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | // Print the template name without its corresponding |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 304 | // nested-name-specifier. |
| 305 | SpecType->getTemplateName().print(OS, InnerPolicy, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 307 | // Print the template argument list. |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 308 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 309 | OS, SpecType->getArgs(), SpecType->getNumArgs(), InnerPolicy); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 310 | } else { |
| 311 | // Print the type normally |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 312 | QualType(T, 0).print(OS, InnerPolicy); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 313 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | OS << "::"; |
| 319 | } |
| 320 | |
Yaron Keren | 015e6c8 | 2015-12-27 14:34:22 +0000 | [diff] [blame] | 321 | void NestedNameSpecifier::dump(const LangOptions &LO) const { |
| 322 | print(llvm::errs(), PrintingPolicy(LO)); |
| 323 | } |
| 324 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 325 | LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { |
Yaron Keren | 015e6c8 | 2015-12-27 14:34:22 +0000 | [diff] [blame] | 326 | LangOptions LO; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 327 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 328 | } |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 329 | |
| 330 | unsigned |
| 331 | NestedNameSpecifierLoc::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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 345 | case NestedNameSpecifier::Super: |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 346 | // 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 | |
| 361 | unsigned |
| 362 | NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) { |
| 363 | unsigned Length = 0; |
| 364 | for (; Qualifier; Qualifier = Qualifier->getPrefix()) |
| 365 | Length += getLocalDataLength(Qualifier); |
| 366 | return Length; |
| 367 | } |
| 368 | |
| 369 | namespace { |
| 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 Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 385 | } |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 386 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 387 | SourceRange NestedNameSpecifierLoc::getSourceRange() const { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 388 | if (!Qualifier) |
| 389 | return SourceRange(); |
| 390 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 391 | NestedNameSpecifierLoc First = *this; |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 392 | while (NestedNameSpecifierLoc Prefix = First.getPrefix()) |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 393 | First = Prefix; |
| 394 | |
| 395 | return SourceRange(First.getLocalSourceRange().getBegin(), |
| 396 | getLocalSourceRange().getEnd()); |
| 397 | } |
| 398 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 399 | SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 400 | if (!Qualifier) |
| 401 | return SourceRange(); |
| 402 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 403 | 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 411 | case NestedNameSpecifier::Super: |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 412 | 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 Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 425 | |
| 426 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 427 | } |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 428 | |
| 429 | TypeLoc 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 Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 439 | |
| 440 | namespace { |
| 441 | void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize, |
| 442 | unsigned &BufferCapacity) { |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 443 | if (Start == End) |
| 444 | return; |
| 445 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 446 | if (BufferSize + (End - Start) > BufferCapacity) { |
| 447 | // Reallocate the buffer. |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 448 | unsigned NewCapacity = std::max( |
| 449 | (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2), |
| 450 | (unsigned)(BufferSize + (End - Start))); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 451 | char *NewBuffer = static_cast<char *>(malloc(NewCapacity)); |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 452 | if (BufferCapacity) { |
| 453 | memcpy(NewBuffer, Buffer, BufferSize); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 454 | free(Buffer); |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 455 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 456 | 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 Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 480 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 482 | NestedNameSpecifierLocBuilder:: |
| 483 | NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 484 | : Representation(Other.Representation), Buffer(nullptr), |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 485 | 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 Pavlov | 9f81d6a | 2014-07-16 18:18:13 +0000 | [diff] [blame] | 498 | Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, |
| 499 | BufferCapacity); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | NestedNameSpecifierLocBuilder & |
| 503 | NestedNameSpecifierLocBuilder:: |
| 504 | operator=(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 Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 522 | Buffer = nullptr; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 523 | 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 Pavlov | 9f81d6a | 2014-07-16 18:18:13 +0000 | [diff] [blame] | 535 | Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, |
| 536 | BufferCapacity); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 537 | return *this; |
| 538 | } |
| 539 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 540 | void 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 | |
| 553 | void 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 | |
| 565 | void 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 | |
| 577 | void 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 | |
| 588 | void 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 597 | void 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 Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 608 | void 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 Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 616 | SmallVector<NestedNameSpecifier *, 4> Stack; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 617 | for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix()) |
| 618 | Stack.push_back(NNS); |
| 619 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 620 | NestedNameSpecifier *NNS = Stack.pop_back_val(); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 621 | 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 639 | case NestedNameSpecifier::Super: |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
| 643 | // Save the location of the '::'. |
| 644 | SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(), |
| 645 | Buffer, BufferSize, BufferCapacity); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) { |
| 650 | if (BufferCapacity) |
| 651 | free(Buffer); |
| 652 | |
| 653 | if (!Other) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 654 | Representation = nullptr; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 655 | 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 | |
| 668 | NestedNameSpecifierLoc |
| 669 | NestedNameSpecifierLocBuilder::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 | } |