Package: BaseTransformationAssistant

BaseTransformationAssistant

nameinstructionbranchcomplexitylinemethod
BaseTransformationAssistant()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
findEnclosingStm(INode)
M: 0 C: 205
100%
M: 2 C: 22
92%
M: 2 C: 11
85%
M: 0 C: 48
100%
M: 0 C: 1
100%
getEnclosingStm(INode, String)
M: 12 C: 8
40%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
replace(INode, INode)
M: 0 C: 13
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
replaceNodeWith(INode, INode)
M: 0 C: 8
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
replaceNodeWithRecursively(INode, INode, DepthFirstAnalysisAdaptor)
M: 0 C: 11
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
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.assistants;
23:
24: import java.util.LinkedList;
25: import java.util.List;
26:
27: import org.overture.codegen.ir.INode;
28: import org.overture.codegen.ir.SStmIR;
29: import org.overture.codegen.ir.analysis.AnalysisException;
30: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
31: import org.overture.codegen.ir.declarations.AVarDeclIR;
32: import org.overture.codegen.ir.expressions.AAnonymousClassExpIR;
33: import org.overture.codegen.ir.statements.ABlockStmIR;
34: import org.overture.codegen.ir.statements.AElseIfStmIR;
35: import org.overture.codegen.ir.statements.AIfStmIR;
36:
37: public class BaseTransformationAssistant
38: {
39:         public void replaceNodeWith(INode original, INode replacement)
40:         {
41:•                if (original != replacement)
42:                 {
43:                         replace(original, replacement);
44:                 }
45:         }
46:
47:         public void replaceNodeWithRecursively(INode original, INode replacement,
48:                         DepthFirstAnalysisAdaptor analysis) throws AnalysisException
49:         {
50:•                if (original != replacement)
51:                 {
52:                         replaceNodeWith(original, replacement);
53:                         replacement.apply(analysis);
54:                 }
55:         }
56:
57:         private void replace(INode original, INode replacement)
58:         {
59:                 INode parent = original.parent();
60:
61:•                if (parent != null)
62:                 {
63:                         parent.replaceChild(original, replacement);
64:                 }
65:
66:                 original.parent(null);
67:         }
68:
69:         public SStmIR findEnclosingStm(INode node) throws AnalysisException
70:
71:         {
72:•                if (node.getAncestor(AAnonymousClassExpIR.class) == null)
73:                 {
74:                         AVarDeclIR localDecl = node.getAncestor(AVarDeclIR.class);
75:
76:•                        if (localDecl != null && localDecl.parent() instanceof ABlockStmIR)
77:                         {
78:                                 ABlockStmIR block = (ABlockStmIR) localDecl.parent();
79:
80:•                                if (block.getLocalDefs().size() <= 1)
81:                                 {
82:                                         return block;
83:                                 }
84:
85:                                 List<AVarDeclIR> defsToLift = new LinkedList<AVarDeclIR>();
86:
87:                                 int i = 0;
88:•                                for (; i < block.getLocalDefs().size(); i++)
89:                                 {
90:                                         AVarDeclIR currentDef = block.getLocalDefs().get(i);
91:
92:•                                        if (currentDef == localDecl)
93:                                         {
94:                                                 defsToLift.add(currentDef);
95:                                                 i++;
96:•                                                for (; i < block.getLocalDefs().size(); i++)
97:                                                 {
98:                                                         currentDef = block.getLocalDefs().get(i);
99:                                                         defsToLift.add(currentDef);
100:                                                 }
101:                                         }
102:                                 }
103:
104:                                 block.getLocalDefs().removeAll(defsToLift);
105:                                 LinkedList<SStmIR> statementsToLift = block.getStatements();
106:
107:                                 ABlockStmIR liftedBlock = new ABlockStmIR();
108:                                 liftedBlock.setScoped(block.getScoped());
109:                                 liftedBlock.setLocalDefs(defsToLift);
110:                                 liftedBlock.setStatements(statementsToLift);
111:
112:                                 block.getStatements().clear();
113:                                 block.getStatements().add(liftedBlock);
114:
115:                                 return liftedBlock;
116:                         }
117:                 }
118:
119:                 SStmIR enclosingStm = node.getAncestor(SStmIR.class);
120:
121:•                if (enclosingStm == null)
122:                 {
123:                         return null;
124:                 }
125:
126:•                if (enclosingStm instanceof AElseIfStmIR)
127:                 {
128:                         AElseIfStmIR elseIf = (AElseIfStmIR) enclosingStm;
129:                         AIfStmIR enclosingIf = elseIf.getAncestor(AIfStmIR.class);
130:
131:                         LinkedList<AElseIfStmIR> elseIfList = new LinkedList<AElseIfStmIR>(enclosingIf.getElseIf());
132:•                        for (int i = 0; i < elseIfList.size(); i++)
133:                         {
134:                                 AElseIfStmIR currentElseIf = elseIfList.get(i);
135:•                                if (elseIf == currentElseIf)
136:                                 {
137:                                         enclosingIf.getElseIf().remove(currentElseIf);
138:                                         AIfStmIR elsePart = new AIfStmIR();
139:                                         elsePart.setIfExp(currentElseIf.getElseIf());
140:                                         elsePart.setThenStm(currentElseIf.getThenStm());
141:
142:•                                        for (int j = i + 1; j < elseIfList.size(); j++)
143:                                         {
144:                                                 enclosingIf.getElseIf().remove(elseIfList.get(j));
145:                                                 elsePart.getElseIf().add(elseIfList.get(j));
146:                                         }
147:
148:                                         ABlockStmIR block = new ABlockStmIR();
149:                                         block.getStatements().add(elsePart);
150:
151:                                         elsePart.setElseStm(enclosingIf.getElseStm());
152:                                         enclosingIf.setElseStm(block);
153:
154:                                         return elsePart;
155:                                 }
156:                         }
157:                 }
158:
159:                 return enclosingStm;
160:         }
161:
162:         public SStmIR getEnclosingStm(INode node, String nodeStr)
163:                         throws AnalysisException
164:         {
165:                 SStmIR enclosingStm = findEnclosingStm(node);
166:
167:•                if (enclosingStm == null)
168:                 {
169:                         new AnalysisException(String.format("Could not find enclosing statement for %s", node));
170:                 }
171:
172:                 return enclosingStm;
173:         }
174: }