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 org.overturetool.vdmj.lex.LexNameList;
27  import org.overturetool.vdmj.lex.LexNameToken;
28  import org.overturetool.vdmj.runtime.Context;
29  import org.overturetool.vdmj.typechecker.Environment;
30  import org.overturetool.vdmj.typechecker.NameScope;
31  import org.overturetool.vdmj.types.Type;
32  import org.overturetool.vdmj.values.NameValuePair;
33  import org.overturetool.vdmj.values.NameValuePairList;
34  
35  /***
36   * A class to hold a renamed import definition.
37   */
38  
39  public class RenamedDefinition extends Definition
40  {
41  	private static final long serialVersionUID = 1L;
42  	public final Definition def;
43  
44  	public RenamedDefinition(LexNameToken name, Definition def, NameScope scope)
45  	{
46  		super(def.pass, name.location, name, scope);
47  		this.def = def;
48  	}
49  
50  	@Override
51  	public Type getType()
52  	{
53  		return def.getType();
54  	}
55  
56  	@Override
57  	public String toString()
58  	{
59  		return def + " renamed " + name;
60  	}
61  
62  	@Override
63  	public void typeCheck(Environment base, NameScope scope)
64  	{
65  		def.typeCheck(base, scope);
66  	}
67  
68  	@Override
69  	public DefinitionList getDefinitions()
70  	{
71  		return new DefinitionList(this);
72  	}
73  
74  	@Override
75  	public LexNameList getVariableNames()
76  	{
77  		LexNameList both = new LexNameList(name);
78  		both.add(def.name);
79  		return both;
80  	}
81  
82  	@Override
83  	public Definition findName(LexNameToken sought, NameScope scope)
84  	{
85  		Definition renamed = super.findName(sought, scope);
86  
87  		if (renamed != null)
88  		{
89  			def.markUsed();
90  			return renamed;
91  		}
92  		else
93  		{
94  			return def.findName(sought, scope);
95  		}
96  	}
97  
98  	@Override
99  	public Definition findType(LexNameToken sought)
100 	{
101 		Definition renamed = findName(sought, NameScope.TYPENAME);
102 
103 		if (renamed != null)
104 		{
105 			def.markUsed();
106 			return renamed;
107 		}
108 		else
109 		{
110 			return def.findType(sought);
111 		}
112 	}
113 
114 	@Override
115 	public NameValuePairList getNamedValues(Context ctxt)
116 	{
117 		NameValuePairList renamed = new NameValuePairList();
118 
119 		for (NameValuePair nv: def.getNamedValues(ctxt))
120 		{
121 			// We exclude any name from the definition other than the one
122 			// explicitly renamed. Otherwise, generated names like pre_f
123 			// come through and are not renamed.
124 
125 			if (nv.name.equals(def.name))
126 			{
127 				renamed.add(new NameValuePair(name, nv.value));
128 			}
129 		}
130 
131 		return renamed;
132 	}
133 
134 	@Override
135 	public String kind()
136 	{
137 		return def.kind();
138 	}
139 
140 	@Override
141 	public boolean isFunctionOrOperation()
142 	{
143 		return def.isFunctionOrOperation();
144 	}
145 
146 	@Override
147 	public boolean isCallableOperation()
148 	{
149 		return def.isCallableOperation();
150 	}
151 
152 	@Override
153 	public boolean isTypeDefinition()
154 	{
155 		return def.isTypeDefinition();
156 	}
157 
158 	@Override
159 	public boolean isValueDefinition()
160 	{
161 		return def.isValueDefinition();
162 	}
163 
164 	@Override
165 	public boolean isRuntime()
166 	{
167 		return def.isRuntime();
168 	}
169 }