35c37b22f55d5d96c9294d68174ac20c147d7fad
[nit.git] / src / compiler / compiler_serialization.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Serialization support for the compiler
16 module compiler_serialization
17
18 import model::serialize_model
19 import abstract_compiler
20
21 redef class ModelBuilder
22 redef fun write_and_make(compiler)
23 do
24 write_poset_to_file(compiler, "nit_class_inheritance_metamodel", compiler.mainmodule.flatten_mclass_hierarchy)
25
26 super
27 end
28
29 # Write `poset` to a C file
30 private fun write_poset_to_file(compiler: AbstractCompiler, name: String, poset: POSet[Object])
31 do
32 var json = poset.to_thin_json
33
34 var code = new CodeFile(name)
35 compiler.files.add code
36
37 var writer = new CodeWriter(code)
38 writer.add """
39 char *{{{name}}} = "{{{json.escape_to_c}}}";
40 """
41 end
42 end
43
44 redef class AMethPropdef
45 redef fun compile_intern_to_c(v, mpropdef, arguments)
46 do
47 var pname = mpropdef.mproperty.name
48 var ret = mpropdef.msignature.as(not null).return_mtype
49 if pname == "class_inheritance_metamodel_json" then
50 v.add("extern char* nit_class_inheritance_metamodel;")
51 v.ret(v.new_expr("nit_class_inheritance_metamodel", ret.as(not null)))
52 return true
53 end
54
55 return super
56 end
57 end