Package: ConstructorTrans

ConstructorTrans

nameinstructionbranchcomplexitylinemethod
ConstructorTrans(TransAssistantIR, String)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
caseAMethodDeclIR(AMethodDeclIR)
M: 24 C: 143
86%
M: 2 C: 12
86%
M: 2 C: 6
75%
M: 4 C: 39
91%
M: 0 C: 1
100%
caseAPlainCallStmIR(APlainCallStmIR)
M: 0 C: 46
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
getInitName(AMethodDeclIR)
M: 0 C: 23
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 7
100%
M: 0 C: 1
100%
getInitName(APlainCallStmIR)
M: 0 C: 44
100%
M: 2 C: 10
83%
M: 2 C: 5
71%
M: 0 C: 13
100%
M: 0 C: 1
100%
getObjectInitializerCall(AExplicitOperationDefinition)
M: 0 C: 43
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import java.util.HashMap;
4: import java.util.Map;
5:
6: import org.apache.log4j.Logger;
7: import org.overture.ast.definitions.AExplicitOperationDefinition;
8: import org.overture.ast.definitions.AInheritedDefinition;
9: import org.overture.ast.definitions.PDefinition;
10: import org.overture.ast.definitions.SClassDefinition;
11: import org.overture.ast.node.INode;
12: import org.overture.ast.statements.ACallStm;
13: import org.overture.codegen.ir.IRGeneratedTag;
14: import org.overture.codegen.ir.SExpIR;
15: import org.overture.codegen.ir.SPatternIR;
16: import org.overture.codegen.ir.analysis.AnalysisException;
17: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
18: import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
19: import org.overture.codegen.ir.declarations.AFormalParamLocalParamIR;
20: import org.overture.codegen.ir.declarations.AMethodDeclIR;
21: import org.overture.codegen.ir.declarations.ASystemClassDeclIR;
22: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
23: import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
24: import org.overture.codegen.ir.statements.APlainCallStmIR;
25: import org.overture.codegen.ir.types.AVoidTypeIR;
26: import org.overture.codegen.trans.assistants.TransAssistantIR;
27:
28: public class ConstructorTrans extends DepthFirstAnalysisAdaptor
29: {
30:         private TransAssistantIR assist;
31:
32:         // To look up object initializer call names
33:         private Map<AExplicitOperationDefinition, String> objectInitCallNames;
34:
35:         // Object initialization call prefix
36:         private String objectInitCallPrefix;
37:
38:         private Logger log = Logger.getLogger(this.getClass().getName());
39:
40:         public ConstructorTrans(TransAssistantIR assist,
41:                         String objectInitCallPrefix)
42:         {
43:                 this.assist = assist;
44:                 this.objectInitCallPrefix = objectInitCallPrefix;
45:                 this.objectInitCallNames = new HashMap<AExplicitOperationDefinition, String>();
46:         }
47:
48:         @Override
49:         public void caseAMethodDeclIR(AMethodDeclIR node) throws AnalysisException
50:         {
51:•                if (node.parent() instanceof ASystemClassDeclIR)
52:                 {
53:                         return;
54:                 }
55:
56:•                if (node.getIsConstructor())
57:                 {
58:                         String initName = getInitName(node);
59:
60:•                        if (initName == null)
61:                         {
62:                                 return;
63:                         }
64:
65:                         AMethodDeclIR objInitializer = node.clone();
66:                         objInitializer.setSourceNode(node.getSourceNode());
67:                         objInitializer.setTag(new IRGeneratedTag(getClass().getName()));
68:                         objInitializer.setName(initName);
69:                         objInitializer.getMethodType().setResult(new AVoidTypeIR());
70:                         objInitializer.setIsConstructor(false);
71:                         objInitializer.setPreCond(null);
72:                         objInitializer.setPostCond(null);
73:
74:                         ADefaultClassDeclIR classCg = node.getAncestor(ADefaultClassDeclIR.class);
75:
76:•                        if (classCg == null)
77:                         {
78:                                 log.error("Could not find enclosing class of constructor "
79:                                                 + node.getName());
80:                                 return;
81:                         }
82:
83:                         classCg.getMethods().addFirst(objInitializer);
84:
85:                         // Apply transformation recursively
86:                         objInitializer.apply(this);
87:
88:                         APlainCallStmIR initCall = new APlainCallStmIR();
89:                         initCall.setType(objInitializer.getMethodType().getResult().clone());
90:                         initCall.setClassType(null);
91:                         initCall.setName(initName);
92:
93:•                        for (AFormalParamLocalParamIR param : node.getFormalParams())
94:                         {
95:                                 SPatternIR pattern = param.getPattern();
96:
97:•                                if (pattern instanceof AIdentifierPatternIR)
98:                                 {
99:                                         AIdentifierPatternIR idPattern = (AIdentifierPatternIR) pattern;
100:
101:                                         AIdentifierVarExpIR var = new AIdentifierVarExpIR();
102:                                         var.setIsLocal(true);
103:                                         var.setType(param.getType().clone());
104:                                         var.setName(idPattern.getName());
105:                                         var.setIsLambda(false);
106:                                         var.setSourceNode(pattern.getSourceNode());
107:
108:                                         initCall.getArgs().add(var);
109:                                 } else
110:                                 {
111:                                         log.error("Expected all parameters to be identifier patterns by now. Got: "
112:                                                         + pattern);
113:                                 }
114:                         }
115:
116:                         node.setBody(initCall);
117:                 }
118:
119:•                if (node.getBody() != null)
120:                 {
121:                         node.getBody().apply(this);
122:                 }
123:         }
124:
125:         @Override
126:         public void caseAPlainCallStmIR(APlainCallStmIR node)
127:                         throws AnalysisException
128:         {
129:                 String initName = getInitName(node);
130:
131:•                if (initName == null)
132:                 {
133:                         // No 'initName' exists for non-constructor calls
134:                         return;
135:                 }
136:
137:                 APlainCallStmIR callStm = new APlainCallStmIR();
138:                 callStm.setType(new AVoidTypeIR());
139:                 callStm.setClassType(null);
140:                 callStm.setName(initName);
141:
142:•                for (SExpIR a : node.getArgs())
143:                 {
144:                         callStm.getArgs().add(a.clone());
145:                 }
146:
147:                 assist.replaceNodeWith(node, callStm);
148:         }
149:
150:         public String getObjectInitializerCall(AExplicitOperationDefinition vdmOp)
151:         {
152:•                if (objectInitCallNames.containsKey(vdmOp))
153:                 {
154:                         return objectInitCallNames.get(vdmOp);
155:                 } else
156:                 {
157:                         String enclosingClassName = vdmOp.getAncestor(SClassDefinition.class).getName().getName();
158:                         String initName = assist.getInfo().getTempVarNameGen().nextVarName(objectInitCallPrefix
159:                                         + enclosingClassName + "_");
160:                         objectInitCallNames.put(vdmOp, initName);
161:
162:                         return initName;
163:                 }
164:         }
165:
166:         private String getInitName(APlainCallStmIR node)
167:         {
168:•                if (node.getSourceNode() != null
169:•                                && node.getSourceNode().getVdmNode() != null)
170:                 {
171:                         INode vdmNode = node.getSourceNode().getVdmNode();
172:
173:•                        if (vdmNode instanceof ACallStm)
174:                         {
175:                                 ACallStm c = (ACallStm) vdmNode;
176:
177:                                 PDefinition rootDef = c.getRootdef();
178:
179:•                                while (rootDef instanceof AInheritedDefinition)
180:                                 {
181:                                         rootDef = ((AInheritedDefinition) rootDef).getSuperdef();
182:                                 }
183:
184:•                                if (rootDef instanceof AExplicitOperationDefinition)
185:                                 {
186:                                         AExplicitOperationDefinition op = (AExplicitOperationDefinition) rootDef;
187:
188:•                                        if (op.getIsConstructor())
189:                                         {
190:                                                 return getObjectInitializerCall(op);
191:                                         }
192:                                 }
193:                         }
194:                 }
195:
196:                 return null;
197:         }
198:
199:         private String getInitName(AMethodDeclIR node)
200:         {
201:•                if (node.getSourceNode() != null
202:•                                && node.getSourceNode().getVdmNode() != null)
203:                 {
204:                         INode vdmNode = node.getSourceNode().getVdmNode();
205:
206:•                        if (vdmNode instanceof AExplicitOperationDefinition)
207:                         {
208:                                 AExplicitOperationDefinition op = (AExplicitOperationDefinition) vdmNode;
209:
210:                                 return getObjectInitializerCall(op);
211:                         }
212:                 }
213:
214:                 return null;
215:         }
216: }