/* vim: set ts=4 sw=4 tw=99 et:
 *
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of SourcePawn.
 *
 * SourcePawn is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_jc_structure_h_
#define _include_jc_structure_h_

#include "Objects.h"
#include "Handles.h"
#include "Vector.h"
#include "Heap.h"

namespace ke {

class Type;
class String;
class FixedArray;

// When describing composite structures, the GC must be able to trace interior
// references to Objects. To make this possible, maps that describe composite
// structures must provide a list of offsets into their objects that are
// traceable. StructureBufferBuilder assists with this.
class StructureBufferBuilder
{
  public:
    StructureBufferBuilder();

    bool add(Handle<Type> type, unsigned *offset);

    unsigned structureSize() const {
        return size_;
    }
    unsigned traceableOffsetCount() const {
        return offsets_.length();
    }
    const unsigned *offsets() const {
        return offsets_.buffer();
    }

  private:
    Vector<unsigned> offsets_;
    unsigned size_;
};

// Represents a property descriptor for a structure field.
class Descriptor : public Object
{
    // The name of the field.
    Barriered<String> name_;

    // The type of the field.
    Barriered<Type> type_;

    // The offset into an object at which this field can be accessed.
    unsigned offset_;

  public:
    static Descriptor *New(Zone *zone, Handle<String> name, Handle<Type> type,
                           unsigned offset);

    String *name() {
        return name_;
    }
    Type *type() {
        return type_;
    }
    unsigned offset() {
        return offset_;
    }

    // Begin GC descriptor.
    static inline size_t offsetOfName() {
        return OFFSETOF(Descriptor, name_);
    }
    static inline size_t offsetOfType() {
        return OFFSETOF(Descriptor, type_);
    }
    // End GC descriptor.
    
    static inline Descriptor *cast(Object *obj) {
        assert(!obj || obj->is(MapKind_Descriptor));
        return reinterpret_cast<Descriptor *>(obj);
    }
};

class StructMap : public Map
{
    unsigned structureSize_;
    unsigned traceOffsetCount_;
    Barriered<Type> type_;

  public:
    static StructMap *New(Zone *zone, const StructureBufferBuilder &builder);

    unsigned structureSize() {
        return structureSize_;
    }
    unsigned traceOffsetCount() {
        return traceOffsetCount_;
    }
    const unsigned *traceOffsets() {
        return reinterpret_cast<unsigned *>(this + 1);
    }
    size_t size() {
        return Align(sizeof(StructMap) + sizeof(unsigned) * traceOffsetCount(), kObjectAlignment);
    }
    Type *type() {
        return type_;
    }
    void setType(Type *type) {
        assert(!type_);
        type_ = type;
    }

    // Begin GC descriptors.
    static inline size_t offsetOfType() {
        return OFFSETOF(StructMap, type_);
    }
    // End GC descriptors.

    static inline StructMap *cast(Object *obj) {
        assert(!obj || obj->is(MapKind_MetaStruct));
        return reinterpret_cast<StructMap *>(obj);
    }
};

class Struct : public Object
{
  public:
    static Struct *New(Zone *zone, Handle<StructMap> map, Heap::Tenure tenure = Heap::Tenure_Default);

    size_t size() {
        return Align(sizeof(Struct) + map()->structureSize(), kObjectAlignment);
    }
    StructMap *map() {
        return StructMap::cast(Object::map());
    }

    // This must NOT be used with any Object * type!
    template <typename T> T get(Descriptor *desc) {
        return *reinterpret_cast<T *>(
                    reinterpret_cast<Address>(this) +
                    offsetOfSlotOffset(desc->offset()));
    }

    template <typename T> void set(Descriptor *desc, const T &t) {
        *reinterpret_cast<T *>(
            reinterpret_cast<Address>(this) +
            offsetOfSlotOffset(desc->offset())) = t;
    }

    void set(Zone *zone, Descriptor *desc, Object *obj) {
        // :TODO: write barrier
        *reinterpret_cast<Object **>(
            reinterpret_cast<Address>(this) +
            offsetOfSlotOffset(desc->offset())) = obj;
    }

    static inline size_t offsetOfSlotOffset(unsigned offset) {
        return sizeof(Struct) + offset;
    }

    static inline Struct *cast(Object *obj) {
        assert(!obj || obj->is(MapKind_Struct));
        return reinterpret_cast<Struct *>(obj);
    }
};

}

#endif // _include_jc_structure_h_

