blob: d6594cdfd02f8fc6c89da642d1c516f6a5fccc28 [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 *
Mike Stump1eb44332009-09-09 15:08:12 +000025NestedNameSpecifier::FindOrInsert(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 *
Mike Stump1eb44332009-09-09 15:08:12 +000042NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +000043 IdentifierInfo *II) {
44 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 *
Mike Stump1eb44332009-09-09 15:08:12 +000055NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +000056 NamespaceDecl *NS) {
57 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 *
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 Gregor2700dcd2009-09-02 23:58:38 +000078
79NestedNameSpecifier *
80NestedNameSpecifier::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
Douglas Gregorab452ba2009-03-26 23:50:42 +000089NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) {
90 if (!Context.GlobalNestedNameSpecifier)
Douglas Gregor17343172009-04-01 00:28:59 +000091 Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
Douglas Gregorab452ba2009-03-26 23:50:42 +000092 return Context.GlobalNestedNameSpecifier;
93}
94
95/// \brief Whether this nested name specifier refers to a dependent
96/// type or not.
97bool 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();
Douglas Gregorbad35182009-03-19 03:51:16 +0000110 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000111
112 // Necessary to suppress a GCC warning.
113 return false;
114}
115
116/// \brief Print this nested name specifier to the given output
117/// stream.
Mike Stump1eb44332009-09-09 15:08:12 +0000118void
119NestedNameSpecifier::print(llvm::raw_ostream &OS,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000120 const PrintingPolicy &Policy) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000121 if (getPrefix())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000122 getPrefix()->print(OS, Policy);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000123
124 switch (getKind()) {
125 case Identifier:
126 OS << getAsIdentifier()->getName();
127 break;
128
129 case Namespace:
130 OS << getAsNamespace()->getIdentifier()->getName();
131 break;
132
133 case Global:
134 break;
135
136 case TypeSpecWithTemplate:
137 OS << "template ";
138 // Fall through to print the type.
139
140 case TypeSpec: {
141 std::string TypeStr;
142 Type *T = getAsType();
143
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000144 PrintingPolicy InnerPolicy(Policy);
John McCall2191b202009-09-05 06:31:47 +0000145 InnerPolicy.SuppressScope = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Douglas Gregordacd4342009-08-26 00:04:55 +0000147 // Nested-name-specifiers are intended to contain minimally-qualified
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000148 // types. An actual ElaboratedType will not occur, since we'll store
Douglas Gregordacd4342009-08-26 00:04:55 +0000149 // 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
Mike Stump1eb44332009-09-09 15:08:12 +0000151 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
Douglas Gregordacd4342009-08-26 00:04:55 +0000152 // the type requires its own nested-name-specifier for uniqueness, so we
153 // suppress that nested-name-specifier during printing.
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000154 assert(!isa<ElaboratedType>(T) &&
155 "Elaborated type in nested-name-specifier");
Douglas Gregordacd4342009-08-26 00:04:55 +0000156 if (const TemplateSpecializationType *SpecType
157 = dyn_cast<TemplateSpecializationType>(T)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000158 // Print the template name without its corresponding
Douglas Gregordacd4342009-08-26 00:04:55 +0000159 // nested-name-specifier.
160 SpecType->getTemplateName().print(OS, InnerPolicy, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Douglas Gregordacd4342009-08-26 00:04:55 +0000162 // Print the template argument list.
163 TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000164 SpecType->getArgs(),
165 SpecType->getNumArgs(),
Douglas Gregordacd4342009-08-26 00:04:55 +0000166 InnerPolicy);
167 } else {
168 // Print the type normally
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000169 TypeStr = QualType(T, 0).getAsString(InnerPolicy);
Douglas Gregordacd4342009-08-26 00:04:55 +0000170 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000171 OS << TypeStr;
172 break;
173 }
174 }
175
176 OS << "::";
177}
178
179void NestedNameSpecifier::Destroy(ASTContext &Context) {
180 this->~NestedNameSpecifier();
181 Context.Deallocate((void *)this);
Douglas Gregorbad35182009-03-19 03:51:16 +0000182}
Douglas Gregord57959a2009-03-27 23:10:48 +0000183
Chris Lattnere4f21422009-06-30 01:26:17 +0000184void NestedNameSpecifier::dump(const LangOptions &LO) {
185 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregord57959a2009-03-27 23:10:48 +0000186}