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 | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 17 | #include "clang/AST/PrettyPrinter.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 18 | #include "clang/AST/Type.h" |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 20 | #include <cassert> |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 21 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 24 | NestedNameSpecifier * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | NestedNameSpecifier::FindOrInsert(ASTContext &Context, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 26 | const NestedNameSpecifier &Mockup) { |
| 27 | llvm::FoldingSetNodeID ID; |
| 28 | Mockup.Profile(ID); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 30 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | NestedNameSpecifier *NNS |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 32 | = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos); |
| 33 | if (!NNS) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 34 | NNS = new (Context, 4) NestedNameSpecifier(Mockup); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 35 | Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos); |
| 36 | } |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 37 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 38 | return NNS; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 39 | } |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 41 | NestedNameSpecifier * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 43 | IdentifierInfo *II) { |
| 44 | assert(II && "Identifier cannot be NULL"); |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 45 | assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent"); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 46 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 47 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 48 | Mockup.Prefix.setPointer(Prefix); |
| 49 | Mockup.Prefix.setInt(Identifier); |
| 50 | Mockup.Specifier = II; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 51 | return FindOrInsert(Context, Mockup); |
| 52 | } |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 54 | NestedNameSpecifier * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 56 | NamespaceDecl *NS) { |
| 57 | assert(NS && "Namespace cannot be NULL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | assert((!Prefix || |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 59 | (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) && |
| 60 | "Broken nested name specifier"); |
| 61 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 62 | Mockup.Prefix.setPointer(Prefix); |
| 63 | Mockup.Prefix.setInt(Namespace); |
| 64 | Mockup.Specifier = NS; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 65 | return FindOrInsert(Context, Mockup); |
| 66 | } |
| 67 | |
| 68 | NestedNameSpecifier * |
| 69 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
| 70 | bool Template, Type *T) { |
| 71 | assert(T && "Type cannot be NULL"); |
| 72 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 73 | Mockup.Prefix.setPointer(Prefix); |
| 74 | Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec); |
| 75 | Mockup.Specifier = T; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 76 | return FindOrInsert(Context, Mockup); |
| 77 | } |
Douglas Gregor | 2700dcd | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 78 | |
| 79 | NestedNameSpecifier * |
| 80 | NestedNameSpecifier::Create(ASTContext &Context, IdentifierInfo *II) { |
| 81 | assert(II && "Identifier cannot be NULL"); |
| 82 | NestedNameSpecifier Mockup; |
| 83 | Mockup.Prefix.setPointer(0); |
| 84 | Mockup.Prefix.setInt(Identifier); |
| 85 | Mockup.Specifier = II; |
| 86 | return FindOrInsert(Context, Mockup); |
| 87 | } |
| 88 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 89 | NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) { |
| 90 | if (!Context.GlobalNestedNameSpecifier) |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 91 | Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier(); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 92 | return Context.GlobalNestedNameSpecifier; |
| 93 | } |
| 94 | |
| 95 | /// \brief Whether this nested name specifier refers to a dependent |
| 96 | /// type or not. |
| 97 | bool NestedNameSpecifier::isDependent() const { |
| 98 | switch (getKind()) { |
| 99 | case Identifier: |
| 100 | // Identifier specifiers always represent dependent types |
| 101 | return true; |
| 102 | |
| 103 | case Namespace: |
| 104 | case Global: |
| 105 | return false; |
| 106 | |
| 107 | case TypeSpec: |
| 108 | case TypeSpecWithTemplate: |
| 109 | return getAsType()->isDependentType(); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 110 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 111 | |
| 112 | // Necessary to suppress a GCC warning. |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | /// \brief Print this nested name specifier to the given output |
| 117 | /// stream. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | void |
| 119 | NestedNameSpecifier::print(llvm::raw_ostream &OS, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 120 | const PrintingPolicy &Policy) const { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 121 | if (getPrefix()) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 122 | getPrefix()->print(OS, Policy); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 123 | |
| 124 | switch (getKind()) { |
| 125 | case Identifier: |
| 126 | OS << getAsIdentifier()->getName(); |
| 127 | break; |
| 128 | |
| 129 | case Namespace: |
| 130 | OS << getAsNamespace()->getIdentifier()->getName(); |
| 131 | break; |
| 132 | |
| 133 | case Global: |
| 134 | break; |
| 135 | |
| 136 | case TypeSpecWithTemplate: |
| 137 | OS << "template "; |
| 138 | // Fall through to print the type. |
| 139 | |
| 140 | case TypeSpec: { |
| 141 | std::string TypeStr; |
| 142 | Type *T = getAsType(); |
| 143 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 144 | PrintingPolicy InnerPolicy(Policy); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 145 | InnerPolicy.SuppressScope = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 147 | // Nested-name-specifiers are intended to contain minimally-qualified |
| 148 | // types. An actual QualifiedNameType will not occur, since we'll store |
| 149 | // just the type that is referred to in the nested-name-specifier (e.g., |
| 150 | // a TypedefType, TagType, etc.). However, when we are dealing with |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | // dependent template-id types (e.g., Outer<T>::template Inner<U>), |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 152 | // the type requires its own nested-name-specifier for uniqueness, so we |
| 153 | // suppress that nested-name-specifier during printing. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | assert(!isa<QualifiedNameType>(T) && |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 155 | "Qualified name type in nested-name-specifier"); |
| 156 | if (const TemplateSpecializationType *SpecType |
| 157 | = dyn_cast<TemplateSpecializationType>(T)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | // Print the template name without its corresponding |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 159 | // nested-name-specifier. |
| 160 | SpecType->getTemplateName().print(OS, InnerPolicy, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 162 | // Print the template argument list. |
| 163 | TypeStr = TemplateSpecializationType::PrintTemplateArgumentList( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | SpecType->getArgs(), |
| 165 | SpecType->getNumArgs(), |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 166 | InnerPolicy); |
| 167 | } else { |
| 168 | // Print the type normally |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 169 | TypeStr = QualType(T, 0).getAsString(InnerPolicy); |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 170 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 171 | OS << TypeStr; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | OS << "::"; |
| 177 | } |
| 178 | |
| 179 | void NestedNameSpecifier::Destroy(ASTContext &Context) { |
| 180 | this->~NestedNameSpecifier(); |
| 181 | Context.Deallocate((void *)this); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 182 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 183 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 184 | void NestedNameSpecifier::dump(const LangOptions &LO) { |
| 185 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 186 | } |