blob: 0376f9db6be0d891f60e3c29e6ba6def5d5408b7 [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 *
25NestedNameSpecifier::FindOrInsert(ASTContext &Context,
26 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;
31 NestedNameSpecifier *NNS
32 = 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 *
42NestedNameSpecifier::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 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 *
55NestedNameSpecifier::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 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 *
69NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix,
70 bool Template, Type *T) {
71 assert(T && "Type cannot be NULL");
72 NestedNameSpecifier Mockup;
Douglas Gregor17343172009-04-01 00:28:59 +000073 Mockup.Prefix.setPointer(Prefix);
74 Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec);
75 Mockup.Specifier = T;
Douglas Gregorab452ba2009-03-26 23:50:42 +000076 return FindOrInsert(Context, Mockup);
77}
Douglas Gregorbad35182009-03-19 03:51:16 +000078
Douglas Gregorab452ba2009-03-26 23:50:42 +000079NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) {
80 if (!Context.GlobalNestedNameSpecifier)
Douglas Gregor17343172009-04-01 00:28:59 +000081 Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
Douglas Gregorab452ba2009-03-26 23:50:42 +000082 return Context.GlobalNestedNameSpecifier;
83}
84
85/// \brief Whether this nested name specifier refers to a dependent
86/// type or not.
87bool 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 Gregorbad35182009-03-19 03:51:16 +0000100 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000101
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 Gregord249e1d1f2009-05-29 20:38:28 +0000108void
109NestedNameSpecifier::print(llvm::raw_ostream &OS,
110 const PrintingPolicy &Policy) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000111 if (getPrefix())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000112 getPrefix()->print(OS, Policy);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000113
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 Gregord249e1d1f2009-05-29 20:38:28 +0000134 PrintingPolicy InnerPolicy(Policy);
135 InnerPolicy.SuppressTagKind = true;
Douglas Gregordacd4342009-08-26 00:04:55 +0000136
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 Gregorab452ba2009-03-26 23:50:42 +0000161 OS << TypeStr;
162 break;
163 }
164 }
165
166 OS << "::";
167}
168
169void NestedNameSpecifier::Destroy(ASTContext &Context) {
170 this->~NestedNameSpecifier();
171 Context.Deallocate((void *)this);
Douglas Gregorbad35182009-03-19 03:51:16 +0000172}
Douglas Gregord57959a2009-03-27 23:10:48 +0000173
Chris Lattnere4f21422009-06-30 01:26:17 +0000174void NestedNameSpecifier::dump(const LangOptions &LO) {
175 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregord57959a2009-03-27 23:10:48 +0000176}