Browse Source

More code tidying with `Array`

gingerBill 7 years ago
parent
commit
69062ba3ab
7 changed files with 89 additions and 82 deletions
  1. 31 15
      src/array.cpp
  2. 14 17
      src/check_type.cpp
  3. 2 3
      src/checker.cpp
  4. 14 16
      src/ir.cpp
  5. 1 3
      src/main.cpp
  6. 13 13
      src/string.cpp
  7. 14 15
      src/types.cpp

+ 31 - 15
src/array.cpp

@@ -24,20 +24,21 @@ struct Array {
 	}
 };
 
-template <typename T> void     array_init        (Array<T> *array, gbAllocator const &a);
-template <typename T> void     array_init        (Array<T> *array, gbAllocator const &a, isize count);
-template <typename T> void     array_init        (Array<T> *array, gbAllocator const &a, isize count, isize capacity);
-template <typename T> Array<T> array_make        (gbAllocator const &a);
-template <typename T> Array<T> array_make        (gbAllocator const &a, isize count);
-template <typename T> Array<T> array_make        (gbAllocator const &a, isize count, isize capacity);
+template <typename T> void     array_init         (Array<T> *array, gbAllocator const &a);
+template <typename T> void     array_init         (Array<T> *array, gbAllocator const &a, isize count);
+template <typename T> void     array_init         (Array<T> *array, gbAllocator const &a, isize count, isize capacity);
+template <typename T> Array<T> array_make         (gbAllocator const &a);
+template <typename T> Array<T> array_make         (gbAllocator const &a, isize count);
+template <typename T> Array<T> array_make         (gbAllocator const &a, isize count, isize capacity);
 template <typename T> Array<T> array_make_from_ptr(T *data, isize count, isize capacity);
-template <typename T> void     array_free        (Array<T> *array);
-template <typename T> void     array_add         (Array<T> *array, T const &t);
-template <typename T> T        array_pop         (Array<T> *array);
-template <typename T> void     array_clear       (Array<T> *array);
-template <typename T> void     array_reserve     (Array<T> *array, isize capacity);
-template <typename T> void     array_resize      (Array<T> *array, isize count);
-template <typename T> void     array_set_capacity(Array<T> *array, isize capacity);
+template <typename T> void     array_free         (Array<T> *array);
+template <typename T> void     array_add          (Array<T> *array, T const &t);
+template <typename T> T        array_pop          (Array<T> *array);
+template <typename T> void     array_clear        (Array<T> *array);
+template <typename T> void     array_reserve      (Array<T> *array, isize capacity);
+template <typename T> void     array_resize       (Array<T> *array, isize count);
+template <typename T> void     array_set_capacity (Array<T> *array, isize capacity);
+template <typename T> Array<T> array_slice        (Array<T> const &array, isize lo, isize hi);
 
 template <typename T>
 gb_inline void array_init(Array<T> *array, gbAllocator const &a) {
@@ -102,7 +103,7 @@ gb_inline Array<T> array_make(gbAllocator const &a, isize count, isize capacity)
 
 
 template <typename T>
-void array_free(Array<T> *array) {
+gb_inline void array_free(Array<T> *array) {
 	if (array->allocator.proc != nullptr) {
 		gb_free(array->allocator, array->data);
 	}
@@ -129,7 +130,7 @@ void array_add(Array<T> *array, T const &t) {
 }
 
 template <typename T>
-T array_pop(Array<T> *array) {
+gb_inline T array_pop(Array<T> *array) {
 	GB_ASSERT(array->count > 0);
 	array->count--;
 	return array->data[array->count];
@@ -175,6 +176,21 @@ void array_set_capacity(Array<T> *array, isize capacity) {
 	array->capacity = capacity;
 }
 
+
+template <typename T>
+gb_inline Array<T> array_slice(Array<T> const &array, isize lo, isize hi) {
+	GB_ASSERT(0 <= lo && lo <= hi && hi <= array.count);
+	Array<T> out = {};
+	isize len = hi-lo;
+	if (len > 0) {
+		out.data = array.data+lo;
+		out.count = len;
+		out.capacity = len;
+	}
+	return out;
+}
+
+
 #endif
 
 #if 0

+ 14 - 17
src/check_type.cpp

@@ -792,10 +792,9 @@ void check_bit_field_type(Checker *c, Type *bit_field_type, AstNode *node) {
 	Map<Entity *> entity_map = {}; // Key: String
 	map_init(&entity_map, c->tmp_allocator, 2*(bft->fields.count));
 
-	isize field_count = 0;
-	Entity **fields  = gb_alloc_array(c->allocator, Entity *, bft->fields.count);
-	u32 *    sizes   = gb_alloc_array(c->allocator, u32,      bft->fields.count);
-	u32 *    offsets = gb_alloc_array(c->allocator, u32,      bft->fields.count);
+	auto fields  = array_make<Entity*>(c->allocator, 0, bft->fields.count);
+	auto sizes   = array_make<u32>    (c->allocator, 0, bft->fields.count);
+	auto offsets = array_make<u32>    (c->allocator, 0, bft->fields.count);
 
 	u32 curr_offset = 0;
 	for_array(i, bft->fields) {
@@ -821,13 +820,14 @@ void check_bit_field_type(Checker *c, Type *bit_field_type, AstNode *node) {
 			error(value, "Bit field bit size must be a constant integer");
 			continue;
 		}
-		i64 bits = v.value_integer;
-		if (bits < 0 || bits > 64) {
-			error(value, "Bit field's bit size must be within the range 1..<64, got %lld", cast(long long)bits);
+		i64 bits_ = v.value_integer;
+		if (bits_ < 0 || bits_ > 64) {
+			error(value, "Bit field's bit size must be within the range 1...64, got %lld", cast(long long)bits_);
 			continue;
 		}
+		u32 bits = cast(u32)bits_;
 
-		Type *value_type = make_type_bit_field_value(c->allocator, cast(i32)bits);
+		Type *value_type = make_type_bit_field_value(c->allocator, bits);
 		Entity *e = make_entity_variable(c->allocator, bit_field_type->BitField.scope, ident->Ident.token, value_type, false);
 		e->identifier = ident;
 		e->flags |= EntityFlag_BitFieldValue;
@@ -841,22 +841,19 @@ void check_bit_field_type(Checker *c, Type *bit_field_type, AstNode *node) {
 			add_entity(c, c->context.scope, nullptr, e);
 			add_entity_use(c, field, e);
 
-			fields [field_count] = e;
-			offsets[field_count] = curr_offset;
-			sizes  [field_count] = cast(i32)bits;
-			field_count++;
+			array_add(&fields,  e);
+			array_add(&offsets, curr_offset);
+			array_add(&sizes,   bits);
 
-			curr_offset += cast(i32)bits;
+			curr_offset += bits;
 		}
 	}
-	GB_ASSERT(field_count <= bft->fields.count);
+	GB_ASSERT(fields.count <= bft->fields.count);
 
 	bit_field_type->BitField.fields      = fields;
-	bit_field_type->BitField.field_count = cast(i32)field_count;
 	bit_field_type->BitField.sizes       = sizes;
 	bit_field_type->BitField.offsets     = offsets;
 
-
 	if (bft->align != nullptr) {
 		i64 custom_align = 1;
 		if (check_custom_align(c, bft->align, &custom_align)) {
@@ -1708,7 +1705,7 @@ bool check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node, Array
 	type->Proc.is_polymorphic = is_polymorphic;
 
 
-	type->Proc.abi_compat_params = gb_alloc_array(c->allocator, Type *, param_count);
+	type->Proc.abi_compat_params = array_make<Type *>(c->allocator, param_count);
 	for (isize i = 0; i < param_count; i++) {
 		Entity *e = type->Proc.params->Tuple.variables[i];
 		if (e->kind == Entity_Variable) {

+ 2 - 3
src/checker.cpp

@@ -2037,8 +2037,7 @@ String path_to_entity_name(String name, String fullpath) {
 		slash = i;
 	}
 
-	filename.text += slash;
-	filename.len -= slash;
+	filename = substring(filename, slash, filename.len);
 
 	dot = filename.len;
 	while (dot --> 0) {
@@ -2048,7 +2047,7 @@ String path_to_entity_name(String name, String fullpath) {
 		}
 	}
 
-	filename.len = dot;
+	filename = substring(filename, 0, dot);
 
 	if (is_string_an_identifier(filename)) {
 		return filename;

+ 14 - 16
src/ir.cpp

@@ -5115,8 +5115,7 @@ irValue *ir_build_expr_internal(irProcedure *proc, AstNode *expr) {
 		TypeProc *pt = &proc_type_->Proc;
 
 		if (is_call_expr_field_value(ce)) {
-			isize param_count = pt->param_count;
-			irValue **args = gb_alloc_array(proc->module->allocator, irValue *, param_count);
+			auto args = array_make<irValue *>(proc->module->allocator, pt->param_count);
 
 			for_array(arg_index, ce->args) {
 				AstNode *arg = ce->args[arg_index];
@@ -5133,7 +5132,7 @@ irValue *ir_build_expr_internal(irProcedure *proc, AstNode *expr) {
 				}
 			}
 			TypeTuple *params = &pt->params->Tuple;
-			for (isize i = 0; i < param_count; i++) {
+			for (isize i = 0; i < args.count; i++) {
 				Entity *e = params->variables[i];
 				if (e->kind == Entity_TypeName) {
 					args[i] = ir_value_nil(proc->module->allocator, e->type);
@@ -5152,8 +5151,7 @@ irValue *ir_build_expr_internal(irProcedure *proc, AstNode *expr) {
 					}
 				}
 			}
-			auto call_args = array_make_from_ptr(args, param_count, param_count);
-			return ir_emit_call(proc, value, call_args);
+			return ir_emit_call(proc, value, args);
 		}
 
 		isize arg_index = 0;
@@ -5178,7 +5176,7 @@ irValue *ir_build_expr_internal(irProcedure *proc, AstNode *expr) {
 			param_count = pt->params->Tuple.variables.count;
 		}
 
-		irValue **args = gb_alloc_array(proc->module->allocator, irValue *, gb_max(param_count, arg_count));
+		auto args = array_make<irValue *>(proc->module->allocator, gb_max(param_count, arg_count));
 		isize variadic_index = pt->variadic_index;
 		bool variadic = pt->variadic && variadic_index >= 0;
 		bool vari_expand = ce->ellipsis.pos.line != 0;
@@ -5331,7 +5329,7 @@ irValue *ir_build_expr_internal(irProcedure *proc, AstNode *expr) {
 			final_count = arg_count;
 		}
 
-		auto call_args = array_make_from_ptr(args, final_count, final_count);
+		auto call_args = array_slice(args, 0, final_count);
 		return ir_emit_call(proc, value, call_args);
 	case_end;
 
@@ -7409,7 +7407,7 @@ void ir_begin_procedure_body(irProcedure *proc) {
 				}
 			}
 		} else {
-			Type **abi_types = proc->type->Proc.abi_compat_params;
+			auto abi_types = proc->type->Proc.abi_compat_params;
 
 			for_array(i, params->variables) {
 				Entity *e = params->variables[i];
@@ -7417,8 +7415,8 @@ void ir_begin_procedure_body(irProcedure *proc) {
 					continue;
 				}
 				Type *abi_type = e->type;
-				if (abi_types != nullptr) {
-					abi_type = proc->type->Proc.abi_compat_params[i];
+				if (abi_types.count > 0) {
+					abi_type = abi_types[i];
 				}
 				if (e->token.string != "" && !is_blank_ident(e->token)) {
 					irValue *param = ir_add_param(proc, e, nullptr, abi_type);
@@ -8158,9 +8156,9 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
 			// names:   []string;
 			// bits:    []u32;
 			// offsets: []u32;
-			isize count = t->BitField.field_count;
+			isize count = t->BitField.fields.count;
 			if (count > 0) {
-				Entity **fields = t->BitField.fields;
+				auto fields = t->BitField.fields;
 				irValue *name_array   = ir_generate_array(m, t_string, count, str_lit("__$bit_field_names"),   cast(i64)entry_index);
 				irValue *bit_array    = ir_generate_array(m, t_i32,    count, str_lit("__$bit_field_bits"),    cast(i64)entry_index);
 				irValue *offset_array = ir_generate_array(m, t_i32,    count, str_lit("__$bit_field_offsets"), cast(i64)entry_index);
@@ -8471,7 +8469,7 @@ void ir_gen_tree(irGen *s) {
 		                                 proc_results, 1, false, ProcCC_StdCall);
 
 		// TODO(bill): make this more robust
-		proc_type->Proc.abi_compat_params = gb_alloc_array(a, Type *, proc_params->Tuple.variables.count);
+		proc_type->Proc.abi_compat_params = array_make<Type *>(a, proc_params->Tuple.variables.count);
 		for_array(i, proc_params->Tuple.variables) {
 			proc_type->Proc.abi_compat_params[i] = proc_params->Tuple.variables[i]->type;
 		}
@@ -8549,15 +8547,15 @@ void ir_gen_tree(irGen *s) {
 		                                 proc_results, 1, false, ProcCC_CDecl);
 
 		// TODO(bill): make this more robust
-		proc_type->Proc.abi_compat_params = gb_alloc_array(a, Type *, proc_params->Tuple.variables.count);
+		proc_type->Proc.abi_compat_params = array_make<Type *>(a, proc_params->Tuple.variables.count);
 		for_array(i, proc_params->Tuple.variables) {
 			proc_type->Proc.abi_compat_params[i] = proc_params->Tuple.variables[i]->type;
 		}
 		proc_type->Proc.abi_compat_result_type = proc_results->Tuple.variables[0]->type;
 
 		AstNode *body = gb_alloc_item(a, AstNode);
-		Entity *e = make_entity_procedure(a, nullptr, make_token_ident(name), proc_type, 0);
-		irValue *p = ir_value_procedure(a, m, e, proc_type, nullptr, body, name);
+		Entity *e     = make_entity_procedure(a, nullptr, make_token_ident(name), proc_type, 0);
+		irValue *p    = ir_value_procedure(a, m, e, proc_type, nullptr, body, name);
 
 		map_set(&m->values, hash_entity(e), p);
 		map_set(&m->members, hash_string(name), p);

+ 1 - 3
src/main.cpp

@@ -254,9 +254,7 @@ bool parse_build_flags(Array<String> args) {
 
 
 	GB_ASSERT(args.count >= 3);
-	Array<String> flag_args = args;
-	flag_args.data  += 3;
-	flag_args.count -= 3;
+	Array<String> flag_args = array_slice(args, 3, args.count);
 
 	bool set_flags[BuildFlag_COUNT] = {};
 

+ 13 - 13
src/string.cpp

@@ -82,7 +82,7 @@ gb_inline String make_string_c(char *text) {
 	return make_string(cast(u8 *)cast(void *)text, gb_strlen(text));
 }
 
-String substring(String s, isize lo, isize hi) {
+String substring(String const &s, isize lo, isize hi) {
 	isize max = s.len;
 	GB_ASSERT_MSG(lo <= hi && hi <= max, "%td..%td..%td", lo, hi, max);
 
@@ -92,14 +92,14 @@ String substring(String s, isize lo, isize hi) {
 
 
 
-gb_inline bool str_eq_ignore_case(String a, String b) {
+gb_inline bool str_eq_ignore_case(String const &a, String const &b) {
 	if (a.len == b.len) {
-		isize i;
-		for (i = 0; i < a.len; i++) {
+		for (isize i = 0; i < a.len; i++) {
 			char x = cast(char)a[i];
 			char y = cast(char)b[i];
-			if (gb_char_to_lower(x) != gb_char_to_lower(y))
+			if (gb_char_to_lower(x) != gb_char_to_lower(y)) {
 				return false;
+			}
 		}
 		return true;
 	}
@@ -180,7 +180,7 @@ template <isize N> bool operator >= (String const &a, char const (&b)[N]) { retu
 
 
 
-gb_inline bool string_starts_with(String s, String prefix) {
+gb_inline bool string_starts_with(String const &s, String const &prefix) {
 	if (prefix.len > s.len) {
 		return false;
 	}
@@ -188,7 +188,7 @@ gb_inline bool string_starts_with(String s, String prefix) {
 	return substring(s, 0, prefix.len) == prefix;
 }
 
-gb_inline bool string_ends_with(String s, String suffix) {
+gb_inline bool string_ends_with(String const &s, String const &suffix) {
 	if (suffix.len > s.len) {
 		return false;
 	}
@@ -196,7 +196,7 @@ gb_inline bool string_ends_with(String s, String suffix) {
 	return substring(s, s.len-suffix.len, s.len) == suffix;
 }
 
-gb_inline isize string_extension_position(String str) {
+gb_inline isize string_extension_position(String const &str) {
 	isize dot_pos = -1;
 	isize i = str.len;
 	while (i --> 0) {
@@ -211,7 +211,7 @@ gb_inline isize string_extension_position(String str) {
 	return dot_pos;
 }
 
-String path_extension(String str) {
+String path_extension(String const &str) {
 	isize pos = string_extension_position(str);
 	if (pos < 0) {
 		return make_string(nullptr, 0);
@@ -237,7 +237,7 @@ String string_trim_whitespace(String str) {
 	return str;
 }
 
-bool string_contains_char(String s, u8 c) {
+bool string_contains_char(String const &s, u8 c) {
 	isize i;
 	for (i = 0; i < s.len; i++) {
 		if (s[i] == c)
@@ -262,7 +262,7 @@ String filename_from_path(String s) {
 	return make_string(nullptr, 0);
 }
 
-String remove_directory_from_path(String s) {
+String remove_directory_from_path(String const &s) {
 	isize len = 0;
 	for (isize i = s.len-1; i >= 0; i--) {
 		if (s[i] == '/' ||
@@ -275,7 +275,7 @@ String remove_directory_from_path(String s) {
 }
 
 
-String concatenate_strings(gbAllocator a, String x, String y) {
+String concatenate_strings(gbAllocator a, String const &x, String const &y) {
 	isize len = x.len+y.len;
 	u8 *data = gb_alloc_array(a, u8, len+1);
 	gb_memmove(data,       x.text, x.len);
@@ -284,7 +284,7 @@ String concatenate_strings(gbAllocator a, String x, String y) {
 	return make_string(data, len);
 }
 
-String copy_string(gbAllocator a, String s) {
+String copy_string(gbAllocator a, String const &s) {
 	u8 *data = gb_alloc_array(a, u8, s.len+1);
 	gb_memmove(data, s.text, s.len);
 	data[s.len] = 0;

+ 14 - 15
src/types.cpp

@@ -96,7 +96,7 @@ struct TypeStruct {
 };
 
 #define TYPE_KINDS                                        \
-	TYPE_KIND(Basic,   BasicType)                         \
+	TYPE_KIND(Basic, BasicType)                           \
 	TYPE_KIND(Named, struct {                             \
 		String  name;                                     \
 		Type *  base;                                     \
@@ -156,7 +156,7 @@ struct TypeStruct {
 		Type *   results; /* Type_Tuple */                \
 		i32      param_count;                             \
 		i32      result_count;                            \
-		Type **  abi_compat_params;                       \
+		Array<Type *> abi_compat_params;                  \
 		Type *   abi_compat_result_type;                  \
 		bool     return_by_pointer;                       \
 		bool     variadic;                                \
@@ -172,12 +172,11 @@ struct TypeStruct {
 	})                                                    \
 	TYPE_KIND(BitFieldValue, struct { u32 bits; })        \
 	TYPE_KIND(BitField, struct {                          \
-		Scope *  scope;                                   \
-		Entity **fields;                                  \
-		i32      field_count;                             \
-		u32 *    offsets;                                 \
-		u32 *    sizes;                                   \
-		i64      custom_align;                            \
+		Array<Entity *> fields;                           \
+		Array<u32>      offsets;                          \
+		Array<u32>      sizes;                            \
+		Scope *         scope;                            \
+		i64             custom_align;                     \
 	})                                                    \
 
 
@@ -1159,9 +1158,9 @@ bool are_types_identical(Type *x, Type *y) {
 
 	case Type_BitField:
 		if (y->kind == Type_BitField) {
-			if (x->BitField.field_count == y->BitField.field_count &&
+			if (x->BitField.fields.count == y->BitField.fields.count &&
 			    x->BitField.custom_align == y->BitField.custom_align) {
-				for (i32 i = 0; i < x->BitField.field_count; i++) {
+				for (i32 i = 0; i < x->BitField.fields.count; i++) {
 					if (x->BitField.offsets[i] != y->BitField.offsets[i]) {
 						return false;
 					}
@@ -1509,7 +1508,7 @@ Selection lookup_field_from_index(gbAllocator a, Type *type, i64 index) {
 	switch (type->kind) {
 	case Type_Struct:   max_count = type->Struct.fields.count;   break;
 	case Type_Tuple:    max_count = type->Tuple.variables.count; break;
-	case Type_BitField: max_count = type->BitField.field_count;  break;
+	case Type_BitField: max_count = type->BitField.fields.count; break;
 	}
 
 	if (index >= max_count) {
@@ -1719,7 +1718,7 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n
 			}
 		}
 	} else if (type->kind == Type_BitField) {
-		for (isize i = 0; i < type->BitField.field_count; i++) {
+		for_array(i, type->BitField.fields) {
 			Entity *f = type->BitField.fields[i];
 			if (f->kind != Entity_Variable ||
 			    (f->flags & EntityFlag_BitFieldValue) == 0) {
@@ -2172,8 +2171,8 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
 	case Type_BitField: {
 		i64 align = 8*type_align_of_internal(allocator, t, path);
 		i64 end = 0;
-		if (t->BitField.field_count > 0) {
-			i64 last = t->BitField.field_count-1;
+		if (t->BitField.fields.count > 0) {
+			i64 last = t->BitField.fields.count-1;
 			end = t->BitField.offsets[last] + t->BitField.sizes[last];
 		}
 		i64 bits = align_formula(end, align);
@@ -2472,7 +2471,7 @@ gbString write_type_to_string(gbString str, Type *type) {
 		}
 		str = gb_string_append_rune(str, '{');
 
-		for (isize i = 0; i < type->BitField.field_count; i++) {
+		for_array(i, type->BitField.fields)	{
 			Entity *f = type->BitField.fields[i];
 			GB_ASSERT(f->kind == Entity_Variable);
 			GB_ASSERT(f->type != nullptr && f->type->kind == Type_BitFieldValue);