clang  7.0.0svn
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/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"
22 #include <cassert>
23 
24 namespace llvm {
25 
26 class raw_ostream;
27 
28 } // namespace llvm
29 
30 namespace clang {
31 
32 class Decl;
33 class DeclContext;
34 class UsingDirectiveDecl;
35 class VarDecl;
36 
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
39 /// declaration.
40 class Scope {
41 public:
42  /// ScopeFlags - These are bitfields that are or'd together when creating a
43  /// scope, which defines the sorts of things the scope contains.
44  enum ScopeFlags {
45  /// This indicates that the scope corresponds to a function, which
46  /// means that labels are set here.
47  FnScope = 0x01,
48 
49  /// This is a while, do, switch, for, etc that can have break
50  /// statements embedded into it.
51  BreakScope = 0x02,
52 
53  /// This is a while, do, for, which can have continue statements
54  /// embedded into it.
55  ContinueScope = 0x04,
56 
57  /// This is a scope that can contain a declaration. Some scopes
58  /// just contain loop constructs but don't contain decls.
59  DeclScope = 0x08,
60 
61  /// The controlling scope in a if/switch/while/for statement.
62  ControlScope = 0x10,
63 
64  /// The scope of a struct/union/class definition.
65  ClassScope = 0x20,
66 
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.
71  BlockScope = 0x40,
72 
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,
78 
79  /// This is a scope that corresponds to the
80  /// parameters within a function prototype.
81  FunctionPrototypeScope = 0x100,
82 
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
86  /// set as well.
87  FunctionDeclarationScope = 0x200,
88 
89  /// This is a scope that corresponds to the Objective-C
90  /// \@catch statement.
91  AtCatchScope = 0x400,
92 
93  /// This scope corresponds to an Objective-C method body.
94  /// It always has FnScope and DeclScope set as well.
95  ObjCMethodScope = 0x800,
96 
97  /// This is a scope that corresponds to a switch statement.
98  SwitchScope = 0x1000,
99 
100  /// This is the scope of a C++ try statement.
101  TryScope = 0x2000,
102 
103  /// This is the scope for a function-level C++ try or catch scope.
104  FnTryCatchScope = 0x4000,
105 
106  /// This is the scope of OpenMP executable directive.
107  OpenMPDirectiveScope = 0x8000,
108 
109  /// This is the scope of some OpenMP loop directive.
110  OpenMPLoopDirectiveScope = 0x10000,
111 
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,
116 
117  /// This scope corresponds to an enum.
118  EnumScope = 0x40000,
119 
120  /// This scope corresponds to an SEH try.
121  SEHTryScope = 0x80000,
122 
123  /// This scope corresponds to an SEH except.
124  SEHExceptScope = 0x100000,
125 
126  /// We are currently in the filter expression of an SEH except block.
127  SEHFilterScope = 0x200000,
128 
129  /// This is a compound statement scope.
130  CompoundStmtScope = 0x400000,
131 
132  /// We are between inheritance colon and the real class/struct definition scope.
133  ClassInheritanceScope = 0x800000,
134  };
135 
136 private:
137  /// The parent scope for this scope. This is null for the translation-unit
138  /// scope.
139  Scope *AnyParent;
140 
141  /// Flags - This contains a set of ScopeFlags, which indicates how the scope
142  /// interrelates with other control flow statements.
143  unsigned Flags;
144 
145  /// Depth - This is the depth of this scope. The translation-unit scope has
146  /// depth 0.
147  unsigned short Depth;
148 
149  /// Declarations with static linkage are mangled with the number of
150  /// scopes seen as a component.
151  unsigned short MSLastManglingNumber;
152 
153  unsigned short MSCurManglingNumber;
154 
155  /// PrototypeDepth - This is the number of function prototype scopes
156  /// enclosing this scope, including this scope.
157  unsigned short PrototypeDepth;
158 
159  /// PrototypeIndex - This is the number of parameters currently
160  /// declared in this scope.
161  unsigned short PrototypeIndex;
162 
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.
165  Scope *FnParent;
166  Scope *MSLastManglingParent;
167 
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;
173 
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.
176  Scope *BlockParent;
177 
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;
183 
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;
192 
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.
196  DeclContext *Entity;
197 
199  UsingDirectivesTy UsingDirectives;
200 
201  /// Used to determine if errors occurred in this scope.
202  DiagnosticErrorTrap ErrorTrap;
203 
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;
207 
208  void setFlags(Scope *Parent, unsigned F);
209 
210 public:
212  : ErrorTrap(Diag) {
213  Init(Parent, ScopeFlags);
214  }
215 
216  /// getFlags - Return the flags for this scope.
217  unsigned getFlags() const { return Flags; }
218 
219  void setFlags(unsigned F) { setFlags(getParent(), F); }
220 
221  /// isBlockScope - Return true if this scope correspond to a closure.
222  bool isBlockScope() const { return Flags & BlockScope; }
223 
224  /// getParent - Return the scope that this is nested in.
225  const Scope *getParent() const { return AnyParent; }
226  Scope *getParent() { return AnyParent; }
227 
228  /// getFnParent - Return the closest scope that is a function body.
229  const Scope *getFnParent() const { return FnParent; }
230  Scope *getFnParent() { return FnParent; }
231 
233  return MSLastManglingParent;
234  }
235  Scope *getMSLastManglingParent() { return MSLastManglingParent; }
236 
237  /// getContinueParent - Return the closest scope that a continue statement
238  /// would be affected by.
240  return ContinueParent;
241  }
242 
243  const Scope *getContinueParent() const {
244  return const_cast<Scope*>(this)->getContinueParent();
245  }
246 
247  /// getBreakParent - Return the closest scope that a break statement
248  /// would be affected by.
250  return BreakParent;
251  }
252  const Scope *getBreakParent() const {
253  return const_cast<Scope*>(this)->getBreakParent();
254  }
255 
256  Scope *getBlockParent() { return BlockParent; }
257  const Scope *getBlockParent() const { return BlockParent; }
258 
259  Scope *getTemplateParamParent() { return TemplateParamParent; }
260  const Scope *getTemplateParamParent() const { return TemplateParamParent; }
261 
262  /// Returns the depth of this scope. The translation-unit has scope depth 0.
263  unsigned getDepth() const { return Depth; }
264 
265  /// Returns the number of function prototype scopes in this scope
266  /// chain.
267  unsigned getFunctionPrototypeDepth() const {
268  return PrototypeDepth;
269  }
270 
271  /// Return the number of parameters declared in this function
272  /// prototype, increasing it by one for the next call.
274  assert(isFunctionPrototypeScope());
275  return PrototypeIndex++;
276  }
277 
278  using decl_range = llvm::iterator_range<DeclSetTy::iterator>;
279 
280  decl_range decls() const {
281  return decl_range(DeclsInScope.begin(), DeclsInScope.end());
282  }
283 
284  bool decl_empty() const { return DeclsInScope.empty(); }
285 
286  void AddDecl(Decl *D) {
287  DeclsInScope.insert(D);
288  }
289 
290  void RemoveDecl(Decl *D) {
291  DeclsInScope.erase(D);
292  }
293 
295  if (Scope *MSLMP = getMSLastManglingParent()) {
296  MSLMP->MSLastManglingNumber += 1;
297  MSCurManglingNumber += 1;
298  }
299  }
300 
302  if (Scope *MSLMP = getMSLastManglingParent()) {
303  MSLMP->MSLastManglingNumber -= 1;
304  MSCurManglingNumber -= 1;
305  }
306  }
307 
308  unsigned getMSLastManglingNumber() const {
309  if (const Scope *MSLMP = getMSLastManglingParent())
310  return MSLMP->MSLastManglingNumber;
311  return 1;
312  }
313 
314  unsigned getMSCurManglingNumber() const {
315  return MSCurManglingNumber;
316  }
317 
318  /// isDeclScope - Return true if this is the scope that the specified decl is
319  /// declared in.
320  bool isDeclScope(Decl *D) {
321  return DeclsInScope.count(D) != 0;
322  }
323 
324  DeclContext *getEntity() const { return Entity; }
325  void setEntity(DeclContext *E) { Entity = E; }
326 
327  bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
328 
330  return ErrorTrap.hasUnrecoverableErrorOccurred();
331  }
332 
333  /// isFunctionScope() - Return true if this scope is a function scope.
334  bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
335 
336  /// isClassScope - Return true if this scope is a class/struct/union scope.
337  bool isClassScope() const {
338  return (getFlags() & Scope::ClassScope);
339  }
340 
341  /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
342  /// method scope or is inside one.
344  if (const Scope *FnS = getFnParent()) {
345  assert(FnS->getParent() && "TUScope not created?");
346  return FnS->getParent()->isClassScope();
347  }
348  return false;
349  }
350 
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)
357  return true;
358  }
359  return false;
360  }
361 
362  /// isInObjcMethodOuterScope - Return true if this scope is an
363  /// Objective-C method outer most body.
365  if (const Scope *S = this) {
366  // If this scope is an objc method scope, then we succeed.
367  if (S->getFlags() & ObjCMethodScope)
368  return true;
369  }
370  return false;
371  }
372 
373  /// isTemplateParamScope - Return true if this scope is a C++
374  /// template parameter scope.
375  bool isTemplateParamScope() const {
376  return getFlags() & Scope::TemplateParamScope;
377  }
378 
379  /// isFunctionPrototypeScope - Return true if this scope is a
380  /// function prototype scope.
382  return getFlags() & Scope::FunctionPrototypeScope;
383  }
384 
385  /// isAtCatchScope - Return true if this scope is \@catch.
386  bool isAtCatchScope() const {
387  return getFlags() & Scope::AtCatchScope;
388  }
389 
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)
394  return true;
395  else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
396  Scope::BlockScope | Scope::TemplateParamScope |
397  Scope::FunctionPrototypeScope |
398  Scope::AtCatchScope | Scope::ObjCMethodScope))
399  return false;
400  }
401  return false;
402  }
403 
404  /// Determines whether this scope is the OpenMP directive scope
405  bool isOpenMPDirectiveScope() const {
406  return (getFlags() & Scope::OpenMPDirectiveScope);
407  }
408 
409  /// Determine whether this scope is some OpenMP loop directive scope
410  /// (for example, 'omp for', 'omp simd').
412  if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
413  assert(isOpenMPDirectiveScope() &&
414  "OpenMP loop directive scope is not a directive scope");
415  return true;
416  }
417  return false;
418  }
419 
420  /// Determine whether this scope is (or is nested into) some OpenMP
421  /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
423  return getFlags() & Scope::OpenMPSimdDirectiveScope;
424  }
425 
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();
431  }
432 
433  /// Determine whether this scope is a C++ 'try' block.
434  bool isTryScope() const { return getFlags() & Scope::TryScope; }
435 
436  /// Determine whether this scope is a SEH '__try' block.
437  bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
438 
439  /// Determine whether this scope is a SEH '__except' block.
440  bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
441 
442  /// Determine whether this scope is a compound statement scope.
443  bool isCompoundStmtScope() const {
444  return getFlags() & Scope::CompoundStmtScope;
445  }
446 
447  /// Returns if rhs has a higher scope depth than this.
448  ///
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; }
452 
453  /// containedInPrototypeScope - Return true if this or a parent scope
454  /// is a FunctionPrototypeScope.
455  bool containedInPrototypeScope() const;
456 
458  UsingDirectives.push_back(UDir);
459  }
460 
461  using using_directives_range =
462  llvm::iterator_range<UsingDirectivesTy::iterator>;
463 
465  return using_directives_range(UsingDirectives.begin(),
466  UsingDirectives.end());
467  }
468 
470  if (NRVO.getInt())
471  return;
472  if (NRVO.getPointer() == nullptr) {
473  NRVO.setPointer(VD);
474  return;
475  }
476  if (NRVO.getPointer() != VD)
477  setNoNRVO();
478  }
479 
480  void setNoNRVO() {
481  NRVO.setInt(true);
482  NRVO.setPointer(nullptr);
483  }
484 
485  void mergeNRVOIntoParent();
486 
487  /// Init - This is used by the parser to implement scope caching.
488  void Init(Scope *parent, unsigned flags);
489 
490  /// Sets up the specified scope flags and adjusts the scope state
491  /// variables accordingly.
492  void AddFlags(unsigned Flags);
493 
494  void dumpImpl(raw_ostream &OS) const;
495  void dump() const;
496 };
497 
498 } // namespace clang
499 
500 #endif // LLVM_CLANG_SEMA_SCOPE_H
Scope * getFnParent()
Definition: Scope.h:230
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:260
bool isBlockScope() const
isBlockScope - Return true if this scope correspond to a closure.
Definition: Scope.h:222
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
llvm::iterator_range< DeclSetTy::iterator > decl_range
Definition: Scope.h:278
Scope * getBlockParent()
Definition: Scope.h:256
void setFlags(unsigned F)
Definition: Scope.h:219
StringRef P
bool isTemplateParamScope() const
isTemplateParamScope - Return true if this scope is a C++ template parameter scope.
Definition: Scope.h:375
void AddDecl(Decl *D)
Definition: Scope.h:286
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:239
unsigned getDepth() const
Returns the depth of this scope. The translation-unit has scope depth 0.
Definition: Scope.h:263
Represents a variable declaration or definition.
Definition: Decl.h:812
ScopeFlags
ScopeFlags - These are bitfields that are or&#39;d together when creating a scope, which defines the sort...
Definition: Scope.h:44
bool isInObjcMethodScope() const
isInObjcMethodScope - Return true if this scope is, or is contained in, an Objective-C method body...
Definition: Scope.h:353
RAII class that determines when any errors have occurred between the time the instance was created an...
Definition: Diagnostic.h:998
Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
Definition: Scope.h:211
Scope * getTemplateParamParent()
Definition: Scope.h:259
bool isAtCatchScope() const
isAtCatchScope - Return true if this scope is @catch.
Definition: Scope.h:386
unsigned getFunctionPrototypeDepth() const
Returns the number of function prototype scopes in this scope chain.
Definition: Scope.h:267
void decrementMSManglingNumber()
Definition: Scope.h:301
void setNoNRVO()
Definition: Scope.h:480
Scope * getBreakParent()
getBreakParent - Return the closest scope that a break statement would be affected by...
Definition: Scope.h:249
bool isFunctionPrototypeScope() const
isFunctionPrototypeScope - Return true if this scope is a function prototype scope.
Definition: Scope.h:381
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:334
unsigned getMSLastManglingNumber() const
Definition: Scope.h:308
Defines the Diagnostic-related interfaces.
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
using_directives_range using_directives()
Definition: Scope.h:464
void incrementMSManglingNumber()
Definition: Scope.h:294
NodeId Parent
Definition: ASTDiff.cpp:192
unsigned getFlags() const
getFlags - Return the flags for this scope.
Definition: Scope.h:217
bool decl_empty() const
Definition: Scope.h:284
bool isInCXXInlineMethodScope() const
isInCXXInlineMethodScope - Return true if this scope is a C++ inline method scope or is inside one...
Definition: Scope.h:343
bool isInObjcMethodOuterScope() const
isInObjcMethodOuterScope - Return true if this scope is an Objective-C method outer most body...
Definition: Scope.h:364
bool isDeclScope(Decl *D)
isDeclScope - Return true if this is the scope that the specified decl is declared in...
Definition: Scope.h:320
DeclContext * getEntity() const
Definition: Scope.h:324
Scope * getMSLastManglingParent()
Definition: Scope.h:235
bool isSEHTryScope() const
Determine whether this scope is a SEH &#39;__try&#39; block.
Definition: Scope.h:437
int Depth
Definition: ASTDiff.cpp:191
bool isClassScope() const
isClassScope - Return true if this scope is a class/struct/union scope.
Definition: Scope.h:337
decl_range decls() const
Definition: Scope.h:280
bool hasErrorOccurred() const
Definition: Scope.h:327
bool isCompoundStmtScope() const
Determine whether this scope is a compound statement scope.
Definition: Scope.h:443
bool Contains(const Scope &rhs) const
Returns if rhs has a higher scope depth than this.
Definition: Scope.h:451
unsigned getMSCurManglingNumber() const
Definition: Scope.h:314
bool isOpenMPLoopScope() const
Determine whether this scope is a loop having OpenMP loop directive attached.
Definition: Scope.h:428
const Scope * getBreakParent() const
Definition: Scope.h:252
bool hasErrorOccurred() const
Determine whether any errors have occurred since this object instance was created.
Definition: Diagnostic.h:1009
Scope * getParent()
Definition: Scope.h:226
void setEntity(DeclContext *E)
Definition: Scope.h:325
const Scope * getContinueParent() const
Definition: Scope.h:243
bool isOpenMPDirectiveScope() const
Determines whether this scope is the OpenMP directive scope.
Definition: Scope.h:405
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:225
bool isSEHExceptScope() const
Determine whether this scope is a SEH &#39;__except&#39; block.
Definition: Scope.h:440
void addNRVOCandidate(VarDecl *VD)
Definition: Scope.h:469
unsigned getNextFunctionPrototypeIndex()
Return the number of parameters declared in this function prototype, increasing it by one for the nex...
Definition: Scope.h:273
bool hasUnrecoverableErrorOccurred() const
Determine whether any unrecoverable errors have occurred since this object instance was created...
Definition: Diagnostic.h:1015
void PushUsingDirective(UsingDirectiveDecl *UDir)
Definition: Scope.h:457
void RemoveDecl(Decl *D)
Definition: Scope.h:290
bool isOpenMPLoopDirectiveScope() const
Determine whether this scope is some OpenMP loop directive scope (for example, &#39;omp for&#39;...
Definition: Scope.h:411
const Scope * getMSLastManglingParent() const
Definition: Scope.h:232
bool isOpenMPSimdDirectiveScope() const
Determine whether this scope is (or is nested into) some OpenMP loop simd directive scope (for exampl...
Definition: Scope.h:422
bool isSwitchScope() const
isSwitchScope - Return true if this scope is a switch scope.
Definition: Scope.h:391
const Scope * getFnParent() const
getFnParent - Return the closest scope that is a function body.
Definition: Scope.h:229
bool isTryScope() const
Determine whether this scope is a C++ &#39;try&#39; block.
Definition: Scope.h:434
bool hasUnrecoverableErrorOccurred() const
Definition: Scope.h:329
Represents C++ using-directive.
Definition: DeclCXX.h:2909
const Scope * getBlockParent() const
Definition: Scope.h:257
llvm::iterator_range< UsingDirectivesTy::iterator > using_directives_range
Definition: Scope.h:462