7bdd2d49f1e5d83adb53f85c4bd85a92a9940312
[nit.git] / src / serialization_phase.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Jean-Philippe Caissy <jpcaissy@piji.ca>
4 # Copyright 2013 Guillaume Auger <jeho@resist.ca>
5 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18
19 # Phase generating methods to
20 module serialization_phase
21
22 import phase
23 import parser_util
24
25 redef class ToolContext
26 var serialization_phase: Phase = new SerializationPhase(self, null)
27 end
28
29 # TODO automaticaly add Serializable as a super class
30 # TODO Sequences
31 # TODO add annotations on attributes (volatile, sensitive or do_not_serialize?)
32 private class SerializationPhase
33 super Phase
34
35 redef fun process_annotated_node(nclassdef, nat)
36 do
37 # Skip if we are not interested
38 if nat.n_atid.n_id.text != "auto_serializable" then return
39 if not nclassdef isa AStdClassdef then
40 toolcontext.error(nclassdef.location, "Syntax error: only a concrete class can be automatically serialized.")
41 return
42 end
43
44 generate_serialization_method(nclassdef)
45
46 generate_deserialization_init(nclassdef)
47 end
48
49 redef fun process_nmodule(nmodule)
50 do
51 # collect all classes
52 var auto_serializable_nclassdefs = new Array[AStdClassdef]
53 for nclassdef in nmodule.n_classdefs do
54 if nclassdef isa AStdClassdef and
55 not nclassdef.collect_annotations_by_name("auto_serializable").is_empty then
56 auto_serializable_nclassdefs.add nclassdef
57 end
58 end
59
60 if not auto_serializable_nclassdefs.is_empty then
61 generate_deserialization_method(nmodule, auto_serializable_nclassdefs)
62 end
63 end
64
65 private fun generate_serialization_method(nclassdef: AClassdef)
66 do
67 var npropdefs = nclassdef.n_propdefs
68
69 var code = new Array[String]
70 code.add "redef fun core_serialize_to(v)"
71 code.add "do"
72 code.add " super"
73
74 for attribute in npropdefs do if attribute isa AAttrPropdef then
75 var name = attribute.name
76 code.add " v.serialize_attribute(\"{name}\", {name})"
77 end
78
79 code.add "end"
80
81 # Create method Node and add it to the AST
82 npropdefs.push(toolcontext.parse_propdef(code.join("\n")))
83 end
84
85 # Add a constructor to the automated nclassdef
86 private fun generate_deserialization_init(nclassdef: AClassdef)
87 do
88 var npropdefs = nclassdef.n_propdefs
89
90 var code = new Array[String]
91 code.add "init from_deserializer(v: Deserializer)"
92 code.add "do"
93 code.add " v.notify_of_creation self"
94
95 for attribute in npropdefs do if attribute isa AAttrPropdef then
96 var name = attribute.name
97 var type_name = attribute.type_name
98 code.add ""
99 code.add "\tvar {name} = v.deserialize_attribute(\"{name}\")"
100 code.add "\tassert {name} isa {type_name} else print \"Expected attribute '{name}' to be of type '{type_name}'\""
101 code.add "\tself.{name} = {name}"
102 end
103
104 code.add "end"
105 npropdefs.add(toolcontext.parse_propdef(code.join("\n")))
106 end
107
108 # Added to the abstract serialization service
109 private fun generate_deserialization_method(nmodule: AModule, nclassdefs: Array[AStdClassdef])
110 do
111 var code = new Array[String]
112
113 var deserializer_nclassdef = nmodule.deserializer_nclassdef
114 var deserializer_npropdef
115 if deserializer_nclassdef == null then
116 # create the class
117 code.add "redef class Deserializer"
118 deserializer_npropdef = null
119 else
120 deserializer_npropdef = deserializer_nclassdef.deserializer_npropdef
121 end
122
123 if deserializer_npropdef == null then
124 # create the property
125 code.add " redef fun deserialize_class(name)"
126 code.add " do"
127 else
128 toolcontext.error(deserializer_npropdef.location, "Annotation error: you cannont define Deserializer::deserialize_class in a module where you use \"auto_serializable\".")
129 return
130 end
131
132 for nclassdef in nclassdefs do
133 var name = nclassdef.n_id.text
134 if not name.has('[') then # FIXME this is a temporary hack
135 code.add " if name == \"{name}\" then return new {name}.from_deserializer(self)"
136 end
137 end
138
139 code.add " return super"
140 code.add " end"
141
142 if deserializer_nclassdef == null then
143 code.add "end"
144 nmodule.n_classdefs.add toolcontext.parse_classdef(code.join("\n"))
145 else
146 deserializer_nclassdef.n_propdefs.add(toolcontext.parse_propdef(code.join("\n")))
147 end
148 end
149 end
150
151 redef class AAttrPropdef
152 private fun name: String
153 do
154 if n_id == null then return n_id2.text
155 return n_id.text
156 end
157
158 private fun type_name: String
159 do
160 var name = n_type.n_id.text
161
162 if n_type.n_kwnullable != null then name = "nullable {name}"
163
164 var types = n_type.n_types
165 if not types.is_empty then
166 var params = new Array[String]
167 for t in types do params.add(t.n_id.text)
168 return "{name}[{params.join(", ")}]"
169 else return name
170 end
171 end
172
173 redef class AModule
174 private fun deserializer_nclassdef: nullable AStdClassdef
175 do
176 for nclassdef in n_classdefs do
177 if nclassdef isa AStdClassdef and nclassdef.n_id.text == "Deserialization" then
178 return nclassdef
179 end
180 end
181
182 return null
183 end
184 end
185
186 redef class AStdClassdef
187 private fun deserializer_npropdef: nullable AMethPropdef
188 do
189 for npropdef in n_propdefs do if npropdef isa AMethPropdef then
190 var id = npropdef.n_methid
191 if id isa AIdMethid and id.n_id.text == "deserialize_class" then
192 return npropdef
193 end
194 end
195
196 return null
197 end
198 end