48
48
// line_nr int
49
49
cheaders strings.Builder
50
50
preincludes strings.Builder // allows includes to go before `definitions`
51
+ postincludes strings.Builder // allows includes to go after all the rest of the code generation
51
52
includes strings.Builder // all C #includes required by V modules
52
53
typedefs strings.Builder
53
54
enum_typedefs strings.Builder // enum types
@@ -300,6 +301,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
300
301
cheaders: strings.new_builder (15000 )
301
302
includes: strings.new_builder (100 )
302
303
preincludes: strings.new_builder (100 )
304
+ postincludes: strings.new_builder (100 )
303
305
typedefs: strings.new_builder (100 )
304
306
enum_typedefs: strings.new_builder (100 )
305
307
type_definitions: strings.new_builder (100 )
@@ -387,6 +389,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
387
389
global_g.out.write (g.out) or { panic (err) }
388
390
global_g.cheaders.write (g.cheaders) or { panic (err) }
389
391
global_g.preincludes.write (g.preincludes) or { panic (err) }
392
+ global_g.postincludes.write (g.postincludes) or { panic (err) }
390
393
global_g.includes.write (g.includes) or { panic (err) }
391
394
global_g.typedefs.write (g.typedefs) or { panic (err) }
392
395
global_g.type_definitions.write (g.type_definitions) or { panic (err) }
@@ -567,7 +570,6 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
567
570
}
568
571
b.write_string2 ('\n // V comptime_definitions:\n ' , g.comptime_definitions.str ())
569
572
b.write_string2 ('\n // V typedefs:\n ' , g.typedefs.str ())
570
- b.write_string2 ('\n // V preincludes:\n ' , g.preincludes.str ())
571
573
b.write_string2 ('\n // V cheaders:' , g.cheaders.str ())
572
574
if g.pcs_declarations.len > 0 {
573
575
b.write_string2 ('\n // V profile counters:\n ' , g.pcs_declarations.str ())
@@ -741,6 +743,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
741
743
extern_out_str := g.extern_out.str ()
742
744
b.write_string (out_str)
743
745
b.writeln ('// THE END.' )
746
+ b.write_string2 ('\n // V postincludes:\n ' , g.postincludes.str ())
744
747
util.timing_measure ('cgen common' )
745
748
$if trace_all_generic_fn_keys ? {
746
749
gkeys := g.table.fn_generic_types.keys ()
@@ -5436,6 +5439,21 @@ fn (mut g Gen) gen_option_error(target_type ast.Type, expr ast.Expr) {
5436
5439
g.write (', .data={EMPTY_STRUCT_INITIALIZATION} }' )
5437
5440
}
5438
5441
5442
+ fn (mut g Gen) hash_stmt_guarded_include (node ast.HashStmt) string {
5443
+ mut missing_message := 'Header file ${node.main} , needed for module `${node.mod} ` was not found.'
5444
+ if node.msg != '' {
5445
+ missing_message + = ' ${node.msg} .'
5446
+ } else {
5447
+ missing_message + = ' Please install the corresponding development headers.'
5448
+ }
5449
+ mut guarded_include := get_guarded_include_text (node.main, missing_message)
5450
+ if node.main == '<errno.h>' {
5451
+ // fails with musl-gcc and msvc; but an unguarded include works:
5452
+ guarded_include = '#include ${node.main} '
5453
+ }
5454
+ return guarded_include
5455
+ }
5456
+
5439
5457
fn (mut g Gen) hash_stmt (node ast.HashStmt) {
5440
5458
line_nr := node.pos.line_nr + 1
5441
5459
mut ct_condition := ''
@@ -5451,17 +5469,7 @@ fn (mut g Gen) hash_stmt(node ast.HashStmt) {
5451
5469
}
5452
5470
// #include etc
5453
5471
if node.kind == 'include' {
5454
- mut missing_message := 'Header file ${node.main} , needed for module `${node.mod} ` was not found.'
5455
- if node.msg != '' {
5456
- missing_message + = ' ${node.msg} .'
5457
- } else {
5458
- missing_message + = ' Please install the corresponding development headers.'
5459
- }
5460
- mut guarded_include := get_guarded_include_text (node.main, missing_message)
5461
- if node.main == '<errno.h>' {
5462
- // fails with musl-gcc and msvc; but an unguarded include works:
5463
- guarded_include = '#include ${node.main} '
5464
- }
5472
+ guarded_include := g.hash_stmt_guarded_include (node)
5465
5473
if node.main.contains ('.m' ) {
5466
5474
g.definitions.writeln ('' )
5467
5475
if ct_condition != '' {
@@ -5486,17 +5494,7 @@ fn (mut g Gen) hash_stmt(node ast.HashStmt) {
5486
5494
}
5487
5495
}
5488
5496
} else if node.kind == 'preinclude' {
5489
- mut missing_message := 'Header file ${node.main} , needed for module `${node.mod} ` was not found.'
5490
- if node.msg != '' {
5491
- missing_message + = ' ${node.msg} .'
5492
- } else {
5493
- missing_message + = ' Please install the corresponding development headers.'
5494
- }
5495
- mut guarded_include := get_guarded_include_text (node.main, missing_message)
5496
- if node.main == '<errno.h>' {
5497
- // fails with musl-gcc and msvc; but an unguarded include works:
5498
- guarded_include = '#include ${node.main} '
5499
- }
5497
+ guarded_include := g.hash_stmt_guarded_include (node)
5500
5498
if node.main.contains ('.m' ) {
5501
5499
// Might need to support '#preinclude' for .m files as well but for the moment
5502
5500
// this does the same as '#include' for them
@@ -5522,6 +5520,17 @@ fn (mut g Gen) hash_stmt(node ast.HashStmt) {
5522
5520
g.preincludes.writeln ('#endif // \$ if ${ct_condition} ' )
5523
5521
}
5524
5522
}
5523
+ } else if node.kind == 'postinclude' {
5524
+ guarded_include := g.hash_stmt_guarded_include (node)
5525
+ g.postincludes.writeln ('' )
5526
+ if ct_condition != '' {
5527
+ g.postincludes.writeln ('#if ${ct_condition} ' )
5528
+ }
5529
+ g.postincludes.writeln ('// added by module `${node.mod} `, file: ${os.file_name(node.source_file)} :${line_nr} :' )
5530
+ g.postincludes.writeln (guarded_include)
5531
+ if ct_condition != '' {
5532
+ g.postincludes.writeln ('#endif // \$ if ${ct_condition} ' )
5533
+ }
5525
5534
} else if node.kind == 'insert' {
5526
5535
if ct_condition != '' {
5527
5536
g.includes.writeln ('#if ${ct_condition} ' )
0 commit comments