• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang types.Named类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中golang.org/x/tools/go/types.Named的典型用法代码示例。如果您正苦于以下问题:Golang Named类的具体用法?Golang Named怎么用?Golang Named使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Named类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: usedObjects

func (u *Unexporter) usedObjects() map[types.Object]bool {
	objs := make(map[types.Object]bool)
	for _, pkgInfo := range u.packages {
		// easy path
		for id, obj := range pkgInfo.Uses {
			// ignore builtin value
			if obj.Pkg() == nil {
				continue
			}
			// if it's a type from different package, store it
			if obj.Pkg() != pkgInfo.Pkg {
				objs[obj] = true
			}
			// embedded fields are marked as used, no much which package the original type belongs to,
			// so that they won't show up in the renaming list #16
			if field := pkgInfo.Defs[id]; field != nil {
				// embdded field identifier is the same as it's type
				objs[field] = true
			}
		}
	}
	// Check assignability
	for key := range u.satisfy() {
		var (
			lhs, rhs *types.Named
			ok       bool
		)
		if lhs, ok = key.LHS.(*types.Named); !ok {
			continue
		}
		switch r := key.RHS.(type) {
		case *types.Named:
			rhs = r
		case *types.Pointer: // the receiver could be a pointer, see #14
			rhs = r.Elem().(*types.Named)
		default:
			continue
		}

		lset := u.msets.MethodSet(key.LHS)
		rset := u.msets.MethodSet(key.RHS)
		for i := 0; i < lset.Len(); i++ {
			obj := lset.At(i).Obj()
			// LHS are the abstract methods, they are only exported if there are other packages using it
			if lhs.Obj().Pkg() != rhs.Obj().Pkg() {
				objs[obj] = true
			}
			// if satisfied by type within the same package only, it should be unexported
			// however, we should not rename from the concret method side, but from the
			// interface side, carefully exclude concret methods that don't implement an abstract method (see #14, #17)
			rsel := rset.Lookup(rhs.Obj().Pkg(), obj.Name())
			objs[rsel.Obj()] = true
		}
	}
	return objs
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:56,代码来源:unexport.go


示例2: getConsts

func getConsts(named *types.Named) string {
	pkg := named.Obj().Pkg()

	var s []string
	for _, name := range pkg.Scope().Names() {
		obj := pkg.Scope().Lookup(name)

		if konst, ok := obj.(*types.Const); ok {
			if konst.Type() == named {
				s = append(s, strings.Replace(konst.Val().String(), "\"", "", -1))
			}
		}
	}

	sort.Strings(s)

	return strings.Trim(strings.Join(s, " | "), " ")
}
开发者ID:johan--,项目名称:kube-annotator,代码行数:18,代码来源:intermediate.go


示例3: lookupNamedField

func (w *PkgWalker) lookupNamedField(named *types.Named, name string) *types.Named {
	if istruct, ok := named.Underlying().(*types.Struct); ok {
		for i := 0; i < istruct.NumFields(); i++ {
			field := istruct.Field(i)
			if field.Anonymous() {
				fieldType := orgType(field.Type())
				if typ, ok := fieldType.(*types.Named); ok {
					if na := w.lookupNamedField(typ, name); na != nil {
						return na
					}
				}
			} else {
				if field.Name() == name {
					return named
				}
			}
		}
	}
	return nil
}
开发者ID:donseba,项目名称:gotools,代码行数:20,代码来源:types.go


示例4: assignMethodPaths

func (g *Grapher) assignMethodPaths(named *types.Named, prefix []string, pkgscope bool) {
	for i := 0; i < named.NumMethods(); i++ {
		m := named.Method(i)
		path := append(append([]string{}, prefix...), m.Name())
		g.paths[m] = path

		g.exported[m] = ast.IsExported(m.Name())
		g.pkgscope[m] = pkgscope

		if s := m.Scope(); s != nil {
			g.assignPaths(s, path, false)
		}
	}

	if iface, ok := named.Underlying().(*types.Interface); ok {
		for i := 0; i < iface.NumExplicitMethods(); i++ {
			m := iface.Method(i)
			path := append(append([]string{}, prefix...), m.Name())
			g.paths[m] = path

			g.exported[m] = ast.IsExported(m.Name())
			g.pkgscope[m] = pkgscope

			if s := m.Scope(); s != nil {
				g.assignPaths(s, path, false)
			}
		}
	}
}
开发者ID:ildarisaev,项目名称:srclib-go,代码行数:29,代码来源:scope.go


示例5: descriptorNamed

func (d *DIBuilder) descriptorNamed(t *types.Named) llvm.Value {
	// Create a placeholder for the named type, to terminate cycles.
	placeholder := llvm.MDNode(nil)
	d.types.Set(t, placeholder)
	var diFile llvm.Value
	var line int
	if file := d.fset.File(t.Obj().Pos()); file != nil {
		line = file.Line(t.Obj().Pos())
		diFile = d.getFile(file)
	}
	typedef := d.builder.CreateTypedef(llvm.DITypedef{
		Type: d.DIType(t.Underlying()),
		Name: t.Obj().Name(),
		File: diFile,
		Line: line,
	})
	placeholder.ReplaceAllUsesWith(typedef)
	return typedef
}
开发者ID:hinike,项目名称:llgo,代码行数:19,代码来源:debug.go


示例6: lookupNamedMethod

func (w *PkgWalker) lookupNamedMethod(named *types.Named, name string) (types.Object, *types.Named) {
	if iface, ok := named.Underlying().(*types.Interface); ok {
		for i := 0; i < iface.NumMethods(); i++ {
			fn := iface.Method(i)
			if fn.Name() == name {
				return fn, named
			}
		}
		for i := 0; i < iface.NumEmbeddeds(); i++ {
			if obj, na := w.lookupNamedMethod(iface.Embedded(i), name); obj != nil {
				return obj, na
			}
		}
		return nil, nil
	}
	if istruct, ok := named.Underlying().(*types.Struct); ok {
		for i := 0; i < named.NumMethods(); i++ {
			fn := named.Method(i)
			if fn.Name() == name {
				return fn, named
			}
		}
		for i := 0; i < istruct.NumFields(); i++ {
			field := istruct.Field(i)
			if !field.Anonymous() {
				continue
			}
			if typ, ok := field.Type().(*types.Named); ok {
				if obj, na := w.lookupNamedMethod(typ, name); obj != nil {
					return obj, na
				}
			}
		}
	}
	return nil, nil
}
开发者ID:donseba,项目名称:gotools,代码行数:36,代码来源:types.go


示例7: TypeStart

// Type definitions are only carried through to Haxe to allow access to objects as if they were native Haxe classes.
// TODO consider renaming
func (l langType) TypeStart(nt *types.Named, err string) string {
	typName := "GoType" + l.LangName("", nt.String())
	hxTyp := l.LangType(nt.Obj().Type(), false, nt.String())
	ret := ""
	switch hxTyp {
	case "Object":
		ret += "class " + typName
		ret += " extends " + hxTyp + " {\n"
	default:
		ret += "abstract " + typName + "(" + hxTyp + ") from " + hxTyp + " to " + hxTyp + " {\n"
	}
	switch nt.Underlying().(type) {
	case *types.Struct:
		str := nt.Underlying().(*types.Struct)
		ret += "inline public function new(){ super new(" + strconv.Itoa(int(haxeStdSizes.Sizeof(nt.Obj().Type()))) + "); }\n"
		flds := []string{}
		for f := 0; f < str.NumFields(); f++ {
			fName := str.Field(f).Name()
			if len(fName) > 0 {
				if unicode.IsUpper(rune(fName[0])) {
					flds = append(flds, fName)
				}
			}
		}
		sort.Strings(flds) // make sure the fields are always in the same order in the file
		for _, fName := range flds {
			for f := 0; f < str.NumFields(); f++ {
				if fName == str.Field(f).Name() {
					haxeTyp := l.LangType(str.Field(f).Type(), false, nt.String())
					fOff := fieldOffset(str, f)
					sfx := loadStoreSuffix(str.Field(f).Type(), true)
					ret += fmt.Sprintf("public var _%s(get,set):%s;\n", fName, haxeTyp)
					ret += fmt.Sprintf("function get__%s():%s { return get%s%d); }\n",
						fName, haxeTyp, sfx, fOff)
					ret += fmt.Sprintf("function set__%s(v:%s):%s { return set%s%d,v); }\n",
						fName, haxeTyp, haxeTyp, sfx, fOff)
					break
				}
			}
		}
	case *types.Array:
		ret += "inline public function new(){ super new(" + strconv.Itoa(int(haxeStdSizes.Sizeof(nt.Obj().Type()))) + "); }\n"
	default: // TODO not yet sure how to handle named types that are not structs
		ret += "inline public function new(v:" + hxTyp + ") { this = v; }\n"
	}

	meths := []string{}
	for m := 0; m < nt.NumMethods(); m++ {
		mName := nt.Method(m).Name()
		if len(mName) > 0 {
			if unicode.IsUpper(rune(mName[0])) {
				meths = append(meths, mName)
			}
		}
	}
	sort.Strings(meths) // make sure the methods always appear in the same order in the file
	for _, mName := range meths {
		for m := 0; m < nt.NumMethods(); m++ {
			meth := nt.Method(m)
			if mName == meth.Name() {
				sig := meth.Type().(*types.Signature)
				ret += "// " + mName + " " + sig.String() + "\n"
				ret += "public function _" + mName + "("
				for p := 0; p < sig.Params().Len(); p++ {
					if p > 0 {
						ret += ","
					}
					ret += "_" + sig.Params().At(p).Name() + ":" + l.LangType(sig.Params().At(p).Type(), false, nt.String())
				}
				ret += ")"
				switch sig.Results().Len() {
				case 0:
					ret += ":Void "
				case 1:
					ret += ":" + l.LangType(sig.Results().At(0).Type(), false, nt.String())
				default:
					ret += ":{"
					for r := 0; r < sig.Results().Len(); r++ {
						if r > 0 {
							ret += ","
						}
						ret += fmt.Sprintf("r%d:%s", r, l.LangType(sig.Results().At(r).Type(), false, nt.String()))
					}
					ret += "}"
				}
				ret += "{\n\t"
				if sig.Results().Len() > 0 {
					ret += "return "
				}
				fnToCall := l.LangName(
					nt.Obj().Pkg().Name()+":"+sig.Recv().Type().String(),
					meth.Name())
				ret += `Go_` + fnToCall + `.hx(this`
				for p := 0; p < sig.Params().Len(); p++ {
					ret += ", _" + sig.Params().At(p).Name()
				}
				ret += ");\n}\n"
			}
//.........这里部分代码省略.........
开发者ID:henrylee2cn,项目名称:tardisgo,代码行数:101,代码来源:types.go


示例8: addRuntimeType

// addRuntimeType is called for each concrete type that can be the
// dynamic type of some interface or reflect.Value.
// Adapted from needMethods in go/ssa/builder.go
//
func (r *rta) addRuntimeType(T types.Type, skip bool) {
	if prev, ok := r.result.RuntimeTypes.At(T).(bool); ok {
		if skip && !prev {
			r.result.RuntimeTypes.Set(T, skip)
		}
		return
	}
	r.result.RuntimeTypes.Set(T, skip)

	mset := r.prog.MethodSets.MethodSet(T)

	if _, ok := T.Underlying().(*types.Interface); !ok {
		// T is a new concrete type.
		for i, n := 0, mset.Len(); i < n; i++ {
			sel := mset.At(i)
			m := sel.Obj()

			if m.Exported() {
				// Exported methods are always potentially callable via reflection.
				r.addReachable(r.prog.MethodValue(sel), true)
			}
		}

		// Add callgraph edge for each existing dynamic
		// "invoke"-mode call via that interface.
		for _, I := range r.interfaces(T) {
			sites, _ := r.invokeSites.At(I).([]ssa.CallInstruction)
			for _, site := range sites {
				r.addInvokeEdge(site, T)
			}
		}
	}

	// Precondition: T is not a method signature (*Signature with Recv()!=nil).
	// Recursive case: skip => don't call makeMethods(T).
	// Each package maintains its own set of types it has visited.

	var n *types.Named
	switch T := T.(type) {
	case *types.Named:
		n = T
	case *types.Pointer:
		n, _ = T.Elem().(*types.Named)
	}
	if n != nil {
		owner := n.Obj().Pkg()
		if owner == nil {
			return // built-in error type
		}
	}

	// Recursion over signatures of each exported method.
	for i := 0; i < mset.Len(); i++ {
		if mset.At(i).Obj().Exported() {
			sig := mset.At(i).Type().(*types.Signature)
			r.addRuntimeType(sig.Params(), true)  // skip the Tuple itself
			r.addRuntimeType(sig.Results(), true) // skip the Tuple itself
		}
	}

	switch t := T.(type) {
	case *types.Basic:
		// nop

	case *types.Interface:
		// nop---handled by recursion over method set.

	case *types.Pointer:
		r.addRuntimeType(t.Elem(), false)

	case *types.Slice:
		r.addRuntimeType(t.Elem(), false)

	case *types.Chan:
		r.addRuntimeType(t.Elem(), false)

	case *types.Map:
		r.addRuntimeType(t.Key(), false)
		r.addRuntimeType(t.Elem(), false)

	case *types.Signature:
		if t.Recv() != nil {
			panic(fmt.Sprintf("Signature %s has Recv %s", t, t.Recv()))
		}
		r.addRuntimeType(t.Params(), true)  // skip the Tuple itself
		r.addRuntimeType(t.Results(), true) // skip the Tuple itself

	case *types.Named:
		// A pointer-to-named type can be derived from a named
		// type via reflection.  It may have methods too.
		r.addRuntimeType(types.NewPointer(T), false)

		// Consider 'type T struct{S}' where S has methods.
		// Reflection provides no way to get from T to struct{S},
		// only to S, so the method set of struct{S} is unwanted,
		// so set 'skip' flag during recursion.
//.........这里部分代码省略.........
开发者ID:binaryedge,项目名称:40fy-client,代码行数:101,代码来源:rta.go



注:本文中的golang.org/x/tools/go/types.Named类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang types.Object类代码示例发布时间:2022-05-28
下一篇:
Golang types.MethodSet类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap