diff options
Diffstat (limited to 'include/clang/AST/Stmt.h')
-rw-r--r-- | include/clang/AST/Stmt.h | 162 |
1 files changed, 82 insertions, 80 deletions
diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 9662f1e4d1..1254926fac 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -182,6 +182,9 @@ protected: }; union { + // FIXME: this is wasteful on 64-bit platforms. + void *Aligner; + StmtBitfields StmtBits; CompoundStmtBitfields CompoundStmtBits; LabelStmtBitfields LabelStmtBits; @@ -232,7 +235,6 @@ public: StmtBits.sClass = SC; if (Stmt::CollectingStats()) Stmt::addStmtClass(SC); } - virtual ~Stmt() {} StmtClass getStmtClass() const { return static_cast<StmtClass>(StmtBits.sClass); @@ -242,7 +244,8 @@ public: /// SourceLocation tokens are not useful in isolation - they are low level /// value objects created/interpreted by SourceManager. We assume AST /// clients will have a pointer to the respective SourceManager. - virtual SourceRange getSourceRange() const = 0; + SourceRange getSourceRange() const; + SourceLocation getLocStart() const { return getSourceRange().getBegin(); } SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } @@ -289,22 +292,25 @@ public: /// within CFGs. bool hasImplicitControlFlow() const; - /// Child Iterators: All subclasses must implement child_begin and child_end - /// to permit easy iteration over the substatements/subexpessions of an - /// AST node. This permits easy iteration over all nodes in the AST. + /// Child Iterators: All subclasses must implement 'children' + /// to permit easy iteration over the substatements/subexpessions of an + /// AST node. This permits easy iteration over all nodes in the AST. typedef StmtIterator child_iterator; typedef ConstStmtIterator const_child_iterator; - virtual child_iterator child_begin() = 0; - virtual child_iterator child_end() = 0; + typedef StmtRange child_range; + typedef ConstStmtRange const_child_range; - const_child_iterator child_begin() const { - return const_child_iterator(const_cast<Stmt*>(this)->child_begin()); + child_range children(); + const_child_range children() const { + return const_cast<Stmt*>(this)->children(); } - const_child_iterator child_end() const { - return const_child_iterator(const_cast<Stmt*>(this)->child_end()); - } + child_iterator child_begin() { return children().first; } + child_iterator child_end() { return children().second; } + + const_child_iterator child_begin() const { return children().first; } + const_child_iterator child_end() const { return children().second; } /// \brief Produce a unique representation of the given statement. /// @@ -367,8 +373,10 @@ public: static bool classof(const DeclStmt *) { return true; } // Iterators over subexpressions. - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + return child_range(child_iterator(DG.begin(), DG.end()), + child_iterator(DG.end(), DG.end())); + } typedef DeclGroupRef::iterator decl_iterator; typedef DeclGroupRef::const_iterator const_decl_iterator; @@ -402,16 +410,14 @@ public: bool hasLeadingEmptyMacro() const { return LeadingEmptyMacro; } - virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); } + SourceRange getSourceRange() const { return SourceRange(SemiLoc); } static bool classof(const Stmt *T) { return T->getStmtClass() == NullStmtClass; } static bool classof(const NullStmt *) { return true; } - // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { return child_range(); } friend class ASTStmtReader; friend class ASTStmtWriter; @@ -482,7 +488,7 @@ public: return const_reverse_body_iterator(body_begin()); } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(LBracLoc, RBracLoc); } @@ -497,8 +503,9 @@ public: static bool classof(const CompoundStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts); + } }; // SwitchCase is the base class for CaseStmt and DefaultStmt, @@ -517,17 +524,15 @@ public: void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } - Stmt *getSubStmt() { return v_getSubStmt(); } + Stmt *getSubStmt(); - virtual SourceRange getSourceRange() const { return SourceRange(); } + SourceRange getSourceRange() const { return SourceRange(); } static bool classof(const Stmt *T) { return T->getStmtClass() == CaseStmtClass || - T->getStmtClass() == DefaultStmtClass; + T->getStmtClass() == DefaultStmtClass; } static bool classof(const SwitchCase *) { return true; } -protected: - virtual Stmt* v_getSubStmt() = 0; }; class CaseStmt : public SwitchCase { @@ -537,8 +542,6 @@ class CaseStmt : public SwitchCase { SourceLocation CaseLoc; SourceLocation EllipsisLoc; SourceLocation ColonLoc; - - virtual Stmt* v_getSubStmt() { return getSubStmt(); } public: CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc, SourceLocation ellipsisLoc, SourceLocation colonLoc) @@ -578,7 +581,7 @@ public: void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { // Handle deeply nested case statements with iteration instead of recursion. const CaseStmt *CS = this; while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt())) @@ -592,15 +595,15 @@ public: static bool classof(const CaseStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + return child_range(&SubExprs[0], &SubExprs[END_EXPR]); + } }; class DefaultStmt : public SwitchCase { Stmt* SubStmt; SourceLocation DefaultLoc; SourceLocation ColonLoc; - virtual Stmt* v_getSubStmt() { return getSubStmt(); } public: DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) : SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL), @@ -618,7 +621,7 @@ public: SourceLocation getColonLoc() const { return ColonLoc; } void setColonLoc(SourceLocation L) { ColonLoc = L; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(DefaultLoc, SubStmt->getLocEnd()); } static bool classof(const Stmt *T) { @@ -627,8 +630,7 @@ public: static bool classof(const DefaultStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { return child_range(&SubStmt, &SubStmt+1); } }; class LabelStmt : public Stmt { @@ -665,17 +667,15 @@ public: bool HasUnusedAttribute() const { return LabelStmtBits.HasUnusedAttr; } void setUnusedAttribute(bool U) { LabelStmtBits.HasUnusedAttr = U; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(IdentLoc, SubStmt->getLocEnd()); } + child_range children() { return child_range(&SubStmt, &SubStmt+1); } + static bool classof(const Stmt *T) { return T->getStmtClass() == LabelStmtClass; } static bool classof(const LabelStmt *) { return true; } - - // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); }; @@ -722,22 +722,23 @@ public: SourceLocation getElseLoc() const { return ElseLoc; } void setElseLoc(SourceLocation L) { ElseLoc = L; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { if (SubExprs[ELSE]) return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); else return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); } + // Iterators over subexpressions. The iterators will include iterating + // over the initialization expression referenced by the condition variable. + child_range children() { + return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); + } + static bool classof(const Stmt *T) { return T->getStmtClass() == IfStmtClass; } static bool classof(const IfStmt *) { return true; } - - // Iterators over subexpressions. The iterators will include iterating - // over the initialization expression referenced by the condition variable. - virtual child_iterator child_begin(); - virtual child_iterator child_end(); }; /// SwitchStmt - This represents a 'switch' stmt. @@ -813,17 +814,18 @@ public: return (bool) AllEnumCasesCovered; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd()); } + // Iterators + child_range children() { + return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); + } + static bool classof(const Stmt *T) { return T->getStmtClass() == SwitchStmtClass; } static bool classof(const SwitchStmt *) { return true; } - - // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); }; @@ -861,7 +863,7 @@ public: SourceLocation getWhileLoc() const { return WhileLoc; } void setWhileLoc(SourceLocation L) { WhileLoc = L; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); } static bool classof(const Stmt *T) { @@ -870,8 +872,9 @@ public: static bool classof(const WhileStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); + } }; /// DoStmt - This represents a 'do/while' stmt. @@ -909,7 +912,7 @@ public: SourceLocation getRParenLoc() const { return RParenLoc; } void setRParenLoc(SourceLocation L) { RParenLoc = L; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(DoLoc, RParenLoc); } static bool classof(const Stmt *T) { @@ -918,8 +921,9 @@ public: static bool classof(const DoStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); + } }; @@ -974,7 +978,7 @@ public: SourceLocation getRParenLoc() const { return RParenLoc; } void setRParenLoc(SourceLocation L) { RParenLoc = L; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); } static bool classof(const Stmt *T) { @@ -983,8 +987,9 @@ public: static bool classof(const ForStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); + } }; /// GotoStmt - This represents a direct goto. @@ -1008,7 +1013,7 @@ public: SourceLocation getLabelLoc() const { return LabelLoc; } void setLabelLoc(SourceLocation L) { LabelLoc = L; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(GotoLoc, LabelLoc); } static bool classof(const Stmt *T) { @@ -1017,8 +1022,7 @@ public: static bool classof(const GotoStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { return child_range(); } }; /// IndirectGotoStmt - This represents an indirect goto. @@ -1053,7 +1057,7 @@ public: return const_cast<IndirectGotoStmt*>(this)->getConstantTarget(); } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(GotoLoc, Target->getLocEnd()); } @@ -1063,8 +1067,7 @@ public: static bool classof(const IndirectGotoStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { return child_range(&Target, &Target+1); } }; @@ -1081,7 +1084,7 @@ public: SourceLocation getContinueLoc() const { return ContinueLoc; } void setContinueLoc(SourceLocation L) { ContinueLoc = L; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(ContinueLoc); } @@ -1091,8 +1094,7 @@ public: static bool classof(const ContinueStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { return child_range(); } }; /// BreakStmt - This represents a break. @@ -1108,7 +1110,7 @@ public: SourceLocation getBreakLoc() const { return BreakLoc; } void setBreakLoc(SourceLocation L) { BreakLoc = L; } - virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); } + SourceRange getSourceRange() const { return SourceRange(BreakLoc); } static bool classof(const Stmt *T) { return T->getStmtClass() == BreakStmtClass; @@ -1116,8 +1118,7 @@ public: static bool classof(const BreakStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { return child_range(); } }; @@ -1161,7 +1162,7 @@ public: const VarDecl *getNRVOCandidate() const { return NRVOCandidate; } void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; } - virtual SourceRange getSourceRange() const; + SourceRange getSourceRange() const; static bool classof(const Stmt *T) { return T->getStmtClass() == ReturnStmtClass; @@ -1169,8 +1170,10 @@ public: static bool classof(const ReturnStmt *) { return true; } // Iterators - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + if (RetExpr) return child_range(&RetExpr, &RetExpr+1); + return child_range(); + } }; /// AsmStmt - This represents a GNU inline-assembly statement extension. @@ -1368,7 +1371,7 @@ public: StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } - virtual SourceRange getSourceRange() const { + SourceRange getSourceRange() const { return SourceRange(AsmLoc, RParenLoc); } @@ -1415,10 +1418,9 @@ public: return &Exprs[0] + NumOutputs; } - // Child iterators - - virtual child_iterator child_begin(); - virtual child_iterator child_end(); + child_range children() { + return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs); + } }; } // end namespace clang |