本文整理汇总了Golang中go/types.Type.Elem方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Elem方法的具体用法?Golang Type.Elem怎么用?Golang Type.Elem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/types.Type
的用法示例。
在下文中一共展示了Type.Elem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: 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
}
示例4: isSupported
// isSupported returns whether the generators can handle the type.
func (g *generator) isSupported(t types.Type) bool {
if isErrorType(t) {
return true
}
switch t := t.(type) {
case *types.Basic:
return true
case *types.Slice:
switch e := t.Elem().(type) {
case *types.Basic:
return e.Kind() == types.Uint8
}
case *types.Pointer:
switch t := t.Elem().(type) {
case *types.Named:
return g.validPkg(t.Obj().Pkg())
}
case *types.Named:
switch t.Underlying().(type) {
case *types.Interface, *types.Pointer:
return g.validPkg(t.Obj().Pkg())
}
}
return false
}
示例5: cgoType
// cgoType returns the name of a Cgo type suitable for converting a value of
// the given type.
func (g *generator) cgoType(t types.Type) string {
if isErrorType(t) {
return g.cgoType(types.Typ[types.String])
}
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.Bool, types.UntypedBool:
return "char"
case types.Int:
return "nint"
case types.Int8:
return "int8_t"
case types.Int16:
return "int16_t"
case types.Int32, types.UntypedRune: // types.Rune
return "int32_t"
case types.Int64, types.UntypedInt:
return "int64_t"
case types.Uint8: // types.Byte
return "uint8_t"
// TODO(crawshaw): case types.Uint, types.Uint16, types.Uint32, types.Uint64:
case types.Float32:
return "float"
case types.Float64, types.UntypedFloat:
return "double"
case types.String:
return "nstring"
default:
g.errorf("unsupported basic type: %s", t)
}
case *types.Slice:
switch e := t.Elem().(type) {
case *types.Basic:
switch e.Kind() {
case types.Uint8: // Byte.
return "nbyteslice"
default:
g.errorf("unsupported slice type: %s", t)
}
default:
g.errorf("unsupported slice type: %s", t)
}
case *types.Pointer:
if _, ok := t.Elem().(*types.Named); ok {
return g.cgoType(t.Elem())
}
g.errorf("unsupported pointer to type: %s", t)
case *types.Named:
return "int32_t"
default:
g.errorf("unsupported type: %s", t)
}
return "TODO"
}
示例6: genWrite
func (g *goGen) genWrite(toVar, fromVar string, t types.Type, mode varMode) {
if isErrorType(t) {
g.Printf("var %s_str string\n", toVar)
g.Printf("if %s == nil {\n", fromVar)
g.Printf(" %s_str = \"\"\n", toVar)
g.Printf("} else {\n")
g.Printf(" %s_str = %s.Error()\n", toVar, fromVar)
g.Printf("}\n")
g.genWrite(toVar, toVar+"_str", types.Typ[types.String], mode)
return
}
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
g.Printf("%s := encodeString(%s)\n", toVar, fromVar)
case types.Bool:
g.Printf("var %s C.%s = 0\n", toVar, g.cgoType(t))
g.Printf("if %s { %s = 1 }\n", fromVar, toVar)
default:
g.Printf("%s := C.%s(%s)\n", toVar, g.cgoType(t), fromVar)
}
case *types.Slice:
switch e := t.Elem().(type) {
case *types.Basic:
switch e.Kind() {
case types.Uint8: // Byte.
g.Printf("%s := fromSlice(%s, %v)\n", toVar, fromVar, mode == modeRetained)
default:
g.errorf("unsupported type: %s", t)
}
default:
g.errorf("unsupported type: %s", t)
}
case *types.Pointer:
// TODO(crawshaw): test *int
// TODO(crawshaw): test **Generator
switch t := t.Elem().(type) {
case *types.Named:
g.genToRefNum(toVar, fromVar)
default:
g.errorf("unsupported type %s", t)
}
case *types.Named:
switch u := t.Underlying().(type) {
case *types.Interface, *types.Pointer:
g.genToRefNum(toVar, fromVar)
default:
g.errorf("unsupported, direct named type %s: %s", t, u)
}
default:
g.errorf("unsupported type %s", t)
}
}
示例7: jniType
// jniType returns a string that can be used as a JNI type.
func (g *javaGen) jniType(T types.Type) string {
if isErrorType(T) {
// The error type is usually translated into an exception in
// Java, however the type can be exposed in other ways, such
// as an exported field.
return g.jniType(types.Typ[types.String])
}
switch T := T.(type) {
case *types.Basic:
switch T.Kind() {
case types.Bool, types.UntypedBool:
return "jboolean"
case types.Int:
return "jlong"
case types.Int8:
return "jbyte"
case types.Int16:
return "jshort"
case types.Int32, types.UntypedRune: // types.Rune
return "jint"
case types.Int64, types.UntypedInt:
return "jlong"
case types.Uint8: // types.Byte
// TODO(crawshaw): Java bytes are signed, so this is
// questionable, but vital.
return "jbyte"
// TODO(crawshaw): case types.Uint, types.Uint16, types.Uint32, types.Uint64:
case types.Float32:
return "jfloat"
case types.Float64, types.UntypedFloat:
return "jdouble"
case types.String, types.UntypedString:
return "jstring"
default:
g.errorf("unsupported basic type: %s", T)
return "TODO"
}
case *types.Slice:
return "jbyteArray"
case *types.Pointer:
if _, ok := T.Elem().(*types.Named); ok {
return g.jniType(T.Elem())
}
g.errorf("unsupported pointer to type: %s", T)
case *types.Named:
return "jobject"
default:
g.errorf("unsupported jniType: %#+v, %s\n", T, T)
}
return "TODO"
}
示例8: isExported
func isExported(t types.Type) bool {
if isErrorType(t) {
return true
}
switch t := t.(type) {
case *types.Basic:
return true
case *types.Named:
return t.Obj().Exported()
case *types.Pointer:
return isExported(t.Elem())
default:
return true
}
}
示例9: 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)
}
}
示例10: genRead
func (g *javaGen) genRead(resName, seqName string, T types.Type) {
switch T := T.(type) {
case *types.Pointer:
// TODO(crawshaw): test *int
// TODO(crawshaw): test **Generator
switch T := T.Elem().(type) {
case *types.Named:
o := T.Obj()
if o.Pkg() != g.pkg {
g.errorf("type %s not defined in %s", T, g.pkg)
return
}
g.Printf("%s = new %s(%s.readRef());\n", resName, o.Name(), seqName)
default:
g.errorf("unsupported type %s", T)
}
case *types.Named:
switch T.Underlying().(type) {
case *types.Interface, *types.Pointer:
o := T.Obj()
if o.Pkg() != g.pkg {
g.errorf("type %s not defined in %s", T, g.pkg)
return
}
g.Printf("%s = new %s.Proxy(%s.readRef());\n", resName, o.Name(), seqName)
default:
g.errorf("unsupported, direct named type %s", T)
}
default:
g.Printf("%s = %s.read%s();\n", resName, seqName, seqType(T))
}
}
示例11: 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)
}
}
示例12: genCToJava
func (g *javaGen) genCToJava(toName, fromName string, t types.Type, mode varMode) {
if isErrorType(t) {
g.genCToJava(toName, fromName, types.Typ[types.String], mode)
return
}
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
g.Printf("jstring %s = go_seq_to_java_string(env, %s);\n", toName, fromName)
case types.Bool:
g.Printf("jboolean %s = %s ? JNI_TRUE : JNI_FALSE;\n", toName, fromName)
default:
g.Printf("%s %s = (%s)%s;\n", g.jniType(t), toName, g.jniType(t), fromName)
}
case *types.Slice:
switch e := t.Elem().(type) {
case *types.Basic:
switch e.Kind() {
case types.Uint8: // Byte.
g.Printf("jbyteArray %s = go_seq_to_java_bytearray(env, %s, %d);\n", toName, fromName, g.toCFlag(mode == modeRetained))
default:
g.errorf("unsupported type: %s", t)
}
default:
g.errorf("unsupported type: %s", t)
}
case *types.Pointer:
// TODO(crawshaw): test *int
// TODO(crawshaw): test **Generator
switch t := t.Elem().(type) {
case *types.Named:
g.genFromRefnum(toName, fromName, t, t.Obj())
default:
g.errorf("unsupported type %s", t)
}
case *types.Named:
switch t.Underlying().(type) {
case *types.Interface, *types.Pointer:
g.genFromRefnum(toName, fromName, t, t.Obj())
default:
g.errorf("unsupported, direct named type %s", t)
}
default:
g.Printf("%s %s = (%s)%s;\n", g.jniType(t), toName, g.jniType(t), fromName)
}
}
示例13: jniType
// jniType returns a string that can be used as a JNI type.
func (g *JavaGen) jniType(T types.Type) string {
switch T := T.(type) {
case *types.Basic:
switch T.Kind() {
case types.Bool, types.UntypedBool:
return "jboolean"
case types.Int:
return "jlong"
case types.Int8:
return "jbyte"
case types.Int16:
return "jshort"
case types.Int32, types.UntypedRune: // types.Rune
return "jint"
case types.Int64, types.UntypedInt:
return "jlong"
case types.Uint8: // types.Byte
// TODO(crawshaw): Java bytes are signed, so this is
// questionable, but vital.
return "jbyte"
// TODO(crawshaw): case types.Uint, types.Uint16, types.Uint32, types.Uint64:
case types.Float32:
return "jfloat"
case types.Float64, types.UntypedFloat:
return "jdouble"
case types.String, types.UntypedString:
return "jstring"
default:
g.errorf("unsupported basic type: %s", T)
return "TODO"
}
case *types.Slice:
return "jbyteArray"
case *types.Pointer:
if _, ok := T.Elem().(*types.Named); ok {
return g.jniType(T.Elem())
}
g.errorf("unsupported pointer to type: %s", T)
case *types.Named:
return "jobject"
default:
g.errorf("unsupported jniType: %#+v, %s\n", T, T)
}
return "TODO"
}
示例14: genRead
func (g *objcGen) genRead(toName, fromName string, t types.Type, mode varMode) {
if isErrorType(t) {
g.genRead(toName, fromName, types.Typ[types.String], mode)
return
}
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
g.Printf("NSString *%s = go_seq_to_objc_string(%s);\n", toName, fromName)
case types.Bool:
g.Printf("BOOL %s = %s ? YES : NO;\n", toName, fromName)
default:
g.Printf("%s %s = (%s)%s;\n", g.objcType(t), toName, g.objcType(t), fromName)
}
case *types.Slice:
switch e := t.Elem().(type) {
case *types.Basic:
switch e.Kind() {
case types.Uint8: // Byte.
g.Printf("NSData *%s = go_seq_to_objc_bytearray(%s, %d);\n", toName, fromName, g.toCFlag(mode == modeRetained))
default:
g.errorf("unsupported type: %s", t)
}
default:
g.errorf("unsupported type: %s", t)
}
case *types.Pointer:
switch t := t.Elem().(type) {
case *types.Named:
g.genRefRead(toName, fromName, types.NewPointer(t))
default:
g.errorf("unsupported type %s", t)
}
case *types.Named:
switch t.Underlying().(type) {
case *types.Interface, *types.Pointer:
g.genRefRead(toName, fromName, t)
default:
g.errorf("unsupported, direct named type %s", t)
}
default:
g.Printf("%s %s = (%s)%s;\n", g.objcType(t), toName, g.objcType(t), fromName)
}
}
示例15: store
// store stores value v of type T into *addr.
func store(T types.Type, addr *value, v value) {
switch T := T.Underlying().(type) {
case *types.Struct:
lhs := (*addr).(structure)
rhs := v.(structure)
for i := range lhs {
store(T.Field(i).Type(), &lhs[i], rhs[i])
}
case *types.Array:
lhs := (*addr).(array)
rhs := v.(array)
for i := range lhs {
store(T.Elem(), &lhs[i], rhs[i])
}
default:
*addr = v
}
}