github.com/goplus/gox@v1.14.13-0.20240308130321-6ff7f61cfae8/c_test.go (about)

     1  /*
     2   Copyright 2022 The GoPlus Authors (goplus.org)
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6       http://www.apache.org/licenses/LICENSE-2.0
     7   Unless required by applicable law or agreed to in writing, software
     8   distributed under the License is distributed on an "AS IS" BASIS,
     9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10   See the License for the specific language governing permissions and
    11   limitations under the License.
    12  */
    13  
    14  package gox_test
    15  
    16  import (
    17  	"go/token"
    18  	"go/types"
    19  	"testing"
    20  
    21  	"github.com/goplus/gox"
    22  )
    23  
    24  // ----------------------------------------------------------------------------
    25  
    26  func TestBitFields(t *testing.T) {
    27  	pkg := newMainPackage()
    28  	fields := []*types.Var{
    29  		types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false),
    30  		types.NewField(token.NoPos, pkg.Types, "y", types.Typ[types.Uint], false),
    31  	}
    32  	tyT := pkg.NewType("T").InitType(pkg, types.NewStruct(fields, nil))
    33  	pkg.SetVFields(tyT, gox.NewBitFields([]*gox.BitField{
    34  		{Name: "z1", FldName: "x", Off: 0, Bits: 1},
    35  		{Name: "z2", FldName: "x", Off: 1, Bits: 3},
    36  		{Name: "u1", FldName: "y", Off: 0, Bits: 1},
    37  		{Name: "u2", FldName: "y", Off: 1, Bits: 3},
    38  	}))
    39  	cb := pkg.NewFunc(nil, "test", nil, nil, false).BodyStart(pkg).
    40  		NewVar(tyT, "a").
    41  		NewVarStart(types.Typ[types.Int], "z").
    42  		VarVal("a").MemberVal("z1").UnaryOp(token.SUB).
    43  		VarVal("a").MemberVal("z2").
    44  		BinaryOp(token.MUL).EndInit(1).
    45  		NewVarStart(types.Typ[types.Uint], "u").
    46  		VarVal("a").MemberVal("u1").UnaryOp(token.XOR).
    47  		VarVal("a").MemberVal("u2").
    48  		BinaryOp(token.MUL).EndInit(1).
    49  		VarVal("a").MemberRef("z1").Val(1).Assign(1).
    50  		VarVal("a").MemberRef("z2").Val(1).Assign(1).
    51  		End()
    52  	domTest(t, pkg, `package main
    53  
    54  type T struct {
    55  	x int
    56  	y uint
    57  }
    58  
    59  func test() {
    60  	var a T
    61  	var z int = -(a.x << 63 >> 63) * (a.x << 60 >> 61)
    62  	var u uint = ^(a.y & 1) * (a.y >> 1 & 7)
    63  	{
    64  		_autoGo_1 := &a.x
    65  		*_autoGo_1 = *_autoGo_1&^1 | 1&1
    66  	}
    67  	{
    68  		_autoGo_2 := &a.x
    69  		*_autoGo_2 = *_autoGo_2&^14 | 1&7<<1
    70  	}
    71  }
    72  `)
    73  	cb.NewVar(tyT, "a").VarVal("a")
    74  	func() {
    75  		defer func() {
    76  			if e := recover(); e == nil {
    77  				t.Fatal("TestBitFields: no error?")
    78  			}
    79  		}()
    80  		cb.MemberVal("z3")
    81  	}()
    82  	func() {
    83  		defer func() {
    84  			if e := recover(); e == nil {
    85  				t.Fatal("TestBitFields: no error?")
    86  			}
    87  		}()
    88  		cb.MemberRef("z3")
    89  	}()
    90  }
    91  
    92  func TestUnionFields(t *testing.T) {
    93  	pkg := newMainPackage()
    94  	fields := []*types.Var{
    95  		types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false),
    96  		types.NewField(token.NoPos, pkg.Types, "y", types.Typ[types.String], false),
    97  	}
    98  	tyT := pkg.NewType("T").InitType(pkg, types.NewStruct(fields, nil))
    99  	tyFlt := types.Typ[types.Float32]
   100  	pkg.SetVFields(tyT, gox.NewUnionFields([]*gox.UnionField{
   101  		{Name: "z", Type: tyFlt},
   102  		{Name: "val", Type: tyFlt, Off: 4},
   103  	}))
   104  	barFields := []*types.Var{
   105  		types.NewField(token.NoPos, pkg.Types, "a", types.Typ[types.Int], false),
   106  		types.NewField(token.NoPos, pkg.Types, "T", tyT, true),
   107  	}
   108  	tyBar := pkg.NewType("Bar").InitType(pkg, types.NewStruct(barFields, nil))
   109  	cb := pkg.NewFunc(nil, "test", nil, nil, false).BodyStart(pkg).
   110  		NewVar(tyT, "a").NewVar(types.NewPointer(tyT), "b").
   111  		NewVar(tyBar, "bara").NewVar(types.NewPointer(tyBar), "barb").
   112  		NewVarStart(tyFlt, "z").VarVal("a").MemberVal("z").EndInit(1).
   113  		NewVarStart(tyFlt, "val").VarVal("a").MemberVal("val").EndInit(1).
   114  		NewVarStart(tyFlt, "z2").VarVal("b").MemberVal("z").EndInit(1).
   115  		NewVarStart(tyFlt, "val2").VarVal("b").MemberVal("val").EndInit(1).
   116  		NewVarStart(tyFlt, "barz").Val(ctxRef(pkg, "bara")).MemberVal("z").EndInit(1).
   117  		NewVarStart(tyFlt, "barval").Val(ctxRef(pkg, "bara")).MemberVal("val").EndInit(1).
   118  		NewVarStart(tyFlt, "barz2").Val(ctxRef(pkg, "barb")).MemberVal("z").EndInit(1).
   119  		NewVarStart(tyFlt, "barval2").Val(ctxRef(pkg, "barb")).MemberVal("val").EndInit(1).
   120  		VarVal("a").MemberRef("z").Val(1).Assign(1).
   121  		Val(ctxRef(pkg, "barb")).MemberRef("val").Val(1.2).Assign(1).
   122  		End()
   123  	domTest(t, pkg, `package main
   124  
   125  import "unsafe"
   126  
   127  type T struct {
   128  	x int
   129  	y string
   130  }
   131  type Bar struct {
   132  	a int
   133  	T
   134  }
   135  
   136  func test() {
   137  	var a T
   138  	var b *T
   139  	var bara Bar
   140  	var barb *Bar
   141  	var z float32 = *(*float32)(unsafe.Pointer(&a))
   142  	var val float32 = *(*float32)(unsafe.Pointer(uintptr(unsafe.Pointer(&a)) + 4))
   143  	var z2 float32 = *(*float32)(unsafe.Pointer(b))
   144  	var val2 float32 = *(*float32)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + 4))
   145  	var barz float32 = *(*float32)(unsafe.Pointer(&bara.T))
   146  	var barval float32 = *(*float32)(unsafe.Pointer(uintptr(unsafe.Pointer(&bara.T)) + 4))
   147  	var barz2 float32 = *(*float32)(unsafe.Pointer(&barb.T))
   148  	var barval2 float32 = *(*float32)(unsafe.Pointer(uintptr(unsafe.Pointer(&barb.T)) + 4))
   149  	*(*float32)(unsafe.Pointer(&a)) = 1
   150  	*(*float32)(unsafe.Pointer(uintptr(unsafe.Pointer(&barb.T)) + 4)) = 1.2
   151  }
   152  `)
   153  	defer func() {
   154  		if e := recover(); e == nil {
   155  			t.Fatal("TestUnionFields: no error?")
   156  		}
   157  	}()
   158  	cb.NewVar(tyT, "a").VarVal("a").MemberVal("unknown")
   159  }
   160  
   161  // ----------------------------------------------------------------------------
   162  
   163  func TestCFunc(t *testing.T) {
   164  	pkg := newMainPackage()
   165  	cfn := gox.NewCSignature(nil, nil, false)
   166  	pkg.NewFunc(nil, "test", nil, nil, false).BodyStart(pkg).
   167  		NewVar(cfn, "f").
   168  		VarVal("f").Call(0).EndStmt().
   169  		End()
   170  	domTest(t, pkg, `package main
   171  
   172  func test() {
   173  	var f func()
   174  	f()
   175  }
   176  `)
   177  }
   178  
   179  // ----------------------------------------------------------------------------