Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Location.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 |
comparison
equal
deleted
inserted
replaced
18406:194041c3fdab | 18407:f439fdb137a3 |
---|---|
1 /* | |
2 * Copyright (c) 2013, 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 com.oracle.truffle.api.*; | |
28 | |
29 /** | |
30 * Property location. | |
31 * | |
32 * @see Shape | |
33 * @see Property | |
34 * @see DynamicObject | |
35 */ | |
36 public abstract class Location implements BaseLocation { | |
37 protected static IncompatibleLocationException incompatibleLocation() throws IncompatibleLocationException { | |
38 CompilerDirectives.transferToInterpreterAndInvalidate(); | |
39 throw new IncompatibleLocationException(); | |
40 } | |
41 | |
42 protected static FinalLocationException finalLocation() throws FinalLocationException { | |
43 CompilerDirectives.transferToInterpreterAndInvalidate(); | |
44 throw new FinalLocationException(); | |
45 } | |
46 | |
47 public final Object get(DynamicObject store, Shape shape) { | |
48 return get(store, checkShape(store, shape)); | |
49 } | |
50 | |
51 public Object get(DynamicObject store, boolean condition) { | |
52 return getInternal(store); | |
53 } | |
54 | |
55 public void set(DynamicObject store, Object value, Shape shape) throws IncompatibleLocationException, FinalLocationException { | |
56 setInternal(store, value); | |
57 } | |
58 | |
59 public final void set(DynamicObject store, Object value, Shape oldShape, Shape newShape) throws IncompatibleLocationException { | |
60 if (canStore(value)) { | |
61 store.setShapeAndGrow(oldShape, newShape); | |
62 try { | |
63 setInternal(store, value); | |
64 } catch (IncompatibleLocationException ex) { | |
65 throw new IllegalStateException(); | |
66 } | |
67 } else { | |
68 throw incompatibleLocation(); | |
69 } | |
70 } | |
71 | |
72 public final void set(DynamicObject store, Object value) throws IncompatibleLocationException, FinalLocationException { | |
73 set(store, value, null); | |
74 } | |
75 | |
76 protected abstract Object getInternal(DynamicObject store); | |
77 | |
78 /** | |
79 * Like {@link #set(DynamicObject, Object, Shape)}, but does not invalidate final locations. For | |
80 * internal use only and subject to change, use {@link DynamicObjectFactory} to create objects | |
81 * with predefined properties. | |
82 * | |
83 * @throws IncompatibleLocationException if value is of non-assignable type | |
84 */ | |
85 protected abstract void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException; | |
86 | |
87 /** | |
88 * Returns {@code true} if the location can be set to the value. | |
89 * | |
90 * @param store the receiver object | |
91 * @param value the value in question | |
92 */ | |
93 public boolean canSet(DynamicObject store, Object value) { | |
94 return canStore(value); | |
95 } | |
96 | |
97 /** | |
98 * Returns {@code true} if the location is compatible with the value. | |
99 * | |
100 * The value may still be rejected if {@link #canSet(DynamicObject, Object)} returns false. | |
101 * | |
102 * @param value the value in question | |
103 */ | |
104 public boolean canStore(Object value) { | |
105 return true; | |
106 } | |
107 | |
108 /** | |
109 * Returns {@code true} if this is a final location, i.e. readonly once set. | |
110 */ | |
111 public boolean isFinal() { | |
112 return false; | |
113 } | |
114 | |
115 /** | |
116 * Returns {@code true} if this is an immutable constant location. | |
117 */ | |
118 public boolean isConstant() { | |
119 return false; | |
120 } | |
121 | |
122 /* | |
123 * Abstract to force overriding. | |
124 */ | |
125 @Override | |
126 public abstract int hashCode(); | |
127 | |
128 /* | |
129 * Abstract to force overriding. | |
130 */ | |
131 @Override | |
132 public abstract boolean equals(Object obj); | |
133 | |
134 protected static boolean checkShape(DynamicObject store, Shape shape) { | |
135 return store.getShape() == shape; | |
136 } | |
137 } |