本文整理汇总了Golang中go/types.Type类的典型用法代码示例。如果您正苦于以下问题:Golang Type类的具体用法?Golang Type怎么用?Golang Type使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Type类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: genWrite
func (g *ObjcGen) genWrite(varName string, t types.Type, mode varMode) {
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
g.Printf("nstring _%s = go_seq_from_objc_string(%s);\n", varName, varName)
default:
g.Printf("%s _%s = (%s)%s;\n", g.cgoType(t), varName, g.cgoType(t), varName)
}
case *types.Slice:
switch e := t.Elem().(type) {
case *types.Basic:
switch e.Kind() {
case types.Uint8: // Byte.
g.Printf("nbyteslice _%s = go_seq_from_objc_bytearray(%s, %d);\n", varName, varName, toCFlag(mode == modeRetained))
default:
g.errorf("unsupported type: %s", t)
}
default:
g.errorf("unsupported type: %s", t)
}
case *types.Named:
switch u := t.Underlying().(type) {
case *types.Interface:
g.genRefWrite(varName)
default:
g.errorf("unsupported named type: %s / %T", u, u)
}
case *types.Pointer:
g.genRefWrite(varName)
default:
g.Printf("%s _%s = (%s)%s;\n", g.cgoType(t), varName, g.cgoType(t), varName)
}
}
示例2: lockPath
// lockPath returns a typePath describing the location of a lock value
// contained in typ. If there is no contained lock, it returns nil.
func lockPath(tpkg *types.Package, typ types.Type) typePath {
if typ == nil {
return nil
}
// We're only interested in the case in which the underlying
// type is a struct. (Interfaces and pointers are safe to copy.)
styp, ok := typ.Underlying().(*types.Struct)
if !ok {
return nil
}
// We're looking for cases in which a reference to this type
// can be locked, but a value cannot. This differentiates
// embedded interfaces from embedded values.
if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil {
if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil {
return []types.Type{typ}
}
}
nfields := styp.NumFields()
for i := 0; i < nfields; i++ {
ftyp := styp.Field(i).Type()
subpath := lockPath(tpkg, ftyp)
if subpath != nil {
return append(subpath, typ)
}
}
return nil
}
示例3: asmKindForType
func asmKindForType(t types.Type, size int) asmKind {
switch t := t.Underlying().(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
return asmString
case types.Complex64, types.Complex128:
return asmComplex
}
return asmKind(size)
case *types.Pointer, *types.Chan, *types.Map, *types.Signature:
return asmKind(size)
case *types.Struct:
return asmStruct
case *types.Interface:
if t.Empty() {
return asmEmptyInterface
}
return asmInterface
case *types.Array:
return asmArray
case *types.Slice:
return asmSlice
}
panic("unreachable")
}
示例4: javaTypeDefault
// javaTypeDefault returns a string that represents the default value of the mapped java type.
// TODO(hyangah): Combine javaType and javaTypeDefault?
func (g *javaGen) javaTypeDefault(T types.Type) string {
switch T := T.(type) {
case *types.Basic:
switch T.Kind() {
case types.Bool:
return "false"
case types.Int, types.Int8, types.Int16, types.Int32,
types.Int64, types.Uint8:
return "0"
case types.Float32, types.Float64:
return "0.0"
case types.String:
return "null"
default:
g.errorf("unsupported return type: %s", T)
return "TODO"
}
case *types.Slice, *types.Pointer, *types.Named:
return "null"
default:
g.errorf("unsupported javaType: %#+v, %s\n", T, T)
return "TODO"
}
}
示例5: UnderlyingTarget
func (f Field) UnderlyingTarget() fieldser {
var t types.Type
switch v := f.v.Type().(type) {
case elemer:
t = v.Elem()
case *types.Named:
t = v
}
if _, ok := t.(underlyinger); !ok {
return nil
}
u := t.(underlyinger).Underlying()
switch t := u.(type) {
case *types.Struct:
return fields{
g: f.gen,
target: t,
}
case *types.Pointer:
return fields{
g: f.gen,
target: t.Elem().(*types.Named).Underlying().(*types.Struct),
}
}
return nil
}
示例6: needWrapType
func needWrapType(typ types.Type) bool {
switch typ := typ.(type) {
case *types.Basic:
return false
case *types.Struct:
return true
case *types.Named:
switch ut := typ.Underlying().(type) {
case *types.Basic:
return false
default:
return needWrapType(ut)
}
case *types.Array:
return true
case *types.Map:
return true
case *types.Slice:
return true
case *types.Interface:
wrap := true
if typ.Underlying() == universe.syms["error"].GoType().Underlying() {
wrap = false
}
return wrap
case *types.Signature:
return true
case *types.Pointer:
return needWrapType(typ.Elem())
}
return false
}
示例7: genWrite
func (g *goGen) genWrite(valName, seqName string, T types.Type) {
if isErrorType(T) {
g.Printf("if %s == nil {\n", valName)
g.Printf(" %s.WriteString(\"\");\n", seqName)
g.Printf("} else {\n")
g.Printf(" %s.WriteString(%s.Error());\n", seqName, valName)
g.Printf("}\n")
return
}
switch T := T.(type) {
case *types.Pointer:
// TODO(crawshaw): test *int
// TODO(crawshaw): test **Generator
switch T := T.Elem().(type) {
case *types.Named:
obj := T.Obj()
if obj.Pkg() != g.pkg {
g.errorf("type %s not defined in package %s", T, g.pkg)
return
}
g.Printf("%s.WriteGoRef(%s)\n", seqName, valName)
default:
g.errorf("unsupported type %s", T)
}
case *types.Named:
switch u := T.Underlying().(type) {
case *types.Interface, *types.Pointer:
g.Printf("%s.WriteGoRef(%s)\n", seqName, valName)
default:
g.errorf("unsupported, direct named type %s: %s", T, u)
}
default:
g.Printf("%s.Write%s(%s);\n", seqName, seqType(T), valName)
}
}
示例8: validVarDeclType
func (f *File) validVarDeclType(typ types.Type) *Error {
if e := f.validResultType(typ); e != nil {
switch typ.(type) {
default:
return e
case *types.Interface:
return f.validVarDeclType(typ.Underlying())
case *types.Named:
named, ok := typ.(*types.Named)
if !ok {
panic("ERROR can't cast to named type")
}
tname := named.Obj()
/*i := Int(0)
simdInt := reflect.TypeOf(i)
var i4 Int4
simdInt4 := reflect.TypeOf(i4)
switch tname.Name() {
default:*/
return &Error{errors.New(fmt.Sprintf("invalid type (%v)", tname.Name())), 0}
/*case simdInt.Name():
return nil
case simdInt4.Name():
return nil
}*/
}
}
return nil
}
示例9: MethodSet
// MethodSet returns the method set of type T. It is thread-safe.
//
// If cache is nil, this function is equivalent to types.NewMethodSet(T).
// Utility functions can thus expose an optional *MethodSetCache
// parameter to clients that care about performance.
//
func (cache *MethodSetCache) MethodSet(T types.Type) *types.MethodSet {
if cache == nil {
return types.NewMethodSet(T)
}
cache.mu.Lock()
defer cache.mu.Unlock()
switch T := T.(type) {
case *types.Named:
return cache.lookupNamed(T).value
case *types.Pointer:
if N, ok := T.Elem().(*types.Named); ok {
return cache.lookupNamed(N).pointer
}
}
// all other types
// (The map uses pointer equivalence, not type identity.)
mset := cache.others[T]
if mset == nil {
mset = types.NewMethodSet(T)
if cache.others == nil {
cache.others = make(map[types.Type]*types.MethodSet)
}
cache.others[T] = mset
}
return mset
}
示例10: addInterfaceType
func (sym *symtab) addInterfaceType(pkg *types.Package, obj types.Object, t types.Type, kind symkind, id, n string) {
fn := sym.typename(t, nil)
typ := t.Underlying().(*types.Interface)
kind |= skInterface
// special handling of 'error'
if isErrorType(typ) {
return
}
sym.syms[fn] = &symbol{
gopkg: pkg,
goobj: obj,
gotyp: t,
kind: kind,
id: id,
goname: n,
cgoname: "cgo_type_" + id,
cpyname: "cpy_type_" + id,
pyfmt: "O&",
pybuf: "P",
pysig: "object",
c2py: "cgopy_cnv_c2py_" + id,
py2c: "cgopy_cnv_py2c_" + id,
pychk: fmt.Sprintf("cpy_func_%[1]s_check(%%s)", id),
}
}
示例11: hash
func (x array) hash(t types.Type) int {
h := 0
tElt := t.Underlying().(*types.Array).Elem()
for _, xi := range x {
h += hash(tElt, xi)
}
return h
}
示例12: encodeType
func encodeType(t types.Type) Type {
t = t.Underlying()
switch t.(type) {
case *types.Basic:
b := t.(*types.Basic)
untyped := (b.Info() & types.IsUntyped) != 0
return NewBasic(basicKindString(b), untyped)
case *types.Pointer:
p := t.(*types.Pointer)
pt := encodeType(p.Elem())
return NewPointer(pt)
case *types.Array:
a := t.(*types.Array)
at := encodeType(a.Elem())
return NewArray(at, a.Len())
case *types.Slice:
s := t.(*types.Slice)
st := encodeType(s.Elem())
return NewSlice(st)
case *types.Signature:
sig := t.(*types.Signature)
v := sig.Recv()
var vt *Type
if v != nil {
t := encodeType(v.Type())
vt = &t
}
return NewSignature(
sig.Variadic(),
vt,
tupleToSlice(sig.Params()),
tupleToSlice(sig.Results()))
case *types.Named:
n := t.(*types.Named)
return NewNamed(
n.Obj().Pkg().Name(),
n.Obj().Name(),
n.Underlying())
case *types.Interface:
i := t.(*types.Interface)
if i.Empty() {
return NewInterface()
} else {
return NewUnsupported("Interfaces")
}
case *types.Tuple:
return NewUnsupported("Tuples")
case *types.Map:
return NewUnsupported("Maps")
case *types.Chan:
return NewUnsupported("Channels")
case *types.Struct:
return NewUnsupported("Structs")
default:
return NewUnsupported(t.String())
}
}
示例13: indirectType
// indirect(typ) assumes that typ is a pointer type,
// or named alias thereof, and returns its base type.
// Panic ensures if it is not a pointer.
//
func indirectType(ptr types.Type) types.Type {
if v, ok := underlyingType(ptr).(*types.Pointer); ok {
return v.Base
}
// When debugging it is convenient to comment out this line
// and let it continue to print the (illegal) SSA form.
panic("indirect() of non-pointer type: " + ptr.String())
return nil
}
示例14: eq
func (x array) eq(t types.Type, _y interface{}) bool {
y := _y.(array)
tElt := t.Underlying().(*types.Array).Elem()
for i, xi := range x {
if !equals(tElt, xi, y[i]) {
return false
}
}
return true
}
示例15: makeImplementsType
func makeImplementsType(T types.Type, fset *token.FileSet) serial.ImplementsType {
var pos token.Pos
if nt, ok := deref(T).(*types.Named); ok { // implementsResult.t may be non-named
pos = nt.Obj().Pos()
}
return serial.ImplementsType{
Name: T.String(),
Pos: fset.Position(pos).String(),
Kind: typeKind(T),
}
}