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 //===--- Statement bitfields classes ---===//
93
94 class StmtBitfields {
95 friend class Stmt;
96
97 /// The statement class.
98 unsigned sClass : 8;
99 };
100 enum { NumStmtBits = 8 };
101
102 class NullStmtBitfields {
103 friend class ASTStmtReader;
104 friend class ASTStmtWriter;
105 friend class NullStmt;
106
107 unsigned : NumStmtBits;
108
109 /// True if the null statement was preceded by an empty macro, e.g:
110 /// @code
111 /// #define CALL(x)
112 /// CALL(0);
113 /// @endcode
114 unsigned HasLeadingEmptyMacro : 1;
115
116 /// The location of the semi-colon.
117 SourceLocation SemiLoc;
118 };
119
120 class CompoundStmtBitfields {
121 friend class ASTStmtReader;
122 friend class CompoundStmt;
123
124 unsigned : NumStmtBits;
125
126 unsigned NumStmts : 32 - NumStmtBits;
127
128 /// The location of the opening "{".
129 SourceLocation LBraceLoc;
130 };
131
132 class LabelStmtBitfields {
133 friend class LabelStmt;
134
135 unsigned : NumStmtBits;
136
137 SourceLocation IdentLoc;
138 };
139
140 class AttributedStmtBitfields {
141 friend class ASTStmtReader;
142 friend class AttributedStmt;
143
144 unsigned : NumStmtBits;
145
146 /// Number of attributes.
147 unsigned NumAttrs : 32 - NumStmtBits;
148
149 /// The location of the attribute.
150 SourceLocation AttrLoc;
151 };
152
153 class IfStmtBitfields {
154 friend class ASTStmtReader;
155 friend class IfStmt;
156
157 unsigned : NumStmtBits;
158
159 /// True if this if statement is a constexpr if.
160 unsigned IsConstexpr : 1;
161
162 /// True if this if statement has storage for an else statement.
163 unsigned HasElse : 1;
164
165 /// True if this if statement has storage for a variable declaration.
166 unsigned HasVar : 1;
167
168 /// True if this if statement has storage for an init statement.
169 unsigned HasInit : 1;
170
171 /// The location of the "if".
172 SourceLocation IfLoc;
173 };
174
175 class SwitchStmtBitfields {
176 friend class SwitchStmt;
177
178 unsigned : NumStmtBits;
179
180 /// True if the SwitchStmt has storage for an init statement.
181 unsigned HasInit : 1;
182
183 /// True if the SwitchStmt has storage for a condition variable.
184 unsigned HasVar : 1;
185
186 /// If the SwitchStmt is a switch on an enum value, records whether all
187 /// the enum values were covered by CaseStmts. The coverage information
188 /// value is meant to be a hint for possible clients.
189 unsigned AllEnumCasesCovered : 1;
190
191 /// The location of the "switch".
192 SourceLocation SwitchLoc;
193 };
194
195 class WhileStmtBitfields {
196 friend class ASTStmtReader;
197 friend class WhileStmt;
198
199 unsigned : NumStmtBits;
200
201 /// True if the WhileStmt has storage for a condition variable.
202 unsigned HasVar : 1;
203
204 /// The location of the "while".
205 SourceLocation WhileLoc;
206 };
207
208 class DoStmtBitfields {
209 friend class DoStmt;
210
211 unsigned : NumStmtBits;
212
213 /// The location of the "do".
214 SourceLocation DoLoc;
215 };
216
217 class ForStmtBitfields {
218 friend class ForStmt;
219
220 unsigned : NumStmtBits;
221
222 /// The location of the "for".
223 SourceLocation ForLoc;
224 };
225
226 class GotoStmtBitfields {
227 friend class GotoStmt;
228 friend class IndirectGotoStmt;
229
230 unsigned : NumStmtBits;
231
232 /// The location of the "goto".
233 SourceLocation GotoLoc;
234 };
235
236 class ContinueStmtBitfields {
237 friend class ContinueStmt;
238
239 unsigned : NumStmtBits;
240
241 /// The location of the "continue".
242 SourceLocation ContinueLoc;
243 };
244
245 class BreakStmtBitfields {
246 friend class BreakStmt;
247
248 unsigned : NumStmtBits;
249
250 /// The location of the "break".
251 SourceLocation BreakLoc;
252 };
253
254 class ReturnStmtBitfields {
255 friend class ReturnStmt;
256
257 unsigned : NumStmtBits;
258
259 /// True if this ReturnStmt has storage for an NRVO candidate.
260 unsigned HasNRVOCandidate : 1;
261
262 /// The location of the "return".
263 SourceLocation RetLoc;
264 };
265
266 class SwitchCaseBitfields {
267 friend class SwitchCase;
268 friend class CaseStmt;
269
270 unsigned : NumStmtBits;
271
272 /// Used by CaseStmt to store whether it is a case statement
273 /// of the form case LHS ... RHS (a GNU extension).
274 unsigned CaseStmtIsGNURange : 1;
275
276 /// The location of the "case" or "default" keyword.
277 SourceLocation KeywordLoc;
278 };
279
280 //===--- Expression bitfields classes ---===//
281
282 class ExprBitfields {
283 friend class ASTStmtReader; // deserialization
284 friend class AtomicExpr; // ctor
285 friend class BlockDeclRefExpr; // ctor
286 friend class CallExpr; // ctor
287 friend class CXXConstructExpr; // ctor
288 friend class CXXDependentScopeMemberExpr; // ctor
289 friend class CXXNewExpr; // ctor
290 friend class CXXUnresolvedConstructExpr; // ctor
291 friend class DeclRefExpr; // computeDependence
292 friend class DependentScopeDeclRefExpr; // ctor
293 friend class DesignatedInitExpr; // ctor
294 friend class Expr;
295 friend class InitListExpr; // ctor
296 friend class ObjCArrayLiteral; // ctor
297 friend class ObjCDictionaryLiteral; // ctor
298 friend class ObjCMessageExpr; // ctor
299 friend class OffsetOfExpr; // ctor
300 friend class OpaqueValueExpr; // ctor
301 friend class OverloadExpr; // ctor
302 friend class ParenListExpr; // ctor
303 friend class PseudoObjectExpr; // ctor
304 friend class ShuffleVectorExpr; // ctor
305
306 unsigned : NumStmtBits;
307
308 unsigned ValueKind : 2;
309 unsigned ObjectKind : 3;
310 unsigned TypeDependent : 1;
311 unsigned ValueDependent : 1;
312 unsigned InstantiationDependent : 1;
313 unsigned ContainsUnexpandedParameterPack : 1;
314 };
315 enum { NumExprBits = 17 };
316
317 class PredefinedExprBitfields {
318 friend class ASTStmtReader;
319 friend class PredefinedExpr;
320
321 unsigned : NumExprBits;
322
323 /// The kind of this PredefinedExpr. One of the enumeration values
324 /// in PredefinedExpr::IdentKind.
325 unsigned Kind : 4;
326
327 /// True if this PredefinedExpr has a trailing "StringLiteral *"
328 /// for the predefined identifier.
329 unsigned HasFunctionName : 1;
330
331 /// The location of this PredefinedExpr.
332 SourceLocation Loc;
333 };
334
335 class CharacterLiteralBitfields {
336 friend class CharacterLiteral;
337
338 unsigned : NumExprBits;
339
340 unsigned Kind : 3;
341 };
342
343 enum APFloatSemantics {
344 IEEEhalf,
345 IEEEsingle,
346 IEEEdouble,
347 x87DoubleExtended,
348 IEEEquad,
349 PPCDoubleDouble
350 };
351
352 class FloatingLiteralBitfields {
353 friend class FloatingLiteral;
354
355 unsigned : NumExprBits;
356
357 unsigned Semantics : 3; // Provides semantics for APFloat construction
358 unsigned IsExact : 1;
359 };
360
361 class UnaryExprOrTypeTraitExprBitfields {
362 friend class UnaryExprOrTypeTraitExpr;
363
364 unsigned : NumExprBits;
365
366 unsigned Kind : 3;
367 unsigned IsType : 1; // true if operand is a type, false if an expression.
368 };
369
370 class DeclRefExprBitfields {
371 friend class ASTStmtReader; // deserialization
372 friend class DeclRefExpr;
373
374 unsigned : NumExprBits;
375
376 unsigned HasQualifier : 1;
377 unsigned HasTemplateKWAndArgsInfo : 1;
378 unsigned HasFoundDecl : 1;
379 unsigned HadMultipleCandidates : 1;
380 unsigned RefersToEnclosingVariableOrCapture : 1;
381
382 /// The location of the declaration name itself.
383 SourceLocation Loc;
384 };
385
386 class CastExprBitfields {
387 friend class CastExpr;
388 friend class ImplicitCastExpr;
389
390 unsigned : NumExprBits;
391
392 unsigned Kind : 6;
393 unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr.
394 unsigned BasePathIsEmpty : 1;
395 };
396
397 class CallExprBitfields {
398 friend class CallExpr;
399
400 unsigned : NumExprBits;
401
402 unsigned NumPreArgs : 1;
403 };
404
405 class ExprWithCleanupsBitfields {
406 friend class ASTStmtReader; // deserialization
407 friend class ExprWithCleanups;
408
409 unsigned : NumExprBits;
410
411 // When false, it must not have side effects.
412 unsigned CleanupsHaveSideEffects : 1;
413
414 unsigned NumObjects : 32 - 1 - NumExprBits;
415 };
416
417 class PseudoObjectExprBitfields {
418 friend class ASTStmtReader; // deserialization
419 friend class PseudoObjectExpr;
420
421 unsigned : NumExprBits;
422
423 // These don't need to be particularly wide, because they're
424 // strictly limited by the forms of expressions we permit.
425 unsigned NumSubExprs : 8;
426 unsigned ResultIndex : 32 - 8 - NumExprBits;
427 };
428
429 class OpaqueValueExprBitfields {
430 friend class OpaqueValueExpr;
431
432 unsigned : NumExprBits;
433
434 /// The OVE is a unique semantic reference to its source expressio if this
435 /// bit is set to true.
436 unsigned IsUnique : 1;
437 };
438
439 class ObjCIndirectCopyRestoreExprBitfields {
440 friend class ObjCIndirectCopyRestoreExpr;
441
442 unsigned : NumExprBits;
443
444 unsigned ShouldCopy : 1;
445 };
446
447 class InitListExprBitfields {
448 friend class InitListExpr;
449
450 unsigned : NumExprBits;
451
452 /// Whether this initializer list originally had a GNU array-range
453 /// designator in it. This is a temporary marker used by CodeGen.
454 unsigned HadArrayRangeDesignator : 1;
455 };
456
457 class TypeTraitExprBitfields {
458 friend class ASTStmtReader;
459 friend class ASTStmtWriter;
460 friend class TypeTraitExpr;
461
462 unsigned : NumExprBits;
463
464 /// The kind of type trait, which is a value of a TypeTrait enumerator.
465 unsigned Kind : 8;
466
467 /// If this expression is not value-dependent, this indicates whether
468 /// the trait evaluated true or false.
469 unsigned Value : 1;
470
471 /// The number of arguments to this type trait.
472 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
473 };
474
475 class CoawaitExprBitfields {
476 friend class CoawaitExpr;
477
478 unsigned : NumExprBits;
479
480 unsigned IsImplicit : 1;
481 };
482
483 union {
484 // Statements
485 StmtBitfields StmtBits;
486 NullStmtBitfields NullStmtBits;
487 CompoundStmtBitfields CompoundStmtBits;
488 LabelStmtBitfields LabelStmtBits;
489 AttributedStmtBitfields AttributedStmtBits;
490 IfStmtBitfields IfStmtBits;
491 SwitchStmtBitfields SwitchStmtBits;
492 WhileStmtBitfields WhileStmtBits;
493 DoStmtBitfields DoStmtBits;
494 ForStmtBitfields ForStmtBits;
495 GotoStmtBitfields GotoStmtBits;
496 ContinueStmtBitfields ContinueStmtBits;
497 BreakStmtBitfields BreakStmtBits;
498 ReturnStmtBitfields ReturnStmtBits;
499 SwitchCaseBitfields SwitchCaseBits;
500
501 // Expressions
502 ExprBitfields ExprBits;
503 PredefinedExprBitfields PredefinedExprBits;
504 CharacterLiteralBitfields CharacterLiteralBits;
505 FloatingLiteralBitfields FloatingLiteralBits;
506 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
507 DeclRefExprBitfields DeclRefExprBits;
508 CastExprBitfields CastExprBits;
509 CallExprBitfields CallExprBits;
510 ExprWithCleanupsBitfields ExprWithCleanupsBits;
511 PseudoObjectExprBitfields PseudoObjectExprBits;
512 OpaqueValueExprBitfields OpaqueValueExprBits;
513 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
514 InitListExprBitfields InitListExprBits;
515 TypeTraitExprBitfields TypeTraitExprBits;
516 CoawaitExprBitfields CoawaitBits;
517 };
518
519public:
520 // Only allow allocation of Stmts using the allocator in ASTContext
521 // or by doing a placement new.
522 void* operator new(size_t bytes, const ASTContext& C,
523 unsigned alignment = 8);
524
525 void* operator new(size_t bytes, const ASTContext* C,
526 unsigned alignment = 8) {
527 return operator new(bytes, *C, alignment);
528 }
529
530 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
531
532 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
533 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
534 void operator delete(void *, size_t) noexcept {}
535 void operator delete(void *, void *) noexcept {}
536
537public:
538 /// A placeholder type used to construct an empty shell of a
539 /// type, that will be filled in later (e.g., by some
540 /// de-serialization).
541 struct EmptyShell {};
542
543protected:
544 /// Iterator for iterating over Stmt * arrays that contain only Expr *
545 ///
546 /// This is needed because AST nodes use Stmt* arrays to store
547 /// references to children (to be compatible with StmtIterator).
548 struct ExprIterator
549 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
550 std::random_access_iterator_tag, Expr *> {
551 ExprIterator() : iterator_adaptor_base(nullptr) {}
552 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
553
554 reference operator*() const {
555 assert((*I)->getStmtClass() >= firstExprConstant &&
556 (*I)->getStmtClass() <= lastExprConstant);
557 return *reinterpret_cast<Expr **>(I);
558 }
559 };
560
561 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
562 struct ConstExprIterator
563 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
564 std::random_access_iterator_tag,
565 const Expr *const> {
566 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
567 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
568
569 reference operator*() const {
570 assert((*I)->getStmtClass() >= firstExprConstant &&
571 (*I)->getStmtClass() <= lastExprConstant);
572 return *reinterpret_cast<const Expr *const *>(I);
573 }
574 };
575
576private:
577 /// Whether statistic collection is enabled.
578 static bool StatisticsEnabled;
579
580protected:
581 /// Construct an empty statement.
582 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
583
584public:
585 Stmt(StmtClass SC) {
586 static_assert(sizeof(*this) <= 8,
587 "changing bitfields changed sizeof(Stmt)");
588 static_assert(sizeof(*this) % alignof(void *) == 0,
589 "Insufficient alignment!");
590 StmtBits.sClass = SC;
591 if (StatisticsEnabled) Stmt::addStmtClass(SC);
592 }
593
594 StmtClass getStmtClass() const {
595 return static_cast<StmtClass>(StmtBits.sClass);
596 }
597
598 const char *getStmtClassName() const;
599
600 /// SourceLocation tokens are not useful in isolation - they are low level
601 /// value objects created/interpreted by SourceManager. We assume AST
602 /// clients will have a pointer to the respective SourceManager.
603 SourceRange getSourceRange() const LLVM_READONLY;
604 SourceLocation getBeginLoc() const LLVM_READONLY;
605 SourceLocation getEndLoc() const LLVM_READONLY;
606
607 // global temp stats (until we have a per-module visitor)
608 static void addStmtClass(const StmtClass s);
609 static void EnableStatistics();
610 static void PrintStats();
611
612 /// Dumps the specified AST fragment and all subtrees to
613 /// \c llvm::errs().
614 void dump() const;
615 void dump(SourceManager &SM) const;
616 void dump(raw_ostream &OS, SourceManager &SM) const;
617 void dump(raw_ostream &OS) const;
618
619 /// \return Unique reproducible object identifier
620 int64_t getID(const ASTContext &Context) const;
621
622 /// dumpColor - same as dump(), but forces color highlighting.
623 void dumpColor() const;
624
625 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
626 /// back to its original source language syntax.
627 void dumpPretty(const ASTContext &Context) const;
628 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
629 const PrintingPolicy &Policy, unsigned Indentation = 0,
630 StringRef NewlineSymbol = "\n",
631 const ASTContext *Context = nullptr) const;
632
633 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
634 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
635 void viewAST() const;
636
637 /// Skip past any implicit AST nodes which might surround this
638 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
639 Stmt *IgnoreImplicit();
640 const Stmt *IgnoreImplicit() const {
641 return const_cast<Stmt *>(this)->IgnoreImplicit();
642 }
643
644 /// Skip no-op (attributed, compound) container stmts and skip captured
645 /// stmt at the top, if \a IgnoreCaptured is true.
646 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
647 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
648 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
649 }
650
651 const Stmt *stripLabelLikeStatements() const;
652 Stmt *stripLabelLikeStatements() {
653 return const_cast<Stmt*>(
654 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
655 }
656
657 /// Child Iterators: All subclasses must implement 'children'
658 /// to permit easy iteration over the substatements/subexpessions of an
659 /// AST node. This permits easy iteration over all nodes in the AST.
660 using child_iterator = StmtIterator;
661 using const_child_iterator = ConstStmtIterator;
662
663 using child_range = llvm::iterator_range<child_iterator>;
664 using const_child_range = llvm::iterator_range<const_child_iterator>;
665
666 child_range children();
667
668 const_child_range children() const {
669 auto Children = const_cast<Stmt *>(this)->children();
670 return const_child_range(Children.begin(), Children.end());
671 }
672
673 child_iterator child_begin() { return children().begin(); }
674 child_iterator child_end() { return children().end(); }
675
676 const_child_iterator child_begin() const { return children().begin(); }
677 const_child_iterator child_end() const { return children().end(); }
678
679 /// Produce a unique representation of the given statement.
680 ///
681 /// \param ID once the profiling operation is complete, will contain
682 /// the unique representation of the given statement.
683 ///
684 /// \param Context the AST context in which the statement resides
685 ///
686 /// \param Canonical whether the profile should be based on the canonical
687 /// representation of this statement (e.g., where non-type template
688 /// parameters are identified by index/level rather than their
689 /// declaration pointers) or the exact representation of the statement as
690 /// written in the source.
691 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
692 bool Canonical) const;
693
694 /// Calculate a unique representation for a statement that is
695 /// stable across compiler invocations.
696 ///
697 /// \param ID profile information will be stored in ID.
698 ///
699 /// \param Hash an ODRHash object which will be called where pointers would
700 /// have been used in the Profile function.
701 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
702};
703
704/// DeclStmt - Adaptor class for mixing declarations with statements and
705/// expressions. For example, CompoundStmt mixes statements, expressions
706/// and declarations (variables, types). Another example is ForStmt, where
707/// the first statement can be an expression or a declaration.
708class DeclStmt : public Stmt {
709 DeclGroupRef DG;
710 SourceLocation StartLoc, EndLoc;
711
712public:
713 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
714 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
715
716 /// Build an empty declaration statement.
717 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
718
719 /// isSingleDecl - This method returns true if this DeclStmt refers
720 /// to a single Decl.
721 bool isSingleDecl() const { return DG.isSingleDecl(); }
722
723 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
724 Decl *getSingleDecl() { return DG.getSingleDecl(); }
725
726 const DeclGroupRef getDeclGroup() const { return DG; }
727 DeclGroupRef getDeclGroup() { return DG; }
728 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
729
730 void setStartLoc(SourceLocation L) { StartLoc = L; }
731 SourceLocation getEndLoc() const { return EndLoc; }
732 void setEndLoc(SourceLocation L) { EndLoc = L; }
733
734 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLoc; }
735
736 static bool classof(const Stmt *T) {
737 return T->getStmtClass() == DeclStmtClass;
738 }
739
740 // Iterators over subexpressions.
741 child_range children() {
742 return child_range(child_iterator(DG.begin(), DG.end()),
743 child_iterator(DG.end(), DG.end()));
744 }
745
746 using decl_iterator = DeclGroupRef::iterator;
747 using const_decl_iterator = DeclGroupRef::const_iterator;
748 using decl_range = llvm::iterator_range<decl_iterator>;
749 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
750
751 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
752
753 decl_const_range decls() const {
754 return decl_const_range(decl_begin(), decl_end());
755 }
756
757 decl_iterator decl_begin() { return DG.begin(); }
758 decl_iterator decl_end() { return DG.end(); }
759 const_decl_iterator decl_begin() const { return DG.begin(); }
760 const_decl_iterator decl_end() const { return DG.end(); }
761
762 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
763
764 reverse_decl_iterator decl_rbegin() {
765 return reverse_decl_iterator(decl_end());
766 }
767
768 reverse_decl_iterator decl_rend() {
769 return reverse_decl_iterator(decl_begin());
770 }
771};
772
773/// NullStmt - This is the null statement ";": C99 6.8.3p3.
774///
775class NullStmt : public Stmt {
776public:
777 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
778 : Stmt(NullStmtClass) {
779 NullStmtBits.HasLeadingEmptyMacro = hasLeadingEmptyMacro;
780 setSemiLoc(L);
781 }
782
783 /// Build an empty null statement.
784 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
785
786 SourceLocation getSemiLoc() const { return NullStmtBits.SemiLoc; }
787 void setSemiLoc(SourceLocation L) { NullStmtBits.SemiLoc = L; }
788
789 bool hasLeadingEmptyMacro() const {
790 return NullStmtBits.HasLeadingEmptyMacro;
791 }
792
793 SourceLocation getBeginLoc() const { return getSemiLoc(); }
794 SourceLocation getEndLoc() const { return getSemiLoc(); }
795
796 static bool classof(const Stmt *T) {
797 return T->getStmtClass() == NullStmtClass;
798 }
799
800 child_range children() {
801 return child_range(child_iterator(), child_iterator());
802 }
803};
804
805/// CompoundStmt - This represents a group of statements like { stmt stmt }.
806class CompoundStmt final : public Stmt,
807 private llvm::TrailingObjects<CompoundStmt, Stmt *> {
808 friend class ASTStmtReader;
809 friend TrailingObjects;
810
811 /// The location of the closing "}". LBraceLoc is stored in CompoundStmtBits.
812 SourceLocation RBraceLoc;
813
814 CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
815 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
816
817 void setStmts(ArrayRef<Stmt *> Stmts);
818
819public:
820 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
821 SourceLocation LB, SourceLocation RB);
822
823 // Build an empty compound statement with a location.
824 explicit CompoundStmt(SourceLocation Loc)
825 : Stmt(CompoundStmtClass), RBraceLoc(Loc) {
826 CompoundStmtBits.NumStmts = 0;
827 CompoundStmtBits.LBraceLoc = Loc;
828 }
829
830 // Build an empty compound statement.
831 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
832
833 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
834 unsigned size() const { return CompoundStmtBits.NumStmts; }
835
836 using body_iterator = Stmt **;
837 using body_range = llvm::iterator_range<body_iterator>;
838
839 body_range body() { return body_range(body_begin(), body_end()); }
840 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
841 body_iterator body_end() { return body_begin() + size(); }
842 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
843
844 Stmt *body_back() {
845 return !body_empty() ? body_begin()[size() - 1] : nullptr;
846 }
847
848 void setLastStmt(Stmt *S) {
849 assert(!body_empty() && "setLastStmt");
850 body_begin()[size() - 1] = S;
851 }
852
853 using const_body_iterator = Stmt *const *;
854 using body_const_range = llvm::iterator_range<const_body_iterator>;
855
856 body_const_range body() const {
857 return body_const_range(body_begin(), body_end());
858 }
859
860 const_body_iterator body_begin() const {
861 return getTrailingObjects<Stmt *>();
862 }
863
864 const_body_iterator body_end() const { return body_begin() + size(); }
865
866 const Stmt *body_front() const {
867 return !body_empty() ? body_begin()[0] : nullptr;
868 }
869
870 const Stmt *body_back() const {
871 return !body_empty() ? body_begin()[size() - 1] : nullptr;
872 }
873
874 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
875
876 reverse_body_iterator body_rbegin() {
877 return reverse_body_iterator(body_end());
878 }
879
880 reverse_body_iterator body_rend() {
881 return reverse_body_iterator(body_begin());
882 }
883
884 using const_reverse_body_iterator =
885 std::reverse_iterator<const_body_iterator>;
886
887 const_reverse_body_iterator body_rbegin() const {
888 return const_reverse_body_iterator(body_end());
889 }
890
891 const_reverse_body_iterator body_rend() const {
892 return const_reverse_body_iterator(body_begin());
893 }
894
895 SourceLocation getBeginLoc() const { return CompoundStmtBits.LBraceLoc; }
896 SourceLocation getEndLoc() const { return RBraceLoc; }
897
898 SourceLocation getLBracLoc() const { return CompoundStmtBits.LBraceLoc; }
899 SourceLocation getRBracLoc() const { return RBraceLoc; }
900
901 static bool classof(const Stmt *T) {
902 return T->getStmtClass() == CompoundStmtClass;
903 }
904
905 // Iterators
906 child_range children() { return child_range(body_begin(), body_end()); }
907
908 const_child_range children() const {
909 return const_child_range(body_begin(), body_end());
910 }
911};
912
913// SwitchCase is the base class for CaseStmt and DefaultStmt,
914class SwitchCase : public Stmt {
915protected:
916 /// The location of the ":".
917 SourceLocation ColonLoc;
918
919 // The location of the "case" or "default" keyword. Stored in SwitchCaseBits.
920 // SourceLocation KeywordLoc;
921
922 /// A pointer to the following CaseStmt or DefaultStmt class,
923 /// used by SwitchStmt.
924 SwitchCase *NextSwitchCase = nullptr;
925
926 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
927 : Stmt(SC), ColonLoc(ColonLoc) {
928 setKeywordLoc(KWLoc);
929 }
930
931 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
932
933public:
934 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
935 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
936 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
937
938 SourceLocation getKeywordLoc() const { return SwitchCaseBits.KeywordLoc; }
939 void setKeywordLoc(SourceLocation L) { SwitchCaseBits.KeywordLoc = L; }
940 SourceLocation getColonLoc() const { return ColonLoc; }
941 void setColonLoc(SourceLocation L) { ColonLoc = L; }
942
943 inline Stmt *getSubStmt();
944 const Stmt *getSubStmt() const {
945 return const_cast<SwitchCase *>(this)->getSubStmt();
946 }
947
948 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
949 inline SourceLocation getEndLoc() const LLVM_READONLY;
950
951 static bool classof(const Stmt *T) {
952 return T->getStmtClass() == CaseStmtClass ||
953 T->getStmtClass() == DefaultStmtClass;
954 }
955};
956
957/// CaseStmt - Represent a case statement. It can optionally be a GNU case
958/// statement of the form LHS ... RHS representing a range of cases.
959class CaseStmt final
960 : public SwitchCase,
961 private llvm::TrailingObjects<CaseStmt, Stmt *, SourceLocation> {
962 friend TrailingObjects;
963
964 // CaseStmt is followed by several trailing objects, some of which optional.
965 // Note that it would be more convenient to put the optional trailing objects
966 // at the end but this would impact children().
967 // The trailing objects are in order:
968 //
969 // * A "Stmt *" for the LHS of the case statement. Always present.
970 //
971 // * A "Stmt *" for the RHS of the case statement. This is a GNU extension
972 // which allow ranges in cases statement of the form LHS ... RHS.
973 // Present if and only if caseStmtIsGNURange() is true.
974 //
975 // * A "Stmt *" for the substatement of the case statement. Always present.
976 //
977 // * A SourceLocation for the location of the ... if this is a case statement
978 // with a range. Present if and only if caseStmtIsGNURange() is true.
979 enum { LhsOffset = 0, SubStmtOffsetFromRhs = 1 };
980 enum { NumMandatoryStmtPtr = 2 };
981
982 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
983 return NumMandatoryStmtPtr + caseStmtIsGNURange();
984 }
985
986 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
987 return caseStmtIsGNURange();
988 }
989
990 unsigned lhsOffset() const { return LhsOffset; }
991 unsigned rhsOffset() const { return LhsOffset + caseStmtIsGNURange(); }
992 unsigned subStmtOffset() const { return rhsOffset() + SubStmtOffsetFromRhs; }
993
994 /// Build a case statement assuming that the storage for the
995 /// trailing objects has been properly allocated.
996 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
997 SourceLocation ellipsisLoc, SourceLocation colonLoc)
998 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
999 // Handle GNU case statements of the form LHS ... RHS.
1000 bool IsGNURange = rhs != nullptr;
1001 SwitchCaseBits.CaseStmtIsGNURange = IsGNURange;
1002 setLHS(lhs);
1003 setSubStmt(nullptr);
1004 if (IsGNURange) {
1005 setRHS(rhs);
1006 setEllipsisLoc(ellipsisLoc);
1007 }
1008 }
1009
1010 /// Build an empty switch case statement.
1011 explicit CaseStmt(EmptyShell Empty, bool CaseStmtIsGNURange)
1012 : SwitchCase(CaseStmtClass, Empty) {
1013 SwitchCaseBits.CaseStmtIsGNURange = CaseStmtIsGNURange;
1014 }
1015
1016public:
1017 /// Build a case statement.
1018 static CaseStmt *Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1019 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1020 SourceLocation colonLoc);
1021
1022 /// Build an empty case statement.
1023 static CaseStmt *CreateEmpty(const ASTContext &Ctx, bool CaseStmtIsGNURange);
1024
1025 /// True if this case statement is of the form case LHS ... RHS, which
1026 /// is a GNU extension. In this case the RHS can be obtained with getRHS()
1027 /// and the location of the ellipsis can be obtained with getEllipsisLoc().
1028 bool caseStmtIsGNURange() const { return SwitchCaseBits.CaseStmtIsGNURange; }
1029
1030 SourceLocation getCaseLoc() const { return getKeywordLoc(); }
1031 void setCaseLoc(SourceLocation L) { setKeywordLoc(L); }
1032
1033 /// Get the location of the ... in a case statement of the form LHS ... RHS.
1034 SourceLocation getEllipsisLoc() const {
1035 return caseStmtIsGNURange() ? *getTrailingObjects<SourceLocation>()
1036 : SourceLocation();
1037 }
1038
1039 /// Set the location of the ... in a case statement of the form LHS ... RHS.
1040 /// Assert that this case statement is of this form.
1041 void setEllipsisLoc(SourceLocation L) {
1042 assert(
1043 caseStmtIsGNURange() &&
1044 "setEllipsisLoc but this is not a case stmt of the form LHS ... RHS!");
1045 *getTrailingObjects<SourceLocation>() = L;
1046 }
1047
1048 Expr *getLHS() {
1049 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1050 }
1051
1052 const Expr *getLHS() const {
1053 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1054 }
1055
1056 void setLHS(Expr *Val) {
1057 getTrailingObjects<Stmt *>()[lhsOffset()] = reinterpret_cast<Stmt *>(Val);
1058 }
1059
1060 Expr *getRHS() {
1061 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1062 getTrailingObjects<Stmt *>()[rhsOffset()])
1063 : nullptr;
1064 }
1065
1066 const Expr *getRHS() const {
1067 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1068 getTrailingObjects<Stmt *>()[rhsOffset()])
1069 : nullptr;
1070 }
1071
1072 void setRHS(Expr *Val) {
1073 assert(caseStmtIsGNURange() &&
1074 "setRHS but this is not a case stmt of the form LHS ... RHS!");
1075 getTrailingObjects<Stmt *>()[rhsOffset()] = reinterpret_cast<Stmt *>(Val);
1076 }
1077
1078 Stmt *getSubStmt() { return getTrailingObjects<Stmt *>()[subStmtOffset()]; }
1079 const Stmt *getSubStmt() const {
1080 return getTrailingObjects<Stmt *>()[subStmtOffset()];
1081 }
1082
1083 void setSubStmt(Stmt *S) {
1084 getTrailingObjects<Stmt *>()[subStmtOffset()] = S;
1085 }
1086
1087 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1088 SourceLocation getEndLoc() const LLVM_READONLY {
1089 // Handle deeply nested case statements with iteration instead of recursion.
1090 const CaseStmt *CS = this;
1091 while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
1092 CS = CS2;
1093
1094 return CS->getSubStmt()->getEndLoc();
1095 }
1096
1097 static bool classof(const Stmt *T) {
1098 return T->getStmtClass() == CaseStmtClass;
1099 }
1100
1101 // Iterators
1102 child_range children() {
1103 return child_range(getTrailingObjects<Stmt *>(),
1104 getTrailingObjects<Stmt *>() +
1105 numTrailingObjects(OverloadToken<Stmt *>()));
1106 }
1107};
1108
1109class DefaultStmt : public SwitchCase {
1110 Stmt *SubStmt;
1111
1112public:
1113 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt)
1114 : SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
1115
1116 /// Build an empty default statement.
1117 explicit DefaultStmt(EmptyShell Empty)
1118 : SwitchCase(DefaultStmtClass, Empty) {}
1119
1120 Stmt *getSubStmt() { return SubStmt; }
1121 const Stmt *getSubStmt() const { return SubStmt; }
1122 void setSubStmt(Stmt *S) { SubStmt = S; }
1123
1124 SourceLocation getDefaultLoc() const { return getKeywordLoc(); }
1125 void setDefaultLoc(SourceLocation L) { setKeywordLoc(L); }
1126
1127 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1128 SourceLocation getEndLoc() const LLVM_READONLY {
1129 return SubStmt->getEndLoc();
1130 }
1131
1132 static bool classof(const Stmt *T) {
1133 return T->getStmtClass() == DefaultStmtClass;
1134 }
1135
1136 // Iterators
1137 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1138};
1139
1140SourceLocation SwitchCase::getEndLoc() const {
1141 if (const auto *CS = dyn_cast<CaseStmt>(this))
1142 return CS->getEndLoc();
1143 else if (const auto *DS = dyn_cast<DefaultStmt>(this))
1144 return DS->getEndLoc();
1145 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1146}
1147
1148Stmt *SwitchCase::getSubStmt() {
1149 if (auto *CS = dyn_cast<CaseStmt>(this))
1150 return CS->getSubStmt();
1151 else if (auto *DS = dyn_cast<DefaultStmt>(this))
1152 return DS->getSubStmt();
1153 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1154}
1155
1156/// LabelStmt - Represents a label, which has a substatement. For example:
1157/// foo: return;
1158class LabelStmt : public Stmt {
1159 LabelDecl *TheDecl;
1160 Stmt *SubStmt;
1161
1162public:
1163 /// Build a label statement.
1164 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
1165 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt) {
1166 setIdentLoc(IL);
1167 }
1168
1169 /// Build an empty label statement.
1170 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) {}
1171
1172 SourceLocation getIdentLoc() const { return LabelStmtBits.IdentLoc; }
1173 void setIdentLoc(SourceLocation L) { LabelStmtBits.IdentLoc = L; }
1174
1175 LabelDecl *getDecl() const { return TheDecl; }
1176 void setDecl(LabelDecl *D) { TheDecl = D; }
1177
1178 const char *getName() const;
1179 Stmt *getSubStmt() { return SubStmt; }
1180
1181 const Stmt *getSubStmt() const { return SubStmt; }
1182 void setSubStmt(Stmt *SS) { SubStmt = SS; }
1183
1184 SourceLocation getBeginLoc() const { return getIdentLoc(); }
1185 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1186
1187 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1188
1189 static bool classof(const Stmt *T) {
1190 return T->getStmtClass() == LabelStmtClass;
1191 }
1192};
1193
1194/// Represents an attribute applied to a statement.
1195///
1196/// Represents an attribute applied to a statement. For example:
1197/// [[omp::for(...)]] for (...) { ... }
1198class AttributedStmt final
1199 : public Stmt,
1200 private llvm::TrailingObjects<AttributedStmt, const Attr *> {
1201 friend class ASTStmtReader;
1202 friend TrailingObjects;
1203
1204 Stmt *SubStmt;
1205
1206 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr *> Attrs,
1207 Stmt *SubStmt)
1208 : Stmt(AttributedStmtClass), SubStmt(SubStmt) {
1209 AttributedStmtBits.NumAttrs = Attrs.size();
1210 AttributedStmtBits.AttrLoc = Loc;
1211 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
1212 }
1213
1214 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
1215 : Stmt(AttributedStmtClass, Empty) {
1216 AttributedStmtBits.NumAttrs = NumAttrs;
1217 AttributedStmtBits.AttrLoc = SourceLocation{};
1218 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
1219 }
1220
1221 const Attr *const *getAttrArrayPtr() const {
1222 return getTrailingObjects<const Attr *>();
1223 }
1224 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
1225
1226public:
1227 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
1228 ArrayRef<const Attr *> Attrs, Stmt *SubStmt);
1229
1230 // Build an empty attributed statement.
1231 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
1232
1233 SourceLocation getAttrLoc() const { return AttributedStmtBits.AttrLoc; }
1234 ArrayRef<const Attr *> getAttrs() const {
1235 return llvm::makeArrayRef(getAttrArrayPtr(), AttributedStmtBits.NumAttrs);
1236 }
1237
1238 Stmt *getSubStmt() { return SubStmt; }
1239 const Stmt *getSubStmt() const { return SubStmt; }
1240
1241 SourceLocation getBeginLoc() const { return getAttrLoc(); }
1242 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1243
1244 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1245
1246 static bool classof(const Stmt *T) {
1247 return T->getStmtClass() == AttributedStmtClass;
1248 }
1249};
1250
1251/// IfStmt - This represents an if/then/else.
1252class IfStmt final
1253 : public Stmt,
1254 private llvm::TrailingObjects<IfStmt, Stmt *, SourceLocation> {
1255 friend TrailingObjects;
1256
1257 // IfStmt is followed by several trailing objects, some of which optional.
1258 // Note that it would be more convenient to put the optional trailing
1259 // objects at then end but this would change the order of the children.
1260 // The trailing objects are in order:
1261 //
1262 // * A "Stmt *" for the init statement.
1263 // Present if and only if hasInitStorage().
1264 //
1265 // * A "Stmt *" for the condition variable.
1266 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
1267 //
1268 // * A "Stmt *" for the condition.
1269 // Always present. This is in fact a "Expr *".
1270 //
1271 // * A "Stmt *" for the then statement.
1272 // Always present.
1273 //
1274 // * A "Stmt *" for the else statement.
1275 // Present if and only if hasElseStorage().
1276 //
1277 // * A "SourceLocation" for the location of the "else".
1278 // Present if and only if hasElseStorage().
1279 enum { InitOffset = 0, ThenOffsetFromCond = 1, ElseOffsetFromCond = 2 };
1280 enum { NumMandatoryStmtPtr = 2 };
1281
1282 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1283 return NumMandatoryStmtPtr + hasElseStorage() + hasVarStorage() +
1284 hasInitStorage();
1285 }
1286
1287 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
1288 return hasElseStorage();
1289 }
1290
1291 unsigned initOffset() const { return InitOffset; }
1292 unsigned varOffset() const { return InitOffset + hasInitStorage(); }
1293 unsigned condOffset() const {
1294 return InitOffset + hasInitStorage() + hasVarStorage();
1295 }
1296 unsigned thenOffset() const { return condOffset() + ThenOffsetFromCond; }
1297 unsigned elseOffset() const { return condOffset() + ElseOffsetFromCond; }
1298
1299 /// Build an if/then/else statement.
1300 IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr, Stmt *Init,
1301 VarDecl *Var, Expr *Cond, Stmt *Then, SourceLocation EL, Stmt *Else);
1302
1303 /// Build an empty if/then/else statement.
1304 explicit IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit);
1305
1306public:
1307 /// Create an IfStmt.
1308 static IfStmt *Create(const ASTContext &Ctx, SourceLocation IL,
1309 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
1310 Stmt *Then, SourceLocation EL = SourceLocation(),
1311 Stmt *Else = nullptr);
1312
1313 /// Create an empty IfStmt optionally with storage for an else statement,
1314 /// condition variable and init expression.
1315 static IfStmt *CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
1316 bool HasInit);
1317
1318 /// True if this IfStmt has the storage for an init statement.
1319 bool hasInitStorage() const { return IfStmtBits.HasInit; }
1320
1321 /// True if this IfStmt has storage for a variable declaration.
1322 bool hasVarStorage() const { return IfStmtBits.HasVar; }
1323
1324 /// True if this IfStmt has storage for an else statement.
1325 bool hasElseStorage() const { return IfStmtBits.HasElse; }
1326
1327 Expr *getCond() {
1328 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1329 }
1330
1331 const Expr *getCond() const {
1332 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1333 }
1334
1335 void setCond(Expr *Cond) {
1336 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
1337 }
1338
1339 Stmt *getThen() { return getTrailingObjects<Stmt *>()[thenOffset()]; }
1340 const Stmt *getThen() const {
1341 return getTrailingObjects<Stmt *>()[thenOffset()];
1342 }
1343
1344 void setThen(Stmt *Then) {
1345 getTrailingObjects<Stmt *>()[thenOffset()] = Then;
1346 }
1347
1348 Stmt *getElse() {
1349 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
1350 : nullptr;
1351 }
1352
1353 const Stmt *getElse() const {
1354 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
1355 : nullptr;
1356 }
1357
1358 void setElse(Stmt *Else) {
1359 assert(hasElseStorage() &&
1360 "This if statement has no storage for an else statement!");
1361 getTrailingObjects<Stmt *>()[elseOffset()] = Else;
1362 }
1363
1364 /// Retrieve the variable declared in this "if" statement, if any.
1365 ///
1366 /// In the following example, "x" is the condition variable.
1367 /// \code
1368 /// if (int x = foo()) {
1369 /// printf("x is %d", x);
1370 /// }
1371 /// \endcode
1372 VarDecl *getConditionVariable();
1373 const VarDecl *getConditionVariable() const {
1374 return const_cast<IfStmt *>(this)->getConditionVariable();
1375 }
1376
1377 /// Set the condition variable for this if statement.
1378 /// The if statement must have storage for the condition variable.
1379 void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
1380
1381 /// If this IfStmt has a condition variable, return the faux DeclStmt
1382 /// associated with the creation of that condition variable.
1383 DeclStmt *getConditionVariableDeclStmt() {
1384 return hasVarStorage() ? static_cast<DeclStmt *>(
1385 getTrailingObjects<Stmt *>()[varOffset()])
1386 : nullptr;
1387 }
1388
1389 const DeclStmt *getConditionVariableDeclStmt() const {
1390 return hasVarStorage() ? static_cast<DeclStmt *>(
1391 getTrailingObjects<Stmt *>()[varOffset()])
1392 : nullptr;
1393 }
1394
1395 Stmt *getInit() {
1396 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
1397 : nullptr;
1398 }
1399
1400 const Stmt *getInit() const {
1401 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
1402 : nullptr;
1403 }
1404
1405 void setInit(Stmt *Init) {
1406 assert(hasInitStorage() &&
1407 "This if statement has no storage for an init statement!");
1408 getTrailingObjects<Stmt *>()[initOffset()] = Init;
1409 }
1410
1411 SourceLocation getIfLoc() const { return IfStmtBits.IfLoc; }
1412 void setIfLoc(SourceLocation IfLoc) { IfStmtBits.IfLoc = IfLoc; }
1413
1414 SourceLocation getElseLoc() const {
1415 return hasElseStorage() ? *getTrailingObjects<SourceLocation>()
1416 : SourceLocation();
1417 }
1418
1419 void setElseLoc(SourceLocation ElseLoc) {
1420 assert(hasElseStorage() &&
1421 "This if statement has no storage for an else statement!");
1422 *getTrailingObjects<SourceLocation>() = ElseLoc;
1423 }
1424
1425 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
1426 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
1427
1428 bool isObjCAvailabilityCheck() const;
1429
1430 SourceLocation getBeginLoc() const { return getIfLoc(); }
1431 SourceLocation getEndLoc() const LLVM_READONLY {
1432 if (getElse())
1433 return getElse()->getEndLoc();
1434 return getThen()->getEndLoc();
1435 }
1436
1437 // Iterators over subexpressions. The iterators will include iterating
1438 // over the initialization expression referenced by the condition variable.
1439 child_range children() {
1440 return child_range(getTrailingObjects<Stmt *>(),
1441 getTrailingObjects<Stmt *>() +
1442 numTrailingObjects(OverloadToken<Stmt *>()));
1443 }
1444
1445 static bool classof(const Stmt *T) {
1446 return T->getStmtClass() == IfStmtClass;
1447 }
1448};
1449
1450/// SwitchStmt - This represents a 'switch' stmt.
1451class SwitchStmt final : public Stmt,
1452 private llvm::TrailingObjects<SwitchStmt, Stmt *> {
1453 friend TrailingObjects;
1454
1455 /// Points to a linked list of case and default statements.
1456 SwitchCase *FirstCase;
1457
1458 // SwitchStmt is followed by several trailing objects,
1459 // some of which optional. Note that it would be more convenient to
1460 // put the optional trailing objects at the end but this would change
1461 // the order in children().
1462 // The trailing objects are in order:
1463 //
1464 // * A "Stmt *" for the init statement.
1465 // Present if and only if hasInitStorage().
1466 //
1467 // * A "Stmt *" for the condition variable.
1468 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
1469 //
1470 // * A "Stmt *" for the condition.
1471 // Always present. This is in fact an "Expr *".
1472 //
1473 // * A "Stmt *" for the body.
1474 // Always present.
1475 enum { InitOffset = 0, BodyOffsetFromCond = 1 };
1476 enum { NumMandatoryStmtPtr = 2 };
1477
1478 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1479 return NumMandatoryStmtPtr + hasInitStorage() + hasVarStorage();
1480 }
1481
1482 unsigned initOffset() const { return InitOffset; }
1483 unsigned varOffset() const { return InitOffset + hasInitStorage(); }
1484 unsigned condOffset() const {
1485 return InitOffset + hasInitStorage() + hasVarStorage();
1486 }
1487 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
1488
1489 /// Build a switch statement.
1490 SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, Expr *Cond);
1491
1492 /// Build a empty switch statement.
1493 explicit SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar);
1494
1495public:
1496 /// Create a switch statement.
1497 static SwitchStmt *Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
1498 Expr *Cond);
1499
1500 /// Create an empty switch statement optionally with storage for
1501 /// an init expression and a condition variable.
1502 static SwitchStmt *CreateEmpty(const ASTContext &Ctx, bool HasInit,
1503 bool HasVar);
1504
1505 /// True if this SwitchStmt has storage for an init statement.
1506 bool hasInitStorage() const { return SwitchStmtBits.HasInit; }
1507
1508 /// True if this SwitchStmt has storage for a condition variable.
1509 bool hasVarStorage() const { return SwitchStmtBits.HasVar; }
1510
1511 Expr *getCond() {
1512 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1513 }
1514
1515 const Expr *getCond() const {
1516 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1517 }
1518
1519 void setCond(Expr *Cond) {
1520 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
1521 }
1522
1523 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
1524 const Stmt *getBody() const {
1525 return getTrailingObjects<Stmt *>()[bodyOffset()];
1526 }
1527
1528 void setBody(Stmt *Body) {
1529 getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
1530 }
1531
1532 Stmt *getInit() {
1533 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
1534 : nullptr;
1535 }
1536
1537 const Stmt *getInit() const {
1538 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
1539 : nullptr;
1540 }
1541
1542 void setInit(Stmt *Init) {
1543 assert(hasInitStorage() &&
1544 "This switch statement has no storage for an init statement!");
1545 getTrailingObjects<Stmt *>()[initOffset()] = Init;
1546 }
1547
1548 /// Retrieve the variable declared in this "switch" statement, if any.
1549 ///
1550 /// In the following example, "x" is the condition variable.
1551 /// \code
1552 /// switch (int x = foo()) {
1553 /// case 0: break;
1554 /// // ...
1555 /// }
1556 /// \endcode
1557 VarDecl *getConditionVariable();
1558 const VarDecl *getConditionVariable() const {
1559 return const_cast<SwitchStmt *>(this)->getConditionVariable();
1560 }
1561
1562 /// Set the condition variable in this switch statement.
1563 /// The switch statement must have storage for it.
1564 void setConditionVariable(const ASTContext &Ctx, VarDecl *VD);
1565
1566 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1567 /// associated with the creation of that condition variable.
1568 DeclStmt *getConditionVariableDeclStmt() {
1569 return hasVarStorage() ? static_cast<DeclStmt *>(
1570 getTrailingObjects<Stmt *>()[varOffset()])
1571 : nullptr;
1572 }
1573
1574 const DeclStmt *getConditionVariableDeclStmt() const {
1575 return hasVarStorage() ? static_cast<DeclStmt *>(
1576 getTrailingObjects<Stmt *>()[varOffset()])
1577 : nullptr;
1578 }
1579
1580 SwitchCase *getSwitchCaseList() { return FirstCase; }
1581 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
1582 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
1583
1584 SourceLocation getSwitchLoc() const { return SwitchStmtBits.SwitchLoc; }
1585 void setSwitchLoc(SourceLocation L) { SwitchStmtBits.SwitchLoc = L; }
1586
1587 void setBody(Stmt *S, SourceLocation SL) {
1588 setBody(S);
1589 setSwitchLoc(SL);
1590 }
1591
1592 void addSwitchCase(SwitchCase *SC) {
1593 assert(!SC->getNextSwitchCase() &&
1594 "case/default already added to a switch");
1595 SC->setNextSwitchCase(FirstCase);
1596 FirstCase = SC;
1597 }
1598
1599 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1600 /// switch over an enum value then all cases have been explicitly covered.
1601 void setAllEnumCasesCovered() { SwitchStmtBits.AllEnumCasesCovered = true; }
1602
1603 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1604 /// have been explicitly covered.
1605 bool isAllEnumCasesCovered() const {
1606 return SwitchStmtBits.AllEnumCasesCovered;
1607 }
1608
1609 SourceLocation getBeginLoc() const { return getSwitchLoc(); }
1610 SourceLocation getEndLoc() const LLVM_READONLY {
1611 return getBody() ? getBody()->getEndLoc()
1612 : reinterpret_cast<const Stmt *>(getCond())->getEndLoc();
1613 }
1614
1615 // Iterators
1616 child_range children() {
1617 return child_range(getTrailingObjects<Stmt *>(),
1618 getTrailingObjects<Stmt *>() +
1619 numTrailingObjects(OverloadToken<Stmt *>()));
1620 }
1621
1622 static bool classof(const Stmt *T) {
1623 return T->getStmtClass() == SwitchStmtClass;
1624 }
1625};
1626
1627/// WhileStmt - This represents a 'while' stmt.
1628class WhileStmt final : public Stmt,
1629 private llvm::TrailingObjects<WhileStmt, Stmt *> {
1630 friend TrailingObjects;
1631
1632 // WhileStmt is followed by several trailing objects,
1633 // some of which optional. Note that it would be more
1634 // convenient to put the optional trailing object at the end
1635 // but this would affect children().
1636 // The trailing objects are in order:
1637 //
1638 // * A "Stmt *" for the condition variable.
1639 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
1640 //
1641 // * A "Stmt *" for the condition.
1642 // Always present. This is in fact an "Expr *".
1643 //
1644 // * A "Stmt *" for the body.
1645 // Always present.
1646 //
1647 enum { VarOffset = 0, BodyOffsetFromCond = 1 };
1648 enum { NumMandatoryStmtPtr = 2 };
1649
1650 unsigned varOffset() const { return VarOffset; }
1651 unsigned condOffset() const { return VarOffset + hasVarStorage(); }
1652 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
1653
1654 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1655 return NumMandatoryStmtPtr + hasVarStorage();
1656 }
1657
1658 /// Build a while statement.
1659 WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, Stmt *Body,
1660 SourceLocation WL);
1661
1662 /// Build an empty while statement.
1663 explicit WhileStmt(EmptyShell Empty, bool HasVar);
1664
1665public:
1666 /// Create a while statement.
1667 static WhileStmt *Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
1668 Stmt *Body, SourceLocation WL);
1669
1670 /// Create an empty while statement optionally with storage for
1671 /// a condition variable.
1672 static WhileStmt *CreateEmpty(const ASTContext &Ctx, bool HasVar);
1673
1674 /// True if this WhileStmt has storage for a condition variable.
1675 bool hasVarStorage() const { return WhileStmtBits.HasVar; }
1676
1677 Expr *getCond() {
1678 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1679 }
1680
1681 const Expr *getCond() const {
1682 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1683 }
1684
1685 void setCond(Expr *Cond) {
1686 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
1687 }
1688
1689 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
1690 const Stmt *getBody() const {
1691 return getTrailingObjects<Stmt *>()[bodyOffset()];
1692 }
1693
1694 void setBody(Stmt *Body) {
1695 getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
1696 }
1697
1698 /// Retrieve the variable declared in this "while" statement, if any.
1699 ///
1700 /// In the following example, "x" is the condition variable.
1701 /// \code
1702 /// while (int x = random()) {
1703 /// // ...
1704 /// }
1705 /// \endcode
1706 VarDecl *getConditionVariable();
1707 const VarDecl *getConditionVariable() const {
1708 return const_cast<WhileStmt *>(this)->getConditionVariable();
1709 }
1710
1711 /// Set the condition variable of this while statement.
1712 /// The while statement must have storage for it.
1713 void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
1714
1715 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1716 /// associated with the creation of that condition variable.
1717 DeclStmt *getConditionVariableDeclStmt() {
1718 return hasVarStorage() ? static_cast<DeclStmt *>(
1719 getTrailingObjects<Stmt *>()[varOffset()])
1720 : nullptr;
1721 }
1722
1723 const DeclStmt *getConditionVariableDeclStmt() const {
1724 return hasVarStorage() ? static_cast<DeclStmt *>(
1725 getTrailingObjects<Stmt *>()[varOffset()])
1726 : nullptr;
1727 }
1728
1729 SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; }
1730 void setWhileLoc(SourceLocation L) { WhileStmtBits.WhileLoc = L; }
1731
1732 SourceLocation getBeginLoc() const { return getWhileLoc(); }
1733 SourceLocation getEndLoc() const LLVM_READONLY {
1734 return getBody()->getEndLoc();
1735 }
1736
1737 static bool classof(const Stmt *T) {
1738 return T->getStmtClass() == WhileStmtClass;
1739 }
1740
1741 // Iterators
1742 child_range children() {
1743 return child_range(getTrailingObjects<Stmt *>(),
1744 getTrailingObjects<Stmt *>() +
1745 numTrailingObjects(OverloadToken<Stmt *>()));
1746 }
1747};
1748
1749/// DoStmt - This represents a 'do/while' stmt.
1750class DoStmt : public Stmt {
1751 enum { BODY, COND, END_EXPR };
1752 Stmt *SubExprs[END_EXPR];
1753 SourceLocation WhileLoc;
1754 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1755
1756public:
1757 DoStmt(Stmt *Body, Expr *Cond, SourceLocation DL, SourceLocation WL,
1758 SourceLocation RP)
1759 : Stmt(DoStmtClass), WhileLoc(WL), RParenLoc(RP) {
1760 setCond(Cond);
1761 setBody(Body);
1762 setDoLoc(DL);
1763 }
1764
1765 /// Build an empty do-while statement.
1766 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
1767
1768 Expr *getCond() { return reinterpret_cast<Expr *>(SubExprs[COND]); }
1769 const Expr *getCond() const {
1770 return reinterpret_cast<Expr *>(SubExprs[COND]);
1771 }
1772
1773 void setCond(Expr *Cond) { SubExprs[COND] = reinterpret_cast<Stmt *>(Cond); }
1774
1775 Stmt *getBody() { return SubExprs[BODY]; }
1776 const Stmt *getBody() const { return SubExprs[BODY]; }
1777 void setBody(Stmt *Body) { SubExprs[BODY] = Body; }
1778
1779 SourceLocation getDoLoc() const { return DoStmtBits.DoLoc; }
1780 void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L; }
1781 SourceLocation getWhileLoc() const { return WhileLoc; }
1782 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1783 SourceLocation getRParenLoc() const { return RParenLoc; }
1784 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1785
1786 SourceLocation getBeginLoc() const { return getDoLoc(); }
1787 SourceLocation getEndLoc() const { return getRParenLoc(); }
1788
1789 static bool classof(const Stmt *T) {
1790 return T->getStmtClass() == DoStmtClass;
1791 }
1792
1793 // Iterators
1794 child_range children() {
1795 return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
1796 }
1797};
1798
1799/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1800/// the init/cond/inc parts of the ForStmt will be null if they were not
1801/// specified in the source.
1802class ForStmt : public Stmt {
1803 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1804 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1805 SourceLocation LParenLoc, RParenLoc;
1806
1807public:
1808 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1809 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1810 SourceLocation RP);
1811
1812 /// Build an empty for statement.
1813 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
1814
1815 Stmt *getInit() { return SubExprs[INIT]; }
1816
1817 /// Retrieve the variable declared in this "for" statement, if any.
1818 ///
1819 /// In the following example, "y" is the condition variable.
1820 /// \code
1821 /// for (int x = random(); int y = mangle(x); ++x) {
1822 /// // ...
1823 /// }
1824 /// \endcode
1825 VarDecl *getConditionVariable() const;
1826 void setConditionVariable(const ASTContext &C, VarDecl *V);
1827
1828 /// If this ForStmt has a condition variable, return the faux DeclStmt
1829 /// associated with the creation of that condition variable.
1830 const DeclStmt *getConditionVariableDeclStmt() const {
1831 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1832 }
1833
1834 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1835 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1836 Stmt *getBody() { return SubExprs[BODY]; }
1837
1838 const Stmt *getInit() const { return SubExprs[INIT]; }
1839 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1840 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1841 const Stmt *getBody() const { return SubExprs[BODY]; }
1842
1843 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1844 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1845 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1846 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1847
1848 SourceLocation getForLoc() const { return ForStmtBits.ForLoc; }
1849 void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L; }
1850 SourceLocation getLParenLoc() const { return LParenLoc; }
1851 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1852 SourceLocation getRParenLoc() const { return RParenLoc; }
1853 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1854
1855 SourceLocation getBeginLoc() const { return getForLoc(); }
1856 SourceLocation getEndLoc() const { return getBody()->getEndLoc(); }
1857
1858 static bool classof(const Stmt *T) {
1859 return T->getStmtClass() == ForStmtClass;
1860 }
1861
1862 // Iterators
1863 child_range children() {
1864 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1865 }
1866};
1867
1868/// GotoStmt - This represents a direct goto.
1869class GotoStmt : public Stmt {
1870 LabelDecl *Label;
1871 SourceLocation LabelLoc;
1872
1873public:
1874 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1875 : Stmt(GotoStmtClass), Label(label), LabelLoc(LL) {
1876 setGotoLoc(GL);
1877 }
1878
1879 /// Build an empty goto statement.
1880 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
1881
1882 LabelDecl *getLabel() const { return Label; }
1883 void setLabel(LabelDecl *D) { Label = D; }
1884
1885 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
1886 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
1887 SourceLocation getLabelLoc() const { return LabelLoc; }
1888 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1889
1890 SourceLocation getBeginLoc() const { return getGotoLoc(); }
1891 SourceLocation getEndLoc() const { return getLabelLoc(); }
1892
1893 static bool classof(const Stmt *T) {
1894 return T->getStmtClass() == GotoStmtClass;
1895 }
1896
1897 // Iterators
1898 child_range children() {
1899 return child_range(child_iterator(), child_iterator());
1900 }
1901};
1902
1903/// IndirectGotoStmt - This represents an indirect goto.
1904class IndirectGotoStmt : public Stmt {
1905 SourceLocation StarLoc;
1906 Stmt *Target;
1907
1908public:
1909 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, Expr *target)
1910 : Stmt(IndirectGotoStmtClass), StarLoc(starLoc) {
1911 setTarget(target);
1912 setGotoLoc(gotoLoc);
1913 }
1914
1915 /// Build an empty indirect goto statement.
1916 explicit IndirectGotoStmt(EmptyShell Empty)
1917 : Stmt(IndirectGotoStmtClass, Empty) {}
1918
1919 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
1920 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
1921 void setStarLoc(SourceLocation L) { StarLoc = L; }
1922 SourceLocation getStarLoc() const { return StarLoc; }
1923
1924 Expr *getTarget() { return reinterpret_cast<Expr *>(Target); }
1925 const Expr *getTarget() const {
1926 return reinterpret_cast<const Expr *>(Target);
1927 }
1928 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt *>(E); }
1929
1930 /// getConstantTarget - Returns the fixed target of this indirect
1931 /// goto, if one exists.
1932 LabelDecl *getConstantTarget();
1933 const LabelDecl *getConstantTarget() const {
1934 return const_cast<IndirectGotoStmt *>(this)->getConstantTarget();
1935 }
1936
1937 SourceLocation getBeginLoc() const { return getGotoLoc(); }
1938 SourceLocation getEndLoc() const LLVM_READONLY { return Target->getEndLoc(); }
1939
1940 static bool classof(const Stmt *T) {
1941 return T->getStmtClass() == IndirectGotoStmtClass;
1942 }
1943
1944 // Iterators
1945 child_range children() { return child_range(&Target, &Target + 1); }
1946};
1947
1948/// ContinueStmt - This represents a continue.
1949class ContinueStmt : public Stmt {
1950public:
1951 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass) {
1952 setContinueLoc(CL);
1953 }
1954
1955 /// Build an empty continue statement.
1956 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
1957
1958 SourceLocation getContinueLoc() const { return ContinueStmtBits.ContinueLoc; }
1959 void setContinueLoc(SourceLocation L) { ContinueStmtBits.ContinueLoc = L; }
1960
1961 SourceLocation getBeginLoc() const { return getContinueLoc(); }
1962 SourceLocation getEndLoc() const { return getContinueLoc(); }
1963
1964 static bool classof(const Stmt *T) {
1965 return T->getStmtClass() == ContinueStmtClass;
1966 }
1967
1968 // Iterators
1969 child_range children() {
1970 return child_range(child_iterator(), child_iterator());
1971 }
1972};
1973
1974/// BreakStmt - This represents a break.
1975class BreakStmt : public Stmt {
1976public:
1977 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass) {
1978 setBreakLoc(BL);
1979 }
1980
1981 /// Build an empty break statement.
1982 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
1983
1984 SourceLocation getBreakLoc() const { return BreakStmtBits.BreakLoc; }
1985 void setBreakLoc(SourceLocation L) { BreakStmtBits.BreakLoc = L; }
1986
1987 SourceLocation getBeginLoc() const { return getBreakLoc(); }
1988 SourceLocation getEndLoc() const { return getBreakLoc(); }
1989
1990 static bool classof(const Stmt *T) {
1991 return T->getStmtClass() == BreakStmtClass;
1992 }
1993
1994 // Iterators
1995 child_range children() {
1996 return child_range(child_iterator(), child_iterator());
1997 }
1998};
1999
2000/// ReturnStmt - This represents a return, optionally of an expression:
2001/// return;
2002/// return 4;
2003///
2004/// Note that GCC allows return with no argument in a function declared to
2005/// return a value, and it allows returning a value in functions declared to
2006/// return void. We explicitly model this in the AST, which means you can't
2007/// depend on the return type of the function and the presence of an argument.
2008class ReturnStmt final
2009 : public Stmt,
2010 private llvm::TrailingObjects<ReturnStmt, const VarDecl *> {
2011 friend TrailingObjects;
2012
2013 /// The return expression.
2014 Stmt *RetExpr;
2015
2016 // ReturnStmt is followed optionally by a trailing "const VarDecl *"
2017 // for the NRVO candidate. Present if and only if hasNRVOCandidate().
2018
2019 /// True if this ReturnStmt has storage for an NRVO candidate.
2020 bool hasNRVOCandidate() const { return ReturnStmtBits.HasNRVOCandidate; }
2021
2022 unsigned numTrailingObjects(OverloadToken<const VarDecl *>) const {
2023 return hasNRVOCandidate();
2024 }
2025
2026 /// Build a return statement.
2027 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate);
2028
2029 /// Build an empty return statement.
2030 explicit ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate);
2031
2032public:
2033 /// Create a return statement.
2034 static ReturnStmt *Create(const ASTContext &Ctx, SourceLocation RL, Expr *E,
2035 const VarDecl *NRVOCandidate);
2036
2037 /// Create an empty return statement, optionally with
2038 /// storage for an NRVO candidate.
2039 static ReturnStmt *CreateEmpty(const ASTContext &Ctx, bool HasNRVOCandidate);
2040
2041 Expr *getRetValue() { return reinterpret_cast<Expr *>(RetExpr); }
2042 const Expr *getRetValue() const { return reinterpret_cast<Expr *>(RetExpr); }
2043 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt *>(E); }
2044
2045 /// Retrieve the variable that might be used for the named return
2046 /// value optimization.
2047 ///
2048 /// The optimization itself can only be performed if the variable is
2049 /// also marked as an NRVO object.
2050 const VarDecl *getNRVOCandidate() const {
2051 return hasNRVOCandidate() ? *getTrailingObjects<const VarDecl *>()
2052 : nullptr;
2053 }
2054
2055 /// Set the variable that might be used for the named return value
2056 /// optimization. The return statement must have storage for it,
2057 /// which is the case if and only if hasNRVOCandidate() is true.
2058 void setNRVOCandidate(const VarDecl *Var) {
2059 assert(hasNRVOCandidate() &&
2060 "This return statement has no storage for an NRVO candidate!");
2061 *getTrailingObjects<const VarDecl *>() = Var;
2062 }
2063
2064 SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; }
2065 void setReturnLoc(SourceLocation L) { ReturnStmtBits.RetLoc = L; }
2066
2067 SourceLocation getBeginLoc() const { return getReturnLoc(); }
2068 SourceLocation getEndLoc() const LLVM_READONLY {
2069 return RetExpr ? RetExpr->getEndLoc() : getReturnLoc();
2070 }
2071
2072 static bool classof(const Stmt *T) {
2073 return T->getStmtClass() == ReturnStmtClass;
2074 }
2075
2076 // Iterators
2077 child_range children() {
2078 if (RetExpr)
2079 return child_range(&RetExpr, &RetExpr + 1);
2080 return child_range(child_iterator(), child_iterator());
2081 }
2082};
2083
2084/// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
2085class AsmStmt : public Stmt {
2086protected:
2087 friend class ASTStmtReader;
2088
2089 SourceLocation AsmLoc;
2090
2091 /// True if the assembly statement does not have any input or output
2092 /// operands.
2093 bool IsSimple;
2094
2095 /// If true, treat this inline assembly as having side effects.
2096 /// This assembly statement should not be optimized, deleted or moved.
2097 bool IsVolatile;
2098
2099 unsigned NumOutputs;
2100 unsigned NumInputs;
2101 unsigned NumClobbers;
2102
2103 Stmt **Exprs = nullptr;
2104
2105 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
2106 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
2107 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
2108 NumOutputs(numoutputs), NumInputs(numinputs),
2109 NumClobbers(numclobbers) {}
2110
2111public:
2112 /// Build an empty inline-assembly statement.
2113 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
2114
2115 SourceLocation getAsmLoc() const { return AsmLoc; }
2116 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
2117
2118 bool isSimple() const { return IsSimple; }
2119 void setSimple(bool V) { IsSimple = V; }
2120
2121 bool isVolatile() const { return IsVolatile; }
2122 void setVolatile(bool V) { IsVolatile = V; }
2123
2124 SourceLocation getBeginLoc() const LLVM_READONLY { return {}; }
2125 SourceLocation getEndLoc() const LLVM_READONLY { return {}; }
2126
2127 //===--- Asm String Analysis ---===//
2128
2129 /// Assemble final IR asm string.
2130 std::string generateAsmString(const ASTContext &C) const;
2131
2132 //===--- Output operands ---===//
2133
2134 unsigned getNumOutputs() const { return NumOutputs; }
2135
2136 /// getOutputConstraint - Return the constraint string for the specified
2137 /// output operand. All output constraints are known to be non-empty (either
2138 /// '=' or '+').
2139 StringRef getOutputConstraint(unsigned i) const;
2140
2141 /// isOutputPlusConstraint - Return true if the specified output constraint
2142 /// is a "+" constraint (which is both an input and an output) or false if it
2143 /// is an "=" constraint (just an output).
2144 bool isOutputPlusConstraint(unsigned i) const {
2145 return getOutputConstraint(i)[0] == '+';
2146 }
2147
2148 const Expr *getOutputExpr(unsigned i) const;
2149
2150 /// getNumPlusOperands - Return the number of output operands that have a "+"
2151 /// constraint.
2152 unsigned getNumPlusOperands() const;
2153
2154 //===--- Input operands ---===//
2155
2156 unsigned getNumInputs() const { return NumInputs; }
2157
2158 /// getInputConstraint - Return the specified input constraint. Unlike output
2159 /// constraints, these can be empty.
2160 StringRef getInputConstraint(unsigned i) const;
2161
2162 const Expr *getInputExpr(unsigned i) const;
2163
2164 //===--- Other ---===//
2165
2166 unsigned getNumClobbers() const { return NumClobbers; }
2167 StringRef getClobber(unsigned i) const;
2168
2169 static bool classof(const Stmt *T) {
2170 return T->getStmtClass() == GCCAsmStmtClass ||
2171 T->getStmtClass() == MSAsmStmtClass;
2172 }
2173
2174 // Input expr iterators.
2175
2176 using inputs_iterator = ExprIterator;
2177 using const_inputs_iterator = ConstExprIterator;
2178 using inputs_range = llvm::iterator_range<inputs_iterator>;
2179 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
2180
2181 inputs_iterator begin_inputs() {
2182 return &Exprs[0] + NumOutputs;
2183 }
2184
2185 inputs_iterator end_inputs() {
2186 return &Exprs[0] + NumOutputs + NumInputs;
2187 }
2188
2189 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
2190
2191 const_inputs_iterator begin_inputs() const {
2192 return &Exprs[0] + NumOutputs;
2193 }
2194
2195 const_inputs_iterator end_inputs() const {
2196 return &Exprs[0] + NumOutputs + NumInputs;
2197 }
2198
2199 inputs_const_range inputs() const {
2200 return inputs_const_range(begin_inputs(), end_inputs());
2201 }
2202
2203 // Output expr iterators.
2204
2205 using outputs_iterator = ExprIterator;
2206 using const_outputs_iterator = ConstExprIterator;
2207 using outputs_range = llvm::iterator_range<outputs_iterator>;
2208 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
2209
2210 outputs_iterator begin_outputs() {
2211 return &Exprs[0];
2212 }
2213
2214 outputs_iterator end_outputs() {
2215 return &Exprs[0] + NumOutputs;
2216 }
2217
2218 outputs_range outputs() {
2219 return outputs_range(begin_outputs(), end_outputs());
2220 }
2221
2222 const_outputs_iterator begin_outputs() const {
2223 return &Exprs[0];
2224 }
2225
2226 const_outputs_iterator end_outputs() const {
2227 return &Exprs[0] + NumOutputs;
2228 }
2229
2230 outputs_const_range outputs() const {
2231 return outputs_const_range(begin_outputs(), end_outputs());
2232 }
2233
2234 child_range children() {
2235 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
2236 }
2237};
2238
2239/// This represents a GCC inline-assembly statement extension.
2240class GCCAsmStmt : public AsmStmt {
2241 friend class ASTStmtReader;
2242
2243 SourceLocation RParenLoc;
2244 StringLiteral *AsmStr;
2245
2246 // FIXME: If we wanted to, we could allocate all of these in one big array.
2247 StringLiteral **Constraints = nullptr;
2248 StringLiteral **Clobbers = nullptr;
2249 IdentifierInfo **Names = nullptr;
2250
2251public:
2252 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
2253 bool isvolatile, unsigned numoutputs, unsigned numinputs,
2254 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
2255 StringLiteral *asmstr, unsigned numclobbers,
2256 StringLiteral **clobbers, SourceLocation rparenloc);
2257
2258 /// Build an empty inline-assembly statement.
2259 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
2260
2261 SourceLocation getRParenLoc() const { return RParenLoc; }
2262 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2263
2264 //===--- Asm String Analysis ---===//
2265
2266 const StringLiteral *getAsmString() const { return AsmStr; }
2267 StringLiteral *getAsmString() { return AsmStr; }
2268 void setAsmString(StringLiteral *E) { AsmStr = E; }
2269
2270 /// AsmStringPiece - this is part of a decomposed asm string specification
2271 /// (for use with the AnalyzeAsmString function below). An asm string is
2272 /// considered to be a concatenation of these parts.
2273 class AsmStringPiece {
2274 public:
2275 enum Kind {
2276 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
2277 Operand // Operand reference, with optional modifier %c4.
2278 };
2279
2280 private:
2281 Kind MyKind;
2282 std::string Str;
2283 unsigned OperandNo;
2284
2285 // Source range for operand references.
2286 CharSourceRange Range;
2287
2288 public:
2289 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
2290 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
2291 SourceLocation End)
2292 : MyKind(Operand), Str(S), OperandNo(OpNo),
2293 Range(CharSourceRange::getCharRange(Begin, End)) {}
2294
2295 bool isString() const { return MyKind == String; }
2296 bool isOperand() const { return MyKind == Operand; }
2297
2298 const std::string &getString() const { return Str; }
2299
2300 unsigned getOperandNo() const {
2301 assert(isOperand());
2302 return OperandNo;
2303 }
2304
2305 CharSourceRange getRange() const {
2306 assert(isOperand() && "Range is currently used only for Operands.");
2307 return Range;
2308 }
2309
2310 /// getModifier - Get the modifier for this operand, if present. This
2311 /// returns '\0' if there was no modifier.
2312 char getModifier() const;
2313 };
2314
2315 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
2316 /// it into pieces. If the asm string is erroneous, emit errors and return
2317 /// true, otherwise return false. This handles canonicalization and
2318 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
2319 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
2320 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
2321 const ASTContext &C, unsigned &DiagOffs) const;
2322
2323 /// Assemble final IR asm string.
2324 std::string generateAsmString(const ASTContext &C) const;
2325
2326 //===--- Output operands ---===//
2327
2328 IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
2329
2330 StringRef getOutputName(unsigned i) const {
2331 if (IdentifierInfo *II = getOutputIdentifier(i))
2332 return II->getName();
2333
2334 return {};
2335 }
2336
2337 StringRef getOutputConstraint(unsigned i) const;
2338
2339 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
2340 return Constraints[i];
2341 }
2342 StringLiteral *getOutputConstraintLiteral(unsigned i) {
2343 return Constraints[i];
2344 }
2345
2346 Expr *getOutputExpr(unsigned i);
2347
2348 const Expr *getOutputExpr(unsigned i) const {
2349 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
2350 }
2351
2352 //===--- Input operands ---===//
2353
2354 IdentifierInfo *getInputIdentifier(unsigned i) const {
2355 return Names[i + NumOutputs];
2356 }
2357
2358 StringRef getInputName(unsigned i) const {
2359 if (IdentifierInfo *II = getInputIdentifier(i))
2360 return II->getName();
2361
2362 return {};
2363 }
2364
2365 StringRef getInputConstraint(unsigned i) const;
2366
2367 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
2368 return Constraints[i + NumOutputs];
2369 }
2370 StringLiteral *getInputConstraintLiteral(unsigned i) {
2371 return Constraints[i + NumOutputs];
2372 }
2373
2374 Expr *getInputExpr(unsigned i);
2375 void setInputExpr(unsigned i, Expr *E);
2376
2377 const Expr *getInputExpr(unsigned i) const {
2378 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
2379 }
2380
2381private:
2382 void setOutputsAndInputsAndClobbers(const ASTContext &C,
2383 IdentifierInfo **Names,
2384 StringLiteral **Constraints,
2385 Stmt **Exprs,
2386 unsigned NumOutputs,
2387 unsigned NumInputs,
2388 StringLiteral **Clobbers,
2389 unsigned NumClobbers);
2390
2391public:
2392 //===--- Other ---===//
2393
2394 /// getNamedOperand - Given a symbolic operand reference like %[foo],
2395 /// translate this into a numeric value needed to reference the same operand.
2396 /// This returns -1 if the operand name is invalid.
2397 int getNamedOperand(StringRef SymbolicName) const;
2398
2399 StringRef getClobber(unsigned i) const;
2400
2401 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
2402 const StringLiteral *getClobberStringLiteral(unsigned i) const {
2403 return Clobbers[i];
2404 }
2405
2406 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
2407 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
2408
2409 static bool classof(const Stmt *T) {
2410 return T->getStmtClass() == GCCAsmStmtClass;
2411 }
2412};
2413
2414/// This represents a Microsoft inline-assembly statement extension.
2415class MSAsmStmt : public AsmStmt {
2416 friend class ASTStmtReader;
2417
2418 SourceLocation LBraceLoc, EndLoc;
2419 StringRef AsmStr;
2420
2421 unsigned NumAsmToks = 0;
2422
2423 Token *AsmToks = nullptr;
2424 StringRef *Constraints = nullptr;
2425 StringRef *Clobbers = nullptr;
2426
2427public:
2428 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
2429 SourceLocation lbraceloc, bool issimple, bool isvolatile,
2430 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
2431 ArrayRef<StringRef> constraints,
2432 ArrayRef<Expr*> exprs, StringRef asmstr,
2433 ArrayRef<StringRef> clobbers, SourceLocation endloc);
2434
2435 /// Build an empty MS-style inline-assembly statement.
2436 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
2437
2438 SourceLocation getLBraceLoc() const { return LBraceLoc; }
2439 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
2440 SourceLocation getEndLoc() const { return EndLoc; }
2441 void setEndLoc(SourceLocation L) { EndLoc = L; }
2442
2443 bool hasBraces() const { return LBraceLoc.isValid(); }
2444
2445 unsigned getNumAsmToks() { return NumAsmToks; }
2446 Token *getAsmToks() { return AsmToks; }
2447
2448 //===--- Asm String Analysis ---===//
2449 StringRef getAsmString() const { return AsmStr; }
2450
2451 /// Assemble final IR asm string.
2452 std::string generateAsmString(const ASTContext &C) const;
2453
2454 //===--- Output operands ---===//
2455
2456 StringRef getOutputConstraint(unsigned i) const {
2457 assert(i < NumOutputs);
2458 return Constraints[i];
2459 }
2460
2461 Expr *getOutputExpr(unsigned i);
2462
2463 const Expr *getOutputExpr(unsigned i) const {
2464 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
2465 }
2466
2467 //===--- Input operands ---===//
2468
2469 StringRef getInputConstraint(unsigned i) const {
2470 assert(i < NumInputs);
2471 return Constraints[i + NumOutputs];
2472 }
2473
2474 Expr *getInputExpr(unsigned i);
2475 void setInputExpr(unsigned i, Expr *E);
2476
2477 const Expr *getInputExpr(unsigned i) const {
2478 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
2479 }
2480
2481 //===--- Other ---===//
2482
2483 ArrayRef<StringRef> getAllConstraints() const {
2484 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
2485 }
2486
2487 ArrayRef<StringRef> getClobbers() const {
2488 return llvm::makeArrayRef(Clobbers, NumClobbers);
2489 }
2490
2491 ArrayRef<Expr*> getAllExprs() const {
2492 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
2493 NumInputs + NumOutputs);
2494 }
2495
2496 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
2497
2498private:
2499 void initialize(const ASTContext &C, StringRef AsmString,
2500 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
2501 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
2502
2503public:
2504 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
2505
2506 static bool classof(const Stmt *T) {
2507 return T->getStmtClass() == MSAsmStmtClass;
2508 }
2509
2510 child_range children() {
2511 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
2512 }
2513};
2514
2515class SEHExceptStmt : public Stmt {
2516 friend class ASTReader;
2517 friend class ASTStmtReader;
2518
2519 SourceLocation Loc;
2520 Stmt *Children[2];
2521
2522 enum { FILTER_EXPR, BLOCK };
2523
2524 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
2525 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
2526
2527public:
2528 static SEHExceptStmt* Create(const ASTContext &C,
2529 SourceLocation ExceptLoc,
2530 Expr *FilterExpr,
2531 Stmt *Block);
2532
2533 SourceLocation getBeginLoc() const LLVM_READONLY { return getExceptLoc(); }
2534
2535 SourceLocation getExceptLoc() const { return Loc; }
2536 SourceLocation getEndLoc() const { return getBlock()->getEndLoc(); }
2537
2538 Expr *getFilterExpr() const {
2539 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
2540 }
2541
2542 CompoundStmt *getBlock() const {
2543 return cast<CompoundStmt>(Children[BLOCK]);
2544 }
2545
2546 child_range children() {
2547 return child_range(Children, Children+2);
2548 }
2549
2550 static bool classof(const Stmt *T) {
2551 return T->getStmtClass() == SEHExceptStmtClass;
2552 }
2553};
2554
2555class SEHFinallyStmt : public Stmt {
2556 friend class ASTReader;
2557 friend class ASTStmtReader;
2558
2559 SourceLocation Loc;
2560 Stmt *Block;
2561
2562 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
2563 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
2564
2565public:
2566 static SEHFinallyStmt* Create(const ASTContext &C,
2567 SourceLocation FinallyLoc,
2568 Stmt *Block);
2569
2570 SourceLocation getBeginLoc() const LLVM_READONLY { return getFinallyLoc(); }
2571
2572 SourceLocation getFinallyLoc() const { return Loc; }
2573 SourceLocation getEndLoc() const { return Block->getEndLoc(); }
2574
2575 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
2576
2577 child_range children() {
2578 return child_range(&Block,&Block+1);
2579 }
2580
2581 static bool classof(const Stmt *T) {
2582 return T->getStmtClass() == SEHFinallyStmtClass;
2583 }
2584};
2585
2586class SEHTryStmt : public Stmt {
2587 friend class ASTReader;
2588 friend class ASTStmtReader;
2589
2590 bool IsCXXTry;
2591 SourceLocation TryLoc;
2592 Stmt *Children[2];
2593
2594 enum { TRY = 0, HANDLER = 1 };
2595
2596 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
2597 SourceLocation TryLoc,
2598 Stmt *TryBlock,
2599 Stmt *Handler);
2600
2601 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
2602
2603public:
2604 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
2605 SourceLocation TryLoc, Stmt *TryBlock,
2606 Stmt *Handler);
2607
2608 SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
2609
2610 SourceLocation getTryLoc() const { return TryLoc; }
2611 SourceLocation getEndLoc() const { return Children[HANDLER]->getEndLoc(); }
2612
2613 bool getIsCXXTry() const { return IsCXXTry; }
2614
2615 CompoundStmt* getTryBlock() const {
2616 return cast<CompoundStmt>(Children[TRY]);
2617 }
2618
2619 Stmt *getHandler() const { return Children[HANDLER]; }
2620
2621 /// Returns 0 if not defined
2622 SEHExceptStmt *getExceptHandler() const;
2623 SEHFinallyStmt *getFinallyHandler() const;
2624
2625 child_range children() {
2626 return child_range(Children, Children+2);
2627 }
2628
2629 static bool classof(const Stmt *T) {
2630 return T->getStmtClass() == SEHTryStmtClass;
2631 }
2632};
2633
2634/// Represents a __leave statement.
2635class SEHLeaveStmt : public Stmt {
2636 SourceLocation LeaveLoc;
2637
2638public:
2639 explicit SEHLeaveStmt(SourceLocation LL)
2640 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2641
2642 /// Build an empty __leave statement.
2643 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
2644
2645 SourceLocation getLeaveLoc() const { return LeaveLoc; }
2646 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2647
2648 SourceLocation getBeginLoc() const LLVM_READONLY { return LeaveLoc; }
2649 SourceLocation getEndLoc() const LLVM_READONLY { return LeaveLoc; }
2650
2651 static bool classof(const Stmt *T) {
2652 return T->getStmtClass() == SEHLeaveStmtClass;
2653 }
2654
2655 // Iterators
2656 child_range children() {
2657 return child_range(child_iterator(), child_iterator());
2658 }
2659};
2660
2661/// This captures a statement into a function. For example, the following
2662/// pragma annotated compound statement can be represented as a CapturedStmt,
2663/// and this compound statement is the body of an anonymous outlined function.
2664/// @code
2665/// #pragma omp parallel
2666/// {
2667/// compute();
2668/// }
2669/// @endcode
2670class CapturedStmt : public Stmt {
2671public:
2672 /// The different capture forms: by 'this', by reference, capture for
2673 /// variable-length array type etc.
2674 enum VariableCaptureKind {
2675 VCK_This,
2676 VCK_ByRef,
2677 VCK_ByCopy,
2678 VCK_VLAType,
2679 };
2680
2681 /// Describes the capture of either a variable, or 'this', or
2682 /// variable-length array type.
2683 class Capture {
2684 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2685 SourceLocation Loc;
2686
2687 public:
2688 friend class ASTStmtReader;
2689
2690 /// Create a new capture.
2691 ///
2692 /// \param Loc The source location associated with this capture.
2693 ///
2694 /// \param Kind The kind of capture (this, ByRef, ...).
2695 ///
2696 /// \param Var The variable being captured, or null if capturing this.
2697 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2698 VarDecl *Var = nullptr);
2699
2700 /// Determine the kind of capture.
2701 VariableCaptureKind getCaptureKind() const;
2702
2703 /// Retrieve the source location at which the variable or 'this' was
2704 /// first used.
2705 SourceLocation getLocation() const { return Loc; }
2706
2707 /// Determine whether this capture handles the C++ 'this' pointer.
2708 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2709
2710 /// Determine whether this capture handles a variable (by reference).
2711 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2712
2713 /// Determine whether this capture handles a variable by copy.
2714 bool capturesVariableByCopy() const {
2715 return getCaptureKind() == VCK_ByCopy;
2716 }
2717
2718 /// Determine whether this capture handles a variable-length array
2719 /// type.
2720 bool capturesVariableArrayType() const {
2721 return getCaptureKind() == VCK_VLAType;
2722 }
2723
2724 /// Retrieve the declaration of the variable being captured.
2725 ///
2726 /// This operation is only valid if this capture captures a variable.
2727 VarDecl *getCapturedVar() const;
2728 };
2729
2730private:
2731 /// The number of variable captured, including 'this'.
2732 unsigned NumCaptures;
2733
2734 /// The pointer part is the implicit the outlined function and the
2735 /// int part is the captured region kind, 'CR_Default' etc.
2736 llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
2737
2738 /// The record for captured variables, a RecordDecl or CXXRecordDecl.
2739 RecordDecl *TheRecordDecl = nullptr;
2740
2741 /// Construct a captured statement.
2742 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2743 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2744
2745 /// Construct an empty captured statement.
2746 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2747
2748 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2749
2750 Stmt *const *getStoredStmts() const {
2751 return reinterpret_cast<Stmt *const *>(this + 1);
2752 }
2753
2754 Capture *getStoredCaptures() const;
2755
2756 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2757
2758public:
2759 friend class ASTStmtReader;
2760
2761 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2762 CapturedRegionKind Kind,
2763 ArrayRef<Capture> Captures,
2764 ArrayRef<Expr *> CaptureInits,
2765 CapturedDecl *CD, RecordDecl *RD);
2766
2767 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2768 unsigned NumCaptures);
2769
2770 /// Retrieve the statement being captured.
2771 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2772 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2773
2774 /// Retrieve the outlined function declaration.
2775 CapturedDecl *getCapturedDecl();
2776 const CapturedDecl *getCapturedDecl() const;
2777
2778 /// Set the outlined function declaration.
2779 void setCapturedDecl(CapturedDecl *D);
2780
2781 /// Retrieve the captured region kind.
2782 CapturedRegionKind getCapturedRegionKind() const;
2783
2784 /// Set the captured region kind.
2785 void setCapturedRegionKind(CapturedRegionKind Kind);
2786
2787 /// Retrieve the record declaration for captured variables.
2788 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2789
2790 /// Set the record declaration for captured variables.
2791 void setCapturedRecordDecl(RecordDecl *D) {
2792 assert(D && "null RecordDecl");
2793 TheRecordDecl = D;
2794 }
2795
2796 /// True if this variable has been captured.
2797 bool capturesVariable(const VarDecl *Var) const;
2798
2799 /// An iterator that walks over the captures.
2800 using capture_iterator = Capture *;
2801 using const_capture_iterator = const Capture *;
2802 using capture_range = llvm::iterator_range<capture_iterator>;
2803 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
2804
2805 capture_range captures() {
2806 return capture_range(capture_begin(), capture_end());
2807 }
2808 capture_const_range captures() const {
2809 return capture_const_range(capture_begin(), capture_end());
2810 }
2811
2812 /// Retrieve an iterator pointing to the first capture.
2813 capture_iterator capture_begin() { return getStoredCaptures(); }
2814 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2815
2816 /// Retrieve an iterator pointing past the end of the sequence of
2817 /// captures.
2818 capture_iterator capture_end() const {
2819 return getStoredCaptures() + NumCaptures;
2820 }
2821
2822 /// Retrieve the number of captures, including 'this'.
2823 unsigned capture_size() const { return NumCaptures; }
2824
2825 /// Iterator that walks over the capture initialization arguments.
2826 using capture_init_iterator = Expr **;
2827 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
2828
2829 /// Const iterator that walks over the capture initialization
2830 /// arguments.
2831 using const_capture_init_iterator = Expr *const *;
2832 using const_capture_init_range =
2833 llvm::iterator_range<const_capture_init_iterator>;
2834
2835 capture_init_range capture_inits() {
2836 return capture_init_range(capture_init_begin(), capture_init_end());
2837 }
2838
2839 const_capture_init_range capture_inits() const {
2840 return const_capture_init_range(capture_init_begin(), capture_init_end());
2841 }
2842
2843 /// Retrieve the first initialization argument.
2844 capture_init_iterator capture_init_begin() {
2845 return reinterpret_cast<Expr **>(getStoredStmts());
2846 }
2847
2848 const_capture_init_iterator capture_init_begin() const {
2849 return reinterpret_cast<Expr *const *>(getStoredStmts());
2850 }
2851
2852 /// Retrieve the iterator pointing one past the last initialization
2853 /// argument.
2854 capture_init_iterator capture_init_end() {
2855 return capture_init_begin() + NumCaptures;
2856 }
2857
2858 const_capture_init_iterator capture_init_end() const {
2859 return capture_init_begin() + NumCaptures;
2860 }
2861
2862 SourceLocation getBeginLoc() const LLVM_READONLY {
2863 return getCapturedStmt()->getBeginLoc();
2864 }
2865
2866 SourceLocation getEndLoc() const LLVM_READONLY {
2867 return getCapturedStmt()->getEndLoc();
2868 }
2869
2870 SourceRange getSourceRange() const LLVM_READONLY {
2871 return getCapturedStmt()->getSourceRange();
2872 }
2873
2874 static bool classof(const Stmt *T) {
2875 return T->getStmtClass() == CapturedStmtClass;
2876 }
2877
2878 child_range children();
2879};
2880
2881} // namespace clang
2882
2883#endif // LLVM_CLANG_AST_STMT_H
2884