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.types;
25
26 import org.overturetool.vdmj.definitions.AccessSpecifier;
27 import org.overturetool.vdmj.definitions.TypeDefinition;
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.typechecker.Environment;
32 import org.overturetool.vdmj.typechecker.TypeCheckException;
33
34 public class SetType extends Type
35 {
36 private static final long serialVersionUID = 1L;
37 public Type setof;
38 public final boolean empty;
39
40 public SetType(LexLocation location, Type type)
41 {
42 super(location);
43 this.setof = type;
44 this.empty = false;
45 }
46
47 public SetType(LexLocation location)
48 {
49 super(location);
50 this.setof = new UnknownType(location);
51 this.empty = true;
52 }
53
54 @Override
55 public boolean narrowerThan(AccessSpecifier accessSpecifier)
56 {
57 return setof.narrowerThan(accessSpecifier);
58 }
59
60 @Override
61 public boolean isSet()
62 {
63 return true;
64 }
65
66 @Override
67 public SetType getSet()
68 {
69 return this;
70 }
71
72 @Override
73 public void unResolve()
74 {
75 if (!resolved) return; else { resolved = false; }
76 setof.unResolve();
77 }
78
79 @Override
80 public Type typeResolve(Environment env, TypeDefinition root)
81 {
82 if (resolved) return this; else { resolved = true; }
83
84 try
85 {
86 setof = setof.typeResolve(env, root);
87 if (root != null) root.infinite = false;
88 return this;
89 }
90 catch (TypeCheckException e)
91 {
92 unResolve();
93 throw e;
94 }
95 }
96
97 @Override
98 public Type polymorph(LexNameToken pname, Type actualType)
99 {
100 return new SetType(location, setof.polymorph(pname, actualType));
101 }
102
103 @Override
104 public void typeParamCheck(LexNameList typeParams)
105 {
106 setof.typeParamCheck(typeParams);
107 }
108
109 @Override
110 public String toDisplay()
111 {
112 return empty ? "{}" : "set of (" + setof + ")";
113 }
114
115 @Override
116 public boolean equals(Object other)
117 {
118 while (other instanceof BracketType)
119 {
120 other = ((BracketType)other).type;
121 }
122
123 if (other instanceof SetType)
124 {
125 SetType os = (SetType)other;
126
127 return empty || os.empty || setof.equals(os.setof);
128 }
129
130 return false;
131 }
132
133 @Override
134 public int hashCode()
135 {
136 return empty ? 0 : setof.hashCode();
137 }
138 }