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