+ if (parser->tok == TOKEN_DOTS) {
+ /* '...' indicates a varargs function */
+ variadic = true;
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected parameter");
+ return NULL;
+ }
+ 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;
+ vec_push(params, param);
+ 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 (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
+ (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
+
+ /* 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;
+
+ var->expression.params = params;
+ params = NULL;
+
+ return var;
+
+on_error:
+ ast_delete(var);
+ for (i = 0; i < vec_size(params); ++i)
+ ast_delete(params[i]);
+ vec_free(params);
+ 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;
+ }
+ }
+
+ /* finally name it */
+ if (name) {
+ if (!ast_value_set_name(var, name)) {
+ ast_delete(var);
+ parseerror(parser, "internal error: failed to set name");
+ return NULL;
+ }
+ /* free the name, ast_value_set_name duplicates */
+ mem_d((void*)name);
+ }
+
+ return var;
+}
+
+static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields)
+{
+ ast_value *var;
+ ast_value *proto;
+ ast_expression *old;
+ bool was_end;
+ size_t i;
+
+ ast_value *basetype = NULL;
+ bool retval = true;
+ bool isparam = false;
+ bool isvector = false;
+ bool cleanvar = true;
+ bool wasarray = false;
+
+ varentry_t varent, ve[3];
+
+ /* get the first complete variable */
+ var = parse_typename(parser, &basetype);
+ if (!var) {
+ if (basetype)
+ ast_delete(basetype);
+ return false;
+ }
+
+ memset(&varent, 0, sizeof(varent));
+ memset(&ve, 0, sizeof(ve));
+
+ while (true) {
+ proto = NULL;
+ wasarray = false;
+
+ /* Part 0: finish the type */
+ if (parser->tok == '(') {
+ if (opts_standard == COMPILER_QCC)
+ parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
+ var = parse_parameter_list(parser, var);
+ if (!var) {
+ retval = false;
+ goto cleanup;
+ }
+ }
+ /* we only allow 1-dimensional arrays */
+ if (parser->tok == '[') {
+ wasarray = true;
+ var = parse_arraysize(parser, var);
+ if (!var) {
+ retval = false;
+ goto cleanup;
+ }
+ }
+ if (parser->tok == '(' && wasarray) {
+ parseerror(parser, "arrays as part of a return type is not supported");
+ /* we'll still parse the type completely for now */
+ }
+ /* for functions returning functions */
+ while (parser->tok == '(') {
+ if (opts_standard == COMPILER_QCC)
+ parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
+ var = parse_parameter_list(parser, var);
+ if (!var) {