jitcraft/Types.h
changeset 0 c455ebb9a5dc
child 2 46fc53b8fd2f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/Types.h	Mon Jun 25 02:57:28 2012 -0700
     1.3 @@ -0,0 +1,109 @@
     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_jc_types_h_
    1.22 +#define _include_jc_types_h_
    1.23 +
    1.24 +#include "StringPool.h"
    1.25 +
    1.26 +namespace jc {
    1.27 +
    1.28 +// Tags are a weak form of user-created types.
    1.29 +class Tag
    1.30 +{
    1.31 +    Name *name_;
    1.32 +
    1.33 +  public:
    1.34 +    Tag(Name *name);
    1.35 +
    1.36 +    Name *name() const {
    1.37 +        return name_;
    1.38 +    }
    1.39 +};
    1.40 +
    1.41 +enum PrimitiveType
    1.42 +{
    1.43 +    PrimitiveType_Int32
    1.44 +};
    1.45 +
    1.46 +class Type
    1.47 +{
    1.48 +    enum Kind {
    1.49 +        PRIMITIVE,
    1.50 +        UNSAFE_ARRAY
    1.51 +    };
    1.52 +
    1.53 +  public:
    1.54 +    static const int UNKNOWN_SIZE = -1;
    1.55 +    static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
    1.56 +
    1.57 +    static Type *NewPrimitive(Tag *tag);
    1.58 +    static Type *NewUnsafeArray(Type *contained, int elements);
    1.59 +
    1.60 +    bool isPrimitive() const {
    1.61 +        return kind_ == PRIMITIVE;
    1.62 +    }
    1.63 +    bool isArray() const {
    1.64 +        return isUnsafeArray();
    1.65 +    }
    1.66 +    bool isUnsafeArray() const {
    1.67 +        return kind_ == UNSAFE_ARRAY;
    1.68 +    }
    1.69 +    PrimitiveType primitive() const {
    1.70 +        assert(isPrimitive());
    1.71 +        return primitive_;
    1.72 +    }
    1.73 +    Type *contained() const {
    1.74 +        assert(isArray());
    1.75 +        return contained_;
    1.76 +    }
    1.77 +    int elements() const {
    1.78 +        assert(isArray());
    1.79 +        return elements_;
    1.80 +    }
    1.81 +    unsigned levels() const {
    1.82 +        assert(isArray());
    1.83 +        return levels_;
    1.84 +    }
    1.85 +    Type *innermost() const {
    1.86 +        assert(isArray());
    1.87 +        Type *temp = contained();
    1.88 +        while (temp->isArray())
    1.89 +            temp = temp->contained();
    1.90 +        return temp;
    1.91 +    }
    1.92 +
    1.93 +  private:
    1.94 +    Kind kind_;
    1.95 +
    1.96 +    // If a primitive type, these tell the kind of type.
    1.97 +    Tag *tag_;
    1.98 +    PrimitiveType primitive_;
    1.99 +
   1.100 +    // The contained type of an array.
   1.101 +    Type *contained_;
   1.102 +
   1.103 +    // The number of elements in the array.
   1.104 +    int elements_;
   1.105 +
   1.106 +    // Levels deep this array goes (minimum is 1).
   1.107 +    unsigned levels_;
   1.108 +};
   1.109 +
   1.110 +}
   1.111 +
   1.112 +#endif // _include_jc_types_h_