+ if (parser->tok != ')') {
+ parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
+ goto on_error;
+ }
+ }
+ else
+ {
+ /* for anything else just parse a typename */
+ param = parse_typename(parser, NULL);
+ if (!param)
+ goto on_error;
+ if (!paramlist_t_p_add(¶ms, param)) {
+ ast_delete(param);
+ goto on_error;
+ }
+ if (param->expression.vtype >= TYPE_VARIANT) {
+ char typename[1024];
+ ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
+ parseerror(parser, "type not supported as part of a parameter list: %s", typename);
+ goto on_error;
+ }
+ }
+ }
+
+ /* sanity check */
+ if (params.p_count > 8)
+ parseerror(parser, "more than 8 parameters are currently not supported");
+
+ /* parse-out */
+ if (!parser_next(parser)) {
+ parseerror(parser, "parse error after typename");
+ goto on_error;
+ }
+
+ /* now turn 'var' into a function type */
+ fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
+ fval->expression.next = (ast_expression*)var;
+ fval->expression.variadic = variadic;
+ var = fval;
+
+ MEM_VECTOR_MOVE(¶ms, p, &var->expression, params);
+
+ return var;
+
+on_error:
+ ast_delete(var);
+ for (i = 0; i < params.p_count; ++i)
+ ast_delete(params.p[i]);
+ MEM_VECTOR_CLEAR(¶ms, p);
+ return NULL;
+}
+
+static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
+{
+ ast_expression *cexp;
+ ast_value *cval, *tmp;
+ lex_ctx ctx;
+
+ ctx = parser_ctx(parser);
+
+ if (!parser_next(parser)) {
+ ast_delete(var);
+ parseerror(parser, "expected array-size");
+ return NULL;
+ }
+
+ cexp = parse_expression_leave(parser, true);
+
+ if (!cexp || !ast_istype(cexp, ast_value)) {
+ if (cexp)
+ ast_unref(cexp);
+ ast_delete(var);
+ parseerror(parser, "expected array-size as constant positive integer");
+ return NULL;
+ }
+ cval = (ast_value*)cexp;
+
+ tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
+ tmp->expression.next = (ast_expression*)var;
+ var = tmp;
+
+ if (cval->expression.vtype == TYPE_INTEGER)
+ tmp->expression.count = cval->constval.vint;
+ else if (cval->expression.vtype == TYPE_FLOAT)
+ tmp->expression.count = cval->constval.vfloat;
+ else {
+ ast_unref(cexp);
+ ast_delete(var);
+ parseerror(parser, "array-size must be a positive integer constant");
+ return NULL;
+ }
+ ast_unref(cexp);
+
+ if (parser->tok != ']') {
+ ast_delete(var);
+ parseerror(parser, "expected ']' after array-size");
+ return NULL;
+ }
+ if (!parser_next(parser)) {
+ ast_delete(var);
+ parseerror(parser, "error after parsing array size");
+ return NULL;
+ }
+ return var;
+}
+
+/* Parse a complete typename.
+ * for single-variables (ie. function parameters or typedefs) storebase should be NULL
+ * but when parsing variables separated by comma
+ * 'storebase' should point to where the base-type should be kept.
+ * The base type makes up every bit of type information which comes *before* the
+ * variable name.
+ *
+ * The following will be parsed in its entirety:
+ * void() foo()
+ * The 'basetype' in this case is 'void()'
+ * and if there's a comma after it, say:
+ * void() foo(), bar
+ * then the type-information 'void()' can be stored in 'storebase'
+ */
+static ast_value *parse_typename(parser_t *parser, ast_value **storebase)
+{
+ ast_value *var, *tmp;
+ lex_ctx ctx;
+
+ const char *name = NULL;
+ bool isfield = false;
+ bool wasarray = false;
+
+ ctx = parser_ctx(parser);
+
+ /* types may start with a dot */
+ if (parser->tok == '.') {
+ isfield = true;
+ /* if we parsed a dot we need a typename now */
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected typename for field definition");
+ return NULL;
+ }
+ if (parser->tok != TOKEN_TYPENAME) {
+ parseerror(parser, "expected typename");
+ return NULL;
+ }
+ }
+
+ /* generate the basic type value */
+ var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
+ /* do not yet turn into a field - remember:
+ * .void() foo; is a field too
+ * .void()() foo; is a function
+ */
+
+ /* parse on */
+ if (!parser_next(parser)) {
+ ast_delete(var);
+ parseerror(parser, "parse error after typename");
+ return NULL;
+ }
+
+ /* an opening paren now starts the parameter-list of a function
+ * this is where original-QC has parameter lists.
+ * We allow a single parameter list here.
+ * Much like fteqcc we don't allow `float()() x`
+ */
+ if (parser->tok == '(') {
+ var = parse_parameter_list(parser, var);
+ if (!var)
+ return NULL;
+ }
+
+ /* store the base if requested */
+ if (storebase) {
+ *storebase = ast_value_copy(var);
+ if (isfield) {
+ tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
+ tmp->expression.next = (ast_expression*)*storebase;
+ *storebase = tmp;
+ }
+ }
+
+ /* there may be a name now */
+ if (parser->tok == TOKEN_IDENT) {
+ name = util_strdup(parser_tokval(parser));
+ /* parse on */
+ if (!parser_next(parser)) {
+ ast_delete(var);
+ parseerror(parser, "error after variable or field declaration");
+ return NULL;
+ }
+ }
+
+ /* now this may be an array */
+ if (parser->tok == '[') {
+ wasarray = true;
+ var = parse_arraysize(parser, var);
+ if (!var)
+ return NULL;
+ }
+
+ /* This is the point where we can turn it into a field */
+ if (isfield) {
+ /* turn it into a field if desired */
+ tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
+ tmp->expression.next = (ast_expression*)var;
+ var = tmp;
+ }
+
+ /* now there may be function parens again */
+ if (parser->tok == '(' && opts_standard == COMPILER_QCC)
+ parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
+ if (parser->tok == '(' && wasarray)
+ parseerror(parser, "arrays as part of a return type is not supported");
+ while (parser->tok == '(') {
+ var = parse_parameter_list(parser, var);
+ if (!var) {
+ if (name)
+ mem_d((void*)name);
+ ast_delete(var);
+ return NULL;