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 | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 27 | NestedNameSpecifier NNS |
| 28 | = NestedNameSpecifier::getFromOpaquePtr(SS.getCurrentScopeRep()); |
| 29 | return NNS.computeDeclContext(Context); |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame^] | 32 | bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { |
| 33 | if (!SS.isSet() || SS.isInvalid()) |
| 34 | return false; |
| 35 | |
| 36 | NestedNameSpecifier NNS |
| 37 | = NestedNameSpecifier::getFromOpaquePtr(SS.getCurrentScopeRep()); |
| 38 | |
| 39 | if (Type *T = NNS.getAsType()) |
| 40 | return T->isDependentType(); |
| 41 | |
| 42 | // FIXME: What about the injected-class-name of a class template? It |
| 43 | // is dependent, but we represent it as a declaration. |
| 44 | return false; |
| 45 | } |
| 46 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 47 | /// \brief Require that the context specified by SS be complete. |
| 48 | /// |
| 49 | /// If SS refers to a type, this routine checks whether the type is |
| 50 | /// complete enough (or can be made complete enough) for name lookup |
| 51 | /// into the DeclContext. A type that is not yet completed can be |
| 52 | /// considered "complete enough" if it is a class/struct/union/enum |
| 53 | /// that is currently being defined. Or, if we have a type that names |
| 54 | /// a class template specialization that is not a complete type, we |
| 55 | /// will attempt to instantiate that class template. |
| 56 | bool Sema::RequireCompleteDeclContext(const CXXScopeSpec &SS) { |
| 57 | if (!SS.isSet() || SS.isInvalid()) |
| 58 | return false; |
| 59 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 60 | DeclContext *DC = computeDeclContext(SS); |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 61 | if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
| 62 | // If we're currently defining this type, then lookup into the |
| 63 | // type is okay: don't complain that it isn't complete yet. |
| 64 | const TagType *TagT = Context.getTypeDeclType(Tag)->getAsTagType(); |
| 65 | if (TagT->isBeingDefined()) |
| 66 | return false; |
| 67 | |
| 68 | // The type must be complete. |
| 69 | return RequireCompleteType(SS.getRange().getBegin(), |
| 70 | Context.getTypeDeclType(Tag), |
| 71 | diag::err_incomplete_nested_name_spec, |
| 72 | SS.getRange()); |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 77 | |
| 78 | /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the |
| 79 | /// global scope ('::'). |
| 80 | Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, |
| 81 | SourceLocation CCLoc) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 82 | return NestedNameSpecifier(Context.getTranslationUnitDecl()).getAsOpaquePtr(); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /// ActOnCXXNestedNameSpecifier - Called during parsing of a |
| 86 | /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now |
| 87 | /// we want to resolve "bar::". 'SS' is empty or the previously parsed |
| 88 | /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', |
| 89 | /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. |
| 90 | /// Returns a CXXScopeTy* object representing the C++ scope. |
| 91 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 92 | const CXXScopeSpec &SS, |
| 93 | SourceLocation IdLoc, |
| 94 | SourceLocation CCLoc, |
| 95 | IdentifierInfo &II) { |
| 96 | NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName); |
| 97 | |
| 98 | if (SD) { |
| 99 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 100 | if (TD->getUnderlyingType()->isRecordType()) |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 101 | return NestedNameSpecifier(Context.getTypeDeclType(TD).getTypePtr()) |
| 102 | .getAsOpaquePtr(); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 103 | } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 104 | return NestedNameSpecifier(cast<DeclContext>(SD)).getAsOpaquePtr(); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // FIXME: Template parameters and dependent types. |
| 108 | // FIXME: C++0x scoped enums |
| 109 | |
| 110 | // Fall through to produce an error: we found something that isn't |
| 111 | // a class or a namespace. |
| 112 | } |
| 113 | |
| 114 | // If we didn't find anything during our lookup, try again with |
| 115 | // ordinary name lookup, which can help us produce better error |
| 116 | // messages. |
| 117 | if (!SD) |
| 118 | SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName); |
| 119 | unsigned DiagID; |
| 120 | if (SD) |
| 121 | DiagID = diag::err_expected_class_or_namespace; |
| 122 | else if (SS.isSet()) |
| 123 | DiagID = diag::err_typecheck_no_member; |
| 124 | else |
| 125 | DiagID = diag::err_undeclared_var_use; |
| 126 | |
| 127 | if (SS.isSet()) |
| 128 | Diag(IdLoc, DiagID) << &II << SS.getRange(); |
| 129 | else |
| 130 | Diag(IdLoc, DiagID) << &II; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 135 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 136 | const CXXScopeSpec &SS, |
| 137 | TypeTy *Ty, |
| 138 | SourceRange TypeRange, |
| 139 | SourceLocation CCLoc) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 140 | return NestedNameSpecifier(QualType::getFromOpaquePtr(Ty).getTypePtr()) |
| 141 | .getAsOpaquePtr(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 144 | /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global |
| 145 | /// scope or nested-name-specifier) is parsed, part of a declarator-id. |
| 146 | /// After this method is called, according to [C++ 3.4.3p3], names should be |
| 147 | /// looked up in the declarator-id's scope, until the declarator is parsed and |
| 148 | /// ActOnCXXExitDeclaratorScope is called. |
| 149 | /// The 'SS' should be a non-empty valid CXXScopeSpec. |
| 150 | void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 151 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
| 152 | assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?"); |
| 153 | PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity()); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 154 | CurContext = computeDeclContext(SS); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 155 | S->setEntity(CurContext); |
| 156 | } |
| 157 | |
| 158 | /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously |
| 159 | /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same |
| 160 | /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. |
| 161 | /// Used to indicate that names should revert to being looked up in the |
| 162 | /// defining scope. |
| 163 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 164 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 165 | assert(S->getEntity() == computeDeclContext(SS) && "Context imbalance!"); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 166 | S->setEntity(PreDeclaratorDC); |
| 167 | PreDeclaratorDC = 0; |
| 168 | |
| 169 | // Reset CurContext to the nearest enclosing context. |
| 170 | while (!S->getEntity() && S->getParent()) |
| 171 | S = S->getParent(); |
| 172 | CurContext = static_cast<DeclContext*>(S->getEntity()); |
| 173 | } |