blob: 193e259f1da41a73e0aa498fcd3cf2ecdc64928b [file] [log] [blame]
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001//===--- 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 Gregore267ff32008-12-11 20:41:00 +000018#include "llvm/ADT/STLExtras.h"
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000019using namespace clang;
20
21
22namespace {
23 Decl *LookupNestedName(DeclContext *LookupCtx, bool LookInParentCtx,
Douglas Gregor44b43212008-12-11 16:49:14 +000024 DeclarationName Name, bool &IdIsUndeclared,
Douglas Gregore267ff32008-12-11 20:41:00 +000025 ASTContext &Context) {
Douglas Gregor44b43212008-12-11 16:49:14 +000026 if (LookupCtx && !LookInParentCtx) {
27 IdIsUndeclared = true;
Douglas Gregore267ff32008-12-11 20:41:00 +000028 DeclContext::lookup_const_iterator I, E;
29 for (llvm::tie(I, E) = LookupCtx->lookup(Context, Name); I != E; ++I) {
Douglas Gregor44b43212008-12-11 16:49:14 +000030 IdIsUndeclared = false;
Douglas Gregore267ff32008-12-11 20:41:00 +000031 if (((*I)->getIdentifierNamespace() & Decl::IDNS_Tag) &&
32 !isa<EnumDecl>(*I))
33 return *I;
Douglas Gregor44b43212008-12-11 16:49:14 +000034 }
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 Kyrtzidisef6e6472008-11-08 17:17:31 +000043 IdentifierResolver::iterator
Douglas Gregor2def4832008-11-17 20:34:05 +000044 I = IdentifierResolver::begin(Name, LookupCtx, LookInParentCtx),
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000045 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 ('::').
74Sema::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.
85Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
86 const CXXScopeSpec &SS,
87 SourceLocation IdLoc,
88 SourceLocation CCLoc,
Douglas Gregor2def4832008-11-17 20:34:05 +000089 IdentifierInfo &II) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000090 DeclContext *DC = static_cast<DeclContext*>(SS.getScopeRep());
91 Decl *SD;
92 bool IdIsUndeclared;
93
94 if (DC)
Douglas Gregor44b43212008-12-11 16:49:14 +000095 SD = LookupNestedName(DC, false/*LookInParentCtx*/, &II, IdIsUndeclared,
96 Context);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000097 else
Douglas Gregor2def4832008-11-17 20:34:05 +000098 SD = LookupNestedName(CurContext, true/*LookInParent*/, &II,
Douglas Gregor44b43212008-12-11 16:49:14 +000099 IdIsUndeclared, Context);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000100
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 Lattner3c73c412008-11-19 08:23:25 +0000121 Diag(IdLoc, DiagID) << &II << SS.getRange();
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000122 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000123 Diag(IdLoc, DiagID) << &II;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000124
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.
134void 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.
146void 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}