Skip to content

Commit

Permalink
CallExpr: use util.NewCallExpr (#244)
Browse files Browse the repository at this point in the history
* CallExpr: use util.NewCallExpr

* fix test

* fix code style
  • Loading branch information
Konstantin8105 committed Nov 12, 2018
1 parent 2cd005b commit 7ac939d
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 91 deletions.
2 changes: 2 additions & 0 deletions ast/ast.go
Expand Up @@ -247,6 +247,8 @@ func Parse(fullline string) (returnNode Node, err error) {
return parseReturnsTwiceAttr(line), nil
case "SentinelAttr":
return parseSentinelAttr(line), nil
case "StaticAssertDecl":
return parseStaticAssertDecl(line), nil
case "StmtExpr":
return parseStmtExpr(line), nil
case "StringLiteral":
Expand Down
2 changes: 2 additions & 0 deletions ast/position.go
Expand Up @@ -390,6 +390,8 @@ func setPosition(node Node, position Position) {
n.Pos = position
case *StmtExpr:
n.Pos = position
case *StaticAssertDecl:
n.Pos = position
case *StringLiteral:
n.Pos = position
case *SwitchStmt:
Expand Down
50 changes: 50 additions & 0 deletions ast/static_assert_decl.go
@@ -0,0 +1,50 @@
package ast

import "strings"

// StaticAssertDecl
type StaticAssertDecl struct {
Addr Address
Pos Position
Position2 string
ChildNodes []Node
}

func parseStaticAssertDecl(line string) *StaticAssertDecl {
groups := groupsFromRegex(
`<(?P<position>.*)>
(?P<position2> col:\d+| line:\d+:\d+)?
`,
line,
)

return &StaticAssertDecl{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
Position2: strings.TrimSpace(groups["position2"]),
ChildNodes: []Node{},
}
}

// AddChild adds a new child node. Child nodes can then be accessed with the
// Children attribute.
func (n *StaticAssertDecl) AddChild(node Node) {
n.ChildNodes = append(n.ChildNodes, node)
}

// Address returns the numeric address of the node. See the documentation for
// the Address type for more information.
func (n *StaticAssertDecl) Address() Address {
return n.Addr
}

// Children returns the child nodes. If this node does not have any children or
// this node does not support children it will always return an empty slice.
func (n *StaticAssertDecl) Children() []Node {
return n.ChildNodes
}

// Position returns the position in the original source code.
func (n *StaticAssertDecl) Position() Position {
return n.Pos
}
18 changes: 18 additions & 0 deletions ast/static_assert_decl_test.go
@@ -0,0 +1,18 @@
package ast

import (
"testing"
)

func TestStaticAssertDecl(t *testing.T) {
nodes := map[string]Node{
`0x3526a70 <line:3108:1, col:80> col:1`: &StaticAssertDecl{
Addr: 0x3526a70,
Pos: NewPositionFromString("line:3108:1, col:80"),
Position2: "col:1",
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
}
27 changes: 7 additions & 20 deletions transpiler/call.go
Expand Up @@ -179,19 +179,11 @@ func simplificationCallExprPrintf(call *ast.CallExpr, p *program.Program) (
// }
p.AddImport("fmt")
printfText = strconv.Quote(printfText)
return &goast.CallExpr{
Fun: &goast.SelectorExpr{
X: goast.NewIdent("fmt"),
Sel: goast.NewIdent("Printf"),
},
Lparen: 1,
Args: []goast.Expr{
&goast.BasicLit{
Kind: token.STRING,
Value: printfText,
},
},
}, true
return util.NewCallExpr("fmt"+"."+"Printf",
&goast.BasicLit{
Kind: token.STRING,
Value: printfText,
}), true
}

// transpileCallExpr transpiles expressions that calls a function, for example:
Expand Down Expand Up @@ -629,13 +621,8 @@ func transpileCallExprCalloc(n *ast.CallExpr, p *program.Program) (
}
}

return &goast.CallExpr{
Fun: util.NewIdent("make"),
Args: []goast.Expr{
goType,
size,
},
}, n.Type, preStmts, postStmts, nil
return util.NewCallExpr("make", goType, size),
n.Type, preStmts, postStmts, nil
}

func transpileCallExprQsort(n *ast.CallExpr, p *program.Program) (
Expand Down
13 changes: 3 additions & 10 deletions transpiler/cast.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Konstantin8105/c4go/ast"
"github.com/Konstantin8105/c4go/program"
"github.com/Konstantin8105/c4go/types"
"github.com/Konstantin8105/c4go/util"
)

func transpileImplicitCastExpr(n *ast.ImplicitCastExpr, p *program.Program, exprIsStmt bool) (
Expand Down Expand Up @@ -51,11 +52,7 @@ func transpileImplicitCastExpr(n *ast.ImplicitCastExpr, p *program.Program, expr
if len(n.Type) != 0 && len(n.Type2) != 0 && n.Type != n.Type2 && cast {
var tt string
tt, err = types.ResolveType(p, n.Type)
expr = &goast.CallExpr{
Fun: goast.NewIdent(tt),
Lparen: 1,
Args: []goast.Expr{expr},
}
expr = util.NewCallExpr(tt, expr)
exprType = n.Type
return
}
Expand Down Expand Up @@ -162,11 +159,7 @@ func transpileCStyleCastExpr(n *ast.CStyleCastExpr, p *program.Program, exprIsSt
if len(n.Type) != 0 && len(n.Type2) != 0 && n.Type != n.Type2 {
var tt string
tt, err = types.ResolveType(p, n.Type)
expr = &goast.CallExpr{
Fun: goast.NewIdent(tt),
Lparen: 1,
Args: []goast.Expr{expr},
}
expr = util.NewCallExpr(tt, expr)
exprType = n.Type
return
}
Expand Down
7 changes: 1 addition & 6 deletions transpiler/functions.go
Expand Up @@ -124,12 +124,7 @@ func transpileFunctionDecl(n *ast.FunctionDecl, p *program.Program) (

if p.IncludeHeaderIsExists("stdlib.h") && n.Name == "main" {
body.List = append([]goast.Stmt{&goast.DeferStmt{
Call: &goast.CallExpr{
Fun: &goast.SelectorExpr{
X: goast.NewIdent("noarch"),
Sel: goast.NewIdent("AtexitRun"),
},
},
Call: util.NewCallExpr("noarch.AtexitRun"),
}}, body.List...)
p.AddImport("github.com/Konstantin8105/c4go/noarch")
}
Expand Down
23 changes: 8 additions & 15 deletions transpiler/offset.go
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Konstantin8105/c4go/ast"
"github.com/Konstantin8105/c4go/program"
"github.com/Konstantin8105/c4go/util"
)

func transpileOffsetOfExpr(n *ast.OffsetOfExpr, p *program.Program) (
Expand Down Expand Up @@ -60,22 +61,14 @@ func transpileOffsetOfExpr(n *ast.OffsetOfExpr, p *program.Program) (
}

p.AddImport("unsafe")
expr = &goast.CallExpr{
Fun: &goast.SelectorExpr{
X: goast.NewIdent("unsafe"),
Sel: goast.NewIdent("Offsetof"),
},
Lparen: 1,
Args: []goast.Expr{
&goast.SelectorExpr{
X: &goast.CompositeLit{
Type: goast.NewIdent(string(arguments[0])),
Lbrace: 1,
},
Sel: goast.NewIdent(string(arguments[1])),
expr = util.NewCallExpr("unsafe.Offsetof",
&goast.SelectorExpr{
X: &goast.CompositeLit{
Type: goast.NewIdent(string(arguments[0])),
Lbrace: 1,
},
},
}
Sel: goast.NewIdent(string(arguments[1])),
})

exprType = n.Type
return
Expand Down
6 changes: 1 addition & 5 deletions transpiler/unary.go
Expand Up @@ -203,11 +203,7 @@ func transpileUnaryOperatorNot(n *ast.UnaryOperator, p *program.Program) (
if p.IncludeHeaderIsExists("stdbool.h") {
if t == "_Bool" {
t = "int"
e = &goast.CallExpr{
Fun: goast.NewIdent("int"),
Lparen: 1,
Args: []goast.Expr{e},
}
e = util.NewCallExpr("int", e)
}
}

Expand Down
9 changes: 3 additions & 6 deletions transpiler/variables.go
Expand Up @@ -502,13 +502,10 @@ func transpileImplicitValueInitExpr(n *ast.ImplicitValueInitExpr, p *program.Pro
return
}

expr = &goast.CallExpr{
Fun: goast.NewIdent(t),
Lparen: 1,
Args: []goast.Expr{&goast.BasicLit{
expr = util.NewCallExpr(t,
&goast.BasicLit{
Kind: token.INT,
Value: "0",
}},
}
})
return
}
40 changes: 12 additions & 28 deletions types/cast.go
Expand Up @@ -192,20 +192,12 @@ func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (
return expr, err
}

return &goast.CallExpr{
Fun: &goast.Ident{
Name: toType,
},
Lparen: 1,
Args: []goast.Expr{
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
},
},
Rparen: 2,
}, nil
return util.NewCallExpr(toType,
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
}), nil
}
e, err := CastExpr(p, expr, fromType, v)
if err != nil {
Expand All @@ -218,20 +210,12 @@ func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (
if err != nil {
return expr, err
}
expr = &goast.CallExpr{
Fun: &goast.Ident{
Name: t,
},
Lparen: 1,
Args: []goast.Expr{
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
},
},
Rparen: 2,
}
expr = util.NewCallExpr(t,
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
})
if toType == v {
return expr, nil
}
Expand Down
5 changes: 4 additions & 1 deletion util/goast.go
Expand Up @@ -116,7 +116,10 @@ func internalTypeToExpr(goType string) goast.Expr {
// function name is deemed to be not valid.
func NewCallExpr(functionName string, args ...goast.Expr) *goast.CallExpr {
for i := range args {
PanicIfNil(args[i], "Argument of function is cannot be nil")
// Argument of function is cannot be nil
if args[i] == nil {
args[i] = goast.NewIdent("C4GO_NOT_TRANSPILED_EXPRESSION")
}
}
index := strings.Index(functionName, ".")
if index > 0 {
Expand Down

0 comments on commit 7ac939d

Please sign in to comment.