Coverage Report

Created: 2018-09-23 03:40

/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
499
  SourceLocation getBeginLoc() const LLVM_READONLY { return CatchLoc; }
45
617
  SourceLocation getEndLoc() const LLVM_READONLY {
46
617
    return HandlerBlock->getEndLoc();
47
617
  }
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
316
  SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
90
91
640
  SourceLocation getTryLoc() const { return TryLoc; }
92
363
  SourceLocation getEndLoc() const {
93
363
    return getStmts()[NumHandlers]->getEndLoc();
94
363
  }
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
448k
  static bool classof(const Stmt *T) {
112
448k
    return T->getStmtClass() == CXXTryStmtClass;
113
448k
  }
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
///
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 { 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(DeclStmt *Range, DeclStmt *Begin, DeclStmt *End,
139
                  Expr *Cond, Expr *Inc, DeclStmt *LoopVar, Stmt *Body,
140
                  SourceLocation FL, SourceLocation CAL, SourceLocation CL,
141
                  SourceLocation RPL);
142
8
  CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
143
144
145
  VarDecl *getLoopVariable();
146
  Expr *getRangeInit();
147
148
  const VarDecl *getLoopVariable() const;
149
  const Expr *getRangeInit() const;
150
151
152
4.37k
  DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
153
4.17k
  DeclStmt *getBeginStmt() {
154
4.17k
    return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
155
4.17k
  }
156
4.17k
  DeclStmt *getEndStmt() { return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]); }
157
6.25k
  Expr *getCond() { return cast_or_null<Expr>(SubExprs[COND]); }
158
2.12k
  Expr *getInc() { return cast_or_null<Expr>(SubExprs[INC]); }
159
4.35k
  DeclStmt *getLoopVarStmt() { return cast<DeclStmt>(SubExprs[LOOPVAR]); }
160
2.20k
  Stmt *getBody() { return SubExprs[BODY]; }
161
162
810
  const DeclStmt *getRangeStmt() const {
163
810
    return cast<DeclStmt>(SubExprs[RANGE]);
164
810
  }
165
744
  const DeclStmt *getBeginStmt() const {
166
744
    return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
167
744
  }
168
744
  const DeclStmt *getEndStmt() const {
169
744
    return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
170
744
  }
171
1.60k
  const Expr *getCond() const {
172
1.60k
    return cast_or_null<Expr>(SubExprs[COND]);
173
1.60k
  }
174
869
  const Expr *getInc() const {
175
869
    return cast_or_null<Expr>(SubExprs[INC]);
176
869
  }
177
833
  const DeclStmt *getLoopVarStmt() const {
178
833
    return cast<DeclStmt>(SubExprs[LOOPVAR]);
179
833
  }
180
1.59k
  const Stmt *getBody() const { return SubExprs[BODY]; }
181
182
0
  void setRangeInit(Expr *E) { SubExprs[RANGE] = reinterpret_cast<Stmt*>(E); }
183
8
  void setRangeStmt(Stmt *S) { SubExprs[RANGE] = S; }
184
8
  void setBeginStmt(Stmt *S) { SubExprs[BEGINSTMT] = S; }
185
8
  void setEndStmt(Stmt *S) { SubExprs[ENDSTMT] = S; }
186
8
  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
187
8
  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
188
8
  void setLoopVarStmt(Stmt *S) { SubExprs[LOOPVAR] = S; }
189
2.72k
  void setBody(Stmt *S) { SubExprs[BODY] = S; }
190
191
80
  SourceLocation getForLoc() const { return ForLoc; }
192
80
  SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
193
98
  SourceLocation getColonLoc() const { return ColonLoc; }
194
2.79k
  SourceLocation getRParenLoc() const { return RParenLoc; }
195
196
11.5k
  SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
197
2.47k
  SourceLocation getEndLoc() const LLVM_READONLY {
198
2.47k
    return SubExprs[BODY]->getEndLoc();
199
2.47k
  }
200
201
37.6k
  static bool classof(const Stmt *T) {
202
37.6k
    return T->getStmtClass() == CXXForRangeStmtClass;
203
37.6k
  }
204
205
  // Iterators
206
979
  child_range children() {
207
979
    return child_range(&SubExprs[0], &SubExprs[END]);
208
979
  }
209
};
210
211
/// Representation of a Microsoft __if_exists or __if_not_exists
212
/// statement with a dependent name.
213
///
214
/// The __if_exists statement can be used to include a sequence of statements
215
/// in the program only when a particular dependent name does not exist. For
216
/// example:
217
///
218
/// \code
219
/// template<typename T>
220
/// void call_foo(T &t) {
221
///   __if_exists (T::foo) {
222
///     t.foo(); // okay: only called when T::foo exists.
223
///   }
224
/// }
225
/// \endcode
226
///
227
/// Similarly, the __if_not_exists statement can be used to include the
228
/// statements when a particular name does not exist.
229
///
230
/// Note that this statement only captures __if_exists and __if_not_exists
231
/// statements whose name is dependent. All non-dependent cases are handled
232
/// directly in the parser, so that they don't introduce a new scope. Clang
233
/// introduces scopes in the dependent case to keep names inside the compound
234
/// statement from leaking out into the surround statements, which would
235
/// compromise the template instantiation model. This behavior differs from
236
/// Visual C++ (which never introduces a scope), but is a fairly reasonable
237
/// approximation of the VC++ behavior.
238
class MSDependentExistsStmt : public Stmt {
239
  SourceLocation KeywordLoc;
240
  bool IsIfExists;
241
  NestedNameSpecifierLoc QualifierLoc;
242
  DeclarationNameInfo NameInfo;
243
  Stmt *SubStmt;
244
245
  friend class ASTReader;
246
  friend class ASTStmtReader;
247
248
public:
249
  MSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists,
250
                        NestedNameSpecifierLoc QualifierLoc,
251
                        DeclarationNameInfo NameInfo,
252
                        CompoundStmt *SubStmt)
253
  : Stmt(MSDependentExistsStmtClass),
254
    KeywordLoc(KeywordLoc), IsIfExists(IsIfExists),
255
    QualifierLoc(QualifierLoc), NameInfo(NameInfo),
256
8
    SubStmt(reinterpret_cast<Stmt *>(SubStmt)) { }
257
258
  /// Retrieve the location of the __if_exists or __if_not_exists
259
  /// keyword.
260
6
  SourceLocation getKeywordLoc() const { return KeywordLoc; }
261
262
  /// Determine whether this is an __if_exists statement.
263
8
  bool isIfExists() const { return IsIfExists; }
264
265
  /// Determine whether this is an __if_exists statement.
266
4
  bool isIfNotExists() const { return !IsIfExists; }
267
268
  /// Retrieve the nested-name-specifier that qualifies this name, if
269
  /// any.
270
30
  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
271
272
  /// Retrieve the name of the entity we're testing for, along with
273
  /// location information
274
14
  DeclarationNameInfo getNameInfo() const { return NameInfo; }
275
276
  /// Retrieve the compound statement that will be included in the
277
  /// program only if the existence of the symbol matches the initial keyword.
278
8
  CompoundStmt *getSubStmt() const {
279
8
    return reinterpret_cast<CompoundStmt *>(SubStmt);
280
8
  }
281
282
34
  SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
283
34
  SourceLocation getEndLoc() const LLVM_READONLY {
284
34
    return SubStmt->getEndLoc();
285
34
  }
286
287
2
  child_range children() {
288
2
    return child_range(&SubStmt, &SubStmt+1);
289
2
  }
290
291
  static bool classof(const Stmt *T) {
292
    return T->getStmtClass() == MSDependentExistsStmtClass;
293
  }
294
};
295
296
/// Represents the body of a coroutine. This wraps the normal function
297
/// body and holds the additional semantic context required to set up and tear
298
/// down the coroutine frame.
299
class CoroutineBodyStmt final
300
    : public Stmt,
301
      private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
302
  enum SubStmt {
303
    Body,          ///< The body of the coroutine.
304
    Promise,       ///< The promise statement.
305
    InitSuspend,   ///< The initial suspend statement, run before the body.
306
    FinalSuspend,  ///< The final suspend statement, run after the body.
307
    OnException,   ///< Handler for exceptions thrown in the body.
308
    OnFallthrough, ///< Handler for control flow falling off the body.
309
    Allocate,      ///< Coroutine frame memory allocation.
310
    Deallocate,    ///< Coroutine frame memory deallocation.
311
    ReturnValue,   ///< Return value for thunk function: p.get_return_object().
312
    ResultDecl,    ///< Declaration holding the result of get_return_object.
313
    ReturnStmt,    ///< Return statement for the thunk function.
314
    ReturnStmtOnAllocFailure, ///< Return statement if allocation failed.
315
    FirstParamMove ///< First offset for move construction of parameter copies.
316
  };
317
  unsigned NumParams;
318
319
  friend class ASTStmtReader;
320
  friend class ASTReader;
321
  friend TrailingObjects;
322
323
217
  Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
324
325
1.75k
  Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
326
327
public:
328
329
  struct CtorArgs {
330
    Stmt *Body = nullptr;
331
    Stmt *Promise = nullptr;
332
    Expr *InitialSuspend = nullptr;
333
    Expr *FinalSuspend = nullptr;
334
    Stmt *OnException = nullptr;
335
    Stmt *OnFallthrough = nullptr;
336
    Expr *Allocate = nullptr;
337
    Expr *Deallocate = nullptr;
338
    Expr *ReturnValue = nullptr;
339
    Stmt *ResultDecl = nullptr;
340
    Stmt *ReturnStmt = nullptr;
341
    Stmt *ReturnStmtOnAllocFailure = nullptr;
342
    ArrayRef<Stmt *> ParamMoves;
343
  };
344
345
private:
346
347
  CoroutineBodyStmt(CtorArgs const& Args);
348
349
public:
350
  static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
351
  static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
352
                                   unsigned NumParams);
353
354
41
  bool hasDependentPromiseType() const {
355
41
    return getPromiseDecl()->getType()->isDependentType();
356
41
  }
357
358
  /// Retrieve the body of the coroutine as written. This will be either
359
  /// a CompoundStmt or a TryStmt.
360
741
  Stmt *getBody() const {
361
741
    return getStoredStmts()[SubStmt::Body];
362
741
  }
363
364
172
  Stmt *getPromiseDeclStmt() const {
365
172
    return getStoredStmts()[SubStmt::Promise];
366
172
  }
367
131
  VarDecl *getPromiseDecl() const {
368
131
    return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
369
131
  }
370
371
90
  Stmt *getInitSuspendStmt() const {
372
90
    return getStoredStmts()[SubStmt::InitSuspend];
373
90
  }
374
88
  Stmt *getFinalSuspendStmt() const {
375
88
    return getStoredStmts()[SubStmt::FinalSuspend];
376
88
  }
377
378
46
  Stmt *getExceptionHandler() const {
379
46
    return getStoredStmts()[SubStmt::OnException];
380
46
  }
381
80
  Stmt *getFallthroughHandler() const {
382
80
    return getStoredStmts()[SubStmt::OnFallthrough];
383
80
  }
384
385
46
  Expr *getAllocate() const {
386
46
    return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
387
46
  }
388
46
  Expr *getDeallocate() const {
389
46
    return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
390
46
  }
391
41
  Expr *getReturnValueInit() const {
392
41
    return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
393
41
  }
394
68
  Stmt *getResultDecl() const { return getStoredStmts()[SubStmt::ResultDecl]; }
395
46
  Stmt *getReturnStmt() const { return getStoredStmts()[SubStmt::ReturnStmt]; }
396
46
  Stmt *getReturnStmtOnAllocFailure() const {
397
46
    return getStoredStmts()[SubStmt::ReturnStmtOnAllocFailure];
398
46
  }
399
241
  ArrayRef<Stmt const *> getParamMoves() const {
400
241
    return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
401
241
  }
402
403
65
  SourceLocation getBeginLoc() const LLVM_READONLY {
404
65
    return getBody() ? getBody()->getBeginLoc()
405
65
                     : 
getPromiseDecl()->getBeginLoc()0
;
406
65
  }
407
251
  SourceLocation getEndLoc() const LLVM_READONLY {
408
251
    return getBody() ? getBody()->getEndLoc() : 
getPromiseDecl()->getEndLoc()0
;
409
251
  }
410
411
8
  child_range children() {
412
8
    return child_range(getStoredStmts(),
413
8
                       getStoredStmts() + SubStmt::FirstParamMove + NumParams);
414
8
  }
415
416
1.91M
  static bool classof(const Stmt *T) {
417
1.91M
    return T->getStmtClass() == CoroutineBodyStmtClass;
418
1.91M
  }
419
};
420
421
/// Represents a 'co_return' statement in the C++ Coroutines TS.
422
///
423
/// This statament models the initialization of the coroutine promise
424
/// (encapsulating the eventual notional return value) from an expression
425
/// (or braced-init-list), followed by termination of the coroutine.
426
///
427
/// This initialization is modeled by the evaluation of the operand
428
/// followed by a call to one of:
429
///   <promise>.return_value(<operand>)
430
///   <promise>.return_void()
431
/// which we name the "promise call".
432
class CoreturnStmt : public Stmt {
433
  SourceLocation CoreturnLoc;
434
435
  enum SubStmt { Operand, PromiseCall, Count };
436
  Stmt *SubStmts[SubStmt::Count];
437
438
  bool IsImplicit : 1;
439
440
  friend class ASTStmtReader;
441
public:
442
  CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall,
443
               bool IsImplicit = false)
444
      : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc),
445
210
        IsImplicit(IsImplicit) {
446
210
    SubStmts[SubStmt::Operand] = Operand;
447
210
    SubStmts[SubStmt::PromiseCall] = PromiseCall;
448
210
  }
449
450
2
  CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
451
452
18
  SourceLocation getKeywordLoc() const { return CoreturnLoc; }
453
454
  /// Retrieve the operand of the 'co_return' statement. Will be nullptr
455
  /// if none was specified.
456
74
  Expr *getOperand() const { return static_cast<Expr*>(SubStmts[Operand]); }
457
458
  /// Retrieve the promise call that results from this 'co_return'
459
  /// statement. Will be nullptr if either the coroutine has not yet been
460
  /// finalized or the coroutine has no eventual return type.
461
41
  Expr *getPromiseCall() const {
462
41
    return static_cast<Expr*>(SubStmts[PromiseCall]);
463
41
  }
464
465
18
  bool isImplicit() const { return IsImplicit; }
466
  void setIsImplicit(bool value = true) { IsImplicit = value; }
467
468
2
  SourceLocation getBeginLoc() const LLVM_READONLY { return CoreturnLoc; }
469
1
  SourceLocation getEndLoc() const LLVM_READONLY {
470
1
    return getOperand() ? 
getOperand()->getEndLoc()0
: getBeginLoc();
471
1
  }
472
473
16
  child_range children() {
474
16
    if (!getOperand())
475
5
      return child_range(SubStmts + SubStmt::PromiseCall,
476
5
                         SubStmts + SubStmt::Count);
477
11
    return child_range(SubStmts, SubStmts + SubStmt::Count);
478
11
  }
479
480
267
  static bool classof(const Stmt *T) {
481
267
    return T->getStmtClass() == CoreturnStmtClass;
482
267
  }
483
};
484
485
}  // end namespace clang
486
487
#endif