aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2018-06-12 19:10:14 +0100
committergingerBill <bill@gingerbill.org>2018-06-12 19:10:14 +0100
commite9e7ce2606ae18dd96fde356860fe613ff5c5430 (patch)
tree513221bea901cbba620f5ebe3cc32d4617bd9cb8 /src
parent915dcb0c28623e1221729cb46f998c5d6435689b (diff)
Allow `.allocator` for dynamic arrays; Add `mem.Pool`
Diffstat (limited to 'src')
-rw-r--r--src/check_expr.cpp3
-rw-r--r--src/checker.cpp4
-rw-r--r--src/ir.cpp14
-rw-r--r--src/types.cpp108
4 files changed, 71 insertions, 58 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp
index 855be62e1..59f285ed1 100644
--- a/src/check_expr.cpp
+++ b/src/check_expr.cpp
@@ -2648,6 +2648,9 @@ Entity *check_selector(CheckerContext *c, Operand *operand, AstNode *node, Type
if (entity == nullptr && selector->kind == AstNode_Ident) {
String field_name = selector->Ident.token.string;
+ if (is_type_dynamic_array(type_deref(operand->type))) {
+ init_mem_allocator(c->checker);
+ }
sel = lookup_field(operand->type, field_name, operand->mode == Addressing_Type);
entity = sel.entity;
diff --git a/src/checker.cpp b/src/checker.cpp
index d874a924e..3ec95e842 100644
--- a/src/checker.cpp
+++ b/src/checker.cpp
@@ -1588,7 +1588,7 @@ void init_core_type_info(Checker *c) {
t_type_info_bit_field_ptr = alloc_type_pointer(t_type_info_bit_field);
}
-void init_core_allocator(Checker *c) {
+void init_mem_allocator(Checker *c) {
if (t_allocator != nullptr) {
return;
}
@@ -1633,7 +1633,7 @@ void init_core_map_type(Checker *c) {
void init_preload(Checker *c) {
init_core_type_info(c);
- init_core_allocator(c);
+ init_mem_allocator(c);
init_core_context(c);
init_core_source_code_location(c);
init_core_map_type(c);
diff --git a/src/ir.cpp b/src/ir.cpp
index 507cfd0fc..e60de290b 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -2631,9 +2631,9 @@ irValue *ir_emit_struct_ep(irProcedure *proc, irValue *s, i32 index) {
} else if (is_type_dynamic_array(t)) {
switch (index) {
case 0: result_type = alloc_type_pointer(alloc_type_pointer(t->DynamicArray.elem)); break;
- case 1: result_type = t_int_ptr; break;
- case 2: result_type = t_int_ptr; break;
- case 3: result_type = t_allocator_ptr; break;
+ case 1: result_type = t_int_ptr; break;
+ case 2: result_type = t_int_ptr; break;
+ case 3: result_type = t_allocator_ptr; break;
}
} /* else if (is_type_map(t)) {
init_map_internal_types(t);
@@ -2650,7 +2650,7 @@ irValue *ir_emit_struct_ep(irProcedure *proc, irValue *s, i32 index) {
GB_PANIC("TODO(bill): struct_gep type: %s, %d", type_to_string(ir_type(s)), index);
}
- GB_ASSERT(result_type != nullptr);
+ GB_ASSERT_MSG(result_type != nullptr, "%s %d", type_to_string(t), index);
return ir_emit(proc, ir_instr_struct_element_ptr(proc, s, index, result_type));
}
@@ -2708,9 +2708,9 @@ irValue *ir_emit_struct_ev(irProcedure *proc, irValue *s, i32 index) {
case Type_DynamicArray:
switch (index) {
case 0: result_type = alloc_type_pointer(t->DynamicArray.elem); break;
- case 1: result_type = t_int; break;
- case 2: result_type = t_int; break;
- case 3: result_type = t_allocator; break;
+ case 1: result_type = t_int; break;
+ case 2: result_type = t_int; break;
+ case 3: result_type = t_allocator; break;
}
break;
diff --git a/src/types.cpp b/src/types.cpp
index 13e069a15..a28bbf608 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -1611,55 +1611,6 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
type = base_type(type);
- if (type->kind == Type_Basic) {
- switch (type->Basic.kind) {
- case Basic_any: {
- #if 1
- // IMPORTANT TODO(bill): Should these members be available to should I only allow them with
- // `Raw_Any` type?
- String data_str = str_lit("data");
- String typeid_str = str_lit("typeid");
- gb_local_persist Entity *entity__any_data = alloc_entity_field(nullptr, make_token_ident(data_str), t_rawptr, false, 0);
- gb_local_persist Entity *entity__any_typeid = alloc_entity_field(nullptr, make_token_ident(typeid_str), t_typeid, false, 1);
-
- if (field_name == data_str) {
- selection_add_index(&sel, 0);
- sel.entity = entity__any_data;;
- return sel;
- } else if (field_name == typeid_str) {
- selection_add_index(&sel, 1);
- sel.entity = entity__any_typeid;
- return sel;
- }
- #endif
- } break;
- }
-
- return sel;
- } else if (type->kind == Type_Array) {
- if (type->Array.count <= 4) {
- // HACK(bill): Memory leak
- switch (type->Array.count) {
- #define _ARRAY_FIELD_CASE(_length, _name) \
- case (_length): \
- if (field_name == _name) { \
- selection_add_index(&sel, (_length)-1); \
- sel.entity = alloc_entity_array_elem(nullptr, make_token_ident(str_lit(_name)), type->Array.elem, (_length)-1); \
- return sel; \
- } \
- /*fallthrough*/
-
- _ARRAY_FIELD_CASE(4, "w");
- _ARRAY_FIELD_CASE(3, "z");
- _ARRAY_FIELD_CASE(2, "y");
- _ARRAY_FIELD_CASE(1, "x");
- default: break;
-
- #undef _ARRAY_FIELD_CASE
- }
- }
- }
-
if (is_type) {
switch (type->kind) {
case Type_Struct:
@@ -1769,6 +1720,65 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
return sel;
}
}
+ } else if (type->kind == Type_Basic) {
+ switch (type->Basic.kind) {
+ case Basic_any: {
+ #if 1
+ // IMPORTANT TODO(bill): Should these members be available to should I only allow them with
+ // `Raw_Any` type?
+ String data_str = str_lit("data");
+ String typeid_str = str_lit("typeid");
+ gb_local_persist Entity *entity__any_data = alloc_entity_field(nullptr, make_token_ident(data_str), t_rawptr, false, 0);
+ gb_local_persist Entity *entity__any_typeid = alloc_entity_field(nullptr, make_token_ident(typeid_str), t_typeid, false, 1);
+
+ if (field_name == data_str) {
+ selection_add_index(&sel, 0);
+ sel.entity = entity__any_data;
+ return sel;
+ } else if (field_name == typeid_str) {
+ selection_add_index(&sel, 1);
+ sel.entity = entity__any_typeid;
+ return sel;
+ }
+ #endif
+ } break;
+ }
+
+ return sel;
+ } else if (type->kind == Type_Array) {
+ if (type->Array.count <= 4) {
+ // HACK(bill): Memory leak
+ switch (type->Array.count) {
+ #define _ARRAY_FIELD_CASE(_length, _name) \
+ case (_length): \
+ if (field_name == _name) { \
+ selection_add_index(&sel, (_length)-1); \
+ sel.entity = alloc_entity_array_elem(nullptr, make_token_ident(str_lit(_name)), type->Array.elem, (_length)-1); \
+ return sel; \
+ } \
+ /*fallthrough*/
+
+ _ARRAY_FIELD_CASE(4, "w");
+ _ARRAY_FIELD_CASE(3, "z");
+ _ARRAY_FIELD_CASE(2, "y");
+ _ARRAY_FIELD_CASE(1, "x");
+ default: break;
+
+ #undef _ARRAY_FIELD_CASE
+ }
+ }
+ } else if (type->kind == Type_DynamicArray) {
+ // IMPORTANT TODO(bill): Should these members be available to should I only allow them with
+ // `Raw_Dynamic_Array` type?
+ GB_ASSERT(t_allocator != nullptr);
+ String allocator_str = str_lit("allocator");
+ gb_local_persist Entity *entity__allocator = alloc_entity_field(nullptr, make_token_ident(allocator_str), t_allocator, false, 0);
+
+ if (field_name == allocator_str) {
+ selection_add_index(&sel, 3);
+ sel.entity = entity__allocator;
+ return sel;
+ }
}
return sel;