jitcraft/NameBinding.h
changeset 0 c455ebb9a5dc
child 1 27f12e2fc369
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/NameBinding.h	Mon Jun 25 02:57:28 2012 -0700
     1.3 @@ -0,0 +1,123 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#ifndef _include_name_binding_h_
    1.22 +#define _include_name_binding_h_
    1.23 +
    1.24 +#include "PoolAllocator.h"
    1.25 +#include "StringPool.h"
    1.26 +
    1.27 +namespace jc {
    1.28 +
    1.29 +class Variable;
    1.30 +class NamedFunction;
    1.31 +class Scope;
    1.32 +class Type;
    1.33 +
    1.34 +class BoundName : public PoolObject
    1.35 +{
    1.36 +    Name *name_;
    1.37 +    Scope *scope_;
    1.38 +    Type *type_;
    1.39 +
    1.40 +  public:
    1.41 +    enum Kind {
    1.42 +      VARIABLE,
    1.43 +      FUNCTION
    1.44 +    };
    1.45 +
    1.46 +    BoundName(Scope *scope, Name *name);
    1.47 +
    1.48 +    virtual Kind kind() const = 0;
    1.49 +
    1.50 +    Name *name() const {
    1.51 +        return name_;
    1.52 +    }
    1.53 +    Scope  *scope() const {
    1.54 +        return scope_;
    1.55 +    }
    1.56 +    Variable *toVariable() {
    1.57 +        assert(kind() == VARIABLE);
    1.58 +        return reinterpret_cast<Variable *>(this);
    1.59 +    }
    1.60 +    NamedFunction *toNamedFunction() {
    1.61 +        assert(kind() == FUNCTION);
    1.62 +        return reinterpret_cast<NamedFunction *>(this);
    1.63 +    }
    1.64 +    Type *type() const {
    1.65 +        return type_;
    1.66 +    }
    1.67 +    void setType(Type *type) {
    1.68 +        type_ = type;
    1.69 +    }
    1.70 +};
    1.71 +
    1.72 +class NamedFunction : public BoundName
    1.73 +{
    1.74 +    unsigned slot_;
    1.75 +
    1.76 +  public:
    1.77 +    NamedFunction(Scope *scope, Name *name);
    1.78 +
    1.79 +    Kind kind() const {
    1.80 +        return FUNCTION;
    1.81 +    }
    1.82 +    void allocate(unsigned slot) {
    1.83 +        slot_ = slot;
    1.84 +    }
    1.85 +    unsigned slot() const {
    1.86 +        return slot_;
    1.87 +    }
    1.88 +};
    1.89 +
    1.90 +class Variable : public BoundName
    1.91 +{
    1.92 +  public:
    1.93 +    enum Location {
    1.94 +        UNALLOCATED,
    1.95 +        STACK,
    1.96 +        PARAMETER
    1.97 +    };
    1.98 +
    1.99 +    Variable(Scope *scope, Name *name);
   1.100 +
   1.101 +    Kind kind() const {
   1.102 +        return VARIABLE;
   1.103 +    }
   1.104 +    bool unallocated() const {
   1.105 +        return location() == UNALLOCATED;
   1.106 +    }
   1.107 +    void allocate(Location location, unsigned slot) {
   1.108 +        location_ = location;
   1.109 +        slot_ = slot;
   1.110 +    }
   1.111 +    Location location() const {
   1.112 +        return location_;
   1.113 +    }
   1.114 +    unsigned slot() const {
   1.115 +        assert(!unallocated());
   1.116 +        return slot_;
   1.117 +    }
   1.118 +
   1.119 +  private:
   1.120 +    Location location_;
   1.121 +    unsigned slot_;
   1.122 +};
   1.123 +
   1.124 +}
   1.125 +
   1.126 +#endif // _include_name_binding_h_