provides tools to write C .c and .h files

Introduced classes

class CCompilationUnit

nitc :: CCompilationUnit

Accumulates all C code for a compilation unit
class CFunction

nitc :: CFunction

Accumulates C code related to a specific function
class ExternCFile

nitc :: ExternCFile

An extern C file to compile
class ExternFile

nitc :: ExternFile

An extern file to compile

All class definitions

class CCompilationUnit

nitc $ CCompilationUnit

Accumulates all C code for a compilation unit
class CFunction

nitc $ CFunction

Accumulates C code related to a specific function
class ExternCFile

nitc $ ExternCFile

An extern C file to compile
class ExternFile

nitc $ ExternFile

An extern file to compile
package_diagram nitc::c_tools c_tools template template nitc::c_tools->template core core template->core ...core ... ...core->core nitc::light_ffi_base light_ffi_base nitc::light_ffi_base->nitc::c_tools nitc::ffi_base ffi_base nitc::ffi_base->nitc::light_ffi_base nitc::extern_classes extern_classes nitc::extern_classes->nitc::light_ffi_base nitc::light_c light_c nitc::light_c->nitc::light_ffi_base nitc::ffi_base... ... nitc::ffi_base...->nitc::ffi_base nitc::extern_classes... ... nitc::extern_classes...->nitc::extern_classes nitc::light_c... ... nitc::light_c...->nitc::light_c

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module template

template :: template

Basic template system

Children

module light_ffi_base

nitc :: light_ffi_base

Tools and utilities for implement FFI with different languages

Descendants

module a_star-m

a_star-m

module abstract_compiler

nitc :: abstract_compiler

Abstract compiler
module android

nitc :: android

Compile program for the Android platform
module c

nitc :: c

Support for nesting C code within a Nit program using its FFI
module c_compiler_options

nitc :: c_compiler_options

Offers the annotations cflags and ldflags to specify
module compiler

nitc :: compiler

Compilation to C
module compiler_ffi

nitc :: compiler_ffi

Full FFI support for the compiler
module compiler_serialization

nitc :: compiler_serialization

Serialization support for the compiler
module cpp

nitc :: cpp

Supports the use of the C++ language through the FFI
module dynamic_loading_ffi

nitc :: dynamic_loading_ffi

Execute FFI code by creating and loading shared libraries
module emscripten

nitc :: emscripten

Compile to JavaScript using the Emscripten SDK
module extern_classes

nitc :: extern_classes

Manages all extern classes and their associated foreign type.
module extra_java_files

nitc :: extra_java_files

Intro the annotation extra_java_files to compile extra java files
module ffi

nitc :: ffi

Full FFI support, independent of the compiler
module ffi_base

nitc :: ffi_base

Tools and utilities for implement FFI with different languages
module global_compiler

nitc :: global_compiler

Global compilation of a Nit program
module header_dependency

nitc :: header_dependency

Tracks which modules has public header code that must be imported
module interpreter

nitc :: interpreter

Interpretation of Nit programs
module ios

nitc :: ios

Compile programs for the iOS platform
module java

nitc :: java

FFI support for the Java language
module light

nitc :: light

Light FFI support for the compiler
module light_c

nitc :: light_c

Support for nesting C code within a Nit program using its FFI
module light_ffi

nitc :: light_ffi

Light FFI support, independent of the compiler
module light_only

nitc :: light_only

Compiler support for the light FFI only, detects unsupported usage of callbacks
module memory_logger

nitc :: memory_logger

Extension to inject memory-tracing instrumentation in code generated by nitc.
module nit

nitc :: nit

A naive Nit interpreter
module nitc

nitc :: nitc

A Nit compiler
module nith

nitc :: nith

A ligHt Nit compiler
module objc

nitc :: objc

FFI support for Objective-C
module on_demand_compiler

nitc :: on_demand_compiler

Compiles extern code within a module to a static library, as needed
module pkgconfig

nitc :: pkgconfig

Offers the PkgconfigPhase to use the external program "pkg-config" in order
module separate_compiler

nitc :: separate_compiler

Separate compilation of a Nit program
module separate_erasure_compiler

nitc :: separate_erasure_compiler

Separate compilation of a Nit program with generic type erasure
module test_astbuilder

nitc :: test_astbuilder

Program used to test the clone method of the astbuilder tool
module xcode_templates

nitc :: xcode_templates

Templates and other services to create XCode projects
# provides tools to write C .c and .h files
module c_tools

import template

# 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

# Accumulates C code related to a specific function
class CFunction
	var signature : String

	var decls = new Template
	var exprs = new Template

	fun to_writer: Template
	do
		var w = new Template

		w.add(signature)
		w.add("\n\{\n")
		w.add(decls)
		w.add("\n")
		w.add(exprs)
		w.add("\}\n")

		return w
	end
end

# An extern file to compile
class ExternFile

	# Filename relative to the nit-compile folder
	var filename: String

	# The name of the target in the Makefile
	# Usually the produced .o file
	fun makefile_rule_name: String is abstract

	# The content of the rule in the make
	# Usually the one-line shell command after the tabulation
	fun makefile_rule_content: String is abstract

	fun compiles_to_o_file: Bool do return false

	# Is `self` a Java file to include in the JAR archive?
	fun add_to_jar: Bool do return false

	# Additional libraries needed for the compilation
	# Will be used with pkg-config
	var pkgconfigs = new Array[String]
end

# An extern C file to compile
class ExternCFile
	super ExternFile

	# Custom options for the C compiler (CFLAGS)
	var cflags: String

	redef fun hash do return filename.hash
	redef fun ==(o) do return o isa ExternCFile and filename == o.filename

	redef fun makefile_rule_name do
		var basename = filename.basename(".c")
		var res = "{basename}.extern.o"
		return res
	end

	redef fun makefile_rule_content do
		var ff = filename.basename
		var o = makefile_rule_name
		var pkg = ""
		if not pkgconfigs.is_empty then
			pkg = "`pkg-config --cflags {pkgconfigs.join(" ")}`"
		end
		return "$(CC) $(CFLAGS) -Wall -Wno-unused-function {self.cflags} {pkg} -c -o {o} {ff}"
	end

	redef fun compiles_to_o_file do return true
end
src/c_tools.nit:17,1--157,3