Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for type declarations on pointer types #1733

Merged
merged 13 commits into from
Apr 10, 2024
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/op_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (m *Machine) doOpStar() {
case *PointerType:
pv := xv.V.(PointerValue)
if pv.TV.T == DataByteType {
tv := TypedValue{T: xv.T.(*PointerType).Elt}
tv := TypedValue{T: bt.Elt}
dbv := pv.TV.V.(DataByteValue)
tv.SetUint8(dbv.GetByte())
m.PushValue(tv)
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/gnolang/op_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func (m *Machine) doOpStaticTypeOf() {
m.PushOp(OpStaticTypeOf)
m.Run() // XXX replace
xt := m.ReapValues(start)[0].V.(TypeValue).Type
if pt, ok := xt.(*PointerType); ok {
if pt, ok := baseOf(xt).(*PointerType); ok {
m.PushValue(asValue(&SliceType{
Elt: pt.Elt.Elem(),
}))
Expand All @@ -485,7 +485,7 @@ func (m *Machine) doOpStaticTypeOf() {
m.PushOp(OpStaticTypeOf)
m.Run() // XXX replace
xt := m.ReapValues(start)[0].GetType()
if pt, ok := xt.(*PointerType); ok {
if pt, ok := baseOf(xt).(*PointerType); ok {
m.PushValue(asValue(pt.Elt))
} else if _, ok := xt.(*TypeType); ok {
m.PushValue(asValue(gTypeType))
Expand Down
5 changes: 4 additions & 1 deletion gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2474,7 +2474,7 @@ func checkType(xt Type, dt Type, autoNative bool) {
// Special case if xt or dt is *PointerType to *NativeType,
// convert to *NativeType of pointer kind.
if pxt, ok := xt.(*PointerType); ok {
// *gonative{x} is gonative{*x}
// *gonative{x} is(to) gonative{*x}
//nolint:misspell
if enxt, ok := pxt.Elt.(*NativeType); ok {
xt = &NativeType{
Expand Down Expand Up @@ -2953,6 +2953,7 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De
ft := evalStaticType(store, last, &cd.Type).(*FuncType)
ft = ft.UnboundType(rft)
dt := (*DeclaredType)(nil)

if pt, ok := rt.(*PointerType); ok {
dt = pt.Elem().(*DeclaredType)
} else {
Expand Down Expand Up @@ -3096,6 +3097,8 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) {
t = &MapType{}
case *StructTypeExpr:
t = &StructType{}
case *StarExpr:
t = &PointerType{}
case *NameExpr:
if tv := last.GetValueRef(store, tx.Name); tv != nil {
// (file) block name
Expand Down
28 changes: 28 additions & 0 deletions gnovm/tests/files/type33.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import "fmt"

// Define a base type
type Base int

// Declare a new type that is a pointer to the base type
type PtrToBase *Base

func main() {
var b Base = 42 // Initialize a variable of the base type
var p PtrToBase = &b // Initialize a variable of the new pointer type with the address of b

fmt.Printf("The value of b is: %d\n", b)

// Using the new pointer type
fmt.Printf("The value pointed to by p is: %d\n", *p)

// Modifying the value pointed to by p
*p = 100
fmt.Printf("The new value of b after modification through p is: %d\n", b)
}

// Output:
// The value of b is: 42
// The value pointed to by p is: 42
// The new value of b after modification through p is: 100
12 changes: 12 additions & 0 deletions gnovm/tests/files/type34.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type BytePtr *byte

func main() {
bs := []byte("hello")
var p BytePtr = &bs[0]
println(*p)
}

// Output:
// 104
15 changes: 15 additions & 0 deletions gnovm/tests/files/type35.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

type IntPtr *int

func main() {
var a, b int
a = 1 // Set a to 104
s := []IntPtr{}
s = append(s, &a)
s = append(s, &b)
println(*s[0])
}

// Output:
// 1
17 changes: 17 additions & 0 deletions gnovm/tests/files/type36.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

type Arr [2]int
type Ptr *Arr

func main() {
var arr Arr
arr[0] = 0
arr[1] = 1

p := Ptr(&arr)

println(p[:])
}

// Output:
// slice[(0 int),(1 int)]
20 changes: 20 additions & 0 deletions gnovm/tests/files/type37.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

type IntArray []int
type Arr *IntArray
deelawn marked this conversation as resolved.
Show resolved Hide resolved

func (a Arr) Add(x int) { // receiver is val, not ptr
*a = append(*a, x)
}

func main() {
a := new(IntArray)
Arr(a).Add(4)

fmt.Println(*a)
}

// Output:
// [4]
Loading