Warning: That file was not part of the compilation database. It may have many parsing errors.

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

Warning: That file was not part of the compilation database. It may have many parsing errors.