View Javadoc

1   /********************************************************************************
2    *
3    *	Copyright (c) 2008 Fujitsu Services Ltd.
4    *
5    *	Author: Nick Battle
6    *
7    *	This file is part of VDMJ.
8    *
9    *	VDMJ is free software: you can redistribute it and/or modify
10   *	it under the terms of the GNU General Public License as published by
11   *	the Free Software Foundation, either version 3 of the License, or
12   *	(at your option) any later version.
13   *
14   *	VDMJ is distributed in the hope that it will be useful,
15   *	but WITHOUT ANY WARRANTY; without even the implied warranty of
16   *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   *	GNU General Public License for more details.
18   *
19   *	You should have received a copy of the GNU General Public License
20   *	along with VDMJ.  If not, see <http://www.gnu.org/licenses/>.
21   *
22   ******************************************************************************/
23  
24  package org.overturetool.vdmj.definitions;
25  
26  import java.util.List;
27  
28  import org.overturetool.vdmj.lex.LexLocation;
29  import org.overturetool.vdmj.lex.LexNameList;
30  import org.overturetool.vdmj.lex.LexNameToken;
31  import org.overturetool.vdmj.patterns.MultipleBind;
32  import org.overturetool.vdmj.typechecker.Environment;
33  import org.overturetool.vdmj.typechecker.NameScope;
34  import org.overturetool.vdmj.typechecker.Pass;
35  import org.overturetool.vdmj.types.Type;
36  import org.overturetool.vdmj.types.UnknownType;
37  import org.overturetool.vdmj.util.Utils;
38  
39  
40  /***
41   * A class to hold a multiple bind list definition.
42   */
43  
44  public class MultiBindListDefinition extends Definition
45  {
46  	private static final long serialVersionUID = 1L;
47  	public final List<MultipleBind> bindings;
48  	private DefinitionList defs = null;
49  
50  	public MultiBindListDefinition(LexLocation location, List<MultipleBind> bindings)
51  	{
52  		super(Pass.DEFS, location, null, null);
53  		this.bindings = bindings;
54  	}
55  
56  	@Override
57  	public String toString()
58  	{
59  		return "def " + Utils.listToString(bindings);
60  	}
61  
62  	@Override
63  	public void typeCheck(Environment base, NameScope scope)
64  	{
65  		defs = new DefinitionList();
66  
67  		for (MultipleBind mb: bindings)
68  		{
69  			Type type = mb.typeCheck(base, scope);
70  			defs.addAll(mb.getDefinitions(type, scope));
71  		}
72  
73  		defs.typeCheck(base, scope);
74  	}
75  
76  	@Override
77  	public Definition findName(LexNameToken sought, NameScope incState)
78  	{
79  		if (defs != null)
80  		{
81  			Definition def = defs.findName(sought, incState);
82  
83  			if (def != null)
84  			{
85  				return def;
86  			}
87  		}
88  
89  		return null;
90  	}
91  
92  	@Override
93  	public Type getType()
94  	{
95  		if (defs != null && defs.size() == 1)
96  		{
97  			return defs.get(0).getType();
98  		}
99  
100 		return new UnknownType(location);
101 	}
102 
103 	@Override
104 	public void unusedCheck()
105 	{
106 		if (defs != null)
107 		{
108 			defs.unusedCheck();
109 		}
110 	}
111 
112 	@Override
113 	public DefinitionList getDefinitions()
114 	{
115 		return defs == null ? new DefinitionList() : defs;
116 	}
117 
118 	@Override
119 	public LexNameList getVariableNames()
120 	{
121 		return defs == null ? new LexNameList() : defs.getVariableNames();
122 	}
123 
124 	@Override
125 	public String kind()
126 	{
127 		return "bind";
128 	}
129 }