Coverage Report

Created: 2019-02-23 12:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/AST/StmtCXX.h
Line
Count
Source (jump to first uncovered line)
1
//===--- StmtCXX.h - Classes for representing C++ statements ----*- 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 C++ statement AST node classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_AST_STMTCXX_H
14
#define LLVM_CLANG_AST_STMTCXX_H
15
16
#include "clang/AST/DeclarationName.h"
17
#include "clang/AST/Expr.h"
18
#include "clang/AST/NestedNameSpecifier.h"
19
#include "clang/AST/Stmt.h"
20
#include "llvm/Support/Compiler.h"
21
22
namespace clang {
23
24
class VarDecl;
25
26
/// CXXCatchStmt - This represents a C++ catch block.
27
///
28
class CXXCatchStmt : public Stmt {
29
  SourceLocation CatchLoc;
30
  /// The exception-declaration of the type.
31
  VarDecl *ExceptionDecl;
32
  /// The handler block.
33
  Stmt *HandlerBlock;
34
35
public:
36
  CXXCatchStmt(SourceLocation catchLoc, VarDecl *exDecl, Stmt *handlerBlock)
37
  : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl),
38
12.4k
    HandlerBlock(handlerBlock) {}
39
40
  CXXCatchStmt(EmptyShell Empty)
41
4
  : Stmt(CXXCatchStmtClass), ExceptionDecl(nullptr), HandlerBlock(nullptr) {}
42
43
504
  SourceLocation getBeginLoc() const LLVM_READONLY { return CatchLoc; }
44
628
  SourceLocation getEndLoc() const LLVM_READONLY {
45
628
    return HandlerBlock->getEndLoc();
46
628
  }
47
48
303
  SourceLocation getCatchLoc() const { return CatchLoc; }
49
15.3k
  VarDecl *getExceptionDecl() const { return ExceptionDecl; }
50
  QualType getCaughtType() const;
51
1.89k
  Stmt *getHandlerBlock() const { return HandlerBlock; }
52
53
65
  static bool classof(const Stmt *T) {
54
65
    return T->getStmtClass() == CXXCatchStmtClass;
55
65
  }
56
57
791
  child_range children() { return child_range(&HandlerBlock, &HandlerBlock+1); }
58
59
  friend class ASTStmtReader;
60
};
61
62
/// CXXTryStmt - A C++ try block, including all handlers.
63
///
64
class CXXTryStmt final : public Stmt,
65
                         private llvm::TrailingObjects<CXXTryStmt, Stmt *> {
66
67
  friend TrailingObjects;
68
  friend class ASTStmtReader;
69
70
  SourceLocation TryLoc;
71
  unsigned NumHandlers;
72
0
  size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; }
73
74
  CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers);
75
  CXXTryStmt(EmptyShell Empty, unsigned numHandlers)
76
4
    : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { }
77
78
2.16k
  Stmt *const *getStmts() const { return getTrailingObjects<Stmt *>(); }
79
15.7k
  Stmt **getStmts() { return getTrailingObjects<Stmt *>(); }
80
81
public:
82
  static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc,
83
                            Stmt *tryBlock, ArrayRef<Stmt*> handlers);
84
85
  static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty,
86
                            unsigned numHandlers);
87
88
326
  SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
89
90
654
  SourceLocation getTryLoc() const { return TryLoc; }
91
369
  SourceLocation getEndLoc() const {
92
369
    return getStmts()[NumHandlers]->getEndLoc();
93
369
  }
94
95
1.08k
  CompoundStmt *getTryBlock() {
96
1.08k
    return cast<CompoundStmt>(getStmts()[0]);
97
1.08k
  }
98
516
  const CompoundStmt *getTryBlock() const {
99
516
    return cast<CompoundStmt>(getStmts()[0]);
100
516
  }
101
102
2.68k
  unsigned getNumHandlers() const { return NumHandlers; }
103
1.12k
  CXXCatchStmt *getHandler(unsigned i) {
104
1.12k
    return cast<CXXCatchStmt>(getStmts()[i + 1]);
105
1.12k
  }
106
1.28k
  const CXXCatchStmt *getHandler(unsigned i) const {
107
1.28k
    return cast<CXXCatchStmt>(getStmts()[i + 1]);
108
1.28k
  }
109
110
458k
  static bool classof(const Stmt *T) {
111
458k
    return T->getStmtClass() == CXXTryStmtClass;
112
458k
  }
113
114
662
  child_range children() {
115
662
    return child_range(getStmts(), getStmts() + getNumHandlers() + 1);
116
662
  }
117
};
118
119
/// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
120
/// statement, represented as 'for (range-declarator : range-expression)'
121
/// or 'for (init-statement range-declarator : range-expression)'.
122
///
123
/// This is stored in a partially-desugared form to allow full semantic
124
/// analysis of the constituent components. The original syntactic components
125
/// can be extracted using getLoopVariable and getRangeInit.
126
class CXXForRangeStmt : public Stmt {
127
  SourceLocation ForLoc;
128
  enum { INIT, RANGE, BEGINSTMT, ENDSTMT, COND, INC, LOOPVAR, BODY, END };
129
  // SubExprs[RANGE] is an expression or declstmt.
130
  // SubExprs[COND] and SubExprs[INC] are expressions.
131
  Stmt *SubExprs[END];
132
  SourceLocation CoawaitLoc;
133
  SourceLocation ColonLoc;
134
  SourceLocation RParenLoc;
135
136
  friend class ASTStmtReader;
137
public:
138
  CXXForRangeStmt(Stmt *InitStmt, DeclStmt *Range, DeclStmt *Begin,
139
                  DeclStmt *End, Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
140
                  Stmt *Body, SourceLocation FL, SourceLocation CAL,
141
                  SourceLocation CL, SourceLocation RPL);
142
9
  CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
143
144
2.20k
  Stmt *getInit() { return SubExprs[INIT]; }
145
  VarDecl *getLoopVariable();
146
  Expr *getRangeInit();
147
148
776
  const Stmt *getInit() const { return SubExprs[INIT]; }
149
  const VarDecl *getLoopVariable() const;
150
  const Expr *getRangeInit() const;
151
152
153
4.23k
  DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
154
3.99k
  DeclStmt *getBeginStmt() {
155
3.99k
    return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
156
3.99k
  }
157
3.99k
  DeclStmt *getEndStmt() { return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]); }
158
5.96k
  Expr *getCond() { return cast_or_null<Expr>(SubExprs[COND]); }
159
2.05k
  Expr *getInc() { return cast_or_null<Expr>(SubExprs[INC]); }
160
4.18k
  DeclStmt *getLoopVarStmt() { return cast<DeclStmt>(SubExprs[LOOPVAR]); }
161
2.17k
  Stmt *getBody() { return SubExprs[BODY]; }
162
163
840
  const DeclStmt *getRangeStmt() const {
164
840
    return cast<DeclStmt>(SubExprs[RANGE]);
165
840
  }
166
770
  const DeclStmt *getBeginStmt() const {
167
770
    return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
168
770
  }
169
770
  const DeclStmt *getEndStmt() const {
170
770
    return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
171
770
  }
172
1.67k
  const Expr *getCond() const {
173
1.67k
    return cast_or_null<Expr>(SubExprs[COND]);
174
1.67k
  }
175
904
  const Expr *getInc() const {
176
904
    return cast_or_null<Expr>(SubExprs[INC]);
177
904
  }
178
867
  const DeclStmt *getLoopVarStmt() const {
179
867
    return cast<DeclStmt>(SubExprs[LOOPVAR]);
180
867
  }
181
1.65k
  const Stmt *getBody() const { return SubExprs[BODY]; }
182
183
9
  void setInit(Stmt *S) { SubExprs[INIT] = S; }
184
0
  void setRangeInit(Expr *E) { SubExprs[RANGE] = reinterpret_cast<Stmt*>(E); }
185
9
  void setRangeStmt(Stmt *S) { SubExprs[RANGE] = S; }
186
9
  void setBeginStmt(Stmt *S) { SubExprs[BEGINSTMT] = S; }
187
9
  void setEndStmt(Stmt *S) { SubExprs[ENDSTMT] = S; }
188
9
  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
189
9
  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
190
9
  void setLoopVarStmt(Stmt *S) { SubExprs[LOOPVAR] = S; }
191
2.66k
  void setBody(Stmt *S) { SubExprs[BODY] = S; }
192
193
116
  SourceLocation getForLoc() const { return ForLoc; }
194
116
  SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
195
138
  SourceLocation getColonLoc() const { return ColonLoc; }
196
2.77k
  SourceLocation getRParenLoc() const { return RParenLoc; }
197
198
11.5k
  SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
199
2.55k
  SourceLocation getEndLoc() const LLVM_READONLY {
200
2.55k
    return SubExprs[BODY]->getEndLoc();
201
2.55k
  }
202
203
43.3k
  static bool classof(const Stmt *T) {
204
43.3k
    return T->getStmtClass() == CXXForRangeStmtClass;
205
43.3k
  }
206
207
  // Iterators
208
1.06k
  child_range children() {
209
1.06k
    return child_range(&SubExprs[0], &SubExprs[END]);
210
1.06k
  }
211
};
212
213
/// Representation of a Microsoft __if_exists or __if_not_exists
214
/// statement with a dependent name.
215
///
216
/// The __if_exists statement can be used to include a sequence of statements
217
/// in the program only when a particular dependent name does not exist. For
218
/// example:
219
///
220
/// \code
221
/// template<typename T>
222
/// void call_foo(T &t) {
223
///   __if_exists (T::foo) {
224
///     t.foo(); // okay: only called when T::foo exists.
225
///   }
226
/// }
227
/// \endcode
228
///
229
/// Similarly, the __if_not_exists statement can be used to include the
230
/// statements when a particular name does not exist.
231
///
232
/// Note that this statement only captures __if_exists and __if_not_exists
233
/// statements whose name is dependent. All non-dependent cases are handled
234
/// directly in the parser, so that they don't introduce a new scope. Clang
235
/// introduces scopes in the dependent case to keep names inside the compound
236
/// statement from leaking out into the surround statements, which would
237
/// compromise the template instantiation model. This behavior differs from
238
/// Visual C++ (which never introduces a scope), but is a fairly reasonable
239
/// approximation of the VC++ behavior.
240
class MSDependentExistsStmt : public Stmt {
241
  SourceLocation KeywordLoc;
242
  bool IsIfExists;
243
  NestedNameSpecifierLoc QualifierLoc;
244
  DeclarationNameInfo NameInfo;
245
  Stmt *SubStmt;
246
247
  friend class ASTReader;
248
  friend class ASTStmtReader;
249
250
public:
251
  MSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists,
252
                        NestedNameSpecifierLoc QualifierLoc,
253
                        DeclarationNameInfo NameInfo,
254
                        CompoundStmt *SubStmt)
255
  : Stmt(MSDependentExistsStmtClass),
256
    KeywordLoc(KeywordLoc), IsIfExists(IsIfExists),
257
    QualifierLoc(QualifierLoc), NameInfo(NameInfo),
258
8
    SubStmt(reinterpret_cast<Stmt *>(SubStmt)) { }
259
260
  /// Retrieve the location of the __if_exists or __if_not_exists
261
  /// keyword.
262
6
  SourceLocation getKeywordLoc() const { return KeywordLoc; }
263
264
  /// Determine whether this is an __if_exists statement.
265
8
  bool isIfExists() const { return IsIfExists; }
266
267
  /// Determine whether this is an __if_exists statement.
268
4
  bool isIfNotExists() const { return !IsIfExists; }
269
270
  /// Retrieve the nested-name-specifier that qualifies this name, if
271
  /// any.
272
30
  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
273
274
  /// Retrieve the name of the entity we're testing for, along with
275
  /// location information
276
14
  DeclarationNameInfo getNameInfo() const { return NameInfo; }
277
278
  /// Retrieve the compound statement that will be included in the
279
  /// program only if the existence of the symbol matches the initial keyword.
280
8
  CompoundStmt *getSubStmt() const {
281
8
    return reinterpret_cast<CompoundStmt *>(SubStmt);
282
8
  }
283
284
34
  SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
285
34
  SourceLocation getEndLoc() const LLVM_READONLY {
286
34
    return SubStmt->getEndLoc();
287
34
  }
288
289
2
  child_range children() {
290
2
    return child_range(&SubStmt, &SubStmt+1);
291
2
  }
292
293
  static bool classof(const Stmt *T) {
294
    return T->getStmtClass() == MSDependentExistsStmtClass;
295
  }
296
};
297
298
/// Represents the body of a coroutine. This wraps the normal function
299
/// body and holds the additional semantic context required to set up and tear
300
/// down the coroutine frame.
301
class CoroutineBodyStmt final
302
    : public Stmt,
303
      private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
304
  enum SubStmt {
305
    Body,          ///< The body of the coroutine.
306
    Promise,       ///< The promise statement.
307
    InitSuspend,   ///< The initial suspend statement, run before the body.
308
    FinalSuspend,  ///< The final suspend statement, run after the body.
309
    OnException,   ///< Handler for exceptions thrown in the body.
310
    OnFallthrough, ///< Handler for control flow falling off the body.
311
    Allocate,      ///< Coroutine frame memory allocation.
312
    Deallocate,    ///< Coroutine frame memory deallocation.
313
    ReturnValue,   ///< Return value for thunk function: p.get_return_object().
314
    ResultDecl,    ///< Declaration holding the result of get_return_object.
315
    ReturnStmt,    ///< Return statement for the thunk function.
316
    ReturnStmtOnAllocFailure, ///< Return statement if allocation failed.
317
    FirstParamMove ///< First offset for move construction of parameter copies.
318
  };
319
  unsigned NumParams;
320
321
  friend class ASTStmtReader;
322
  friend class ASTReader;
323
  friend TrailingObjects;
324
325
222
  Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
326
327
1.79k
  Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
328
329
public:
330
331
  struct CtorArgs {
332
    Stmt *Body = nullptr;
333
    Stmt *Promise = nullptr;
334
    Expr *InitialSuspend = nullptr;
335
    Expr *FinalSuspend = nullptr;
336
    Stmt *OnException = nullptr;
337
    Stmt *OnFallthrough = nullptr;
338
    Expr *Allocate = nullptr;
339
    Expr *Deallocate = nullptr;
340
    Expr *ReturnValue = nullptr;
341
    Stmt *ResultDecl = nullptr;
342
    Stmt *ReturnStmt = nullptr;
343
    Stmt *ReturnStmtOnAllocFailure = nullptr;
344
    ArrayRef<Stmt *> ParamMoves;
345
  };
346
347
private:
348
349
  CoroutineBodyStmt(CtorArgs const& Args);
350
351
public:
352
  static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
353
  static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
354
                                   unsigned NumParams);
355
356
41
  bool hasDependentPromiseType() const {
357
41
    return getPromiseDecl()->getType()->isDependentType();
358
41
  }
359
360
  /// Retrieve the body of the coroutine as written. This will be either
361
  /// a CompoundStmt or a TryStmt.
362
777
  Stmt *getBody() const {
363
777
    return getStoredStmts()[SubStmt::Body];
364
777
  }
365
366
172
  Stmt *getPromiseDeclStmt() const {
367
172
    return getStoredStmts()[SubStmt::Promise];
368
172
  }
369
131
  VarDecl *getPromiseDecl() const {
370
131
    return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
371
131
  }
372
373
90
  Stmt *getInitSuspendStmt() const {
374
90
    return getStoredStmts()[SubStmt::InitSuspend];
375
90
  }
376
88
  Stmt *getFinalSuspendStmt() const {
377
88
    return getStoredStmts()[SubStmt::FinalSuspend];
378
88
  }
379
380
46
  Stmt *getExceptionHandler() const {
381
46
    return getStoredStmts()[SubStmt::OnException];
382
46
  }
383
83
  Stmt *getFallthroughHandler() const {
384
83
    return getStoredStmts()[SubStmt::OnFallthrough];
385
83
  }
386
387
46
  Expr *getAllocate() const {
388
46
    return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
389
46
  }
390
46
  Expr *getDeallocate() const {
391
46
    return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
392
46
  }
393
41
  Expr *getReturnValueInit() const {
394
41
    return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
395
41
  }
396
68
  Stmt *getResultDecl() const { return getStoredStmts()[SubStmt::ResultDecl]; }
397
46
  Stmt *getReturnStmt() const { return getStoredStmts()[SubStmt::ReturnStmt]; }
398
46
  Stmt *getReturnStmtOnAllocFailure() const {
399
46
    return getStoredStmts()[SubStmt::ReturnStmtOnAllocFailure];
400
46
  }
401
244
  ArrayRef<Stmt const *> getParamMoves() const {
402
244
    return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
403
244
  }
404
405
73
  SourceLocation getBeginLoc() const LLVM_READONLY {
406
73
    return getBody() ? getBody()->getBeginLoc()
407
73
                     : 
getPromiseDecl()->getBeginLoc()0
;
408
73
  }
409
260
  SourceLocation getEndLoc() const LLVM_READONLY {
410
260
    return getBody() ? getBody()->getEndLoc() : 
getPromiseDecl()->getEndLoc()0
;
411
260
  }
412
413
9
  child_range children() {
414
9
    return child_range(getStoredStmts(),
415
9
                       getStoredStmts() + SubStmt::FirstParamMove + NumParams);
416
9
  }
417
418
2.01M
  static bool classof(const Stmt *T) {
419
2.01M
    return T->getStmtClass() == CoroutineBodyStmtClass;
420
2.01M
  }
421
};
422
423
/// Represents a 'co_return' statement in the C++ Coroutines TS.
424
///
425
/// This statament models the initialization of the coroutine promise
426
/// (encapsulating the eventual notional return value) from an expression
427
/// (or braced-init-list), followed by termination of the coroutine.
428
///
429
/// This initialization is modeled by the evaluation of the operand
430
/// followed by a call to one of:
431
///   <promise>.return_value(<operand>)
432
///   <promise>.return_void()
433
/// which we name the "promise call".
434
class CoreturnStmt : public Stmt {
435
  SourceLocation CoreturnLoc;
436
437
  enum SubStmt { Operand, PromiseCall, Count };
438
  Stmt *SubStmts[SubStmt::Count];
439
440
  bool IsImplicit : 1;
441
442
  friend class ASTStmtReader;
443
public:
444
  CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall,
445
               bool IsImplicit = false)
446
      : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc),
447
214
        IsImplicit(IsImplicit) {
448
214
    SubStmts[SubStmt::Operand] = Operand;
449
214
    SubStmts[SubStmt::PromiseCall] = PromiseCall;
450
214
  }
451
452
2
  CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
453
454
18
  SourceLocation getKeywordLoc() const { return CoreturnLoc; }
455
456
  /// Retrieve the operand of the 'co_return' statement. Will be nullptr
457
  /// if none was specified.
458
86
  Expr *getOperand() const { return static_cast<Expr*>(SubStmts[Operand]); }
459
460
  /// Retrieve the promise call that results from this 'co_return'
461
  /// statement. Will be nullptr if either the coroutine has not yet been
462
  /// finalized or the coroutine has no eventual return type.
463
41
  Expr *getPromiseCall() const {
464
41
    return static_cast<Expr*>(SubStmts[PromiseCall]);
465
41
  }
466
467
18
  bool isImplicit() const { return IsImplicit; }
468
  void setIsImplicit(bool value = true) { IsImplicit = value; }
469
470
22
  SourceLocation getBeginLoc() const LLVM_READONLY { return CoreturnLoc; }
471
9
  SourceLocation getEndLoc() const LLVM_READONLY {
472
9
    return getOperand() ? 
getOperand()->getEndLoc()0
: getBeginLoc();
473
9
  }
474
475
20
  child_range children() {
476
20
    if (!getOperand())
477
7
      return child_range(SubStmts + SubStmt::PromiseCall,
478
7
                         SubStmts + SubStmt::Count);
479
13
    return child_range(SubStmts, SubStmts + SubStmt::Count);
480
13
  }
481
482
282
  static bool classof(const Stmt *T) {
483
282
    return T->getStmtClass() == CoreturnStmtClass;
484
282
  }
485
};
486
487
}  // end namespace clang
488
489
#endif