本文整理汇总了Golang中code/google/com/p/go/tools/go/types.TypeName类的典型用法代码示例。如果您正苦于以下问题:Golang TypeName类的具体用法?Golang TypeName怎么用?Golang TypeName使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TypeName类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: translateType
func (c *funcContext) translateType(o *types.TypeName, toplevel bool) {
typeName := c.objectName(o)
lhs := typeName
if toplevel {
lhs += " = go$pkg." + typeName
}
size := int64(0)
constructor := "null"
switch t := o.Type().Underlying().(type) {
case *types.Struct:
params := make([]string, t.NumFields())
for i := 0; i < t.NumFields(); i++ {
params[i] = fieldName(t, i) + "_"
}
constructor = fmt.Sprintf("function(%s) {\n%sthis.go$val = this;\n", strings.Join(params, ", "), strings.Repeat("\t", c.p.indentation+1))
for i := 0; i < t.NumFields(); i++ {
name := fieldName(t, i)
constructor += fmt.Sprintf("%sthis.%s = %s_ !== undefined ? %s_ : %s;\n", strings.Repeat("\t", c.p.indentation+1), name, name, name, c.zeroValue(t.Field(i).Type()))
}
constructor += strings.Repeat("\t", c.p.indentation) + "}"
case *types.Basic:
if t.Info()&types.IsInteger != 0 {
size = sizes32.Sizeof(t)
}
}
c.Printf(`%s = go$newType(%d, "%s", "%s.%s", "%s", "%s", %s);`, lhs, size, typeKind(o.Type()), o.Pkg().Name(), o.Name(), o.Name(), o.Pkg().Path(), constructor)
}
开发者ID:pombredanne,项目名称:gopherjs,代码行数:27,代码来源:package.go
示例2: methodsFor
// methodsFor returns the named type and corresponding methods if the type
// denoted by obj is not an interface and has methods. Otherwise it returns
// the zero value.
func methodsFor(obj *types.TypeName) (*types.Named, []*types.Selection) {
named, _ := obj.Type().(*types.Named)
if named == nil {
// A type name's type can also be the
// exported basic type unsafe.Pointer.
return nil, nil
}
if _, ok := named.Underlying().(*types.Interface); ok {
// ignore interfaces
return nil, nil
}
methods := combinedMethodSet(named)
if len(methods) == 0 {
return nil, nil
}
return named, methods
}
开发者ID:samuelyao314,项目名称:mygo,代码行数:20,代码来源:print.go
示例3: emitType
func (w *Walker) emitType(obj *types.TypeName) {
name := obj.Name()
typ := obj.Type()
switch typ := typ.Underlying().(type) {
case *types.Struct:
w.emitStructType(name, typ)
case *types.Interface:
w.emitIfaceType(name, typ)
return // methods are handled by emitIfaceType
default:
w.emitf("type %s %s", name, w.typeString(typ.Underlying()))
}
// emit methods with value receiver
var methodNames map[string]bool
vset := typ.MethodSet()
for i, n := 0, vset.Len(); i < n; i++ {
m := vset.At(i)
if m.Obj().IsExported() {
w.emitMethod(m)
if methodNames == nil {
methodNames = make(map[string]bool)
}
methodNames[m.Obj().Name()] = true
}
}
// emit methods with pointer receiver; exclude
// methods that we have emitted already
// (the method set of *T includes the methods of T)
pset := types.NewPointer(typ).MethodSet()
for i, n := 0, pset.Len(); i < n; i++ {
m := pset.At(i)
if m.Obj().IsExported() && !methodNames[m.Obj().Name()] {
w.emitMethod(m)
}
}
}
开发者ID:bryanxu,项目名称:go-zh,代码行数:38,代码来源:goapi.go
示例4: namedType
func (a *analysis) namedType(obj *types.TypeName, implements map[*types.Named]implementsFacts) {
this := obj.Pkg()
T := obj.Type().(*types.Named)
v := &TypeInfoJSON{
Name: obj.Name(),
Size: sizes.Sizeof(T),
Align: sizes.Alignof(T),
Methods: []anchorJSON{}, // (JS wants non-nil)
}
// addFact adds the fact "is implemented by T" (by) or
// "implements T" (!by) to group.
addFact := func(group *implGroupJSON, T types.Type, by bool) {
Tobj := deref(T).(*types.Named).Obj()
var byKind string
if by {
// Show underlying kind of implementing type,
// e.g. "slice", "array", "struct".
s := reflect.TypeOf(T.Underlying()).String()
byKind = strings.ToLower(strings.TrimPrefix(s, "*types."))
}
group.Facts = append(group.Facts, implFactJSON{
ByKind: byKind,
Other: anchorJSON{
Href: a.posURL(Tobj.Pos(), len(Tobj.Name())),
Text: types.TypeString(this, T),
},
})
}
// IMPLEMENTS
if r, ok := implements[T]; ok {
if isInterface(T) {
// "T is implemented by <conc>" ...
// "T is implemented by <iface>"...
// "T implements <iface>"...
group := implGroupJSON{
Descr: types.TypeString(this, T),
}
// Show concrete types first; use two passes.
for _, sub := range r.to {
if !isInterface(sub) {
addFact(&group, sub, true)
}
}
for _, sub := range r.to {
if isInterface(sub) {
addFact(&group, sub, true)
}
}
for _, super := range r.from {
addFact(&group, super, false)
}
v.ImplGroups = append(v.ImplGroups, group)
} else {
// T is concrete.
if r.from != nil {
// "T implements <iface>"...
group := implGroupJSON{
Descr: types.TypeString(this, T),
}
for _, super := range r.from {
addFact(&group, super, false)
}
v.ImplGroups = append(v.ImplGroups, group)
}
if r.fromPtr != nil {
// "*C implements <iface>"...
group := implGroupJSON{
Descr: "*" + types.TypeString(this, T),
}
for _, psuper := range r.fromPtr {
addFact(&group, psuper, false)
}
v.ImplGroups = append(v.ImplGroups, group)
}
}
}
// METHOD SETS
for _, sel := range typeutil.IntuitiveMethodSet(T, &a.prog.MethodSets) {
meth := sel.Obj().(*types.Func)
pos := meth.Pos() // may be 0 for error.Error
v.Methods = append(v.Methods, anchorJSON{
Href: a.posURL(pos, len(meth.Name())),
Text: types.SelectionString(this, sel),
})
}
// Since there can be many specs per decl, we
// can't attach the link to the keyword 'type'
// (as we do with 'func'); we use the Ident.
fi, offset := a.fileAndOffset(obj.Pos())
fi.addLink(aLink{
start: offset,
end: offset + len(obj.Name()),
title: fmt.Sprintf("type info for %s", obj.Name()),
onclick: fmt.Sprintf("onClickTypeInfo(%d)", fi.addData(v)),
})
//.........这里部分代码省略.........
开发者ID:bryanxu,项目名称:go-zh.tools,代码行数:101,代码来源:typeinfo.go
注:本文中的code/google/com/p/go/tools/go/types.TypeName类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论