From 8189f202d95bf78a751c99e3f48b72b26d751b1a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sat, 15 Nov 2014 20:26:46 -0500 Subject: [PATCH] nitserial: add depth option MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- src/nitserial.nit | 72 +++++++++++++++++++++++++++++++++-------- tests/sav/nitserial_args1.res | 1 + 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/nitserial.nit b/src/nitserial.nit index ea2e4b4..db70d4c 100644 --- a/src/nitserial.nit +++ b/src/nitserial.nit @@ -69,9 +69,13 @@ redef class ToolContext # Where do we put the result? var opt_dir: OptionString = new OptionString("Output directory", "--dir") + # Depth of the visit and generation + var opt_depth = new OptionEnum(["module", "group", "project"], + "Depth of the visit and generation", 0, "-d", "--depth") + redef init do - option_context.add_option(opt_output, opt_dir) + option_context.add_option(opt_output, opt_dir, opt_depth) super end end @@ -130,10 +134,8 @@ var modelbuilder = new ModelBuilder(model, toolcontext) var mmodules = modelbuilder.parse_full(arguments) modelbuilder.run_phases -# Create a distinct support module per targetted modules +# Create a distinct support module per target modules for mmodule in mmodules do - var rta = modelbuilder.do_rapid_type_analysis(mmodule) - # Name of the support module var module_name @@ -155,13 +157,47 @@ for mmodule in mmodules do module_path += ".nit" end + var target_modules = null + var importations = null + var mgroup = mmodule.mgroup + if toolcontext.opt_depth.value == 1 and mgroup != null then + modelbuilder.visit_group mgroup + target_modules = mgroup.mmodules + else if toolcontext.opt_depth.value == 2 then + # project + target_modules = new Array[MModule] + importations = new Array[MModule] + if mgroup != null then + for g in mgroup.mproject.mgroups do + target_modules.add_all g.mmodules + end + + for g in mgroup.in_nesting.direct_smallers do + var dm = g.default_mmodule + if dm != null then + importations.add dm + end + end + + for m in mgroup.mmodules do + importations.add m + end + end + end + + if target_modules == null then target_modules = [mmodule] + if importations == null then importations = target_modules + var nit_module = new NitModule(module_name) nit_module.header = """ # This file is generated by nitserial # Do not modify, but you can redef """ - nit_module.imports.add mmodule.name + for importation in importations do + nit_module.imports.add importation.name + end + nit_module.imports.add "serialization" nit_module.content.add """ @@ -170,15 +206,25 @@ redef class Deserializer do""" var serializable_type = mmodule.serializable_type - for mtype in rta.live_types do - # We are only interested in instanciated generics, subtypes of Serializable - # and which are visibles. - if mtype isa MGenericType and - mtype.is_subtype(mmodule, null, serializable_type) and - mtype.is_visible_from(mmodule) then - - nit_module.content.add """ + var compiled_types = new Array[MType] + for m in target_modules do + nit_module.content.add """ + # Module: {{{m.to_s}}}""" + + var rta = modelbuilder.do_rapid_type_analysis(m) + + for mtype in rta.live_types do + # We are only interested in instanciated generics, subtypes of Serializable + # and which are visibles. + if mtype isa MGenericType and + mtype.is_subtype(m, null, serializable_type) and + mtype.is_visible_from(mmodule) and + not compiled_types.has(mtype) then + + compiled_types.add mtype + nit_module.content.add """ if name == \"{{{mtype}}}\" then return new {{{mtype}}}.from_deserializer(self)""" + end end end diff --git a/tests/sav/nitserial_args1.res b/tests/sav/nitserial_args1.res index 62be912..b69dc33 100644 --- a/tests/sav/nitserial_args1.res +++ b/tests/sav/nitserial_args1.res @@ -8,6 +8,7 @@ import serialization redef class Deserializer redef fun deserialize_class(name) do + # Module: test_serialization if name == "Array[Object]" then return new Array[Object].from_deserializer(self) if name == "Array[nullable Object]" then return new Array[nullable Object].from_deserializer(self) if name == "Array[Serializable]" then return new Array[Serializable].from_deserializer(self) -- 1.7.9.5