Skip to content

Commit

Permalink
Cleaned up type checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Aug 5, 2024
1 parent 6a8b035 commit 9894752
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions compiler/ast/ssagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,15 +1867,15 @@ func (ast *Slice) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
return nil, nil, ctx.Errorf(ast, "invalid expression")
}
expr := exprs[0]
elementType := expr.IndirectType()
if !elementType.Type.Array() {
arrayType := expr.IndirectType()
if !arrayType.Type.Array() {
return nil, nil, ctx.Errorf(ast, "invalid operation: cannot slice %v",
expr.Type.Type)
}
elementSize := elementType.ElementType.Bits
elementSize := arrayType.ElementType.Bits

var from, to types.Size
block, from, to, err = ast.limitsSSA(block, ctx, gen, elementType)
block, from, to, err = ast.limitsSSA(block, ctx, gen, arrayType)
if err != nil {
return nil, nil, err
}
Expand All @@ -1885,16 +1885,11 @@ func (ast *Slice) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
var t ssa.Value

if expr.Type.Type == types.TPtr {
if !elementType.Type.Array() {
return nil, nil, ctx.Errorf(ast, "slice of %s not supported",
expr.Type)
}

// Take a copy of the PtrInfo and adjust its offset.
ptrInfo := *expr.PtrInfo
ptrInfo.Offset += from * elementSize

et := elementType
et := arrayType
et.Type = types.TSlice
et.ID = 0
et.ArraySize = to - from
Expand All @@ -1910,16 +1905,14 @@ func (ast *Slice) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
t.PtrInfo = &ptrInfo
} else {
ti := types.Info{
Type: elementType.Type,
Type: arrayType.Type,
IsConcrete: true,
Bits: bits,
MinBits: bits,
}
if elementType.Type.Array() {
ti.Type = types.TSlice
ti.ElementType = elementType.ElementType
ti.ArraySize = ti.Bits / ti.ElementType.Bits
}
ti.Type = types.TSlice
ti.ElementType = arrayType.ElementType
ti.ArraySize = ti.Bits / ti.ElementType.Bits

t = gen.AnonVal(ti)
}
Expand Down

0 comments on commit 9894752

Please sign in to comment.