Compiler that translates Nit code to Java code

Introduced properties

type VISITOR: JavaCompilerVisitor

nitc :: JavaCompiler :: VISITOR

Kind of visitor to use
private var _box_kinds: Array[MClassType]

nitc :: JavaCompiler :: _box_kinds

Types of boxes used to represent Java primitive types
private var _compile_dir: String

nitc :: JavaCompiler :: _compile_dir

Where to generate tmp files
private var _files: Array[JavaCodeFile]

nitc :: JavaCompiler :: _files

The list of all associated files
private var _mainmodule: MModule

nitc :: JavaCompiler :: _mainmodule

The main module of the program currently compiled
private var _modelbuilder: ModelBuilder

nitc :: JavaCompiler :: _modelbuilder

Modelbuilder used to know the model and the AST
private var _outname: String

nitc :: JavaCompiler :: _outname

Name of the generated executable
private var _rt_model: JavaRuntimeModel

nitc :: JavaCompiler :: _rt_model

RuntimeModel representation
private var _runtime_type_analysis: RapidTypeAnalysis

nitc :: JavaCompiler :: _runtime_type_analysis

The result of the RTA (used to know live types and methods)
fun box_kinds: Array[MClassType]

nitc :: JavaCompiler :: box_kinds

Types of boxes used to represent Java primitive types
protected fun box_kinds=(box_kinds: Array[MClassType])

nitc :: JavaCompiler :: box_kinds=

Types of boxes used to represent Java primitive types
fun compile_box_kinds

nitc :: JavaCompiler :: compile_box_kinds

Prepare the boxes used to represent Java primitive types
fun compile_dir: String

nitc :: JavaCompiler :: compile_dir

Where to generate tmp files
protected fun compile_dir=(compile_dir: String)

nitc :: JavaCompiler :: compile_dir=

Where to generate tmp files
fun compile_main_function

nitc :: JavaCompiler :: compile_main_function

Generate Java main that call Sys.main
fun compile_mclasses_to_java

nitc :: JavaCompiler :: compile_mclasses_to_java

Generate a RTClass for each MClass found in model
fun compile_mmethods_to_java

nitc :: JavaCompiler :: compile_mmethods_to_java

Generate a RTMethod for each MMethodDef found in model
init defaultinit(mainmodule: MModule, modelbuilder: ModelBuilder, runtime_type_analysis: RapidTypeAnalysis)

nitc :: JavaCompiler :: defaultinit

fun do_compilation

nitc :: JavaCompiler :: do_compilation

Compile Nit code to Java
fun files: Array[JavaCodeFile]

nitc :: JavaCompiler :: files

The list of all associated files
protected fun files=(files: Array[JavaCodeFile])

nitc :: JavaCompiler :: files=

The list of all associated files
fun mainmodule: MModule

nitc :: JavaCompiler :: mainmodule

The main module of the program currently compiled
protected fun mainmodule=(mainmodule: MModule)

nitc :: JavaCompiler :: mainmodule=

The main module of the program currently compiled
fun modelbuilder: ModelBuilder

nitc :: JavaCompiler :: modelbuilder

Modelbuilder used to know the model and the AST
protected fun modelbuilder=(modelbuilder: ModelBuilder)

nitc :: JavaCompiler :: modelbuilder=

Modelbuilder used to know the model and the AST
fun new_file(name: String): JavaCodeFile

nitc :: JavaCompiler :: new_file

Force the creation of a new file
fun new_visitor(filename: String): VISITOR

nitc :: JavaCompiler :: new_visitor

Initialize a visitor specific for the compiler engine
fun outname: String

nitc :: JavaCompiler :: outname

Name of the generated executable
protected fun outname=(outname: String)

nitc :: JavaCompiler :: outname=

Name of the generated executable
private fun rt_model: JavaRuntimeModel

nitc :: JavaCompiler :: rt_model

RuntimeModel representation
private fun rt_model=(rt_model: JavaRuntimeModel)

nitc :: JavaCompiler :: rt_model=

RuntimeModel representation
fun runtime_type_analysis: RapidTypeAnalysis

nitc :: JavaCompiler :: runtime_type_analysis

The result of the RTA (used to know live types and methods)
protected fun runtime_type_analysis=(runtime_type_analysis: RapidTypeAnalysis)

nitc :: JavaCompiler :: runtime_type_analysis=

The result of the RTA (used to know live types and methods)

Redefined properties

redef type SELF: JavaCompiler

nitc $ JavaCompiler :: 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
type VISITOR: JavaCompilerVisitor

nitc :: JavaCompiler :: VISITOR

Kind of visitor to use
private var _box_kinds: Array[MClassType]

nitc :: JavaCompiler :: _box_kinds

Types of boxes used to represent Java primitive types
private var _compile_dir: String

nitc :: JavaCompiler :: _compile_dir

Where to generate tmp files
private var _files: Array[JavaCodeFile]

nitc :: JavaCompiler :: _files

The list of all associated files
private var _mainmodule: MModule

nitc :: JavaCompiler :: _mainmodule

The main module of the program currently compiled
private var _modelbuilder: ModelBuilder

nitc :: JavaCompiler :: _modelbuilder

Modelbuilder used to know the model and the AST
private var _outname: String

nitc :: JavaCompiler :: _outname

Name of the generated executable
private var _rt_model: JavaRuntimeModel

nitc :: JavaCompiler :: _rt_model

RuntimeModel representation
private var _runtime_type_analysis: RapidTypeAnalysis

nitc :: JavaCompiler :: _runtime_type_analysis

The result of the RTA (used to know live types and methods)
fun box_kinds: Array[MClassType]

nitc :: JavaCompiler :: box_kinds

Types of boxes used to represent Java primitive types
protected fun box_kinds=(box_kinds: Array[MClassType])

nitc :: JavaCompiler :: box_kinds=

Types of boxes used to represent Java primitive types
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_box_kinds

nitc :: JavaCompiler :: compile_box_kinds

Prepare the boxes used to represent Java primitive types
fun compile_dir: String

nitc :: JavaCompiler :: compile_dir

Where to generate tmp files
protected fun compile_dir=(compile_dir: String)

nitc :: JavaCompiler :: compile_dir=

Where to generate tmp files
fun compile_main_function

nitc :: JavaCompiler :: compile_main_function

Generate Java main that call Sys.main
fun compile_mclasses_to_java

nitc :: JavaCompiler :: compile_mclasses_to_java

Generate a RTClass for each MClass found in model
fun compile_mmethods_to_java

nitc :: JavaCompiler :: compile_mmethods_to_java

Generate a RTMethod for each MMethodDef found in model
init defaultinit(mainmodule: MModule, modelbuilder: ModelBuilder, runtime_type_analysis: RapidTypeAnalysis)

nitc :: JavaCompiler :: defaultinit

fun do_compilation

nitc :: JavaCompiler :: do_compilation

Compile Nit code to Java
fun files: Array[JavaCodeFile]

nitc :: JavaCompiler :: files

The list of all associated files
protected fun files=(files: Array[JavaCodeFile])

nitc :: JavaCompiler :: files=

The list of all associated files
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.
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.
fun mainmodule: MModule

nitc :: JavaCompiler :: mainmodule

The main module of the program currently compiled
protected fun mainmodule=(mainmodule: MModule)

nitc :: JavaCompiler :: mainmodule=

The main module of the program currently compiled
fun modelbuilder: ModelBuilder

nitc :: JavaCompiler :: modelbuilder

Modelbuilder used to know the model and the AST
protected fun modelbuilder=(modelbuilder: ModelBuilder)

nitc :: JavaCompiler :: modelbuilder=

Modelbuilder used to know the model and the AST
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
fun new_file(name: String): JavaCodeFile

nitc :: JavaCompiler :: new_file

Force the creation of a new file
fun new_visitor(filename: String): VISITOR

nitc :: JavaCompiler :: new_visitor

Initialize a visitor specific for the compiler engine
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun outname: String

nitc :: JavaCompiler :: outname

Name of the generated executable
protected fun outname=(outname: String)

nitc :: JavaCompiler :: outname=

Name of the generated executable
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).
private fun rt_model: JavaRuntimeModel

nitc :: JavaCompiler :: rt_model

RuntimeModel representation
private fun rt_model=(rt_model: JavaRuntimeModel)

nitc :: JavaCompiler :: rt_model=

RuntimeModel representation
fun runtime_type_analysis: RapidTypeAnalysis

nitc :: JavaCompiler :: runtime_type_analysis

The result of the RTA (used to know live types and methods)
protected fun runtime_type_analysis=(runtime_type_analysis: RapidTypeAnalysis)

nitc :: JavaCompiler :: runtime_type_analysis=

The result of the RTA (used to know live types and methods)
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::JavaCompiler JavaCompiler core::Object Object nitc::JavaCompiler->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

nitc $ JavaCompiler
# Compiler that translates Nit code to Java code
class JavaCompiler
	# The main module of the program currently compiled
	var mainmodule: MModule

	# Modelbuilder used to know the model and the AST
	var modelbuilder: ModelBuilder

	# The result of the RTA (used to know live types and methods)
	var runtime_type_analysis: RapidTypeAnalysis

	# Where to generate tmp files
	var compile_dir: String is lazy do
		var dir = modelbuilder.toolcontext.opt_compile_dir.value
		if dir == null then dir = "nitj_compile"
		return dir
	end

	# Name of the generated executable
	var outname: String is lazy do
		var name = modelbuilder.toolcontext.opt_output.value
		if name == null then name = mainmodule.jname
		return name
	end

	# The list of all associated files
	# Used to generate .java files
	var files: Array[JavaCodeFile] = new Array[JavaCodeFile]

	# Force the creation of a new file
	# The point is to avoid contamination between must-be-compiled-separately files
	fun new_file(name: String): JavaCodeFile do
		var file = new JavaCodeFile(name)
		files.add(file)
		return file
	end

	# Kind of visitor to use
	type VISITOR: JavaCompilerVisitor

	# Initialize a visitor specific for the compiler engine
	fun new_visitor(filename: String): VISITOR do
		return new JavaCompilerVisitor(self, new_file(filename))
	end

	# RuntimeModel representation
	private var rt_model: JavaRuntimeModel is lazy do return new JavaRuntimeModel

	# Compile Nit code to Java
	fun do_compilation do
		# compile java classes used to represents the runtime model of the program
		rt_model.compile_rtmodel(self)
		compile_box_kinds

		# compile class structures
		compile_mclasses_to_java

		# compile method structures
		compile_mmethods_to_java

		# compile main
		compile_main_function
	end

	# Prepare the boxes used to represent Java primitive types
	fun compile_box_kinds do
		# Collect all bas box class
		# FIXME: this is not completely fine with a separate compilation scheme
		for classname in ["Int", "Bool", "Byte", "Char", "Float"] do
			var classes = mainmodule.model.get_mclasses_by_name(classname)
			if classes == null then continue
			assert classes.length == 1 else print classes.join(", ")
			box_kinds.add(classes.first.mclass_type)
		end
	end

	# Types of boxes used to represent Java primitive types
	var box_kinds = new Array[MClassType]

	# Generate a `RTClass` for each `MClass` found in model
	#
	# This is a global phase because we need to know all the program to build
	# attributes, fill vft and type table.
	fun compile_mclasses_to_java do
		for mclass in mainmodule.model.mclasses do
			mclass.compile_to_java(new_visitor("{mclass.rt_name}.java"))
		end
	end

	# Generate a `RTMethod` for each `MMethodDef` found in model
	#
	# This is a separate phase.
	fun compile_mmethods_to_java do
		for mmodule in mainmodule.in_importation.greaters do
			for mclassdef in mmodule.mclassdefs do
				for mdef in mclassdef.mpropdefs do
					if mdef isa MMethodDef then
						mdef.compile_to_java(new_visitor("{mdef.rt_name}.java"))
					end
				end
			end
		end
	end

	# Generate Java main that call Sys.main
	fun compile_main_function do
		var v = new_visitor("{mainmodule.jname}_Main.java")
		v.add("public class {mainmodule.jname}_Main \{")
		v.add("  public static void main(String[] args) \{")

		var main_type = mainmodule.sys_type
		if main_type != null then
			var mainmodule = v.compiler.mainmodule
			var glob_sys = v.init_instance(main_type)
			var main_init = mainmodule.try_get_primitive_method("init", main_type.mclass)
			if main_init != null then
				v.send(main_init, [glob_sys])
			end
			var main_method = mainmodule.try_get_primitive_method("run", main_type.mclass) or else
				mainmodule.try_get_primitive_method("main", main_type.mclass)
			if main_method != null then
				v.send(main_method, [glob_sys])
			end
		end
		v.add("  \}")
		v.add("\}")
	end
end
src/compiler/java_compiler.nit:228,1--355,3