blob: 699f8908079cfa799d4b055751ef11df77541a20 [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"
17#include "clang/Basic/Diagnostic.h"
Douglas Gregor39677622008-12-11 20:41:00 +000018#include "llvm/ADT/STLExtras.h"
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000019using namespace clang;
20
21
Argiris Kirtzidis054a2632008-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 Gregorb0212bd2008-11-17 20:34:05 +000039 IdentifierInfo &II) {
Douglas Gregor78d70132009-01-14 22:20:51 +000040 Decl *SD = LookupParsedName(S, SS, &II,
41 LookupCriteria(LookupCriteria::NestedNameSpecifier,
42 /*RedeclarationOnly=*/false,
43 /*CPlusPlus=*/true));
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000044
45 if (SD) {
46 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
Douglas Gregorb6bf5442008-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);
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000051 }
52
Sebastian Redl75555032009-01-24 21:16:55 +000053 // FIXME: Template parameters and dependent types.
Douglas Gregor78d70132009-01-14 22:20:51 +000054 // FIXME: C++0x scoped enums
55
Douglas Gregorb6bf5442008-12-16 06:37:47 +000056 // Fall through to produce an error: we found something that isn't
57 // a class or a namespace.
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000058 }
59
Douglas Gregor78d70132009-01-14 22:20:51 +000060 // If we didn't find anything during our lookup, try again with
61 // ordinary name lookup, which can help us produce better error
62 // messages.
63 if (!SD)
64 SD = LookupParsedName(S, SS, &II,
65 LookupCriteria(LookupCriteria::Ordinary,
66 /*RedeclarationOnly=*/false,
67 /*CPlusPlus=*/true));
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000068 unsigned DiagID;
Douglas Gregor78d70132009-01-14 22:20:51 +000069 if (SD)
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000070 DiagID = diag::err_expected_class_or_namespace;
Douglas Gregor78d70132009-01-14 22:20:51 +000071 else if (SS.isSet())
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000072 DiagID = diag::err_typecheck_no_member;
73 else
74 DiagID = diag::err_undeclared_var_use;
75
Douglas Gregor78d70132009-01-14 22:20:51 +000076 if (SS.isSet())
Chris Lattner65cae292008-11-19 08:23:25 +000077 Diag(IdLoc, DiagID) << &II << SS.getRange();
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000078 else
Chris Lattner65cae292008-11-19 08:23:25 +000079 Diag(IdLoc, DiagID) << &II;
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000080
81 return 0;
82}
83
84/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
85/// scope or nested-name-specifier) is parsed, part of a declarator-id.
86/// After this method is called, according to [C++ 3.4.3p3], names should be
87/// looked up in the declarator-id's scope, until the declarator is parsed and
88/// ActOnCXXExitDeclaratorScope is called.
89/// The 'SS' should be a non-empty valid CXXScopeSpec.
90void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
91 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
92 assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
Douglas Gregor24effa82008-12-16 00:38:16 +000093 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
Douglas Gregor605de8d2008-12-16 21:30:33 +000094 CurContext = static_cast<DeclContext*>(SS.getScopeRep());
95 S->setEntity(CurContext);
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000096}
97
98/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
99/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
100/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
101/// Used to indicate that names should revert to being looked up in the
102/// defining scope.
Douglas Gregor24effa82008-12-16 00:38:16 +0000103void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
Argiris Kirtzidis054a2632008-11-08 17:17:31 +0000104 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor24effa82008-12-16 00:38:16 +0000105 assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
106 S->setEntity(PreDeclaratorDC);
Argiris Kirtzidis054a2632008-11-08 17:17:31 +0000107 PreDeclaratorDC = 0;
Douglas Gregor605de8d2008-12-16 21:30:33 +0000108
109 // Reset CurContext to the nearest enclosing context.
110 while (!S->getEntity() && S->getParent())
111 S = S->getParent();
112 CurContext = static_cast<DeclContext*>(S->getEntity());
Argiris Kirtzidis054a2632008-11-08 17:17:31 +0000113}