Skip to content

Commit bba7468

Browse files
committed
generate: iterate on function support
1 parent f880f6c commit bba7468

File tree

8 files changed

+85
-32
lines changed

8 files changed

+85
-32
lines changed

generate/codegen/gen_function.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (f *Function) GoReturn(currentModule *modules.Module) string {
4747
if f.ReturnType == nil {
4848
return ""
4949
}
50+
log.Printf("rendering GoReturn function return: %s %T", f.ReturnType, f.ReturnType)
5051
return f.ReturnType.GoName(currentModule, true)
5152
}
5253

@@ -127,10 +128,26 @@ func (f *Function) WriteGoCallCode(currentModule *modules.Module, cw *CodeWriter
127128
cw.WriteLine("}")
128129
}
129130

131+
func (f *Function) WriteObjcWrapper(currentModule *modules.Module, cw *CodeWriter) {
132+
if f.Deprecated {
133+
return
134+
cw.WriteLine("// deprecated")
135+
}
136+
returnTypeStr := f.Type.ReturnType.CName()
137+
cw.WriteLineF("%v %v(%v) {", returnTypeStr, f.GoName, f.CArgs(currentModule))
138+
cw.Indent()
139+
var args []string
140+
for _, p := range f.Parameters {
141+
args = append(args, p.Name)
142+
}
143+
cw.WriteLineF("return %v(%v);", f.Type.Name, strings.Join(args, ", "))
144+
cw.UnIndent()
145+
cw.WriteLine("}")
146+
}
147+
130148
func (f *Function) WriteCSignature(currentModule *modules.Module, cw *CodeWriter) {
131149
var returnTypeStr string
132150
rt := f.Type.ReturnType
133-
log.Printf("rt: %T", rt)
134151
returnTypeStr = rt.CName()
135152
cw.WriteLineF("// %v %v(%v); ", returnTypeStr, f.GoName, f.CArgs(currentModule))
136153
}

generate/codegen/gen_struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// Struct is code generator for objective-c struct
88
type Struct struct {
9-
Type *typing.StructType
9+
Type typing.Type
1010
Name string // the first part of objc function name
1111
GoName string
1212
Deprecated bool // if has been deprecated

generate/codegen/modulewriter.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (m *ModuleWriter) WriteCode() {
3333
m.WriteTypeAliases()
3434
m.WriteStructs()
3535
m.WriteFunctions()
36+
m.WriteFunctionWrappers()
3637
if m.Module.Package == "coreimage" {
3738
// filter protocols maybe arent "real" protocols?
3839
// get "cannot find protocol declaration" with protocol imports
@@ -125,19 +126,20 @@ func (m *ModuleWriter) WriteStructs() {
125126
cw.WriteLine(")")
126127

127128
for _, s := range m.Structs {
128-
if s.DocURL != "" {
129-
cw.WriteLine(fmt.Sprintf("// %s [Full Topic]", s.Description))
130-
cw.WriteLine(fmt.Sprintf("//\n// [Full Topic]: %s", s.DocURL))
131-
}
132-
133129
// if Ref type, allias to unsafe.Pointer
134130
if strings.HasSuffix(s.Name, "Ref") {
131+
if s.DocURL != "" {
132+
cw.WriteLine(fmt.Sprintf("// %s [Full Topic]", s.Description))
133+
cw.WriteLine(fmt.Sprintf("//\n// [Full Topic]: %s", s.DocURL))
134+
}
135+
135136
cw.WriteLineF("type %s unsafe.Pointer", s.GoName)
136137
continue
137138
}
138139
}
139140
}
140141

142+
// WriteFunctions writes the go code to call exposed functions.
141143
func (m *ModuleWriter) WriteFunctions() {
142144
if len(m.Functions) == 0 {
143145
return
@@ -156,10 +158,10 @@ func (m *ModuleWriter) WriteFunctions() {
156158
cw.WriteLine("package " + m.Module.Package)
157159

158160
//TODO: determine imports from functions
159-
cw.WriteLine(`// #import <stdlib.h>
160-
// #import <stdint.h>
161-
// #import <stdbool.h>
162-
// #import <CoreGraphics/CGGeometry.h>`)
161+
cw.WriteLineF(`// #import <stdlib.h>
162+
// #import <stdint.h>
163+
// #import <stdbool.h>
164+
// #import "%s"`, m.Module.Header)
163165
for _, f := range m.Functions {
164166
f.WriteCSignature(&m.Module, cw)
165167
}
@@ -185,6 +187,32 @@ func (m *ModuleWriter) WriteFunctions() {
185187
}
186188
}
187189

190+
// WriteFunctionWrappers writes the objc code to wrap exposed functions.
191+
// The cgo type system is unaware of objective c types so these wrappers must exist to allow
192+
// us to call the functions and return appropritely.
193+
func (m *ModuleWriter) WriteFunctionWrappers() {
194+
if len(m.Functions) == 0 {
195+
return
196+
}
197+
198+
filePath := filepath.Join(m.PlatformDir, m.Module.Package, "functions.gen.m")
199+
os.MkdirAll(filepath.Dir(filePath), 0755)
200+
f, err := os.Create(filePath)
201+
if err != nil {
202+
panic(err)
203+
}
204+
defer f.Close()
205+
206+
cw := &CodeWriter{Writer: f, IndentStr: "\t"}
207+
cw.WriteLine(AutoGeneratedMark)
208+
209+
//TODO: determine appropriate imports
210+
cw.WriteLineF("#import \"%s\"", m.Module.Header)
211+
for _, f := range m.Functions {
212+
f.WriteObjcWrapper(&m.Module, cw)
213+
}
214+
}
215+
188216
func (m *ModuleWriter) WriteEnumAliases() {
189217
enums := make([]*AliasInfo, len(m.EnumAliases))
190218
copy(enums, m.EnumAliases)

generate/function.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66

77
"github.com/progrium/macdriver/generate/codegen"
8+
"github.com/progrium/macdriver/generate/modules"
89
"github.com/progrium/macdriver/generate/typing"
910
)
1011

@@ -253,7 +254,7 @@ func (db *Generator) ToFunction(fw string, sym Symbol) *codegen.Function {
253254
}
254255
fn := &codegen.Function{
255256
Name: sym.Name,
256-
GoName: sym.Name,
257+
GoName: modules.TrimPrefix(sym.Name),
257258
Description: sym.Description,
258259
DocURL: sym.DocURL(),
259260
Type: fntyp,

generate/generator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ func (db *Generator) Generate(platform string, version int, rootDir string, fram
144144
}
145145
mw.Functions = append(mw.Functions, fn)
146146
case "Struct":
147-
fn := db.ToStruct(framework, s)
148-
if fn == nil {
147+
s := db.ToStruct(framework, s)
148+
if s == nil {
149149
continue
150150
}
151-
mw.Structs = append(mw.Structs, fn)
151+
mw.Structs = append(mw.Structs, s)
152152
}
153153
}
154154
mw.WriteCode()

generate/struct.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

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

1110
func (db *Generator) ToStruct(fw string, sym Symbol) *codegen.Struct {
@@ -16,16 +15,12 @@ func (db *Generator) ToStruct(fw string, sym Symbol) *codegen.Struct {
1615
return nil
1716
}
1817
typ := db.TypeFromSymbol(sym)
19-
styp, ok := typ.(*typing.StructType)
20-
if !ok {
21-
return nil
22-
}
2318
s := &codegen.Struct{
2419
Name: sym.Name,
2520
GoName: modules.TrimPrefix(sym.Name),
2621
Description: sym.Description,
2722
DocURL: sym.DocURL(),
28-
Type: styp,
23+
Type: typ,
2924
}
3025

3126
return s

generate/types.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
5454
}
5555
case "Union":
5656
return &typing.RefType{
57-
Name: sym.Name,
57+
Name: sym.Name,
58+
GName: modules.TrimPrefix(sym.Name),
5859
}
5960
case "Type":
6061
if sym.Type != "Type Alias" {
@@ -66,7 +67,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
6667
// sym.Name == "NSZone" ||
6768
sym.Name == "MusicSequence" {
6869
return &typing.RefType{
69-
Name: sym.Name,
70+
Name: sym.Name,
71+
GName: modules.TrimPrefix(sym.Name),
7072
}
7173
}
7274
st, err := sym.Parse()
@@ -76,7 +78,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
7678
}
7779
if st.Struct != nil {
7880
return &typing.RefType{
79-
Name: st.Struct.Name,
81+
Name: st.Struct.Name,
82+
GName: modules.TrimPrefix(sym.Name),
8083
}
8184
}
8285
if st.TypeAlias == nil {
@@ -97,7 +100,9 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
97100
case "Struct":
98101
if strings.HasSuffix(sym.Name, "Ref") {
99102
return &typing.RefType{
100-
Name: sym.Name,
103+
Name: sym.Name,
104+
GName: modules.TrimPrefix(sym.Name),
105+
Module: modules.Get(module),
101106
}
102107
}
103108
return &typing.StructType{
@@ -106,7 +111,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
106111
Module: modules.Get(module),
107112
}
108113
case "Function":
109-
if sym.Name != "CGDisplayCreateImage" {
114+
if sym.Name != "CGDisplayCreateImage" &&
115+
sym.Name != "CGMainDisplayID" {
110116
return nil
111117
}
112118
typ, err := sym.Parse()

generate/typing/ref_type.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ import (
88

99
// for weird struct refs like those ending in "Ref"
1010
type RefType struct {
11-
Name string // c and objc type name
12-
// GName string // the go struct name
13-
// Module *modules.Module // the module
11+
Name string // c and objc type name
12+
GName string // the go struct name
13+
Module *modules.Module // the module
1414
}
1515

1616
func (s *RefType) GoImports() set.Set[string] {
17-
return set.New("unsafe")
17+
if s.Module == nil {
18+
return set.New("unsafe")
19+
}
20+
return set.New("github.com/progrium/macdriver/macos/" + s.Module.Package)
1821
}
1922

2023
func (s *RefType) GoName(currentModule *modules.Module, receiveFromObjc bool) string {
21-
return "unsafe.Pointer"
24+
if s.Module == nil {
25+
return "unsafe.Pointer"
26+
}
27+
return FullGoName(*s.Module, s.GName, *currentModule)
2228
}
2329

2430
func (s *RefType) ObjcName() string {
@@ -30,5 +36,5 @@ func (s *RefType) CName() string {
3036
}
3137

3238
func (s *RefType) DeclareModule() *modules.Module {
33-
return nil
39+
return s.Module
3440
}

0 commit comments

Comments
 (0)