comparison graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Shape.java @ 18407:f439fdb137a3

Truffle: initial commit of object API
author Andreas Woess <andreas.woess@jku.at>
date Tue, 18 Nov 2014 16:18:45 +0100
parents
children f929f601bdca
comparison
equal deleted inserted replaced
18406:194041c3fdab 18407:f439fdb137a3
1 /*
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.api.object;
26
27 import java.util.*;
28
29 import com.oracle.truffle.api.*;
30
31 /**
32 * Shape objects create a mapping of Property objects to Locations. Shapes are immutable; adding or
33 * deleting a property yields a new Shape which links to the old one. This allows inline caching to
34 * simply check the identity of an object's Shape to determine if the cache is valid. There is one
35 * exception to this immutability, the transition map, but that is used simply to assure that an
36 * identical series of property additions and deletions will yield the same Shape object.
37 *
38 * @see DynamicObject
39 * @see Property
40 * @see Location
41 */
42 public abstract class Shape {
43 /**
44 * Get a property entry by key.
45 *
46 * @param key the identifier to look up
47 * @return a Property object, or null if not found
48 */
49 public abstract Property getProperty(Object key);
50
51 /**
52 * Add a new property in the map, yielding a new or cached Shape object.
53 *
54 * @param property the property to add
55 * @return the new Shape
56 */
57 public abstract Shape addProperty(Property property);
58
59 /**
60 * An {@link Iterable} over the shape's properties in insertion order.
61 */
62 public abstract Iterable<Property> getProperties();
63
64 /**
65 * Get a list of properties that this Shape stores.
66 *
67 * @return list of properties
68 */
69 public abstract List<Property> getPropertyList(Pred<Property> filter);
70
71 /**
72 * Get a list of all properties that this Shape stores.
73 *
74 * @return list of properties
75 */
76 public abstract List<Property> getPropertyList();
77
78 /**
79 * Returns all (also hidden) property objects in this shape.
80 *
81 * @param ascending desired order ({@code true} for insertion order, {@code false} for reverse
82 * insertion order)
83 */
84 public abstract List<Property> getPropertyListInternal(boolean ascending);
85
86 /**
87 * Get a filtered list of property keys in insertion order.
88 */
89 public abstract List<Object> getKeyList(Pred<Property> filter);
90
91 /**
92 * Get a list of all property keys in insertion order.
93 */
94 public abstract List<Object> getKeyList();
95
96 /**
97 * Get all property keys in insertion order.
98 */
99 public abstract Iterable<Object> getKeys();
100
101 /**
102 * Get an assumption that the shape is valid.
103 */
104 public abstract Assumption getValidAssumption();
105
106 /**
107 * Check whether this shape is valid.
108 */
109 public abstract boolean isValid();
110
111 /**
112 * Get an assumption that the shape is a leaf.
113 */
114 public abstract Assumption getLeafAssumption();
115
116 /**
117 * Check whether this shape is a leaf in the transition graph, i.e. transitionless.
118 */
119 public abstract boolean isLeaf();
120
121 /**
122 * @return the parent shape or {@code null} if none.
123 */
124 public abstract Shape getParent();
125
126 /**
127 * Check whether the shape has a property with the given key.
128 */
129 public abstract boolean hasProperty(Object key);
130
131 /**
132 * Remove the given property from the shape.
133 */
134 public abstract Shape removeProperty(Property property);
135
136 /**
137 * Replace a property in the shape.
138 */
139 public abstract Shape replaceProperty(Property oldProperty, Property newProperty);
140
141 /**
142 * Get the last added property.
143 */
144 public abstract Property getLastProperty();
145
146 public abstract int getId();
147
148 /**
149 * Append the property, relocating it to the next allocated location.
150 */
151 public abstract Shape append(Property oldProperty);
152
153 /**
154 * Obtain an {@link Allocator} instance for the purpose of allocating locations.
155 */
156 public abstract Allocator allocator();
157
158 /**
159 * For copying over properties after exchanging the prototype of an object.
160 */
161 public abstract Shape copyOverPropertiesInternal(Shape destination);
162
163 /**
164 * Get number of properties in this shape.
165 */
166 public abstract int getPropertyCount();
167
168 /**
169 * Get the shape's operations.
170 */
171 public abstract ObjectType getObjectType();
172
173 /**
174 * Get the root shape.
175 */
176 public abstract Shape getRoot();
177
178 /**
179 * Check whether this shape is identical to the given shape.
180 */
181 public abstract boolean check(DynamicObject subject);
182
183 /**
184 * Get the shape's layout.
185 */
186 public abstract Layout getLayout();
187
188 /**
189 * Get the shape's custom data.
190 */
191 public abstract Object getData();
192
193 /**
194 * Get the shape's shared data.
195 */
196 public abstract Object getSharedData();
197
198 /**
199 * Query whether the shape has a transition with the given key.
200 */
201 public abstract boolean hasTransitionWithKey(Object key);
202
203 /**
204 * Clone off a separate shape with new shared data.
205 */
206 public abstract Shape createSeparateShape(Object sharedData);
207
208 /**
209 * Change the shape's type, yielding a new shape.
210 */
211 public abstract Shape changeType(ObjectType newOps);
212
213 /**
214 * Reserve the primitive extension array field.
215 */
216 public abstract Shape reservePrimitiveExtensionArray();
217
218 /**
219 * Create a new {@link DynamicObject} instance with this shape.
220 */
221 public abstract DynamicObject newInstance();
222
223 /**
224 * Create a {@link DynamicObjectFactory} for creating instances of this shape.
225 */
226 public abstract DynamicObjectFactory createFactory();
227
228 /**
229 * Get mutex object shared by related shapes, i.e. shapes with a common root.
230 */
231 public abstract Object getMutex();
232
233 public abstract int getObjectArraySize();
234
235 public abstract int getObjectFieldSize();
236
237 public abstract int getPrimitiveArraySize();
238
239 public abstract int getPrimitiveFieldSize();
240
241 public abstract int getObjectArrayCapacity();
242
243 public abstract int getPrimitiveArrayCapacity();
244
245 public abstract boolean hasPrimitiveArray();
246
247 /**
248 * Are these two shapes related, i.e. do they have the same root?
249 *
250 * @param other Shape to compare to
251 * @return true if one shape is an upcast of the other, or the Shapes are equal
252 */
253 public abstract boolean isRelated(Shape other);
254
255 public abstract Shape tryMerge(Shape other);
256
257 public <R> R accept(ShapeVisitor<R> visitor) {
258 return visitor.visitShape(this);
259 }
260
261 public abstract static class Allocator {
262 protected abstract Location locationForValue(Object value, boolean useFinal, boolean nonNull);
263
264 public final Location locationForValue(Object value) {
265 return locationForValue(value, false, value != null);
266 }
267
268 public final Location locationForValue(Object value, EnumSet<LocationModifier> modifiers) {
269 assert value != null || !modifiers.contains(LocationModifier.NonNull);
270 return locationForValue(value, modifiers.contains(LocationModifier.Final), modifiers.contains(LocationModifier.NonNull));
271 }
272
273 protected abstract Location locationForType(Class<?> type, boolean useFinal, boolean nonNull);
274
275 public final Location locationForType(Class<?> type) {
276 return locationForType(type, false, false);
277 }
278
279 public final Location locationForType(Class<?> type, EnumSet<LocationModifier> modifiers) {
280 return locationForType(type, modifiers.contains(LocationModifier.Final), modifiers.contains(LocationModifier.NonNull));
281 }
282
283 public abstract Location constantLocation(Object value);
284
285 public abstract Location declaredLocation(Object value);
286
287 public abstract Allocator addLocation(Location location);
288 }
289
290 /**
291 * Represents a predicate (boolean-valued function) of one argument. For Java 7 compatibility.
292 *
293 * @param <T> the type of the input to the predicate
294 */
295 public interface Pred<T> {
296 /**
297 * Evaluates this predicate on the given argument.
298 *
299 * @param t the input argument
300 * @return {@code true} if the input argument matches the predicate, otherwise {@code false}
301 */
302 boolean test(T t);
303 }
304 }