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