X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/574d47203e63ac4a85f0d609098118d19e6bbf09..dc15fe8ace017fdef39202474c1563c1ae0ae67a:/Decode.cpp?ds=sidebyside diff --git a/Decode.cpp b/Decode.cpp index 8bda223..63bb9ef 100644 --- a/Decode.cpp +++ b/Decode.cpp @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2015 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -27,155 +27,192 @@ namespace sig { template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeVariable("bool")); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVariable("bool")); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeCharacter(CYTypeNeutral)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeNeutral)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeVariable("double")); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeFloating(1)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeVariable("float")); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeFloating(0)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeCharacter(CYTypeSigned)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeFloating(2)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeSigned, 1)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeSigned)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeSigned, 2)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 1)); } +#ifdef __SIZEOF_INT128__ template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeSigned, 3)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeInt128(CYTypeSigned)); } +#endif template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeSigned, 0)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 2)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeCharacter(CYTypeUnsigned)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 3)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeUnsigned, 1)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeSigned, 0)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeUnsigned, 2)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeUnsigned)); } template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeUnsigned, 3)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 1)); } +#ifdef __SIZEOF_INT128__ template <> -CYTypedIdentifier *Primitive::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeIntegral(CYTypeUnsigned, 0)); +CYType *Primitive::Decode(CYPool &pool) const { + return $ CYType($ CYTypeInt128(CYTypeUnsigned)); } +#endif -CYTypedIdentifier *Void::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeVoid()); +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)); } -CYTypedIdentifier *Unknown::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeError()); +CYType *Void::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVoid()); } -CYTypedIdentifier *String::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeCharacter(CYTypeNeutral), $ CYTypePointerTo()); +CYType *Unknown::Decode(CYPool &pool) const { + return $ CYType($ CYTypeError()); } -CYTypedIdentifier *Meta::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeVariable("Class")); +CYType *String::Decode(CYPool &pool) const { + return $ CYType($ CYTypeCharacter(CYTypeNeutral), $ CYTypePointerTo()); } -CYTypedIdentifier *Selector::Decode(CYPool &pool) const { - return $ CYTypedIdentifier($ CYTypeVariable("SEL")); +#ifdef CY_OBJECTIVEC +CYType *Meta::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVariable("Class")); } -CYTypedIdentifier *Bits::Decode(CYPool &pool) const { +CYType *Selector::Decode(CYPool &pool) const { + return $ CYType($ CYTypeVariable("SEL")); +} +#endif + +CYType *Bits::Decode(CYPool &pool) const { _assert(false); } -CYTypedIdentifier *Pointer::Decode(CYPool &pool) const { +CYType *Pointer::Decode(CYPool &pool) const { return CYDecodeType(pool, &type)->Modify($ CYTypePointerTo()); } -CYTypedIdentifier *Array::Decode(CYPool &pool) const { +CYType *Array::Decode(CYPool &pool) const { return CYDecodeType(pool, &type)->Modify($ CYTypeArrayOf($D(size))); } -CYTypedIdentifier *Object::Decode(CYPool &pool) const { +#ifdef CY_OBJECTIVEC +CYType *Object::Decode(CYPool &pool) const { if (name == NULL) - return $ CYTypedIdentifier($ CYTypeVariable("id")); + return $ CYType($ CYTypeVariable("id")); else - return $ CYTypedIdentifier($ CYTypeVariable(name), $ CYTypePointerTo()); + return $ CYType($ CYTypeVariable(name), $ CYTypePointerTo()); } +#endif -CYTypedIdentifier *Aggregate::Decode(CYPool &pool) const { +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]); - CYTypedIdentifier *typed(CYDecodeType(pool, element.type)); - if (element.name != NULL) - typed->identifier_ = $I(element.name); - fields = $ CYTypeStructField(typed, fields); + fields = $ CYTypeStructField(CYDecodeType(pool, element.type), element.name == NULL ? NULL : $I(element.name), fields); } CYIdentifier *identifier(name == NULL ? NULL : $I(name)); - return $ CYTypedIdentifier($ CYTypeStruct(identifier, $ CYStructTail(fields))); + return $ CYType($ CYTypeStruct(identifier, $ CYStructTail(fields))); } -CYTypedIdentifier *Callable::Decode(CYPool &pool) const { +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), parameters); + parameters = $ CYTypedParameter(CYDecodeType(pool, signature.elements[i].type), NULL, parameters); return Modify(pool, CYDecodeType(pool, signature.elements[0].type), parameters); } -CYTypedIdentifier *Function::Modify(CYPool &pool, CYTypedIdentifier *result, CYTypedParameter *parameters) const { +CYType *Function::Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const { return result->Modify($ CYTypeFunctionWith(variadic, parameters)); } -CYTypedIdentifier *Block::Modify(CYPool &pool, CYTypedIdentifier *result, CYTypedParameter *parameters) const { +#ifdef CY_OBJECTIVEC +CYType *Block::Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const { return result->Modify($ CYTypeBlockWith(parameters)); } -CYTypedIdentifier *Block::Decode(CYPool &pool) const { +CYType *Block::Decode(CYPool &pool) const { if (signature.count == 0) - return $ CYTypedIdentifier($ CYTypeVariable("NSBlock"), $ CYTypePointerTo()); + return $ CYType($ CYTypeVariable("NSBlock"), $ CYTypePointerTo()); return Callable::Decode(pool); } +#endif } -CYTypedIdentifier *CYDecodeType(CYPool &pool, struct sig::Type *type) { - CYTypedIdentifier *typed(type->Decode(pool)); +CYType *CYDecodeType(CYPool &pool, struct sig::Type *type) { + CYType *typed(type->Decode(pool)); if ((type->flags & JOC_TYPE_CONST) != 0) { if (dynamic_cast(type) != NULL) typed->modifier_ = $ CYTypeConstant(typed->modifier_);