src/NameBinding.h
changeset 195 c1ba166f3fc4
parent 194 b029d63565a2
child 196 9904633b1dfc
     1.1 --- a/src/NameBinding.h	Mon Sep 17 00:10:01 2012 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,257 +0,0 @@
     1.4 -/* vim: set ts=4 sw=4 tw=99 et:
     1.5 - *
     1.6 - * Copyright (C) 2012 David Anderson
     1.7 - *
     1.8 - * This file is part of SourcePawn.
     1.9 - *
    1.10 - * SourcePawn is free software: you can redistribute it and/or modify it under
    1.11 - * the terms of the GNU General Public License as published by the Free
    1.12 - * Software Foundation, either version 3 of the License, or (at your option)
    1.13 - * any later version.
    1.14 - * 
    1.15 - * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 - *
    1.19 - * You should have received a copy of the GNU General Public License along with
    1.20 - * SourcePawn. If not, see http://www.gnu.org/licenses/.
    1.21 - */
    1.22 -#ifndef _include_name_binding_h_
    1.23 -#define _include_name_binding_h_
    1.24 -
    1.25 -#include "PoolAllocator.h"
    1.26 -#include "Types.h"
    1.27 -#include "Scanner.h"
    1.28 -
    1.29 -namespace ke {
    1.30 -
    1.31 -class Variable;
    1.32 -class NamedFunction;
    1.33 -class Scope;
    1.34 -class Type;
    1.35 -class NamedConstant;
    1.36 -class NamedType;
    1.37 -class UnboundName;
    1.38 -class String;
    1.39 -
    1.40 -class BoundName : public PoolObject
    1.41 -{
    1.42 -    Scope *scope_;
    1.43 -    ScopedRoot<String> name_;
    1.44 -    ScopedRoot<Type> type_;
    1.45 -    bool used_;
    1.46 -
    1.47 -  protected:
    1.48 -    SourcePosition pos_;
    1.49 -
    1.50 -  public:
    1.51 -    enum Kind {
    1.52 -      VARIABLE,
    1.53 -      FUNCTION,
    1.54 -      CONSTANT,
    1.55 -      TYPE,
    1.56 -      UNBOUND
    1.57 -    };
    1.58 -
    1.59 -    BoundName(Scope *scope, Handle<String> name, const SourcePosition &pos);
    1.60 -
    1.61 -    virtual Kind kind() const = 0;
    1.62 -
    1.63 -    String *name() const {
    1.64 -        return name_;
    1.65 -    }
    1.66 -    Scope *scope() const {
    1.67 -        return scope_;
    1.68 -    }
    1.69 -    Variable *toVariable() {
    1.70 -        assert(kind() == VARIABLE);
    1.71 -        return reinterpret_cast<Variable *>(this);
    1.72 -    }
    1.73 -    NamedFunction *toNamedFunction() {
    1.74 -        assert(kind() == FUNCTION);
    1.75 -        return reinterpret_cast<NamedFunction *>(this);
    1.76 -    }
    1.77 -    NamedConstant *toNamedConstant() {
    1.78 -        assert(kind() == CONSTANT);
    1.79 -        return reinterpret_cast<NamedConstant *>(this);
    1.80 -    }
    1.81 -    NamedType *toNamedType() {
    1.82 -        assert(kind() == TYPE);
    1.83 -        return reinterpret_cast<NamedType *>(this);
    1.84 -    }
    1.85 -    Type *type() const {
    1.86 -        return type_;
    1.87 -    }
    1.88 -    void setType(Handle<Type> type) {
    1.89 -        type_ = type;
    1.90 -    }
    1.91 -    void setUsed() {
    1.92 -        used_ = true;
    1.93 -    }
    1.94 -    bool used() const {
    1.95 -        return used_;
    1.96 -    }
    1.97 -    const SourcePosition &pos() {
    1.98 -        return pos_;
    1.99 -    }
   1.100 -};
   1.101 -
   1.102 -class NamedFunction : public BoundName
   1.103 -{
   1.104 -    unsigned slot_;
   1.105 -    TokenKind token_;
   1.106 -    ScopedRoot<Type> forwardType_;
   1.107 -    bool isVariadicNative_;
   1.108 -    Vector<BoundName *, PoolAllocationPolicy> globals_;
   1.109 -
   1.110 -  public:
   1.111 -    NamedFunction(Scope *scope, Handle<String> name, const SourcePosition &pos, TokenKind token);
   1.112 -
   1.113 -    Kind kind() const {
   1.114 -        return FUNCTION;
   1.115 -    }
   1.116 -    void allocate(unsigned slot) {
   1.117 -        slot_ = slot;
   1.118 -    }
   1.119 -    bool isForward() const {
   1.120 -        return token_ == TOK_FORWARD;
   1.121 -    }
   1.122 -    bool isPublic() const {
   1.123 -        return token_ == TOK_PUBLIC;
   1.124 -    }
   1.125 -    bool isNative() const {
   1.126 -        return token_ == TOK_NATIVE;
   1.127 -    }
   1.128 -    TokenKind token() const {
   1.129 -        return token_;
   1.130 -    }
   1.131 -    void setForwardType(Handle<Type> type) {
   1.132 -        forwardType_ = type;
   1.133 -    }
   1.134 -    Type *forwardType() {
   1.135 -        return forwardType_;
   1.136 -    }
   1.137 -    void setImplemented(const SourcePosition &at) {
   1.138 -        token_ = TOK_PUBLIC;
   1.139 -        pos_ = at;
   1.140 -    }
   1.141 -    void markNativeVariadic() {
   1.142 -        assert(token_ == TOK_NATIVE);
   1.143 -        isVariadicNative_ = true;
   1.144 -    }
   1.145 -    bool isNativeVariadic() {
   1.146 -        assert(token_ == TOK_NATIVE);
   1.147 -        return isVariadicNative_;
   1.148 -    }
   1.149 -    unsigned slot() const {
   1.150 -        return slot_;
   1.151 -    }
   1.152 -    bool addUsedGlobal(BoundName *name) {
   1.153 -        return globals_.append(name);
   1.154 -    }
   1.155 -    Vector<BoundName *, PoolAllocationPolicy> *usedGlobals() {
   1.156 -        return &globals_;
   1.157 -    }
   1.158 -};
   1.159 -
   1.160 -class NamedConstant : public BoundName
   1.161 -{
   1.162 -    BoxedPrimitive value_;
   1.163 -
   1.164 -  public:
   1.165 -    NamedConstant(Scope *scope, Handle<String> name, const SourcePosition &pos)
   1.166 -      : BoundName(scope, name, pos)
   1.167 -    {
   1.168 -    }
   1.169 -
   1.170 -    Kind kind() const {
   1.171 -        return CONSTANT;
   1.172 -    }
   1.173 -    void set(Handle<Type> type, const BoxedPrimitive &value) {
   1.174 -        value_ = value;
   1.175 -        setType(type);
   1.176 -    }
   1.177 -    BoxedPrimitive value() const {
   1.178 -        return value_;
   1.179 -    }
   1.180 -};
   1.181 -
   1.182 -class NamedType : public BoundName
   1.183 -{
   1.184 -  public:
   1.185 -    NamedType(Scope *scope, Handle<String> name, Handle<Type> type)
   1.186 -      : BoundName(scope, name, SourcePosition())
   1.187 -    {
   1.188 -        setType(type);
   1.189 -    }
   1.190 -
   1.191 -    NamedType(Scope *scope, Handle<String> name, const SourcePosition &pos)
   1.192 -      : BoundName(scope, name, pos)
   1.193 -    {
   1.194 -    }
   1.195 -
   1.196 -    Kind kind() const {
   1.197 -        return TYPE;
   1.198 -    }
   1.199 -};
   1.200 -
   1.201 -class UnboundName : public BoundName
   1.202 -{
   1.203 -  public:
   1.204 -    UnboundName(Scope *scope, Handle<String> name)
   1.205 -      : BoundName(scope, name, SourcePosition())
   1.206 -    {
   1.207 -    }
   1.208 -
   1.209 -    Kind kind() const {
   1.210 -        return UNBOUND;
   1.211 -    }
   1.212 -};
   1.213 -
   1.214 -class Variable : public BoundName
   1.215 -{
   1.216 -  public:
   1.217 -    enum Location {
   1.218 -        UNALLOCATED,
   1.219 -        STACK,
   1.220 -        PARAMETER,
   1.221 -        ENVIRONMENT
   1.222 -    };
   1.223 -
   1.224 -    Variable(Scope *scope, Handle<String> name, const SourcePosition &pos);
   1.225 -
   1.226 -    Kind kind() const {
   1.227 -        return VARIABLE;
   1.228 -    }
   1.229 -    bool unallocated() const {
   1.230 -        return location() == UNALLOCATED;
   1.231 -    }
   1.232 -    void allocate(Location location, unsigned slot) {
   1.233 -        location_ = location;
   1.234 -        slot_ = slot;
   1.235 -    }
   1.236 -    Location location() const {
   1.237 -        return location_;
   1.238 -    }
   1.239 -    unsigned slot() const {
   1.240 -        assert(!unallocated());
   1.241 -        return slot_;
   1.242 -    }
   1.243 -    void setPublic() {
   1.244 -        assert(!public_);
   1.245 -        public_ = true;
   1.246 -    }
   1.247 -    bool isPublic() const {
   1.248 -        return public_;
   1.249 -    }
   1.250 -    bool needsHeapAllocation();
   1.251 -
   1.252 -  private:
   1.253 -    Location location_;
   1.254 -    unsigned slot_;
   1.255 -    bool public_;
   1.256 -};
   1.257 -
   1.258 -}
   1.259 -
   1.260 -#endif // _include_name_binding_h_