Package: DefinitionFinder$Newquestion

DefinitionFinder$Newquestion

nameinstructionbranchcomplexitylinemethod
DefinitionFinder.Newquestion(ILexNameToken, 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%

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.utilities;
23:
24: import org.overture.ast.analysis.AnalysisException;
25: import org.overture.ast.analysis.QuestionAnswerAdaptor;
26: import org.overture.ast.definitions.AImportedDefinition;
27: import org.overture.ast.definitions.AInheritedDefinition;
28: import org.overture.ast.definitions.ARenamedDefinition;
29: import org.overture.ast.definitions.AStateDefinition;
30: import org.overture.ast.definitions.ATypeDefinition;
31: import org.overture.ast.definitions.PDefinition;
32: import org.overture.ast.definitions.SClassDefinition;
33: import org.overture.ast.intf.lex.ILexNameToken;
34: import org.overture.ast.node.INode;
35: import org.overture.ast.typechecker.NameScope;
36: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
37:
38: /**
39: * This class implements a way to find type from a node in the AST
40: *
41: * @author kel
42: */
43: public class DefinitionFinder extends
44:                 QuestionAnswerAdaptor<DefinitionFinder.Newquestion, PDefinition>
45: {
46:         public static class Newquestion
47:         {
48:                 final ILexNameToken sought;
49:                 final String fromModule;
50:
51:                 public Newquestion(ILexNameToken sought, String fromModule)
52:                 {
53:                         this.fromModule = fromModule;
54:                         this.sought = sought;
55:                 }
56:
57:         }
58:
59:         protected ITypeCheckerAssistantFactory af;
60:
61:         public DefinitionFinder(ITypeCheckerAssistantFactory af)
62:         {
63:                 this.af = af;
64:         }
65:
66:         @Override
67:         public PDefinition defaultSClassDefinition(SClassDefinition node,
68:                         Newquestion question) throws AnalysisException
69:         {
70:                 if (!question.sought.getExplicit()
71:                                 && question.sought.getName().equals(node.getName().getName())
72:                                 || question.sought.equals(node.getName().getClassName()))
73:                 {
74:                         return node; // Class referred to as "A" or "CLASS`A"
75:                 }
76:
77:                 PDefinition def = af.createPDefinitionAssistant().findType(node.getDefinitions(), question.sought, null);
78:
79:                 if (def == null)
80:                 {
81:                         for (PDefinition d : node.getAllInheritedDefinitions())
82:                         {
83:                                 PDefinition indef = af.createPDefinitionAssistant().findType(d, question.sought, null);
84:
85:                                 if (indef != null)
86:                                 {
87:                                         def = indef;
88:                                         break;
89:                                 }
90:                         }
91:                 }
92:
93:                 return def;
94:         }
95:
96:         @Override
97:         public PDefinition caseAImportedDefinition(AImportedDefinition node,
98:                         Newquestion question) throws AnalysisException
99:         {
100:                 // We can only find an import if it is being sought from the module that
101:                 // imports it.
102:
103: //                if (question.fromModule != null
104: //                                && !node.getLocation().getModule().equals(question.fromModule))
105: //                {
106: //                        return null; // Someone else's import
107: //                }
108:
109:                 PDefinition def = af.createPDefinitionAssistant().findType(node.getDef(), question.sought, question.fromModule);
110:
111:                 if (def != null)
112:                 {
113:                         af.createPDefinitionAssistant().markUsed(node);
114:                 }
115:
116:                 return def;
117:         }
118:
119:         @Override
120:         public PDefinition caseAInheritedDefinition(AInheritedDefinition node,
121:                         Newquestion question) throws AnalysisException
122:         {
123:                 if (node.getSuperdef() instanceof ATypeDefinition
124:                                 && question.sought.equals(node.getName()))
125:                 {
126:                         return node;
127:                 }
128:
129:                 return null;
130:         }
131:
132:         @Override
133:         public PDefinition caseARenamedDefinition(ARenamedDefinition node,
134:                         Newquestion question) throws AnalysisException
135:         {
136:                 // We can only find an import if it is being sought from the module that
137:                 // imports it.
138:
139: //                if (question.fromModule != null
140: //                                && !node.getLocation().getModule().equals(question.fromModule))
141: //                {
142: //                        return null; // Someone else's import
143: //                }
144:
145:                 PDefinition renamed = af.createPDefinitionAssistant().findName(node, question.sought, NameScope.TYPENAME);
146:
147:                 if (renamed != null && node.getDef() instanceof ATypeDefinition)
148:                 {
149:                         af.createPDefinitionAssistant().markUsed(node.getDef());
150:                         return renamed;
151:                 } else
152:                 {
153:                         return af.createPDefinitionAssistant().findType(node.getDef(), question.sought, question.fromModule);
154:                 }
155:         }
156:
157:         @Override
158:         public PDefinition caseAStateDefinition(AStateDefinition node,
159:                         Newquestion question) throws AnalysisException
160:         {
161:                 if (af.createPDefinitionAssistant().findName(node, question.sought, NameScope.STATE) != null)
162:                 {
163:                         return node;
164:                 }
165:
166:                 return null;
167:         }
168:
169:         @Override
170:         public PDefinition caseATypeDefinition(ATypeDefinition node,
171:                         Newquestion question) throws AnalysisException
172:         {
173:                 if (!node.getComposeDefinitions().isEmpty())
174:                 {
175:                         for (PDefinition compose : node.getComposeDefinitions())
176:                         {
177:                                 PDefinition found = af.createPDefinitionAssistant().findNameBaseCase(compose, question.sought, NameScope.TYPENAME);
178:
179:                                 if (found != null)
180:                                 {
181:                                         return found;
182:                                 }
183:                         }
184:                 }
185:
186:                 return af.createPDefinitionAssistant().findNameBaseCase(node, question.sought, NameScope.TYPENAME);
187:         }
188:
189:         @Override
190:         public PDefinition defaultPDefinition(PDefinition node, Newquestion question)
191:                         throws AnalysisException
192:         {
193:                 return null;
194:         }
195:
196:         @Override
197:         public PDefinition createNewReturnValue(INode node, Newquestion question)
198:         {
199:                 assert false : "default case should never happen in TypeFinder";
200:                 return null;
201:         }
202:
203:         @Override
204:         public PDefinition createNewReturnValue(Object node, Newquestion question)
205:         {
206:                 assert false : "default case should never happen in TypeFinder";
207:                 return null;
208:         }
209:
210: }