Static Analysis Framework base

A lot of TODOs still missing:

  • flow breaks (AReturnExpr, AEscapeExpr, AAbortExpr)
  • other conditionnals (AIfexprExpr, AAssertExpr, ABonBoolExpr)
  • flow through AAttrPropDef

Introduced classes

interface ADoBlockHelper

nitc :: ADoBlockHelper

Represent all kind of do .. end blocks.
class FlowHashMap[K: nullable Object, V: nullable Object]

nitc :: FlowHashMap

A FlowSet based on a HashMap.
class FlowHashSet[E: nullable Object]

nitc :: FlowHashSet

A FlowSet based on a HashSet.
interface FlowSet

nitc :: FlowSet

FlowSet are used to represent data at the entry and exit of a statement.
class ForwardAnalysis

nitc :: ForwardAnalysis

An analysis go forward from the entry point to the exit point.
abstract class StaticAnalysis

nitc :: StaticAnalysis

The base of all analysis performed statically.

Redefined classes

redef class ADoExpr

nitc :: saf_base $ ADoExpr

A do statement
redef class AForExpr

nitc :: saf_base $ AForExpr

A for statement
redef class AIfExpr

nitc :: saf_base $ AIfExpr

A if statement
redef class ALoopExpr

nitc :: saf_base $ ALoopExpr

A loop statement
redef class AMethPropdef

nitc :: saf_base $ AMethPropdef

A definition of all kind of method (including constructors)
redef abstract class ANode

nitc :: saf_base $ ANode

Root of the AST class-hierarchy
redef class AWhileExpr

nitc :: saf_base $ AWhileExpr

A while statement

All class definitions

interface ADoBlockHelper

nitc $ ADoBlockHelper

Represent all kind of do .. end blocks.
redef class ADoExpr

nitc :: saf_base $ ADoExpr

A do statement
redef class AForExpr

nitc :: saf_base $ AForExpr

A for statement
redef class AIfExpr

nitc :: saf_base $ AIfExpr

A if statement
redef class ALoopExpr

nitc :: saf_base $ ALoopExpr

A loop statement
redef class AMethPropdef

nitc :: saf_base $ AMethPropdef

A definition of all kind of method (including constructors)
redef abstract class ANode

nitc :: saf_base $ ANode

Root of the AST class-hierarchy
redef class AWhileExpr

nitc :: saf_base $ AWhileExpr

A while statement
class FlowHashMap[K: nullable Object, V: nullable Object]

nitc $ FlowHashMap

A FlowSet based on a HashMap.
class FlowHashSet[E: nullable Object]

nitc $ FlowHashSet

A FlowSet based on a HashSet.
interface FlowSet

nitc $ FlowSet

FlowSet are used to represent data at the entry and exit of a statement.
class ForwardAnalysis

nitc $ ForwardAnalysis

An analysis go forward from the entry point to the exit point.
abstract class StaticAnalysis

nitc $ StaticAnalysis

The base of all analysis performed statically.
package_diagram nitc::saf_base saf_base nitc::modelbuilder modelbuilder nitc::saf_base->nitc::modelbuilder nitc::loader loader nitc::modelbuilder->nitc::loader nitc::phase phase nitc::modelbuilder->nitc::phase ...nitc::loader ... ...nitc::loader->nitc::loader ...nitc::phase ... ...nitc::phase->nitc::phase nitc::reaching_defs reaching_defs nitc::reaching_defs->nitc::saf_base nitc::saf saf nitc::saf->nitc::reaching_defs nitc::saf... ... nitc::saf...->nitc::saf

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 caching

serialization :: caching

Services for caching serialization engines
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 console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

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

graph :: digraph

Implementation of directed graphs, also called digraphs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
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 ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module lexer

nitc :: lexer

Lexer and its tokens.
module lexer_work

nitc :: lexer_work

Internal algorithm and data structures for the Nit lexer
module list

core :: list

This module handle double linked lists
module loader

nitc :: loader

Loading of Nit source files
module location

nitc :: location

Nit source-file and locations in source-file
module math

core :: math

Mathematical operations
module mdoc

nitc :: mdoc

Documentation of model entities
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module mmodule

nitc :: mmodule

modules and module hierarchies in the metamodel
module model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module mpackage

nitc :: mpackage

Modelisation of a Nit package
module native

core :: native

Native structures for text and bytes
module nitpm_shared

nitc :: nitpm_shared

Services related to the Nit package manager
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module ordered_tree

ordered_tree :: ordered_tree

Manipulation and presentation of ordered trees.
module parser

nitc :: parser

Parser.
module parser_nodes

nitc :: parser_nodes

AST nodes of the Nit language
module parser_prod

nitc :: parser_prod

Production AST nodes full definition.
module parser_work

nitc :: parser_work

Internal algorithm and data structures for the Nit parser
module phase

nitc :: phase

Phases of the processing of nit programs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
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 serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
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 tables

nitc :: tables

Module that interfaces the parsing tables.
module template

template :: template

Basic template system
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 toolcontext

nitc :: toolcontext

Common command-line tool infrastructure than handle options and error messages
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
module version

nitc :: version

This file was generated by git-gen-version.sh

Parents

Children

Descendants

module a_star-m

a_star-m

module nitsaf

nitc :: nitsaf

Nit Static Analysis Framework client example.
module saf

nitc :: saf

Nit Static Analysis Framework.
# Static Analysis Framework base
#
# A lot of TODOs still missing:
# * flow breaks (AReturnExpr, AEscapeExpr, AAbortExpr)
# * other conditionnals (AIfexprExpr, AAssertExpr, ABonBoolExpr)
# * flow through AAttrPropDef
module saf_base

import modelbuilder

# The base of all analysis performed statically.
abstract class StaticAnalysis
	super Visitor

	# Type of FlowSet representation used by the StaticAnalysis.
	type FLOW: FlowSet

	# "in" set for the currently visited node.
	var current_inset: FLOW is noinit, writable

	# 'out' set for the currently visited node.
	var current_outset: FLOW is noinit, writable

	# Sets at the entry of each node.
	#
	# Each node is associated with the `current_inset` it got.
	var insets = new HashMap[ANode, FLOW]

	# Sets at the exit of each node.
	#
	# Each node is associated with the `current_outset` it got.
	var outsets = new HashMap[ANode, FLOW]

	init do
		current_inset = new_initial_flow
		current_outset = new_initial_flow
	end

	# Initial flow set to use.
	#
	# Because the initial flow set depends on the analysis performed, the
	# implementation of this method is the responsability the subclass.
	fun new_initial_flow: FLOW is abstract

	# Initial flow set to use within methods.
	#
	# Returns `new_initial_flow` by default.
	# Redefine this method to inject things in the inset like parameters from
	# the signature.
	fun new_initial_method_flow(v: AMethPropdef): FLOW do return new_initial_flow

	# The merge operation on sets for confluence.
	#
	# Depends on the analysis performed.
	fun merge(s1, s2: FLOW): FLOW is abstract

	# ModelBuilder used to lookup AST nodes.
	var modelbuilder: ModelBuilder

	# Run `self` on an AST `node`.
	fun start_analysis(node: ANode) do visit(node)

	# Pretty print the outsets of this analysis.
	#
	# Mainly used for debug and testing.
	fun pretty_print is abstract
end

# An analysis go forward from the entry point to the exit point.
#
# With a forward analysis, output properties are determined by the inputs.
class ForwardAnalysis
	super StaticAnalysis

	redef fun visit(n) do n.accept_forward_analysis(self)
end

# FlowSet are used to represent data at the entry and exit of a statement.
interface FlowSet
	super Cloneable

	# Merge `self` with another flow set.
	fun flow_union(o: SELF): SELF is abstract
end

# A FlowSet based on a HashMap.
class FlowHashMap[K, V]
	super FlowSet
	super HashMap[K, V]

	# Init `self` with the content of `map`.
	init from(map: Map[K, V]) do
		init
		for k, v in map do self[k] = v
	end

	# Not a deep copy.
	redef fun clone do return new FlowHashMap[K, V].from(self)
end

# A FlowSet based on a HashSet.
class FlowHashSet[E]
	super FlowSet
	super HashSet[E]

	redef fun clone do return new FlowHashSet[E].from(self)

	# Remove all items found in `other` from `self`.
	fun remove_from(other: Collection[E]) do
		for e in other do remove(e)
	end

	redef fun flow_union(o) do return new FlowHashSet[E].from(union(o))
end

redef class ANode

	# Apply the forward analysis `v` to `self`.
	fun accept_forward_analysis(v: ForwardAnalysis) do
		v.current_inset = v.current_outset.clone
		v.current_outset = v.current_inset.clone
		v.insets[self] = v.current_inset
		visit_all(v)
		v.outsets[self] = v.current_outset
	end
end

redef class AIfExpr

	# Merge flow on if .. else constructs.
	redef fun accept_forward_analysis(v) do
		v.enter_visit(n_expr)
		var inset = v.current_inset
		var outset = v.current_outset

		if n_then != null then v.enter_visit(n_then)
		var then_outset = v.current_outset

		v.current_inset = inset
		v.current_outset = outset

		if n_else != null then
			v.enter_visit(n_else)
			outset = v.merge(then_outset, v.current_outset)
		else
			outset = v.merge(then_outset, v.current_inset)
		end
		v.current_inset = inset
		v.current_outset = outset
	end
end

# Represent all kind of `do .. end` blocks.
#
# Used to factorize implementations across do blocks, whiles, fors and loops.
#
# This factorization makes sense since all these contructs can be flow managed
# through contine and breack statements.
#
# TODO move this up in the module hierarchy
interface ADoBlockHelper
	# Lookup fix point for this loop.
	fun loop_fix_point(v: StaticAnalysis, node: ANode) do
		var inset = v.current_inset.clone
		var last: nullable FlowSet = null
		while v.current_outset != last do
			v.enter_visit(node)
			v.current_inset = v.merge(inset, v.current_outset)
			v.current_outset = v.current_inset.clone
			last = v.current_outset.clone
		end
		v.current_inset = inset
		v.current_outset = v.merge(inset, v.current_outset)
	end

	# Factorize loop forward analysis.
	fun accept_loop_forward_analysis(v: StaticAnalysis) do
		var n_block = loop_block
		if not n_block == null then loop_fix_point(v, n_block)
	end

	# The block contained by this loop.
	fun loop_block: nullable ANode is abstract
end

redef class ADoExpr
	super ADoBlockHelper

	redef fun loop_block do return self.n_block
	redef fun accept_forward_analysis(v) do accept_loop_forward_analysis(v)
end

redef class ALoopExpr
	super ADoBlockHelper

	redef fun loop_block do return self.n_block
	redef fun accept_forward_analysis(v) do accept_loop_forward_analysis(v)
end

redef class AWhileExpr
	super ADoBlockHelper

	redef fun loop_block do return self.n_block

	redef fun accept_forward_analysis(v) do
		v.enter_visit(n_expr)
		accept_loop_forward_analysis(v)
	end
end

redef class AForExpr
	super ADoBlockHelper

	redef fun loop_block do return self.n_block

	redef fun accept_forward_analysis(v) do
		for n_group in n_groups do
			v.enter_visit(n_group.n_expr)
		end
		accept_loop_forward_analysis(v)
	end
end

redef class AMethPropdef
	redef fun accept_forward_analysis(v) do
		v.current_inset = v.new_initial_method_flow(self)
		v.current_outset = v.current_inset.clone
		v.insets[self] = v.current_inset
		visit_all(v)
		v.outsets[self] = v.current_outset
	end
end
src/saf/saf_base.nit:15,1--246,3