blob: 5fb2740fcf31a6b12e059c87f7b9ac26ea60d5c4 [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;
Steve Naroff0701bbb2009-01-08 17:28:14 +000029 for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I) {
Douglas Gregor44b43212008-12-11 16:49:14 +000030 IdIsUndeclared = false;
Douglas Gregor3dde5a32008-12-16 06:37:47 +000031 if (((*I)->getIdentifierNamespace() & Decl::IDNS_Tag) ||
32 isa<TypedefDecl>(*I))
Douglas Gregore267ff32008-12-11 20:41:00 +000033 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) {
Douglas Gregor3dde5a32008-12-16 06:37:47 +000059 if (isa<TypedefDecl>(*I)) {
60 break;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000061 }
Douglas Gregor3dde5a32008-12-16 06:37:47 +000062 if (((*I)->getIdentifierNamespace() & Decl::IDNS_Tag))
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000063 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 ('::').
72Sema::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.
83Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
84 const CXXScopeSpec &SS,
85 SourceLocation IdLoc,
86 SourceLocation CCLoc,
Douglas Gregor2def4832008-11-17 20:34:05 +000087 IdentifierInfo &II) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000088 DeclContext *DC = static_cast<DeclContext*>(SS.getScopeRep());
89 Decl *SD;
90 bool IdIsUndeclared;
91
92 if (DC)
Douglas Gregor44b43212008-12-11 16:49:14 +000093 SD = LookupNestedName(DC, false/*LookInParentCtx*/, &II, IdIsUndeclared,
94 Context);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000095 else
Douglas Gregor2def4832008-11-17 20:34:05 +000096 SD = LookupNestedName(CurContext, true/*LookInParent*/, &II,
Douglas Gregor44b43212008-12-11 16:49:14 +000097 IdIsUndeclared, Context);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000098
99 if (SD) {
100 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
Douglas Gregor3dde5a32008-12-16 06:37:47 +0000101 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 Kyrtzidisef6e6472008-11-08 17:17:31 +0000105 }
106
Douglas Gregor3dde5a32008-12-16 06:37:47 +0000107 // Fall through to produce an error: we found something that isn't
108 // a class or a namespace.
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000109 }
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?");
Douglas Gregor0a59acb2008-12-16 00:38:16 +0000136 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
Douglas Gregor72b505b2008-12-16 21:30:33 +0000137 CurContext = static_cast<DeclContext*>(SS.getScopeRep());
138 S->setEntity(CurContext);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000139}
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.
Douglas Gregor0a59acb2008-12-16 00:38:16 +0000146void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000147 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor0a59acb2008-12-16 00:38:16 +0000148 assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
149 S->setEntity(PreDeclaratorDC);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000150 PreDeclaratorDC = 0;
Douglas Gregor72b505b2008-12-16 21:30:33 +0000151
152 // Reset CurContext to the nearest enclosing context.
153 while (!S->getEntity() && S->getParent())
154 S = S->getParent();
155 CurContext = static_cast<DeclContext*>(S->getEntity());
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000156}