src/Structures.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 234 26e3035cdffc
child 263 ba85a47ee414
permissions -rw-r--r--
Initial implementation of module imports.
[email protected]
     1
/* vim: set ts=4 sw=4 tw=99 et:
[email protected]
     2
 *
[email protected]
     3
 * Copyright (C) 2012 David Anderson
[email protected]
     4
 *
[email protected]
     5
 * This file is part of SourcePawn.
[email protected]
     6
 *
[email protected]
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
[email protected]
     8
 * the terms of the GNU General Public License as published by the Free
[email protected]
     9
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
    10
 * any later version.
[email protected]
    11
 * 
[email protected]
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
[email protected]
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    15
 *
[email protected]
    16
 * You should have received a copy of the GNU General Public License along with
[email protected]
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
[email protected]
    18
 */
[email protected]
    19
#ifndef _include_jc_structure_h_
[email protected]
    20
#define _include_jc_structure_h_
[email protected]
    21
[email protected]
    22
#include "Objects.h"
[email protected]
    23
#include "Handles.h"
[email protected]
    24
#include "Vector.h"
[email protected]
    25
#include "Heap.h"
[email protected]
    26
[email protected]
    27
namespace ke {
[email protected]
    28
[email protected]
    29
class Type;
[email protected]
    30
class String;
[email protected]
    31
class FixedArray;
[email protected]
    32
class StructType;
[email protected]
    33
[email protected]
    34
// When describing composite structures, the GC must be able to trace interior
[email protected]
    35
// references to Objects. To make this possible, maps that describe composite
[email protected]
    36
// structures must provide a list of offsets into their objects that are
[email protected]
    37
// traceable. StructureBufferBuilder assists with this.
[email protected]
    38
class StructureBufferBuilder
[email protected]
    39
{
[email protected]
    40
  public:
[email protected]
    41
    StructureBufferBuilder();
[email protected]
    42
[email protected]
    43
    bool add(Handle<Type> type, unsigned *offset);
[email protected]
    44
[email protected]
    45
    unsigned structureSize() const {
[email protected]
    46
        return size_;
[email protected]
    47
    }
[email protected]
    48
    unsigned traceableOffsetCount() const {
[email protected]
    49
        return offsets_.length();
[email protected]
    50
    }
[email protected]
    51
    const unsigned *offsets() const {
[email protected]
    52
        return offsets_.buffer();
[email protected]
    53
    }
[email protected]
    54
[email protected]
    55
  private:
[email protected]
    56
    Vector<unsigned> offsets_;
[email protected]
    57
    unsigned size_;
[email protected]
    58
};
[email protected]
    59
[email protected]
    60
// Represents a property descriptor for a structure field.
[email protected]
    61
class Descriptor : public Object
[email protected]
    62
{
[email protected]
    63
  public:
[email protected]
    64
    enum Visibility {
[email protected]
    65
        Private,
[email protected]
    66
        Protected,
[email protected]
    67
        Public
[email protected]
    68
    };
[email protected]
    69
[email protected]
    70
  private:
[email protected]
    71
    // The name of the field.
[email protected]
    72
    Barriered<String> name_;
[email protected]
    73
[email protected]
    74
    // The type of the field.
[email protected]
    75
    Barriered<Type> type_;
[email protected]
    76
[email protected]
    77
    // The offset into an object at which this field can be accessed.
[email protected]
    78
    unsigned offset_;
[email protected]
    79
[email protected]
    80
    Visibility visibility_;
[email protected]
    81
[email protected]
    82
  public:
[email protected]
    83
    static Descriptor *New(Zone *zone, Handle<String> name, Handle<Type> type, Visibility visibility);
[email protected]
    84
[email protected]
    85
    String *name() {
[email protected]
    86
        return name_;
[email protected]
    87
    }
[email protected]
    88
    Type *type() {
[email protected]
    89
        return type_;
[email protected]
    90
    }
[email protected]
    91
    unsigned offset() {
[email protected]
    92
        assert(offset_ != unsigned(-1));
[email protected]
    93
        return offset_;
[email protected]
    94
    }
[email protected]
    95
    void setOffset(unsigned offset) {
[email protected]
    96
        assert(offset_ == unsigned(-1));
[email protected]
    97
        offset_ = offset;
[email protected]
    98
    }
[email protected]
    99
[email protected]
   100
    // Begin GC descriptor.
[email protected]
   101
    static inline size_t offsetOfName() {
[email protected]
   102
        return OFFSETOF(Descriptor, name_);
[email protected]
   103
    }
[email protected]
   104
    static inline size_t offsetOfType() {
[email protected]
   105
        return OFFSETOF(Descriptor, type_);
[email protected]
   106
    }
[email protected]
   107
    // End GC descriptor.
[email protected]
   108
    
[email protected]
   109
    static inline Descriptor *cast(Object *obj) {
[email protected]
   110
        assert(!obj || obj->is(MapKind_Descriptor));
[email protected]
   111
        return reinterpret_cast<Descriptor *>(obj);
[email protected]
   112
    }
[email protected]
   113
};
[email protected]
   114
[email protected]
   115
class StructMap : public Map
[email protected]
   116
{
[email protected]
   117
    unsigned structureSize_;
[email protected]
   118
    unsigned traceOffsetCount_;
[email protected]
   119
    Barriered<StructType> type_;
[email protected]
   120
[email protected]
   121
    // List of descriptors which must be allocated upon initialization.
[email protected]
   122
    Barriered<FixedArray> composed_;
[email protected]
   123
[email protected]
   124
  public:
[email protected]
   125
    static StructMap *New(Zone *zone, const StructureBufferBuilder &builder, Handle<StructType> type);
[email protected]
   126
[email protected]
   127
    unsigned structureSize() {
[email protected]
   128
        return structureSize_;
[email protected]
   129
    }
[email protected]
   130
    unsigned traceOffsetCount() {
[email protected]
   131
        return traceOffsetCount_;
[email protected]
   132
    }
[email protected]
   133
    const unsigned *traceOffsets() {
[email protected]
   134
        return reinterpret_cast<unsigned *>(this + 1);
[email protected]
   135
    }
[email protected]
   136
    size_t objSize() {
[email protected]
   137
        return sizeof(StructMap) + sizeof(unsigned) * traceOffsetCount();
[email protected]
   138
    }
[email protected]
   139
    StructType *type() {
[email protected]
   140
        return type_;
[email protected]
   141
    }
[email protected]
   142
    FixedArray *composed() {
[email protected]
   143
        return composed_;
[email protected]
   144
    }
[email protected]
   145
[email protected]
   146
    void setType(StructType *type) {
[email protected]
   147
        assert(!type_);
[email protected]
   148
        type_ = type;
[email protected]
   149
    }
[email protected]
   150
[email protected]
   151
    // Begin GC descriptors.
[email protected]
   152
    static inline size_t offsetOfType() {
[email protected]
   153
        return OFFSETOF(StructMap, type_);
[email protected]
   154
    }
[email protected]
   155
    static inline size_t offsetOfComposed() {
[email protected]
   156
        return OFFSETOF(StructMap, composed_);
[email protected]
   157
    }
[email protected]
   158
    // End GC descriptors.
[email protected]
   159
[email protected]
   160
    static inline StructMap *cast(Object *obj) {
[email protected]
   161
        assert(!obj || obj->is(MapKind_MetaStruct));
[email protected]
   162
        return reinterpret_cast<StructMap *>(obj);
[email protected]
   163
    }
[email protected]
   164
};
[email protected]
   165
[email protected]
   166
class Struct : public Object
[email protected]
   167
{
[email protected]
   168
  public:
[email protected]
   169
    static Struct *New(Zone *zone, Handle<StructMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
[email protected]
   170
[email protected]
   171
    static bool Copy(Zone *zone, Handle<Struct> dest, Handle<Struct> src);
[email protected]
   172
[email protected]
   173
    StructMap *map() {
[email protected]
   174
        return StructMap::cast(Object::map());
[email protected]
   175
    }
[email protected]
   176
[email protected]
   177
    void *ref(Descriptor *desc) {
[email protected]
   178
        return reinterpret_cast<Address>(this) + offsetOfSlotOffset(desc->offset());
[email protected]
   179
    }
[email protected]
   180
[email protected]
   181
    static inline size_t SizeFor(StructMap *map) {
[email protected]
   182
        return Max(sizeof(Struct) + map->structureSize(), kMinObjectSize);
[email protected]
   183
    }
[email protected]
   184
[email protected]
   185
    template <typename T> T get(Descriptor *desc) {
[email protected]
   186
        return *reinterpret_cast<T *>(ref(desc));
[email protected]
   187
    }
[email protected]
   188
[email protected]
   189
    // This must NOT be used with any Object * type!
[email protected]
   190
    // :TODO: prevent with type system
[email protected]
   191
    template <typename T> void set(Descriptor *desc, const T &t) {
[email protected]
   192
        *reinterpret_cast<T *>(ref(desc)) = t;
[email protected]
   193
    }
[email protected]
   194
[email protected]
   195
    void set(Zone *zone, Descriptor *desc, Object *obj) {
[email protected]
   196
        // :TODO: write barrier
[email protected]
   197
        *reinterpret_cast<Object **>(
[email protected]
   198
            reinterpret_cast<Address>(this) +
[email protected]
   199
            offsetOfSlotOffset(desc->offset())) = obj;
[email protected]
   200
    }
[email protected]
   201
[email protected]
   202
    static inline size_t offsetOfSlotOffset(unsigned offset) {
[email protected]
   203
        return sizeof(Struct) + offset;
[email protected]
   204
    }
[email protected]
   205
[email protected]
   206
    static inline Struct *cast(Object *obj) {
[email protected]
   207
        assert(!obj || obj->is(MapKind_Struct));
[email protected]
   208
        return reinterpret_cast<Struct *>(obj);
[email protected]
   209
    }
[email protected]
   210
};
[email protected]
   211
[email protected]
   212
}
[email protected]
   213
[email protected]
   214
#endif // _include_jc_structure_h_
[email protected]
   215