Package: DelegateTrans

DelegateTrans

nameinstructionbranchcomplexitylinemethod
DelegateTrans(Map, TransAssistantIR, Log)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
caseADefaultClassDeclIR(ADefaultClassDeclIR)
M: 60 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
consDelegateCall(String, AMethodDeclIR)
M: 94 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
getFullDelegateName(ADefaultClassDeclIR)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isBridgeClass(ADefaultClassDeclIR)
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%
isDelegateCall(SStmIR)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.codegen.mojocg.util;
2:
3: import java.util.Map;
4:
5: import org.apache.maven.plugin.logging.Log;
6: import org.overture.codegen.ir.SPatternIR;
7: import org.overture.codegen.ir.SStmIR;
8: import org.overture.codegen.ir.analysis.AnalysisException;
9: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
10: import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
11: import org.overture.codegen.ir.declarations.AFormalParamLocalParamIR;
12: import org.overture.codegen.ir.declarations.AMethodDeclIR;
13: import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
14: import org.overture.codegen.ir.statements.APlainCallStmIR;
15: import org.overture.codegen.ir.types.AExternalTypeIR;
16: import org.overture.codegen.trans.assistants.TransAssistantIR;
17:
18: public class DelegateTrans extends DepthFirstAnalysisAdaptor
19: {
20:         private static final String FALLBACK_PARAM_NAME_PREFIX = "_param_";
21:
22:         private Map<String, String> delegateMap;
23:         private TransAssistantIR assist;
24:         private Log log;
25:
26:         public DelegateTrans(Map<String, String> buidDelegateMap,
27:                         TransAssistantIR assist, Log log)
28:         {
29:                 this.delegateMap = buidDelegateMap;
30:                 this.assist = assist;
31:                 this.log = log;
32:         }
33:
34:         @Override
35:         public void caseADefaultClassDeclIR(ADefaultClassDeclIR node)
36:                         throws AnalysisException
37:         {
38:•                if (isBridgeClass(node))
39:                 {
40:•                        for (AMethodDeclIR m : node.getMethods())
41:                         {
42:•                                if (isDelegateCall(m.getBody()))
43:                                 {
44:                                         log.debug("Updating " + node.getName() + "." + m.getName()
45:                                                         + " to use delegate method "
46:                                                         + getFullDelegateName(node) + "." + m.getName());
47:                                         assist.replaceNodeWith(m.getBody(), consDelegateCall(node.getName(), m));
48:                                 }
49:                         }
50:                 }
51:         }
52:
53:         private boolean isBridgeClass(ADefaultClassDeclIR node)
54:         {
55:                 return delegateMap.containsKey(node.getName());
56:         }
57:
58:         private String getFullDelegateName(ADefaultClassDeclIR node)
59:         {
60:                 return delegateMap.get(node.getName());
61:         }
62:
63:         private boolean isDelegateCall(SStmIR body)
64:         {
65:                 DelegateSearch search = new DelegateSearch();
66:
67:•                if (body != null)
68:                 {
69:                         try
70:                         {
71:                                 body.apply(search);
72:                                 return search.isDelegateCall();
73:                         } catch (AnalysisException e)
74:                         {
75:                                 e.printStackTrace();
76:                                 log.error("Unexpected error encountered when checking "
77:                                                 + "if method is a delegate call: " + e.getMessage());
78:                         }
79:                 }
80:
81:                 return false;
82:         }
83:
84:         private APlainCallStmIR consDelegateCall(String className, AMethodDeclIR m)
85:         {
86:                 AExternalTypeIR delegateType = new AExternalTypeIR();
87:                 delegateType.setName(delegateMap.get(className));
88:
89:                 APlainCallStmIR call = new APlainCallStmIR();
90:                 call.setClassType(delegateType);
91:                 call.setIsStatic(m.getStatic());
92:                 call.setName(m.getName());
93:                 call.setSourceNode(m.getBody().getSourceNode());
94:                 call.setTag(m.getBody().getTag());
95:                 call.setType(m.getMethodType().getResult().clone());
96:
97:•                for (int i = 0; i < m.getFormalParams().size(); i++)
98:                 {
99:                         AFormalParamLocalParamIR param = m.getFormalParams().get(i);
100:
101:                         SPatternIR pattern = param.getPattern();
102:
103:                         String argName = null;
104:
105:•                        if (pattern instanceof AIdentifierPatternIR)
106:                         {
107:                                 argName = ((AIdentifierPatternIR) pattern).getName();
108:                         } else
109:                         {
110:                                 // Should not happen...
111:                                 argName = FALLBACK_PARAM_NAME_PREFIX + i;
112:                         }
113:
114:                         call.getArgs().add(assist.getInfo().getExpAssistant().consIdVar(argName, param.getType().clone()));
115:                 }
116:
117:                 return call;
118:         }
119: }