blob: e30ec2adcf275bb7225df2ce7dd4cbbfc42a69c5 [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"
16#include "clang/Parse/DeclSpec.h"
17#include "llvm/ADT/STLExtras.h"
18using namespace clang;
19
Douglas Gregorca5e77f2009-03-18 00:36:05 +000020/// \brief Retrieve the scope represented by this scope specifier as a
21/// DeclContext.
22DeclContext *Sema::getScopeRepAsDeclContext(const CXXScopeSpec &SS) {
23 if (SS.isInvalid() || !SS.getScopeRep())
24 return 0;
25 uintptr_t Rep = reinterpret_cast<uintptr_t>(SS.getScopeRep());
26 if ((Rep & 0x01) == 0)
27 return reinterpret_cast<DeclContext *>(Rep);
28
29 // Retrieve the DeclContext associated with this type.
30 QualType T = QualType(reinterpret_cast<Type *>(Rep & ~0x01), 0);
31 const TagType *TagT = T->getAsTagType();
32 assert(TagT && "No DeclContext from a non-tag type");
33 return TagT->getDecl();
34}
35
36/// \brief Retrieve the scope represented by this scope specifier as a
37/// type.
38QualType Sema::getScopeRepAsType(const CXXScopeSpec &SS) {
39 if (SS.isInvalid() || !SS.getScopeRep())
40 return QualType();
41
42 uintptr_t Rep = reinterpret_cast<uintptr_t>(SS.getScopeRep());
43 if ((Rep & 0x01) == 0)
44 return QualType();
45 return QualType(reinterpret_cast<Type *>(Rep & ~0x01), 0);
46}
47
48Action::CXXScopeTy *Sema::createScopeRep(QualType T) {
49 assert(((reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()) & 0x01) == 0) &&
50 "Scope type with cv-qualifiers");
51 if (T.isNull())
52 return 0;
53
54 return reinterpret_cast<CXXScopeTy *>(
55 reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()) | 0x01);
56}
57
58bool Sema::isScopeRepType(const CXXScopeSpec &SS) {
59 uintptr_t Rep = reinterpret_cast<uintptr_t>(SS.getScopeRep());
60 return Rep & 0x01;
61}
62
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +000063/// \brief Require that the context specified by SS be complete.
64///
65/// If SS refers to a type, this routine checks whether the type is
66/// complete enough (or can be made complete enough) for name lookup
67/// into the DeclContext. A type that is not yet completed can be
68/// considered "complete enough" if it is a class/struct/union/enum
69/// that is currently being defined. Or, if we have a type that names
70/// a class template specialization that is not a complete type, we
71/// will attempt to instantiate that class template.
72bool Sema::RequireCompleteDeclContext(const CXXScopeSpec &SS) {
73 if (!SS.isSet() || SS.isInvalid())
74 return false;
75
Douglas Gregorca5e77f2009-03-18 00:36:05 +000076 DeclContext *DC = getScopeRepAsDeclContext(SS);
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +000077 if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
78 // If we're currently defining this type, then lookup into the
79 // type is okay: don't complain that it isn't complete yet.
80 const TagType *TagT = Context.getTypeDeclType(Tag)->getAsTagType();
81 if (TagT->isBeingDefined())
82 return false;
83
84 // The type must be complete.
85 return RequireCompleteType(SS.getRange().getBegin(),
86 Context.getTypeDeclType(Tag),
87 diag::err_incomplete_nested_name_spec,
88 SS.getRange());
89 }
90
91 return false;
92}
Cedric Venet3d658642009-02-14 20:20:19 +000093
94/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
95/// global scope ('::').
96Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
97 SourceLocation CCLoc) {
Douglas Gregorca5e77f2009-03-18 00:36:05 +000098 return createScopeRep(Context.getTranslationUnitDecl());
Cedric Venet3d658642009-02-14 20:20:19 +000099}
100
101/// ActOnCXXNestedNameSpecifier - Called during parsing of a
102/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
103/// we want to resolve "bar::". 'SS' is empty or the previously parsed
104/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
105/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
106/// Returns a CXXScopeTy* object representing the C++ scope.
107Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
108 const CXXScopeSpec &SS,
109 SourceLocation IdLoc,
110 SourceLocation CCLoc,
111 IdentifierInfo &II) {
112 NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName);
113
114 if (SD) {
115 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000116 if (TD->getUnderlyingType()->isRecordType())
117 return createScopeRep(Context.getTypeDeclType(TD));
Cedric Venet3d658642009-02-14 20:20:19 +0000118 } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000119 return createScopeRep(cast<DeclContext>(SD));
Cedric Venet3d658642009-02-14 20:20:19 +0000120 }
121
122 // FIXME: Template parameters and dependent types.
123 // FIXME: C++0x scoped enums
124
125 // Fall through to produce an error: we found something that isn't
126 // a class or a namespace.
127 }
128
129 // If we didn't find anything during our lookup, try again with
130 // ordinary name lookup, which can help us produce better error
131 // messages.
132 if (!SD)
133 SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName);
134 unsigned DiagID;
135 if (SD)
136 DiagID = diag::err_expected_class_or_namespace;
137 else if (SS.isSet())
138 DiagID = diag::err_typecheck_no_member;
139 else
140 DiagID = diag::err_undeclared_var_use;
141
142 if (SS.isSet())
143 Diag(IdLoc, DiagID) << &II << SS.getRange();
144 else
145 Diag(IdLoc, DiagID) << &II;
146
147 return 0;
148}
149
Douglas Gregor39a8de12009-02-25 19:37:18 +0000150Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
151 const CXXScopeSpec &SS,
152 TypeTy *Ty,
153 SourceRange TypeRange,
154 SourceLocation CCLoc) {
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000155 return createScopeRep(QualType::getFromOpaquePtr(Ty));
Douglas Gregor39a8de12009-02-25 19:37:18 +0000156}
157
Cedric Venet3d658642009-02-14 20:20:19 +0000158/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
159/// scope or nested-name-specifier) is parsed, part of a declarator-id.
160/// After this method is called, according to [C++ 3.4.3p3], names should be
161/// looked up in the declarator-id's scope, until the declarator is parsed and
162/// ActOnCXXExitDeclaratorScope is called.
163/// The 'SS' should be a non-empty valid CXXScopeSpec.
164void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
165 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
166 assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
167 PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000168 CurContext = getScopeRepAsDeclContext(SS);
Cedric Venet3d658642009-02-14 20:20:19 +0000169 S->setEntity(CurContext);
170}
171
172/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
173/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
174/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
175/// Used to indicate that names should revert to being looked up in the
176/// defining scope.
177void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
178 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000179 assert(S->getEntity() == getScopeRepAsDeclContext(SS) &&
180 "Context imbalance!");
Cedric Venet3d658642009-02-14 20:20:19 +0000181 S->setEntity(PreDeclaratorDC);
182 PreDeclaratorDC = 0;
183
184 // Reset CurContext to the nearest enclosing context.
185 while (!S->getEntity() && S->getParent())
186 S = S->getParent();
187 CurContext = static_cast<DeclContext*>(S->getEntity());
188}