2014-07-31 19:09:13 +00:00
|
|
|
package bind
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2014-11-09 21:55:57 +00:00
|
|
|
"golang.org/x/tools/go/types"
|
2014-07-31 19:09:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// seqType returns a string that can be used for reading and writing a
|
|
|
|
// type using the seq library.
|
2015-01-06 18:50:43 +00:00
|
|
|
// TODO(hyangah): avoid panic; gobind needs to output the problematic code location.
|
2014-07-31 19:09:13 +00:00
|
|
|
func seqType(t types.Type) string {
|
|
|
|
if isErrorType(t) {
|
2015-04-06 20:43:18 +00:00
|
|
|
return "String"
|
2014-07-31 19:09:13 +00:00
|
|
|
}
|
|
|
|
switch t := t.(type) {
|
|
|
|
case *types.Basic:
|
|
|
|
switch t.Kind() {
|
2015-05-14 19:46:09 +00:00
|
|
|
case types.Bool:
|
|
|
|
return "Bool"
|
2014-07-31 19:09:13 +00:00
|
|
|
case types.Int:
|
|
|
|
return "Int"
|
|
|
|
case types.Int8:
|
|
|
|
return "Int8"
|
|
|
|
case types.Int16:
|
|
|
|
return "Int16"
|
|
|
|
case types.Int32:
|
|
|
|
return "Int32"
|
|
|
|
case types.Int64:
|
|
|
|
return "Int64"
|
2014-12-19 17:59:43 +00:00
|
|
|
case types.Uint8: // Byte.
|
2014-07-31 19:09:13 +00:00
|
|
|
// TODO(crawshaw): questionable, but vital?
|
|
|
|
return "Byte"
|
|
|
|
// TODO(crawshaw): case types.Uint, types.Uint16, types.Uint32, types.Uint64:
|
|
|
|
case types.Float32:
|
|
|
|
return "Float32"
|
|
|
|
case types.Float64:
|
|
|
|
return "Float64"
|
|
|
|
case types.String:
|
2015-04-06 20:43:18 +00:00
|
|
|
return "String"
|
2014-07-31 19:09:13 +00:00
|
|
|
default:
|
|
|
|
// Should be caught earlier in processing.
|
2014-12-19 17:59:43 +00:00
|
|
|
panic(fmt.Sprintf("unsupported basic seqType: %s", t))
|
2014-07-31 19:09:13 +00:00
|
|
|
}
|
|
|
|
case *types.Named:
|
|
|
|
switch u := t.Underlying().(type) {
|
|
|
|
case *types.Interface:
|
|
|
|
return "Ref"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unsupported named seqType: %s / %T", u, u))
|
|
|
|
}
|
2014-12-19 17:59:43 +00:00
|
|
|
case *types.Slice:
|
|
|
|
switch e := t.Elem().(type) {
|
|
|
|
case *types.Basic:
|
|
|
|
switch e.Kind() {
|
|
|
|
case types.Uint8: // Byte.
|
|
|
|
return "ByteArray"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unsupported seqType: %s(%s) / %T(%T)", t, e, t, e))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unsupported seqType: %s(%s) / %T(%T)", t, e, t, e))
|
|
|
|
}
|
|
|
|
// TODO: let the types.Array case handled like types.Slice?
|
2015-01-06 18:50:43 +00:00
|
|
|
case *types.Pointer:
|
|
|
|
if _, ok := t.Elem().(*types.Named); ok {
|
|
|
|
return "Ref"
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("not supported yet, pointer type: %s / %T", t, t))
|
|
|
|
|
2014-07-31 19:09:13 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unsupported seqType: %s / %T", t, t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func seqRead(o types.Type) string {
|
|
|
|
t := seqType(o)
|
|
|
|
return t + "()"
|
|
|
|
}
|
|
|
|
|
|
|
|
func seqWrite(o types.Type, name string) string {
|
|
|
|
t := seqType(o)
|
|
|
|
if t == "Ref" {
|
|
|
|
// TODO(crawshaw): do something cleaner, i.e. genWrite.
|
|
|
|
return t + "(" + name + ".ref())"
|
|
|
|
}
|
|
|
|
return t + "(" + name + ")"
|
|
|
|
}
|