1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: fix struct order bug

This commit is contained in:
Alexander Medvednikov 2019-08-29 01:52:32 +03:00
parent c6b79dfd24
commit 2fe20cd092
4 changed files with 199 additions and 156 deletions

View File

@ -14,7 +14,6 @@ struct CGen {
typedefs []string
type_aliases []string
includes []string
//types []string
thread_args []string
thread_fns []string
consts []string
@ -217,15 +216,6 @@ fn (p mut Parser) print_prof_counters() string {
return res.join(';\n')
}
/*
fn (p mut Parser) gen_type(s string) {
if !p.first_pass() {
return
}
p.cgen.types << s
}
*/
fn (p mut Parser) gen_typedef(s string) {
if !p.first_pass() {
return
@ -297,24 +287,38 @@ fn platform_postfix_to_ifdefguard(name string) string {
}
// C struct definitions, ordered
// Sort the types, make sure types that are referenced by other types
// are added before them.
fn (v mut V) c_type_definitions() string {
mut types := v.table.types
// Sort the types, make sure types that are referenced by other types
// are added before them.
for i in 0 .. types.len {
for j in 0 .. i {
t := types[i]
if types[j].contains_field_type(t.name) {
types[i] = types[j]
types[j] = t
mut types := []Type // structs that need to be sorted
mut top_types := []Type // builtin types and types that only have primitive fields
for t in v.table.types {
if !t.name[0].is_capital() {
top_types << t
continue
}
mut only_builtin_fields := true
for field in t.fields {
if field.typ[0].is_capital() {
only_builtin_fields = false
break
}
}
if only_builtin_fields {
top_types << t
continue
}
types << t
}
sort_structs(mut types)
// Generate C code
return types_to_c(top_types, v.table) + '\n/*----*/\n' +
types_to_c(types, v.table)
}
fn types_to_c(types []Type, table &Table) string {
mut sb := strings.new_builder(10)
for t in v.table.types {
for t in types {
if t.cat != .union_ && t.cat != .struct_ {
continue
}
@ -327,14 +331,38 @@ fn (v mut V) c_type_definitions() string {
kind := if t.cat == .union_ {'union'} else {'struct'}
sb.writeln('$kind $t.name {')
for field in t.fields {
sb.writeln(v.table.cgen_name_type_pair(field.name,
sb.writeln(table.cgen_name_type_pair(field.name,
field.typ) + ';')
}
sb.writeln('};\n')
//if is_objc {
//p.gen_type('@end')
//sb.writeln('@end')
//}
}
return sb.str()
}
// pretty inefficient algo, works fine with N < 1000 (TODO optimize)
fn sort_structs(types mut []Type) {
mut cnt := 0
for i := 0; i < types.len; i++ {
for j in 0 .. i {
t := types[i]
//t2 := types[j]
// check if any of the types before `t` reference `t`
if types[j].contains_field_type(t.name) {
//println('moving up: $t.name len=$types.len')
types.insert(j, t)
types.delete(i+1)
i = 0 // Start from scratch
cnt++
if cnt > 500 {
println('infinite type loop (perhaps you have a recursive struct `$t.name`?)')
exit(1)
}
continue
}
}
}
}

View File

@ -4,9 +4,11 @@
module main
import os
import time
import strings
import (
os
time
strings
)
const (
Version = '0.1.18'
@ -298,7 +300,6 @@ fn (v mut V) compile() {
mut d := strings.new_builder(10000)// Avoid unnecessary allocations
d.writeln(cgen.includes.join_lines())
d.writeln(cgen.typedefs.join_lines())
//d.writeln(cgen.types.join_lines())
d.writeln(v.c_type_definitions())
d.writeln('\nstring _STR(const char*, ...);\n')
d.writeln('\nstring _STR_TMP(const char*, ...);\n')

View File

@ -7,29 +7,6 @@ module main
import math
import strings
struct Var {
mut:
typ string
name string
is_arg bool
is_const bool
args []Var // function args
attr string // [json] etc
is_mut bool
is_alloc bool
ptr bool
ref bool
parent_fn string // Variables can only be defined in functions
mod string // module where this var is stored
line_nr int
access_mod AccessMod
is_global bool // __global (translated from C only)
is_used bool
is_changed bool
scope_level int
}
struct Table {
mut:
types []Type
@ -45,6 +22,9 @@ mut:
obfuscate bool
}
struct GenTable {
fn_name string
types []string
@ -77,6 +57,30 @@ enum TypeCategory {
c_typedef
}
struct Var {
mut:
typ string
name string
is_arg bool
is_const bool
args []Var // function args
attr string // [json] etc
is_mut bool
is_alloc bool
ptr bool
ref bool
parent_fn string // Variables can only be defined in functions
mod string // module where this var is stored
line_nr int
access_mod AccessMod
is_global bool // __global (translated from C only)
is_used bool
is_changed bool
scope_level int
}
struct Type {
mut:
mod string
@ -96,6 +100,13 @@ mut:
gen_str bool // needs `.str()` method generation
}
struct TypeNode {
mut:
next &TypeNode
typ Type
}
// For debugging types
fn (t Type) str() string {
mut s := 'type "$t.name" {'
@ -928,6 +939,9 @@ fn (fit &FileImportTable) resolve_alias(alias string) string {
}
fn (t &Type) contains_field_type(typ string) bool {
if !t.name[0].is_capital() {
return false
}
for field in t.fields {
if field.typ == typ {
return true

View File

@ -8,14 +8,14 @@ import strings
struct map {
element_size int
root *Node
root *mapnode
pub:
size int
}
struct Node {
left *Node
right *Node
struct mapnode {
left *mapnode
right *mapnode
is_empty bool
key string
val voidptr
@ -41,8 +41,8 @@ fn new_map_init(cap, elm_size int, keys *string, vals voidptr) map {
return res
}
fn new_node(key string, val voidptr, element_size int) *Node {
new_e := &Node {
fn new_node(key string, val voidptr, element_size int) *mapnode {
new_e := &mapnode {
key: key
val: malloc(element_size)
left: 0
@ -52,7 +52,7 @@ fn new_node(key string, val voidptr, element_size int) *Node {
return new_e
}
fn (m mut map) insert(n mut Node, key string, val voidptr) {
fn (m mut map) insert(n mut mapnode, key string, val voidptr) {
if n.key == key {
C.memcpy(n.val, val, m.element_size)
return
@ -74,7 +74,7 @@ fn (m mut map) insert(n mut Node, key string, val voidptr) {
}
}
fn (n & Node) find(key string, out voidptr, element_size int) bool{
fn (n & mapnode) find(key string, out voidptr, element_size int) bool{
if n.key == key {
C.memcpy(out, n.val, element_size)
return true
@ -96,7 +96,7 @@ fn (n & Node) find(key string, out voidptr, element_size int) bool{
}
// same as `find`, but doesn't return a value. Used by `exists`
fn (n & Node) find2(key string, element_size int) bool{
fn (n & mapnode) find2(key string, element_size int) bool{
if n.key == key {
return true
}
@ -152,7 +152,7 @@ fn (m map) bs(query string, start, end int, out voidptr) {
}
*/
fn preorder_keys(node &Node, keys mut []string, key_i int) int {
fn preorder_keys(node &mapnode, keys mut []string, key_i int) int {
mut i := key_i
if !node.is_empty {
mut a := *keys
@ -184,7 +184,7 @@ fn (m map) get(key string, out voidptr) bool {
return m.root.find(key, out, m.element_size)
}
pub fn (n mut Node) delete(key string, element_size int) {
pub fn (n mut mapnode) delete(key string, element_size int) {
if n.key == key {
C.memset(n.val, 0, element_size)
n.is_empty = true