Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Sema/Scope.h
Line
Count
Source (jump to first uncovered line)
1
//===- Scope.h - Scope interface --------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the Scope interface.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_SEMA_SCOPE_H
14
#define LLVM_CLANG_SEMA_SCOPE_H
15
16
#include "clang/AST/Decl.h"
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
    /// This is the scope of a C++ catch statement.
136
    CatchScope = 0x1000000,
137
  };
138
139
private:
140
  /// The parent scope for this scope.  This is null for the translation-unit
141
  /// scope.
142
  Scope *AnyParent;
143
144
  /// Flags - This contains a set of ScopeFlags, which indicates how the scope
145
  /// interrelates with other control flow statements.
146
  unsigned Flags;
147
148
  /// Depth - This is the depth of this scope.  The translation-unit scope has
149
  /// depth 0.
150
  unsigned short Depth;
151
152
  /// Declarations with static linkage are mangled with the number of
153
  /// scopes seen as a component.
154
  unsigned short MSLastManglingNumber;
155
156
  unsigned short MSCurManglingNumber;
157
158
  /// PrototypeDepth - This is the number of function prototype scopes
159
  /// enclosing this scope, including this scope.
160
  unsigned short PrototypeDepth;
161
162
  /// PrototypeIndex - This is the number of parameters currently
163
  /// declared in this scope.
164
  unsigned short PrototypeIndex;
165
166
  /// FnParent - If this scope has a parent scope that is a function body, this
167
  /// pointer is non-null and points to it.  This is used for label processing.
168
  Scope *FnParent;
169
  Scope *MSLastManglingParent;
170
171
  /// BreakParent/ContinueParent - This is a direct link to the innermost
172
  /// BreakScope/ContinueScope which contains the contents of this scope
173
  /// for control flow purposes (and might be this scope itself), or null
174
  /// if there is no such scope.
175
  Scope *BreakParent, *ContinueParent;
176
177
  /// BlockParent - This is a direct link to the immediately containing
178
  /// BlockScope if this scope is not one, or null if there is none.
179
  Scope *BlockParent;
180
181
  /// TemplateParamParent - This is a direct link to the
182
  /// immediately containing template parameter scope. In the
183
  /// case of nested templates, template parameter scopes can have
184
  /// other template parameter scopes as parents.
185
  Scope *TemplateParamParent;
186
187
  /// DeclsInScope - This keeps track of all declarations in this scope.  When
188
  /// the declaration is added to the scope, it is set as the current
189
  /// declaration for the identifier in the IdentifierTable.  When the scope is
190
  /// popped, these declarations are removed from the IdentifierTable's notion
191
  /// of current declaration.  It is up to the current Action implementation to
192
  /// implement these semantics.
193
  using DeclSetTy = llvm::SmallPtrSet<Decl *, 32>;
194
  DeclSetTy DeclsInScope;
195
196
  /// The DeclContext with which this scope is associated. For
197
  /// example, the entity of a class scope is the class itself, the
198
  /// entity of a function scope is a function, etc.
199
  DeclContext *Entity;
200
201
  using UsingDirectivesTy = SmallVector<UsingDirectiveDecl *, 2>;
202
  UsingDirectivesTy UsingDirectives;
203
204
  /// Used to determine if errors occurred in this scope.
205
  DiagnosticErrorTrap ErrorTrap;
206
207
  /// A lattice consisting of undefined, a single NRVO candidate variable in
208
  /// this scope, or over-defined. The bit is true when over-defined.
209
  llvm::PointerIntPair<VarDecl *, 1, bool> NRVO;
210
211
  void setFlags(Scope *Parent, unsigned F);
212
213
public:
214
  Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag)
215
239k
      : ErrorTrap(Diag) {
216
239k
    Init(Parent, ScopeFlags);
217
239k
  }
218
219
  /// getFlags - Return the flags for this scope.
220
240M
  unsigned getFlags() const { return Flags; }
221
222
2.58M
  void setFlags(unsigned F) { setFlags(getParent(), F); }
223
224
  /// isBlockScope - Return true if this scope correspond to a closure.
225
0
  bool isBlockScope() const { return Flags & BlockScope; }
226
227
  /// getParent - Return the scope that this is nested in.
228
311k
  const Scope *getParent() const { return AnyParent; }
229
361M
  Scope *getParent() { return AnyParent; }
230
231
  /// getFnParent - Return the closest scope that is a function body.
232
221k
  const Scope *getFnParent() const { return FnParent; }
233
5.01M
  Scope *getFnParent() { return FnParent; }
234
235
25.4M
  const Scope *getMSLastManglingParent() const {
236
25.4M
    return MSLastManglingParent;
237
25.4M
  }
238
7.23M
  Scope *getMSLastManglingParent() { return MSLastManglingParent; }
239
240
  /// getContinueParent - Return the closest scope that a continue statement
241
  /// would be affected by.
242
7.37k
  Scope *getContinueParent() {
243
7.37k
    return ContinueParent;
244
7.37k
  }
245
246
0
  const Scope *getContinueParent() const {
247
0
    return const_cast<Scope*>(this)->getContinueParent();
248
0
  }
249
250
  /// getBreakParent - Return the closest scope that a break statement
251
  /// would be affected by.
252
50.5k
  Scope *getBreakParent() {
253
50.5k
    return BreakParent;
254
50.5k
  }
255
0
  const Scope *getBreakParent() const {
256
0
    return const_cast<Scope*>(this)->getBreakParent();
257
0
  }
258
259
69.2k
  Scope *getBlockParent() { return BlockParent; }
260
0
  const Scope *getBlockParent() const { return BlockParent; }
261
262
596k
  Scope *getTemplateParamParent() { return TemplateParamParent; }
263
0
  const Scope *getTemplateParamParent() const { return TemplateParamParent; }
264
265
  /// Returns the depth of this scope. The translation-unit has scope depth 0.
266
6.37k
  unsigned getDepth() const { return Depth; }
267
268
  /// Returns the number of function prototype scopes in this scope
269
  /// chain.
270
36.2M
  unsigned getFunctionPrototypeDepth() const {
271
36.2M
    return PrototypeDepth;
272
36.2M
  }
273
274
  /// Return the number of parameters declared in this function
275
  /// prototype, increasing it by one for the next call.
276
14.7M
  unsigned getNextFunctionPrototypeIndex() {
277
14.7M
    assert(isFunctionPrototypeScope());
278
14.7M
    return PrototypeIndex++;
279
14.7M
  }
280
281
  using decl_range = llvm::iterator_range<DeclSetTy::iterator>;
282
283
16.5M
  decl_range decls() const {
284
16.5M
    return decl_range(DeclsInScope.begin(), DeclsInScope.end());
285
16.5M
  }
286
287
16.0M
  bool decl_empty() const { return DeclsInScope.empty(); }
288
289
45.2M
  void AddDecl(Decl *D) {
290
45.2M
    DeclsInScope.insert(D);
291
45.2M
  }
292
293
432k
  void RemoveDecl(Decl *D) {
294
432k
    DeclsInScope.erase(D);
295
432k
  }
296
297
7.06M
  void incrementMSManglingNumber() {
298
7.06M
    if (Scope *MSLMP = getMSLastManglingParent()) {
299
6.79M
      MSLMP->MSLastManglingNumber += 1;
300
6.79M
      MSCurManglingNumber += 1;
301
6.79M
    }
302
7.06M
  }
303
304
169k
  void decrementMSManglingNumber() {
305
169k
    if (Scope *MSLMP = getMSLastManglingParent()) {
306
169k
      MSLMP->MSLastManglingNumber -= 1;
307
169k
      MSCurManglingNumber -= 1;
308
169k
    }
309
169k
  }
310
311
25.4M
  unsigned getMSLastManglingNumber() const {
312
25.4M
    if (const Scope *MSLMP = getMSLastManglingParent())
313
5.32M
      return MSLMP->MSLastManglingNumber;
314
20.1M
    return 1;
315
20.1M
  }
316
317
31
  unsigned getMSCurManglingNumber() const {
318
31
    return MSCurManglingNumber;
319
31
  }
320
321
  /// isDeclScope - Return true if this is the scope that the specified decl is
322
  /// declared in.
323
116M
  bool isDeclScope(Decl *D) {
324
116M
    return DeclsInScope.count(D) != 0;
325
116M
  }
326
327
544M
  DeclContext *getEntity() const { return Entity; }
328
5.25M
  void setEntity(DeclContext *E) { Entity = E; }
329
330
0
  bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
331
332
30.6M
  bool hasUnrecoverableErrorOccurred() const {
333
30.6M
    return ErrorTrap.hasUnrecoverableErrorOccurred();
334
30.6M
  }
335
336
  /// isFunctionScope() - Return true if this scope is a function scope.
337
224
  bool isFunctionScope() const { return (getFlags() & Scope::FnScope); }
338
339
  /// isClassScope - Return true if this scope is a class/struct/union scope.
340
23.7M
  bool isClassScope() const {
341
23.7M
    return (getFlags() & Scope::ClassScope);
342
23.7M
  }
343
344
  /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline
345
  /// method scope or is inside one.
346
0
  bool isInCXXInlineMethodScope() const {
347
0
    if (const Scope *FnS = getFnParent()) {
348
0
      assert(FnS->getParent() && "TUScope not created?");
349
0
      return FnS->getParent()->isClassScope();
350
0
    }
351
0
    return false;
352
0
  }
353
354
  /// isInObjcMethodScope - Return true if this scope is, or is contained in, an
355
  /// Objective-C method body.  Note that this method is not constant time.
356
82.5k
  bool isInObjcMethodScope() const {
357
222k
    for (const Scope *S = this; S; 
S = S->getParent()139k
) {
358
140k
      // If this scope is an objc method scope, then we succeed.
359
140k
      if (S->getFlags() & ObjCMethodScope)
360
1.35k
        return true;
361
140k
    }
362
82.5k
    
return false81.2k
;
363
82.5k
  }
364
365
  /// isInObjcMethodOuterScope - Return true if this scope is an
366
  /// Objective-C method outer most body.
367
0
  bool isInObjcMethodOuterScope() const {
368
0
    if (const Scope *S = this) {
369
0
      // If this scope is an objc method scope, then we succeed.
370
0
      if (S->getFlags() & ObjCMethodScope)
371
0
        return true;
372
0
    }
373
0
    return false;
374
0
  }
375
376
  /// isTemplateParamScope - Return true if this scope is a C++
377
  /// template parameter scope.
378
96.9M
  bool isTemplateParamScope() const {
379
96.9M
    return getFlags() & Scope::TemplateParamScope;
380
96.9M
  }
381
382
  /// isFunctionPrototypeScope - Return true if this scope is a
383
  /// function prototype scope.
384
50.4M
  bool isFunctionPrototypeScope() const {
385
50.4M
    return getFlags() & Scope::FunctionPrototypeScope;
386
50.4M
  }
387
388
  /// isFunctionDeclarationScope - Return true if this scope is a
389
  /// function prototype scope.
390
91
  bool isFunctionDeclarationScope() const {
391
91
    return getFlags() & Scope::FunctionDeclarationScope;
392
91
  }
393
394
  /// isAtCatchScope - Return true if this scope is \@catch.
395
26
  bool isAtCatchScope() const {
396
26
    return getFlags() & Scope::AtCatchScope;
397
26
  }
398
399
  /// isSwitchScope - Return true if this scope is a switch scope.
400
19
  bool isSwitchScope() const {
401
53
    for (const Scope *S = this; S; 
S = S->getParent()34
) {
402
53
      if (S->getFlags() & Scope::SwitchScope)
403
18
        return true;
404
35
      else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope |
405
35
                                Scope::BlockScope | Scope::TemplateParamScope |
406
35
                                Scope::FunctionPrototypeScope |
407
35
                                Scope::AtCatchScope | Scope::ObjCMethodScope))
408
1
        return false;
409
53
    }
410
19
    
return false0
;
411
19
  }
412
413
  /// Determines whether this scope is the OpenMP directive scope
414
232
  bool isOpenMPDirectiveScope() const {
415
232
    return (getFlags() & Scope::OpenMPDirectiveScope);
416
232
  }
417
418
  /// Determine whether this scope is some OpenMP loop directive scope
419
  /// (for example, 'omp for', 'omp simd').
420
31.6k
  bool isOpenMPLoopDirectiveScope() const {
421
31.6k
    if (getFlags() & Scope::OpenMPLoopDirectiveScope) {
422
232
      assert(isOpenMPDirectiveScope() &&
423
232
             "OpenMP loop directive scope is not a directive scope");
424
232
      return true;
425
232
    }
426
31.3k
    return false;
427
31.3k
  }
428
429
  /// Determine whether this scope is (or is nested into) some OpenMP
430
  /// loop simd directive scope (for example, 'omp simd', 'omp for simd').
431
23.3k
  bool isOpenMPSimdDirectiveScope() const {
432
23.3k
    return getFlags() & Scope::OpenMPSimdDirectiveScope;
433
23.3k
  }
434
435
  /// Determine whether this scope is a loop having OpenMP loop
436
  /// directive attached.
437
31.6k
  bool isOpenMPLoopScope() const {
438
31.6k
    const Scope *P = getParent();
439
31.6k
    return P && P->isOpenMPLoopDirectiveScope();
440
31.6k
  }
441
442
  /// Determine whether this scope is a C++ 'try' block.
443
0
  bool isTryScope() const { return getFlags() & Scope::TryScope; }
444
445
  /// Determine whether this scope is a SEH '__try' block.
446
65
  bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
447
448
  /// Determine whether this scope is a SEH '__except' block.
449
62
  bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; }
450
451
  /// Determine whether this scope is a compound statement scope.
452
681
  bool isCompoundStmtScope() const {
453
681
    return getFlags() & Scope::CompoundStmtScope;
454
681
  }
455
456
  /// Returns if rhs has a higher scope depth than this.
457
  ///
458
  /// The caller is responsible for calling this only if one of the two scopes
459
  /// is an ancestor of the other.
460
32
  bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
461
462
  /// containedInPrototypeScope - Return true if this or a parent scope
463
  /// is a FunctionPrototypeScope.
464
  bool containedInPrototypeScope() const;
465
466
2.19k
  void PushUsingDirective(UsingDirectiveDecl *UDir) {
467
2.19k
    UsingDirectives.push_back(UDir);
468
2.19k
  }
469
470
  using using_directives_range =
471
      llvm::iterator_range<UsingDirectivesTy::iterator>;
472
473
29.3M
  using_directives_range using_directives() {
474
29.3M
    return using_directives_range(UsingDirectives.begin(),
475
29.3M
                                  UsingDirectives.end());
476
29.3M
  }
477
478
55.9k
  void addNRVOCandidate(VarDecl *VD) {
479
55.9k
    if (NRVO.getInt())
480
2.18k
      return;
481
53.7k
    if (NRVO.getPointer() == nullptr) {
482
52.7k
      NRVO.setPointer(VD);
483
52.7k
      return;
484
52.7k
    }
485
1.07k
    if (NRVO.getPointer() != VD)
486
17
      setNoNRVO();
487
1.07k
  }
488
489
2.66M
  void setNoNRVO() {
490
2.66M
    NRVO.setInt(true);
491
2.66M
    NRVO.setPointer(nullptr);
492
2.66M
  }
493
494
  void mergeNRVOIntoParent();
495
496
  /// Init - This is used by the parser to implement scope caching.
497
  void Init(Scope *parent, unsigned flags);
498
499
  /// Sets up the specified scope flags and adjusts the scope state
500
  /// variables accordingly.
501
  void AddFlags(unsigned Flags);
502
503
  void dumpImpl(raw_ostream &OS) const;
504
  void dump() const;
505
};
506
507
} // namespace clang
508
509
#endif // LLVM_CLANG_SEMA_SCOPE_H