Skip to content

Commit d87b8c2

Browse files
committed
checker: improve virtual C consts
1 parent 4dc9167 commit d87b8c2

5 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎vlib/net/address_darwin.c.v‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ mut:
3434
sin_zero [8]char
3535
}
3636

37+
const C.AF_INET u8
38+
3739
pub struct C.sockaddr_un {
3840
mut:
3941
sun_len u8

‎vlib/v/checker/checker.v‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4297,8 +4297,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
42974297
if node.name == 'C.NULL' {
42984298
return ast.voidptr_type
42994299
}
4300-
// TODO remove main. to avoid extra concats
4301-
if x := c.table.global_scope.find_const('main.' + node.name) {
4300+
if x := c.table.global_scope.find_const(node.name) {
43024301
return x.typ
43034302
}
43044303
return ast.int_type

‎vlib/v/gen/c/consts_and_globals.v‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ fn (mut g Gen) c_const_name(name string) string {
309309
}
310310

311311
fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ ast.Type, surround_cbr bool) {
312+
if name.starts_with('C__') {
313+
return
314+
}
312315
// Initialize more complex consts in `void _vinit/2{}`
313316
// (C doesn't allow init expressions that can't be resolved at compile time).
314317
mut styp := g.styp(typ)

‎vlib/v/parser/parser.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
23842384
p.error_with_pos('const names cannot contain uppercase letters, use snake_case instead',
23852385
pos)
23862386
}
2387-
full_name := p.prepend_mod(name)
2387+
full_name := if is_virtual_c_const { name } else { p.prepend_mod(name) }
23882388
if p.tok.kind == .comma {
23892389
p.error_with_pos('const declaration do not support multiple assign yet', p.tok.pos())
23902390
}

‎vlib/v/tests/c_const_u8_test.v‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
const C.AF_INET u16
1+
import net
22

3-
fn x(n u16) bool {
3+
const C.AF_UNIX u16
4+
5+
fn x16(n u16) bool {
6+
return true
7+
}
8+
9+
fn xint(n int) bool {
410
return true
511
}
612

713
fn test_const() {
8-
assert x(C.AF_INET) == true
14+
assert xint(C.EOF) == true // a random libc const is int by default
15+
16+
assert x16(C.AF_INET) == true // defined in V's net module
917
}

0 commit comments

Comments
 (0)