/
cgen.v
7300 lines (7080 loc) · 225 KB
/
cgen.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module c
import os
import strings
import hash.fnv1a
import v.ast
import v.pref
import v.token
import v.util
import v.util.version
import v.depgraph
import v.comptime
import sync.pool
// Note: some of the words in c_reserved, are not reserved in C, but are
// in C++, or have special meaning in V, thus need escaping too. `small`
// should not be needed, but see:
// https://stackoverflow.com/questions/5874215/what-is-rpcndr-h
const c_reserved = ['array', 'auto', 'bool', 'break', 'calloc', 'case', 'char', 'class', 'complex',
'const', 'continue', 'default', 'delete', 'do', 'double', 'else', 'enum', 'error', 'exit',
'export', 'extern', 'false', 'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link',
'long', 'malloc', 'namespace', 'new', 'nil', 'panic', 'register', 'restrict', 'return', 'short',
'signed', 'sizeof', 'static', 'string', 'struct', 'switch', 'typedef', 'typename', 'union',
'unix', 'unsigned', 'void', 'volatile', 'while', 'template', 'true', 'small', 'stdout', 'stdin',
'stderr', 'far', 'near', 'huge', 'requires']
const c_reserved_chk = token.new_keywords_matcher_from_array_trie(c_reserved)
// same order as in token.Kind
const cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
// when operands are switched
const cmp_rev = ['eq', 'ne', 'lt', 'gt', 'le', 'ge']
const result_name = '_result'
const option_name = '_option'
fn string_array_to_map(a []string) map[string]bool {
mut res := map[string]bool{}
for x in a {
res[x] = true
}
return res
}
pub struct Gen {
pref &pref.Preferences = unsafe { nil }
field_data_type ast.Type // cache her to avoid map lookups
enum_data_type ast.Type // cache her to avoid map lookups
variant_data_type ast.Type // cache her to avoid map lookups
module_built string
timers_should_print bool
mut:
out strings.Builder
cheaders strings.Builder
preincludes strings.Builder // allows includes to go before `definitions`
includes strings.Builder // all C #includes required by V modules
typedefs strings.Builder
enum_typedefs strings.Builder // enum types
definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file)
type_definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file)
alias_definitions strings.Builder // alias fixed array of non-builtin
hotcode_definitions strings.Builder // -live declarations & functions
channel_definitions strings.Builder // channel related code
thread_definitions strings.Builder // thread defines
comptime_definitions strings.Builder // custom defines, given by -d/-define flags on the CLI
cleanup strings.Builder
cleanups map[string]strings.Builder // contents of `void _vcleanup(){}`
gowrappers strings.Builder // all go callsite wrappers
auto_str_funcs strings.Builder // function bodies of all auto generated _str funcs
dump_funcs strings.Builder // function bodies of all auto generated _str funcs
pcs_declarations strings.Builder // -prof profile counter declarations for each function
embedded_data strings.Builder // data to embed in the executable/binary
shared_types strings.Builder // shared/lock types
shared_functions strings.Builder // shared constructors
out_options_forward strings.Builder // forward `option_xxxx` types
out_options strings.Builder // `option_xxxx` types
out_results_forward strings.Builder // forward`result_xxxx` types
out_results strings.Builder // `result_xxxx` types
json_forward_decls strings.Builder // json type forward decls
sql_buf strings.Builder // for writing exprs to args via `sqlite3_bind_int()` etc
global_const_defs map[string]GlobalConstDef
sorted_global_const_names []string
file &ast.File = unsafe { nil }
table &ast.Table = unsafe { nil }
unique_file_path_hash u64 // a hash of file.path, used for making auxiliary fn generation unique (like `compare_xyz`)
fn_decl &ast.FnDecl = unsafe { nil } // pointer to the FnDecl we are currently inside otherwise 0
last_fn_c_name string
tmp_count int // counter for unique tmp vars (_tmp1, _tmp2 etc); resets at the start of each fn.
tmp_count_af int // a separate tmp var counter for autofree fn calls
tmp_count_declarations int // counter for unique tmp names (_d1, _d2 etc); does NOT reset, used for C declarations
global_tmp_count int // like tmp_count but global and not reset in each function
discard_or_result bool // do not safe last ExprStmt of `or` block in tmp variable to defer ongoing expr usage
is_direct_array_access bool // inside a `[direct_array_access fn a() {}` function
is_assign_lhs bool // inside left part of assign expr (for array_set(), etc)
is_void_expr_stmt bool // ExprStmt whose result is discarded
is_arraymap_set bool // map or array set value state
is_amp bool // for `&Foo{}` to merge PrefixExpr `&` and StructInit `Foo{}`; also for `&u8(0)` etc
is_sql bool // Inside `sql db{}` statement, generating sql instead of C (e.g. `and` instead of `&&` etc)
is_shared bool // for initialization of hidden mutex in `[rw]shared` literals
is_vlines_enabled bool // is it safe to generate #line directives when -g is passed
is_autofree bool // false, inside the bodies of fns marked with [manualfree], otherwise === g.pref.autofree
is_builtin_mod bool
is_json_fn bool // inside json.encode()
is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)`
is_fn_index_call bool
is_cc_msvc bool // g.pref.ccompiler == 'msvc'
is_option_auto_heap bool
vlines_path string // set to the proper path for generating #line directives
options_pos_forward int // insertion point to forward
options_forward []string // to forward
options map[string]string // to avoid duplicates
results_forward []string // to forward
results map[string]string // to avoid duplicates
done_options shared []string // to avoid duplicates
done_results shared []string // to avoid duplicates
chan_pop_options map[string]string // types for `x := <-ch or {...}`
chan_push_options map[string]string // types for `ch <- x or {...}`
mtxs string // array of mutexes if the `lock` has multiple variables
labeled_loops map[string]&ast.Stmt
inner_loop &ast.Stmt = unsafe { nil }
shareds map[int]string // types with hidden mutex for which decl has been emitted
inside_ternary int // ?: comma separated statements on a single line
inside_map_postfix bool // inside map++/-- postfix expr
inside_map_infix bool // inside map<</+=/-= infix expr
inside_assign bool
inside_map_index bool
inside_array_index bool
inside_opt_or_res bool
inside_opt_data bool
inside_if_option bool
inside_if_result bool
inside_match_option bool
inside_match_result bool
inside_vweb_tmpl bool
inside_return bool
inside_return_tmpl bool
inside_struct_init bool
inside_or_block bool
inside_call bool
inside_curry_call bool // inside foo()()!, foo()()?, foo()()
expected_fixed_arr bool
inside_for_c_stmt bool
// inside_comptime_for_field bool
inside_cast_in_heap int // inside cast to interface type in heap (resolve recursive calls)
inside_cast bool
inside_const bool
inside_array_item bool
inside_const_opt_or_res bool
inside_lambda bool
inside_cinit bool
inside_interface_deref bool
last_tmp_call_var []string
loop_depth int
ternary_names map[string]string
ternary_level_names map[string][]string
arraymap_set_pos int // map or array set value position
stmt_path_pos []int // positions of each statement start, for inserting C statements before the current statement
skip_stmt_pos bool // for handling if expressions + autofree (since both prepend C statements)
left_is_opt bool // left hand side on assignment is an option
right_is_opt bool // right hand side on assignment is an option
assign_ct_type ast.Type // left hand side resolved comptime type
indent int
empty_line bool
assign_op token.Kind // *=, =, etc (for array_set)
defer_stmts []ast.DeferStmt
defer_ifdef string
defer_profile_code string
defer_vars []string
str_types []StrType // types that need automatic str() generation
generated_str_fns []StrType // types that already have a str() function
str_fn_names []string // remove duplicate function names
threaded_fns shared []string // for generating unique wrapper types and fns for `go xxx()`
waiter_fns shared []string // functions that wait for `go xxx()` to finish
needed_equality_fns []ast.Type
generated_eq_fns []ast.Type
array_sort_fn shared []string
array_contains_types []ast.Type
array_index_types []ast.Type
auto_fn_definitions []string // auto generated functions definition list
sumtype_casting_fns []SumtypeCastingFn
anon_fn_definitions []string // anon generated functions definition list
sumtype_definitions map[int]bool // `_TypeA_to_sumtype_TypeB()` fns that have been generated
json_types []ast.Type // to avoid json gen duplicates
pcs []ProfileCounterMeta // -prof profile counter fn_names => fn counter name
hotcode_fn_names []string
hotcode_fpaths []string
embedded_files []ast.EmbeddedFile
sql_i int
sql_stmt_name string
sql_bind_name string
sql_idents []string
sql_idents_types []ast.Type
sql_left_type ast.Type
sql_table_name string
sql_fkey string
sql_parent_id string
sql_side SqlExprSide // left or right, to distinguish idents in `name == name`
strs_to_free0 []string // strings.Builder
// strs_to_free []string // strings.Builder
// tmp_arg_vars_to_free []string
// autofree_pregen map[string]string
// autofree_pregen_buf strings.Builder
// autofree_tmp_vars []string // to avoid redefining the same tmp vars in a single function
// nr_vars_to_free int
// doing_autofree_tmp bool
comptime_info_stack []comptime.ComptimeInfo // stores the values from the above on each $for loop, to make nesting them easier
comptime comptime.ComptimeInfo
prevent_sum_type_unwrapping_once bool // needed for assign new values to sum type
// used in match multi branch
// TypeOne, TypeTwo {}
// where an aggregate (at least two types) is generated
// sum type deref needs to know which index to deref because unions take care of the correct field
aggregate_type_idx int
branch_parent_pos int // used in BranchStmt (continue/break) for autofree stop position
returned_var_name string // to detect that a var doesn't need to be freed since it's being returned
infix_left_var_name string // a && if expr
called_fn_name string
timers &util.Timers = util.get_timers()
force_main_console bool // true when [console] used on fn main()
as_cast_type_names map[string]string // table for type name lookup in runtime (for __as_cast)
obf_table map[string]string
referenced_fns shared map[string]bool // functions that have been referenced
nr_closures int
expected_cast_type ast.Type // for match expr of sumtypes
or_expr_return_type ast.Type // or { 0, 1 } return type
anon_fn bool
tests_inited bool
has_main bool
// main_fn_decl_node ast.FnDecl
cur_mod ast.Module
cur_concrete_types []ast.Type // do not use table.cur_concrete_types because table is global, so should not be accessed by different threads
cur_fn &ast.FnDecl = unsafe { nil } // same here
cur_lock ast.LockExpr
cur_struct_init_typ ast.Type
autofree_methods map[int]bool
generated_free_methods map[int]bool
autofree_scope_stmts []string
use_segfault_handler bool = true
test_function_names []string
/////////
// out_parallel []strings.Builder
// out_idx int
out_fn_start_pos []int // for generating multiple .c files, stores locations of all fn positions in `out` string builder
static_modifier string // for parallel_cc
has_reflection bool
reflection_strings &map[string]int
defer_return_tmp_var string
vweb_filter_fn_name string // vweb__filter or x__vweb__filter, used by $vweb.html() for escaping strings in the templates, depending on which `vweb` import is used
}
// global or const variable definition string
struct GlobalConstDef {
mod string // module name
def string // definition
init string // init later (in _vinit)
dep_names []string // the names of all the consts, that this const depends on
order int // -1 for simple defines, string literals, anonymous function names, extern declarations etc
is_precomputed bool // can be declared as a const in C: primitive, and a simple definition
}
pub fn gen(files []&ast.File, table &ast.Table, pref_ &pref.Preferences) (string, string, string, []int) {
mut module_built := ''
if pref_.build_mode == .build_module {
for file in files {
if file.path.contains(pref_.path)
&& file.mod.short_name == pref_.path.all_after_last(os.path_separator).trim_right(os.path_separator) {
module_built = file.mod.name
break
}
}
}
mut timers_should_print := false
$if time_cgening ? {
timers_should_print = true
}
mut reflection_strings := map[string]int{}
mut global_g := Gen{
file: unsafe { nil }
out: strings.new_builder(512000)
cheaders: strings.new_builder(15000)
includes: strings.new_builder(100)
preincludes: strings.new_builder(100)
typedefs: strings.new_builder(100)
enum_typedefs: strings.new_builder(100)
type_definitions: strings.new_builder(100)
alias_definitions: strings.new_builder(100)
hotcode_definitions: strings.new_builder(100)
channel_definitions: strings.new_builder(100)
thread_definitions: strings.new_builder(100)
comptime_definitions: strings.new_builder(100)
definitions: strings.new_builder(100)
gowrappers: strings.new_builder(100)
auto_str_funcs: strings.new_builder(100)
dump_funcs: strings.new_builder(100)
pcs_declarations: strings.new_builder(100)
embedded_data: strings.new_builder(1000)
out_options_forward: strings.new_builder(100)
out_options: strings.new_builder(100)
out_results_forward: strings.new_builder(100)
out_results: strings.new_builder(100)
shared_types: strings.new_builder(100)
shared_functions: strings.new_builder(100)
json_forward_decls: strings.new_builder(100)
sql_buf: strings.new_builder(100)
table: table
pref: pref_
fn_decl: unsafe { nil }
is_autofree: pref_.autofree
indent: -1
module_built: module_built
timers_should_print: timers_should_print
timers: util.new_timers(should_print: timers_should_print, label: 'global_cgen')
inner_loop: &ast.empty_stmt
field_data_type: ast.Type(table.find_type_idx('FieldData'))
enum_data_type: ast.Type(table.find_type_idx('EnumData'))
is_cc_msvc: pref_.ccompiler == 'msvc'
use_segfault_handler: !('no_segfault_handler' in pref_.compile_defines
|| pref_.os in [.wasm32, .wasm32_emscripten])
static_modifier: if pref_.parallel_cc { 'static' } else { '' }
has_reflection: 'v.reflection' in table.modules
reflection_strings: &reflection_strings
}
global_g.comptime = &comptime.ComptimeInfo{
resolver: &global_g
table: table
}
/*
global_g.out_parallel = []strings.Builder{len: nr_cpus}
for i in 0 .. nr_cpus {
global_g.out_parallel[i] = strings.new_builder(100000)
global_g.out_parallel[i].writeln('#include "out.h"\n')
}
println('LEN=')
println(global_g.out_parallel.len)
*/
// anon fn may include assert and thus this needs
// to be included before any test contents are written
if pref_.is_test {
global_g.write_tests_definitions()
}
util.timing_start('cgen init')
for mod in global_g.table.modules {
global_g.cleanups[mod] = strings.new_builder(100)
}
global_g.init()
util.timing_measure('cgen init')
global_g.tests_inited = false
global_g.file = files.last()
if !pref_.no_parallel {
util.timing_start('cgen parallel processing')
mut pp := pool.new_pool_processor(callback: cgen_process_one_file_cb)
pp.set_shared_context(global_g) // TODO: make global_g shared
pp.work_on_items(files)
util.timing_measure('cgen parallel processing')
util.timing_start('cgen unification')
for g in pp.get_results_ref[Gen]() {
global_g.embedded_files << g.embedded_files
global_g.out.write(g.out) or { panic(err) }
global_g.cheaders.write(g.cheaders) or { panic(err) }
global_g.preincludes.write(g.preincludes) or { panic(err) }
global_g.includes.write(g.includes) or { panic(err) }
global_g.typedefs.write(g.typedefs) or { panic(err) }
global_g.type_definitions.write(g.type_definitions) or { panic(err) }
global_g.alias_definitions.write(g.alias_definitions) or { panic(err) }
global_g.definitions.write(g.definitions) or { panic(err) }
global_g.gowrappers.write(g.gowrappers) or { panic(err) }
global_g.auto_str_funcs.write(g.auto_str_funcs) or { panic(err) }
global_g.dump_funcs.write(g.auto_str_funcs) or { panic(err) }
global_g.comptime_definitions.write(g.comptime_definitions) or { panic(err) }
global_g.pcs_declarations.write(g.pcs_declarations) or { panic(err) }
global_g.hotcode_definitions.write(g.hotcode_definitions) or { panic(err) }
global_g.embedded_data.write(g.embedded_data) or { panic(err) }
global_g.shared_types.write(g.shared_types) or { panic(err) }
global_g.shared_functions.write(g.channel_definitions) or { panic(err) }
global_g.force_main_console = global_g.force_main_console || g.force_main_console
// merge maps
for k, v in g.global_const_defs {
global_g.global_const_defs[k] = v
}
for k, v in g.shareds {
global_g.shareds[k] = v
}
for k, v in g.chan_pop_options {
global_g.chan_pop_options[k] = v
}
for k, v in g.chan_push_options {
global_g.chan_push_options[k] = v
}
for k, v in g.options {
global_g.options[k] = v
}
for k, v in g.results {
global_g.results[k] = v
}
for k, v in g.as_cast_type_names {
global_g.as_cast_type_names[k] = v
}
for k, v in g.sumtype_definitions {
global_g.sumtype_definitions[k] = v
}
global_g.json_forward_decls.write(g.json_forward_decls) or { panic(err) }
global_g.enum_typedefs.write(g.enum_typedefs) or { panic(err) }
global_g.channel_definitions.write(g.channel_definitions) or { panic(err) }
global_g.thread_definitions.write(g.thread_definitions) or { panic(err) }
global_g.sql_buf.write(g.sql_buf) or { panic(err) }
global_g.cleanups[g.file.mod.name].write(g.cleanup) or { panic(err) } // strings.Builder.write never fails; it is like that in the source
for str_type in g.str_types {
global_g.str_types << str_type
}
for scf in g.sumtype_casting_fns {
if scf !in global_g.sumtype_casting_fns {
global_g.sumtype_casting_fns << scf
}
}
global_g.nr_closures += g.nr_closures
global_g.has_main = global_g.has_main || g.has_main
global_g.auto_fn_definitions << g.auto_fn_definitions
global_g.anon_fn_definitions << g.anon_fn_definitions
global_g.needed_equality_fns << g.needed_equality_fns // duplicates are resolved later in gen_equality_fns
global_g.array_contains_types << g.array_contains_types
global_g.array_index_types << g.array_index_types
global_g.pcs << g.pcs
global_g.json_types << g.json_types
global_g.hotcode_fn_names << g.hotcode_fn_names
global_g.hotcode_fpaths << g.hotcode_fpaths
global_g.test_function_names << g.test_function_names
unsafe { g.free_builders() }
for k, v in g.autofree_methods {
global_g.autofree_methods[k] = v
}
}
} else {
util.timing_start('cgen serial processing')
for file in files {
global_g.file = file
global_g.gen_file()
global_g.cleanups[file.mod.name].drain_builder(mut global_g.cleanup, 100)
}
util.timing_measure('cgen serial processing')
util.timing_start('cgen unification')
}
global_g.gen_jsons()
global_g.dump_expr_definitions() // this uses global_g.get_str_fn, so it has to go before the below for loop
for i := 0; i < global_g.str_types.len; i++ {
global_g.final_gen_str(global_g.str_types[i])
}
for sumtype_casting_fn in global_g.sumtype_casting_fns {
global_g.write_sumtype_casting_fn(sumtype_casting_fn)
}
global_g.write_shareds()
global_g.write_chan_pop_option_fns()
global_g.write_chan_push_option_fns()
global_g.gen_array_contains_methods()
global_g.gen_array_index_methods()
global_g.gen_equality_fns()
global_g.gen_free_methods()
global_g.write_results()
global_g.write_options()
global_g.sort_globals_consts()
util.timing_measure('cgen unification')
mut g := global_g
util.timing_start('cgen common')
// to make sure type idx's are the same in cached mods
if g.pref.build_mode == .build_module {
for idx, sym in g.table.type_symbols {
if idx in [0, 31] {
continue
}
g.definitions.writeln('int _v_type_idx_${sym.cname}();')
}
} else if g.pref.use_cache {
for idx, sym in g.table.type_symbols {
if idx in [0, 31] {
continue
}
g.definitions.writeln('int _v_type_idx_${sym.cname}() { return ${idx}; };')
}
}
//
// v files are finished, what remains is pure C code
g.gen_vlines_reset()
if g.pref.build_mode != .build_module {
// no init in builtin.o
g.write_init_function()
}
// insert for options forward
if g.out_options_forward.len > 0 || g.out_results_forward.len > 0 {
tail := g.type_definitions.cut_to(g.options_pos_forward)
if g.out_options_forward.len > 0 {
g.type_definitions.writeln('// #start V forward option_xxx definitions:')
g.type_definitions.writeln(g.out_options_forward.str())
g.type_definitions.writeln('// #end V forward option_xxx definitions\n')
}
if g.out_results_forward.len > 0 {
g.type_definitions.writeln('// #start V forward result_xxx definitions:')
g.type_definitions.writeln(g.out_results_forward.str())
g.type_definitions.writeln('// #end V forward result_xxx definitions\n')
}
g.type_definitions.writeln(tail)
}
g.finish()
mut b := strings.new_builder(640000)
b.write_string(g.hashes())
if g.use_segfault_handler {
b.writeln('\n#define V_USE_SIGNAL_H')
}
b.writeln('\n// V comptime_definitions:')
b.write_string(g.comptime_definitions.str())
b.writeln('\n// V typedefs:')
b.write_string(g.typedefs.str())
b.writeln('\n // V preincludes:')
b.write_string(g.preincludes.str())
b.writeln('\n// V cheaders:')
b.write_string(g.cheaders.str())
if g.pcs_declarations.len > 0 {
b.writeln('\n// V profile counters:')
b.write_string(g.pcs_declarations.str())
}
b.writeln('\n// V includes:')
b.write_string(g.includes.str())
b.writeln('\n// Enum definitions:')
b.write_string(g.enum_typedefs.str())
b.writeln('\n// Thread definitions:')
b.write_string(g.thread_definitions.str())
b.writeln('\n// V type definitions:')
b.write_string(g.type_definitions.str())
b.writeln('\n// V alias definitions:')
b.write_string(g.alias_definitions.str())
b.writeln('\n// V shared types:')
b.write_string(g.shared_types.str())
b.writeln('\n// V Option_xxx definitions:')
b.write_string(g.out_options.str())
b.writeln('\n// V result_xxx definitions:')
b.write_string(g.out_results.str())
b.writeln('\n// V json forward decls:')
b.write_string(g.json_forward_decls.str())
b.writeln('\n// V definitions:')
b.write_string(g.definitions.str())
b.writeln('\n// V global/const definitions:')
for var_name in g.sorted_global_const_names {
if var := g.global_const_defs[var_name] {
b.writeln(var.def)
}
}
interface_table := g.interface_table()
if interface_table.len > 0 {
b.writeln('\n// V interface table:')
b.write_string(interface_table)
}
if g.gowrappers.len > 0 {
b.writeln('\n// V gowrappers:')
b.write_string(g.gowrappers.str())
}
if g.hotcode_definitions.len > 0 {
b.writeln('\n// V hotcode definitions:')
b.write_string(g.hotcode_definitions.str())
}
if g.embedded_data.len > 0 {
b.writeln('\n// V embedded data:')
b.write_string(g.embedded_data.str())
}
if g.shared_functions.len > 0 {
b.writeln('\n// V shared type functions:')
b.write_string(g.shared_functions.str())
b.write_string(c_concurrency_helpers)
}
if g.channel_definitions.len > 0 {
b.writeln('\n// V channel code:')
b.write_string(g.channel_definitions.str())
}
if g.auto_str_funcs.len > 0 {
b.writeln('\n// V auto str functions:')
b.write_string(g.auto_str_funcs.str())
}
if g.dump_funcs.len > 0 {
b.writeln('\n// V dump functions:')
b.write_string(g.dump_funcs.str())
}
if g.auto_fn_definitions.len > 0 {
b.writeln('\n// V auto functions:')
for fn_def in g.auto_fn_definitions {
b.writeln(fn_def)
}
}
if g.anon_fn_definitions.len > 0 {
if g.nr_closures > 0 {
b.writeln('\n// V closure helpers')
b.writeln(c_closure_helpers(g.pref))
}
b.writeln('\n// V anon functions:')
for fn_def in g.anon_fn_definitions {
b.writeln(fn_def)
}
}
b.writeln('\n// end of V out')
mut header := b.last_n(b.len)
header = '#ifndef V_HEADER_FILE\n#define V_HEADER_FILE' + header
header += '\n#endif\n'
out_str := g.out.str()
b.write_string(out_str)
b.writeln('\n// THE END.')
util.timing_measure('cgen common')
res := b.str()
$if trace_all_generic_fn_keys ? {
gkeys := g.table.fn_generic_types.keys()
for gkey in gkeys {
eprintln('>> g.table.fn_generic_types key: ${gkey}')
}
}
out_fn_start_pos := g.out_fn_start_pos.clone()
unsafe { b.free() }
unsafe { g.free_builders() }
return header, res, out_str, out_fn_start_pos
}
fn cgen_process_one_file_cb(mut p pool.PoolProcessor, idx int, wid int) &Gen {
file := p.get_item[&ast.File](idx)
mut global_g := unsafe { &Gen(p.get_shared_context()) }
mut g := &Gen{
file: file
out: strings.new_builder(512000)
cheaders: strings.new_builder(15000)
includes: strings.new_builder(100)
typedefs: strings.new_builder(100)
type_definitions: strings.new_builder(100)
alias_definitions: strings.new_builder(100)
definitions: strings.new_builder(100)
gowrappers: strings.new_builder(100)
auto_str_funcs: strings.new_builder(100)
comptime_definitions: strings.new_builder(100)
pcs_declarations: strings.new_builder(100)
hotcode_definitions: strings.new_builder(100)
embedded_data: strings.new_builder(1000)
out_options_forward: strings.new_builder(100)
out_options: strings.new_builder(100)
out_results_forward: strings.new_builder(100)
out_results: strings.new_builder(100)
shared_types: strings.new_builder(100)
shared_functions: strings.new_builder(100)
channel_definitions: strings.new_builder(100)
thread_definitions: strings.new_builder(100)
json_forward_decls: strings.new_builder(100)
enum_typedefs: strings.new_builder(100)
sql_buf: strings.new_builder(100)
cleanup: strings.new_builder(100)
table: global_g.table
pref: global_g.pref
fn_decl: unsafe { nil }
indent: -1
module_built: global_g.module_built
timers: util.new_timers(
should_print: global_g.timers_should_print
label: 'cgen_process_one_file_cb idx: ${idx}, wid: ${wid}'
)
inner_loop: &ast.empty_stmt
field_data_type: ast.Type(global_g.table.find_type_idx('FieldData'))
enum_data_type: ast.Type(global_g.table.find_type_idx('EnumData'))
array_sort_fn: global_g.array_sort_fn
waiter_fns: global_g.waiter_fns
threaded_fns: global_g.threaded_fns
options_forward: global_g.options_forward
results_forward: global_g.results_forward
done_options: global_g.done_options
done_results: global_g.done_results
is_autofree: global_g.pref.autofree
obf_table: global_g.obf_table
referenced_fns: global_g.referenced_fns
is_cc_msvc: global_g.is_cc_msvc
use_segfault_handler: global_g.use_segfault_handler
has_reflection: 'v.reflection' in global_g.table.modules
reflection_strings: global_g.reflection_strings
}
g.comptime = &comptime.ComptimeInfo{
resolver: g
table: global_g.table
}
g.gen_file()
return g
}
// free_builders should be called only when a Gen would NOT be used anymore
// it frees the bulk of the memory that is private to the Gen instance
// (the various string builders)
@[unsafe]
pub fn (mut g Gen) free_builders() {
unsafe {
g.out.free()
g.cheaders.free()
g.includes.free()
g.typedefs.free()
g.type_definitions.free()
g.alias_definitions.free()
g.definitions.free()
g.cleanup.free()
g.gowrappers.free()
g.auto_str_funcs.free()
g.dump_funcs.free()
g.comptime_definitions.free()
g.pcs_declarations.free()
g.hotcode_definitions.free()
g.embedded_data.free()
g.shared_types.free()
g.shared_functions.free()
g.channel_definitions.free()
g.thread_definitions.free()
g.out_options_forward.free()
g.out_options.free()
g.out_results_forward.free()
g.out_results.free()
g.json_forward_decls.free()
g.enum_typedefs.free()
g.sql_buf.free()
for _, mut v in g.cleanups {
v.free()
}
}
}
pub fn (mut g Gen) gen_file() {
g.timers.start('cgen_file ${g.file.path}')
g.unique_file_path_hash = fnv1a.sum64_string(g.file.path)
if g.pref.is_vlines {
g.vlines_path = util.vlines_escape_path(g.file.path, g.pref.ccompiler)
g.is_vlines_enabled = true
g.inside_ternary = 0
}
g.stmts(g.file.stmts)
// Transfer embedded files
for path in g.file.embedded_files {
if path !in g.embedded_files {
g.embedded_files << path
}
}
g.timers.show('cgen_file ${g.file.path}')
}
pub fn (g &Gen) hashes() string {
return c_commit_hash_default.replace('@@@', version.vhash())
}
pub fn (mut g Gen) init() {
if g.pref.custom_prelude != '' {
g.cheaders.writeln(g.pref.custom_prelude)
} else if !g.pref.no_preludes {
g.cheaders.writeln('// Generated by the V compiler')
if g.pref.os == .wasm32 {
g.cheaders.writeln('#define VWASM 1')
// Include <stdint.h> instead of <inttypes.h> for WASM target
g.cheaders.writeln('#include <stdint.h>')
g.cheaders.writeln('#include <stddef.h>')
} else {
tcc_undef_has_include := '
#if defined(__TINYC__) && defined(__has_include)
// tcc does not support has_include properly yet, turn it off completely
#undef __has_include
#endif'
g.preincludes.writeln(tcc_undef_has_include)
g.cheaders.writeln(tcc_undef_has_include)
g.includes.writeln(tcc_undef_has_include)
if g.pref.os == .freebsd {
g.cheaders.writeln('#include <inttypes.h>')
g.cheaders.writeln('#include <stddef.h>')
} else {
g.cheaders.writeln(get_guarded_include_text('<inttypes.h>', 'The C compiler can not find <inttypes.h>. Please install build-essentials')) // int64_t etc
if g.pref.os == .ios {
g.cheaders.writeln(get_guarded_include_text('<stdbool.h>', 'The C compiler can not find <stdbool.h>. Please install build-essentials')) // bool, true, false
}
g.cheaders.writeln(get_guarded_include_text('<stddef.h>', 'The C compiler can not find <stddef.h>. Please install build-essentials')) // size_t, ptrdiff_t
}
}
if g.pref.nofloat {
g.cheaders.writeln('#define VNOFLOAT 1')
}
g.cheaders.writeln(c_builtin_types)
if g.pref.is_bare {
g.cheaders.writeln(c_bare_headers)
} else {
g.cheaders.writeln(c_headers)
}
if !g.pref.skip_unused || g.table.used_maps > 0 {
g.cheaders.writeln(c_wyhash_headers)
}
}
if g.pref.os == .ios {
g.cheaders.writeln('#define __TARGET_IOS__ 1')
g.cheaders.writeln('#include <spawn.h>')
}
g.write_builtin_types()
g.options_pos_forward = g.type_definitions.len
g.write_typedef_types()
g.write_typeof_functions()
g.write_sorted_types()
g.write_array_fixed_return_types()
g.write_multi_return_types()
g.definitions.writeln('// end of definitions #endif')
if g.pref.compile_defines_all.len > 0 {
g.comptime_definitions.writeln('// V compile time defines by -d or -define flags:')
g.comptime_definitions.writeln('// All custom defines : ' +
g.pref.compile_defines_all.join(','))
g.comptime_definitions.writeln('// Turned ON custom defines: ' +
g.pref.compile_defines.join(','))
for cdefine in g.pref.compile_defines {
g.comptime_definitions.writeln('#define CUSTOM_DEFINE_${cdefine}')
}
g.comptime_definitions.writeln('')
}
if g.table.gostmts > 0 {
g.comptime_definitions.writeln('#define __VTHREADS__ (1)')
}
if g.pref.gc_mode in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt, .boehm_leak] {
g.comptime_definitions.writeln('#define _VGCBOEHM (1)')
}
if g.pref.is_debug || 'debug' in g.pref.compile_defines {
g.comptime_definitions.writeln('#define _VDEBUG (1)')
}
if g.pref.is_prod || 'prod' in g.pref.compile_defines {
g.comptime_definitions.writeln('#define _VPROD (1)')
}
if g.pref.is_test || 'test' in g.pref.compile_defines {
g.comptime_definitions.writeln('#define _VTEST (1)')
}
if g.pref.is_prof || 'profile' in g.pref.compile_defines {
g.comptime_definitions.writeln('#define _VPROFILE (1)')
}
if g.pref.autofree {
g.comptime_definitions.writeln('#define _VAUTOFREE (1)')
} else {
g.comptime_definitions.writeln('#define _VAUTOFREE (0)')
}
if g.pref.prealloc {
g.comptime_definitions.writeln('#define _VPREALLOC (1)')
}
if g.pref.use_cache {
g.comptime_definitions.writeln('#define _VUSECACHE (1)')
}
if g.pref.build_mode == .build_module {
g.comptime_definitions.writeln('#define _VBUILDMODULE (1)')
}
if g.pref.is_livemain || g.pref.is_liveshared {
g.generate_hotcode_reloading_declarations()
}
// Obfuscate only functions in the main module for now.
// Generate the obf_ast.
if g.pref.obfuscate {
mut i := 0
// fns
for key, f in g.table.fns {
if f.mod != 'main' && key != 'main' {
continue
}
g.obf_table[key] = '_f${i}'
i++
}
// methods
for type_sym in g.table.type_symbols {
if type_sym.mod != 'main' {
continue
}
for method in type_sym.methods {
g.obf_table[type_sym.name + '.' + method.name] = '_f${i}'
i++
}
}
}
// we know that this is being called before the multi-threading starts
// and this is being called in the main thread, so we can mutate the table
mut muttable := unsafe { &ast.Table(g.table) }
if g.use_segfault_handler {
muttable.used_fns['v_segmentation_fault_handler'] = true
}
muttable.used_fns['eprintln'] = true
muttable.used_fns['print_backtrace'] = true
muttable.used_fns['exit'] = true
}
pub fn (mut g Gen) finish() {
if g.pref.is_prof && g.pref.build_mode != .build_module {
g.gen_vprint_profile_stats()
}
if g.pref.is_livemain || g.pref.is_liveshared {
g.generate_hotcode_reloader_code()
}
g.handle_embedded_files_finish()
if g.pref.is_test {
g.gen_c_main_for_tests()
} else {
g.gen_c_main()
}
}
pub fn (mut g Gen) write_typeof_functions() {
g.writeln('')
g.writeln('// >> typeof() support for sum types / interfaces')
for ityp, sym in g.table.type_symbols {
if sym.kind == .sum_type {
static_prefix := if g.pref.build_mode == .build_module { 'static ' } else { '' }
sum_info := sym.info as ast.SumType
if sum_info.is_generic {
continue
}
g.writeln('${static_prefix}char * v_typeof_sumtype_${sym.cname}(int sidx) { /* ${sym.name} */ ')
if g.pref.build_mode == .build_module {
g.writeln('\t\tif( sidx == _v_type_idx_${sym.cname}() ) return "${util.strip_main_name(sym.name)}";')
for v in sum_info.variants {
subtype := g.table.sym(v)
g.writeln('\tif( sidx == _v_type_idx_${subtype.cname}() ) return "${util.strip_main_name(subtype.name)}";')
}
g.writeln('\treturn "unknown ${util.strip_main_name(sym.name)}";')
} else {
tidx := g.table.find_type_idx(sym.name)
g.writeln('\tswitch(sidx) {')
g.writeln('\t\tcase ${tidx}: return "${util.strip_main_name(sym.name)}";')
for v in sum_info.variants {
subtype := g.table.sym(v)
g.writeln('\t\tcase ${v.idx()}: return "${util.strip_main_name(subtype.name)}";')
}
g.writeln('\t\tdefault: return "unknown ${util.strip_main_name(sym.name)}";')
g.writeln('\t}')
}
g.writeln('}')
g.writeln('')
g.writeln('${static_prefix}int v_typeof_sumtype_idx_${sym.cname}(int sidx) { /* ${sym.name} */ ')
if g.pref.build_mode == .build_module {
g.writeln('\t\tif( sidx == _v_type_idx_${sym.cname}() ) return ${int(ityp)};')
for v in sum_info.variants {
subtype := g.table.sym(v)
g.writeln('\tif( sidx == _v_type_idx_${subtype.cname}() ) return ${int(v)};')
}
g.writeln('\treturn ${int(ityp)};')
} else {
tidx := g.table.find_type_idx(sym.name)
g.writeln('\tswitch(sidx) {')
g.writeln('\t\tcase ${tidx}: return ${int(ityp)};')
for v in sum_info.variants {
g.writeln('\t\tcase ${v.idx()}: return ${int(v)};')
}
g.writeln('\t\tdefault: return ${int(ityp)};')
g.writeln('\t}')
}
g.writeln('}')
} else if sym.kind == .interface_ {
if sym.info !is ast.Interface {
continue
}
inter_info := sym.info as ast.Interface
if inter_info.is_generic {
continue
}
g.definitions.writeln('static char * v_typeof_interface_${sym.cname}(int sidx);')
g.writeln('static char * v_typeof_interface_${sym.cname}(int sidx) { /* ${sym.name} */ ')
for t in inter_info.types {
sub_sym := g.table.sym(ast.mktyp(t))
g.writeln('\tif (sidx == _${sym.cname}_${sub_sym.cname}_index) return "${util.strip_main_name(sub_sym.name)}";')
}
g.writeln('\treturn "unknown ${util.strip_main_name(sym.name)}";')
g.writeln('}')
g.writeln('')
g.writeln('static int v_typeof_interface_idx_${sym.cname}(int sidx) { /* ${sym.name} */ ')
for t in inter_info.types {
sub_sym := g.table.sym(ast.mktyp(t))
g.writeln('\tif (sidx == _${sym.cname}_${sub_sym.cname}_index) return ${int(t.set_nr_muls(0))};')
}
g.writeln('\treturn ${int(ityp)};')
g.writeln('}')
}
}
g.writeln('// << typeof() support for sum types')
g.writeln('')
}
// V type to C typecc
fn (mut g Gen) typ(t ast.Type) string {
if t.has_flag(.option) {
// Register an optional if it's not registered yet
return g.register_option(t)
} else if t.has_flag(.result) {
return g.register_result(t)
} else {
return g.base_type(t)
}
}