Package: AExplicitOperationDefinitionAssistantTC

AExplicitOperationDefinitionAssistantTC

nameinstructionbranchcomplexitylinemethod
AExplicitOperationDefinitionAssistantTC(ITypeCheckerAssistantFactory, String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getParamDefinitions(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%
getParamPatternList(AExplicitOperationDefinition)
M: 30 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getPostDefinition(AExplicitOperationDefinition, Environment)
M: 0 C: 143
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 20
100%
M: 0 C: 1
100%
getPreDefinition(AExplicitOperationDefinition, Environment)
M: 0 C: 105
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 14
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * The VDM Type Checker
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.typechecker.assistant.definition;
23:
24: import java.util.ArrayList;
25: import java.util.Iterator;
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.overture.ast.assistant.IAstAssistant;
30: import org.overture.ast.definitions.AExplicitFunctionDefinition;
31: import org.overture.ast.definitions.AExplicitOperationDefinition;
32: import org.overture.ast.definitions.AStateDefinition;
33: import org.overture.ast.definitions.PDefinition;
34: import org.overture.ast.expressions.APostOpExp;
35: import org.overture.ast.expressions.APreOpExp;
36: import org.overture.ast.factory.AstFactory;
37: import org.overture.ast.lex.LexNameToken;
38: import org.overture.ast.patterns.PPattern;
39: import org.overture.ast.typechecker.NameScope;
40: import org.overture.ast.types.AOperationType;
41: import org.overture.ast.types.AVoidType;
42: import org.overture.ast.types.PType;
43: import org.overture.typechecker.Environment;
44: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
45:
46: public class AExplicitOperationDefinitionAssistantTC implements IAstAssistant
47: {
48:         protected final ITypeCheckerAssistantFactory af;
49:         protected final String fromModule;
50:
51:         public AExplicitOperationDefinitionAssistantTC(
52:                         ITypeCheckerAssistantFactory af, String fromModule)
53:         {
54:                 this.af = af;
55:                 this.fromModule = fromModule;
56:         }
57:
58:         public List<? extends PDefinition> getParamDefinitions(
59:                         AExplicitOperationDefinition node)
60:         {
61:
62:                 List<PDefinition> defs = new Vector<PDefinition>();
63:                 Iterator<PType> titer = ((AOperationType) node.getType()).getParameters().iterator();
64:
65:•                for (PPattern p : node.getParameterPatterns())
66:                 {
67:                         defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(p, titer.next(), NameScope.LOCAL));
68:                 }
69:
70:                 return af.createPDefinitionAssistant().checkDuplicatePatterns(node, defs);
71:         }
72:
73:         @SuppressWarnings("unchecked")
74:         public AExplicitFunctionDefinition getPostDefinition(
75:                         AExplicitOperationDefinition d, Environment base)
76:         {
77:
78:                 List<List<PPattern>> parameters = new Vector<List<PPattern>>();
79:                 List<PPattern> plist = new Vector<PPattern>();
80:                 plist.addAll((List<PPattern>) d.getParameterPatterns().clone());
81:
82:•                if (!(((AOperationType) d.getType()).getResult() instanceof AVoidType))
83:                 {
84:                         LexNameToken result = new LexNameToken(d.getName().getModule(), "RESULT", d.getLocation());
85:                         plist.add(AstFactory.newAIdentifierPattern(result));
86:                 }
87:
88:                 AStateDefinition state = d.getState();
89:
90:•                if (state != null) // Two args, called Sigma~ and Sigma
91:                 {
92:                         plist.add(AstFactory.newAIdentifierPattern(state.getName().getOldName()));
93:                         plist.add(AstFactory.newAIdentifierPattern(state.getName().clone()));
94:•                } else if (base.isVDMPP())
95:                 {
96:                         // Two arguments called "self~" and "self"
97:                         plist.add(AstFactory.newAIdentifierPattern(d.getName().getSelfName().getOldName()));
98:
99:•                        if (!af.createPAccessSpecifierAssistant().isStatic(d.getAccess()))
100:                         {
101:                                 plist.add(AstFactory.newAIdentifierPattern(d.getName().getSelfName()));
102:                         }
103:                 }
104:
105:                 parameters.add(plist);
106:                 APostOpExp postop = AstFactory.newAPostOpExp(d.getName().clone(), d.getPrecondition(), d.getPostcondition(), null, d.getState());
107:
108:                 AExplicitFunctionDefinition def = AstFactory.newAExplicitFunctionDefinition(d.getName().getPostName(d.getPostcondition().getLocation()), NameScope.GLOBAL, null, af.createAOperationTypeAssistant().getPostType((AOperationType) d.getType(), state, d.getClassDefinition(), af.createPAccessSpecifierAssistant().isStatic(d.getAccess())), parameters, postop, null, null, false, null);
109:
110:                 // Operation postcondition functions are effectively not static as
111:                 // their expression can directly refer to instance variables, even
112:                 // though at runtime these are passed via a "self" parameter.
113:
114:                 def.setAccess(af.createPAccessSpecifierAssistant().getStatic(d, false));
115:                 def.setClassDefinition(d.getClassDefinition());
116:                 return def;
117:
118:         }
119:
120:         @SuppressWarnings("unchecked")
121:         public AExplicitFunctionDefinition getPreDefinition(
122:                         AExplicitOperationDefinition d, Environment base)
123:         {
124:
125:                 List<List<PPattern>> parameters = new Vector<List<PPattern>>();
126:                 List<PPattern> plist = new Vector<PPattern>();
127:                 plist.addAll((List<PPattern>) d.getParameterPatterns().clone());
128:
129:•                if (d.getState() != null)
130:                 {
131:                         plist.add(AstFactory.newAIdentifierPattern(d.getState().getName().clone()));
132:•                } else if (base.isVDMPP()
133:•                                && !af.createPAccessSpecifierAssistant().isStatic(d.getAccess()))
134:                 {
135:                         plist.add(AstFactory.newAIdentifierPattern(d.getName().getSelfName()));
136:                 }
137:
138:                 parameters.add(plist);
139:                 APreOpExp preop = AstFactory.newAPreOpExp(d.getName().clone(), d.getPrecondition(), null, d.getState());
140:
141:                 AExplicitFunctionDefinition def = AstFactory.newAExplicitFunctionDefinition(d.getName().getPreName(d.getPrecondition().getLocation()), NameScope.GLOBAL, null, af.createAOperationTypeAssistant().getPreType((AOperationType) d.getType(), d.getState(), d.getClassDefinition(), af.createPAccessSpecifierAssistant().isStatic(d.getAccess())), parameters, preop, null, null, false, null);
142:
143:                 // Operation precondition functions are effectively not static as
144:                 // their expression can directly refer to instance variables, even
145:                 // though at runtime these are passed via a "self" parameter.
146:
147:                 def.setAccess(af.createPAccessSpecifierAssistant().getStatic(def, false));
148:                 def.setClassDefinition(d.getClassDefinition());
149:                 return def;
150:         }
151:
152:         public List<List<PPattern>> getParamPatternList(
153:                         AExplicitOperationDefinition func)
154:         {
155:                 List<List<PPattern>> parameters = new ArrayList<List<PPattern>>();
156:                 List<PPattern> plist = new ArrayList<PPattern>();
157:
158:•                for (PPattern p : func.getParameterPatterns())
159:                 {
160:                         plist.add(p);
161:                 }
162:
163:                 parameters.add(plist);
164:                 return parameters;
165:         }
166:
167: }