本文整理汇总了Golang中go/types.Type.Kind方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Kind方法的具体用法?Golang Type.Kind怎么用?Golang Type.Kind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/types.Type
的用法示例。
在下文中一共展示了Type.Kind方法的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: zeroConst
// zeroConst returns a new "zero" constant of the specified type,
// which must not be an array or struct type: the zero values of
// aggregates are well-defined but cannot be represented by Const.
//
func zeroConst(t types.Type) *Const {
switch t := t.(type) {
case *types.Basic:
switch {
case t.Info()&types.IsBoolean != 0:
return NewConst(exact.MakeBool(false), t)
case t.Info()&types.IsNumeric != 0:
return NewConst(exact.MakeInt64(0), t)
case t.Info()&types.IsString != 0:
return NewConst(exact.MakeString(""), t)
case t.Kind() == types.UnsafePointer:
fallthrough
case t.Kind() == types.UntypedNil:
return nilConst(t)
default:
panic(fmt.Sprint("zeroConst for unexpected type:", t))
}
case *types.Pointer, *types.Slice, *types.Interface, *types.Chan, *types.Map, *types.Signature:
return nilConst(t)
case *types.Named:
return NewConst(zeroConst(t.Underlying()).Value, t)
case *types.Array, *types.Struct, *types.Tuple:
panic(fmt.Sprint("zeroConst applied to aggregate:", t))
}
panic(fmt.Sprint("zeroConst: unexpected ", t))
}
示例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: 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: javaType
// javaType returns a string that can be used as a Java type.
func (g *javaGen) javaType(T types.Type) string {
switch T := T.(type) {
case *types.Basic:
switch T.Kind() {
case types.Bool:
return "boolean"
case types.Int:
return "long"
case types.Int8:
return "byte"
case types.Int16:
return "short"
case types.Int32:
return "int"
case types.Int64:
return "long"
case types.Uint8:
// TODO(crawshaw): Java bytes are signed, so this is
// questionable, but vital.
return "byte"
// TODO(crawshaw): case types.Uint, types.Uint16, types.Uint32, types.Uint64:
case types.Float32:
return "float"
case types.Float64:
return "double"
case types.String:
return "String"
default:
g.errorf("unsupported return type: %s", T)
return "TODO"
}
case *types.Slice:
elem := g.javaType(T.Elem())
return elem + "[]"
case *types.Pointer:
if _, ok := T.Elem().(*types.Named); ok {
return g.javaType(T.Elem())
}
panic(fmt.Sprintf("unsupporter pointer to type: %s", T))
case *types.Named:
n := T.Obj()
if n.Pkg() != g.pkg {
panic(fmt.Sprintf("type %s is in package %s, must be defined in package %s", n.Name(), n.Pkg().Name(), g.pkg.Name()))
}
// TODO(crawshaw): more checking here
return n.Name()
default:
g.errorf("unsupported javaType: %#+v, %s\n", T, T)
return "TODO"
}
}
示例8: 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"
}
示例9: 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)
}
}
示例10: 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"
}
示例11: 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)
}
}
示例12: jniSigType
func (g *javaGen) jniSigType(T types.Type) string {
if isErrorType(T) {
return g.jniSigType(types.Typ[types.String])
}
switch T := T.(type) {
case *types.Basic:
switch T.Kind() {
case types.Bool, types.UntypedBool:
return "Z"
case types.Int:
return "J"
case types.Int8:
return "B"
case types.Int16:
return "S"
case types.Int32, types.UntypedRune: // types.Rune
return "I"
case types.Int64, types.UntypedInt:
return "J"
case types.Uint8: // types.Byte
return "B"
case types.Float32:
return "F"
case types.Float64, types.UntypedFloat:
return "D"
case types.String, types.UntypedString:
return "Ljava/lang/String;"
default:
g.errorf("unsupported basic type: %s", T)
return "TODO"
}
case *types.Slice:
return "[" + g.jniSigType(T.Elem())
case *types.Pointer:
if _, ok := T.Elem().(*types.Named); ok {
return g.jniSigType(T.Elem())
}
g.errorf("unsupported pointer to type: %s", T)
case *types.Named:
return "L" + g.jniClassSigPrefix(T.Obj().Pkg()) + T.Obj().Name() + ";"
default:
g.errorf("unsupported jniType: %#+v, %s\n", T, T)
}
return "TODO"
}
示例13: validResultType
func (f *File) validResultType(typ types.Type) *Error {
switch typ.(type) {
default:
name := typ.String()
if named, ok := typ.(*types.Named); ok {
name = named.Obj().Name()
}
return &Error{errors.New(fmt.Sprint("Invalid result type:", name)), 0}
case *types.Basic:
typ := typ.(*types.Basic)
switch typ.Kind() {
default:
return &Error{errors.New(fmt.Sprint("Invalid basic type for result type :", typ.Info())), 0}
case types.Bool:
return nil
case types.Int:
return nil
case types.Int8:
return nil
case types.Int16:
return nil
case types.Int32:
return nil
case types.Int64:
return nil
case types.Uint:
return nil
case types.Uint8:
return nil
case types.Uint16:
return nil
case types.Uint32:
return nil
case types.Uint64:
return nil
case types.Float32:
return nil
case types.Float64:
return nil
}
}
}
示例14: jniCallType
func (g *javaGen) jniCallType(t types.Type) string {
if isErrorType(t) {
return g.jniCallType(types.Typ[types.String])
}
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.Bool, types.UntypedBool:
return "Boolean"
case types.Int:
return "Long"
case types.Int8, types.Uint8: // types.Byte
return "Byte"
case types.Int16:
return "Short"
case types.Int32, types.UntypedRune: // types.Rune
return "Int"
case types.Int64, types.UntypedInt:
return "Long"
case types.Float32:
return "Float"
case types.Float64, types.UntypedFloat:
return "Double"
case types.String, types.UntypedString:
return "Object"
default:
g.errorf("unsupported basic type: %s", t)
}
case *types.Slice:
return "Object"
case *types.Pointer:
if _, ok := t.Elem().(*types.Named); ok {
return g.jniCallType(t.Elem())
}
g.errorf("unsupported pointer to type: %s", t)
case *types.Named:
return "Object"
default:
return "Object"
}
return "TODO"
}
示例15: genJavaToC
func (g *javaGen) genJavaToC(varName string, t types.Type, mode varMode) {
if isErrorType(t) {
g.genJavaToC(varName, types.Typ[types.String], mode)
return
}
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
g.Printf("nstring _%s = go_seq_from_java_string(env, %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_java_bytearray(env, %s, %d);\n", varName, varName, g.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.Printf("int32_t _%s = go_seq_to_refnum(env, %s);\n", varName, varName)
default:
g.errorf("unsupported named type: %s / %T", u, u)
}
case *types.Pointer:
g.Printf("int32_t _%s = go_seq_to_refnum(env, %s);\n", varName, varName)
default:
g.Printf("%s _%s = (%s)%s;\n", g.cgoType(t), varName, g.cgoType(t), varName)
}
}