Introduced properties

init defaultinit(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: CPPLanguage :: defaultinit

Redefined properties

redef type SELF: CPPLanguage

nitc $ CPPLanguage :: SELF

Type of this instance, automatically specialized in every class
redef fun compile_callback(callback: NitniCallback, mmodule: MModule, mainmodule: MModule, ecc: CCompilationUnit)

nitc $ CPPLanguage :: compile_callback

Generate the code to offer this callback if foreign code
redef fun compile_extern_class(block: AExternCodeBlock, m: AClassdef, ecc: CCompilationUnit, mmodule: MModule)

nitc $ CPPLanguage :: compile_extern_class

Generate wrapper code for this extern class
redef fun compile_extern_method(block: AExternCodeBlock, m: AMethPropdef, ecc: CCompilationUnit, mmodule: MModule)

nitc $ CPPLanguage :: compile_extern_method

We call C++ from C using 2 more files (ffi.c and ffi.cpp) and multiple generated functions:
redef fun compile_module_block(block: AExternCodeBlock, ecc: CCompilationUnit, mmodule: MModule)

nitc $ CPPLanguage :: compile_module_block

Generate wrapper code for this module/header code block
redef fun compile_to_files(mmodule: MModule, compdir: String)

nitc $ CPPLanguage :: compile_to_files

Complete compilation of generated code
redef fun get_ftype(block: AExternCodeBlock, m: AClassdef): ForeignType

nitc $ CPPLanguage :: get_ftype

Get the foreign type of this extern class definition
redef fun identify_language(n: AExternCodeBlock): Bool

nitc $ CPPLanguage :: identify_language

Is this block written in this language?

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _ffi_language_assignation_phase: FFILanguageAssignationPhase

nitc :: FFILanguage :: _ffi_language_assignation_phase

FFILanguageAssignationPhase assigning self to AExternCodeBlocks
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun compile_callback(callback: NitniCallback, mmodule: MModule, mainmmodule: MModule, ecc: CCompilationUnit)

nitc :: FFILanguage :: compile_callback

Generate the code to offer this callback if foreign code
abstract fun compile_extern_class(block: AExternCodeBlock, m: AClassdef, ecc: CCompilationUnit, mmodule: MModule)

nitc :: FFILanguage :: compile_extern_class

Generate wrapper code for this extern class
abstract fun compile_extern_method(block: AExternCodeBlock, m: AMethPropdef, ecc: CCompilationUnit, nmodule: MModule)

nitc :: FFILanguage :: compile_extern_method

Generate wrapper code for this extern method
abstract fun compile_module_block(block: AExternCodeBlock, ecc: CCompilationUnit, mmodule: MModule)

nitc :: FFILanguage :: compile_module_block

Generate wrapper code for this module/header code block
fun compile_to_files(mmodule: MModule, directory: String)

nitc :: FFILanguage :: compile_to_files

Complete compilation of generated code
init defaultinit(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: FFILanguage :: defaultinit

init defaultinit(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: CPPLanguage :: defaultinit

fun ffi_language_assignation_phase: FFILanguageAssignationPhase

nitc :: FFILanguage :: ffi_language_assignation_phase

FFILanguageAssignationPhase assigning self to AExternCodeBlocks
protected fun ffi_language_assignation_phase=(ffi_language_assignation_phase: FFILanguageAssignationPhase)

nitc :: FFILanguage :: ffi_language_assignation_phase=

FFILanguageAssignationPhase assigning self to AExternCodeBlocks
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
abstract fun get_ftype(block: AExternCodeBlock, m: AClassdef): ForeignType

nitc :: FFILanguage :: get_ftype

Get the foreign type of this extern class definition
fun hash: Int

core :: Object :: hash

The hash code of the object.
abstract fun identify_language(block: AExternCodeBlock): Bool

nitc :: FFILanguage :: identify_language

Is this block written in this language?
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram nitc::CPPLanguage CPPLanguage nitc::FFILanguage FFILanguage nitc::CPPLanguage->nitc::FFILanguage core::Object Object nitc::FFILanguage->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

class FFILanguage

nitc :: FFILanguage

Visitor for a specific languages. Works kinda like a Phase and is executed

Class definitions

nitc $ CPPLanguage
class CPPLanguage
	super FFILanguage

	redef fun identify_language(n) do return n.is_cpp

	redef fun compile_module_block(block, ecc, mmodule)
	do
		if mmodule.cpp_file == null then mmodule.cpp_file = new CPPCompilationUnit

		if block.is_cpp_header then
			mmodule.cpp_file.header_custom.add(block.location.as_line_pragma)
			mmodule.cpp_file.header_custom.add(block.code)
		else if block.is_cpp_body then
			mmodule.cpp_file.body_custom.add( block.location.as_line_pragma )
			mmodule.cpp_file.body_custom.add( block.code )
		end
	end

	# We call C++ from C using 2 more files (_ffi.c and _ffi.cpp) and multiple generated functions:
	# 1. The standard C implementation function (___impl) expected by the common FFI
	# 2. The indirection function (___cpp_impl_mid) is a C function, called from C but implemented as `extern "C"` in C++
	# 3. The actual C++ implementation function (___cpp_impl)
	redef fun compile_extern_method(block, m, ecc, mmodule)
	do
		if mmodule.cpp_file == null then mmodule.cpp_file = new CPPCompilationUnit

		var mclass_type = m.parent.as(AClassdef).mclass.mclass_type
		var mproperty = m.mpropdef.mproperty

		# Signature of the indirection function implemented as `extern "C"` in C++
		var indirection_sig = mproperty.build_csignature(mclass_type, mmodule, "___cpp_impl_mid", long_signature, internal_call_context)

		## In C file (__ffi.c)

		# Declare the indirection function in C
		ecc.body_decl.add("{indirection_sig};\n")

		# Call the indirection function from C (___impl)
		var fc: CFunction = new ExternCFunction(m, mmodule)
		fc.exprs.add(mproperty.build_ccall(mclass_type, mmodule, "___cpp_impl_mid", long_signature, cpp_call_context, null))
		fc.exprs.add("\n")
		ecc.add_exported_function( fc )

		## In C++ file (__ffi.cpp)

		# Declare the indirection function in C++
		mmodule.cpp_file.header_decl.add("extern \"C\" \{\n")
		mmodule.cpp_file.header_decl.add("{indirection_sig};\n")
		mmodule.cpp_file.header_decl.add("\}\n")

		# Implement the indirection function as extern in C++
		# Will convert C arguments to C++ and call the C++ implementation function.
		fc = new CFunction(indirection_sig)
		if not mproperty.is_init then
			var param_name = "self"
			var type_name = to_cpp_call_context.name_mtype(mclass_type)
			if mclass_type.mclass.ftype isa ForeignCppType then
				fc.exprs.add("{type_name} {param_name}_for_cpp = static_cast<{type_name}>({param_name});\n")
			else
				fc.exprs.add("{type_name} {param_name}_for_cpp = {param_name};\n")
			end
		end
		for param in m.mpropdef.msignature.mparameters do
			var param_name = param.name
			var type_name = to_cpp_call_context.name_mtype(param.mtype)
			if mclass_type.mclass.ftype isa ForeignCppType then
				fc.exprs.add("{type_name} {param_name}_for_cpp = static_cast<{type_name}>({param_name});\n")
			else
				fc.exprs.add("{type_name} {param_name}_for_cpp = {param_name};\n")
			end
		end
		fc.exprs.add(mproperty.build_ccall(mclass_type, mmodule, "___cpp_impl", long_signature, cpp_call_context, "_for_cpp"))
		fc.exprs.add("\n")
		mmodule.cpp_file.add_exported_function(fc)

		# Custom C++, the body of the Nit C++ method is copied to its own C++ function
		var cpp_signature = mproperty.build_csignature(mclass_type, mmodule, "___cpp_impl", long_signature, cpp_call_context)
		fc = new CFunction(cpp_signature)
		fc.decls.add( block.location.as_line_pragma )
		fc.exprs.add( block.code )
		mmodule.cpp_file.add_local_function( fc )
	end

	redef fun compile_extern_class(block, m, ecc, mmodule) do end

	redef fun get_ftype(block, m) do return new ForeignCppType(block.code)

	redef fun compile_to_files(mmodule, compdir)
	do
		var cpp_file = mmodule.cpp_file
		assert cpp_file != null

		# write .cpp and .hpp file
		cpp_file.header_custom.add("extern \"C\" \{\n")
		cpp_file.header_custom.add("#include \"{mmodule.c_name}._ffi.h\"\n")
		cpp_file.header_custom.add("\}\n")

		var file = cpp_file.write_to_files(mmodule, compdir)

		# add complation to makefile
		mmodule.ffi_files.add(file)

		# add linked option to support C++
		mmodule.ldflags.add_one("", "-lstdc++")
	end

	redef fun compile_callback(callback, mmodule, mainmodule, ecc)
	do
		callback.compile_callback_to_cpp(mmodule, mainmodule)
	end
end
src/ffi/cpp.nit:34,1--144,3