Skip to content

Commit 94f0f6d

Browse files
authored
cgen,checker: allow for pub type C.HINSTANCE = voidptr, being used in @[export: "wWinMain"] fn mymain(x C.HINSTANCE, xprev C.HINSTANCE, lpcmdline &C.WCHAR, cmdshow int) int { in module no_main programs (#23812)
1 parent c826923 commit 94f0f6d

5 files changed

Lines changed: 50 additions & 11 deletions

File tree

‎vlib/v/checker/checker.v‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,10 @@ fn (mut c Checker) alias_type_decl(mut node ast.AliasTypeDecl) {
550550
}
551551
.alias {
552552
orig_sym := c.table.sym((parent_typ_sym.info as ast.Alias).parent_type)
553-
c.error('type `${parent_typ_sym.str()}` is an alias, use the original alias type `${orig_sym.name}` instead',
554-
node.type_pos)
553+
if !node.name.starts_with('C.') {
554+
c.error('type `${parent_typ_sym.str()}` is an alias, use the original alias type `${orig_sym.name}` instead',
555+
node.type_pos)
556+
}
555557
}
556558
.chan {
557559
c.error('aliases of `chan` types are not allowed', node.type_pos)

‎vlib/v/gen/c/cgen.v‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,12 @@ pub fn (mut g Gen) write_alias_typesymbol_declaration(sym ast.TypeSymbol) {
18151815
// TODO: remove this check; it is here just to fix V rebuilding in -cstrict mode with clang-12
18161816
return
18171817
}
1818+
if sym.name.starts_with('C.') {
1819+
// `pub type C.HINSTANCE = voidptr` means that `HINSTANCE` should be treated as a voidptr by V.
1820+
// The C type itself however already exists on the C side, so just treat C__HINSTANCE as a macro for it:
1821+
g.type_definitions.writeln('#define ${sym.cname} ${sym.cname#[3..]}')
1822+
return
1823+
}
18181824
if is_fixed_array_of_non_builtin && levels == 0 {
18191825
g.alias_definitions.writeln('typedef ${parent_styp} ${sym.cname};')
18201826
} else {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typedef struct { int i; float f; char c; } CStruct, *PCStruct;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "@VMODROOT/vlib/v/gen/c/testdata/multiple_c_cources/file3.c"
2+
3+
@[typedef]
4+
struct C.CStruct {
5+
mut:
6+
c char
7+
i int
8+
f f32
9+
}
10+
11+
type C.PCStruct = &C.CStruct
12+
13+
struct WrapperStruct {
14+
mut:
15+
arr_fixed_c_type [2]C.PCStruct
16+
}
17+
18+
fn test_main() {
19+
s1 := &C.CStruct{
20+
c: char(`A`)
21+
f: 3.14
22+
i: 42
23+
}
24+
s2 := &C.CStruct{
25+
c: char(`B`)
26+
f: 1.4142
27+
i: 2
28+
}
29+
mut w := WrapperStruct{}
30+
w.arr_fixed_c_type[0] = s1
31+
w.arr_fixed_c_type[1] = s2
32+
dump(w)
33+
assert w.arr_fixed_c_type[0].c == char(`A`)
34+
assert w.arr_fixed_c_type[1].c == char(`B`)
35+
assert w.arr_fixed_c_type[0].f == 3.14
36+
assert w.arr_fixed_c_type[1].f == 1.4142
37+
assert w.arr_fixed_c_type[0].i == 42
38+
assert w.arr_fixed_c_type[1].i == 2
39+
}

‎vlib/v/tests/builtin_arrays/array_fixed_c_test.v‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)