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