Skip to content

Commit

Permalink
Major improvements to the PathNode and Solidity v0.4.... parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Jul 26, 2024
1 parent 3c6f2c7 commit 2185b26
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 59 deletions.
9 changes: 7 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@

Contract Issues

- [ ] 0x258FD2E6b5C155aa5f3e84326A622288bd70f376
- [x] 0x258FD2E6b5C155aa5f3e84326A622288bd70f376
- [ ] 0x2aa101BF99CaeF7fc1355D4c493a1fe187A007cE
- [ ] 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84
- [ ] 0x6000da47483062A0D734Ba3dc7576Ce6A0B645C4
- [ ] 0x609c690e8F7D68a59885c9132e812eEbDaAf0c9e
- [ ] 0x76264869a3eBF51a59FCa5ABa84ee2867c7F190e
- [ ] 0x6eF81a18E1E432C289DC0d1a670B78E8bbF9AA35
- [ ] 0x98D951E9b0C0Bb180F1b3ed40DDE6E1B1B521Cc1
- [ ] 0xCD7ae3373F7e76A817238261b8303FA17D2AF585
- [ ] 0xdEb43523E2857b7ec29D078c77b73709D958c62F
- [ ] 0x8CB3649114051cA5119141a34C200D65dc0Faa73
- [ ] 0xD101dCC414F310268c37eEb4cD376CcFA507F571
- [ ] 0x09B33A99B954e52907c61514B6f8cD37De71076f
- [ ] 0x09B33A99B954e52907c61514B6f8cD37De71076f
6 changes: 3 additions & 3 deletions abi/error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package abi

import (
"fmt"

"github.com/unpackdev/solgo/ir"
)

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

for _, parameter := range unit.GetParameters() {
if parameter.GetTypeDescription() == nil {
return nil, fmt.Errorf("nil type description for error parameter %s", parameter.GetName())
//utils.DumpNodeWithExit(unit)
//return nil, fmt.Errorf("nil type description for error parameter %s", parameter.GetName())
continue
}

methodIo := MethodIO{
Expand Down
5 changes: 3 additions & 2 deletions abi/state_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package abi

import (
"github.com/unpackdev/solgo/ir"
"github.com/unpackdev/solgo/utils"
)

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

/* if stateVar.GetTypeDescription() == nil {
if stateVar.GetTypeDescription() == nil {
utils.DumpNodeWithExit(stateVar)
}*/
}

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

Expand Down
7 changes: 6 additions & 1 deletion abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ func (t *TypeResolver) ResolveStructType(typeName *ast.TypeDescription) MethodIO
nameCleaned = strings.TrimRight(nameCleaned, "[]")
nameParts := strings.Split(nameCleaned, ".")

methodType := "tuple"
if strings.Contains(typeName.GetString(), "[]") {
methodType = "tuple[]"
}

toReturn := MethodIO{
Name: nameParts[1],
Components: make([]MethodIO, 0),
Type: "tuple",
Type: methodType,
InternalType: typeName.GetString(),
}

Expand Down
1 change: 0 additions & 1 deletion ast/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ast

import (
"fmt"

ast_pb "github.com/unpackdev/protos/dist/go/ast"
"github.com/unpackdev/solgo/parser"
)
Expand Down
24 changes: 12 additions & 12 deletions ast/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func (r *Resolver) ResolveByNode(node Node[NodeType], name string) (int64, *Type

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

//fmt.Println(name, cleanedName, isSlice, isPrefixSlice, rNodeType.TypeString, rNode)

// Somewhere in the code [] is applied to rNodeType where it should not be...
// TODO: Remove it from here and fix wherever it's happening. I don't give a crap atm about this...
if !strings.Contains(name, "[]") && strings.Contains(rNodeType.TypeString, "[]") {
rNodeType.TypeString = strings.ReplaceAll(rNodeType.TypeString, "[]", "")
}
/* if !strings.Contains(name, "[]") && strings.Contains(rNodeType.TypeString, "[]") {
rNodeType.TypeString = strings.ReplaceAll(rNodeType.TypeString, "[]", "")
}*/
}

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

if node, nodeType := r.byGlobals(name); nodeType != nil {
return node, nodeType
}

if node, nodeType := r.byStateVariables(name); nodeType != nil {
return node, nodeType
}
Expand Down Expand Up @@ -136,6 +135,10 @@ func (r *Resolver) resolveByNode(name string, baseNode Node[NodeType]) (int64, *
return node, nodeType
}

if node, nodeType := r.byGlobals(name); nodeType != nil {
return node, nodeType
}

if node, nodeType := r.byImport(name, baseNode); nodeType != nil {
return node, nodeType
}
Expand Down Expand Up @@ -345,10 +348,6 @@ func (r *Resolver) byImport(name string, baseNode Node[NodeType]) (int64, *TypeD
if node.GetType() == ast_pb.NodeType_IMPORT_DIRECTIVE {
importNode := node.(*Import)

if importNode.GetName() == name {
return importNode.GetId(), importNode.GetTypeDescription()
}

if importNode.GetUnitAlias() == name {
return importNode.GetId(), importNode.GetTypeDescription()
}
Expand Down Expand Up @@ -564,6 +563,7 @@ func (r *Resolver) byStructs(name string) (int64, *TypeDescription) {

for _, node := range r.currentStructs {
structNode := node.(*StructDefinition)

if structNode.GetName() == name {
return node.GetId(), node.GetTypeDescription()
}
Expand Down
3 changes: 3 additions & 0 deletions ast/source_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func (b *ASTBuilder) EnterSourceUnit(ctx *parser.SourceUnitContext) {
sourceUnit.Kind = ast_pb.NodeType_KIND_INTERFACE
interfaceNode := NewInterfaceDefinition(b)
interfaceNode.Parse(ctx, interfaceCtx, rootNode, sourceUnit)
//fmt.Println("Interface found...", interfaceCtx.Identifier().GetText())
b.sourceUnits = append(b.sourceUnits, sourceUnit)
}

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

Expand All @@ -329,6 +331,7 @@ func (b *ASTBuilder) EnterSourceUnit(ctx *parser.SourceUnitContext) {
sourceUnit.Kind = ast_pb.NodeType_KIND_CONTRACT
contractNode := NewContractDefinition(b)
contractNode.Parse(ctx, contractCtx, rootNode, sourceUnit)
//fmt.Println("Contract found...", contractCtx.Identifier().GetText())
b.sourceUnits = append(b.sourceUnits, sourceUnit)
}
}
Expand Down
13 changes: 13 additions & 0 deletions ast/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ast
import (
"fmt"
ast_pb "github.com/unpackdev/protos/dist/go/ast"
"math"
"strconv"
"strings"
)
Expand Down Expand Up @@ -52,6 +53,18 @@ func (t *TypeName) StorageSize() (int64, bool) {
return 256, true
}

if strings.Contains(t.GetTypeDescription().GetString(), "int_const") {
rationalParts := strings.Split(t.GetTypeDescription().GetIdentifier(), "_by_")
if len(rationalParts) == 2 {
numerator, err1 := strconv.Atoi(rationalParts[0][len(rationalParts[0])-2:])
denominator, err2 := strconv.Atoi(rationalParts[1])
if err1 == nil && err2 == nil {
bitSize := int64(math.Ceil(math.Log2(float64(numerator / denominator))))
return bitSize, true
}
}
}

return 0, false

// Add cases for other node types like struct, enum, etc., as needed.
Expand Down
23 changes: 11 additions & 12 deletions ast/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ package ast
import (
"fmt"
"github.com/goccy/go-json"

ast_pb "github.com/unpackdev/protos/dist/go/ast"
"github.com/unpackdev/solgo/parser"
)

// StructDefinition represents a struct definition in the Solidity abstract syntax tree (AST).
type StructDefinition struct {
*ASTBuilder // Embedding the ASTBuilder for common functionality
SourceUnitName string `json:"-"` // Name of the source unit
Id int64 `json:"id"` // Unique identifier for the struct definition
NodeType ast_pb.NodeType `json:"nodeType"` // Type of the node (STRUCT_DEFINITION for struct definition)
Src SrcNode `json:"src"` // Source information about the struct definition
Name string `json:"name"` // Name of the struct
NameLocation SrcNode `json:"nameLocation"` // Source information about the name of the struct
CanonicalName string `json:"canonicalName"` // Canonical name of the struct
SourceUnitName string `json:"-"` // Name of the source unit
Id int64 `json:"id"` // Unique identifier for the struct definition
NodeType ast_pb.NodeType `json:"nodeType"` // Type of the node (STRUCT_DEFINITION for struct definition)
Src SrcNode `json:"src"` // Source information about the struct definition
Name string `json:"name"` // Name of the struct
NameLocation SrcNode `json:"nameLocation"` // Source information about the name of the struct
CanonicalName string `json:"canonicalName"` // Canonical name of the struct
ReferencedDeclaration int64 `json:"referencedDeclaration,omitempty"` // Referenced declaration of the struct definition
TypeDescription *TypeDescription `json:"typeDescription"` // Type description of the struct definition
Members []Node[NodeType] `json:"members"` // Members of the struct definition
Visibility ast_pb.Visibility `json:"visibility"` // Visibility of the struct definition
StorageLocation ast_pb.StorageLocation `json:"storageLocation"` // Storage location of the struct definition
TypeDescription *TypeDescription `json:"typeDescription"` // Type description of the struct definition
Members []Node[NodeType] `json:"members"` // Members of the struct definition
Visibility ast_pb.Visibility `json:"visibility"` // Visibility of the struct definition
StorageLocation ast_pb.StorageLocation `json:"storageLocation"` // Storage location of the struct definition
}

// NewStructDefinition creates a new StructDefinition instance.
Expand Down
4 changes: 2 additions & 2 deletions ast/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func (t *Tree) UpdateNodeReferenceById(nodeId int64, nodeRefId int64, typeRef *T
return false
}

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

for _, child := range t.astRoot.GetNodes() {
for _, child := range t.astRoot.GetGlobalNodes() {
if n := t.byRecursiveReferenceUpdate(child, nodeId, nodeRefId, typeRef); n {
return n
}
Expand Down
Loading

0 comments on commit 2185b26

Please sign in to comment.