From 56a9ba5ea53a5d0b6268939d6a6e89837c2e4658 Mon Sep 17 00:00:00 2001
From: Leo Tenenbaum <pommicket@gmail.com>
Date: Fri, 10 Jan 2020 22:39:01 -0500
Subject: made Field.type not a pointer

---
 cgen.c           |  2 +-
 copy.c           |  2 +-
 decls_cgen.c     |  4 ++--
 eval.c           |  8 ++++----
 infer.c          |  2 +-
 instance_table.c |  4 ++--
 package.c        | 16 +++++++++++++---
 parse.c          |  3 +--
 types.c          |  8 ++++----
 types.h          | 48 +++++++++++++++++++++++++-----------------------
 10 files changed, 54 insertions(+), 43 deletions(-)

diff --git a/cgen.c b/cgen.c
index 2131abd..384cb0b 100644
--- a/cgen.c
+++ b/cgen.c
@@ -1702,7 +1702,7 @@ static bool cgen_val_ptr(CGenerator *g, void *v, Type *t, Location where) {
 		arr_foreach(t->struc->fields, Field, f) {
 			if (f != t->struc->fields)
 				cgen_write(g, ", ");
-			cgen_val_ptr(g, (char *)v + f->offset, f->type, where);
+			cgen_val_ptr(g, (char *)v + f->offset, &f->type, where);
 		}
 		cgen_write(g, "}");
 		break;
diff --git a/copy.c b/copy.c
index 80d5a52..58ba390 100644
--- a/copy.c
+++ b/copy.c
@@ -126,7 +126,7 @@ static void copy_type(Copier *c, Type *out, Type *in) {
 			Field *fout = &out->struc->fields[i];
 			Field *fin = &in->struc->fields[i];
 			*fout = *fin;
-			copy_type(c, fout->type = allocr_malloc(c->allocr, sizeof *fout->type), fin->type);
+			copy_type(c, &fout->type, &fin->type);
 		}
 	} break;
 	}
diff --git a/decls_cgen.c b/decls_cgen.c
index e8f7cca..bc6df42 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -25,10 +25,10 @@ static bool cgen_decls_type(CGenerator *g, Type *type, Location where) {
 			cgen_nl(g);
 			++g->indent_lvl;
 			arr_foreach(sdef->fields, Field, f) {
-				if (!cgen_type_pre(g, f->type, where)) return false;
+				if (!cgen_type_pre(g, &f->type, where)) return false;
 				cgen_write(g, " ");
 				cgen_ident(g, f->name);
-				if (!cgen_type_post(g, f->type, where)) return false;
+				if (!cgen_type_post(g, &f->type, where)) return false;
 				cgen_write(g, ";");
 				cgen_nl(g);
 			}
diff --git a/eval.c b/eval.c
index e13c5ba..0eab329 100644
--- a/eval.c
+++ b/eval.c
@@ -65,7 +65,7 @@ static void eval_struct_find_offsets(Type *t) {
 		size_t bytes = 0;
 		size_t total_align = 0;
 		arr_foreach(t->struc->fields, Field, f) {
-			size_t falign = compiler_alignof(f->type);
+			size_t falign = compiler_alignof(&f->type);
 			if (falign > total_align)
 				total_align = falign;
 			/* align */
@@ -73,7 +73,7 @@ static void eval_struct_find_offsets(Type *t) {
 			assert(bytes % falign == 0);
 			f->offset = bytes;
 			/* add size */
-			bytes += compiler_sizeof(f->type);
+			bytes += compiler_sizeof(&f->type);
 		}
 		bytes += ((total_align - bytes) % total_align + total_align) % total_align; /* = -bytes mod align */
 		t->struc->size = bytes;
@@ -344,7 +344,7 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) {
 				fprintf(f, ", ");
 			fprint_ident_debug(f, fi->name);
 			fprintf(f, ": ");
-			fprint_val_ptr(f, (char *)p + fi->offset, fi->type);
+			fprint_val_ptr(f, (char *)p + fi->offset, &fi->type);
 		}
 		fprintf(f, "]");
 		break;
@@ -843,7 +843,7 @@ static bool eval_set(Evaluator *ev, Expression *set, Value *to) {
 		case BINARY_DOT: {
 			void *ptr = eval_ptr_to_struct_field(ev, set);
 			if (!ptr) return false;
-			eval_deref_set(ptr, to, set->binary.field->type);
+			eval_deref_set(ptr, to, &set->binary.field->type);
 		} break;
 		default: assert(0); break;
 		}
diff --git a/infer.c b/infer.c
index 48102e0..98d5635 100644
--- a/infer.c
+++ b/infer.c
@@ -123,7 +123,7 @@ static bool infer_from_type(Typer *tr, Type *match, Type *to, Identifier *idents
 		size_t i, len = arr_len(fields_m);
 		if (len != arr_len(fields_t)) return true;
 		for (i = 0; i < len; ++i) {
-			if (!infer_from_type(tr, fields_m[i].type, fields_t[i].type, idents, vals, types))
+			if (!infer_from_type(tr, &fields_m[i].type, &fields_t[i].type, idents, vals, types))
 				return false;
 		}
 	} break;
diff --git a/instance_table.c b/instance_table.c
index 0e5827f..fe667b8 100644
--- a/instance_table.c
+++ b/instance_table.c
@@ -187,7 +187,7 @@ static U64 val_ptr_hash(void *v, Type *t) {
 		U32 x = 1;
 		U64 hash = 0;
 		arr_foreach(t->struc->fields, Field, f) {
-			hash += (U64)x * val_ptr_hash((char *)v + f->offset, f->type);
+			hash += (U64)x * val_ptr_hash((char *)v + f->offset, &f->type);
 			x = rand_u32(x);
 		}
 		return hash;
@@ -271,7 +271,7 @@ static bool val_ptr_eq(void *u, void *v, Type *t) {
 	}
 	case TYPE_STRUCT:
 		arr_foreach(t->struc->fields, Field, f) {
-			if (!val_ptr_eq((char *)u + f->offset, (char *)v + f->offset, f->type))
+			if (!val_ptr_eq((char *)u + f->offset, (char *)v + f->offset, &f->type))
 				return false;
 		}
 		return true;
diff --git a/package.c b/package.c
index 8920bd8..518bf93 100644
--- a/package.c
+++ b/package.c
@@ -256,7 +256,7 @@ static bool import_pkg(Allocator *allocr, Package *p, FILE *f, const char *fname
 static bool export_type(Exporter *ex, Type *type, Location where) {
 	assert(type->flags & TYPE_IS_RESOLVED);
 	if (type->kind == TYPE_BUILTIN) {
-		export_u8(ex, (U8)(type->builtin + TYPE_COUNT));
+		export_u8(ex, (U8)((int)type->builtin + TYPE_COUNT));
 	} else {	
 		export_u8(ex, (U8)type->kind);
 	} 
@@ -312,6 +312,16 @@ static bool export_type(Exporter *ex, Type *type, Location where) {
 	return true;
 }
 
+static void import_type(Importer *im, Type *type) {
+	U8 kind = import_u8(im);
+	if (kind > TYPE_COUNT) {
+		type->kind = TYPE_BUILTIN;
+		type->builtin = (BuiltinType)(kind - TYPE_COUNT);
+	} else {
+		
+	}
+}
+
 static bool export_fn_ptr(Exporter *ex, FnExpr *f, Location where) {
 	if (f->export.id == 0) {
 		FnExpr **fptr = arr_add(&ex->exported_fns);
@@ -378,7 +388,7 @@ static bool export_val_ptr(Exporter *ex, void *val, Type *type, Location where)
 	case TYPE_STRUCT:
 		eval_struct_find_offsets(type);
 		arr_foreach(type->struc->fields, Field, f) {
-			if (!export_val_ptr(ex, (char *)val + f->offset, f->type, where))
+			if (!export_val_ptr(ex, (char *)val + f->offset, &f->type, where))
 				return false;
 		}
 		break;
@@ -709,7 +719,7 @@ static bool export_struct(Exporter *ex, StructDef *s) {
 	export_len(ex, arr_len(s->fields));
 	arr_foreach(s->fields, Field, f) {
 		export_ident(ex, f->name);
-		if (!export_type(ex, f->type, s->where))
+		if (!export_type(ex, &f->type, s->where))
 			return false;
 	}
 	return true;
diff --git a/parse.c b/parse.c
index 2c6fcf7..9a636b4 100644
--- a/parse.c
+++ b/parse.c
@@ -603,8 +603,7 @@ static bool parse_type(Parser *p, Type *type) {
 						Type *ftype = field_decl.type.kind == TYPE_TUPLE ? &field_decl.type.tuple[idx] : &field_decl.type;
 						Field *f = parser_arr_add(p, &type->struc->fields);
 						f->name = *fident;
-						f->type = parser_malloc(p, sizeof *f->type);
-						*f->type = *ftype;
+						f->type = *ftype;
 						++idx;
 					}
 				}
diff --git a/types.c b/types.c
index c5c00b6..06bb21b 100644
--- a/types.c
+++ b/types.c
@@ -216,7 +216,7 @@ static bool type_is_compileonly(Type *t) {
 		return false;
 	case TYPE_STRUCT:
 		arr_foreach(t->struc->fields, Field, f)
-			if (type_is_compileonly(f->type))
+			if (type_is_compileonly(&f->type))
 				return true;
 		return false;
 	case TYPE_EXPR: break;
@@ -556,7 +556,7 @@ static bool type_resolve_(Typer *tr, Type *t, Location where, bool is_reference)
 		break;
 	case TYPE_STRUCT:
 		arr_foreach(t->struc->fields, Field, f) {
-			if (!type_resolve_(tr, f->type, where, is_reference))
+			if (!type_resolve_(tr, &f->type, where, is_reference))
 				return false;
 		}
 		break;
@@ -1898,7 +1898,7 @@ static bool types_expr(Typer *tr, Expression *e) {
 				arr_foreach(lhs_type->struc->fields, Field, f) {
 					if (ident_eq_str(f->name, field_name.slice.data)) {
 						is_field = true;
-						*t = *f->type;
+						*t = f->type;
 						e->binary.field = f;
 					}
 				}
@@ -1937,7 +1937,7 @@ static bool types_expr(Typer *tr, Expression *e) {
 					arr_foreach(struct_type->struc->fields, Field, f) {
 						if (f->name == rhs->ident) {
 							is_field = true;
-							*t = *f->type;
+							*t = f->type;
 							e->binary.field = f;
 						}
 					}
diff --git a/types.h b/types.h
index 35a1bad..af14aa2 100644
--- a/types.h
+++ b/types.h
@@ -377,13 +377,6 @@ typedef enum {
 			  BUILTIN_PKG
 } BuiltinType;
 
-/* field of a struct */
-typedef struct Field {
-	Identifier name;
-	struct Type *type;
-	size_t offset; /* offset during compile time */
-} Field;
-
 
 typedef U8 Constness;
 
@@ -401,21 +394,6 @@ enum {
 	  STRUCT_DEF_CGENERATED = 0x02,
 };
 
-typedef struct {
-	Field *fields;
-	Location where;
-	U16 flags;
-	size_t size; /* size of this struct during compile time */
-	size_t align;
-	struct {
-		Identifier name;
-		IdentID id;
-	} c;
-	struct {
-		U32 id; /* (index into exptr->exported_structs) + 1, or 0 if hasn't been exported */
-	} export;
-} StructDef;
-
 enum {
 	  TYPE_IS_FLEXIBLE = 0x01,
 	  TYPE_IS_RESOLVED = 0x02,
@@ -439,11 +417,35 @@ typedef struct Type {
 		} arr;
 		struct Type *ptr;
 		struct Type *slice;
-		StructDef *struc; /* it's a pointer so that multiple Types can reference the same struct definition */
+		struct StructDef *struc; /* it's a pointer so that multiple Types can reference the same struct definition */
 		struct Expression *expr;
 	};
 } Type;
 
+
+/* field of a struct */
+typedef struct Field {
+	Identifier name;
+	Type type;
+	size_t offset; /* offset during compile time */
+} Field;
+
+typedef struct StructDef {
+	Field *fields;
+	Location where;
+	U16 flags;
+	size_t size; /* size of this struct during compile time */
+	size_t align;
+	struct {
+		Identifier name;
+		IdentID id;
+	} c;
+	struct {
+		U32 id; /* (index into exptr->exported_structs) + 1, or 0 if hasn't been exported */
+	} export;
+} StructDef;
+
+
 enum {
 	  BLOCK_IS_FN = 0x01,
 	  BLOCK_FOUND_TYPES = 0x02,
-- 
cgit v1.2.3