Visitor to find and explain asserts

Introduced properties

private var _assert_node: AAssertExpr

nitc :: ExplainAssertVisitor :: _assert_node

Visited assert node
private var _builder: ASTBuilder

nitc :: ExplainAssertVisitor :: _builder

Tool to modify the AST
private var _explain_str: ASuperstringExpr

nitc :: ExplainAssertVisitor :: _explain_str

Superstring in construction to explain the assert_node
private var _mmodule: MModule

nitc :: ExplainAssertVisitor :: _mmodule

The visited module
private var _string_mtype: MType

nitc :: ExplainAssertVisitor :: _string_mtype

Type of String (the generated code does not work without a String)
private var _toolcontext: ToolContext

nitc :: ExplainAssertVisitor :: _toolcontext

The toolcontext is our entry point to most services
private fun assert_node: AAssertExpr

nitc :: ExplainAssertVisitor :: assert_node

Visited assert node
private fun assert_node=(assert_node: AAssertExpr)

nitc :: ExplainAssertVisitor :: assert_node=

Visited assert node
private fun builder: ASTBuilder

nitc :: ExplainAssertVisitor :: builder

Tool to modify the AST
private fun builder=(builder: ASTBuilder)

nitc :: ExplainAssertVisitor :: builder=

Tool to modify the AST
init defaultinit(toolcontext: ToolContext, mmodule: MModule, string_mtype: MType)

nitc :: ExplainAssertVisitor :: defaultinit

private fun explain_args(n_args: AExprs)

nitc :: ExplainAssertVisitor :: explain_args

Add all the arguments in AExprs to explain_str
private fun explain_expr(v_expr: AExpr)

nitc :: ExplainAssertVisitor :: explain_expr

Add the value of v_expr to explain_str and protect null values
private fun explain_str: ASuperstringExpr

nitc :: ExplainAssertVisitor :: explain_str

Superstring in construction to explain the assert_node
private fun explain_str=(explain_str: ASuperstringExpr)

nitc :: ExplainAssertVisitor :: explain_str=

Superstring in construction to explain the assert_node
private fun explain_string(value: String, auto_add: nullable Bool): AStringExpr

nitc :: ExplainAssertVisitor :: explain_string

Build an AStringExpr containing value
private fun mmodule: MModule

nitc :: ExplainAssertVisitor :: mmodule

The visited module
private fun mmodule=(mmodule: MModule)

nitc :: ExplainAssertVisitor :: mmodule=

The visited module
private fun string_mtype: MType

nitc :: ExplainAssertVisitor :: string_mtype

Type of String (the generated code does not work without a String)
private fun string_mtype=(string_mtype: MType)

nitc :: ExplainAssertVisitor :: string_mtype=

Type of String (the generated code does not work without a String)
private fun toolcontext: ToolContext

nitc :: ExplainAssertVisitor :: toolcontext

The toolcontext is our entry point to most services
private fun toolcontext=(toolcontext: ToolContext)

nitc :: ExplainAssertVisitor :: toolcontext=

The toolcontext is our entry point to most services

Redefined properties

redef type SELF: ExplainAssertVisitor

nitc $ ExplainAssertVisitor :: SELF

Type of this instance, automatically specialized in every class
redef fun visit(node: ANode)

nitc $ ExplainAssertVisitor :: visit

What the visitor do when a node is visited

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 _assert_node: AAssertExpr

nitc :: ExplainAssertVisitor :: _assert_node

Visited assert node
private var _builder: ASTBuilder

nitc :: ExplainAssertVisitor :: _builder

Tool to modify the AST
private var _current_node: nullable ANode

nitc :: Visitor :: _current_node

The current visited node
private var _explain_str: ASuperstringExpr

nitc :: ExplainAssertVisitor :: _explain_str

Superstring in construction to explain the assert_node
private var _mmodule: MModule

nitc :: ExplainAssertVisitor :: _mmodule

The visited module
private var _string_mtype: MType

nitc :: ExplainAssertVisitor :: _string_mtype

Type of String (the generated code does not work without a String)
private var _toolcontext: ToolContext

nitc :: ExplainAssertVisitor :: _toolcontext

The toolcontext is our entry point to most services
private fun assert_node: AAssertExpr

nitc :: ExplainAssertVisitor :: assert_node

Visited assert node
private fun assert_node=(assert_node: AAssertExpr)

nitc :: ExplainAssertVisitor :: assert_node=

Visited assert node
private fun builder: ASTBuilder

nitc :: ExplainAssertVisitor :: builder

Tool to modify the AST
private fun builder=(builder: ASTBuilder)

nitc :: ExplainAssertVisitor :: builder=

Tool to modify the AST
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 current_node: nullable ANode

nitc :: Visitor :: current_node

The current visited node
fun current_node=(current_node: nullable ANode)

nitc :: Visitor :: current_node=

The current visited node
init defaultinit(toolcontext: ToolContext, mmodule: MModule, string_mtype: MType)

nitc :: ExplainAssertVisitor :: defaultinit

fun enter_visit(e: nullable ANode)

nitc :: Visitor :: enter_visit

Ask the visitor to visit a given node.
private fun explain_args(n_args: AExprs)

nitc :: ExplainAssertVisitor :: explain_args

Add all the arguments in AExprs to explain_str
private fun explain_expr(v_expr: AExpr)

nitc :: ExplainAssertVisitor :: explain_expr

Add the value of v_expr to explain_str and protect null values
private fun explain_str: ASuperstringExpr

nitc :: ExplainAssertVisitor :: explain_str

Superstring in construction to explain the assert_node
private fun explain_str=(explain_str: ASuperstringExpr)

nitc :: ExplainAssertVisitor :: explain_str=

Superstring in construction to explain the assert_node
private fun explain_string(value: String, auto_add: nullable Bool): AStringExpr

nitc :: ExplainAssertVisitor :: explain_string

Build an AStringExpr containing value
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.
private fun mmodule: MModule

nitc :: ExplainAssertVisitor :: mmodule

The visited module
private fun mmodule=(mmodule: MModule)

nitc :: ExplainAssertVisitor :: mmodule=

The visited module
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
private fun string_mtype: MType

nitc :: ExplainAssertVisitor :: string_mtype

Type of String (the generated code does not work without a String)
private fun string_mtype=(string_mtype: MType)

nitc :: ExplainAssertVisitor :: string_mtype=

Type of String (the generated code does not work without a String)
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 toolcontext: ToolContext

nitc :: ExplainAssertVisitor :: toolcontext

The toolcontext is our entry point to most services
private fun toolcontext=(toolcontext: ToolContext)

nitc :: ExplainAssertVisitor :: toolcontext=

The toolcontext is our entry point to most services
protected abstract fun visit(e: ANode)

nitc :: Visitor :: visit

What the visitor do when a node is visited
package_diagram nitc::explain_assert::ExplainAssertVisitor ExplainAssertVisitor nitc::Visitor Visitor nitc::explain_assert::ExplainAssertVisitor->nitc::Visitor core::Object Object nitc::Visitor->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Visitor

nitc :: Visitor

Abstract standard visitor on the AST

Class definitions

nitc $ ExplainAssertVisitor
# Visitor to find and explain asserts
private class ExplainAssertVisitor
	super Visitor

	# The toolcontext is our entry point to most services
	var toolcontext: ToolContext

	# The visited module
	var mmodule: MModule

	# Type of `String` (the generated code does not work without a `String`)
	var string_mtype: MType

	# Tool to modify the AST
	var builder = new ASTBuilder(mmodule) is lazy

	redef fun visit(node)
	do
		# Recursively visit all sub-nodes
		node.visit_all(self)

		# Only work on asserts
		if not node isa AAssertExpr then return
		var expr = node.n_expr

		# Skip assert on a single boolean var and asserts on false:
		# ~~~
		# assert false
		# # or
		# var x = false # Or any boolean expression
		# assert x
		# ~~~
		if expr isa AVarExpr or expr isa AFalseExpr then return

		# Build the superstring to explain the assert
		var explain_str = new ASuperstringExpr

		# Prepare attribute used by visited nodes
		self.assert_node = node
		self.explain_str = explain_str
		expr.accept_explain_assert self

		# Done! Store the superstring in the assert's node
		if explain_str.n_exprs.not_empty then
			node.explain_assert_str = explain_str
		end
	end

	# Visited assert node
	var assert_node: AAssertExpr is noinit

	# Superstring in construction to explain the `assert_node`
	var explain_str: ASuperstringExpr is noinit

	# Build an `AStringExpr` containing `value`
	#
	# Add it to `explain_str` if `auto_add == true`, the default.
	fun explain_string(value: String, auto_add: nullable Bool): AStringExpr
	do
		auto_add = auto_add or else true

		var tk = new TString
		tk.text = "\"{value}\""
		var op = new AStringExpr
		op.n_string = tk
		op.mtype = string_mtype
		op.value = value
		op.location = assert_node.location

		if auto_add then explain_str.n_exprs.add op
		return op
	end

	# Add the value of `v_expr` to `explain_str` and protect null values
	fun explain_expr(v_expr: AExpr)
	do
		var mtype = v_expr.mtype
		if mtype == null then
			explain_string "<unexpected error>"
			return
		end

		# Set the expression value aside
		var old_parent = v_expr.parent
		var expr = v_expr.make_var_read
		if old_parent != null then old_parent.validate

		# Protect nullable types
		if mtype isa MNullType then
			explain_string "null"
			return
		else if mtype isa MNullableType then
			var e = new AOrElseExpr
			e.n_expr = expr
			e.n_expr2 = explain_string("null", false)
			e.location = assert_node.location
			e.mtype = mmodule.object_type

			explain_str.n_exprs.add e
			return
		end

		explain_str.n_exprs.add expr
	end

	# Add all the arguments in `AExprs` to `explain_str`
	fun explain_args(n_args: AExprs)
	do
		var first = true
		for n_arg in n_args.to_a do
			if not first then
				explain_string ", "
			else first = false

			explain_expr n_arg
		end
	end
end
src/frontend/explain_assert.nit:65,1--182,3