/* 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/.
 */
#include "Structures.h"
#include "Zone.h"
#include "Types.h"
#include "Spaces-inl.h"
#include "Heap-inl.h"

using namespace ke;

StructureBufferBuilder::StructureBufferBuilder()
  : size_(0)
{
}

bool
StructureBufferBuilder::add(Handle<Type> type, unsigned *offset)
{
    *offset = size_;

    if (!type->isTraceable()) {
        // For now, assume the size of primitives is 4-byte.
        assert(type->isInt32() || type->isFloat());
        size_ += 4;
        return true;
    }

    if (!offsets_.append(size_))
        return false;

    size_ += sizeof(Object *);
    return true;
}

Descriptor *
Descriptor::New(Zone *zone, Handle<String> name, Handle<Type> type, unsigned offset)
{
    Local<Descriptor> desc(zone,
        Descriptor::cast(zone->allocate(MapKind_Descriptor, sizeof(Descriptor), Heap::Tenure_Old)));
    if (!desc)
        return NULL;

    desc->name_ = name;
    desc->type_ = type;
    desc->offset_ = offset;
    return desc;
}

