nitc & niti: support intern method to generate serialization_metadata
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 8 Sep 2016 17:32:00 +0000 (13:32 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 14 Oct 2016 20:02:20 +0000 (16:02 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

src/compiler/compiler.nit
src/compiler/compiler_serialization.nit [new file with mode: 0644]
src/interpreter/naive_interpreter.nit
src/model/serialize_model.nit [new file with mode: 0644]

index 3c0ab59..4dce71a 100644 (file)
@@ -19,6 +19,7 @@ import separate_erasure_compiler
 import global_compiler
 import compiler_ffi
 import memory_logger
+import compiler_serialization
 
 import platform::android
 import platform::pnacl
diff --git a/src/compiler/compiler_serialization.nit b/src/compiler/compiler_serialization.nit
new file mode 100644 (file)
index 0000000..35c37b2
--- /dev/null
@@ -0,0 +1,57 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# 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.
+
+# Serialization support for the compiler
+module compiler_serialization
+
+import model::serialize_model
+import abstract_compiler
+
+redef class ModelBuilder
+       redef fun write_and_make(compiler)
+       do
+               write_poset_to_file(compiler, "nit_class_inheritance_metamodel", compiler.mainmodule.flatten_mclass_hierarchy)
+
+               super
+       end
+
+       # Write `poset` to a C file
+       private fun write_poset_to_file(compiler: AbstractCompiler, name: String, poset: POSet[Object])
+       do
+               var json = poset.to_thin_json
+
+               var code = new CodeFile(name)
+               compiler.files.add code
+
+               var writer = new CodeWriter(code)
+               writer.add """
+char *{{{name}}} = "{{{json.escape_to_c}}}";
+               """
+       end
+end
+
+redef class AMethPropdef
+       redef fun compile_intern_to_c(v, mpropdef, arguments)
+       do
+               var pname = mpropdef.mproperty.name
+               var ret = mpropdef.msignature.as(not null).return_mtype
+               if pname == "class_inheritance_metamodel_json" then
+                       v.add("extern char* nit_class_inheritance_metamodel;")
+                       v.ret(v.new_expr("nit_class_inheritance_metamodel", ret.as(not null)))
+                       return true
+               end
+
+               return super
+       end
+end
index 1c48d8f..62cb4d8 100644 (file)
@@ -22,6 +22,7 @@ import semantize
 private import parser::tables
 import mixin
 import primitive_types
+private import model::serialize_model
 
 redef class ToolContext
        # --discover-call-trace
@@ -950,6 +951,8 @@ redef class AMethPropdef
                        return v.bool_instance(args[0].mtype == args[1].mtype)
                else if pname == "is_same_instance" then
                        return v.bool_instance(args[0].eq_is(args[1]))
+               else if pname == "class_inheritance_metamodel_json" then
+                       return v.native_string_instance(v.mainmodule.flatten_mclass_hierarchy.to_thin_json)
                else if pname == "exit" then
                        exit(args[1].to_i)
                        abort
diff --git a/src/model/serialize_model.nit b/src/model/serialize_model.nit
new file mode 100644 (file)
index 0000000..e0ba1ad
--- /dev/null
@@ -0,0 +1,36 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# 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.
+
+# Service to serialize `POSet` to JSON
+module serialize_model
+
+private import json::serialization_write
+
+import model
+
+redef class POSet[E]
+
+       # Serialize `self` to JSON using the `to_s` representation of each item
+       fun to_thin_json: String
+       do
+               var thin_poset = new POSet[String]
+               for e in self do
+                       var from = (e or else "null").to_s
+                       for g in self[e].direct_greaters do
+                               thin_poset.add_edge(from, (g or else "null").to_s)
+                       end
+               end
+               return thin_poset.serialize_to_json
+       end
+end