comparison graal/com.oracle.truffle.object/src/com/oracle/truffle/object/LocationImpl.java @ 18408:2c3666f44855

Truffle: initial commit of object API implementation
author Andreas Woess <andreas.woess@jku.at>
date Tue, 18 Nov 2014 23:19:43 +0100
parents
children e9cbe1618733
comparison
equal deleted inserted replaced
18407:f439fdb137a3 18408:2c3666f44855
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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.object;
24
25 import com.oracle.truffle.api.object.*;
26
27 public abstract class LocationImpl extends Location {
28 @Override
29 public void set(DynamicObject store, Object value, Shape shape) throws IncompatibleLocationException, FinalLocationException {
30 setInternal(store, value);
31 }
32
33 @Override
34 protected final Object getInternal(DynamicObject store) {
35 throw new UnsupportedOperationException();
36 }
37
38 @Override
39 protected abstract void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException;
40
41 @Override
42 public final boolean canSet(DynamicObject store, Object value) {
43 return canStore(value) && canStoreFinal(store, value);
44 }
45
46 @Override
47 public boolean canStore(Object value) {
48 return true;
49 }
50
51 @SuppressWarnings("unused")
52 protected boolean canStoreFinal(DynamicObject store, Object value) {
53 return true;
54 }
55
56 public interface EffectivelyFinalLocation<T extends Location> {
57 T toNonFinalLocation();
58 }
59
60 public interface TypedObjectLocation<T extends Location & ObjectLocation> extends ObjectLocation {
61 T toUntypedLocation();
62 }
63
64 public interface InternalLongLocation extends LongLocation {
65 void setLongInternal(DynamicObject store, long value);
66 }
67
68 @Override
69 public boolean isFinal() {
70 return false;
71 }
72
73 @Override
74 public boolean isConstant() {
75 return false;
76 }
77
78 @Override
79 public int hashCode() {
80 final int prime = 31;
81 int result = 1;
82 result = prime * result + (isFinal() ? 1231 : 1237);
83 return result;
84 }
85
86 @Override
87 public boolean equals(Object obj) {
88 if (this == obj) {
89 return true;
90 }
91 if (obj == null) {
92 return false;
93 }
94 if (getClass() != obj.getClass()) {
95 return false;
96 }
97 Location other = (Location) obj;
98 if (isFinal() != other.isFinal()) {
99 return false;
100 }
101 return true;
102 }
103
104 @Override
105 public String toString() {
106 String finalString = isFinal() ? "f" : "";
107 String typeString = this instanceof IntLocation ? "i" : (this instanceof DoubleLocation ? "d" : (this instanceof BooleanLocation ? "b"
108 : (this instanceof TypedLocation ? ((TypedLocation) this).getType().getSimpleName() : "o")));
109 return finalString + typeString + getWhereString();
110 }
111
112 protected String getWhereString() {
113 return "";
114 }
115
116 /**
117 * Get the number of object array elements this location requires.
118 */
119 public int objectArrayCount() {
120 return 0;
121 }
122
123 /**
124 * Get the number of in-object {@link Object} fields this location requires.
125 */
126 public int objectFieldCount() {
127 return 0;
128 }
129
130 /**
131 * Get the number of in-object primitive fields this location requires.
132 */
133 public int primitiveFieldCount() {
134 return 0;
135 }
136
137 /**
138 * Get the number of primitive array elements this location requires.
139 */
140 public int primitiveArrayCount() {
141 return 0;
142 }
143
144 /**
145 * Boxed values need to be compared by value not by reference.
146 *
147 * The first parameter should be the one with the more precise type information.
148 *
149 * For sets to final locations, otherValue.equals(thisValue) seems more beneficial, since we
150 * usually know more about the value to be set.
151 */
152 public static boolean valueEquals(Object val1, Object val2) {
153 return val1 == val2 || (val1 != null && val1.equals(val2));
154 }
155 }