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/Basic/Diagnostic.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/iterator_range.h"
34 class UsingDirectiveDecl;
37 /// Scope - A scope is a transient data structure that is used while parsing the
38 /// program. It assists with resolving identifiers to the appropriate
42 /// ScopeFlags - These are bitfields that are or'd together when creating a
43 /// scope, which defines the sorts of things the scope contains.
45 /// This indicates that the scope corresponds to a function, which
46 /// means that labels are set here.
49 /// This is a while, do, switch, for, etc that can have break
50 /// statements embedded into it.
53 /// This is a while, do, for, which can have continue statements
57 /// This is a scope that can contain a declaration. Some scopes
58 /// just contain loop constructs but don't contain decls.
61 /// The controlling scope in a if/switch/while/for statement.
64 /// The scope of a struct/union/class definition.
67 /// This is a scope that corresponds to a block/closure object.
68 /// Blocks serve as top-level scopes for some objects like labels, they
69 /// also prevent things like break and continue. BlockScopes always have
70 /// the FnScope and DeclScope flags set as well.
73 /// This is a scope that corresponds to the
74 /// template parameters of a C++ template. Template parameter
75 /// scope starts at the 'template' keyword and ends when the
76 /// template declaration ends.
77 TemplateParamScope = 0x80,
79 /// This is a scope that corresponds to the
80 /// parameters within a function prototype.
81 FunctionPrototypeScope = 0x100,
83 /// This is a scope that corresponds to the parameters within
84 /// a function prototype for a function declaration (as opposed to any
85 /// other kind of function declarator). Always has FunctionPrototypeScope
87 FunctionDeclarationScope = 0x200,
89 /// This is a scope that corresponds to the Objective-C
90 /// \@catch statement.
93 /// This scope corresponds to an Objective-C method body.
94 /// It always has FnScope and DeclScope set as well.
95 ObjCMethodScope = 0x800,
97 /// This is a scope that corresponds to a switch statement.
100 /// This is the scope of a C++ try statement.
103 /// This is the scope for a function-level C++ try or catch scope.
104 FnTryCatchScope = 0x4000,
106 /// This is the scope of OpenMP executable directive.
107 OpenMPDirectiveScope = 0x8000,
109 /// This is the scope of some OpenMP loop directive.
110 OpenMPLoopDirectiveScope = 0x10000,
112 /// This is the scope of some OpenMP simd directive.
113 /// For example, it is used for 'omp simd', 'omp for simd'.
114 /// This flag is propagated to children scopes.
115 OpenMPSimdDirectiveScope = 0x20000,
117 /// This scope corresponds to an enum.
120 /// This scope corresponds to an SEH try.
121 SEHTryScope = 0x80000,
123 /// This scope corresponds to an SEH except.
124 SEHExceptScope = 0x100000,
126 /// We are currently in the filter expression of an SEH except block.
127 SEHFilterScope = 0x200000,
129 /// This is a compound statement scope.
130 CompoundStmtScope = 0x400000,
132 /// We are between inheritance colon and the real class/struct definition scope.
133 ClassInheritanceScope = 0x800000,
137 /// The parent scope for this scope. This is null for the translation-unit
141 /// Flags - This contains a set of ScopeFlags, which indicates how the scope
142 /// interrelates with other control flow statements.
145 /// Depth - This is the depth of this scope. The translation-unit scope has
147 unsigned short Depth;
149 /// Declarations with static linkage are mangled with the number of
150 /// scopes seen as a component.
151 unsigned short MSLastManglingNumber;
153 unsigned short MSCurManglingNumber;
155 /// PrototypeDepth - This is the number of function prototype scopes
156 /// enclosing this scope, including this scope.
157 unsigned short PrototypeDepth;
159 /// PrototypeIndex - This is the number of parameters currently
160 /// declared in this scope.
161 unsigned short PrototypeIndex;
163 /// FnParent - If this scope has a parent scope that is a function body, this
164 /// pointer is non-null and points to it. This is used for label processing.
166 Scope *MSLastManglingParent;
168 /// BreakParent/ContinueParent - This is a direct link to the innermost
169 /// BreakScope/ContinueScope which contains the contents of this scope
170 /// for control flow purposes (and might be this scope itself), or null
171 /// if there is no such scope.
172 Scope *BreakParent, *ContinueParent;
174 /// BlockParent - This is a direct link to the immediately containing
175 /// BlockScope if this scope is not one, or null if there is none.
178 /// TemplateParamParent - This is a direct link to the
179 /// immediately containing template parameter scope. In the
180 /// case of nested templates, template parameter scopes can have
181 /// other template parameter scopes as parents.
182 Scope *TemplateParamParent;
184 /// DeclsInScope - This keeps track of all declarations in this scope. When
185 /// the declaration is added to the scope, it is set as the current
186 /// declaration for the identifier in the IdentifierTable. When the scope is
187 /// popped, these declarations are removed from the IdentifierTable's notion
188 /// of current declaration. It is up to the current Action implementation to
189 /// implement these semantics.
190 using DeclSetTy = llvm::SmallPtrSet<Decl *, 32>;
191 DeclSetTy DeclsInScope;
193 /// The DeclContext with which this scope is associated. For
194 /// example, the entity of a class scope is the class itself, the
195 /// entity of a function scope is a function, etc.
198 using UsingDirectivesTy = SmallVector<UsingDirectiveDecl *, 2>;
199 UsingDirectivesTy UsingDirectives;
201 /// Used to determine if errors occurred in this scope.
202 DiagnosticErrorTrap ErrorTrap;
204 /// A lattice consisting of undefined, a single NRVO candidate variable in
205 /// this scope, or over-defined. The bit is true when over-defined.
206 llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
208 void setFlags(Scope *Parent, unsigned F);
211 Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
213 Init(Parent, ScopeFlags);
216 /// getFlags - Return the flags for this scope.
217 unsigned getFlags() const { return Flags; }
219 void setFlags(unsigned F) { setFlags(getParent(), F); }
221 /// isBlockScope - Return true if this scope correspond to a closure.
222 bool isBlockScope() const { return Flags & BlockScope; }
224 /// getParent - Return the scope that this is nested in.
225 const Scope *getParent() const { return AnyParent; }
226 Scope *getParent() { return AnyParent; }
228 /// 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 depth of this scope. The translation-unit has scope depth 0.
263 unsigned getDepth() const { return Depth; }
265 /// Returns the number of function prototype scopes in this scope
267 unsigned getFunctionPrototypeDepth() const {
268 return PrototypeDepth;
271 /// Return the number of parameters declared in this function
272 /// prototype, increasing it by one for the next call.
273 unsigned getNextFunctionPrototypeIndex() {
274 assert(isFunctionPrototypeScope());
275 return PrototypeIndex++;
278 using decl_range = llvm::iterator_range<DeclSetTy::iterator>;
280 decl_range decls() const {
281 return decl_range(DeclsInScope.begin(), DeclsInScope.end());
284 bool decl_empty() const { return DeclsInScope.empty(); }
286 void AddDecl(Decl *D) {
287 DeclsInScope.insert(D);
290 void RemoveDecl(Decl *D) {
291 DeclsInScope.erase(D);
294 void incrementMSManglingNumber() {
295 if (Scope *MSLMP = getMSLastManglingParent()) {
296 MSLMP->MSLastManglingNumber += 1;
297 MSCurManglingNumber += 1;
301 void decrementMSManglingNumber() {
302 if (Scope *MSLMP = getMSLastManglingParent()) {
303 MSLMP->MSLastManglingNumber -= 1;
304 MSCurManglingNumber -= 1;
308 unsigned getMSLastManglingNumber() const {
309 if (const Scope *MSLMP = getMSLastManglingParent())
310 return MSLMP->MSLastManglingNumber;
314 unsigned getMSCurManglingNumber() const {
315 return MSCurManglingNumber;
318 /// isDeclScope - Return true if this is the scope that the specified decl is
320 bool isDeclScope(Decl *D) {
321 return DeclsInScope.count(D) != 0;
324 DeclContext *getEntity() const { return Entity; }
325 void setEntity(DeclContext *E) { Entity = E; }
327 bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
329 bool hasUnrecoverableErrorOccurred() const {
330 return ErrorTrap.hasUnrecoverableErrorOccurred();
333 /// isFunctionScope() - Return true if this scope is a function scope.
334 bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
336 /// isClassScope - Return true if this scope is a class/struct/union scope.
337 bool isClassScope() const {
338 return (getFlags() & Scope::ClassScope);
341 /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
342 /// method scope or is inside one.
343 bool isInCXXInlineMethodScope() const {
344 if (const Scope *FnS = getFnParent()) {
345 assert(FnS->getParent() && "TUScope not created?");
346 return FnS->getParent()->isClassScope();
351 /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
352 /// Objective-C method body. Note that this method is not constant time.
353 bool isInObjcMethodScope() const {
354 for (const Scope *S = this; S; S = S->getParent()) {
355 // If this scope is an objc method scope, then we succeed.
356 if (S->getFlags() & ObjCMethodScope)
362 /// isInObjcMethodOuterScope - Return true if this scope is an
363 /// Objective-C method outer most body.
364 bool isInObjcMethodOuterScope() const {
365 if (const Scope *S = this) {
366 // If this scope is an objc method scope, then we succeed.
367 if (S->getFlags() & ObjCMethodScope)
373 /// isTemplateParamScope - Return true if this scope is a C++
374 /// template parameter scope.
375 bool isTemplateParamScope() const {
376 return getFlags() & Scope::TemplateParamScope;
379 /// isFunctionPrototypeScope - Return true if this scope is a
380 /// function prototype scope.
381 bool isFunctionPrototypeScope() const {
382 return getFlags() & Scope::FunctionPrototypeScope;
385 /// isAtCatchScope - Return true if this scope is \@catch.
386 bool isAtCatchScope() const {
387 return getFlags() & Scope::AtCatchScope;
390 /// isSwitchScope - Return true if this scope is a switch scope.
391 bool isSwitchScope() const {
392 for (const Scope *S = this; S; S = S->getParent()) {
393 if (S->getFlags() & Scope::SwitchScope)
395 else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
396 Scope::BlockScope | Scope::TemplateParamScope |
397 Scope::FunctionPrototypeScope |
398 Scope::AtCatchScope | Scope::ObjCMethodScope))
404 /// Determines whether this scope is the OpenMP directive scope
405 bool isOpenMPDirectiveScope() const {
406 return (getFlags() & Scope::OpenMPDirectiveScope);
409 /// Determine whether this scope is some OpenMP loop directive scope
410 /// (for example, 'omp for', 'omp simd').
411 bool isOpenMPLoopDirectiveScope() const {
412 if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
413 assert(isOpenMPDirectiveScope() &&
414 "OpenMP loop directive scope is not a directive scope");
420 /// Determine whether this scope is (or is nested into) some OpenMP
421 /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
422 bool isOpenMPSimdDirectiveScope() const {
423 return getFlags() & Scope::OpenMPSimdDirectiveScope;
426 /// Determine whether this scope is a loop having OpenMP loop
427 /// directive attached.
428 bool isOpenMPLoopScope() const {
429 const Scope *P = getParent();
430 return P && P->isOpenMPLoopDirectiveScope();
433 /// Determine whether this scope is a C++ 'try' block.
434 bool isTryScope() const { return getFlags() & Scope::TryScope; }
436 /// Determine whether this scope is a SEH '__try' block.
437 bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
439 /// Determine whether this scope is a SEH '__except' block.
440 bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
442 /// Determine whether this scope is a compound statement scope.
443 bool isCompoundStmtScope() const {
444 return getFlags() & Scope::CompoundStmtScope;
447 /// Returns if rhs has a higher scope depth than this.
449 /// The caller is responsible for calling this only if one of the two scopes
450 /// is an ancestor of the other.
451 bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
453 /// containedInPrototypeScope - Return true if this or a parent scope
454 /// is a FunctionPrototypeScope.
455 bool containedInPrototypeScope() const;
457 void PushUsingDirective(UsingDirectiveDecl *UDir) {
458 UsingDirectives.push_back(UDir);
461 using using_directives_range =
462 llvm::iterator_range<UsingDirectivesTy::iterator>;
464 using_directives_range using_directives() {
465 return using_directives_range(UsingDirectives.begin(),
466 UsingDirectives.end());
469 void addNRVOCandidate(VarDecl *VD) {
472 if (NRVO.getPointer() == nullptr) {
476 if (NRVO.getPointer() != VD)
482 NRVO.setPointer(nullptr);
485 void mergeNRVOIntoParent();
487 /// Init - This is used by the parser to implement scope caching.
488 void Init(Scope *parent, unsigned flags);
490 /// Sets up the specified scope flags and adjusts the scope state
491 /// variables accordingly.
492 void AddFlags(unsigned Flags);
494 void dumpImpl(raw_ostream &OS) const;
500 #endif // LLVM_CLANG_SEMA_SCOPE_H