From ae4c1073849ff361d4dc937d1e1e828b06095563 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 3 Jan 2025 17:55:08 +0900 Subject: [PATCH] [qfcc] Add type_process for type functions I'm not quite sure it's what I need, but it does help with resolving types with complex expressions (eg, `@vector(bool,@width(vec))`). --- tools/qfcc/include/expr.h | 1 + tools/qfcc/source/expr_process.c | 3 +-- tools/qfcc/source/expr_type.c | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index a6f89588f..5963361f8 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -1050,6 +1050,7 @@ expr_t *new_type_function (int op, const expr_t *params); const expr_t *type_function (int op, const expr_t *params); symbol_t *type_parameter (symbol_t *sym, const expr_t *type); const type_t *resolve_type (const expr_t *te, rua_ctx_t *ctx); +const expr_t *process_type (const expr_t *te, rua_ctx_t *ctx); const type_t **expand_type (const expr_t *te, rua_ctx_t *ctx); const expr_t *evaluate_type (const expr_t *te, rua_ctx_t *ctx); diff --git a/tools/qfcc/source/expr_process.c b/tools/qfcc/source/expr_process.c index 2be78e295..8be6dfcb0 100644 --- a/tools/qfcc/source/expr_process.c +++ b/tools/qfcc/source/expr_process.c @@ -571,8 +571,7 @@ static const expr_t * proc_type (const expr_t *expr, rua_ctx_t *ctx) { scoped_src_loc (expr); - auto type = resolve_type (expr, ctx); - return new_type_expr (type); + return process_type (expr, ctx); } static const expr_t * diff --git a/tools/qfcc/source/expr_type.c b/tools/qfcc/source/expr_type.c index 72e65b9da..d3cc30921 100644 --- a/tools/qfcc/source/expr_type.c +++ b/tools/qfcc/source/expr_type.c @@ -174,6 +174,9 @@ evaluate_int (const expr_t *expr, rua_ctx_t *ctx) if (is_integral_val (expr)) { return expr; } + if (expr->type != ex_type) { + internal_error (expr, "invalid type op"); + } int op = expr->typ.op; int ind = op - QC_GENERIC; auto type = expr->typ.type; @@ -744,6 +747,37 @@ resolve_type (const expr_t *te, rua_ctx_t *ctx) return type_funcs[ind].resolve (arg_count, args, ctx); } +const expr_t * +process_type (const expr_t *te, rua_ctx_t *ctx) +{ + if (te->type != ex_type) { + internal_error (te, "not a type expression"); + } + if (!te->typ.op) { + if (!te->typ.type) { + internal_error (te, "no type in reference"); + } + return new_type_expr (te->typ.type); + } + int op = te->typ.op; + unsigned ind = op - QC_GENERIC; + if (ind >= sizeof (type_funcs) / sizeof (type_funcs[0]) + || !type_funcs[ind].name) { + internal_error (te, "invalid type op: %d", op); + } + int arg_count = list_count (&te->typ.params->list); + const expr_t *args[arg_count]; + list_scatter (&te->typ.params->list, args); + if (type_funcs[ind].resolve) { + auto type = type_funcs[ind].resolve (arg_count, args, ctx); + return new_type_expr (type); + } else if (type_funcs[ind].evaluate) { + return type_funcs[ind].evaluate (arg_count, args, ctx); + } else { + internal_error (te, "invalid type op: %s", type_funcs[ind].name); + } +} + const type_t ** expand_type (const expr_t *te, rua_ctx_t *ctx) {