Debian Codegraph @

bind/genobjcw.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package bind
6
7 import (
8 "path"
9 "strings"
10
11 "golang.org/x/mobile/internal/importers/objc"
12 )
13
14 type (
15 // ObjCWrapper generates Go and C stubs for ObjC interfaces and protocols.
16 ObjcWrapper struct {
17 *Printer
18 imported map[string]*objc.Named
19 // The list of ObjC types.
20 types []*objc.Named
21 // The list of Go package paths with ObjC wrappers.
22 pkgNames []string
23 modules []string
24 // For each ObjC module, the list of ObjC types within.
25 modMap map[string][]*objc.Named
26 // For each module/name Go package path, the ObjC type
27 // with static functions or constants.
28 typePkgs map[string]*objc.Named
29 // supers is the map of types that need Super methods.
30 supers map[string]struct{}
31 }
32 )
33
34 // Init initializes the ObjC types wrapper generator. Types is the
35 // list of types to wrap, genNames the list of generated type names.
36 func (g *ObjcWrapper) Init(types []*objc.Named, genNames []string) {
37 g.supers = make(map[string]struct{})
38 for _, s := range genNames {
39 g.supers[s] = struct{}{}
40 }
41 g.types = types
42 g.imported = make(map[string]*objc.Named)
43 g.modMap = make(map[string][]*objc.Named)
44 g.typePkgs = make(map[string]*objc.Named)
45 pkgSet := make(map[string]struct{})
46 for _, n := range types {
47 g.imported[n.GoName] = n
48 typePkg := n.Module + "/" + n.GoName
49 g.typePkgs[typePkg] = n
50 if !n.Generated {
51 if _, exists := g.modMap[n.Module]; !exists {
52 g.modules = append(g.modules, n.Module)
53 }
54 }
55 g.modMap[n.Module] = append(g.modMap[n.Module], n)
56 if _, exists := pkgSet[n.Module]; !exists {
57 pkgSet[n.Module] = struct{}{}
58 g.pkgNames = append(g.pkgNames, n.Module)
59 }
60 g.pkgNames = append(g.pkgNames, typePkg)
61 }
62 }
63
64 func (g *ObjcWrapper) GenM() {
65 g.Printf(gobindPreamble)
66 // For objc_msgSend* functions.
67 g.Printf("@import ObjectiveC.message;\n")
68 g.Printf("#include \"seq.h\"\n")
69 g.Printf("#include \"interfaces.h\"\n\n")
70 for _, n := range g.types {
71 g.genM(n)
72 }
73 g.Printf("\n")
74 for _, n := range g.types {
75 for _, f := range n.AllMethods {
76 if !g.isFuncSupported(f) {
77 continue
78 }
79 g.genCFuncDecl("cproxy", n.GoName, f)
80 g.genCFuncBody(n, f, false)
81 if _, exists := g.supers[n.GoName]; exists {
82 g.genCFuncDecl("csuper", n.GoName, f)
83 g.genCFuncBody(n, f, true)
84 }
85 }
86 }
87 }
88
89 func (g *ObjcWrapper) genCFuncBody(n *objc.Named, f *objc.Func, super bool) {
90 g.Printf(" {\n")
91 g.Indent()
92 if !f.Static {
93 g.Printf("%s _this = go_seq_from_refnum(this).obj;\n", n.ObjcType())
94 }
95 var errParam *objc.Param
96 for i, a := range f.Params {
97 if i == len(f.Params)-1 && g.isErrorType(a.Type) {
98 errParam = a
99 break
100 }
101 g.genCToObjC(a.Name, a.Type, modeTransient)
102 }
103 if errParam != nil {
104 g.Printf("NSError *%s = nil;\n", errParam.Name)
105 }
106 if f.Constructor {
107 g.Printf("%s _this = [%s alloc];\n", n.ObjcType(), n.Name)
108 }
109 if super {
110 g.Printf("struct objc_super _super = {\n")
111 g.Printf(" .receiver = _this,\n")
112 g.Printf(" .super_class = class_getSuperclass([%s class]),\n", n.Name)
113 g.Printf("};\n")
114 }
115 retType := "void"
116 if f.Ret != nil {
117 retType = g.objcType(f.Ret)
118 g.Printf("%s res = ", retType)
119 }
120 // There is no direct way to send a message to a class' super
121 // class from outside the class itself. Use objc_msgSendSuper instead
122 // which is what the compiler uses itself. To keep us honest and to exercise
123 // the code paths more use objc_msgSend for regular calls as well.
124 //
125 // A regular call looks like this:
126 //
127 // res = ((<return type> (*)(id, SEL, <argument_types>))objc_msgSend)(_this, @selector(...), <arguments>)
128 //
129 // a call to super looks like this:
130 //
131 // ret = ((<return type> (*)(id, SEL, <argument_types>))objc_msgSendSuper)(<struct objc_super>, <arguments>)
132 if f.Ret != nil {
133 switch f.Ret.Kind {
134 case objc.String, objc.Bool, objc.Data, objc.Int, objc.Uint, objc.Short, objc.Ushort, objc.Char, objc.Uchar, objc.Float, objc.Double, objc.Class, objc.Protocol:
135 default:
136 // If support for struct results is added, objc_msgSend_stret must be used
137 panic("unsupported type kind - use objc_msgSend_stret?")
138 }
139 }
140 g.Printf("((%s (*)(", retType)
141 if super {
142 g.Printf("struct objc_super *")
143 } else {
144 g.Printf("id")
145 }
146 g.Printf(", SEL")
147 for _, a := range f.Params {
148 g.Printf(", %s", g.objcType(a.Type))
149 }
150 g.Printf("))")
151 if super {
152 g.Printf("objc_msgSendSuper")
153 } else {
154 g.Printf("objc_msgSend")
155 }
156 g.Printf(")(")
157 if f.Static && !f.Constructor {
158 g.Printf("[%s class]", n.Name)
159 } else {
160 if super {
161 g.Printf("&_super")
162 } else {
163 g.Printf("_this")
164 }
165 }
166 g.Printf(", @selector(%s)", f.Sig)
167 for _, a := range f.Params {
168 arg := "_" + a.Name
169 if a == errParam {
170 arg = "&" + a.Name
171 }
172 g.Printf(", %s", arg)
173 }
174 g.Printf(");\n")
175 if errParam != nil {
176 g.Printf("NSError *_%s = nil;\n", errParam.Name)
177 if f.Ret != nil {
178 g.Printf("if (!res && %s != nil) {\n", errParam.Name)
179 } else {
180 g.Printf("if (%s != nil) {\n", errParam.Name)
181 }
182 g.Printf(" _%[1]s = %[1]s;\n", errParam.Name)
183 g.Printf("}\n")
184 g.genObjCToC("_"+errParam.Name, g.errType(), modeRetained)
185 }
186 ret := f.Ret
187 if ret != nil && ret.Kind == objc.Bool && errParam != nil {
188 ret = nil
189 }
190 if ret != nil {
191 g.genObjCToC("res", ret, modeRetained)
192 }
193 switch {
194 case ret != nil && errParam != nil:
195 stype := strings.Replace(g.cType(ret), " ", "_", -1)
196 g.Printf("ret_%s _sres = {_res, __%s};\n", stype, errParam.Name)
197 g.Printf("return _sres;\n")
198 case ret != nil:
199 g.Printf("return _res;\n")
200 case errParam != nil:
201 g.Printf("return __%s;\n", errParam.Name)
202 }
203 g.Outdent()
204 g.Printf("}\n\n")
205 }
206
207 func (_ *ObjcWrapper) errType() *objc.Type {
208 return &objc.Type{Kind: objc.Class, Name: "NSError"}
209 }
210
211 func (g *ObjcWrapper) genM(n *objc.Named) {
212 for _, f := range n.Funcs {
213 if !g.isFuncSupported(f) {
214 continue
215 }
216 g.genCFuncDecl("cproxy", n.GoName, f)
217 g.genCFuncBody(n, f, false)
218 }
219 }
220
221 func (g *ObjcWrapper) GenH() {
222 g.Printf(gobindPreamble)
223 g.Printf("#include \"seq.h\"\n\n")
224 for _, m := range g.modules {
225 g.Printf("@import %s;\n", m)
226 }
227 // Include header files for generated types
228 for _, n := range g.pkgNames {
229 hasGen := false
230 for _, t := range g.modMap[n] {
231 if t.Generated {
232 hasGen = true
233 break
234 }
235 }
236 if hasGen {
237 g.Printf("#import %q\n", n+".objc.h")
238 }
239 }
240 for _, tn := range []string{"int", "nstring", "nbyteslice", "long", "unsigned long", "short", "unsigned short", "bool", "char", "unsigned char", "float", "double"} {
241 sn := strings.Replace(tn, " ", "_", -1)
242 g.Printf("typedef struct ret_%s {\n", sn)
243 g.Printf(" %s res;\n", tn)
244 g.Printf(" int err;\n")
245 g.Printf("} ret_%s;\n", sn)
246 }
247 g.Printf("\n")
248 for _, n := range g.types {
249 for _, f := range n.AllMethods {
250 if !g.isFuncSupported(f) {
251 continue
252 }
253 g.Printf("extern ")
254 g.genCFuncDecl("cproxy", n.GoName, f)
255 g.Printf(";\n")
256 if _, exists := g.supers[n.GoName]; exists {
257 g.Printf("extern ")
258 g.genCFuncDecl("csuper", n.GoName, f)
259 g.Printf(";\n")
260 }
261 }
262 }
263 for _, cls := range g.types {
264 g.genH(cls)
265 }
266 }
267
268 func (g *ObjcWrapper) genH(n *objc.Named) {
269 for _, f := range n.Funcs {
270 if !g.isFuncSupported(f) {
271 continue
272 }
273 g.Printf("extern ")
274 g.genCFuncDecl("cproxy", n.GoName, f)
275 g.Printf(";\n")
276 }
277 }
278
279 func (g *ObjcWrapper) genCFuncDecl(prefix, name string, f *objc.Func) {
280 returnsErr := len(f.Params) > 0 && g.isErrorType(f.Params[len(f.Params)-1].Type)
281 ret := f.Ret
282 if ret != nil && returnsErr && ret.Kind == objc.Bool {
283 ret = nil
284 }
285 switch {
286 case ret != nil && returnsErr:
287 g.Printf("ret_%s", strings.Replace(g.cType(ret), " ", "_", -1))
288 case ret != nil:
289 g.Printf("%s", g.cType(ret))
290 case returnsErr:
291 g.Printf("int")
292 default:
293 g.Printf("void")
294 }
295 g.Printf(" %s", prefix)
296 if f.Static {
297 g.Printf("_s")
298 }
299 g.Printf("_%s_%s(", name, f.GoName)
300 if !f.Static {
301 g.Printf("int this")
302 }
303 for i, p := range f.Params {
304 if i == len(f.Params)-1 && returnsErr {
305 break
306 }
307 if !f.Static || i > 0 {
308 g.Printf(", ")
309 }
310 g.Printf("%s %s", g.cType(p.Type), p.Name)
311 }
312 g.Printf(")")
313 }
314
315 func (g *ObjcWrapper) GenGo() {
316 g.Printf(gobindPreamble)
317 g.Printf("package main\n\n")
318 g.Printf("// #include \"interfaces.h\"\n")
319 g.Printf("import \"C\"\n\n")
320 g.Printf("import \"ObjC\"\n")
321 g.Printf("import _seq \"golang.org/x/mobile/bind/seq\"\n")
322
323 for _, n := range g.types {
324 for _, f := range n.Funcs {
325 if g.isFuncSupported(f) {
326 pkgName := n.Module + "/" + n.GoName
327 g.Printf("import %q\n", "ObjC/"+pkgName)
328 break
329 }
330 }
331 }
332 g.Printf("\n")
333 g.Printf("type proxy interface { Bind_proxy_refnum__() int32 }\n\n")
334 g.Printf("// Suppress unused package error\n\n")
335 g.Printf("var _ = _seq.FromRefNum\n")
336 g.Printf("const _ = ObjC.Dummy\n\n")
337 for _, n := range g.types {
338 g.genGo(n)
339 }
340 }
341
342 func (g *ObjcWrapper) genGo(n *objc.Named) {
343 g.Printf("func init() {\n")
344 g.Indent()
345 for _, f := range n.Funcs {
346 if !g.isFuncSupported(f) {
347 continue
348 }
349 g.Printf("%s.%s = func", n.GoName, f.GoName)
350 g.genFuncDecl(false, f)
351 g.genFuncBody(n, f, "cproxy")
352 }
353 g.Outdent()
354 g.Printf("}\n\n")
355 g.Printf("type proxy_class_%s _seq.Ref\n\n", n.GoName)
356 g.Printf("func (p *proxy_class_%s) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }\n\n", n.GoName)
357 for _, f := range n.AllMethods {
358 if !g.isFuncSupported(f) {
359 continue
360 }
361 g.Printf("func (p *proxy_class_%s) %s", n.GoName, f.GoName)
362 g.genFuncDecl(false, f)
363 g.genFuncBody(n, f, "cproxy")
364 }
365 if _, exists := g.supers[n.GoName]; exists {
366 g.Printf("func (p *proxy_class_%s) Super() ObjC.%s {\n", n.GoName, n.Module+"_"+n.GoName)
367 g.Printf(" return &super_%s{p}\n", n.GoName)
368 g.Printf("}\n\n")
369 g.Printf("type super_%s struct {*proxy_class_%[1]s}\n\n", n.GoName)
370 for _, f := range n.AllMethods {
371 if !g.isFuncSupported(f) {
372 continue
373 }
374 g.Printf("func (p *super_%s) %s", n.GoName, f.GoName)
375 g.genFuncDecl(false, f)
376 g.genFuncBody(n, f, "csuper")
377 }
378 }
379 }
380
381 func (g *ObjcWrapper) genFuncBody(n *objc.Named, f *objc.Func, prefix string) {
382 g.Printf(" {\n")
383 g.Indent()
384 var errParam *objc.Param
385 for i, a := range f.Params {
386 if i == len(f.Params)-1 && g.isErrorType(a.Type) {
387 errParam = a
388 break
389 }
390 g.genWrite(a)
391 }
392 ret := f.Ret
393 if ret != nil && errParam != nil && ret.Kind == objc.Bool {
394 ret = nil
395 }
396 if ret != nil || errParam != nil {
397 g.Printf("res := ")
398 }
399 g.Printf("C.%s", prefix)
400 if f.Static {
401 g.Printf("_s")
402 }
403 g.Printf("_%s_%s(", n.GoName, f.GoName)
404 if !f.Static {
405 g.Printf("C.int(p.Bind_proxy_refnum__())")
406 }
407 for i, a := range f.Params {
408 if a == errParam {
409 break
410 }
411 if !f.Static || i > 0 {
412 g.Printf(", ")
413 }
414 g.Printf("_%s", a.Name)
415 }
416 g.Printf(")\n")
417 switch {
418 case ret != nil && errParam != nil:
419 g.genRead("_res", "res.res", ret)
420 g.genRefRead("_"+errParam.Name, "res.err", "error", "proxy_error")
421 g.Printf("return _res, _%s\n", errParam.Name)
422 case ret != nil:
423 g.genRead("_res", "res", ret)
424 g.Printf("return _res\n")
425 case errParam != nil:
426 g.genRefRead("_"+errParam.Name, "res", "error", "proxy_error")
427 g.Printf("return _%s\n", errParam.Name)
428 }
429 g.Outdent()
430 g.Printf("}\n\n")
431 }
432
433 func (g *ObjcWrapper) genCToObjC(name string, t *objc.Type, mode varMode) {
434 switch t.Kind {
435 case objc.String:
436 g.Printf("NSString *_%s = go_seq_to_objc_string(%s);\n", name, name)
437 case objc.Bool:
438 g.Printf("BOOL _%s = %s ? YES : NO;\n", name, name)
439 case objc.Data:
440 g.Printf("NSData *_%s = go_seq_to_objc_bytearray(%s, %d);\n", name, name, toCFlag(mode == modeRetained))
441 case objc.Int, objc.Uint, objc.Short, objc.Ushort, objc.Char, objc.Uchar, objc.Float, objc.Double:
442 g.Printf("%s _%s = (%s)%s;\n", g.objcType(t), name, g.objcType(t), name)
443 case objc.Class, objc.Protocol:
444 g.Printf("GoSeqRef* %s_ref = go_seq_from_refnum(%s);\n", name, name)
445 g.Printf("%s _%s;\n", g.objcType(t), name)
446 g.Printf("if (%s_ref != NULL) {\n", name)
447 g.Printf(" _%s = %s_ref.obj;\n", name, name)
448 g.Printf("}\n")
449 default:
450 panic("invalid kind")
451 }
452 }
453
454 func (g *ObjcWrapper) genObjCToC(name string, t *objc.Type, mode varMode) {
455 switch t.Kind {
456 case objc.String:
457 g.Printf("nstring _%s = go_seq_from_objc_string(%s);\n", name, name)
458 case objc.Data:
459 g.Printf("nbyteslice _%s = go_seq_from_objc_bytearray(%s, %d);\n", name, name, toCFlag(mode == modeRetained))
460 case objc.Bool, objc.Int, objc.Uint, objc.Short, objc.Ushort, objc.Char, objc.Uchar, objc.Float, objc.Double:
461 g.Printf("%s _%s = (%s)%s;\n", g.cType(t), name, g.cType(t), name)
462 case objc.Protocol, objc.Class:
463 g.Printf("int _%s = go_seq_to_refnum(%s);\n", name, name)
464 default:
465 panic("invalid kind")
466 }
467 }
468
469 func (g *ObjcWrapper) genWrite(a *objc.Param) {
470 switch a.Type.Kind {
471 case objc.String:
472 g.Printf("_%s := encodeString(%s)\n", a.Name, a.Name)
473 case objc.Data:
474 g.Printf("_%s := fromSlice(%s, false)\n", a.Name, a.Name)
475 case objc.Bool:
476 g.Printf("_%s := %s(0)\n", a.Name, g.cgoType(a.Type))
477 g.Printf("if %s {\n", a.Name)
478 g.Printf(" _%s = %s(1)\n", a.Name, g.cgoType(a.Type))
479 g.Printf("}\n")
480 case objc.Int, objc.Uint, objc.Short, objc.Ushort, objc.Char, objc.Uchar, objc.Float, objc.Double:
481 g.Printf("_%s := %s(%s)\n", a.Name, g.cgoType(a.Type), a.Name)
482 case objc.Protocol, objc.Class:
483 g.Printf("var _%s %s = _seq.NullRefNum\n", a.Name, g.cgoType(a.Type))
484 g.Printf("if %s != nil {\n", a.Name)
485 g.Printf(" _%s = %s(_seq.ToRefNum(%s))\n", a.Name, g.cgoType(a.Type), a.Name)
486 g.Printf("}\n")
487 default:
488 panic("invalid kind")
489 }
490 }
491
492 func (g *ObjcWrapper) genRead(to, from string, t *objc.Type) {
493 switch t.Kind {
494 case objc.Int, objc.Uint, objc.Uchar, objc.Short, objc.Ushort, objc.Char, objc.Float, objc.Double:
495 g.Printf("%s := %s(%s)\n", to, g.goType(t, false), from)
496 case objc.Bool:
497 g.Printf("%s := %s != 0\n", to, from)
498 case objc.String:
499 g.Printf("%s := decodeString(%s)\n", to, from)
500 case objc.Data:
501 g.Printf("%s := toSlice(%s, true)\n", to, from)
502 case objc.Protocol, objc.Class:
503 var proxyName string
504 if n := g.lookupImported(t); n != nil {
505 proxyName = "proxy_class_" + n.GoName
506 }
507 g.genRefRead(to, from, g.goType(t, false), proxyName)
508 default:
509 panic("invalid kind")
510 }
511 }
512
513 func (g *ObjcWrapper) genRefRead(to, from string, intfName, proxyName string) {
514 g.Printf("var %s %s\n", to, intfName)
515 g.Printf("%s_ref := _seq.FromRefNum(int32(%s))\n", to, from)
516 g.Printf("if %s_ref != nil {\n", to)
517 g.Printf(" if %s < 0 { // go object\n", from)
518 g.Printf(" %s = %s_ref.Get().(%s)\n", to, to, intfName)
519 if proxyName != "" {
520 g.Printf(" } else { // foreign object\n")
521 g.Printf(" %s = (*%s)(%s_ref)\n", to, proxyName, to)
522 }
523 g.Printf(" }\n")
524 g.Printf("}\n")
525 }
526
527 // Packages return the list of Go packages to be generated.
528 func (g *ObjcWrapper) Packages() []string {
529 return g.pkgNames
530 }
531
532 func (g *ObjcWrapper) GenPackage(idx int) {
533 pkg := g.pkgNames[idx]
534 g.Printf(gobindPreamble)
535 g.Printf("package %s\n\n", path.Base(pkg))
536 g.Printf("import \"ObjC\"\n\n")
537 g.Printf("const _ = ObjC.Dummy\n\n")
538 for _, n := range g.modMap[pkg] {
539 g.Printf("type %s ObjC.%s\n", n.GoName, n.Module+"_"+n.GoName)
540 }
541 if n, ok := g.typePkgs[pkg]; ok {
542 g.Printf("var (\n")
543 g.Indent()
544 // Functions
545 for _, f := range n.Funcs {
546 if !g.isFuncSupported(f) {
547 continue
548 }
549 g.Printf("%s func", f.GoName)
550 g.genFuncDecl(false, f)
551 g.Printf("\n")
552 }
553 g.Outdent()
554 g.Printf(")\n\n")
555 }
556 }
557
558 func (g *ObjcWrapper) GenInterfaces() {
559 g.Printf(gobindPreamble)
560 g.Printf("package ObjC\n\n")
561 g.Printf("// Used to silence this package not used errors\n")
562 g.Printf("const Dummy = 0\n\n")
563 for _, n := range g.types {
564 g.genInterface(n)
565 }
566 }
567
568 func (g *ObjcWrapper) genInterface(n *objc.Named) {
569 g.Printf("type %s interface {\n", n.Module+"_"+n.GoName)
570 g.Indent()
571 // Methods
572 for _, f := range n.AllMethods {
573 if !g.isFuncSupported(f) {
574 continue
575 }
576 g.Printf("%s", f.GoName)
577 g.genFuncDecl(true, f)
578 g.Printf("\n")
579 }
580 if _, exists := g.supers[n.GoName]; exists {
581 g.Printf("Super() %s\n", n.Module+"_"+n.GoName)
582 }
583 g.Outdent()
584 g.Printf("}\n\n")
585 }
586
587 func (g *ObjcWrapper) genFuncDecl(local bool, f *objc.Func) {
588 var returnsErr bool
589 g.Printf("(")
590 for i, p := range f.Params {
591 if i == len(f.Params)-1 && g.isErrorType(p.Type) {
592 returnsErr = true
593 break
594 }
595 if i > 0 {
596 g.Printf(", ")
597 }
598 g.Printf("%s %s", p.Name, g.goType(p.Type, local))
599 }
600 g.Printf(")")
601 if f.Ret != nil || returnsErr {
602 ret := f.Ret
603 if ret.Kind == objc.Bool && returnsErr {
604 // Skip the bool result and use the error results.
605 ret = nil
606 }
607 if ret != nil {
608 g.Printf(" (%s", g.goType(f.Ret, local))
609 if returnsErr {
610 g.Printf(", error")
611 }
612 g.Printf(")")
613 } else {
614 g.Printf(" error")
615 }
616 }
617 }
618
619 func (g *ObjcWrapper) isFuncSupported(f *objc.Func) bool {
620 for i, p := range f.Params {
621 if !g.isSupported(p.Type) {
622 if i < len(f.Params)-1 || !g.isErrorType(p.Type) {
623 return false
624 }
625 }
626 }
627 if f.Ret != nil {
628 return g.isSupported(f.Ret)
629 }
630 return true
631 }
632
633 func (g *ObjcWrapper) isErrorType(t *objc.Type) bool {
634 // Must be a NSError ** type
635 return t.Kind == objc.Class && t.Indirect && t.Name == "NSError"
636 }
637
638 func (g *ObjcWrapper) isSupported(t *objc.Type) bool {
639 if t.Indirect {
640 return false
641 }
642 switch t.Kind {
643 case objc.Unknown:
644 return false
645 case objc.Protocol:
646 // TODO: support inout parameters.
647 return !strings.HasSuffix(t.Decl, " *")
648 case objc.Class:
649 return t.Name != "SEL" && t.Name != "void"
650 default:
651 return true
652 }
653 }
654
655 func (g *ObjcWrapper) cgoType(t *objc.Type) string {
656 switch t.Kind {
657 case objc.Uint:
658 return "C.ulong"
659 case objc.Ushort:
660 return "C.ushort"
661 case objc.Uchar:
662 return "C.uchar"
663 default:
664 return "C." + g.cType(t)
665 }
666 }
667
668 func (g *ObjcWrapper) cType(t *objc.Type) string {
669 switch t.Kind {
670 case objc.Protocol, objc.Class:
671 return "int"
672 case objc.String:
673 return "nstring"
674 case objc.Data:
675 return "nbyteslice"
676 case objc.Int:
677 return "long"
678 case objc.Uint:
679 return "unsigned long"
680 case objc.Short:
681 return "short"
682 case objc.Ushort:
683 return "unsigned short"
684 case objc.Bool:
685 return "char"
686 case objc.Char:
687 return "char"
688 case objc.Uchar:
689 return "unsigned char"
690 case objc.Float:
691 return "float"
692 case objc.Double:
693 return "double"
694 default:
695 panic("invalid kind")
696 }
697 }
698
699 func (g *ObjcWrapper) objcType(t *objc.Type) string {
700 return t.Decl
701 }
702
703 func (g *ObjcWrapper) lookupImported(t *objc.Type) *objc.Named {
704 var mangled string
705 switch t.Kind {
706 case objc.Class:
707 mangled = t.Name + "C"
708 case objc.Protocol:
709 mangled = t.Name + "P"
710 default:
711 panic("invalid type kind")
712 }
713 if n, exists := g.imported[mangled]; exists {
714 return n
715 }
716 return g.imported[t.Name]
717 }
718
719 func (g *ObjcWrapper) goType(t *objc.Type, local bool) string {
720 switch t.Kind {
721 case objc.String:
722 return "string"
723 case objc.Data:
724 return "[]byte"
725 case objc.Int:
726 return "int"
727 case objc.Uint:
728 return "uint"
729 case objc.Short:
730 return "int16"
731 case objc.Ushort:
732 return "uint16"
733 case objc.Bool:
734 return "bool"
735 case objc.Char:
736 return "byte"
737 case objc.Uchar:
738 return "uint8"
739 case objc.Float:
740 return "float32"
741 case objc.Double:
742 return "float64"
743 case objc.Protocol, objc.Class:
744 var n *objc.Named
745 n = g.lookupImported(t)
746 name := n.Module + "_" + n.GoName
747 if !local {
748 name = "ObjC." + name
749 }
750 return name
751 default:
752 panic("invalid kind")
753 }
754 }