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