Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 1 | //===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// |
| 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 implements C++ semantic analysis for scope specifiers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 16 | #include "clang/AST/NestedNameSpecifier.h" |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 17 | #include "clang/Parse/DeclSpec.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | using namespace clang; |
| 20 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 21 | /// \brief Compute the DeclContext that is associated with the given |
| 22 | /// scope specifier. |
| 23 | DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS) { |
| 24 | if (!SS.isSet() || SS.isInvalid()) |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 25 | return 0; |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 27 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame^] | 28 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 29 | if (NNS->isDependent()) |
| 30 | return 0; |
| 31 | |
| 32 | switch (NNS->getKind()) { |
| 33 | case NestedNameSpecifier::Identifier: |
| 34 | assert(false && "Dependent nested-name-specifier has no DeclContext"); |
| 35 | break; |
| 36 | |
| 37 | case NestedNameSpecifier::Namespace: |
| 38 | return NNS->getAsNamespace(); |
| 39 | |
| 40 | case NestedNameSpecifier::TypeSpec: |
| 41 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 42 | const TagType *Tag = NNS->getAsType()->getAsTagType(); |
| 43 | assert(Tag && "Non-tag type in nested-name-specifier"); |
| 44 | return Tag->getDecl(); |
| 45 | } break; |
| 46 | |
| 47 | case NestedNameSpecifier::Global: |
| 48 | return Context.getTranslationUnitDecl(); |
| 49 | } |
| 50 | |
| 51 | // Required to silence a GCC warning. |
| 52 | return 0; |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 55 | bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { |
| 56 | if (!SS.isSet() || SS.isInvalid()) |
| 57 | return false; |
| 58 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 59 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame^] | 60 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 61 | return NNS->isDependent(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 64 | /// \brief Require that the context specified by SS be complete. |
| 65 | /// |
| 66 | /// If SS refers to a type, this routine checks whether the type is |
| 67 | /// complete enough (or can be made complete enough) for name lookup |
| 68 | /// into the DeclContext. A type that is not yet completed can be |
| 69 | /// considered "complete enough" if it is a class/struct/union/enum |
| 70 | /// that is currently being defined. Or, if we have a type that names |
| 71 | /// a class template specialization that is not a complete type, we |
| 72 | /// will attempt to instantiate that class template. |
| 73 | bool Sema::RequireCompleteDeclContext(const CXXScopeSpec &SS) { |
| 74 | if (!SS.isSet() || SS.isInvalid()) |
| 75 | return false; |
| 76 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 77 | DeclContext *DC = computeDeclContext(SS); |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 78 | if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
| 79 | // If we're currently defining this type, then lookup into the |
| 80 | // type is okay: don't complain that it isn't complete yet. |
| 81 | const TagType *TagT = Context.getTypeDeclType(Tag)->getAsTagType(); |
| 82 | if (TagT->isBeingDefined()) |
| 83 | return false; |
| 84 | |
| 85 | // The type must be complete. |
| 86 | return RequireCompleteType(SS.getRange().getBegin(), |
| 87 | Context.getTypeDeclType(Tag), |
| 88 | diag::err_incomplete_nested_name_spec, |
| 89 | SS.getRange()); |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 94 | |
| 95 | /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the |
| 96 | /// global scope ('::'). |
| 97 | Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, |
| 98 | SourceLocation CCLoc) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 99 | return NestedNameSpecifier::GlobalSpecifier(Context); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /// ActOnCXXNestedNameSpecifier - Called during parsing of a |
| 103 | /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now |
| 104 | /// we want to resolve "bar::". 'SS' is empty or the previously parsed |
| 105 | /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', |
| 106 | /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. |
| 107 | /// Returns a CXXScopeTy* object representing the C++ scope. |
| 108 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 109 | const CXXScopeSpec &SS, |
| 110 | SourceLocation IdLoc, |
| 111 | SourceLocation CCLoc, |
| 112 | IdentifierInfo &II) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 113 | NestedNameSpecifier *Prefix |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame^] | 114 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 115 | |
| 116 | // If the prefix is already dependent, there is no name lookup to |
| 117 | // perform. Just build the resulting nested-name-specifier. |
| 118 | if (Prefix && Prefix->isDependent()) |
| 119 | return NestedNameSpecifier::Create(Context, Prefix, &II); |
| 120 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 121 | NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName); |
| 122 | |
| 123 | if (SD) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 124 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) |
| 125 | return NestedNameSpecifier::Create(Context, Prefix, Namespace); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 126 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 127 | if (TypeDecl *Type = dyn_cast<TypeDecl>(SD)) { |
| 128 | // Determine whether we have a class (or, in C++0x, an enum) or |
| 129 | // a typedef thereof. If so, build the nested-name-specifier. |
| 130 | QualType T; |
| 131 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
| 132 | if (TD->getUnderlyingType()->isRecordType() || |
| 133 | (getLangOptions().CPlusPlus0x && |
| 134 | TD->getUnderlyingType()->isEnumeralType())) |
| 135 | T = Context.getTypeDeclType(TD); |
| 136 | } else if (isa<RecordDecl>(Type) || |
| 137 | (getLangOptions().CPlusPlus0x && isa<EnumDecl>(Type))) |
| 138 | T = Context.getTypeDeclType(Type); |
| 139 | |
| 140 | if (!T.isNull()) |
| 141 | return NestedNameSpecifier::Create(Context, Prefix, false, |
| 142 | T.getTypePtr()); |
| 143 | } |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 144 | |
| 145 | // Fall through to produce an error: we found something that isn't |
| 146 | // a class or a namespace. |
| 147 | } |
| 148 | |
| 149 | // If we didn't find anything during our lookup, try again with |
| 150 | // ordinary name lookup, which can help us produce better error |
| 151 | // messages. |
| 152 | if (!SD) |
| 153 | SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName); |
| 154 | unsigned DiagID; |
| 155 | if (SD) |
| 156 | DiagID = diag::err_expected_class_or_namespace; |
| 157 | else if (SS.isSet()) |
| 158 | DiagID = diag::err_typecheck_no_member; |
| 159 | else |
| 160 | DiagID = diag::err_undeclared_var_use; |
| 161 | |
| 162 | if (SS.isSet()) |
| 163 | Diag(IdLoc, DiagID) << &II << SS.getRange(); |
| 164 | else |
| 165 | Diag(IdLoc, DiagID) << &II; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 170 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 171 | const CXXScopeSpec &SS, |
| 172 | TypeTy *Ty, |
| 173 | SourceRange TypeRange, |
| 174 | SourceLocation CCLoc) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 175 | NestedNameSpecifier *Prefix |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame^] | 176 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 177 | return NestedNameSpecifier::Create(Context, Prefix, /*FIXME:*/false, |
| 178 | QualType::getFromOpaquePtr(Ty).getTypePtr()); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 181 | /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global |
| 182 | /// scope or nested-name-specifier) is parsed, part of a declarator-id. |
| 183 | /// After this method is called, according to [C++ 3.4.3p3], names should be |
| 184 | /// looked up in the declarator-id's scope, until the declarator is parsed and |
| 185 | /// ActOnCXXExitDeclaratorScope is called. |
| 186 | /// The 'SS' should be a non-empty valid CXXScopeSpec. |
| 187 | void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 188 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
| 189 | assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?"); |
| 190 | PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity()); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 191 | CurContext = computeDeclContext(SS); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 192 | assert(CurContext && "No context?"); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 193 | S->setEntity(CurContext); |
| 194 | } |
| 195 | |
| 196 | /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously |
| 197 | /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same |
| 198 | /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. |
| 199 | /// Used to indicate that names should revert to being looked up in the |
| 200 | /// defining scope. |
| 201 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 202 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 203 | assert(S->getEntity() == computeDeclContext(SS) && "Context imbalance!"); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 204 | S->setEntity(PreDeclaratorDC); |
| 205 | PreDeclaratorDC = 0; |
| 206 | |
| 207 | // Reset CurContext to the nearest enclosing context. |
| 208 | while (!S->getEntity() && S->getParent()) |
| 209 | S = S->getParent(); |
| 210 | CurContext = static_cast<DeclContext*>(S->getEntity()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 211 | assert(CurContext && "No context?"); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 212 | } |