Commit 36c0b179 authored by Hugo Moreau's avatar Hugo Moreau
Browse files

Dependencies: Documented code

For a better understanding all functions and structs have been
documented.

 * transform/dependencies.go : Modified.
parent efa6106d
Pipeline #28072 failed with stage
in 38 seconds
......@@ -10,30 +10,37 @@ import (
"os"
)
// Variable details according to a function scope.
type VarDetails struct {
UsedStmt []int
VarDependencies []*ast.Object
UsedStmt []int // Used Statements.
VarDependencies []*ast.Object // Variables dependencies.
}
// Function details.
type FuncDetails struct {
BodyStmt []ast.Stmt
ToIgnoreStmt []int
Parameters []*ast.Field
Variables map[*ast.Object]*VarDetails
BodyStmt []ast.Stmt // Functions Statements list.
ToIgnoreStmt []int // To be ignored statements.
Parameters []*ast.Field // Functions parameters.
Variables map[*ast.Object]*VarDetails // Variable Details map.
Visited bool
}
// Function informations, only used in the Pre traversal.
// This struct is only used in order to collect informations,
// once a statement is visited we store its informations in FuncDetails struct.
type FuncInfos struct {
Body *ast.BlockStmt
Obj *ast.Object
Index int
Variables []*ast.Object
Body *ast.BlockStmt // Function BlockStmt.
Obj *ast.Object // Function Object.
Index int // Current Statement Index.
Variables []*ast.Object // All used variables in current statement.
}
// Dependencies struct.
// It hold all our informations in the Pre and Post traversal.
type Dependencies struct {
currentFunc FuncInfos
FuncsVar map[*ast.Object]*FuncDetails
GlobalVar []*ast.Object
currentFunc FuncInfos // Current Function
FuncsVar map[*ast.Object]*FuncDetails // All functions informations.
GlobalVar []*ast.Object // Global variables list.
}
func findObj(objList []*ast.Object, obj *ast.Object) bool {
......@@ -74,6 +81,8 @@ func (infos *FuncInfos) getIndex() int {
return infos.Index - 1
}
// Convert FuncInfos into FuncDetails once a statement has been visited.
// Variables dependencies are generated if they are in the same statement.
func (infos *FuncInfos) convert(funcsVar map[*ast.Object]*FuncDetails) {
if len(infos.Variables) == 0 || infos.Index <= 0 || infos.Obj == nil {
return
......@@ -97,6 +106,9 @@ func (infos *FuncInfos) convert(funcsVar map[*ast.Object]*FuncDetails) {
}
}
// Register all our variables in funcsVar using convert function,
// and increment our index to go to next statement.
// If the given index is incorect, it return false, true otherwise.
func (infos *FuncInfos) registerVariables(funcsVar map[*ast.Object]*FuncDetails, node ast.Node) bool {
if infos.Index < len(infos.Body.List) &&
node == infos.Body.List[infos.Index] {
......@@ -108,6 +120,7 @@ func (infos *FuncInfos) registerVariables(funcsVar map[*ast.Object]*FuncDetails,
return false
}
// Init all Func Informations as it is the beginning of the function.
func (infos *FuncInfos) init(funcDecl *ast.FuncDecl) {
infos.Obj = funcDecl.Name.Obj
infos.Body = funcDecl.Body
......@@ -214,7 +227,7 @@ func (t *Dependencies) Print() {
}
}
// Output a graphviz graph.
// Output a GraphViz graph.
func (t *Dependencies) PrintGraph() {
indent := &Indent{}
fmt.Println("graph G {")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment