Package: LocationAssistantIR$2

LocationAssistantIR$2

nameinstructionbranchcomplexitylinemethod
compare(VdmNodeInfo, VdmNodeInfo)
M: 26 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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.assistant;
23:
24: import org.overture.ast.intf.lex.ILexLocation;
25: import org.overture.ast.node.INode;
26: import org.overture.codegen.ir.*;
27:
28: import java.util.*;
29:
30: public class LocationAssistantIR extends AssistantBase
31: {
32:         public LocationAssistantIR(AssistantManager assistantManager)
33:         {
34:                 super(assistantManager);
35:         }
36:
37:         public ILexLocation findLocation(INode node)
38:         {
39:                 Map<String, Object> children = node.getChildren(true);
40:
41:                 Set<String> allKeys = children.keySet();
42:
43:                 for (String key : allKeys)
44:                 {
45:                         Object child = children.get(key);
46:
47:                         if (child instanceof ILexLocation)
48:                         {
49:                                 return (ILexLocation) child;
50:                         }
51:                 }
52:
53:                 return null;
54:         }
55:
56:         public int compareLocations(ILexLocation firstLoc, ILexLocation secondLoc)
57:         {
58:                 String firstModule = firstLoc.getModule();
59:                 String secondModule = secondLoc.getModule();
60:
61:                 if (!firstModule.equals(secondModule))
62:                 {
63:                         return firstModule.compareTo(secondModule);
64:                 }
65:
66:                 int firstLine = firstLoc.getStartLine();
67:                 int secondLine = secondLoc.getStartLine();
68:
69:                 if (firstLine == secondLine)
70:                 {
71:                         int firstPos = firstLoc.getStartPos();
72:                         int secondPos = secondLoc.getStartPos();
73:
74:                         return firstPos - secondPos;
75:                 } else
76:                 {
77:                         return firstLine - secondLine;
78:                 }
79:         }
80:
81:         public INode getVdmNode(IrNodeInfo info)
82:         {
83:                 if (info == null)
84:                 {
85:                         return null;
86:                 }
87:
88:                 if (info.getNode() == null || !(info.getNode() instanceof PIR))
89:                 {
90:                         return null;
91:                 }
92:
93:                 SourceNode sourceNode = ((PIR) info.getNode()).getSourceNode();
94:
95:                 if (sourceNode == null)
96:                 {
97:                         return null;
98:                 }
99:
100:                 return sourceNode.getVdmNode();
101:         }
102:
103:         public ILexLocation findLocation(IrNodeInfo info)
104:         {
105:                 INode vdmNode = getVdmNode(info);
106:
107:                 if (vdmNode == null)
108:                 {
109:                         return null;
110:                 }
111:
112:                 return findLocation(vdmNode);
113:         }
114:
115:         public List<IrNodeInfo> getIrNodeInfoLocationSorted(Set<IrNodeInfo> nodes)
116:         {
117:                 List<IrNodeInfo> list = new LinkedList<IrNodeInfo>(nodes);
118:
119:                 Collections.sort(list, new Comparator<IrNodeInfo>()
120:                 {
121:
122:                         @Override
123:                         public int compare(IrNodeInfo first, IrNodeInfo second)
124:                         {
125:                                 INode vdmNode = getVdmNode(first);
126:                                 ILexLocation firstLoc = vdmNode == null ? null
127:                                                 : findLocation(vdmNode);
128:
129:                                 if (firstLoc == null)
130:                                 {
131:                                         return -1;
132:                                 }
133:
134:                                 vdmNode = getVdmNode(second);
135:                                 ILexLocation secondLoc = vdmNode == null ? null
136:                                                 : findLocation(vdmNode);
137:
138:                                 if (secondLoc == null)
139:                                 {
140:                                         return 1;
141:                                 }
142:
143:                                 return compareLocations(firstLoc, secondLoc);
144:                         }
145:                 });
146:
147:                 return list;
148:         }
149:
150:         public List<VdmNodeInfo> getVdmNodeInfoLocationSorted(
151:                         Set<VdmNodeInfo> nodes)
152:         {
153:                 List<VdmNodeInfo> list = new LinkedList<VdmNodeInfo>(nodes);
154:
155:                 Collections.sort(list, new Comparator<VdmNodeInfo>()
156:                 {
157:                         @Override
158:                         public int compare(VdmNodeInfo first, VdmNodeInfo second)
159:                         {
160:                                 ILexLocation firstLoc = findLocation(first.getNode());
161:
162:•                                if (firstLoc == null)
163:                                 {
164:                                         return -1;
165:                                 }
166:
167:                                 ILexLocation secondLoc = findLocation(second.getNode());
168:
169:•                                if (secondLoc == null)
170:                                 {
171:                                         return 1;
172:                                 }
173:
174:                                 return compareLocations(firstLoc, secondLoc);
175:                         }
176:                 });
177:
178:                 return list;
179:         }
180:
181:
182:         public String consVdmNodeInfoStr(STypeIR t) {
183:
184:                 StringBuilder sb = new StringBuilder();
185:                 INode vdmNode = AssistantBase.getVdmNode(t);
186:
187:                 if(vdmNode != null)
188:                 {
189:                         sb.append("VDM node: " + vdmNode + ".");
190:
191:                         ILexLocation loc = findLocation(vdmNode);
192:
193:                         if(loc != null)
194:                         {
195:                                 sb.append(" VDM node location: " + loc + ".");
196:                         }
197:                 }
198:
199:                 return sb.toString();
200:         }
201: }