Package: LetBeStStrategy

LetBeStStrategy

nameinstructionbranchcomplexitylinemethod
LetBeStStrategy(TransAssistantIR, SExpIR, STypeIR, ILanguageIterator, ITempVarGen, IterationVarPrefixes)
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
consDeclarationTag()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getForLoopCond(AIdentifierVarExpIR, List, SPatternIR)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getForLoopStms(AIdentifierVarExpIR, List, SPatternIR)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getNextElementAssigned(AIdentifierVarExpIR, List, SPatternIR)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getNextElementDeclared(AIdentifierVarExpIR, List, SPatternIR)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getOuterBlockDecls(AIdentifierVarExpIR, List)
M: 0 C: 60
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getPostOuterBlockStms(AIdentifierVarExpIR, List)
M: 0 C: 35
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getPreForLoopStms(AIdentifierVarExpIR, List, SPatternIR)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.trans.let;
23:
24: import java.util.LinkedList;
25: import java.util.List;
26:
27: import org.overture.codegen.ir.ITempVarGen;
28: import org.overture.codegen.ir.SExpIR;
29: import org.overture.codegen.ir.SPatternIR;
30: import org.overture.codegen.ir.SStmIR;
31: import org.overture.codegen.ir.STypeIR;
32: import org.overture.codegen.ir.analysis.AnalysisException;
33: import org.overture.codegen.ir.declarations.AVarDeclIR;
34: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
35: import org.overture.codegen.ir.expressions.ALetBeStNoBindingRuntimeErrorExpIR;
36: import org.overture.codegen.ir.statements.AAssignToExpStmIR;
37: import org.overture.codegen.ir.statements.AIfStmIR;
38: import org.overture.codegen.ir.statements.ALocalPatternAssignmentStmIR;
39: import org.overture.codegen.ir.statements.ARaiseErrorStmIR;
40: import org.overture.codegen.ir.types.AErrorTypeIR;
41: import org.overture.codegen.trans.AbstractIterationStrategy;
42: import org.overture.codegen.trans.DeclarationTag;
43: import org.overture.codegen.trans.IterationVarPrefixes;
44: import org.overture.codegen.trans.assistants.TransAssistantIR;
45: import org.overture.codegen.trans.iterator.ILanguageIterator;
46:
47: public class LetBeStStrategy extends AbstractIterationStrategy
48: {
49:         protected String successVarName;
50:         protected SExpIR suchThat;
51:         protected STypeIR setSeqType;
52:
53:         protected int count = 0;
54:         protected List<AVarDeclIR> decls = new LinkedList<AVarDeclIR>();
55:
56:         public LetBeStStrategy(TransAssistantIR transformationAssistant,
57:                         SExpIR suchThat, STypeIR setSeqType, ILanguageIterator langIterator,
58:                         ITempVarGen tempGen, IterationVarPrefixes iteVarPrefixes)
59:         {
60:                 super(transformationAssistant, langIterator, tempGen, iteVarPrefixes);
61:
62:                 String successVarNamePrefix = iteVarPrefixes.success();
63:                 ITempVarGen tempVarNameGen = transformationAssistant.getInfo().getTempVarNameGen();
64:
65:                 this.successVarName = tempVarNameGen.nextVarName(successVarNamePrefix);
66:                 this.suchThat = suchThat;
67:                 this.setSeqType = setSeqType;
68:         }
69:
70:         @Override
71:         public List<AVarDeclIR> getOuterBlockDecls(AIdentifierVarExpIR setVar,
72:                         List<SPatternIR> patterns) throws AnalysisException
73:         {
74:                 List<AVarDeclIR> outerBlockDecls = new LinkedList<AVarDeclIR>();
75:
76:                 STypeIR elementType = transAssist.getElementType(setSeqType);
77:
78:•                for (SPatternIR id : patterns)
79:                 {
80:                         AVarDeclIR decl = transAssist.getInfo().getDeclAssistant().consLocalVarDecl(elementType.clone(), id.clone(), transAssist.getInfo().getExpAssistant().consUndefinedExp());
81:                         decls.add(decl);
82:                         outerBlockDecls.add(decl);
83:                 }
84:
85:                 successVarDecl = transAssist.consBoolVarDecl(successVarName, false);
86:                 outerBlockDecls.add(successVarDecl);
87:
88:                 return outerBlockDecls;
89:         }
90:
91:         @Override
92:         public List<SStmIR> getPreForLoopStms(AIdentifierVarExpIR setVar,
93:                         List<SPatternIR> patterns, SPatternIR pattern)
94:         {
95:•                if (count > 0)
96:                 {
97:                         AAssignToExpStmIR successAssignment = new AAssignToExpStmIR();
98:                         successAssignment.setExp(transAssist.getInfo().getExpAssistant().consBoolLiteral(false));
99:                         successAssignment.setTarget(transAssist.consSuccessVar(successVarName));
100:
101:                         return packStm(successAssignment);
102:                 } else
103:                 {
104:                         return null;
105:                 }
106:         }
107:
108:         @Override
109:         public SExpIR getForLoopCond(AIdentifierVarExpIR setVar,
110:                         List<SPatternIR> patterns, SPatternIR pattern)
111:                         throws AnalysisException
112:         {
113:                 SExpIR left = langIterator.getForLoopCond(setVar, patterns, pattern);
114:                 SExpIR right = transAssist.consBoolCheck(successVarName, true);
115:
116:                 return transAssist.consAndExp(left, right);
117:         }
118:
119:         @Override
120:         public DeclarationTag consDeclarationTag()
121:         {
122:                 return new DeclarationTag(true, successVarDecl);
123:         }
124:
125:         @Override
126:         public AVarDeclIR getNextElementDeclared(AIdentifierVarExpIR setVar,
127:                         List<SPatternIR> patterns, SPatternIR pattern)
128:                         throws AnalysisException
129:         {
130:                 AVarDeclIR nextElementDecl = decls.get(count++);
131:                 tagNextElementDeclared(nextElementDecl);
132:                 return null;
133:         }
134:
135:         @Override
136:         public ALocalPatternAssignmentStmIR getNextElementAssigned(
137:                         AIdentifierVarExpIR setVar, List<SPatternIR> patterns,
138:                         SPatternIR pattern) throws AnalysisException
139:         {
140:                 return langIterator.getNextElementAssigned(setVar, patterns, pattern, successVarDecl, this.nextElementDeclared);
141:         }
142:
143:         @Override
144:         public List<SStmIR> getForLoopStms(AIdentifierVarExpIR setVar,
145:                         List<SPatternIR> patterns, SPatternIR pattern)
146:         {
147:                 return packStm(transAssist.consBoolVarAssignment(suchThat, successVarName));
148:         }
149:
150:         @Override
151:         public List<SStmIR> getPostOuterBlockStms(AIdentifierVarExpIR setVar,
152:                         List<SPatternIR> patterns)
153:         {
154:                 ALetBeStNoBindingRuntimeErrorExpIR noBinding = new ALetBeStNoBindingRuntimeErrorExpIR();
155:                 noBinding.setType(new AErrorTypeIR());
156:
157:                 ARaiseErrorStmIR raise = new ARaiseErrorStmIR();
158:                 raise.setError(noBinding);
159:
160:                 AIfStmIR ifStm = new AIfStmIR();
161:                 ifStm.setIfExp(transAssist.consBoolCheck(successVarName, true));
162:                 ifStm.setThenStm(raise);
163:
164:                 return packStm(ifStm);
165:         }
166: }