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 { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame^] | 174 | switch (Prefix.getInt()) { |
| 175 | case StoredIdentifier: |
| 176 | return nullptr; |
| 177 | |
| 178 | case StoredDecl: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 179 | return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier)); |
| 180 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame^] | 181 | case StoredTypeSpec: |
| 182 | case StoredTypeSpecWithTemplate: |
| 183 | return getAsType()->getAsCXXRecordDecl(); |
| 184 | } |
| 185 | |
| 186 | llvm_unreachable("Invalid NNS Kind!"); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 187 | } |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 189 | /// \brief Whether this nested name specifier refers to a dependent |
| 190 | /// type or not. |
| 191 | bool NestedNameSpecifier::isDependent() const { |
| 192 | switch (getKind()) { |
| 193 | case Identifier: |
| 194 | // Identifier specifiers always represent dependent types |
| 195 | return true; |
| 196 | |
| 197 | case Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 198 | case NamespaceAlias: |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 199 | case Global: |
| 200 | return false; |
| 201 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 202 | 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 Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 211 | case TypeSpec: |
| 212 | case TypeSpecWithTemplate: |
| 213 | return getAsType()->isDependentType(); |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 214 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 215 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 216 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 219 | /// \brief Whether this nested name specifier refers to a dependent |
| 220 | /// type or not. |
| 221 | bool 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 230 | case Super: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 231 | return false; |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 232 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 233 | case TypeSpec: |
| 234 | case TypeSpecWithTemplate: |
| 235 | return getAsType()->isInstantiationDependentType(); |
| 236 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 237 | |
| 238 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 241 | bool NestedNameSpecifier::containsUnexpandedParameterPack() const { |
| 242 | switch (getKind()) { |
| 243 | case Identifier: |
| 244 | return getPrefix() && getPrefix()->containsUnexpandedParameterPack(); |
| 245 | |
| 246 | case Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 247 | case NamespaceAlias: |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 248 | case Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 249 | case Super: |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 250 | return false; |
| 251 | |
| 252 | case TypeSpec: |
| 253 | case TypeSpecWithTemplate: |
| 254 | return getAsType()->containsUnexpandedParameterPack(); |
| 255 | } |
| 256 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 257 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 260 | /// \brief Print this nested name specifier to the given output |
| 261 | /// stream. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | void |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 263 | NestedNameSpecifier::print(raw_ostream &OS, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 264 | const PrintingPolicy &Policy) const { |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 265 | if (getPrefix()) |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 266 | getPrefix()->print(OS, Policy); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 267 | |
| 268 | switch (getKind()) { |
| 269 | case Identifier: |
| 270 | OS << getAsIdentifier()->getName(); |
| 271 | break; |
| 272 | |
| 273 | case Namespace: |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 274 | if (getAsNamespace()->isAnonymousNamespace()) |
| 275 | return; |
| 276 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 277 | OS << getAsNamespace()->getName(); |
| 278 | break; |
| 279 | |
| 280 | case NamespaceAlias: |
| 281 | OS << getAsNamespaceAlias()->getName(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 282 | break; |
| 283 | |
| 284 | case Global: |
| 285 | break; |
| 286 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 287 | case Super: |
| 288 | OS << "__super"; |
| 289 | break; |
| 290 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 291 | case TypeSpecWithTemplate: |
| 292 | OS << "template "; |
| 293 | // Fall through to print the type. |
| 294 | |
| 295 | case TypeSpec: { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 296 | const Type *T = getAsType(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 297 | |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 298 | PrintingPolicy InnerPolicy(Policy); |
John McCall | b2e195a | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 299 | InnerPolicy.SuppressScope = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 301 | // Nested-name-specifiers are intended to contain minimally-qualified |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 302 | // types. An actual ElaboratedType will not occur, since we'll store |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 303 | // 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 Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | // dependent template-id types (e.g., Outer<T>::template Inner<U>), |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 306 | // the type requires its own nested-name-specifier for uniqueness, so we |
| 307 | // suppress that nested-name-specifier during printing. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 308 | assert(!isa<ElaboratedType>(T) && |
| 309 | "Elaborated type in nested-name-specifier"); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 310 | if (const TemplateSpecializationType *SpecType |
| 311 | = dyn_cast<TemplateSpecializationType>(T)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | // Print the template name without its corresponding |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 313 | // nested-name-specifier. |
| 314 | SpecType->getTemplateName().print(OS, InnerPolicy, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 316 | // Print the template argument list. |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 317 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 318 | OS, SpecType->getArgs(), SpecType->getNumArgs(), InnerPolicy); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 319 | } else { |
| 320 | // Print the type normally |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 321 | QualType(T, 0).print(OS, InnerPolicy); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 322 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | OS << "::"; |
| 328 | } |
| 329 | |
Yaron Keren | 015e6c8 | 2015-12-27 14:34:22 +0000 | [diff] [blame] | 330 | void NestedNameSpecifier::dump(const LangOptions &LO) const { |
| 331 | print(llvm::errs(), PrintingPolicy(LO)); |
| 332 | } |
| 333 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 334 | LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { |
Yaron Keren | 015e6c8 | 2015-12-27 14:34:22 +0000 | [diff] [blame] | 335 | LangOptions LO; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 336 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 337 | } |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 338 | |
| 339 | unsigned |
| 340 | NestedNameSpecifierLoc::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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 354 | case NestedNameSpecifier::Super: |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 355 | // 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 | |
| 370 | unsigned |
| 371 | NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) { |
| 372 | unsigned Length = 0; |
| 373 | for (; Qualifier; Qualifier = Qualifier->getPrefix()) |
| 374 | Length += getLocalDataLength(Qualifier); |
| 375 | return Length; |
| 376 | } |
| 377 | |
| 378 | namespace { |
| 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 Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 394 | } |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 395 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 396 | SourceRange NestedNameSpecifierLoc::getSourceRange() const { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 397 | if (!Qualifier) |
| 398 | return SourceRange(); |
| 399 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 400 | NestedNameSpecifierLoc First = *this; |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 401 | while (NestedNameSpecifierLoc Prefix = First.getPrefix()) |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 402 | First = Prefix; |
| 403 | |
| 404 | return SourceRange(First.getLocalSourceRange().getBegin(), |
| 405 | getLocalSourceRange().getEnd()); |
| 406 | } |
| 407 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 408 | SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 409 | if (!Qualifier) |
| 410 | return SourceRange(); |
| 411 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 412 | 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 420 | case NestedNameSpecifier::Super: |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 421 | 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 Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 434 | |
| 435 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 436 | } |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 437 | |
| 438 | TypeLoc 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 Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 448 | |
| 449 | namespace { |
| 450 | void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize, |
| 451 | unsigned &BufferCapacity) { |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 452 | if (Start == End) |
| 453 | return; |
| 454 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 455 | if (BufferSize + (End - Start) > BufferCapacity) { |
| 456 | // Reallocate the buffer. |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 457 | unsigned NewCapacity = std::max( |
| 458 | (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2), |
| 459 | (unsigned)(BufferSize + (End - Start))); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 460 | char *NewBuffer = static_cast<char *>(malloc(NewCapacity)); |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 461 | if (BufferCapacity) { |
| 462 | memcpy(NewBuffer, Buffer, BufferSize); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 463 | free(Buffer); |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 464 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 465 | 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 Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 489 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 490 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 491 | NestedNameSpecifierLocBuilder:: |
| 492 | NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 493 | : Representation(Other.Representation), Buffer(nullptr), |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 494 | 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 Pavlov | 9f81d6a | 2014-07-16 18:18:13 +0000 | [diff] [blame] | 507 | Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, |
| 508 | BufferCapacity); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | NestedNameSpecifierLocBuilder & |
| 512 | NestedNameSpecifierLocBuilder:: |
| 513 | operator=(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 Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 531 | Buffer = nullptr; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 532 | 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 Pavlov | 9f81d6a | 2014-07-16 18:18:13 +0000 | [diff] [blame] | 544 | Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, |
| 545 | BufferCapacity); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 546 | return *this; |
| 547 | } |
| 548 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 549 | void 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 | |
| 562 | void 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 | |
| 574 | void 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 | |
| 586 | void 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 | |
| 597 | void 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 606 | void 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 Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 617 | void 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 Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 625 | SmallVector<NestedNameSpecifier *, 4> Stack; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 626 | for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix()) |
| 627 | Stack.push_back(NNS); |
| 628 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 629 | NestedNameSpecifier *NNS = Stack.pop_back_val(); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 630 | 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 Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 648 | case NestedNameSpecifier::Super: |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | |
| 652 | // Save the location of the '::'. |
| 653 | SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(), |
| 654 | Buffer, BufferSize, BufferCapacity); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) { |
| 659 | if (BufferCapacity) |
| 660 | free(Buffer); |
| 661 | |
| 662 | if (!Other) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 663 | Representation = nullptr; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 664 | 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 | |
| 677 | NestedNameSpecifierLoc |
| 678 | NestedNameSpecifierLocBuilder::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 | } |