From: Alexis Laferrière Date: Sat, 1 Feb 2014 18:50:33 +0000 (-0500) Subject: frontend: intro the serialization phase X-Git-Tag: v0.6.4~16^2~13 X-Git-Url: http://nitlanguage.org frontend: intro the serialization phase Signed-off-by: Alexis Laferrière --- diff --git a/src/frontend.nit b/src/frontend.nit index 04254a7..a5ebfc6 100644 --- a/src/frontend.nit +++ b/src/frontend.nit @@ -25,6 +25,7 @@ import typing import auto_super_init import div_by_zero import cached +import serialization_phase redef class ToolContext # FIXME: there is conflict in linex in nitc, so use this trick to force invocation diff --git a/src/literal.nit b/src/literal.nit index 668f72a..0e08350 100644 --- a/src/literal.nit +++ b/src/literal.nit @@ -20,9 +20,10 @@ module literal import parser import toolcontext import phase +import serialization_phase redef class ToolContext - var literal_phase: Phase = new LiteralPhase(self, null) + var literal_phase: Phase = new LiteralPhase(self, [serialization_phase]) end private class LiteralPhase diff --git a/src/serialization_phase.nit b/src/serialization_phase.nit new file mode 100644 index 0000000..f129a40 --- /dev/null +++ b/src/serialization_phase.nit @@ -0,0 +1,70 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2013 Jean-Philippe Caissy +# Copyright 2013 Guillaume Auger +# Copyright 2014 Alexis Laferrière +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Phase generating methods to +module serialization_phase + +import phase +import parser_util + +redef class ToolContext + var serialization_phase: Phase = new SerializationPhase(self, null) +end + +# TODO automaticaly add Serializable as a super class +# TODO Sequences +# TODO add annotations on attributes (volatile, sensitive or do_not_serialize?) +private class SerializationPhase + super Phase + + redef fun process_annotated_node(nclassdef, nat) + do + # Skip if we are not interested + if nat.n_atid.n_id.text != "auto_serializable" then return + if not nclassdef isa AStdClassdef then + toolcontext.error(nclassdef.location, "Syntax error: only a concrete class can be automatically serialized.") + return + end + + generate_serialization_method(nclassdef) + end + + private fun generate_serialization_method(nclassdef: AClassdef) + do + var npropdefs = nclassdef.n_propdefs + + var code = new Array[String] + code.add "redef fun core_serialize_to(v)" + code.add "do" + code.add " super" + + for attribute in npropdefs do if attribute isa AAttrPropdef then + var name + if attribute.n_id == null then + name = attribute.n_id2.text + else name = attribute.n_id.text + + code.add " v.serialize_attribute(\"{name}\", {name})" + end + + code.add "end" + + # Create method Node and add it to the AST + npropdefs.push(toolcontext.parse_propdef(code.join("\n"))) + end +end