Method: PrePostTrans(IRInfo)

1: package org.overture.codegen.trans;
2:
3: import java.util.LinkedList;
4:
5: import org.apache.log4j.Logger;
6: import org.overture.codegen.ir.IRConstants;
7: import org.overture.codegen.ir.IRInfo;
8: import org.overture.codegen.ir.SDeclIR;
9: import org.overture.codegen.ir.STypeIR;
10: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
11: import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
12: import org.overture.codegen.ir.declarations.AFormalParamLocalParamIR;
13: import org.overture.codegen.ir.declarations.AMethodDeclIR;
14:
15: public class PrePostTrans extends DepthFirstAnalysisAdaptor
16: {
17:
18:         private IRInfo info;
19:
20:         private Logger log = Logger.getLogger(this.getClass().getName());
21:
22:         public PrePostTrans(IRInfo info)
23:         {
24:                 this.info = info;
25:         }
26:
27:         @Override
28:         public void caseAMethodDeclIR(AMethodDeclIR node)
29:                         throws org.overture.codegen.ir.analysis.AnalysisException
30:         {
31:                 ADefaultClassDeclIR enclosingClass = node.getAncestor(ADefaultClassDeclIR.class);
32:
33:                 if (enclosingClass == null)
34:                 {
35:                         log.error("Could not find enclosing class for method: " + node);
36:                         return;
37:                 }
38:
39:                 if(info.getSettings().generatePreConds()) {
40:                         SDeclIR preCond = node.getPreCond();
41:                         if (preCond instanceof AMethodDeclIR) {
42:                                 AMethodDeclIR preCondMethod = (AMethodDeclIR) preCond;
43:
44:                                 if (info.getSettings().makePreCondsPublic()) {
45:                                         preCondMethod.setAccess(IRConstants.PUBLIC);
46:                                 }
47:
48:                                 enclosingClass.getMethods().add(preCondMethod);
49:
50:                                 if (node.getStatic() != null && !node.getStatic()) {
51:                                         preCondMethod.setStatic(false);
52:
53:                                         // No need to pass self as the last argument
54:                                         LinkedList<STypeIR> paramTypes = preCondMethod.getMethodType().getParams();
55:                                         paramTypes.remove(paramTypes.size() - 1);
56:
57:                                         LinkedList<AFormalParamLocalParamIR> formalParams = preCondMethod.getFormalParams();
58:                                         formalParams.remove(formalParams.size() - 1);
59:                                 }
60:                         }
61:                 }
62:
63:                 if(info.getSettings().generatePostConds()) {
64:                         SDeclIR postCond = node.getPostCond();
65:                         if (postCond instanceof AMethodDeclIR) {
66:                                 AMethodDeclIR postCondMethod = (AMethodDeclIR) postCond;
67:
68:                                 if (info.getSettings().makePostCondsPublic()) {
69:                                         postCondMethod.setAccess(IRConstants.PUBLIC);
70:                                 }
71:
72:                                 // Generation of a post condition is only supported for static operations
73:                                 // where no 'self' and '~self' are being passed
74:                                 if (node.getStatic() != null && node.getStatic()) {
75:                                         enclosingClass.getMethods().add(postCondMethod);
76:                                 } else {
77:                                         info.addTransformationWarning(postCondMethod, "Generation of a post condition is only supported for static operations");
78:                                 }
79:                         }
80:                 }
81:         }
82: }