cgen,fmt: improve sizeof(), fix `type PPType = &&Type`

pull/9369/head
Delyan Angelov 2021-03-19 22:49:46 +02:00
parent c5884a5f4d
commit 4a12546971
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 59 additions and 19 deletions

View File

@ -2186,16 +2186,7 @@ pub fn (mut f Fmt) selector_expr(node ast.SelectorExpr) {
pub fn (mut f Fmt) size_of(node ast.SizeOf) {
f.write('sizeof(')
if node.is_type {
sym := f.table.get_type_symbol(node.typ)
if sym.name != '' {
if f.is_external_name(sym.name) {
f.write(sym.name)
} else {
f.write(f.short_module(sym.name))
}
} else {
f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias))
}
f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias))
} else {
f.expr(node.expr)
}
@ -2350,3 +2341,9 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(node ast.Expr) {
f.expr(node)
}
}
fn (mut f Fmt) trace(fbase string, message string) {
if f.file.path_base == fbase {
println('> f.trace | ${fbase:-10s} | $message')
}
}

View File

@ -727,13 +727,13 @@ fn (g &Gen) type_sidx(t table.Type) string {
//
pub fn (mut g Gen) write_typedef_types() {
for typ in g.table.types {
for typ in g.table.type_symbols {
if typ.name in c.builtins {
continue
}
match typ.kind {
.alias {
parent := unsafe { &g.table.types[typ.parent_idx] }
parent := unsafe { &g.table.type_symbols[typ.parent_idx] }
is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.`
mut is_typedef := false
if parent.info is table.Struct {
@ -746,6 +746,10 @@ pub fn (mut g Gen) write_typedef_types() {
} else {
parent_styp = parent.cname[3..]
}
} else {
if typ.info is table.Alias {
parent_styp = g.typ(typ.info.parent_type)
}
}
g.type_definitions.writeln('typedef $parent_styp $typ.cname;')
}
@ -822,7 +826,7 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym table.TypeSymbol) {
pub fn (mut g Gen) write_multi_return_types() {
g.typedefs.writeln('\n// BEGIN_multi_return_typedefs')
g.type_definitions.writeln('\n// BEGIN_multi_return_structs')
for sym in g.table.types {
for sym in g.table.type_symbols {
if sym.kind != .multi_return {
continue
}
@ -2745,9 +2749,9 @@ fn (mut g Gen) map_fn_ptrs(key_typ table.TypeSymbol) (string, string, string, st
}
.voidptr {
ts := if g.pref.m64 {
&g.table.types[table.u64_type_idx]
&g.table.type_symbols[table.u64_type_idx]
} else {
&g.table.types[table.u32_type_idx]
&g.table.type_symbols[table.u32_type_idx]
}
return g.map_fn_ptrs(ts)
}
@ -5233,7 +5237,7 @@ fn (mut g Gen) write_builtin_types() {
// builtin types need to be on top
// everything except builtin will get sorted
for builtin_name in c.builtins {
sym := g.table.types[g.table.type_idxs[builtin_name]]
sym := g.table.type_symbols[g.table.type_idxs[builtin_name]]
if sym.kind == .interface_ {
g.write_interface_typesymbol_declaration(sym)
} else {
@ -5248,7 +5252,7 @@ fn (mut g Gen) write_builtin_types() {
// are added before them.
fn (mut g Gen) write_sorted_types() {
mut types := []table.TypeSymbol{} // structs that need to be sorted
for typ in g.table.types {
for typ in g.table.type_symbols {
if typ.name !in c.builtins {
types << typ
}
@ -5447,7 +5451,7 @@ fn (g &Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
// sort types
mut types_sorted := []table.TypeSymbol{}
for node in dep_graph_sorted.nodes {
types_sorted << g.table.types[g.table.type_idxs[node.name]]
types_sorted << g.table.type_symbols[g.table.type_idxs[node.name]]
}
return types_sorted
}
@ -6026,7 +6030,7 @@ fn (mut g Gen) is_expr(node ast.InfixExpr) {
// Generates interface table and interface indexes
fn (mut g Gen) interface_table() string {
mut sb := strings.new_builder(100)
for ityp in g.table.types {
for ityp in g.table.type_symbols {
if ityp.kind != .interface_ {
continue
}

View File

@ -0,0 +1,39 @@
struct MyStructInt {
x int
}
type ZZInt = int
type ZZMyStructInt = MyStructInt
type PZZInt = &int
type PZZMyStructInt = &MyStructInt
type PPZZInt = &&int
type PPZZMyStructInt = &&MyStructInt
type PPPZZInt = &&&int
type PPPZZMyStructInt = &&&MyStructInt
fn test_alias_of_pointer_types() {
size_of_int := int(sizeof(int))
size_of_int_ptr := int(sizeof(&int))
dump(size_of_int)
dump(size_of_int_ptr)
eprintln('--------------------------')
dump(sizeof(ZZInt))
dump(sizeof(ZZMyStructInt))
dump(sizeof(PZZInt))
dump(sizeof(PZZMyStructInt))
dump(sizeof(PPZZInt))
dump(sizeof(PPZZMyStructInt))
dump(sizeof(PPPZZInt))
dump(sizeof(PPPZZMyStructInt))
//
assert sizeof(ZZInt) == sizeof(int)
assert sizeof(ZZMyStructInt) == sizeof(int)
//
assert sizeof(PZZInt) == sizeof(voidptr)
assert sizeof(PZZMyStructInt) == sizeof(voidptr)
assert sizeof(PPZZInt) == sizeof(voidptr)
assert sizeof(PPZZMyStructInt) == sizeof(voidptr)
}