static void writeADT(A2PWriter writer, A2PType expected, ATerm value){ int termType = ATgetType(value); if(termType == AT_APPL){ ATermAppl appl = (ATermAppl) value; AFun fun = ATgetAFun(appl); char *name = ATgetName(fun); int arity = ATgetArity(fun); A2PType constructorType = A2PlookupConstructorType(expected, name, arity); if(constructorType == NULL){ fprintf(stderr, "Unable to find a constructor that matches the given ADT type. Name: %s, arity: %d, ADT name: %s.\n", name, arity, ((A2PAbstractDataType) expected->theType)->name); exit(1); } writeConstructor(writer, constructorType, appl); }else{ A2PType wrapper; switch(termType){ case AT_INT: wrapper = A2PlookupConstructorWrapper(expected, A2PintegerType()); break; case AT_REAL: wrapper = A2PlookupConstructorWrapper(expected, A2PrealType()); break; default: fprintf(stderr, "The given ATerm of type: %d, can not be a constructor.\n", termType); exit(1); } if(wrapper == NULL){ fprintf(stderr, "Unable to find constructor wrapper for ATerm with type : %d.\n", termType); exit(1);} writeConstructor(writer, wrapper, ATmakeAppl1(ATmakeAFun(((A2PConstructorType) wrapper->theType)->name, 1, ATfalse), value)); } }
void BinWriter::writeClass(json_t *jclass) { writeMemberInfo(jclass); // handle class modifiers json_t *attr_array = json_object_get(jclass, "classattributes"); bytes.writeInt((int)json_array_size(attr_array)); for (size_t i = 0; i < json_array_size(attr_array); i++) { bytes.writeInt((int)poolJString(json_array_get(attr_array, i))); } // base class int ibaseType = -1; utString sbaseType = json_string_value( json_object_get(jclass, "baseType")); if (sbaseType.size() > 0) { ibaseType = poolString(sbaseType.c_str()); } bytes.writeInt(ibaseType); // interfaces json_t *jinterfaces = json_object_get(jclass, "interfaces"); bytes.writeInt((int)json_array_size(jinterfaces)); for (size_t i = 0; i < json_array_size(jinterfaces); i++) { json_t *o = json_array_get(jinterfaces, i); bytes.writeInt(poolString(json_string_value(o))); } // delegate types json_t *jdelegate_types = json_object_get(jclass, "delegateTypes"); bytes.writeInt((int)json_array_size(jdelegate_types)); for (size_t i = 0; i < json_array_size(jdelegate_types); i++) { json_t *o = json_array_get(jdelegate_types, i); bytes.writeInt(poolString(json_string_value(o))); } // delegate return type int idelegatereturntype = -1; utString sdelegateReturnType = json_string_value( json_object_get(jclass, "delegateReturnType")); if (sdelegateReturnType.size() > 0) { idelegatereturntype = poolString(sdelegateReturnType.c_str()); } bytes.writeInt(idelegatereturntype); // write imports json_t *import_array = json_object_get(jclass, "imports"); bytes.writeInt((int)json_array_size(import_array)); for (size_t i = 0; i < json_array_size(import_array); i++) { json_t *jimport = json_array_get(import_array, i); bytes.writeInt(poolString(json_string_value(jimport))); } // write constructor json_t *jconstructor = json_object_get(jclass, "constructor"); if (jconstructor) { bytes.writeBoolean(true); writeConstructor(jconstructor); } else { bytes.writeBoolean(false); } // write fields json_t *field_array = json_object_get(jclass, "fields"); bytes.writeInt((int)json_array_size(field_array)); for (size_t i = 0; i < json_array_size(field_array); i++) { json_t *fo = json_array_get(field_array, i); writeField(fo); } // write properties json_t *prop_array = json_object_get(jclass, "properties"); bytes.writeInt((int)json_array_size(prop_array)); for (size_t i = 0; i < json_array_size(prop_array); i++) { json_t *po = json_array_get(prop_array, i); writeProperty(po); } // write methods json_t *method_array = json_object_get(jclass, "methods"); bytes.writeInt((int)json_array_size(method_array)); for (size_t i = 0; i < json_array_size(method_array); i++) { json_t *jmethod = json_array_get(method_array, i); writeMethodInfo(jmethod); } // static initializer byte code utString bc = json_string_value(json_object_get(jclass, "bytecode_staticinitializer")); bytes.writeString(bc.c_str()); // instance initializer byte code bc = json_string_value(json_object_get(jclass, "bytecode_instanceinitializer")); bytes.writeString(bc.c_str()); }
static void doSerialize(A2PWriter writer, A2PType expected, ATerm value){ DKISIndexedSet sharedValues = writer->valueSharingMap; int valueHash = hashValue(value); int valueId = DKISget(sharedValues, (void*) value, (void*) expected, valueHash); /* TODO: Fix sharing (check types). */ if(valueId != -1){ writeByteToBuffer(writer->buffer, PDB_SHARED_FLAG); printInteger(writer->buffer, valueId); return; } switch(expected->id){ case PDB_VALUE_TYPE_HEADER: serializeUntypedTerm(writer, value); break; case PDB_BOOL_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Boolean didn't have AT_APPL type.\n"); exit(1); } writeBool(writer, (ATermAppl) value); break; case PDB_INTEGER_TYPE_HEADER: if(ATgetType(value) != AT_INT){ fprintf(stderr, "Integer didn't have AT_INT type.\n"); exit(1); } writeInteger(writer, (ATermInt) value); break; case PDB_DOUBLE_TYPE_HEADER: if(ATgetType(value) != AT_REAL){ fprintf(stderr, "Double didn't have AT_REAL type.\n"); exit(1); } writeDouble(writer, (ATermReal) value); break; case PDB_STRING_TYPE_HEADER: if(ATgetType(value) != AT_APPL || ATisQuoted(ATgetAFun((ATermAppl) value)) == ATfalse){ fprintf(stderr, "String didn't have 'quoted' AT_APPL type.\n"); ATabort(""); exit(1); } writeString(writer, (ATermAppl) value); break; case PDB_SOURCE_LOCATION_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Source location didn't have AT_APPL type.\n"); exit(1); } writeSourceLocation(writer, (ATermAppl) value); break; case PDB_NODE_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Node didn't have AT_APPL type.\n"); exit(1); } { ATermList annotations = (ATermList) ATgetAnnotations(value); if(annotations == NULL){ writeNode(writer, expected, (ATermAppl) value); }else{ if(((A2PNodeType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Node term has annotations, but none are declared.\n"); exit(1); } writeAnnotatedNode(writer, expected, (ATermAppl) value, annotations); } } break; case PDB_TUPLE_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Tuple didn't have AT_APPL type.\n"); exit(1); } writeTuple(writer, expected, (ATermAppl) value); break; case PDB_LIST_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "List didn't have AT_LIST type.\n"); exit(1); } writeList(writer, expected, (ATermList) value); break; case PDB_SET_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Set didn't have AT_LIST type.\n"); exit(1); } writeSet(writer, expected, (ATermList) value); break; case PDB_RELATION_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Relation didn't have AT_LIST type.\n"); exit(1); } writeRelation(writer, expected, (ATermList) value); break; case PDB_MAP_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Map didn't have AT_LIST type.\n"); exit(1); } writeMap(writer, expected, (ATermList) value); break; case PDB_CONSTRUCTOR_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Constructor didn't have AT_APPL type.\n"); exit(1); } { ATermList annotations = (ATermList) ATgetAnnotations(value); if(annotations == NULL){ writeConstructor(writer, expected, (ATermAppl) value); }else{ if(((A2PConstructorType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Constructor term has annotations, but none are declared.\n"); exit(1); } writeAnnotatedConstructor(writer, expected, (ATermAppl) value, annotations); } } break; case PDB_ADT_TYPE_HEADER: writeADT(writer, expected, value); break; default: fprintf(stderr, "Unserializable type: %d\n.", expected->id); exit(1); } DKISstore(sharedValues, (void*) value, (void*) expected, valueHash); }