More const preparation: allow type casts to follow qualification.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jitcraft_array_h_
20 #define _include_jitcraft_array_h_
25 #include "FixedArray.h"
31 class Array : public Object
33 friend class ArrayMap;
35 // Note: we do not write barrier |elements_|, since it can be set to
36 // arbitrary pointers.
44 void updateLengthAndElements(Zone *zone, unsigned length, Object *array) {
46 write(zone, &objelements_, array);
50 static Array *NewFixed(Zone *zone, Handle<ArrayMap> map, Heap::Tenure = Heap::Tenure_Default);
51 static Array *NewString(Zone *zone, Handle<ArrayMap> map, char *buffer, size_t length, Heap::Tenure = Heap::Tenure_Default);
52 static Array *NewSized(Zone *zone, Handle<ArrayMap> map, int sizes[MAX_ARRAY_DEPTH], Heap::Tenure = Heap::Tenure_Default);
54 static Array *New1D(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure = Heap::Tenure_Default);
55 static Array *NewEmpty(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
56 static Array *NewRaw(Zone *zone, Handle<ArrayMap> map, unsigned nelements, Heap::Tenure tenure);
58 static Array *NewExternal(Zone *zone,
62 Heap::Tenure tenure = Heap::Tenure_Default);
64 // Copies an array from one reference to another, obeying language semantics.
65 static bool ShallowCopy(Zone *zone, Handle<Array> dest, Handle<Array> source);
67 // Creates a full duplicate of an array, following all array references as if
68 // they were fixed. Array references inside structs or objects use ShallowCopy
69 // semantics, as this is only intended for duplicating array literals, which
70 // cannot contain structs.
71 static Array *DeepCopy(Zone *zone, Handle<Array> source, Heap::Tenure tenure);
77 inline ArrayType *type();
79 inline int *int32Vec();
80 inline float *floatVec();
84 bool resize(Zone *zone, size_t newLength);
86 static inline size_t SizeForElement(ArrayType *type) {
87 if (type->contained()->isTraceable())
88 return sizeof(ke::Object *);
89 if (type->contained()->isInt32())
91 if (type->contained()->primitive() == PrimitiveType_Char)
93 assert(type->contained()->isFloat());
97 inline size_t objSize(ArrayMap *map);
99 int readInt32(size_t index) {
100 assert(index < length());
101 return int32Vec()[index];
103 void writeInt32(size_t index, int i) {
104 assert(index < length());
105 int32Vec()[index] = i;
107 char readChar(size_t index) {
108 assert(index < length());
109 return chars()[index];
111 void writeChar(size_t index, char c) {
112 assert(index < length());
115 float readFloat(size_t index) {
116 assert(index < length());
117 return floatVec()[index];
119 void writeFloat(size_t index, float f) {
120 assert(index < length());
121 floatVec()[index] = f;
123 Object *readObject(size_t index) {
124 return FixedArray::cast(objelements_)->at(index);
126 void writeObject(Zone *zone, size_t index, Object *ptr) {
127 FixedArray::cast(objelements_)->set(zone, index, ptr);
131 static inline size_t offsetOfElements() {
132 return offsetof(Array, objelements_);
134 // End GC Descriptor.
136 static Array *cast(Object *obj) {
137 assert(!obj || obj->is(MapKind_Array));
138 return reinterpret_cast<Array *>(obj);
142 class ArrayMap : public Map
144 Barriered<ArrayType> type_;
147 static ArrayMap *New(Zone *zone, Handle<ArrayType> type);
148 static ArrayMap *Attach(Zone *zone, Handle<ArrayType> type);
155 static inline size_t offsetOfType() {
156 return OFFSETOF(ArrayMap, type_);
158 // End GC Descriptor.
160 static ArrayMap *cast(Object *obj) {
161 assert(!obj || obj->is(MapKind_MetaArray));
162 return reinterpret_cast<ArrayMap *>(obj);
169 assert(!type()->contained()->isTraceable());
170 assert(type()->hasInlineElements() ||
171 type()->isExternalArray() ||
173 ByteArray::cast((Object *)elements_));
175 return Address(elements_) + ByteArray::offsetToElements();
181 assert(type()->contained()->isInt32());
182 return reinterpret_cast<int *>(bytes());
188 assert(type()->contained()->isFloat());
189 return reinterpret_cast<float *>(bytes());
195 assert(type()->isCharArray());
196 return reinterpret_cast<char *>(bytes());
202 return ArrayMap::cast(map())->type();
206 Array::objSize(ArrayMap *map)
208 assert(this->map() == map);
209 if (map->type()->hasInlineElements())
210 return sizeof(Array) + Array::SizeForElement(map->type()) * length();
211 return sizeof(Array);
216 #endif // _include_jitcraft_array_h_