Coverage Report

Created: 2018-09-19 08:35

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Sema/Scope.h
Line
Count
Source (jump to first uncovered line)
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
198
  using UsingDirectivesTy = SmallVector<UsingDirectiveDecl *, 2>;
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:
211
  Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
212
215k
      : ErrorTrap(Diag) {
213
215k
    Init(Parent, ScopeFlags);
214
215k
  }
215
216
  /// getFlags - Return the flags for this scope.
217
207M
  unsigned getFlags() const { return Flags; }
218
219
3.10M
  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
411k
  const Scope *getParent() const { return AnyParent; }
226
463M
  Scope *getParent() { return AnyParent; }
227
228
  /// getFnParent - Return the closest scope that is a function body.
229
315k
  const Scope *getFnParent() const { return FnParent; }
230
4.86M
  Scope *getFnParent() { return FnParent; }
231
232
25.3M
  const Scope *getMSLastManglingParent() const {
233
25.3M
    return MSLastManglingParent;
234
25.3M
  }
235
9.15M
  Scope *getMSLastManglingParent() { return MSLastManglingParent; }
236
237
  /// getContinueParent - Return the closest scope that a continue statement
238
  /// would be affected by.
239
8.05k
  Scope *getContinueParent() {
240
8.05k
    return ContinueParent;
241
8.05k
  }
242
243
0
  const Scope *getContinueParent() const {
244
0
    return const_cast<Scope*>(this)->getContinueParent();
245
0
  }
246
247
  /// getBreakParent - Return the closest scope that a break statement
248
  /// would be affected by.
249
115k
  Scope *getBreakParent() {
250
115k
    return BreakParent;
251
115k
  }
252
0
  const Scope *getBreakParent() const {
253
0
    return const_cast<Scope*>(this)->getBreakParent();
254
0
  }
255
256
118k
  Scope *getBlockParent() { return BlockParent; }
257
0
  const Scope *getBlockParent() const { return BlockParent; }
258
259
600k
  Scope *getTemplateParamParent() { return TemplateParamParent; }
260
0
  const Scope *getTemplateParamParent() const { return TemplateParamParent; }
261
262
  /// Returns the depth of this scope. The translation-unit has scope depth 0.
263
5.50k
  unsigned getDepth() const { return Depth; }
264
265
  /// Returns the number of function prototype scopes in this scope
266
  /// chain.
267
9.57M
  unsigned getFunctionPrototypeDepth() const {
268
9.57M
    return PrototypeDepth;
269
9.57M
  }
270
271
  /// Return the number of parameters declared in this function
272
  /// prototype, increasing it by one for the next call.
273
9.57M
  unsigned getNextFunctionPrototypeIndex() {
274
9.57M
    assert(isFunctionPrototypeScope());
275
9.57M
    return PrototypeIndex++;
276
9.57M
  }
277
278
  using decl_range = llvm::iterator_range<DeclSetTy::iterator>;
279
280
11.8M
  decl_range decls() const {
281
11.8M
    return decl_range(DeclsInScope.begin(), DeclsInScope.end());
282
11.8M
  }
283
284
15.0M
  bool decl_empty() const { return DeclsInScope.empty(); }
285
286
30.7M
  void AddDecl(Decl *D) {
287
30.7M
    DeclsInScope.insert(D);
288
30.7M
  }
289
290
376k
  void RemoveDecl(Decl *D) {
291
376k
    DeclsInScope.erase(D);
292
376k
  }
293
294
8.93M
  void incrementMSManglingNumber() {
295
8.93M
    if (Scope *MSLMP = getMSLastManglingParent()) {
296
8.70M
      MSLMP->MSLastManglingNumber += 1;
297
8.70M
      MSCurManglingNumber += 1;
298
8.70M
    }
299
8.93M
  }
300
301
220k
  void decrementMSManglingNumber() {
302
220k
    if (Scope *MSLMP = getMSLastManglingParent()) {
303
220k
      MSLMP->MSLastManglingNumber -= 1;
304
220k
      MSCurManglingNumber -= 1;
305
220k
    }
306
220k
  }
307
308
25.3M
  unsigned getMSLastManglingNumber() const {
309
25.3M
    if (const Scope *MSLMP = getMSLastManglingParent())
310
8.24M
      return MSLMP->MSLastManglingNumber;
311
17.1M
    return 1;
312
17.1M
  }
313
314
20
  unsigned getMSCurManglingNumber() const {
315
20
    return MSCurManglingNumber;
316
20
  }
317
318
  /// isDeclScope - Return true if this is the scope that the specified decl is
319
  /// declared in.
320
113M
  bool isDeclScope(Decl *D) {
321
113M
    return DeclsInScope.count(D) != 0;
322
113M
  }
323
324
638M
  DeclContext *getEntity() const { return Entity; }
325
4.59M
  void setEntity(DeclContext *E) { Entity = E; }
326
327
  bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
328
329
25.5M
  bool hasUnrecoverableErrorOccurred() const {
330
25.5M
    return ErrorTrap.hasUnrecoverableErrorOccurred();
331
25.5M
  }
332
333
  /// isFunctionScope() - Return true if this scope is a function scope.
334
204
  bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
335
336
  /// isClassScope - Return true if this scope is a class/struct/union scope.
337
23.3M
  bool isClassScope() const {
338
23.3M
    return (getFlags() & Scope::ClassScope);
339
23.3M
  }
340
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();
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
132k
  bool isInObjcMethodScope() const {
354
313k
    for (const Scope *S = this; S; 
S = S->getParent()181k
) {
355
182k
      // If this scope is an objc method scope, then we succeed.
356
182k
      if (S->getFlags() & ObjCMethodScope)
357
955
        return true;
358
182k
    }
359
132k
    
return false131k
;
360
132k
  }
361
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)
368
        return true;
369
    }
370
    return false;
371
  }
372
373
  /// isTemplateParamScope - Return true if this scope is a C++
374
  /// template parameter scope.
375
131M
  bool isTemplateParamScope() const {
376
131M
    return getFlags() & Scope::TemplateParamScope;
377
131M
  }
378
379
  /// isFunctionPrototypeScope - Return true if this scope is a
380
  /// function prototype scope.
381
10.7M
  bool isFunctionPrototypeScope() const {
382
10.7M
    return getFlags() & Scope::FunctionPrototypeScope;
383
10.7M
  }
384
385
  /// isAtCatchScope - Return true if this scope is \@catch.
386
21
  bool isAtCatchScope() const {
387
21
    return getFlags() & Scope::AtCatchScope;
388
21
  }
389
390
  /// isSwitchScope - Return true if this scope is a switch scope.
391
19
  bool isSwitchScope() const {
392
53
    for (const Scope *S = this; S; 
S = S->getParent()34
) {
393
53
      if (S->getFlags() & Scope::SwitchScope)
394
18
        return true;
395
35
      else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
396
35
                                Scope::BlockScope | Scope::TemplateParamScope |
397
35
                                Scope::FunctionPrototypeScope |
398
35
                                Scope::AtCatchScope | Scope::ObjCMethodScope))
399
1
        return false;
400
53
    }
401
19
    
return false0
;
402
19
  }
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').
411
61.2k
  bool isOpenMPLoopDirectiveScope() const {
412
61.2k
    if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
413
106
      assert(isOpenMPDirectiveScope() &&
414
106
             "OpenMP loop directive scope is not a directive scope");
415
106
      return true;
416
106
    }
417
61.1k
    return false;
418
61.1k
  }
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').
422
19.3k
  bool isOpenMPSimdDirectiveScope() const {
423
19.3k
    return getFlags() & Scope::OpenMPSimdDirectiveScope;
424
19.3k
  }
425
426
  /// Determine whether this scope is a loop having OpenMP loop
427
  /// directive attached.
428
61.2k
  bool isOpenMPLoopScope() const {
429
61.2k
    const Scope *P = getParent();
430
61.2k
    return P && P->isOpenMPLoopDirectiveScope();
431
61.2k
  }
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
65
  bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
438
439
  /// Determine whether this scope is a SEH '__except' block.
440
61
  bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
441
442
  /// Determine whether this scope is a compound statement scope.
443
882
  bool isCompoundStmtScope() const {
444
882
    return getFlags() & Scope::CompoundStmtScope;
445
882
  }
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
32
  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
457
1.39k
  void PushUsingDirective(UsingDirectiveDecl *UDir) {
458
1.39k
    UsingDirectives.push_back(UDir);
459
1.39k
  }
460
461
  using using_directives_range =
462
      llvm::iterator_range<UsingDirectivesTy::iterator>;
463
464
43.6M
  using_directives_range using_directives() {
465
43.6M
    return using_directives_range(UsingDirectives.begin(),
466
43.6M
                                  UsingDirectives.end());
467
43.6M
  }
468
469
61.2k
  void addNRVOCandidate(VarDecl *VD) {
470
61.2k
    if (NRVO.getInt())
471
3.92k
      return;
472
57.3k
    if (NRVO.getPointer() == nullptr) {
473
56.3k
      NRVO.setPointer(VD);
474
56.3k
      return;
475
56.3k
    }
476
975
    if (NRVO.getPointer() != VD)
477
21
      setNoNRVO();
478
975
  }
479
480
2.97M
  void setNoNRVO() {
481
2.97M
    NRVO.setInt(true);
482
2.97M
    NRVO.setPointer(nullptr);
483
2.97M
  }
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