aboutsummaryrefslogtreecommitdiff
path: root/src/check_expr.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-10-15 16:12:47 +0100
committergingerBill <bill@gingerbill.org>2020-10-15 16:12:47 +0100
commit5a28a7e0f568641a51f438a778fc083b7606c661 (patch)
tree0ed359d70415c3535eba900169825d436797597f /src/check_expr.cpp
parentf8e697dbbbe1dea940482c812eb5428a90a35657 (diff)
Add `intrinsics.type_field_index_of`
Diffstat (limited to 'src/check_expr.cpp')
-rw-r--r--src/check_expr.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp
index 9a48aadb7..fe43a12a6 100644
--- a/src/check_expr.cpp
+++ b/src/check_expr.cpp
@@ -5997,6 +5997,48 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
}
break;
+
+ case BuiltinProc_type_field_index_of:
+ {
+ Operand op = {};
+ Type *bt = check_type(c, ce->args[0]);
+ Type *type = base_type(bt);
+ if (type == nullptr || type == t_invalid) {
+ error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
+ return false;
+ }
+ Operand x = {};
+ check_expr(c, &x, ce->args[1]);
+
+ if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
+ error(ce->args[1], "Expected a const string for field argument");
+ return false;
+ }
+
+ String field_name = x.value.value_string;
+
+ Selection sel = lookup_field(type, field_name, false);
+ if (sel.entity == nullptr) {
+ gbString type_str = type_to_string(bt);
+ error(ce->args[0],
+ "'%s' has no field named '%.*s'", type_str, LIT(field_name));
+ gb_string_free(type_str);
+ return false;
+ }
+ if (sel.indirect) {
+ gbString type_str = type_to_string(bt);
+ error(ce->args[0],
+ "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
+ gb_string_free(type_str);
+ return false;
+ }
+
+ operand->mode = Addressing_Constant;
+ operand->value = exact_value_u64(sel.index[0]);
+ operand->type = t_uintptr;
+ break;
+ }
+ break;
}
return true;