Skip to content

Commit eafdd3c

Browse files
authored
camel_to_snake(); globals consts keep their c names; remove is_builtin check, this is no need any more; build the new c2v.tree with only needed nodes, remove header files' node; (#179)
1 parent b0dc0fa commit eafdd3c

10 files changed

+608
-287
lines changed

src/c2v.v

+521-201
Large diffs are not rendered by default.

src/node.v

+23-17
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module main
77
struct Node {
88
id string
99
kind_str string @[json: 'kind'] // e.g. "IntegerLiteral"
10-
location NodeLocation @[json: 'loc']
11-
range Range
1210
previous_declaration string @[json: 'previousDecl']
1311
name string // e.g. "my_var_name"
1412
ast_type AstJsonType @[json: 'type']
@@ -19,39 +17,57 @@ struct Node {
1917
value_number int @[json: 'value'] // For CharacterLiterals, since `value` is a number there, not at string
2018
opcode string // e.g. "+" in BinaryOperator
2119
ast_argument_type AstJsonType @[json: 'argType']
22-
array_filler []Node // for InitListExpr
2320
declaration_id string @[json: 'declId'] // for goto labels
2421
label_id string @[json: 'targetLabelDeclId'] // for goto statements
2522
is_postfix bool @[json: 'isPostfix']
2623
mut:
2724
//parent_node &Node [skip] = unsafe {nil }
25+
location NodeLocation @[json: 'loc']
26+
comment string @[skip] // comment string before this node
27+
unique_id int = -1 @[skip]
28+
range Range
2829
inner []Node
30+
array_filler []Node // for InitListExpr
2931
ref_declaration RefDeclarationNode @[json: 'referencedDecl'] //&Node
3032
kind NodeKind @[skip]
3133
current_child_id int @[skip]
32-
is_builtin_type bool @[skip]
3334
redeclarations_count int @[skip] // increased when some *other* Node had previous_decl == this Node.id
3435
}
3536
// vfmt on
3637

3738
struct NodeLocation {
39+
mut:
3840
offset int
39-
file string
41+
file string @[json: 'file']
4042
line int
4143
source_file SourceFile @[json: 'includedFrom']
4244
spelling_file SourceFile @[json: 'spellingLoc']
45+
file_index int = -1
4346
}
4447

4548
struct Range {
49+
mut:
4650
begin Begin
51+
end End
4752
}
4853

4954
struct Begin {
50-
spelling_file SourceFile @[json: 'spellingLoc']
55+
mut:
56+
offset int
57+
spelling_file SourceFile @[json: 'spellingLoc']
58+
expansion_file SourceFile @[json: 'expansionLoc']
59+
}
60+
61+
struct End {
62+
mut:
63+
offset int
64+
spelling_file SourceFile @[json: 'spellingLoc']
65+
expansion_file SourceFile @[json: 'expansionLoc']
5166
}
5267

5368
struct SourceFile {
54-
path string @[json: 'file']
69+
offset int @[json: 'offset']
70+
path string @[json: 'file']
5571
}
5672

5773
struct AstJsonType {
@@ -146,13 +162,3 @@ fn (mut node Node) initialize_node_and_children() {
146162
child.initialize_node_and_children()
147163
}
148164
}
149-
150-
fn (node &Node) is_builtin() bool {
151-
return (node.location.file == '' && node.location.line == 0 && node.location.offset == 0
152-
&& node.location.spelling_file.path == '' && node.range.begin.spelling_file.path == '')
153-
|| line_is_builtin_header(node.location.file)
154-
|| line_is_builtin_header(node.location.source_file.path)
155-
|| line_is_builtin_header(node.location.spelling_file.path)
156-
|| line_is_builtin_header(node.range.begin.spelling_file.path)
157-
|| node.name in builtin_fn_names
158-
}

src/struct.v

+25-26
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn (mut c C2V) record_decl(node &Node) {
99
if node.kindof(.record_decl) && node.inner.len == 0 {
1010
return
1111
}
12-
mut name := node.name
12+
mut c_name := node.name
1313
// Dont generate struct header if it was already generated by typedef
1414
// Confusing, but typedefs in C AST are really messy.
1515
// ...
@@ -21,41 +21,41 @@ fn (mut c C2V) record_decl(node &Node) {
2121
if c.is_verbose {
2222
c.genln('// typedef struct')
2323
}
24-
name = next_node.name
25-
if name.contains('apthing_t') {
24+
c_name = next_node.name
25+
if c_name.contains('apthing_t') {
2626
vprintln(node.str())
2727
}
2828
}
2929
}
3030

31-
if name in builtin_type_names {
31+
if c_name in builtin_type_names {
3232
return
3333
}
3434
if c.is_verbose {
35-
c.genln('// struct decl name="${name}"')
35+
c.genln('// struct decl name="${c_name}"')
3636
}
37-
if name in c.types {
37+
if c_name in c.types {
3838
return
3939
}
4040
// Anonymous struct, most likely the next node is a vardecl with this anon struct type, so remember it
41-
if name == '' {
42-
name = 'AnonStruct_${node.location.line}'
43-
c.last_declared_type_name = name
41+
if c_name == '' {
42+
c_name = 'AnonStruct_${node.location.line}'
43+
c.last_declared_type_name = c_name
4444
}
45-
if name !in ['struct', 'union'] {
46-
c.types << name
47-
name = capitalize_type(name)
45+
if c_name !in ['struct', 'union'] {
46+
v_name := c.add_struct_name(mut c.types, c_name)
4847
if node.tags.contains('union') {
49-
c.genln('union ${name} { ')
48+
c.genln('union ${v_name} { ')
5049
} else {
51-
c.genln('struct ${name} { ')
50+
c.genln('struct ${v_name} { ')
5251
}
5352
}
5453
mut new_struct := Struct{}
5554
// in V it's `field struct {...}`, but in C we get struct definition first, so save it and use it in the
5655
// next child
5756
mut anon_struct_definition := ''
5857
for field in node.inner {
58+
c.gen_comment(field)
5959
// Handle anon structs
6060
if field.kind == .record_decl {
6161
anon_struct_definition = c.anon_struct_field_type(field)
@@ -89,7 +89,7 @@ fn (mut c C2V) record_decl(node &Node) {
8989
c.genln('\t${field_name} ${field_type_name}')
9090
}
9191
}
92-
c.structs[name] = new_struct
92+
c.structs[c_name] = new_struct
9393
c.genln('}')
9494
}
9595

@@ -115,41 +115,40 @@ fn (mut c C2V) typedef_decl(node &Node) {
115115
// just a single line typedef: (alias)
116116
// typedef sha1_context_t sha1_context_s ;
117117
// typedef after enum decl, just generate "enum NAME {" header
118-
mut alias_name := node.name // get_val(-2)
119-
vprintln('TYPEDEF "${node.name}" ${node.is_builtin_type} ${typ}')
120-
if alias_name.contains('et_context_t') {
118+
mut c_alias_name := node.name // get_val(-2)
119+
if c_alias_name.contains('et_context_t') {
121120
// TODO remove this
122121
return
123122
}
124-
if node.name in builtin_type_names {
123+
if c_alias_name in builtin_type_names {
125124
return
126125
}
127126

128-
if alias_name in c.types || alias_name in c.enums {
127+
if c_alias_name in c.types || c_alias_name in c.enums {
129128
// This means that this is a struct/enum typedef that has already been defined.
130129
return
131130
}
132131

133-
c.types << alias_name
132+
v_alias_name := c.add_var_func_name(mut c.types, c_alias_name)
134133

135134
if typ.starts_with('struct ') && typ.ends_with(' *') {
136135
// Opaque pointer, for example: typedef struct TSTexture_t *TSTexture;
137-
c.genln('type ${alias_name} = voidptr')
136+
c.genln('type ${v_alias_name} = voidptr')
138137
return
139138
}
140139

141-
if !typ.contains(alias_name) {
140+
if !typ.contains(c_alias_name) {
142141
if typ.contains('(*)') {
143142
tt := convert_type(typ)
144143
typ = tt.name
145144
}
146145
// Struct types have junk before spaces
147146
else {
148-
alias_name = alias_name.all_after(' ')
147+
c_alias_name = c_alias_name.all_after(' ')
149148
tt := convert_type(typ)
150149
typ = tt.name
151150
}
152-
if alias_name.starts_with('__') {
151+
if c_alias_name.starts_with('__') {
153152
// Skip internal stuff like __builtin_ms_va_list
154153
return
155154
}
@@ -166,7 +165,7 @@ fn (mut c C2V) typedef_decl(node &Node) {
166165
// TODO handle this better
167166
cgen_alias = cgen_alias.capitalize()
168167
}
169-
c.genln('type ${alias_name.capitalize()} = ${cgen_alias}') // typedef alias (SINGLE LINE)')
168+
c.genln('type ${c_alias_name.capitalize()} = ${cgen_alias}') // typedef alias (SINGLE LINE)')
170169
return
171170
}
172171
if typ.contains('enum ') {

tests/10.jni.out

+16-16
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,26 @@ const jvm_constant_class = 7
4343
const jvm_constant_string = 8
4444
const jvm_constant_fieldref = 9
4545
const jvm_constant_methodref = 10
46-
const jvm_constant_interfacemethodref = 11
47-
const jvm_constant_nameandtype = 12
48-
const jvm_constant_methodhandle = 15
49-
const jvm_constant_methodtype = 16
46+
const jvm_constant_interface_methodref = 11
47+
const jvm_constant_name_and_type = 12
48+
const jvm_constant_method_handle = 15
49+
const jvm_constant_method_type = 16
5050
const jvm_constant_dynamic = 17
51-
const jvm_constant_invokedynamic = 18
51+
const jvm_constant_invoke_dynamic = 18
5252
const jvm_constant_module = 19
5353
const jvm_constant_package = 20
54-
const jvm_constant_externalmax = 20
54+
const jvm_constant_external_max = 20
5555

5656
// empty enum
57-
const jvm_ref_getfield = 1
58-
const jvm_ref_getstatic = 2
59-
const jvm_ref_putfield = 3
60-
const jvm_ref_putstatic = 4
61-
const jvm_ref_invokevirtual = 5
62-
const jvm_ref_invokestatic = 6
63-
const jvm_ref_invokespecial = 7
64-
const jvm_ref_newinvokespecial = 8
65-
const jvm_ref_invokeinterface = 9
57+
const jvm_ref_get_field = 1
58+
const jvm_ref_get_static = 2
59+
const jvm_ref_put_field = 3
60+
const jvm_ref_put_static = 4
61+
const jvm_ref_invoke_virtual = 5
62+
const jvm_ref_invoke_static = 6
63+
const jvm_ref_invoke_special = 7
64+
const jvm_ref_new_invoke_special = 8
65+
const jvm_ref_invoke_interface = 9
6666

6767
// empty enum
6868
const jvm_item_top = 0
@@ -71,7 +71,7 @@ const jvm_item_float = 2
7171
const jvm_item_double = 3
7272
const jvm_item_long = 4
7373
const jvm_item_null = 5
74-
const jvm_item_uninitializedthis = 6
74+
const jvm_item_uninitialized_this = 6
7575
const jvm_item_object = 7
7676
const jvm_item_uninitialized = 8
7777

tests/11.enum_default.out

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ fn enum_func_const(a MyEnum) {
2929
}
3030

3131
fn main() {
32-
myenumvar := MyEnum.a
33-
myenumvar2 := MyAnotherEnum.d
34-
myenumvar3 := MyStrangeEnum.g
35-
myintvar := j
36-
enum_func(myenumvar)
37-
enum_func_const(myenumvar)
32+
my_enum_var := MyEnum.a
33+
my_enum_var2 := MyAnotherEnum.d
34+
my_enum_var3 := MyStrangeEnum.g
35+
my_int_var := j
36+
enum_func(my_enum_var)
37+
enum_func_const(my_enum_var)
3838
return
3939
}

tests/8.simple_func_header.out

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ module tests
33

44
fn C.testFunc()
55

6-
pub fn testfunc() {
6+
pub fn test_func() {
77
C.testFunc()
88
}
99

1010
fn C.testFunc2(remove int)
1111

12-
pub fn testfunc2(remove int) {
12+
pub fn test_func2(remove int) {
1313
C.testFunc2(remove)
1414
}
1515

1616
fn C.testFunc3(shared_ int)
1717

18-
pub fn testfunc3(shared_ int) {
18+
pub fn test_func3(shared_ int) {
1919
C.testFunc3(shared_)
2020
}
2121

2222
fn C.testFunc4(select_ int)
2323

24-
pub fn testfunc4(select_ int) {
24+
pub fn test_func4(select_ int) {
2525
C.testFunc4(select_)
2626
}

tests/9.func_declaration.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module tests
33

44
fn C.testFunc()
55

6-
pub fn testfunc() {
6+
pub fn test_func() {
77
C.testFunc()
88
}

tests/run_doom_tests.vsh

+5-7
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ const files = [
6363
'd_items',
6464
]
6565

66-
const (
67-
exe = executable()
68-
tests_dir = dir(exe)
69-
c2v_dir = dir(tests_dir)
70-
doom_dir = join_path(dir(c2v_dir), 'doom')
66+
const exe = executable()
67+
const tests_dir = dir(exe)
68+
const c2v_dir = dir(tests_dir)
69+
const doom_dir = join_path(dir(c2v_dir), 'doom')
7170

72-
src_dir = join_path(doom_dir, 'src/doom')
73-
)
71+
const src_dir = join_path(doom_dir, 'src/doom')
7472

7573
fn main() {
7674
println(src_dir)

tests/run_tests.vsh

+7-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
import term
77
import os
88

9-
const (
10-
c2v_dir = @VMODROOT
11-
tests_dir = join_path(c2v_dir, 'tests')
12-
exe_path = join_path(c2v_dir, $if windows {
13-
'c2v.exe'
14-
} $else {
15-
'c2v'
16-
})
17-
)
9+
const c2v_dir = @VMODROOT
10+
const tests_dir = join_path(c2v_dir, 'tests')
11+
const exe_path = join_path(c2v_dir, $if windows {
12+
'c2v.exe'
13+
} $else {
14+
'c2v'
15+
})
1816

1917
fn replace_file_extension(file_path string, old_extension string, new_extension string) string {
2018
// NOTE: It can't be just `file_path.replace(old_extenstion, new_extension)`, because it will replace all occurencies of old_extenstion string.

tools/build_doom_file.vsh

100644100755
File mode changed.

0 commit comments

Comments
 (0)