1//===- Stmt.h - Classes for representing 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 Stmt interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_STMT_H
15#define LLVM_CLANG_AST_STMT_H
16
17#include "clang/AST/DeclGroup.h"
18#include "clang/AST/StmtIterator.h"
19#include "clang/Basic/CapturedStmt.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LLVM.h"
22#include "clang/Basic/SourceLocation.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/ADT/iterator_range.h"
28#include "llvm/Support/Casting.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/ErrorHandling.h"
31#include <algorithm>
32#include <cassert>
33#include <cstddef>
34#include <iterator>
35#include <string>
36
37namespace llvm {
38
39class FoldingSetNodeID;
40
41} // namespace llvm
42
43namespace clang {
44
45class ASTContext;
46class Attr;
47class CapturedDecl;
48class Decl;
49class Expr;
50class LabelDecl;
51class ODRHash;
52class PrinterHelper;
53struct PrintingPolicy;
54class RecordDecl;
55class SourceManager;
56class StringLiteral;
57class Token;
58class VarDecl;
59
60//===----------------------------------------------------------------------===//
61// AST classes for statements.
62//===----------------------------------------------------------------------===//
63
64/// Stmt - This represents one statement.
65///
66class alignas(void *) Stmt {
67public:
68 enum StmtClass {
69 NoStmtClass = 0,
70#define STMT(CLASS, PARENT) CLASS##Class,
71#define STMT_RANGE(BASE, FIRST, LAST) \
72 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
73#define LAST_STMT_RANGE(BASE, FIRST, LAST) \
74 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
75#define ABSTRACT_STMT(STMT)
76#include "clang/AST/StmtNodes.inc"
77 };
78
79 // Make vanilla 'new' and 'delete' illegal for Stmts.
80protected:
81 friend class ASTStmtReader;
82 friend class ASTStmtWriter;
83
84 void *operator new(size_t bytes) noexcept {
85 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
86 }
87
88 void operator delete(void *data) noexcept {
89 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
90 }
91
92 class StmtBitfields {
93 friend class Stmt;
94
95 /// The statement class.
96 unsigned sClass : 8;
97 };
98 enum { NumStmtBits = 8 };
99
100 class CompoundStmtBitfields {
101 friend class CompoundStmt;
102
103 unsigned : NumStmtBits;
104
105 unsigned NumStmts : 32 - NumStmtBits;
106 };
107
108 class IfStmtBitfields {
109 friend class IfStmt;
110
111 unsigned : NumStmtBits;
112
113 unsigned IsConstexpr : 1;
114 };
115
116 class ExprBitfields {
117 friend class ASTStmtReader; // deserialization
118 friend class AtomicExpr; // ctor
119 friend class BlockDeclRefExpr; // ctor
120 friend class CallExpr; // ctor
121 friend class CXXConstructExpr; // ctor
122 friend class CXXDependentScopeMemberExpr; // ctor
123 friend class CXXNewExpr; // ctor
124 friend class CXXUnresolvedConstructExpr; // ctor
125 friend class DeclRefExpr; // computeDependence
126 friend class DependentScopeDeclRefExpr; // ctor
127 friend class DesignatedInitExpr; // ctor
128 friend class Expr;
129 friend class InitListExpr; // ctor
130 friend class ObjCArrayLiteral; // ctor
131 friend class ObjCDictionaryLiteral; // ctor
132 friend class ObjCMessageExpr; // ctor
133 friend class OffsetOfExpr; // ctor
134 friend class OpaqueValueExpr; // ctor
135 friend class OverloadExpr; // ctor
136 friend class ParenListExpr; // ctor
137 friend class PseudoObjectExpr; // ctor
138 friend class ShuffleVectorExpr; // ctor
139
140 unsigned : NumStmtBits;
141
142 unsigned ValueKind : 2;
143 unsigned ObjectKind : 3;
144 unsigned TypeDependent : 1;
145 unsigned ValueDependent : 1;
146 unsigned InstantiationDependent : 1;
147 unsigned ContainsUnexpandedParameterPack : 1;
148 };
149 enum { NumExprBits = 17 };
150
151 class CharacterLiteralBitfields {
152 friend class CharacterLiteral;
153
154 unsigned : NumExprBits;
155
156 unsigned Kind : 3;
157 };
158
159 enum APFloatSemantics {
160 IEEEhalf,
161 IEEEsingle,
162 IEEEdouble,
163 x87DoubleExtended,
164 IEEEquad,
165 PPCDoubleDouble
166 };
167
168 class FloatingLiteralBitfields {
169 friend class FloatingLiteral;
170
171 unsigned : NumExprBits;
172
173 unsigned Semantics : 3; // Provides semantics for APFloat construction
174 unsigned IsExact : 1;
175 };
176
177 class UnaryExprOrTypeTraitExprBitfields {
178 friend class UnaryExprOrTypeTraitExpr;
179
180 unsigned : NumExprBits;
181
182 unsigned Kind : 2;
183 unsigned IsType : 1; // true if operand is a type, false if an expression.
184 };
185
186 class DeclRefExprBitfields {
187 friend class ASTStmtReader; // deserialization
188 friend class DeclRefExpr;
189
190 unsigned : NumExprBits;
191
192 unsigned HasQualifier : 1;
193 unsigned HasTemplateKWAndArgsInfo : 1;
194 unsigned HasFoundDecl : 1;
195 unsigned HadMultipleCandidates : 1;
196 unsigned RefersToEnclosingVariableOrCapture : 1;
197 };
198
199 class CastExprBitfields {
200 friend class CastExpr;
201
202 unsigned : NumExprBits;
203
204 unsigned Kind : 6;
205 unsigned BasePathSize : 32 - 6 - NumExprBits;
206 };
207
208 class CallExprBitfields {
209 friend class CallExpr;
210
211 unsigned : NumExprBits;
212
213 unsigned NumPreArgs : 1;
214 };
215
216 class ExprWithCleanupsBitfields {
217 friend class ASTStmtReader; // deserialization
218 friend class ExprWithCleanups;
219
220 unsigned : NumExprBits;
221
222 // When false, it must not have side effects.
223 unsigned CleanupsHaveSideEffects : 1;
224
225 unsigned NumObjects : 32 - 1 - NumExprBits;
226 };
227
228 class PseudoObjectExprBitfields {
229 friend class ASTStmtReader; // deserialization
230 friend class PseudoObjectExpr;
231
232 unsigned : NumExprBits;
233
234 // These don't need to be particularly wide, because they're
235 // strictly limited by the forms of expressions we permit.
236 unsigned NumSubExprs : 8;
237 unsigned ResultIndex : 32 - 8 - NumExprBits;
238 };
239
240 class OpaqueValueExprBitfields {
241 friend class OpaqueValueExpr;
242
243 unsigned : NumExprBits;
244
245 /// The OVE is a unique semantic reference to its source expressio if this
246 /// bit is set to true.
247 unsigned IsUnique : 1;
248 };
249
250 class ObjCIndirectCopyRestoreExprBitfields {
251 friend class ObjCIndirectCopyRestoreExpr;
252
253 unsigned : NumExprBits;
254
255 unsigned ShouldCopy : 1;
256 };
257
258 class InitListExprBitfields {
259 friend class InitListExpr;
260
261 unsigned : NumExprBits;
262
263 /// Whether this initializer list originally had a GNU array-range
264 /// designator in it. This is a temporary marker used by CodeGen.
265 unsigned HadArrayRangeDesignator : 1;
266 };
267
268 class TypeTraitExprBitfields {
269 friend class ASTStmtReader;
270 friend class ASTStmtWriter;
271 friend class TypeTraitExpr;
272
273 unsigned : NumExprBits;
274
275 /// The kind of type trait, which is a value of a TypeTrait enumerator.
276 unsigned Kind : 8;
277
278 /// If this expression is not value-dependent, this indicates whether
279 /// the trait evaluated true or false.
280 unsigned Value : 1;
281
282 /// The number of arguments to this type trait.
283 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
284 };
285
286 class CoawaitExprBitfields {
287 friend class CoawaitExpr;
288
289 unsigned : NumExprBits;
290
291 unsigned IsImplicit : 1;
292 };
293
294 union {
295 StmtBitfields StmtBits;
296 CompoundStmtBitfields CompoundStmtBits;
297 IfStmtBitfields IfStmtBits;
298 ExprBitfields ExprBits;
299 CharacterLiteralBitfields CharacterLiteralBits;
300 FloatingLiteralBitfields FloatingLiteralBits;
301 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
302 DeclRefExprBitfields DeclRefExprBits;
303 CastExprBitfields CastExprBits;
304 CallExprBitfields CallExprBits;
305 ExprWithCleanupsBitfields ExprWithCleanupsBits;
306 PseudoObjectExprBitfields PseudoObjectExprBits;
307 OpaqueValueExprBitfields OpaqueValueExprBits;
308 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
309 InitListExprBitfields InitListExprBits;
310 TypeTraitExprBitfields TypeTraitExprBits;
311 CoawaitExprBitfields CoawaitBits;
312 };
313
314public:
315 // Only allow allocation of Stmts using the allocator in ASTContext
316 // or by doing a placement new.
317 void* operator new(size_t bytes, const ASTContext& C,
318 unsigned alignment = 8);
319
320 void* operator new(size_t bytes, const ASTContext* C,
321 unsigned alignment = 8) {
322 return operator new(bytes, *C, alignment);
323 }
324
325 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
326
327 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
328 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
329 void operator delete(void *, size_t) noexcept {}
330 void operator delete(void *, void *) noexcept {}
331
332public:
333 /// A placeholder type used to construct an empty shell of a
334 /// type, that will be filled in later (e.g., by some
335 /// de-serialization).
336 struct EmptyShell {};
337
338protected:
339 /// Iterator for iterating over Stmt * arrays that contain only Expr *
340 ///
341 /// This is needed because AST nodes use Stmt* arrays to store
342 /// references to children (to be compatible with StmtIterator).
343 struct ExprIterator
344 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
345 std::random_access_iterator_tag, Expr *> {
346 ExprIterator() : iterator_adaptor_base(nullptr) {}
347 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
348
349 reference operator*() const {
350 assert((*I)->getStmtClass() >= firstExprConstant &&
351 (*I)->getStmtClass() <= lastExprConstant);
352 return *reinterpret_cast<Expr **>(I);
353 }
354 };
355
356 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
357 struct ConstExprIterator
358 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
359 std::random_access_iterator_tag,
360 const Expr *const> {
361 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
362 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
363
364 reference operator*() const {
365 assert((*I)->getStmtClass() >= firstExprConstant &&
366 (*I)->getStmtClass() <= lastExprConstant);
367 return *reinterpret_cast<const Expr *const *>(I);
368 }
369 };
370
371private:
372 /// Whether statistic collection is enabled.
373 static bool StatisticsEnabled;
374
375protected:
376 /// Construct an empty statement.
377 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
378
379public:
380 Stmt(StmtClass SC) {
381 static_assert(sizeof(*this) == sizeof(void *),
382 "changing bitfields changed sizeof(Stmt)");
383 static_assert(sizeof(*this) % alignof(void *) == 0,
384 "Insufficient alignment!");
385 StmtBits.sClass = SC;
386 if (StatisticsEnabled) Stmt::addStmtClass(SC);
387 }
388
389 StmtClass getStmtClass() const {
390 return static_cast<StmtClass>(StmtBits.sClass);
391 }
392
393 const char *getStmtClassName() const;
394
395 /// SourceLocation tokens are not useful in isolation - they are low level
396 /// value objects created/interpreted by SourceManager. We assume AST
397 /// clients will have a pointer to the respective SourceManager.
398 SourceRange getSourceRange() const LLVM_READONLY;
399 SourceLocation getLocStart() const LLVM_READONLY;
400 SourceLocation getLocEnd() const LLVM_READONLY;
401
402 // global temp stats (until we have a per-module visitor)
403 static void addStmtClass(const StmtClass s);
404 static void EnableStatistics();
405 static void PrintStats();
406
407 /// Dumps the specified AST fragment and all subtrees to
408 /// \c llvm::errs().
409 void dump() const;
410 void dump(SourceManager &SM) const;
411 void dump(raw_ostream &OS, SourceManager &SM) const;
412 void dump(raw_ostream &OS) const;
413
414 /// dumpColor - same as dump(), but forces color highlighting.
415 void dumpColor() const;
416
417 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
418 /// back to its original source language syntax.
419 void dumpPretty(const ASTContext &Context) const;
420 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
421 const PrintingPolicy &Policy, unsigned Indentation = 0,
422 const ASTContext *Context = nullptr) const;
423
424 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
425 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
426 void viewAST() const;
427
428 /// Skip past any implicit AST nodes which might surround this
429 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
430 Stmt *IgnoreImplicit();
431 const Stmt *IgnoreImplicit() const {
432 return const_cast<Stmt *>(this)->IgnoreImplicit();
433 }
434
435 /// Skip no-op (attributed, compound) container stmts and skip captured
436 /// stmt at the top, if \a IgnoreCaptured is true.
437 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
438 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
439 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
440 }
441
442 const Stmt *stripLabelLikeStatements() const;
443 Stmt *stripLabelLikeStatements() {
444 return const_cast<Stmt*>(
445 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
446 }
447
448 /// Child Iterators: All subclasses must implement 'children'
449 /// to permit easy iteration over the substatements/subexpessions of an
450 /// AST node. This permits easy iteration over all nodes in the AST.
451 using child_iterator = StmtIterator;
452 using const_child_iterator = ConstStmtIterator;
453
454 using child_range = llvm::iterator_range<child_iterator>;
455 using const_child_range = llvm::iterator_range<const_child_iterator>;
456
457 child_range children();
458
459 const_child_range children() const {
460 auto Children = const_cast<Stmt *>(this)->children();
461 return const_child_range(Children.begin(), Children.end());
462 }
463
464 child_iterator child_begin() { return children().begin(); }
465 child_iterator child_end() { return children().end(); }
466
467 const_child_iterator child_begin() const { return children().begin(); }
468 const_child_iterator child_end() const { return children().end(); }
469
470 /// Produce a unique representation of the given statement.
471 ///
472 /// \param ID once the profiling operation is complete, will contain
473 /// the unique representation of the given statement.
474 ///
475 /// \param Context the AST context in which the statement resides
476 ///
477 /// \param Canonical whether the profile should be based on the canonical
478 /// representation of this statement (e.g., where non-type template
479 /// parameters are identified by index/level rather than their
480 /// declaration pointers) or the exact representation of the statement as
481 /// written in the source.
482 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
483 bool Canonical) const;
484
485 /// Calculate a unique representation for a statement that is
486 /// stable across compiler invocations.
487 ///
488 /// \param ID profile information will be stored in ID.
489 ///
490 /// \param Hash an ODRHash object which will be called where pointers would
491 /// have been used in the Profile function.
492 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
493};
494
495/// DeclStmt - Adaptor class for mixing declarations with statements and
496/// expressions. For example, CompoundStmt mixes statements, expressions
497/// and declarations (variables, types). Another example is ForStmt, where
498/// the first statement can be an expression or a declaration.
499class DeclStmt : public Stmt {
500 DeclGroupRef DG;
501 SourceLocation StartLoc, EndLoc;
502
503public:
504 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
505 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
506
507 /// Build an empty declaration statement.
508 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
509
510 /// isSingleDecl - This method returns true if this DeclStmt refers
511 /// to a single Decl.
512 bool isSingleDecl() const {
513 return DG.isSingleDecl();
514 }
515
516 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
517 Decl *getSingleDecl() { return DG.getSingleDecl(); }
518
519 const DeclGroupRef getDeclGroup() const { return DG; }
520 DeclGroupRef getDeclGroup() { return DG; }
521 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
522
523 SourceLocation getStartLoc() const { return StartLoc; }
524 void setStartLoc(SourceLocation L) { StartLoc = L; }
525 SourceLocation getEndLoc() const { return EndLoc; }
526 void setEndLoc(SourceLocation L) { EndLoc = L; }
527
528 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
529 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
530
531 static bool classof(const Stmt *T) {
532 return T->getStmtClass() == DeclStmtClass;
533 }
534
535 // Iterators over subexpressions.
536 child_range children() {
537 return child_range(child_iterator(DG.begin(), DG.end()),
538 child_iterator(DG.end(), DG.end()));
539 }
540
541 using decl_iterator = DeclGroupRef::iterator;
542 using const_decl_iterator = DeclGroupRef::const_iterator;
543 using decl_range = llvm::iterator_range<decl_iterator>;
544 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
545
546 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
547
548 decl_const_range decls() const {
549 return decl_const_range(decl_begin(), decl_end());
550 }
551
552 decl_iterator decl_begin() { return DG.begin(); }
553 decl_iterator decl_end() { return DG.end(); }
554 const_decl_iterator decl_begin() const { return DG.begin(); }
555 const_decl_iterator decl_end() const { return DG.end(); }
556
557 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
558
559 reverse_decl_iterator decl_rbegin() {
560 return reverse_decl_iterator(decl_end());
561 }
562
563 reverse_decl_iterator decl_rend() {
564 return reverse_decl_iterator(decl_begin());
565 }
566};
567
568/// NullStmt - This is the null statement ";": C99 6.8.3p3.
569///
570class NullStmt : public Stmt {
571 SourceLocation SemiLoc;
572
573 /// True if the null statement was preceded by an empty macro, e.g:
574 /// @code
575 /// #define CALL(x)
576 /// CALL(0);
577 /// @endcode
578 bool HasLeadingEmptyMacro = false;
579
580public:
581 friend class ASTStmtReader;
582 friend class ASTStmtWriter;
583
584 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
585 : Stmt(NullStmtClass), SemiLoc(L),
586 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
587
588 /// Build an empty null statement.
589 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
590
591 SourceLocation getSemiLoc() const { return SemiLoc; }
592 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
593
594 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
595
596 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
597 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
598
599 static bool classof(const Stmt *T) {
600 return T->getStmtClass() == NullStmtClass;
601 }
602
603 child_range children() {
604 return child_range(child_iterator(), child_iterator());
605 }
606};
607
608/// CompoundStmt - This represents a group of statements like { stmt stmt }.
609class CompoundStmt final : public Stmt,
610 private llvm::TrailingObjects<CompoundStmt, Stmt *> {
611 friend class ASTStmtReader;
612 friend TrailingObjects;
613
614 SourceLocation LBraceLoc, RBraceLoc;
615
616 CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
617 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
618
619 void setStmts(ArrayRef<Stmt *> Stmts);
620
621public:
622 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
623 SourceLocation LB, SourceLocation RB);
624
625 // Build an empty compound statement with a location.
626 explicit CompoundStmt(SourceLocation Loc)
627 : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) {
628 CompoundStmtBits.NumStmts = 0;
629 }
630
631 // Build an empty compound statement.
632 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
633
634 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
635 unsigned size() const { return CompoundStmtBits.NumStmts; }
636
637 using body_iterator = Stmt **;
638 using body_range = llvm::iterator_range<body_iterator>;
639
640 body_range body() { return body_range(body_begin(), body_end()); }
641 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
642 body_iterator body_end() { return body_begin() + size(); }
643 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
644
645 Stmt *body_back() {
646 return !body_empty() ? body_begin()[size() - 1] : nullptr;
647 }
648
649 void setLastStmt(Stmt *S) {
650 assert(!body_empty() && "setLastStmt");
651 body_begin()[size() - 1] = S;
652 }
653
654 using const_body_iterator = Stmt* const *;
655 using body_const_range = llvm::iterator_range<const_body_iterator>;
656
657 body_const_range body() const {
658 return body_const_range(body_begin(), body_end());
659 }
660
661 const_body_iterator body_begin() const {
662 return getTrailingObjects<Stmt *>();
663 }
664
665 const_body_iterator body_end() const { return body_begin() + size(); }
666
667 const Stmt *body_front() const {
668 return !body_empty() ? body_begin()[0] : nullptr;
669 }
670
671 const Stmt *body_back() const {
672 return !body_empty() ? body_begin()[size() - 1] : nullptr;
673 }
674
675 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
676
677 reverse_body_iterator body_rbegin() {
678 return reverse_body_iterator(body_end());
679 }
680
681 reverse_body_iterator body_rend() {
682 return reverse_body_iterator(body_begin());
683 }
684
685 using const_reverse_body_iterator =
686 std::reverse_iterator<const_body_iterator>;
687
688 const_reverse_body_iterator body_rbegin() const {
689 return const_reverse_body_iterator(body_end());
690 }
691
692 const_reverse_body_iterator body_rend() const {
693 return const_reverse_body_iterator(body_begin());
694 }
695
696 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
697 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
698
699 SourceLocation getLBracLoc() const { return LBraceLoc; }
700 SourceLocation getRBracLoc() const { return RBraceLoc; }
701
702 static bool classof(const Stmt *T) {
703 return T->getStmtClass() == CompoundStmtClass;
704 }
705
706 // Iterators
707 child_range children() { return child_range(body_begin(), body_end()); }
708
709 const_child_range children() const {
710 return const_child_range(body_begin(), body_end());
711 }
712};
713
714// SwitchCase is the base class for CaseStmt and DefaultStmt,
715class SwitchCase : public Stmt {
716protected:
717 // A pointer to the following CaseStmt or DefaultStmt class,
718 // used by SwitchStmt.
719 SwitchCase *NextSwitchCase = nullptr;
720 SourceLocation KeywordLoc;
721 SourceLocation ColonLoc;
722
723 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
724 : Stmt(SC), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
725
726 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
727
728public:
729 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
730
731 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
732
733 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
734
735 SourceLocation getKeywordLoc() const { return KeywordLoc; }
736 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
737 SourceLocation getColonLoc() const { return ColonLoc; }
738 void setColonLoc(SourceLocation L) { ColonLoc = L; }
739
740 Stmt *getSubStmt();
741 const Stmt *getSubStmt() const {
742 return const_cast<SwitchCase*>(this)->getSubStmt();
743 }
744
745 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
746 SourceLocation getLocEnd() const LLVM_READONLY;
747
748 static bool classof(const Stmt *T) {
749 return T->getStmtClass() == CaseStmtClass ||
750 T->getStmtClass() == DefaultStmtClass;
751 }
752};
753
754class CaseStmt : public SwitchCase {
755 SourceLocation EllipsisLoc;
756 enum { LHS, RHS, SUBSTMT, END_EXPR };
757 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
758 // GNU "case 1 ... 4" extension
759
760public:
761 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
762 SourceLocation ellipsisLoc, SourceLocation colonLoc)
763 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
764 SubExprs[SUBSTMT] = nullptr;
765 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
766 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
767 EllipsisLoc = ellipsisLoc;
768 }
769
770 /// Build an empty switch case statement.
771 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) {}
772
773 SourceLocation getCaseLoc() const { return KeywordLoc; }
774 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
775 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
776 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
777 SourceLocation getColonLoc() const { return ColonLoc; }
778 void setColonLoc(SourceLocation L) { ColonLoc = L; }
779
780 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
781 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
782 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
783
784 const Expr *getLHS() const {
785 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
786 }
787
788 const Expr *getRHS() const {
789 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
790 }
791
792 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
793
794 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
795 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
796 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
797
798 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
799
800 SourceLocation getLocEnd() const LLVM_READONLY {
801 // Handle deeply nested case statements with iteration instead of recursion.
802 const CaseStmt *CS = this;
803 while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
804 CS = CS2;
805
806 return CS->getSubStmt()->getLocEnd();
807 }
808
809 static bool classof(const Stmt *T) {
810 return T->getStmtClass() == CaseStmtClass;
811 }
812
813 // Iterators
814 child_range children() {
815 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
816 }
817};
818
819class DefaultStmt : public SwitchCase {
820 Stmt* SubStmt;
821
822public:
823 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
824 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
825
826 /// Build an empty default statement.
827 explicit DefaultStmt(EmptyShell Empty)
828 : SwitchCase(DefaultStmtClass, Empty) {}
829
830 Stmt *getSubStmt() { return SubStmt; }
831 const Stmt *getSubStmt() const { return SubStmt; }
832 void setSubStmt(Stmt *S) { SubStmt = S; }
833
834 SourceLocation getDefaultLoc() const { return KeywordLoc; }
835 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
836 SourceLocation getColonLoc() const { return ColonLoc; }
837 void setColonLoc(SourceLocation L) { ColonLoc = L; }
838
839 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
840 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
841
842 static bool classof(const Stmt *T) {
843 return T->getStmtClass() == DefaultStmtClass;
844 }
845
846 // Iterators
847 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
848};
849
850inline SourceLocation SwitchCase::getLocEnd() const {
851 if (const auto *CS = dyn_cast<CaseStmt>(this))
852 return CS->getLocEnd();
853 return cast<DefaultStmt>(this)->getLocEnd();
854}
855
856/// LabelStmt - Represents a label, which has a substatement. For example:
857/// foo: return;
858class LabelStmt : public Stmt {
859 SourceLocation IdentLoc;
860 LabelDecl *TheDecl;
861 Stmt *SubStmt;
862
863public:
864 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
865 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
866 static_assert(sizeof(LabelStmt) ==
867 2 * sizeof(SourceLocation) + 2 * sizeof(void *),
868 "LabelStmt too big");
869 }
870
871 // Build an empty label statement.
872 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) {}
873
874 SourceLocation getIdentLoc() const { return IdentLoc; }
875 LabelDecl *getDecl() const { return TheDecl; }
876 void setDecl(LabelDecl *D) { TheDecl = D; }
877 const char *getName() const;
878 Stmt *getSubStmt() { return SubStmt; }
879 const Stmt *getSubStmt() const { return SubStmt; }
880 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
881 void setSubStmt(Stmt *SS) { SubStmt = SS; }
882
883 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
884 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
885
886 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
887
888 static bool classof(const Stmt *T) {
889 return T->getStmtClass() == LabelStmtClass;
890 }
891};
892
893/// Represents an attribute applied to a statement.
894///
895/// Represents an attribute applied to a statement. For example:
896/// [[omp::for(...)]] for (...) { ... }
897class AttributedStmt final
898 : public Stmt,
899 private llvm::TrailingObjects<AttributedStmt, const Attr *> {
900 friend class ASTStmtReader;
901 friend TrailingObjects;
902
903 Stmt *SubStmt;
904 SourceLocation AttrLoc;
905 unsigned NumAttrs;
906
907 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
908 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
909 NumAttrs(Attrs.size()) {
910 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
911 }
912
913 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
914 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
915 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
916 }
917
918 const Attr *const *getAttrArrayPtr() const {
919 return getTrailingObjects<const Attr *>();
920 }
921 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
922
923public:
924 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
925 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
926
927 // Build an empty attributed statement.
928 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
929
930 SourceLocation getAttrLoc() const { return AttrLoc; }
931 ArrayRef<const Attr*> getAttrs() const {
932 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
933 }
934
935 Stmt *getSubStmt() { return SubStmt; }
936 const Stmt *getSubStmt() const { return SubStmt; }
937
938 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
939 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
940
941 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
942
943 static bool classof(const Stmt *T) {
944 return T->getStmtClass() == AttributedStmtClass;
945 }
946};
947
948/// IfStmt - This represents an if/then/else.
949class IfStmt : public Stmt {
950 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
951 Stmt* SubExprs[END_EXPR];
952
953 SourceLocation IfLoc;
954 SourceLocation ElseLoc;
955
956public:
957 IfStmt(const ASTContext &C, SourceLocation IL,
958 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
959 Stmt *then, SourceLocation EL = SourceLocation(),
960 Stmt *elsev = nullptr);
961
962 /// Build an empty if/then/else statement
963 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) {}
964
965 /// Retrieve the variable declared in this "if" statement, if any.
966 ///
967 /// In the following example, "x" is the condition variable.
968 /// \code
969 /// if (int x = foo()) {
970 /// printf("x is %d", x);
971 /// }
972 /// \endcode
973 VarDecl *getConditionVariable() const;
974 void setConditionVariable(const ASTContext &C, VarDecl *V);
975
976 /// If this IfStmt has a condition variable, return the faux DeclStmt
977 /// associated with the creation of that condition variable.
978 const DeclStmt *getConditionVariableDeclStmt() const {
979 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
980 }
981
982 Stmt *getInit() { return SubExprs[INIT]; }
983 const Stmt *getInit() const { return SubExprs[INIT]; }
984 void setInit(Stmt *S) { SubExprs[INIT] = S; }
985 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
986 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
987 const Stmt *getThen() const { return SubExprs[THEN]; }
988 void setThen(Stmt *S) { SubExprs[THEN] = S; }
989 const Stmt *getElse() const { return SubExprs[ELSE]; }
990 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
991
992 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
993 Stmt *getThen() { return SubExprs[THEN]; }
994 Stmt *getElse() { return SubExprs[ELSE]; }
995
996 SourceLocation getIfLoc() const { return IfLoc; }
997 void setIfLoc(SourceLocation L) { IfLoc = L; }
998 SourceLocation getElseLoc() const { return ElseLoc; }
999 void setElseLoc(SourceLocation L) { ElseLoc = L; }
1000
1001 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
1002 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
1003
1004 bool isObjCAvailabilityCheck() const;
1005
1006 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
1007
1008 SourceLocation getLocEnd() const LLVM_READONLY {
1009 if (SubExprs[ELSE])
1010 return SubExprs[ELSE]->getLocEnd();
1011 else
1012 return SubExprs[THEN]->getLocEnd();
1013 }
1014
1015 // Iterators over subexpressions. The iterators will include iterating
1016 // over the initialization expression referenced by the condition variable.
1017 child_range children() {
1018 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1019 }
1020
1021 static bool classof(const Stmt *T) {
1022 return T->getStmtClass() == IfStmtClass;
1023 }
1024};
1025
1026/// SwitchStmt - This represents a 'switch' stmt.
1027class SwitchStmt : public Stmt {
1028 SourceLocation SwitchLoc;
1029 enum { INIT, VAR, COND, BODY, END_EXPR };
1030 Stmt* SubExprs[END_EXPR];
1031
1032 // This points to a linked list of case and default statements and, if the
1033 // SwitchStmt is a switch on an enum value, records whether all the enum
1034 // values were covered by CaseStmts. The coverage information value is meant
1035 // to be a hint for possible clients.
1036 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
1037
1038public:
1039 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
1040
1041 /// Build a empty switch statement.
1042 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) {}
1043
1044 /// Retrieve the variable declared in this "switch" statement, if any.
1045 ///
1046 /// In the following example, "x" is the condition variable.
1047 /// \code
1048 /// switch (int x = foo()) {
1049 /// case 0: break;
1050 /// // ...
1051 /// }
1052 /// \endcode
1053 VarDecl *getConditionVariable() const;
1054 void setConditionVariable(const ASTContext &C, VarDecl *V);
1055
1056 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1057 /// associated with the creation of that condition variable.
1058 const DeclStmt *getConditionVariableDeclStmt() const {
1059 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1060 }
1061
1062 Stmt *getInit() { return SubExprs[INIT]; }
1063 const Stmt *getInit() const { return SubExprs[INIT]; }
1064 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1065 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1066 const Stmt *getBody() const { return SubExprs[BODY]; }
1067 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1068
1069 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1070 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1071 Stmt *getBody() { return SubExprs[BODY]; }
1072 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1073 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1074
1075 /// Set the case list for this switch statement.
1076 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1077
1078 SourceLocation getSwitchLoc() const { return SwitchLoc; }
1079 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1080
1081 void setBody(Stmt *S, SourceLocation SL) {
1082 SubExprs[BODY] = S;
1083 SwitchLoc = SL;
1084 }
1085
1086 void addSwitchCase(SwitchCase *SC) {
1087 assert(!SC->getNextSwitchCase()
1088 && "case/default already added to a switch");
1089 SC->setNextSwitchCase(FirstCase.getPointer());
1090 FirstCase.setPointer(SC);
1091 }
1092
1093 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1094 /// switch over an enum value then all cases have been explicitly covered.
1095 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1096
1097 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1098 /// have been explicitly covered.
1099 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1100
1101 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1102
1103 SourceLocation getLocEnd() const LLVM_READONLY {
1104 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1105 }
1106
1107 // Iterators
1108 child_range children() {
1109 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1110 }
1111
1112 static bool classof(const Stmt *T) {
1113 return T->getStmtClass() == SwitchStmtClass;
1114 }
1115};
1116
1117/// WhileStmt - This represents a 'while' stmt.
1118class WhileStmt : public Stmt {
1119 SourceLocation WhileLoc;
1120 enum { VAR, COND, BODY, END_EXPR };
1121 Stmt* SubExprs[END_EXPR];
1122
1123public:
1124 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1125 SourceLocation WL);
1126
1127 /// Build an empty while statement.
1128 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) {}
1129
1130 /// Retrieve the variable declared in this "while" statement, if any.
1131 ///
1132 /// In the following example, "x" is the condition variable.
1133 /// \code
1134 /// while (int x = random()) {
1135 /// // ...
1136 /// }
1137 /// \endcode
1138 VarDecl *getConditionVariable() const;
1139 void setConditionVariable(const ASTContext &C, VarDecl *V);
1140
1141 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1142 /// associated with the creation of that condition variable.
1143 const DeclStmt *getConditionVariableDeclStmt() const {
1144 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1145 }
1146
1147 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1148 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1149 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1150 Stmt *getBody() { return SubExprs[BODY]; }
1151 const Stmt *getBody() const { return SubExprs[BODY]; }
1152 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1153
1154 SourceLocation getWhileLoc() const { return WhileLoc; }
1155 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1156
1157 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1158
1159 SourceLocation getLocEnd() const LLVM_READONLY {
1160 return SubExprs[BODY]->getLocEnd();
1161 }
1162
1163 static bool classof(const Stmt *T) {
1164 return T->getStmtClass() == WhileStmtClass;
1165 }
1166
1167 // Iterators
1168 child_range children() {
1169 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1170 }
1171};
1172
1173/// DoStmt - This represents a 'do/while' stmt.
1174class DoStmt : public Stmt {
1175 SourceLocation DoLoc;
1176 enum { BODY, COND, END_EXPR };
1177 Stmt* SubExprs[END_EXPR];
1178 SourceLocation WhileLoc;
1179 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1180
1181public:
1182 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1183 SourceLocation RP)
1184 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1185 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1186 SubExprs[BODY] = body;
1187 }
1188
1189 /// Build an empty do-while statement.
1190 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
1191
1192 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1193 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1194 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1195 Stmt *getBody() { return SubExprs[BODY]; }
1196 const Stmt *getBody() const { return SubExprs[BODY]; }
1197 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1198
1199 SourceLocation getDoLoc() const { return DoLoc; }
1200 void setDoLoc(SourceLocation L) { DoLoc = L; }
1201 SourceLocation getWhileLoc() const { return WhileLoc; }
1202 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1203
1204 SourceLocation getRParenLoc() const { return RParenLoc; }
1205 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1206
1207 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1208 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1209
1210 static bool classof(const Stmt *T) {
1211 return T->getStmtClass() == DoStmtClass;
1212 }
1213
1214 // Iterators
1215 child_range children() {
1216 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1217 }
1218};
1219
1220/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1221/// the init/cond/inc parts of the ForStmt will be null if they were not
1222/// specified in the source.
1223class ForStmt : public Stmt {
1224 SourceLocation ForLoc;
1225 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1226 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1227 SourceLocation LParenLoc, RParenLoc;
1228
1229public:
1230 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1231 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1232 SourceLocation RP);
1233
1234 /// Build an empty for statement.
1235 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
1236
1237 Stmt *getInit() { return SubExprs[INIT]; }
1238
1239 /// Retrieve the variable declared in this "for" statement, if any.
1240 ///
1241 /// In the following example, "y" is the condition variable.
1242 /// \code
1243 /// for (int x = random(); int y = mangle(x); ++x) {
1244 /// // ...
1245 /// }
1246 /// \endcode
1247 VarDecl *getConditionVariable() const;
1248 void setConditionVariable(const ASTContext &C, VarDecl *V);
1249
1250 /// If this ForStmt has a condition variable, return the faux DeclStmt
1251 /// associated with the creation of that condition variable.
1252 const DeclStmt *getConditionVariableDeclStmt() const {
1253 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1254 }
1255
1256 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1257 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1258 Stmt *getBody() { return SubExprs[BODY]; }
1259
1260 const Stmt *getInit() const { return SubExprs[INIT]; }
1261 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1262 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1263 const Stmt *getBody() const { return SubExprs[BODY]; }
1264
1265 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1266 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1267 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1268 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1269
1270 SourceLocation getForLoc() const { return ForLoc; }
1271 void setForLoc(SourceLocation L) { ForLoc = L; }
1272 SourceLocation getLParenLoc() const { return LParenLoc; }
1273 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1274 SourceLocation getRParenLoc() const { return RParenLoc; }
1275 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1276
1277 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1278
1279 SourceLocation getLocEnd() const LLVM_READONLY {
1280 return SubExprs[BODY]->getLocEnd();
1281 }
1282
1283 static bool classof(const Stmt *T) {
1284 return T->getStmtClass() == ForStmtClass;
1285 }
1286
1287 // Iterators
1288 child_range children() {
1289 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1290 }
1291};
1292
1293/// GotoStmt - This represents a direct goto.
1294class GotoStmt : public Stmt {
1295 LabelDecl *Label;
1296 SourceLocation GotoLoc;
1297 SourceLocation LabelLoc;
1298
1299public:
1300 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1301 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1302
1303 /// Build an empty goto statement.
1304 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
1305
1306 LabelDecl *getLabel() const { return Label; }
1307 void setLabel(LabelDecl *D) { Label = D; }
1308
1309 SourceLocation getGotoLoc() const { return GotoLoc; }
1310 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1311 SourceLocation getLabelLoc() const { return LabelLoc; }
1312 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1313
1314 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1315 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1316
1317 static bool classof(const Stmt *T) {
1318 return T->getStmtClass() == GotoStmtClass;
1319 }
1320
1321 // Iterators
1322 child_range children() {
1323 return child_range(child_iterator(), child_iterator());
1324 }
1325};
1326
1327/// IndirectGotoStmt - This represents an indirect goto.
1328class IndirectGotoStmt : public Stmt {
1329 SourceLocation GotoLoc;
1330 SourceLocation StarLoc;
1331 Stmt *Target;
1332
1333public:
1334 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1335 Expr *target)
1336 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1337 Target((Stmt*)target) {}
1338
1339 /// Build an empty indirect goto statement.
1340 explicit IndirectGotoStmt(EmptyShell Empty)
1341 : Stmt(IndirectGotoStmtClass, Empty) {}
1342
1343 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1344 SourceLocation getGotoLoc() const { return GotoLoc; }
1345 void setStarLoc(SourceLocation L) { StarLoc = L; }
1346 SourceLocation getStarLoc() const { return StarLoc; }
1347
1348 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1349 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1350 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1351
1352 /// getConstantTarget - Returns the fixed target of this indirect
1353 /// goto, if one exists.
1354 LabelDecl *getConstantTarget();
1355 const LabelDecl *getConstantTarget() const {
1356 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1357 }
1358
1359 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1360 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1361
1362 static bool classof(const Stmt *T) {
1363 return T->getStmtClass() == IndirectGotoStmtClass;
1364 }
1365
1366 // Iterators
1367 child_range children() { return child_range(&Target, &Target+1); }
1368};
1369
1370/// ContinueStmt - This represents a continue.
1371class ContinueStmt : public Stmt {
1372 SourceLocation ContinueLoc;
1373
1374public:
1375 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1376
1377 /// Build an empty continue statement.
1378 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
1379
1380 SourceLocation getContinueLoc() const { return ContinueLoc; }
1381 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1382
1383 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1384 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1385
1386 static bool classof(const Stmt *T) {
1387 return T->getStmtClass() == ContinueStmtClass;
1388 }
1389
1390 // Iterators
1391 child_range children() {
1392 return child_range(child_iterator(), child_iterator());
1393 }
1394};
1395
1396/// BreakStmt - This represents a break.
1397class BreakStmt : public Stmt {
1398 SourceLocation BreakLoc;
1399
1400public:
1401 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1402 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1403 "BreakStmt too large");
1404 }
1405
1406 /// Build an empty break statement.
1407 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
1408
1409 SourceLocation getBreakLoc() const { return BreakLoc; }
1410 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1411
1412 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1413 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1414
1415 static bool classof(const Stmt *T) {
1416 return T->getStmtClass() == BreakStmtClass;
1417 }
1418
1419 // Iterators
1420 child_range children() {
1421 return child_range(child_iterator(), child_iterator());
1422 }
1423};
1424
1425/// ReturnStmt - This represents a return, optionally of an expression:
1426/// return;
1427/// return 4;
1428///
1429/// Note that GCC allows return with no argument in a function declared to
1430/// return a value, and it allows returning a value in functions declared to
1431/// return void. We explicitly model this in the AST, which means you can't
1432/// depend on the return type of the function and the presence of an argument.
1433class ReturnStmt : public Stmt {
1434 SourceLocation RetLoc;
1435 Stmt *RetExpr;
1436 const VarDecl *NRVOCandidate;
1437
1438public:
1439 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1440
1441 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1442 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1443 NRVOCandidate(NRVOCandidate) {}
1444
1445 /// Build an empty return expression.
1446 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) {}
1447
1448 const Expr *getRetValue() const;
1449 Expr *getRetValue();
1450 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1451
1452 SourceLocation getReturnLoc() const { return RetLoc; }
1453 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1454
1455 /// Retrieve the variable that might be used for the named return
1456 /// value optimization.
1457 ///
1458 /// The optimization itself can only be performed if the variable is
1459 /// also marked as an NRVO object.
1460 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1461 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1462
1463 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1464
1465 SourceLocation getLocEnd() const LLVM_READONLY {
1466 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1467 }
1468
1469 static bool classof(const Stmt *T) {
1470 return T->getStmtClass() == ReturnStmtClass;
1471 }
1472
1473 // Iterators
1474 child_range children() {
1475 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1476 return child_range(child_iterator(), child_iterator());
1477 }
1478};
1479
1480/// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1481class AsmStmt : public Stmt {
1482protected:
1483 friend class ASTStmtReader;
1484
1485 SourceLocation AsmLoc;
1486
1487 /// True if the assembly statement does not have any input or output
1488 /// operands.
1489 bool IsSimple;
1490
1491 /// If true, treat this inline assembly as having side effects.
1492 /// This assembly statement should not be optimized, deleted or moved.
1493 bool IsVolatile;
1494
1495 unsigned NumOutputs;
1496 unsigned NumInputs;
1497 unsigned NumClobbers;
1498
1499 Stmt **Exprs = nullptr;
1500
1501 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1502 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
1503 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1504 NumOutputs(numoutputs), NumInputs(numinputs),
1505 NumClobbers(numclobbers) {}
1506
1507public:
1508 /// Build an empty inline-assembly statement.
1509 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
1510
1511 SourceLocation getAsmLoc() const { return AsmLoc; }
1512 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1513
1514 bool isSimple() const { return IsSimple; }
1515 void setSimple(bool V) { IsSimple = V; }
1516
1517 bool isVolatile() const { return IsVolatile; }
1518 void setVolatile(bool V) { IsVolatile = V; }
1519
1520 SourceLocation getLocStart() const LLVM_READONLY { return {}; }
1521 SourceLocation getLocEnd() const LLVM_READONLY { return {}; }
1522
1523 //===--- Asm String Analysis ---===//
1524
1525 /// Assemble final IR asm string.
1526 std::string generateAsmString(const ASTContext &C) const;
1527
1528 //===--- Output operands ---===//
1529
1530 unsigned getNumOutputs() const { return NumOutputs; }
1531
1532 /// getOutputConstraint - Return the constraint string for the specified
1533 /// output operand. All output constraints are known to be non-empty (either
1534 /// '=' or '+').
1535 StringRef getOutputConstraint(unsigned i) const;
1536
1537 /// isOutputPlusConstraint - Return true if the specified output constraint
1538 /// is a "+" constraint (which is both an input and an output) or false if it
1539 /// is an "=" constraint (just an output).
1540 bool isOutputPlusConstraint(unsigned i) const {
1541 return getOutputConstraint(i)[0] == '+';
1542 }
1543
1544 const Expr *getOutputExpr(unsigned i) const;
1545
1546 /// getNumPlusOperands - Return the number of output operands that have a "+"
1547 /// constraint.
1548 unsigned getNumPlusOperands() const;
1549
1550 //===--- Input operands ---===//
1551
1552 unsigned getNumInputs() const { return NumInputs; }
1553
1554 /// getInputConstraint - Return the specified input constraint. Unlike output
1555 /// constraints, these can be empty.
1556 StringRef getInputConstraint(unsigned i) const;
1557
1558 const Expr *getInputExpr(unsigned i) const;
1559
1560 //===--- Other ---===//
1561
1562 unsigned getNumClobbers() const { return NumClobbers; }
1563 StringRef getClobber(unsigned i) const;
1564
1565 static bool classof(const Stmt *T) {
1566 return T->getStmtClass() == GCCAsmStmtClass ||
1567 T->getStmtClass() == MSAsmStmtClass;
1568 }
1569
1570 // Input expr iterators.
1571
1572 using inputs_iterator = ExprIterator;
1573 using const_inputs_iterator = ConstExprIterator;
1574 using inputs_range = llvm::iterator_range<inputs_iterator>;
1575 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
1576
1577 inputs_iterator begin_inputs() {
1578 return &Exprs[0] + NumOutputs;
1579 }
1580
1581 inputs_iterator end_inputs() {
1582 return &Exprs[0] + NumOutputs + NumInputs;
1583 }
1584
1585 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1586
1587 const_inputs_iterator begin_inputs() const {
1588 return &Exprs[0] + NumOutputs;
1589 }
1590
1591 const_inputs_iterator end_inputs() const {
1592 return &Exprs[0] + NumOutputs + NumInputs;
1593 }
1594
1595 inputs_const_range inputs() const {
1596 return inputs_const_range(begin_inputs(), end_inputs());
1597 }
1598
1599 // Output expr iterators.
1600
1601 using outputs_iterator = ExprIterator;
1602 using const_outputs_iterator = ConstExprIterator;
1603 using outputs_range = llvm::iterator_range<outputs_iterator>;
1604 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
1605
1606 outputs_iterator begin_outputs() {
1607 return &Exprs[0];
1608 }
1609
1610 outputs_iterator end_outputs() {
1611 return &Exprs[0] + NumOutputs;
1612 }
1613
1614 outputs_range outputs() {
1615 return outputs_range(begin_outputs(), end_outputs());
1616 }
1617
1618 const_outputs_iterator begin_outputs() const {
1619 return &Exprs[0];
1620 }
1621
1622 const_outputs_iterator end_outputs() const {
1623 return &Exprs[0] + NumOutputs;
1624 }
1625
1626 outputs_const_range outputs() const {
1627 return outputs_const_range(begin_outputs(), end_outputs());
1628 }
1629
1630 child_range children() {
1631 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1632 }
1633};
1634
1635/// This represents a GCC inline-assembly statement extension.
1636class GCCAsmStmt : public AsmStmt {
1637 friend class ASTStmtReader;
1638
1639 SourceLocation RParenLoc;
1640 StringLiteral *AsmStr;
1641
1642 // FIXME: If we wanted to, we could allocate all of these in one big array.
1643 StringLiteral **Constraints = nullptr;
1644 StringLiteral **Clobbers = nullptr;
1645 IdentifierInfo **Names = nullptr;
1646
1647public:
1648 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1649 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1650 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1651 StringLiteral *asmstr, unsigned numclobbers,
1652 StringLiteral **clobbers, SourceLocation rparenloc);
1653
1654 /// Build an empty inline-assembly statement.
1655 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
1656
1657 SourceLocation getRParenLoc() const { return RParenLoc; }
1658 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1659
1660 //===--- Asm String Analysis ---===//
1661
1662 const StringLiteral *getAsmString() const { return AsmStr; }
1663 StringLiteral *getAsmString() { return AsmStr; }
1664 void setAsmString(StringLiteral *E) { AsmStr = E; }
1665
1666 /// AsmStringPiece - this is part of a decomposed asm string specification
1667 /// (for use with the AnalyzeAsmString function below). An asm string is
1668 /// considered to be a concatenation of these parts.
1669 class AsmStringPiece {
1670 public:
1671 enum Kind {
1672 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1673 Operand // Operand reference, with optional modifier %c4.
1674 };
1675
1676 private:
1677 Kind MyKind;
1678 std::string Str;
1679 unsigned OperandNo;
1680
1681 // Source range for operand references.
1682 CharSourceRange Range;
1683
1684 public:
1685 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1686 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1687 SourceLocation End)
1688 : MyKind(Operand), Str(S), OperandNo(OpNo),
1689 Range(CharSourceRange::getCharRange(Begin, End)) {}
1690
1691 bool isString() const { return MyKind == String; }
1692 bool isOperand() const { return MyKind == Operand; }
1693
1694 const std::string &getString() const { return Str; }
1695
1696 unsigned getOperandNo() const {
1697 assert(isOperand());
1698 return OperandNo;
1699 }
1700
1701 CharSourceRange getRange() const {
1702 assert(isOperand() && "Range is currently used only for Operands.");
1703 return Range;
1704 }
1705
1706 /// getModifier - Get the modifier for this operand, if present. This
1707 /// returns '\0' if there was no modifier.
1708 char getModifier() const;
1709 };
1710
1711 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1712 /// it into pieces. If the asm string is erroneous, emit errors and return
1713 /// true, otherwise return false. This handles canonicalization and
1714 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1715 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1716 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1717 const ASTContext &C, unsigned &DiagOffs) const;
1718
1719 /// Assemble final IR asm string.
1720 std::string generateAsmString(const ASTContext &C) const;
1721
1722 //===--- Output operands ---===//
1723
1724 IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
1725
1726 StringRef getOutputName(unsigned i) const {
1727 if (IdentifierInfo *II = getOutputIdentifier(i))
1728 return II->getName();
1729
1730 return {};
1731 }
1732
1733 StringRef getOutputConstraint(unsigned i) const;
1734
1735 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1736 return Constraints[i];
1737 }
1738 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1739 return Constraints[i];
1740 }
1741
1742 Expr *getOutputExpr(unsigned i);
1743
1744 const Expr *getOutputExpr(unsigned i) const {
1745 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1746 }
1747
1748 //===--- Input operands ---===//
1749
1750 IdentifierInfo *getInputIdentifier(unsigned i) const {
1751 return Names[i + NumOutputs];
1752 }
1753
1754 StringRef getInputName(unsigned i) const {
1755 if (IdentifierInfo *II = getInputIdentifier(i))
1756 return II->getName();
1757
1758 return {};
1759 }
1760
1761 StringRef getInputConstraint(unsigned i) const;
1762
1763 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1764 return Constraints[i + NumOutputs];
1765 }
1766 StringLiteral *getInputConstraintLiteral(unsigned i) {
1767 return Constraints[i + NumOutputs];
1768 }
1769
1770 Expr *getInputExpr(unsigned i);
1771 void setInputExpr(unsigned i, Expr *E);
1772
1773 const Expr *getInputExpr(unsigned i) const {
1774 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1775 }
1776
1777private:
1778 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1779 IdentifierInfo **Names,
1780 StringLiteral **Constraints,
1781 Stmt **Exprs,
1782 unsigned NumOutputs,
1783 unsigned NumInputs,
1784 StringLiteral **Clobbers,
1785 unsigned NumClobbers);
1786
1787public:
1788 //===--- Other ---===//
1789
1790 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1791 /// translate this into a numeric value needed to reference the same operand.
1792 /// This returns -1 if the operand name is invalid.
1793 int getNamedOperand(StringRef SymbolicName) const;
1794
1795 StringRef getClobber(unsigned i) const;
1796
1797 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1798 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1799 return Clobbers[i];
1800 }
1801
1802 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1803 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1804
1805 static bool classof(const Stmt *T) {
1806 return T->getStmtClass() == GCCAsmStmtClass;
1807 }
1808};
1809
1810/// This represents a Microsoft inline-assembly statement extension.
1811class MSAsmStmt : public AsmStmt {
1812 friend class ASTStmtReader;
1813
1814 SourceLocation LBraceLoc, EndLoc;
1815 StringRef AsmStr;
1816
1817 unsigned NumAsmToks = 0;
1818
1819 Token *AsmToks = nullptr;
1820 StringRef *Constraints = nullptr;
1821 StringRef *Clobbers = nullptr;
1822
1823public:
1824 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1825 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1826 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1827 ArrayRef<StringRef> constraints,
1828 ArrayRef<Expr*> exprs, StringRef asmstr,
1829 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1830
1831 /// Build an empty MS-style inline-assembly statement.
1832 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
1833
1834 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1835 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1836 SourceLocation getEndLoc() const { return EndLoc; }
1837 void setEndLoc(SourceLocation L) { EndLoc = L; }
1838
1839 bool hasBraces() const { return LBraceLoc.isValid(); }
1840
1841 unsigned getNumAsmToks() { return NumAsmToks; }
1842 Token *getAsmToks() { return AsmToks; }
1843
1844 //===--- Asm String Analysis ---===//
1845 StringRef getAsmString() const { return AsmStr; }
1846
1847 /// Assemble final IR asm string.
1848 std::string generateAsmString(const ASTContext &C) const;
1849
1850 //===--- Output operands ---===//
1851
1852 StringRef getOutputConstraint(unsigned i) const {
1853 assert(i < NumOutputs);
1854 return Constraints[i];
1855 }
1856
1857 Expr *getOutputExpr(unsigned i);
1858
1859 const Expr *getOutputExpr(unsigned i) const {
1860 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1861 }
1862
1863 //===--- Input operands ---===//
1864
1865 StringRef getInputConstraint(unsigned i) const {
1866 assert(i < NumInputs);
1867 return Constraints[i + NumOutputs];
1868 }
1869
1870 Expr *getInputExpr(unsigned i);
1871 void setInputExpr(unsigned i, Expr *E);
1872
1873 const Expr *getInputExpr(unsigned i) const {
1874 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1875 }
1876
1877 //===--- Other ---===//
1878
1879 ArrayRef<StringRef> getAllConstraints() const {
1880 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1881 }
1882
1883 ArrayRef<StringRef> getClobbers() const {
1884 return llvm::makeArrayRef(Clobbers, NumClobbers);
1885 }
1886
1887 ArrayRef<Expr*> getAllExprs() const {
1888 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1889 NumInputs + NumOutputs);
1890 }
1891
1892 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1893
1894private:
1895 void initialize(const ASTContext &C, StringRef AsmString,
1896 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1897 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1898
1899public:
1900 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1901 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1902
1903 static bool classof(const Stmt *T) {
1904 return T->getStmtClass() == MSAsmStmtClass;
1905 }
1906
1907 child_range children() {
1908 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1909 }
1910};
1911
1912class SEHExceptStmt : public Stmt {
1913 friend class ASTReader;
1914 friend class ASTStmtReader;
1915
1916 SourceLocation Loc;
1917 Stmt *Children[2];
1918
1919 enum { FILTER_EXPR, BLOCK };
1920
1921 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
1922 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
1923
1924public:
1925 static SEHExceptStmt* Create(const ASTContext &C,
1926 SourceLocation ExceptLoc,
1927 Expr *FilterExpr,
1928 Stmt *Block);
1929
1930 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1931 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1932
1933 SourceLocation getExceptLoc() const { return Loc; }
1934 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1935
1936 Expr *getFilterExpr() const {
1937 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1938 }
1939
1940 CompoundStmt *getBlock() const {
1941 return cast<CompoundStmt>(Children[BLOCK]);
1942 }
1943
1944 child_range children() {
1945 return child_range(Children, Children+2);
1946 }
1947
1948 static bool classof(const Stmt *T) {
1949 return T->getStmtClass() == SEHExceptStmtClass;
1950 }
1951};
1952
1953class SEHFinallyStmt : public Stmt {
1954 friend class ASTReader;
1955 friend class ASTStmtReader;
1956
1957 SourceLocation Loc;
1958 Stmt *Block;
1959
1960 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
1961 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
1962
1963public:
1964 static SEHFinallyStmt* Create(const ASTContext &C,
1965 SourceLocation FinallyLoc,
1966 Stmt *Block);
1967
1968 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1969 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1970
1971 SourceLocation getFinallyLoc() const { return Loc; }
1972 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1973
1974 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1975
1976 child_range children() {
1977 return child_range(&Block,&Block+1);
1978 }
1979
1980 static bool classof(const Stmt *T) {
1981 return T->getStmtClass() == SEHFinallyStmtClass;
1982 }
1983};
1984
1985class SEHTryStmt : public Stmt {
1986 friend class ASTReader;
1987 friend class ASTStmtReader;
1988
1989 bool IsCXXTry;
1990 SourceLocation TryLoc;
1991 Stmt *Children[2];
1992
1993 enum { TRY = 0, HANDLER = 1 };
1994
1995 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1996 SourceLocation TryLoc,
1997 Stmt *TryBlock,
1998 Stmt *Handler);
1999
2000 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
2001
2002public:
2003 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
2004 SourceLocation TryLoc, Stmt *TryBlock,
2005 Stmt *Handler);
2006
2007 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
2008 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2009
2010 SourceLocation getTryLoc() const { return TryLoc; }
2011 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
2012
2013 bool getIsCXXTry() const { return IsCXXTry; }
2014
2015 CompoundStmt* getTryBlock() const {
2016 return cast<CompoundStmt>(Children[TRY]);
2017 }
2018
2019 Stmt *getHandler() const { return Children[HANDLER]; }
2020
2021 /// Returns 0 if not defined
2022 SEHExceptStmt *getExceptHandler() const;
2023 SEHFinallyStmt *getFinallyHandler() const;
2024
2025 child_range children() {
2026 return child_range(Children, Children+2);
2027 }
2028
2029 static bool classof(const Stmt *T) {
2030 return T->getStmtClass() == SEHTryStmtClass;
2031 }
2032};
2033
2034/// Represents a __leave statement.
2035class SEHLeaveStmt : public Stmt {
2036 SourceLocation LeaveLoc;
2037
2038public:
2039 explicit SEHLeaveStmt(SourceLocation LL)
2040 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2041
2042 /// Build an empty __leave statement.
2043 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
2044
2045 SourceLocation getLeaveLoc() const { return LeaveLoc; }
2046 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2047
2048 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
2049 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
2050
2051 static bool classof(const Stmt *T) {
2052 return T->getStmtClass() == SEHLeaveStmtClass;
2053 }
2054
2055 // Iterators
2056 child_range children() {
2057 return child_range(child_iterator(), child_iterator());
2058 }
2059};
2060
2061/// This captures a statement into a function. For example, the following
2062/// pragma annotated compound statement can be represented as a CapturedStmt,
2063/// and this compound statement is the body of an anonymous outlined function.
2064/// @code
2065/// #pragma omp parallel
2066/// {
2067/// compute();
2068/// }
2069/// @endcode
2070class CapturedStmt : public Stmt {
2071public:
2072 /// The different capture forms: by 'this', by reference, capture for
2073 /// variable-length array type etc.
2074 enum VariableCaptureKind {
2075 VCK_This,
2076 VCK_ByRef,
2077 VCK_ByCopy,
2078 VCK_VLAType,
2079 };
2080
2081 /// Describes the capture of either a variable, or 'this', or
2082 /// variable-length array type.
2083 class Capture {
2084 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2085 SourceLocation Loc;
2086
2087 public:
2088 friend class ASTStmtReader;
2089
2090 /// Create a new capture.
2091 ///
2092 /// \param Loc The source location associated with this capture.
2093 ///
2094 /// \param Kind The kind of capture (this, ByRef, ...).
2095 ///
2096 /// \param Var The variable being captured, or null if capturing this.
2097 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2098 VarDecl *Var = nullptr);
2099
2100 /// Determine the kind of capture.
2101 VariableCaptureKind getCaptureKind() const;
2102
2103 /// Retrieve the source location at which the variable or 'this' was
2104 /// first used.
2105 SourceLocation getLocation() const { return Loc; }
2106
2107 /// Determine whether this capture handles the C++ 'this' pointer.
2108 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2109
2110 /// Determine whether this capture handles a variable (by reference).
2111 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2112
2113 /// Determine whether this capture handles a variable by copy.
2114 bool capturesVariableByCopy() const {
2115 return getCaptureKind() == VCK_ByCopy;
2116 }
2117
2118 /// Determine whether this capture handles a variable-length array
2119 /// type.
2120 bool capturesVariableArrayType() const {
2121 return getCaptureKind() == VCK_VLAType;
2122 }
2123
2124 /// Retrieve the declaration of the variable being captured.
2125 ///
2126 /// This operation is only valid if this capture captures a variable.
2127 VarDecl *getCapturedVar() const;
2128 };
2129
2130private:
2131 /// The number of variable captured, including 'this'.
2132 unsigned NumCaptures;
2133
2134 /// The pointer part is the implicit the outlined function and the
2135 /// int part is the captured region kind, 'CR_Default' etc.
2136 llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
2137
2138 /// The record for captured variables, a RecordDecl or CXXRecordDecl.
2139 RecordDecl *TheRecordDecl = nullptr;
2140
2141 /// Construct a captured statement.
2142 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2143 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2144
2145 /// Construct an empty captured statement.
2146 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2147
2148 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2149
2150 Stmt *const *getStoredStmts() const {
2151 return reinterpret_cast<Stmt *const *>(this + 1);
2152 }
2153
2154 Capture *getStoredCaptures() const;
2155
2156 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2157
2158public:
2159 friend class ASTStmtReader;
2160
2161 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2162 CapturedRegionKind Kind,
2163 ArrayRef<Capture> Captures,
2164 ArrayRef<Expr *> CaptureInits,
2165 CapturedDecl *CD, RecordDecl *RD);
2166
2167 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2168 unsigned NumCaptures);
2169
2170 /// Retrieve the statement being captured.
2171 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2172 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2173
2174 /// Retrieve the outlined function declaration.
2175 CapturedDecl *getCapturedDecl();
2176 const CapturedDecl *getCapturedDecl() const;
2177
2178 /// Set the outlined function declaration.
2179 void setCapturedDecl(CapturedDecl *D);
2180
2181 /// Retrieve the captured region kind.
2182 CapturedRegionKind getCapturedRegionKind() const;
2183
2184 /// Set the captured region kind.
2185 void setCapturedRegionKind(CapturedRegionKind Kind);
2186
2187 /// Retrieve the record declaration for captured variables.
2188 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2189
2190 /// Set the record declaration for captured variables.
2191 void setCapturedRecordDecl(RecordDecl *D) {
2192 assert(D && "null RecordDecl");
2193 TheRecordDecl = D;
2194 }
2195
2196 /// True if this variable has been captured.
2197 bool capturesVariable(const VarDecl *Var) const;
2198
2199 /// An iterator that walks over the captures.
2200 using capture_iterator = Capture *;
2201 using const_capture_iterator = const Capture *;
2202 using capture_range = llvm::iterator_range<capture_iterator>;
2203 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
2204
2205 capture_range captures() {
2206 return capture_range(capture_begin(), capture_end());
2207 }
2208 capture_const_range captures() const {
2209 return capture_const_range(capture_begin(), capture_end());
2210 }
2211
2212 /// Retrieve an iterator pointing to the first capture.
2213 capture_iterator capture_begin() { return getStoredCaptures(); }
2214 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2215
2216 /// Retrieve an iterator pointing past the end of the sequence of
2217 /// captures.
2218 capture_iterator capture_end() const {
2219 return getStoredCaptures() + NumCaptures;
2220 }
2221
2222 /// Retrieve the number of captures, including 'this'.
2223 unsigned capture_size() const { return NumCaptures; }
2224
2225 /// Iterator that walks over the capture initialization arguments.
2226 using capture_init_iterator = Expr **;
2227 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
2228
2229 /// Const iterator that walks over the capture initialization
2230 /// arguments.
2231 using const_capture_init_iterator = Expr *const *;
2232 using const_capture_init_range =
2233 llvm::iterator_range<const_capture_init_iterator>;
2234
2235 capture_init_range capture_inits() {
2236 return capture_init_range(capture_init_begin(), capture_init_end());
2237 }
2238
2239 const_capture_init_range capture_inits() const {
2240 return const_capture_init_range(capture_init_begin(), capture_init_end());
2241 }
2242
2243 /// Retrieve the first initialization argument.
2244 capture_init_iterator capture_init_begin() {
2245 return reinterpret_cast<Expr **>(getStoredStmts());
2246 }
2247
2248 const_capture_init_iterator capture_init_begin() const {
2249 return reinterpret_cast<Expr *const *>(getStoredStmts());
2250 }
2251
2252 /// Retrieve the iterator pointing one past the last initialization
2253 /// argument.
2254 capture_init_iterator capture_init_end() {
2255 return capture_init_begin() + NumCaptures;
2256 }
2257
2258 const_capture_init_iterator capture_init_end() const {
2259 return capture_init_begin() + NumCaptures;
2260 }
2261
2262 SourceLocation getLocStart() const LLVM_READONLY {
2263 return getCapturedStmt()->getLocStart();
2264 }
2265
2266 SourceLocation getLocEnd() const LLVM_READONLY {
2267 return getCapturedStmt()->getLocEnd();
2268 }
2269
2270 SourceRange getSourceRange() const LLVM_READONLY {
2271 return getCapturedStmt()->getSourceRange();
2272 }
2273
2274 static bool classof(const Stmt *T) {
2275 return T->getStmtClass() == CapturedStmtClass;
2276 }
2277
2278 child_range children();
2279};
2280
2281} // namespace clang
2282
2283#endif // LLVM_CLANG_AST_STMT_H
2284