Coverage Report

Created: 2018-11-16 02:38

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