Using the new ObjC type analyzer API, scan the bound packages for references to ObjC classes and protocols and generate Go wrappers for them. This is the second part of the implementation of proposal golang/go#17102. For golang/go#17102 Change-Id: I773db7b0362a7ff526d0a0fd6da5b2fa33301144 Reviewed-on: https://go-review.googlesource.com/29174 Reviewed-by: David Crawshaw <crawshaw@golang.org>
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Package bind implements a code generator for gobind.
|
|
//
|
|
// See the documentation on the gobind command for usage details
|
|
// and the list of currently supported types.
|
|
// (http://godoc.org/golang.org/x/mobile/cmd/gobind)
|
|
package bind // import "golang.org/x/mobile/bind"
|
|
|
|
// TODO(crawshaw): slice support
|
|
// TODO(crawshaw): channel support
|
|
|
|
import (
|
|
"bytes"
|
|
"go/format"
|
|
"go/token"
|
|
"go/types"
|
|
"io"
|
|
)
|
|
|
|
type (
|
|
GeneratorConfig struct {
|
|
Writer io.Writer
|
|
Fset *token.FileSet
|
|
Pkg *types.Package
|
|
AllPkg []*types.Package
|
|
}
|
|
|
|
fileType int
|
|
)
|
|
|
|
// GenGo generates a Go stub to support foreign language APIs.
|
|
func GenGo(conf *GeneratorConfig) error {
|
|
buf := new(bytes.Buffer)
|
|
g := &goGen{
|
|
Generator: &Generator{
|
|
Printer: &Printer{Buf: buf, IndentEach: []byte("\t")},
|
|
Fset: conf.Fset,
|
|
AllPkg: conf.AllPkg,
|
|
Pkg: conf.Pkg,
|
|
},
|
|
}
|
|
g.Init()
|
|
if err := g.gen(); err != nil {
|
|
return err
|
|
}
|
|
src := buf.Bytes()
|
|
srcf, err := format.Source(src)
|
|
if err != nil {
|
|
conf.Writer.Write(src) // for debugging
|
|
return err
|
|
}
|
|
_, err = conf.Writer.Write(srcf)
|
|
return err
|
|
}
|