blob: f0edf105686008797385aa191026ffcfc987fc0a [file] [log] [blame]
Douglas Gregore4e5b052009-03-19 00:18:19 +00001//===--- 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 Gregord249e1d1f2009-05-29 20:38:28 +000017#include "clang/AST/PrettyPrinter.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000018#include "clang/AST/Type.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000019#include "llvm/Support/raw_ostream.h"
Douglas Gregorab452ba2009-03-26 23:50:42 +000020#include <cassert>
Douglas Gregorbad35182009-03-19 03:51:16 +000021
Douglas Gregore4e5b052009-03-19 00:18:19 +000022using namespace clang;
23
Douglas Gregorab452ba2009-03-26 23:50:42 +000024NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000025NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
Douglas Gregorab452ba2009-03-26 23:50:42 +000026 const NestedNameSpecifier &Mockup) {
27 llvm::FoldingSetNodeID ID;
28 Mockup.Profile(ID);
Douglas Gregore4e5b052009-03-19 00:18:19 +000029
Douglas Gregorab452ba2009-03-26 23:50:42 +000030 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000031 NestedNameSpecifier *NNS
Douglas Gregorab452ba2009-03-26 23:50:42 +000032 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
33 if (!NNS) {
Douglas Gregor17343172009-04-01 00:28:59 +000034 NNS = new (Context, 4) NestedNameSpecifier(Mockup);
Douglas Gregorab452ba2009-03-26 23:50:42 +000035 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
36 }
Douglas Gregore4e5b052009-03-19 00:18:19 +000037
Douglas Gregorab452ba2009-03-26 23:50:42 +000038 return NNS;
Douglas Gregore4e5b052009-03-19 00:18:19 +000039}
Douglas Gregorbad35182009-03-19 03:51:16 +000040
Douglas Gregorab452ba2009-03-26 23:50:42 +000041NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000042NestedNameSpecifier::Create(const ASTContext &Context,
43 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
Douglas Gregorab452ba2009-03-26 23:50:42 +000044 assert(II && "Identifier cannot be NULL");
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000045 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
Douglas Gregorbad35182009-03-19 03:51:16 +000046
Douglas Gregorab452ba2009-03-26 23:50:42 +000047 NestedNameSpecifier Mockup;
Douglas Gregor17343172009-04-01 00:28:59 +000048 Mockup.Prefix.setPointer(Prefix);
49 Mockup.Prefix.setInt(Identifier);
50 Mockup.Specifier = II;
Douglas Gregorab452ba2009-03-26 23:50:42 +000051 return FindOrInsert(Context, Mockup);
52}
Douglas Gregorbad35182009-03-19 03:51:16 +000053
Douglas Gregorab452ba2009-03-26 23:50:42 +000054NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000055NestedNameSpecifier::Create(const ASTContext &Context,
56 NestedNameSpecifier *Prefix, NamespaceDecl *NS) {
Douglas Gregorab452ba2009-03-26 23:50:42 +000057 assert(NS && "Namespace cannot be NULL");
Mike Stump1eb44332009-09-09 15:08:12 +000058 assert((!Prefix ||
Douglas Gregorab452ba2009-03-26 23:50:42 +000059 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
60 "Broken nested name specifier");
61 NestedNameSpecifier Mockup;
Douglas Gregor17343172009-04-01 00:28:59 +000062 Mockup.Prefix.setPointer(Prefix);
63 Mockup.Prefix.setInt(Namespace);
64 Mockup.Specifier = NS;
Douglas Gregorab452ba2009-03-26 23:50:42 +000065 return FindOrInsert(Context, Mockup);
66}
67
68NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000069NestedNameSpecifier::Create(const ASTContext &Context,
70 NestedNameSpecifier *Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +000071 bool Template, Type *T) {
72 assert(T && "Type cannot be NULL");
73 NestedNameSpecifier Mockup;
Douglas Gregor17343172009-04-01 00:28:59 +000074 Mockup.Prefix.setPointer(Prefix);
75 Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec);
76 Mockup.Specifier = T;
Douglas Gregorab452ba2009-03-26 23:50:42 +000077 return FindOrInsert(Context, Mockup);
78}
Douglas Gregor2700dcd2009-09-02 23:58:38 +000079
80NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000081NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
Douglas Gregor2700dcd2009-09-02 23:58:38 +000082 assert(II && "Identifier cannot be NULL");
83 NestedNameSpecifier Mockup;
84 Mockup.Prefix.setPointer(0);
85 Mockup.Prefix.setInt(Identifier);
86 Mockup.Specifier = II;
87 return FindOrInsert(Context, Mockup);
88}
89
Jay Foad4ba2a172011-01-12 09:06:06 +000090NestedNameSpecifier *
91NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
Douglas Gregorab452ba2009-03-26 23:50:42 +000092 if (!Context.GlobalNestedNameSpecifier)
Douglas Gregor17343172009-04-01 00:28:59 +000093 Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
Douglas Gregorab452ba2009-03-26 23:50:42 +000094 return Context.GlobalNestedNameSpecifier;
95}
96
97/// \brief Whether this nested name specifier refers to a dependent
98/// type or not.
99bool NestedNameSpecifier::isDependent() const {
100 switch (getKind()) {
101 case Identifier:
102 // Identifier specifiers always represent dependent types
103 return true;
104
105 case Namespace:
106 case Global:
107 return false;
108
109 case TypeSpec:
110 case TypeSpecWithTemplate:
111 return getAsType()->isDependentType();
Douglas Gregorbad35182009-03-19 03:51:16 +0000112 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000113
114 // Necessary to suppress a GCC warning.
115 return false;
116}
117
Douglas Gregord0937222010-12-13 22:49:22 +0000118bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
119 switch (getKind()) {
120 case Identifier:
121 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
122
123 case Namespace:
124 case Global:
125 return false;
126
127 case TypeSpec:
128 case TypeSpecWithTemplate:
129 return getAsType()->containsUnexpandedParameterPack();
130 }
131
132 // Necessary to suppress a GCC warning.
133 return false;
134}
135
Douglas Gregorab452ba2009-03-26 23:50:42 +0000136/// \brief Print this nested name specifier to the given output
137/// stream.
Mike Stump1eb44332009-09-09 15:08:12 +0000138void
139NestedNameSpecifier::print(llvm::raw_ostream &OS,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000140 const PrintingPolicy &Policy) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000141 if (getPrefix())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000142 getPrefix()->print(OS, Policy);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000143
144 switch (getKind()) {
145 case Identifier:
146 OS << getAsIdentifier()->getName();
147 break;
148
149 case Namespace:
150 OS << getAsNamespace()->getIdentifier()->getName();
151 break;
152
153 case Global:
154 break;
155
156 case TypeSpecWithTemplate:
157 OS << "template ";
158 // Fall through to print the type.
159
160 case TypeSpec: {
161 std::string TypeStr;
162 Type *T = getAsType();
163
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000164 PrintingPolicy InnerPolicy(Policy);
John McCall2191b202009-09-05 06:31:47 +0000165 InnerPolicy.SuppressScope = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Douglas Gregordacd4342009-08-26 00:04:55 +0000167 // Nested-name-specifiers are intended to contain minimally-qualified
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000168 // types. An actual ElaboratedType will not occur, since we'll store
Douglas Gregordacd4342009-08-26 00:04:55 +0000169 // just the type that is referred to in the nested-name-specifier (e.g.,
170 // a TypedefType, TagType, etc.). However, when we are dealing with
Mike Stump1eb44332009-09-09 15:08:12 +0000171 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
Douglas Gregordacd4342009-08-26 00:04:55 +0000172 // the type requires its own nested-name-specifier for uniqueness, so we
173 // suppress that nested-name-specifier during printing.
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000174 assert(!isa<ElaboratedType>(T) &&
175 "Elaborated type in nested-name-specifier");
Douglas Gregordacd4342009-08-26 00:04:55 +0000176 if (const TemplateSpecializationType *SpecType
177 = dyn_cast<TemplateSpecializationType>(T)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000178 // Print the template name without its corresponding
Douglas Gregordacd4342009-08-26 00:04:55 +0000179 // nested-name-specifier.
180 SpecType->getTemplateName().print(OS, InnerPolicy, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Douglas Gregordacd4342009-08-26 00:04:55 +0000182 // Print the template argument list.
183 TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000184 SpecType->getArgs(),
185 SpecType->getNumArgs(),
Douglas Gregordacd4342009-08-26 00:04:55 +0000186 InnerPolicy);
187 } else {
188 // Print the type normally
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000189 TypeStr = QualType(T, 0).getAsString(InnerPolicy);
Douglas Gregordacd4342009-08-26 00:04:55 +0000190 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000191 OS << TypeStr;
192 break;
193 }
194 }
195
196 OS << "::";
197}
198
Chris Lattnere4f21422009-06-30 01:26:17 +0000199void NestedNameSpecifier::dump(const LangOptions &LO) {
200 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregord57959a2009-03-27 23:10:48 +0000201}