00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "ifexpression.h"
00036
00037 IfExpression::IfExpression(CondExpression * _cond, Expression * _thenexp, Expression * _elseexp):
00038 Expression(et_if), cond(_cond), thenexp(_thenexp), elseexp(_elseexp) {
00039 }
00040
00041 IfExpression::~IfExpression() {
00042 delete cond;
00043 delete thenexp;
00044 delete elseexp;
00045 }
00046
00047 void IfExpression::scopeCheck(vector < unsigned >&def_vars,
00048 vector < unsigned >&introduced_vars,
00049 vector < unsigned >&violating_vars) {
00050 cond->scopeCheck(def_vars, introduced_vars, violating_vars);
00051 thenexp->scopeCheck(def_vars, introduced_vars, violating_vars);
00052 elseexp->scopeCheck(def_vars, introduced_vars, violating_vars);
00053 }
00054
00055 void IfExpression::replaceVarId(unsigned old_id, unsigned new_id) {
00056 cond->replaceVarId(old_id, new_id);
00057 thenexp->replaceVarId(old_id, new_id);
00058 elseexp->replaceVarId(old_id, new_id);
00059 }
00060
00061 void IfExpression::mergeSequences() {
00062 cond->mergeSequences();
00063 thenexp->mergeSequences();
00064 elseexp->mergeSequences();
00065 }
00066
00067 void IfExpression::extractFSAMap(FSAMap * fsamap, unsigned parent_var) {
00068 cond->extractFSAMap(fsamap, parent_var);
00069 thenexp->extractFSAMap(fsamap, parent_var);
00070 elseexp->extractFSAMap(fsamap, parent_var);
00071 }
00072
00073 void IfExpression::extractParVarMap(ParVarMap * parvarmap) {
00074 cond->extractParVarMap(parvarmap);
00075 thenexp->extractParVarMap(parvarmap);
00076 elseexp->extractParVarMap(parvarmap);
00077 }
00078
00079 void IfExpression::extractDependencies(vector < DependencySet * >*depset) {
00080 cond->extractDependencies(depset);
00081 thenexp->extractDependencies(depset);
00082 elseexp->extractDependencies(depset);
00083 }
00084
00085 Expression *IfExpression::placeSignOffs(vector < SignOffExpression * >&signoffs) {
00086 cond = (CondExpression *) cond->placeSignOffs(signoffs);
00087 thenexp = thenexp->placeSignOffs(signoffs);
00088 elseexp = elseexp->placeSignOffs(signoffs);
00089
00090 return this;
00091 }
00092
00093 void IfExpression::rewriteWhereExps() {
00094 cond->rewriteWhereExps();
00095 thenexp->rewriteWhereExps();
00096 elseexp->rewriteWhereExps();
00097 }
00098
00099 void IfExpression::rewriteEmptyFuncts() {
00100 cond->rewriteEmptyFuncts();
00101 thenexp->rewriteEmptyFuncts();
00102 elseexp->rewriteEmptyFuncts();
00103 }
00104
00105 void IfExpression::rewriteVarstepCondExps() {
00106 cond->rewriteVarstepCondExps();
00107 thenexp->rewriteVarstepCondExps();
00108 elseexp->rewriteVarstepCondExps();
00109 }
00110
00111 void IfExpression::rewriteAggregateFuncts() {
00112 cond->rewriteAggregateFuncts();
00113 thenexp->rewriteAggregateFuncts();
00114 elseexp->rewriteAggregateFuncts();
00115 }
00116
00117 void IfExpression::rewriteVarsteps() {
00118 if (thenexp->getType() == et_varstep) {
00119 VarStepExpression *sub = ((VarStepExpression *) thenexp)->clone();
00120 delete thenexp;
00121 VarExpression *tmpvar =
00122 new VarExpression(VarName::getInstance()->getFreshVarname(),
00123 true);
00124 ForExpression *forexp = new ForExpression(tmpvar, sub, tmpvar->clone());
00125
00126 thenexp = forexp;
00127 } else {
00128 thenexp->rewriteVarsteps();
00129 }
00130
00131 if (elseexp->getType() == et_varstep) {
00132 VarStepExpression *sub = ((VarStepExpression *) elseexp)->clone();
00133 delete elseexp;
00134 VarExpression *tmpvar =
00135 new VarExpression(VarName::getInstance()->getFreshVarname(),
00136 true);
00137 ForExpression *forexp = new ForExpression(tmpvar, sub, tmpvar->clone());
00138
00139 elseexp = forexp;
00140 } else {
00141 elseexp->rewriteVarsteps();
00142 }
00143 }
00144
00145 void IfExpression::print(OutputStream & dos) const {
00146 dos << "if (" << (*cond) << ")" << NEWLINE << incrementIndents() <<
00147 writeIndents();
00148
00149 dos << "then " << NEWLINE << incrementIndents() << writeIndents();
00150
00151 dos << (*thenexp) << NEWLINE << decrementIndents() << writeIndents();
00152
00153 dos << "else " << NEWLINE << incrementIndents() << writeIndents();
00154
00155 dos << (*elseexp) << decrementIndents() << decrementIndents();
00156 }
00157
00158 void IfExpression::init(BufferNode * root) {
00159 cond->init(root);
00160 thenexp->init(root);
00161 elseexp->init(root);
00162 }
00163
00164 void IfExpression::eval(OutputStream & eos, Environment * env, unsigned modus) {
00165
00166 switch (modus) {
00167
00168 case EVAL_QUERY:
00169
00170
00171
00172 if (cond->evalCond(eos, env, modus)) {
00173 thenexp->eval(eos, env, EVAL_QUERY);
00174 elseexp->eval(eos, env, EVAL_SIGNOFF);
00175 } else {
00176 elseexp->eval(eos, env, EVAL_QUERY);
00177 thenexp->eval(eos, env, EVAL_SIGNOFF);
00178 }
00179 break;
00180
00181 case EVAL_SIGNOFF:
00182 cond->eval(eos, env, EVAL_SIGNOFF);
00183 thenexp->eval(eos, env, EVAL_SIGNOFF);
00184 elseexp->eval(eos, env, EVAL_SIGNOFF);
00185 break;
00186
00187 default:
00188 throw RuntimeException("IfExpression: Illegal Evaluation Mode",
00189 eid_runtime_illegalmode);
00190 break;
00191 }
00192 }
00193
00194 bool IfExpression::containsDirectOutput() {
00195
00196
00197 switch (thenexp->getType()) {
00198 case et_nodeconstr:
00199 case et_stringconst:
00200 return true;
00201 case et_numericconst:
00202 return true;
00203 case et_sequence:
00204 case et_if:
00205 if (thenexp->containsDirectOutput()) {
00206 return true;
00207 }
00208 default:
00209 break;
00210 }
00211
00212
00213 switch (elseexp->getType()) {
00214 case et_nodeconstr:
00215 case et_stringconst:
00216 return true;
00217 case et_numericconst:
00218 return true;
00219 case et_sequence:
00220 case et_if:
00221 if (elseexp->containsDirectOutput()) {
00222 return true;
00223 }
00224 default:
00225 break;
00226 }
00227
00228
00229 return false;
00230 }