X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/3fe16be7c02b11754e3d367ffd228beb14da2555..2f33dd04e61fcd883fc6ddcb8f2add8fd9a45eba:/Decode.cpp diff --git a/Decode.cpp b/Decode.cpp index 9cb165c..63bb9ef 100644 --- a/Decode.cpp +++ b/Decode.cpp @@ -1,21 +1,21 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2013 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ -/* GNU General Public License, Version 3 {{{ */ +/* GNU Affero General Public License, Version 3 {{{ */ /* - * Cycript 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. - * - * Cycript is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program 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 Cycript. If not, see . + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . **/ /* }}} */ @@ -24,80 +24,197 @@ #include "Decode.hpp" #include "Replace.hpp" -CYTypedIdentifier *Decode_(CYPool &pool, struct sig::Type *type) { - switch (type->primitive) { - case sig::function_P: { - _assert(type->data.signature.count != 0); - CYTypedParameter *parameter(NULL); - for (size_t i(type->data.signature.count - 1); i != 0; --i) - parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter); - return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeFunctionWith(parameter)); - } break; - - case sig::typename_P: return $ CYTypedIdentifier($V("Class")); - case sig::union_P: _assert(false); break; - case sig::string_P: return $ CYTypedIdentifier($V("char"), $ CYTypePointerTo()); - case sig::selector_P: return $ CYTypedIdentifier($V("SEL")); - - case sig::block_P: { - _assert(type->data.signature.count != 0); - CYTypedParameter *parameter(NULL); - for (size_t i(type->data.signature.count - 1); i != 0; --i) - parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter); - return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeBlockWith(parameter)); - } break; - - case sig::object_P: { - if (type->name == NULL) - return $ CYTypedIdentifier($V("id")); - else - return $ CYTypedIdentifier($V(type->name), $ CYTypePointerTo()); - } break; - - case sig::boolean_P: return $ CYTypedIdentifier($V("bool")); - case sig::uchar_P: return $ CYTypedIdentifier($V("uchar")); - case sig::uint_P: return $ CYTypedIdentifier($V("uint")); - case sig::ulong_P: return $ CYTypedIdentifier($V("ulong")); - case sig::ulonglong_P: return $ CYTypedIdentifier($V("ulonglong")); - case sig::ushort_P: return $ CYTypedIdentifier($V("ushort")); - case sig::array_P: return Decode(pool, type->data.data.type)->Modify($ CYTypeArrayOf($D(type->data.data.size))); - - // XXX: once again, the issue of void here is incorrect - case sig::pointer_P: { - CYTypedIdentifier *typed; - if (type->data.data.type == NULL) - typed = $ CYTypedIdentifier($V("void")); - else - typed = Decode(pool, type->data.data.type); - return typed->Modify($ CYTypePointerTo()); - } break; - - case sig::bit_P: _assert(false); break; - case sig::char_P: return $ CYTypedIdentifier($V("char")); - case sig::double_P: return $ CYTypedIdentifier($V("double")); - case sig::float_P: return $ CYTypedIdentifier($V("float")); - case sig::int_P: return $ CYTypedIdentifier($V("int")); - case sig::long_P: return $ CYTypedIdentifier($V("long")); - case sig::longlong_P: return $ CYTypedIdentifier($V("longlong")); - case sig::short_P: return $ CYTypedIdentifier($V("short")); - - // XXX: this happens to work, but is totally wrong - case sig::void_P: return $ CYTypedIdentifier($V("void")); - - case sig::struct_P: { - _assert(type->name != NULL); - return $ CYTypedIdentifier($V(type->name)); - } break; - } +namespace sig { + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVariable("bool")); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeNeutral)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeFloating(1)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeFloating(0)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeFloating(2)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeSigned)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 1)); +} + +#ifdef __SIZEOF_INT128__ +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeInt128(CYTypeSigned)); +} +#endif + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 2)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 3)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 0)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeUnsigned)); +} +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 1)); +} + +#ifdef __SIZEOF_INT128__ +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeInt128(CYTypeUnsigned)); +} +#endif + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 2)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 3)); +} + +template <> +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 0)); +} + +CYType *Void::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVoid()); +} + +CYType *Unknown::Decode(CYPool &pool) const { + return $ CYType($ CYTypeError()); +} + +CYType *String::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeNeutral), $ CYTypePointerTo()); +} + +#ifdef CY_OBJECTIVEC +CYType *Meta::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVariable("Class")); +} + +CYType *Selector::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVariable("SEL")); +} +#endif + +CYType *Bits::Decode(CYPool &pool) const { _assert(false); - return NULL; } -CYTypedIdentifier *Decode(CYPool &pool, struct sig::Type *type) { - CYTypedIdentifier *typed(Decode_(pool, type)); +CYType *Pointer::Decode(CYPool &pool) const { + return CYDecodeType(pool, &type)->Modify($ CYTypePointerTo()); +} + +CYType *Array::Decode(CYPool &pool) const { + return CYDecodeType(pool, &type)->Modify($ CYTypeArrayOf($D(size))); +} + +#ifdef CY_OBJECTIVEC +CYType *Object::Decode(CYPool &pool) const { + if (name == NULL) + return $ CYType($ CYTypeVariable("id")); + else + return $ CYType($ CYTypeVariable(name), $ CYTypePointerTo()); +} +#endif + +CYType *Enum::Decode(CYPool &pool) const { + CYEnumConstant *values(NULL); + for (size_t i(count); i != 0; --i) + values = $ CYEnumConstant($I(pool.strdup(constants[i - 1].name)), $D(constants[i - 1].value), values); + CYIdentifier *identifier(name == NULL ? NULL : $I(name)); + CYType *typed(type.Decode(pool)); + _assert(typed->modifier_ == NULL); + return $ CYType($ CYTypeEnum(identifier, typed->specifier_, values)); +} + +CYType *Aggregate::Decode(CYPool &pool) const { + _assert(!overlap); + + if (signature.count == _not(size_t)) { + _assert(name != NULL); + return $ CYType($ CYTypeReference(CYTypeReferenceStruct, $I($pool.strdup(name)))); + } + + CYTypeStructField *fields(NULL); + for (size_t i(signature.count); i != 0; --i) { + sig::Element &element(signature.elements[i - 1]); + fields = $ CYTypeStructField(CYDecodeType(pool, element.type), element.name == NULL ? NULL : $I(element.name), fields); + } + CYIdentifier *identifier(name == NULL ? NULL : $I(name)); + return $ CYType($ CYTypeStruct(identifier, $ CYStructTail(fields))); +} + +CYType *Callable::Decode(CYPool &pool) const { + _assert(signature.count != 0); + CYTypedParameter *parameters(NULL); + for (size_t i(signature.count - 1); i != 0; --i) + parameters = $ CYTypedParameter(CYDecodeType(pool, signature.elements[i].type), NULL, parameters); + return Modify(pool, CYDecodeType(pool, signature.elements[0].type), parameters); +} + +CYType *Function::Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const { + return result->Modify($ CYTypeFunctionWith(variadic, parameters)); +} + +#ifdef CY_OBJECTIVEC +CYType *Block::Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const { + return result->Modify($ CYTypeBlockWith(parameters)); +} + +CYType *Block::Decode(CYPool &pool) const { + if (signature.count == 0) + return $ CYType($ CYTypeVariable("NSBlock"), $ CYTypePointerTo()); + return Callable::Decode(pool); +} +#endif + +} + +CYType *CYDecodeType(CYPool &pool, struct sig::Type *type) { + CYType *typed(type->Decode(pool)); if ((type->flags & JOC_TYPE_CONST) != 0) { - if (type->primitive == sig::string_P) + if (dynamic_cast(type) != NULL) typed->modifier_ = $ CYTypeConstant(typed->modifier_); else typed = typed->Modify($ CYTypeConstant());