1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/TypeManager.cpp Tue Jul 24 23:53:34 2012 -0700
1.3 @@ -0,0 +1,119 @@
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 JITCraft.
1.9 + *
1.10 + * JITCraft 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 + * Foobar 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 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.21 + */
1.22 +#include "Types.h"
1.23 +#include "TypeManager.h"
1.24 +#include "Zone.h"
1.25 +#include "Strings.h"
1.26 +
1.27 +using namespace ke;
1.28 +
1.29 +
1.30 +TypeManager::TypeManager(Zone *zone)
1.31 + : zone_(zone)
1.32 +{
1.33 +}
1.34 +
1.35 +bool
1.36 +TypeManager::initialize()
1.37 +{
1.38 + voidType_ = Type::NewVoid(zone_);
1.39 + if (!voidType_)
1.40 + return false;
1.41 +
1.42 + Local<String> floatTag(zone_, zone_->makeSymbol("Float"));
1.43 + if (!floatTag)
1.44 + return false;
1.45 +
1.46 + Local<String> stringTag(zone_, zone_->makeSymbol("String"));
1.47 + if (!stringTag)
1.48 + return false;
1.49 +
1.50 + Local<String> nullTag(zone_);
1.51 +
1.52 + if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, nullTag)) == NULL)
1.53 + return false;
1.54 + if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, floatTag)) == NULL)
1.55 + return false;
1.56 + if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
1.57 + return false;
1.58 + if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, stringTag)) == NULL)
1.59 + return false;
1.60 +
1.61 + Local<Type> type(zone_);
1.62 + for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
1.63 + type = primitiveTypes_[i];
1.64 + if ((referenceTypes_[i] = Type::NewReference(zone_, type, false)) == NULL)
1.65 + return false;
1.66 + }
1.67 +
1.68 + return true;
1.69 +}
1.70 +
1.71 +Type *
1.72 +TypeManager::getPrimitiveForTag(Handle<String> tag)
1.73 +{
1.74 + if (!tag)
1.75 + return getPrimitive(PrimitiveType_Int32);
1.76 + if (strcmp(tag->chars(), "Float") == 0)
1.77 + return getPrimitive(PrimitiveType_Float);
1.78 + if (strcmp(tag->chars(), "String") == 0)
1.79 + return getPrimitive(PrimitiveType_Char);
1.80 + return NULL;
1.81 +}
1.82 +
1.83 +Type *
1.84 +TypeManager::unsafeArray(Handle<Type> contained, int elements, bool isConst)
1.85 +{
1.86 + // :TODO: cache this.
1.87 + return Type::NewArray(zone_, contained, elements, isConst);
1.88 +}
1.89 +
1.90 +Type *
1.91 +TypeManager::getReference(Handle<Type> type, bool isConst)
1.92 +{
1.93 + if (!isConst && (type->isFloat() || (type->isInt32() && !type->tag())))
1.94 + return referenceTypes_[type->primitive()];
1.95 +
1.96 + // :TODO: cache this.
1.97 + return Type::NewReference(zone_, type, isConst);
1.98 +}
1.99 +
1.100 +Type *
1.101 +TypeManager::newFunction(Handle<Type> returnType, Handle<FixedArray> args)
1.102 +{
1.103 + // :TODO: cache this.
1.104 + return Type::NewFunction(zone_, returnType, args);
1.105 +}
1.106 +
1.107 +Type *
1.108 +TypeManager::newNative(Handle<Type> returnType, Handle<FixedArray> args, bool variadic)
1.109 +{
1.110 + // :TODO: cache this.
1.111 + return Type::NewNative(zone_, returnType, args, variadic);
1.112 +}
1.113 +
1.114 +void
1.115 +TypeManager::accept(VirtualObjectVisitor *visitor)
1.116 +{
1.117 + visitor->visit(voidType_.address());
1.118 + for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
1.119 + visitor->visit(primitiveTypes_[i].address());
1.120 + visitor->visit(referenceTypes_[i].address());
1.121 + }
1.122 +}