forked from progrium/darwinkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_type.go
80 lines (71 loc) · 1.61 KB
/
block_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package typing
import (
"fmt"
"strings"
"github.com/progrium/macdriver/generate/modules"
"github.com/progrium/macdriver/internal/set"
)
var _ Type = (*BlockType)(nil)
type BlockParam struct {
Type Type
Name string // the param name
}
// BlockType
type BlockType struct {
ReturnType Type
Params []BlockParam
}
func (a *BlockType) GoImports() set.Set[string] {
imports := set.New("github.com/progrium/macdriver/objc")
imports.AddSet(a.ReturnType.GoImports())
for _, p := range a.Params {
imports.AddSet(p.Type.GoImports())
}
return imports
}
func (a *BlockType) GoName(currentModule *modules.Module, receiveFromObjc bool) string {
var sb strings.Builder
sb.WriteString("func (")
for i, p := range a.Params {
if i > 0 {
sb.WriteString(", ")
}
if _, ok := p.Type.(*VoidType); ok {
continue
}
switch p.Name {
case "range", "type":
sb.WriteString(p.Name + "_")
case "":
sb.WriteString(fmt.Sprintf("arg%d", i))
default:
sb.WriteString(p.Name)
}
sb.WriteString(" ")
sb.WriteString(p.Type.GoName(currentModule, true))
}
sb.WriteString(")")
if _, ok := a.ReturnType.(*VoidType); !ok {
sb.WriteByte(' ')
sb.WriteString(a.ReturnType.GoName(currentModule, true))
}
return sb.String()
}
func (a *BlockType) ObjcName() string {
var sb strings.Builder
sb.WriteString(a.ReturnType.ObjcName())
sb.WriteString(" (^)(")
for i, p := range a.Params {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(p.Type.ObjcName())
sb.WriteString(" ")
sb.WriteString(p.Name)
}
sb.WriteString(")")
return sb.String()
}
func (a *BlockType) DeclareModule() *modules.Module {
return nil
}