parser: support casting to a pointer to array (#7861)

pull/7867/head
Nick Treleaven 2021-01-04 16:22:04 +00:00 committed by GitHub
parent 22085041f1
commit b9c6011602
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 6 deletions

View File

@ -831,10 +831,9 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) {
mut is_pe_amp_ce := false
mut ce := ast.CastExpr{}
if fexpr is ast.PrefixExpr {
if fexpr.right is ast.CastExpr && fexpr.op == .amp {
ce = fexpr.right as ast.CastExpr
mut ce := fexpr.right as ast.CastExpr
ce.typname = f.table.get_type_symbol(ce.typ).name
is_pe_amp_ce = true
f.expr(ce)

View File

@ -1,19 +1,15 @@
const (
x = &Test{}
y = []&Test{}
z = &[]&Test{}
xx = &&Test{}
yy = []&&Test{}
zz = &&[]&&Test{}
)
fn test_type_ptr() {
_ := &Test{}
_ := []&Test{}
_ := &[]&Test{}
_ := &&Test{}
_ := []&&Test{}
_ := &&[]&&Test{}
}
struct Test {

View File

@ -127,6 +127,17 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
if p.expecting_type {
// parse json.decode type (`json.decode([]User, s)`)
node = p.name_expr()
} else if p.is_amp && p.peek_tok.kind == .rsbr {
pos := p.tok.position()
typ := p.parse_type().to_ptr()
p.check(.lpar)
expr := p.expr(0)
p.check(.rpar)
node = ast.CastExpr{
typ: typ
expr: expr
pos: pos
}
} else {
node = p.array_init()
}

View File

@ -0,0 +1,19 @@
fn test_array_cast() {
mut keys := ['']
unsafe {
vp := voidptr(&keys)
mut p := &[]string(vp)
(*p)[0] = 'hi'
assert *p == ['hi']
}
assert keys[0] == 'hi'
}
fn test_int() {
mut arr := [2.3,3]
unsafe {
vp := voidptr(&arr)
p := &[]f64(vp)
assert *p == arr
}
}