blob: c77325f42d877f7b9d9877721c9d2c4ba014b3a0 [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"
Douglas Gregore267ff32008-12-11 20:41:00 +000017#include "llvm/ADT/STLExtras.h"
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000018using namespace clang;
19
20
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000021/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
22/// global scope ('::').
23Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
24 SourceLocation CCLoc) {
25 return cast<DeclContext>(Context.getTranslationUnitDecl());
26}
27
28/// ActOnCXXNestedNameSpecifier - Called during parsing of a
29/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
30/// we want to resolve "bar::". 'SS' is empty or the previously parsed
31/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
32/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
33/// Returns a CXXScopeTy* object representing the C++ scope.
34Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
35 const CXXScopeSpec &SS,
36 SourceLocation IdLoc,
37 SourceLocation CCLoc,
Douglas Gregor2def4832008-11-17 20:34:05 +000038 IdentifierInfo &II) {
Douglas Gregor4c921ae2009-01-30 01:04:22 +000039 Decl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000040
41 if (SD) {
42 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
Douglas Gregor3dde5a32008-12-16 06:37:47 +000043 if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType())
44 return cast<DeclContext>(Record->getDecl());
45 } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
46 return cast<DeclContext>(SD);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000047 }
48
Sebastian Redlf30208a2009-01-24 21:16:55 +000049 // FIXME: Template parameters and dependent types.
Douglas Gregoreb11cd02009-01-14 22:20:51 +000050 // FIXME: C++0x scoped enums
51
Douglas Gregor3dde5a32008-12-16 06:37:47 +000052 // Fall through to produce an error: we found something that isn't
53 // a class or a namespace.
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000054 }
55
Douglas Gregoreb11cd02009-01-14 22:20:51 +000056 // If we didn't find anything during our lookup, try again with
57 // ordinary name lookup, which can help us produce better error
58 // messages.
59 if (!SD)
Douglas Gregor4c921ae2009-01-30 01:04:22 +000060 SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000061 unsigned DiagID;
Douglas Gregoreb11cd02009-01-14 22:20:51 +000062 if (SD)
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000063 DiagID = diag::err_expected_class_or_namespace;
Douglas Gregoreb11cd02009-01-14 22:20:51 +000064 else if (SS.isSet())
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000065 DiagID = diag::err_typecheck_no_member;
66 else
67 DiagID = diag::err_undeclared_var_use;
68
Douglas Gregoreb11cd02009-01-14 22:20:51 +000069 if (SS.isSet())
Chris Lattner3c73c412008-11-19 08:23:25 +000070 Diag(IdLoc, DiagID) << &II << SS.getRange();
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000071 else
Chris Lattner3c73c412008-11-19 08:23:25 +000072 Diag(IdLoc, DiagID) << &II;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000073
74 return 0;
75}
76
77/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
78/// scope or nested-name-specifier) is parsed, part of a declarator-id.
79/// After this method is called, according to [C++ 3.4.3p3], names should be
80/// looked up in the declarator-id's scope, until the declarator is parsed and
81/// ActOnCXXExitDeclaratorScope is called.
82/// The 'SS' should be a non-empty valid CXXScopeSpec.
83void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
84 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
85 assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
Douglas Gregor0a59acb2008-12-16 00:38:16 +000086 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
Douglas Gregor72b505b2008-12-16 21:30:33 +000087 CurContext = static_cast<DeclContext*>(SS.getScopeRep());
88 S->setEntity(CurContext);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000089}
90
91/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
92/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
93/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
94/// Used to indicate that names should revert to being looked up in the
95/// defining scope.
Douglas Gregor0a59acb2008-12-16 00:38:16 +000096void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000097 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor0a59acb2008-12-16 00:38:16 +000098 assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
99 S->setEntity(PreDeclaratorDC);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000100 PreDeclaratorDC = 0;
Douglas Gregor72b505b2008-12-16 21:30:33 +0000101
102 // Reset CurContext to the nearest enclosing context.
103 while (!S->getEntity() && S->getParent())
104 S = S->getParent();
105 CurContext = static_cast<DeclContext*>(S->getEntity());
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000106}