1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

&C.Foo(0) cast to replace &C.Foo{!} hack

This commit is contained in:
Alexander Medvednikov 2019-12-04 02:52:32 +03:00
parent a9aaa13a09
commit 2bafd41183
3 changed files with 23 additions and 4 deletions

View File

@ -182,6 +182,22 @@ fn (p mut Parser) name_expr() string {
if p.peek() == .lcbr && p.table.known_type(name) {
return p.get_struct_type(name, true, ptr)
}
if ptr && p.peek() == .lpar {
peek2 := p.tokens[p.token_idx+1]
// `&C.Foo(0)` cast (replacing old `&C.Foo{!}`)
if peek2.tok == .number && peek2.lit == '0' {
p.cgen.insert_before('struct /*C.Foo(0)*/ ')
p.gen('0')
p.next()
p.next()
p.next()
p.next()
return name + '*'
}
// `&C.Foo(foo)` cast
p.cast(name + '*')
return name + '*'
}
// C function
if p.peek() == .lpar {
return p.get_c_func_type(name)
@ -232,22 +248,22 @@ fn (p mut Parser) name_expr() string {
if p.known_var_check_new_var(name) {
return p.get_var_type(name, ptr, deref_nr)
}
// if known_type || is_c_struct_init || (p.first_pass() && p.peek() == .lcbr) {
// known type? int(4.5) or Color.green (enum)
if p.table.known_type(name) {
// cast expression: float(5), byte(0), (*int)(ptr) etc
if !is_c && ( p.peek() == .lpar || (deref && p.peek() == .rpar) ) {
//if !is_c && ( p.peek() == .lpar || (deref && p.peek() == .rpar) ) {
if p.peek() == .lpar || (deref && p.peek() == .rpar) {
if deref {
name += '*'.repeat(deref_nr )
}
else if ptr {
name += '*'.repeat(mul_nr)
}
p.gen('(')
//p.gen('(')
mut typ := name
p.cast(typ)
p.gen(')')
//p.gen(')')
for p.tok == .dot {
typ = p.dot(typ, ph)
}

View File

@ -507,6 +507,8 @@ fn (p mut Parser) gen_empty_map(typ string) {
}
fn (p mut Parser) cast(typ string) {
p.gen('(')
defer { p.gen(')') }
p.next()
pos := p.cgen.add_placeholder()
if p.tok == .rpar {

View File

@ -31,6 +31,7 @@ pub fn ls(path string) ?[]string {
if isnil(dir) {
return error('ls() couldnt open dir "$path"')
}
//mut ent := &C.dirent(0)
mut ent := &C.dirent{!}
for {
ent = C.readdir(dir)