commit cc7a7b79602d530c8362e0fc4c24cb118666f1d2
parent 58dd87d6a4baeb26b5ae14e54bc2f4e8c278d40a
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 3 Sep 2021 12:46:56 +0200
cmd/haretype: handle null & undefined sizes
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 31 insertions(+), 15 deletions(-)
diff --git a/cmd/haretype/main.ha b/cmd/haretype/main.ha
@@ -10,21 +10,33 @@ use os;
use strings;
export fn main() void = {
+ const store = types::store(types::x86_64, null, null);
+ defer types::store_free(store);
for (let i = 1z; i < len(os::args); i += 1) {
- const stream = bufio::fixed(
- strings::toutf8(os::args[i]),
- io::mode::READ);
- defer io::close(stream);
- const lexer = lex::init(stream, "-");
- const atype = parse::_type(&lexer)!;
- defer ast::type_free(atype);
- unparse::_type(os::stdout, 0, atype)!;
- fmt::println()!;
- const store = types::store(types::x86_64, null, null);
- defer types::store_free(store);
- const hatype = types::lookup(store, &atype)!;
+ const hatype = if (os::args[i] == "null") {
+ fmt::println("null")!;
+ yield types::lookup_builtin(store,
+ ast::builtin_type::NULL);
+ } else {
+ const stream = bufio::fixed(
+ strings::toutf8(os::args[i]),
+ io::mode::READ);
+ defer io::close(stream);
+ const lexer = lex::init(stream, "-");
+ const atype = parse::_type(&lexer)!;
+ defer ast::type_free(atype);
+ unparse::_type(os::stdout, 0, atype)!;
+ fmt::println()!;
+ yield types::lookup(store, &atype)!;
+ };
fmt::printfln("\tid: {}", hatype.id)!;
- fmt::printfln("\tsize: {}", hatype.sz)!;
- fmt::printfln("\talign: {}", hatype.align)!;
+ fmt::printfln("\tsize: {}",
+ if (hatype.sz == types::SIZE_UNDEFINED)
+ "undefined"
+ else hatype.sz)!;
+ fmt::printfln("\talign: {}",
+ if (hatype.align == types::SIZE_UNDEFINED)
+ "undefined"
+ else hatype.align)!;
};
};
diff --git a/hare/types/store.ha b/hare/types/store.ha
@@ -201,7 +201,11 @@ fn fromast(store: *typestore, atype: *ast::_type) (_type | deferred | error) = {
sz = 0; align = 0;
yield builtin::VOID;
},
- ast::builtin_type::NULL => builtin::NULL,
+ ast::builtin_type::NULL => {
+ sz = store.arch._pointer;
+ align = store.arch._pointer;
+ yield builtin::NULL;
+ },
ast::builtin_type::ICONST,
ast::builtin_type::FCONST => abort(), // TODO?
},