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