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"
17 #include "clang/AST/PrettyPrinter.h"
18 #include "clang/AST/Type.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <cassert>
21 
22 using namespace clang;
23 
24 NestedNameSpecifier *
25 NestedNameSpecifier::FindOrInsert(ASTContext &Context,
26                                   const NestedNameSpecifier &Mockup) {
27   llvm::FoldingSetNodeID ID;
28   Mockup.Profile(ID);
29 
30   void *InsertPos = 0;
31   NestedNameSpecifier *NNS
32     = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
33   if (!NNS) {
34     NNS = new (Context, 4) NestedNameSpecifier(Mockup);
35     Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
36   }
37 
38   return NNS;
39 }
40 
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");
46 
47   NestedNameSpecifier Mockup;
48   Mockup.Prefix.setPointer(Prefix);
49   Mockup.Prefix.setInt(Identifier);
50   Mockup.Specifier = II;
51   return FindOrInsert(Context, Mockup);
52 }
53 
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;
62   Mockup.Prefix.setPointer(Prefix);
63   Mockup.Prefix.setInt(Namespace);
64   Mockup.Specifier = NS;
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;
73   Mockup.Prefix.setPointer(Prefix);
74   Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec);
75   Mockup.Specifier = T;
76   return FindOrInsert(Context, Mockup);
77 }
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 
89 NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) {
90   if (!Context.GlobalNestedNameSpecifier)
91     Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
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();
110   }
111 
112   // Necessary to suppress a GCC warning.
113   return false;
114 }
115 
116 bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
117   switch (getKind()) {
118   case Identifier:
119     return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
120 
121   case Namespace:
122   case Global:
123     return false;
124 
125   case TypeSpec:
126   case TypeSpecWithTemplate:
127     return getAsType()->containsUnexpandedParameterPack();
128   }
129 
130   // Necessary to suppress a GCC warning.
131   return false;
132 }
133 
134 /// \brief Print this nested name specifier to the given output
135 /// stream.
136 void
137 NestedNameSpecifier::print(llvm::raw_ostream &OS,
138                            const PrintingPolicy &Policy) const {
139   if (getPrefix())
140     getPrefix()->print(OS, Policy);
141 
142   switch (getKind()) {
143   case Identifier:
144     OS << getAsIdentifier()->getName();
145     break;
146 
147   case Namespace:
148     OS << getAsNamespace()->getIdentifier()->getName();
149     break;
150 
151   case Global:
152     break;
153 
154   case TypeSpecWithTemplate:
155     OS << "template ";
156     // Fall through to print the type.
157 
158   case TypeSpec: {
159     std::string TypeStr;
160     Type *T = getAsType();
161 
162     PrintingPolicy InnerPolicy(Policy);
163     InnerPolicy.SuppressScope = true;
164 
165     // Nested-name-specifiers are intended to contain minimally-qualified
166     // types. An actual ElaboratedType will not occur, since we'll store
167     // just the type that is referred to in the nested-name-specifier (e.g.,
168     // a TypedefType, TagType, etc.). However, when we are dealing with
169     // dependent template-id types (e.g., Outer<T>::template Inner<U>),
170     // the type requires its own nested-name-specifier for uniqueness, so we
171     // suppress that nested-name-specifier during printing.
172     assert(!isa<ElaboratedType>(T) &&
173            "Elaborated type in nested-name-specifier");
174     if (const TemplateSpecializationType *SpecType
175           = dyn_cast<TemplateSpecializationType>(T)) {
176       // Print the template name without its corresponding
177       // nested-name-specifier.
178       SpecType->getTemplateName().print(OS, InnerPolicy, true);
179 
180       // Print the template argument list.
181       TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
182                                                           SpecType->getArgs(),
183                                                        SpecType->getNumArgs(),
184                                                                  InnerPolicy);
185     } else {
186       // Print the type normally
187       TypeStr = QualType(T, 0).getAsString(InnerPolicy);
188     }
189     OS << TypeStr;
190     break;
191   }
192   }
193 
194   OS << "::";
195 }
196 
197 void NestedNameSpecifier::dump(const LangOptions &LO) {
198   print(llvm::errs(), PrintingPolicy(LO));
199 }
200