Skip to content

Commit 2185b26

Browse files
committed
Major improvements to the PathNode and Solidity v0.4.... parsing
1 parent 3c6f2c7 commit 2185b26

File tree

16 files changed

+198
-59
lines changed

16 files changed

+198
-59
lines changed

TODO.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11

22
Contract Issues
33

4-
- [ ] 0x258FD2E6b5C155aa5f3e84326A622288bd70f376
4+
- [x] 0x258FD2E6b5C155aa5f3e84326A622288bd70f376
5+
- [ ] 0x2aa101BF99CaeF7fc1355D4c493a1fe187A007cE
6+
- [ ] 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84
7+
- [ ] 0x6000da47483062A0D734Ba3dc7576Ce6A0B645C4
8+
- [ ] 0x609c690e8F7D68a59885c9132e812eEbDaAf0c9e
9+
- [ ] 0x76264869a3eBF51a59FCa5ABa84ee2867c7F190e
510
- [ ] 0x6eF81a18E1E432C289DC0d1a670B78E8bbF9AA35
611
- [ ] 0x98D951E9b0C0Bb180F1b3ed40DDE6E1B1B521Cc1
712
- [ ] 0xCD7ae3373F7e76A817238261b8303FA17D2AF585
813
- [ ] 0xdEb43523E2857b7ec29D078c77b73709D958c62F
914
- [ ] 0x8CB3649114051cA5119141a34C200D65dc0Faa73
1015
- [ ] 0xD101dCC414F310268c37eEb4cD376CcFA507F571
11-
- [ ] 0x09B33A99B954e52907c61514B6f8cD37De71076f
16+
- [ ] 0x09B33A99B954e52907c61514B6f8cD37De71076f

abi/error.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package abi
22

33
import (
4-
"fmt"
5-
64
"github.com/unpackdev/solgo/ir"
75
)
86

@@ -19,7 +17,9 @@ func (b *Builder) processError(unit *ir.Error) (*Method, error) {
1917

2018
for _, parameter := range unit.GetParameters() {
2119
if parameter.GetTypeDescription() == nil {
22-
return nil, fmt.Errorf("nil type description for error parameter %s", parameter.GetName())
20+
//utils.DumpNodeWithExit(unit)
21+
//return nil, fmt.Errorf("nil type description for error parameter %s", parameter.GetName())
22+
continue
2323
}
2424

2525
methodIo := MethodIO{

abi/state_variable.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package abi
22

33
import (
44
"github.com/unpackdev/solgo/ir"
5+
"github.com/unpackdev/solgo/utils"
56
)
67

78
// processStateVariable processes the provided StateVariable from the IR and constructs a Method representation.
@@ -16,9 +17,9 @@ func (b *Builder) processStateVariable(stateVar *ir.StateVariable) *Method {
1617
StateMutability: b.normalizeStateMutability(stateVar.GetStateMutability()),
1718
}
1819

19-
/* if stateVar.GetTypeDescription() == nil {
20+
if stateVar.GetTypeDescription() == nil {
2021
utils.DumpNodeWithExit(stateVar)
21-
}*/
22+
}
2223

2324
typeName := b.resolver.ResolveType(stateVar.GetTypeDescription())
2425

abi/type.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ func (t *TypeResolver) ResolveStructType(typeName *ast.TypeDescription) MethodIO
9797
nameCleaned = strings.TrimRight(nameCleaned, "[]")
9898
nameParts := strings.Split(nameCleaned, ".")
9999

100+
methodType := "tuple"
101+
if strings.Contains(typeName.GetString(), "[]") {
102+
methodType = "tuple[]"
103+
}
104+
100105
toReturn := MethodIO{
101106
Name: nameParts[1],
102107
Components: make([]MethodIO, 0),
103-
Type: "tuple",
108+
Type: methodType,
104109
InternalType: typeName.GetString(),
105110
}
106111

ast/error.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ast
22

33
import (
44
"fmt"
5-
65
ast_pb "github.com/unpackdev/protos/dist/go/ast"
76
"github.com/unpackdev/solgo/parser"
87
)

ast/reference.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func (r *Resolver) ResolveByNode(node Node[NodeType], name string) (int64, *Type
6666

6767
// Node could not be found in this moment, we are going to see if we can discover it in the
6868
// future at the end of whole parsing process.
69-
if rNodeType == nil {
69+
// It can be that within the import it's discovered and we have to move away from it.
70+
if rNodeType == nil || rNodeType.TypeString == "import" {
7071
r.UnprocessedNodes[node.GetId()] = UnprocessedNode{
7172
Id: node.GetId(),
7273
Name: name,
@@ -79,11 +80,13 @@ func (r *Resolver) ResolveByNode(node Node[NodeType], name string) (int64, *Type
7980
rNodeType.TypeString = "[]" + rNodeType.TypeString
8081
}
8182

83+
//fmt.Println(name, cleanedName, isSlice, isPrefixSlice, rNodeType.TypeString, rNode)
84+
8285
// Somewhere in the code [] is applied to rNodeType where it should not be...
8386
// TODO: Remove it from here and fix wherever it's happening. I don't give a crap atm about this...
84-
if !strings.Contains(name, "[]") && strings.Contains(rNodeType.TypeString, "[]") {
85-
rNodeType.TypeString = strings.ReplaceAll(rNodeType.TypeString, "[]", "")
86-
}
87+
/* if !strings.Contains(name, "[]") && strings.Contains(rNodeType.TypeString, "[]") {
88+
rNodeType.TypeString = strings.ReplaceAll(rNodeType.TypeString, "[]", "")
89+
}*/
8790
}
8891

8992
return rNode, rNodeType
@@ -96,10 +99,6 @@ func (r *Resolver) resolveByNode(name string, baseNode Node[NodeType]) (int64, *
9699
return node, nodeType
97100
}
98101

99-
if node, nodeType := r.byGlobals(name); nodeType != nil {
100-
return node, nodeType
101-
}
102-
103102
if node, nodeType := r.byStateVariables(name); nodeType != nil {
104103
return node, nodeType
105104
}
@@ -136,6 +135,10 @@ func (r *Resolver) resolveByNode(name string, baseNode Node[NodeType]) (int64, *
136135
return node, nodeType
137136
}
138137

138+
if node, nodeType := r.byGlobals(name); nodeType != nil {
139+
return node, nodeType
140+
}
141+
139142
if node, nodeType := r.byImport(name, baseNode); nodeType != nil {
140143
return node, nodeType
141144
}
@@ -345,10 +348,6 @@ func (r *Resolver) byImport(name string, baseNode Node[NodeType]) (int64, *TypeD
345348
if node.GetType() == ast_pb.NodeType_IMPORT_DIRECTIVE {
346349
importNode := node.(*Import)
347350

348-
if importNode.GetName() == name {
349-
return importNode.GetId(), importNode.GetTypeDescription()
350-
}
351-
352351
if importNode.GetUnitAlias() == name {
353352
return importNode.GetId(), importNode.GetTypeDescription()
354353
}
@@ -564,6 +563,7 @@ func (r *Resolver) byStructs(name string) (int64, *TypeDescription) {
564563

565564
for _, node := range r.currentStructs {
566565
structNode := node.(*StructDefinition)
566+
567567
if structNode.GetName() == name {
568568
return node.GetId(), node.GetTypeDescription()
569569
}

ast/source_unit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ func (b *ASTBuilder) EnterSourceUnit(ctx *parser.SourceUnitContext) {
311311
sourceUnit.Kind = ast_pb.NodeType_KIND_INTERFACE
312312
interfaceNode := NewInterfaceDefinition(b)
313313
interfaceNode.Parse(ctx, interfaceCtx, rootNode, sourceUnit)
314+
//fmt.Println("Interface found...", interfaceCtx.Identifier().GetText())
314315
b.sourceUnits = append(b.sourceUnits, sourceUnit)
315316
}
316317

@@ -320,6 +321,7 @@ func (b *ASTBuilder) EnterSourceUnit(ctx *parser.SourceUnitContext) {
320321
sourceUnit.Kind = ast_pb.NodeType_KIND_LIBRARY
321322
libraryNode := NewLibraryDefinition(b)
322323
libraryNode.Parse(ctx, libraryCtx, rootNode, sourceUnit)
324+
//fmt.Println("Library found...", libraryCtx.Identifier().GetText())
323325
b.sourceUnits = append(b.sourceUnits, sourceUnit)
324326
}
325327

@@ -329,6 +331,7 @@ func (b *ASTBuilder) EnterSourceUnit(ctx *parser.SourceUnitContext) {
329331
sourceUnit.Kind = ast_pb.NodeType_KIND_CONTRACT
330332
contractNode := NewContractDefinition(b)
331333
contractNode.Parse(ctx, contractCtx, rootNode, sourceUnit)
334+
//fmt.Println("Contract found...", contractCtx.Identifier().GetText())
332335
b.sourceUnits = append(b.sourceUnits, sourceUnit)
333336
}
334337
}

ast/storage.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ast
33
import (
44
"fmt"
55
ast_pb "github.com/unpackdev/protos/dist/go/ast"
6+
"math"
67
"strconv"
78
"strings"
89
)
@@ -52,6 +53,18 @@ func (t *TypeName) StorageSize() (int64, bool) {
5253
return 256, true
5354
}
5455

56+
if strings.Contains(t.GetTypeDescription().GetString(), "int_const") {
57+
rationalParts := strings.Split(t.GetTypeDescription().GetIdentifier(), "_by_")
58+
if len(rationalParts) == 2 {
59+
numerator, err1 := strconv.Atoi(rationalParts[0][len(rationalParts[0])-2:])
60+
denominator, err2 := strconv.Atoi(rationalParts[1])
61+
if err1 == nil && err2 == nil {
62+
bitSize := int64(math.Ceil(math.Log2(float64(numerator / denominator))))
63+
return bitSize, true
64+
}
65+
}
66+
}
67+
5568
return 0, false
5669

5770
// Add cases for other node types like struct, enum, etc., as needed.

ast/struct.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@ package ast
33
import (
44
"fmt"
55
"github.com/goccy/go-json"
6-
76
ast_pb "github.com/unpackdev/protos/dist/go/ast"
87
"github.com/unpackdev/solgo/parser"
98
)
109

1110
// StructDefinition represents a struct definition in the Solidity abstract syntax tree (AST).
1211
type StructDefinition struct {
1312
*ASTBuilder // Embedding the ASTBuilder for common functionality
14-
SourceUnitName string `json:"-"` // Name of the source unit
15-
Id int64 `json:"id"` // Unique identifier for the struct definition
16-
NodeType ast_pb.NodeType `json:"nodeType"` // Type of the node (STRUCT_DEFINITION for struct definition)
17-
Src SrcNode `json:"src"` // Source information about the struct definition
18-
Name string `json:"name"` // Name of the struct
19-
NameLocation SrcNode `json:"nameLocation"` // Source information about the name of the struct
20-
CanonicalName string `json:"canonicalName"` // Canonical name of the struct
13+
SourceUnitName string `json:"-"` // Name of the source unit
14+
Id int64 `json:"id"` // Unique identifier for the struct definition
15+
NodeType ast_pb.NodeType `json:"nodeType"` // Type of the node (STRUCT_DEFINITION for struct definition)
16+
Src SrcNode `json:"src"` // Source information about the struct definition
17+
Name string `json:"name"` // Name of the struct
18+
NameLocation SrcNode `json:"nameLocation"` // Source information about the name of the struct
19+
CanonicalName string `json:"canonicalName"` // Canonical name of the struct
2120
ReferencedDeclaration int64 `json:"referencedDeclaration,omitempty"` // Referenced declaration of the struct definition
22-
TypeDescription *TypeDescription `json:"typeDescription"` // Type description of the struct definition
23-
Members []Node[NodeType] `json:"members"` // Members of the struct definition
24-
Visibility ast_pb.Visibility `json:"visibility"` // Visibility of the struct definition
25-
StorageLocation ast_pb.StorageLocation `json:"storageLocation"` // Storage location of the struct definition
21+
TypeDescription *TypeDescription `json:"typeDescription"` // Type description of the struct definition
22+
Members []Node[NodeType] `json:"members"` // Members of the struct definition
23+
Visibility ast_pb.Visibility `json:"visibility"` // Visibility of the struct definition
24+
StorageLocation ast_pb.StorageLocation `json:"storageLocation"` // Storage location of the struct definition
2625
}
2726

2827
// NewStructDefinition creates a new StructDefinition instance.

ast/tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ func (t *Tree) UpdateNodeReferenceById(nodeId int64, nodeRefId int64, typeRef *T
7070
return false
7171
}
7272

73-
for _, child := range t.astRoot.GetGlobalNodes() {
73+
for _, child := range t.astRoot.GetNodes() {
7474
if n := t.byRecursiveReferenceUpdate(child, nodeId, nodeRefId, typeRef); n {
7575
return n
7676
}
7777
}
7878

79-
for _, child := range t.astRoot.GetNodes() {
79+
for _, child := range t.astRoot.GetGlobalNodes() {
8080
if n := t.byRecursiveReferenceUpdate(child, nodeId, nodeRefId, typeRef); n {
8181
return n
8282
}

0 commit comments

Comments
 (0)