github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/bind/seq.go (about)

     1  package bind
     2  
     3  import (
     4  	"fmt"
     5  	"go/types"
     6  )
     7  
     8  // seqType returns a string that can be used for reading and writing a
     9  // type using the seq library.
    10  // TODO(hyangah): avoid panic; gobind needs to output the problematic code location.
    11  func seqType(t types.Type) string {
    12  	if isErrorType(t) {
    13  		return "String"
    14  	}
    15  	switch t := t.(type) {
    16  	case *types.Basic:
    17  		switch t.Kind() {
    18  		case types.Bool:
    19  			return "Bool"
    20  		case types.Int:
    21  			return "Int"
    22  		case types.Int8:
    23  			return "Int8"
    24  		case types.Int16:
    25  			return "Int16"
    26  		case types.Int32:
    27  			return "Int32"
    28  		case types.Int64:
    29  			return "Int64"
    30  		case types.Uint8: // Byte.
    31  			// TODO(crawshaw): questionable, but vital?
    32  			return "Byte"
    33  		// TODO(crawshaw): case types.Uint, types.Uint16, types.Uint32, types.Uint64:
    34  		case types.Float32:
    35  			return "Float32"
    36  		case types.Float64:
    37  			return "Float64"
    38  		case types.String:
    39  			return "String"
    40  		default:
    41  			// Should be caught earlier in processing.
    42  			panic(fmt.Sprintf("unsupported basic seqType: %s", t))
    43  		}
    44  	case *types.Named:
    45  		switch u := t.Underlying().(type) {
    46  		case *types.Interface:
    47  			return "Ref"
    48  		default:
    49  			panic(fmt.Sprintf("unsupported named seqType: %s / %T", u, u))
    50  		}
    51  	case *types.Slice:
    52  		switch e := t.Elem().(type) {
    53  		case *types.Basic:
    54  			switch e.Kind() {
    55  			case types.Uint8: // Byte.
    56  				return "ByteArray"
    57  			default:
    58  				panic(fmt.Sprintf("unsupported seqType: %s(%s) / %T(%T)", t, e, t, e))
    59  			}
    60  		default:
    61  			panic(fmt.Sprintf("unsupported seqType: %s(%s) / %T(%T)", t, e, t, e))
    62  		}
    63  	// TODO: let the types.Array case handled like types.Slice?
    64  	case *types.Pointer:
    65  		if _, ok := t.Elem().(*types.Named); ok {
    66  			return "Ref"
    67  		}
    68  		panic(fmt.Sprintf("not supported yet, pointer type: %s / %T", t, t))
    69  
    70  	default:
    71  		panic(fmt.Sprintf("unsupported seqType: %s / %T", t, t))
    72  	}
    73  }
    74  
    75  func seqRead(o types.Type) string {
    76  	t := seqType(o)
    77  	return t + "()"
    78  }
    79  
    80  func seqWrite(o types.Type, name string) string {
    81  	t := seqType(o)
    82  	if t == "Ref" {
    83  		// TODO(crawshaw): do something cleaner, i.e. genWrite.
    84  		return t + "(" + name + ".ref())"
    85  	}
    86  	return t + "(" + name + ")"
    87  }