Callref expression support for Separate Compiler.
[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 var uses_json_serialization_read = false
25 for mod in compiler.mainmodule.in_importation.greaters do
26 var concern = mod.parent_concern
27 if mod.name == "serialization_read" and concern != null and concern.name == "json" then
28 uses_json_serialization_read = true
29 break
30 end
31 end
32
33 if uses_json_serialization_read then
34 write_poset_to_file(compiler, "nit_class_inheritance_metamodel", compiler.mainmodule.flatten_mclass_hierarchy)
35 end
36
37 super
38 end
39
40 # Write `poset` to a C file
41 private fun write_poset_to_file(compiler: AbstractCompiler, name: String, poset: POSet[Object])
42 do
43 var json = poset.to_thin_json
44
45 var code = new CodeFile(name)
46 compiler.files.add code
47
48 var writer = new CodeWriter(code)
49 writer.add """
50 char *{{{name}}} = "{{{json.escape_to_c}}}";
51 """
52 end
53 end
54
55 redef class AMethPropdef
56 redef fun compile_intern_to_c(v, mpropdef, arguments)
57 do
58 var pname = mpropdef.mproperty.name
59 var ret = mpropdef.msignature.as(not null).return_mtype
60 if pname == "class_inheritance_metamodel_json" then
61 v.add("extern char* nit_class_inheritance_metamodel;")
62 v.ret(v.new_expr("nit_class_inheritance_metamodel", ret.as(not null)))
63 return true
64 end
65
66 return super
67 end
68 end