本文整理汇总了Golang中go/types.Package.Path方法的典型用法代码示例。如果您正苦于以下问题:Golang Package.Path方法的具体用法?Golang Package.Path怎么用?Golang Package.Path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/types.Package
的用法示例。
在下文中一共展示了Package.Path方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PrintSkeleton
//!+
func PrintSkeleton(pkg *types.Package, ifacename, concname string) error {
obj := pkg.Scope().Lookup(ifacename)
if obj == nil {
return fmt.Errorf("%s.%s not found", pkg.Path(), ifacename)
}
if _, ok := obj.(*types.TypeName); !ok {
return fmt.Errorf("%v is not a named type", obj)
}
iface, ok := obj.Type().Underlying().(*types.Interface)
if !ok {
return fmt.Errorf("type %v is a %T, not an interface",
obj, obj.Type().Underlying())
}
// Use first letter of type name as receiver parameter.
if !isValidIdentifier(concname) {
return fmt.Errorf("invalid concrete type name: %q", concname)
}
r, _ := utf8.DecodeRuneInString(concname)
fmt.Printf("// *%s implements %s.%s.\n", concname, pkg.Path(), ifacename)
fmt.Printf("type %s struct{}\n", concname)
mset := types.NewMethodSet(iface)
for i := 0; i < mset.Len(); i++ {
meth := mset.At(i).Obj()
sig := types.TypeString(meth.Type(), (*types.Package).Name)
fmt.Printf("func (%c *%s) %s%s {\n\tpanic(\"unimplemented\")\n}\n",
r, concname, meth.Name(),
strings.TrimPrefix(sig, "func"))
}
return nil
}
示例2: convertPackage
func (c *converter) convertPackage(v *gotypes.Package) *types.Package {
if v == nil {
return nil
}
if v, ok := c.converted[v]; ok {
return v.(*types.Package)
}
ret := types.NewPackage(v.Path(), v.Name())
if c.ret == nil {
c.ret = ret
}
c.converted[v] = ret
var imports []*types.Package
for _, imported := range v.Imports() {
imports = append(imports, c.convertPackage(imported))
}
ret.SetImports(imports)
c.convertScope(ret.Scope(), v.Scope())
for _, iface := range c.ifaces {
iface.Complete()
}
return ret
}
示例3: GenGo
func (b *binder) GenGo(pkg *types.Package, allPkg []*types.Package, outdir string) error {
pkgName := "go_"
pkgPath := ""
if pkg != nil {
pkgName += pkg.Name()
pkgPath = pkg.Path()
}
goFile := filepath.Join(outdir, pkgName+"main.go")
generate := func(w io.Writer) error {
if buildX {
printcmd("gobind -lang=go -outdir=%s %s", outdir, pkgPath)
}
if buildN {
return nil
}
conf := &bind.GeneratorConfig{
Writer: w,
Fset: b.fset,
Pkg: pkg,
AllPkg: allPkg,
}
return bind.GenGo(conf)
}
if err := writeFile(goFile, generate); err != nil {
return err
}
return nil
}
示例4: pkg
func (p *exporter) pkg(pkg *types.Package, emptypath bool) {
if pkg == nil {
log.Fatalf("gcimporter: unexpected nil pkg")
}
// if we saw the package before, write its index (>= 0)
if i, ok := p.pkgIndex[pkg]; ok {
p.index('P', i)
return
}
// otherwise, remember the package, write the package tag (< 0) and package data
if trace {
p.tracef("P%d = { ", len(p.pkgIndex))
defer p.tracef("} ")
}
p.pkgIndex[pkg] = len(p.pkgIndex)
p.tag(packageTag)
p.string(pkg.Name())
if emptypath {
p.string("")
} else {
p.string(pkg.Path())
}
}
示例5: pkgName
// pkgName retuns the package name and adds the package to the list of
// imports.
func (g *goGen) pkgName(pkg *types.Package) string {
// The error type has no package
if pkg == nil {
return ""
}
g.imports[pkg.Path()] = struct{}{}
return pkg.Name() + "."
}
示例6: GenObjc
func (b *binder) GenObjc(pkg *types.Package, allPkg []*types.Package, outdir string) (string, error) {
const bindPrefixDefault = "Go"
if bindPrefix == "" {
bindPrefix = bindPrefixDefault
}
name := strings.Title(pkg.Name())
bindOption := "-lang=objc"
if bindPrefix != bindPrefixDefault {
bindOption += " -prefix=" + bindPrefix
}
fileBase := bindPrefix + name
mfile := filepath.Join(outdir, fileBase+".m")
hfile := filepath.Join(outdir, fileBase+".h")
gohfile := filepath.Join(outdir, pkg.Name()+".h")
conf := &bind.GeneratorConfig{
Fset: b.fset,
Pkg: pkg,
AllPkg: allPkg,
}
generate := func(w io.Writer) error {
if buildX {
printcmd("gobind %s -outdir=%s %s", bindOption, outdir, pkg.Path())
}
if buildN {
return nil
}
conf.Writer = w
return bind.GenObjc(conf, bindPrefix, bind.ObjcM)
}
if err := writeFile(mfile, generate); err != nil {
return "", err
}
generate = func(w io.Writer) error {
if buildN {
return nil
}
conf.Writer = w
return bind.GenObjc(conf, bindPrefix, bind.ObjcH)
}
if err := writeFile(hfile, generate); err != nil {
return "", err
}
generate = func(w io.Writer) error {
if buildN {
return nil
}
conf.Writer = w
return bind.GenObjc(conf, bindPrefix, bind.ObjcGoH)
}
if err := writeFile(gohfile, generate); err != nil {
return "", err
}
return fileBase, nil
}
示例7: pkgVar
func (c *funcContext) pkgVar(pkg *types.Package) string {
if pkg == c.p.Pkg {
return "$pkg"
}
pkgVar, found := c.p.pkgVars[pkg.Path()]
if !found {
pkgVar = fmt.Sprintf(`$packages["%s"]`, pkg.Path())
}
return pkgVar
}
示例8: pkgFirstElem
func pkgFirstElem(p *types.Package) string {
if p == nil {
return ""
}
path := p.Path()
idx := strings.Index(path, "/")
if idx == -1 {
return ""
}
return path[:idx]
}
示例9: export
// export emits the exported package features.
func (w *Walker) export(pkg *types.Package) {
if *verbose {
log.Println(pkg)
}
pop := w.pushScope("pkg " + pkg.Path())
w.current = pkg
scope := pkg.Scope()
for _, name := range scope.Names() {
if ast.IsExported(name) {
w.emitObj(scope.Lookup(name))
}
}
pop()
}
示例10: checkExport
func (r *renamer) checkExport(id *ast.Ident, pkg *types.Package, from types.Object) bool {
// Reject cross-package references if r.to is unexported.
// (Such references may be qualified identifiers or field/method
// selections.)
if !ast.IsExported(r.to) && pkg != from.Pkg() {
r.errorf(from.Pos(),
"renaming this %s %q to %q would make it unexported",
objectKind(from), from.Name(), r.to)
r.errorf(id.Pos(), "\tbreaking references from packages such as %q",
pkg.Path())
return false
}
return true
}
示例11: GenJava
func (b *binder) GenJava(pkg *types.Package, allPkg []*types.Package, outdir, javadir string) error {
className := strings.Title(pkg.Name())
javaFile := filepath.Join(javadir, className+".java")
cFile := filepath.Join(outdir, "java_"+pkg.Name()+".c")
hFile := filepath.Join(outdir, pkg.Name()+".h")
bindOption := "-lang=java"
if bindJavaPkg != "" {
bindOption += " -javapkg=" + bindJavaPkg
}
conf := &bind.GeneratorConfig{
Fset: b.fset,
Pkg: pkg,
AllPkg: allPkg,
}
generate := func(w io.Writer) error {
if buildX {
printcmd("gobind %s -outdir=%s %s", bindOption, javadir, pkg.Path())
}
if buildN {
return nil
}
conf.Writer = w
return bind.GenJava(conf, bindJavaPkg, bind.Java)
}
if err := writeFile(javaFile, generate); err != nil {
return err
}
generate = func(w io.Writer) error {
if buildN {
return nil
}
conf.Writer = w
return bind.GenJava(conf, bindJavaPkg, bind.JavaC)
}
if err := writeFile(cFile, generate); err != nil {
return err
}
generate = func(w io.Writer) error {
if buildN {
return nil
}
conf.Writer = w
return bind.GenJava(conf, bindJavaPkg, bind.JavaH)
}
return writeFile(hFile, generate)
}
示例12: GenObjc
func (b *binder) GenObjc(pkg *types.Package, outdir string) (string, error) {
const bindPrefixDefault = "Go"
if bindPrefix == "" {
bindPrefix = bindPrefixDefault
}
name := strings.Title(pkg.Name())
bindOption := "-lang=objc"
if bindPrefix != bindPrefixDefault {
bindOption += " -prefix=" + bindPrefix
}
fileBase := bindPrefix + name
mfile := filepath.Join(outdir, fileBase+".m")
hfile := filepath.Join(outdir, fileBase+".h")
generate := func(w io.Writer) error {
if buildX {
printcmd("gobind %s -outdir=%s %s", bindOption, outdir, pkg.Path())
}
if buildN {
return nil
}
return bind.GenObjc(w, b.fset, pkg, bindPrefix, false)
}
if err := writeFile(mfile, generate); err != nil {
return "", err
}
generate = func(w io.Writer) error {
if buildN {
return nil
}
return bind.GenObjc(w, b.fset, pkg, bindPrefix, true)
}
if err := writeFile(hfile, generate); err != nil {
return "", err
}
objcPkg, err := ctx.Import("golang.org/x/mobile/bind/objc", "", build.FindOnly)
if err != nil {
return "", err
}
if err := copyFile(filepath.Join(outdir, "seq.h"), filepath.Join(objcPkg.Dir, "seq.h")); err != nil {
return "", err
}
return fileBase, nil
}
示例13: GenGo
func (b *binder) GenGo(pkg *types.Package, outdir string) error {
pkgName := "go_" + pkg.Name()
outdir = filepath.Join(outdir, pkgName)
goFile := filepath.Join(outdir, pkgName+"main.go")
generate := func(w io.Writer) error {
if buildX {
printcmd("gobind -lang=go -outdir=%s %s", outdir, pkg.Path())
}
if buildN {
return nil
}
return bind.GenGo(w, b.fset, pkg)
}
if err := writeFile(goFile, generate); err != nil {
return err
}
return nil
}
示例14: BuildPackage
// BuildPackage builds an SSA program with IR for a single package.
//
// It populates pkg by type-checking the specified file ASTs. All
// dependencies are loaded using the importer specified by tc, which
// typically loads compiler export data; SSA code cannot be built for
// those packages. BuildPackage then constructs an ssa.Program with all
// dependency packages created, and builds and returns the SSA package
// corresponding to pkg.
//
// The caller must have set pkg.Path() to the import path.
//
// The operation fails if there were any type-checking or import errors.
//
// See ../ssa/example_test.go for an example.
//
func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error) {
if fset == nil {
panic("no token.FileSet")
}
if pkg.Path() == "" {
panic("package has no import path")
}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Scopes: make(map[ast.Node]*types.Scope),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
return nil, nil, err
}
prog := ssa.NewProgram(fset, mode)
// Create SSA packages for all imports.
// Order is not significant.
created := make(map[*types.Package]bool)
var createAll func(pkgs []*types.Package)
createAll = func(pkgs []*types.Package) {
for _, p := range pkgs {
if !created[p] {
created[p] = true
prog.CreatePackage(p, nil, nil, true)
createAll(p.Imports())
}
}
}
createAll(pkg.Imports())
// Create and build the primary package.
ssapkg := prog.CreatePackage(pkg, files, info, false)
ssapkg.Build()
return ssapkg, info, nil
}
示例15: GenJava
func (b *binder) GenJava(pkg *types.Package, outdir string) error {
className := strings.Title(pkg.Name())
javaFile := filepath.Join(outdir, className+".java")
bindOption := "-lang=java"
if bindJavaPkg != "" {
bindOption += " -javapkg=" + bindJavaPkg
}
generate := func(w io.Writer) error {
if buildX {
printcmd("gobind %s -outdir=%s %s", bindOption, outdir, pkg.Path())
}
if buildN {
return nil
}
return bind.GenJava(w, b.fset, pkg, bindJavaPkg)
}
if err := writeFile(javaFile, generate); err != nil {
return err
}
return nil
}