diff --git a/internal/encoder/code.go b/internal/encoder/code.go index c842b8b..71189a5 100644 --- a/internal/encoder/code.go +++ b/internal/encoder/code.go @@ -109,10 +109,9 @@ func (c *UintCode) ToOpcode(ctx *compileContext) Opcodes { } type FloatCode struct { - typ *runtime.Type - bitSize uint8 - isString bool - isPtr bool + typ *runtime.Type + bitSize uint8 + isPtr bool } func (c *FloatCode) Kind() CodeKind { @@ -142,9 +141,8 @@ func (c *FloatCode) ToOpcode(ctx *compileContext) Opcodes { } type StringCode struct { - typ *runtime.Type - isString bool - isPtr bool + typ *runtime.Type + isPtr bool } func (c *StringCode) Kind() CodeKind { @@ -152,16 +150,16 @@ func (c *StringCode) Kind() CodeKind { } func (c *StringCode) ToOpcode(ctx *compileContext) Opcodes { - isJsonNumberType := c.typ == runtime.Type2RType(jsonNumberType) + isJSONNumberType := c.typ == runtime.Type2RType(jsonNumberType) var code *Opcode if c.isPtr { - if isJsonNumberType { + if isJSONNumberType { code = newOpCode(ctx, c.typ, OpNumberPtr) } else { code = newOpCode(ctx, c.typ, OpStringPtr) } } else { - if isJsonNumberType { + if isJSONNumberType { code = newOpCode(ctx, c.typ, OpNumber) } else { code = newOpCode(ctx, c.typ, OpString) @@ -172,9 +170,8 @@ func (c *StringCode) ToOpcode(ctx *compileContext) Opcodes { } type BoolCode struct { - typ *runtime.Type - isString bool - isPtr bool + typ *runtime.Type + isPtr bool } func (c *BoolCode) Kind() CodeKind { @@ -337,12 +334,11 @@ func (c *MapCode) ToOpcode(ctx *compileContext) Opcodes { type StructCode struct { typ *runtime.Type - isPtr bool fields []*StructFieldCode + isPtr bool disableIndirectConversion bool isIndirect bool isRecursive bool - recursiveCodes Opcodes } func (c *StructCode) Kind() CodeKind { @@ -362,7 +358,7 @@ func (c *StructCode) lastFieldCode(field *StructFieldCode, firstField *Opcode) * func (c *StructCode) lastAnonymousFieldCode(firstField *Opcode) *Opcode { // firstField is special StructHead operation for anonymous structure. - // So, StructHead's next operation is truely struct head operation. + // So, StructHead's next operation is truly struct head operation. lastField := firstField.Next for lastField.NextField != nil { lastField = lastField.NextField diff --git a/internal/encoder/compiler.go b/internal/encoder/compiler.go index a7e6e99..cbcdb65 100644 --- a/internal/encoder/compiler.go +++ b/internal/encoder/compiler.go @@ -248,102 +248,167 @@ func (c *Compiler) typeToCodeWithPtr(typ *runtime.Type, isPtr bool) (Code, error const intSize = 32 << (^uint(0) >> 63) +//nolint:unparam func (c *Compiler) intCode(typ *runtime.Type, isPtr bool) (*IntCode, error) { return &IntCode{typ: typ, bitSize: intSize, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) int8Code(typ *runtime.Type, isPtr bool) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 8, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) int16Code(typ *runtime.Type, isPtr bool) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 16, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) int32Code(typ *runtime.Type, isPtr bool) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) int64Code(typ *runtime.Type, isPtr bool) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) uintCode(typ *runtime.Type, isPtr bool) (*UintCode, error) { return &UintCode{typ: typ, bitSize: intSize, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) uint8Code(typ *runtime.Type, isPtr bool) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 8, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) uint16Code(typ *runtime.Type, isPtr bool) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 16, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) uint32Code(typ *runtime.Type, isPtr bool) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) uint64Code(typ *runtime.Type, isPtr bool) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) float32Code(typ *runtime.Type, isPtr bool) (*FloatCode, error) { return &FloatCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) float64Code(typ *runtime.Type, isPtr bool) (*FloatCode, error) { return &FloatCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) stringCode(typ *runtime.Type, isPtr bool) (*StringCode, error) { return &StringCode{typ: typ, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) boolCode(typ *runtime.Type, isPtr bool) (*BoolCode, error) { return &BoolCode{typ: typ, isPtr: isPtr}, nil } +//nolint:unparam func (c *Compiler) intStringCode(typ *runtime.Type) (*IntCode, error) { return &IntCode{typ: typ, bitSize: intSize, isString: true}, nil } +//nolint:unparam func (c *Compiler) int8StringCode(typ *runtime.Type) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 8, isString: true}, nil } +//nolint:unparam func (c *Compiler) int16StringCode(typ *runtime.Type) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 16, isString: true}, nil } +//nolint:unparam func (c *Compiler) int32StringCode(typ *runtime.Type) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 32, isString: true}, nil } +//nolint:unparam func (c *Compiler) int64StringCode(typ *runtime.Type) (*IntCode, error) { return &IntCode{typ: typ, bitSize: 64, isString: true}, nil } +//nolint:unparam func (c *Compiler) uintStringCode(typ *runtime.Type) (*UintCode, error) { return &UintCode{typ: typ, bitSize: intSize, isString: true}, nil } +//nolint:unparam func (c *Compiler) uint8StringCode(typ *runtime.Type) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 8, isString: true}, nil } +//nolint:unparam func (c *Compiler) uint16StringCode(typ *runtime.Type) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 16, isString: true}, nil } +//nolint:unparam func (c *Compiler) uint32StringCode(typ *runtime.Type) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 32, isString: true}, nil } +//nolint:unparam func (c *Compiler) uint64StringCode(typ *runtime.Type) (*UintCode, error) { return &UintCode{typ: typ, bitSize: 64, isString: true}, nil } +//nolint:unparam +func (c *Compiler) bytesCode(typ *runtime.Type, isPtr bool) (*BytesCode, error) { + return &BytesCode{typ: typ, isPtr: isPtr}, nil +} + +//nolint:unparam +func (c *Compiler) interfaceCode(typ *runtime.Type, isPtr bool) (*InterfaceCode, error) { + return &InterfaceCode{typ: typ, isPtr: isPtr}, nil +} + +//nolint:unparam +func (c *Compiler) marshalJSONCode(typ *runtime.Type) (*MarshalJSONCode, error) { + return &MarshalJSONCode{ + typ: typ, + isAddrForMarshaler: c.isPtrMarshalJSONType(typ), + isNilableType: c.isNilableType(typ), + isMarshalerContext: typ.Implements(marshalJSONContextType) || runtime.PtrTo(typ).Implements(marshalJSONContextType), + }, nil +} + +//nolint:unparam +func (c *Compiler) marshalTextCode(typ *runtime.Type) (*MarshalTextCode, error) { + return &MarshalTextCode{ + typ: typ, + isAddrForMarshaler: c.isPtrMarshalTextType(typ), + isNilableType: c.isNilableType(typ), + }, nil +} + +func (c *Compiler) ptrCode(typ *runtime.Type) (*PtrCode, error) { + code, err := c.typeToCodeWithPtr(typ.Elem(), true) + if err != nil { + return nil, err + } + ptr, ok := code.(*PtrCode) + if ok { + return &PtrCode{typ: typ, value: ptr.value, ptrNum: ptr.ptrNum + 1}, nil + } + return &PtrCode{typ: typ, value: code, ptrNum: 1}, nil +} + func (c *Compiler) sliceCode(typ *runtime.Type) (*SliceCode, error) { elem := typ.Elem() code, err := c.listElemCode(elem) @@ -386,43 +451,6 @@ func (c *Compiler) mapCode(typ *runtime.Type) (*MapCode, error) { return &MapCode{typ: typ, key: keyCode, value: valueCode}, nil } -func (c *Compiler) bytesCode(typ *runtime.Type, isPtr bool) (*BytesCode, error) { - return &BytesCode{typ: typ, isPtr: isPtr}, nil -} - -func (c *Compiler) interfaceCode(typ *runtime.Type, isPtr bool) (*InterfaceCode, error) { - return &InterfaceCode{typ: typ, isPtr: isPtr}, nil -} - -func (c *Compiler) marshalJSONCode(typ *runtime.Type) (*MarshalJSONCode, error) { - return &MarshalJSONCode{ - typ: typ, - isAddrForMarshaler: c.isPtrMarshalJSONType(typ), - isNilableType: c.isNilableType(typ), - isMarshalerContext: typ.Implements(marshalJSONContextType) || runtime.PtrTo(typ).Implements(marshalJSONContextType), - }, nil -} - -func (c *Compiler) marshalTextCode(typ *runtime.Type) (*MarshalTextCode, error) { - return &MarshalTextCode{ - typ: typ, - isAddrForMarshaler: c.isPtrMarshalTextType(typ), - isNilableType: c.isNilableType(typ), - }, nil -} - -func (c *Compiler) ptrCode(typ *runtime.Type) (*PtrCode, error) { - code, err := c.typeToCodeWithPtr(typ.Elem(), true) - if err != nil { - return nil, err - } - ptr, ok := code.(*PtrCode) - if ok { - return &PtrCode{typ: typ, value: ptr.value, ptrNum: ptr.ptrNum + 1}, nil - } - return &PtrCode{typ: typ, value: code, ptrNum: 1}, nil -} - func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) { switch { case c.isPtrMarshalJSONType(typ): diff --git a/internal/encoder/opcode.go b/internal/encoder/opcode.go index 3601f2b..cfd2d5a 100644 --- a/internal/encoder/opcode.go +++ b/internal/encoder/opcode.go @@ -428,32 +428,6 @@ func (c *Opcode) TotalLength() int { return idx + 1 } -func (c *Opcode) decOpcodeIndex() { - for code := c; !code.IsEnd(); { - code.DisplayIdx-- - if code.Idx > 0 { - code.Idx -= uintptrSize - } - if code.ElemIdx > 0 { - code.ElemIdx -= uintptrSize - } - if code.MapIter > 0 { - code.MapIter -= uintptrSize - } - if code.Length > 0 && code.Op.CodeType() != CodeArrayHead && code.Op.CodeType() != CodeArrayElem { - code.Length -= uintptrSize - } - code = code.IterNext() - } -} - -func (c *Opcode) decIndent() { - for code := c; !code.IsEnd(); { - code.Indent-- - code = code.IterNext() - } -} - func (c *Opcode) dumpHead(code *Opcode) string { var length uint32 if code.Op.CodeType() == CodeArrayHead {