Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 1 | //===- NestedNameSpecifier.cpp - C++ nested name specifiers ---------------===// |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 14 | |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 15 | #include "clang/AST/NestedNameSpecifier.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 19 | #include "clang/AST/PrettyPrinter.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 20 | #include "clang/AST/TemplateName.h" |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 23 | #include "clang/Basic/LLVM.h" |
| 24 | #include "clang/Basic/LangOptions.h" |
| 25 | #include "clang/Basic/SourceLocation.h" |
| 26 | #include "llvm/ADT/FoldingSet.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/Support/Casting.h" |
| 29 | #include "llvm/Support/Compiler.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 33 | #include <cassert> |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 34 | #include <cstdlib> |
| 35 | #include <cstring> |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 39 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 40 | NestedNameSpecifier::FindOrInsert(const ASTContext &Context, |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 41 | const NestedNameSpecifier &Mockup) { |
| 42 | llvm::FoldingSetNodeID ID; |
| 43 | Mockup.Profile(ID); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 44 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 45 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | NestedNameSpecifier *NNS |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 47 | = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos); |
| 48 | if (!NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 49 | NNS = |
| 50 | new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 51 | Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos); |
| 52 | } |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 54 | return NNS; |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 55 | } |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 56 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 57 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 58 | NestedNameSpecifier::Create(const ASTContext &Context, |
| 59 | NestedNameSpecifier *Prefix, IdentifierInfo *II) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 60 | assert(II && "Identifier cannot be NULL"); |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 61 | assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent"); |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 63 | NestedNameSpecifier Mockup; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 64 | Mockup.Prefix.setPointer(Prefix); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 65 | Mockup.Prefix.setInt(StoredIdentifier); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 66 | Mockup.Specifier = II; |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 67 | return FindOrInsert(Context, Mockup); |
| 68 | } |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 70 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 71 | NestedNameSpecifier::Create(const ASTContext &Context, |
Dmitri Gribenko | aeeca77 | 2013-01-23 17:06:56 +0000 | [diff] [blame] | 72 | NestedNameSpecifier *Prefix, |
| 73 | const NamespaceDecl *NS) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 74 | assert(NS && "Namespace cannot be NULL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | assert((!Prefix || |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 76 | (Prefix->getAsType() == nullptr && |
| 77 | Prefix->getAsIdentifier() == nullptr)) && |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 78 | "Broken nested name specifier"); |
| 79 | NestedNameSpecifier Mockup; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 80 | Mockup.Prefix.setPointer(Prefix); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 81 | Mockup.Prefix.setInt(StoredDecl); |
Dmitri Gribenko | aeeca77 | 2013-01-23 17:06:56 +0000 | [diff] [blame] | 82 | Mockup.Specifier = const_cast<NamespaceDecl *>(NS); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 83 | return FindOrInsert(Context, Mockup); |
| 84 | } |
| 85 | |
| 86 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 87 | NestedNameSpecifier::Create(const ASTContext &Context, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 88 | NestedNameSpecifier *Prefix, |
| 89 | NamespaceAliasDecl *Alias) { |
| 90 | assert(Alias && "Namespace alias cannot be NULL"); |
| 91 | assert((!Prefix || |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 92 | (Prefix->getAsType() == nullptr && |
| 93 | Prefix->getAsIdentifier() == nullptr)) && |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 94 | "Broken nested name specifier"); |
| 95 | NestedNameSpecifier Mockup; |
| 96 | Mockup.Prefix.setPointer(Prefix); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 97 | Mockup.Prefix.setInt(StoredDecl); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 98 | Mockup.Specifier = Alias; |
| 99 | return FindOrInsert(Context, Mockup); |
| 100 | } |
| 101 | |
| 102 | NestedNameSpecifier * |
| 103 | NestedNameSpecifier::Create(const ASTContext &Context, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 104 | NestedNameSpecifier *Prefix, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 105 | bool Template, const Type *T) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 106 | assert(T && "Type cannot be NULL"); |
| 107 | NestedNameSpecifier Mockup; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 108 | Mockup.Prefix.setPointer(Prefix); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 109 | Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 110 | Mockup.Specifier = const_cast<Type*>(T); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 111 | return FindOrInsert(Context, Mockup); |
| 112 | } |
Douglas Gregor | 64792e0 | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 113 | |
| 114 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 115 | NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) { |
Douglas Gregor | 64792e0 | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 116 | assert(II && "Identifier cannot be NULL"); |
| 117 | NestedNameSpecifier Mockup; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 118 | Mockup.Prefix.setPointer(nullptr); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 119 | Mockup.Prefix.setInt(StoredIdentifier); |
Douglas Gregor | 64792e0 | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 120 | Mockup.Specifier = II; |
| 121 | return FindOrInsert(Context, Mockup); |
| 122 | } |
| 123 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 124 | NestedNameSpecifier * |
| 125 | NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 126 | if (!Context.GlobalNestedNameSpecifier) |
Richard Smith | beb386a | 2012-08-15 01:41:43 +0000 | [diff] [blame] | 127 | Context.GlobalNestedNameSpecifier = |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 128 | new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 129 | return Context.GlobalNestedNameSpecifier; |
| 130 | } |
| 131 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 132 | NestedNameSpecifier * |
| 133 | NestedNameSpecifier::SuperSpecifier(const ASTContext &Context, |
| 134 | CXXRecordDecl *RD) { |
| 135 | NestedNameSpecifier Mockup; |
| 136 | Mockup.Prefix.setPointer(nullptr); |
| 137 | Mockup.Prefix.setInt(StoredDecl); |
| 138 | Mockup.Specifier = RD; |
| 139 | return FindOrInsert(Context, Mockup); |
| 140 | } |
| 141 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 142 | NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 143 | if (!Specifier) |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 144 | return Global; |
| 145 | |
| 146 | switch (Prefix.getInt()) { |
| 147 | case StoredIdentifier: |
| 148 | return Identifier; |
| 149 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 150 | case StoredDecl: { |
| 151 | NamedDecl *ND = static_cast<NamedDecl *>(Specifier); |
| 152 | if (isa<CXXRecordDecl>(ND)) |
| 153 | return Super; |
| 154 | return isa<NamespaceDecl>(ND) ? Namespace : NamespaceAlias; |
| 155 | } |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 156 | |
| 157 | case StoredTypeSpec: |
| 158 | return TypeSpec; |
| 159 | |
| 160 | case StoredTypeSpecWithTemplate: |
| 161 | return TypeSpecWithTemplate; |
| 162 | } |
| 163 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 164 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 167 | /// \brief Retrieve the namespace stored in this nested name specifier. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 168 | NamespaceDecl *NestedNameSpecifier::getAsNamespace() const { |
Yaron Keren | e0bcdd4 | 2016-10-08 06:45:10 +0000 | [diff] [blame] | 169 | if (Prefix.getInt() == StoredDecl) |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 170 | return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier)); |
| 171 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 172 | return nullptr; |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 175 | /// \brief Retrieve the namespace alias stored in this nested name specifier. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 176 | NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const { |
Yaron Keren | e0bcdd4 | 2016-10-08 06:45:10 +0000 | [diff] [blame] | 177 | if (Prefix.getInt() == StoredDecl) |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 178 | return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier)); |
| 179 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 180 | return nullptr; |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 183 | /// \brief Retrieve the record declaration stored in this nested name specifier. |
| 184 | CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 185 | switch (Prefix.getInt()) { |
| 186 | case StoredIdentifier: |
| 187 | return nullptr; |
| 188 | |
| 189 | case StoredDecl: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 190 | return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier)); |
| 191 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 192 | case StoredTypeSpec: |
| 193 | case StoredTypeSpecWithTemplate: |
| 194 | return getAsType()->getAsCXXRecordDecl(); |
| 195 | } |
| 196 | |
| 197 | llvm_unreachable("Invalid NNS Kind!"); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 198 | } |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 199 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 200 | /// \brief Whether this nested name specifier refers to a dependent |
| 201 | /// type or not. |
| 202 | bool NestedNameSpecifier::isDependent() const { |
| 203 | switch (getKind()) { |
| 204 | case Identifier: |
| 205 | // Identifier specifiers always represent dependent types |
| 206 | return true; |
| 207 | |
| 208 | case Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 209 | case NamespaceAlias: |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 210 | case Global: |
| 211 | return false; |
| 212 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 213 | case Super: { |
| 214 | CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier); |
| 215 | for (const auto &Base : RD->bases()) |
| 216 | if (Base.getType()->isDependentType()) |
| 217 | return true; |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 222 | case TypeSpec: |
| 223 | case TypeSpecWithTemplate: |
| 224 | return getAsType()->isDependentType(); |
Douglas Gregor | 1835391 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 225 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 226 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 227 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 230 | /// \brief Whether this nested name specifier refers to a dependent |
| 231 | /// type or not. |
| 232 | bool NestedNameSpecifier::isInstantiationDependent() const { |
| 233 | switch (getKind()) { |
| 234 | case Identifier: |
| 235 | // Identifier specifiers always represent dependent types |
| 236 | return true; |
| 237 | |
| 238 | case Namespace: |
| 239 | case NamespaceAlias: |
| 240 | case Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 241 | case Super: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 242 | return false; |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 243 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 244 | case TypeSpec: |
| 245 | case TypeSpecWithTemplate: |
| 246 | return getAsType()->isInstantiationDependentType(); |
| 247 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 248 | |
| 249 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 252 | bool NestedNameSpecifier::containsUnexpandedParameterPack() const { |
| 253 | switch (getKind()) { |
| 254 | case Identifier: |
| 255 | return getPrefix() && getPrefix()->containsUnexpandedParameterPack(); |
| 256 | |
| 257 | case Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 258 | case NamespaceAlias: |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 259 | case Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 260 | case Super: |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 261 | return false; |
| 262 | |
| 263 | case TypeSpec: |
| 264 | case TypeSpecWithTemplate: |
| 265 | return getAsType()->containsUnexpandedParameterPack(); |
| 266 | } |
| 267 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 268 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 271 | /// \brief Print this nested name specifier to the given output |
| 272 | /// stream. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | void |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 274 | NestedNameSpecifier::print(raw_ostream &OS, |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 275 | const PrintingPolicy &Policy) const { |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 276 | if (getPrefix()) |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 277 | getPrefix()->print(OS, Policy); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 278 | |
| 279 | switch (getKind()) { |
| 280 | case Identifier: |
| 281 | OS << getAsIdentifier()->getName(); |
| 282 | break; |
| 283 | |
| 284 | case Namespace: |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 285 | if (getAsNamespace()->isAnonymousNamespace()) |
| 286 | return; |
| 287 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 288 | OS << getAsNamespace()->getName(); |
| 289 | break; |
| 290 | |
| 291 | case NamespaceAlias: |
| 292 | OS << getAsNamespaceAlias()->getName(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case Global: |
| 296 | break; |
| 297 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 298 | case Super: |
| 299 | OS << "__super"; |
| 300 | break; |
| 301 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 302 | case TypeSpecWithTemplate: |
| 303 | OS << "template "; |
| 304 | // Fall through to print the type. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 305 | LLVM_FALLTHROUGH; |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 306 | |
| 307 | case TypeSpec: { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 308 | const Type *T = getAsType(); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 309 | |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 310 | PrintingPolicy InnerPolicy(Policy); |
John McCall | b2e195a | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 311 | InnerPolicy.SuppressScope = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 313 | // Nested-name-specifiers are intended to contain minimally-qualified |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 314 | // types. An actual ElaboratedType will not occur, since we'll store |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 315 | // just the type that is referred to in the nested-name-specifier (e.g., |
| 316 | // a TypedefType, TagType, etc.). However, when we are dealing with |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | // dependent template-id types (e.g., Outer<T>::template Inner<U>), |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 318 | // the type requires its own nested-name-specifier for uniqueness, so we |
| 319 | // suppress that nested-name-specifier during printing. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 320 | assert(!isa<ElaboratedType>(T) && |
| 321 | "Elaborated type in nested-name-specifier"); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 322 | if (const TemplateSpecializationType *SpecType |
| 323 | = dyn_cast<TemplateSpecializationType>(T)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | // Print the template name without its corresponding |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 325 | // nested-name-specifier. |
| 326 | SpecType->getTemplateName().print(OS, InnerPolicy, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 328 | // Print the template argument list. |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 329 | TemplateSpecializationType::PrintTemplateArgumentList( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 330 | OS, SpecType->template_arguments(), InnerPolicy); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 331 | } else { |
| 332 | // Print the type normally |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 333 | QualType(T, 0).print(OS, InnerPolicy); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 334 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | OS << "::"; |
| 340 | } |
| 341 | |
Yaron Keren | 015e6c8 | 2015-12-27 14:34:22 +0000 | [diff] [blame] | 342 | void NestedNameSpecifier::dump(const LangOptions &LO) const { |
| 343 | print(llvm::errs(), PrintingPolicy(LO)); |
| 344 | } |
| 345 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 346 | LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { |
Yaron Keren | 015e6c8 | 2015-12-27 14:34:22 +0000 | [diff] [blame] | 347 | LangOptions LO; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 348 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 349 | } |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 350 | |
| 351 | unsigned |
| 352 | NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) { |
| 353 | assert(Qualifier && "Expected a non-NULL qualifier"); |
| 354 | |
| 355 | // Location of the trailing '::'. |
| 356 | unsigned Length = sizeof(unsigned); |
| 357 | |
| 358 | switch (Qualifier->getKind()) { |
| 359 | case NestedNameSpecifier::Global: |
| 360 | // Nothing more to add. |
| 361 | break; |
| 362 | |
| 363 | case NestedNameSpecifier::Identifier: |
| 364 | case NestedNameSpecifier::Namespace: |
| 365 | case NestedNameSpecifier::NamespaceAlias: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 366 | case NestedNameSpecifier::Super: |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 367 | // The location of the identifier or namespace name. |
| 368 | Length += sizeof(unsigned); |
| 369 | break; |
| 370 | |
| 371 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 372 | case NestedNameSpecifier::TypeSpec: |
| 373 | // The "void*" that points at the TypeLoc data. |
| 374 | // Note: the 'template' keyword is part of the TypeLoc. |
| 375 | Length += sizeof(void *); |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | return Length; |
| 380 | } |
| 381 | |
| 382 | unsigned |
| 383 | NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) { |
| 384 | unsigned Length = 0; |
| 385 | for (; Qualifier; Qualifier = Qualifier->getPrefix()) |
| 386 | Length += getLocalDataLength(Qualifier); |
| 387 | return Length; |
| 388 | } |
| 389 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 390 | /// \brief Load a (possibly unaligned) source location from a given address |
| 391 | /// and offset. |
| 392 | static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) { |
| 393 | unsigned Raw; |
| 394 | memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned)); |
| 395 | return SourceLocation::getFromRawEncoding(Raw); |
| 396 | } |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 397 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 398 | /// \brief Load a (possibly unaligned) pointer from a given address and |
| 399 | /// offset. |
| 400 | static void *LoadPointer(void *Data, unsigned Offset) { |
| 401 | void *Result; |
| 402 | memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*)); |
| 403 | return Result; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 404 | } |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 406 | SourceRange NestedNameSpecifierLoc::getSourceRange() const { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 407 | if (!Qualifier) |
| 408 | return SourceRange(); |
| 409 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 410 | NestedNameSpecifierLoc First = *this; |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 411 | while (NestedNameSpecifierLoc Prefix = First.getPrefix()) |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 412 | First = Prefix; |
| 413 | |
| 414 | return SourceRange(First.getLocalSourceRange().getBegin(), |
| 415 | getLocalSourceRange().getEnd()); |
| 416 | } |
| 417 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 418 | SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 419 | if (!Qualifier) |
| 420 | return SourceRange(); |
| 421 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 422 | unsigned Offset = getDataLength(Qualifier->getPrefix()); |
| 423 | switch (Qualifier->getKind()) { |
| 424 | case NestedNameSpecifier::Global: |
| 425 | return LoadSourceLocation(Data, Offset); |
| 426 | |
| 427 | case NestedNameSpecifier::Identifier: |
| 428 | case NestedNameSpecifier::Namespace: |
| 429 | case NestedNameSpecifier::NamespaceAlias: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 430 | case NestedNameSpecifier::Super: |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 431 | return SourceRange(LoadSourceLocation(Data, Offset), |
| 432 | LoadSourceLocation(Data, Offset + sizeof(unsigned))); |
| 433 | |
| 434 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 435 | case NestedNameSpecifier::TypeSpec: { |
| 436 | // The "void*" that points at the TypeLoc data. |
| 437 | // Note: the 'template' keyword is part of the TypeLoc. |
| 438 | void *TypeData = LoadPointer(Data, Offset); |
| 439 | TypeLoc TL(Qualifier->getAsType(), TypeData); |
| 440 | return SourceRange(TL.getBeginLoc(), |
| 441 | LoadSourceLocation(Data, Offset + sizeof(void*))); |
| 442 | } |
| 443 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 444 | |
| 445 | llvm_unreachable("Invalid NNS Kind!"); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 446 | } |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 447 | |
| 448 | TypeLoc NestedNameSpecifierLoc::getTypeLoc() const { |
| 449 | assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec || |
| 450 | Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) && |
| 451 | "Nested-name-specifier location is not a type"); |
| 452 | |
| 453 | // The "void*" that points at the TypeLoc data. |
| 454 | unsigned Offset = getDataLength(Qualifier->getPrefix()); |
| 455 | void *TypeData = LoadPointer(Data, Offset); |
| 456 | return TypeLoc(Qualifier->getAsType(), TypeData); |
| 457 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 458 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 459 | static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize, |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 460 | unsigned &BufferCapacity) { |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 461 | if (Start == End) |
| 462 | return; |
Chandler Carruth | b6708d8 | 2015-08-04 03:52:56 +0000 | [diff] [blame] | 463 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 464 | if (BufferSize + (End - Start) > BufferCapacity) { |
| 465 | // Reallocate the buffer. |
| 466 | unsigned NewCapacity = std::max( |
| 467 | (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2), |
| 468 | (unsigned)(BufferSize + (End - Start))); |
| 469 | char *NewBuffer = static_cast<char *>(malloc(NewCapacity)); |
| 470 | if (BufferCapacity) { |
| 471 | memcpy(NewBuffer, Buffer, BufferSize); |
| 472 | free(Buffer); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 473 | } |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 474 | Buffer = NewBuffer; |
| 475 | BufferCapacity = NewCapacity; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 476 | } |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 477 | |
| 478 | memcpy(Buffer + BufferSize, Start, End - Start); |
| 479 | BufferSize += End-Start; |
| 480 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 481 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 482 | /// \brief Save a source location to the given buffer. |
| 483 | static void SaveSourceLocation(SourceLocation Loc, char *&Buffer, |
| 484 | unsigned &BufferSize, unsigned &BufferCapacity) { |
| 485 | unsigned Raw = Loc.getRawEncoding(); |
| 486 | Append(reinterpret_cast<char *>(&Raw), |
| 487 | reinterpret_cast<char *>(&Raw) + sizeof(unsigned), |
| 488 | Buffer, BufferSize, BufferCapacity); |
| 489 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 490 | |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 491 | /// \brief Save a pointer to the given buffer. |
| 492 | static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize, |
| 493 | unsigned &BufferCapacity) { |
| 494 | Append(reinterpret_cast<char *>(&Ptr), |
| 495 | reinterpret_cast<char *>(&Ptr) + sizeof(void *), |
| 496 | Buffer, BufferSize, BufferCapacity); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 497 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 499 | NestedNameSpecifierLocBuilder:: |
| 500 | NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other) |
Eugene Zelenko | 21fadad | 2017-11-21 23:26:08 +0000 | [diff] [blame] | 501 | : Representation(Other.Representation) { |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 502 | if (!Other.Buffer) |
| 503 | return; |
| 504 | |
| 505 | if (Other.BufferCapacity == 0) { |
| 506 | // Shallow copy is okay. |
| 507 | Buffer = Other.Buffer; |
| 508 | BufferSize = Other.BufferSize; |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | // Deep copy |
Serge Pavlov | 9f81d6a | 2014-07-16 18:18:13 +0000 | [diff] [blame] | 513 | Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, |
| 514 | BufferCapacity); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | NestedNameSpecifierLocBuilder & |
| 518 | NestedNameSpecifierLocBuilder:: |
| 519 | operator=(const NestedNameSpecifierLocBuilder &Other) { |
| 520 | Representation = Other.Representation; |
| 521 | |
| 522 | if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) { |
| 523 | // Re-use our storage. |
| 524 | BufferSize = Other.BufferSize; |
| 525 | memcpy(Buffer, Other.Buffer, BufferSize); |
| 526 | return *this; |
| 527 | } |
| 528 | |
| 529 | // Free our storage, if we have any. |
| 530 | if (BufferCapacity) { |
| 531 | free(Buffer); |
| 532 | BufferCapacity = 0; |
| 533 | } |
| 534 | |
| 535 | if (!Other.Buffer) { |
| 536 | // Empty. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 537 | Buffer = nullptr; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 538 | BufferSize = 0; |
| 539 | return *this; |
| 540 | } |
| 541 | |
| 542 | if (Other.BufferCapacity == 0) { |
| 543 | // Shallow copy is okay. |
| 544 | Buffer = Other.Buffer; |
| 545 | BufferSize = Other.BufferSize; |
| 546 | return *this; |
| 547 | } |
| 548 | |
| 549 | // Deep copy. |
Serge Pavlov | 9f81d6a | 2014-07-16 18:18:13 +0000 | [diff] [blame] | 550 | Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, |
| 551 | BufferCapacity); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 552 | return *this; |
| 553 | } |
| 554 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 555 | void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, |
| 556 | SourceLocation TemplateKWLoc, |
| 557 | TypeLoc TL, |
| 558 | SourceLocation ColonColonLoc) { |
| 559 | Representation = NestedNameSpecifier::Create(Context, Representation, |
| 560 | TemplateKWLoc.isValid(), |
| 561 | TL.getTypePtr()); |
| 562 | |
| 563 | // Push source-location info into the buffer. |
| 564 | SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity); |
| 565 | SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); |
| 566 | } |
| 567 | |
| 568 | void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, |
| 569 | IdentifierInfo *Identifier, |
| 570 | SourceLocation IdentifierLoc, |
| 571 | SourceLocation ColonColonLoc) { |
| 572 | Representation = NestedNameSpecifier::Create(Context, Representation, |
| 573 | Identifier); |
| 574 | |
| 575 | // Push source-location info into the buffer. |
| 576 | SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity); |
| 577 | SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); |
| 578 | } |
| 579 | |
| 580 | void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, |
| 581 | NamespaceDecl *Namespace, |
| 582 | SourceLocation NamespaceLoc, |
| 583 | SourceLocation ColonColonLoc) { |
| 584 | Representation = NestedNameSpecifier::Create(Context, Representation, |
| 585 | Namespace); |
| 586 | |
| 587 | // Push source-location info into the buffer. |
| 588 | SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity); |
| 589 | SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); |
| 590 | } |
| 591 | |
| 592 | void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, |
| 593 | NamespaceAliasDecl *Alias, |
| 594 | SourceLocation AliasLoc, |
| 595 | SourceLocation ColonColonLoc) { |
| 596 | Representation = NestedNameSpecifier::Create(Context, Representation, Alias); |
| 597 | |
| 598 | // Push source-location info into the buffer. |
| 599 | SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity); |
| 600 | SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); |
| 601 | } |
| 602 | |
| 603 | void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context, |
| 604 | SourceLocation ColonColonLoc) { |
| 605 | assert(!Representation && "Already have a nested-name-specifier!?"); |
| 606 | Representation = NestedNameSpecifier::GlobalSpecifier(Context); |
| 607 | |
| 608 | // Push source-location info into the buffer. |
| 609 | SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); |
| 610 | } |
| 611 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 612 | void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context, |
| 613 | CXXRecordDecl *RD, |
| 614 | SourceLocation SuperLoc, |
| 615 | SourceLocation ColonColonLoc) { |
| 616 | Representation = NestedNameSpecifier::SuperSpecifier(Context, RD); |
| 617 | |
| 618 | // Push source-location info into the buffer. |
| 619 | SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity); |
| 620 | SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); |
| 621 | } |
| 622 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 623 | void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context, |
| 624 | NestedNameSpecifier *Qualifier, |
| 625 | SourceRange R) { |
| 626 | Representation = Qualifier; |
| 627 | |
| 628 | // Construct bogus (but well-formed) source information for the |
| 629 | // nested-name-specifier. |
| 630 | BufferSize = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 631 | SmallVector<NestedNameSpecifier *, 4> Stack; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 632 | for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix()) |
| 633 | Stack.push_back(NNS); |
| 634 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 635 | NestedNameSpecifier *NNS = Stack.pop_back_val(); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 636 | switch (NNS->getKind()) { |
| 637 | case NestedNameSpecifier::Identifier: |
| 638 | case NestedNameSpecifier::Namespace: |
| 639 | case NestedNameSpecifier::NamespaceAlias: |
| 640 | SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity); |
| 641 | break; |
| 642 | |
| 643 | case NestedNameSpecifier::TypeSpec: |
| 644 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 645 | TypeSourceInfo *TSInfo |
| 646 | = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0), |
| 647 | R.getBegin()); |
| 648 | SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize, |
| 649 | BufferCapacity); |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 654 | case NestedNameSpecifier::Super: |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | |
| 658 | // Save the location of the '::'. |
| 659 | SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(), |
| 660 | Buffer, BufferSize, BufferCapacity); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) { |
| 665 | if (BufferCapacity) |
| 666 | free(Buffer); |
| 667 | |
| 668 | if (!Other) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 669 | Representation = nullptr; |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 670 | BufferSize = 0; |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | // Rather than copying the data (which is wasteful), "adopt" the |
| 675 | // pointer (which points into the ASTContext) but set the capacity to zero to |
| 676 | // indicate that we don't own it. |
| 677 | Representation = Other.getNestedNameSpecifier(); |
| 678 | Buffer = static_cast<char *>(Other.getOpaqueData()); |
| 679 | BufferSize = Other.getDataLength(); |
| 680 | BufferCapacity = 0; |
| 681 | } |
| 682 | |
| 683 | NestedNameSpecifierLoc |
| 684 | NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const { |
| 685 | if (!Representation) |
| 686 | return NestedNameSpecifierLoc(); |
| 687 | |
| 688 | // If we adopted our data pointer from elsewhere in the AST context, there's |
| 689 | // no need to copy the memory. |
| 690 | if (BufferCapacity == 0) |
| 691 | return NestedNameSpecifierLoc(Representation, Buffer); |
| 692 | |
| 693 | // FIXME: After copying the source-location information, should we free |
| 694 | // our (temporary) buffer and adopt the ASTContext-allocated memory? |
| 695 | // Doing so would optimize repeated calls to getWithLocInContext(). |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 696 | void *Mem = Context.Allocate(BufferSize, alignof(void *)); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 697 | memcpy(Mem, Buffer, BufferSize); |
| 698 | return NestedNameSpecifierLoc(Representation, Mem); |
| 699 | } |