bool Expression::ReplaceAll(const ExprVector &rules, Calculator *calculator, bool *changed) { for(ExprVector::const_iterator rule = rules.begin(); rule != rules.end(); ++rule) if(Replace(*rule, calculator, changed)) return true; for(ExprVector::const_iterator leaf = leaves.begin(); leaf != leaves.end(); ++leaf) if((*leaf)->ReplaceAll(rules, calculator, changed)) return true; return false; }
void Expression::SubstituteSlots(const ExprVector &slots) { string functionName = FunctionName(); if(functionName == "Slot") { IntegerType index; if(leaves.empty()) index = 1; else { //MachineInteger *indexInt = leaves.at(0)->MachineIntegerHead(); Integer *indexInt(dynamic_cast<Integer*>(leaves.at(0)->NumberHead())); if(indexInt) index = indexInt->IntValue(); else throw EvaluateException("Slot expected to have an Integer argument."); } Expression *slot; if(index > 0 && index-1 < slots.size()) slot = slots.at(static_cast<ExprVector::size_type>(index-1)); else throw EvaluateException("Slot not given."); AssignCloned(slot); } else if(functionName == "SlotSequence") { delete head; head = new Expression("Sequence"); DeleteLeaves(); leaves.reserve(slots.size()); for(ExprVector::const_iterator leaf = slots.begin(); leaf != slots.end(); ++leaf) AppendLeaf((*leaf)->Clone()); } else if(functionName != "Function") { if(head) head->SubstituteSlots(slots); for(ExprVector::const_iterator leaf = leaves.begin(); leaf != leaves.end(); ++leaf) (*leaf)->SubstituteSlots(slots); } }
void ReduceArrayDim::rewriteSubscriptExpr(const ExprVector &IdxExprs) { ExprVector::const_iterator I = IdxExprs.begin(); const Expr *LastE = (*I); ++I; const Expr *SecE = (*I); RewriteHelper->removeArraySubscriptExpr(LastE); int LastIdx = -1; int SecIdx = -1; if (isIntegerExpr(LastE)) LastIdx = getIndexAsInteger(LastE); if (isIntegerExpr(SecE)) SecIdx = getIndexAsInteger(SecE); if ((LastIdx >= 0) && (SecIdx >= 0)) { int NewIdx = (SecIdx * ArraySz + LastIdx); std::stringstream TmpSS; TmpSS << NewIdx; RewriteHelper->replaceExpr(SecE, TmpSS.str()); return; } std::string LastStr, SecStr, newStr; RewriteHelper->getExprString(LastE, LastStr); RewriteHelper->getExprString(SecE, SecStr); std::stringstream TmpSS; if (ArraySz == 1) { TmpSS << SecStr << "+" << LastStr; } else if (SecIdx == 1) { TmpSS << ArraySz << "+" << LastStr; } else { TmpSS << "(" << SecStr << ")*" << ArraySz << "+" << LastStr; } RewriteHelper->replaceExpr(SecE, TmpSS.str()); }
void RemoveUnusedStructField::handleOneVarDecl(const VarDecl *VD) { const Type *Ty = VD->getType().getTypePtr(); const RecordDecl *RD = getBaseRecordDef(Ty); if (!RD) return; IndexVector *IdxVec = RecordDeclToField[RD]; if (!IdxVec) return; const Expr *InitE = VD->getInit(); TransAssert(InitE && "Need initializer!"); ExprVector InitExprs; getInitExprs(Ty, InitE, IdxVec, InitExprs); for (ExprVector::iterator I = InitExprs.begin(), E = InitExprs.end(); I != E; ++I) { removeOneInitExpr(*I); } }