--- /dev/null
+# 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
private import parser::tables
import mixin
import primitive_types
+private import model::serialize_model
redef class ToolContext
# --discover-call-trace
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
--- /dev/null
+# 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