/* 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 <string.h>
#include "Types.h"
#include "Zone.h"
#include "Strings.h"
#include "Structures.h"
#include "Heap-inl.h"

using namespace ke;

void
Type::init(Kind kind)
{
    this->kind_ = kind;
    this->root_ = this;
}

Type *
Type::NewVoid(Zone *zone)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(VOID);
    return type;
}

Type *
Type::NewString(Zone *zone)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(STRING);
    return type;
}

Type *
Type::NewUnchecked(Zone *zone)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(UNCHECKED);
    return type;
}

Type *
Type::NewPrimitive(Zone *zone, PrimitiveType prim)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(PRIMITIVE);
    type->primitive_ = prim;
    return type;
}

ArrayType *
ArrayType::New(Zone *zone, Handle<Type> contained, int elements)
{
    if (contained->isArray() &&
        ArrayType::cast(contained)->levels() == MAX_ARRAY_DEPTH)
    {
        zone->reportError(Message_MaximumArrayDepth);
        return NULL;
    }

    Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(ARRAY);
    type->contained_ = contained;
    type->elements_ = elements;
    if (!contained->isArray())
        type->levels_ = 1;
    else
        type->levels_ = ArrayType::cast(contained)->levels() + 1;
    return type;
}

ArrayType *
ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
{
    Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    assert(contained->isPrimitive());

    type->init(EXTERNAL_ARRAY);
    type->contained_ = contained;
    type->elements_ = DYNAMIC_ARRAY_SIZE;
    type->levels_ = 1;
    return type;
}

ReferenceType *
ReferenceType::New(Zone *zone, Handle<Type> contained)
{
    Local<ReferenceType> type(zone, ReferenceType::cast(zone->allocate(MapKind_ReferenceType, sizeof(ReferenceType), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(REFERENCE);
    type->contained_ = contained;
    return type;
}

EnumType *
EnumType::New(Zone *zone, Handle<String> name)
{
    Local<EnumType> type(zone, EnumType::cast(zone->allocate(MapKind_EnumType, sizeof(EnumType), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(ENUM);
    type->name_ = name;
    return type;
}

StructType *
StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
{
    Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(STRUCT);
    type->newMap_ = NULL;
    type->fields_ = fields;
    type->name_ = name;

    // Fill in field offsets.
    StructureBufferBuilder builder;
    Local<Type> fieldType(zone);
    for (size_t i = 0; i < fields->length(); i++) {
        Descriptor *desc = Descriptor::cast(fields->at(i));
        fieldType = desc->type();

        unsigned offset;
        if (!builder.add(fieldType, &offset))
            return NULL;
        desc->setOffset(offset);
    }

    Local<StructMap> map(zone, StructMap::New(zone, builder, type));
    if (!map)
        return NULL;

    type->newMap_ = map;

    return type;
}

Descriptor *
StructType::lookupField(Handle<String> name, unsigned *index)
{
    for (unsigned i = 0; i < fields_->length(); i++) {
        Descriptor *desc = Descriptor::cast(fields_->at(i));
        if (desc->name() == name) {
            if (index)
                *index = i;
            return desc;
        }
    }

    return NULL;
}

FunctionType *
FunctionType::New(Zone *zone, Handle<String> name, TokenKind token)
{
    Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->init(FUNCTION);
    type->parameters_ = NULL;
    type->defaults_ = NULL;
    type->returnType_ = NULL;
    type->name_ = name;
    type->token_ = token;
    return type;
}

// :TODO: this leaks an interior pointer.
const char *
ke::GetTypeName(Type *type)
{
    if (type->isArray())
        return "array";
    if (type->isFunction())
        return "function";
    if (type->isVoid())
        return "void";
    if (type->isEnum())
        return EnumType::cast(type)->name()->chars();
    if (type->isStruct())
        return StructType::cast(type)->name()->chars();
    if (type->isReference())
        type = ReferenceType::cast(type)->contained();
    switch (type->primitive()) {
      case PrimitiveType_Bool:
        return "bool";
      case PrimitiveType_Char:
        return "String";
      case PrimitiveType_Int32:
        return "int";
      case PrimitiveType_Float:
        return "Float";
      default:
        return "<unknown>";
    }
}

