1 //===--- NestedNameSpecifier.cpp - C++ nested name specifiers -----*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the NestedNameSpecifier class, which represents
11 // a C++ nested-name-specifier.
13 //===----------------------------------------------------------------------===//
14 #include "clang/AST/NestedNameSpecifier.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/PrettyPrinter.h"
18 #include "clang/AST/Type.h"
19 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
25 NestedNameSpecifier::FindOrInsert(ASTContext &Context,
26 const NestedNameSpecifier &Mockup) {
27 llvm::FoldingSetNodeID ID;
31 NestedNameSpecifier *NNS
32 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
34 NNS = new (Context, 4) NestedNameSpecifier(Mockup);
35 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
42 NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix,
44 assert(II && "Identifier cannot be NULL");
45 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
47 NestedNameSpecifier Mockup;
48 Mockup.Prefix.setPointer(Prefix);
49 Mockup.Prefix.setInt(Identifier);
50 Mockup.Specifier = II;
51 return FindOrInsert(Context, Mockup);
55 NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix,
57 assert(NS && "Namespace cannot be NULL");
59 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
60 "Broken nested name specifier");
61 NestedNameSpecifier Mockup;
62 Mockup.Prefix.setPointer(Prefix);
63 Mockup.Prefix.setInt(Namespace);
64 Mockup.Specifier = NS;
65 return FindOrInsert(Context, Mockup);
69 NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix,
70 bool Template, Type *T) {
71 assert(T && "Type cannot be NULL");
72 NestedNameSpecifier Mockup;
73 Mockup.Prefix.setPointer(Prefix);
74 Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec);
76 return FindOrInsert(Context, Mockup);
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);
89 NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) {
90 if (!Context.GlobalNestedNameSpecifier)
91 Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
92 return Context.GlobalNestedNameSpecifier;
95 /// \brief Whether this nested name specifier refers to a dependent
97 bool NestedNameSpecifier::isDependent() const {
100 // Identifier specifiers always represent dependent types
108 case TypeSpecWithTemplate:
109 return getAsType()->isDependentType();
112 // Necessary to suppress a GCC warning.
116 /// \brief Print this nested name specifier to the given output
119 NestedNameSpecifier::print(llvm::raw_ostream &OS,
120 const PrintingPolicy &Policy) const {
122 getPrefix()->print(OS, Policy);
126 OS << getAsIdentifier()->getName();
130 OS << getAsNamespace()->getIdentifier()->getName();
136 case TypeSpecWithTemplate:
138 // Fall through to print the type.
142 Type *T = getAsType();
144 PrintingPolicy InnerPolicy(Policy);
145 InnerPolicy.SuppressScope = true;
147 // Nested-name-specifiers are intended to contain minimally-qualified
148 // types. An actual ElaboratedType 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
151 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
152 // the type requires its own nested-name-specifier for uniqueness, so we
153 // suppress that nested-name-specifier during printing.
154 assert(!isa<ElaboratedType>(T) &&
155 "Elaborated type in nested-name-specifier");
156 if (const TemplateSpecializationType *SpecType
157 = dyn_cast<TemplateSpecializationType>(T)) {
158 // Print the template name without its corresponding
159 // nested-name-specifier.
160 SpecType->getTemplateName().print(OS, InnerPolicy, true);
162 // Print the template argument list.
163 TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
165 SpecType->getNumArgs(),
168 // Print the type normally
169 TypeStr = QualType(T, 0).getAsString(InnerPolicy);
179 void NestedNameSpecifier::dump(const LangOptions &LO) {
180 print(llvm::errs(), PrintingPolicy(LO));