summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--a.c23
-rw-r--r--a.toc28
-rw-r--r--copy.c3
-rw-r--r--package.c45
-rwxr-xr-xpkg.sh5
-rw-r--r--test.toc6
-rw-r--r--types.c1
7 files changed, 90 insertions, 21 deletions
diff --git a/a.c b/a.c
new file mode 100644
index 0000000..2d2c03a
--- /dev/null
+++ b/a.c
@@ -0,0 +1,23 @@
+#include <stdint.h>
+#include <stdio.h>
+typedef int8_t i8;
+typedef int16_t i16;
+typedef int32_t i32;
+typedef int64_t i64;
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+typedef float f32;
+typedef double f64;
+typedef u8 bool;
+typedef struct { void *data; i64 n; } slice_;
+#define false ((bool)0)
+#define true ((bool)1)
+static slice_ mkslice_(void *data, i64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; }
+static void free_(void *data) { extern void free(void *data); free(data); }
+static void *e__calloc(size_t n, size_t sz) { extern void *calloc(size_t n, size_t size); extern void abort(void); void *ret = calloc(n, sz); if (n && sz && !ret) { fprintf(stderr, "Out of memory.\n"); abort(); } return ret; }
+
+
+/* declarations */
+/* code */
diff --git a/a.toc b/a.toc
new file mode 100644
index 0000000..60c31dd
--- /dev/null
+++ b/a.toc
@@ -0,0 +1,28 @@
+// array package
+pkg "a";
+
+#export Arr ::= fn (t :: Type) Type {
+ struct {
+ data : []t;
+ len, cap : int;
+ }
+};
+/*
+#export arr_add ::= fn(t ::=, a : &Arr(t), x : t) {
+ if a.len >= a.cap {
+ a.cap = a.cap * 2 + 2;
+ new_data := new(t, a.cap);
+ each i := 0..a.len-1 {
+ new_data[i] = a.data[i];
+ }
+ a.data = new_data;
+ }
+ a.data[a.len] = x;
+ a.len += 1;
+};
+
+#export arr_foreach ::= fn(t ::=, a : Arr(t), f : fn(&t)) {
+ each i := 0..a.len-1 {
+ f(&a.data[i]);
+ }
+};*/ \ No newline at end of file
diff --git a/copy.c b/copy.c
index 4ac56e3..bcb04aa 100644
--- a/copy.c
+++ b/copy.c
@@ -70,7 +70,7 @@ static void copy_val_full(Copier *c, Value *out, Value *in, Type *t) {
}
}
-/* only works on unresolved and resolved types */
+/* works on unresolved and resolved types */
static void copy_type(Copier *c, Type *out, Type *in) {
*out = *in;
switch (in->kind) {
@@ -118,6 +118,7 @@ static void copy_type(Copier *c, Type *out, Type *in) {
case TYPE_STRUCT: {
out->struc = allocr_malloc(c->allocr, sizeof *out->struc);
*out->struc = *in->struc;
+ printf("\n--- USE %p\n",in->struc);
size_t nfields = arr_len(in->struc->fields);
out->struc->fields = NULL;
diff --git a/package.c b/package.c
index e9d6946..0dbbd56 100644
--- a/package.c
+++ b/package.c
@@ -925,12 +925,11 @@ static void export_ident_name(Exporter *ex, Identifier ident) {
static bool export_decl(Exporter *ex, Declaration *d) {
assert(ex->started);
+ /* printf("EXPORT %ld\n",ftell(ex->out)); */
possibly_static_assert(sizeof d->flags == 2);
export_u16(ex, d->flags);
- unsigned found_type = d->flags & DECL_FOUND_TYPE;
-
- if (found_type && d->type.kind == TYPE_UNKNOWN) {
+ if ((d->flags & DECL_FOUND_TYPE) && d->type.kind == TYPE_UNKNOWN) {
err_print(d->where, "Can't export declaration of unknown type.");
return false;
}
@@ -946,7 +945,7 @@ static bool export_decl(Exporter *ex, Declaration *d) {
export_ident(ex, *ident);
}
- if (found_type) {
+ if (d->flags & (DECL_FOUND_TYPE | DECL_ANNOTATES_TYPE)) {
if (!export_type(ex, &d->type, d->where))
return false;
}
@@ -962,6 +961,7 @@ static bool export_decl(Exporter *ex, Declaration *d) {
static void import_decl(Importer *im, Declaration *d) {
possibly_static_assert(sizeof d->flags == 2);
+ /* printf("IMPORT %ld\n",ftell(im->in)); */
d->flags = import_u16(im);
d->flags &= (DeclFlags)~(DeclFlags)DECL_EXPORT;
d->where = import_location(im);
@@ -970,7 +970,7 @@ static void import_decl(Importer *im, Declaration *d) {
for (size_t i = 0; i < n_idents; ++i) {
d->idents[i] = import_ident(im);
}
- if (d->flags & DECL_FOUND_TYPE) {
+ if (d->flags & (DECL_FOUND_TYPE | DECL_ANNOTATES_TYPE)) {
import_type(im, &d->type);
}
if (d->flags & DECL_FOUND_VAL) {
@@ -1104,6 +1104,7 @@ static bool export_struct(Exporter *ex, StructDef *s) {
}
static void import_struct(Importer *im, StructDef *s) {
+ printf("---IMPORT %p\n",s);
s->name = import_ident(im);
size_t nfields = import_arr(im, &s->fields);
for (size_t i = 0; i < nfields; ++i) {
@@ -1125,15 +1126,10 @@ static bool exptr_finish(Exporter *ex) {
/* position in file of where the position in file of identifier info is */
long ident_info_offset_offset = ftell(ex->out);
export_u64(ex, 0); /* identifier info offset */
+ long struct_info_offset_offset = ftell(ex->out);
+ export_u64(ex, 0); /* struct info offset */
+
- export_len(ex, arr_len(ex->exported_structs));
- typedef StructDef *StructDefPtr;
- arr_foreach(ex->exported_structs, StructDefPtr, s) {
- if (!export_struct(ex, *s))
- return false;
- }
- arr_clear(&ex->exported_structs);
-
export_len(ex, arr_len(ex->exported_fns));
typedef FnExpr *FnExprPtr;
arr_foreach(ex->exported_fns, FnExprPtr, f) {
@@ -1142,6 +1138,15 @@ static bool exptr_finish(Exporter *ex) {
}
arr_clear(&ex->exported_fns);
+ long struct_info_offset = ftell(ex->out);
+ export_len(ex, arr_len(ex->exported_structs));
+ typedef StructDef *StructDefPtr;
+ arr_foreach(ex->exported_structs, StructDefPtr, s) {
+ if (!export_struct(ex, *s))
+ return false;
+ }
+ arr_clear(&ex->exported_structs);
+
long ident_info_offset = ftell(ex->out);
/* export number of identifiers *whose names matter* */
fseek(ex->out, ident_info_offset, SEEK_SET);
@@ -1159,6 +1164,9 @@ static bool exptr_finish(Exporter *ex) {
export_u64(ex, (U64)ident_info_offset);
arr_clear(&ex->exported_idents);
+
+ fseek(ex->out, struct_info_offset_offset, SEEK_SET);
+ export_u64(ex, (U64)struct_info_offset);
if (ferror(ex->out)) {
warn_print(LOCATION_NONE, "An error occured while writing the package output. It may be incorrect.");
@@ -1169,8 +1177,8 @@ static bool exptr_finish(Exporter *ex) {
static bool import_footer(Importer *im) {
size_t i;
+ long footer_offset = ftell(im->in);
U64 ident_info_offset = import_u64(im);
- long main_footer_offset = ftell(im->in);
fseek(im->in, (long)ident_info_offset, SEEK_SET);
im->max_ident_id = import_u64(im);
@@ -1194,8 +1202,11 @@ static bool import_footer(Importer *im) {
}
}
- fseek(im->in, main_footer_offset, SEEK_SET);
-
+ fseek(im->in, footer_offset + 8, SEEK_SET);
+ U64 struct_offset = import_u64(im);
+ fseek(im->in, (long)struct_offset, SEEK_SET);
+
+
import_arr(im, &im->structs);
#ifdef TOC_DEBUG
/* for debugging: so that struct names show up as "anonymous struct" if they haven't been imported yet */
@@ -1204,6 +1215,8 @@ static bool import_footer(Importer *im) {
arr_foreach(im->structs, StructDef, s)
import_struct(im, s);
+ fseek(im->in, footer_offset + 16, SEEK_SET);
+
import_arr(im, &im->fns);
arr_zero(im->fns);
arr_foreach(im->fns, FnExpr, f) {
diff --git a/pkg.sh b/pkg.sh
new file mode 100755
index 0000000..0e5eb94
--- /dev/null
+++ b/pkg.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+valgrind -q ./toc $1.toc -o $1.c || exit 1
+valgrind -q ./toc test.toc || exit 1
+gcc test.toc $1.c || exit 1
+./a.out
diff --git a/test.toc b/test.toc
index 79e07e8..6112afa 100644
--- a/test.toc
+++ b/test.toc
@@ -7,10 +7,8 @@ putf ::= fn(x: float) {
");
};
-point ::= pkg "point";
+arr ::= pkg "a";
main ::= fn() {
- x : point.Point = point.mk_point(y = 13, 14);
- puti(x.x + x.y);
- point.x = new(int,10);
+ x : arr.Arr(int);
}; \ No newline at end of file
diff --git a/types.c b/types.c
index dba9820..43a0414 100644
--- a/types.c
+++ b/types.c
@@ -2140,6 +2140,7 @@ static bool types_decl(Typer *tr, Declaration *d) {
if (d->flags & DECL_ANNOTATES_TYPE) {
/* type supplied */
assert(d->type.kind != TYPE_VOID); /* there's no way to annotate void */
+ print_location(d->where);
if (!type_resolve(tr, &d->type, d->where)) {
success = false;
goto ret;