+ /* for anything else just parse a typename */
+ param = parse_typename(parser, NULL);
+ if (!param)
+ goto on_error;
+ if (!paramlist_t_p_add(¶ms, param))
+ 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;
+}
+
+/* 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;
+
+ 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 */
+ if (parser->tok == '(') {
+ var = parse_parameter_list(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;
+ }
+
+ while (parser->tok == '(') {
+ var = parse_parameter_list(parser, var);
+ if (!var)
+ return NULL;
+ }
+
+ /* store the base if requested */
+ if (storebase) {
+ *storebase = ast_value_copy(var);
+ }
+
+ /* there may be a name now */
+ if (parser->tok == TOKEN_IDENT) {
+ name = util_strdup(parser_tokval(parser));
+ /* parse on */
+ if (!parser_next(parser)) {
+ parseerror(parser, "error after variable or field declaration");
+ return NULL;
+ }
+ }
+
+ /* 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");
+ while (parser->tok == '(') {
+ var = parse_parameter_list(parser, var);
+ if (!var) {
+ if (name)
+ mem_d((void*)name);
+ 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)
+{
+ 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;
+
+ 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;
+
+ /* Part 0: finish the type */
+ 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) {
+ retval = false;
+ goto cleanup;
+ }
+ }
+
+ /* Part 1:
+ * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
+ * Also: if there was a prototype, `var` will be deleted and set to `proto` which
+ * is then filled with the previous definition and the parameter-names replaced.
+ */
+ if (!localblock) {
+ /* Deal with end_sys_ vars */
+ was_end = false;
+ if (!strcmp(var->name, "end_sys_globals")) {
+ parser->crc_globals = parser->globals_count;
+ was_end = true;
+ }
+ else if (!strcmp(var->name, "end_sys_fields")) {
+ parser->crc_fields = parser->fields_count;
+ was_end = true;
+ }
+ if (was_end && var->expression.vtype == TYPE_FIELD) {
+ if (parsewarning(parser, WARN_END_SYS_FIELDS,
+ "global '%s' hint should not be a field",
+ parser_tokval(parser)))
+ {
+ retval = false;
+ goto cleanup;
+ }
+ }
+
+ if (var->expression.vtype == TYPE_FIELD)