github.com/gotranspile/cxgo@v0.3.7/types/env_c.go (about)

     1  package types
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  type C struct {
     8  	e   *Env
     9  	pkg *Package
    10  
    11  	charT    Type
    12  	wcharT   Type
    13  	assertF  *Ident
    14  	mallocF  *Ident
    15  	freeF    *Ident
    16  	callocF  *Ident
    17  	memmoveF *Ident
    18  	memcpyF  *Ident
    19  	memsetF  *Ident
    20  	strdupF  *Ident
    21  	strndupF *Ident
    22  }
    23  
    24  // C returns a package containing builtin C types.
    25  func (e *Env) C() *C {
    26  	return &e.c
    27  }
    28  
    29  func (e *Env) initC() {
    30  	e.c.e = e
    31  	e.c.pkg = newPackage("", "")
    32  	e.c.init()
    33  }
    34  
    35  func (c *C) init() {
    36  	g := c.e.Go()
    37  
    38  	c.pkg.NewAlias("bool", "", c.Bool())
    39  	c.charT = c.pkg.NewAlias("char", "", IntT(1)) // signed
    40  	if c.e.conf.WCharSize == 2 && !c.e.conf.WCharSigned {
    41  		c.wcharT = c.pkg.NewTypeGo("wchar_t", "libc.WChar", UintT(2))
    42  	} else if c.e.conf.WCharSigned {
    43  		c.wcharT = c.pkg.NewTypeC("wchar_t", IntT(c.e.conf.WCharSize))
    44  	} else {
    45  		c.wcharT = c.pkg.NewTypeC("wchar_t", UintT(c.e.conf.WCharSize))
    46  	}
    47  
    48  	c.pkg.NewAlias("BOOL", "", c.Bool())
    49  	c.pkg.NewAlias("CHAR", "", c.Char())
    50  	c.pkg.NewAlias("BYTE", "", g.Byte())
    51  	c.pkg.NewAlias("_BYTE", "", g.Byte())
    52  	c.pkg.NewAlias("INT", "", c.Int())
    53  	c.pkg.NewAlias("UINT", "", c.UnsignedInt())
    54  	c.pkg.NewAlias("LONG", "", c.Long())
    55  	c.pkg.NewAlias("ULONG", "", c.UnsignedLong())
    56  	c.pkg.NewAlias("LONGLONG", "", c.LongLong())
    57  	c.pkg.NewAlias("ULONGLONG", "", c.UnsignedLongLong())
    58  	c.pkg.NewAlias("WORD", "", UintT(2))
    59  	c.pkg.NewAlias("_WORD", "", UintT(2))
    60  	c.pkg.NewAlias("DWORD", "", UintT(4))
    61  	c.pkg.NewAlias("_DWORD", "", UintT(4))
    62  	c.pkg.NewAlias("QWORD", "", UintT(8))
    63  	c.pkg.NewAlias("_QWORD", "", UintT(8))
    64  
    65  	unsafePtr := g.UnsafePtr()
    66  	cstring := c.String()
    67  	c.assertF = NewIdentGo("assert", "libc.Assert", c.e.FuncTT(nil, g.Any()))
    68  	c.mallocF = NewIdentGo("__builtin_malloc", "libc.Malloc", c.e.FuncTT(unsafePtr, g.Int()))
    69  	c.freeF = NewIdentGo("free", "libc.Free", c.e.FuncTT(nil, unsafePtr))
    70  	c.callocF = NewIdentGo("calloc", "libc.Calloc", c.e.FuncTT(unsafePtr, g.Int(), g.Int()))
    71  	c.memmoveF = NewIdentGo("__builtin_memmove", "libc.MemMove", c.e.FuncTT(unsafePtr, unsafePtr, unsafePtr, g.Int()))
    72  	c.memcpyF = NewIdentGo("__builtin_memcpy", "libc.MemCpy", c.e.FuncTT(unsafePtr, unsafePtr, unsafePtr, g.Int()))
    73  	c.memsetF = NewIdentGo("__builtin_memset", "libc.MemSet", c.e.FuncTT(unsafePtr, unsafePtr, g.Byte(), g.Int()))
    74  	c.strdupF = NewIdentGo("__builtin_strdup", "libc.StrDup", c.e.FuncTT(cstring, cstring))
    75  	c.strndupF = NewIdentGo("__builtin_strndup", "libc.StrNDup", c.e.FuncTT(cstring, cstring, g.Int()))
    76  }
    77  
    78  func (c *C) WCharSize() int {
    79  	return c.e.conf.WCharSize
    80  }
    81  
    82  func (c *C) WIntSize() int {
    83  	return c.WCharSize() * 2
    84  }
    85  
    86  func (c *C) WCharSigned() bool {
    87  	return c.e.conf.WCharSigned
    88  }
    89  
    90  // Type finds a C builtin type by name.
    91  func (c *C) Type(name string) Type {
    92  	if t := c.pkg.CType(name); t != nil {
    93  		return nil
    94  	}
    95  	// check if the element type of LP* type has an override as well
    96  	if strings.HasPrefix(name, "LP") {
    97  		if elem := c.pkg.CType(name[2:]); elem != nil {
    98  			return c.e.PtrT(elem)
    99  		}
   100  	}
   101  	// same for types with a _PTR suffix
   102  	if strings.HasSuffix(name, "_PTR") {
   103  		if elem := c.pkg.CType(name[:len(name)-4]); elem != nil {
   104  			return c.e.PtrT(elem)
   105  		}
   106  	}
   107  	return nil
   108  }
   109  
   110  // Named finds a named C builtin type.
   111  func (c *C) NamedType(name string) Named {
   112  	// to run the same type resolution
   113  	nt, _ := c.Type(name).(Named)
   114  	return nt
   115  }
   116  
   117  // Bool returns C bool type.
   118  func (c *C) Bool() Type {
   119  	// TODO: support custom bool types
   120  	return c.e.Go().Bool()
   121  }
   122  
   123  // Char returns C char type.
   124  func (c *C) Char() Type {
   125  	return c.charT
   126  }
   127  
   128  // WChar returns C wchar_t type.
   129  func (c *C) WChar() Type {
   130  	return c.wcharT
   131  }
   132  
   133  // SignedChar returns C signed char type.
   134  func (c *C) SignedChar() Type {
   135  	return IntT(1)
   136  }
   137  
   138  // UnsignedChar returns C unsigned char type.
   139  func (c *C) UnsignedChar() Type {
   140  	return UintT(1)
   141  }
   142  
   143  // Short returns C short type.
   144  func (c *C) Short() Type {
   145  	return IntT(2)
   146  }
   147  
   148  // UnsignedShort returns C unsigned short type.
   149  func (c *C) UnsignedShort() Type {
   150  	return UintT(2)
   151  }
   152  
   153  // Int returns C int type.
   154  func (c *C) Int() Type {
   155  	if c.e.conf.UseGoInt {
   156  		return c.e.Go().Int()
   157  	}
   158  	return c.e.DefIntT()
   159  }
   160  
   161  // UnsignedInt returns C unsigned int type.
   162  func (c *C) UnsignedInt() Type {
   163  	if c.e.conf.UseGoInt {
   164  		return c.e.Go().Uint()
   165  	}
   166  	return c.e.DefUintT()
   167  }
   168  
   169  // Long returns C long type.
   170  func (c *C) Long() Type {
   171  	return c.Int()
   172  }
   173  
   174  // UnsignedLong returns C unsigned long type.
   175  func (c *C) UnsignedLong() Type {
   176  	return c.UnsignedInt()
   177  }
   178  
   179  // LongLong returns C long long type.
   180  func (c *C) LongLong() Type {
   181  	return IntT(8)
   182  }
   183  
   184  // UnsignedLongLong returns C unsigned long long type.
   185  func (c *C) UnsignedLongLong() Type {
   186  	return UintT(8)
   187  }
   188  
   189  // Float returns C float type.
   190  func (c *C) Float() Type {
   191  	return FloatT(4)
   192  }
   193  
   194  // Double returns C double type.
   195  func (c *C) Double() Type {
   196  	return FloatT(8)
   197  }
   198  
   199  // String returns C char* type.
   200  func (c *C) String() PtrType {
   201  	// we represent it differently
   202  	return c.e.PtrT(c.e.Go().Byte())
   203  }
   204  
   205  // WString returns C wchar_t* type.
   206  func (c *C) WString() PtrType {
   207  	return c.e.PtrT(c.WChar())
   208  }
   209  
   210  // BytesN returns C char[N] type.
   211  func (c *C) BytesN(n int) Type {
   212  	return ArrayT(c.e.Go().Byte(), n)
   213  }
   214  
   215  // AssertFunc returns C assert function ident.
   216  func (c *C) AssertFunc() *Ident {
   217  	return c.assertF
   218  }
   219  
   220  // MallocFunc returns C malloc function ident.
   221  func (c *C) MallocFunc() *Ident {
   222  	return c.mallocF
   223  }
   224  
   225  // FreeFunc returns C free function ident.
   226  func (c *C) FreeFunc() *Ident {
   227  	return c.freeF
   228  }
   229  
   230  // CallocFunc returns C calloc function ident.
   231  func (c *C) CallocFunc() *Ident {
   232  	return c.callocF
   233  }
   234  
   235  // MemmoveFunc returns C memmove function ident.
   236  func (c *C) MemmoveFunc() *Ident {
   237  	return c.memmoveF
   238  }
   239  
   240  // MemcpyFunc returns C memcpy function ident.
   241  func (c *C) MemcpyFunc() *Ident {
   242  	return c.memcpyF
   243  }
   244  
   245  // MemsetFunc returns C memset function ident.
   246  func (c *C) MemsetFunc() *Ident {
   247  	return c.memsetF
   248  }
   249  
   250  // StrdupFunc returns C strdup function ident.
   251  func (c *C) StrdupFunc() *Ident {
   252  	return c.strdupF
   253  }
   254  
   255  // StrndupFunc returns C strndup function ident.
   256  func (c *C) StrndupFunc() *Ident {
   257  	return c.strndupF
   258  }