/* 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;

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->kind_ = VOID;
    type->contained_ = NULL;
    type->returnType_ = NULL;
    type->parameters_ = NULL;
    type->fields_ = NULL;
    type->newMap_ = NULL;
    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->kind_ = PRIMITIVE;
    type->name_ = NULL;
    type->contained_ = NULL;
    type->returnType_ = NULL;
    type->parameters_ = NULL;
    type->fields_ = NULL;
    type->newMap_ = NULL;
    type->newMap_ = NULL;
    type->primitive_ = prim;
    return type;
}

Type *
Type::NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->kind_ = ARRAY;
    type->name_ = NULL;
    type->contained_ = contained;
    type->returnType_ = NULL;
    type->parameters_ = NULL;
    type->fields_ = NULL;
    type->newMap_ = NULL;
    type->elements_ = elements;
    if (!contained->isArray())
        type->levels_ = 1;
    else
        type->levels_ = contained->levels() + 1;
    type->const_ = isConst;
    return type;
}

Type *
Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->kind_ = REFERENCE;
    type->name_ = NULL;
    type->contained_ = contained;
    type->returnType_ = NULL;
    type->parameters_ = NULL;
    type->fields_ = NULL;
    type->newMap_ = NULL;
    type->const_ = isConst;
    return type;
}

Type *
Type::NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->kind_ = FUNCTION;
    type->name_ = NULL;
    type->contained_ = NULL;
    type->fields_ = NULL;
    type->newMap_ = NULL;
    type->returnType_ = returnType;
    type->parameters_ = parameters;
    return type;
}

Type *
Type::NewEnum(Zone *zone, Handle<String> name)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->kind_ = ENUM;
    type->name_ = name;
    type->contained_ = NULL;
    type->returnType_ = NULL;
    type->parameters_ = NULL;
    type->fields_ = NULL;
    type->newMap_ = NULL;
    return type;
}

Type *
Type::NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields, Handle<Map> newMap)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->kind_ = STRUCT;
    type->name_ = name;
    type->contained_ = NULL;
    type->returnType_ = NULL;
    type->parameters_ = NULL;
    type->newMap_ = newMap;
    type->fields_ = fields;
    return type;
}

Type *
Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool variadic)
{
    Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    if (!type)
        return NULL;

    type->kind_ = NATIVE;
    type->name_ = NULL;
    type->contained_ = NULL;
    type->fields_ = NULL;
    type->newMap_ = NULL;
    type->returnType_ = returnType;
    type->parameters_ = parameters;
    type->variadicNative_ = variadic;
    return type;
}

bool
Type::Compare(Type *left, Type *right)
{
    if (left == right)
        return true;

    if (left->kind() != right->kind())
        return false;

    switch (left->kind()) {
      case Type::PRIMITIVE:
        return left->primitive() == right->primitive();

      case Type::ARRAY:
      {
        if (left->levels() != right->levels())
          return false;
        if (left->elements() != right->elements())
          return false;
        return Compare(left->contained(), right->contained());
      }

      case Type::FUNCTION:
      {
        if (!Compare(left->returnType(), right->returnType()))
            return false;
        if (left->parameters()->length() != right->parameters()->length())
            return false;
        if (left->isNative() != right->isNative())
            return false;
        if (left->isNative() && (left->isNativeVariadic() != right->isNativeVariadic()))
            return false;
        for (unsigned i = 0; i < left->parameters()->length(); i++) {
            Type *leftparam = left->parameterAt(i);
            Type *rightparam = right->parameterAt(i);
            if (!Compare(leftparam, rightparam))
                return false;
        }
        return true;
      }

      case Type::ENUM:
        return false;

      case Type::VOID:
      	return true;

      default:
        assert(left->kind() == Type::REFERENCE);
        return Compare(left->contained(), right->contained());
    }
}

Descriptor *
Type::lookupField(Handle<String> name, unsigned *index)
{
    assert(isStruct());

    for (unsigned i = 0; i < fields_->length(); i++) {
        Descriptor *desc = Descriptor::cast(fields_->at(i));
        if (desc->name() == name) {
            *index = i;
            return desc;
        }
    }

    return NULL;
}

