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