Esempio n. 1
0
CompoundStmt *TransformWCR::NewStmtList(StmtVector &Stmts) {
  CompoundStmt *CS = NewCompoundStmt(Stmts);
  CS->setIsStmtList(true);
  return CS;
}
Esempio n. 2
0
Stmt *TransformWCR::VisitForStmt(ForStmt *S) {
  StmtVector OuterBody;
  StmtVector BodyStmts;
  StmtVector WCRBody;

  // If Init of ForStmt has a DeclStmt, we make a CompoundStmt that encloses
  // an Init stmt and WhileStmt made from orginal ForStmt.
  bool HasDecl = false;

  // Init
  if (Stmt *Init = S->getInit()) {
    if (DeclStmt *DS = dyn_cast<DeclStmt>(Init)) {
      HasDecl = true;
      StmtVector tmpWCR;
      VisitRawDeclStmt(DS, OuterBody, tmpWCR);
      MergeBodyAndWCR(OuterBody, tmpWCR);
    } else {
      MergeBodyAndCond(OuterBody, Init);
    }
  }

  // Declaration of condition variable
  VarDecl *CondVD = NewCondVarDecl(ASTCtx.IntTy);
  CondDecls.push_back(NewDeclStmt(CondVD));

  // Computation of condition
  DeclRefExpr *LHS = new (ASTCtx) DeclRefExpr(CondVD,
      CondVD->getType(), VK_RValue, SourceLocation());
  Expr *Cond = S->getCond();
  if (!Cond) Cond = CLExprs.getExpr(CLExpressions::ONE);
  BinaryOperator *CondBinOp = new (ASTCtx) BinaryOperator(
      LHS, Cond, BO_Assign, LHS->getType(), 
      VK_RValue, OK_Ordinary, SourceLocation());

  // Cond WCR
  MergeBodyAndCond(BodyStmts, CondBinOp);

  // Exit condition
  DeclRefExpr *CondRef = new (ASTCtx) DeclRefExpr(CondVD,
      CondVD->getType(), VK_RValue, SourceLocation());
  UnaryOperator *NotCond = new (ASTCtx) UnaryOperator(
      CondRef, UO_LNot, CondVD->getType(), VK_RValue, OK_Ordinary,
      SourceLocation());
  BreakStmt *ThenStmt = new (ASTCtx) BreakStmt(SourceLocation());
  IfStmt *ExitStmt = new (ASTCtx) IfStmt(ASTCtx, SourceLocation(), 
                                         NULL, NotCond, ThenStmt);
  BodyStmts.push_back(ExitStmt);
  
  // Body
  Stmt *Body = S->getBody();
  CompoundStmt *CS = dyn_cast<CompoundStmt>(Body);
  assert(CS && "Not a CompoundStmt");

  // Identify each WCR
  VisitRawCompoundStmt(CS, BodyStmts, WCRBody);
  MergeBodyAndWCR(BodyStmts, WCRBody);

  // Inc
  if (Expr *Inc = S->getInc()) MergeBodyAndCond(BodyStmts, Inc);

  ASTCtx.Deallocate(S);
  ASTCtx.Deallocate(CS);

  // Outer WhileStmt 
  Cond = CLExprs.getExpr(CLExpressions::ONE);
  Body = MergeWCRsAndMakeCompoundStmt(BodyStmts);
  WhileStmt *WS = new (ASTCtx) WhileStmt(ASTCtx, NULL, Cond, Body,
                                         SourceLocation());
  OuterBody.push_back(WS);

  // Landing pad - empty WCR
  OuterBody.push_back(NewLandingPad(WS));

  CompoundStmt *Out = NewCompoundStmt(OuterBody);
  if (!HasDecl) Out->setIsStmtList(true);
  return Out;
}