summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-01-04 00:59:40 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-01-04 00:59:40 -0500
commit0c06ed6f6933d0eeac797f877d352dd7ec37056e (patch)
tree796226a5440bb9d7207d2eb6322b2245ca39ff1e
parent50c892e0a68c7e6d17ce49edf0c13ada7e1cdbfc (diff)
fixed potential future bug, which wasnt causing problems right now
-rw-r--r--allocator.c2
-rw-r--r--arr.c2
-rw-r--r--package.c24
-rw-r--r--toc.c1
-rw-r--r--types.c3
-rw-r--r--types.h8
6 files changed, 31 insertions, 9 deletions
diff --git a/allocator.c b/allocator.c
index 3c0aec0..2fc607d 100644
--- a/allocator.c
+++ b/allocator.c
@@ -8,7 +8,7 @@ static void *err_malloc(size_t bytes);
static void *err_calloc(size_t n, size_t sz);
static void *err_realloc(void *prev, size_t new_size);
#ifdef TOC_DEBUG
-/* #define NO_ALLOCATOR 1 /\* useful for debugging; valgrind (maybe) checks writing past the end of a malloc, but that won't work with an allocator *\/ */
+#define NO_ALLOCATOR 1 /* useful for debugging; valgrind (maybe) checks writing past the end of a malloc, but that won't work with an allocator */
#endif
/* number of bytes a page hold, not including the header */
#define PAGE_BYTES (16384 - sizeof(Page))
diff --git a/arr.c b/arr.c
index 16584f1..400210a 100644
--- a/arr.c
+++ b/arr.c
@@ -186,7 +186,7 @@ You shouldn't rely on this, though, e.g. by doing
#define arr_last(arr) arr_last_((void *)(arr), sizeof *(arr))
/* one past last, or NULL if empty */
#define arr_end(arr) arr_end_((void *)(arr), sizeof *(arr))
-#define arr_foreach(arr, type, var) for (type *var = arr, *JOIN(var,_foreach_end) = arr_end(arr); var < JOIN(var,_foreach_end); ++var) /* NOTE: < is useful here because currently it's possible for var_foreach_end to be NULL but var could start out not null */
+#define arr_foreach(arr, type, var) for (type *var = arr, *var##_foreach_end = arr_end(arr); var < var##_foreach_end; ++var) /* NOTE: < is useful here because currently it's possible for var_foreach_end to be NULL but var could start out not null */
#define arr_remove_last(arr) arr_remove_last_((void **)(arr)), (void)sizeof **(arr)
#define arr_remove_lasta(arr, a) arr_remove_lasta_((void **)(arr), sizeof **(arr), (a))
#define arr_copya(out, in, a) do { assert(sizeof *(in) == sizeof **(out)); arr_copya_((void **)(out), (in), sizeof **(out), (a)); } while(0)
diff --git a/package.c b/package.c
index 3dfaab6..fb7912f 100644
--- a/package.c
+++ b/package.c
@@ -7,10 +7,11 @@ static bool export_decl(Exporter *ex, Declaration *d);
static bool export_block(Exporter *ex, Block *b);
-static void exptr_create(Exporter *exptr, FILE *out) {
- exptr->out = out;
- exptr->export_locations = true;
- exptr->exported_fns = NULL;
+static void exptr_create(Exporter *ex, FILE *out) {
+ ex->out = out;
+ ex->export_locations = true;
+ ex->exported_fns = NULL;
+ ex->exported_structs = NULL;
}
@@ -412,6 +413,15 @@ static bool export_fn(Exporter *ex, FnExpr *f, Location where) {
return true;
}
+static bool export_struct(Exporter *ex, StructDef *s, Location where) {
+ arr_foreach(s->fields, Field, f) {
+ export_ident(ex, f->name);
+ if (!export_type(ex, f->type, where))
+ return false;
+ }
+ return true;
+}
+
/* does NOT close the file */
static bool exptr_finish(Exporter *ex) {
if (!export_len32(ex, arr_len(ex->exported_fns), "exported functions", LOCATION_NONE))
@@ -420,5 +430,11 @@ static bool exptr_finish(Exporter *ex) {
if (!export_fn(ex, f->fn, f->where))
return false;
}
+ arr_clear(&ex->exported_fns);
+ arr_foreach(ex->exported_structs, StructDefWithLocation, s) {
+ if (!export_struct(ex, s->struc, s->where))
+ return false;
+ }
+ arr_clear(&ex->exported_structs);
return true;
}
diff --git a/toc.c b/toc.c
index 18c79bb..7bbafcc 100644
--- a/toc.c
+++ b/toc.c
@@ -33,7 +33,6 @@ static void print_val(Value v, Type *t);
/* misc */
-#define JOIN(a,b) a##b
#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
diff --git a/types.c b/types.c
index 486dac5..e744fb7 100644
--- a/types.c
+++ b/types.c
@@ -1930,7 +1930,8 @@ static bool types_block(Typer *tr, Block *b) {
success = false;
goto ret;
}
- b->ret_expr = e;
+ b->ret_expr = typer_malloc(tr, sizeof *b->ret_expr);
+ *b->ret_expr = *e;
arr_remove_lasta(&b->stmts, tr->allocr);
}
}
diff --git a/types.h b/types.h
index e15e550..b4b32d4 100644
--- a/types.h
+++ b/types.h
@@ -73,7 +73,7 @@ typedef U32 IdentID; /* identifier ID for cgen (anonymous variables). not to be
#ifdef TOC_DEBUG
#define SOURCE_LOCATION char *src_file; int src_line;
#define SOURCE_LOCATION_PARAMS char *src_file, int src_line,
-#define DEBUG_UNDERSCORE(x) JOIN(x,_)
+#define DEBUG_UNDERSCORE(x) x##_
#else
#define SOURCE_LOCATION
#define SOURCE_LOCATION_PARAMS
@@ -754,10 +754,16 @@ typedef struct {
Location where;
} FnWithLocation;
+typedef struct {
+ StructDef *struc;
+ Location where;
+} StructDefWithLocation;
+
typedef struct Exporter {
FILE *out; /* .top (toc package) to output to */
bool export_locations;
FnWithLocation *exported_fns;
+ StructDefWithLocation *exported_structs;
} Exporter;
typedef struct CGenerator {