jitcraft/Types.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 22:45:25 2012 -0700 (2012-06-30)
changeset 16 23627f8bc421
parent 14 0784ac5cc347
child 21 af5d8e985502
permissions -rw-r--r--
Added support for arbitrary expression references.
     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 #include "Types.h"
    19 #include "Zone.h"
    20 #include "Heap-inl.h"
    21 
    22 using namespace jc;
    23 
    24 Type *
    25 Type::NewPrimitive(Zone *zone, Name *tag)
    26 {
    27     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    28     if (!obj)
    29         return NULL;
    30 
    31     Type *type = Type::cast(obj);
    32     type->kind_ = PRIMITIVE;
    33     type->tag_ = tag;
    34     type->contained_ = NULL;
    35 
    36     if (tag && strcmp(tag, "Float") == 0)
    37         type->primitive_ = PrimitiveType_Float;
    38     else
    39         type->primitive_ = PrimitiveType_Int32;
    40     return type;
    41 }
    42 
    43 Type *
    44 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
    45 {
    46     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    47     if (!obj)
    48         return NULL;
    49 
    50     Type *type = Type::cast(obj);
    51     type->kind_ = PRIMITIVE;
    52     type->tag_ = NULL;
    53     type->contained_ = NULL;
    54     type->primitive_ = prim;
    55     return type;
    56 }
    57 
    58 Type *
    59 Type::NewUnsafeArray(Zone *zone, Type *contained, int elements)
    60 {
    61     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    62     if (!obj)
    63         return NULL;
    64 
    65     Type *type = Type::cast(obj);
    66     type->kind_ = ARRAY;
    67     type->tag_ = NULL;
    68     type->contained_ = contained;
    69     type->elements_ = elements;
    70     if (contained->isPrimitive())
    71         type->levels_ = 1;
    72     else
    73         type->levels_ = contained->levels() + 1;
    74     return type;
    75 }
    76 
    77 Type *
    78 Type::NewReference(Zone *zone, Type *contained)
    79 {
    80     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    81     if (!obj)
    82         return NULL;
    83 
    84     Type *type = Type::cast(obj);
    85     type->kind_ = REFERENCE;
    86     type->tag_ = NULL;
    87     type->contained_ = contained;
    88     return type;
    89 }
    90 
    91 Type *
    92 Type::NewFunction(Zone *zone, Type *returnType, FixedArray *parameters)
    93 {
    94     Object *obj = zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old);
    95     if (!obj)
    96         return NULL;
    97 
    98     Type *type = Type::cast(obj);
    99     type->kind_ = FUNCTION;
   100     type->contained_ = returnType;
   101     type->parameters_ = parameters;
   102     return type;
   103 }