From 36c0b45babfec89c73b83e9aca38ff9294a32e3a Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 11 Dec 2024 13:54:11 +0900 Subject: [PATCH] [qfcc] Add an @construct type function I decided to not mess with actual casts. In retrospect, this is probably for the best as it avoids any bit-cast conflicts, but should allow for block initializers with a few tweaks. `@construct (type, args...)` in Ruamoko follows the same path as `type(args...)` in glsl. --- tools/qfcc/source/expr_call.c | 2 +- tools/qfcc/source/expr_construct.c | 2 +- tools/qfcc/source/expr_process.c | 2 +- tools/qfcc/source/glsl-builtins.c | 4 ++-- tools/qfcc/source/glsl-parse.y | 9 +-------- tools/qfcc/source/qc-parse.y | 14 +++++++++++++- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/tools/qfcc/source/expr_call.c b/tools/qfcc/source/expr_call.c index d26b1e67c..5edec8c9e 100644 --- a/tools/qfcc/source/expr_call.c +++ b/tools/qfcc/source/expr_call.c @@ -416,7 +416,7 @@ build_function_call (const expr_t *fexpr, const type_t *ftype, const expr_t * function_expr (const expr_t *fexpr, const expr_t *args) { - if (fexpr->type == ex_symbol && fexpr->symbol->sy_type == sy_type) { + if (fexpr->type == ex_type) { return constructor_expr (fexpr, args); } diff --git a/tools/qfcc/source/expr_construct.c b/tools/qfcc/source/expr_construct.c index 97f7914a4..726cd74d5 100644 --- a/tools/qfcc/source/expr_construct.c +++ b/tools/qfcc/source/expr_construct.c @@ -287,7 +287,7 @@ math_constructor (const type_t *type, const expr_t *params, const expr_t *e) const expr_t * constructor_expr (const expr_t *e, const expr_t *params) { - auto type = e->symbol->type; + auto type = e->typ.type; if (is_algebra (type)) { return error (e, "algebra not implemented"); } diff --git a/tools/qfcc/source/expr_process.c b/tools/qfcc/source/expr_process.c index a2658e53b..70b82f2e1 100644 --- a/tools/qfcc/source/expr_process.c +++ b/tools/qfcc/source/expr_process.c @@ -337,7 +337,7 @@ proc_symbol (const expr_t *expr, rua_ctx_t *ctx) } sym = symtab_lookup (current_symtab, sym->name); if (sym) { - if (sym->sy_type == sy_expr) { + if (sym->sy_type == sy_expr || sym->sy_type == sy_type_param) { return sym->expr; } if (sym->sy_type == sy_convert) { diff --git a/tools/qfcc/source/glsl-builtins.c b/tools/qfcc/source/glsl-builtins.c index 87028c31b..dd48c722e 100644 --- a/tools/qfcc/source/glsl-builtins.c +++ b/tools/qfcc/source/glsl-builtins.c @@ -438,10 +438,10 @@ SRC_LINE "genUType clamp(genUType x, uint minVal, uint maxVal);" "\n" "genFType mix(genFType x, genFType y, genFType a) = " GLSL(46) ";" "\n" "genFType mix(genFType x, genFType y, float a)" "\n" -"{ return mix (x, y, (genFType) a); }" "\n" +"{ return mix (x, y, @construct (genFType, a)); }" "\n" "genDType mix(genDType x, genDType y, genDType a) = " GLSL(46) ";" "\n" "genDType mix(genDType x, genDType y, double a)" "\n" -"{ return mix (x, y, (genDType) a); }" "\n" +"{ return mix (x, y, @construct (genDType, a)); }" "\n" "genFType mix(genFType x, genFType y, genBType a) = " SPV(169) ";" "\n" "genDType mix(genDType x, genDType y, genBType a) = " SPV(169) ";" "\n" "genIType mix(genIType x, genIType y, genBType a) = " SPV(169) ";" "\n" diff --git a/tools/qfcc/source/glsl-parse.y b/tools/qfcc/source/glsl-parse.y index 32ccd1238..3ca5f062d 100644 --- a/tools/qfcc/source/glsl-parse.y +++ b/tools/qfcc/source/glsl-parse.y @@ -436,14 +436,7 @@ function_call_header ; function_identifier - : type_specifier - { - auto type = $1.type; - auto sym = new_symbol (type->name); - sym->sy_type = sy_type; - sym->type = type; - $$ = new_symbol_expr (sym); - } + : type_specifier { $$ = new_type_expr ($1.type); } | postfix_expression ; diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index 44606b544..5b16cf5b8 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -163,7 +163,7 @@ int yylex (YYSTYPE *yylval, YYLTYPE *yylloc); %token CLASS DEFS ENCODE END IMPLEMENTATION INTERFACE PRIVATE %token PROTECTED PROTOCOL PUBLIC SELECTOR REFERENCE SELF THIS -%token GENERIC +%token GENERIC CONSTRUCT %token AT_FUNCTION AT_FIELD AT_POINTER AT_ARRAY %token AT_BASE AT_WIDTH AT_VECTOR AT_ROWS AT_COLS AT_MATRIX %token AT_INT AT_UINT AT_BOOL AT_FLOAT @@ -2153,6 +2153,17 @@ cast_expr } $$ = new_binary_expr ('C', type_expr, $4); } + | CONSTRUCT '(' typename ',' expr_list[args] ')' //FIXME arg_expr instead? + { + auto spec = $3; + auto args = $args; + auto type_expr = spec.type_expr; + if (!type_expr) { + spec = default_type ($typename, nullptr); + type_expr = new_type_expr (spec.type); + } + $$ = new_call_expr (type_expr, args, nullptr); + } | unary_expr %prec LOW ; @@ -3024,6 +3035,7 @@ static keyword_t qf_keywords[] = { {"@dual", QC_DUAL, }, {"@undual", QC_UNDUAL, }, + {"@construct", QC_CONSTRUCT, }, {"@generic", QC_GENERIC, }, {"@function", QC_AT_FUNCTION, }, {"@field", QC_AT_FIELD, },