blob: 0c0fa35a5e763c5ad99b4deef6fc733c3fcc6ee7 [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
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000022/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
23/// global scope ('::').
24Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
25 SourceLocation CCLoc) {
26 return cast<DeclContext>(Context.getTranslationUnitDecl());
27}
28
29/// ActOnCXXNestedNameSpecifier - Called during parsing of a
30/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
31/// we want to resolve "bar::". 'SS' is empty or the previously parsed
32/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
33/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
34/// Returns a CXXScopeTy* object representing the C++ scope.
35Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
36 const CXXScopeSpec &SS,
37 SourceLocation IdLoc,
38 SourceLocation CCLoc,
Douglas Gregor2def4832008-11-17 20:34:05 +000039 IdentifierInfo &II) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +000040 Decl *SD = LookupParsedName(S, SS, &II,
41 LookupCriteria(LookupCriteria::NestedNameSpecifier,
42 /*RedeclarationOnly=*/false,
43 /*CPlusPlus=*/true));
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000044
45 if (SD) {
46 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
Douglas Gregor3dde5a32008-12-16 06:37:47 +000047 if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType())
48 return cast<DeclContext>(Record->getDecl());
49 } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
50 return cast<DeclContext>(SD);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000051 }
52
Douglas Gregoreb11cd02009-01-14 22:20:51 +000053 // FIXME: C++0x scoped enums
54
Douglas Gregor3dde5a32008-12-16 06:37:47 +000055 // Fall through to produce an error: we found something that isn't
56 // a class or a namespace.
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000057 }
58
Douglas Gregoreb11cd02009-01-14 22:20:51 +000059 // If we didn't find anything during our lookup, try again with
60 // ordinary name lookup, which can help us produce better error
61 // messages.
62 if (!SD)
63 SD = LookupParsedName(S, SS, &II,
64 LookupCriteria(LookupCriteria::Ordinary,
65 /*RedeclarationOnly=*/false,
66 /*CPlusPlus=*/true));
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000067 unsigned DiagID;
Douglas Gregoreb11cd02009-01-14 22:20:51 +000068 if (SD)
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000069 DiagID = diag::err_expected_class_or_namespace;
Douglas Gregoreb11cd02009-01-14 22:20:51 +000070 else if (SS.isSet())
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000071 DiagID = diag::err_typecheck_no_member;
72 else
73 DiagID = diag::err_undeclared_var_use;
74
Douglas Gregoreb11cd02009-01-14 22:20:51 +000075 if (SS.isSet())
Chris Lattner3c73c412008-11-19 08:23:25 +000076 Diag(IdLoc, DiagID) << &II << SS.getRange();
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000077 else
Chris Lattner3c73c412008-11-19 08:23:25 +000078 Diag(IdLoc, DiagID) << &II;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000079
80 return 0;
81}
82
83/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
84/// scope or nested-name-specifier) is parsed, part of a declarator-id.
85/// After this method is called, according to [C++ 3.4.3p3], names should be
86/// looked up in the declarator-id's scope, until the declarator is parsed and
87/// ActOnCXXExitDeclaratorScope is called.
88/// The 'SS' should be a non-empty valid CXXScopeSpec.
89void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
90 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
91 assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
Douglas Gregor0a59acb2008-12-16 00:38:16 +000092 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
Douglas Gregor72b505b2008-12-16 21:30:33 +000093 CurContext = static_cast<DeclContext*>(SS.getScopeRep());
94 S->setEntity(CurContext);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000095}
96
97/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
98/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
99/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
100/// Used to indicate that names should revert to being looked up in the
101/// defining scope.
Douglas Gregor0a59acb2008-12-16 00:38:16 +0000102void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000103 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor0a59acb2008-12-16 00:38:16 +0000104 assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
105 S->setEntity(PreDeclaratorDC);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000106 PreDeclaratorDC = 0;
Douglas Gregor72b505b2008-12-16 21:30:33 +0000107
108 // Reset CurContext to the nearest enclosing context.
109 while (!S->getEntity() && S->getParent())
110 S = S->getParent();
111 CurContext = static_cast<DeclContext*>(S->getEntity());
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000112}