Add |from x import names| support.
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_jc_structure_h_
20 #define _include_jc_structure_h_
34 // When describing composite structures, the GC must be able to trace interior
35 // references to Objects. To make this possible, maps that describe composite
36 // structures must provide a list of offsets into their objects that are
37 // traceable. StructureBufferBuilder assists with this.
38 class StructureBufferBuilder
41 StructureBufferBuilder();
43 bool add(Handle<Type> type, unsigned *offset);
45 unsigned structureSize() const {
48 unsigned traceableOffsetCount() const {
49 return offsets_.length();
51 const unsigned *offsets() const {
52 return offsets_.buffer();
56 Vector<unsigned> offsets_;
60 // Represents a property descriptor for a structure field.
61 class Descriptor : public Object
71 // The name of the field.
72 Barriered<String> name_;
74 // The type of the field.
75 Barriered<Type> type_;
77 // The offset into an object at which this field can be accessed.
80 Visibility visibility_;
83 static Descriptor *New(Zone *zone, Handle<String> name, Handle<Type> type, Visibility visibility);
92 assert(offset_ != unsigned(-1));
95 void setOffset(unsigned offset) {
96 assert(offset_ == unsigned(-1));
100 // Begin GC descriptor.
101 static inline size_t offsetOfName() {
102 return OFFSETOF(Descriptor, name_);
104 static inline size_t offsetOfType() {
105 return OFFSETOF(Descriptor, type_);
107 // End GC descriptor.
109 static inline Descriptor *cast(Object *obj) {
110 assert(!obj || obj->is(MapKind_Descriptor));
111 return reinterpret_cast<Descriptor *>(obj);
115 class StructMap : public Map
117 unsigned structureSize_;
118 unsigned traceOffsetCount_;
119 Barriered<StructType> type_;
121 // List of descriptors which must be allocated upon initialization.
122 Barriered<FixedArray> composed_;
125 static StructMap *New(Zone *zone, const StructureBufferBuilder &builder, Handle<StructType> type);
127 unsigned structureSize() {
128 return structureSize_;
130 unsigned traceOffsetCount() {
131 return traceOffsetCount_;
133 const unsigned *traceOffsets() {
134 return reinterpret_cast<unsigned *>(this + 1);
137 return sizeof(StructMap) + sizeof(unsigned) * traceOffsetCount();
142 FixedArray *composed() {
146 void setType(StructType *type) {
151 // Begin GC descriptors.
152 static inline size_t offsetOfType() {
153 return OFFSETOF(StructMap, type_);
155 static inline size_t offsetOfComposed() {
156 return OFFSETOF(StructMap, composed_);
158 // End GC descriptors.
160 static inline StructMap *cast(Object *obj) {
161 assert(!obj || obj->is(MapKind_MetaStruct));
162 return reinterpret_cast<StructMap *>(obj);
166 class Struct : public Object
169 static Struct *New(Zone *zone, Handle<StructMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
171 static bool Copy(Zone *zone, Handle<Struct> dest, Handle<Struct> src);
174 return StructMap::cast(Object::map());
177 void *ref(Descriptor *desc) {
178 return reinterpret_cast<Address>(this) + offsetOfSlotOffset(desc->offset());
181 static inline size_t SizeFor(StructMap *map) {
182 return Max(sizeof(Struct) + map->structureSize(), kMinObjectSize);
185 template <typename T> T get(Descriptor *desc) {
186 return *reinterpret_cast<T *>(ref(desc));
189 // This must NOT be used with any Object * type!
190 // :TODO: prevent with type system
191 template <typename T> void set(Descriptor *desc, const T &t) {
192 *reinterpret_cast<T *>(ref(desc)) = t;
195 void set(Zone *zone, Descriptor *desc, Object *obj) {
196 // :TODO: write barrier
197 *reinterpret_cast<Object **>(
198 reinterpret_cast<Address>(this) +
199 offsetOfSlotOffset(desc->offset())) = obj;
202 static inline size_t offsetOfSlotOffset(unsigned offset) {
203 return sizeof(Struct) + offset;
206 static inline Struct *cast(Object *obj) {
207 assert(!obj || obj->is(MapKind_Struct));
208 return reinterpret_cast<Struct *>(obj);
214 #endif // _include_jc_structure_h_