blob: d01552c373b8ca617c7bfdf7fd9418c7a344fca0 [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"
18using namespace clang;
19
20
21namespace {
22 Decl *LookupNestedName(DeclContext *LookupCtx, bool LookInParentCtx,
Douglas Gregor44b43212008-12-11 16:49:14 +000023 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 Kyrtzidisef6e6472008-11-08 17:17:31 +000042 IdentifierResolver::iterator
Douglas Gregor2def4832008-11-17 20:34:05 +000043 I = IdentifierResolver::begin(Name, LookupCtx, LookInParentCtx),
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000044 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 ('::').
73Sema::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.
84Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
85 const CXXScopeSpec &SS,
86 SourceLocation IdLoc,
87 SourceLocation CCLoc,
Douglas Gregor2def4832008-11-17 20:34:05 +000088 IdentifierInfo &II) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000089 DeclContext *DC = static_cast<DeclContext*>(SS.getScopeRep());
90 Decl *SD;
91 bool IdIsUndeclared;
92
93 if (DC)
Douglas Gregor44b43212008-12-11 16:49:14 +000094 SD = LookupNestedName(DC, false/*LookInParentCtx*/, &II, IdIsUndeclared,
95 Context);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000096 else
Douglas Gregor2def4832008-11-17 20:34:05 +000097 SD = LookupNestedName(CurContext, true/*LookInParent*/, &II,
Douglas Gregor44b43212008-12-11 16:49:14 +000098 IdIsUndeclared, Context);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000099
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 Lattner3c73c412008-11-19 08:23:25 +0000120 Diag(IdLoc, DiagID) << &II << SS.getRange();
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000121 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000122 Diag(IdLoc, DiagID) << &II;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000123
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.
133void 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.
145void 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}