X-Git-Url: http://nitlanguage.org diff --git a/src/serialization_phase.nit b/src/serialization_phase.nit index d17a40a..f565f56 100644 --- a/src/serialization_phase.nit +++ b/src/serialization_phase.nit @@ -16,7 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Phase generating methods to +# Phase generating methods to serialize Nit objects to different formats module serialization_phase import phase @@ -42,6 +42,24 @@ private class SerializationPhase end generate_serialization_method(nclassdef) + + generate_deserialization_init(nclassdef) + end + + redef fun process_nmodule(nmodule) + do + # collect all classes + var auto_serializable_nclassdefs = new Array[AStdClassdef] + for nclassdef in nmodule.n_classdefs do + if nclassdef isa AStdClassdef and + not nclassdef.collect_annotations_by_name("auto_serializable").is_empty then + auto_serializable_nclassdefs.add nclassdef + end + end + + if not auto_serializable_nclassdefs.is_empty then + generate_deserialization_method(nmodule, auto_serializable_nclassdefs) + end end private fun generate_serialization_method(nclassdef: AClassdef) @@ -63,6 +81,75 @@ private class SerializationPhase # Create method Node and add it to the AST npropdefs.push(toolcontext.parse_propdef(code.join("\n"))) end + + # Add a constructor to the automated nclassdef + private fun generate_deserialization_init(nclassdef: AClassdef) + do + var npropdefs = nclassdef.n_propdefs + + var code = new Array[String] + code.add "init from_deserializer(v: Deserializer)" + code.add "do" + code.add " v.notify_of_creation self" + + for attribute in npropdefs do if attribute isa AAttrPropdef then + if attribute.n_type == null then + toolcontext.error(attribute.location, "NOT YET IMPLEMENTED: all attributes of an auto_serialized class definition must define a type.") + continue + end + var name = attribute.name + var type_name = attribute.type_name + code.add "" + code.add "\tvar {name} = v.deserialize_attribute(\"{name}\")" + code.add "\tassert {name} isa {type_name} else print \"Expected attribute '{name}' to be of type '{type_name}'\"" + code.add "\tself.{name} = {name}" + end + + code.add "end" + npropdefs.add(toolcontext.parse_propdef(code.join("\n"))) + end + + # Added to the abstract serialization service + private fun generate_deserialization_method(nmodule: AModule, nclassdefs: Array[AStdClassdef]) + do + var code = new Array[String] + + var deserializer_nclassdef = nmodule.deserializer_nclassdef + var deserializer_npropdef + if deserializer_nclassdef == null then + # create the class + code.add "redef class Deserializer" + deserializer_npropdef = null + else + deserializer_npropdef = deserializer_nclassdef.deserializer_npropdef + end + + if deserializer_npropdef == null then + # create the property + code.add " redef fun deserialize_class(name)" + code.add " do" + else + toolcontext.error(deserializer_npropdef.location, "Annotation error: you cannont define Deserializer::deserialize_class in a module where you use \"auto_serializable\".") + return + end + + for nclassdef in nclassdefs do + var name = nclassdef.n_id.text + if not name.chars.has('[') then # FIXME this is a temporary hack + code.add " if name == \"{name}\" then return new {name}.from_deserializer(self)" + end + end + + code.add " return super" + code.add " end" + + if deserializer_nclassdef == null then + code.add "end" + nmodule.n_classdefs.add toolcontext.parse_classdef(code.join("\n")) + else + deserializer_nclassdef.n_propdefs.add(toolcontext.parse_propdef(code.join("\n"))) + end + end end redef class AAttrPropdef @@ -76,6 +163,8 @@ redef class AAttrPropdef do var name = n_type.n_id.text + if n_type.n_kwnullable != null then name = "nullable {name}" + var types = n_type.n_types if not types.is_empty then var params = new Array[String] @@ -84,3 +173,30 @@ redef class AAttrPropdef else return name end end + +redef class AModule + private fun deserializer_nclassdef: nullable AStdClassdef + do + for nclassdef in n_classdefs do + if nclassdef isa AStdClassdef and nclassdef.n_id.text == "Deserialization" then + return nclassdef + end + end + + return null + end +end + +redef class AStdClassdef + private fun deserializer_npropdef: nullable AMethPropdef + do + for npropdef in n_propdefs do if npropdef isa AMethPropdef then + var id = npropdef.n_methid + if id isa AIdMethid and id.n_id.text == "deserialize_class" then + return npropdef + end + end + + return null + end +end