checker/cgen: add checks and fix cgen for [typedef] for C structs (#8169)

pull/8172/head
Swastik Baranwal 2021-01-17 20:48:07 +05:30 committed by GitHub
parent 460f32baf2
commit 334b66b311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 2 deletions

View File

@ -4,7 +4,6 @@ module big
#flag -I @VROOT/thirdparty/bignum #flag -I @VROOT/thirdparty/bignum
#flag @VROOT/thirdparty/bignum/bn.o #flag @VROOT/thirdparty/bignum/bn.o
#include "bn.h" #include "bn.h"
[typedef]
struct C.bn { struct C.bn {
mut: mut:
array [32]u32 array [32]u32

View File

@ -387,6 +387,11 @@ pub fn (mut c Checker) struct_decl(mut decl ast.StructDecl) {
c.error('`$embed_sym.name` is not a struct', embed.pos) c.error('`$embed_sym.name` is not a struct', embed.pos)
} }
} }
for attr in decl.attrs {
if attr.name == 'typedef' && decl.language != .c {
c.error('`typedef` attribute can only be used with C structs', decl.pos)
}
}
for i, field in decl.fields { for i, field in decl.fields {
if decl.language == .v { if decl.language == .v {
c.check_valid_snake_case(field.name, 'field name', field.pos) c.check_valid_snake_case(field.name, 'field name', field.pos)

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/typedef_attr_v_struct_err.vv:2:1: error: `typedef` attribute can only be used with C structs
1 | [typedef]
2 | struct Point{}
| ~~~~~~~~~~~~
3 |
4 | fn main() {

View File

@ -0,0 +1,7 @@
[typedef]
struct Point{}
fn main() {
p := Point{}
println(p)
}

View File

@ -660,7 +660,15 @@ typedef struct {
.alias { .alias {
parent := unsafe { &g.table.types[typ.parent_idx] } parent := unsafe { &g.table.types[typ.parent_idx] }
is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.` is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.`
parent_styp := if is_c_parent { 'struct ' + parent.cname[3..] } else { parent.cname } mut is_typedef := false
if parent.info is table.Struct {
is_typedef = parent.info.is_typedef
}
parent_styp := if is_c_parent {
if !is_typedef { 'struct ' + parent.cname[3..] } else { parent.cname[3..] }
} else {
parent.cname
}
g.type_definitions.writeln('typedef $parent_styp $typ.cname;') g.type_definitions.writeln('typedef $parent_styp $typ.cname;')
} }
.array { .array {