github.com/gotranspile/cxgo@v0.3.7/libs/stddef.go (about) 1 package libs 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/gotranspile/cxgo/types" 8 ) 9 10 // TODO: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stddef.h.html 11 12 const ( 13 stddefH = "stddef.h" 14 ) 15 16 func init() { 17 RegisterLibrary(stddefH, func(c *Env) *Library { 18 return &Library{ 19 Header: incStdDef(c.Env), 20 Types: typesStdDef(c.Env), 21 } 22 }) 23 } 24 25 func incStdDef(e *types.Env) string { 26 var buf strings.Builder 27 buf.WriteString(` 28 #include <` + stdintH + `> 29 #define NULL 0 30 typedef intptr_t ptrdiff_t; 31 typedef uintptr_t size_t; 32 `) 33 sz := e.C().WCharSize() 34 signed := e.C().WCharSigned() 35 _, _ = fmt.Fprintf(&buf, "typedef %s wchar_t;\n", 36 buildinFixedIntName(sz*8, !signed), 37 ) 38 return buf.String() 39 } 40 41 func typesStdDef(e *types.Env) map[string]types.Type { 42 return map[string]types.Type{ 43 "wchar_t": e.C().WChar(), 44 "size_t": e.UintPtrT(), 45 "ptrdiff_t": e.IntPtrT(), 46 } 47 }