1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Serialization support compiler, a tool to support deserialization of live generic types
19 # Executing on the module `game_logic` will create the module `game_logic_serial`
20 # in the local directory. Mixing the generated module to the main module with
21 # `nitc game_logic.nit -m game_logic_serial` will create a program supporting
22 # deserialization of all generic types visible from the main module.
24 # Because the generation is limited to the visible types, a module author might want
25 # generate and include its own serialization support module.
32 import rapid_type_analysis
34 redef class ToolContext
35 # Where do we put a single result?
36 var opt_output
: OptionString = new OptionString("Output file (can also be 'stdout')", "-o", "--output")
38 # Where do we put the result?
39 var opt_dir
: OptionString = new OptionString("Output directory", "--dir")
41 # Depth of the visit and generation
42 var opt_depth
= new OptionEnum(["module", "group", "package"],
43 "Depth of the visit and generation", 0, "-d", "--depth")
47 option_context
.add_option
(opt_output
, opt_dir
, opt_depth
)
53 # Get the type of the class `Serializable`
54 var serializable_type
: MClassType is lazy
do
55 return self.get_primitive_class
("Serializable").mclass_type
60 # Is this type fully visible from `mmodule`?
61 fun is_visible_from
(mmodule
: MModule): Bool is abstract
64 redef class MClassType
65 redef fun is_visible_from
(mmodule
) do
66 return mmodule
.is_visible
(mclass
.intro_mmodule
, mclass
.visibility
)
70 redef class MNullableType
71 redef fun is_visible_from
(mmodule
) do return mtype
.is_visible_from
(mmodule
)
74 redef class MGenericType
75 redef fun is_visible_from
(mmodule
)
77 for arg_mtype
in arguments
do if not arg_mtype
.is_visible_from
(mmodule
) then return false
82 var toolcontext
= new ToolContext
83 toolcontext
.tooldescription
= """
84 Usage: nitserial [OPTION] program.nit [other_program.nit [...]]
85 Generates a serialization support module"""
87 toolcontext
.process_options
(args
)
88 var arguments
= toolcontext
.option_context
.rest
91 if toolcontext
.opt_output
.value
!= null and toolcontext
.opt_dir
.value
!= null then
92 print
"Error: cannot use both --dir and --output"
95 if arguments
.length
> 1 and toolcontext
.opt_output
.value
!= null then
96 print
"Error: --output needs a single source file. Do you prefer --dir?"
100 var model
= new Model
101 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
103 var mmodules
= modelbuilder
.parse_full
(arguments
)
104 modelbuilder
.run_phases
106 # Create a distinct support module per target modules
107 for mmodule
in mmodules
do
108 # Name of the support module
111 # Path to the support module
112 var module_path
= toolcontext
.opt_output
.value
113 if module_path
== null then
114 module_name
= "{mmodule.name}_serial"
115 module_path
= "{module_name}.nit"
117 var dir
= toolcontext
.opt_dir
.value
118 if dir
!= null then module_path
= dir
.join_path
(module_path
)
119 else if module_path
== "stdout" then
120 module_name
= "{mmodule.name}_serial"
122 else if module_path
.has_suffix
(".nit") then
123 module_name
= module_path
.basename
(".nit")
125 module_name
= module_path
.basename
126 module_path
+= ".nit"
129 var target_modules
= null
130 var importations
= null
131 var mgroup
= mmodule
.mgroup
132 if toolcontext
.opt_depth
.value
== 1 and mgroup
!= null then
133 modelbuilder
.scan_group mgroup
134 target_modules
= mgroup
.mmodules
135 else if toolcontext
.opt_depth
.value
== 2 then
137 target_modules
= new Array[MModule]
138 importations
= new Array[MModule]
139 if mgroup
!= null then
140 for g
in mgroup
.mpackage
.mgroups
do
141 target_modules
.add_all g
.mmodules
144 for g
in mgroup
.in_nesting
.direct_smallers
do
145 var dm
= g
.default_mmodule
151 for m
in mgroup
.mmodules
do
157 if target_modules
== null then target_modules
= [mmodule
]
158 if importations
== null then importations
= target_modules
160 var nit_module
= new NitModule(module_name
)
161 nit_module
.annotations
.add
"""no_warning("property-conflict")"""
162 nit_module
.header
= """
163 # This file is generated by nitserial
164 # Do not modify, but you can redef
167 for importation
in importations
do
168 nit_module
.imports
.add importation
.name
171 nit_module
.imports
.add
"serialization"
173 nit_module
.content
.add
"""
174 redef class Deserializer
175 redef fun deserialize_class(name)
178 var serializable_type
= mmodule
.serializable_type
179 var compiled_types
= new Array[MType]
180 for m
in target_modules
do
181 nit_module
.content
.add
"""
182 # Module: {{{m.to_s}}}"""
184 var rta
= modelbuilder
.do_rapid_type_analysis
(m
)
186 for mtype
in rta
.live_types
do
187 # We are only interested in instanciated generics, subtypes of Serializable
188 # and which are visible.
189 if mtype
isa MGenericType and
190 mtype
.is_subtype
(m
, null, serializable_type
) and
191 mtype
.is_visible_from
(mmodule
) and
192 mtype
.mclass
.kind
== concrete_kind
and
193 not compiled_types
.has
(mtype
) then
195 compiled_types
.add mtype
196 nit_module
.content
.add
"""
197 if name == \"{{{mtype}}}\" then return new {{{mtype}}}.from_deserializer(self)"""
202 nit_module
.content
.add
"""
207 # Compile support module
208 if module_path
!= null then
210 nit_module
.write_to_file module_path
213 nit_module
.write_to stdout