blob: cf93571eb231958668ab3f1b71e46103b1f840f7 [file] [log] [blame]
Argiris Kirtzidis054a2632008-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 Gregor39677622008-12-11 20:41:00 +000017#include "llvm/ADT/STLExtras.h"
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000018using namespace clang;
19
20
Argiris Kirtzidis054a2632008-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 Gregorb0212bd2008-11-17 20:34:05 +000038 IdentifierInfo &II) {
Douglas Gregor78d70132009-01-14 22:20:51 +000039 Decl *SD = LookupParsedName(S, SS, &II,
40 LookupCriteria(LookupCriteria::NestedNameSpecifier,
41 /*RedeclarationOnly=*/false,
42 /*CPlusPlus=*/true));
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000043
44 if (SD) {
45 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
Douglas Gregorb6bf5442008-12-16 06:37:47 +000046 if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType())
47 return cast<DeclContext>(Record->getDecl());
48 } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
49 return cast<DeclContext>(SD);
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000050 }
51
Sebastian Redl75555032009-01-24 21:16:55 +000052 // FIXME: Template parameters and dependent types.
Douglas Gregor78d70132009-01-14 22:20:51 +000053 // FIXME: C++0x scoped enums
54
Douglas Gregorb6bf5442008-12-16 06:37:47 +000055 // Fall through to produce an error: we found something that isn't
56 // a class or a namespace.
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000057 }
58
Douglas Gregor78d70132009-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));
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000067 unsigned DiagID;
Douglas Gregor78d70132009-01-14 22:20:51 +000068 if (SD)
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000069 DiagID = diag::err_expected_class_or_namespace;
Douglas Gregor78d70132009-01-14 22:20:51 +000070 else if (SS.isSet())
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000071 DiagID = diag::err_typecheck_no_member;
72 else
73 DiagID = diag::err_undeclared_var_use;
74
Douglas Gregor78d70132009-01-14 22:20:51 +000075 if (SS.isSet())
Chris Lattner65cae292008-11-19 08:23:25 +000076 Diag(IdLoc, DiagID) << &II << SS.getRange();
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000077 else
Chris Lattner65cae292008-11-19 08:23:25 +000078 Diag(IdLoc, DiagID) << &II;
Argiris Kirtzidis054a2632008-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 Gregor24effa82008-12-16 00:38:16 +000092 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
Douglas Gregor605de8d2008-12-16 21:30:33 +000093 CurContext = static_cast<DeclContext*>(SS.getScopeRep());
94 S->setEntity(CurContext);
Argiris Kirtzidis054a2632008-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 Gregor24effa82008-12-16 00:38:16 +0000102void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
Argiris Kirtzidis054a2632008-11-08 17:17:31 +0000103 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor24effa82008-12-16 00:38:16 +0000104 assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
105 S->setEntity(PreDeclaratorDC);
Argiris Kirtzidis054a2632008-11-08 17:17:31 +0000106 PreDeclaratorDC = 0;
Douglas Gregor605de8d2008-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());
Argiris Kirtzidis054a2632008-11-08 17:17:31 +0000112}