Skip to content

Commit

Permalink
fix(gnovm): update ValuePath depth access to use method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed Feb 14, 2025
1 parent 45aa50d commit dbd86be
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 62 deletions.
48 changes: 30 additions & 18 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1808,9 +1808,9 @@ func (sb *StaticBlock) GetBlockNodeForPath(store Store, path ValuePath) BlockNod
panic("expected block type value path but got " + path.Type.String())
}

// NOTE: path.Depth == 1 means it's in bn.
// NOTE: path.Depth() == 1 means it's in bn.
bn := sb.GetSource(store)
for i := 1; i < int(path.Depth); i++ {
for i := 1; i < int(path.Depth()); i++ {
bn = bn.GetParentNode(store)
}

Expand Down Expand Up @@ -1903,7 +1903,7 @@ func (sb *StaticBlock) GetStaticTypeOf(store Store, n Name) Type {
// Implements BlockNode.
func (sb *StaticBlock) GetStaticTypeOfAt(store Store, path ValuePath) Type {
if debug {
if path.Depth == 0 {
if path.Depth() == 0 {
panic("should not happen")
}
}
Expand Down Expand Up @@ -2119,11 +2119,23 @@ func (x *PackageNode) SetBody(b Body) {
// see tests/selector_test.go.
type ValuePath struct {
Type VPType // see VPType* consts.
Depth uint8 // see doc for ValuePath.
depth uint8 // see doc for ValuePath.
Index uint16 // index of value, field, or method.
Name Name // name of value, field, or method.
}

func (vp ValuePath) Depth() uint8 {
return vp.depth
}

func (vp *ValuePath) SetDepth(d uint8) {
if d > 127 {
panic("value path depth overflow")
}

Check warning on line 2134 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L2134

Added line #L2134 was not covered by tests

vp.depth = d
}

type VPType uint8

const (
Expand All @@ -2145,7 +2157,7 @@ const (
func NewValuePath(t VPType, depth uint8, index uint16, n Name) ValuePath {
vp := ValuePath{
Type: t,
Depth: depth,
depth: depth,
Index: index,
Name: n,
}
Expand Down Expand Up @@ -2203,61 +2215,61 @@ func NewValuePathNative(n Name) ValuePath {

func (vp ValuePath) Validate() {
// no ValuePath depths should exceed uint8/2
if vp.Depth > 127 {
if vp.Depth() > 127 {
panic("value path depth overflow")
}

switch vp.Type {
case VPUverse:
if vp.Depth != 0 {
if vp.Depth() != 0 {
panic("uverse value path must have depth 0")
}
case VPBlock:
// 0 ok ("_" blank)
case VPField:
if vp.Depth > 1 {
if vp.Depth() > 1 {
panic("field value path must have depth 0 or 1")
}
case VPValMethod:
if vp.Depth != 0 {
if vp.Depth() != 0 {
panic("method value path must have depth 0")
}
case VPPtrMethod:
if vp.Depth != 0 {
if vp.Depth() != 0 {
panic("ptr receiver method value path must have depth 0")
}
case VPInterface:
if vp.Depth != 0 {
if vp.Depth() != 0 {
panic("interface method value path must have depth 0")
}
if vp.Name == "" {
panic("interface value path must have name")
}
case VPSubrefField:
if vp.Depth > 3 {
if vp.Depth() > 3 {

Check warning on line 2249 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L2249

Added line #L2249 was not covered by tests
panic("subref field value path must have depth 0, 1, 2, or 3")
}
case VPDerefField:
if vp.Depth > 3 {
if vp.Depth() > 3 {

Check warning on line 2253 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L2253

Added line #L2253 was not covered by tests
panic("deref field value path must have depth 0, 1, 2, or 3")
}
case VPDerefValMethod:
if vp.Depth != 0 {
if vp.Depth() != 0 {

Check warning on line 2257 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L2257

Added line #L2257 was not covered by tests
panic("(deref) method value path must have depth 0")
}
case VPDerefPtrMethod:
if vp.Depth != 0 {
if vp.Depth() != 0 {

Check warning on line 2261 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L2261

Added line #L2261 was not covered by tests
panic("(deref) ptr receiver method value path must have depth 0")
}
case VPDerefInterface:
if vp.Depth != 0 {
if vp.Depth() != 0 {

Check warning on line 2265 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L2265

Added line #L2265 was not covered by tests
panic("(deref) interface method value path must have depth 0")
}
if vp.Name == "" {
panic("(deref) interface value path must have name")
}
case VPNative:
if vp.Depth != 0 {
if vp.Depth() != 0 {
panic("native value path must have depth 0")
}
if vp.Name == "" {
Expand All @@ -2271,7 +2283,7 @@ func (vp ValuePath) Validate() {
}

func (vp ValuePath) IsBlockBlankPath() bool {
return vp.Type == VPBlock && vp.Depth == 0 && vp.Index == 0
return vp.Type == VPBlock && vp.Depth() == 0 && vp.Index == 0
}

func (vp ValuePath) IsDerefType() bool {
Expand Down
8 changes: 4 additions & 4 deletions gnovm/pkg/gnolang/nodes_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ func (vp ValuePath) String() string {
case VPUverse:
return fmt.Sprintf("VPUverse(%d)", vp.Index)
case VPBlock:
return fmt.Sprintf("VPBlock(%d,%d)", vp.Depth, vp.Index)
return fmt.Sprintf("VPBlock(%d,%d)", vp.Depth(), vp.Index)
case VPField:
return fmt.Sprintf("VPField(%d,%d,%s)", vp.Depth, vp.Index, vp.Name)
return fmt.Sprintf("VPField(%d,%d,%s)", vp.Depth(), vp.Index, vp.Name)
case VPSubrefField:
return fmt.Sprintf("VPSubrefField(%d,%d,%s)", vp.Depth, vp.Index, vp.Name)
return fmt.Sprintf("VPSubrefField(%d,%d,%s)", vp.Depth(), vp.Index, vp.Name)

Check warning on line 76 in gnovm/pkg/gnolang/nodes_string.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes_string.go#L76

Added line #L76 was not covered by tests
case VPValMethod:
return fmt.Sprintf("VPValMethod(%d,%s)", vp.Index, vp.Name)
case VPPtrMethod:
return fmt.Sprintf("VPPtrMethod(%d,%s)", vp.Index, vp.Name)
case VPInterface:
return fmt.Sprintf("VPInterface(%s)", vp.Name)
case VPDerefField:
return fmt.Sprintf("VPDerefField(%d,%d,%s)", vp.Depth, vp.Index, vp.Name)
return fmt.Sprintf("VPDerefField(%d,%d,%s)", vp.Depth(), vp.Index, vp.Name)

Check warning on line 84 in gnovm/pkg/gnolang/nodes_string.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes_string.go#L84

Added line #L84 was not covered by tests
case VPDerefValMethod:
return fmt.Sprintf("VPDerefValMethod(%d,%s)", vp.Index, vp.Name)
case VPDerefPtrMethod:
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/op_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (m *Machine) doOpEval() {
// TODO: understand this better.
if nx, ok := x.(*NameExpr); ok {
m.PopExpr()
if nx.Path.Depth == 0 {
if nx.Path.Depth() == 0 {
// Name is in uverse (global).
gv := Uverse().GetBlock(nil).GetPointerTo(nil, nx.Path)
m.PushValue(gv.Deref())
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/op_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func (m *Machine) doOpStructLit() {
fnx := x.Elts[i].Key.(*NameExpr)
ftv := ftvs[i]
if debug {
if fnx.Path.Depth != 0 {
if fnx.Path.Depth() != 0 {

Check warning on line 736 in gnovm/pkg/gnolang/op_expressions.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_expressions.go#L736

Added line #L736 was not covered by tests
panic("unexpected struct composite lit key path generation value")
}
if !ftv.IsUndefined() && ftv.T.Kind() == InterfaceKind {
Expand Down
26 changes: 13 additions & 13 deletions gnovm/pkg/gnolang/op_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (m *Machine) doOpStaticTypeOf() {
switch x := x.(type) {
case *NameExpr:
// NOTE: duplicated from doOpEval
if x.Path.Depth == 0 {
if x.Path.Depth() == 0 {
// Name is in uverse (global).
gv := Uverse().GetBlock(nil).GetPointerTo(nil, x.Path)
m.PushValue(asValue(gv.TV.T))
Expand Down Expand Up @@ -234,50 +234,50 @@ func (m *Machine) doOpStaticTypeOf() {
path := x.Path // mutable
switch path.Type {
case VPField:
switch path.Depth { // see tests/selector_test.go for cases.
switch path.Depth() { // see tests/selector_test.go for cases.
case 0:
dxt = xt
case 1:
dxt = baseOf(xt)
path.Depth = 0
path.SetDepth(0)
default:
panic("should not happen")
}
case VPSubrefField:
switch path.Depth {
switch path.Depth() {

Check warning on line 247 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L247

Added line #L247 was not covered by tests
case 0:
dxt = xt.Elem()
path.Depth = 0
path.SetDepth(0)

Check warning on line 250 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L250

Added line #L250 was not covered by tests
case 1:
dxt = xt.Elem()
path.Depth = 0
path.SetDepth(0)

Check warning on line 253 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L253

Added line #L253 was not covered by tests
case 2:
dxt = baseOf(xt.Elem())
path.Depth = 0
path.SetDepth(0)

Check warning on line 256 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L256

Added line #L256 was not covered by tests
case 3:
dxt = baseOf(xt.Elem())
path.Depth = 0
path.SetDepth(0)

Check warning on line 259 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L259

Added line #L259 was not covered by tests
default:
panic("should not happen")
}
case VPDerefField:
switch path.Depth {
switch path.Depth() {
case 0:
dxt = xt.Elem()
path.Type = VPField
path.Depth = 0
path.SetDepth(0)

Check warning on line 268 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L268

Added line #L268 was not covered by tests
case 1:
dxt = xt.Elem()
path.Type = VPField
path.Depth = 0
path.SetDepth(0)

Check warning on line 272 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L272

Added line #L272 was not covered by tests
case 2:
dxt = baseOf(xt.Elem())
path.Type = VPField
path.Depth = 0
path.SetDepth(0)
case 3:
dxt = baseOf(xt.Elem())
path.Type = VPField
path.Depth = 0
path.SetDepth(0)

Check warning on line 280 in gnovm/pkg/gnolang/op_types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_types.go#L280

Added line #L280 was not covered by tests
default:
panic("should not happen")
}
Expand Down
8 changes: 4 additions & 4 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
fillNameExprPath(last, n, false)
}
// If uverse, return a *ConstExpr.
if n.Path.Depth == 0 { // uverse
if n.Path.Depth() == 0 { // uverse
cx := evalConst(store, last, n)
// built-in functions must be called.
if !cx.IsUndefined() &&
Expand Down Expand Up @@ -2886,7 +2886,7 @@ func findLoopUses1(ctx BlockNode, bn BlockNode) {
idx := addHeapCapture(dbn, fle, n.Name)
// adjust NameExpr type.
n.Type = NameExprTypeHeapUse
n.Path.Depth = uint8(depth)
n.Path.SetDepth(uint8(depth))
n.Path.Index = idx
} else {
if ftype == TRANS_REF_X {
Expand Down Expand Up @@ -2982,7 +2982,7 @@ func addHeapCapture(dbn BlockNode, fle *FuncLitExpr, name Name) (idx uint16) {

// add name to fle.HeapCaptures.
vp := fle.GetPathForName(nil, name)
vp.Depth -= 1 // minus 1 for fle itself.
vp.SetDepth(vp.Depth() - 1) // minus 1 for fle itself.
ne := NameExpr{
Path: vp,
Name: name,
Expand Down Expand Up @@ -5066,7 +5066,7 @@ func fillNameExprPath(last BlockNode, nx *NameExpr, isDefineLHS bool) {
break
}
}
path.Depth += uint8(i)
path.SetDepth(path.Depth() + uint8(i))
path.Validate()
nx.Path = path
return
Expand Down
17 changes: 9 additions & 8 deletions gnovm/pkg/gnolang/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,13 +692,13 @@ func (pt *PointerType) FindEmbeddedFieldType(callerPath string, n Name, m map[Ty
} else {
// Case 2: otherwise, is just a deref field.
trail[0].Type = VPDerefField
switch trail[0].Depth {
switch trail[0].Depth() {
case 0:
// *PointerType > *StructType.Field has depth 0.
case 1:
// *DeclaredType > *StructType.Field has depth 1 (& type VPField).
// *PointerType > *DeclaredType > *StructType.Field has depth 2.
trail[0].Depth = 2
trail[0].SetDepth(2)
/*
// If trail[-1].Type == VPPtrMethod, set VPDerefPtrMethod.
if len(trail) > 1 && trail[1].Type == VPPtrMethod {
Expand Down Expand Up @@ -804,7 +804,7 @@ func (st *StructType) GetPathForName(n Name) ValuePath {

func (st *StructType) GetStaticTypeOfAt(path ValuePath) Type {
if debug {
if path.Depth != 0 {
if path.Depth() != 0 {

Check warning on line 807 in gnovm/pkg/gnolang/types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/types.go#L807

Added line #L807 was not covered by tests
panic("expected path.Depth of 0")
}
}
Expand Down Expand Up @@ -1602,7 +1602,7 @@ func (dt *DeclaredType) GetPathForName(n Name) ValuePath {
}
// Otherwise it is underlying.
path := dt.Base.(ValuePather).GetPathForName(n)
path.Depth += 1
path.SetDepth(path.Depth() + 1)

Check warning on line 1605 in gnovm/pkg/gnolang/types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/types.go#L1605

Added line #L1605 was not covered by tests
return path
}

Expand Down Expand Up @@ -1668,11 +1668,12 @@ func (dt *DeclaredType) FindEmbeddedFieldType(callerPath string, n Name, m map[T
return trail, hasPtr, rcvr, ft, false
case VPField, VPDerefField:
if debug {
if trail[0].Depth != 0 && trail[0].Depth != 2 {
if trail[0].Depth() != 0 && trail[0].Depth() != 2 {

Check warning on line 1671 in gnovm/pkg/gnolang/types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/types.go#L1671

Added line #L1671 was not covered by tests
panic("should not happen")
}
}
trail[0].Depth += 1

trail[0].SetDepth(trail[0].Depth() + 1)
return trail, hasPtr, rcvr, ft, false
default:
panic("should not happen")
Expand All @@ -1697,7 +1698,7 @@ func (dt *DeclaredType) GetValueAt(alloc *Allocator, store Store, path ValuePath
// should call *DT.FindEmbeddedFieldType(name) instead.
// tr, hp, rt, ft := dt.FindEmbeddedFieldType(n)
case VPValMethod, VPPtrMethod, VPField:
if path.Depth == 0 {
if path.Depth() == 0 {
mtv := dt.Methods[path.Index]
// Fill in *FV.Closure.
ft := mtv.T
Expand All @@ -1722,7 +1723,7 @@ func (dt *DeclaredType) GetStaticValueAt(path ValuePath) TypedValue {
// should call *DT.FindEmbeddedFieldType(name) instead.
// tr, hp, rt, ft := dt.FindEmbeddedFieldType(n)
case VPValMethod, VPPtrMethod, VPField:
if path.Depth == 0 {
if path.Depth() == 0 {
return dt.Methods[path.Index]
} else {
panic("DeclaredType.GetStaticValueAt() expects depth == 0")
Expand Down
Loading

0 comments on commit dbd86be

Please sign in to comment.