Package: FieldOrderTrans

FieldOrderTrans

nameinstructionbranchcomplexitylinemethod
FieldOrderTrans()
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%
caseADefaultClassDeclIR(ADefaultClassDeclIR)
M: 0 C: 115
100%
M: 0 C: 14
100%
M: 0 C: 8
100%
M: 0 C: 27
100%
M: 0 C: 1
100%
findDependencies(String, AFieldDeclIR, Map)
M: 0 C: 34
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import org.jgrapht.graph.DefaultDirectedGraph;
4: import org.jgrapht.graph.DefaultEdge;
5: import org.jgrapht.traverse.TopologicalOrderIterator;
6: import org.overture.ast.definitions.ARenamedDefinition;
7: import org.overture.ast.definitions.PDefinition;
8: import org.overture.ast.expressions.AVariableExp;
9: import org.overture.ast.node.INode;
10: import org.overture.codegen.assistant.AssistantBase;
11: import org.overture.codegen.ir.SExpIR;
12: import org.overture.codegen.ir.STypeIR;
13: import org.overture.codegen.ir.analysis.AnalysisException;
14: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
15: import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
16: import org.overture.codegen.ir.declarations.AFieldDeclIR;
17: import org.overture.codegen.ir.expressions.AExplicitVarExpIR;
18: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
19: import org.overture.codegen.ir.expressions.SVarExpIR;
20: import org.overture.codegen.ir.types.AClassTypeIR;
21:
22: import java.util.*;
23:
24: public class FieldOrderTrans extends DepthFirstAnalysisAdaptor {
25:
26: static class VarExpFinder extends DepthFirstAnalysisAdaptor
27: {
28: private List<SVarExpIR> vars;
29:
30: private String className;
31:
32: private VarExpFinder(String className)
33: {
34: this.vars = new LinkedList<>();
35: this.className = className;
36: }
37:
38: public static List<SVarExpIR> findOccurences(String className, SExpIR root) throws AnalysisException {
39:
40: VarExpFinder finder = new VarExpFinder(className);
41:
42: root.apply(finder);
43:
44: return finder.vars;
45: }
46:
47: @Override
48: public void caseAIdentifierVarExpIR(AIdentifierVarExpIR node) throws AnalysisException {
49:
50: INode var = AssistantBase.getVdmNode(node);
51:
52: if(var instanceof AVariableExp) {
53: PDefinition def = ((AVariableExp) var).getVardef();
54:
55: if (def instanceof ARenamedDefinition) {
56:
57: return;
58: }
59: }
60:
61: vars.add(node);
62: }
63:
64: @Override
65: public void caseAExplicitVarExpIR(AExplicitVarExpIR node) throws AnalysisException {
66:
67: STypeIR classType = node.getClassType();
68:
69: if(classType instanceof AClassTypeIR)
70: {
71: String fieldClassName = ((AClassTypeIR) classType).getName();
72:
73: if(className.equals(fieldClassName))
74: {
75: vars.add(node);
76: }
77: }
78: }
79: }
80:
81: @Override
82: public void caseADefaultClassDeclIR(ADefaultClassDeclIR node) throws AnalysisException {
83:
84: final String className = node.getName();
85:
86: final LinkedList<AFieldDeclIR> fields = node.getFields();
87:
88: Map<String, AFieldDeclIR> fieldMap = new HashMap<>();
89: DefaultDirectedGraph<AFieldDeclIR, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
90:
91:• for(AFieldDeclIR f : fields)
92: {
93: fieldMap.put(f.getName(), f);
94: graph.addVertex(f);
95: }
96:
97: boolean hasDependencies = false;
98:
99:• for(AFieldDeclIR f : fields)
100: {
101: List<AFieldDeclIR> dependencies = findDependencies(className, f, fieldMap);
102:
103:• hasDependencies = hasDependencies || !dependencies.isEmpty();
104:
105:• for(AFieldDeclIR d : dependencies)
106: {
107: graph.addEdge(f, d);
108: }
109: }
110:
111:• if(!hasDependencies)
112: {
113: return;
114: }
115:
116: List<AFieldDeclIR> ordered = new LinkedList<>();
117:
118: TopologicalOrderIterator<AFieldDeclIR, DefaultEdge> ite = new TopologicalOrderIterator(graph, new Comparator<AFieldDeclIR>() {
119: @Override
120: public int compare(AFieldDeclIR f1, AFieldDeclIR f2) {
121:
122: // Try to maintain the original order
123: return fields.indexOf(f2) - fields.indexOf(f1);
124: }
125: });
126:
127:• while(ite.hasNext())
128: {
129: AFieldDeclIR next = ite.next();
130: ordered.add(next);
131: }
132:
133: Collections.reverse(ordered);
134:
135: node.setFields(ordered);
136: }
137:
138: private List<AFieldDeclIR> findDependencies(String className, AFieldDeclIR f, Map<String, AFieldDeclIR> fieldMap) throws AnalysisException {
139:
140: List<SVarExpIR> varExps = VarExpFinder.findOccurences(className, f.getInitial());
141:
142: List<AFieldDeclIR> dependencies = new LinkedList<>();
143:
144:• for(SVarExpIR v : varExps)
145: {
146: AFieldDeclIR field = fieldMap.get(v.getName());
147:• if(field != null)
148: {
149: dependencies.add(field);
150: }
151: }
152:
153: return dependencies;
154: }
155: }