corto_string _dDDS_toMetaXml(corto_struct type) { /* $begin(dDDS/toMetaXml) */ return dDDS_metaXmlGet(corto_type(type)); /* $end */ }
/* Create if statement based on ternary operator expressions */ ast_If ast_Ternary_createIf(ast_Expression condition, ast_Node ifTrue, ast_Node ifFalse) { ast_Block trueBlock; ast_Block falseBlock; ast_If falseIf; ast_If result; if (corto_instanceof(corto_type(ast_Block_o), ifTrue)) { trueBlock = ast_BlockCreate(yparser()->block); falseBlock = ast_BlockCreate(yparser()->block); ast_Block_addStatement(trueBlock, ast_Node(ifTrue)); ast_Block_addStatement(falseBlock, ast_Node(ifFalse)); ast_Parser_collect(yparser(), trueBlock); ast_Parser_collect(yparser(), falseBlock); } else { trueBlock = ast_Block(ifTrue); falseBlock = ast_Block(ifFalse); } falseIf = ast_IfCreate(NULL, falseBlock, NULL); result = ast_IfCreate(condition, trueBlock, falseIf); /* Because a ternary operator always has a true and false branch it there is not much use * in reporting unreachable code. If a ternary operator has a compile-time resolvable * condition, it is probably intended. */ ast_If_noWarnUnreachable(result); ast_Parser_collect(yparser(), falseIf); return result; }
static corto_type corto_valueExpr_getType(corto_type src, corto_type target, corto_bool targetIsRef) { corto_type result = src; if (!src) { if (target) { if (target->reference) { result = target; } else if ((target->kind == CORTO_PRIMITIVE) && (corto_primitive(target)->kind == CORTO_TEXT)) { result = corto_type(corto_string_o); } else { if (targetIsRef) { result = target; } else { corto_seterr( "invalid usage of null with non-string and non-reference type '%s'", corto_fullpath(NULL, target)); goto error; } } } else { result = corto_void_o; } } else if ((target && (target->kind == CORTO_VOID) && target->reference)) { result = corto_object_o; } return result; error: return NULL; }
corto_int16 corto_value_unaryOp( corto_operatorKind _operator, corto_value *value, corto_value *result) { corto_uint64 *v = &result->is.value.storage; corto_type t = corto_value_typeof(value); corto_type returnType = t; if (corto_valueExpr_isOperatorConditional(_operator)) { returnType = corto_type(corto_bool_o); } if (corto_ptr_unaryOp(t, _operator, corto_value_ptrof(value), v)) { goto error; } result->kind = CORTO_VALUE; result->is.value.t = returnType; result->is.value.v = (void*)v; return 0; error: return -1; }
corto_void _test_compositeUnionApi_tc_defineDefault( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_defineDefault) */ corto_int16 result; test_compositeUnionType *o = test_compositeUnionTypeDeclareChild(root_o, "o"); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 2); test_assertstr(o->is.str, "foo"); result = test_compositeUnionTypeDefine_other(o, 4, 10); test_assert(result == 0); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 4); test_assert(o->is.other == 10); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeApi_tc_define( test_compositeApi _this) { /* $begin(test/compositeApi/tc_define) */ corto_int16 result; test_compositeType *o = test_compositeTypeDeclareChild(root_o, "o"); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->x == 1); test_assert(o->y == 2); result = test_compositeTypeDefine(o, 10, 20); test_assert(result == 0); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->x == 10); test_assert(o->y == 20); test_assert(!corto_delete(o)); /* $end */ }
corto_string _md_DocStr(md_Doc value) { corto_string result; corto_value v; v = corto_value_object(value, corto_type(md_Doc_o)); result = corto_strv(&v, 0); return result; }
static int16_t corto_fmt_ptr_toValue( corto_fmt_opt *opt, corto_value *v, const void* ptr) { corto_type t = corto_type(*(corto_type*)CORTO_OFFSET(ptr, -sizeof(corto_type))); *v = corto_value_mem((void*)ptr, t); return 0; }
corto_void _test_compositeApi_tc_createAuto( test_compositeApi _this) { /* $begin(test/compositeApi/tc_createAuto) */ test_compositeTypeCreate_auto(o, 10, 20); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->x == 10); test_assert(o->y == 20); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_createAutoDefault( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_createAutoDefault) */ test_compositeUnionTypeCreate_other_auto(o, 4, 10); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 4); test_assert(o->is.other == 10); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_create( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_create) */ test_compositeUnionType *o = test_compositeUnionTypeCreate_flt(1, 10.5); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 1); test_assert(o->is.flt == 10.5); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_declareAuto( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_declareAuto) */ test_compositeUnionTypeDeclare_auto(o); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 2); test_assertstr(o->is.str, "foo"); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeApi_tc_declare( test_compositeApi _this) { /* $begin(test/compositeApi/tc_declare) */ test_compositeType *o = test_compositeTypeDeclare(); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(o->x == 1); test_assert(o->y == 2); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_declareChildAuto( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_declareChildAuto) */ test_compositeUnionTypeDeclareChild_auto(root_o, o); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 2); test_assertstr(o->is.str, "foo"); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeApi_tc_createChildAuto( test_compositeApi _this) { /* $begin(test/compositeApi/tc_createChildAuto) */ test_compositeTypeCreateChild_auto(root_o, o, 10, 20); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->x == 10); test_assert(o->y == 20); test_assert(!corto_delete(o)); /* $end */ }
/* Do metawalk on type */ int16_t _corto_metawalk( corto_walk_opt* s, corto_type type, void* userData) { corto__object* o; int16_t result; /* Since we potentially overwrite some callbacks, make private copy */ corto_walk_opt private = *s; ut_assert(type != NULL, "corto_metawalk called with NULL type"); /* Instantiate dummy-object */ o = corto_alloc(sizeof(corto__object) + type->size); memset(o, 0, sizeof(corto__object) + type->size); o->type = corto_type(type); o->refcount = 1; #ifndef NDEBUG o->magic = CORTO_MAGIC; #endif private.visitAllCases = TRUE;
corto_void _test_compositeUnionApi_tc_createChildAutoDefault( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_createChildAutoDefault) */ test_compositeUnionTypeCreateChild_other_auto( root_o, o, 4, 10); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 4); test_assert(o->is.other == 10); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_createChild( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_createChild) */ test_compositeUnionType *o = test_compositeUnionTypeCreateChild_flt( root_o, "o", 1, 10.5); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 1); test_assert(o->is.flt == 10.5); test_assert(!corto_delete(o)); /* $end */ }
md_Doc md_DocFromStr(md_Doc value, corto_string str) { corto_fromStrp(&value, corto_type(md_Doc_o), str); return value; }
corto_type corto_value_typeof(corto_value* val) { corto_type result; switch(val->kind) { case CORTO_OBJECT: result = val->is.object.t; break; case CORTO_BASE: result = val->is.base.t; break; case CORTO_VALUE: case CORTO_MEM: result = val->is.value.t; break; case CORTO_LITERAL: switch(val->is.literal.kind) { case CORTO_LITERAL_BOOLEAN: result = corto_type(corto_bool_o); break; case CORTO_LITERAL_CHARACTER: result = corto_type(corto_char_o); break; case CORTO_LITERAL_INTEGER: result = corto_type(corto_int64_o); break; case CORTO_LITERAL_UNSIGNED_INTEGER: result = corto_type(corto_uint64_o); break; case CORTO_LITERAL_FLOATING_POINT: result = corto_type(corto_float64_o); break; case CORTO_LITERAL_STRING: result = corto_type(corto_string_o); break; case CORTO_LITERAL_NULL: result = NULL; break; default: corto_critical("corto_value_typeof: invalid corto_literalKind(%d)", val->is.literal.kind); result = NULL; break; } break; case CORTO_MEMBER: result = val->is.member.t->type; break; case CORTO_CONSTANT: result = corto_value_typeof(val->parent); break; case CORTO_ELEMENT: result = val->is.element.t.type; break; case CORTO_MAP_ELEMENT: result = val->is.mapElement.t.type; break; default: corto_critical("corto_value_typeof: invalid corto_valueKind(%d).", val->kind); result = NULL; break; } return result; }
corto_int16 corto_valueExpr_getTypeForBinary( corto_type leftType, corto_bool t1ByRef, corto_type rightType, corto_bool t2ByRef, corto_operatorKind _operator, corto_type *operandType, corto_type *resultType) { corto_type castType = NULL; corto_bool equal = FALSE; corto_assert(operandType != NULL, "NULL provided for out-parameter operandType"); corto_assert(resultType != NULL, "NULL provided for out-parameter resultType"); /* If operator is condition result type is a boolean */ if (corto_valueExpr_isOperatorConditional(_operator)) { *resultType = corto_type(corto_bool_o); } /* Get leftType and rightType */ if (!(leftType = corto_valueExpr_getType(leftType, NULL, FALSE))) { goto error; } if (!(rightType = corto_valueExpr_getType(rightType, leftType, t1ByRef))) { goto error; } if (!leftType) { leftType = rightType; } /* If objects are not scoped, verify whether they're equal */ if (!corto_checkAttr(leftType, CORTO_ATTR_NAMED) && !corto_checkAttr(rightType, CORTO_ATTR_NAMED)) { if (corto_compare(leftType, rightType) == CORTO_EQ) { equal = TRUE; } } else { equal = leftType == rightType; } if (_operator == CORTO_DIV) { castType = corto_type(corto_float64_o); } else if (!equal) { /* Can only cast between primitive types */ if ((leftType->kind == CORTO_PRIMITIVE ) && (rightType->kind == CORTO_PRIMITIVE)) { corto_primitive ltype = corto_primitive(leftType), rtype = corto_primitive(rightType); corto_int8 lscore, rscore; corto_int8 lCastScore, rCastScore; lscore = corto_valueExpr_getTypeScore(ltype); rscore = corto_valueExpr_getTypeScore(rtype); lCastScore = corto_valueExpr_getCastScore(ltype); rCastScore = corto_valueExpr_getCastScore(rtype); /* If expression is an assignment, always take type of lvalue. * Otherwise determine based on expressibility score which type to * cast to. */ if (corto_valueExpr_isOperatorAssignment(_operator)) { if (lCastScore == rCastScore) { if (ltype->width != rtype->width) { castType = leftType; } } else { castType = leftType; } } else { if (lCastScore == rCastScore) { if (ltype->width == rtype->width) { /* If width and kind are equal, no conversion is required. */ if (lscore > rscore) { *operandType = leftType; } else if (lscore < rscore) { *operandType = rightType; } } else { /* If lvalue has a larger width than rvalue, cast rvalue to leftType (and vice versa) */ if (ltype->width > rtype->width) { castType = leftType; } else { castType = rightType; } } /* If kinds do not match figure a cast is potentially needed. Figure out which type to cast to */ } else { if (lscore > rscore) { castType = leftType; } else { castType = rightType; } } } } else if (leftType->reference && rightType->reference) { castType = NULL; /* Check if types are compatible */ } else if ((rightType->reference || (t2ByRef)) && !leftType->reference) { if (corto_type_castable(leftType, corto_object_o)) { castType = leftType; } } else { corto_seterr("cannot cast from '%s' to '%s'", corto_fullpath(NULL, leftType), corto_fullpath(NULL, rightType)); goto error; } } else if ((rightType->reference || (t2ByRef)) && !leftType->reference) { if (corto_type_castable(leftType, corto_object_o)) { castType = leftType; } } if (!castType) { castType = leftType; } if (castType) { *operandType = castType; } if (!*resultType) { *resultType = *operandType; } return 0; error: return -1; }
/* Parse scope */ static corto_string corto_string_deserParseScope(corto_string str, struct corto_string_deserIndexInfo* info, corto_string_deser_t* data) { struct corto_string_deserIndexInfo rootInfo; corto_typeKind kind; void *ptr = data->ptr; if (data->allocValue) { ptr = data->allocValue(ptr, data); } /* Prepare privateData */ corto_string_deser_t privateData = { .out = data->out, .scope = data->scope, .anonymousObjects = data->anonymousObjects, .type = data->type, .isObject = data->isObject, .members = {0, NULL} }; /* Offset the scope-members with the current offset */ if (info && info->m) { ptr = corto_getMemberPtr(data->isObject ? data->out : NULL, ptr, info->m); } privateData.ptr = ptr; /* Open scope of type */ if (!info) { if (!data->type) { rootInfo.type = corto_typeof(data->out); } else { rootInfo.type = data->type; } rootInfo.m = NULL; rootInfo.parsed = FALSE; info = &rootInfo; } /* Check if type is any, composite or collection */ kind = info->type->kind; if ((kind != CORTO_COMPOSITE) && (kind != CORTO_COLLECTION) && (kind != CORTO_ANY)) { corto_seterr("'{' unexpected for type '%s'", corto_fullpath(NULL, info->type)); goto error; } /* Build index for type */ if (info->type->kind == CORTO_COMPOSITE) { if (corto_interface(info->type)->kind == CORTO_UNION) { /* Parse discriminator */ struct corto_string_deserIndexInfo dInfo; dInfo.type = (corto_type)corto_int32_o; dInfo.parsed = FALSE; dInfo.m = NULL; str = corto_string_deserParse(str + 1, &dInfo, &privateData); if (!str) { goto error; } /* Find corresponding union case */ corto_int32 d = *(corto_int32*)ptr; corto_member m = safe_corto_union_findCase(info->type, d); if (!m) { corto_seterr("discriminator '%d' invalid for union '%s'", d, corto_fullpath(NULL, info->type)); goto error; } /* Build index for one member */ struct corto_string_deserIndexInfo *caseInfo = corto_alloc(sizeof(struct corto_string_deserIndexInfo)); caseInfo->type = m->type; caseInfo->parsed = FALSE; caseInfo->m = m; corto_string_deserIndexInsert(&privateData, caseInfo); } else { corto_walk_opt s = corto_string_deserBuildIndex(); s.members = data->members; if (s.members.length) { s.access = 0; } if (corto_metawalk(&s, info->type, &privateData)) { goto error; } } /* Create iterator for index */ if (privateData.index) { privateData.currentIter = _corto_ll_iter(privateData.index, &privateData.iterData); } /* If type is a collection, build index with one node for element */ } else if (info->type->kind == CORTO_COLLECTION) { struct corto_string_deserIndexInfo *elementNode; elementNode = corto_alloc(sizeof(struct corto_string_deserIndexInfo)); elementNode->m = NULL; elementNode->parsed = FALSE; elementNode->type = corto_collection(info->type)->elementType; corto_string_deserIndexInsert(&privateData, elementNode); /* Create iterator for index */ privateData.currentIter = _corto_ll_iter(privateData.index, &privateData.iterData); privateData.allocValue = corto_string_deserAllocElem; privateData.allocUdata = info->type; if (ptr) { switch(corto_collection(info->type)->kind) { case CORTO_ARRAY: case CORTO_SEQUENCE: break; case CORTO_LIST: if (!*(corto_ll*)ptr) { *(corto_ll*)ptr = corto_ll_new(); } else { corto_ll_clear(*(corto_ll*)ptr); } privateData.ptr = ptr; break; default: break; } } } else if (info->type->kind == CORTO_ANY) { struct corto_string_deserIndexInfo *anyNode; anyNode = corto_alloc(sizeof(struct corto_string_deserIndexInfo)); anyNode->m = NULL; anyNode->parsed = FALSE; anyNode->type = corto_type(corto_type_o); corto_string_deserIndexInsert(&privateData, anyNode); privateData.currentIter = _corto_ll_iter(privateData.index, &privateData.iterData); privateData.allocValue = corto_string_deserAllocAny; privateData.allocUdata = anyNode; } /* Parse scope */ if (!(str = corto_string_deserParse(str+1, info, &privateData))) { goto error; } corto_string_deserFreeIndex(&privateData); data->anonymousObjects = privateData.anonymousObjects; return str; error: return NULL; } /* Get pointer to current destination value */ corto_int16 corto_string_getDestinationPtr( struct corto_string_deserIndexInfo* info, corto_string_deser_t* data, void **ptr_out) { void *result = data->ptr; *ptr_out = NULL; if (!info) { corto_seterr("excess elements in string"); goto error; } if (data->allocValue) { result = data->allocValue(result, data); } if (info->m) { result = corto_getMemberPtr(data->isObject ? data->out : NULL, data->ptr, info->m); } *ptr_out = result; return 0; error: return -1; }
static corto_int16 json_deserComposite(void* p, corto_type t, JSON_Value *v) { corto_assert(t->kind == CORTO_COMPOSITE, "not deserializing composite"); if (json_value_get_type(v) != JSONObject) { corto_seterr("expected object, got %s", json_valueTypeToString(v)); goto error; } JSON_Object* o = json_value_get_object(v); size_t count = json_object_get_count(o); size_t i; corto_bool isUnion = corto_interface(t)->kind == CORTO_UNION; corto_int32 discriminator = 0; corto_member unionMember = NULL; for (i = 0; i < count; i++) { const char* memberName = json_object_get_name(o, i); corto_member member_o; if (!strcmp(memberName, "super")) { JSON_Value* value = json_object_get_value(o, memberName); if (json_deserItem(p, corto_type(corto_interface(t)->base), value)) { goto error; } } else if (!strcmp(memberName, "_d") && isUnion) { JSON_Value* value = json_object_get_value(o, memberName); if (json_deserPrimitive(&discriminator, corto_union(t)->discriminator, value)) { goto error; } unionMember = corto_union_findCase(t, discriminator); if (!unionMember) { corto_seterr("discriminator '%d' invalid for union '%s'", discriminator, corto_fullpath(NULL, t)); } } else { member_o = corto_interface_resolveMember(t, (char*)memberName); /* Ensure that we're not resolving members from a base type */ if (!member_o || (corto_parentof(member_o) != t)) { corto_seterr( "cannot find member '%s' in type '%s'", memberName, corto_fullpath(NULL, t)); goto error; } if (isUnion && (unionMember != member_o)) { corto_seterr( "member '%s' does not match discriminator '%d' (expected member '%s')", memberName, discriminator, corto_idof(unionMember)); goto error; } else if (isUnion) { corto_int32 prev = *(corto_int32*)p; if (prev != discriminator) { corto_member prevMember = corto_union_findCase(t, prev); corto_deinitp(CORTO_OFFSET(p, prevMember->offset), prevMember->type); memset(CORTO_OFFSET(p, member_o->offset), 0, member_o->type->size); } *(corto_int32*)p = discriminator; } if (!json_deserMustSkip(member_o, p)) { JSON_Value* value = json_object_get_value(o, memberName); void *offset = CORTO_OFFSET(p, member_o->offset); if (member_o->modifiers & CORTO_OBSERVABLE) { offset = *(void**)offset; if (json_deserType(offset, member_o->type, value)) { goto error; } } else { if (member_o->modifiers & CORTO_OPTIONAL) { if (*(void**)offset) { corto_deinitp(*(void**)offset, member_o->type); memset(*(void**)offset, 0, member_o->type->size); } else { *(void**)offset = corto_calloc(member_o->type->size); } offset = *(void**)offset; } if (json_deserItem(offset, member_o->type, value)) { goto error; } } } } } return 0; error: return -1; }
static corto_string corto_string_parseAnonymous( corto_string str, corto_string value, struct corto_string_deserIndexInfo *info, corto_string_deser_t *data) { corto_object o = NULL; char *ptr = str; char *valuePtr = value; corto_uint32 index = 0; void *offset; if (corto_string_getDestinationPtr(info, data, &offset)) { corto_seterr("%s: %s", str, corto_lasterr()); goto error; } /* Check if this is a 'named' anonymous object in which case it is * prefixed with <[id]> */ if ((valuePtr = corto_string_deserParseAnonymousId(valuePtr, &index))) { if (data->anonymousObjects && (index <= corto_ll_size(data->anonymousObjects))) { if (!*valuePtr) { o = corto_ll_get(data->anonymousObjects, index - 1); corto_claim(o); } else { /* If the <n> is followed up with more data, a new anonymous * object is being created while one existed already with the * specified number */ corto_seterr("identifier <%d> is already defined (%s)", index, value); goto error; } } } else { valuePtr = value; } /* If no object has been referenced, create an anonymous object */ if (!o && *valuePtr) { corto_object type = corto_resolve(NULL, valuePtr); if (!type) { corto_seterr("unresolved type '%s'", valuePtr); goto error; } if (offset) { o = *(corto_object*)offset; if (!o || (type != corto_typeof(o))) { if (!corto_instanceof(corto_type_o, type)) { corto_seterr("'%s' is not a type", corto_fullpath(NULL, type)); goto error; } o = corto_declare(type); if (!o) { corto_seterr("failed to declare %s: %s", value, corto_lasterr()); goto error; } } else { corto_claim(o); } } corto_string_deser_t privateData = { .out = data->out, // Preserve original out to determine ownership .ptr = o, .type = type, .anonymousObjects = data->anonymousObjects, .scope = data->scope, .isObject = data->isObject }; if (corto_type(type)->kind == CORTO_PRIMITIVE) { ptr ++; if (!(ptr = corto_string_deserParse(ptr, NULL, &privateData))) { goto error; } } else { if (!(ptr = corto_string_deserParseScope(ptr, NULL, &privateData))) { goto error; } } if (o) { if (corto_define(o)) { goto error; } data->anonymousObjects = privateData.anonymousObjects; if (!data->anonymousObjects) { data->anonymousObjects = corto_ll_new(); } corto_ll_append(data->anonymousObjects, o); } } if (offset) corto_ptr_setref(offset, o); if (o) corto_release(o); return ptr; error: if (o) corto_release(o); return NULL; }
/* This is a managed file. Do not delete this comment. */ #include <corto> int16_t corto_table_construct( corto_table this) { corto_set_ref(&corto_type(this)->parent_type, corto_tableinstance_o); return safe_corto_container_construct(this); }
/* Deserialize string in object */ corto_string corto_string_deser(corto_string str, corto_string_deser_t* data) { corto_char *ptr; corto_bool createdNew = FALSE; { corto_id buffer; corto_char *bptr, ch; /* Parse typename that potentially precedes string */ bptr = buffer; ptr = str; while((ch = *ptr) && (ch != '{')) { if (!((ch == ' ') || (ch == '\n') || (ch == '\t'))) { *bptr = ch; bptr++; } ptr++; } *bptr = '\0'; /* If no type is found, reset ptr */ if ((ch != '{') || (ptr == str)) { ptr = str; } else { corto_object type; /* If no out is provided create an object of the specified type */ if ((bptr != buffer)) { type = corto_resolve(NULL, buffer); if (type) { if (data->type && (type != data->type)) { corto_seterr( "type of object ('%s') does not match string ('%s')", corto_fullpath(NULL, data->type), corto_fullpath(NULL, type)); corto_release(type); goto error; } if (corto_instanceof(corto_type(corto_type_o), type)) { if (!data->out) { data->out = corto_declare(type); createdNew = TRUE; } data->type = type; } else { corto_seterr("'%s' is not a type", buffer); corto_release(type); goto error; } corto_release(type); } else { corto_seterr("unknown type '%s'", buffer); goto error; } } else { type = data->type; } if (type && (corto_type(type)->kind == CORTO_PRIMITIVE)) { ptr++; } } } data->current = 0; data->index = NULL; data->ptr = data->out; data->anonymousObjects = NULL; data->allocValue = NULL; data->allocUdata = NULL; if (data->out && data->isObject) { if (!data->type) { data->type = corto_typeof(data->out); } else if (!corto_instanceofType(corto_typeof(data->out), data->type)) { corto_seterr("object '%s' of type '%s' is not an instance of type '%s'", corto_fullpath(NULL, data->out), corto_fullpath(NULL, corto_typeof(data->out)), corto_fullpath(NULL, data->type)); goto error; } } if (!data->type) { corto_seterr("no type provided for '%s'", str); goto error; } if (data->type->kind == CORTO_PRIMITIVE) { ptr = corto_string_deserParse(ptr, NULL, data); } else { ptr = corto_string_deserParseScope(ptr, NULL, data); if (ptr) { ptr ++; } } if (!ptr) { if (!corto_lasterr()) { corto_seterr("failed to deserialize '%s'", str); } else { corto_seterr("failed to deserialize '%s': %s", str, corto_lasterr()); } goto error; } if (data->anonymousObjects) { corto_ll_free(data->anonymousObjects); } return ptr; error: if (data->out) { if (createdNew) { corto_release(data->out); } data->out = NULL; } return NULL; }
#include <include/test.h> void test_ValueExpr_tc_add( test_ValueExpr this) { corto_value left = corto_value_int(10); corto_value right = corto_value_int(20); corto_value out = corto_value_value(NULL, NULL); corto_int16 ret = corto_value_binaryOp(CORTO_ADD, &left, &right, &out); test_assert(ret == 0); test_assert(out.kind == CORTO_VALUE); corto_type type = corto_value_typeof(&out); test_assert(type != NULL); test_assert(type == corto_type(corto_int64_o)); corto_int64 *ptr = corto_value_ptrof(&out); test_assert(ptr != NULL); test_assertint(*ptr, 30); corto_value_free(&out); } void test_ValueExpr_tc_and( test_ValueExpr this) { corto_value left = corto_value_int(12); corto_value right = corto_value_int(9); corto_value out = corto_value_value(NULL, NULL);