Commit 63dfa906 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 1e542169
This diff is collapsed.
...@@ -211,7 +211,7 @@ func (b *Buffer) emit(format string, a ...interface{}) { ...@@ -211,7 +211,7 @@ func (b *Buffer) emit(format string, a ...interface{}) {
// interface of codegenerator for coder/decoder // interface of codegenerator for coder/decoder
type CodecCodeGen interface { type CodecCodeGen interface {
// tell codegen it should generate code for top-level function // tell codegen it should generate code for top-level function
setFunc(recvName, typeName string) setFunc(recvName, typeName string, typ types.Type)
// emit code to process basic fixed types (not string) // emit code to process basic fixed types (not string)
// userType is type actually used in source (for which typ is underlying), or nil // userType is type actually used in source (for which typ is underlying), or nil
...@@ -237,14 +237,16 @@ type CodecCodeGen interface { ...@@ -237,14 +237,16 @@ type CodecCodeGen interface {
type commonCoder struct { type commonCoder struct {
recvName string // receiver/type for top-level func recvName string // receiver/type for top-level func
typeName string // or empty typeName string // or empty
typ types.Type
varN int // suffix to add to variables (size0, size1, ...) - for nested computations varN int // suffix to add to variables (size0, size1, ...) - for nested computations
varUsed map[string]bool // whether a variable was used varUsed map[string]bool // whether a variable was used
} }
func (c *commonCoder) setFunc(recvName, typeName string) { func (c *commonCoder) setFunc(recvName, typeName string, typ types.Type) {
c.recvName = recvName c.recvName = recvName
c.typeName = typeName c.typeName = typeName
c.typ = typ
} }
// get variable name for varname // get variable name for varname
...@@ -367,9 +369,11 @@ func (d *decoder) generatedCode() string { ...@@ -367,9 +369,11 @@ func (d *decoder) generatedCode() string {
} }
code.emit("return %v, nil", retexpr) code.emit("return %v, nil", retexpr)
// overflow is not used only for empty structs
if (&types.StdSizes{8, 8}).Sizeof(d.typ) > 0 {
code.emit("\noverflow:") code.emit("\noverflow:")
code.emit("return 0, ErrDecodeOverflow") code.emit("return 0, ErrDecodeOverflow")
code.emit("goto overflow") // TODO check if overflow used at all and remove }
code.emit("}\n") code.emit("}\n")
return code.String() return code.String()
...@@ -638,12 +642,11 @@ func codegenType(path string, typ types.Type, obj types.Object, codegen CodecCod ...@@ -638,12 +642,11 @@ func codegenType(path string, typ types.Type, obj types.Object, codegen CodecCod
// generate encoder/decode funcs for a type declaration typespec // generate encoder/decode funcs for a type declaration typespec
func generateCodecCode(typespec *ast.TypeSpec, codec CodecCodeGen) string { func generateCodecCode(typespec *ast.TypeSpec, codec CodecCodeGen) string {
codec.setFunc("p", typespec.Name.Name)
// type & object which refers to this type // type & object which refers to this type
typ := info.Types[typespec.Type].Type typ := info.Types[typespec.Type].Type
obj := info.Defs[typespec.Name] obj := info.Defs[typespec.Name]
codec.setFunc("p", typespec.Name.Name, typ)
codegenType("p", typ, obj, codec) codegenType("p", typ, obj, codec)
return codec.generatedCode() return codec.generatedCode()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment