@@ -33,18 +33,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
33
33
}
34
34
}
35
35
name := c_name (field.name)
36
- mut const_name := '_const_' + name
37
- if g.pref.translated && ! g.is_builtin_mod
38
- && ! util.module_is_builtin (field.name.all_before_last ('.' )) {
39
- if name.starts_with ('main__' ) {
40
- const_name = name.all_after_first ('main__' )
41
- }
42
- }
43
- if ! g.is_builtin_mod {
44
- if cattr := node.attrs.find_first ('export' ) {
45
- const_name = cattr.arg
46
- }
47
- }
36
+ const_name := g.c_const_name (field.name)
48
37
field_expr := field.expr
49
38
match field.expr {
50
39
ast.ArrayInit {
@@ -63,10 +52,11 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
63
52
} else if field.expr.is_fixed && ! field.expr.has_index
64
53
&& ((g.is_cc_msvc && field.expr.elem_type == ast.string_type)
65
54
|| ! elems_are_const) {
66
- g.const_decl_init_later_msvc_string_fixed_array (field.mod, name, field.expr ,
67
- field.typ)
55
+ g.const_decl_init_later_msvc_string_fixed_array (field.mod, name, const_name ,
56
+ field.expr, field. typ)
68
57
} else {
69
- g.const_decl_init_later (field.mod, name, field.expr, field.typ, false )
58
+ g.const_decl_init_later (field.mod, name, const_name, field.expr, field.typ,
59
+ false )
70
60
}
71
61
}
72
62
ast.StringLiteral {
@@ -85,10 +75,12 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
85
75
old_inside_const_opt_or_res := g.inside_const_opt_or_res
86
76
g.inside_const_opt_or_res = true
87
77
unwrap_opt_res := field.expr.or_block.kind != .absent
88
- g.const_decl_init_later (field.mod, name, field.expr, field.typ, unwrap_opt_res)
78
+ g.const_decl_init_later (field.mod, name, const_name, field.expr, field.typ,
79
+ unwrap_opt_res)
89
80
g.inside_const_opt_or_res = old_inside_const_opt_or_res
90
81
} else {
91
- g.const_decl_init_later (field.mod, name, field.expr, field.typ, false )
82
+ g.const_decl_init_later (field.mod, name, const_name, field.expr, field.typ,
83
+ false )
92
84
}
93
85
}
94
86
else {
@@ -105,8 +97,8 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
105
97
use_cache_mode := g.pref.build_mode == .build_module || g.pref.use_cache
106
98
if ! use_cache_mode {
107
99
if ct_value := field.comptime_expr_value () {
108
- if g.const_decl_precomputed (field.mod, name, field.name, ct_value ,
109
- field.typ)
100
+ if g.const_decl_precomputed (field.mod, name, const_name, field.name,
101
+ ct_value, field.typ)
110
102
{
111
103
continue
112
104
}
@@ -115,7 +107,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
115
107
if field.is_simple_define_const () {
116
108
// "Simple" expressions are not going to need multiple statements,
117
109
// only the ones which are inited later, so it's safe to use expr_string
118
- g.const_decl_simple_define (field.mod, field.name, g.expr_string (field_expr))
110
+ g.const_decl_simple_define (field.mod, field.name, const_name, g.expr_string (field_expr))
119
111
} else if field.expr is ast.CastExpr {
120
112
if field.expr.expr is ast.ArrayInit {
121
113
if field.expr.expr.is_fixed && g.pref.build_mode != .build_module {
@@ -131,27 +123,29 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
131
123
}
132
124
should_surround := field.expr.expr is ast.CallExpr
133
125
&& field.expr.expr.or_block.kind != .absent
134
- g.const_decl_init_later (field.mod, name, field.expr, field.typ, should_surround)
126
+ g.const_decl_init_later (field.mod, name, const_name, field.expr, field.typ,
127
+ should_surround)
135
128
} else if field.expr is ast.InfixExpr {
136
129
mut has_unwrap_opt_res := false
137
130
if field.expr.left is ast.CallExpr {
138
131
has_unwrap_opt_res = field.expr.left.or_block.kind != .absent
139
132
} else if field.expr.right is ast.CallExpr {
140
133
has_unwrap_opt_res = field.expr.right.or_block.kind != .absent
141
134
}
142
- g.const_decl_init_later (field.mod, name, field.expr, field.typ, has_unwrap_opt_res)
135
+ g.const_decl_init_later (field.mod, name, const_name, field.expr, field.typ,
136
+ has_unwrap_opt_res)
143
137
} else {
144
- g.const_decl_init_later (field.mod, name, field.expr, field.typ, true )
138
+ g.const_decl_init_later (field.mod, name, const_name, field.expr, field.typ,
139
+ true )
145
140
}
146
141
}
147
142
}
148
143
}
149
144
}
150
145
151
- fn (mut g Gen) const_decl_precomputed (mod string , name string , field_name string , ct_value ast.ComptTimeConstValue,
146
+ fn (mut g Gen) const_decl_precomputed (mod string , name string , cname string , field_name string , ct_value ast.ComptTimeConstValue,
152
147
typ ast.Type) bool {
153
148
mut styp := g.styp (typ)
154
- cname := if g.pref.translated && ! g.is_builtin_mod { name } else { '_const_${name} ' }
155
149
$if trace_const_precomputed ? {
156
150
eprintln ('> styp: ${styp} | cname: ${cname} | ct_value: ${ct_value} | ${ct_value.type_name()} ' )
157
151
}
@@ -179,7 +173,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string
179
173
// with -cstrict. Add checker errors for overflows instead,
180
174
// so V can catch them earlier, instead of relying on the
181
175
// C compiler for that.
182
- g.const_decl_simple_define (mod, name, ct_value.str ())
176
+ g.const_decl_simple_define (mod, name, cname, ct_value.str ())
183
177
return true
184
178
}
185
179
if typ == ast.u64_ type {
@@ -274,48 +268,52 @@ fn (mut g Gen) const_decl_write_precomputed(mod string, styp string, cname strin
274
268
}
275
269
}
276
270
277
- fn (mut g Gen) const_decl_simple_define (mod string , name string , val string ) {
271
+ fn (mut g Gen) const_decl_simple_define (mod string , name string , cname string , val string ) {
278
272
// Simple expressions should use a #define
279
273
// so that we don't pollute the binary with unnecessary global vars
280
274
// Do not do this when building a module, otherwise the consts
281
275
// will not be accessible.
282
- mut x := util.no_dots (name)
283
- if g.pref.translated && ! g.is_builtin_mod && ! util.module_is_builtin (name.all_before_last ('.' )) {
284
- // Don't prepend "_const" to translated C consts,
285
- // but only in user code, continue prepending "_const" to builtin consts.
286
- if x.starts_with ('main__' ) {
287
- x = x['main__' .len..]
288
- }
289
- } else {
290
- x = '_const_${x} '
291
- }
292
276
if g.pref.translated {
293
277
g.global_const_defs[util.no_dots (name)] = GlobalConstDef{
294
278
mod: mod
295
- def: 'const ${ast.int_type_name} ${x } = ${val} ;'
279
+ def: 'const ${ast.int_type_name} ${cname } = ${val} ;'
296
280
order: - 1
297
281
}
298
282
} else {
299
283
g.global_const_defs[util.no_dots (name)] = GlobalConstDef{
300
284
mod: mod
301
- def: '#define ${x } ${val} '
285
+ def: '#define ${cname } ${val} '
302
286
order: - 1
303
287
}
304
288
}
305
289
}
306
290
307
291
fn (mut g Gen) c_const_name (name string ) string {
308
- return if g.pref.translated && ! g.is_builtin_mod { name } else { '_const_${name} ' }
292
+ if name in g.table.export_names {
293
+ // `@[export] name
294
+ return g.table.export_names[name]
295
+ }
296
+ mut const_name := util.no_dots (name)
297
+ if g.pref.translated && ! g.is_builtin_mod && ! util.module_is_builtin (name.all_before_last ('.' )) {
298
+ if name.starts_with ('main.' ) {
299
+ const_name = util.no_dots (name.all_after_first ('main.' ))
300
+ }
301
+ }
302
+
303
+ return if g.pref.translated && ! g.is_builtin_mod {
304
+ const_name
305
+ } else {
306
+ '_const_' + g.get_ternary_name (c_name (name))
307
+ }
309
308
}
310
309
311
- fn (mut g Gen) const_decl_init_later (mod string , name string , expr ast.Expr, typ ast.Type, surround_cbr bool ) {
310
+ fn (mut g Gen) const_decl_init_later (mod string , name string , cname string , expr ast.Expr, typ ast.Type, surround_cbr bool ) {
312
311
if name.starts_with ('C__' ) {
313
312
return
314
313
}
315
314
// Initialize more complex consts in `void _vinit/2{}`
316
315
// (C doesn't allow init expressions that can't be resolved at compile time).
317
316
mut styp := g.styp (typ)
318
- cname := g.c_const_name (name)
319
317
mut init := strings.new_builder (100 )
320
318
321
319
if surround_cbr {
@@ -372,10 +370,9 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ
372
370
}
373
371
}
374
372
375
- fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array (mod string , name string , expr ast.ArrayInit,
373
+ fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array (mod string , name string , cname string , expr ast.ArrayInit,
376
374
typ ast.Type) {
377
375
mut styp := g.styp (typ)
378
- cname := g.c_const_name (name)
379
376
mut init := strings.new_builder (100 )
380
377
for i, elem_expr in expr.exprs {
381
378
if elem_expr is ast.ArrayInit && elem_expr.is_fixed {
0 commit comments