/*
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of JITCraft.
 *
 * JITCraft 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.
 * 
 * Foobar 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
 * JITCraft. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_jitcraft_function_h_
#define _include_jitcraft_function_h_

#include "Heap.h"
#include "Barriers.h"
#include "Token.h"
#include "Types.h"

namespace ke {

class Code;
class Module;
class Type;
class String;

class Function : public Object
{
    Barriered<Code> code_;
    Barriered<Type> type_;
    Barriered<String> name_;
    SourcePosition pos_;

 public:
    static Function *New(Zone *zone, Handle<Code> code, Handle<Type> type, Handle<String> name,
                         const SourcePosition &pos);

    Code *code() const {
        return code_;
    }
    Type *type() const {
        return type_;
    }
    String *name() const {
        return name_;
    }
    const SourcePosition &pos() {
        return pos_;
    }
    unsigned nargs() {
        return type()->parameters()->length();
    }
    Type *argType(size_t index) {
        return type()->parameterAt(index);
    }

    // Begin GC Descriptor.
    static inline size_t offsetOfCode() {
        return offsetof(Function, code_);
    }
    static inline size_t offsetOfType() {
        return offsetof(Function, type_);
    }
    static inline size_t offsetOfName() {
        return offsetof(Function, name_);
    }
    // End GC Descriptor.

    static inline Function *cast(Object *obj) {
        assert(!obj || obj->is(MapKind_Function));
        return reinterpret_cast<Function *>(obj);
    }
};

// A Native is a function with a very specialized calling convention, intended
// for earlier ISourcePawnEngine API compatibility.
class Native : public Object
{
    Barriered<Type> type_;
    Barriered<String> name_;
    void *function_;

  public:
    static Native *New(Zone *zone, Handle<Type> type, Handle<String> name);

    void *function() {
        return function_;
    }
    Type *type() {
        return type_;
    }
    String *name() {
        return name_;
    }
    void bind(void *function) {
        function_ = function;
    }

    // Begin GC Descriptor.
    static inline size_t offsetOfType() {
        return offsetof(Native, type_);
    }
    static inline size_t offsetOfName() {
        return offsetof(Native, name_);
    }
    // End GC Descriptor.

    static inline Native *cast(Object *obj) {
        assert(!obj || obj->is(MapKind_Native));
        return reinterpret_cast<Native *>(obj);
    }
};

}

#endif // _include_jitcraft_function_h_
