+bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_value *arr;
+ ast_value *idx;
+
+ if (!lvalue && self->expression.outr) {
+ *out = self->expression.outr;
+ }
+ if (lvalue && self->expression.outl) {
+ *out = self->expression.outl;
+ }
+
+ if (!ast_istype(self->array, ast_value)) {
+ asterror(ast_ctx(self), "array indexing this way is not supported");
+ /* note this would actually be pointer indexing because the left side is
+ * not an actual array but (hopefully) an indexable expression.
+ * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
+ * support this path will be filled.
+ */
+ return false;
+ }
+
+ arr = (ast_value*)self->array;
+ idx = (ast_value*)self->index;
+
+ if (!ast_istype(self->index, ast_value) || !idx->isconst) {
+ /* Time to use accessor functions */
+ ast_expression_codegen *cgen;
+ ir_value *iridx, *funval;
+ ir_instr *call;
+
+ if (lvalue) {
+ asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
+ return false;
+ }
+
+ if (!arr->getter) {
+ asterror(ast_ctx(self), "value has no getter, don't know how to index it");
+ return false;
+ }
+
+ cgen = self->index->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
+ return false;
+
+ cgen = arr->getter->expression.codegen;
+ if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
+ return false;
+
+ call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
+ if (!call)
+ return false;
+ ir_call_param(call, iridx);
+
+ *out = ir_call_value(call);
+ self->expression.outr = *out;
+ return true;
+ }
+
+ if (idx->expression.vtype == TYPE_FLOAT)
+ *out = arr->ir_values[(int)idx->constval.vfloat];
+ else if (idx->expression.vtype == TYPE_INTEGER)
+ *out = arr->ir_values[idx->constval.vint];
+ else {
+ asterror(ast_ctx(self), "array indexing here needs an integer constant");
+ return false;
+ }
+ return true;
+}
+