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,
129 /// The parent scope for this scope. This is null for the translation-unit
133 /// Flags - This contains a set of ScopeFlags, which indicates how the scope
134 /// interrelates with other control flow statements.
137 /// Depth - This is the depth of this scope. The translation-unit scope has
139 unsigned short Depth;
141 /// \brief Declarations with static linkage are mangled with the number of
142 /// scopes seen as a component.
143 unsigned short MSLastManglingNumber;
145 unsigned short MSCurManglingNumber;
147 /// PrototypeDepth - This is the number of function prototype scopes
148 /// enclosing this scope, including this scope.
149 unsigned short PrototypeDepth;
151 /// PrototypeIndex - This is the number of parameters currently
152 /// declared in this scope.
153 unsigned short PrototypeIndex;
155 /// FnParent - If this scope has a parent scope that is a function body, this
156 /// pointer is non-null and points to it. This is used for label processing.
158 Scope *MSLastManglingParent;
160 /// BreakParent/ContinueParent - This is a direct link to the innermost
161 /// BreakScope/ContinueScope which contains the contents of this scope
162 /// for control flow purposes (and might be this scope itself), or null
163 /// if there is no such scope.
164 Scope *BreakParent, *ContinueParent;
166 /// BlockParent - This is a direct link to the immediately containing
167 /// BlockScope if this scope is not one, or null if there is none.
170 /// TemplateParamParent - This is a direct link to the
171 /// immediately containing template parameter scope. In the
172 /// case of nested templates, template parameter scopes can have
173 /// other template parameter scopes as parents.
174 Scope *TemplateParamParent;
176 /// DeclsInScope - This keeps track of all declarations in this scope. When
177 /// the declaration is added to the scope, it is set as the current
178 /// declaration for the identifier in the IdentifierTable. When the scope is
179 /// popped, these declarations are removed from the IdentifierTable's notion
180 /// of current declaration. It is up to the current Action implementation to
181 /// implement these semantics.
182 typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
183 DeclSetTy DeclsInScope;
185 /// The DeclContext with which this scope is associated. For
186 /// example, the entity of a class scope is the class itself, the
187 /// entity of a function scope is a function, etc.
190 typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
191 UsingDirectivesTy UsingDirectives;
193 /// \brief Used to determine if errors occurred in this scope.
194 DiagnosticErrorTrap ErrorTrap;
196 /// A lattice consisting of undefined, a single NRVO candidate variable in
197 /// this scope, or over-defined. The bit is true when over-defined.
198 llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
200 void setFlags(Scope *Parent, unsigned F);
203 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
205 Init(Parent, ScopeFlags);
208 /// getFlags - Return the flags for this scope.
210 unsigned getFlags() const { return Flags; }
211 void setFlags(unsigned F) { setFlags(getParent(), F); }
213 /// isBlockScope - Return true if this scope correspond to a closure.
214 bool isBlockScope() const { return Flags & BlockScope; }
216 /// getParent - Return the scope that this is nested in.
218 const Scope *getParent() const { return AnyParent; }
219 Scope *getParent() { return AnyParent; }
221 /// getFnParent - Return the closest scope that is a function body.
223 const Scope *getFnParent() const { return FnParent; }
224 Scope *getFnParent() { return FnParent; }
226 const Scope *getMSLastManglingParent() const {
227 return MSLastManglingParent;
229 Scope *getMSLastManglingParent() { return MSLastManglingParent; }
231 /// getContinueParent - Return the closest scope that a continue statement
232 /// would be affected by.
233 Scope *getContinueParent() {
234 return ContinueParent;
237 const Scope *getContinueParent() const {
238 return const_cast<Scope*>(this)->getContinueParent();
241 /// getBreakParent - Return the closest scope that a break statement
242 /// would be affected by.
243 Scope *getBreakParent() {
246 const Scope *getBreakParent() const {
247 return const_cast<Scope*>(this)->getBreakParent();
250 Scope *getBlockParent() { return BlockParent; }
251 const Scope *getBlockParent() const { return BlockParent; }
253 Scope *getTemplateParamParent() { return TemplateParamParent; }
254 const Scope *getTemplateParamParent() const { return TemplateParamParent; }
256 /// Returns the number of function prototype scopes in this scope
258 unsigned getFunctionPrototypeDepth() const {
259 return PrototypeDepth;
262 /// Return the number of parameters declared in this function
263 /// prototype, increasing it by one for the next call.
264 unsigned getNextFunctionPrototypeIndex() {
265 assert(isFunctionPrototypeScope());
266 return PrototypeIndex++;
269 typedef llvm::iterator_range<DeclSetTy::iterator> decl_range;
270 decl_range decls() const {
271 return decl_range(DeclsInScope.begin(), DeclsInScope.end());
273 bool decl_empty() const { return DeclsInScope.empty(); }
275 void AddDecl(Decl *D) {
276 DeclsInScope.insert(D);
279 void RemoveDecl(Decl *D) {
280 DeclsInScope.erase(D);
283 void incrementMSManglingNumber() {
284 if (Scope *MSLMP = getMSLastManglingParent()) {
285 MSLMP->MSLastManglingNumber += 1;
286 MSCurManglingNumber += 1;
290 void decrementMSManglingNumber() {
291 if (Scope *MSLMP = getMSLastManglingParent()) {
292 MSLMP->MSLastManglingNumber -= 1;
293 MSCurManglingNumber -= 1;
297 unsigned getMSLastManglingNumber() const {
298 if (const Scope *MSLMP = getMSLastManglingParent())
299 return MSLMP->MSLastManglingNumber;
303 unsigned getMSCurManglingNumber() const {
304 return MSCurManglingNumber;
307 /// isDeclScope - Return true if this is the scope that the specified decl is
309 bool isDeclScope(Decl *D) {
310 return DeclsInScope.count(D) != 0;
313 DeclContext *getEntity() const { return Entity; }
314 void setEntity(DeclContext *E) { Entity = E; }
316 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
318 bool hasUnrecoverableErrorOccurred() const {
319 return ErrorTrap.hasUnrecoverableErrorOccurred();
322 /// isFunctionScope() - Return true if this scope is a function scope.
323 bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
325 /// isClassScope - Return true if this scope is a class/struct/union scope.
326 bool isClassScope() const {
327 return (getFlags() & Scope::ClassScope);
330 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
331 /// method scope or is inside one.
332 bool isInCXXInlineMethodScope() const {
333 if (const Scope *FnS = getFnParent()) {
334 assert(FnS->getParent() && "TUScope not created?");
335 return FnS->getParent()->isClassScope();
340 /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
341 /// Objective-C method body. Note that this method is not constant time.
342 bool isInObjcMethodScope() const {
343 for (const Scope *S = this; S; S = S->getParent()) {
344 // If this scope is an objc method scope, then we succeed.
345 if (S->getFlags() & ObjCMethodScope)
351 /// isInObjcMethodOuterScope - Return true if this scope is an
352 /// Objective-C method outer most body.
353 bool isInObjcMethodOuterScope() const {
354 if (const Scope *S = this) {
355 // If this scope is an objc method scope, then we succeed.
356 if (S->getFlags() & ObjCMethodScope)
363 /// isTemplateParamScope - Return true if this scope is a C++
364 /// template parameter scope.
365 bool isTemplateParamScope() const {
366 return getFlags() & Scope::TemplateParamScope;
369 /// isFunctionPrototypeScope - Return true if this scope is a
370 /// function prototype scope.
371 bool isFunctionPrototypeScope() const {
372 return getFlags() & Scope::FunctionPrototypeScope;
375 /// isAtCatchScope - Return true if this scope is \@catch.
376 bool isAtCatchScope() const {
377 return getFlags() & Scope::AtCatchScope;
380 /// isSwitchScope - Return true if this scope is a switch scope.
381 bool isSwitchScope() const {
382 for (const Scope *S = this; S; S = S->getParent()) {
383 if (S->getFlags() & Scope::SwitchScope)
385 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
386 Scope::BlockScope | Scope::TemplateParamScope |
387 Scope::FunctionPrototypeScope |
388 Scope::AtCatchScope | Scope::ObjCMethodScope))
394 /// \brief Determines whether this scope is the OpenMP directive scope
395 bool isOpenMPDirectiveScope() const {
396 return (getFlags() & Scope::OpenMPDirectiveScope);
399 /// \brief Determine whether this scope is some OpenMP loop directive scope
400 /// (for example, 'omp for', 'omp simd').
401 bool isOpenMPLoopDirectiveScope() const {
402 if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
403 assert(isOpenMPDirectiveScope() &&
404 "OpenMP loop directive scope is not a directive scope");
410 /// \brief Determine whether this scope is (or is nested into) some OpenMP
411 /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
412 bool isOpenMPSimdDirectiveScope() const {
413 return getFlags() & Scope::OpenMPSimdDirectiveScope;
416 /// \brief Determine whether this scope is a loop having OpenMP loop
417 /// directive attached.
418 bool isOpenMPLoopScope() const {
419 const Scope *P = getParent();
420 return P && P->isOpenMPLoopDirectiveScope();
423 /// \brief Determine whether this scope is a C++ 'try' block.
424 bool isTryScope() const { return getFlags() & Scope::TryScope; }
426 /// \brief Determine whether this scope is a SEH '__try' block.
427 bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
429 /// \brief Determine whether this scope is a SEH '__except' block.
430 bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
432 /// \brief Returns if rhs has a higher scope depth than this.
434 /// The caller is responsible for calling this only if one of the two scopes
435 /// is an ancestor of the other.
436 bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
438 /// containedInPrototypeScope - Return true if this or a parent scope
439 /// is a FunctionPrototypeScope.
440 bool containedInPrototypeScope() const;
442 void PushUsingDirective(UsingDirectiveDecl *UDir) {
443 UsingDirectives.push_back(UDir);
446 typedef llvm::iterator_range<UsingDirectivesTy::iterator>
447 using_directives_range;
449 using_directives_range using_directives() {
450 return using_directives_range(UsingDirectives.begin(),
451 UsingDirectives.end());
454 void addNRVOCandidate(VarDecl *VD) {
457 if (NRVO.getPointer() == nullptr) {
461 if (NRVO.getPointer() != VD)
467 NRVO.setPointer(nullptr);
470 void mergeNRVOIntoParent();
472 /// Init - This is used by the parser to implement scope caching.
474 void Init(Scope *parent, unsigned flags);
476 /// \brief Sets up the specified scope flags and adjusts the scope state
477 /// variables accordingly.
479 void AddFlags(unsigned Flags);
481 void dumpImpl(raw_ostream &OS) const;
485 } // end namespace clang