Douglas Gregor | e4e5b05 | 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 | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/Type.h" |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 21 | #include <cassert> |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 22 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 25 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 26 | NestedNameSpecifier::FindOrInsert(const ASTContext &Context, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 27 | const NestedNameSpecifier &Mockup) { |
| 28 | llvm::FoldingSetNodeID ID; |
| 29 | Mockup.Profile(ID); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 31 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | NestedNameSpecifier *NNS |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 33 | = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos); |
| 34 | if (!NNS) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 35 | NNS = new (Context, 4) NestedNameSpecifier(Mockup); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 36 | Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos); |
| 37 | } |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 39 | return NNS; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 40 | } |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 42 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 43 | NestedNameSpecifier::Create(const ASTContext &Context, |
| 44 | NestedNameSpecifier *Prefix, IdentifierInfo *II) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 45 | assert(II && "Identifier cannot be NULL"); |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 46 | assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent"); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 48 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 49 | Mockup.Prefix.setPointer(Prefix); |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 50 | Mockup.Prefix.setInt(StoredIdentifier); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 51 | Mockup.Specifier = II; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 52 | return FindOrInsert(Context, Mockup); |
| 53 | } |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 55 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 56 | NestedNameSpecifier::Create(const ASTContext &Context, |
| 57 | NestedNameSpecifier *Prefix, NamespaceDecl *NS) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 58 | assert(NS && "Namespace cannot be NULL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | assert((!Prefix || |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 60 | (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) && |
| 61 | "Broken nested name specifier"); |
| 62 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 63 | Mockup.Prefix.setPointer(Prefix); |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 64 | Mockup.Prefix.setInt(StoredNamespaceOrAlias); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 65 | Mockup.Specifier = NS; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 66 | return FindOrInsert(Context, Mockup); |
| 67 | } |
| 68 | |
| 69 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 70 | NestedNameSpecifier::Create(const ASTContext &Context, |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 71 | NestedNameSpecifier *Prefix, |
| 72 | NamespaceAliasDecl *Alias) { |
| 73 | assert(Alias && "Namespace alias cannot be NULL"); |
| 74 | assert((!Prefix || |
| 75 | (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) && |
| 76 | "Broken nested name specifier"); |
| 77 | NestedNameSpecifier Mockup; |
| 78 | Mockup.Prefix.setPointer(Prefix); |
| 79 | Mockup.Prefix.setInt(StoredNamespaceOrAlias); |
| 80 | Mockup.Specifier = Alias; |
| 81 | return FindOrInsert(Context, Mockup); |
| 82 | } |
| 83 | |
| 84 | NestedNameSpecifier * |
| 85 | NestedNameSpecifier::Create(const ASTContext &Context, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 86 | NestedNameSpecifier *Prefix, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 87 | bool Template, const Type *T) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 88 | assert(T && "Type cannot be NULL"); |
| 89 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 90 | Mockup.Prefix.setPointer(Prefix); |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 91 | Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 92 | Mockup.Specifier = const_cast<Type*>(T); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 93 | return FindOrInsert(Context, Mockup); |
| 94 | } |
Douglas Gregor | 2700dcd | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 95 | |
| 96 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 97 | NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) { |
Douglas Gregor | 2700dcd | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 98 | assert(II && "Identifier cannot be NULL"); |
| 99 | NestedNameSpecifier Mockup; |
| 100 | Mockup.Prefix.setPointer(0); |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 101 | Mockup.Prefix.setInt(StoredIdentifier); |
Douglas Gregor | 2700dcd | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 102 | Mockup.Specifier = II; |
| 103 | return FindOrInsert(Context, Mockup); |
| 104 | } |
| 105 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 106 | NestedNameSpecifier * |
| 107 | NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 108 | if (!Context.GlobalNestedNameSpecifier) |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 109 | Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier(); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 110 | return Context.GlobalNestedNameSpecifier; |
| 111 | } |
| 112 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 113 | NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const { |
| 114 | if (Specifier == 0) |
| 115 | return Global; |
| 116 | |
| 117 | switch (Prefix.getInt()) { |
| 118 | case StoredIdentifier: |
| 119 | return Identifier; |
| 120 | |
| 121 | case StoredNamespaceOrAlias: |
| 122 | return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace |
| 123 | : NamespaceAlias; |
| 124 | |
| 125 | case StoredTypeSpec: |
| 126 | return TypeSpec; |
| 127 | |
| 128 | case StoredTypeSpecWithTemplate: |
| 129 | return TypeSpecWithTemplate; |
| 130 | } |
| 131 | |
| 132 | return Global; |
| 133 | } |
| 134 | |
| 135 | /// \brief Retrieve the namespace stored in this nested name |
| 136 | /// specifier. |
| 137 | NamespaceDecl *NestedNameSpecifier::getAsNamespace() const { |
| 138 | if (Prefix.getInt() == StoredNamespaceOrAlias) |
| 139 | return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier)); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /// \brief Retrieve the namespace alias stored in this nested name |
| 145 | /// specifier. |
| 146 | NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const { |
| 147 | if (Prefix.getInt() == StoredNamespaceOrAlias) |
| 148 | return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier)); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 154 | /// \brief Whether this nested name specifier refers to a dependent |
| 155 | /// type or not. |
| 156 | bool NestedNameSpecifier::isDependent() const { |
| 157 | switch (getKind()) { |
| 158 | case Identifier: |
| 159 | // Identifier specifiers always represent dependent types |
| 160 | return true; |
| 161 | |
| 162 | case Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 163 | case NamespaceAlias: |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 164 | case Global: |
| 165 | return false; |
| 166 | |
| 167 | case TypeSpec: |
| 168 | case TypeSpecWithTemplate: |
| 169 | return getAsType()->isDependentType(); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 170 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 171 | |
| 172 | // Necessary to suppress a GCC warning. |
| 173 | return false; |
| 174 | } |
| 175 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 176 | bool NestedNameSpecifier::containsUnexpandedParameterPack() const { |
| 177 | switch (getKind()) { |
| 178 | case Identifier: |
| 179 | return getPrefix() && getPrefix()->containsUnexpandedParameterPack(); |
| 180 | |
| 181 | case Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 182 | case NamespaceAlias: |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 183 | case Global: |
| 184 | return false; |
| 185 | |
| 186 | case TypeSpec: |
| 187 | case TypeSpecWithTemplate: |
| 188 | return getAsType()->containsUnexpandedParameterPack(); |
| 189 | } |
| 190 | |
| 191 | // Necessary to suppress a GCC warning. |
| 192 | return false; |
| 193 | } |
| 194 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 195 | /// \brief Print this nested name specifier to the given output |
| 196 | /// stream. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | void |
| 198 | NestedNameSpecifier::print(llvm::raw_ostream &OS, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 199 | const PrintingPolicy &Policy) const { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 200 | if (getPrefix()) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 201 | getPrefix()->print(OS, Policy); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 202 | |
| 203 | switch (getKind()) { |
| 204 | case Identifier: |
| 205 | OS << getAsIdentifier()->getName(); |
| 206 | break; |
| 207 | |
| 208 | case Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame^] | 209 | OS << getAsNamespace()->getName(); |
| 210 | break; |
| 211 | |
| 212 | case NamespaceAlias: |
| 213 | OS << getAsNamespaceAlias()->getName(); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 214 | break; |
| 215 | |
| 216 | case Global: |
| 217 | break; |
| 218 | |
| 219 | case TypeSpecWithTemplate: |
| 220 | OS << "template "; |
| 221 | // Fall through to print the type. |
| 222 | |
| 223 | case TypeSpec: { |
| 224 | std::string TypeStr; |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 225 | const Type *T = getAsType(); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 227 | PrintingPolicy InnerPolicy(Policy); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 228 | InnerPolicy.SuppressScope = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 230 | // Nested-name-specifiers are intended to contain minimally-qualified |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 231 | // types. An actual ElaboratedType will not occur, since we'll store |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 232 | // just the type that is referred to in the nested-name-specifier (e.g., |
| 233 | // a TypedefType, TagType, etc.). However, when we are dealing with |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | // dependent template-id types (e.g., Outer<T>::template Inner<U>), |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 235 | // the type requires its own nested-name-specifier for uniqueness, so we |
| 236 | // suppress that nested-name-specifier during printing. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 237 | assert(!isa<ElaboratedType>(T) && |
| 238 | "Elaborated type in nested-name-specifier"); |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 239 | if (const TemplateSpecializationType *SpecType |
| 240 | = dyn_cast<TemplateSpecializationType>(T)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | // Print the template name without its corresponding |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 242 | // nested-name-specifier. |
| 243 | SpecType->getTemplateName().print(OS, InnerPolicy, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 245 | // Print the template argument list. |
| 246 | TypeStr = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | SpecType->getArgs(), |
| 248 | SpecType->getNumArgs(), |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 249 | InnerPolicy); |
| 250 | } else { |
| 251 | // Print the type normally |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 252 | TypeStr = QualType(T, 0).getAsString(InnerPolicy); |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 253 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 254 | OS << TypeStr; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | OS << "::"; |
| 260 | } |
| 261 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 262 | void NestedNameSpecifier::dump(const LangOptions &LO) { |
| 263 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 264 | } |