From aee7f7551139af26cc15fe593de8825af77dea4f Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 14 Jul 2020 14:50:08 -0400 Subject: formatting cleanup --- cgen.c | 20 +++++++-------- copy.c | 12 ++++----- data_structures.c | 6 ++--- err.c | 14 +++++----- eval.c | 16 ++++++------ foreign64.c | 4 +-- foreign_msvc32.c | 6 ++--- foreign_unix.c | 2 +- infer.c | 5 ++-- instance_table.c | 11 ++++---- main.c | 8 +++--- misc.c | 6 ++--- parse.c | 37 +++++++++++++-------------- tokenizer.c | 10 ++++---- types.c | 76 +++++++++++++++++++++++++++---------------------------- types.h | 33 ++++++++++++------------ win64call_test.c | 54 +++++++++++++++++++-------------------- 17 files changed, 159 insertions(+), 161 deletions(-) diff --git a/cgen.c b/cgen.c index 6d72842..6c03065 100644 --- a/cgen.c +++ b/cgen.c @@ -447,7 +447,7 @@ static void cgen_val_ptr(CGenerator *g, void *v, Type *t) { case BUILTIN_U64: cgen_write(g, U64_FMT, *(U64 *)v); break; case BUILTIN_F32: cgen_write(g, F32_FMT "f", *(F32 *)v); break; case BUILTIN_F64: cgen_write(g, F64_FMT, *(F64 *)v); break; - case BUILTIN_CHAR: + case BUILTIN_CHAR: cgen_write(g, "'"); cgen_char(g, *(char *)v); cgen_write(g, "'"); @@ -487,7 +487,7 @@ static void cgen_fn_params(CGenerator *g, FnExpr *f) { if (type_is_builtin(&d->type, BUILTIN_VARARGS)) { int idx = 0; arr_foreach(d->val.varargs, VarArg, varg) { - if (any_params) + if (any_params) cgen_write(g, ", "); any_params = true; cgen_type_pre(g, varg->type); @@ -500,7 +500,7 @@ static void cgen_fn_params(CGenerator *g, FnExpr *f) { } else { int idx = 0; arr_foreach(d->idents, Identifier, i) { - if (any_params) + if (any_params) cgen_write(g, ", "); any_params = true; Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[idx++] : &d->type; @@ -575,7 +575,7 @@ static void cgen_fn_header(CGenerator *g, FnExpr *f) { cgen_type_pre(g, &f->ret_type); cgen_write(g, " "); } - cgen_fn_name(g, f); + cgen_fn_name(g, f); cgen_fn_params(g, f); if (!out_param) { cgen_type_post(g, &f->ret_type); @@ -601,8 +601,8 @@ static inline void cgen_deferred_up_to_not_including(CGenerator *g, Block *to) { cgen_deferred_from_block(g, b); } -/* - Either set_expr or set_str should be NULL and either to_expr or to_str should be NULL +/* + Either set_expr or set_str should be NULL and either to_expr or to_str should be NULL Also, set_str and/or to_str should be NULL this DOES NOT call cgen_expr_pre for set_expr or to_expr */ @@ -1020,7 +1020,7 @@ static void cgen_expr(CGenerator *g, Expression *e) { s = "/"; break; case BINARY_MOD: s = "%"; break; - case BINARY_SET: + case BINARY_SET: cgen_set(g, lhs, NULL, rhs, NULL); break; case BINARY_GT: @@ -1249,7 +1249,7 @@ static void cgen_expr(CGenerator *g, Expression *e) { cgen_expr(g, e->cast.expr); cgen_write(g, ")"); if (from->kind == TYPE_SLICE // casting from a slice to a non-slice - && to->kind != TYPE_SLICE) + && to->kind != TYPE_SLICE) cgen_write(g, ".data"); cgen_write(g, ")"); } @@ -1983,7 +1983,7 @@ static void cgen_file(CGenerator *g, ParsedFile *f, Typer *tr) { // if in debug mode, don't buffer output C file setbuf(cgen_writing_to(g), NULL); #endif - cgen_write(g, + cgen_write(g, "#include \n" "#include \n" "typedef int8_t i8;\n" @@ -2023,7 +2023,7 @@ static void cgen_file(CGenerator *g, ParsedFile *f, Typer *tr) { cgen_write(g, "struct "); if (!sdef->name) { sdef->c.id = ++g->ident_counter; - } + } cgen_struct_name(g, sdef); cgen_write(g, ";"); cgen_nl(g); diff --git a/copy.c b/copy.c index fad251e..630f556 100644 --- a/copy.c +++ b/copy.c @@ -6,10 +6,10 @@ these copy functions MUST be used before typing!!!! (except for copy_val) - ----- - IMPORTANT: + ----- + IMPORTANT: These functions are like memcpy, in that in and out must not overlap! - ----- + ----- */ typedef struct { @@ -157,7 +157,7 @@ static void copy_type(Copier *c, Type *out, Type *in) { } } break; case TYPE_ARR:{ - ArrType *oarr = out->arr = copier_malloc(c, sizeof *oarr), *iarr = in->arr; + ArrType *oarr = out->arr = copier_malloc(c, sizeof *oarr), *iarr = in->arr; *oarr = *iarr; if (!(in->flags & TYPE_IS_RESOLVED)) { oarr->n_expr = copy_expr_(c, iarr->n_expr); @@ -174,7 +174,7 @@ static void copy_type(Copier *c, Type *out, Type *in) { if (in->flags & TYPE_IS_RESOLVED) { // we don't actually need to make a copy of the struct here } else { - /* + /* it's okay to copy the struct definition here, because before resolving, only one thing can point to a given StructDef */ @@ -292,7 +292,7 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { out->c.code = copy_expr_(c, in->c.code); break; case EXPR_BUILTIN: - out->builtin.which.expr = copy_expr_(c, in->builtin.which.expr); + out->builtin.which.expr = copy_expr_(c, in->builtin.which.expr); break; case EXPR_SLICE: { SliceExpr *sin = &in->slice; diff --git a/data_structures.c b/data_structures.c index e8d0e99..b1d24e3 100644 --- a/data_structures.c +++ b/data_structures.c @@ -1,4 +1,4 @@ -/* +/* Dynamic arrays and hash tables in C This is free and unencumbered software released into the public domain. @@ -54,7 +54,7 @@ static WarnUnusedResult void *arr_resv_(void *arr, size_t n, size_t item_sz) { hdr->cap = (U32)n; hdr = err_realloc(hdr, item_sz * n + sizeof(ArrHeader)); if (hdr->len > hdr->cap) hdr->len = hdr->cap; - } + } return hdr->data; } static WarnUnusedResult void *arr_resva_(void *arr, size_t n, size_t item_sz, Allocator *a) { @@ -205,7 +205,7 @@ static WarnUnusedResult void *arr_copya_(void *out, void *in, size_t item_sz, Al #endif #if HAS_TYPEOF -/* +/* this is to cast the return value of arr_add so that gcc produces a warning if you do something like: float *arr = NULL; diff --git a/err.c b/err.c index bd294d2..000fd5e 100644 --- a/err.c +++ b/err.c @@ -202,9 +202,10 @@ static void err_vprint(Location where, const char *fmt, va_list args) { static void err_print_( #if ERR_SHOW_SOURCE_LOCATION - int line, const char *file, + int line, const char *file, #endif - Location where, const char *fmt, ...) { + Location where, const char *fmt, ...) { + va_list args; #if ERR_SHOW_SOURCE_LOCATION ErrCtx *ctx = where.file->ctx; @@ -236,9 +237,10 @@ static void info_print(Location where, const char *fmt, ...) { static void warn_print_( #if ERR_SHOW_SOURCE_LOCATION - int line, const char *file, + int line, const char *file, #endif - Location where, const char *fmt, ...) { + Location where, const char *fmt, ...) { + va_list args; ErrCtx *ctx = where.file->ctx; if (!ctx->enabled) return; @@ -288,7 +290,7 @@ static void *err_malloc_(size_t size fprintf(stderr, "Error: Out of memory.\n"); exit(EXIT_FAILURE); } -#ifdef MALLOC_TRACKER +#ifdef MALLOC_TRACKER if (streq(file, "eval.c")) { AllocTrackerEntry *entry = &eval_c[line]; entry->line = line; @@ -314,7 +316,7 @@ static void *err_calloc_(size_t n, size_t size fprintf(stderr, "Error: Out of memory.\n"); exit(EXIT_FAILURE); } -#ifdef MALLOC_TRACKER +#ifdef MALLOC_TRACKER if (streq(file, "eval.c")) { AllocTrackerEntry *entry = &eval_c[line]; entry->amount += n * size; diff --git a/eval.c b/eval.c index f0b53b9..04ecdc9 100644 --- a/eval.c +++ b/eval.c @@ -252,7 +252,7 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) { } fprintf(f, "]"); break; - case TYPE_EXPR: + case TYPE_EXPR: assert(0); break; } @@ -741,7 +741,7 @@ static Status eval_ptr_to_struct_field(Evaluator *ev, Expression *dot_expr, void *p = (char *)struc_data + dot_expr->binary.field->offset; } else if (struct_type->kind == TYPE_SLICE) { String member = dot_expr->binary.rhs->ident_str; - void *ptr; + void *ptr; if (is_ptr) { Value v; if (!eval_expr(ev, lhs, &v)) @@ -878,7 +878,7 @@ static void eval_numerical_bin_op(Value lhs, Type *lhs_type, BinaryOp op, Value eval_binary_op_one(u8, U8, op); \ eval_binary_op_one(u16, U16, op); \ eval_binary_op_one(u32, U32, op); \ - eval_binary_op_one(u64, U64, op); + eval_binary_op_one(u64, U64, op); #define eval_binary_op_nums(builtin, op) \ eval_binary_op_ints(builtin, op); \ @@ -1209,7 +1209,7 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) { return false; eval_deref(v, ptr, type); } break; - } + } } break; case EXPR_LITERAL_INT: assert(e->type.kind == TYPE_BUILTIN); @@ -1293,7 +1293,7 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) { Declaration *params = fn->params, *ret_decls = fn->ret_decls; Expression *arg = e->call.arg_exprs; size_t pbytes = arr_len(params) * sizeof(Value); - Value *pvals = + Value *pvals = #if ALLOCA_AVAILABLE toc_alloca(pbytes); #else @@ -1330,7 +1330,7 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) { } size_t dbytes = arr_len(ret_decls) * sizeof(Value); - Value *dvals = + Value *dvals = #if ALLOCA_AVAILABLE toc_alloca(dbytes); #else @@ -1413,7 +1413,7 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) { free(pvals); #endif // remove ret decl values - arr_foreach(ret_decls, Declaration, d) + arr_foreach(ret_decls, Declaration, d) decl_remove_val(d, false); #if !ALLOCA_AVAILABLE free(dvals); @@ -1654,7 +1654,7 @@ static Status eval_stmt(Evaluator *ev, Statement *stmt) { } else { compare_binop = step_is_negative ? BINARY_GT : BINARY_LT; } - if (!(flags & FOR_INCLUDES_FROM)) { + if (!(flags & FOR_INCLUDES_FROM)) { eval_numerical_bin_op(x, value_type, BINARY_ADD, stepval, value_type, &x, value_type); } while (1) { diff --git a/foreign64.c b/foreign64.c index 41d7a19..4156680 100644 --- a/foreign64.c +++ b/foreign64.c @@ -129,11 +129,11 @@ static Status foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *ret_type, switch (ret_type->kind) { case TYPE_BUILTIN: switch (ret_type->builtin) { - case BUILTIN_I8: + case BUILTIN_I8: case BUILTIN_I16: case BUILTIN_I32: case BUILTIN_I64: - case BUILTIN_U8: + case BUILTIN_U8: case BUILTIN_U16: case BUILTIN_U32: case BUILTIN_U64: diff --git a/foreign_msvc32.c b/foreign_msvc32.c index da98f2d..83c7252 100644 --- a/foreign_msvc32.c +++ b/foreign_msvc32.c @@ -56,7 +56,7 @@ static Status val_to_words(Value v, Type *t, Location where, Word *w) { } -/* +/* because of the way the MSVC "cdecl" calling convention works, the only things that affect the way a function is called are the number of arguments and whether the function returns an integer (or pointer), floating-point number, or struct (struct return values are not supported yet). @@ -190,11 +190,11 @@ static Status foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *ret_type, switch (ret_type->kind) { case TYPE_BUILTIN: switch (ret_type->builtin) { - case BUILTIN_I8: + case BUILTIN_I8: case BUILTIN_I16: case BUILTIN_I32: case BUILTIN_I64: - case BUILTIN_U8: + case BUILTIN_U8: case BUILTIN_U16: case BUILTIN_U32: case BUILTIN_U64: diff --git a/foreign_unix.c b/foreign_unix.c index 0e39b95..bb5dfb6 100644 --- a/foreign_unix.c +++ b/foreign_unix.c @@ -37,7 +37,7 @@ static FnPtr foreign_get_fn_ptr(ForeignFnManager *ffmgr, FnExpr *fn, Location ca err_print(call_where, "Could not get function from dynamic library: %s.", name); return NULL; } - fn->foreign.fn_ptr = fn_ptr; + fn->foreign.fn_ptr = fn_ptr; } return fn_ptr; } diff --git a/infer.c b/infer.c index 095c946..bd498f0 100644 --- a/infer.c +++ b/infer.c @@ -121,8 +121,7 @@ static bool infer_from_type(Typer *tr, Type *match, Type *to, Identifier *idents return false; break; case TYPE_STRUCT: - /* this would be difficult because match could contain #ifs and - no sane person will ever write something that needs this */ + // this would be difficult because match could contain #ifs and no one will ever need this break; case TYPE_EXPR: { Type type; @@ -148,7 +147,7 @@ static bool infer_from_type(Typer *tr, Type *match, Type *to, Identifier *idents return true; } -/* +/* match and to are dynamic arrays of equal size idents is a dyn array of distinct identifiers find the value of each ident by matching match[i] to to[i], i = 0..arr_len(match)-1 diff --git a/instance_table.c b/instance_table.c index f40ffeb..254b9c8 100644 --- a/instance_table.c +++ b/instance_table.c @@ -3,9 +3,9 @@ This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever. You should have received a copy of the GNU General Public License along with toc. If not, see . */ -/* - @TODO: better hash functions, especially for integers - (right now, nearby integers are close together in hash +/* + @TODO: better hash functions, especially for integers + (right now, nearby integers are close together in hash space, which is bad with the way these hash tables are designed) */ @@ -237,7 +237,7 @@ static bool val_ptr_eq(void *u, void *v, Type *t) { return false; } return true; - } + } case BUILTIN_TYPE: return type_eq_exact(*(Type **)u, *(Type **)v); case BUILTIN_NMS: @@ -306,8 +306,7 @@ static bool val_eq(Value u, Value v, Type *t) { make sure v's data remains valid */ // @OPTIM: store instances in a block array (remember that the pointers need to stay valid!) -static Instance *instance_table_adda(Allocator *a, HashTable *h, Value v, Type *t, - bool *already_exists) { +static Instance *instance_table_adda(Allocator *a, HashTable *h, Value v, Type *t, bool *already_exists) { if (h->n * 2 >= h->cap) { U64 new_cap = h->cap * 2 + 3; Instance **new_data = allocr_malloc(a, (size_t)new_cap * sizeof *new_data); diff --git a/main.c b/main.c index 8d8be18..0a75e61 100644 --- a/main.c +++ b/main.c @@ -5,7 +5,7 @@ */ /* -see development.md for development information +see development.md for development information @TODO: see todo in test.toc @@ -27,7 +27,7 @@ unions bitwise operations --- #compile_only for functions only used at compile time -if we do bar ::= nms { #include "foo.toc", foo; } ; #include "foo.toc", foo; +if we do bar ::= nms { #include "foo.toc", foo; } ; #include "foo.toc", foo; foo's functions should be defined as foo__etc, not bar__foo__etc warn about x : u8 = 1283; switch to / add as an alternative: libffi @@ -35,7 +35,7 @@ switch to / add as an alternative: libffi don't bother generating ret_ if nothing's deferred X ::= newtype(int); or something any odd number of "s for a string -give each warning a number; #no_warn(warning), #no_warn_throughout_this_file(warning) +give each warning a number; #no_warn(warning), #no_warn_throughout_this_file(warning) test various parse errors; see if they can be improved, e.g. if else { 3; } use point #except x; optional -Wshadow @@ -153,7 +153,7 @@ int main(int argc, char **argv) { signal(SIGILL, signal_handler); signal(SIGFPE, signal_handler); #endif -#if RUN_TESTS +#if RUN_TESTS printf("running tests...\n"); test_all(); #endif diff --git a/misc.c b/misc.c index dc6076e..8354b69 100644 --- a/misc.c +++ b/misc.c @@ -1,4 +1,4 @@ -/* +/* Miscellaneous C functions which toc uses. This is free and unencumbered software released into the public domain. @@ -24,7 +24,7 @@ For more information, please refer to */ -/* +/* A better alternative to strncpy. dest is guaranteed to be a null-terminated string after this function is run. Returns the number of characters copied to dest, not including the null character. @@ -52,7 +52,7 @@ static char *str_dup(const char *s) { size_t bufsz = strlen(s)+1; char *ret = err_malloc(bufsz); memcpy(ret, s, bufsz); - return ret; + return ret; } static char *cstr(const char *str, size_t len) { diff --git a/parse.c b/parse.c index 4f733f6..2f47d9a 100644 --- a/parse.c +++ b/parse.c @@ -613,7 +613,7 @@ static Status parse_type(Parser *p, Type *type, Location *where) { Location ptr_where; if (!parse_type(p, type->ptr, &ptr_where)) return false; } break; - case KW_ANDAND: { + case KW_ANDAND: { // pointer to pointer type->kind = TYPE_PTR; Type *ptr = type->ptr = parser_malloc(p, sizeof *type->ptr); @@ -699,8 +699,8 @@ static Status parse_type(Parser *p, Type *type, Location *where) { } -/* - is the thing we're looking at definitely a type, as opposed to an expression? +/* + is the thing we're looking at definitely a type, as opposed to an expression? if end is not NULL, it is set to the token one past the last one in the type, assuming it's successful */ @@ -904,10 +904,9 @@ static Status parse_decl_list(Parser *p, Declaration **decls, U16 flags) { bool ret = true; bool first = true; *decls = NULL; - while (t->token->kind != TOKEN_EOF && - (first || ( - !token_is_kw(t->token - 1, KW_RPAREN) && - !token_is_kw(t->token - 1, KW_LBRACE)))) { + while (t->token->kind != TOKEN_EOF && (first || ( + !token_is_kw(t->token - 1, KW_RPAREN) && + !token_is_kw(t->token - 1, KW_LBRACE)))) { first = false; Declaration *decl = parser_arr_add_ptr(p, *decls); if (!parse_decl(p, decl, flags)) { @@ -963,7 +962,7 @@ static Status parse_fn_expr(Parser *p, FnExpr *f) { ++t->token; } else { if (!parse_decl_list(p, &f->params, DECL_CAN_END_WITH_RPAREN - | DECL_CAN_END_WITH_COMMA | PARSE_DECL_ALLOW_CONST_WITH_NO_EXPR + | DECL_CAN_END_WITH_COMMA | PARSE_DECL_ALLOW_CONST_WITH_NO_EXPR | PARSE_DECL_ALLOW_SEMI_CONST | PARSE_DECL_ALLOW_INFER)) return false; arr_foreach(f->params, Declaration, param) { @@ -1403,7 +1402,7 @@ static Status parse_expr(Parser *p, Expression *e, Token *end) { if (!token_is_kw(t->token, KW_RPAREN)) { tokr_err(t, "Expected ) to follow #foreign lib."); return false; - } + } } ++t->token; @@ -1574,8 +1573,8 @@ static Status parse_expr(Parser *p, Expression *e, Token *end) { // Check if this is a unary op not a binary one (e.g. +-3 => +(-3), not (+)-(3)). while (lowest_precedence_op != t->token - && lowest_precedence_op[-1].kind == TOKEN_KW - && op_precedence(lowest_precedence_op[-1].kw) != NOT_AN_OP) { + && lowest_precedence_op[-1].kind == TOKEN_KW + && op_precedence(lowest_precedence_op[-1].kw) != NOT_AN_OP) { --lowest_precedence_op; } if (lowest_precedence_op == t->token) { @@ -1765,7 +1764,7 @@ static Status parse_expr(Parser *p, Expression *e, Token *end) { e->binary.rhs = rhs; if (!parse_expr(p, rhs, end)) { return false; - } + } goto success; } else { // function calls, array accesses, etc. @@ -1814,7 +1813,7 @@ static Status parse_expr(Parser *p, Expression *e, Token *end) { Token *token = t->token; // currently unnecessary: paren_level = square_level = 0; - /* + /* can't call at start, e.g. in (fn() {})(), it is not the empty function "" being called with fn() {} as an argument */ @@ -1913,7 +1912,7 @@ static Status parse_expr(Parser *p, Expression *e, Token *end) { if (!parse_expr(p, e->binary.rhs, iend)) return false; break; - expr_is_slice: + expr_is_slice: case KW_COLON: { // slice SliceExpr *s = &e->slice; @@ -2196,7 +2195,7 @@ static bool is_decl(Tokenizer *t) { // you can only export declarations if (token_is_direct(token, DIRECT_EXPORT)) - return true; + return true; // use decls, e.g. use p: Point if (token_is_kw(token, KW_USE)) @@ -2292,7 +2291,7 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { Token *deferred_start = t->token; s->defer = parser_malloc(p, sizeof *s->defer); if (!parse_stmt(p, s->defer, was_a_statement)) - return false; + return false; if (!*was_a_statement) { err_print(token_location(p->file, deferred_start), "Invalid defer (are you missing a statement?)."); return false; @@ -2422,7 +2421,7 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { goto for_fail; if (token_is_kw(first_end, KW_LBRACE)) { fo->of = first; - } else if (first_end->kind == TOKEN_KW && + } else if (first_end->kind == TOKEN_KW && (is_for_range_separator(first_end->kw) || first_end->kw == KW_COMMA)) { fo->flags |= FOR_IS_RANGE; fo->range.from = first; @@ -2538,7 +2537,7 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { tokr_skip_semicolon(t); return false; } - break; + break; } case DIRECT_INIT: { if (!parser_is_at_top_level(p)) { @@ -2747,7 +2746,7 @@ static void fprint_expr(FILE *out, Expression *e) { lhs_type = lhs_type->ptr; } if (e->binary.op == BINARY_DOT && lhs_type->kind == TYPE_STRUCT) { - fprint_ident(out, e->binary.field->name); + fprint_ident(out, e->binary.field->name); break; } } diff --git a/tokenizer.c b/tokenizer.c index 8beead9..0a57468 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -13,7 +13,7 @@ static Keyword tokenize_kw(char **s) { size_t len = strlen(keywords[k]); if (strncmp(*s, keywords[k], len) == 0) { if (k > KW_LAST_SYMBOL) { - /* + /* it's not a symbol, so we need to check if it's something like "intfoo" */ if (is_ident((*s)[len])) { @@ -149,7 +149,7 @@ static int tokr_esc_seq(Tokenizer *t) { tokr_nextchar(t); tokr_nextchar(t); return (char)(c1 * 16 + c2); - } + } default: return -1; } @@ -241,7 +241,7 @@ static void tokr_get_start_pos(Tokenizer *tokr, Token *t) { tokr->s = tokr->file->contents + t->pos.start; } -/* +/* the allocator you pass it will be used for string literals, so it shouldn't be freed until everything is done */ @@ -382,7 +382,7 @@ static Status tokenize_file(Tokenizer *t, File *file) { tokr_nextchar(t); break; default: - // it's 0/0.something etc. + // it's 0/0.something etc. break; } } @@ -565,7 +565,7 @@ static Status tokenize_file(Tokenizer *t, File *file) { while (is_ident(*t->s)) ++t->s; tokr_put_end_pos(t, token); continue; - } + } tokenization_err(t, "Token not recognized"); err: has_err = 1; diff --git a/types.c b/types.c index 423764f..c9bbec6 100644 --- a/types.c +++ b/types.c @@ -12,8 +12,8 @@ static void val_cast(Value vin, Type *from, Value *vout, Type *to); static U64 val_to_u64(Value v, BuiltinType v_type); static I64 val_to_i64(Value v, BuiltinType v_type); static bool val_truthiness(Value v, Type *t); -static Value val_zero(Allocator *a, Type *t); -static Status eval_stmt(Evaluator *ev, Statement *stmt); +static Value val_zero(Allocator *a, Type *t); +static Status eval_stmt(Evaluator *ev, Statement *stmt); static Status struct_resolve(Typer *tr, StructDef *s); static Status expr_must_usable(Typer *tr, Expression *e); @@ -211,10 +211,10 @@ static Status struct_add_stmts(Typer *tr, StructDef *s, Statement *stmts) { return true; } -/* - set the field pointers of the declarations in this struct, so that when we look up the declaration of +/* + set the field pointers of the declarations in this struct, so that when we look up the declaration of a member of the struct, we know which Field it refers to. - this needs to be a different step after struct_add_stmts, because the pointers to Fields can change + this needs to be a different step after struct_add_stmts, because the pointers to Fields can change during struct_add_stmts */ static void struct_set_decl_field_ptrs(Typer *tr, StructDef *s, Statement *stmts) { @@ -297,7 +297,7 @@ static size_t compiler_sizeof(Type *t) { } static Status struct_resolve(Typer *tr, StructDef *s) { - if (s->flags & STRUCT_DEF_RESOLVING_FAILED) + if (s->flags & STRUCT_DEF_RESOLVING_FAILED) return false; // silently fail; do not try to resolve again, because there'll be duplicate errors if (!(s->flags & STRUCT_DEF_RESOLVED)) { s->flags |= STRUCT_DEF_RESOLVING; @@ -454,15 +454,15 @@ static Type *overriding_type(Type *a, Type *b) { if (b->flags & TYPE_IS_FLEXIBLE) return a; - if (a->kind == TYPE_PTR && type_is_builtin(a->ptr, BUILTIN_VOID)) + if (a->kind == TYPE_PTR && type_is_builtin(a->ptr, BUILTIN_VOID)) return b; // doesn't matter return a; } -/* -prints an error and returns false if the given expression is not an l-value +/* +prints an error and returns false if the given expression is not an l-value purpose is something like "take address of" */ static Status expr_must_lval(Expression *e, const char *purpose) { @@ -502,7 +502,7 @@ static Status expr_must_lval(Expression *e, const char *purpose) { return false; } return true; - case BINARY_DOT: + case BINARY_DOT: if (e->type.kind == TYPE_PTR) return true; // structure->member is always an lvalue return expr_must_lval(e->binary.lhs, purpose); default: break; @@ -512,7 +512,7 @@ static Status expr_must_lval(Expression *e, const char *purpose) { case EXPR_TUPLE: // x, y is an lval, but 3, "hello" is not. arr_foreach(e->tuple, Expression, x) { - if (!expr_must_lval(x, purpose)) + if (!expr_must_lval(x, purpose)) return false; } return true; @@ -675,7 +675,7 @@ static Status type_of_fn(Typer *tr, FnExpr *f, Type *t, U16 flags) { } } } - } + } } U32 is_at_all_const = param->flags & (DECL_IS_CONST | DECL_SEMI_CONST); if (is_at_all_const) { @@ -746,7 +746,7 @@ static Status type_of_fn(Typer *tr, FnExpr *f, Type *t, U16 flags) { success = false; goto ret; } - /* + /* a function which returns a compile-only type but has non-constant parameters is weird... but might be useful, so let's warn */ @@ -850,7 +850,7 @@ static Status type_of_ident(Typer *tr, Location where, Identifier i, Type *t) { #if 0 Block *decl_block = i->idents->scope; if (block_is_at_top_level(decl_block)) { - /* + /* let's type the declaration, and redo this (for calling future functions at compile time) this makes sure the error is right for: foo(); @@ -1231,7 +1231,7 @@ static Status types_fn(Typer *tr, FnExpr *f, Type *t, Instance *instance) { goto ret; } char *expected = type_to_str(ret_type); - err_print(token_location(f->body.where.file, &f->body.where.file->tokens[f->body.where.end-1]), + err_print(token_location(f->body.where.file, &f->body.where.file->tokens[f->body.where.end-1]), "Function which should return %s is missing a return statement (or it does not clearly always return).", expected); free(expected); info_print(f->where, "Function was declared here:"); @@ -1377,13 +1377,11 @@ static Status call_arg_param_order(FnExpr *fn, Type *fn_type, Argument *args, Lo return true; } -/* - *order must be freed, regardless of return value. if (*order)[i] == -1, that parameter was not set. -*/ +// order must be freed, regardless of return value. if (*order)[i] == -1, that parameter was not set. static Status parameterized_struct_arg_order(StructDef *struc, Argument *args, I16 **order, Location where) { size_t nargs = arr_len(args); - /* + /* it would be nice if this code and the code for arguments to normal functions weren't split into two separate functions. */ @@ -1510,7 +1508,7 @@ static void get_builtin_val_type(Allocator *a, BuiltinVal val, Type *t) { } -// gets a struct's constant or parameter, and puts it into e->val. +// gets a struct's constant or parameter, and puts it into e->val. static Status get_struct_constant(StructDef *struc, String ident, Expression *e) { if (struc->params && !(struc->params[0].flags & DECL_FOUND_VAL)) { err_print(e->where, "To access constants from a parameterized struct, you must supply its arguments."); @@ -1631,7 +1629,7 @@ static Status types_expr(Typer *tr, Expression *e) { e->flags |= EXPR_FOUND_TYPE; // even if failed, pretend we found the type if (e->kind == EXPR_VAL) { // can exist, e.g. for null - return true; + return true; } Type *t = &e->type; t->flags = TYPE_IS_RESOLVED; @@ -1696,7 +1694,7 @@ static Status types_expr(Typer *tr, Expression *e) { print_block_location(i->idents->body); printf(" to \n"); print_block_location(translated->idents->body); -#endif +#endif final_ident = translated; undeclared = false; } @@ -1845,7 +1843,7 @@ static Status types_expr(Typer *tr, Expression *e) { bool has_varargs = f->type.kind == TYPE_FN && fn_type_has_varargs(f->type.fn); bool fn_being_used_before_declared = false; - if (expr_is_definitely_const(f) || type_is_builtin(&f->type, BUILTIN_TYPE) || has_varargs) { + if (expr_is_definitely_const(f) || type_is_builtin(&f->type, BUILTIN_TYPE) || has_varargs) { // evaluate the function when possible (this allows us to use named arguments, default arguments, etc.) Value val; if (f->kind == EXPR_IDENT) { @@ -2011,7 +2009,7 @@ static Status types_expr(Typer *tr, Expression *e) { Declaration *last_param = arr_last_ptr(fn_decl->params); arr_foreach(fn_decl->params, Declaration, param) { if (has_varargs && param == last_param) continue; - arr_foreach(param->idents, Identifier, ident) { + arr_foreach(param->idents, Identifier, ident) { I16 arg_idx = order[i]; if (arg_idx == -1) { if (param->flags & DECL_HAS_EXPR) { @@ -2175,7 +2173,7 @@ static Status types_expr(Typer *tr, Expression *e) { if (param->flags & DECL_INFER) { arr_add(inferred_idents, *ident); } else if ((param->flags & DECL_ANNOTATES_TYPE) - && !type_is_builtin(¶m->type, BUILTIN_VARARGS)) { + && !type_is_builtin(¶m->type, BUILTIN_VARARGS)) { // add to stuff infer can use // @OPTIM: don't do this if we're not inferring arr_add(decl_types, ¶m->type); @@ -2301,7 +2299,7 @@ static Status types_expr(Typer *tr, Expression *e) { } } ++i; - } + } } } @@ -2582,14 +2580,14 @@ static Status types_expr(Typer *tr, Expression *e) { t->kind = TYPE_BUILTIN; t->builtin = BUILTIN_I64; } break; - } + } } break; case EXPR_BINARY_OP: { Expression *lhs = e->binary.lhs; Expression *rhs = e->binary.rhs; Type *lhs_type = &lhs->type; Type *rhs_type = &rhs->type; - + BinaryOp o = e->binary.op; if (o != BINARY_DOT) { bool lhs_success = types_expr(tr, lhs); @@ -2718,7 +2716,7 @@ static Status types_expr(Typer *tr, Expression *e) { bool equal = type_eq_exact(lhs_val.type, rhs_val.type); if (o == BINARY_NE) e->val.boolv = !equal; - else + else e->val.boolv = equal; } else { valid = false; @@ -3125,7 +3123,7 @@ static Status types_expr(Typer *tr, Expression *e) { Declaration *decl = ident->decl; e->kind = EXPR_VAL; e->val.i64 = (I64)arr_len(decl->val.varargs); - } else if (struct_type->kind == TYPE_ARR) { + } else if (struct_type->kind == TYPE_ARR) { e->kind = EXPR_VAL; e->val.i64 = (I64)struct_type->arr->n; } @@ -3358,7 +3356,7 @@ static Status types_decl(Typer *tr, Declaration *d) { copy_val(tr->allocr, &d->val, val, dtype); flags |= DECL_FOUND_VAL; if (!(flags & DECL_IS_CONST)) { - /* + /* create a value stack for this declaration so that it can be modified by compile time execution, but not permanently (i.e. output will still have old value) */ @@ -3383,7 +3381,7 @@ static Status types_decl(Typer *tr, Declaration *d) { arr_adda(d->val_stack, val, tr->allocr); // arr_adda because this will never be freed if (n_idents > 1 && dtype->kind != TYPE_TUPLE) { Value *tuple = val->tuple = typer_malloc(tr, n_idents * sizeof *tuple); - for (size_t i = 0; i < n_idents; ++i) + for (size_t i = 0; i < n_idents; ++i) tuple[i] = val_zero(tr->allocr, dtype); } else { *val = val_zero(tr->allocr, dtype); @@ -3588,7 +3586,7 @@ top: { size_t nidents = arr_len(header->idents); if (nidents > 2) { - err_print(header->where, "Expected at most 2 identifiers in for declaration (index and value) but got %lu.", + err_print(header->where, "Expected at most 2 identifiers in for declaration (index and value) but got %lu.", (unsigned long)nidents); goto for_fail; } @@ -3907,7 +3905,7 @@ top: err_print(s->where, "Static for loops can only be used with type int right now.\n" "Either switch from :: to : or cast the range bounds to int."); goto for_fail; - } + } Value from_val, to_val; if (!eval_expr(tr->evalr, r->from, &from_val)) return false; if (!eval_expr(tr->evalr, r->to, &to_val)) return false; @@ -3918,7 +3916,7 @@ top: err_print(s->where, "Only range-based static for loops (e.g. for x ::= 1..10) are supported right now. You need to change :: to :"); goto for_fail; } - if (!(flags & FOR_INCLUDES_FROM)) + if (!(flags & FOR_INCLUDES_FROM)) from += step; Statement *for_body_stmts = fo->body.stmts; @@ -4056,7 +4054,7 @@ top: err_print(s->where, "return outside of a function."); return false; } - r->referring_to = &tr->fn->body; + r->referring_to = &tr->fn->body; if (r->flags & RET_HAS_EXPR) { if (type_is_void(&tr->fn->ret_type)) { err_print(s->where, "Return value in a void function."); @@ -4113,7 +4111,7 @@ top: if (i->decl) { Declaration *d2 = i->decl; // maybe they included it twice into one namespace - if ((d2->flags & DECL_HAS_EXPR) && (d2->expr.kind == EXPR_NMS) && + if ((d2->flags & DECL_HAS_EXPR) && (d2->expr.kind == EXPR_NMS) && (d2->expr.nms->inc_file == inc_f)) { // that's okay; get rid of this declaration s->kind = STMT_INLINE_BLOCK; @@ -4221,7 +4219,7 @@ top: } break; case STMT_MESSAGE: { Message *m = s->message; - char *text = eval_expr_as_cstr(tr, &m->text, "message"); + char *text = eval_expr_as_cstr(tr, &m->text, "message"); if (!text) return false; switch (m->kind) { @@ -4248,7 +4246,7 @@ top: if (!block->c.break_lbl) { block->c.break_lbl = ++tr->lbl_counter; } - } else { + } else { assert(s->kind == STMT_CONT); if (!block->c.cont_lbl) { block->c.cont_lbl = ++tr->lbl_counter; diff --git a/types.h b/types.h index 1eadff5..bca892d 100644 --- a/types.h +++ b/types.h @@ -89,7 +89,7 @@ typedef U8 bool; #endif #if defined __GNUC__ && !defined NO_WARN_UNUSED_RESULT -#define WarnUnusedResult __attribute__((warn_unused_result)) +#define WarnUnusedResult __attribute__((warn_unused_result)) #else #define WarnUnusedResult #endif @@ -435,7 +435,7 @@ typedef U8 Constness; #define CONSTNESS_YES ((Constness)2) typedef struct FnType { - struct Type *types; // dynamic array [0] = ret_type, [1:] = param_types + struct Type *types; // dynamic array [0] = ret_type, [1:] = param_types Constness *constness; // [i] = constness of param #i. iff no parameters are constant, this is NULL. don't use it as a dynamic array, because it might not always be. } FnType; @@ -495,8 +495,9 @@ typedef U8 BlockFlags; typedef struct Block { // NOTE: make sure you check copy.c when you add something to this BlockFlags flags; - BlockKind kind; /* set during the parsing phase, but don't access while this specific block is being - parsed, because sometimes it's set after parse_block */ + // set during the parsing phase, but don't access while this specific block is being + // parsed, because sometimes it's set after parse_block + BlockKind kind; struct { IdentID break_lbl, cont_lbl; // initially 0, set to non-zero values if needed (tr->lbl_counter); set during typing } c; @@ -521,7 +522,7 @@ typedef struct StructDef { // these two only exist after resolving (before then, it's scope.stmts) Field *fields; Location where; - /* + /* use this instead of fields when looking up a field, because it will include "use"d things. this only consists of statements which are declarations after typing (and not #ifs, for example) @@ -563,12 +564,12 @@ typedef enum { EXPR_SLICE, EXPR_TYPE, EXPR_NMS, - /* - a value (it's useful to have this). + /* + a value (it's useful to have this). right now they don't work with cgen_set_tuple (as of yet, that is unneeded) */ - EXPR_VAL + EXPR_VAL } ExprKind; typedef enum { @@ -656,7 +657,7 @@ typedef struct FnExpr { struct { struct Declaration *params; // declarations of the parameters to this function struct Declaration *ret_decls; // array of decls, if this has named return values. otherwise, NULL - Type ret_type; + Type ret_type; Block body; }; struct { @@ -846,7 +847,7 @@ typedef struct Declaration { }; Value val; // only for constant decls, non-constant globals, and varargs. - /* + /* for eval, for non-constant decls the pointers to values need to be fixed, which is why this isn't just Value *. no, this can't be a union with val, because of global variables and varargs @@ -992,7 +993,7 @@ typedef Statement *StatementPtr; /* Statements to be run before any code in main is called. This is mainly for the standard library, so you don't have to do something weird - like io.init(); + like io.init(); */ typedef struct { Statement *stmt; @@ -1009,7 +1010,7 @@ typedef struct { } StatementWithCtx; typedef struct { - /* + /* statements to be run before main function is called. these are in order of appearance (which is the order in which they are called) */ @@ -1039,7 +1040,7 @@ typedef struct { typedef struct Evaluator { Allocator *allocr; struct Typer *typer; - Block *returning; // function body from which we are returning OR loop body in which we are continuing/breaking + Block *returning; // function body from which we are returning OR loop body in which we are continuing/breaking bool is_break; // is returning because of a break, as opposed to a continue? void **to_free; // array of pointers to free once block is exited Declaration **decls_given_values; // array of declarations whose last value in their val stacks should be removed when the block is exited @@ -1048,8 +1049,8 @@ typedef struct Evaluator { } Evaluator; -/* - so there are loops in cgen that generate all the function declarations/definitions +/* + so there are loops in cgen that generate all the function declarations/definitions and they need to know what namespace they're in (because we name mangle stuff in namespaces) */ typedef struct { @@ -1082,7 +1083,7 @@ typedef struct Typer { unsigned long nms_counter; // counter for namespace IDs StrHashTable included_files; // maps to IncludedFile // did we get an error which is bad enough that we should stop typing? - bool had_fatal_err; + bool had_fatal_err; } Typer; typedef struct CGenerator { diff --git a/win64call_test.c b/win64call_test.c index f1950b5..0c686a7 100644 --- a/win64call_test.c +++ b/win64call_test.c @@ -1,4 +1,4 @@ -/* +/* This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or @@ -52,17 +52,17 @@ extern uint64_t win64_call(FnPtr fn, void *args, int64_t nargs); extern float win64_callf(FnPtr fn, void *args, int64_t nargs); extern double win64_calld(FnPtr fn, void *args, int64_t nargs); float asdf(float a, double b, int c, double d, long e, float f) { - return (float)f; + return (float)f; } void foobar(void) { - FnPtr fn = (FnPtr)asdf; - float a = -1.6f; - double b = 3.0, d = 33.7; - unsigned long long args[6] = { - *(uint32_t *)&a, *(uint64_t *)&b, -12, *(uint64_t *)&d, 4, *(uint32_t *)&a - }; - float ret = win64_callf(fn, args, 6); - printf("6 returned: %f\n", ret); + FnPtr fn = (FnPtr)asdf; + float a = -1.6f; + double b = 3.0, d = 33.7; + unsigned long long args[6] = { + *(uint32_t *)&a, *(uint64_t *)&b, -12, *(uint64_t *)&d, 4, *(uint32_t *)&a + }; + float ret = win64_callf(fn, args, 6); + printf("6 returned: %f\n", ret); } uint64_t test_fn(long long a, uint64_t b, uint64_t c, int d) { @@ -78,28 +78,28 @@ double test_fp(double a, double b, float c, int d, float e, int f, double g) { int foo(int a, int b, int c) { - return a+b+c; + return a+b+c; } void main1(void) { - FnPtr fn = (FnPtr)foo; - unsigned long long args[3] = { - -1000, -3, 65 - }; - int ret = (int)win64_call(fn, args, 3); - printf("7 returned: %d\n", ret); + FnPtr fn = (FnPtr)foo; + unsigned long long args[3] = { + -1000, -3, 65 + }; + int ret = (int)win64_call(fn, args, 3); + printf("7 returned: %d\n", ret); } float bar(float a, double b, int c, double d, long e) { - return a-(float)b + sinf((float)c) - (float)cos(d) + (float)e; + return a-(float)b + sinf((float)c) - (float)cos(d) + (float)e; } void main2(void) { - FnPtr fn = (FnPtr)bar; - float a = -1.6f; - double b = 3.0, d = 33.7; - unsigned long long args[5] = { - *(uint32_t *)&a, *(uint64_t *)&b, -12, *(uint64_t *)&d, 4 - }; - float ret = win64_callf(fn, args, 5); - printf("8 returned: %f\n", ret); + FnPtr fn = (FnPtr)bar; + float a = -1.6f; + double b = 3.0, d = 33.7; + unsigned long long args[5] = { + *(uint32_t *)&a, *(uint64_t *)&b, -12, *(uint64_t *)&d, 4 + }; + float ret = win64_callf(fn, args, 5); + printf("8 returned: %f\n", ret); } int main(void) { @@ -144,7 +144,7 @@ int main(void) { *(uint64_t *)&num1, *(uint64_t *)&num2, *(uint32_t *)&two, - -6, + -6, *(uint32_t *)&two, 55, *(uint64_t *)&num3 -- cgit v1.2.3