Browse Source

Fix `using` determination order

gingerBill 7 years ago
parent
commit
41b6d215bb
6 changed files with 19 additions and 29 deletions
  1. 3 3
      src/check_expr.cpp
  2. 10 19
      src/check_type.cpp
  3. 1 1
      src/checker.cpp
  4. 4 4
      src/ir.cpp
  5. 1 1
      src/ir_print.cpp
  6. 0 1
      src/types.cpp

+ 3 - 3
src/check_expr.cpp

@@ -3621,7 +3621,7 @@ break;
 		isize variable_count = type->Struct.fields.count;
 		array_init(&tuple->Tuple.variables, a, variable_count);
 		// TODO(bill): Should I copy each of the entities or is this good enough?
-		gb_memcopy_array(tuple->Tuple.variables.data, type->Struct.fields_in_src_order.data, variable_count);
+		gb_memcopy_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
 
 		operand->type = tuple;
 		operand->mode = Addressing_Value;
@@ -5370,7 +5370,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
 				isize field_count = t->Struct.fields.count;
 				isize min_field_count = t->Struct.fields.count;
 				for (isize i = min_field_count-1; i >= 0; i--) {
-					Entity *e = t->Struct.fields_in_src_order[i];
+					Entity *e = t->Struct.fields[i];
 					GB_ASSERT(e->kind == Entity_Variable);
 					if (e->Variable.default_is_nil) {
 						min_field_count--;
@@ -5454,7 +5454,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
 						}
 
 						if (field == nullptr) {
-							field = t->Struct.fields_in_src_order[index];
+							field = t->Struct.fields[index];
 						}
 
 						check_expr_with_type_hint(c, o, elem, field->type);

+ 10 - 19
src/check_type.cpp

@@ -34,13 +34,12 @@ void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *>
 
 }
 
-// Returns filled field_count
-Array<Entity *> check_struct_fields(Checker *c, AstNode *node, Array<AstNode *> params,
-                                    isize init_field_capacity, Type *named_type, String context) {
+void check_struct_fields(Checker *c, AstNode *node, Array<Entity *> *fields, Array<AstNode *> params,
+                         isize init_field_capacity, Type *named_type, String context) {
 	gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
 	defer (gb_temp_arena_memory_end(tmp));
 
-	auto fields = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
+	*fields = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
 
 	Map<Entity *> entity_map = {};
 	map_init(&entity_map, c->tmp_allocator, 2*init_field_capacity);
@@ -185,14 +184,14 @@ Array<Entity *> check_struct_fields(Checker *c, AstNode *node, Array<AstNode *>
 			field->Variable.default_is_nil = default_is_nil;
 
 			add_entity(c, c->context.scope, name, field);
-			array_add(&fields, field);
+			array_add(fields, field);
 
 			field_src_index += 1;
 		}
 
 
 		if (is_using && p->names.count > 0) {
-			Type *first_type = fields[fields.count-1]->type;
+			Type *first_type = (*fields)[fields->count-1]->type;
 			Type *t = base_type(type_deref(first_type));
 
 			if (!is_type_struct(t) && !is_type_raw_union(t) && !is_type_bit_field(t) &&
@@ -208,8 +207,6 @@ Array<Entity *> check_struct_fields(Checker *c, AstNode *node, Array<AstNode *>
 			populate_using_entity_map(c, node, type, &entity_map);
 		}
 	}
-
-	return fields;
 }
 
 
@@ -544,17 +541,13 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera
 	struct_type->Struct.is_polymorphic          = is_polymorphic;
 	struct_type->Struct.is_poly_specialized     = is_poly_specialized;
 
-	Array<Entity *> fields = {};
 
 	if (!is_polymorphic) {
-		fields = check_struct_fields(c, node, st->fields, min_field_count, named_type, context);
+		check_struct_fields(c, node, &struct_type->Struct.fields, st->fields, min_field_count, named_type, context);
 	}
 
-	struct_type->Struct.fields              = fields;
-	struct_type->Struct.fields_in_src_order = fields;
-
-	for_array(i, fields) {
-		Entity *f = fields[i];
+	for_array(i, struct_type->Struct.fields) {
+		Entity *f = struct_type->Struct.fields[i];
 		if (f->kind == Entity_Variable) {
 			if (f->Variable.default_value.kind == ExactValue_Procedure) {
 				struct_type->Struct.has_proc_default_values = true;
@@ -1801,8 +1794,7 @@ void generate_map_entry_type(gbAllocator a, Type *type) {
 	array_add(&fields, make_entity_field(a, s, make_token_ident(str_lit("value")), type->Map.value, false, 2));
 
 
-	entry_type->Struct.fields              = fields;
-	entry_type->Struct.fields_in_src_order = fields;
+	entry_type->Struct.fields = fields;
 
 	// type_set_offsets(a, entry_type);
 	type->Map.entry_type = entry_type;
@@ -1839,8 +1831,7 @@ void generate_map_internal_types(gbAllocator a, Type *type) {
 	array_add(&fields, make_entity_field(a, s, make_token_ident(str_lit("hashes")),  hashes_type,  false, 0));
 	array_add(&fields, make_entity_field(a, s, make_token_ident(str_lit("entries")), entries_type, false, 1));
 
-	generated_struct_type->Struct.fields              = fields;
-	generated_struct_type->Struct.fields_in_src_order = fields;
+	generated_struct_type->Struct.fields = fields;
 
 	type_set_offsets(a, generated_struct_type);
 	type->Map.generated_struct_type = generated_struct_type;

+ 1 - 1
src/checker.cpp

@@ -1347,7 +1347,7 @@ void init_preload(Checker *c) {
 
 		GB_ASSERT(tis->fields.count == 3);
 
-		Entity *type_info_variant = tis->fields_in_src_order[2];
+		Entity *type_info_variant = tis->fields[2];
 		Type *tiv_type = type_info_variant->type;
 		GB_ASSERT(is_type_union(tiv_type));
 

+ 4 - 4
src/ir.cpp

@@ -683,7 +683,7 @@ bool ir_type_has_default_values(Type *t) {
 	case Type_Struct:
 		if (t->Struct.is_raw_union) return false;
 		for_array(i, t->Struct.fields) {
-			Entity *f = t->Struct.fields_in_src_order[i];
+			Entity *f = t->Struct.fields[i];
 			if (f->kind != Entity_Variable) continue;
 			if (f->Variable.default_is_nil) {
 				// NOTE(bill): This is technically zero
@@ -4735,7 +4735,7 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv
 
 		irValue *tuple = ir_add_local_generated(proc, tv.type);
 		for_array(src_index, t->Struct.fields) {
-			Entity *field = t->Struct.fields_in_src_order[src_index];
+			Entity *field = t->Struct.fields[src_index];
 			i32 field_index = field->Variable.field_index;
 			irValue *f = ir_emit_struct_ev(proc, s, field_index);
 			irValue *ep = ir_emit_struct_ep(proc, tuple, cast(i32)src_index);
@@ -5851,7 +5851,7 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) {
 					} else {
 						TypeAndValue tav = type_and_value_of_expr(proc->module->info, elem);
 						Selection sel = lookup_field_from_index(proc->module->allocator, bt,
-						                                        st->fields_in_src_order[field_index]->Variable.field_src_index);
+						                                        st->fields[field_index]->Variable.field_src_index);
 						index = sel.index[0];
 					}
 
@@ -8141,7 +8141,7 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
 			type_set_offsets(a, t); // NOTE(bill): Just incase the offsets have not been set yet
 			for (isize source_index = 0; source_index < count; source_index++) {
 				// TODO(bill): Order fields in source order not layout order
-				Entity *f = t->Struct.fields_in_src_order[source_index];
+				Entity *f = t->Struct.fields[source_index];
 				irValue *tip = ir_get_type_info_ptr(proc, f->type);
 				i64 foffset = 0;
 				if (!t->Struct.is_raw_union) {

+ 1 - 1
src/ir_print.cpp

@@ -731,7 +731,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
 					}
 				} else {
 					for_array(i, cl->elems) {
-						Entity *f = type->Struct.fields_in_src_order[i];
+						Entity *f = type->Struct.fields[i];
 						TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[i]);
 						ExactValue val = {};
 						if (tav.mode != Addressing_Invalid) {

+ 0 - 1
src/types.cpp

@@ -77,7 +77,6 @@ struct BasicType {
 
 struct TypeStruct {
 	Array<Entity *> fields;
-	Array<Entity *> fields_in_src_order;
 	AstNode *node;
 	Scope *  scope;