Package: ConcreteTypeInstantiator

ConcreteTypeInstantiator

nameinstructionbranchcomplexitylinemethod
ConcreteTypeInstantiator(ITypeCheckerAssistantFactory)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
caseAFunctionType(AFunctionType, Context)
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
caseAOptionalType(AOptionalType, Context)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
caseAParameterType(AParameterType, Context)
M: 35 C: 16
31%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 5 C: 5
50%
M: 0 C: 1
100%
caseAProductType(AProductType, Context)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseASet1SetType(ASet1SetType, Context)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
caseASetSetType(ASetSetType, Context)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
caseAUnionType(AUnionType, Context)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createNewReturnValue(INode, Context)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createNewReturnValue(Object, Context)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
defaultPType(PType, Context)
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
defaultSMapType(SMapType, Context)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
defaultSSeqType(SSeqType, Context)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
instantiate(LinkedList, Context)
M: 0 C: 24
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * The VDM Type Checker
4: * %%
5: * Copyright (C) 2008 - 2017 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.interpreter.utilities.type;
23:
24: import java.util.LinkedList;
25: import java.util.List;
26: import java.util.Vector;
27:
28: import org.overture.ast.analysis.AnalysisException;
29: import org.overture.ast.analysis.QuestionAnswerAdaptor;
30: import org.overture.ast.factory.AstFactory;
31: import org.overture.ast.node.INode;
32: import org.overture.ast.types.AFunctionType;
33: import org.overture.ast.types.AOptionalType;
34: import org.overture.ast.types.AParameterType;
35: import org.overture.ast.types.AProductType;
36: import org.overture.ast.types.ASet1SetType;
37: import org.overture.ast.types.ASetSetType;
38: import org.overture.ast.types.AUnionType;
39: import org.overture.ast.types.PType;
40: import org.overture.ast.types.SMapType;
41: import org.overture.ast.types.SSeqType;
42: import org.overture.interpreter.runtime.Context;
43: import org.overture.interpreter.runtime.VdmRuntimeError;
44: import org.overture.interpreter.values.ParameterValue;
45: import org.overture.interpreter.values.Value;
46: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
47:
48: /**
49: * Implement the concrete type of a given type, given a Context with @T parameter definitions.
50: *
51: * @author ncb
52: */
53: public class ConcreteTypeInstantiator extends QuestionAnswerAdaptor<Context, PType>
54: {
55:         protected ITypeCheckerAssistantFactory af;
56:
57:         public ConcreteTypeInstantiator(ITypeCheckerAssistantFactory af)
58:         {
59:                 this.af = af;
60:         }
61:
62:         @Override
63:         public PType caseAParameterType(AParameterType type, Context ctxt) throws AnalysisException
64:         {
65:                 Value t = ctxt.lookup(type.getName());
66:
67:•                if (t == null)
68:                 {
69:                         VdmRuntimeError.abort(type.getLocation(), 4008,
70:                                 "No such type parameter @" + type.getName() + " in scope", ctxt);
71:                 }
72:•                else if (t instanceof ParameterValue)
73:                 {
74:                         ParameterValue tv = (ParameterValue)t;
75:                         return tv.type;
76:                 }
77:                 else
78:                 {
79:                         VdmRuntimeError.abort(type.getLocation(), 4009,
80:                                 "Type parameter/local variable name clash, @" + type.getName(), ctxt);
81:                 }
82:                 
83:                 return null;
84:         }
85:
86:         @Override
87:         public PType caseAFunctionType(AFunctionType type, Context ctxt) throws AnalysisException
88:         {
89:                 type = AstFactory.newAFunctionType(type.getLocation(), type.getPartial(),
90:                         instantiate(type.getParameters(), ctxt), type.getResult().apply(this, ctxt));
91:                 type.setInstantiated(true);
92:                 return type;
93:         }
94:
95:         @Override
96:         public PType defaultSMapType(SMapType type, Context ctxt) throws AnalysisException
97:         {
98:                 return AstFactory.newAMapMapType(type.getLocation(),
99:                         type.getFrom().apply(this, ctxt),
100:                         type.getTo().apply(this, ctxt));
101:         }
102:
103:         @Override
104:         public PType caseAOptionalType(AOptionalType type, Context ctxt) throws AnalysisException
105:         {
106:                 return AstFactory.newAOptionalType(type.getLocation(), type.getType().apply(this, ctxt));
107:         }
108:
109:         @Override
110:         public PType caseAProductType(AProductType type, Context ctxt) throws AnalysisException
111:         {
112:                 return AstFactory.newAProductType(type.getLocation(), instantiate(type.getTypes(), ctxt));
113:         }
114:
115:         @Override
116:         public PType defaultSSeqType(SSeqType type, Context ctxt) throws AnalysisException
117:         {
118:                 return AstFactory.newASeqSeqType(type.getLocation(), type.getSeqof().apply(this, ctxt));
119:         }
120:
121:         @Override
122:         public PType caseASetSetType(ASetSetType type, Context ctxt) throws AnalysisException
123:         {
124:                 return AstFactory.newASetSetType(type.getLocation(), type.getSetof().apply(this, ctxt));
125:         }
126:
127:         @Override
128:         public PType caseASet1SetType(ASet1SetType type, Context ctxt) throws AnalysisException
129:         {
130:                 return AstFactory.newASet1SetType(type.getLocation(), type.getSetof().apply(this, ctxt));
131:         }
132:
133:         @Override
134:         public PType caseAUnionType(AUnionType type, Context ctxt) throws AnalysisException
135:         {
136:                 return AstFactory.newAUnionType(type.getLocation(), instantiate(type.getTypes(), ctxt));
137:         }
138:
139:         @Override
140:         public PType defaultPType(PType type, Context ctxt)        throws AnalysisException
141:         {
142:                 return type;
143:         }
144:
145:         private List<PType> instantiate(LinkedList<PType> types, Context ctxt) throws AnalysisException
146:         {
147:                 List<PType> instantiated = new Vector<PType>();
148:                 
149:•                for (PType type: types)
150:                 {
151:                         instantiated.add(type.apply(this, ctxt));
152:                 }
153:                 
154:                 return instantiated;
155:         }
156:
157:         @Override
158:         public PType createNewReturnValue(INode node, Context question)        throws AnalysisException
159:         {
160:                 return null;
161:         }
162:
163:         @Override
164:         public PType createNewReturnValue(Object node, Context question) throws AnalysisException
165:         {
166:                 return null;
167:         }
168: }