1 //===--- Scope.h - Scope interface ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Scope interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SEMA_SCOPE_H
15 #define LLVM_CLANG_SEMA_SCOPE_H
17 #include "clang/AST/Decl.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
32 class UsingDirectiveDecl;
35 /// Scope - A scope is a transient data structure that is used while parsing the
36 /// program. It assists with resolving identifiers to the appropriate
41 /// ScopeFlags - These are bitfields that are or'd together when creating a
42 /// scope, which defines the sorts of things the scope contains.
44 /// \brief This indicates that the scope corresponds to a function, which
45 /// means that labels are set here.
48 /// \brief This is a while, do, switch, for, etc that can have break
49 /// statements embedded into it.
52 /// \brief This is a while, do, for, which can have continue statements
56 /// \brief This is a scope that can contain a declaration. Some scopes
57 /// just contain loop constructs but don't contain decls.
60 /// \brief The controlling scope in a if/switch/while/for statement.
63 /// \brief The scope of a struct/union/class definition.
66 /// \brief This is a scope that corresponds to a block/closure object.
67 /// Blocks serve as top-level scopes for some objects like labels, they
68 /// also prevent things like break and continue. BlockScopes always have
69 /// the FnScope and DeclScope flags set as well.
72 /// \brief This is a scope that corresponds to the
73 /// template parameters of a C++ template. Template parameter
74 /// scope starts at the 'template' keyword and ends when the
75 /// template declaration ends.
76 TemplateParamScope = 0x80,
78 /// \brief This is a scope that corresponds to the
79 /// parameters within a function prototype.
80 FunctionPrototypeScope = 0x100,
82 /// \brief This is a scope that corresponds to the parameters within
83 /// a function prototype for a function declaration (as opposed to any
84 /// other kind of function declarator). Always has FunctionPrototypeScope
86 FunctionDeclarationScope = 0x200,
88 /// \brief This is a scope that corresponds to the Objective-C
89 /// \@catch statement.
92 /// \brief This scope corresponds to an Objective-C method body.
93 /// It always has FnScope and DeclScope set as well.
94 ObjCMethodScope = 0x800,
96 /// \brief This is a scope that corresponds to a switch statement.
99 /// \brief This is the scope of a C++ try statement.
102 /// \brief This is the scope for a function-level C++ try or catch scope.
103 FnTryCatchScope = 0x4000,
105 /// \brief This is the scope of OpenMP executable directive.
106 OpenMPDirectiveScope = 0x8000,
108 /// \brief This is the scope of some OpenMP loop directive.
109 OpenMPLoopDirectiveScope = 0x10000,
111 /// \brief This is the scope of some OpenMP simd directive.
112 /// For example, it is used for 'omp simd', 'omp for simd'.
113 /// This flag is propagated to children scopes.
114 OpenMPSimdDirectiveScope = 0x20000,
116 /// This scope corresponds to an enum.
119 /// This scope corresponds to an SEH try.
120 SEHTryScope = 0x80000,
122 /// This scope corresponds to an SEH except.
123 SEHExceptScope = 0x100000,
125 /// We are currently in the filter expression of an SEH except block.
126 SEHFilterScope = 0x200000,
128 /// This is a compound statement scope.
129 CompoundStmtScope = 0x400000,
131 /// We are between inheritance colon and the real class/struct definition scope.
132 ClassInheritanceScope = 0x800000,
135 /// The parent scope for this scope. This is null for the translation-unit
139 /// Flags - This contains a set of ScopeFlags, which indicates how the scope
140 /// interrelates with other control flow statements.
143 /// Depth - This is the depth of this scope. The translation-unit scope has
145 unsigned short Depth;
147 /// \brief Declarations with static linkage are mangled with the number of
148 /// scopes seen as a component.
149 unsigned short MSLastManglingNumber;
151 unsigned short MSCurManglingNumber;
153 /// PrototypeDepth - This is the number of function prototype scopes
154 /// enclosing this scope, including this scope.
155 unsigned short PrototypeDepth;
157 /// PrototypeIndex - This is the number of parameters currently
158 /// declared in this scope.
159 unsigned short PrototypeIndex;
161 /// FnParent - If this scope has a parent scope that is a function body, this
162 /// pointer is non-null and points to it. This is used for label processing.
164 Scope *MSLastManglingParent;
166 /// BreakParent/ContinueParent - This is a direct link to the innermost
167 /// BreakScope/ContinueScope which contains the contents of this scope
168 /// for control flow purposes (and might be this scope itself), or null
169 /// if there is no such scope.
170 Scope *BreakParent, *ContinueParent;
172 /// BlockParent - This is a direct link to the immediately containing
173 /// BlockScope if this scope is not one, or null if there is none.
176 /// TemplateParamParent - This is a direct link to the
177 /// immediately containing template parameter scope. In the
178 /// case of nested templates, template parameter scopes can have
179 /// other template parameter scopes as parents.
180 Scope *TemplateParamParent;
182 /// DeclsInScope - This keeps track of all declarations in this scope. When
183 /// the declaration is added to the scope, it is set as the current
184 /// declaration for the identifier in the IdentifierTable. When the scope is
185 /// popped, these declarations are removed from the IdentifierTable's notion
186 /// of current declaration. It is up to the current Action implementation to
187 /// implement these semantics.
188 typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
189 DeclSetTy DeclsInScope;
191 /// The DeclContext with which this scope is associated. For
192 /// example, the entity of a class scope is the class itself, the
193 /// entity of a function scope is a function, etc.
196 typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
197 UsingDirectivesTy UsingDirectives;
199 /// \brief Used to determine if errors occurred in this scope.
200 DiagnosticErrorTrap ErrorTrap;
202 /// A lattice consisting of undefined, a single NRVO candidate variable in
203 /// this scope, or over-defined. The bit is true when over-defined.
204 llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
206 void setFlags(Scope *Parent, unsigned F);
209 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
211 Init(Parent, ScopeFlags);
214 /// getFlags - Return the flags for this scope.
216 unsigned getFlags() const { return Flags; }
217 void setFlags(unsigned F) { setFlags(getParent(), F); }
219 /// isBlockScope - Return true if this scope correspond to a closure.
220 bool isBlockScope() const { return Flags & BlockScope; }
222 /// getParent - Return the scope that this is nested in.
224 const Scope *getParent() const { return AnyParent; }
225 Scope *getParent() { return AnyParent; }
227 /// getFnParent - Return the closest scope that is a function body.
229 const Scope *getFnParent() const { return FnParent; }
230 Scope *getFnParent() { return FnParent; }
232 const Scope *getMSLastManglingParent() const {
233 return MSLastManglingParent;
235 Scope *getMSLastManglingParent() { return MSLastManglingParent; }
237 /// getContinueParent - Return the closest scope that a continue statement
238 /// would be affected by.
239 Scope *getContinueParent() {
240 return ContinueParent;
243 const Scope *getContinueParent() const {
244 return const_cast<Scope*>(this)->getContinueParent();
247 /// getBreakParent - Return the closest scope that a break statement
248 /// would be affected by.
249 Scope *getBreakParent() {
252 const Scope *getBreakParent() const {
253 return const_cast<Scope*>(this)->getBreakParent();
256 Scope *getBlockParent() { return BlockParent; }
257 const Scope *getBlockParent() const { return BlockParent; }
259 Scope *getTemplateParamParent() { return TemplateParamParent; }
260 const Scope *getTemplateParamParent() const { return TemplateParamParent; }
262 /// Returns the number of function prototype scopes in this scope
264 unsigned getFunctionPrototypeDepth() const {
265 return PrototypeDepth;
268 /// Return the number of parameters declared in this function
269 /// prototype, increasing it by one for the next call.
270 unsigned getNextFunctionPrototypeIndex() {
271 assert(isFunctionPrototypeScope());
272 return PrototypeIndex++;
275 typedef llvm::iterator_range<DeclSetTy::iterator> decl_range;
276 decl_range decls() const {
277 return decl_range(DeclsInScope.begin(), DeclsInScope.end());
279 bool decl_empty() const { return DeclsInScope.empty(); }
281 void AddDecl(Decl *D) {
282 DeclsInScope.insert(D);
285 void RemoveDecl(Decl *D) {
286 DeclsInScope.erase(D);
289 void incrementMSManglingNumber() {
290 if (Scope *MSLMP = getMSLastManglingParent()) {
291 MSLMP->MSLastManglingNumber += 1;
292 MSCurManglingNumber += 1;
296 void decrementMSManglingNumber() {
297 if (Scope *MSLMP = getMSLastManglingParent()) {
298 MSLMP->MSLastManglingNumber -= 1;
299 MSCurManglingNumber -= 1;
303 unsigned getMSLastManglingNumber() const {
304 if (const Scope *MSLMP = getMSLastManglingParent())
305 return MSLMP->MSLastManglingNumber;
309 unsigned getMSCurManglingNumber() const {
310 return MSCurManglingNumber;
313 /// isDeclScope - Return true if this is the scope that the specified decl is
315 bool isDeclScope(Decl *D) {
316 return DeclsInScope.count(D) != 0;
319 DeclContext *getEntity() const { return Entity; }
320 void setEntity(DeclContext *E) { Entity = E; }
322 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
324 bool hasUnrecoverableErrorOccurred() const {
325 return ErrorTrap.hasUnrecoverableErrorOccurred();
328 /// isFunctionScope() - Return true if this scope is a function scope.
329 bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
331 /// isClassScope - Return true if this scope is a class/struct/union scope.
332 bool isClassScope() const {
333 return (getFlags() & Scope::ClassScope);
336 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
337 /// method scope or is inside one.
338 bool isInCXXInlineMethodScope() const {
339 if (const Scope *FnS = getFnParent()) {
340 assert(FnS->getParent() && "TUScope not created?");
341 return FnS->getParent()->isClassScope();
346 /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
347 /// Objective-C method body. Note that this method is not constant time.
348 bool isInObjcMethodScope() const {
349 for (const Scope *S = this; S; S = S->getParent()) {
350 // If this scope is an objc method scope, then we succeed.
351 if (S->getFlags() & ObjCMethodScope)
357 /// isInObjcMethodOuterScope - Return true if this scope is an
358 /// Objective-C method outer most body.
359 bool isInObjcMethodOuterScope() const {
360 if (const Scope *S = this) {
361 // If this scope is an objc method scope, then we succeed.
362 if (S->getFlags() & ObjCMethodScope)
369 /// isTemplateParamScope - Return true if this scope is a C++
370 /// template parameter scope.
371 bool isTemplateParamScope() const {
372 return getFlags() & Scope::TemplateParamScope;
375 /// isFunctionPrototypeScope - Return true if this scope is a
376 /// function prototype scope.
377 bool isFunctionPrototypeScope() const {
378 return getFlags() & Scope::FunctionPrototypeScope;
381 /// isAtCatchScope - Return true if this scope is \@catch.
382 bool isAtCatchScope() const {
383 return getFlags() & Scope::AtCatchScope;
386 /// isSwitchScope - Return true if this scope is a switch scope.
387 bool isSwitchScope() const {
388 for (const Scope *S = this; S; S = S->getParent()) {
389 if (S->getFlags() & Scope::SwitchScope)
391 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
392 Scope::BlockScope | Scope::TemplateParamScope |
393 Scope::FunctionPrototypeScope |
394 Scope::AtCatchScope | Scope::ObjCMethodScope))
400 /// \brief Determines whether this scope is the OpenMP directive scope
401 bool isOpenMPDirectiveScope() const {
402 return (getFlags() & Scope::OpenMPDirectiveScope);
405 /// \brief Determine whether this scope is some OpenMP loop directive scope
406 /// (for example, 'omp for', 'omp simd').
407 bool isOpenMPLoopDirectiveScope() const {
408 if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
409 assert(isOpenMPDirectiveScope() &&
410 "OpenMP loop directive scope is not a directive scope");
416 /// \brief Determine whether this scope is (or is nested into) some OpenMP
417 /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
418 bool isOpenMPSimdDirectiveScope() const {
419 return getFlags() & Scope::OpenMPSimdDirectiveScope;
422 /// \brief Determine whether this scope is a loop having OpenMP loop
423 /// directive attached.
424 bool isOpenMPLoopScope() const {
425 const Scope *P = getParent();
426 return P && P->isOpenMPLoopDirectiveScope();
429 /// \brief Determine whether this scope is a C++ 'try' block.
430 bool isTryScope() const { return getFlags() & Scope::TryScope; }
432 /// \brief Determine whether this scope is a SEH '__try' block.
433 bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
435 /// \brief Determine whether this scope is a SEH '__except' block.
436 bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
438 /// \brief Determine whether this scope is a compound statement scope.
439 bool isCompoundStmtScope() const {
440 return getFlags() & Scope::CompoundStmtScope;
443 /// \brief Returns if rhs has a higher scope depth than this.
445 /// The caller is responsible for calling this only if one of the two scopes
446 /// is an ancestor of the other.
447 bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
449 /// containedInPrototypeScope - Return true if this or a parent scope
450 /// is a FunctionPrototypeScope.
451 bool containedInPrototypeScope() const;
453 void PushUsingDirective(UsingDirectiveDecl *UDir) {
454 UsingDirectives.push_back(UDir);
457 typedef llvm::iterator_range<UsingDirectivesTy::iterator>
458 using_directives_range;
460 using_directives_range using_directives() {
461 return using_directives_range(UsingDirectives.begin(),
462 UsingDirectives.end());
465 void addNRVOCandidate(VarDecl *VD) {
468 if (NRVO.getPointer() == nullptr) {
472 if (NRVO.getPointer() != VD)
478 NRVO.setPointer(nullptr);
481 void mergeNRVOIntoParent();
483 /// Init - This is used by the parser to implement scope caching.
485 void Init(Scope *parent, unsigned flags);
487 /// \brief Sets up the specified scope flags and adjusts the scope state
488 /// variables accordingly.
490 void AddFlags(unsigned Flags);
492 void dumpImpl(raw_ostream &OS) const;
496 } // end namespace clang