Skip to content

Commit 9067b4c

Browse files
committed
Local with upstream
1 parent 2185b26 commit 9067b4c

15 files changed

+90
-71
lines changed

abi/contract.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ func (b *Builder) processContract(contract *ir.Contract) (*Contract, error) {
5151

5252
// Process state variables.
5353
for _, stateVar := range contract.GetStateVariables() {
54+
// Some old contracts will have this broken. It's related to 0.4 contracts.
55+
// Better to have at least something then nothing at all in this point.
56+
if stateVar.Name == "" && stateVar.GetTypeDescription() == nil {
57+
continue
58+
}
5459
method := b.processStateVariable(stateVar)
5560
toReturn = append(toReturn, method)
5661
}
@@ -143,7 +148,9 @@ func (b *Builder) buildMethodIO(method MethodIO, typeDescr *ast.TypeDescription)
143148
}
144149
method.InternalType = typeDescr.GetString()
145150
case "struct":
146-
return b.resolver.ResolveStructType(typeDescr)
151+
structMember := b.resolver.ResolveStructType(typeDescr)
152+
structMember.Name = method.Name
153+
return structMember
147154
default:
148155
method.Type = typeName
149156
method.InternalType = typeDescr.GetString()

abi/state_variable.go

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

33
import (
44
"github.com/unpackdev/solgo/ir"
5-
"github.com/unpackdev/solgo/utils"
65
)
76

87
// processStateVariable processes the provided StateVariable from the IR and constructs a Method representation.
@@ -17,10 +16,6 @@ func (b *Builder) processStateVariable(stateVar *ir.StateVariable) *Method {
1716
StateMutability: b.normalizeStateMutability(stateVar.GetStateMutability()),
1817
}
1918

20-
if stateVar.GetTypeDescription() == nil {
21-
utils.DumpNodeWithExit(stateVar)
22-
}
23-
2419
typeName := b.resolver.ResolveType(stateVar.GetTypeDescription())
2520

2621
switch typeName {

ast/modifier.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,9 @@ func (m *ModifierDefinition) ParseDefinition(
215215
parameters.Src = m.Src
216216
}
217217
m.Parameters = parameters
218-
218+
bodyNode := NewBodyNode(m.ASTBuilder, false)
219219
if ctx.Block() != nil && !ctx.Block().IsEmpty() {
220-
bodyNode := NewBodyNode(m.ASTBuilder, false)
221220
bodyNode.ParseBlock(unit, contractNode, m, ctx.Block())
222-
m.Body = bodyNode
223221

224222
if ctx.Block().AllUncheckedBlock() != nil {
225223
for _, uncheckedCtx := range ctx.Block().AllUncheckedBlock() {
@@ -229,6 +227,7 @@ func (m *ModifierDefinition) ParseDefinition(
229227
}
230228
}
231229
}
230+
m.Body = bodyNode
232231

233232
m.currentModifiers = append(m.currentModifiers, m)
234233
return m

ast/parameter.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,21 @@ func (p *Parameter) Parse(unit *SourceUnit[Node[ast_pb.SourceUnit]], fnNode Node
285285
p.TypeDescription = typeName.GetTypeDescription()
286286
p.currentVariables = append(p.currentVariables, p)
287287

288-
if refId, refTypeDescription := p.GetResolver().ResolveByNode(typeName, typeName.Name); refTypeDescription != nil {
289-
typeName.ReferencedDeclaration = refId
290-
typeName.TypeDescription = refTypeDescription
291-
p.TypeDescription = typeName.GetTypeDescription()
288+
if p.TypeDescription == nil {
289+
if refId, refTypeDescription := p.GetResolver().ResolveByNode(typeName, typeName.Name); refTypeDescription != nil {
290+
typeName.ReferencedDeclaration = refId
291+
typeName.TypeDescription = refTypeDescription
292+
p.TypeDescription = typeName.GetTypeDescription()
293+
}
294+
}
295+
296+
if p.Name == "" && p.TypeName != nil && p.TypeName.Name != "" {
297+
p.Name = p.TypeName.Name
292298
}
299+
300+
/* if p.Id == 4253 {
301+
utils.DumpNodeWithExit(p)
302+
}*/
293303
}
294304

295305
// ParseEventParameter parses the event parameter context and populates the Parameter fields for event parameters.

ast/primary_expression.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ func (p *PrimaryExpression) Parse(
217217
}
218218
}
219219

220+
if ctx.GetText() == "throw" {
221+
p.TypeDescription = &TypeDescription{
222+
TypeIdentifier: "t_magic_throw",
223+
TypeString: "throw",
224+
}
225+
}
226+
220227
if ctx.GetText() == "block" {
221228
p.TypeDescription = &TypeDescription{
222229
TypeIdentifier: "t_magic_block",

ast/reference.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ func (r *Resolver) ResolveByNode(node Node[NodeType], name string) (int64, *Type
5656
isPrefixSlice := strings.HasPrefix(name, "[]")
5757
cleanedName := name
5858

59-
if isSlice {
60-
cleanedName = strings.TrimSuffix(name, "[]")
61-
} else if isPrefixSlice {
62-
cleanedName = strings.TrimPrefix(name, "[]")
59+
if isSlice || isPrefixSlice {
60+
cleanedName = strings.ReplaceAll(name, "[]", "")
6361
}
6462

6563
rNode, rNodeType := r.resolveByNode(cleanedName, node)
@@ -479,6 +477,10 @@ func (r *Resolver) byEvents(name string) (int64, *TypeDescription) {
479477
}
480478

481479
func (r *Resolver) byGlobals(name string) (int64, *TypeDescription) {
480+
if strings.Contains(name, "[]") {
481+
name = strings.ReplaceAll(name, "[]", "")
482+
}
483+
482484
for _, node := range r.globalDefinitions {
483485
switch nodeCtx := node.(type) {
484486
case *EnumDefinition:

ast/storage.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (t *TypeName) StorageSize() (int64, bool) {
5656
if strings.Contains(t.GetTypeDescription().GetString(), "int_const") {
5757
rationalParts := strings.Split(t.GetTypeDescription().GetIdentifier(), "_by_")
5858
if len(rationalParts) == 2 {
59-
numerator, err1 := strconv.Atoi(rationalParts[0][len(rationalParts[0])-2:])
59+
numerator, err1 := strconv.Atoi(strings.Replace(rationalParts[0], "t_rational_", "", -1))
6060
denominator, err2 := strconv.Atoi(rationalParts[1])
6161
if err1 == nil && err2 == nil {
6262
bitSize := int64(math.Ceil(math.Log2(float64(numerator / denominator))))
@@ -91,8 +91,17 @@ func elementaryTypeSizeInBits(typeName string) (int64, bool) {
9191
// `bytes` with a fixed size, and dynamically sized types like `string` and `bytes`.
9292
// Returns the size and a boolean indicating if the type is recognized.
9393
func getTypeSizeInBits(typeName string) (int64, bool) {
94-
// TODO: Make this actually work better... Figure out dynamically what is the size of an array
95-
typeName = strings.TrimSuffix(typeName, "[]")
94+
// Handle array types
95+
if strings.HasSuffix(typeName, "[]") {
96+
elementType := strings.TrimSuffix(typeName, "[]")
97+
elementSize, ok := getTypeSizeInBits(elementType)
98+
if !ok {
99+
return 0, false
100+
}
101+
// For dynamic arrays, the size in bits depends on the actual content
102+
// and includes the overhead for array length (256 bits).
103+
return elementSize + 256, true
104+
}
96105

97106
switch {
98107
case typeName == "bool":
@@ -113,21 +122,27 @@ func getTypeSizeInBits(typeName string) (int64, bool) {
113122
}
114123

115124
return int64(bitSize), true
116-
125+
case typeName == "string":
126+
// Dynamic-size types; the size depends on the actual content.
127+
// It's hard to determine the exact size in bits without the content.
128+
// Returning a default size for the pointer.
129+
return 256, true
130+
case typeName == "bytes":
131+
// Dynamic-size types; the size depends on the actual content.
132+
// It's hard to determine the exact size in bits without the content.
133+
// Returning a default size for the pointer.
134+
return 256, true
117135
case strings.HasPrefix(typeName, "bytes"):
118136
byteSizeStr := strings.TrimPrefix(typeName, "bytes")
137+
if byteSizeStr == "" {
138+
return 0, false // Dynamic-sized bytes array, which is not supported
139+
}
119140
byteSize, err := strconv.Atoi(byteSizeStr)
120141
if err != nil || byteSize < 1 || byteSize > 32 {
121142
return 0, false
122143
}
123144
return int64(byteSize) * 8, true
124145

125-
case typeName == "string", typeName == "bytes":
126-
// Dynamic-size types; the size depends on the actual content.
127-
// It's hard to determine the exact size in bits without the content.
128-
// Returning a default size for the pointer.
129-
return 256, true
130-
131146
default:
132147
return 0, false // Type not recognized
133148
}

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

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

ast/type_name.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,10 @@ func (t *TypeName) SetReferenceDescriptor(refId int64, refDesc *TypeDescription)
7575
if strings.HasSuffix(t.Name, "[]") && refDesc != nil {
7676
if !strings.HasSuffix(refDesc.TypeString, "[]") {
7777
t.TypeDescription.TypeString += "[]"
78-
/*if t.PathNode != nil && t.PathNode.TypeDescription != nil {
79-
if !strings.HasSuffix(t.PathNode.Name, "[]") {
80-
if strings.HasSuffix(t.PathNode.TypeDescription.TypeString, "[]") {
81-
// Kumbayaa through fucking recursion...
82-
t.PathNode.TypeDescription.TypeString = strings.TrimSuffix(
83-
t.PathNode.TypeDescription.TypeString, "[]",
84-
)
85-
}
86-
}
87-
}*/
78+
return true
8879
}
8980
}
9081

91-
// Lets update the parent node as well in case that type description is not set...
92-
/* parentNodeId := t.GetSrc().GetParentIndex()
93-
94-
if parentNodeId > 0 {
95-
if parentNode := t.GetTree().GetById(parentNodeId); parentNode != nil {
96-
if parentNode.GetTypeDescription() == nil {
97-
parentNode.SetReferenceDescriptor(refId, refDesc)
98-
}
99-
}
100-
}
101-
*/
10282
return true
10383
}
10484

contracts/constructor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (c *Contract) DiscoverConstructor(ctx context.Context) error {
2929
abiRoot != nil && abiRoot.GetEntryContract().GetMethodByType("constructor") != nil {
3030
cAbi, _ := utils.ToJSON(abiRoot.GetEntryContract().GetMethodByType("constructor"))
3131
constructorAbi := fmt.Sprintf("[%s]", string(cAbi))
32+
//utils.DumpNodeWithExit(abiRoot.GetEntryContract().GetMethodByType("constructor"))
3233
//utils.DumpNodeWithExit(irRoot.GetEntryContract().GetConstructor().GetParameters())
3334
tx := c.descriptor.Transaction
3435
deployedBytecode := c.descriptor.DeployedBytecode
@@ -50,7 +51,8 @@ func (c *Contract) DiscoverConstructor(ctx context.Context) error {
5051

5152
// TODO: Fix
5253
// - 0x47CE0C6eD5B0Ce3d3A51fdb1C52DC66a7c3c2936 (239 bytes more) - Some shitty text...
53-
54+
//spew.Dump(hex.EncodeToString(tx.Data()))
55+
//fmt.Println("")
5456
//spew.Dump(hex.EncodeToString(adjustedData[constructorDataIndex:]))
5557
//spew.Dump(constructorAbi) // 239
5658
//utils.DumpNodeWithExit(irRoot.GetEntryContract().GetConstructor().GetParameters())

ir/function_call.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ir
22

33
import (
4+
"github.com/unpackdev/solgo/utils"
45
"strings"
56

67
v3 "github.com/cncf/xds/go/xds/type/v3"
@@ -139,6 +140,9 @@ func (b *Builder) processFunctionCall(fn *Function, unit *ast.FunctionCall) *Fun
139140
}
140141

141142
for _, arg := range unit.GetArguments() {
143+
if arg.GetTypeDescription() == nil {
144+
utils.DumpNodeWithExit(arg)
145+
}
142146
toReturn.ArgumentTypes = append(toReturn.ArgumentTypes, arg.GetTypeDescription().ToProto())
143147
}
144148

ir/standards.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ir
33
import (
44
ir_pb "github.com/unpackdev/protos/dist/go/ir"
55
"github.com/unpackdev/solgo/standards"
6-
"github.com/unpackdev/solgo/utils"
76
)
87

98
// Standard represents a specific Ethereum Improvement Proposal standard that a contract may adhere to.
@@ -70,25 +69,22 @@ func (b *Builder) processEips(root *RootSourceUnit) {
7069
outputs := make([]standards.Output, 0)
7170

7271
for _, param := range function.GetParameters() {
72+
if param.GetTypeDescription() == nil {
73+
//utils.DumpNodeWithExit(param)
74+
}
7375
inputs = append(inputs, standards.Input{
7476
Type: param.GetTypeDescription().GetString(),
7577
Indexed: false, // Specific to events...
7678
})
7779
}
7880

7981
for _, ret := range function.GetReturnStatements() {
80-
if ret.GetTypeDescription() != nil {
81-
outputs = append(outputs, standards.Output{
82-
Type: "t_unknown", // Will fix this later on with upgrade of parser to support solidity 0.5+
83-
})
84-
} else {
85-
if ret.GetTypeDescription() == nil {
86-
utils.DumpNodeWithExit(function)
87-
}
88-
outputs = append(outputs, standards.Output{
89-
Type: ret.GetTypeDescription().GetString(),
90-
})
82+
if ret.GetTypeDescription() == nil {
83+
//utils.DumpNodeWithExit(function)
9184
}
85+
outputs = append(outputs, standards.Output{
86+
Type: ret.GetTypeDescription().GetString(),
87+
})
9288
}
9389

9490
contract.Functions = append(contract.Functions, standards.StandardFunction{

ir/variables.go

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

33
import (
4-
"github.com/unpackdev/solgo/utils"
54
"strings"
65

76
ast_pb "github.com/unpackdev/protos/dist/go/ast"
@@ -128,9 +127,9 @@ func (b *Builder) processStateVariables(unit *ast.StateVariableDeclaration) *Sta
128127

129128
// It could be that the name of the type name node is not set, but the type description string is.
130129
if variableNode.Type == "" {
131-
if variableNode.TypeDescription == nil {
132-
utils.DumpNodeWithExit(variableNode)
133-
}
130+
/* if variableNode.TypeDescription == nil {
131+
utils.DumpNodeWithExit(variableNode)
132+
}*/
134133
variableNode.Type = variableNode.TypeDescription.TypeString
135134
}
136135

parser/solidity_parser.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/reader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package storage
33
import (
44
"context"
55
"fmt"
6-
"github.com/unpackdev/solgo/utils"
76
"strings"
87
)
98

@@ -75,8 +74,9 @@ func (r *Reader) CalculateStorageLayout() error {
7574
for _, variable := range variables {
7675
storageSize, found := variable.GetAST().GetTypeName().StorageSize()
7776
if !found {
78-
utils.DumpNodeWithExit(variable.GetAST().GetTypeName())
79-
return fmt.Errorf("error calculating storage size for variable: %s", variable.GetName())
77+
//utils.DumpNodeNoExit(variable.GetAST().GetTypeName())
78+
//return fmt.Errorf("error calculating storage size for variable: %s", variable.GetName())
79+
continue
8080
}
8181

8282
typeName := variable.GetType()

0 commit comments

Comments
 (0)