fix some bugs

This commit is contained in:
Masaaki Goshima 2021-11-26 13:28:30 +09:00
parent 48b67e9378
commit 125177da35
No known key found for this signature in database
GPG Key ID: 6A53785055537153
1 changed files with 40 additions and 21 deletions

View File

@ -381,6 +381,8 @@ func (c *StructCode) ToOpcode(ctx *compileContext) Opcodes {
} else { } else {
fieldCodes.First().End = fieldCodes.Last() fieldCodes.First().End = fieldCodes.Last()
} }
codes = append(codes, fieldCodes...)
break
} }
if field.isAnonymous { if field.isAnonymous {
// fieldCodes.First() is StructHead operation. // fieldCodes.First() is StructHead operation.
@ -400,24 +402,25 @@ func (c *StructCode) ToOpcode(ctx *compileContext) Opcodes {
codes = append(codes, fieldCodes...) codes = append(codes, fieldCodes...)
} }
if len(codes) == 0 { if len(codes) == 0 {
head := &Opcode{
Op: OpStructHead,
Idx: opcodeOffset(ctx.ptrIndex),
Type: c.typ,
DisplayIdx: ctx.opcodeIndex,
Indent: ctx.indent,
}
ctx.incOpcodeIndex()
end := &Opcode{ end := &Opcode{
Op: OpStructEnd, Op: OpStructEnd,
Idx: opcodeOffset(ctx.ptrIndex), Idx: opcodeOffset(ctx.ptrIndex),
DisplayIdx: ctx.opcodeIndex, DisplayIdx: ctx.opcodeIndex,
Indent: ctx.indent, Indent: ctx.indent,
} }
head := &Opcode{ head.NextField = end
Op: OpStructHead, head.Next = end
Idx: opcodeOffset(ctx.ptrIndex), head.End = end
NextField: end,
Type: c.typ,
DisplayIdx: ctx.opcodeIndex,
Indent: ctx.indent,
Next: end,
End: end,
}
codes = append(codes, head, end)
end.PrevField = head end.PrevField = head
codes = append(codes, head, end)
ctx.incIndex() ctx.incIndex()
} }
ctx = ctx.decIndent() ctx = ctx.decIndent()
@ -590,7 +593,12 @@ func (c *StructFieldCode) ToOpcode(ctx *compileContext, isFirstField, isEndField
ctx.incIndex() ctx.incIndex()
var codes Opcodes var codes Opcodes
if c.isAnonymous { if c.isAnonymous {
codes = c.value.(AnonymousCode).ToAnonymousOpcode(ctx.withType(c.typ)) anonymCode, ok := c.value.(AnonymousCode)
if ok {
codes = anonymCode.ToAnonymousOpcode(ctx.withType(c.typ))
} else {
codes = c.value.ToOpcode(ctx.withType(c.typ))
}
} else { } else {
codes = c.value.ToOpcode(ctx.withType(c.typ)) codes = c.value.ToOpcode(ctx.withType(c.typ))
} }
@ -633,7 +641,7 @@ func (c *StructFieldCode) ToOpcode(ctx *compileContext, isFirstField, isEndField
// optimize codes // optimize codes
ctx.decIndex() ctx.decIndex()
} }
if isEndField && !c.isAnonymous { if isEndField {
if isEnableStructEndOptimizationType(c.value.Type()) { if isEnableStructEndOptimizationType(c.value.Type()) {
field.Op = field.Op.FieldToEnd() field.Op = field.Op.FieldToEnd()
} else { } else {
@ -696,7 +704,12 @@ func (c *StructFieldCode) ToAnonymousOpcode(ctx *compileContext, isFirstField, i
ctx.incIndex() ctx.incIndex()
var codes Opcodes var codes Opcodes
if c.isAnonymous { if c.isAnonymous {
codes = c.value.(AnonymousCode).ToAnonymousOpcode(ctx.withType(c.typ)) anonymCode, ok := c.value.(AnonymousCode)
if ok {
codes = anonymCode.ToAnonymousOpcode(ctx.withType(c.typ))
} else {
codes = c.value.ToOpcode(ctx.withType(c.typ))
}
} else { } else {
codes = c.value.ToOpcode(ctx.withType(c.typ)) codes = c.value.ToOpcode(ctx.withType(c.typ))
} }
@ -827,7 +840,13 @@ func (c *PtrCode) ToOpcode(ctx *compileContext) Opcodes {
} }
func (c *PtrCode) ToAnonymousOpcode(ctx *compileContext) Opcodes { func (c *PtrCode) ToAnonymousOpcode(ctx *compileContext) Opcodes {
codes := c.value.(AnonymousCode).ToAnonymousOpcode(ctx.withType(c.typ.Elem())) var codes Opcodes
anonymCode, ok := c.value.(AnonymousCode)
if ok {
codes = anonymCode.ToAnonymousOpcode(ctx.withType(c.typ.Elem()))
} else {
codes = c.value.ToOpcode(ctx.withType(c.typ.Elem()))
}
codes.First().Op = convertPtrOp(codes.First()) codes.First().Op = convertPtrOp(codes.First())
codes.First().PtrNum = c.ptrNum codes.First().PtrNum = c.ptrNum
return codes return codes
@ -1247,12 +1266,12 @@ func compileStruct2(ctx *compileContext, isPtr bool) (*StructCode, error) {
structCode := field.getAnonymousStruct() structCode := field.getAnonymousStruct()
if structCode != nil { if structCode != nil {
structCode.removeFieldsByTags(tags) structCode.removeFieldsByTags(tags)
} if isAssignableIndirect(field, isPtr) {
if isAssignableIndirect(field, isPtr) { if indirect {
if indirect { structCode.isIndirect = true
structCode.isIndirect = true } else {
} else { structCode.isIndirect = false
structCode.isIndirect = false }
} }
} }
} else { } else {