From fff11cdabd35869ad600aec94d794d9e24c5fdd8 Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Wed, 9 Mar 2022 20:40:28 +0100 Subject: [PATCH] WIP --- sources/kernel/syscalls.c | 4 +- sources/libs/bal/abi/helpers.c | 4 +- sources/libs/bal/abi/helpers.h | 4 +- sources/libs/brutal-base/slice.h | 28 +- sources/libs/brutal-debug/assert.h | 16 +- sources/libs/brutal-ds/vec.h | 4 + sources/libs/brutal-parse/lex.c | 14 + sources/libs/brutal-parse/lex.h | 2 + sources/libs/brutal-parse/nums.c | 12 +- sources/libs/brutal-parse/nums.h | 6 +- sources/libs/brutal-parse/scan.c | 28 ++ sources/libs/brutal-parse/scan.h | 6 + sources/libs/cc/ast/decl.h | 7 +- sources/libs/cc/ast/expr.c | 140 --------- sources/libs/cc/ast/expr.h | 15 +- sources/libs/cc/ast/ref.c | 15 - sources/libs/cc/ast/ref.h | 25 +- sources/libs/cc/ast/stmt.c | 25 -- sources/libs/cc/ast/stmt.h | 7 +- sources/libs/cc/ast/type.c | 32 -- sources/libs/cc/ast/type.h | 29 +- sources/libs/cc/ast/unit.c | 25 -- sources/libs/cc/ast/unit.h | 9 +- sources/libs/cc/ast/val.c | 14 - sources/libs/cc/ast/val.h | 28 +- sources/libs/cc/builder/expr.c | 141 +++++++++ sources/libs/cc/builder/expr.h | 12 + sources/libs/cc/builder/stmt.c | 26 ++ sources/libs/cc/builder/stmt.h | 4 + sources/libs/cc/builder/type.c | 128 +++++++- sources/libs/cc/builder/type.h | 30 +- sources/libs/cc/builder/unit.c | 26 ++ sources/libs/cc/builder/unit.h | 6 + sources/libs/cc/builder/val.c | 170 ++++++++++- sources/libs/cc/builder/val.h | 58 ++-- sources/libs/cc/dump/dump.c | 190 ++++++------ sources/libs/cc/lex/lexemes.h | 2 +- sources/libs/cc/lex/lexer.c | 70 ++++- sources/libs/cc/parse/cval.c | 45 ++- sources/libs/cc/parse/decl.c | 2 +- sources/libs/cc/parse/expr.c | 2 +- sources/libs/cc/parse/misc.c | 3 +- sources/libs/cc/parse/type.c | 133 +++++--- sources/libs/cc/sema/cast.c | 2 - sources/libs/cc/sema/cast.h | 5 - sources/libs/cc/sema/check/array_type.c | 23 -- sources/libs/cc/sema/check/array_type.h | 12 - sources/libs/cc/sema/check/call.c | 18 -- sources/libs/cc/sema/check/call.h | 12 - sources/libs/cc/sema/check/decl.c | 56 ---- sources/libs/cc/sema/check/decl.h | 21 -- sources/libs/cc/sema/check/deref_type.c | 22 -- sources/libs/cc/sema/check/deref_type.h | 11 - sources/libs/cc/sema/check/utils.c | 14 - sources/libs/cc/sema/check/utils.h | 7 - sources/libs/cc/sema/checks.c | 262 ---------------- sources/libs/cc/sema/checks.h | 48 --- sources/libs/cc/sema/checks_list.c | 29 -- sources/libs/cc/sema/passes.c | 131 ++++++++ sources/libs/cc/sema/passes.h | 3 + sources/libs/cc/sema/scope.c | 13 +- sources/libs/cc/sema/scope.h | 4 +- sources/libs/cc/sema/sema.c | 38 +-- sources/libs/cc/sema/sema.h | 5 +- sources/libs/cc/sema/visit.c | 385 +++++++++++------------- sources/libs/cc/sema/visit.h | 37 ++- sources/libs/cc/trans.h | 20 +- sources/libs/cc/trans/decl.c | 30 +- sources/libs/cc/trans/expr.c | 64 ++-- sources/libs/cc/trans/stmt.c | 74 ++--- sources/libs/cc/trans/type.c | 102 ++++--- sources/libs/cc/trans/unit.c | 22 +- sources/libs/cc/trans/val.c | 4 +- sources/libs/codec-ssfn2/ssfn2.c | 21 +- sources/libs/codec-ssfn2/ssfn2.h | 2 +- sources/libs/elf/elf.c | 4 +- sources/libs/elf/elf.h | 2 +- sources/libs/hw/fdt/fdt.c | 6 +- sources/libs/hw/fdt/fdt.h | 4 +- sources/libs/json/emit.c | 8 +- sources/libs/json/objects.c | 2 +- sources/libs/json/objects.h | 4 +- sources/libs/json/parser.c | 8 +- sources/libs/stdc-ansi/stdlib.h | 2 +- sources/loader/config.c | 8 +- sources/protos/window.idl | 7 + sources/utils/cc/main.c | 8 +- sources/utils/fontinfo/main.c | 6 +- sources/utils/idl/main.c | 4 +- sources/utils/test/json/json.c | 16 +- 90 files changed, 1562 insertions(+), 1571 deletions(-) delete mode 100644 sources/libs/cc/ast/expr.c delete mode 100644 sources/libs/cc/ast/ref.c delete mode 100644 sources/libs/cc/ast/stmt.c delete mode 100644 sources/libs/cc/ast/type.c delete mode 100644 sources/libs/cc/ast/unit.c delete mode 100644 sources/libs/cc/ast/val.c delete mode 100644 sources/libs/cc/sema/cast.c delete mode 100644 sources/libs/cc/sema/cast.h delete mode 100644 sources/libs/cc/sema/check/array_type.c delete mode 100644 sources/libs/cc/sema/check/array_type.h delete mode 100644 sources/libs/cc/sema/check/call.c delete mode 100644 sources/libs/cc/sema/check/call.h delete mode 100644 sources/libs/cc/sema/check/decl.c delete mode 100644 sources/libs/cc/sema/check/decl.h delete mode 100644 sources/libs/cc/sema/check/deref_type.c delete mode 100644 sources/libs/cc/sema/check/deref_type.h delete mode 100644 sources/libs/cc/sema/check/utils.c delete mode 100644 sources/libs/cc/sema/check/utils.h delete mode 100644 sources/libs/cc/sema/checks.c delete mode 100644 sources/libs/cc/sema/checks.h delete mode 100644 sources/libs/cc/sema/checks_list.c create mode 100644 sources/libs/cc/sema/passes.c create mode 100644 sources/libs/cc/sema/passes.h diff --git a/sources/kernel/syscalls.c b/sources/kernel/syscalls.c index 4447ead01..3b64dc05e 100644 --- a/sources/kernel/syscalls.c +++ b/sources/kernel/syscalls.c @@ -518,9 +518,9 @@ BrResult syscall_dispatch(BrSyscall syscall, BrArg args) { log$("Syscall: Task({}): {}({#p}) -> {}", task_self()->id, - str$(br_syscall_to_string(syscall)), + str$(br_syscall_to_str(syscall)), args, - str$(br_result_to_string(result))); + str$(br_result_to_str(result))); } task_end_syscall(); diff --git a/sources/libs/bal/abi/helpers.c b/sources/libs/bal/abi/helpers.c index dc51529ca..c803f7e90 100644 --- a/sources/libs/bal/abi/helpers.c +++ b/sources/libs/bal/abi/helpers.c @@ -1,7 +1,7 @@ #include #include -char const *br_syscall_to_string(BrSyscall syscall) +char const *br_syscall_to_str(BrSyscall syscall) { static char const *SYSCALL_NAMES[] = { #define ITER(SYSCALL) #SYSCALL, @@ -17,7 +17,7 @@ char const *br_syscall_to_string(BrSyscall syscall) return SYSCALL_NAMES[syscall]; } -char const *br_result_to_string(BrResult result) +char const *br_result_to_str(BrResult result) { static char const *RESULT_NAMES[] = { #define ITER(RESULT) #RESULT, diff --git a/sources/libs/bal/abi/helpers.h b/sources/libs/bal/abi/helpers.h index c811f024d..700d6b8e2 100644 --- a/sources/libs/bal/abi/helpers.h +++ b/sources/libs/bal/abi/helpers.h @@ -2,9 +2,9 @@ #include -char const *br_syscall_to_string(BrSyscall syscall); +char const *br_syscall_to_str(BrSyscall syscall); -char const *br_result_to_string(BrResult result); +char const *br_result_to_str(BrResult result); Error br_result_to_error(BrResult result); diff --git a/sources/libs/brutal-base/slice.h b/sources/libs/brutal-base/slice.h index 94fa721d0..ad6510ffc 100644 --- a/sources/libs/brutal-base/slice.h +++ b/sources/libs/brutal-base/slice.h @@ -4,32 +4,38 @@ typedef struct { + void const *buf; size_t len; + size_t size; - void const *buf; } SliceImpl; #define slice_impl$(SLICE) \ (SliceImpl) { .len = (SLICE).len, .size = sizeof(*(SLICE).buf), .buf = (SLICE).buf, } -#define InlineBuf(T) \ - struct \ - { \ - size_t len; \ - T buf[]; \ - } - #define Slice(T) \ struct \ { \ - size_t len; \ T const *buf; \ + size_t len; \ } typedef Slice(void) VoidSlice; #define slice$(type, buffer, size) \ - (type) { .len = size, .buf = buffer } + (type) { .buf = buffer, .len = size } #define slice_array$(type, buffer) \ - (type) { .len = sizeof(buffer) / sizeof(*buffer), .buf = &(buffer) } + (type) { .buf = (buffer), .len = sizeof(buffer) / sizeof(*buffer), } + +#define slice_begin$(SLICE) ((SLICE).buf) + +#define slice_end$(SLICE) ((SLICE).buf + (SLICE).len) + +#define slice_foreach$(VAR, SELF) \ + if ((SELF).len) \ + for (typeof((SELF).buf) VAR = slice_begin$(SELF); VAR != slice_end$(SELF); VAR++) + +#define slice_foreach_rev$(VAR, SELF) \ + if ((SELF).len) \ + for (typeof((SELF).buf) VAR = slice_end$(SELF) - 1; VAR >= slice_begin$(SELF); VAR--) diff --git a/sources/libs/brutal-debug/assert.h b/sources/libs/brutal-debug/assert.h index 2a9f1aca4..caaf95f6c 100644 --- a/sources/libs/brutal-debug/assert.h +++ b/sources/libs/brutal-debug/assert.h @@ -22,14 +22,14 @@ } \ }) -#define assert_br_success(EXPR) ( \ - { \ - BrResult __value = (EXPR); \ - \ - if (UNLIKELY(__value != BR_SUCCESS)) \ - { \ - panic$("{} is not equal to BR_SUCCESS (but is equal to: '{}') ", #EXPR, br_result_to_string(__value)); \ - } \ +#define assert_br_success(EXPR) ( \ + { \ + BrResult __value = (EXPR); \ + \ + if (UNLIKELY(__value != BR_SUCCESS)) \ + { \ + panic$("{} is not equal to BR_SUCCESS (but is equal to: '{}') ", #EXPR, br_result_to_str(__value)); \ + } \ }) #define assert_falsity(EXPR) ( \ diff --git a/sources/libs/brutal-ds/vec.h b/sources/libs/brutal-ds/vec.h index c97536bfa..9eb1886c0 100644 --- a/sources/libs/brutal-ds/vec.h +++ b/sources/libs/brutal-ds/vec.h @@ -150,6 +150,10 @@ void vec_swap_impl(VecImpl *impl, int idx1, int idx2); if ((SELF)->len) \ for (typeof((SELF)->data) VAR = vec_begin(SELF); VAR != vec_end(SELF); VAR++) +#define vec_foreach_rev(VAR, SELF) \ + if ((SELF)->len) \ + for (typeof((SELF)->data) VAR = vec_end(SELF) - 1; VAR >= vec_begin(SELF); VAR--) + #define vec_foreach_idx(IDX, VAR, SELF) \ if ((SELF)->len) \ for (int IDX = 0; IDX < (SELF)->len; IDX++) \ diff --git a/sources/libs/brutal-parse/lex.c b/sources/libs/brutal-parse/lex.c index 813ebccb5..a8d26c520 100644 --- a/sources/libs/brutal-parse/lex.c +++ b/sources/libs/brutal-parse/lex.c @@ -165,3 +165,17 @@ bool lex_ok(Lex *self) { return !self->has_error; } + +SrcRef clex_src_ref(Lex *lex, int begin, int end) +{ + Lexeme first = lex->lexemes.data[begin]; + Lexeme last = lex->lexemes.data[end]; + + SrcRef cref = { + .begin = first.pos.begin, + .end = last.pos.end, + .translation_unit = first.pos.translation_unit, + }; + + return cref; +} diff --git a/sources/libs/brutal-parse/lex.h b/sources/libs/brutal-parse/lex.h index a7df8e4e8..e5767b980 100644 --- a/sources/libs/brutal-parse/lex.h +++ b/sources/libs/brutal-parse/lex.h @@ -67,3 +67,5 @@ void lex_throw(Lex *self, Str message); bool lex_expect(Lex *self, LexemeType type); bool lex_ok(Lex *self); + +SrcRef lex_src_ref(Lex *lex, int begin, int end); diff --git a/sources/libs/brutal-parse/nums.c b/sources/libs/brutal-parse/nums.c index f9fca00ab..6e0ef83ba 100644 --- a/sources/libs/brutal-parse/nums.c +++ b/sources/libs/brutal-parse/nums.c @@ -146,26 +146,26 @@ bool scan_next_double(Scan *self, double *value) #endif -bool str_to_uint(Str string, unsigned long *value) +bool str_to_uint(Str str, unsigned long *value) { Scan scan = {0}; - scan_init(&scan, string); + scan_init(&scan, str); return scan_next_uint(&scan, value); } -bool str_to_int(Str string, long *value) +bool str_to_int(Str str, long *value) { Scan scan = {0}; - scan_init(&scan, string); + scan_init(&scan, str); return scan_next_int(&scan, value); } #ifndef __freestanding__ -bool str_to_float(Str string, double *value) +bool str_to_float(Str str, double *value) { Scan scan = {0}; - scan_init(&scan, string); + scan_init(&scan, str); return scan_next_double(&scan, value); } diff --git a/sources/libs/brutal-parse/nums.h b/sources/libs/brutal-parse/nums.h index b5e43f42d..bf23231ad 100644 --- a/sources/libs/brutal-parse/nums.h +++ b/sources/libs/brutal-parse/nums.h @@ -16,12 +16,12 @@ bool scan_next_double(Scan *self, double *value); #endif -bool str_to_uint(Str string, unsigned long *value); +bool str_to_uint(Str str, unsigned long *value); -bool str_to_int(Str string, long *value); +bool str_to_int(Str str, long *value); #ifndef __freestanding__ -bool str_to_float(Str string, double *value); +bool str_to_float(Str str, double *value); #endif diff --git a/sources/libs/brutal-parse/scan.c b/sources/libs/brutal-parse/scan.c index 5add8b84f..614908c5f 100644 --- a/sources/libs/brutal-parse/scan.c +++ b/sources/libs/brutal-parse/scan.c @@ -137,6 +137,20 @@ bool scan_skip_word(Scan *self, Str word) return true; } +bool scan_skip_word_nc(Scan *self, Str word) +{ + for (size_t i = 0; i < word.len; i++) + { + if (tolower(scan_peek(self, i)) != tolower(word.buf[i])) + { + return false; + } + } + + scan_next_n(self, word.len); + return true; +} + bool scan_skip_any(Scan *self, Str chars) { for (size_t i = 0; i < chars.len; i++) @@ -162,6 +176,20 @@ bool scan_skip_match(Scan *self, ScanMatch *match) return true; } +bool scan_skip_any_nc(Scan *self, Str chars) +{ + for (size_t i = 0; i < chars.len; i++) + { + if (tolower(scan_curr(self)) == tolower(chars.buf[i])) + { + scan_next(self); + return true; + } + } + + return false; +} + bool scan_eat(Scan *self, ScanMatch *match) { bool result = false; diff --git a/sources/libs/brutal-parse/scan.h b/sources/libs/brutal-parse/scan.h index 58c3934bf..14c56c748 100644 --- a/sources/libs/brutal-parse/scan.h +++ b/sources/libs/brutal-parse/scan.h @@ -103,10 +103,16 @@ bool scan_skip(Scan *self, char c); bool scan_skip_word(Scan *self, Str word); +bool scan_skip_word_nc(Scan *self, Str word); + bool scan_skip_any(Scan *self, Str chars); bool scan_skip_match(Scan *self, ScanMatch *match); +bool scan_skip_any_nc(Scan *self, Str chars); + +Str scan_skip_until(Scan *self, ScanMatch *match); + bool scan_skip_space(Scan *self); bool scan_eat(Scan *self, ScanMatch *match); diff --git a/sources/libs/cc/ast/decl.h b/sources/libs/cc/ast/decl.h index a643a9c50..2fe785bf9 100644 --- a/sources/libs/cc/ast/decl.h +++ b/sources/libs/cc/ast/decl.h @@ -7,9 +7,8 @@ typedef enum { - CDECL_NIL, - CDECL_EMPTY, + CDECL_TYPE, CDECL_VAR, CDECL_FUNC, @@ -34,12 +33,12 @@ typedef struct typedef struct _CDecl { - CRef ref; + SrcRef ref; CDeclType type; CDeclAttr attr; Str name; CType sema_type; - bool global; + union { struct diff --git a/sources/libs/cc/ast/expr.c b/sources/libs/cc/ast/expr.c deleted file mode 100644 index 7ca762554..000000000 --- a/sources/libs/cc/ast/expr.c +++ /dev/null @@ -1,140 +0,0 @@ -#include - -static const char *cop_type_to_str[COP_COUNT] = { - [COP_INVALID] = "invalid", - [COP_INC] = "++", - [COP_DEC] = "--", - [COP_ASSIGN] = "=", - [COP_ASSIGN_ADD] = "+=", - [COP_ASSIGN_SUB] = "-=", - [COP_ASSIGN_MULT] = "*=", - [COP_ASSIGN_DIV] = "/=", - [COP_ASSIGN_MOD] = "%=", - [COP_ASSIGN_BIT_AND] = "&=", - [COP_ASSIGN_BIT_OR] = "|=", - [COP_ASSIGN_BIT_XOR] = "^=", - [COP_ASSIGN_LSHIFT] = "<<=", - [COP_ASSIGN_RSHIFT] = ">>=", - [COP_ADD] = "+", - [COP_SUB] = "-", - [COP_MULT] = "*", - [COP_DIV] = "/", - [COP_MOD] = "%", - [COP_BIT_NOT] = "~", - [COP_BIT_AND] = "&", - [COP_BIT_OR] = "|", - [COP_BIT_XOR] = "^", - [COP_LSHIFT] = "<<", - [COP_RSHIFT] = ">>", - [COP_NOT] = "!", - [COP_AND] = "&&", - [COP_OR] = "||", - [COP_EQ] = "==", - [COP_NOT_EQ] = "!=", - [COP_LT] = "<", - [COP_GT] = ">", - [COP_LT_EQ] = "<=", - [COP_GT_EQ] = ">=", - [COP_INDEX] = "[", - [COP_DEREF] = "*", - [COP_REF] = "&", - [COP_ACCESS] = ".", - [COP_PTR_ACCESS] = "->", -}; - -Str cop_to_str(COp type) -{ - return str$(cop_type_to_str[type]); -} - -static const char *cexpr_type_to_str_raw[CEXPR_COUNT] = { - [CEXPR_EMPTY] = "empty", - [CEXPR_SELF] = "self", - [CEXPR_CONSTANT] = "constant", - [CEXPR_IDENT] = "identifier", - [CEXPR_PREFIX] = "prefix", - [CEXPR_INFIX] = "infix", - [CEXPR_POSTFIX] = "postfix", - [CEXPR_CALL] = "call", - [CEXPR_CAST] = "cast", - [CEXPR_TERNARY] = "ternary", - [CEXPR_INITIALIZER] = "initializer", - [CEXPR_LAMBDA] = "lambda", -}; - -Str cexpr_type_to_str(CExprType type) -{ - return str$(cexpr_type_to_str_raw[type]); -} - -static int cop_precedence_table[COP_COUNT] = { - [COP_INC] = 1, - [COP_DEC] = 1, - [COP_ASSIGN] = 14, - [COP_ASSIGN_ADD] = 14, - [COP_ASSIGN_SUB] = 14, - [COP_ASSIGN_MULT] = 14, - [COP_ASSIGN_DIV] = 14, - [COP_ASSIGN_MOD] = 14, - [COP_ASSIGN_BIT_AND] = 14, - [COP_ASSIGN_BIT_OR] = 14, - [COP_ASSIGN_BIT_XOR] = 14, - [COP_ASSIGN_LSHIFT] = 14, - [COP_ASSIGN_RSHIFT] = 14, - [COP_ADD] = 2, - [COP_SUB] = 2, - [COP_MULT] = 3, - [COP_DIV] = 3, - [COP_MOD] = 3, - [COP_BIT_NOT] = 2, - [COP_BIT_AND] = 8, - [COP_BIT_OR] = 10, - [COP_BIT_XOR] = 9, - [COP_LSHIFT] = 5, - [COP_RSHIFT] = 5, - [COP_NOT] = 2, - [COP_AND] = 12, - [COP_OR] = 11, - [COP_EQ] = 7, - [COP_NOT_EQ] = 7, - [COP_LT] = 6, - [COP_GT] = 6, - [COP_LT_EQ] = 6, - [COP_GT_EQ] = 6, - [COP_INDEX] = 1, - [COP_DEREF] = 2, - [COP_REF] = 2, - [COP_ACCESS] = 1, - [COP_PTR_ACCESS] = 1, -}; - -int cop_pre(COp cop) -{ - return cop_precedence_table[cop]; -} - -int cexpr_pre(CExpr *expr) -{ - switch (expr->type) - { - case CEXPR_PREFIX: - case CEXPR_POSTFIX: - case CEXPR_INFIX: - return cop_pre(expr->infix_.op); - - case CEXPR_IDENT: - case CEXPR_CONSTANT: - return 0; - - case CEXPR_CALL: - case CEXPR_CAST: - case CEXPR_INITIALIZER: - return 1; - - case CEXPR_TERNARY: - return 13; - - default: - return 0; - } -} diff --git a/sources/libs/cc/ast/expr.h b/sources/libs/cc/ast/expr.h index f9bc375ce..5276c10b1 100644 --- a/sources/libs/cc/ast/expr.h +++ b/sources/libs/cc/ast/expr.h @@ -97,12 +97,13 @@ typedef struct struct _CExpr { - CRef ref; + SrcRef ref; CExprType type; CType sema_type; + union { - CVal constant_; /* instead of Cval holding sema_type the cexpr holds it */ + CVal constant_; Str ident_; struct @@ -150,13 +151,3 @@ struct _CExpr } lambda_; }; }; - -COp str_to_cop(Str str); - -Str cop_to_str(COp type); - -Str cexpr_type_to_str(CExprType type); - -int cop_pre(COp cop); - -int cexpr_pre(CExpr *expr); diff --git a/sources/libs/cc/ast/ref.c b/sources/libs/cc/ast/ref.c deleted file mode 100644 index e536bc3d1..000000000 --- a/sources/libs/cc/ast/ref.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -CRef cref(Lex *lex, int begin, int end) -{ - - Lexeme first = lex->lexemes.data[begin]; - Lexeme last = lex->lexemes.data[end]; - CRef cref = { - .begin = first.pos.begin, - .end = last.pos.end, - .translation_unit = first.pos.translation_unit, - }; - - return cref; -} diff --git a/sources/libs/cc/ast/ref.h b/sources/libs/cc/ast/ref.h index aecf754fb..6ddbb38f1 100644 --- a/sources/libs/cc/ast/ref.h +++ b/sources/libs/cc/ast/ref.h @@ -4,35 +4,24 @@ #include #include -typedef SrcRef CRef; - -CRef cref(Lex *lex, int begin, int end); - /** - * @brief Change element to have a cref with begin = begin and end = lex.head + * @brief Change element to have a SrcRef with begin = begin and end = lex.head * It is a macro because it is more simple to do something like this: * ```c * return with_ref$(if_stmt, begin, lex); * ``` * than this: * ```c - * if_stmt.cref = cref(if_stmt, begin, lex); + * if_stmt.ref = lex_src_ref(if_stmt, begin, lex); * return if_stmt; * ``` * @param element The ast element to change. * @param begin Begin index of the ast element. * @param lex The lexer at the end of the element. */ -#define with_cref$(element, begin, lex) \ - ({ \ - typeof(element) _with_ref = (element); \ - if (begin == (lex)->head) \ - { \ - _with_ref.ref = cref((lex), 0, 0); \ - } \ - else \ - { \ - _with_ref.ref = cref((lex), (begin), (lex)->head - 1); \ - } \ - _with_ref; \ +#define with_cref$(element, begin, lex) \ + ({ \ + typeof(element) __element = (element); \ + __element.ref = lex_src_ref((lex), (begin), (lex)->head); \ + __element; \ }) diff --git a/sources/libs/cc/ast/stmt.c b/sources/libs/cc/ast/stmt.c deleted file mode 100644 index e56f93ee7..000000000 --- a/sources/libs/cc/ast/stmt.c +++ /dev/null @@ -1,25 +0,0 @@ -#include - -char const *cstmt_type_to_str_raw[] = { - [CSTMT_EMPTY] = "empty", - [CSTMT_DECL] = "declaration", - [CSTMT_EXPR] = "expression", - [CSTMT_BLOCK] = "block", - [CSTMT_IF] = "if", - [CSTMT_FOR] = "for", - [CSTMT_WHILE] = "while", - [CSTMT_DO] = "do", - [CSTMT_SWITCH] = "switch", - [CSTMT_RETURN] = "return", - [CSTMT_GOTO] = "goto", - [CSTMT_BREAK] = "break", - [CSTMT_CONTINUE] = "continue", - [CSTMT_LABEL] = "label", - [CSTMT_CASE] = "case", - [CSTMT_DEFAULT] = "default", -}; - -Str cstmt_type_to_str(CStmtType type) -{ - return str$(cstmt_type_to_str_raw[type]); -} diff --git a/sources/libs/cc/ast/stmt.h b/sources/libs/cc/ast/stmt.h index 423d23fb3..52e9044b9 100644 --- a/sources/libs/cc/ast/stmt.h +++ b/sources/libs/cc/ast/stmt.h @@ -1,8 +1,9 @@ #pragma once -#include #include #include +#include + typedef enum { CSTMT_INVALID, @@ -33,7 +34,7 @@ typedef struct _CStmt CStmt; struct _CStmt { - CRef ref; + SrcRef ref; CStmtType type; union @@ -96,5 +97,3 @@ struct _CStmt } case_; }; }; - -Str cstmt_type_to_str(CStmtType type); diff --git a/sources/libs/cc/ast/type.c b/sources/libs/cc/ast/type.c deleted file mode 100644 index 3ab1aa416..000000000 --- a/sources/libs/cc/ast/type.c +++ /dev/null @@ -1,32 +0,0 @@ -#include - -const char *ctype_type_to_str[CTYPE_COUNT] = { - [CTYPE_INVALID] = "invalid", - - [CTYPE_ERROR] = "error", - [CTYPE_TAIL] = "tail", - - [CTYPE_VOID] = "void", - [CTYPE_AUTO] = "auto", - [CTYPE_BOOL] = "bool", - [CTYPE_PTR] = "", - [CTYPE_ARRAY] = "", - [CTYPE_SIGNED] = "signed", - [CTYPE_UNSIGNED] = "unsigned", - [CTYPE_FLOAT] = "float", - [CTYPE_STRUCT] = "struct", - [CTYPE_UNION] = "union", - [CTYPE_ENUM] = "enum", -}; - -Str ctype_to_str(CTypeType type) -{ - if (ctype_type_to_str[type]) - { - return str$(ctype_type_to_str[type]); - } - else - { - return str$(""); - } -} diff --git a/sources/libs/cc/ast/type.h b/sources/libs/cc/ast/type.h index 4fa4e4599..a85fb753e 100644 --- a/sources/libs/cc/ast/type.h +++ b/sources/libs/cc/ast/type.h @@ -8,7 +8,6 @@ typedef enum CTYPE_INVALID, CTYPE_TAIL, - CTYPE_ERROR, CTYPE_VOID, CTYPE_AUTO, @@ -17,9 +16,22 @@ typedef enum CTYPE_REF, CTYPE_PARENT, CTYPE_ARRAY, - CTYPE_SIGNED, - CTYPE_UNSIGNED, + + CTYPE_CHAR, + CTYPE_SHORT, + CTYPE_INT, + CTYPE_LONG, + CTYPE_LONGLONG, + + CTYPE_UCHAR, + CTYPE_USHORT, + CTYPE_UINT, + CTYPE_ULONG, + CTYPE_ULONGLONG, + CTYPE_FLOAT, + CTYPE_DOUBLE, + CTYPE_STRUCT, CTYPE_UNION, CTYPE_ENUM, @@ -45,18 +57,13 @@ typedef Vec(CTypeMember) CTypeMembers; struct _CType { - CRef ref; + SrcRef ref; CTypeType type; CTypeAttr attr; Str name; union { - struct - { - int precision; - } signed_, unsigned_, float_; - struct { CType *subtype; @@ -97,7 +104,3 @@ struct _CTypeConst Str name; CVal value; }; - -typedef Slice(CType) CTypeSlice; - -Str ctype_to_str(CTypeType type); diff --git a/sources/libs/cc/ast/unit.c b/sources/libs/cc/ast/unit.c deleted file mode 100644 index 1d29b34ae..000000000 --- a/sources/libs/cc/ast/unit.c +++ /dev/null @@ -1,25 +0,0 @@ -#include - -bool cunit_contains_type(CUnit *context, Str name) -{ - vec_foreach_v(entry, &context->units) - { - if (entry.type == CUNIT_DECLARATION && entry._decl.type == CDECL_TYPE && str_eq(entry._decl.name, name)) - { - return true; - } - } - return false; -} - -bool cunit_contains_decl(CUnit *context, Str name) -{ - vec_foreach_v(entry, &context->units) - { - if (entry.type == CUNIT_DECLARATION && entry._decl.type != CDECL_TYPE && str_eq(entry._decl.name, name)) - { - return true; - } - } - return false; -} diff --git a/sources/libs/cc/ast/unit.h b/sources/libs/cc/ast/unit.h index 3d2aa7683..1cfa6f321 100644 --- a/sources/libs/cc/ast/unit.h +++ b/sources/libs/cc/ast/unit.h @@ -22,7 +22,8 @@ typedef struct typedef enum { - CUNIT_NONE, + CUNIT_INVALID, + CUNIT_INCLUDE, CUNIT_PRAGMA, CUNIT_DECLARATION, @@ -31,7 +32,7 @@ typedef enum typedef struct { - CRef ref; + SrcRef ref; CUnitType type; union @@ -47,7 +48,3 @@ typedef struct { Vec(CUnitEntry) units; } CUnit; - -bool cunit_contains_type(CUnit *context, Str name); - -bool cunit_contains_decl(CUnit *context, Str name); diff --git a/sources/libs/cc/ast/val.c b/sources/libs/cc/ast/val.c deleted file mode 100644 index b61c6c53b..000000000 --- a/sources/libs/cc/ast/val.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -const char *cval_type_to_str_raw[CVAL_COUNT] = - { - [CVAL_SIGNED] = "signed", - [CVAL_UNSIGNED] = "unsigned", - [CVAL_FLOAT] = "float", - [CVAL_STRING] = "string", -}; - -Str cval_type_to_str(CValType type) -{ - return str$(cval_type_to_str_raw[type]); -} diff --git a/sources/libs/cc/ast/val.h b/sources/libs/cc/ast/val.h index 3a33bc713..16046e927 100644 --- a/sources/libs/cc/ast/val.h +++ b/sources/libs/cc/ast/val.h @@ -3,23 +3,35 @@ #include #include -typedef int CSigned; -typedef unsigned int CUnsigned; +typedef int64_t CSigned; +typedef uint64_t CUnsigned; typedef enum { CVAL_INVALID, - CVAL_SIGNED, - CVAL_UNSIGNED, + CVAL_CHAR, + CVAL_SHORT, + CVAL_INT, + CVAL_LONG, + CVAL_LONGLONG, + + CVAL_UCHAR, + CVAL_USHORT, + CVAL_UINT, + CVAL_ULONG, + CVAL_ULONGLONG, + CVAL_FLOAT, + CVAL_DOUBLE, + CVAL_STRING, CVAL_COUNT, } CValType; -typedef struct +typedef struct _CVal { - CRef ref; + SrcRef ref; CValType type; union @@ -27,8 +39,6 @@ typedef struct CSigned signed_; CUnsigned unsigned_; double float_; - Str string_; + Str str_; }; } CVal; - -Str cval_type_to_str(CValType type); diff --git a/sources/libs/cc/builder/expr.c b/sources/libs/cc/builder/expr.c index bc2c422a7..d3547d05a 100644 --- a/sources/libs/cc/builder/expr.c +++ b/sources/libs/cc/builder/expr.c @@ -1,6 +1,147 @@ #include #include +static const char *cop_type_to_str[COP_COUNT] = { + [COP_INVALID] = "invalid", + [COP_INC] = "++", + [COP_DEC] = "--", + [COP_ASSIGN] = "=", + [COP_ASSIGN_ADD] = "+=", + [COP_ASSIGN_SUB] = "-=", + [COP_ASSIGN_MULT] = "*=", + [COP_ASSIGN_DIV] = "/=", + [COP_ASSIGN_MOD] = "%=", + [COP_ASSIGN_BIT_AND] = "&=", + [COP_ASSIGN_BIT_OR] = "|=", + [COP_ASSIGN_BIT_XOR] = "^=", + [COP_ASSIGN_LSHIFT] = "<<=", + [COP_ASSIGN_RSHIFT] = ">>=", + [COP_ADD] = "+", + [COP_SUB] = "-", + [COP_MULT] = "*", + [COP_DIV] = "/", + [COP_MOD] = "%", + [COP_BIT_NOT] = "~", + [COP_BIT_AND] = "&", + [COP_BIT_OR] = "|", + [COP_BIT_XOR] = "^", + [COP_LSHIFT] = "<<", + [COP_RSHIFT] = ">>", + [COP_NOT] = "!", + [COP_AND] = "&&", + [COP_OR] = "||", + [COP_EQ] = "==", + [COP_NOT_EQ] = "!=", + [COP_LT] = "<", + [COP_GT] = ">", + [COP_LT_EQ] = "<=", + [COP_GT_EQ] = ">=", + [COP_INDEX] = "[", + [COP_DEREF] = "*", + [COP_REF] = "&", + [COP_ACCESS] = ".", + [COP_PTR_ACCESS] = "->", +}; + +Str cop_to_str(COp type) +{ + return str$(cop_type_to_str[type]); +} + +static const char *cexpr_to_str_raw[CEXPR_COUNT] = { + [CEXPR_EMPTY] = "empty", + [CEXPR_SELF] = "self", + [CEXPR_CONSTANT] = "constant", + [CEXPR_IDENT] = "identifier", + [CEXPR_PREFIX] = "prefix", + [CEXPR_INFIX] = "infix", + [CEXPR_POSTFIX] = "postfix", + [CEXPR_CALL] = "call", + [CEXPR_CAST] = "cast", + [CEXPR_TERNARY] = "ternary", + [CEXPR_INITIALIZER] = "initializer", + [CEXPR_LAMBDA] = "lambda", +}; + +Str cexpr_to_str(CExprType type) +{ + return str$(cexpr_to_str_raw[type]); +} + +static int cop_precedence_table[COP_COUNT] = { + [COP_INC] = 1, + [COP_DEC] = 1, + [COP_ASSIGN] = 14, + [COP_ASSIGN_ADD] = 14, + [COP_ASSIGN_SUB] = 14, + [COP_ASSIGN_MULT] = 14, + [COP_ASSIGN_DIV] = 14, + [COP_ASSIGN_MOD] = 14, + [COP_ASSIGN_BIT_AND] = 14, + [COP_ASSIGN_BIT_OR] = 14, + [COP_ASSIGN_BIT_XOR] = 14, + [COP_ASSIGN_LSHIFT] = 14, + [COP_ASSIGN_RSHIFT] = 14, + [COP_ADD] = 2, + [COP_SUB] = 2, + [COP_MULT] = 3, + [COP_DIV] = 3, + [COP_MOD] = 3, + [COP_BIT_NOT] = 2, + [COP_BIT_AND] = 8, + [COP_BIT_OR] = 10, + [COP_BIT_XOR] = 9, + [COP_LSHIFT] = 5, + [COP_RSHIFT] = 5, + [COP_NOT] = 2, + [COP_AND] = 12, + [COP_OR] = 11, + [COP_EQ] = 7, + [COP_NOT_EQ] = 7, + [COP_LT] = 6, + [COP_GT] = 6, + [COP_LT_EQ] = 6, + [COP_GT_EQ] = 6, + [COP_INDEX] = 1, + [COP_DEREF] = 2, + [COP_REF] = 2, + [COP_ACCESS] = 1, + [COP_PTR_ACCESS] = 1, +}; + +int cop_pre(COp cop) +{ + return cop_precedence_table[cop]; +} + +int cexpr_pre(CExpr *expr) +{ + switch (expr->type) + { + case CEXPR_PREFIX: + case CEXPR_POSTFIX: + case CEXPR_INFIX: + return cop_pre(expr->infix_.op); + + case CEXPR_IDENT: + case CEXPR_CONSTANT: + return 0; + + case CEXPR_CALL: + case CEXPR_CAST: + case CEXPR_INITIALIZER: + return 1; + + case CEXPR_TERNARY: + return 13; + + default: + return 0; + } +} + +/* --- Builder -------------------------------------------------------------- */ + CExpr cexpr_empty(void) { return (CExpr){ diff --git a/sources/libs/cc/builder/expr.h b/sources/libs/cc/builder/expr.h index a1fd69948..55129aba0 100644 --- a/sources/libs/cc/builder/expr.h +++ b/sources/libs/cc/builder/expr.h @@ -3,6 +3,18 @@ #include #include +COp str_to_cop(Str str); + +Str cop_to_str(COp type); + +Str cexpr_to_str(CExprType type); + +int cop_pre(COp cop); + +int cexpr_pre(CExpr *expr); + +/* --- Builder -------------------------------------------------------------- */ + CExpr cexpr_infix(CExpr left, COp type, CExpr right, Alloc *alloc); CExpr cexpr_empty(void); diff --git a/sources/libs/cc/builder/stmt.c b/sources/libs/cc/builder/stmt.c index c3f32271d..42f1cad20 100644 --- a/sources/libs/cc/builder/stmt.c +++ b/sources/libs/cc/builder/stmt.c @@ -1,6 +1,32 @@ #include #include +char const *cstmt_to_str_raw[] = { + [CSTMT_EMPTY] = "empty", + [CSTMT_DECL] = "declaration", + [CSTMT_EXPR] = "expression", + [CSTMT_BLOCK] = "block", + [CSTMT_IF] = "if", + [CSTMT_FOR] = "for", + [CSTMT_WHILE] = "while", + [CSTMT_DO] = "do", + [CSTMT_SWITCH] = "switch", + [CSTMT_RETURN] = "return", + [CSTMT_GOTO] = "goto", + [CSTMT_BREAK] = "break", + [CSTMT_CONTINUE] = "continue", + [CSTMT_LABEL] = "label", + [CSTMT_CASE] = "case", + [CSTMT_DEFAULT] = "default", +}; + +Str cstmt_to_str(CStmtType type) +{ + return str$(cstmt_to_str_raw[type]); +} + +/* --- Builder -------------------------------------------------------------- */ + CStmt cstmt_empty(void) { return (CStmt){ diff --git a/sources/libs/cc/builder/stmt.h b/sources/libs/cc/builder/stmt.h index f9b722b05..4ab556e9b 100644 --- a/sources/libs/cc/builder/stmt.h +++ b/sources/libs/cc/builder/stmt.h @@ -3,6 +3,10 @@ #include #include +Str cstmt_to_str(CStmtType type); + +/* --- Types --------------------------------------------------------------- */ + CStmt cstmt_empty(void); CStmt cstmt_decl(CDecl decl, Alloc *alloc); diff --git a/sources/libs/cc/builder/type.c b/sources/libs/cc/builder/type.c index f4bfca29c..de2a926af 100644 --- a/sources/libs/cc/builder/type.c +++ b/sources/libs/cc/builder/type.c @@ -1,13 +1,55 @@ #include #include -CType ctype_error(void) +const char *ctype_type_to_str[CTYPE_COUNT] = { + [CTYPE_INVALID] = "", + [CTYPE_TAIL] = "", + + [CTYPE_VOID] = "void", + [CTYPE_AUTO] = "auto", + [CTYPE_BOOL] = "bool", + [CTYPE_PTR] = "", + [CTYPE_ARRAY] = "", + + [CTYPE_CHAR] = "char", + [CTYPE_SHORT] = "short", + [CTYPE_INT] = "int", + [CTYPE_LONG] = "long", + [CTYPE_LONGLONG] = "long long", + [CTYPE_UCHAR] = "unsigned char", + [CTYPE_USHORT] = "unsigned short", + [CTYPE_UINT] = "unsigned int", + [CTYPE_ULONG] = "unsigned long", + [CTYPE_ULONGLONG] = "unsigned long long", + [CTYPE_FLOAT] = "float", + [CTYPE_DOUBLE] = "double", + + [CTYPE_STRUCT] = "struct", + [CTYPE_UNION] = "union", + [CTYPE_ENUM] = "enum", +}; + +Str ctype_to_str(CType type) { - return (CType){ - .type = CTYPE_ERROR, - }; + if (ctype_type_to_str[type.type]) + { + return str$(ctype_type_to_str[type.type]); + } + else + { + return str$(""); + } } +CType ctype_to_unsigned(CType type) +{ + assert_truth(type.type >= CTYPE_CHAR && type.type <= CTYPE_ULONGLONG); + type.type = type.type + CTYPE_UCHAR - CTYPE_CHAR; + return type; +} + +/* --- Builder -------------------------------------------------------------- */ + CType ctype_tail(void) { return (CType){ @@ -63,27 +105,87 @@ CType ctype_array(CType subtype, int size, Alloc *alloc) }; } -CType ctype_signed(int precision) +CType ctype_char(void) { return (CType){ - .type = CTYPE_SIGNED, - .signed_.precision = precision, + .type = CTYPE_CHAR, }; } -CType ctype_unsigned(int precision) +CType ctype_short(void) { return (CType){ - .type = CTYPE_UNSIGNED, - .unsigned_.precision = precision, + .type = CTYPE_SHORT, }; } -CType ctype_float(int precision) +CType ctype_int(void) +{ + return (CType){ + .type = CTYPE_INT, + }; +} + +CType ctype_long(void) +{ + return (CType){ + .type = CTYPE_LONG, + }; +} + +CType ctype_longlong(void) +{ + return (CType){ + .type = CTYPE_LONGLONG, + }; +} + +CType ctype_uchar(void) +{ + return (CType){ + .type = CTYPE_UCHAR, + }; +} + +CType ctype_ushort(void) +{ + return (CType){ + .type = CTYPE_USHORT, + }; +} + +CType ctype_uint(void) +{ + return (CType){ + .type = CTYPE_UINT, + }; +} + +CType ctype_ulong(void) +{ + return (CType){ + .type = CTYPE_ULONG, + }; +} + +CType ctype_ulonglong(void) +{ + return (CType){ + .type = CTYPE_ULONGLONG, + }; +} + +CType ctype_float(void) { return (CType){ .type = CTYPE_FLOAT, - .float_.precision = precision, + }; +} + +CType ctype_double(void) +{ + return (CType){ + .type = CTYPE_DOUBLE, }; } @@ -217,6 +319,6 @@ void ctype_append(CType *type, CType tail) break; default: - panic$("ctype_append on type {}", ctype_to_str(type->type)); + panic$("ctype_append on type {}", ctype_to_str(*type)); } } diff --git a/sources/libs/cc/builder/type.h b/sources/libs/cc/builder/type.h index c7a003d81..8d0f0b930 100644 --- a/sources/libs/cc/builder/type.h +++ b/sources/libs/cc/builder/type.h @@ -2,6 +2,12 @@ #include +Str ctype_to_str(CType type); + +CType ctype_to_unsigned(CType type); + +/* --- Builders ------------------------------------------------------------- */ + CType ctype_error(void); CType ctype_tail(void); @@ -18,11 +24,29 @@ CType ctype_parent(CType subtype, Alloc *alloc); CType ctype_array(CType subtype, int size, Alloc *alloc); -CType ctype_signed(int precision); +CType ctype_char(void); + +CType ctype_short(void); + +CType ctype_int(void); + +CType ctype_long(void); + +CType ctype_longlong(void); + +CType ctype_uchar(void); + +CType ctype_ushort(void); + +CType ctype_uint(void); + +CType ctype_ulong(void); + +CType ctype_ulonglong(void); -CType ctype_unsigned(int precision); +CType ctype_float(void); -CType ctype_float(int precision); +CType ctype_double(void); CType ctype_struct(Alloc *alloc); diff --git a/sources/libs/cc/builder/unit.c b/sources/libs/cc/builder/unit.c index 6ea914a7e..65fac9736 100644 --- a/sources/libs/cc/builder/unit.c +++ b/sources/libs/cc/builder/unit.c @@ -1,5 +1,31 @@ #include +bool cunit_contains_type(CUnit *context, Str name) +{ + vec_foreach_v(entry, &context->units) + { + if (entry.type == CUNIT_DECLARATION && entry._decl.type == CDECL_TYPE && str_eq(entry._decl.name, name)) + { + return true; + } + } + return false; +} + +bool cunit_contains_decl(CUnit *context, Str name) +{ + vec_foreach_v(entry, &context->units) + { + if (entry.type == CUNIT_DECLARATION && entry._decl.type != CDECL_TYPE && str_eq(entry._decl.name, name)) + { + return true; + } + } + return false; +} + +/* --- Builders ------------------------------------------------------------- */ + CUnit cunit(Alloc *alloc) { CUnit unit; diff --git a/sources/libs/cc/builder/unit.h b/sources/libs/cc/builder/unit.h index 8ce351f1b..a48e6e2d3 100644 --- a/sources/libs/cc/builder/unit.h +++ b/sources/libs/cc/builder/unit.h @@ -2,6 +2,12 @@ #include +bool cunit_contains_type(CUnit *context, Str name); + +bool cunit_contains_decl(CUnit *context, Str name); + +/* --- Builders ------------------------------------------------------------- */ + CUnit cunit(Alloc *alloc); void cunit_decl(CUnit *self, CDecl decl); diff --git a/sources/libs/cc/builder/val.c b/sources/libs/cc/builder/val.c index a5f8ea463..0512adcf0 100644 --- a/sources/libs/cc/builder/val.c +++ b/sources/libs/cc/builder/val.c @@ -1,33 +1,183 @@ +#include +#include #include -CVal cval_signed(CSigned v) +static const char *cval_type_to_str_raw[CVAL_COUNT] = { + [CVAL_INVALID] = "invalid", + [CVAL_CHAR] = "char", + [CVAL_SHORT] = "short", + [CVAL_INT] = "int", + [CVAL_LONG] = "long", + [CVAL_LONGLONG] = "long long", + [CVAL_UCHAR] = "unsigned char", + [CVAL_USHORT] = "unsigned short", + [CVAL_UINT] = "unsigned int", + [CVAL_ULONG] = "unsigned long", + [CVAL_ULONGLONG] = "unsigned long long", + [CVAL_FLOAT] = "float", + [CVAL_DOUBLE] = "double", + [CVAL_STRING] = "string", +}; + +Str cval_to_str(CVal val) +{ + return str$(cval_type_to_str_raw[val.type]); +} + +CType cval_type(CVal value, Alloc *alloc) +{ + switch (value.type) + { + case CVAL_FLOAT: + return ctype_float(); + + case CVAL_CHAR: + return ctype_char(); + + case CVAL_SHORT: + return ctype_short(); + + case CVAL_INT: + return ctype_int(); + + case CVAL_LONG: + return ctype_long(); + + case CVAL_LONGLONG: + return ctype_longlong(); + + case CVAL_UCHAR: + return ctype_uchar(); + + case CVAL_USHORT: + return ctype_ushort(); + + case CVAL_UINT: + return ctype_uint(); + + case CVAL_ULONG: + return ctype_ulong(); + + case CVAL_ULONGLONG: + return ctype_ulonglong(); + + case CVAL_STRING: + return ctype_array(ctype_char(), value.str_.len, alloc); + + default: + panic$("invalid cval type"); + } +} + +/* --- Builder -------------------------------------------------------------- */ + +CVal cval_error(void) +{ + return (CVal){ + .type = CVAL_INVALID, + }; +} + +CVal cval_char(CSigned val) +{ + return (CVal){ + .type = CVAL_CHAR, + .signed_ = val, + }; +} + +CVal cval_short(CSigned val) +{ + return (CVal){ + .type = CVAL_SHORT, + .signed_ = val, + }; +} + +CVal cval_int(CSigned val) { return (CVal){ - .type = CVAL_SIGNED, - .signed_ = v, + .type = CVAL_INT, + .signed_ = val, }; } -CVal cval_unsigned(CUnsigned v) +CVal cval_long(CSigned val) { return (CVal){ - .type = CVAL_UNSIGNED, - .unsigned_ = v, + .type = CVAL_LONG, + .signed_ = val, }; } -CVal cval_float(double v) +CVal cval_longlong(CSigned val) +{ + return (CVal){ + .type = CVAL_LONGLONG, + .signed_ = val, + }; +} + +CVal cval_uchar(CUnsigned val) +{ + return (CVal){ + .type = CVAL_UCHAR, + .signed_ = val, + }; +} + +CVal cval_ushort(CUnsigned val) +{ + return (CVal){ + .type = CVAL_USHORT, + .signed_ = val, + }; +} + +CVal cval_uint(CUnsigned val) +{ + return (CVal){ + .type = CVAL_UINT, + .signed_ = val, + }; +} + +CVal cval_ulong(CUnsigned val) +{ + return (CVal){ + .type = CVAL_ULONG, + .signed_ = val, + }; +} + +CVal cval_ulonglong(CUnsigned val) +{ + return (CVal){ + .type = CVAL_ULONGLONG, + .signed_ = val, + }; +} + +CVal cval_float(double val) { return (CVal){ .type = CVAL_FLOAT, - .float_ = v, + .float_ = val, + }; +} + +CVal cval_double(double val) +{ + return (CVal){ + .type = CVAL_DOUBLE, + .float_ = val, }; } -CVal cval_string(Str str) +CVal cval_str(Str str) { return (CVal){ .type = CVAL_STRING, - .string_ = str, + .str_ = str, }; } diff --git a/sources/libs/cc/builder/val.h b/sources/libs/cc/builder/val.h index a884e2710..2bcbbeb12 100644 --- a/sources/libs/cc/builder/val.h +++ b/sources/libs/cc/builder/val.h @@ -1,28 +1,38 @@ #pragma once +#include #include -#define cval$(VAL) \ - _Generic((VAL), \ - signed int \ - : cval_signed, \ - signed long \ - : cval_signed, \ - signed char \ - : cval_signed, \ - signed short \ - : cval_signed, \ - unsigned int \ - : cval_unsigned, \ - unsigned long \ - : cval_unsigned, \ - float \ - : cval_float)(VAL) - -CVal cval_signed(CSigned v); - -CVal cval_unsigned(CUnsigned v); - -CVal cval_float(double v); - -CVal cval_string(Str str); +Str cval_to_str(CVal type); + +CType cval_type(CVal value, Alloc *alloc); + +/* --- Builders ------------------------------------------------------------- */ + +CVal cval_error(void); + +CVal cval_char(CSigned val); + +CVal cval_short(CSigned val); + +CVal cval_int(CSigned val); + +CVal cval_long(CSigned val); + +CVal cval_long_long(CSigned val); + +CVal cval_uchar(CUnsigned val); + +CVal cval_ushort(CUnsigned val); + +CVal cval_uint(CUnsigned val); + +CVal cval_ulong(CUnsigned val); + +CVal cval_ulonglong(CUnsigned val); + +CVal cval_float(double val); + +CVal cval_double(double val); + +CVal cval_str(Str str); diff --git a/sources/libs/cc/dump/dump.c b/sources/libs/cc/dump/dump.c index e4254110b..3e7b621e5 100644 --- a/sources/libs/cc/dump/dump.c +++ b/sources/libs/cc/dump/dump.c @@ -1,37 +1,63 @@ -#include -#include #include +#include +#include -Json cdump_value(CVal value, Alloc *alloc) +Json cval_to_json(CVal val) { - Json json = json_object(alloc); - json_put(&json, str$("type"), json_str(cval_type_to_str(value.type))); - - switch (value.type) - { - case CVAL_SIGNED: - { - json_put(&json, str$("value"), json_number(value.signed_)); - break; - } - case CVAL_UNSIGNED: + switch (val.type) { - json_put(&json, str$("value"), json_number(value.unsigned_)); - break; - } + case CVAL_INVALID: + return json_null(); + + case CVAL_CHAR: + return json_number((char)val.signed_); + + case CVAL_SHORT: + return json_number((short)val.signed_); + + case CVAL_INT: + return json_number((int)val.signed_); + + case CVAL_LONG: + return json_number((long)val.signed_); + + case CVAL_LONGLONG: + return json_number((long long)val.signed_); + + case CVAL_UCHAR: + return json_number((unsigned char)val.unsigned_); + + case CVAL_USHORT: + return json_number((unsigned short)val.unsigned_); + + case CVAL_UINT: + return json_number((unsigned int)val.unsigned_); + + case CVAL_ULONG: + return json_number((unsigned long)val.unsigned_); + + case CVAL_ULONGLONG: + return json_number((unsigned long long)val.unsigned_); + case CVAL_FLOAT: - { - json_put(&json, str$("value"), json_str(str$("float are not supported for the moment"))); - break; - } + return json_number((float)val.float_); + + case CVAL_DOUBLE: + return json_number((double)val.float_); + case CVAL_STRING: - { - json_put(&json, str$("value"), json_str(value.string_)); - break; - } + return json_str(val.str_); + default: - panic$("unknown value type {}", value.type); + panic$("invalid cval type"); } +} + +Json cdump_value(CVal value, Alloc *alloc) +{ + Json json = json_object_with_type(str$("CVal"), alloc); + json_put(&json, str$("type"), json_str(cval_to_str(value))); + json_put(&json, str$("value"), cval_to_json(value)); return json; } @@ -51,98 +77,49 @@ Json cdump_member(CTypeMembers const *members, Alloc *alloc) Json cdump_type(CType type, Alloc *alloc) { + Json json = json_object_with_type(str$("CType"), alloc); + json_put(&json, str$("repr"), json_str(ctype_to_str(type))); + switch (type.type) { - case CTYPE_INVALID: - { - return json_str(str$("")); - } - case CTYPE_TAIL: - { - return json_str(str$("")); - } - case CTYPE_ERROR: - { - return json_str(str$("")); - } - case CTYPE_VOID: - { - return json_str(str$("void")); - } - case CTYPE_AUTO: - { - return json_str(str$("auto")); - } - case CTYPE_BOOL: - { - return json_str(str$("Bool")); - } case CTYPE_PTR: - { - Json json = json_object(alloc); - json_put(&json, str$("ptr"), cdump_type(*type.ptr_.subtype, alloc)); - return json; - } + json_put(&json, str$("subtype"), cdump_type(*type.ptr_.subtype, alloc)); + break; + case CTYPE_PARENT: - { - Json json = json_object(alloc); - json_put(&json, str$("parent"), cdump_type(*type.parent_.subtype, alloc)); - return json; - } + json_put(&json, str$("subtype"), cdump_type(*type.parent_.subtype, alloc)); + break; + case CTYPE_ARRAY: - { - Json json = json_object(alloc); - json_put(&json, str$("array"), cdump_type(*type.array_.subtype, alloc)); + json_put(&json, str$("subtype"), cdump_type(*type.array_.subtype, alloc)); if (type.array_.size != CTYPE_ARRAY_UNBOUNDED) { json_put(&json, str$("size"), json_number(type.array_.size)); } - return json; - } - case CTYPE_SIGNED: - { - return json_str(str$("signed")); - } - case CTYPE_UNSIGNED: - { - return json_str(str$("unsigned")); - } - case CTYPE_FLOAT: - { - return json_str(str$("float")); - } + break; + case CTYPE_STRUCT: - { - Json json = json_object(alloc); - json_put(&json, str$("struct"), cdump_member(&type.struct_.members, alloc)); - return json; - } + json_put(&json, str$("members"), cdump_member(&type.struct_.members, alloc)); + break; + case CTYPE_UNION: - { - Json json = json_object(alloc); - json_put(&json, str$("union"), cdump_member(&type.struct_.members, alloc)); - return json; - } - case CTYPE_ENUM: - { - return json_str(str$("enum")); - } + json_put(&json, str$("members"), cdump_member(&type.struct_.members, alloc)); + break; + case CTYPE_FUNC: - { - Json json = json_object(alloc); - json_put(&json, str$("func"), cdump_type(*type.func_.ret, alloc)); + json_put(&json, str$("ret"), cdump_type(*type.func_.ret, alloc)); json_put(&json, str$("args"), cdump_member(&type.func_.params, alloc)); - return json; - } + break; + case CTYPE_NAME: - { - Json json = json_object(alloc); json_put(&json, str$("name"), json_str(type.name)); - return json; - } + break; + default: - panic$("unknown ctype type {}", type.type); + break; } + + return json; } Json cdump_expr(CExpr expr, Alloc *alloc) @@ -248,7 +225,7 @@ Json cdump_stmt(CStmt stmt, Alloc *alloc) case CSTMT_DEFAULT: case CSTMT_BREAK: { - return json_str(cstmt_type_to_str(stmt.type)); + return json_str(cstmt_to_str(stmt.type)); } case CSTMT_DECL: { @@ -340,6 +317,7 @@ Json cdump_stmt(CStmt stmt, Alloc *alloc) Json cdump_decl(CDecl decl, Alloc *alloc) { Json json = json_object(alloc); + if (decl.sema_type.type != CTYPE_INVALID) { json_put(&json, str$("sema-type"), cdump_type(decl.sema_type, alloc)); @@ -364,7 +342,7 @@ Json cdump_decl(CDecl decl, Alloc *alloc) } else if (decl.type == CDECL_EMPTY) { - json_put(&json, str$("empty"), json_str(str$("empty"))); + json_put(&json, str$("nil"), json_str(str$("nil"))); } else { @@ -376,12 +354,13 @@ Json cdump_decl(CDecl decl, Alloc *alloc) Json cdump_unit(CUnit unit, Alloc *alloc) { Json json = json_object(alloc); - /* todo: add file info and more here */ Json units = json_array(alloc); + vec_foreach_v(entry, &unit.units) { switch (entry.type) { + case CUNIT_INCLUDE: { Json include = json_object(alloc); @@ -390,6 +369,7 @@ Json cdump_unit(CUnit unit, Alloc *alloc) json_append(&units, include); break; } + case CUNIT_PRAGMA: { Json pragma = json_object(alloc); @@ -397,11 +377,13 @@ Json cdump_unit(CUnit unit, Alloc *alloc) json_append(&units, pragma); break; } + case CUNIT_DECLARATION: { json_append(&units, cdump_decl(entry._decl, alloc)); break; } + case CUNIT_DEFINE: { Json def = json_object(alloc); diff --git a/sources/libs/cc/lex/lexemes.h b/sources/libs/cc/lex/lexemes.h index a10bd01e5..a4e250b48 100644 --- a/sources/libs/cc/lex/lexemes.h +++ b/sources/libs/cc/lex/lexemes.h @@ -6,7 +6,7 @@ LEXEME(COMMENT) \ LEXEME(NEWLINE) \ LEXEME(IDENT) \ - LEXEME(INTEGER) \ + LEXEME(NUMBER) \ LEXEME(AUTO) \ LEXEME(BREAK) \ LEXEME(CASE) \ diff --git a/sources/libs/cc/lex/lexer.c b/sources/libs/cc/lex/lexer.c index f9155c7a7..2dbf6da84 100644 --- a/sources/libs/cc/lex/lexer.c +++ b/sources/libs/cc/lex/lexer.c @@ -99,6 +99,55 @@ static int isidentchar(char v) return isalpha(v) || v == '_' || v == '$' || isdigit(v); } +static bool skip_bin_literal(Scan *scan) +{ + if (scan_skip_word(scan, str$("0b")) || scan_skip_word(scan, str$("0B"))) + { + while (scan_skip_any(scan, str$("01"))) + ; + return true; + } + + return false; +} + +static bool skip_octal_literal(Scan *scan) +{ + if (scan_skip_word(scan, str$("0"))) + { + while (scan_skip_any(scan, str$("01234567"))) + ; + return true; + } + + return false; +} + +static bool skip_dec_literal(Scan *scan) +{ + while (scan_skip_any(scan, str$("0123456789"))) + ; + return true; +} + +static bool skip_hex_literal(Scan *scan) +{ + if (scan_skip_word(scan, str$("0x")) || scan_skip_word(scan, str$("0X"))) + { + while (scan_skip_any_nc(scan, str$("0123456789abcdef"))) + ; + return true; + } + + return false; +} + +static void skip_int_suffix(Scan *scan) +{ + while (scan_skip_any_nc(scan, str$("ul"))) + ; +} + static LexemeType clex_impl(Scan *scan) { @@ -129,7 +178,11 @@ static LexemeType clex_impl(Scan *scan) return (CLEX_COMMENT); } - else if (scan_skip_word(scan, str$("\""))) + else if (scan_skip_word(scan, str$("\"")) || + scan_skip_word(scan, str$("u8\"")) || + scan_skip_word(scan, str$("u\"")) || + scan_skip_word(scan, str$("U\"")) || + scan_skip_word(scan, str$("L\""))) { while (!scan_skip(scan, '"') && !scan_ended(scan)) @@ -141,7 +194,7 @@ static LexemeType clex_impl(Scan *scan) } else if (scan_skip_word(scan, str$("'"))) { - if (scan_curr(scan) != '\'') // case for just '' + if (scan_curr(scan) != '\'') { scan_next(scan); } @@ -158,14 +211,13 @@ static LexemeType clex_impl(Scan *scan) } } - if (isdigit(scan_curr(scan))) + if (skip_bin_literal(scan) || + skip_octal_literal(scan) || + skip_dec_literal(scan) || + skip_hex_literal(scan)) { - while (isdigit(scan_curr(scan)) && !scan_ended(scan)) - { - scan_next(scan); - } - - return CLEX_INTEGER; + skip_int_suffix(scan); + return CLEX_NUMBER; } if (isidentchar(scan_curr(scan))) diff --git a/sources/libs/cc/parse/cval.c b/sources/libs/cc/parse/cval.c index 44bc3265f..9028fa686 100644 --- a/sources/libs/cc/parse/cval.c +++ b/sources/libs/cc/parse/cval.c @@ -4,20 +4,20 @@ static CVal cparse_cval_char(Lex *lex) { Str from_str = lex_curr(lex).str; - Str final_string = str_sub(from_str, 1, from_str.len - 1); + Str final_str = str_sub(from_str, 1, from_str.len - 1); lex_skip_type(lex, CLEX_CHARACTER); - return cval_string(final_string); + return cval_str(final_str); } -static CVal cparse_cval_string(Lex *lex) +static CVal cparse_cval_str(Lex *lex) { Str from_str = lex_curr(lex).str; - Str final_string = str_sub(from_str, 1, from_str.len - 1); + Str final_str = str_sub(from_str, 1, from_str.len - 1); lex_skip_type(lex, CLEX_STRING); - return cval_string(final_string); + return cval_str(final_str); } static CVal cparse_cval_integer(Lex *lex) @@ -25,28 +25,43 @@ static CVal cparse_cval_integer(Lex *lex) long value = 0; str_to_int(lex_curr(lex).str, &value); - lex_skip_type(lex, CLEX_INTEGER); + lex_skip_type(lex, CLEX_NUMBER); - return cval_signed(value); + return cval_int(value); +} + +static CVal cparse_cval_float(Lex *lex) +{ + float value = 0; + + str_to_float(lex_curr(lex).str, &value); + lex_skip_type(lex, CLEX_NUMBER); + + return cval_float(value); } CVal cparse_val(Lex *lex) { + CVal result = cval_error(); + int begin = lex->head; + if (lex_curr_type(lex) == CLEX_STRING) { - return with_cref$(cparse_cval_string(lex), begin, lex); + result = cparse_cval_str(lex); } - - if (lex_curr_type(lex) == CLEX_CHARACTER) + else if (lex_curr_type(lex) == CLEX_CHARACTER) { - return with_cref$(cparse_cval_char(lex), begin, lex); + result = cparse_cval_char(lex); } - - if (lex_curr_type(lex) == CLEX_INTEGER) + else if (lex_curr_type(lex) == CLEX_NUMBER) + { + result = cparse_cval_integer(lex); + } + else if (lex_curr_type(lex) == CLEX_NUMBER) { - return with_cref$(cparse_cval_integer(lex), begin, lex); + result = cparse_cval_float(lex); } - return (CVal){.type = CVAL_INVALID}; + return with_cref$(result, begin, lex); } diff --git a/sources/libs/cc/parse/decl.c b/sources/libs/cc/parse/decl.c index 76b5e62fc..248eb9b55 100644 --- a/sources/libs/cc/parse/decl.c +++ b/sources/libs/cc/parse/decl.c @@ -36,7 +36,7 @@ CType cparse_declarator_postfix(Lex *lex, CType type, Alloc *alloc) long size = CTYPE_ARRAY_UNBOUNDED; - if (lex_curr_type(lex) == CLEX_INTEGER) + if (lex_curr_type(lex) == CLEX_NUMBER) { str_to_int(lex_next(lex).str, &size); } diff --git a/sources/libs/cc/parse/expr.c b/sources/libs/cc/parse/expr.c index 49595fcd0..e62167828 100644 --- a/sources/libs/cc/parse/expr.c +++ b/sources/libs/cc/parse/expr.c @@ -77,7 +77,7 @@ static CExpr cparse_primary_expr(Lex *lex, CUnit *context, Alloc *alloc) cparse_skip_separator(lex, CLEX_IDENT); return with_cref$(cexpr_ident(val), begin, lex); } - else if (cparse_is_separator(lex, CLEX_INTEGER)) + else if (cparse_is_separator(lex, CLEX_NUMBER)) { return with_cref$(cexpr_constant(cparse_val(lex)), begin, lex); } diff --git a/sources/libs/cc/parse/misc.c b/sources/libs/cc/parse/misc.c index c888488f7..442447619 100644 --- a/sources/libs/cc/parse/misc.c +++ b/sources/libs/cc/parse/misc.c @@ -33,7 +33,8 @@ bool cparse_is_separator(Lex *lex, LexemeType type) Lexeme cparse_peek_separator(Lex *lex, int offset) { int off = 0; - while (lex_peek_type(lex, off) == CLEX_WHITESPACE && off != offset && !lex_ended(lex)) + while (lex_peek_type(lex, off) == CLEX_WHITESPACE && + off != offset && !lex_ended(lex)) { off++; } diff --git a/sources/libs/cc/parse/type.c b/sources/libs/cc/parse/type.c index 3120e938b..e43c72d9e 100644 --- a/sources/libs/cc/parse/type.c +++ b/sources/libs/cc/parse/type.c @@ -43,74 +43,121 @@ static CType cparse_compound_type(Lex *lex, bool is_union, Alloc *alloc) return compound; } -CType cparse_type(Lex *lex, Alloc *alloc) +CType cparse_enum_type(Lex *lex, Alloc *alloc) { - int begin = lex->head; - if (lex_skip_type(lex, CLEX_STRUCT)) + CType result = ctype_enum(alloc); + + cparse_whitespace(lex); + + if (lex_curr(lex).type == CLEX_IDENT) { - return with_cref$(cparse_compound_type(lex, false, alloc), begin, lex); + Str name = lex_next(lex).str; + ctype_named(result, name); + cparse_whitespace(lex); } - else if (lex_skip_type(lex, CLEX_UNION)) + + if (!lex_skip_type(lex, CLEX_LBRACE)) { - return with_cref$(cparse_compound_type(lex, true, alloc), begin, lex); + return result; } - else if (lex_skip_type(lex, CLEX_ENUM)) - { - CType enum_type = ctype_enum(alloc); + cparse_whitespace(lex); + + while (!lex_ended(lex) && lex_skip_type(lex, CLEX_SEMICOLON)) + { cparse_whitespace(lex); + } - if (lex_curr(lex).type == CLEX_IDENT) - { - Str name = lex_next(lex).str; - ctype_named(enum_type, name); - cparse_whitespace(lex); - } + cparse_whitespace(lex); - if (!lex_skip_type(lex, CLEX_LBRACE)) - { - return with_cref$(enum_type, begin, lex); - } + lex_expect(lex, CLEX_RBRACE); - cparse_whitespace(lex); + return result; +} - while (!lex_ended(lex) && lex_skip_type(lex, CLEX_SEMICOLON)) +CType cparse_signed_type(Lex *lex) +{ + if (lex_skip_type(lex, CLEX_CHAR)) + { + return ctype_char(); + } + else if (lex_skip_type(lex, CLEX_SHORT)) + { + return ctype_char(); + } + else if (lex_skip_type(lex, CLEX_INT)) + { + return ctype_int(); + } + else if (lex_skip_type(lex, CLEX_LONG)) + { + if (lex_skip_type(lex, CLEX_LONG)) { - cparse_whitespace(lex); + return ctype_longlong(); } + else + { + return ctype_long(); + } + } + else + { + lex_throw(lex, str$("Unexpected token")); + return ctype_error(); + } +} - cparse_whitespace(lex); - - lex_expect(lex, CLEX_RBRACE); +CType cparse_type(Lex *lex, Alloc *alloc) +{ + int begin = lex->head; + CType result = ctype_error(); - return with_cref$(enum_type, begin, lex); + if (lex_skip_type(lex, CLEX_STRUCT)) + { + result = cparse_compound_type(lex, false, alloc); + } + else if (lex_skip_type(lex, CLEX_UNION)) + { + result = cparse_compound_type(lex, true, alloc); + } + else if (lex_skip_type(lex, CLEX_ENUM)) + { + result = cparse_enum_type(lex, alloc); + } + else if (lex_curr_type(lex) == CLEX_IDENT) + { + result = ctype_ident(lex_next(lex).str); } else if (lex_skip_type(lex, CLEX_VOID)) { - return with_cref$(ctype_void(), begin, lex); + result = ctype_void(); } else if (lex_skip_type(lex, CLEX_BOOL)) { - return with_cref$(ctype_bool(), begin, lex); + result = ctype_bool(); } - else if (lex_skip_type(lex, CLEX_INT)) + else if (lex_skip_type(lex, CLEX_SIGNED)) { - return with_cref$(ctype_signed(32), begin, lex); + result = cparse_signed_type(lex); } - else if (lex_skip_type(lex, CLEX_CHAR)) + else if (lex_skip_type(lex, CLEX_UNSIGNED)) { - return with_cref$(ctype_signed(8), begin, lex); + result = ctype_to_unsigned(cparse_signed_type(lex)); } - else if (lex_curr_type(lex) == CLEX_IDENT) + else if (lex_skip_type(lex, CLEX_FLOAT)) { - Str name = lex_next(lex).str; - return with_cref$(ctype_ident(name), begin, lex); + result = ctype_float(); + } + else if (lex_skip_type(lex, CLEX_DOUBLE)) + { + result = ctype_double(); } else { - lex_throw(lex, str$("Unexpected token")); - return with_cref$(ctype_error(), begin, lex); + result = cparse_signed_type(lex); } + + return with_cref$(result, begin, lex); } bool is_cparse_type(Lex *lex, CUnit *context) @@ -122,9 +169,19 @@ bool is_cparse_type(Lex *lex, CUnit *context) case CLEX_ENUM: case CLEX_VOID: case CLEX_BOOL: - case CLEX_INT: + case CLEX_CHAR: + case CLEX_SHORT: + case CLEX_INT: + case CLEX_LONG: + + case CLEX_UNSIGNED: + case CLEX_SIGNED: + + case CLEX_FLOAT: + case CLEX_DOUBLE: return true; + case CLEX_IDENT: return cunit_contains_type(context, lex_curr(lex).str); diff --git a/sources/libs/cc/sema/cast.c b/sources/libs/cc/sema/cast.c deleted file mode 100644 index bc97dc7fd..000000000 --- a/sources/libs/cc/sema/cast.c +++ /dev/null @@ -1,2 +0,0 @@ - -#include diff --git a/sources/libs/cc/sema/cast.h b/sources/libs/cc/sema/cast.h deleted file mode 100644 index 119070cbb..000000000 --- a/sources/libs/cc/sema/cast.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include - -bool can_be_casted(bool explicit, CType to, CType from); diff --git a/sources/libs/cc/sema/check/array_type.c b/sources/libs/cc/sema/check/array_type.c deleted file mode 100644 index 71ca49f3a..000000000 --- a/sources/libs/cc/sema/check/array_type.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -void sema_array_check_expr(CSema *self, CExpr *cexpr, MAYBE_UNUSED void *ctx) -{ - if (cexpr->type != CEXPR_INFIX || cexpr->infix_.op != COP_INDEX) - { - return; - } - - CType left_type = cexpr->infix_.lhs->sema_type; - - if (left_type.type != CTYPE_ARRAY && left_type.type != CTYPE_PTR) - { - int id = csema_report$(self, PARSE_ERR, *cexpr, "Trying to use an array operator with a non ptr/array object"); - - Str expression_type_dump = csema_ctype_to_str(*cexpr->infix_.lhs, self->alloc); - - csema_comment$(self, id, *cexpr->infix_.lhs, "Expression with type: {}", expression_type_dump); - - return; - } -} diff --git a/sources/libs/cc/sema/check/array_type.h b/sources/libs/cc/sema/check/array_type.h deleted file mode 100644 index d0c0356b9..000000000 --- a/sources/libs/cc/sema/check/array_type.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -void sema_array_check_expr(CSema *self, CExpr *cexpr, void *ctx); - -#define SEMA_ARRAY_TYPE_VALIDATION \ - { \ - .name = ("array type"), \ - .type = CSEMA_CHECK_EXPR, \ - ._expr = sema_array_check_expr, \ - } diff --git a/sources/libs/cc/sema/check/call.c b/sources/libs/cc/sema/check/call.c deleted file mode 100644 index f894f4f52..000000000 --- a/sources/libs/cc/sema/check/call.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -void sema_call_check_expr(CSema *self, CExpr *cexpr, MAYBE_UNUSED void *ctx) -{ - if (cexpr->type != CEXPR_CALL) - { - return; - } - CType left_type = cexpr->call_.expr->sema_type; - - if (left_type.type != CTYPE_FUNC) - { - int id = csema_report$(self, PARSE_ERR, *cexpr, "calling a non callable expression"); - Str expression_type_dump = csema_ctype_to_str(*cexpr->call_.expr, self->alloc); - csema_comment$(self, id, *cexpr->call_.expr, "Expression with type: {}", expression_type_dump); - } -} diff --git a/sources/libs/cc/sema/check/call.h b/sources/libs/cc/sema/check/call.h deleted file mode 100644 index 5fe4937e0..000000000 --- a/sources/libs/cc/sema/check/call.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -void sema_call_check_expr(CSema *self, CExpr *cexpr, void *ctx); - -#define SEMA_CALL_VALIDATION \ - { \ - .name = ("call type"), \ - .type = CSEMA_CHECK_EXPR, \ - ._expr = sema_call_check_expr, \ - } diff --git a/sources/libs/cc/sema/check/decl.c b/sources/libs/cc/sema/check/decl.c deleted file mode 100644 index 6e8ecb129..000000000 --- a/sources/libs/cc/sema/check/decl.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include - -// error checking with declaration redefinition -void sema_decl_redef_check(CSema *self, CDecl *decl, MAYBE_UNUSED void *ctx) -{ - CDecl other = csema_lookup(self, decl->name); - bool redefinition = false; - if (decl->type == CDECL_VAR) - { - // var + var = error - if (other.type == CDECL_VAR) - { - redefinition = true; - } - // global var + type / func = error - else if ((other.type == CDECL_TYPE || other.type == CDECL_FUNC) && decl->global == true) - { - redefinition = true; - } - } - else - { - // func / type + global var = error - if (other.type == CDECL_VAR && other.global) - { - redefinition = true; - } - // func / type + func / type = error - else if (other.type == CDECL_TYPE || other.type == CDECL_FUNC) - { - redefinition = true; - } - } - - if (redefinition) - { - int id = csema_report$(self, PARSE_ERR, *decl, "redefinition of: {}", decl->name); - csema_comment$(self, id, other, "already defined here"); - } -} - -void sema_decl_ref_check_expr(CSema *self, CExpr *expr, MAYBE_UNUSED void *ctx) -{ - if (expr->type != CEXPR_IDENT) - { - return; - } - - CDecl prev = csema_lookup(self, expr->ident_); - - if (prev.type == CDECL_NIL) - { - csema_report$(self, PARSE_ERR, *expr, "Not defined symbol: {}", expr->ident_); - } -} diff --git a/sources/libs/cc/sema/check/decl.h b/sources/libs/cc/sema/check/decl.h deleted file mode 100644 index 00f0f347c..000000000 --- a/sources/libs/cc/sema/check/decl.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include - -void sema_decl_redef_check(CSema *self, CDecl *decl, void *ctx); - -void sema_decl_ref_check_expr(CSema *self, CExpr *expr, void *ctx); - -#define SEMA_DECL_DEF_VALIDATION \ - { \ - .name = ("decl redefinition"), \ - .type = CSEMA_CHECK_DECL, \ - ._decl = {sema_decl_redef_check, false}, \ - } - -#define SEMA_DECL_REF_VALIDATION \ - { \ - .name = ("decl reference"), \ - .type = CSEMA_CHECK_EXPR, \ - ._expr = sema_decl_ref_check_expr, \ - } diff --git a/sources/libs/cc/sema/check/deref_type.c b/sources/libs/cc/sema/check/deref_type.c deleted file mode 100644 index cdf96f4cd..000000000 --- a/sources/libs/cc/sema/check/deref_type.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -void sema_deref_check_expr(CSema *self, CExpr *cexpr, MAYBE_UNUSED void *ctx) -{ - if (cexpr->type != CEXPR_PREFIX || cexpr->prefix_.op != COP_DEREF) - { - return; - } - CType left_type = cexpr->prefix_.expr->sema_type; - - if (left_type.type != CTYPE_ARRAY && left_type.type != CTYPE_PTR) - { - int id = csema_report$(self, PARSE_ERR, *cexpr, "Trying to use an deref operator with a non ptr/array object"); - - Str expression_type_dump = csema_ctype_to_str(*cexpr->prefix_.expr, self->alloc); - - csema_comment$(self, id, *cexpr->prefix_.expr, "Expression with type: {}", expression_type_dump); - - return; - } -} diff --git a/sources/libs/cc/sema/check/deref_type.h b/sources/libs/cc/sema/check/deref_type.h deleted file mode 100644 index c8c21f3de..000000000 --- a/sources/libs/cc/sema/check/deref_type.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include - -void sema_deref_check_expr(CSema *self, CExpr *cexpr, void *ctx); - -#define SEMA_DEREF_VALIDATION \ - { \ - .name = ("deref type"), \ - .type = CSEMA_CHECK_EXPR, \ - ._expr = sema_deref_check_expr, \ - } diff --git a/sources/libs/cc/sema/check/utils.c b/sources/libs/cc/sema/check/utils.c deleted file mode 100644 index 85a43f958..000000000 --- a/sources/libs/cc/sema/check/utils.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -Str csema_ctype_to_str(CExpr expr, Alloc *alloc) -{ - Emit emit; - Buf buf = {}; - buf_init(&buf, 18, alloc); - emit_init(&emit, buf_writer(&buf)); - - cc_trans_type(&emit, expr.sema_type); - emit_deinit(&emit); - - return str_n$(buf.used, (char *)buf.data); -} diff --git a/sources/libs/cc/sema/check/utils.h b/sources/libs/cc/sema/check/utils.h deleted file mode 100644 index c97719f92..000000000 --- a/sources/libs/cc/sema/check/utils.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include -#include -#include - -Str csema_ctype_to_str(CExpr expr, Alloc *alloc); diff --git a/sources/libs/cc/sema/checks.c b/sources/libs/cc/sema/checks.c deleted file mode 100644 index a6958688d..000000000 --- a/sources/libs/cc/sema/checks.c +++ /dev/null @@ -1,262 +0,0 @@ -#include -#include - -// see checks.h header for more explanation - -void do_sema_check_decl(SemaCheck *check, CDecl *decl, CSema *sema); -void do_sema_check_stmt(SemaCheck *check, CStmt *stmt, CSema *sema); - -void do_sema_check_type(SemaCheck *check, CType *type, CSema *sema) -{ - if (check->type == CSEMA_CHECK_TYPE) - { - check->_type(sema, type, check->ctx); - } - - switch (type->type) - { - case CTYPE_ARRAY: - { - do_sema_check_type(check, type->array_.subtype, sema); - break; - } - - case CTYPE_PTR: - case CTYPE_REF: - case CTYPE_PARENT: - { - do_sema_check_type(check, type->ref_.subtype, sema); - break; - } - case CTYPE_STRUCT: - case CTYPE_UNION: - { - vec_foreach(member, &type->struct_.members) - { - do_sema_check_type(check, &member->type, sema); - } - break; - } - case CTYPE_FUNC: - { - do_sema_check_type(check, type->func_.ret, sema); - vec_foreach(func_arg, &type->func_.params) - { - do_sema_check_type(check, &func_arg->type, sema); - } - - break; - } - default: - { - break; - } - } -} - -void do_sema_check_expr(SemaCheck *check, CExpr *expr, CSema *sema) -{ - if (check->type == CSEMA_CHECK_EXPR) - { - check->_expr(sema, expr, check->ctx); - } - - switch (expr->type) - { - - case CEXPR_IDENT: - case CEXPR_CONSTANT: - case CEXPR_EMPTY: - { - break; - } - case CEXPR_POSTFIX: - case CEXPR_PREFIX: - { - do_sema_check_expr(check, expr->postfix_.expr, sema); - break; - } - case CEXPR_INFIX: - { - do_sema_check_expr(check, expr->infix_.lhs, sema); - do_sema_check_expr(check, expr->infix_.rhs, sema); - break; - } - case CEXPR_CALL: - { - do_sema_check_expr(check, expr->call_.expr, sema); - vec_foreach(arg, &expr->call_.args) - { - do_sema_check_expr(check, arg, sema); - } - break; - } - case CEXPR_CAST: - { - do_sema_check_type(check, &expr->cast_.type, sema); - do_sema_check_expr(check, expr->cast_.expr, sema); - break; - } - case CEXPR_TERNARY: - { - do_sema_check_expr(check, expr->ternary_.expr_cond, sema); - do_sema_check_expr(check, expr->ternary_.expr_true, sema); - do_sema_check_expr(check, expr->ternary_.expr_false, sema); - break; - } - case CEXPR_INITIALIZER: - { - do_sema_check_type(check, &expr->initializer_.type, sema); - vec_foreach(init, &expr->initializer_.initializer) - { - do_sema_check_expr(check, init, sema); - } - break; - } - case CEXPR_LAMBDA: - { - do_sema_check_type(check, &expr->lambda_.type, sema); - do_sema_check_stmt(check, expr->lambda_.body, sema); - break; - } - default: - { - panic$("unhandled expr: {}", expr->type); - } - } -} - -void do_sema_check_stmt(SemaCheck *check, CStmt *stmt, CSema *sema) -{ - - if (check->type == CSEMA_CHECK_STMT) - { - check->_stmt(sema, stmt, check->ctx); - } - - switch (stmt->type) - { - case CSTMT_DECL: - { - do_sema_check_decl(check, stmt->decl_.decl, sema); - break; - } - case CSTMT_EXPR: - { - do_sema_check_expr(check, &stmt->expr_.expr, sema); - break; - } - case CSTMT_BLOCK: - { - csema_scope_enter(sema); - vec_foreach(v, &stmt->block_.stmts) - { - do_sema_check_stmt(check, v, sema); - } - csema_scope_leave(sema); - break; - } - case CSTMT_IF: - { - do_sema_check_expr(check, &stmt->if_.expr, sema); - do_sema_check_stmt(check, stmt->if_.stmt_true, sema); - do_sema_check_stmt(check, stmt->if_.stmt_false, sema); - break; - } - case CSTMT_FOR: - { - do_sema_check_stmt(check, stmt->for_.init_stmt, sema); - do_sema_check_expr(check, &stmt->for_.cond_expr, sema); - do_sema_check_expr(check, &stmt->for_.iter_expr, sema); - - do_sema_check_stmt(check, stmt->for_.stmt, sema); - break; - } - case CSTMT_WHILE: - case CSTMT_DO: - case CSTMT_SWITCH: - { - do_sema_check_expr(check, &stmt->while_.expr, sema); - do_sema_check_stmt(check, stmt->while_.stmt, sema); - break; - } - case CSTMT_RETURN: - { - do_sema_check_expr(check, &stmt->return_.expr, sema); - break; - } - case CSTMT_CASE: - { - do_sema_check_expr(check, &stmt->case_.expr, sema); - break; - } - default: - break; - } -} - -void do_sema_check_decl(SemaCheck *check, CDecl *decl, CSema *sema) -{ - if (check->type != CSEMA_CHECK_DECL || check->_decl.include_recursive_entry) - { - csema_scope_add(sema, *decl); - } - - if (check->type == CSEMA_CHECK_DECL) - { - check->_decl.fn(sema, decl, check->ctx); - } - - switch (decl->type) - { - case CDECL_TYPE: - do_sema_check_type(check, &decl->type_.type, sema); - break; - case CDECL_VAR: - do_sema_check_type(check, &decl->var_.type, sema); - do_sema_check_expr(check, &decl->var_.expr, sema); - break; - case CDECL_FUNC: - { - csema_scope_enter_func(sema, decl->func_.type); - do_sema_check_type(check, &decl->func_.type, sema); - do_sema_check_stmt(check, &decl->func_.body, sema); - csema_scope_leave(sema); - break; - } - default: - break; - } - if (check->type == CSEMA_CHECK_DECL && !check->_decl.include_recursive_entry) - { - csema_scope_add(sema, *decl); - } -} - -static void do_sema_check_unit(SemaCheck *check, CUnit *unit, CSema *sema) -{ - csema_scope_enter(sema); - - if (check->type == CSEMA_CHECK_UNIT) - { - check->_unit(sema, unit, check->ctx); - csema_scope_leave(sema); - return; - } - - vec_foreach(entry, &unit->units) - { - if (entry->type == CUNIT_DECLARATION) - { - - do_sema_check_decl(check, &entry->_decl, sema); - } - } - - csema_scope_leave(sema); -} - -void do_sema_check(SemaCheck *check, CUnit *unit, CSema *sema) -{ - do_sema_check_unit(check, unit, sema); -} diff --git a/sources/libs/cc/sema/checks.h b/sources/libs/cc/sema/checks.h deleted file mode 100644 index bd736f831..000000000 --- a/sources/libs/cc/sema/checks.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include -#include -#include - -typedef enum -{ - CSEMA_CHECK_NONE, - CSEMA_CHECK_EXPR, - CSEMA_CHECK_DECL, - CSEMA_CHECK_STMT, - CSEMA_CHECK_TYPE, - CSEMA_CHECK_UNIT, -} CSemaCheckTypes; - -typedef void (*CSemaCheckExpr)(CSema *self, CExpr *, void *ctx); -typedef void (*CSemaCheckDecl)(CSema *self, CDecl *, void *ctx); -typedef void (*CSemaCheckStmt)(CSema *self, CStmt *, void *ctx); -typedef void (*CSemaCheckType)(CSema *self, CType *, void *ctx); -typedef void (*CSemaCheckUnit)(CSema *self, CUnit *, void *ctx); - -typedef struct -{ - const char *name; - CSemaCheckTypes type; - void *ctx; - - union - { - CSemaCheckExpr _expr; - struct - { - - CSemaCheckDecl fn; - // if this is true, then push the declaration in the context before calling fn - // if this is false, it will push the declaration in the context after calling fn - bool include_recursive_entry; - } _decl; - CSemaCheckStmt _stmt; - CSemaCheckType _type; - CSemaCheckUnit _unit; - }; -} SemaCheck; - -void do_sema_check(SemaCheck *check, CUnit *unit, CSema *sema); - -void do_sema_checks(CUnit *unit, CSema *sema); diff --git a/sources/libs/cc/sema/checks_list.c b/sources/libs/cc/sema/checks_list.c deleted file mode 100644 index 08416f8db..000000000 --- a/sources/libs/cc/sema/checks_list.c +++ /dev/null @@ -1,29 +0,0 @@ - -// here you register every sema check -// note: the order is important - -#include -#include -#include -#include -#include -#include - -SemaCheck check_list[] = { - SEMA_DECL_REF_VALIDATION, - SEMA_DECL_DEF_VALIDATION, - SEMA_ARRAY_TYPE_VALIDATION, - SEMA_CALL_VALIDATION, - SEMA_DEREF_VALIDATION, - -}; - -void do_sema_checks(CUnit *unit, CSema *sema) -{ - for (size_t i = 0; i < sizeof(check_list) / sizeof(check_list[0]); i++) - { - csema_scope_reset(sema); - sema->current_pass = str$(check_list[i].name); - do_sema_check(&check_list[i], unit, sema); - } -} diff --git a/sources/libs/cc/sema/passes.c b/sources/libs/cc/sema/passes.c new file mode 100644 index 000000000..a0ce31e32 --- /dev/null +++ b/sources/libs/cc/sema/passes.c @@ -0,0 +1,131 @@ +#include +#include + +void cpass_scope_propagation(CSema *ctx, int what, void *node) +{ + bool is_scope = + cvisit_node_is(node, CVISIT_DECL, CDECL_FUNC) && + cvisit_node_is(node, CVISIT_STMT, CSTMT_BLOCK) && + cvisit_node_is(node, CVISIT_UNIT, 0); + + if (!is_scope) + return; + + if (what & CVISIT_BEGIN) + { + if (cvisit_node_is(node, CVISIT_DECL, CDECL_FUNC)) + { + csema_scope_enter_func(ctx, ((CDecl *)node)->func_.type); + } + else + { + csema_scope_enter(ctx); + } + } + else + { + csema_scope_leave(ctx); + } +} + +void cpass_types_propagation(CSema *ctx, int what, void *node) +{ + if (what & CVISIT_BEGIN) + { + } + else + { + } +} + +void cpass_op_array(CSema *ctx, int, void *node) +{ + CExpr *expr = node; + + if (expr->type != CEXPR_INFIX || expr->infix_.op != COP_INDEX) + { + return; + } + + CType lhs = expr->infix_.lhs->sema_type; + + if (lhs.type != CTYPE_ARRAY && lhs.type != CTYPE_PTR) + { + int id = csema_report$(ctx, PARSE_ERR, *expr, "Trying to use an array operator with a non ptr/array object"); + + Str str = ctrans_type_str(*expr->infix_.lhs, ctx->alloc); + csema_comment$(ctx, id, *expr->infix_.lhs, "Expression with type: {}", str); + } +} + +void cpass_op_deref(CSema *ctx, int, void *node) +{ + CExpr *expr = node; + + if (expr->type != CEXPR_PREFIX || expr->prefix_.op != COP_DEREF) + { + return; + } + + CType left_type = expr->prefix_.expr->sema_type; + + if (left_type.type != CTYPE_ARRAY && left_type.type != CTYPE_PTR) + { + int id = csema_report$(ctx, PARSE_ERR, *expr, "Trying to use an deref operator with a non ptr/array object"); + + Str str = ctrans_type_str(*expr->prefix_.expr, ctx->alloc); + csema_comment$(ctx, id, *expr->prefix_.expr, "Expression with type: {}", str); + } +} + +void cpass_expr_call(CSema *ctx, int, void *node) +{ + CExpr *expr = node; + + if (expr->type != CEXPR_CALL) + { + return; + } + + CType left_type = expr->call_.expr->sema_type; + + if (left_type.type != CTYPE_FUNC) + { + int id = csema_report$(ctx, PARSE_ERR, *expr, "calling a non callable expression"); + + Str str = ctrans_type_str(*expr->call_.expr, ctx->alloc); + csema_comment$(ctx, id, *expr->call_.expr, "Expression with type: {}", str); + } +} + +void cpass_decl_def(CSema *ctx, int, void *node) +{ + CDecl *decl = node; + + CDecl other; + if (csema_lookup_current_scope(ctx, decl->name, &other)) + { + int id = csema_report$(ctx, PARSE_ERR, *decl, "redefinition of: {}", decl->name); + csema_comment$(ctx, id, other, "already defined here"); + } +} + +void cpass_decl_deref(CSema *ctx, int, void *node) +{ + CExpr *expr = node; + + CDecl prev; + if (expr->type == CEXPR_IDENT && !csema_lookup(ctx, expr->ident_, &prev)) + { + csema_report$(ctx, PARSE_ERR, *expr, "Not defined symbol: {}", expr->ident_); + } +} + +static CVisit passes[] = { + {}, +}; + +CVisites cpass_all(void) +{ + return slice_array$(CVisites, passes); +} diff --git a/sources/libs/cc/sema/passes.h b/sources/libs/cc/sema/passes.h new file mode 100644 index 000000000..34856ace8 --- /dev/null +++ b/sources/libs/cc/sema/passes.h @@ -0,0 +1,3 @@ +#pragma once + +#include diff --git a/sources/libs/cc/sema/scope.c b/sources/libs/cc/sema/scope.c index b70b99a4b..1f0a2737f 100644 --- a/sources/libs/cc/sema/scope.c +++ b/sources/libs/cc/sema/scope.c @@ -3,7 +3,7 @@ void cscope_init(CScope *self, Alloc *alloc) { *self = (CScope){}; - self->expected_result.type = CTYPE_INVALID; + self->ret.type = CTYPE_INVALID; vec_init(&self->decls, alloc); } @@ -12,17 +12,18 @@ void cscope_deinit(CScope *self) vec_deinit(&self->decls); } -CDecl cscope_lookup(CScope *self, Str name) +bool cscope_lookup(CScope *self, Str name, CDecl *out_decl) { - - vec_foreach(decl, &self->decls) + vec_foreach_rev(decl, &self->decls) { if (str_eq(decl->name, name)) { - return *decl; + *out_decl = *decl; + return true; } } - return (CDecl){}; + + return false; } void cscope_add(CScope *self, CDecl decl) diff --git a/sources/libs/cc/sema/scope.h b/sources/libs/cc/sema/scope.h index ec03c5d66..1415a4f7f 100644 --- a/sources/libs/cc/sema/scope.h +++ b/sources/libs/cc/sema/scope.h @@ -5,13 +5,13 @@ typedef struct { Vec(CDecl) decls; - CType expected_result; + CType ret; } CScope; void cscope_init(CScope *self, Alloc *alloc); void cscope_deinit(CScope *self); -CDecl cscope_lookup(CScope *self, Str name); +bool cscope_lookup(CScope *self, Str name, CDecl *decl); void cscope_add(CScope *self, CDecl decl); diff --git a/sources/libs/cc/sema/sema.c b/sources/libs/cc/sema/sema.c index 81df3c41c..1e689b9eb 100644 --- a/sources/libs/cc/sema/sema.c +++ b/sources/libs/cc/sema/sema.c @@ -42,28 +42,35 @@ void csema_scope_add(CSema *self, CDecl decl) cscope_add(scope, decl); } -CDecl csema_lookup(CSema *self, Str name) +bool csema_lookup(CSema *self, Str name, CDecl *decl) { - vec_foreach(scope, &self->scopes) + vec_foreach_rev(scope, &self->scopes) { - CDecl d = cscope_lookup(scope, name); - if (d.type != CDECL_NIL) + if (cscope_lookup(scope, name, decl)) { - return d; + return true; } } - return (CDecl){}; + + return false; +} + +bool csema_lookup_current_scope(CSema *self, Str name, CDecl *decl) +{ + CScope *scope = &vec_last(&self->scopes); + return cscope_lookup(scope, name, decl); } CType csema_scope_return_type(CSema *self) { - vec_foreach(scope, &self->scopes) + vec_foreach_rev(scope, &self->scopes) { - if (scope->expected_result.type != CTYPE_INVALID) + if (scope->ret.type != CTYPE_INVALID) { - return scope->expected_result; + return scope->ret; } } + return (CType){.type = CTYPE_INVALID}; } @@ -71,20 +78,13 @@ void csema_scope_enter_func(CSema *self, CType func_type) { CScope scope; cscope_init(&scope, self->alloc); - scope.expected_result = *func_type.func_.ret; + scope.ret = *func_type.func_.ret; - // add each argument in scope vec_foreach(arg, &func_type.func_.params) { - CDecl decl = (CDecl){ - .type = CDECL_VAR, - .name = str_dup(arg->name, self->alloc), - .var_ = { - .type = arg->type, - .expr = cexpr_empty(), - }, - }; + CDecl decl = cdecl_var(arg->name, arg->type, cexpr_empty()); cscope_add(&scope, decl); } + vec_push(&self->scopes, scope); } diff --git a/sources/libs/cc/sema/sema.h b/sources/libs/cc/sema/sema.h index 693861fcf..f8302d7b1 100644 --- a/sources/libs/cc/sema/sema.h +++ b/sources/libs/cc/sema/sema.h @@ -2,6 +2,7 @@ #include #include + typedef struct { Vec(CScope) scopes; @@ -25,7 +26,9 @@ void csema_scope_reset(CSema *self); void csema_scope_add(CSema *self, CDecl decl); -CDecl csema_lookup(CSema *self, Str name); +bool csema_lookup(CSema *self, Str name, CDecl *decl); + +bool csema_lookup_current_scope(CSema *self, Str name, CDecl *decl); CType csema_scope_return_type(CSema *self); diff --git a/sources/libs/cc/sema/visit.c b/sources/libs/cc/sema/visit.c index 70555bc4d..dc1d9bb7b 100644 --- a/sources/libs/cc/sema/visit.c +++ b/sources/libs/cc/sema/visit.c @@ -1,313 +1,264 @@ +#include #include #include #include -#include -CType csema_value_type(MAYBE_UNUSED CSema *sema, CVal value, Alloc *alloc) +bool cvisit_node_is(void *node, int what, int type) { - /* FIXME: autodetect precision based on architecture */ - switch (value.type) + if (what & CVISIT_DECL) { - case CVAL_FLOAT: - return ctype_float(32); - case CVAL_SIGNED: - return ctype_signed(32); - case CVAL_UNSIGNED: - return ctype_unsigned(32); - case CVAL_STRING: - return ctype_array(ctype_signed(8), value.string_.len, alloc); - default: - return ctype_void(); /* FIXME: better error handling */ + return (int)((CDecl *)node)->type == type; } -} - -static CType csema_decl_type(CSema *self, Str name) -{ - vec_foreach(scopes, &self->scopes) + else if (what & CVISIT_EXPR) { - vec_foreach(decls, &scopes->decls) - { - if (str_eq(decls->name, name)) - { - return decls->sema_type; - } - } + return (int)((CExpr *)node)->type == type; } - return ctype_error(); -} - -static CExpr csema_prefix_expr(CSema *sema, CExpr expr, Alloc *alloc) -{ - *expr.prefix_.expr = csema_expr(sema, *expr.prefix_.expr, alloc); - switch (expr.prefix_.op) - { - case COP_REF: + else if (what & CVISIT_STMT) { - expr.sema_type = ctype_ptr(expr.prefix_.expr->sema_type, alloc); - return expr; + return (int)((CStmt *)node)->type == type; } - case COP_DEREF: + else if (what & CVISIT_TYPE) { - if (!(expr.prefix_.expr->sema_type.type == CTYPE_PTR)) - { - expr.sema_type = ctype_void(); - return expr; - } - expr.sema_type = *expr.prefix_.expr->sema_type.ptr_.subtype; - return expr; + return (int)((CType *)node)->type == type; } - - case COP_NOT: - case COP_DEC: - case COP_INC: + else if (what & CVISIT_UNIT) { - expr.sema_type = expr.prefix_.expr->sema_type; - return expr; + return true; } - default: + else { - return expr; - } + return false; } } -static CExpr csema_postfix_expr(CSema *sema, CExpr expr, Alloc *alloc) +void cvisit_before(CVisites passes, CSema *sema, int what, void *node) { - *expr.postfix_.expr = csema_expr(sema, *expr.postfix_.expr, alloc); - switch (expr.postfix_.op) - { - case COP_DEC: - case COP_INC: + slice_foreach$(pass, passes) { - expr.sema_type = expr.postfix_.expr->sema_type; - return expr; - } - default: - { - return expr; - } + int mask = what | CVISIT_BEGIN; + if ((pass->what & mask) == mask) + { + pass->fn(sema, what, node); + } } } -static CExpr csema_infix_expr(CSema *sema, CExpr expr, Alloc *alloc) +void cvisit_after(CVisites passes, CSema *sema, int what, void *node) { - *expr.infix_.lhs = csema_expr(sema, *expr.infix_.lhs, alloc); - *expr.infix_.rhs = csema_expr(sema, *expr.infix_.rhs, alloc); - - switch (expr.infix_.op) + slice_foreach_rev$(pass, passes) { - case COP_INDEX: - { - if (expr.infix_.lhs->sema_type.type == CTYPE_ARRAY) - { - expr.sema_type = *expr.infix_.lhs->sema_type.array_.subtype; - } - else if (expr.infix_.lhs->sema_type.type == CTYPE_PTR) - { - expr.sema_type = *expr.infix_.lhs->sema_type.ptr_.subtype; - } - else + int mask = what | CVISIT_END; + if ((pass->what & mask) == mask) { - expr.sema_type = expr.infix_.lhs->sema_type; + pass->fn(sema, what, node); } - return expr; } - /* for every unsupported basic op like add sub etc... this works */ - default: +} + +void cvisit_decl(CVisites passes, CSema *sema, CDecl *decl) +{ + cvisit_before(passes, sema, CVISIT_DECL, decl); + + switch (decl->type) { - expr.sema_type = expr.infix_.lhs->sema_type; - return expr; - } + case CDECL_EMPTY: + break; + + case CDECL_TYPE: + cvisit_type(passes, sema, &decl->type_.type); + break; + + case CDECL_VAR: + cvisit_type(passes, sema, &decl->var_.type); + cvisit_expr(passes, sema, &decl->var_.expr); + break; + + case CDECL_FUNC: + cvisit_type(passes, sema, &decl->func_.type); + cvisit_stmt(passes, sema, &decl->func_.body); + break; + + default: + panic$("Unknow decl type"); } + + cvisit_after(passes, sema, CVISIT_DECL, decl); } -CExpr csema_expr(CSema *sema, CExpr expr, Alloc *alloc) +void cvisit_expr(CVisites passes, CSema *sema, CExpr *expr) { - switch (expr.type) + cvisit_before(passes, sema, CVISIT_EXPR, expr); + + switch (expr->type) { + case CEXPR_IDENT: case CEXPR_CONSTANT: - { - expr.sema_type = csema_value_type(sema, expr.constant_, alloc); - return expr; - } case CEXPR_EMPTY: - { - expr.sema_type = ctype_void(); - return expr; - } - case CEXPR_IDENT: - { - expr.sema_type = csema_decl_type(sema, expr.ident_); - return expr; - } - case CEXPR_CAST: - { - /* don't check for the moment */ - expr.sema_type = expr.cast_.type; - return expr; - } - case CEXPR_CALL: - { - *expr.call_.expr = csema_expr(sema, *expr.call_.expr, alloc); + break; + + case CEXPR_POSTFIX: + case CEXPR_PREFIX: + cvisit_expr(passes, sema, expr->postfix_.expr); + break; - if (expr.call_.expr->sema_type.type != CTYPE_FUNC) + case CEXPR_INFIX: + cvisit_expr(passes, sema, expr->infix_.lhs); + cvisit_expr(passes, sema, expr->infix_.rhs); + break; + + case CEXPR_CALL: + cvisit_expr(passes, sema, expr->call_.expr); + vec_foreach(arg, &expr->call_.args) { - expr.sema_type = ctype_void(); - return expr; + cvisit_expr(passes, sema, arg); } - /* FIXME: no check for this for the moment */ - vec_foreach(arg, &expr.call_.args) + break; + + case CEXPR_CAST: + cvisit_type(passes, sema, &expr->cast_.type); + cvisit_expr(passes, sema, expr->cast_.expr); + break; + + case CEXPR_TERNARY: + cvisit_expr(passes, sema, expr->ternary_.expr_cond); + cvisit_expr(passes, sema, expr->ternary_.expr_true); + cvisit_expr(passes, sema, expr->ternary_.expr_false); + break; + + case CEXPR_INITIALIZER: + cvisit_type(passes, sema, &expr->initializer_.type); + + vec_foreach(init, &expr->initializer_.initializer) { - *arg = csema_expr(sema, *arg, alloc); + cvisit_expr(passes, sema, init); } - expr.sema_type = *expr.call_.expr->sema_type.func_.ret; - return expr; - } + break; + case CEXPR_LAMBDA: - { - expr.sema_type = ctype_void(); - return expr; - } - case CEXPR_PREFIX: - { - return csema_prefix_expr(sema, expr, alloc); - } - case CEXPR_INFIX: - { - return csema_infix_expr(sema, expr, alloc); - } - case CEXPR_POSTFIX: - { - return csema_postfix_expr(sema, expr, alloc); - } + cvisit_type(passes, sema, &expr->lambda_.type); + cvisit_stmt(passes, sema, expr->lambda_.body); + break; + default: - panic$("unhandled expr type: {}", expr.type); + panic$("Unknow expr type"); + break; } + + cvisit_after(passes, sema, CVISIT_EXPR, expr); } -/* for the moment statement don't have type maybe later */ -CStmt csema_stmt(CSema *sema, CStmt stmt, Alloc *alloc) +void cvisit_stmt(CVisites passes, CSema *sema, CStmt *stmt) { - switch (stmt.type) + cvisit_before(passes, sema, CVISIT_STMT, stmt); + + switch (stmt->type) { case CSTMT_DECL: - { - *stmt.decl_.decl = csema_decl(sema, *stmt.decl_.decl, alloc); + cvisit_decl(passes, sema, stmt->decl_.decl); break; - } + case CSTMT_EXPR: - { - stmt.expr_.expr = csema_expr(sema, stmt.expr_.expr, alloc); + cvisit_expr(passes, sema, &stmt->expr_.expr); break; - } + case CSTMT_BLOCK: - { - csema_scope_enter(sema); - vec_foreach(sub_stmt, &stmt.block_.stmts) + vec_foreach(v, &stmt->block_.stmts) { - *sub_stmt = csema_stmt(sema, *sub_stmt, alloc); + cvisit_stmt(passes, sema, v); } - csema_scope_leave(sema); break; - } + case CSTMT_IF: - { - stmt.if_.expr = csema_expr(sema, stmt.if_.expr, alloc); + cvisit_expr(passes, sema, &stmt->if_.expr); + cvisit_stmt(passes, sema, stmt->if_.stmt_true); + cvisit_stmt(passes, sema, stmt->if_.stmt_false); break; - } + case CSTMT_FOR: - { - *stmt.for_.init_stmt = csema_stmt(sema, *stmt.for_.init_stmt, alloc); - stmt.for_.cond_expr = csema_expr(sema, stmt.for_.cond_expr, alloc); - stmt.for_.iter_expr = csema_expr(sema, stmt.for_.iter_expr, alloc); - *stmt.for_.stmt = csema_stmt(sema, *stmt.for_.stmt, alloc); + cvisit_stmt(passes, sema, stmt->for_.init_stmt); + cvisit_expr(passes, sema, &stmt->for_.cond_expr); + cvisit_expr(passes, sema, &stmt->for_.iter_expr); + + cvisit_stmt(passes, sema, stmt->for_.stmt); break; - } + case CSTMT_WHILE: case CSTMT_DO: case CSTMT_SWITCH: - { - *stmt.switch_.stmt = csema_stmt(sema, *stmt.switch_.stmt, alloc); - stmt.switch_.expr = csema_expr(sema, stmt.switch_.expr, alloc); + cvisit_expr(passes, sema, &stmt->while_.expr); + cvisit_stmt(passes, sema, stmt->while_.stmt); break; - } - case CSTMT_BREAK: - case CSTMT_CONTINUE: - case CSTMT_LABEL: - case CSTMT_DEFAULT: - case CSTMT_GOTO: - { + + case CSTMT_RETURN: + cvisit_expr(passes, sema, &stmt->return_.expr); break; - } + case CSTMT_CASE: - { - stmt.case_.expr = csema_expr(sema, stmt.case_.expr, alloc); + cvisit_expr(passes, sema, &stmt->case_.expr); break; - } - case CSTMT_RETURN: - { - stmt.return_.expr = csema_expr(sema, stmt.return_.expr, alloc); - break; - } + default: - { + panic$("Unknow stmt type"); break; } - } - return stmt; + + cvisit_after(passes, sema, CVISIT_STMT, stmt); } -CDecl csema_decl(CSema *sema, CDecl decl, Alloc *alloc) +void cvisit_type(CVisites passes, CSema *sema, CType *type) { - switch (decl.type) + cvisit_before(passes, sema, CVISIT_TYPE, type); + + switch (type->type) { - case CDECL_TYPE: + case CTYPE_ARRAY: { - decl.sema_type = decl.type_.type; + cvisit_type(passes, sema, type->array_.subtype); break; } - case CDECL_FUNC: - { - decl.sema_type = decl.func_.type; - csema_scope_add(sema, decl); /* we need to add the function to the scope before the body lookup because the body can call the function it self */ - csema_scope_enter_func(sema, decl.func_.type); - decl.func_.body = csema_stmt(sema, decl.func_.body, alloc); - csema_scope_leave(sema); + case CTYPE_PTR: + case CTYPE_REF: + case CTYPE_PARENT: + cvisit_type(passes, sema, type->ref_.subtype); break; - } - case CDECL_VAR: - { - decl.sema_type = decl.var_.type; - decl.var_.expr = csema_expr(sema, decl.var_.expr, alloc); - csema_scope_add(sema, decl); + + case CTYPE_STRUCT: + case CTYPE_UNION: + vec_foreach(member, &type->struct_.members) + { + cvisit_type(passes, sema, &member->type); + } break; - } + + case CTYPE_FUNC: + cvisit_type(passes, sema, type->func_.ret); + + vec_foreach(func_arg, &type->func_.params) + { + cvisit_type(passes, sema, &func_arg->type); + } + break; + default: - { - decl.sema_type = ctype_void(); + panic$("Unknow type type"); break; } - } - return decl; + cvisit_after(passes, sema, CVISIT_TYPE, type); } -CUnit csema_unit(CSema *sema, CUnit unit, Alloc *alloc) +void cvisit_unit(CVisites passes, CSema *sema, CUnit *unit) { - CUnit result = cunit(alloc); + cvisit_before(passes, sema, CVISIT_UNIT, unit); - csema_scope_enter(sema); - - vec_foreach(entry, &unit.units) + vec_foreach(entry, &unit->units) { if (entry->type == CUNIT_DECLARATION) { - cunit_decl(&result, csema_decl(sema, entry->_decl, alloc)); + cvisit_decl(passes, sema, &entry->_decl); } } - csema_scope_leave(sema); - - return result; + cvisit_after(passes, sema, CVISIT_UNIT, unit); } diff --git a/sources/libs/cc/sema/visit.h b/sources/libs/cc/sema/visit.h index 6caabfd10..bb486aac3 100644 --- a/sources/libs/cc/sema/visit.h +++ b/sources/libs/cc/sema/visit.h @@ -1,16 +1,39 @@ #pragma once -#include +#include #include -CVal csema_value(CSema *sema, CVal value, Alloc *alloc); +#define CVISIT_DECL (1 << 0) +#define CVISIT_EXPR (1 << 1) +#define CVISIT_STMT (1 << 2) +#define CVISIT_TYPE (1 << 3) +#define CVISIT_UNIT (1 << 4) -CType csema_type(CSema *sema, CType type, Alloc *alloc); +#define CVISIT_BEGIN (1 << 5) +#define CVISIT_END (1 << 6) -CExpr csema_expr(CSema *sema, CExpr expr, Alloc *alloc); +typedef void CVisitFn(CSema *ctx, int what, void *node); -CStmt csema_stmt(CSema *sema, CStmt stmt, Alloc *alloc); +typedef struct +{ + CVisitFn *fn; + int what; +} CVisit; -CDecl csema_decl(CSema *sema, CDecl decl, Alloc *alloc); +typedef Slice(CVisit) CVisites; -CUnit csema_unit(CSema *sema, CUnit unit, Alloc *alloc); +bool cvisit_node_is(void *node, int what, int type); + +void cvisit_before(CVisites passes, CSema *ctx, int what, void *node); + +void cvisit_after(CVisites passes, CSema *ctx, int what, void *node); + +void cvisit_decl(CVisites passes, CSema *ctx, CDecl *decl); + +void cvisit_expr(CVisites passes, CSema *ctx, CExpr *expr); + +void cvisit_stmt(CVisites passes, CSema *ctx, CStmt *stmt); + +void cvisit_type(CVisites passes, CSema *ctx, CType *type); + +void cvisit_unit(CVisites passes, CSema *ctx, CUnit *unit); diff --git a/sources/libs/cc/trans.h b/sources/libs/cc/trans.h index a9b38f6c1..3608a2cd7 100644 --- a/sources/libs/cc/trans.h +++ b/sources/libs/cc/trans.h @@ -3,20 +3,22 @@ #include #include -void cc_trans_decl(Emit *emit, CDecl decl); +void ctrans_decl(Emit *emit, CDecl decl); -void cc_trans_expr(Emit *emit, CExpr expr); +void ctrans_expr(Emit *emit, CExpr expr); -void cc_trans_stmt(Emit *emit, CStmt stmt); +void ctrans_stmt(Emit *emit, CStmt stmt); -void cc_trans_type(Emit *emit, CType type); +void ctrans_type(Emit *emit, CType type); -void cc_trans_type_start(Emit *emit, CType type); +void ctrans_type_start(Emit *emit, CType type); -void cc_trans_func_params(Emit *emit, CType type); +void ctrans_func_params(Emit *emit, CType type); -void cc_trans_type_end(Emit *emit, CType type); +void ctrans_type_end(Emit *emit, CType type); -void cc_trans_unit(Emit *emit, CUnit unit); +Str ctrans_type_str(CExpr expr, Alloc *alloc); -void cc_trans_value(Emit *emit, CVal value); +void ctrans_unit(Emit *emit, CUnit unit); + +void ctrans_value(Emit *emit, CVal value); diff --git a/sources/libs/cc/trans/decl.c b/sources/libs/cc/trans/decl.c index bc023c9ec..b0397ed28 100644 --- a/sources/libs/cc/trans/decl.c +++ b/sources/libs/cc/trans/decl.c @@ -1,6 +1,6 @@ #include -static void cc_trans_decl_attr(Emit *emit, CDeclAttr attr) +static void ctrans_decl_attr(Emit *emit, CDeclAttr attr) { if (attr & CDECL_AUTO) { @@ -33,9 +33,9 @@ static void cc_trans_decl_attr(Emit *emit, CDeclAttr attr) } } -void cc_trans_decl(Emit *emit, CDecl decl) +void ctrans_decl(Emit *emit, CDecl decl) { - cc_trans_decl_attr(emit, decl.attr); + ctrans_decl_attr(emit, decl.attr); if (decl.type == CDECL_TYPE) { @@ -46,26 +46,26 @@ void cc_trans_decl(Emit *emit, CDecl decl) emit_fmt$(emit, "typedef "); } - cc_trans_type_start(emit, decl.type_.type); + ctrans_type_start(emit, decl.type_.type); if (is_typedef) { emit_fmt$(emit, " {}", decl.name); } - cc_trans_type_end(emit, decl.type_.type); + ctrans_type_end(emit, decl.type_.type); } else if (decl.type == CDECL_VAR) { - cc_trans_type_start(emit, decl.var_.type); - emit_fmt$(emit, " {} ", decl.name); - cc_trans_type_end(emit, decl.var_.type); + ctrans_type_start(emit, decl.var_.type); + emit_fmt(emit, " {} ", decl.name); + ctrans_type_end(emit, decl.var_.type); if (decl.var_.expr.type != CEXPR_INVALID && decl.var_.expr.type != CEXPR_EMPTY) { - emit_fmt$(emit, "="); - cc_trans_expr(emit, decl.var_.expr); + emit_fmt(emit, "="); + ctrans_expr(emit, decl.var_.expr); } } else if (decl.type == CDECL_FUNC) @@ -73,15 +73,15 @@ void cc_trans_decl(Emit *emit, CDecl decl) CType func_type = decl.func_.type; // Declarator - cc_trans_type_start(emit, func_type); - emit_fmt$(emit, " {}", decl.name); - cc_trans_type_end(emit, func_type); + ctrans_type_start(emit, func_type); + emit_fmt(emit, " {}", decl.name); + ctrans_type_end(emit, func_type); // Body if (decl.func_.body.type != CSTMT_EMPTY) { - emit_fmt$(emit, "\n", decl.name); - cc_trans_stmt(emit, decl.func_.body); + emit_fmt(emit, "\n", decl.name); + ctrans_stmt(emit, decl.func_.body); } else { diff --git a/sources/libs/cc/trans/expr.c b/sources/libs/cc/trans/expr.c index 4755d4706..69781cb0f 100644 --- a/sources/libs/cc/trans/expr.c +++ b/sources/libs/cc/trans/expr.c @@ -1,12 +1,12 @@ #include #include -static void cc_trans_op_fix(Emit *emit, COp op) +static void ctrans_op_fix(Emit *emit, COp op) { emit_fmt$(emit, "{}", cop_to_str(op)); } -static void cc_trans_expr_pre(Emit *emit, CExpr expr, int parent_pre) +static void ctrans_expr_pre(Emit *emit, CExpr expr, int parent_pre) { int pre = cexpr_pre(&expr); @@ -21,7 +21,7 @@ static void cc_trans_expr_pre(Emit *emit, CExpr expr, int parent_pre) break; case CEXPR_CONSTANT: - cc_trans_value(emit, expr.constant_); + ctrans_value(emit, expr.constant_); break; case CEXPR_IDENT: @@ -32,26 +32,26 @@ static void cc_trans_expr_pre(Emit *emit, CExpr expr, int parent_pre) break; case CEXPR_PREFIX: - cc_trans_op_fix(emit, expr.prefix_.op); + ctrans_op_fix(emit, expr.prefix_.op); if (expr.prefix_.expr != nullptr) { - cc_trans_expr_pre(emit, *expr.prefix_.expr, pre); + ctrans_expr_pre(emit, *expr.prefix_.expr, pre); } break; case CEXPR_POSTFIX: if (expr.postfix_.expr != nullptr) { - cc_trans_expr_pre(emit, *expr.postfix_.expr, pre); + ctrans_expr_pre(emit, *expr.postfix_.expr, pre); } - cc_trans_op_fix(emit, expr.postfix_.op); + ctrans_op_fix(emit, expr.postfix_.op); break; case CEXPR_INFIX: - cc_trans_expr_pre(emit, *expr.infix_.lhs, pre); - cc_trans_op_fix(emit, expr.infix_.op); - cc_trans_expr_pre(emit, *expr.infix_.rhs, pre); + ctrans_expr_pre(emit, *expr.infix_.lhs, pre); + ctrans_op_fix(emit, expr.infix_.op); + ctrans_expr_pre(emit, *expr.infix_.rhs, pre); if (expr.infix_.op == COP_INDEX) { emit_fmt$(emit, "]"); @@ -59,8 +59,8 @@ static void cc_trans_expr_pre(Emit *emit, CExpr expr, int parent_pre) break; case CEXPR_CALL: - cc_trans_expr_pre(emit, *expr.call_.expr, pre); - emit_fmt$(emit, "("); + ctrans_expr_pre(emit, *expr.call_.expr, pre); + emit_fmt(emit, "("); bool first = true; vec_foreach_v(v, &expr.call_.args) { @@ -73,38 +73,38 @@ static void cc_trans_expr_pre(Emit *emit, CExpr expr, int parent_pre) emit_fmt$(emit, ", "); } - cc_trans_expr_pre(emit, v, CEXPR_MAX_PRECEDENCE); + ctrans_expr_pre(emit, v, CEXPR_MAX_PRECEDENCE); } emit_fmt$(emit, ")"); break; case CEXPR_CAST: - emit_fmt$(emit, "("); - cc_trans_type(emit, expr.cast_.type); - emit_fmt$(emit, ")"); - cc_trans_expr_pre(emit, *expr.cast_.expr, pre); + emit_fmt(emit, "("); + ctrans_type(emit, expr.cast_.type); + emit_fmt(emit, ")"); + ctrans_expr_pre(emit, *expr.cast_.expr, pre); break; case CEXPR_TERNARY: - cc_trans_expr_pre(emit, *expr.ternary_.expr_cond, pre); - emit_fmt$(emit, " ? "); - cc_trans_expr_pre(emit, *expr.ternary_.expr_true, pre); - emit_fmt$(emit, " : "); - cc_trans_expr_pre(emit, *expr.ternary_.expr_false, pre); + ctrans_expr_pre(emit, *expr.ternary_.expr_cond, pre); + emit_fmt(emit, " ? "); + ctrans_expr_pre(emit, *expr.ternary_.expr_true, pre); + emit_fmt(emit, " : "); + ctrans_expr_pre(emit, *expr.ternary_.expr_false, pre); break; case CEXPR_INITIALIZER: - emit_fmt$(emit, "("); - cc_trans_type(emit, expr.initializer_.type); - emit_fmt$(emit, ")"); + emit_fmt(emit, "("); + ctrans_type(emit, expr.initializer_.type); + emit_fmt(emit, ")"); emit_fmt$(emit, "{{\n"); emit_ident(emit); vec_foreach_v(v, &expr.initializer_.initializer) { - cc_trans_expr_pre(emit, v, CEXPR_MAX_PRECEDENCE); - emit_fmt$(emit, ",\n"); + ctrans_expr_pre(emit, v, CEXPR_MAX_PRECEDENCE); + emit_fmt(emit, ",\n"); } emit_deident(emit); @@ -112,9 +112,9 @@ static void cc_trans_expr_pre(Emit *emit, CExpr expr, int parent_pre) break; case CEXPR_LAMBDA: - emit_fmt$(emit, "[]"); - cc_trans_func_params(emit, expr.lambda_.type); - cc_trans_stmt(emit, *expr.lambda_.body); + emit_fmt(emit, "[]"); + ctrans_func_params(emit, expr.lambda_.type); + ctrans_stmt(emit, *expr.lambda_.body); break; default: @@ -127,7 +127,7 @@ static void cc_trans_expr_pre(Emit *emit, CExpr expr, int parent_pre) } } -void cc_trans_expr(Emit *emit, CExpr expr) +void ctrans_expr(Emit *emit, CExpr expr) { - cc_trans_expr_pre(emit, expr, CEXPR_MAX_PRECEDENCE); + ctrans_expr_pre(emit, expr, CEXPR_MAX_PRECEDENCE); } diff --git a/sources/libs/cc/trans/stmt.c b/sources/libs/cc/trans/stmt.c index cc136152e..901dcc2fb 100644 --- a/sources/libs/cc/trans/stmt.c +++ b/sources/libs/cc/trans/stmt.c @@ -1,6 +1,6 @@ #include -static bool cc_trans_should_stmt_endline(CStmtType type) +static bool ctrans_should_stmt_endline(CStmtType type) { if (type == CSTMT_BLOCK || type == CSTMT_DEFAULT || @@ -13,7 +13,7 @@ static bool cc_trans_should_stmt_endline(CStmtType type) return true; } -void cc_trans_stmt(Emit *emit, CStmt stmt) +void ctrans_stmt(Emit *emit, CStmt stmt) { switch (stmt.type) { @@ -21,11 +21,11 @@ void cc_trans_stmt(Emit *emit, CStmt stmt) break; case CSTMT_DECL: - cc_trans_decl(emit, *stmt.decl_.decl); + ctrans_decl(emit, *stmt.decl_.decl); return; case CSTMT_EXPR: - cc_trans_expr(emit, stmt.expr_.expr); + ctrans_expr(emit, stmt.expr_.expr); return; case CSTMT_BLOCK: @@ -34,8 +34,8 @@ void cc_trans_stmt(Emit *emit, CStmt stmt) vec_foreach_v(v, &stmt.block_.stmts) { - cc_trans_stmt(emit, v); - if (cc_trans_should_stmt_endline(v.type)) + ctrans_stmt(emit, v); + if (ctrans_should_stmt_endline(v.type)) { emit_fmt$(emit, ";"); } @@ -49,18 +49,18 @@ void cc_trans_stmt(Emit *emit, CStmt stmt) case CSTMT_IF: emit_fmt$(emit, "if ("); - cc_trans_expr(emit, stmt.if_.expr); - emit_fmt$(emit, ")\n"); + ctrans_expr(emit, stmt.if_.expr); + emit_fmt(emit, ")\n"); if (stmt.if_.stmt_true->type != CSTMT_BLOCK) { emit_ident(emit); - cc_trans_stmt(emit, *stmt.if_.stmt_true); + ctrans_stmt(emit, *stmt.if_.stmt_true); emit_deident(emit); } else { - cc_trans_stmt(emit, *stmt.if_.stmt_true); + ctrans_stmt(emit, *stmt.if_.stmt_true); } if (stmt.if_.stmt_false->type != CSTMT_EMPTY) @@ -69,58 +69,58 @@ void cc_trans_stmt(Emit *emit, CStmt stmt) if (stmt.if_.stmt_true->type != CSTMT_BLOCK) { emit_ident(emit); - cc_trans_stmt(emit, *stmt.if_.stmt_false); + ctrans_stmt(emit, *stmt.if_.stmt_false); emit_deident(emit); } else { - cc_trans_stmt(emit, *stmt.if_.stmt_false); + ctrans_stmt(emit, *stmt.if_.stmt_false); } } return; case CSTMT_FOR: - emit_fmt$(emit, "for ("); - cc_trans_stmt(emit, *stmt.for_.init_stmt); + emit_fmt(emit, "for ("); + ctrans_stmt(emit, *stmt.for_.init_stmt); - emit_fmt$(emit, "; "); - cc_trans_expr(emit, stmt.for_.cond_expr); + emit_fmt(emit, "; "); + ctrans_expr(emit, stmt.for_.cond_expr); - emit_fmt$(emit, "; "); - cc_trans_expr(emit, stmt.for_.iter_expr); + emit_fmt(emit, "; "); + ctrans_expr(emit, stmt.for_.iter_expr); - emit_fmt$(emit, ")\n"); - cc_trans_stmt(emit, *stmt.for_.stmt); + emit_fmt(emit, ")\n"); + ctrans_stmt(emit, *stmt.for_.stmt); return; case CSTMT_WHILE: - emit_fmt$(emit, "while ("); - cc_trans_expr(emit, stmt.while_.expr); - emit_fmt$(emit, ") \n"); + emit_fmt(emit, "while ("); + ctrans_expr(emit, stmt.while_.expr); + emit_fmt(emit, ") \n"); - cc_trans_stmt(emit, *stmt.while_.stmt); + ctrans_stmt(emit, *stmt.while_.stmt); return; case CSTMT_DO: emit_fmt$(emit, "do \n "); - cc_trans_stmt(emit, *stmt.do_.stmt); + ctrans_stmt(emit, *stmt.do_.stmt); - emit_fmt$(emit, "while ("); - cc_trans_expr(emit, stmt.do_.expr); - emit_fmt$(emit, ") \n"); + emit_fmt(emit, "while ("); + ctrans_expr(emit, stmt.do_.expr); + emit_fmt(emit, ") \n"); return; case CSTMT_SWITCH: - emit_fmt$(emit, "switch ("); - cc_trans_expr(emit, stmt.while_.expr); - emit_fmt$(emit, ")\n"); - cc_trans_stmt(emit, *stmt.while_.stmt); + emit_fmt(emit, "switch ("); + ctrans_expr(emit, stmt.while_.expr); + emit_fmt(emit, ")\n"); + ctrans_stmt(emit, *stmt.while_.stmt); return; case CSTMT_RETURN: - emit_fmt$(emit, "return "); - cc_trans_expr(emit, stmt.return_.expr); + emit_fmt(emit, "return "); + ctrans_expr(emit, stmt.return_.expr); return; case CSTMT_GOTO: @@ -140,9 +140,9 @@ void cc_trans_stmt(Emit *emit, CStmt stmt) return; case CSTMT_CASE: - emit_fmt$(emit, "case "); - cc_trans_expr(emit, stmt.case_.expr); - emit_fmt$(emit, ":"); + emit_fmt(emit, "case "); + ctrans_expr(emit, stmt.case_.expr); + emit_fmt(emit, ":"); return; case CSTMT_DEFAULT: diff --git a/sources/libs/cc/trans/type.c b/sources/libs/cc/trans/type.c index 6d09ced34..38f439019 100644 --- a/sources/libs/cc/trans/type.c +++ b/sources/libs/cc/trans/type.c @@ -1,24 +1,25 @@ +#include #include -static void cc_trans_member(Emit *emit, CTypeMember type) +static void ctrans_member(Emit *emit, CTypeMember type) { - cc_trans_type_start(emit, type.type); - emit_fmt$(emit, " {}", type.name); - cc_trans_type_end(emit, type.type); + ctrans_type_start(emit, type.type); + emit_fmt(emit, " {}", type.name); + ctrans_type_end(emit, type.type); } -static void cc_trans_constant(Emit *emit, CTypeConst member) +static void ctrans_constant(Emit *emit, CTypeConst member) { emit_fmt$(emit, "{}", member.name); if (member.value.type != CVAL_INVALID) { - emit_fmt$(emit, " = "); - cc_trans_value(emit, member.value); + emit_fmt(emit, " = "); + ctrans_value(emit, member.value); } } -static void cc_trans_type_attr(Emit *emit, CTypeAttr attr) +static void ctrans_type_attr(Emit *emit, CTypeAttr attr) { if (attr & CTYPE_CONST) { @@ -36,43 +37,33 @@ static void cc_trans_type_attr(Emit *emit, CTypeAttr attr) } } -void cc_trans_type(Emit *emit, CType type) +void ctrans_type(Emit *emit, CType type) { - cc_trans_type_start(emit, type); - cc_trans_type_end(emit, type); + ctrans_type_start(emit, type); + ctrans_type_end(emit, type); } -void cc_trans_type_start(Emit *emit, CType type) +void ctrans_type_start(Emit *emit, CType type) { if (type.type == CTYPE_NAME) { - emit_fmt$(emit, type.name); - cc_trans_type_attr(emit, type.attr); - } - else if (type.type == CTYPE_SIGNED) - { - emit_fmt(emit, "int{}_t", type.signed_.precision); - cc_trans_type_attr(emit, type.attr); - } - else if (type.type == CTYPE_UNSIGNED) - { - emit_fmt(emit, "uint{}_t", type.signed_.precision); - cc_trans_type_attr(emit, type.attr); + emit_fmt(emit, type.name); + ctrans_type_attr(emit, type.attr); } else if (type.type == CTYPE_PTR) { - cc_trans_type_start(emit, *type.ptr_.subtype); - emit_fmt$(emit, "*"); - cc_trans_type_attr(emit, type.attr); + ctrans_type_start(emit, *type.ptr_.subtype); + emit_fmt(emit, "*"); + ctrans_type_attr(emit, type.attr); } else if (type.type == CTYPE_PARENT) { - cc_trans_type_start(emit, *type.ptr_.subtype); - emit_fmt$(emit, "("); + ctrans_type_start(emit, *type.ptr_.subtype); + emit_fmt(emit, "("); } else if (type.type == CTYPE_FUNC) { - cc_trans_type(emit, *type.func_.ret); // return + ctrans_type(emit, *type.func_.ret); // return } else if ((type.type == CTYPE_STRUCT || type.type == CTYPE_UNION) && type.struct_.members.len != 0) @@ -91,14 +82,14 @@ void cc_trans_type_start(Emit *emit, CType type) emit_fmt$(emit, " {}", type.name); } - cc_trans_type_attr(emit, type.attr); - emit_fmt$(emit, "\n{{\n"); + ctrans_type_attr(emit, type.attr); + emit_fmt(emit, "\n{{\n"); emit_ident(emit); vec_foreach_v(v, &type.struct_.members) { - cc_trans_member(emit, v); - emit_fmt$(emit, ";\n"); + ctrans_member(emit, v); + emit_fmt(emit, ";\n"); } emit_deident(emit); @@ -118,27 +109,27 @@ void cc_trans_type_start(Emit *emit, CType type) vec_foreach_v(v, &type.enum_.constants) { - cc_trans_constant(emit, v); - emit_fmt$(emit, ",\n"); + ctrans_constant(emit, v); + emit_fmt(emit, ",\n"); } emit_deident(emit); emit_fmt$(emit, "}}"); - cc_trans_type_attr(emit, type.attr); + ctrans_type_attr(emit, type.attr); } else if (type.type == CTYPE_ARRAY) { - cc_trans_type_attr(emit, type.attr); - cc_trans_type_start(emit, *type.array_.subtype); + ctrans_type_attr(emit, type.attr); + ctrans_type_start(emit, *type.array_.subtype); } else { - emit_fmt$(emit, ctype_to_str(type.type)); + emit_fmt(emit, ctype_to_str(type)); } } -void cc_trans_func_params(Emit *emit, CType type) +void ctrans_func_params(Emit *emit, CType type) { emit_fmt$(emit, "("); @@ -150,26 +141,26 @@ void cc_trans_func_params(Emit *emit, CType type) emit_fmt$(emit, ", "); } first = false; - cc_trans_member(emit, v); + ctrans_member(emit, v); } emit_fmt$(emit, ")"); } -void cc_trans_type_end(Emit *emit, CType type) +void ctrans_type_end(Emit *emit, CType type) { if (type.type == CTYPE_PTR) { - cc_trans_type_end(emit, *type.ptr_.subtype); + ctrans_type_end(emit, *type.ptr_.subtype); } else if (type.type == CTYPE_PARENT) { - emit_fmt$(emit, ")"); - cc_trans_type_end(emit, *type.ptr_.subtype); + emit_fmt(emit, ")"); + ctrans_type_end(emit, *type.ptr_.subtype); } else if (type.type == CTYPE_FUNC) { - cc_trans_func_params(emit, type); - cc_trans_type_attr(emit, type.attr); + ctrans_func_params(emit, type); + ctrans_type_attr(emit, type.attr); } else if (type.type == CTYPE_ARRAY) { @@ -181,6 +172,19 @@ void cc_trans_type_end(Emit *emit, CType type) { emit_fmt$(emit, "[{}]", type.array_.size); } - cc_trans_type_end(emit, *type.ptr_.subtype); + ctrans_type_end(emit, *type.ptr_.subtype); } } + +Str ctrans_type_str(CExpr expr, Alloc *alloc) +{ + Emit emit; + Buf buf = {}; + buf_init(&buf, 18, alloc); + emit_init(&emit, buf_writer(&buf)); + + ctrans_type(&emit, expr.sema_type); + + emit_deinit(&emit); + return buf_str(&buf); +} diff --git a/sources/libs/cc/trans/unit.c b/sources/libs/cc/trans/unit.c index 1efc66214..921affd91 100644 --- a/sources/libs/cc/trans/unit.c +++ b/sources/libs/cc/trans/unit.c @@ -1,6 +1,6 @@ #include -static void cc_trans_include(Emit *emit, CInclude path) +static void ctrans_include(Emit *emit, CInclude path) { emit_fmt$(emit, "#include "); @@ -14,14 +14,14 @@ static void cc_trans_include(Emit *emit, CInclude path) } } -static void cc_trans_pragma(Emit *emit, CPragma pragma) +static void ctrans_pragma(Emit *emit, CPragma pragma) { emit_fmt$(emit, "#pragma "); emit_fmt$(emit, pragma.text); emit_fmt$(emit, "\n"); } -static void cc_trans_define(Emit *emit, CDefine define) +static void ctrans_define(Emit *emit, CDefine define) { emit_fmt$(emit, "#define "); emit_fmt$(emit, define.name); @@ -43,12 +43,12 @@ static void cc_trans_define(Emit *emit, CDefine define) emit_fmt$(emit, ")"); } - emit_fmt$(emit, " "); - cc_trans_expr(emit, define.expression); - emit_fmt$(emit, "\n"); + emit_fmt(emit, " "); + ctrans_expr(emit, define.expression); + emit_fmt(emit, "\n"); } -void cc_trans_unit(Emit *emit, CUnit unit) +void ctrans_unit(Emit *emit, CUnit unit) { CUnitType prev_type = CUNIT_NONE; @@ -62,17 +62,17 @@ void cc_trans_unit(Emit *emit, CUnit unit) emit_fmt$(emit, "\n"); } - cc_trans_include(emit, entry._include); + ctrans_include(emit, entry._include); break; case CUNIT_PRAGMA: - cc_trans_pragma(emit, entry._pragma); + ctrans_pragma(emit, entry._pragma); break; case CUNIT_DECLARATION: emit_fmt$(emit, "\n"); - cc_trans_decl(emit, entry._decl); + ctrans_decl(emit, entry._decl); if (entry._decl.type != CDECL_FUNC) { @@ -88,7 +88,7 @@ void cc_trans_unit(Emit *emit, CUnit unit) emit_fmt$(emit, "\n"); } - cc_trans_define(emit, entry._define); + ctrans_define(emit, entry._define); break; default: diff --git a/sources/libs/cc/trans/val.c b/sources/libs/cc/trans/val.c index ab69c9ce4..23fe7faa0 100644 --- a/sources/libs/cc/trans/val.c +++ b/sources/libs/cc/trans/val.c @@ -1,7 +1,7 @@ #include #include -void cc_trans_value(Emit *emit, CVal value) +void ctrans_value(Emit *emit, CVal value) { switch (value.type) { @@ -18,7 +18,7 @@ void cc_trans_value(Emit *emit, CVal value) break; case CVAL_STRING: - emit_fmt$(emit, "\"{}\"", value.string_); + emit_fmt(emit, "\"{}\"", value.str_); break; default: diff --git a/sources/libs/codec-ssfn2/ssfn2.c b/sources/libs/codec-ssfn2/ssfn2.c index 704fbbdd8..848656fef 100644 --- a/sources/libs/codec-ssfn2/ssfn2.c +++ b/sources/libs/codec-ssfn2/ssfn2.c @@ -4,7 +4,7 @@ #include "ssfn2.h" -static MaybeError ssfn2_load_string(IoReader reader, char *dst) +static MaybeError ssfn2_load_str(IoReader reader, char *dst) { for (size_t i = 0; i < SSFN2_MAX_STR_LEN; i++) { @@ -21,14 +21,14 @@ static MaybeError ssfn2_load_string(IoReader reader, char *dst) return SUCCESS; } -static MaybeError ssfn2_load_stringtable(IoReader reader, SSFN2Font *font) +static MaybeError ssfn2_load_strtable(IoReader reader, SSFN2Font *font) { - TRY(MaybeError, ssfn2_load_string(reader, font->stringtable.font_name)); - TRY(MaybeError, ssfn2_load_string(reader, font->stringtable.family_name)); - TRY(MaybeError, ssfn2_load_string(reader, font->stringtable.subfamily_name)); - TRY(MaybeError, ssfn2_load_string(reader, font->stringtable.revision)); - TRY(MaybeError, ssfn2_load_string(reader, font->stringtable.manufacturer)); - TRY(MaybeError, ssfn2_load_string(reader, font->stringtable.license)); + TRY(MaybeError, ssfn2_load_str(reader, font->strtable.font_name)); + TRY(MaybeError, ssfn2_load_str(reader, font->strtable.family_name)); + TRY(MaybeError, ssfn2_load_str(reader, font->strtable.subfamily_name)); + TRY(MaybeError, ssfn2_load_str(reader, font->strtable.revision)); + TRY(MaybeError, ssfn2_load_str(reader, font->strtable.manufacturer)); + TRY(MaybeError, ssfn2_load_str(reader, font->strtable.license)); return SUCCESS; } @@ -269,7 +269,7 @@ static void ssfn2_compute_weight(SSFN2Font *font) { font->weight = GFX_FONT_REGULAR; - Str subfamily = str$(font->stringtable.subfamily_name); + Str subfamily = str$(font->strtable.subfamily_name); // Weights if (str_first(subfamily, str$("Black")) >= 0) font->weight = GFX_FONT_BLACK; @@ -312,8 +312,9 @@ static MaybeError ssfn2_load_internal(IoRSeek rseek, SSFN2Collection *collection { SSFN2Font font; font.font_start = start; + TRY(MaybeError, io_read$(rseek, (uint8_t *)&font.header, sizeof(SSFN2FontHeader))); - TRY(MaybeError, ssfn2_load_stringtable(io_reader$(rseek), &font)); + TRY(MaybeError, ssfn2_load_strtable(io_reader$(rseek), &font)); font.glyphs = alloc_make_array(alloc, SSFN2Glyph, 0x110000); diff --git a/sources/libs/codec-ssfn2/ssfn2.h b/sources/libs/codec-ssfn2/ssfn2.h index e87492065..814bf0b64 100644 --- a/sources/libs/codec-ssfn2/ssfn2.h +++ b/sources/libs/codec-ssfn2/ssfn2.h @@ -19,7 +19,7 @@ typedef struct size_t font_start; float weight; SSFN2FontHeader header; - SSFN2StringTable stringtable; + SSFN2StringTable strtable; SSFN2Glyph *glyphs; /* glyphs array */ } SSFN2Font; diff --git a/sources/libs/elf/elf.c b/sources/libs/elf/elf.c index 091881461..71e3f6792 100644 --- a/sources/libs/elf/elf.c +++ b/sources/libs/elf/elf.c @@ -8,7 +8,7 @@ bool elf_validate(Elf64Header const *header) header->ident.magics[3] == 'F'; } -Str elf_string(Elf64Header const *header, size_t offset) +Str elf_str(Elf64Header const *header, size_t offset) { Elf64SectionHeader const *strings = elf_section_by_index(header, header->strings_section_index); @@ -57,7 +57,7 @@ Elf64SectionHeader const *elf_section_by_name(Elf64Header const *header, Str nam for (size_t i = 0; i < header->sections_count; i++) { - Str section_name = elf_string(header, section->name); + Str section_name = elf_str(header, section->name); if (str_eq(section_name, name)) { diff --git a/sources/libs/elf/elf.h b/sources/libs/elf/elf.h index 4c75c70c3..ece8d8888 100644 --- a/sources/libs/elf/elf.h +++ b/sources/libs/elf/elf.h @@ -114,7 +114,7 @@ typedef struct PACKED bool elf_validate(Elf64Header const *header); -Str elf_string(Elf64Header const *header, size_t offset); +Str elf_str(Elf64Header const *header, size_t offset); Str elf_manifest(Elf64Header const *header); diff --git a/sources/libs/hw/fdt/fdt.c b/sources/libs/hw/fdt/fdt.c index b98e4ad14..97b73fb3d 100644 --- a/sources/libs/hw/fdt/fdt.c +++ b/sources/libs/hw/fdt/fdt.c @@ -43,7 +43,7 @@ static FdtNode fdt_tok2node(FdtTokBegin *raw_node, FdtHeader *header) static FdtProp fdt_tok2prop(FdtTokProp *raw_prop, FdtHeader *header) { - Str node_name = str$((char const *)((uintptr_t)header + be_load$(header->strings_offset) + be_load$(raw_prop->name_offset))); + Str node_name = str$((char const *)((uintptr_t)header + load_be(header->strs_offset) + load_be(raw_prop->name_offset))); return (FdtProp){ .name = node_name, @@ -271,8 +271,8 @@ static void fdt_dump_props_value(FdtProp *prop, Emit *emit) } if (prop->value.len > 2) { - Str as_string = str_n$(prop->value.len, (char *)prop->value.buf); - emit_fmt$(emit, "- as string: {} \n", as_string); + Str as_str = str_n$(prop->value.len, (char *)prop->value.buf); + emit_fmt$(emit, "- as string: {} \n", as_str); } emit_fmt$(emit, "- as data: "); diff --git a/sources/libs/hw/fdt/fdt.h b/sources/libs/hw/fdt/fdt.h index c77451bd8..de09ca300 100644 --- a/sources/libs/hw/fdt/fdt.h +++ b/sources/libs/hw/fdt/fdt.h @@ -7,12 +7,12 @@ typedef struct PACKED be_uint32_t magic; be_uint32_t size; be_uint32_t structure_offset; - be_uint32_t strings_offset; + be_uint32_t strs_offset; be_uint32_t memory_reservation_offset; be_uint32_t version; be_uint32_t last_comp_version; be_uint32_t boot_cpuid_phys; - be_uint32_t strings_size; + be_uint32_t strs_size; be_uint32_t structure_size; } FdtHeader; diff --git a/sources/libs/json/emit.c b/sources/libs/json/emit.c index bce54933a..64c36e34b 100644 --- a/sources/libs/json/emit.c +++ b/sources/libs/json/emit.c @@ -1,7 +1,7 @@ #include #include -void json_emit_string(Str str, Emit *emit) +void json_emit_str(Str str, Emit *emit) { emit_fmt$(emit, "\""); @@ -75,8 +75,8 @@ void json_emit(Json const json, Emit *emit) } emit_fmt$(emit, "\n"); - json_emit_string(k, emit); - emit_fmt$(emit, ": "); + json_emit_str(k, emit); + emit_fmt(emit, ": "); json_emit(v, emit); first = false; @@ -93,7 +93,7 @@ void json_emit(Json const json, Emit *emit) break; case JSON_STRING: - json_emit_string(json.string, emit); + json_emit_str(json.str, emit); break; case JSON_NUMBER: diff --git a/sources/libs/json/objects.c b/sources/libs/json/objects.c index 16b45005d..ae9ae66af 100644 --- a/sources/libs/json/objects.c +++ b/sources/libs/json/objects.c @@ -109,6 +109,6 @@ Json json_str(Str str) { return (Json){ .type = JSON_STRING, - .string = str, + .str = str, }; } diff --git a/sources/libs/json/objects.h b/sources/libs/json/objects.h index 5b4cc2111..b52ff7c7a 100644 --- a/sources/libs/json/objects.h +++ b/sources/libs/json/objects.h @@ -5,12 +5,14 @@ typedef enum { JSON_NULL, + JSON_TRUE, JSON_FALSE, JSON_ARRAY, JSON_OBJECT, JSON_STRING, JSON_NUMBER, + JSON_ERROR } JsonType; @@ -27,7 +29,7 @@ struct _Json { JsonObj object; JsonArr array; - Str string; + Str str; long number; }; }; diff --git a/sources/libs/json/parser.c b/sources/libs/json/parser.c index 9a242bdd7..70337c217 100644 --- a/sources/libs/json/parser.c +++ b/sources/libs/json/parser.c @@ -1,6 +1,6 @@ -#include #include #include +#include static bool json_skip_comment(Scan *scan) { @@ -28,7 +28,7 @@ static void json_skip_space_and_comment(Scan *scan) ; } -static int is_closing_string(int c) +static int is_closing_str(int c) { return c != '"'; } @@ -37,7 +37,7 @@ static Str parse_str(Scan *scan) { json_skip_space_and_comment(scan); scan_expect(scan, '"'); - Str name = scan_eat_match(scan, is_closing_string); + Str name = scan_skip_until(scan, is_closing_str); scan_expect(scan, '"'); json_skip_space_and_comment(scan); @@ -108,7 +108,7 @@ Json json_parse(Scan *scan, Alloc *alloc) { json_skip_space_and_comment(scan); - if (scan_curr(scan) == '"') // string + if (scan_curr(scan) == '"') { return json_str(parse_str(scan)); } diff --git a/sources/libs/stdc-ansi/stdlib.h b/sources/libs/stdc-ansi/stdlib.h index a1fc972e8..92e45cb8a 100644 --- a/sources/libs/stdc-ansi/stdlib.h +++ b/sources/libs/stdc-ansi/stdlib.h @@ -91,7 +91,7 @@ int at_quick_exit(void (*func)(void)); _Noreturn void exit(int status); -_Noreturn void _Exit(int status); +_Noreturn void _exit(int status); char *getenv(char const *name); diff --git a/sources/loader/config.c b/sources/loader/config.c index be65d4268..ac3d714d6 100644 --- a/sources/loader/config.c +++ b/sources/loader/config.c @@ -7,8 +7,8 @@ LoaderEntry config_entry_parse(Json json) { LoaderEntry entry; - entry.name = json_get(json, str$("name")).string; - entry.kernel = json_get(json, str$("kernel")).string; + entry.name = json_get(json, str$("name")).str; + entry.kernel = json_get(json, str$("kernel")).str; Json modules; if (json_try_get(json, str$("modules"), &modules)) @@ -20,8 +20,8 @@ LoaderEntry config_entry_parse(Json json) Json module = json_at(modules, i); LoaderModule current_module = {}; - current_module.name = json_get(module, str$("name")).string; - current_module.path = json_get(module, str$("path")).string; + current_module.name = json_get(module, str$("name")).str; + current_module.path = json_get(module, str$("path")).str; vec_push(&entry.modules, current_module); diff --git a/sources/protos/window.idl b/sources/protos/window.idl index 94d1c3d3a..faf302d51 100644 --- a/sources/protos/window.idl +++ b/sources/protos/window.idl @@ -8,6 +8,13 @@ type WindowClientProps = UiWinType type, }; +type WmLayout = enum +{ + WM_LAYOUT_STACK, + WM_LAYOUT_FIBONACCI, + WM_LAYOUT_MONOCLE, +}; + interface Client { method show -> Bool; diff --git a/sources/utils/cc/main.c b/sources/utils/cc/main.c index 8f1c056dd..5e78ea79b 100644 --- a/sources/utils/cc/main.c +++ b/sources/utils/cc/main.c @@ -238,10 +238,10 @@ int main(int argc, char const *argv[]) emit_ident_size(&emit, 4); - emit_fmt$(&emit, "--- BEGIN CODE ---\n"); - cc_trans_unit(&emit, unit); - emit_fmt$(&emit, "--- END CODE ---\n"); - emit_fmt$(&emit, "\n"); + emit_fmt(&emit, "--- BEGIN CODE ---\n"); + ctrans_unit(&emit, unit); + emit_fmt(&emit, "--- END CODE ---\n"); + emit_fmt(&emit, "\n"); heap_alloc_deinit(&heap); diff --git a/sources/utils/fontinfo/main.c b/sources/utils/fontinfo/main.c index f462ad71d..21c3e04da 100644 --- a/sources/utils/fontinfo/main.c +++ b/sources/utils/fontinfo/main.c @@ -27,9 +27,9 @@ int main(int argc, char const *argv[]) vec_foreach(font, &ssfn_coll) { log$(""); - log$("[{}] Name: {}", idx, font->stringtable.font_name); - log$("[{}] Family: {}", idx, font->stringtable.family_name); - log$("[{}] Subfamily: {}", idx, font->stringtable.subfamily_name); + log$("[{}] Name: {}", idx, font->strtable.font_name); + log$("[{}] Family: {}", idx, font->strtable.family_name); + log$("[{}] Subfamily: {}", idx, font->strtable.subfamily_name); idx++; } diff --git a/sources/utils/idl/main.c b/sources/utils/idl/main.c index 5ebcdb2ae..409a89c12 100644 --- a/sources/utils/idl/main.c +++ b/sources/utils/idl/main.c @@ -56,12 +56,12 @@ int main(int argc, char const *argv[]) else if (str_eq(str$("--source"), str$(argv[2]))) { CUnit unit = idl_cgen_source(module, base$(&heap)); - cc_trans_unit(&emit, unit); + ctrans_unit(&emit, unit); } else if (str_eq(str$("--header"), str$(argv[2]))) { CUnit unit = idl_cgen_header(module, base$(&heap)); - cc_trans_unit(&emit, unit); + ctrans_unit(&emit, unit); } else { diff --git a/sources/utils/test/json/json.c b/sources/utils/test/json/json.c index a701539d4..72ed808e3 100644 --- a/sources/utils/test/json/json.c +++ b/sources/utils/test/json/json.c @@ -11,7 +11,7 @@ test$(json_parse_test) Json res = json_get(json, str$("hello")); - expect_str_equal$(res.string, str$("world")); + assert_str_equal(res.str, str$("world")); } test$(json_parse_int) @@ -57,8 +57,8 @@ test$(json_parse_object_in_object) Json oh = json_get(res, str$("oh")); Json is = json_get(res, str$("is")); - expect_str_equal$(oh.string, str$("rust")); - expect_str_equal$(is.string, str$("based")); + assert_str_equal(oh.str, str$("rust")); + assert_str_equal(is.str, str$("based")); } test$(json_parse_table) @@ -72,10 +72,10 @@ test$(json_parse_table) expect_equal$(res.array.len, 4); - expect_str_equal$(res.array.data[0].string, str$("oh")); - expect_str_equal$(res.array.data[1].string, str$("rust")); - expect$(json_is(res.array.data[2], JSON_OBJECT)); - expect_str_equal$(res.array.data[3].string, str$("cool")); + assert_str_equal(res.array.data[0].str, str$("oh")); + assert_str_equal(res.array.data[1].str, str$("rust")); + assert_truth(json_is(res.array.data[2], JSON_OBJECT)); + assert_str_equal(res.array.data[3].str, str$("cool")); } test$(json_parse_comment_test) @@ -87,5 +87,5 @@ test$(json_parse_comment_test) Json res = json_get(json, str$("hello")); - expect_str_equal$(res.string, str$("world")); + assert_str_equal(res.str, str$("world")); }