jitcraft/Types.h
author David Anderson <dvander@alliedmods.net>
Thu Jun 28 00:26:55 2012 -0700 (2012-06-28)
changeset 8 069d207a8f2a
parent 4 633176823669
child 11 a350c2cc736a
permissions -rw-r--r--
Added support for bitwise operators.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_jc_types_h_
    19 #define _include_jc_types_h_
    20 
    21 #include "StringPool.h"
    22 
    23 namespace jc {
    24 
    25 enum PrimitiveType
    26 {
    27     PrimitiveType_Int32,
    28     PrimitiveType_Float
    29 };
    30 
    31 class Type
    32 {
    33     enum Kind {
    34         PRIMITIVE,
    35         UNSAFE_ARRAY
    36     };
    37 
    38   public:
    39     static const int UNKNOWN_SIZE = -1;
    40     static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
    41 
    42     static Type *NewPrimitive(Name *tag);
    43     static Type *NewPrimitive(PrimitiveType type);
    44     static Type *NewUnsafeArray(Type *contained, int elements);
    45 
    46     bool isPrimitive() const {
    47         return kind_ == PRIMITIVE;
    48     }
    49     bool isArray() const {
    50         return isUnsafeArray();
    51     }
    52     bool isUnsafeArray() const {
    53         return kind_ == UNSAFE_ARRAY;
    54     }
    55     PrimitiveType primitive() const {
    56         assert(isPrimitive());
    57         return primitive_;
    58     }
    59     Type *contained() const {
    60         assert(isArray());
    61         return contained_;
    62     }
    63     int elements() const {
    64         assert(isArray());
    65         return elements_;
    66     }
    67     unsigned levels() const {
    68         assert(isArray());
    69         return levels_;
    70     }
    71     Name *tag() const {
    72         assert(isPrimitive());
    73         return tag_;
    74     }
    75     Type *innermost() const {
    76         assert(isArray());
    77         Type *temp = contained();
    78         while (temp->isArray())
    79             temp = temp->contained();
    80         return temp;
    81     }
    82     bool isFloat() const {
    83         return isPrimitive() && primitive() == PrimitiveType_Float;
    84     }
    85     bool isInt() const {
    86         return isPrimitive() && primitive() == PrimitiveType_Int32;
    87     }
    88 
    89   private:
    90     Kind kind_;
    91 
    92     // If a primitive type, these tell the kind of type.
    93     Name *tag_;
    94     PrimitiveType primitive_;
    95 
    96     // The contained type of an array.
    97     Type *contained_;
    98 
    99     // The number of elements in the array.
   100     int elements_;
   101 
   102     // Levels deep this array goes (minimum is 1).
   103     unsigned levels_;
   104 };
   105 
   106 class BoxedPrimitive
   107 {
   108     PrimitiveType type_;
   109     union {
   110         int i_;
   111     };
   112 
   113   public:
   114     static BoxedPrimitive Int(int i) {
   115         BoxedPrimitive b;
   116         b.type_ = PrimitiveType_Int32;
   117         b.i_ = i;
   118         return b;
   119     }
   120 
   121     PrimitiveType type() const {
   122         return type_;
   123     }
   124     int toInt() const {
   125         assert(type() == PrimitiveType_Int32);
   126         return i_;
   127     }
   128 };
   129 
   130 }
   131 
   132 #endif // _include_jc_types_h_