Package: IotaStrategy

IotaStrategy

nameinstructionbranchcomplexitylinemethod
IotaStrategy(TransAssistantIR, SExpIR, String, String, ILanguageIterator, ITempVarGen, IterationVarPrefixes, CounterData)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
consCounterDecl(IRInfo, AIdentifierPatternIR)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
consIotaError(String)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
consIotaMultipleResultsError()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
consIotaNoMatchError()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getForLoopStms(AIdentifierVarExpIR, List, SPatternIR)
M: 13 C: 143
92%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 30
94%
M: 0 C: 1
100%
getOuterBlockDecls(AIdentifierVarExpIR, List)
M: 2 C: 55
96%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 12
92%
M: 0 C: 1
100%
getPostOuterBlockStms(AIdentifierVarExpIR, List)
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans.iota;
2:
3: import org.apache.log4j.Logger;
4: import org.overture.ast.expressions.AIotaExp;
5: import org.overture.ast.patterns.AIdentifierPattern;
6: import org.overture.codegen.ir.*;
7: import org.overture.codegen.ir.analysis.AnalysisException;
8: import org.overture.codegen.ir.declarations.AVarDeclIR;
9: import org.overture.codegen.ir.expressions.*;
10: import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
11: import org.overture.codegen.ir.statements.*;
12: import org.overture.codegen.ir.types.ABoolBasicTypeIR;
13: import org.overture.codegen.ir.types.AErrorTypeIR;
14: import org.overture.codegen.ir.types.AIntNumericBasicTypeIR;
15: import org.overture.codegen.ir.types.ANatNumericBasicTypeIR;
16: import org.overture.codegen.trans.AbstractIterationStrategy;
17: import org.overture.codegen.trans.IterationVarPrefixes;
18: import org.overture.codegen.trans.assistants.TransAssistantIR;
19: import org.overture.codegen.trans.iterator.ILanguageIterator;
20: import org.overture.codegen.trans.quantifier.CounterData;
21:
22: import java.util.LinkedList;
23: import java.util.List;
24:
25: public class IotaStrategy extends AbstractIterationStrategy {
26:
27: protected Logger log = Logger.getLogger(this.getClass().getName());
28:
29: protected String resultVarName;
30: protected String counterName;
31: protected SExpIR predicate;
32:
33: protected static final String MULTIPLE_RESULTS_ERROR_MSG = "Iota selects more than one result";
34: protected static final String NO_RESULT_ERROR_MSG = "Iota does not select a result";
35:
36: protected CounterData counterData;
37:
38: public IotaStrategy(TransAssistantIR transformationAssistant, SExpIR predicate, String resultVarName, String counterName, ILanguageIterator langIterator, ITempVarGen tempGen, IterationVarPrefixes iteVarPrefixes, CounterData counterData) {
39: super(transformationAssistant, langIterator, tempGen, iteVarPrefixes);
40: this.resultVarName = resultVarName;
41: this.counterName = counterName;
42: this.predicate = predicate;
43: this.counterData = counterData;
44: }
45:
46: @Override
47: public List<AVarDeclIR> getOuterBlockDecls(AIdentifierVarExpIR setVar,
48: List<SPatternIR> patterns) throws AnalysisException
49: {
50:• if (firstBind)
51: {
52: IRInfo info = transAssist.getInfo();
53:
54: AIdentifierPatternIR resId = new AIdentifierPatternIR();
55: resId.setName(resultVarName);
56:
57: List<AVarDeclIR> decls = new LinkedList<>();
58:
59: STypeIR elemType = transAssist.getElementType(setVar.getType());
60:
61: AUndefinedExpIR initExp = info.getExpAssistant().consUndefinedExp();
62: decls.add(info.getDeclAssistant().consLocalVarDecl(elemType, resId, initExp));
63:
64: AIdentifierPatternIR countId = new AIdentifierPatternIR();
65: countId.setName(counterName);
66:
67: decls.add(consCounterDecl(info, countId));
68:
69: return decls;
70: } else
71: {
72: return null;
73: }
74: }
75:
76: public AVarDeclIR consCounterDecl(IRInfo info, AIdentifierPatternIR countId) {
77:
78: return transAssist.getInfo().getDeclAssistant().consLocalVarDecl(counterData.getType().clone(), transAssist.getInfo().getPatternAssistant().consIdPattern(counterName), counterData.getExp().clone());
79: }
80:
81: @Override
82: public List<SStmIR> getForLoopStms(AIdentifierVarExpIR setVar,
83: List<SPatternIR> patterns, SPatternIR pattern)
84: {
85:• if(lastBind)
86: {
87: AIdentifierVarExpIR col = new AIdentifierVarExpIR();
88: col.setType(new AIntNumericBasicTypeIR());
89: col.setIsLambda(false);
90: col.setIsLocal(true);
91: col.setName(counterName);
92:
93: AIncrementStmIR inc = new AIncrementStmIR();
94: inc.setVar(col);
95:
96: AGreaterNumericBinaryExpIR tooManyMatches = new AGreaterNumericBinaryExpIR();
97: tooManyMatches.setType(new ABoolBasicTypeIR());
98: tooManyMatches.setLeft(transAssist.getInfo().getExpAssistant().consIdVar(counterName, new ANatNumericBasicTypeIR()));
99: tooManyMatches.setRight(transAssist.getInfo().getExpAssistant().consIntLiteral(1));
100:
101: String name = null;
102:• if(pattern instanceof AIdentifierPatternIR)
103: {
104: name = ((AIdentifierPatternIR) pattern).getName();
105: }
106: else
107: {
108: log.error("Expected pattern to be an identifier at this point. Got: " + pattern);
109: }
110:
111: AAssignToExpStmIR resultAssign = new AAssignToExpStmIR();
112: STypeIR elementType = transAssist.getElementType(setVar.getType());
113: resultAssign.setTarget(transAssist.getInfo().getExpAssistant().consIdVar(resultVarName, elementType));
114: resultAssign.setExp(transAssist.getInfo().getExpAssistant().consIdVar(name, elementType.clone()));
115:
116: AIfStmIR matchesCheck = new AIfStmIR();
117: matchesCheck.setIfExp(tooManyMatches);
118: matchesCheck.setThenStm(consIotaMultipleResultsError());
119: matchesCheck.setElseStm(resultAssign);
120:
121: ABlockStmIR outerThen = new ABlockStmIR();
122: outerThen.getStatements().add(inc);
123: outerThen.getStatements().add(matchesCheck);
124:
125: AIfStmIR outerIf = new AIfStmIR();
126: outerIf.setIfExp(predicate);
127: outerIf.setThenStm(outerThen);
128:
129: return packStm(outerIf);
130: }
131: else
132: {
133: return null;
134: }
135: }
136:
137: @Override
138: public List<SStmIR> getPostOuterBlockStms(AIdentifierVarExpIR setVar,
139: List<SPatternIR> patterns)
140: {
141: AEqualsBinaryExpIR noMatches = new AEqualsBinaryExpIR();
142: noMatches.setType(new ABoolBasicTypeIR());
143: noMatches.setLeft(transAssist.getInfo().getExpAssistant().consIdVar(counterName, new AIntNumericBasicTypeIR()));
144: noMatches.setRight(transAssist.getInfo().getExpAssistant().consIntLiteral(0));
145:
146: AIfStmIR ifStm = new AIfStmIR();
147: ifStm.setIfExp(noMatches);
148: ifStm.setThenStm(consIotaMultipleResultsError());
149:
150: return packStm(ifStm);
151: }
152:
153: public SStmIR consIotaNoMatchError()
154: {
155: return consIotaError(NO_RESULT_ERROR_MSG);
156: }
157:
158: public SStmIR consIotaMultipleResultsError()
159: {
160: return consIotaError(MULTIPLE_RESULTS_ERROR_MSG);
161: }
162:
163: public SStmIR consIotaError(String msg)
164: {
165: AIotaRuntimeErrorExpIR error = new AIotaRuntimeErrorExpIR();
166: error.setType(new AErrorTypeIR());
167: error.setMessage(msg);
168:
169: ARaiseErrorStmIR raise = new ARaiseErrorStmIR();
170: raise.setError(error);
171:
172: return raise;
173: }
174: }