Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 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/Type.h" |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 19 | #include <cassert> |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 20 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 23 | NestedNameSpecifier * |
| 24 | NestedNameSpecifier::FindOrInsert(ASTContext &Context, |
| 25 | const NestedNameSpecifier &Mockup) { |
| 26 | llvm::FoldingSetNodeID ID; |
| 27 | Mockup.Profile(ID); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 29 | void *InsertPos = 0; |
| 30 | NestedNameSpecifier *NNS |
| 31 | = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos); |
| 32 | if (!NNS) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame^] | 33 | NNS = new (Context, 4) NestedNameSpecifier(Mockup); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 34 | Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos); |
| 35 | } |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 37 | return NNS; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 38 | } |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 40 | NestedNameSpecifier * |
| 41 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
| 42 | IdentifierInfo *II) { |
| 43 | assert(II && "Identifier cannot be NULL"); |
| 44 | assert(Prefix && Prefix->isDependent() && "Prefix must be dependent"); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 45 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 46 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame^] | 47 | Mockup.Prefix.setPointer(Prefix); |
| 48 | Mockup.Prefix.setInt(Identifier); |
| 49 | Mockup.Specifier = II; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 50 | return FindOrInsert(Context, Mockup); |
| 51 | } |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 53 | NestedNameSpecifier * |
| 54 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
| 55 | NamespaceDecl *NS) { |
| 56 | assert(NS && "Namespace cannot be NULL"); |
| 57 | assert((!Prefix || |
| 58 | (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) && |
| 59 | "Broken nested name specifier"); |
| 60 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame^] | 61 | Mockup.Prefix.setPointer(Prefix); |
| 62 | Mockup.Prefix.setInt(Namespace); |
| 63 | Mockup.Specifier = NS; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 64 | return FindOrInsert(Context, Mockup); |
| 65 | } |
| 66 | |
| 67 | NestedNameSpecifier * |
| 68 | NestedNameSpecifier::Create(ASTContext &Context, NestedNameSpecifier *Prefix, |
| 69 | bool Template, Type *T) { |
| 70 | assert(T && "Type cannot be NULL"); |
| 71 | NestedNameSpecifier Mockup; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame^] | 72 | Mockup.Prefix.setPointer(Prefix); |
| 73 | Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec); |
| 74 | Mockup.Specifier = T; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 75 | return FindOrInsert(Context, Mockup); |
| 76 | } |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 78 | NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) { |
| 79 | if (!Context.GlobalNestedNameSpecifier) |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame^] | 80 | Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier(); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 81 | return Context.GlobalNestedNameSpecifier; |
| 82 | } |
| 83 | |
| 84 | /// \brief Whether this nested name specifier refers to a dependent |
| 85 | /// type or not. |
| 86 | bool NestedNameSpecifier::isDependent() const { |
| 87 | switch (getKind()) { |
| 88 | case Identifier: |
| 89 | // Identifier specifiers always represent dependent types |
| 90 | return true; |
| 91 | |
| 92 | case Namespace: |
| 93 | case Global: |
| 94 | return false; |
| 95 | |
| 96 | case TypeSpec: |
| 97 | case TypeSpecWithTemplate: |
| 98 | return getAsType()->isDependentType(); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 99 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 100 | |
| 101 | // Necessary to suppress a GCC warning. |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /// \brief Print this nested name specifier to the given output |
| 106 | /// stream. |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 107 | void NestedNameSpecifier::print(llvm::raw_ostream &OS) const { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame^] | 108 | if (getPrefix()) |
| 109 | getPrefix()->print(OS); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 110 | |
| 111 | switch (getKind()) { |
| 112 | case Identifier: |
| 113 | OS << getAsIdentifier()->getName(); |
| 114 | break; |
| 115 | |
| 116 | case Namespace: |
| 117 | OS << getAsNamespace()->getIdentifier()->getName(); |
| 118 | break; |
| 119 | |
| 120 | case Global: |
| 121 | break; |
| 122 | |
| 123 | case TypeSpecWithTemplate: |
| 124 | OS << "template "; |
| 125 | // Fall through to print the type. |
| 126 | |
| 127 | case TypeSpec: { |
| 128 | std::string TypeStr; |
| 129 | Type *T = getAsType(); |
| 130 | |
| 131 | // If this is a qualified name type, suppress the qualification: |
| 132 | // it's part of our nested-name-specifier sequence anyway. FIXME: |
| 133 | // We should be able to assert that this doesn't happen. |
| 134 | if (const QualifiedNameType *QualT = dyn_cast<QualifiedNameType>(T)) |
| 135 | T = QualT->getNamedType().getTypePtr(); |
| 136 | |
| 137 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 138 | TagT->getAsStringInternal(TypeStr, true); |
| 139 | else |
| 140 | T->getAsStringInternal(TypeStr); |
| 141 | OS << TypeStr; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | OS << "::"; |
| 147 | } |
| 148 | |
| 149 | void NestedNameSpecifier::Destroy(ASTContext &Context) { |
| 150 | this->~NestedNameSpecifier(); |
| 151 | Context.Deallocate((void *)this); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 152 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 153 | |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 154 | void NestedNameSpecifier::dump() { |
| 155 | print(llvm::errs()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 156 | } |