Skip to content

Commit d526050

Browse files
committed
generate: iterate on function support
1 parent ef1b73e commit d526050

File tree

1,873 files changed

+156543
-113073
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,873 files changed

+156543
-113073
lines changed

generate/codegen/gen_function.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Function struct {
1616
Type *typing.FunctionType
1717
Name string // the first part of objc function name
1818
GoName string
19-
Params []*Param
19+
Parameters []*Param
2020
ReturnType typing.Type
2121
Deprecated bool // if has been deprecated
2222
Suffix bool // GoName conflicts so add suffix to this function
@@ -28,28 +28,28 @@ type Function struct {
2828
}
2929

3030
// GoArgs return go function args
31-
func (f *Function) GoArgs() string {
31+
func (f *Function) GoArgs(currentModule *modules.Module) string {
3232
var args []string
33-
for _, p := range f.Params {
34-
args = append(args, p.GoName())
33+
for _, p := range f.Parameters {
34+
args = append(args, fmt.Sprintf("%s %s", p.Name, p.Type.GoName(currentModule, true)))
3535
}
3636
return strings.Join(args, ", ")
3737
}
3838

3939
// GoReturn return go function return
40-
func (f *Function) GoReturn() string {
40+
func (f *Function) GoReturn(currentModule *modules.Module) string {
4141
if f.ReturnType == nil {
4242
return ""
4343
}
44-
return f.ReturnType.GoName(nil, true)
44+
return f.ReturnType.GoName(currentModule, true)
4545
}
4646

4747
// Selector return full Objc function name
4848
func (f *Function) Selector() string {
4949
if f.identifier == "" {
5050
var sb strings.Builder
5151
sb.WriteString(f.Name)
52-
for idx, p := range f.Params {
52+
for idx, p := range f.Parameters {
5353
if idx > 0 {
5454
sb.WriteString(p.FieldName)
5555
}
@@ -69,7 +69,7 @@ func (f *Function) NormalizeInstanceTypeFunction(returnType *typing.ClassType) *
6969
nm := &Function{
7070
Name: f.Name,
7171
GoName: f.GoName,
72-
Params: f.Params,
72+
Parameters: f.Parameters,
7373
ReturnType: returnType,
7474
goFuncName: f.goFuncName,
7575
Suffix: f.Suffix,
@@ -107,7 +107,7 @@ func (f *Function) WriteGoCallCode(currentModule *modules.Module, typeName strin
107107
}
108108
callCode := fmt.Sprintf("objc.Call[%s](%s, objc.Sel(\"%s\")", returnTypeStr, receiver, f.Selector())
109109
var sb strings.Builder
110-
for idx, p := range f.Params {
110+
for idx, p := range f.Parameters {
111111
sb.WriteString(", ")
112112
switch tt := p.Type.(type) {
113113
case *typing.ClassType:
@@ -154,7 +154,7 @@ func (f *Function) WriteGoInterfaceCode(currentModule *modules.Module, classType
154154
// GoFuncDeclare generate go function declaration
155155
func (f *Function) GoFuncDeclare(currentModule *modules.Module, goTypeName string) string {
156156
var paramStrs []string
157-
for _, p := range f.Params {
157+
for _, p := range f.Parameters {
158158
paramStrs = append(paramStrs, p.GoDeclare(currentModule, false))
159159
}
160160

@@ -167,11 +167,11 @@ func (f *Function) GoFuncName() string {
167167
if f.goFuncName == "" {
168168
var sb strings.Builder
169169
name := f.GoName
170-
if len(f.Params) == 0 {
170+
if len(f.Parameters) == 0 {
171171
sb.WriteString(stringx.Capitalize(name))
172172
}
173173

174-
for _, p := range f.Params {
174+
for _, p := range f.Parameters {
175175
sb.WriteString(stringx.Capitalize(p.FieldName))
176176
if p.Object {
177177
sb.WriteString("Object")
@@ -189,7 +189,7 @@ func (f *Function) GoFuncName() string {
189189
// ProtocolGoFuncFieldType generate go function declaration for protocol struct impl field
190190
func (f *Function) ProtocolGoFuncFieldType(currentModule *modules.Module) string {
191191
var paramStrs []string
192-
for _, p := range f.Params {
192+
for _, p := range f.Parameters {
193193
paramStrs = append(paramStrs, p.GoDeclare(currentModule, true))
194194
}
195195

@@ -201,7 +201,7 @@ func (f *Function) ProtocolGoFuncName() string {
201201
if f.goFuncName == "" {
202202
var sb strings.Builder
203203
sb.WriteString(stringx.Capitalize(f.Name))
204-
for idx, p := range f.Params {
204+
for idx, p := range f.Parameters {
205205
if idx == 0 {
206206
continue
207207
}
@@ -222,7 +222,7 @@ func (f *Function) ProtocolGoFuncName() string {
222222
// GoImports return all imports for go file
223223
func (f *Function) GoImports() set.Set[string] {
224224
var imports = set.New("github.com/progrium/macdriver/objc")
225-
for _, param := range f.Params {
225+
for _, param := range f.Parameters {
226226
imports.AddSet(param.Type.GoImports())
227227
}
228228
if f.ReturnType != nil {
@@ -232,7 +232,7 @@ func (f *Function) GoImports() set.Set[string] {
232232
}
233233

234234
func (f *Function) HasProtocolParam() bool {
235-
for _, p := range f.Params {
235+
for _, p := range f.Parameters {
236236
switch p.Type.(type) {
237237
case *typing.ProtocolType:
238238
return true
@@ -242,8 +242,8 @@ func (f *Function) HasProtocolParam() bool {
242242
}
243243

244244
func (f *Function) ToProtocolParamAsObjectFunction() *Function {
245-
var newParams = make([]*Param, len(f.Params))
246-
for i, p := range f.Params {
245+
var newParams = make([]*Param, len(f.Parameters))
246+
for i, p := range f.Parameters {
247247
switch p.Type.(type) {
248248
case *typing.ProtocolType:
249249
newParams[i] = &Param{
@@ -259,7 +259,7 @@ func (f *Function) ToProtocolParamAsObjectFunction() *Function {
259259
return &Function{
260260
Name: f.Name,
261261
GoName: f.GoName,
262-
Params: newParams,
262+
Parameters: newParams,
263263
Suffix: f.Suffix,
264264
ReturnType: f.ReturnType,
265265
Deprecated: f.Deprecated,

generate/codegen/modulewriter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (m *ModuleWriter) WriteFunctions() {
119119
cw.WriteLine(fmt.Sprintf("// %s [Full Topic]", fa.Description))
120120
cw.WriteLine(fmt.Sprintf("//\n// [Full Topic]: %s", fa.DocURL))
121121
}
122-
cw.WriteLineF("// func %s(%s) %s", fa.Name, fa.GoArgs(), fa.GoReturn())
122+
cw.WriteLineF("// func %s(%s) %s {}", fa.GoName, fa.GoArgs(&m.Module), fa.GoReturn(&m.Module))
123123
}
124124
}
125125

generate/function.go

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,90 @@ import (
44
"fmt"
55

66
"github.com/progrium/macdriver/generate/codegen"
7-
"github.com/progrium/macdriver/generate/modules"
87
"github.com/progrium/macdriver/generate/typing"
98
)
109

1110
func (db *Generator) ToFunction(fw string, sym Symbol) *codegen.Function {
11+
// these functions have known declparse failures
12+
knownIssues := map[string]bool{
13+
"AUListenerAddParameter": true,
14+
"MIDIClientCreate": true,
15+
"AudioDeviceGetProperty": true,
16+
"AudioFileComponentOpenWithCallbacks": true,
17+
"AudioObjectSetPropertyData": true,
18+
"AudioUnitRemoveRenderNotify": true,
19+
"CFBagRemoveValue": true,
20+
"CFBinaryHeapGetMinimumIfPresent": true,
21+
"CFURLCopyResourcePropertyForKey": true,
22+
"CGBitmapContextCreate": true,
23+
"CGBitmapContextCreateWithData": true,
24+
"CGColorSpaceCreateWithPlatformColorSpace": true,
25+
"CGConvertColorDataWithFormat": true,
26+
"CGDataConsumerCreate": true,
27+
"CGDataProviderCreateDirect": true,
28+
"CGDataProviderCreateSequential": true,
29+
"CGDataProviderCreateWithData": true,
30+
"CGDisplayRegisterReconfigurationCallback": true,
31+
"CGDisplayRemoveReconfigurationCallback": true,
32+
"CGEventPostToPSN": true,
33+
"CGEventTapCreate": true,
34+
"CGEventTapCreateForPSN": true,
35+
"CGEventTapCreateForPid": true,
36+
"CGFontCreateWithPlatformFont": true,
37+
"CGFunctionCreate": true,
38+
"CGPDFArrayApplyBlock": true,
39+
"CGPDFDictionaryApplyBlock": true,
40+
"CGPDFDictionaryApplyFunction": true,
41+
"CGPDFObjectGetValue": true,
42+
"CGPDFScannerCreate": true,
43+
"CGPSConverterCreate": true,
44+
"CGPathApply": true,
45+
"CGPatternCreate": true,
46+
"CGRegisterScreenRefreshCallback": true,
47+
"CGScreenRegisterMoveCallback": true,
48+
"CGScreenUnregisterMoveCallback": true,
49+
"CGUnregisterScreenRefreshCallback": true,
50+
"CMBlockBufferAppendMemoryBlock": true,
51+
"CMIOStreamClockCreate": true,
52+
"MIDIEndpointSetRefCons": true,
53+
"NSBeginAlertSheet": true,
54+
"NSBeginCriticalAlertSheet": true,
55+
"NSShowAnimationEffect": true,
56+
"NSZoneFromPointer": true,
57+
"OBEXAddHTTPHeader": true,
58+
"OBEXAddUserDefinedHeader": true,
59+
}
60+
if knownIssues[sym.Name] {
61+
return nil
62+
}
1263
typ := db.TypeFromSymbol(sym)
13-
fmt.Println("typ:", typ)
14-
type_ := &typing.FunctionType{
15-
Name: sym.Name,
16-
GName: modules.TrimPrefix(sym.Name),
17-
Module: modules.Get(fw),
64+
fntyp, ok := typ.(*typing.FunctionType)
65+
if !ok {
66+
return nil
1867
}
1968
fn := &codegen.Function{
69+
Name: sym.Name,
70+
GoName: sym.Name,
2071
Description: sym.Description,
2172
DocURL: sym.DocURL(),
22-
Type: type_,
73+
Type: fntyp,
2374
}
24-
for _, arg := range sym.Parameters {
25-
fn.Params = append(fn.Params, &codegen.Param{
26-
Name: arg.Name,
75+
// populate params:
76+
for _, p := range fntyp.Parameters {
77+
if p.Type == nil {
78+
fmt.Printf("skipping %s: %s because of nil type\n", sym.Name, p.Name)
79+
continue
80+
}
81+
fn.Parameters = append(fn.Parameters, &codegen.Param{
82+
Name: p.Name,
83+
Type: p.Type,
2784
})
2885
}
86+
// populate return type
87+
if fntyp.ReturnType != nil {
88+
fn.ReturnType = fntyp.ReturnType
89+
}
90+
2991
return fn
3092

3193
}

generate/generator.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ func (db *Generator) Generate(platform string, version int, rootDir string, fram
107107
continue
108108
}
109109
case "Function":
110-
mw.Functions = append(mw.Functions, db.ToFunction(framework, s))
110+
fn := db.ToFunction(framework, s)
111+
if fn == nil {
112+
log.Println("skipping function", s.Name)
113+
continue
114+
}
115+
mw.Functions = append(mw.Functions, fn)
111116
}
112117
}
113118
mw.WriteCode()

generate/modules/modules.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func CanIgnoreNotFound(p any) bool {
100100
"QuickLook",
101101
"force feedback",
102102
"opengl es",
103+
"ColorSync",
103104
} {
104105
if strings.EqualFold(m, mod) {
105106
return true

generate/types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,28 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
9494
Module: modules.Get(module),
9595
}
9696
case "Function":
97+
typ, err := sym.Parse()
98+
if err != nil {
99+
fmt.Printf("TypeFromSymbol: failed to parse %s: %s\n", sym.Declaration, err)
100+
panic("bad function")
101+
}
102+
fn := typ.Function
103+
if fn == nil {
104+
fmt.Printf("TypeFromSymbol: name=%s declaration=%s\n", sym.Name, sym.Declaration)
105+
panic("bad function")
106+
}
97107
ft := &typing.FunctionType{
98108
Name: sym.Name,
99109
GName: modules.TrimPrefix(sym.Name),
100110
Module: modules.Get(module),
101111
}
112+
for _, arg := range fn.Args {
113+
ft.Parameters = append(ft.Parameters, typing.Parameter{
114+
Name: arg.Name,
115+
Type: db.ParseType(arg.Type),
116+
})
117+
}
118+
ft.ReturnType = db.ParseType(fn.ReturnType)
102119
// TODO: parse function params and return type from declaration
103120
return ft
104121
default:

generate/typing/function_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type FunctionType struct {
1919
Module *modules.Module // object-c module
2020

2121
Parameters []Parameter // function parameters
22-
Return Type // function return type
22+
ReturnType Type // function return type
2323
}
2424

2525
var Function = &FunctionType{

0 commit comments

Comments
 (0)