]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
Declaration of type-restricted varargs
[xonotic/gmqcc.git] / parser.c
index e38cedae42fe4624d009ab404a42c2bc13d77cb2..819c1e92030ff8617f05031c3529449ace103669 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -54,6 +54,7 @@ typedef struct {
     ast_value *imm_float_one;
     ast_value *imm_vector_zero;
     ast_value *nil;
+    ast_value *reserved_version;
 
     size_t crc_globals;
     size_t crc_fields;
@@ -99,6 +100,9 @@ typedef struct {
 
     /* pragma flags */
     bool noref;
+
+    /* collected information */
+    size_t     max_param_count;
 } parser_t;
 
 static const ast_expression *intrinsic_debug_typestring = (ast_expression*)0x10;
@@ -1402,6 +1406,8 @@ static bool parser_close_call(parser_t *parser, shunt *sy)
             params->exprs = NULL;
             ast_delete(params);
         }
+        if (parser->max_param_count < paramcount)
+            parser->max_param_count = paramcount;
         (void)!ast_call_check_types(call);
     } else {
         parseerror(parser, "invalid function call");
@@ -2749,7 +2755,7 @@ static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *
             else if (!strcmp(parser_tokval(parser), "deprecated") && !(flags & AST_FLAG_DEPRECATED)) {
                 flags   |= AST_FLAG_DEPRECATED;
                 *message = NULL;
-                
+
                 if (!parser_next(parser)) {
                     parseerror(parser, "parse error in attribute");
                     goto argerr;
@@ -2802,7 +2808,7 @@ static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *
                 }
             }
         }
-        else if (!strcmp(parser_tokval(parser), "static"))
+        else if (with_local && !strcmp(parser_tokval(parser), "static"))
             had_static = true;
         else if (!strcmp(parser_tokval(parser), "const"))
             had_const = true;
@@ -4285,6 +4291,7 @@ static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
     ast_value  *fval;
     bool        first = true;
     bool        variadic = false;
+    ast_value  *varparam = NULL;
 
     ctx = parser_ctx(parser);
 
@@ -4314,11 +4321,7 @@ static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
         if (parser->tok == TOKEN_DOTS) {
             /* '...' indicates a varargs function */
             variadic = true;
-            if (!parser_next(parser)) {
-                parseerror(parser, "expected parameter");
-                return NULL;
-            }
-            if (parser->tok != ')') {
+            if (!parser_next(parser) || parser->tok != ')') {
                 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
                 goto on_error;
             }
@@ -4336,6 +4339,16 @@ static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
                 parseerror(parser, "type not supported as part of a parameter list: %s", tname);
                 goto on_error;
             }
+            /* type-restricted varargs */
+            if (parser->tok == TOKEN_DOTS) {
+                variadic = true;
+                varparam = vec_last(params);
+                vec_pop(params);
+                if (!parser_next(parser) || parser->tok != ')') {
+                    parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
+                    goto on_error;
+                }
+            }
         }
     }
 
@@ -4359,7 +4372,8 @@ static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
         fval->expression.flags |= AST_FLAG_VARIADIC;
     var = fval;
 
-    var->expression.params = params;
+    var->expression.params   = params;
+    var->expression.varparam = (ast_expression*)varparam;
     params = NULL;
 
     return var;
@@ -5447,6 +5461,16 @@ bool parser_init()
     parser->const_vec[0] = ast_value_new(empty_ctx, "<vector.x>", TYPE_NOEXPR);
     parser->const_vec[1] = ast_value_new(empty_ctx, "<vector.y>", TYPE_NOEXPR);
     parser->const_vec[2] = ast_value_new(empty_ctx, "<vector.z>", TYPE_NOEXPR);
+
+    if (opts.add_info) {
+        parser->reserved_version = ast_value_new(empty_ctx, "reserved:version", TYPE_STRING);
+        parser->reserved_version->cvq = CV_CONST;
+        parser->reserved_version->hasvalue = true;
+        parser->reserved_version->expression.flags |= AST_FLAG_INCLUDE_DEF;
+        parser->reserved_version->constval.vstring = util_strdup(GMQCC_FULL_VERSION_STRING);
+    } else {
+        parser->reserved_version = NULL;
+    }
     return true;
 }
 
@@ -5632,6 +5656,13 @@ bool parser_finish(const char *output)
             return false;
         }
     }
+    if (parser->reserved_version &&
+        !ast_global_codegen(parser->reserved_version, ir, false))
+    {
+        con_out("failed to generate reserved::version");
+        ir_builder_delete(ir);
+        return false;
+    }
     for (i = 0; i < vec_size(parser->imm_float); ++i) {
         if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
             con_out("failed to generate global %s\n", parser->imm_float[i]->name);