cgen,fmt: improve sizeof(), fix `type PPType = &&Type`
parent
c5884a5f4d
commit
4a12546971
|
@ -2186,16 +2186,7 @@ pub fn (mut f Fmt) selector_expr(node ast.SelectorExpr) {
|
||||||
pub fn (mut f Fmt) size_of(node ast.SizeOf) {
|
pub fn (mut f Fmt) size_of(node ast.SizeOf) {
|
||||||
f.write('sizeof(')
|
f.write('sizeof(')
|
||||||
if node.is_type {
|
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 {
|
} else {
|
||||||
f.expr(node.expr)
|
f.expr(node.expr)
|
||||||
}
|
}
|
||||||
|
@ -2350,3 +2341,9 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(node ast.Expr) {
|
||||||
f.expr(node)
|
f.expr(node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn (mut f Fmt) trace(fbase string, message string) {
|
||||||
|
if f.file.path_base == fbase {
|
||||||
|
println('> f.trace | ${fbase:-10s} | $message')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -727,13 +727,13 @@ fn (g &Gen) type_sidx(t table.Type) string {
|
||||||
|
|
||||||
//
|
//
|
||||||
pub fn (mut g Gen) write_typedef_types() {
|
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 {
|
if typ.name in c.builtins {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
match typ.kind {
|
match typ.kind {
|
||||||
.alias {
|
.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] == `.`
|
is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.`
|
||||||
mut is_typedef := false
|
mut is_typedef := false
|
||||||
if parent.info is table.Struct {
|
if parent.info is table.Struct {
|
||||||
|
@ -746,6 +746,10 @@ pub fn (mut g Gen) write_typedef_types() {
|
||||||
} else {
|
} else {
|
||||||
parent_styp = parent.cname[3..]
|
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;')
|
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() {
|
pub fn (mut g Gen) write_multi_return_types() {
|
||||||
g.typedefs.writeln('\n// BEGIN_multi_return_typedefs')
|
g.typedefs.writeln('\n// BEGIN_multi_return_typedefs')
|
||||||
g.type_definitions.writeln('\n// BEGIN_multi_return_structs')
|
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 {
|
if sym.kind != .multi_return {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -2745,9 +2749,9 @@ fn (mut g Gen) map_fn_ptrs(key_typ table.TypeSymbol) (string, string, string, st
|
||||||
}
|
}
|
||||||
.voidptr {
|
.voidptr {
|
||||||
ts := if g.pref.m64 {
|
ts := if g.pref.m64 {
|
||||||
&g.table.types[table.u64_type_idx]
|
&g.table.type_symbols[table.u64_type_idx]
|
||||||
} else {
|
} else {
|
||||||
&g.table.types[table.u32_type_idx]
|
&g.table.type_symbols[table.u32_type_idx]
|
||||||
}
|
}
|
||||||
return g.map_fn_ptrs(ts)
|
return g.map_fn_ptrs(ts)
|
||||||
}
|
}
|
||||||
|
@ -5233,7 +5237,7 @@ fn (mut g Gen) write_builtin_types() {
|
||||||
// builtin types need to be on top
|
// builtin types need to be on top
|
||||||
// everything except builtin will get sorted
|
// everything except builtin will get sorted
|
||||||
for builtin_name in c.builtins {
|
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_ {
|
if sym.kind == .interface_ {
|
||||||
g.write_interface_typesymbol_declaration(sym)
|
g.write_interface_typesymbol_declaration(sym)
|
||||||
} else {
|
} else {
|
||||||
|
@ -5248,7 +5252,7 @@ fn (mut g Gen) write_builtin_types() {
|
||||||
// are added before them.
|
// are added before them.
|
||||||
fn (mut g Gen) write_sorted_types() {
|
fn (mut g Gen) write_sorted_types() {
|
||||||
mut types := []table.TypeSymbol{} // structs that need to be sorted
|
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 {
|
if typ.name !in c.builtins {
|
||||||
types << typ
|
types << typ
|
||||||
}
|
}
|
||||||
|
@ -5447,7 +5451,7 @@ fn (g &Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
|
||||||
// sort types
|
// sort types
|
||||||
mut types_sorted := []table.TypeSymbol{}
|
mut types_sorted := []table.TypeSymbol{}
|
||||||
for node in dep_graph_sorted.nodes {
|
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
|
return types_sorted
|
||||||
}
|
}
|
||||||
|
@ -6026,7 +6030,7 @@ fn (mut g Gen) is_expr(node ast.InfixExpr) {
|
||||||
// Generates interface table and interface indexes
|
// Generates interface table and interface indexes
|
||||||
fn (mut g Gen) interface_table() string {
|
fn (mut g Gen) interface_table() string {
|
||||||
mut sb := strings.new_builder(100)
|
mut sb := strings.new_builder(100)
|
||||||
for ityp in g.table.types {
|
for ityp in g.table.type_symbols {
|
||||||
if ityp.kind != .interface_ {
|
if ityp.kind != .interface_ {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue