Skip to content

Commit

Permalink
camel_to_snake(); globals consts keep their c names; remove is_builti…
Browse files Browse the repository at this point in the history
…n check, this is no need any more; build the new c2v.tree with only needed nodes, remove header files' node; (#179)
  • Loading branch information
kbkpbot authored Jul 8, 2024
1 parent b0dc0fa commit eafdd3c
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 287 deletions.
722 changes: 521 additions & 201 deletions src/c2v.v

Large diffs are not rendered by default.

40 changes: 23 additions & 17 deletions src/node.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module main
struct Node {
id string
kind_str string @[json: 'kind'] // e.g. "IntegerLiteral"
location NodeLocation @[json: 'loc']
range Range
previous_declaration string @[json: 'previousDecl']
name string // e.g. "my_var_name"
ast_type AstJsonType @[json: 'type']
Expand All @@ -19,39 +17,57 @@ struct Node {
value_number int @[json: 'value'] // For CharacterLiterals, since `value` is a number there, not at string
opcode string // e.g. "+" in BinaryOperator
ast_argument_type AstJsonType @[json: 'argType']
array_filler []Node // for InitListExpr
declaration_id string @[json: 'declId'] // for goto labels
label_id string @[json: 'targetLabelDeclId'] // for goto statements
is_postfix bool @[json: 'isPostfix']
mut:
//parent_node &Node [skip] = unsafe {nil }
location NodeLocation @[json: 'loc']
comment string @[skip] // comment string before this node
unique_id int = -1 @[skip]
range Range
inner []Node
array_filler []Node // for InitListExpr
ref_declaration RefDeclarationNode @[json: 'referencedDecl'] //&Node
kind NodeKind @[skip]
current_child_id int @[skip]
is_builtin_type bool @[skip]
redeclarations_count int @[skip] // increased when some *other* Node had previous_decl == this Node.id
}
// vfmt on

struct NodeLocation {
mut:
offset int
file string
file string @[json: 'file']
line int
source_file SourceFile @[json: 'includedFrom']
spelling_file SourceFile @[json: 'spellingLoc']
file_index int = -1
}

struct Range {
mut:
begin Begin
end End
}

struct Begin {
spelling_file SourceFile @[json: 'spellingLoc']
mut:
offset int
spelling_file SourceFile @[json: 'spellingLoc']
expansion_file SourceFile @[json: 'expansionLoc']
}

struct End {
mut:
offset int
spelling_file SourceFile @[json: 'spellingLoc']
expansion_file SourceFile @[json: 'expansionLoc']
}

struct SourceFile {
path string @[json: 'file']
offset int @[json: 'offset']
path string @[json: 'file']
}

struct AstJsonType {
Expand Down Expand Up @@ -146,13 +162,3 @@ fn (mut node Node) initialize_node_and_children() {
child.initialize_node_and_children()
}
}

fn (node &Node) is_builtin() bool {
return (node.location.file == '' && node.location.line == 0 && node.location.offset == 0
&& node.location.spelling_file.path == '' && node.range.begin.spelling_file.path == '')
|| line_is_builtin_header(node.location.file)
|| line_is_builtin_header(node.location.source_file.path)
|| line_is_builtin_header(node.location.spelling_file.path)
|| line_is_builtin_header(node.range.begin.spelling_file.path)
|| node.name in builtin_fn_names
}
51 changes: 25 additions & 26 deletions src/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn (mut c C2V) record_decl(node &Node) {
if node.kindof(.record_decl) && node.inner.len == 0 {
return
}
mut name := node.name
mut c_name := node.name
// Dont generate struct header if it was already generated by typedef
// Confusing, but typedefs in C AST are really messy.
// ...
Expand All @@ -21,41 +21,41 @@ fn (mut c C2V) record_decl(node &Node) {
if c.is_verbose {
c.genln('// typedef struct')
}
name = next_node.name
if name.contains('apthing_t') {
c_name = next_node.name
if c_name.contains('apthing_t') {
vprintln(node.str())
}
}
}

if name in builtin_type_names {
if c_name in builtin_type_names {
return
}
if c.is_verbose {
c.genln('// struct decl name="${name}"')
c.genln('// struct decl name="${c_name}"')
}
if name in c.types {
if c_name in c.types {
return
}
// Anonymous struct, most likely the next node is a vardecl with this anon struct type, so remember it
if name == '' {
name = 'AnonStruct_${node.location.line}'
c.last_declared_type_name = name
if c_name == '' {
c_name = 'AnonStruct_${node.location.line}'
c.last_declared_type_name = c_name
}
if name !in ['struct', 'union'] {
c.types << name
name = capitalize_type(name)
if c_name !in ['struct', 'union'] {
v_name := c.add_struct_name(mut c.types, c_name)
if node.tags.contains('union') {
c.genln('union ${name} { ')
c.genln('union ${v_name} { ')
} else {
c.genln('struct ${name} { ')
c.genln('struct ${v_name} { ')
}
}
mut new_struct := Struct{}
// in V it's `field struct {...}`, but in C we get struct definition first, so save it and use it in the
// next child
mut anon_struct_definition := ''
for field in node.inner {
c.gen_comment(field)
// Handle anon structs
if field.kind == .record_decl {
anon_struct_definition = c.anon_struct_field_type(field)
Expand Down Expand Up @@ -89,7 +89,7 @@ fn (mut c C2V) record_decl(node &Node) {
c.genln('\t${field_name} ${field_type_name}')
}
}
c.structs[name] = new_struct
c.structs[c_name] = new_struct
c.genln('}')
}

Expand All @@ -115,41 +115,40 @@ fn (mut c C2V) typedef_decl(node &Node) {
// just a single line typedef: (alias)
// typedef sha1_context_t sha1_context_s ;
// typedef after enum decl, just generate "enum NAME {" header
mut alias_name := node.name // get_val(-2)
vprintln('TYPEDEF "${node.name}" ${node.is_builtin_type} ${typ}')
if alias_name.contains('et_context_t') {
mut c_alias_name := node.name // get_val(-2)
if c_alias_name.contains('et_context_t') {
// TODO remove this
return
}
if node.name in builtin_type_names {
if c_alias_name in builtin_type_names {
return
}

if alias_name in c.types || alias_name in c.enums {
if c_alias_name in c.types || c_alias_name in c.enums {
// This means that this is a struct/enum typedef that has already been defined.
return
}

c.types << alias_name
v_alias_name := c.add_var_func_name(mut c.types, c_alias_name)

if typ.starts_with('struct ') && typ.ends_with(' *') {
// Opaque pointer, for example: typedef struct TSTexture_t *TSTexture;
c.genln('type ${alias_name} = voidptr')
c.genln('type ${v_alias_name} = voidptr')
return
}

if !typ.contains(alias_name) {
if !typ.contains(c_alias_name) {
if typ.contains('(*)') {
tt := convert_type(typ)
typ = tt.name
}
// Struct types have junk before spaces
else {
alias_name = alias_name.all_after(' ')
c_alias_name = c_alias_name.all_after(' ')
tt := convert_type(typ)
typ = tt.name
}
if alias_name.starts_with('__') {
if c_alias_name.starts_with('__') {
// Skip internal stuff like __builtin_ms_va_list
return
}
Expand All @@ -166,7 +165,7 @@ fn (mut c C2V) typedef_decl(node &Node) {
// TODO handle this better
cgen_alias = cgen_alias.capitalize()
}
c.genln('type ${alias_name.capitalize()} = ${cgen_alias}') // typedef alias (SINGLE LINE)')
c.genln('type ${c_alias_name.capitalize()} = ${cgen_alias}') // typedef alias (SINGLE LINE)')
return
}
if typ.contains('enum ') {
Expand Down
32 changes: 16 additions & 16 deletions tests/10.jni.out
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ const jvm_constant_class = 7
const jvm_constant_string = 8
const jvm_constant_fieldref = 9
const jvm_constant_methodref = 10
const jvm_constant_interfacemethodref = 11
const jvm_constant_nameandtype = 12
const jvm_constant_methodhandle = 15
const jvm_constant_methodtype = 16
const jvm_constant_interface_methodref = 11
const jvm_constant_name_and_type = 12
const jvm_constant_method_handle = 15
const jvm_constant_method_type = 16
const jvm_constant_dynamic = 17
const jvm_constant_invokedynamic = 18
const jvm_constant_invoke_dynamic = 18
const jvm_constant_module = 19
const jvm_constant_package = 20
const jvm_constant_externalmax = 20
const jvm_constant_external_max = 20

// empty enum
const jvm_ref_getfield = 1
const jvm_ref_getstatic = 2
const jvm_ref_putfield = 3
const jvm_ref_putstatic = 4
const jvm_ref_invokevirtual = 5
const jvm_ref_invokestatic = 6
const jvm_ref_invokespecial = 7
const jvm_ref_newinvokespecial = 8
const jvm_ref_invokeinterface = 9
const jvm_ref_get_field = 1
const jvm_ref_get_static = 2
const jvm_ref_put_field = 3
const jvm_ref_put_static = 4
const jvm_ref_invoke_virtual = 5
const jvm_ref_invoke_static = 6
const jvm_ref_invoke_special = 7
const jvm_ref_new_invoke_special = 8
const jvm_ref_invoke_interface = 9

// empty enum
const jvm_item_top = 0
Expand All @@ -71,7 +71,7 @@ const jvm_item_float = 2
const jvm_item_double = 3
const jvm_item_long = 4
const jvm_item_null = 5
const jvm_item_uninitializedthis = 6
const jvm_item_uninitialized_this = 6
const jvm_item_object = 7
const jvm_item_uninitialized = 8

Expand Down
12 changes: 6 additions & 6 deletions tests/11.enum_default.out
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ fn enum_func_const(a MyEnum) {
}

fn main() {
myenumvar := MyEnum.a
myenumvar2 := MyAnotherEnum.d
myenumvar3 := MyStrangeEnum.g
myintvar := j
enum_func(myenumvar)
enum_func_const(myenumvar)
my_enum_var := MyEnum.a
my_enum_var2 := MyAnotherEnum.d
my_enum_var3 := MyStrangeEnum.g
my_int_var := j
enum_func(my_enum_var)
enum_func_const(my_enum_var)
return
}
8 changes: 4 additions & 4 deletions tests/8.simple_func_header.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ module tests

fn C.testFunc()

pub fn testfunc() {
pub fn test_func() {
C.testFunc()
}

fn C.testFunc2(remove int)

pub fn testfunc2(remove int) {
pub fn test_func2(remove int) {
C.testFunc2(remove)
}

fn C.testFunc3(shared_ int)

pub fn testfunc3(shared_ int) {
pub fn test_func3(shared_ int) {
C.testFunc3(shared_)
}

fn C.testFunc4(select_ int)

pub fn testfunc4(select_ int) {
pub fn test_func4(select_ int) {
C.testFunc4(select_)
}
2 changes: 1 addition & 1 deletion tests/9.func_declaration.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module tests

fn C.testFunc()

pub fn testfunc() {
pub fn test_func() {
C.testFunc()
}
12 changes: 5 additions & 7 deletions tests/run_doom_tests.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ const files = [
'd_items',
]

const (
exe = executable()
tests_dir = dir(exe)
c2v_dir = dir(tests_dir)
doom_dir = join_path(dir(c2v_dir), 'doom')
const exe = executable()
const tests_dir = dir(exe)
const c2v_dir = dir(tests_dir)
const doom_dir = join_path(dir(c2v_dir), 'doom')

src_dir = join_path(doom_dir, 'src/doom')
)
const src_dir = join_path(doom_dir, 'src/doom')

fn main() {
println(src_dir)
Expand Down
16 changes: 7 additions & 9 deletions tests/run_tests.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import term
import os

const (
c2v_dir = @VMODROOT
tests_dir = join_path(c2v_dir, 'tests')
exe_path = join_path(c2v_dir, $if windows {
'c2v.exe'
} $else {
'c2v'
})
)
const c2v_dir = @VMODROOT
const tests_dir = join_path(c2v_dir, 'tests')
const exe_path = join_path(c2v_dir, $if windows {
'c2v.exe'
} $else {
'c2v'
})

fn replace_file_extension(file_path string, old_extension string, new_extension string) string {
// NOTE: It can't be just `file_path.replace(old_extenstion, new_extension)`, because it will replace all occurencies of old_extenstion string.
Expand Down
Empty file modified tools/build_doom_file.vsh
100644 → 100755
Empty file.

0 comments on commit eafdd3c

Please sign in to comment.