From 5400ce7106abca99242514dd4af696cf7c3836d1 Mon Sep 17 00:00:00 2001 From: Martin Gysel Date: Thu, 17 Nov 2022 21:48:20 +0100 Subject: [PATCH] adjust type_mapping if processor's data model uses 64bit long integers fortunately modernc.org/cc already has this information. An uptate to v4.1.0 is needed as otherwise ABI.Types is not public. Issue: xlab/c-for-go#138 Signed-off-by: Martin Gysel --- go.mod | 6 +++--- go.sum | 19 ++++++++----------- process.go | 2 ++ translator/translator.go | 5 +++-- translator/type_mapping.go | 9 ++++++++- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 2ade10a..c7e42d7 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,8 @@ require ( github.com/xlab/pkgconfig v0.0.0-20170226114623-cea12a0fd245 golang.org/x/tools v0.1.12 gopkg.in/yaml.v2 v2.4.0 - modernc.org/cc/v4 v4.0.0-20220822131148-c47a4b1f7491 - modernc.org/token v1.0.0 + modernc.org/cc/v4 v4.1.0 + modernc.org/token v1.0.1 ) require ( @@ -21,5 +21,5 @@ require ( gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect modernc.org/mathutil v1.5.0 // indirect modernc.org/opt v0.1.3 // indirect - modernc.org/strutil v1.1.2 // indirect + modernc.org/strutil v1.1.3 // indirect ) diff --git a/go.sum b/go.sum index 6cc4d6c..e1b12f8 100644 --- a/go.sum +++ b/go.sum @@ -48,18 +48,15 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -modernc.org/cc/v4 v4.0.0-20220822131148-c47a4b1f7491 h1:JAyEPvNflhGJq9KF+p9OBkEQT3Ajp74VimOcYmbTHkw= -modernc.org/cc/v4 v4.0.0-20220822131148-c47a4b1f7491/go.mod h1:woK7kLDJB0KdcyuHwwhosSTE2XnbxZraVQNm8PvK28w= -modernc.org/ccorpus2 v0.0.15 h1:pFmjMvxYmv8yA0AZVH8/3VfDD9kn4SLGJcHcv6chKaE= -modernc.org/ccorpus2 v0.0.15/go.mod h1:Wifvo4Q/qS/h1aRoC2TffcHsnxwTikmi1AuLANuucJQ= -modernc.org/mathutil v1.2.2/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= -modernc.org/mathutil v1.4.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/cc/v4 v4.1.0 h1:PlApAKux1sNvreOGs1Hr04FFz35QmAWoa98YFjcdH94= +modernc.org/cc/v4 v4.1.0/go.mod h1:T6KFXc8WI0m9k6IOHuRe9+vB+Pb/AaV8BMZoVqHLm1I= +modernc.org/ccorpus2 v1.1.0 h1:r/Z2+wOD5Tmcs1AMVXJgslE9HgRRROVWo0qUox1kJIo= +modernc.org/ccorpus2 v1.1.0/go.mod h1:Wifvo4Q/qS/h1aRoC2TffcHsnxwTikmi1AuLANuucJQ= modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= -modernc.org/strutil v1.1.1/go.mod h1:DE+MQQ/hjKBZS2zNInV5hhcipt5rLPWkmpbGeW5mmdw= -modernc.org/strutil v1.1.2 h1:iFBDH6j1Z0bN/Q9udJnnFoFpENA4252qe/7/5woE5MI= -modernc.org/strutil v1.1.2/go.mod h1:OYajnUAcI/MX+XD/Wx7v1bbdvcQSvxgtb0gC+u3d3eg= -modernc.org/token v1.0.0 h1:a0jaWiNMDhDUtqOj09wvjWWAqd3q7WpBulmL9H2egsk= -modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= +modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY= +modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw= +modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg= +modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/process.go b/process.go index 459312e..219b611 100644 --- a/process.go +++ b/process.go @@ -17,6 +17,7 @@ import ( "github.com/xlab/pkgconfig/pkg" "golang.org/x/tools/imports" "gopkg.in/yaml.v2" + "modernc.org/cc/v4" ) type Buf int @@ -86,6 +87,7 @@ func NewProcess(configPath, outputPath string) (*Process, error) { cfg.Translator = &translator.Config{} } cfg.Translator.IgnoredFiles = cfg.Parser.IgnoredPaths + cfg.Translator.LongIs64Bit = unit.ABI.Types[cc.Long].Size == 8 // learn the model tl, err := translator.New(cfg.Translator) if err != nil { diff --git a/translator/translator.go b/translator/translator.go index a794f75..c13bf14 100644 --- a/translator/translator.go +++ b/translator/translator.go @@ -88,6 +88,7 @@ type Config struct { ConstUCharIsString *bool `yaml:"ConstUCharIsString"` IgnoredFiles []string `yaml:"-"` + LongIs64Bit bool `yaml:"-"` } func New(cfg *Config) (*Translator, error) { @@ -113,8 +114,8 @@ func New(cfg *Config) (*Translator, error) { rules: cfg.Rules, constRules: cfg.ConstRules, typemap: cfg.Typemap, - builtinTypemap: getCTypeMap(constCharAsString, constUCharAsString), - builtinTypemap2: getCTypeMap(true, false), + builtinTypemap: getCTypeMap(constCharAsString, constUCharAsString, cfg.LongIs64Bit), + builtinTypemap2: getCTypeMap(true, false, cfg.LongIs64Bit), compiledRxs: make(map[RuleAction]RxMap), compiledPtrTipRxs: make(PtrTipRxMap), compiledTypeTipRxs: make(TypeTipRxMap), diff --git a/translator/type_mapping.go b/translator/type_mapping.go index bb2dd2a..36f5563 100644 --- a/translator/type_mapping.go +++ b/translator/type_mapping.go @@ -30,7 +30,7 @@ var ( InterfaceSliceSpec = GoTypeSpec{Base: "[]interface{}"} ) -func getCTypeMap(constCharIsString, constUCharIsString bool) CTypeMap { +func getCTypeMap(constCharIsString, constUCharIsString, longIs64Bit bool) CTypeMap { config := make(CTypeMap, len(builtinCTypeMap)+2) for k, v := range builtinCTypeMap { config[k] = v @@ -46,6 +46,13 @@ func getCTypeMap(constCharIsString, constUCharIsString bool) CTypeMap { config[CTypeSpec{Base: "char", Const: true, Unsigned: true, Pointers: 1}] = UStringSpec } + if longIs64Bit { + config[CTypeSpec{Base: "long", Unsigned: true}] = Uint64Spec + config[CTypeSpec{Base: "long", Signed: true}] = Int64Spec + config[CTypeSpec{Base: "int", Long: true, Unsigned: true}] = Uint64Spec + config[CTypeSpec{Base: "int", Long: true}] = Int64Spec + } + return config }