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