Commit bb119a54 authored by Hugo Moreau's avatar Hugo Moreau

Global: treat global before local

In order to avoid conflict with global variables defined after assigning
to it, LocalVariableAssignments has been corrected.

 * main.go,
   transform/localvariableassignments.go : Here.
parent c860d1f7
Pipeline #26627 passed with stage
in 2 minutes and 41 seconds
......@@ -100,7 +100,7 @@ func compileTo(filepath string, src []byte, file *os.File,
meta.Create("LineCounter", g.Name)
}
localvariable := &transform.LocalVariableAssignments{}
localvariable := &transform.LocalVariableAssignments{Global: true}
res := transform.ApplyAll(f, meta, []transform.Transform{
// &transform.Debug{}, // only for debuging a special transformation
&transform.Alive{},
......@@ -113,13 +113,15 @@ func compileTo(filepath string, src []byte, file *os.File,
&transform.ConditionnalCall{},
&transform.ArithmeticCall{},
&transform.InnerCall{},
&transform.Debug{},
&transform.RegularizedAffect{},
&transform.FunctionDefs{},
&transform.RoutineAssignments{
Functions: goroutines,
},
localvariable,
})
res = transform.ApplyAll(res, meta, []transform.Transform{
&transform.LocalVariableAssignments{Global: false, GlobalToMain: localvariable.GlobalToMain},
&transform.AfterChannel{},
&cfg.Transform{},
&transform.RoutineCounter{
......
......@@ -55,8 +55,9 @@ G2PState[7] = 0
// LocalVariableAssignments visitor type, stateless.
type LocalVariableAssignments struct {
currentFunction *ast.Object
globalToMain []ast.Stmt
GlobalToMain []ast.Stmt
GlobalBlackbox map[string]*ast.Object
Global bool
}
// Pre traversal applies the transformation.
......@@ -73,41 +74,43 @@ func (t *LocalVariableAssignments) Pre(meta *Meta, v *Visitor) bool {
t.currentFunction = nil
}
case *ast.ValueSpec:
if t.currentFunction == nil {
if t.currentFunction == nil && t.Global {
for i := 0; i < len(node.Names); i++ {
obj := node.Names[i].Obj
meta.Add(obj, v.Name())
t.GlobalBlackbox[obj.Name] = obj
if node.Values != nil {
t.globalToMain = append(t.globalToMain, meta.Set(obj, node.Values[i]))
t.GlobalToMain = append(t.GlobalToMain, meta.Set(obj, node.Values[i]))
}
}
}
case *ast.AssignStmt:
newLHS := make([]ast.Expr, 0)
for i := 0; i < len(node.Lhs); i++ {
switch lhn := node.Lhs[i].(type) {
case *ast.Ident:
obj := lhn.Obj
id := meta.GetOrAdd(obj, v.Name(), t.currentFunction)
newLHS = append(newLHS, id)
default:
newLHS = append(newLHS, lhn)
if !t.Global {
newLHS := make([]ast.Expr, 0)
for i := 0; i < len(node.Lhs); i++ {
switch lhn := node.Lhs[i].(type) {
case *ast.Ident:
obj := lhn.Obj
id := meta.GetOrAdd(obj, v.Name(), t.currentFunction)
newLHS = append(newLHS, id)
default:
newLHS = append(newLHS, lhn)
}
}
tok := node.Tok
if tok == token.DEFINE {
tok = token.ASSIGN
}
c.Replace(&ast.AssignStmt{
Lhs: newLHS,
// Replace declarations by assignments
Tok: tok,
Rhs: node.Rhs,
})
return true
}
tok := node.Tok
if tok == token.DEFINE {
tok = token.ASSIGN
}
c.Replace(&ast.AssignStmt{
Lhs: newLHS,
// Replace declarations by assignments
Tok: tok,
Rhs: node.Rhs,
})
return true
case *ast.Ident:
if t.currentFunction != nil {
if t.currentFunction != nil && !t.Global {
obj := node.Obj
if !meta.Has(obj) {
......@@ -122,17 +125,19 @@ func (t *LocalVariableAssignments) Pre(meta *Meta, v *Visitor) bool {
// Post traversal sets global values and remove statements.
func (t *LocalVariableAssignments) Post(meta *Meta, v *Visitor) bool {
c := v.Cursor()
switch node := c.Node().(type) {
case *ast.FuncDecl:
if node.Name.Name == "G2PFunction_main" {
node.Body.List = append(t.globalToMain, node.Body.List...)
}
case *ast.GenDecl:
if node.Tok == token.VAR {
switch c.Parent().(type) {
case *ast.File:
c.Delete()
if !t.Global {
c := v.Cursor()
switch node := c.Node().(type) {
case *ast.FuncDecl:
if node.Name.Name == "G2PFunction_main" {
node.Body.List = append(t.GlobalToMain, node.Body.List...)
}
case *ast.GenDecl:
if node.Tok == token.VAR {
switch c.Parent().(type) {
case *ast.File:
c.Delete()
}
}
}
}
......
Markdown is supported
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