clang  8.0.0
Scope.h
Go to the documentation of this file.
1 //===- Scope.h - Scope interface --------------------------------*- C++ -*-===//
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 defines the Scope interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_SEMA_SCOPE_H
15 #define LLVM_CLANG_SEMA_SCOPE_H
16 
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"
22 #include "llvm/ADT/iterator_range.h"
23 #include <cassert>
24 
25 namespace llvm {
26 
27 class raw_ostream;
28 
29 } // namespace llvm
30 
31 namespace clang {
32 
33 class Decl;
34 class DeclContext;
35 class UsingDirectiveDecl;
36 class VarDecl;
37 
38 /// Scope - A scope is a transient data structure that is used while parsing the
39 /// program. It assists with resolving identifiers to the appropriate
40 /// declaration.
41 class Scope {
42 public:
43  /// ScopeFlags - These are bitfields that are or'd together when creating a
44  /// scope, which defines the sorts of things the scope contains.
45  enum ScopeFlags {
46  /// This indicates that the scope corresponds to a function, which
47  /// means that labels are set here.
48  FnScope = 0x01,
49 
50  /// This is a while, do, switch, for, etc that can have break
51  /// statements embedded into it.
52  BreakScope = 0x02,
53 
54  /// This is a while, do, for, which can have continue statements
55  /// embedded into it.
56  ContinueScope = 0x04,
57 
58  /// This is a scope that can contain a declaration. Some scopes
59  /// just contain loop constructs but don't contain decls.
60  DeclScope = 0x08,
61 
62  /// The controlling scope in a if/switch/while/for statement.
63  ControlScope = 0x10,
64 
65  /// The scope of a struct/union/class definition.
66  ClassScope = 0x20,
67 
68  /// This is a scope that corresponds to a block/closure object.
69  /// Blocks serve as top-level scopes for some objects like labels, they
70  /// also prevent things like break and continue. BlockScopes always have
71  /// the FnScope and DeclScope flags set as well.
72  BlockScope = 0x40,
73 
74  /// This is a scope that corresponds to the
75  /// template parameters of a C++ template. Template parameter
76  /// scope starts at the 'template' keyword and ends when the
77  /// template declaration ends.
78  TemplateParamScope = 0x80,
79 
80  /// This is a scope that corresponds to the
81  /// parameters within a function prototype.
82  FunctionPrototypeScope = 0x100,
83 
84  /// This is a scope that corresponds to the parameters within
85  /// a function prototype for a function declaration (as opposed to any
86  /// other kind of function declarator). Always has FunctionPrototypeScope
87  /// set as well.
88  FunctionDeclarationScope = 0x200,
89 
90  /// This is a scope that corresponds to the Objective-C
91  /// \@catch statement.
92  AtCatchScope = 0x400,
93 
94  /// This scope corresponds to an Objective-C method body.
95  /// It always has FnScope and DeclScope set as well.
96  ObjCMethodScope = 0x800,
97 
98  /// This is a scope that corresponds to a switch statement.
99  SwitchScope = 0x1000,
100 
101  /// This is the scope of a C++ try statement.
102  TryScope = 0x2000,
103 
104  /// This is the scope for a function-level C++ try or catch scope.
105  FnTryCatchScope = 0x4000,
106 
107  /// This is the scope of OpenMP executable directive.
108  OpenMPDirectiveScope = 0x8000,
109 
110  /// This is the scope of some OpenMP loop directive.
111  OpenMPLoopDirectiveScope = 0x10000,
112 
113  /// This is the scope of some OpenMP simd directive.
114  /// For example, it is used for 'omp simd', 'omp for simd'.
115  /// This flag is propagated to children scopes.
116  OpenMPSimdDirectiveScope = 0x20000,
117 
118  /// This scope corresponds to an enum.
119  EnumScope = 0x40000,
120 
121  /// This scope corresponds to an SEH try.
122  SEHTryScope = 0x80000,
123 
124  /// This scope corresponds to an SEH except.
125  SEHExceptScope = 0x100000,
126 
127  /// We are currently in the filter expression of an SEH except block.
128  SEHFilterScope = 0x200000,
129 
130  /// This is a compound statement scope.
131  CompoundStmtScope = 0x400000,
132 
133  /// We are between inheritance colon and the real class/struct definition scope.
134  ClassInheritanceScope = 0x800000,
135  };
136 
137 private:
138  /// The parent scope for this scope. This is null for the translation-unit
139  /// scope.
140  Scope *AnyParent;
141 
142  /// Flags - This contains a set of ScopeFlags, which indicates how the scope
143  /// interrelates with other control flow statements.
144  unsigned Flags;
145 
146  /// Depth - This is the depth of this scope. The translation-unit scope has
147  /// depth 0.
148  unsigned short Depth;
149 
150  /// Declarations with static linkage are mangled with the number of
151  /// scopes seen as a component.
152  unsigned short MSLastManglingNumber;
153 
154  unsigned short MSCurManglingNumber;
155 
156  /// PrototypeDepth - This is the number of function prototype scopes
157  /// enclosing this scope, including this scope.
158  unsigned short PrototypeDepth;
159 
160  /// PrototypeIndex - This is the number of parameters currently
161  /// declared in this scope.
162  unsigned short PrototypeIndex;
163 
164  /// FnParent - If this scope has a parent scope that is a function body, this
165  /// pointer is non-null and points to it. This is used for label processing.
166  Scope *FnParent;
167  Scope *MSLastManglingParent;
168 
169  /// BreakParent/ContinueParent - This is a direct link to the innermost
170  /// BreakScope/ContinueScope which contains the contents of this scope
171  /// for control flow purposes (and might be this scope itself), or null
172  /// if there is no such scope.
173  Scope *BreakParent, *ContinueParent;
174 
175  /// BlockParent - This is a direct link to the immediately containing
176  /// BlockScope if this scope is not one, or null if there is none.
177  Scope *BlockParent;
178 
179  /// TemplateParamParent - This is a direct link to the
180  /// immediately containing template parameter scope. In the
181  /// case of nested templates, template parameter scopes can have
182  /// other template parameter scopes as parents.
183  Scope *TemplateParamParent;
184 
185  /// DeclsInScope - This keeps track of all declarations in this scope. When
186  /// the declaration is added to the scope, it is set as the current
187  /// declaration for the identifier in the IdentifierTable. When the scope is
188  /// popped, these declarations are removed from the IdentifierTable's notion
189  /// of current declaration. It is up to the current Action implementation to
190  /// implement these semantics.
191  using DeclSetTy = llvm::SmallPtrSet<Decl *, 32>;
192  DeclSetTy DeclsInScope;
193 
194  /// The DeclContext with which this scope is associated. For
195  /// example, the entity of a class scope is the class itself, the
196  /// entity of a function scope is a function, etc.
197  DeclContext *Entity;
198 
200  UsingDirectivesTy UsingDirectives;
201 
202  /// Used to determine if errors occurred in this scope.
203  DiagnosticErrorTrap ErrorTrap;
204 
205  /// A lattice consisting of undefined, a single NRVO candidate variable in
206  /// this scope, or over-defined. The bit is true when over-defined.
207  llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
208 
209  void setFlags(Scope *Parent, unsigned F);
210 
211 public:
213  : ErrorTrap(Diag) {
214  Init(Parent, ScopeFlags);
215  }
216 
217  /// getFlags - Return the flags for this scope.
218  unsigned getFlags() const { return Flags; }
219 
220  void setFlags(unsigned F) { setFlags(getParent(), F); }
221 
222  /// isBlockScope - Return true if this scope correspond to a closure.
223  bool isBlockScope() const { return Flags & BlockScope; }
224 
225  /// getParent - Return the scope that this is nested in.
226  const Scope *getParent() const { return AnyParent; }
227  Scope *getParent() { return AnyParent; }
228 
229  /// getFnParent - Return the closest scope that is a function body.
230  const Scope *getFnParent() const { return FnParent; }
231  Scope *getFnParent() { return FnParent; }
232 
234  return MSLastManglingParent;
235  }
236  Scope *getMSLastManglingParent() { return MSLastManglingParent; }
237 
238  /// getContinueParent - Return the closest scope that a continue statement
239  /// would be affected by.
241  return ContinueParent;
242  }
243 
244  const Scope *getContinueParent() const {
245  return const_cast<Scope*>(this)->getContinueParent();
246  }
247 
248  /// getBreakParent - Return the closest scope that a break statement
249  /// would be affected by.
251  return BreakParent;
252  }
253  const Scope *getBreakParent() const {
254  return const_cast<Scope*>(this)->getBreakParent();
255  }
256 
257  Scope *getBlockParent() { return BlockParent; }
258  const Scope *getBlockParent() const { return BlockParent; }
259 
260  Scope *getTemplateParamParent() { return TemplateParamParent; }
261  const Scope *getTemplateParamParent() const { return TemplateParamParent; }
262 
263  /// Returns the depth of this scope. The translation-unit has scope depth 0.
264  unsigned getDepth() const { return Depth; }
265 
266  /// Returns the number of function prototype scopes in this scope
267  /// chain.
268  unsigned getFunctionPrototypeDepth() const {
269  return PrototypeDepth;
270  }
271 
272  /// Return the number of parameters declared in this function
273  /// prototype, increasing it by one for the next call.
275  assert(isFunctionPrototypeScope());
276  return PrototypeIndex++;
277  }
278 
279  using decl_range = llvm::iterator_range<DeclSetTy::iterator>;
280 
281  decl_range decls() const {
282  return decl_range(DeclsInScope.begin(), DeclsInScope.end());
283  }
284 
285  bool decl_empty() const { return DeclsInScope.empty(); }
286 
287  void AddDecl(Decl *D) {
288  DeclsInScope.insert(D);
289  }
290 
291  void RemoveDecl(Decl *D) {
292  DeclsInScope.erase(D);
293  }
294 
296  if (Scope *MSLMP = getMSLastManglingParent()) {
297  MSLMP->MSLastManglingNumber += 1;
298  MSCurManglingNumber += 1;
299  }
300  }
301 
303  if (Scope *MSLMP = getMSLastManglingParent()) {
304  MSLMP->MSLastManglingNumber -= 1;
305  MSCurManglingNumber -= 1;
306  }
307  }
308 
309  unsigned getMSLastManglingNumber() const {
310  if (const Scope *MSLMP = getMSLastManglingParent())
311  return MSLMP->MSLastManglingNumber;
312  return 1;
313  }
314 
315  unsigned getMSCurManglingNumber() const {
316  return MSCurManglingNumber;
317  }
318 
319  /// isDeclScope - Return true if this is the scope that the specified decl is
320  /// declared in.
321  bool isDeclScope(Decl *D) {
322  return DeclsInScope.count(D) != 0;
323  }
324 
325  DeclContext *getEntity() const { return Entity; }
326  void setEntity(DeclContext *E) { Entity = E; }
327 
328  bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
329 
331  return ErrorTrap.hasUnrecoverableErrorOccurred();
332  }
333 
334  /// isFunctionScope() - Return true if this scope is a function scope.
335  bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
336 
337  /// isClassScope - Return true if this scope is a class/struct/union scope.
338  bool isClassScope() const {
339  return (getFlags() & Scope::ClassScope);
340  }
341 
342  /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
343  /// method scope or is inside one.
345  if (const Scope *FnS = getFnParent()) {
346  assert(FnS->getParent() && "TUScope not created?");
347  return FnS->getParent()->isClassScope();
348  }
349  return false;
350  }
351 
352  /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
353  /// Objective-C method body. Note that this method is not constant time.
354  bool isInObjcMethodScope() const {
355  for (const Scope *S = this; S; S = S->getParent()) {
356  // If this scope is an objc method scope, then we succeed.
357  if (S->getFlags() & ObjCMethodScope)
358  return true;
359  }
360  return false;
361  }
362 
363  /// isInObjcMethodOuterScope - Return true if this scope is an
364  /// Objective-C method outer most body.
366  if (const Scope *S = this) {
367  // If this scope is an objc method scope, then we succeed.
368  if (S->getFlags() & ObjCMethodScope)
369  return true;
370  }
371  return false;
372  }
373 
374  /// isTemplateParamScope - Return true if this scope is a C++
375  /// template parameter scope.
376  bool isTemplateParamScope() const {
377  return getFlags() & Scope::TemplateParamScope;
378  }
379 
380  /// isFunctionPrototypeScope - Return true if this scope is a
381  /// function prototype scope.
383  return getFlags() & Scope::FunctionPrototypeScope;
384  }
385 
386  /// isAtCatchScope - Return true if this scope is \@catch.
387  bool isAtCatchScope() const {
388  return getFlags() & Scope::AtCatchScope;
389  }
390 
391  /// isSwitchScope - Return true if this scope is a switch scope.
392  bool isSwitchScope() const {
393  for (const Scope *S = this; S; S = S->getParent()) {
394  if (S->getFlags() & Scope::SwitchScope)
395  return true;
396  else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
397  Scope::BlockScope | Scope::TemplateParamScope |
398  Scope::FunctionPrototypeScope |
399  Scope::AtCatchScope | Scope::ObjCMethodScope))
400  return false;
401  }
402  return false;
403  }
404 
405  /// Determines whether this scope is the OpenMP directive scope
406  bool isOpenMPDirectiveScope() const {
407  return (getFlags() & Scope::OpenMPDirectiveScope);
408  }
409 
410  /// Determine whether this scope is some OpenMP loop directive scope
411  /// (for example, 'omp for', 'omp simd').
413  if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
414  assert(isOpenMPDirectiveScope() &&
415  "OpenMP loop directive scope is not a directive scope");
416  return true;
417  }
418  return false;
419  }
420 
421  /// Determine whether this scope is (or is nested into) some OpenMP
422  /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
424  return getFlags() & Scope::OpenMPSimdDirectiveScope;
425  }
426 
427  /// Determine whether this scope is a loop having OpenMP loop
428  /// directive attached.
429  bool isOpenMPLoopScope() const {
430  const Scope *P = getParent();
431  return P && P->isOpenMPLoopDirectiveScope();
432  }
433 
434  /// Determine whether this scope is a C++ 'try' block.
435  bool isTryScope() const { return getFlags() & Scope::TryScope; }
436 
437  /// Determine whether this scope is a SEH '__try' block.
438  bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
439 
440  /// Determine whether this scope is a SEH '__except' block.
441  bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
442 
443  /// Determine whether this scope is a compound statement scope.
444  bool isCompoundStmtScope() const {
445  return getFlags() & Scope::CompoundStmtScope;
446  }
447 
448  /// Returns if rhs has a higher scope depth than this.
449  ///
450  /// The caller is responsible for calling this only if one of the two scopes
451  /// is an ancestor of the other.
452  bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
453 
454  /// containedInPrototypeScope - Return true if this or a parent scope
455  /// is a FunctionPrototypeScope.
456  bool containedInPrototypeScope() const;
457 
459  UsingDirectives.push_back(UDir);
460  }
461 
462  using using_directives_range =
463  llvm::iterator_range<UsingDirectivesTy::iterator>;
464 
466  return using_directives_range(UsingDirectives.begin(),
467  UsingDirectives.end());
468  }
469 
471  if (NRVO.getInt())
472  return;
473  if (NRVO.getPointer() == nullptr) {
474  NRVO.setPointer(VD);
475  return;
476  }
477  if (NRVO.getPointer() != VD)
478  setNoNRVO();
479  }
480 
481  void setNoNRVO() {
482  NRVO.setInt(true);
483  NRVO.setPointer(nullptr);
484  }
485 
486  void mergeNRVOIntoParent();
487 
488  /// Init - This is used by the parser to implement scope caching.
489  void Init(Scope *parent, unsigned flags);
490 
491  /// Sets up the specified scope flags and adjusts the scope state
492  /// variables accordingly.
493  void AddFlags(unsigned Flags);
494 
495  void dumpImpl(raw_ostream &OS) const;
496  void dump() const;
497 };
498 
499 } // namespace clang
500 
501 #endif // LLVM_CLANG_SEMA_SCOPE_H
Scope * getFnParent()
Definition: Scope.h:231
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
const Scope * getTemplateParamParent() const
Definition: Scope.h:261
bool isBlockScope() const
isBlockScope - Return true if this scope correspond to a closure.
Definition: Scope.h:223
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
llvm::iterator_range< DeclSetTy::iterator > decl_range
Definition: Scope.h:279
Scope * getBlockParent()
Definition: Scope.h:257
void setFlags(unsigned F)
Definition: Scope.h:220
StringRef P
bool isTemplateParamScope() const
isTemplateParamScope - Return true if this scope is a C++ template parameter scope.
Definition: Scope.h:376
void AddDecl(Decl *D)
Definition: Scope.h:287
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Scope * getContinueParent()
getContinueParent - Return the closest scope that a continue statement would be affected by...
Definition: Scope.h:240
unsigned getDepth() const
Returns the depth of this scope. The translation-unit has scope depth 0.
Definition: Scope.h:264
Represents a variable declaration or definition.
Definition: Decl.h:813
ScopeFlags
ScopeFlags - These are bitfields that are or&#39;d together when creating a scope, which defines the sort...
Definition: Scope.h:45
bool isInObjcMethodScope() const
isInObjcMethodScope - Return true if this scope is, or is contained in, an Objective-C method body...
Definition: Scope.h:354
RAII class that determines when any errors have occurred between the time the instance was created an...
Definition: Diagnostic.h:999
Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
Definition: Scope.h:212
Scope * getTemplateParamParent()
Definition: Scope.h:260
bool isAtCatchScope() const
isAtCatchScope - Return true if this scope is @catch.
Definition: Scope.h:387
unsigned getFunctionPrototypeDepth() const
Returns the number of function prototype scopes in this scope chain.
Definition: Scope.h:268
void decrementMSManglingNumber()
Definition: Scope.h:302
void setNoNRVO()
Definition: Scope.h:481
Scope * getBreakParent()
getBreakParent - Return the closest scope that a break statement would be affected by...
Definition: Scope.h:250
bool isFunctionPrototypeScope() const
isFunctionPrototypeScope - Return true if this scope is a function prototype scope.
Definition: Scope.h:382
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
bool isFunctionScope() const
isFunctionScope() - Return true if this scope is a function scope.
Definition: Scope.h:335
unsigned getMSLastManglingNumber() const
Definition: Scope.h:309
Defines the Diagnostic-related interfaces.
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
using_directives_range using_directives()
Definition: Scope.h:465
void incrementMSManglingNumber()
Definition: Scope.h:295
NodeId Parent
Definition: ASTDiff.cpp:192
unsigned getFlags() const
getFlags - Return the flags for this scope.
Definition: Scope.h:218
bool decl_empty() const
Definition: Scope.h:285
bool isInCXXInlineMethodScope() const
isInCXXInlineMethodScope - Return true if this scope is a C++ inline method scope or is inside one...
Definition: Scope.h:344
bool isInObjcMethodOuterScope() const
isInObjcMethodOuterScope - Return true if this scope is an Objective-C method outer most body...
Definition: Scope.h:365
bool isDeclScope(Decl *D)
isDeclScope - Return true if this is the scope that the specified decl is declared in...
Definition: Scope.h:321
DeclContext * getEntity() const
Definition: Scope.h:325
Scope * getMSLastManglingParent()
Definition: Scope.h:236
bool isSEHTryScope() const
Determine whether this scope is a SEH &#39;__try&#39; block.
Definition: Scope.h:438
int Depth
Definition: ASTDiff.cpp:191
bool isClassScope() const
isClassScope - Return true if this scope is a class/struct/union scope.
Definition: Scope.h:338
decl_range decls() const
Definition: Scope.h:281
bool hasErrorOccurred() const
Definition: Scope.h:328
bool isCompoundStmtScope() const
Determine whether this scope is a compound statement scope.
Definition: Scope.h:444
bool Contains(const Scope &rhs) const
Returns if rhs has a higher scope depth than this.
Definition: Scope.h:452
unsigned getMSCurManglingNumber() const
Definition: Scope.h:315
bool isOpenMPLoopScope() const
Determine whether this scope is a loop having OpenMP loop directive attached.
Definition: Scope.h:429
const Scope * getBreakParent() const
Definition: Scope.h:253
bool hasErrorOccurred() const
Determine whether any errors have occurred since this object instance was created.
Definition: Diagnostic.h:1010
Scope * getParent()
Definition: Scope.h:227
void setEntity(DeclContext *E)
Definition: Scope.h:326
const Scope * getContinueParent() const
Definition: Scope.h:244
bool isOpenMPDirectiveScope() const
Determines whether this scope is the OpenMP directive scope.
Definition: Scope.h:406
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
const Scope * getParent() const
getParent - Return the scope that this is nested in.
Definition: Scope.h:226
bool isSEHExceptScope() const
Determine whether this scope is a SEH &#39;__except&#39; block.
Definition: Scope.h:441
void addNRVOCandidate(VarDecl *VD)
Definition: Scope.h:470
unsigned getNextFunctionPrototypeIndex()
Return the number of parameters declared in this function prototype, increasing it by one for the nex...
Definition: Scope.h:274
bool hasUnrecoverableErrorOccurred() const
Determine whether any unrecoverable errors have occurred since this object instance was created...
Definition: Diagnostic.h:1016
void PushUsingDirective(UsingDirectiveDecl *UDir)
Definition: Scope.h:458
void RemoveDecl(Decl *D)
Definition: Scope.h:291
bool isOpenMPLoopDirectiveScope() const
Determine whether this scope is some OpenMP loop directive scope (for example, &#39;omp for&#39;...
Definition: Scope.h:412
const Scope * getMSLastManglingParent() const
Definition: Scope.h:233
bool isOpenMPSimdDirectiveScope() const
Determine whether this scope is (or is nested into) some OpenMP loop simd directive scope (for exampl...
Definition: Scope.h:423
bool isSwitchScope() const
isSwitchScope - Return true if this scope is a switch scope.
Definition: Scope.h:392
const Scope * getFnParent() const
getFnParent - Return the closest scope that is a function body.
Definition: Scope.h:230
bool isTryScope() const
Determine whether this scope is a C++ &#39;try&#39; block.
Definition: Scope.h:435
bool hasUnrecoverableErrorOccurred() const
Definition: Scope.h:330
Represents C++ using-directive.
Definition: DeclCXX.h:2916
const Scope * getBlockParent() const
Definition: Scope.h:258
llvm::iterator_range< UsingDirectivesTy::iterator > using_directives_range
Definition: Scope.h:463