Skip to content

Commit c7397a7

Browse files
committed
generate: Progress on functions
1 parent 3765e86 commit c7397a7

File tree

6 files changed

+7625
-17
lines changed

6 files changed

+7625
-17
lines changed

generate/codegen/gen_function.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,32 @@ type Function struct {
2727
identifier string
2828
}
2929

30+
var reservedWords = map[string]bool{
31+
"func": true,
32+
"map": true,
33+
"new": true,
34+
"var": true,
35+
"len": true,
36+
"copy": true,
37+
"range": true,
38+
"type": true,
39+
}
40+
3041
// GoArgs return go function args
3142
func (f *Function) GoArgs(currentModule *modules.Module) string {
32-
// log.Println("rendering function", f.Name)
43+
log.Println("rendering function", f.Name)
3344
var args []string
45+
var blankArgCounter = 0
3446
for _, p := range f.Parameters {
3547
log.Println("rendering function", f.Name, p.Name, p.Type)
3648
log.Printf("rendering function ptype: %T", p.Type)
37-
if pt, ok := p.Type.(*typing.PointerType); ok {
38-
log.Printf("ptr type: %T", pt.Type)
49+
// if is reserved word, add _ suffix
50+
if _, ok := reservedWords[p.Name]; ok {
51+
p.Name = p.Name + "_"
52+
}
53+
if p.Name == "" {
54+
p.Name = fmt.Sprintf("arg%d", blankArgCounter)
55+
blankArgCounter++
3956
}
4057
args = append(args, fmt.Sprintf("%s %s", p.Name, p.Type.GoName(currentModule, true)))
4158
}
@@ -47,7 +64,7 @@ func (f *Function) GoReturn(currentModule *modules.Module) string {
4764
if f.ReturnType == nil {
4865
return ""
4966
}
50-
log.Printf("rendering GoReturn function return: %s %T", f.ReturnType, f.ReturnType)
67+
// log.Printf("rendering GoReturn function return: %s %T", f.ReturnType, f.ReturnType)
5168
return f.ReturnType.GoName(currentModule, true)
5269
}
5370

@@ -56,7 +73,7 @@ func (f *Function) CArgs(currentModule *modules.Module) string {
5673
// log.Println("rendering function", f.Name)
5774
var args []string
5875
for _, p := range f.Parameters {
59-
log.Printf("rendering cfunction arg: %s %s %T", p.Name, p.Type, p.Type)
76+
// log.Printf("rendering cfunction arg: %s %s %T", p.Name, p.Type, p.Type)
6077
args = append(args, fmt.Sprintf("%s %s", p.Type.CName(), p.Name))
6178
}
6279
return strings.Join(args, ", ")
@@ -86,6 +103,11 @@ func (f *Function) String() string {
86103
func (f *Function) WriteGoCallCode(currentModule *modules.Module, cw *CodeWriter) {
87104
funcDeclare := f.GoFuncDeclare(currentModule)
88105

106+
if hasBlockParam(f.Parameters) {
107+
cw.WriteLineF("// // TODO: %v not implemented (missing block param support)", f.Name)
108+
return
109+
}
110+
89111
if f.Deprecated {
90112
return
91113
cw.WriteLine("// deprecated")
@@ -128,7 +150,26 @@ func (f *Function) WriteGoCallCode(currentModule *modules.Module, cw *CodeWriter
128150
cw.WriteLine("}")
129151
}
130152

153+
func hasBlockParam(params []*Param) bool {
154+
for _, p := range params {
155+
if _, ok := p.Type.(*typing.BlockType); ok {
156+
return true
157+
}
158+
if pt, ok := p.Type.(*typing.AliasType); ok {
159+
t := typing.UnwrapAlias(pt.Type)
160+
if _, ok := t.(*typing.BlockType); ok {
161+
return true
162+
}
163+
}
164+
}
165+
return false
166+
}
167+
131168
func (f *Function) WriteObjcWrapper(currentModule *modules.Module, cw *CodeWriter) {
169+
if hasBlockParam(f.Parameters) {
170+
cw.WriteLineF("// // TODO: %v not implemented (missing block param support)", f.Name)
171+
return
172+
}
132173
if f.Deprecated {
133174
return
134175
cw.WriteLine("// deprecated")
@@ -149,6 +190,17 @@ func (f *Function) WriteCSignature(currentModule *modules.Module, cw *CodeWriter
149190
var returnTypeStr string
150191
rt := f.Type.ReturnType
151192
returnTypeStr = rt.CName()
193+
if v, ok := map[string]string{
194+
"NSInteger": "int",
195+
"NSUInteger": "uint",
196+
"BOOL": "bool",
197+
}[returnTypeStr]; ok {
198+
returnTypeStr = v
199+
}
200+
if hasBlockParam(f.Parameters) {
201+
cw.WriteLineF("// // TODO: %v not implemented (missing block param support)", f.Name)
202+
return
203+
}
152204
cw.WriteLineF("// %v %v(%v); ", returnTypeStr, f.GoName, f.CArgs(currentModule))
153205
}
154206

generate/codegen/modulewriter.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ func (m *ModuleWriter) WriteStructs() {
139139
}
140140
}
141141

142+
func shouldSkipFunction(f *Function) bool {
143+
if f.Deprecated {
144+
return true
145+
}
146+
if hasBlockParam(f.Parameters) {
147+
return true
148+
}
149+
if _, ok := map[string]bool{
150+
"CGDirectDisplayCopyCurrentMetalDevice": true,
151+
}[f.Name]; ok {
152+
return true
153+
}
154+
return false
155+
}
156+
142157
// WriteFunctions writes the go code to call exposed functions.
143158
func (m *ModuleWriter) WriteFunctions() {
144159
if len(m.Functions) == 0 {
@@ -163,6 +178,9 @@ func (m *ModuleWriter) WriteFunctions() {
163178
// #import <stdbool.h>
164179
// #import "%s"`, m.Module.Header)
165180
for _, f := range m.Functions {
181+
if shouldSkipFunction(f) {
182+
continue
183+
}
166184
f.WriteCSignature(&m.Module, cw)
167185
}
168186
cw.WriteLine(`import "C"`)
@@ -175,6 +193,10 @@ func (m *ModuleWriter) WriteFunctions() {
175193
}
176194
cw.Indent()
177195
imports.ForEach(func(value string) {
196+
if value == "github.com/progrium/macdriver/macos/"+m.Module.Package {
197+
return
198+
}
199+
// avoid cycles:
178200
if value != "github.com/progrium/macdriver/macos/objc" {
179201
cw.WriteLine("\"" + value + "\"")
180202
}
@@ -183,6 +205,9 @@ func (m *ModuleWriter) WriteFunctions() {
183205
cw.WriteLine(")")
184206

185207
for _, f := range m.Functions {
208+
if shouldSkipFunction(f) {
209+
continue
210+
}
186211
f.WriteGoCallCode(&m.Module, cw)
187212
}
188213
}

generate/types.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
111111
Module: modules.Get(module),
112112
}
113113
case "Function":
114-
if sym.Name != "CGDisplayCreateImage" &&
115-
sym.Name != "CGMainDisplayID" {
116-
return nil
117-
}
118114
typ, err := sym.Parse()
119115
if err != nil {
120116
fmt.Printf("TypeFromSymbol: failed to parse %s: %s\n", sym.Declaration, err)

generate/typing/block_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (a *BlockType) ObjcName() string {
7676
}
7777

7878
func (a *BlockType) CName() string {
79-
return a.ObjcName()
79+
return "implement me"
8080
}
8181

8282
func (a *BlockType) DeclareModule() *modules.Module {

0 commit comments

Comments
 (0)