Skip to content

Commit

Permalink
support function overloading with different arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Sep 20, 2024
1 parent 1b6ce6a commit 201c4b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
16 changes: 11 additions & 5 deletions compiler/compiler/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func (c *Compiler) funcType(params, returnTypes []parser.TypeNode, isMethod bool
}

// ABI description
// method: package + _method_ + type + _ + name
// named function: package + name
// cffi(extern): function_name
// method: package + _method_ + type + _ + name + args
// named function: package + name + args
// cffi(extern): function_name + args
// lambda: package + anonName
func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {
var compiledName string
Expand Down Expand Up @@ -120,8 +120,15 @@ func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {
}

argTypes := make([]parser.TypeNode, len(v.Arguments))
argTypesName := ""
for k, v := range v.Arguments {
argTypes[k] = v.Type
argTypesName += v.Type.Type()
}

// add arguments types to support overloading
if !v.IsExtern {
compiledName += argTypesName
}

retTypes := make([]parser.TypeNode, len(v.ReturnValues))
Expand Down Expand Up @@ -161,7 +168,7 @@ func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {
LlvmReturnType: funcRetType,
ReturnTypes: treReturnTypes,
IsVariadic: isVariadicFunc,
IsExtern: v.IsExtern,
IsExtern: v.IsExtern,
ArgumentTypes: treParams,
}

Expand Down Expand Up @@ -560,7 +567,6 @@ func (c *Compiler) compileCallNode(v *parser.CallNode) value.Value {
}
}


llvmArgs[i] = val
}

Expand Down
19 changes: 19 additions & 0 deletions tests/examples/func.cell
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package main
import "debug"

type A table {}

func (a *A) bar(a uint32) {

}

func (a *A) bar(a uint64) {

}

func foo(a uint32) {

}

func foo(a uint64) {

}

func main() {
hi := func() int64 {
return 999
Expand Down

0 comments on commit 201c4b7

Please sign in to comment.