AModule is not the right class to do or store model related things.
Migrating these services simplify the code.
Signed-off-by: Jean Privat <jean@pryen.org>
# FFI
var m2m = toolcontext.modelbuilder.mmodule2nmodule
- for m in compiler.mainmodule.in_importation.greaters do if m2m.keys.has(m) then
- var amodule = m2m[m]
- compiler.finalize_ffi_for_module(amodule)
+ for m in compiler.mainmodule.in_importation.greaters do
+ compiler.finalize_ffi_for_module(m)
end
# Copy original .[ch] files to compile_dir
var linker_options = new HashSet[String]
var m2m = toolcontext.modelbuilder.mmodule2nmodule
- for m in mainmodule.in_importation.greaters do if m2m.keys.has(m) then
- var amod = m2m[m]
- var libs = amod.collect_linker_libs
+ for m in mainmodule.in_importation.greaters do
+ var libs = m.collect_linker_libs
if libs != null then linker_options.add_all(libs)
end
return ((a*10000/b).to_f / 100.0).to_precision(2)
end
- fun finalize_ffi_for_module(nmodule: AModule)
+ fun finalize_ffi_for_module(mmodule: MModule)
do
var visitor = new_visitor
- nmodule.finalize_ffi(visitor, modelbuilder)
- nmodule.finalize_nitni(visitor)
+ mmodule.finalize_ffi(visitor, modelbuilder)
+ mmodule.finalize_nitni(visitor)
end
end
return properties_cache[mclass]
end
private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
-end
-redef class AModule
# Write FFI results to file
fun finalize_ffi(v: AbstractCompilerVisitor, modelbuilder: ModelBuilder) do end
fun as_line_pragma: String do return "#line {line_start} \"{file.filename}\"\n"
end
-redef class AModule
+redef class MModule
var c_compiler_options writable = ""
var c_linker_options writable = ""
end
end
# retreive module
- var nmodule = nmoduledecl.parent.as(AModule)
+ var mmodule = nmoduledecl.parent.as(AModule).mmodule.as(not null)
for opt in simplified_options do
var cmd = opt.option
if annotation_name == compiler_annotation_name then
- process_c_compiler_annotation(nmodule, cmd)
+ process_c_compiler_annotation(mmodule, cmd)
else if annotation_name == linker_annotation_name then
- process_c_linker_annotation(nmodule, cmd)
+ process_c_linker_annotation(mmodule, cmd)
else if annotation_name == cpp_compiler_annotation_name then
- process_cpp_compiler_annotation(nmodule, cmd)
+ process_cpp_compiler_annotation(mmodule, cmd)
else abort
end
end
- fun process_c_compiler_annotation(nmodule: AModule, opt: String)
+ fun process_c_compiler_annotation(mmodule: MModule, opt: String)
do
- nmodule.c_compiler_options = "{nmodule.c_compiler_options} {opt}"
+ mmodule.c_compiler_options = "{mmodule.c_compiler_options} {opt}"
end
- fun process_c_linker_annotation(nmodule: AModule, opt: String)
+ fun process_c_linker_annotation(mmodule: MModule, opt: String)
do
- nmodule.c_linker_options = "{nmodule.c_linker_options} {opt}"
+ mmodule.c_linker_options = "{mmodule.c_linker_options} {opt}"
end
- fun process_cpp_compiler_annotation(nmodule: AModule, opt: String)
+ fun process_cpp_compiler_annotation(mmodule: MModule, opt: String)
do
- nmodule.cpp_compiler_options = "{nmodule.cpp_compiler_options} {opt}"
+ mmodule.cpp_compiler_options = "{mmodule.cpp_compiler_options} {opt}"
end
end
end
ffi_ccu.write_as_impl(self, compdir)
- for filename in ffi_ccu.files do ffi_files.add(new ExternCFile(filename, self.c_compiler_options))
+ for filename in ffi_ccu.files do ffi_files.add(new ExternCFile(filename, mmodule.c_compiler_options))
end
end
redef class AModule
private var cpp_file: nullable CPPCompilationUnit = null
+end
+
+redef class MModule
var cpp_compiler_options writable = ""
end
nmodule.ffi_files.add(file)
# add linked option to support C++
- nmodule.c_linker_options = "{nmodule.c_linker_options} -lstdc++"
+ nmodule.mmodule.c_linker_options = "{nmodule.mmodule.c_linker_options} -lstdc++"
end
redef fun compile_callback(callback, nmodule, mmodule, ecc)
fun write_to_files(amodule: AModule, compdir: String): ExternCppFile
do
- var base_name = "{amodule.mmodule.name}._ffi"
+ var mmodule = amodule.mmodule.as(not null)
+ var base_name = "{mmodule.name}._ffi"
var h_file = "{base_name}.hpp"
var guard = "{amodule.cname.to_s.to_upper}_NIT_HPP"
files.add("{compdir}/{c_file}")
- return new ExternCppFile("{compdir}/{c_file}", amodule)
+ return new ExternCppFile("{compdir}/{c_file}", mmodule)
end
end
class ExternCppFile
super ExternFile
- var amodule: AModule
- init(path: String, amodule: AModule)
+ var mmodule: MModule
+ init(path: String, mmodule: MModule)
do
super
- self.amodule = amodule
+ self.mmodule = mmodule
end
redef fun makefile_rule_name do return "{filename.basename("")}.o"
- redef fun makefile_rule_content do return "g++ {amodule.cpp_compiler_options} -c {filename.basename("")} -o {filename.basename("")}.o"
+ redef fun makefile_rule_content do return "g++ {mmodule.cpp_compiler_options} -c {filename.basename("")} -o {filename.basename("")}.o"
end
class ForeignCppType
# retreive module
var nmodule = nmoduledecl.parent.as(AModule)
+ var mmodule = nmodule.mmodule.as(not null)
# check availability of pkg-config
var proc_which = new IProcess("which", "pkg-config")
# compiler
var proc = new IProcess("pkg-config", "--cflags", pkg)
var compiler_opts = proc.read_all
- nmodule.c_compiler_options = "{nmodule.c_compiler_options} {compiler_opts.replace("\n", " ")}"
+ mmodule.c_compiler_options = "{mmodule.c_compiler_options} {compiler_opts.replace("\n", " ")}"
# linker
proc = new IProcess("pkg-config", "--libs", pkg)
var linker_opts = proc.read_all
- nmodule.c_linker_options = "{nmodule.c_linker_options} {linker_opts.replace("\n", " ")}"
+ mmodule.c_linker_options = "{mmodule.c_linker_options} {linker_opts.replace("\n", " ")}"
end
end
intrude import common_ffi
import nitni
-redef class AModule
+redef class MModule
private var foreign_callbacks = new ForeignCallbackSet
var nitni_ccu: nullable CCompilationUnit = null
+ private fun nmodule(v: AbstractCompilerVisitor): nullable AModule
+ do
+ var m2n = v.compiler.modelbuilder.mmodule2nmodule
+ return m2n.get_or_null(self)
+ end
+
redef fun finalize_ffi(v: AbstractCompilerVisitor, modelbuilder: ModelBuilder)
do
- finalize_ffi_wrapper(v.compiler.modelbuilder.compile_dir, v.compiler.mainmodule)
- for file in ffi_files do v.compiler.extern_bodies.add(file)
+ var n = nmodule(v)
+ if n == null then return
+ n.finalize_ffi_wrapper(v.compiler.modelbuilder.compile_dir, v.compiler.mainmodule)
+ for file in n.ffi_files do v.compiler.extern_bodies.add(file)
end
fun ensure_compile_nitni_base(v: AbstractCompilerVisitor)
redef fun finalize_nitni(v: AbstractCompilerVisitor)
do
+ var n = nmodule(v)
+ if n == null then return
+
ensure_compile_nitni_base(v)
- nitni_ccu.header_c_types.add("#include \"{mmodule.name}._ffi.h\"\n")
+ nitni_ccu.header_c_types.add("#include \"{name}._ffi.h\"\n")
- nitni_ccu.write_as_nitni(self, v.compiler.modelbuilder.compile_dir)
+ nitni_ccu.write_as_nitni(n, v.compiler.modelbuilder.compile_dir)
for file in nitni_ccu.files do
v.compiler.extern_bodies.add(new ExternCFile(file, c_compiler_options))
fun compile_ffi_support_to_c(v: AbstractCompilerVisitor)
do
var mmodule = mpropdef.mclassdef.mmodule
- var amainmodule = v.compiler.modelbuilder.mmodule2nmodule[v.compiler.mainmodule]
+ var mainmodule = v.compiler.mainmodule
+ var amainmodule = v.compiler.modelbuilder.mmodule2nmodule[mainmodule]
var amodule = v.compiler.modelbuilder.mmodule2nmodule[mmodule]
var mclass_type = mpropdef.mclassdef.bound_mtype
compile_ffi_method(amodule)
# nitni - Compile missing callbacks
- amodule.ensure_compile_nitni_base(v)
- var ccu = amodule.nitni_ccu.as(not null)
+ mmodule.ensure_compile_nitni_base(v)
+ var ccu = mmodule.nitni_ccu.as(not null)
for mtype in foreign_callbacks.types do
if not mtype.is_cprimitive then
mtype.compile_extern_type(v, ccu)
# has callbacks already been compiled? (this may very well happen with global compilation)
- if amodule.check_callback_compilation(mtype) then mtype.compile_extern_helper_functions(v, ccu)
+ if mmodule.check_callback_compilation(mtype) then mtype.compile_extern_helper_functions(v, ccu)
end
end
# compile callbacks
- for cb in foreign_callbacks.callbacks do if amainmodule.check_callback_compilation(cb) then
+ for cb in foreign_callbacks.callbacks do if mainmodule.check_callback_compilation(cb) then
cb.compile_extern_callback(v, ccu)
end
- for cb in foreign_callbacks.supers do if amainmodule.check_callback_compilation(cb) then
+ for cb in foreign_callbacks.supers do if mainmodule.check_callback_compilation(cb) then
cb.compile_extern_callback(v, ccu)
end
- for cb in foreign_callbacks.casts do if amainmodule.check_callback_compilation(cb) then
+ for cb in foreign_callbacks.casts do if mainmodule.check_callback_compilation(cb) then
cb.compile_extern_callbacks(v, ccu)
end
# manage nitni callback set
- amodule.foreign_callbacks.join(foreign_callbacks)
+ mmodule.foreign_callbacks.join(foreign_callbacks)
end
end
self.header.add_decl("struct nitni_instance \{struct instance *value;\};")
end
- redef fun finalize_ffi_for_module(nmodule)
+ redef fun finalize_ffi_for_module(mmodule)
do
var old_module = self.mainmodule
- self.mainmodule = nmodule.mmodule.as(not null)
+ self.mainmodule = mmodule
super
self.mainmodule = old_module
end