Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +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 "clang/Basic/Diagnostic.h"
|
| 18 | using namespace clang;
|
| 19 |
|
| 20 |
|
| 21 | namespace {
|
| 22 | Decl *LookupNestedName(DeclContext *LookupCtx, bool LookInParentCtx,
|
| 23 | const IdentifierInfo &II, bool &IdIsUndeclared) {
|
| 24 | IdentifierResolver::iterator
|
| 25 | I = IdentifierResolver::begin(&II, LookupCtx, LookInParentCtx),
|
| 26 | E = IdentifierResolver::end();
|
| 27 |
|
| 28 | if (I == E) {
|
| 29 | IdIsUndeclared = true;
|
| 30 | return 0;
|
| 31 | }
|
| 32 | IdIsUndeclared = false;
|
| 33 |
|
| 34 | // C++ 3.4.3p1 :
|
| 35 | // During the lookup for a name preceding the :: scope resolution operator,
|
| 36 | // object, function, and enumerator names are ignored. If the name found is
|
| 37 | // not a class-name or namespace-name, the program is ill-formed.
|
| 38 |
|
| 39 | for (; I != E; ++I) {
|
| 40 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(*I)) {
|
| 41 | if (TD->getUnderlyingType()->isRecordType())
|
| 42 | break;
|
| 43 | continue;
|
| 44 | }
|
| 45 | if (((*I)->getIdentifierNamespace()&Decl::IDNS_Tag) && !isa<EnumDecl>(*I))
|
| 46 | break;
|
| 47 | }
|
| 48 |
|
| 49 | return (I != E ? *I : 0);
|
| 50 | }
|
| 51 | } // anonymous namespace
|
| 52 |
|
| 53 | /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
|
| 54 | /// global scope ('::').
|
| 55 | Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
|
| 56 | SourceLocation CCLoc) {
|
| 57 | return cast<DeclContext>(Context.getTranslationUnitDecl());
|
| 58 | }
|
| 59 |
|
| 60 | /// ActOnCXXNestedNameSpecifier - Called during parsing of a
|
| 61 | /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
|
| 62 | /// we want to resolve "bar::". 'SS' is empty or the previously parsed
|
| 63 | /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
|
| 64 | /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
|
| 65 | /// Returns a CXXScopeTy* object representing the C++ scope.
|
| 66 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
|
| 67 | const CXXScopeSpec &SS,
|
| 68 | SourceLocation IdLoc,
|
| 69 | SourceLocation CCLoc,
|
| 70 | const IdentifierInfo &II) {
|
| 71 | DeclContext *DC = static_cast<DeclContext*>(SS.getScopeRep());
|
| 72 | Decl *SD;
|
| 73 | bool IdIsUndeclared;
|
| 74 |
|
| 75 | if (DC)
|
| 76 | SD = LookupNestedName(DC, false/*LookInParentCtx*/, II, IdIsUndeclared);
|
| 77 | else
|
| 78 | SD = LookupNestedName(CurContext, true/*LookInParent*/, II, IdIsUndeclared);
|
| 79 |
|
| 80 | if (SD) {
|
| 81 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
|
| 82 | assert(TD->getUnderlyingType()->isRecordType() &&"Invalid Scope Decl!");
|
| 83 | SD = TD->getUnderlyingType()->getAsRecordType()->getDecl();
|
| 84 | }
|
| 85 |
|
| 86 | assert((isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) &&
|
| 87 | "Invalid Scope Decl!");
|
| 88 | return cast<DeclContext>(SD);
|
| 89 | }
|
| 90 |
|
| 91 | unsigned DiagID;
|
| 92 | if (!IdIsUndeclared)
|
| 93 | DiagID = diag::err_expected_class_or_namespace;
|
| 94 | else if (DC)
|
| 95 | DiagID = diag::err_typecheck_no_member;
|
| 96 | else
|
| 97 | DiagID = diag::err_undeclared_var_use;
|
| 98 |
|
| 99 | if (DC)
|
| 100 | Diag(IdLoc, DiagID, II.getName(), SS.getRange());
|
| 101 | else
|
| 102 | Diag(IdLoc, DiagID, II.getName());
|
| 103 |
|
| 104 | return 0;
|
| 105 | }
|
| 106 |
|
| 107 | /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
|
| 108 | /// scope or nested-name-specifier) is parsed, part of a declarator-id.
|
| 109 | /// After this method is called, according to [C++ 3.4.3p3], names should be
|
| 110 | /// looked up in the declarator-id's scope, until the declarator is parsed and
|
| 111 | /// ActOnCXXExitDeclaratorScope is called.
|
| 112 | /// The 'SS' should be a non-empty valid CXXScopeSpec.
|
| 113 | void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
| 114 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
| 115 | assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
|
| 116 | PreDeclaratorDC = CurContext;
|
| 117 | CurContext = static_cast<DeclContext*>(SS.getScopeRep());
|
| 118 | }
|
| 119 |
|
| 120 | /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
|
| 121 | /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
|
| 122 | /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
|
| 123 | /// Used to indicate that names should revert to being looked up in the
|
| 124 | /// defining scope.
|
| 125 | void Sema::ActOnCXXExitDeclaratorScope(const CXXScopeSpec &SS) {
|
| 126 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
| 127 | assert(CurContext == static_cast<DeclContext*>(SS.getScopeRep()) &&
|
| 128 | "Context imbalance!");
|
| 129 | CurContext = PreDeclaratorDC;
|
| 130 | PreDeclaratorDC = 0;
|
| 131 | }
|