/*
 * 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_name_binding_h_
#define _include_name_binding_h_

#include "PoolAllocator.h"
#include "Types.h"
#include "Scanner.h"

namespace ke {

class Variable;
class NamedFunction;
class Scope;
class Type;
class NamedEnum;
class NamedConstant;
class String;

class BoundName : public PoolObject
{
    Scope *scope_;
    ScopedRoot<String> name_;
    ScopedRoot<Type> type_;
    SourcePosition pos_;

  public:
    enum Kind {
      VARIABLE,
      FUNCTION,
      ENUM,
      CONSTANT
    };

    BoundName(Scope *scope, Handle<String> name, const SourcePosition &pos);

    virtual Kind kind() const = 0;

    String *name() const {
        return name_;
    }
    Scope *scope() const {
        return scope_;
    }
    Variable *toVariable() {
        assert(kind() == VARIABLE);
        return reinterpret_cast<Variable *>(this);
    }
    NamedFunction *toNamedFunction() {
        assert(kind() == FUNCTION);
        return reinterpret_cast<NamedFunction *>(this);
    }
    NamedConstant *toNamedConstant() {
        assert(kind() == CONSTANT);
        return reinterpret_cast<NamedConstant *>(this);
    }
    Type *type() const {
        return type_;
    }
    void setType(Handle<Type> type) {
        type_ = type;
    }
    const SourcePosition &pos() {
        return pos_;
    }
};

class NamedFunction : public BoundName
{
    unsigned slot_;

  public:
    NamedFunction(Scope *scope, Handle<String> name, const SourcePosition &pos);

    Kind kind() const {
        return FUNCTION;
    }
    void allocate(unsigned slot) {
        slot_ = slot;
    }
    unsigned slot() const {
        return slot_;
    }
};

class NamedEnum : public BoundName
{
  public:
    NamedEnum(Scope *scope, Handle<String> name, const SourcePosition &pos)
      : BoundName(scope, name, pos)
    {
    }

    Kind kind() const {
        return ENUM;
    }
};

class NamedConstant : public BoundName
{
    BoxedPrimitive value_;
    Type *type_;

  public:
    NamedConstant(Scope *scope, Handle<String> name, const SourcePosition &pos)
      : BoundName(scope, name, pos),
        type_(NULL)
    {
    }

    Kind kind() const {
        return CONSTANT;
    }
    void set(Type *type, const BoxedPrimitive &value) {
        value_ = value;
        type_ = type;
    }
    Type *type() const {
        return type_;
    }
    BoxedPrimitive value() const {
        return value_;
    }
};

class Variable : public BoundName
{
  public:
    enum Location {
        UNALLOCATED,
        STACK,
        PARAMETER
    };

    Variable(Scope *scope, Handle<String> name, const SourcePosition &pos);

    Kind kind() const {
        return VARIABLE;
    }
    bool unallocated() const {
        return location() == UNALLOCATED;
    }
    void allocate(Location location, unsigned slot) {
        location_ = location;
        slot_ = slot;
    }
    Location location() const {
        return location_;
    }
    unsigned slot() const {
        assert(!unallocated());
        return slot_;
    }

  private:
    Location location_;
    unsigned slot_;
};

}

#endif // _include_name_binding_h_
