Package: Dependencies$1

Dependencies$1

nameinstructionbranchcomplexitylinemethod
defaultInSVarExpIR(SVarExpIR)
M: 0 C: 18
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
{...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * The VDM to Isabelle Translator
4: * %%
5: * Copyright (C) 2008 - 2015 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.overturetool.cgisa.transformations;
23:
24: import java.util.HashSet;
25: import java.util.List;
26: import java.util.Map;
27: import java.util.Set;
28: import java.util.Vector;
29:
30: import org.apache.commons.collections4.map.HashedMap;
31: import org.jgrapht.DirectedGraph;
32: import org.jgrapht.graph.DefaultDirectedGraph;
33: import org.jgrapht.graph.DefaultEdge;
34: import org.overture.codegen.ir.analysis.AnalysisException;
35: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
36: import org.overture.codegen.ir.declarations.AFieldDeclIR;
37: import org.overture.codegen.ir.declarations.AFuncDeclIR;
38: import org.overture.codegen.ir.expressions.SVarExpIR;
39: import org.overturetool.cgisa.IsaChecks;
40: import org.overture.codegen.ir.SDeclIR;
41:
42: public class Dependencies
43: {
44:
45:         private IsaChecks isaUtils = new IsaChecks();
46:
47:         public DirectedGraph<AFuncDeclIR, DefaultEdge> calDepsAsGraph(
48:                         List<AFuncDeclIR> decls) throws AnalysisException
49:         {
50:                 DirectedGraph<AFuncDeclIR, DefaultEdge> r = new DefaultDirectedGraph<>(DefaultEdge.class);
51:                 for (SDeclIR decl : decls)
52:                 {
53:                         if (decl instanceof AFuncDeclIR)
54:                         {
55:                                 List<SDeclIR> dependencies = findDependencies(decl, decls);
56:                                 if (!dependencies.isEmpty())
57:                                 {
58:                                         r.addVertex((AFuncDeclIR) decl);
59:                                         for (SDeclIR dep : dependencies)
60:                                         {
61:                                                 if (dep instanceof AFuncDeclIR)
62:                                                 {
63:                                                         r.addVertex((AFuncDeclIR) dep);
64:                                                         r.addEdge((AFuncDeclIR) decl, (AFuncDeclIR) dep);
65:                                                 }
66:                                         }
67:                                 }
68:                         }
69:                 }
70:                 return r;
71:         }
72:
73:         public Map<SDeclIR, List<SDeclIR>> calcDepsAsMap(List<SDeclIR> decls)
74:         {
75:                 Map<SDeclIR, List<SDeclIR>> r = new HashedMap<>();
76:                 for (SDeclIR decl : decls)
77:                 {
78:                         try
79:                         {
80:                                 List<SDeclIR> dependencies = findDependencies(decl, decls);
81:                                 if (!dependencies.isEmpty())
82:                                 {
83:                                         r.put(decl, dependencies);
84:                                 } else
85:                                 {
86:                                         r.put(decl, new Vector<SDeclIR>());
87:                                 }
88:                         } catch (AnalysisException e)
89:                         {
90:                                 e.printStackTrace();
91:                         }
92:                 }
93:                 return r;
94:         }
95:
96:         private List<SDeclIR> findDependencies(SDeclIR decl,
97:                         List<? extends SDeclIR> decls) throws AnalysisException
98:         {
99:                 final Set<SVarExpIR> vars = new HashSet<SVarExpIR>();
100:
101:                 decl.apply(new DepthFirstAnalysisAdaptor()
102:                 {
103:                         public void defaultInSVarExpIR(SVarExpIR node)
104:                                         throws AnalysisException
105:                         {
106:•                                if (isaUtils.isRoot(node) || isaUtils.isFieldRHS(node))
107:                                 {
108:                                         vars.add(node);
109:                                 }
110:                         }
111:                 });
112:
113:                 List<SDeclIR> deps = new Vector<SDeclIR>();
114:                 for (SVarExpIR v : vars)
115:                 {
116:                         for (SDeclIR d : decls)
117:                         {
118:                                 String n = "";
119:                                 if (d instanceof AFuncDeclIR)
120:                                 {
121:                                         n = ((AFuncDeclIR) d).getName();
122:                                 }
123:
124:                                 else
125:                                 {
126:                                         if (d instanceof AFieldDeclIR)
127:                                         {
128:                                                 n = ((AFieldDeclIR) d).getName();
129:                                         }
130:                                 }
131:
132:                                 if (v.getName().equals(n))
133:                                 {
134:                                         deps.add(d);
135:                                         break;
136:                                 }
137:                         }
138:                 }
139:
140:                 return deps;
141:         }
142: }