Accumulates all C code for a compilation unit

Introduced properties

private var _body_custom: Template

nitc :: CCompilationUnit :: _body_custom

custom code and generated for ffi
private var _body_decl: Template

nitc :: CCompilationUnit :: _body_decl

body
private var _body_impl: Template

nitc :: CCompilationUnit :: _body_impl

implementation body of extern methods
private var _files: Array[String]

nitc :: CCompilationUnit :: _files

files to compile TODO check is appropriate
private var _header_c_types: Template

nitc :: CCompilationUnit :: _header_c_types

types of extern classes and friendly types
private var _header_custom: Template

nitc :: CCompilationUnit :: _header_custom

custom C header code or generated for other languages
private var _header_decl: Template

nitc :: CCompilationUnit :: _header_decl

implementation declaration for extern methods
fun add_exported_function(c_function: CFunction)

nitc :: CCompilationUnit :: add_exported_function

Add a public c_function accessible from outside this compilation unit
fun add_local_function(c_function: CFunction)

nitc :: CCompilationUnit :: add_local_function

Add a static c_function to be strictly local to this unit
private fun add_non_static_local_function(c_function: CFunction)

nitc :: CCompilationUnit :: add_non_static_local_function

Similar to add_local_function but not static
fun body_custom: Template

nitc :: CCompilationUnit :: body_custom

custom code and generated for ffi
protected fun body_custom=(body_custom: Template)

nitc :: CCompilationUnit :: body_custom=

custom code and generated for ffi
protected fun body_decl=(body_decl: Template)

nitc :: CCompilationUnit :: body_decl=

body
fun body_impl: Template

nitc :: CCompilationUnit :: body_impl

implementation body of extern methods
protected fun body_impl=(body_impl: Template)

nitc :: CCompilationUnit :: body_impl=

implementation body of extern methods
fun compile_body_core(stream: Writer)

nitc :: CCompilationUnit :: compile_body_core

Write the core of the body to stream
fun compile_header_core(stream: Writer)

nitc :: CCompilationUnit :: compile_header_core

Write the core of the header to stream
fun files: Array[String]

nitc :: CCompilationUnit :: files

files to compile TODO check is appropriate
protected fun files=(files: Array[String])

nitc :: CCompilationUnit :: files=

files to compile TODO check is appropriate
protected fun header_c_base=(header_c_base: Template)

nitc :: CCompilationUnit :: header_c_base=

header
fun header_c_types: Template

nitc :: CCompilationUnit :: header_c_types

types of extern classes and friendly types
protected fun header_c_types=(header_c_types: Template)

nitc :: CCompilationUnit :: header_c_types=

types of extern classes and friendly types
fun header_custom: Template

nitc :: CCompilationUnit :: header_custom

custom C header code or generated for other languages
protected fun header_custom=(header_custom: Template)

nitc :: CCompilationUnit :: header_custom=

custom C header code or generated for other languages
fun header_decl: Template

nitc :: CCompilationUnit :: header_decl

implementation declaration for extern methods
protected fun header_decl=(header_decl: Template)

nitc :: CCompilationUnit :: header_decl=

implementation declaration for extern methods
private fun write_as_foreign_lib_api(mmodule: MModule, compdir: String)

nitc :: CCompilationUnit :: write_as_foreign_lib_api

Write this compilation unit as the API of a foreign code library
fun write_as_impl(mmodule: MModule, compdir: String)

nitc :: CCompilationUnit :: write_as_impl

Compile as _ffi files which contains the implementation of extern methods
fun write_as_nitni(mmodule: MModule, compdir: String)

nitc :: CCompilationUnit :: write_as_nitni

Compile a _nitni files, used to implement nitni features for the compiler
fun write_body_to_file(mmodule: MModule, file: String, includes: Array[String])

nitc :: CCompilationUnit :: write_body_to_file

Write the body part to file including all includes
fun write_header_to_file(mmodule: MModule, file: String, includes: Array[String], guard: String)

nitc :: CCompilationUnit :: write_header_to_file

Write the header part to file including all includes using the guard

Redefined properties

redef type SELF: CCompilationUnit

nitc $ CCompilationUnit :: SELF

Type of this instance, automatically specialized in every class

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 _body_custom: Template

nitc :: CCompilationUnit :: _body_custom

custom code and generated for ffi
private var _body_decl: Template

nitc :: CCompilationUnit :: _body_decl

body
private var _body_impl: Template

nitc :: CCompilationUnit :: _body_impl

implementation body of extern methods
private var _files: Array[String]

nitc :: CCompilationUnit :: _files

files to compile TODO check is appropriate
private var _header_c_types: Template

nitc :: CCompilationUnit :: _header_c_types

types of extern classes and friendly types
private var _header_custom: Template

nitc :: CCompilationUnit :: _header_custom

custom C header code or generated for other languages
private var _header_decl: Template

nitc :: CCompilationUnit :: _header_decl

implementation declaration for extern methods
fun add_exported_function(c_function: CFunction)

nitc :: CCompilationUnit :: add_exported_function

Add a public c_function accessible from outside this compilation unit
fun add_local_function(c_function: CFunction)

nitc :: CCompilationUnit :: add_local_function

Add a static c_function to be strictly local to this unit
private fun add_non_static_local_function(c_function: CFunction)

nitc :: CCompilationUnit :: add_non_static_local_function

Similar to add_local_function but not static
fun body_custom: Template

nitc :: CCompilationUnit :: body_custom

custom code and generated for ffi
protected fun body_custom=(body_custom: Template)

nitc :: CCompilationUnit :: body_custom=

custom code and generated for ffi
protected fun body_decl=(body_decl: Template)

nitc :: CCompilationUnit :: body_decl=

body
fun body_impl: Template

nitc :: CCompilationUnit :: body_impl

implementation body of extern methods
protected fun body_impl=(body_impl: Template)

nitc :: CCompilationUnit :: body_impl=

implementation body of extern methods
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.
fun compile_body_core(stream: Writer)

nitc :: CCompilationUnit :: compile_body_core

Write the core of the body to stream
fun compile_header_core(stream: Writer)

nitc :: CCompilationUnit :: compile_header_core

Write the core of the header to stream
fun files: Array[String]

nitc :: CCompilationUnit :: files

files to compile TODO check is appropriate
protected fun files=(files: Array[String])

nitc :: CCompilationUnit :: files=

files to compile TODO check is appropriate
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
protected fun header_c_base=(header_c_base: Template)

nitc :: CCompilationUnit :: header_c_base=

header
fun header_c_types: Template

nitc :: CCompilationUnit :: header_c_types

types of extern classes and friendly types
protected fun header_c_types=(header_c_types: Template)

nitc :: CCompilationUnit :: header_c_types=

types of extern classes and friendly types
fun header_custom: Template

nitc :: CCompilationUnit :: header_custom

custom C header code or generated for other languages
protected fun header_custom=(header_custom: Template)

nitc :: CCompilationUnit :: header_custom=

custom C header code or generated for other languages
fun header_decl: Template

nitc :: CCompilationUnit :: header_decl

implementation declaration for extern methods
protected fun header_decl=(header_decl: Template)

nitc :: CCompilationUnit :: header_decl=

implementation declaration for extern methods
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.
private fun write_as_foreign_lib_api(mmodule: MModule, compdir: String)

nitc :: CCompilationUnit :: write_as_foreign_lib_api

Write this compilation unit as the API of a foreign code library
fun write_as_impl(mmodule: MModule, compdir: String)

nitc :: CCompilationUnit :: write_as_impl

Compile as _ffi files which contains the implementation of extern methods
fun write_as_nitni(mmodule: MModule, compdir: String)

nitc :: CCompilationUnit :: write_as_nitni

Compile a _nitni files, used to implement nitni features for the compiler
fun write_body_to_file(mmodule: MModule, file: String, includes: Array[String])

nitc :: CCompilationUnit :: write_body_to_file

Write the body part to file including all includes
fun write_header_to_file(mmodule: MModule, file: String, includes: Array[String], guard: String)

nitc :: CCompilationUnit :: write_header_to_file

Write the header part to file including all includes using the guard
package_diagram nitc::CCompilationUnit CCompilationUnit core::Object Object nitc::CCompilationUnit->core::Object nitc::CPPCompilationUnit CPPCompilationUnit nitc::CPPCompilationUnit->nitc::CCompilationUnit nitc::objc::ObjCCompilationUnit ObjCCompilationUnit nitc::objc::ObjCCompilationUnit->nitc::CCompilationUnit

Parents

interface Object

core :: Object

The root of the class hierarchy.

Children

Class definitions

nitc $ CCompilationUnit
# Accumulates all C code for a compilation unit
class CCompilationUnit
	## header
	# comments and native interface imports
	var header_c_base = new Template

	# custom C header code or generated for other languages
	var header_custom = new Template

	# types of extern classes and friendly types
	var header_c_types = new Template

	# implementation declaration for extern methods
	var header_decl = new Template

	## body
	# comments, imports, etc
	var body_decl = new Template

	# custom code and generated for ffi
	var body_custom = new Template

	# implementation body of extern methods
	var body_impl = new Template

	# files to compile TODO check is appropriate
	var files = new Array[String]

	# Add a `static` `c_function` to be strictly local to this unit
	fun add_local_function(c_function: CFunction)
	do
		body_decl.add "static {c_function.signature};\n"
		body_impl.add "\n"
		body_impl.add c_function.to_writer
	end

	# Add a public `c_function` accessible from outside this compilation unit
	fun add_exported_function(c_function: CFunction)
	do
		body_decl.add "{c_function.signature};\n"
		body_impl.add "\n"
		body_impl.add c_function.to_writer
	end

	# Write the core of the header to `stream`
	fun compile_header_core(stream: Writer)
	do
		header_c_base.write_to stream
		header_custom.write_to stream
		header_c_types.write_to stream
		header_decl.write_to stream
	end

	# Write the core of the body to `stream`
	fun compile_body_core(stream: Writer)
	do
		body_decl.write_to stream
		body_custom.write_to stream
		body_impl.write_to stream
	end
end
src/c_tools.nit:22,1--82,3

nitc :: light_ffi_base $ CCompilationUnit
redef class CCompilationUnit
	# Compile as `_ffi` files which contains the implementation of extern methods
	fun write_as_impl(mmodule: MModule, compdir: String)
	do
		var base_name = "{mmodule.c_name}._ffi"

		var h_file = "{base_name}.h"
		var guard = "{mmodule.c_name.to_upper}_NIT_H"
		write_header_to_file(mmodule, "{compdir}/{h_file}", ["<stdint.h>"], guard)

		var c_file = "{base_name}.c"
		write_body_to_file(mmodule, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "<stdint.h>", "\"{h_file}\""])

		files.add( "{compdir}/{c_file}" )
	end

	# Write the header part to `file` including all `includes` using the `guard`
	fun write_header_to_file(mmodule: MModule, file: String, includes: Array[String], guard: String)
	do
		var stream = new FileWriter.open( file )

		# header comments
		var module_info = "/*\n\tExtern implementation of Nit module {mmodule.name}\n*/\n"

		stream.write( module_info )

		stream.write( "#ifndef {guard}\n" )
		stream.write( "#define {guard}\n\n" )

		for incl in includes do stream.write( "#include {incl}\n" )

		compile_header_core( stream )

		# header file guard close
		stream.write( "#endif\n" )
		stream.close
	end

	# Write the body part to `file` including all `includes`
	fun write_body_to_file(mmodule: MModule, file: String, includes: Array[String])
	do
		var stream = new FileWriter.open(file)

		var module_info = "/*\n\tExtern implementation of Nit module {mmodule.name}\n*/\n"

		stream.write( module_info )
		for incl in includes do stream.write( "#include {incl}\n" )

		compile_body_core( stream )

		stream.close
	end
end
src/ffi/light_ffi_base.nit:156,1--208,3

nitc :: java $ CCompilationUnit
redef class CCompilationUnit
	# Similar to `add_local_function` but not `static`
	#
	# Used when the signature contains a visibility attribute.
	private fun add_non_static_local_function(c_function: CFunction)
	do
		body_decl.add c_function.signature
		body_decl.add ";\n"

		body_impl.add "\n"
		body_impl.add c_function.to_writer
	end
end
src/ffi/java.nit:838,1--850,3

nitc :: light $ CCompilationUnit
redef class CCompilationUnit
	# Compile a `_nitni` files, used to implement nitni features for the compiler
	fun write_as_nitni(mmodule: MModule, compdir: String)
	do
		var base_name = "{mmodule.c_name}._nitni"

		var h_file = "{base_name}.h"
		write_header_to_file( mmodule, "{compdir}/{h_file}", new Array[String],
			"{mmodule.c_name.to_s.to_upper}_NITG_NITNI_H")

		var c_file = "{base_name}.c"
		write_body_to_file( mmodule, "{compdir}/{c_file}", ["\"{h_file}\""] )

		files.add( "{compdir}/{c_file}" )
	end
end
src/compiler/compiler_ffi/light.nit:223,1--238,3

nitc :: on_demand_compiler $ CCompilationUnit
redef class CCompilationUnit
	# Write this compilation unit as the API of a foreign code library
	private fun write_as_foreign_lib_api(mmodule: MModule, compdir: String)
	do
		# The FFI expects the support header to end with `._nitni.h`
		var base_name = mmodule.c_name + "._nitni"
		var guard = mmodule.c_name.to_s.to_upper + "_API_H"
		var header_comment = """
/*
	Public API to foreign code of the Nit module {{{mmodule.name}}}
*/
"""

		# Header file
		var h_file = base_name+".h"
		var stream = new FileWriter.open(compdir/h_file)
		stream.write header_comment
		stream.write """
#ifndef {{{guard}}}
#define {{{guard}}}
"""
		compile_header_core stream
		stream.write """

#endif
"""
		stream.close

		# Body file
		var c_file = base_name+".c"
		stream = new FileWriter.open(compdir/c_file)
		stream.write header_comment
		stream.write """
#include "{{{h_file}}}"
"""
		compile_body_core stream
		stream.close

		# Only the C files needs compiling
		files.add compdir / c_file
	end
end
src/interpreter/dynamic_loading_ffi/on_demand_compiler.nit:240,1--281,3