blob: f22ca0c712cbfd84cd36ab620a8db496475b5436 [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 Gregor2def4832008-11-17 20:34:05 +000023 DeclarationName Name, bool &IdIsUndeclared) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000024 IdentifierResolver::iterator
Douglas Gregor2def4832008-11-17 20:34:05 +000025 I = IdentifierResolver::begin(Name, LookupCtx, LookInParentCtx),
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000026 E = IdentifierResolver::end();
27
28 if (I == E) {
29 IdIsUndeclared = true;
30 return 0;
31 }
32 IdIsUndeclared = false;
33
34 // C++ 3.4.3p1 :
35 // During the lookup for a name preceding the :: scope resolution operator,
36 // object, function, and enumerator names are ignored. If the name found is
37 // not a class-name or namespace-name, the program is ill-formed.
38
39 for (; I != E; ++I) {
40 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(*I)) {
41 if (TD->getUnderlyingType()->isRecordType())
42 break;
43 continue;
44 }
45 if (((*I)->getIdentifierNamespace()&Decl::IDNS_Tag) && !isa<EnumDecl>(*I))
46 break;
47 }
48
49 return (I != E ? *I : 0);
50 }
51} // anonymous namespace
52
53/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
54/// global scope ('::').
55Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
56 SourceLocation CCLoc) {
57 return cast<DeclContext>(Context.getTranslationUnitDecl());
58}
59
60/// ActOnCXXNestedNameSpecifier - Called during parsing of a
61/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
62/// we want to resolve "bar::". 'SS' is empty or the previously parsed
63/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
64/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
65/// Returns a CXXScopeTy* object representing the C++ scope.
66Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
67 const CXXScopeSpec &SS,
68 SourceLocation IdLoc,
69 SourceLocation CCLoc,
Douglas Gregor2def4832008-11-17 20:34:05 +000070 IdentifierInfo &II) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000071 DeclContext *DC = static_cast<DeclContext*>(SS.getScopeRep());
72 Decl *SD;
73 bool IdIsUndeclared;
74
75 if (DC)
Douglas Gregor2def4832008-11-17 20:34:05 +000076 SD = LookupNestedName(DC, false/*LookInParentCtx*/, &II, IdIsUndeclared);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000077 else
Douglas Gregor2def4832008-11-17 20:34:05 +000078 SD = LookupNestedName(CurContext, true/*LookInParent*/, &II,
79 IdIsUndeclared);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000080
81 if (SD) {
82 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
83 assert(TD->getUnderlyingType()->isRecordType() &&"Invalid Scope Decl!");
84 SD = TD->getUnderlyingType()->getAsRecordType()->getDecl();
85 }
86
87 assert((isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) &&
88 "Invalid Scope Decl!");
89 return cast<DeclContext>(SD);
90 }
91
92 unsigned DiagID;
93 if (!IdIsUndeclared)
94 DiagID = diag::err_expected_class_or_namespace;
95 else if (DC)
96 DiagID = diag::err_typecheck_no_member;
97 else
98 DiagID = diag::err_undeclared_var_use;
99
100 if (DC)
101 Diag(IdLoc, DiagID, II.getName(), SS.getRange());
102 else
103 Diag(IdLoc, DiagID, II.getName());
104
105 return 0;
106}
107
108/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
109/// scope or nested-name-specifier) is parsed, part of a declarator-id.
110/// After this method is called, according to [C++ 3.4.3p3], names should be
111/// looked up in the declarator-id's scope, until the declarator is parsed and
112/// ActOnCXXExitDeclaratorScope is called.
113/// The 'SS' should be a non-empty valid CXXScopeSpec.
114void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
115 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
116 assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
117 PreDeclaratorDC = CurContext;
118 CurContext = static_cast<DeclContext*>(SS.getScopeRep());
119}
120
121/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
122/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
123/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
124/// Used to indicate that names should revert to being looked up in the
125/// defining scope.
126void Sema::ActOnCXXExitDeclaratorScope(const CXXScopeSpec &SS) {
127 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
128 assert(CurContext == static_cast<DeclContext*>(SS.getScopeRep()) &&
129 "Context imbalance!");
130 CurContext = PreDeclaratorDC;
131 PreDeclaratorDC = 0;
132}