blob: 156f3a12fbb44073d4dcfd0dbe119f9c299614dd [file] [log] [blame]
Cedric Venet3d658642009-02-14 20:20:19 +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"
Douglas Gregore4e5b052009-03-19 00:18:19 +000016#include "clang/AST/NestedNameSpecifier.h"
Cedric Venet3d658642009-02-14 20:20:19 +000017#include "clang/Parse/DeclSpec.h"
18#include "llvm/ADT/STLExtras.h"
19using namespace clang;
20
Douglas Gregore4e5b052009-03-19 00:18:19 +000021/// \brief Compute the DeclContext that is associated with the given
22/// scope specifier.
23DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS) {
24 if (!SS.isSet() || SS.isInvalid())
Douglas Gregorca5e77f2009-03-18 00:36:05 +000025 return 0;
Douglas Gregorca5e77f2009-03-18 00:36:05 +000026
Douglas Gregore4e5b052009-03-19 00:18:19 +000027 NestedNameSpecifier NNS
28 = NestedNameSpecifier::getFromOpaquePtr(SS.getCurrentScopeRep());
29 return NNS.computeDeclContext(Context);
Douglas Gregorca5e77f2009-03-18 00:36:05 +000030}
31
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +000032/// \brief Require that the context specified by SS be complete.
33///
34/// If SS refers to a type, this routine checks whether the type is
35/// complete enough (or can be made complete enough) for name lookup
36/// into the DeclContext. A type that is not yet completed can be
37/// considered "complete enough" if it is a class/struct/union/enum
38/// that is currently being defined. Or, if we have a type that names
39/// a class template specialization that is not a complete type, we
40/// will attempt to instantiate that class template.
41bool Sema::RequireCompleteDeclContext(const CXXScopeSpec &SS) {
42 if (!SS.isSet() || SS.isInvalid())
43 return false;
44
Douglas Gregore4e5b052009-03-19 00:18:19 +000045 DeclContext *DC = computeDeclContext(SS);
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +000046 if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
47 // If we're currently defining this type, then lookup into the
48 // type is okay: don't complain that it isn't complete yet.
49 const TagType *TagT = Context.getTypeDeclType(Tag)->getAsTagType();
50 if (TagT->isBeingDefined())
51 return false;
52
53 // The type must be complete.
54 return RequireCompleteType(SS.getRange().getBegin(),
55 Context.getTypeDeclType(Tag),
56 diag::err_incomplete_nested_name_spec,
57 SS.getRange());
58 }
59
60 return false;
61}
Cedric Venet3d658642009-02-14 20:20:19 +000062
63/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
64/// global scope ('::').
65Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
66 SourceLocation CCLoc) {
Douglas Gregore4e5b052009-03-19 00:18:19 +000067 return NestedNameSpecifier(Context.getTranslationUnitDecl()).getAsOpaquePtr();
Cedric Venet3d658642009-02-14 20:20:19 +000068}
69
70/// ActOnCXXNestedNameSpecifier - Called during parsing of a
71/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
72/// we want to resolve "bar::". 'SS' is empty or the previously parsed
73/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
74/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
75/// Returns a CXXScopeTy* object representing the C++ scope.
76Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
77 const CXXScopeSpec &SS,
78 SourceLocation IdLoc,
79 SourceLocation CCLoc,
80 IdentifierInfo &II) {
81 NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName);
82
83 if (SD) {
84 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
Douglas Gregorca5e77f2009-03-18 00:36:05 +000085 if (TD->getUnderlyingType()->isRecordType())
Douglas Gregore4e5b052009-03-19 00:18:19 +000086 return NestedNameSpecifier(Context.getTypeDeclType(TD).getTypePtr())
87 .getAsOpaquePtr();
Cedric Venet3d658642009-02-14 20:20:19 +000088 } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
Douglas Gregore4e5b052009-03-19 00:18:19 +000089 return NestedNameSpecifier(cast<DeclContext>(SD)).getAsOpaquePtr();
Cedric Venet3d658642009-02-14 20:20:19 +000090 }
91
92 // FIXME: Template parameters and dependent types.
93 // FIXME: C++0x scoped enums
94
95 // Fall through to produce an error: we found something that isn't
96 // a class or a namespace.
97 }
98
99 // If we didn't find anything during our lookup, try again with
100 // ordinary name lookup, which can help us produce better error
101 // messages.
102 if (!SD)
103 SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName);
104 unsigned DiagID;
105 if (SD)
106 DiagID = diag::err_expected_class_or_namespace;
107 else if (SS.isSet())
108 DiagID = diag::err_typecheck_no_member;
109 else
110 DiagID = diag::err_undeclared_var_use;
111
112 if (SS.isSet())
113 Diag(IdLoc, DiagID) << &II << SS.getRange();
114 else
115 Diag(IdLoc, DiagID) << &II;
116
117 return 0;
118}
119
Douglas Gregor39a8de12009-02-25 19:37:18 +0000120Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
121 const CXXScopeSpec &SS,
122 TypeTy *Ty,
123 SourceRange TypeRange,
124 SourceLocation CCLoc) {
Douglas Gregore4e5b052009-03-19 00:18:19 +0000125 return NestedNameSpecifier(QualType::getFromOpaquePtr(Ty).getTypePtr())
126 .getAsOpaquePtr();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000127}
128
Cedric Venet3d658642009-02-14 20:20:19 +0000129/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
130/// scope or nested-name-specifier) is parsed, part of a declarator-id.
131/// After this method is called, according to [C++ 3.4.3p3], names should be
132/// looked up in the declarator-id's scope, until the declarator is parsed and
133/// ActOnCXXExitDeclaratorScope is called.
134/// The 'SS' should be a non-empty valid CXXScopeSpec.
135void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
136 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
137 assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
138 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
Douglas Gregore4e5b052009-03-19 00:18:19 +0000139 CurContext = computeDeclContext(SS);
Cedric Venet3d658642009-02-14 20:20:19 +0000140 S->setEntity(CurContext);
141}
142
143/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
144/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
145/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
146/// Used to indicate that names should revert to being looked up in the
147/// defining scope.
148void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
149 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregore4e5b052009-03-19 00:18:19 +0000150 assert(S->getEntity() == computeDeclContext(SS) && "Context imbalance!");
Cedric Venet3d658642009-02-14 20:20:19 +0000151 S->setEntity(PreDeclaratorDC);
152 PreDeclaratorDC = 0;
153
154 // Reset CurContext to the nearest enclosing context.
155 while (!S->getEntity() && S->getParent())
156 S = S->getParent();
157 CurContext = static_cast<DeclContext*>(S->getEntity());
158}