Skip to content

Commit

Permalink
cxgo: fix regression with builtin macros
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Feb 19, 2021
1 parent 34f6b27 commit 610f147
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func (g *translator) newIdent(name string, t types.Type) *types.Ident {
}

func (g *translator) convMacro(name string, fnc func() Expr) Expr {
if g.env.ForceMacro(name) {
return fnc()
}
id, ok := g.macros[name]
if ok {
return IdentExpr{id}
Expand Down
3 changes: 3 additions & 0 deletions libs/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ void* malloc(_cxgo_go_int);
Types: map[string]types.Type{
"__builtin_va_list": valistT,
},
ForceMacros: map[string]bool{
"NULL": true,
},
}
for _, t := range c.Go().Types() {
if nt, ok := t.(types.Named); ok {
Expand Down
6 changes: 6 additions & 0 deletions libs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ func NewEnv(conf types.Config) *Env {
"math": "math",
"libc": RuntimeLibc,
},
macros: make(map[string]bool),
}
}

type Env struct {
*types.Env
libs map[string]*Library
imports map[string]string
macros map[string]bool
}

func (c *Env) Clone() *Env {
Expand All @@ -35,6 +37,10 @@ func (c *Env) Clone() *Env {
for k, v := range c.imports {
c2.imports[k] = v
}
c2.macros = make(map[string]bool)
for k, v := range c.macros {
c2.macros[k] = v
}
return c2
}

Expand Down
10 changes: 10 additions & 0 deletions libs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Library struct {
Idents map[string]*types.Ident

Imports map[string]string

ForceMacros map[string]bool
}

func (l *Library) GetType(name string) types.Type {
Expand Down Expand Up @@ -154,6 +156,9 @@ func (c *Env) GetLibrary(name string) (*Library, bool) {
for k, v := range l.Imports {
c.imports[k] = v
}
for k, v := range l.ForceMacros {
c.macros[k] = v
}

ifdef := "_cxgo_" + strings.ToUpper(defPathReplacer.Replace(name))
l.Header = fmt.Sprintf(`
Expand Down Expand Up @@ -210,3 +215,8 @@ func (c *Env) IdentByName(name string) (*types.Ident, bool) {
_, id, ok := c.LibIdentByName(name)
return id, ok
}

func (c *Env) ForceMacro(name string) bool {
ok := c.macros[name]
return ok
}
5 changes: 5 additions & 0 deletions libs/stdbool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func init() {
#define true 1
#define __bool_true_false_are_defined
`,
ForceMacros: map[string]bool{
"bool": true,
"false": true,
"true": true,
},
}
})
}

0 comments on commit 610f147

Please sign in to comment.