blob: 90ec4d33fdfeec25e37198a883e68834a2a09bbe [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
134 // If this is a qualified name type, suppress the qualification:
135 // it's part of our nested-name-specifier sequence anyway. FIXME:
136 // We should be able to assert that this doesn't happen.
137 if (const QualifiedNameType *QualT = dyn_cast<QualifiedNameType>(T))
138 T = QualT->getNamedType().getTypePtr();
139
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000140 PrintingPolicy InnerPolicy(Policy);
141 InnerPolicy.SuppressTagKind = true;
142 T->getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000143 OS << TypeStr;
144 break;
145 }
146 }
147
148 OS << "::";
149}
150
151void NestedNameSpecifier::Destroy(ASTContext &Context) {
152 this->~NestedNameSpecifier();
153 Context.Deallocate((void *)this);
Douglas Gregorbad35182009-03-19 03:51:16 +0000154}
Douglas Gregord57959a2009-03-27 23:10:48 +0000155
Chris Lattnere4f21422009-06-30 01:26:17 +0000156void NestedNameSpecifier::dump(const LangOptions &LO) {
157 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregord57959a2009-03-27 23:10:48 +0000158}