From 610f147b9f832bebbcf08be847dd5164e05c45d2 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Sat, 20 Feb 2021 01:27:26 +0200 Subject: [PATCH] cxgo: fix regression with builtin macros --- convert.go | 3 +++ libs/builtin.go | 3 +++ libs/config.go | 6 ++++++ libs/registry.go | 10 ++++++++++ libs/stdbool.go | 5 +++++ 5 files changed, 27 insertions(+) diff --git a/convert.go b/convert.go index f1ddfec..027d70f 100644 --- a/convert.go +++ b/convert.go @@ -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} diff --git a/libs/builtin.go b/libs/builtin.go index 9cf65ee..c9dfc68 100644 --- a/libs/builtin.go +++ b/libs/builtin.go @@ -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 { diff --git a/libs/config.go b/libs/config.go index fe5f3d6..d6ed3ad 100644 --- a/libs/config.go +++ b/libs/config.go @@ -16,6 +16,7 @@ func NewEnv(conf types.Config) *Env { "math": "math", "libc": RuntimeLibc, }, + macros: make(map[string]bool), } } @@ -23,6 +24,7 @@ type Env struct { *types.Env libs map[string]*Library imports map[string]string + macros map[string]bool } func (c *Env) Clone() *Env { @@ -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 } diff --git a/libs/registry.go b/libs/registry.go index c203758..2feb56f 100644 --- a/libs/registry.go +++ b/libs/registry.go @@ -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 { @@ -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(` @@ -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 +} diff --git a/libs/stdbool.go b/libs/stdbool.go index 3e897a8..2a8bffe 100644 --- a/libs/stdbool.go +++ b/libs/stdbool.go @@ -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, + }, } }) }