/* 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/.
 */
#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 NamedConstant;
class NamedType;
class UnboundName;
class String;

class BoundName : public PoolObject
{
    Scope *scope_;
    ScopedRoot<String> name_;
    ScopedRoot<Type> type_;
    bool used_;

  protected:
    SourcePosition pos_;

  public:
    enum Kind {
      VARIABLE,
      FUNCTION,
      CONSTANT,
      TYPE,
      UNBOUND
    };

    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);
    }
    NamedType *toNamedType() {
        assert(kind() == TYPE);
        return reinterpret_cast<NamedType *>(this);
    }
    Type *type() const {
        return type_;
    }
    void setType(Handle<Type> type) {
        type_ = type;
    }
    void setUsed() {
        used_ = true;
    }
    bool used() const {
        return used_;
    }
    const SourcePosition &pos() {
        return pos_;
    }
};

class NamedFunction : public BoundName
{
    unsigned slot_;
    TokenKind token_;
    ScopedRoot<Type> forwardType_;
    bool isVariadicNative_;
    Vector<BoundName *, PoolAllocationPolicy> globals_;

  public:
    NamedFunction(Scope *scope, Handle<String> name, const SourcePosition &pos, TokenKind token);

    Kind kind() const {
        return FUNCTION;
    }
    void allocate(unsigned slot) {
        slot_ = slot;
    }
    bool isForward() const {
        return token_ == TOK_FORWARD;
    }
    bool isPublic() const {
        return token_ == TOK_PUBLIC;
    }
    bool isNative() const {
        return token_ == TOK_NATIVE;
    }
    void setForwardType(Handle<Type> type) {
        forwardType_ = type;
    }
    Type *forwardType() {
        return forwardType_;
    }
    void setImplemented(const SourcePosition &at) {
        token_ = TOK_PUBLIC;
        pos_ = at;
    }
    void markNativeVariadic() {
        assert(token_ == TOK_NATIVE);
        isVariadicNative_ = true;
    }
    bool isNativeVariadic() {
        assert(token_ == TOK_NATIVE);
        return isVariadicNative_;
    }
    unsigned slot() const {
        return slot_;
    }
    bool addUsedGlobal(BoundName *name) {
        return globals_.append(name);
    }
    Vector<BoundName *, PoolAllocationPolicy> *usedGlobals() {
        return &globals_;
    }
};

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 NamedType : public BoundName
{
  public:
    NamedType(Scope *scope, Handle<String> name, Handle<Type> type)
      : BoundName(scope, name, SourcePosition())
    {
        setType(type);
    }

    NamedType(Scope *scope, Handle<String> name, const SourcePosition &pos)
      : BoundName(scope, name, pos)
    {
    }

    Kind kind() const {
        return TYPE;
    }
};

class UnboundName : public BoundName
{
  public:
    UnboundName(Scope *scope, Handle<String> name)
      : BoundName(scope, name, SourcePosition())
    {
    }

    Kind kind() const {
        return UNBOUND;
    }
};

class Variable : public BoundName
{
  public:
    enum Location {
        UNALLOCATED,
        STACK,
        PARAMETER,
        ENVIRONMENT
    };

    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_;
    }
    bool needsHeapAllocation();

  private:
    Location location_;
    unsigned slot_;
};

}

#endif // _include_name_binding_h_
