hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit d9358a46c585378ab30f5272a0b694ed5ceaad9a
parent 6d742c153fc816775636fa8b69864620a5ae16d7
Author: Sebastian <sebastian@sebsite.pw>
Date:   Fri, 18 Aug 2023 01:30:51 -0400

hare::*: add opaque

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/ast/type.ha | 2+-
Mhare/lex/token.ha | 2++
Mhare/parse/type.ha | 4+++-
Mhare/types/+test.ha | 8++++----
Mhare/types/builtins.ha | 8++++++++
Mhare/types/hash.ha | 8+++++---
Mhare/types/store.ha | 6++++++
Mhare/types/types.ha | 2+-
Mhare/unparse/type.ha | 2++
9 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/hare/ast/type.ha b/hare/ast/type.ha @@ -12,7 +12,7 @@ export type alias_type = struct { // A built-in primitive type (int, bool, str, etc). export type builtin_type = enum { - BOOL, F32, F64, FCONST, I16, I32, I64, I8, ICONST, INT, NULL, + BOOL, F32, F64, FCONST, I16, I32, I64, I8, ICONST, INT, NULL, OPAQUE, RUNE, SIZE, STR, U16, U32, U64, U8, UINT, UINTPTR, VALIST, VOID, }; diff --git a/hare/lex/token.ha b/hare/lex/token.ha @@ -56,6 +56,7 @@ export type ltok = enum uint { NULL, NULLABLE, OFFSET, + OPAQUE, RETURN, RUNE, SIZE, @@ -206,6 +207,7 @@ const bmap: [_]str = [ "null", "nullable", "offset", + "opaque", "return", "rune", "size", diff --git a/hare/parse/type.ha b/hare/parse/type.ha @@ -124,6 +124,8 @@ fn primitive_type(lexer: *lex::lexer) (ast::_type | error) = { yield builtin_type::VALIST; case ltok::VOID => yield builtin_type::VOID; + case ltok::OPAQUE => + yield builtin_type::OPAQUE; case => return syntaxerr(lex::mkloc(lexer), "Unexected {}, was expecting primitive type", @@ -480,7 +482,7 @@ export fn _type(lexer: *lex::lexer) (ast::_type | error) = { ltok::U8, ltok::U16, ltok::U32, ltok::U64, ltok::INT, ltok::UINT, ltok::UINTPTR, ltok::SIZE, ltok::F32, ltok::F64, - ltok::VALIST, ltok::VOID => + ltok::VALIST, ltok::VOID, ltok::OPAQUE => yield primitive_type(lexer)?; case ltok::ENUM => yield enum_type(lexer)?; diff --git a/hare/types/+test.ha b/hare/types/+test.ha @@ -321,8 +321,8 @@ fn resolve( assert(htype._align == st.arch._int); let t = htype.repr as tagged; assert(len(t) == 2); - assert(t[0].repr as builtin == builtin::INT); - assert(t[1].repr as builtin == builtin::VOID); + assert(t[0].repr as builtin == builtin::VOID); + assert(t[1].repr as builtin == builtin::INT); let atype = parse_type("(int | (int | str | void))"); defer ast::type_finish(atype); @@ -331,9 +331,9 @@ fn resolve( assert(htype._align == 8); let t = htype.repr as tagged; assert(len(t) == 3); - assert(t[0].repr as builtin == builtin::STR); + assert(t[0].repr as builtin == builtin::VOID); assert(t[1].repr as builtin == builtin::INT); - assert(t[2].repr as builtin == builtin::VOID); + assert(t[2].repr as builtin == builtin::STR); }; @test fn alias() void = { diff --git a/hare/types/builtins.ha b/hare/types/builtins.ha @@ -57,6 +57,13 @@ export const builtin_null: _type = _type { ... }; +// [[_type]] representation of opaque. +export const builtin_opaque: _type = _type { + repr = builtin::OPAQUE, + sz = SIZE_UNDEFINED, _align = SIZE_UNDEFINED, + ... +}; + // [[_type]] representation of rune. export const builtin_rune: _type = _type { repr = builtin::RUNE, @@ -109,6 +116,7 @@ export const builtin_void: _type = _type { &builtin_i32, &builtin_i64, &builtin_null, + &builtin_opaque, &builtin_rune, &builtin_u8, &builtin_u16, diff --git a/hare/types/hash.ha b/hare/types/hash.ha @@ -8,9 +8,9 @@ use strings; // Keep ordered with respect to bootstrap harec:include/types.h type storage = enum u8 { - BOOL, F32, F64, I16, I32, I64, I8, INT, NULL, RUNE, SIZE, STRING, - U16, U32, U64, U8, UINT, UINTPTR, VOID, ALIAS, ARRAY, ENUM, FUNCTION, - POINTER, SLICE, STRUCT, TAGGED, TUPLE, UNION, + BOOL, F32, F64, I16, I32, I64, I8, INT, NULL, OPAQUE, + RUNE, SIZE, STRING, U16, U32, U64, U8, UINT, UINTPTR, VOID, ALIAS, + ARRAY, ENUM, FUNCTION, POINTER, SLICE, STRUCT, TAGGED, TUPLE, UNION, }; fn builtin_storage(b: builtin) u8 = { @@ -33,6 +33,8 @@ fn builtin_storage(b: builtin) u8 = { return storage::INT; case builtin::NULL => return storage::NULL; + case builtin::OPAQUE => + return storage::OPAQUE; case builtin::RUNE => return storage::RUNE; case builtin::SIZE => diff --git a/hare/types/store.ha b/hare/types/store.ha @@ -138,6 +138,8 @@ export fn lookup( return &builtin_i32; case builtin::I64 => return &builtin_i64; + case builtin::OPAQUE => + return &builtin_opaque; case builtin::RUNE => return &builtin_rune; case builtin::U8 => @@ -252,6 +254,10 @@ fn fromast(store: *typestore, atype: *ast::_type) (_type | deferred | error) = { yield builtin::NULL; case ast::builtin_type::ICONST, ast::builtin_type::FCONST => abort(); // TODO? + case ast::builtin_type::OPAQUE => + sz = SIZE_UNDEFINED; + _align = SIZE_UNDEFINED; + yield builtin::OPAQUE; }; case let f: ast::func_type => yield func_from_ast(store, &f)?; diff --git a/hare/types/types.ha b/hare/types/types.ha @@ -22,7 +22,7 @@ export type array = struct { export type builtin = enum u8 { // Keep me consistent with ast::builtin - BOOL, F32, F64, FCONST, I16, I32, I64, I8, ICONST, INT, NULL, + BOOL, F32, F64, FCONST, I16, I32, I64, I8, ICONST, INT, NULL, OPAQUE, RUNE, SIZE, STR, U16, U32, U64, U8, UINT, UINTPTR, VALIST, VOID, }; diff --git a/hare/unparse/type.ha b/hare/unparse/type.ha @@ -32,6 +32,8 @@ case ast::builtin_type::INT => yield "int"; case ast::builtin_type::NULL => yield "null"; +case ast::builtin_type::OPAQUE => + yield "opaque"; case ast::builtin_type::RUNE => yield "rune"; case ast::builtin_type::SIZE =>