pep8analysis: add copyright info for viz.js
[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 serialize Nit objects to different formats
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 if attribute.n_type == null then
97 toolcontext.error(attribute.location, "NOT YET IMPLEMENTED: all attributes of an auto_serialized class definition must define a type.")
98 continue
99 end
100 var name = attribute.name
101 var type_name = attribute.type_name
102 code.add ""
103 code.add "\tvar {name} = v.deserialize_attribute(\"{name}\")"
104 code.add "\tassert {name} isa {type_name} else print \"Expected attribute '{name}' to be of type '{type_name}'\""
105 code.add "\tself.{name} = {name}"
106 end
107
108 code.add "end"
109 npropdefs.add(toolcontext.parse_propdef(code.join("\n")))
110 end
111
112 # Added to the abstract serialization service
113 private fun generate_deserialization_method(nmodule: AModule, nclassdefs: Array[AStdClassdef])
114 do
115 var code = new Array[String]
116
117 var deserializer_nclassdef = nmodule.deserializer_nclassdef
118 var deserializer_npropdef
119 if deserializer_nclassdef == null then
120 # create the class
121 code.add "redef class Deserializer"
122 deserializer_npropdef = null
123 else
124 deserializer_npropdef = deserializer_nclassdef.deserializer_npropdef
125 end
126
127 if deserializer_npropdef == null then
128 # create the property
129 code.add " redef fun deserialize_class(name)"
130 code.add " do"
131 else
132 toolcontext.error(deserializer_npropdef.location, "Annotation error: you cannont define Deserializer::deserialize_class in a module where you use \"auto_serializable\".")
133 return
134 end
135
136 for nclassdef in nclassdefs do
137 var name = nclassdef.n_id.text
138 if not name.chars.has('[') then # FIXME this is a temporary hack
139 code.add " if name == \"{name}\" then return new {name}.from_deserializer(self)"
140 end
141 end
142
143 code.add " return super"
144 code.add " end"
145
146 if deserializer_nclassdef == null then
147 code.add "end"
148 nmodule.n_classdefs.add toolcontext.parse_classdef(code.join("\n"))
149 else
150 deserializer_nclassdef.n_propdefs.add(toolcontext.parse_propdef(code.join("\n")))
151 end
152 end
153 end
154
155 redef class AAttrPropdef
156 private fun name: String
157 do
158 if n_id == null then return n_id2.text
159 return n_id.text
160 end
161
162 private fun type_name: String
163 do
164 var name = n_type.n_id.text
165
166 if n_type.n_kwnullable != null then name = "nullable {name}"
167
168 var types = n_type.n_types
169 if not types.is_empty then
170 var params = new Array[String]
171 for t in types do params.add(t.n_id.text)
172 return "{name}[{params.join(", ")}]"
173 else return name
174 end
175 end
176
177 redef class AModule
178 private fun deserializer_nclassdef: nullable AStdClassdef
179 do
180 for nclassdef in n_classdefs do
181 if nclassdef isa AStdClassdef and nclassdef.n_id.text == "Deserialization" then
182 return nclassdef
183 end
184 end
185
186 return null
187 end
188 end
189
190 redef class AStdClassdef
191 private fun deserializer_npropdef: nullable AMethPropdef
192 do
193 for npropdef in n_propdefs do if npropdef isa AMethPropdef then
194 var id = npropdef.n_methid
195 if id isa AIdMethid and id.n_id.text == "deserialize_class" then
196 return npropdef
197 end
198 end
199
200 return null
201 end
202 end