Skip to content

Commit

Permalink
experiment: support eliding for struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Nov 22, 2023
1 parent 8e2386a commit d570f7c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
24 changes: 24 additions & 0 deletions examples/gno.land/p/demo/aaa/aaa.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Experiment with eliding

package aaa

type Foo struct {
a string
b int
}

type Bar struct {
c string
d Foo
}

func Experiment() {
// a := [3]T{0: "a", 1: "b", 2: "c"}
// var a []Foo = []Foo{{"A", 1}, {"B", 2}}
var b []Bar = []Bar{
{"A", {"A", 1}},
{"B", {"B", 2}},
}
// _ = a
_ = b
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/aaa/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/aaa
41 changes: 34 additions & 7 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -3245,6 +3245,10 @@ func fileNameOf(n BlockNode) Name {
}

func elideCompositeElements(clx *CompositeLitExpr, clt Type) {
oldBaseOfClt := baseOf(clt)
oldClx := *clx
oldClt := clt

switch clt := baseOf(clt).(type) {
/*
case *PointerType:
Expand All @@ -3262,20 +3266,23 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) {
}
*/
case *ArrayType:
fmt.Println("ArrayType:")
et := clt.Elt
el := len(clx.Elts)
for i := 0; i < el; i++ {
kvx := &clx.Elts[i]
elideCompositeExpr(&kvx.Value, et)
}
case *SliceType:
fmt.Println("SliceType:")
et := clt.Elt
el := len(clx.Elts)
for i := 0; i < el; i++ {
kvx := &clx.Elts[i]
elideCompositeExpr(&kvx.Value, et)
}
case *MapType:
fmt.Println("MapType:")
kt := clt.Key
vt := clt.Value
el := len(clx.Elts)
Expand All @@ -3285,19 +3292,19 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) {
elideCompositeExpr(&kvx.Value, vt)
}
case *StructType:
fmt.Println("StructType:")
// Struct fields cannot be elided in Go for
// legibility, but Gno could support them (e.g. for
// certain tagged struct fields).
// TODO: support eliding.
for _, kvx := range clx.Elts {
vx := kvx.Value
if vclx, ok := vx.(*CompositeLitExpr); ok {
if vclx.Type == nil {
panic("types cannot be elided in composite literals for struct types")
}
}
el := len(clx.Elts)
for i := 0; i < el; i++ {
kvx := &clx.Elts[i]
ft := clt.Fields[i].Type
elideCompositeExpr(&kvx.Value, ft)
}
case *NativeType:
fmt.Println("NativeType:")
// TODO: support eliding.
for _, kvx := range clx.Elts {
vx := kvx.Value
Expand All @@ -3312,6 +3319,26 @@ func elideCompositeElements(clx *CompositeLitExpr, clt Type) {
"unexpected composite lit type %s",
clt.String()))
}

fmt.Println("\n\n <xxx> \n")

fmt.Println("oldBaseOfClt:")
fmt.Println("-", oldBaseOfClt)
fmt.Println("+", baseOf(clt))

fmt.Println("\n --- \n")

fmt.Println("clt:")
fmt.Println("-", oldClt)
fmt.Println("+", clt)

fmt.Println("\n --- \n")

fmt.Println("clx:")
fmt.Println("-", oldClx)
fmt.Println("+", clx)

fmt.Println("\n <xxx> \n\n")
}

// if *vx is composite lit type, fill in elided type.
Expand Down

0 comments on commit d570f7c

Please sign in to comment.