diff -r 06a0015b6a5c -r 32ad0523b309 src/TypeManager.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/TypeManager.cpp Tue Jul 24 23:53:34 2012 -0700 @@ -0,0 +1,119 @@ +/* vim: set ts=4 sw=4 tw=99 et: + * + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ +#include "Types.h" +#include "TypeManager.h" +#include "Zone.h" +#include "Strings.h" + +using namespace ke; + + +TypeManager::TypeManager(Zone *zone) + : zone_(zone) +{ +} + +bool +TypeManager::initialize() +{ + voidType_ = Type::NewVoid(zone_); + if (!voidType_) + return false; + + Local floatTag(zone_, zone_->makeSymbol("Float")); + if (!floatTag) + return false; + + Local stringTag(zone_, zone_->makeSymbol("String")); + if (!stringTag) + return false; + + Local nullTag(zone_); + + if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, nullTag)) == NULL) + return false; + if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, floatTag)) == NULL) + return false; + if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL) + return false; + if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, stringTag)) == NULL) + return false; + + Local type(zone_); + for (unsigned i = 0; i < PrimitiveTypes_Total; i++) { + type = primitiveTypes_[i]; + if ((referenceTypes_[i] = Type::NewReference(zone_, type, false)) == NULL) + return false; + } + + return true; +} + +Type * +TypeManager::getPrimitiveForTag(Handle tag) +{ + if (!tag) + return getPrimitive(PrimitiveType_Int32); + if (strcmp(tag->chars(), "Float") == 0) + return getPrimitive(PrimitiveType_Float); + if (strcmp(tag->chars(), "String") == 0) + return getPrimitive(PrimitiveType_Char); + return NULL; +} + +Type * +TypeManager::unsafeArray(Handle contained, int elements, bool isConst) +{ + // :TODO: cache this. + return Type::NewArray(zone_, contained, elements, isConst); +} + +Type * +TypeManager::getReference(Handle type, bool isConst) +{ + if (!isConst && (type->isFloat() || (type->isInt32() && !type->tag()))) + return referenceTypes_[type->primitive()]; + + // :TODO: cache this. + return Type::NewReference(zone_, type, isConst); +} + +Type * +TypeManager::newFunction(Handle returnType, Handle args) +{ + // :TODO: cache this. + return Type::NewFunction(zone_, returnType, args); +} + +Type * +TypeManager::newNative(Handle returnType, Handle args, bool variadic) +{ + // :TODO: cache this. + return Type::NewNative(zone_, returnType, args, variadic); +} + +void +TypeManager::accept(VirtualObjectVisitor *visitor) +{ + visitor->visit(voidType_.address()); + for (unsigned i = 0; i < PrimitiveTypes_Total; i++) { + visitor->visit(primitiveTypes_[i].address()); + visitor->visit(referenceTypes_[i].address()); + } +}