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 * |
| 25 | NestedNameSpecifier::FindOrInsert(ASTContext &Context, |
| 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; |
| 31 | NestedNameSpecifier *NNS |
| 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 * |
| 42 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
| 43 | IdentifierInfo *II) { |
| 44 | assert(II && "Identifier cannot be NULL"); |
| 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 * |
| 55 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
| 56 | NamespaceDecl *NS) { |
| 57 | assert(NS && "Namespace cannot be NULL"); |
| 58 | assert((!Prefix || |
| 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 | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 78 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 79 | NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) { |
| 80 | if (!Context.GlobalNestedNameSpecifier) |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 81 | Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier(); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 82 | return Context.GlobalNestedNameSpecifier; |
| 83 | } |
| 84 | |
| 85 | /// \brief Whether this nested name specifier refers to a dependent |
| 86 | /// type or not. |
| 87 | bool NestedNameSpecifier::isDependent() const { |
| 88 | switch (getKind()) { |
| 89 | case Identifier: |
| 90 | // Identifier specifiers always represent dependent types |
| 91 | return true; |
| 92 | |
| 93 | case Namespace: |
| 94 | case Global: |
| 95 | return false; |
| 96 | |
| 97 | case TypeSpec: |
| 98 | case TypeSpecWithTemplate: |
| 99 | return getAsType()->isDependentType(); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 100 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 101 | |
| 102 | // Necessary to suppress a GCC warning. |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | /// \brief Print this nested name specifier to the given output |
| 107 | /// stream. |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 108 | void |
| 109 | NestedNameSpecifier::print(llvm::raw_ostream &OS, |
| 110 | const PrintingPolicy &Policy) const { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 111 | if (getPrefix()) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 112 | getPrefix()->print(OS, Policy); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 113 | |
| 114 | switch (getKind()) { |
| 115 | case Identifier: |
| 116 | OS << getAsIdentifier()->getName(); |
| 117 | break; |
| 118 | |
| 119 | case Namespace: |
| 120 | OS << getAsNamespace()->getIdentifier()->getName(); |
| 121 | break; |
| 122 | |
| 123 | case Global: |
| 124 | break; |
| 125 | |
| 126 | case TypeSpecWithTemplate: |
| 127 | OS << "template "; |
| 128 | // Fall through to print the type. |
| 129 | |
| 130 | case TypeSpec: { |
| 131 | std::string TypeStr; |
| 132 | Type *T = getAsType(); |
| 133 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 134 | PrintingPolicy InnerPolicy(Policy); |
| 135 | InnerPolicy.SuppressTagKind = true; |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 136 | |
| 137 | // Nested-name-specifiers are intended to contain minimally-qualified |
| 138 | // types. An actual QualifiedNameType will not occur, since we'll store |
| 139 | // just the type that is referred to in the nested-name-specifier (e.g., |
| 140 | // a TypedefType, TagType, etc.). However, when we are dealing with |
| 141 | // dependent template-id types (e.g., Outer<T>::template Inner<U>), |
| 142 | // the type requires its own nested-name-specifier for uniqueness, so we |
| 143 | // suppress that nested-name-specifier during printing. |
| 144 | assert(!isa<QualifiedNameType>(T) && |
| 145 | "Qualified name type in nested-name-specifier"); |
| 146 | if (const TemplateSpecializationType *SpecType |
| 147 | = dyn_cast<TemplateSpecializationType>(T)) { |
| 148 | // Print the template name without its corresponding |
| 149 | // nested-name-specifier. |
| 150 | SpecType->getTemplateName().print(OS, InnerPolicy, true); |
| 151 | |
| 152 | // Print the template argument list. |
| 153 | TypeStr = TemplateSpecializationType::PrintTemplateArgumentList( |
| 154 | SpecType->getArgs(), |
| 155 | SpecType->getNumArgs(), |
| 156 | InnerPolicy); |
| 157 | } else { |
| 158 | // Print the type normally |
| 159 | T->getAsStringInternal(TypeStr, InnerPolicy); |
| 160 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 161 | OS << TypeStr; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | OS << "::"; |
| 167 | } |
| 168 | |
| 169 | void NestedNameSpecifier::Destroy(ASTContext &Context) { |
| 170 | this->~NestedNameSpecifier(); |
| 171 | Context.Deallocate((void *)this); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 172 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 173 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 174 | void NestedNameSpecifier::dump(const LangOptions &LO) { |
| 175 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 176 | } |