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 | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame^] | 31 | if (((*I)->getIdentifierNamespace() & Decl::IDNS_Tag) ||
|
| 32 | isa<TypedefDecl>(*I))
|
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 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) {
|
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame^] | 59 | if (isa<TypedefDecl>(*I)) {
|
| 60 | break;
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 61 | }
|
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame^] | 62 | if (((*I)->getIdentifierNamespace() & Decl::IDNS_Tag))
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 63 | break;
|
| 64 | }
|
| 65 |
|
| 66 | return (I != E ? *I : 0);
|
| 67 | }
|
| 68 | } // anonymous namespace
|
| 69 |
|
| 70 | /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
|
| 71 | /// global scope ('::').
|
| 72 | Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
|
| 73 | SourceLocation CCLoc) {
|
| 74 | return cast<DeclContext>(Context.getTranslationUnitDecl());
|
| 75 | }
|
| 76 |
|
| 77 | /// ActOnCXXNestedNameSpecifier - Called during parsing of a
|
| 78 | /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
|
| 79 | /// we want to resolve "bar::". 'SS' is empty or the previously parsed
|
| 80 | /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
|
| 81 | /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
|
| 82 | /// Returns a CXXScopeTy* object representing the C++ scope.
|
| 83 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
|
| 84 | const CXXScopeSpec &SS,
|
| 85 | SourceLocation IdLoc,
|
| 86 | SourceLocation CCLoc,
|
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 87 | IdentifierInfo &II) {
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 88 | DeclContext *DC = static_cast<DeclContext*>(SS.getScopeRep());
|
| 89 | Decl *SD;
|
| 90 | bool IdIsUndeclared;
|
| 91 |
|
| 92 | if (DC)
|
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 93 | SD = LookupNestedName(DC, false/*LookInParentCtx*/, &II, IdIsUndeclared,
|
| 94 | Context);
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 95 | else
|
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 96 | SD = LookupNestedName(CurContext, true/*LookInParent*/, &II,
|
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 97 | IdIsUndeclared, Context);
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 98 |
|
| 99 | if (SD) {
|
| 100 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
|
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame^] | 101 | if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType())
|
| 102 | return cast<DeclContext>(Record->getDecl());
|
| 103 | } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
|
| 104 | return cast<DeclContext>(SD);
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 105 | }
|
| 106 |
|
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame^] | 107 | // Fall through to produce an error: we found something that isn't
|
| 108 | // a class or a namespace.
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 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?");
|
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 136 | PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
|
| 137 | S->setEntity(static_cast<DeclContext*>(SS.getScopeRep()));
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 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.
|
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 145 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 146 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 147 | assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
|
| 148 | S->setEntity(PreDeclaratorDC);
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 149 | PreDeclaratorDC = 0;
|
| 150 | }
|