Scope.h revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
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
22namespace llvm {
23
24class raw_ostream;
25
26}
27
28namespace clang {
29
30class Decl;
31class UsingDirectiveDecl;
32class VarDecl;
33
34/// Scope - A scope is a transient data structure that is used while parsing the
35/// program.  It assists with resolving identifiers to the appropriate
36/// declaration.
37///
38class Scope {
39public:
40  /// ScopeFlags - These are bitfields that are or'd together when creating a
41  /// scope, which defines the sorts of things the scope contains.
42  enum ScopeFlags {
43    /// \brief This indicates that the scope corresponds to a function, which
44    /// means that labels are set here.
45    FnScope       = 0x01,
46
47    /// \brief This is a while, do, switch, for, etc that can have break
48    /// statements embedded into it.
49    BreakScope    = 0x02,
50
51    /// \brief This is a while, do, for, which can have continue statements
52    /// embedded into it.
53    ContinueScope = 0x04,
54
55    /// \brief This is a scope that can contain a declaration.  Some scopes
56    /// just contain loop constructs but don't contain decls.
57    DeclScope = 0x08,
58
59    /// \brief The controlling scope in a if/switch/while/for statement.
60    ControlScope = 0x10,
61
62    /// \brief The scope of a struct/union/class definition.
63    ClassScope = 0x20,
64
65    /// \brief This is a scope that corresponds to a block/closure object.
66    /// Blocks serve as top-level scopes for some objects like labels, they
67    /// also prevent things like break and continue.  BlockScopes always have
68    /// the FnScope and DeclScope flags set as well.
69    BlockScope = 0x40,
70
71    /// \brief This is a scope that corresponds to the
72    /// template parameters of a C++ template. Template parameter
73    /// scope starts at the 'template' keyword and ends when the
74    /// template declaration ends.
75    TemplateParamScope = 0x80,
76
77    /// \brief This is a scope that corresponds to the
78    /// parameters within a function prototype.
79    FunctionPrototypeScope = 0x100,
80
81    /// \brief This is a scope that corresponds to the parameters within
82    /// a function prototype for a function declaration (as opposed to any
83    /// other kind of function declarator). Always has FunctionPrototypeScope
84    /// set as well.
85    FunctionDeclarationScope = 0x200,
86
87    /// \brief This is a scope that corresponds to the Objective-C
88    /// \@catch statement.
89    AtCatchScope = 0x400,
90
91    /// \brief This scope corresponds to an Objective-C method body.
92    /// It always has FnScope and DeclScope set as well.
93    ObjCMethodScope = 0x800,
94
95    /// \brief This is a scope that corresponds to a switch statement.
96    SwitchScope = 0x1000,
97
98    /// \brief This is the scope of a C++ try statement.
99    TryScope = 0x2000,
100
101    /// \brief This is the scope for a function-level C++ try or catch scope.
102    FnTryCatchScope = 0x4000,
103
104    /// \brief This is the scope of OpenMP executable directive
105    OpenMPDirectiveScope = 0x8000
106  };
107private:
108  /// The parent scope for this scope.  This is null for the translation-unit
109  /// scope.
110  Scope *AnyParent;
111
112  /// Depth - This is the depth of this scope.  The translation-unit scope has
113  /// depth 0.
114  unsigned short Depth;
115
116  /// Flags - This contains a set of ScopeFlags, which indicates how the scope
117  /// interrelates with other control flow statements.
118  unsigned short Flags;
119
120  /// \brief Declarations with static linkage are mangled with the number of
121  /// scopes seen as a component.
122  unsigned short MSLocalManglingNumber;
123
124  /// PrototypeDepth - This is the number of function prototype scopes
125  /// enclosing this scope, including this scope.
126  unsigned short PrototypeDepth;
127
128  /// PrototypeIndex - This is the number of parameters currently
129  /// declared in this scope.
130  unsigned short PrototypeIndex;
131
132  /// FnParent - If this scope has a parent scope that is a function body, this
133  /// pointer is non-null and points to it.  This is used for label processing.
134  Scope *FnParent;
135  Scope *MSLocalManglingParent;
136
137  /// BreakParent/ContinueParent - This is a direct link to the innermost
138  /// BreakScope/ContinueScope which contains the contents of this scope
139  /// for control flow purposes (and might be this scope itself), or null
140  /// if there is no such scope.
141  Scope *BreakParent, *ContinueParent;
142
143  /// BlockParent - This is a direct link to the immediately containing
144  /// BlockScope if this scope is not one, or null if there is none.
145  Scope *BlockParent;
146
147  /// TemplateParamParent - This is a direct link to the
148  /// immediately containing template parameter scope. In the
149  /// case of nested templates, template parameter scopes can have
150  /// other template parameter scopes as parents.
151  Scope *TemplateParamParent;
152
153  /// DeclsInScope - This keeps track of all declarations in this scope.  When
154  /// the declaration is added to the scope, it is set as the current
155  /// declaration for the identifier in the IdentifierTable.  When the scope is
156  /// popped, these declarations are removed from the IdentifierTable's notion
157  /// of current declaration.  It is up to the current Action implementation to
158  /// implement these semantics.
159  typedef llvm::SmallPtrSet<Decl *, 32> DeclSetTy;
160  DeclSetTy DeclsInScope;
161
162  /// The DeclContext with which this scope is associated. For
163  /// example, the entity of a class scope is the class itself, the
164  /// entity of a function scope is a function, etc.
165  DeclContext *Entity;
166
167  typedef SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
168  UsingDirectivesTy UsingDirectives;
169
170  /// \brief Used to determine if errors occurred in this scope.
171  DiagnosticErrorTrap ErrorTrap;
172
173  /// A lattice consisting of undefined, a single NRVO candidate variable in
174  /// this scope, or over-defined. The bit is true when over-defined.
175  llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
176
177public:
178  Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
179    : ErrorTrap(Diag) {
180    Init(Parent, ScopeFlags);
181  }
182
183  /// getFlags - Return the flags for this scope.
184  ///
185  unsigned getFlags() const { return Flags; }
186  void setFlags(unsigned F) { Flags = F; }
187
188  /// isBlockScope - Return true if this scope correspond to a closure.
189  bool isBlockScope() const { return Flags & BlockScope; }
190
191  /// getParent - Return the scope that this is nested in.
192  ///
193  const Scope *getParent() const { return AnyParent; }
194  Scope *getParent() { return AnyParent; }
195
196  /// getFnParent - Return the closest scope that is a function body.
197  ///
198  const Scope *getFnParent() const { return FnParent; }
199  Scope *getFnParent() { return FnParent; }
200
201  const Scope *getMSLocalManglingParent() const {
202    return MSLocalManglingParent;
203  }
204  Scope *getMSLocalManglingParent() { return MSLocalManglingParent; }
205
206  /// getContinueParent - Return the closest scope that a continue statement
207  /// would be affected by.
208  Scope *getContinueParent() {
209    return ContinueParent;
210  }
211
212  const Scope *getContinueParent() const {
213    return const_cast<Scope*>(this)->getContinueParent();
214  }
215
216  /// getBreakParent - Return the closest scope that a break statement
217  /// would be affected by.
218  Scope *getBreakParent() {
219    return BreakParent;
220  }
221  const Scope *getBreakParent() const {
222    return const_cast<Scope*>(this)->getBreakParent();
223  }
224
225  Scope *getBlockParent() { return BlockParent; }
226  const Scope *getBlockParent() const { return BlockParent; }
227
228  Scope *getTemplateParamParent() { return TemplateParamParent; }
229  const Scope *getTemplateParamParent() const { return TemplateParamParent; }
230
231  /// Returns the number of function prototype scopes in this scope
232  /// chain.
233  unsigned getFunctionPrototypeDepth() const {
234    return PrototypeDepth;
235  }
236
237  /// Return the number of parameters declared in this function
238  /// prototype, increasing it by one for the next call.
239  unsigned getNextFunctionPrototypeIndex() {
240    assert(isFunctionPrototypeScope());
241    return PrototypeIndex++;
242  }
243
244  typedef llvm::iterator_range<DeclSetTy::iterator> decl_range;
245  decl_range decls() const {
246    return decl_range(DeclsInScope.begin(), DeclsInScope.end());
247  }
248  bool decl_empty() const { return DeclsInScope.empty(); }
249
250  void AddDecl(Decl *D) {
251    DeclsInScope.insert(D);
252  }
253
254  void RemoveDecl(Decl *D) {
255    DeclsInScope.erase(D);
256  }
257
258  void incrementMSLocalManglingNumber() {
259    if (Scope *MSLMP = getMSLocalManglingParent())
260      MSLMP->MSLocalManglingNumber += 1;
261  }
262
263  void decrementMSLocalManglingNumber() {
264    if (Scope *MSLMP = getMSLocalManglingParent())
265      MSLMP->MSLocalManglingNumber -= 1;
266  }
267
268  unsigned getMSLocalManglingNumber() const {
269    if (const Scope *MSLMP = getMSLocalManglingParent())
270      return MSLMP->MSLocalManglingNumber;
271    return 1;
272  }
273
274  /// isDeclScope - Return true if this is the scope that the specified decl is
275  /// declared in.
276  bool isDeclScope(Decl *D) {
277    return DeclsInScope.count(D) != 0;
278  }
279
280  DeclContext *getEntity() const { return Entity; }
281  void setEntity(DeclContext *E) { Entity = E; }
282
283  bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
284
285  bool hasUnrecoverableErrorOccurred() const {
286    return ErrorTrap.hasUnrecoverableErrorOccurred();
287  }
288
289  /// isClassScope - Return true if this scope is a class/struct/union scope.
290  bool isClassScope() const {
291    return (getFlags() & Scope::ClassScope);
292  }
293
294  /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
295  /// method scope or is inside one.
296  bool isInCXXInlineMethodScope() const {
297    if (const Scope *FnS = getFnParent()) {
298      assert(FnS->getParent() && "TUScope not created?");
299      return FnS->getParent()->isClassScope();
300    }
301    return false;
302  }
303
304  /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
305  /// Objective-C method body.  Note that this method is not constant time.
306  bool isInObjcMethodScope() const {
307    for (const Scope *S = this; S; S = S->getParent()) {
308      // If this scope is an objc method scope, then we succeed.
309      if (S->getFlags() & ObjCMethodScope)
310        return true;
311    }
312    return false;
313  }
314
315  /// isInObjcMethodOuterScope - Return true if this scope is an
316  /// Objective-C method outer most body.
317  bool isInObjcMethodOuterScope() const {
318    if (const Scope *S = this) {
319      // If this scope is an objc method scope, then we succeed.
320      if (S->getFlags() & ObjCMethodScope)
321        return true;
322    }
323    return false;
324  }
325
326
327  /// isTemplateParamScope - Return true if this scope is a C++
328  /// template parameter scope.
329  bool isTemplateParamScope() const {
330    return getFlags() & Scope::TemplateParamScope;
331  }
332
333  /// isFunctionPrototypeScope - Return true if this scope is a
334  /// function prototype scope.
335  bool isFunctionPrototypeScope() const {
336    return getFlags() & Scope::FunctionPrototypeScope;
337  }
338
339  /// isAtCatchScope - Return true if this scope is \@catch.
340  bool isAtCatchScope() const {
341    return getFlags() & Scope::AtCatchScope;
342  }
343
344  /// isSwitchScope - Return true if this scope is a switch scope.
345  bool isSwitchScope() const {
346    for (const Scope *S = this; S; S = S->getParent()) {
347      if (S->getFlags() & Scope::SwitchScope)
348        return true;
349      else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
350                                Scope::BlockScope | Scope::TemplateParamScope |
351                                Scope::FunctionPrototypeScope |
352                                Scope::AtCatchScope | Scope::ObjCMethodScope))
353        return false;
354    }
355    return false;
356  }
357
358  /// \brief Determines whether this scope is the OpenMP directive scope
359  bool isOpenMPDirectiveScope() const {
360    return (getFlags() & Scope::OpenMPDirectiveScope);
361  }
362
363  /// \brief Determine whether this scope is a C++ 'try' block.
364  bool isTryScope() const { return getFlags() & Scope::TryScope; }
365
366  /// containedInPrototypeScope - Return true if this or a parent scope
367  /// is a FunctionPrototypeScope.
368  bool containedInPrototypeScope() const;
369
370  void PushUsingDirective(UsingDirectiveDecl *UDir) {
371    UsingDirectives.push_back(UDir);
372  }
373
374  typedef llvm::iterator_range<UsingDirectivesTy::iterator>
375    using_directives_range;
376
377  using_directives_range using_directives() {
378    return using_directives_range(UsingDirectives.begin(),
379                                  UsingDirectives.end());
380  }
381
382  void addNRVOCandidate(VarDecl *VD) {
383    if (NRVO.getInt())
384      return;
385    if (NRVO.getPointer() == nullptr) {
386      NRVO.setPointer(VD);
387      return;
388    }
389    if (NRVO.getPointer() != VD)
390      setNoNRVO();
391  }
392
393  void setNoNRVO() {
394    NRVO.setInt(1);
395    NRVO.setPointer(nullptr);
396  }
397
398  void mergeNRVOIntoParent();
399
400  /// Init - This is used by the parser to implement scope caching.
401  ///
402  void Init(Scope *parent, unsigned flags);
403
404  /// \brief Sets up the specified scope flags and adjusts the scope state
405  /// variables accordingly.
406  ///
407  void AddFlags(unsigned Flags);
408
409  void dumpImpl(raw_ostream &OS) const;
410  void dump() const;
411};
412
413}  // end namespace clang
414
415#endif
416