+ parseerror(parser, "parse error");
+ ast_delete(ontrue);
+ ast_delete(cond);
+ return false;
+ }
+
+ cond = process_condition(parser, cond, &ifnot);
+ if (!cond) {
+ ast_delete(ontrue);
+ return false;
+ }
+ aloop = ast_loop_new(ctx, NULL, NULL, false, cond, ifnot, NULL, ontrue);
+ *out = (ast_expression*)aloop;
+ return true;
+}
+
+static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out);
+static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
+{
+ bool rv;
+ char *label = NULL;
+
+ /* skip the 'for' and check for opening paren */
+ if (!parser_next(parser)) {
+ if (OPTS_FLAG(LOOP_LABELS))
+ parseerror(parser, "expected loop label or 'for' expressions in parenthesis");
+ else
+ parseerror(parser, "expected 'for' expressions in parenthesis");
+ return false;
+ }
+
+ if (parser->tok == ':') {
+ if (!OPTS_FLAG(LOOP_LABELS))
+ parseerror(parser, "labeled loops not activated, try using -floop-labels");
+ if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
+ parseerror(parser, "expected loop label");
+ return false;
+ }
+ label = util_strdup(parser_tokval(parser));
+ if (!parser_next(parser)) {
+ mem_d(label);
+ parseerror(parser, "expected 'for' expressions in parenthesis");
+ return false;
+ }
+ }
+
+ if (parser->tok != '(') {
+ parseerror(parser, "expected 'for' expressions in parenthesis");
+ return false;
+ }
+
+ vec_push(parser->breaks, label);
+ vec_push(parser->continues, label);
+
+ rv = parse_for_go(parser, block, out);
+ if (label)
+ mem_d(label);
+ if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
+ parseerror(parser, "internal error: label stack corrupted");
+ rv = false;
+ ast_delete(*out);
+ *out = NULL;
+ }
+ else {
+ vec_pop(parser->breaks);
+ vec_pop(parser->continues);
+ }
+ return rv;
+}
+static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out)
+{
+ ast_loop *aloop;
+ ast_expression *initexpr, *cond, *increment, *ontrue;
+ ast_value *typevar;
+
+ bool retval = true;
+ bool ifnot = false;
+
+ lex_ctx ctx = parser_ctx(parser);
+
+ parser_enterblock(parser);
+
+ initexpr = NULL;
+ cond = NULL;
+ increment = NULL;
+ ontrue = NULL;
+
+ /* parse into the expression */
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected 'for' initializer after opening paren");
+ goto onerr;
+ }
+
+ typevar = NULL;
+ if (parser->tok == TOKEN_IDENT)
+ typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
+
+ if (typevar || parser->tok == TOKEN_TYPENAME) {
+#if 0
+ if (opts.standard != COMPILER_GMQCC) {
+ if (parsewarning(parser, WARN_EXTENSIONS,
+ "current standard does not allow variable declarations in for-loop initializers"))
+ goto onerr;
+ }
+#endif
+ if (!parse_variable(parser, block, true, CV_VAR, typevar, false, false, 0, NULL))
+ goto onerr;
+ }
+ else if (parser->tok != ';')
+ {
+ initexpr = parse_expression_leave(parser, false, false, false);
+ if (!initexpr)
+ goto onerr;
+ }
+
+ /* move on to condition */
+ if (parser->tok != ';') {
+ parseerror(parser, "expected semicolon after for-loop initializer");
+ goto onerr;
+ }
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected for-loop condition");
+ goto onerr;
+ }
+
+ /* parse the condition */
+ if (parser->tok != ';') {
+ cond = parse_expression_leave(parser, false, true, false);
+ if (!cond)
+ goto onerr;
+ }
+
+ /* move on to incrementor */
+ if (parser->tok != ';') {
+ parseerror(parser, "expected semicolon after for-loop initializer");
+ goto onerr;
+ }
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected for-loop condition");
+ goto onerr;
+ }
+
+ /* parse the incrementor */
+ if (parser->tok != ')') {
+ increment = parse_expression_leave(parser, false, false, false);
+ if (!increment)
+ goto onerr;
+ if (!ast_side_effects(increment)) {
+ if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
+ goto onerr;
+ }
+ }
+
+ /* closing paren */
+ if (parser->tok != ')') {
+ parseerror(parser, "expected closing paren after 'for-loop' incrementor");
+ goto onerr;
+ }
+ /* parse into the 'then' branch */
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected for-loop body");
+ goto onerr;
+ }
+ if (!parse_statement_or_block(parser, &ontrue))
+ goto onerr;
+
+ if (cond) {
+ cond = process_condition(parser, cond, &ifnot);
+ if (!cond)
+ goto onerr;
+ }
+ aloop = ast_loop_new(ctx, initexpr, cond, ifnot, NULL, false, increment, ontrue);
+ *out = (ast_expression*)aloop;
+
+ if (!parser_leaveblock(parser))
+ retval = false;
+ return retval;
+onerr:
+ if (initexpr) ast_delete(initexpr);
+ if (cond) ast_delete(cond);
+ if (increment) ast_delete(increment);
+ (void)!parser_leaveblock(parser);
+ return false;
+}
+
+static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
+{
+ ast_expression *exp = NULL;
+ ast_return *ret = NULL;
+ ast_value *expected = parser->function->vtype;
+
+ lex_ctx ctx = parser_ctx(parser);
+
+ (void)block; /* not touching */
+
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected return expression");
+ return false;
+ }
+
+ if (parser->tok != ';') {
+ exp = parse_expression(parser, false, false);
+ if (!exp)
+ return false;
+
+ if (exp->expression.vtype != TYPE_NIL &&
+ exp->expression.vtype != expected->expression.next->expression.vtype)
+ {
+ parseerror(parser, "return with invalid expression");
+ }
+
+ ret = ast_return_new(ctx, exp);
+ if (!ret) {
+ ast_delete(exp);
+ return false;
+ }
+ } else {
+ if (!parser_next(parser))
+ parseerror(parser, "parse error");
+ if (expected->expression.next->expression.vtype != TYPE_VOID) {
+ (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
+ }
+ ret = ast_return_new(ctx, NULL);
+ }
+ *out = (ast_expression*)ret;
+ return true;
+}
+
+static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
+{
+ size_t i;
+ unsigned int levels = 0;
+ lex_ctx ctx = parser_ctx(parser);
+ const char **loops = (is_continue ? parser->continues : parser->breaks);
+
+ (void)block; /* not touching */
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected semicolon or loop label");
+ return false;
+ }
+
+ if (parser->tok == TOKEN_IDENT) {
+ if (!OPTS_FLAG(LOOP_LABELS))
+ parseerror(parser, "labeled loops not activated, try using -floop-labels");
+ i = vec_size(loops);
+ while (i--) {
+ if (loops[i] && !strcmp(loops[i], parser_tokval(parser)))
+ break;
+ if (!i) {
+ parseerror(parser, "no such loop to %s: `%s`",
+ (is_continue ? "continue" : "break out of"),
+ parser_tokval(parser));
+ return false;
+ }
+ ++levels;
+ }
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected semicolon");
+ return false;
+ }
+ }
+
+ if (parser->tok != ';') {
+ parseerror(parser, "expected semicolon");
+ return false;
+ }
+
+ if (!parser_next(parser))
+ parseerror(parser, "parse error");
+
+ *out = (ast_expression*)ast_breakcont_new(ctx, is_continue, levels);
+ return true;
+}
+
+/* returns true when it was a variable qualifier, false otherwise!
+ * on error, cvq is set to CV_WRONG
+ */
+static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags, char **message)
+{
+ bool had_const = false;
+ bool had_var = false;
+ bool had_noref = false;
+ bool had_attrib = false;
+ bool had_static = false;
+ uint32_t flags = 0;
+
+ *cvq = CV_NONE;
+ for (;;) {
+ if (parser->tok == TOKEN_ATTRIBUTE_OPEN) {
+ had_attrib = true;
+ /* parse an attribute */
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected attribute after `[[`");
+ *cvq = CV_WRONG;
+ return false;
+ }
+ if (!strcmp(parser_tokval(parser), "noreturn")) {
+ flags |= AST_FLAG_NORETURN;
+ if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
+ parseerror(parser, "`noreturn` attribute has no parameters, expected `]]`");
+ *cvq = CV_WRONG;
+ return false;
+ }
+ }
+ else if (!strcmp(parser_tokval(parser), "noref")) {
+ had_noref = true;
+ if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
+ parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
+ *cvq = CV_WRONG;
+ return false;
+ }
+ }
+ else if (!strcmp(parser_tokval(parser), "inline")) {
+ flags |= AST_FLAG_INLINE;
+ if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
+ parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
+ *cvq = CV_WRONG;
+ return false;
+ }
+ }
+
+
+ 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;
+ }
+
+ if (parser->tok == '(') {
+ if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
+ parseerror(parser, "`deprecated` attribute missing parameter");
+ goto argerr;
+ }
+
+ *message = util_strdup(parser_tokval(parser));
+
+ if (!parser_next(parser)) {
+ parseerror(parser, "parse error in attribute");
+ goto argerr;
+ }
+
+ if(parser->tok != ')') {
+ parseerror(parser, "`deprecated` attribute expected `)` after parameter");
+ goto argerr;
+ }
+
+ if (!parser_next(parser)) {
+ parseerror(parser, "parse error in attribute");
+ goto argerr;
+ }
+ }
+ /* no message */
+ if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
+ parseerror(parser, "`deprecated` attribute expected `]]`");
+
+ argerr: /* ugly */
+ if (*message) mem_d(*message);
+ *message = NULL;
+ *cvq = CV_WRONG;
+ return false;
+ }
+ }
+ else
+ {
+ /* Skip tokens until we hit a ]] */
+ (void)!parsewarning(parser, WARN_UNKNOWN_ATTRIBUTE, "unknown attribute starting with `%s`", parser_tokval(parser));
+ while (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
+ if (!parser_next(parser)) {
+ parseerror(parser, "error inside attribute");
+ *cvq = CV_WRONG;
+ return false;
+ }
+ }
+ }
+ }
+ else if (!strcmp(parser_tokval(parser), "static"))
+ had_static = true;
+ else if (!strcmp(parser_tokval(parser), "const"))
+ had_const = true;
+ else if (!strcmp(parser_tokval(parser), "var"))
+ had_var = true;
+ else if (with_local && !strcmp(parser_tokval(parser), "local"))
+ had_var = true;
+ else if (!strcmp(parser_tokval(parser), "noref"))
+ had_noref = true;
+ else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
+ return false;
+ }
+ else
+ break;
+ if (!parser_next(parser))
+ goto onerr;
+ }
+ if (had_const)
+ *cvq = CV_CONST;
+ else if (had_var)
+ *cvq = CV_VAR;
+ else
+ *cvq = CV_NONE;
+ *noref = had_noref;
+ *is_static = had_static;
+ *_flags = flags;
+ return true;
+onerr:
+ parseerror(parser, "parse error after variable qualifier");
+ *cvq = CV_WRONG;
+ return true;
+}
+
+static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out);
+static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
+{
+ bool rv;
+ char *label = NULL;
+
+ /* skip the 'while' and get the body */
+ if (!parser_next(parser)) {
+ if (OPTS_FLAG(LOOP_LABELS))
+ parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
+ else
+ parseerror(parser, "expected 'switch' operand in parenthesis");
+ return false;
+ }
+
+ if (parser->tok == ':') {
+ if (!OPTS_FLAG(LOOP_LABELS))
+ parseerror(parser, "labeled loops not activated, try using -floop-labels");
+ if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
+ parseerror(parser, "expected loop label");
+ return false;
+ }
+ label = util_strdup(parser_tokval(parser));
+ if (!parser_next(parser)) {
+ mem_d(label);
+ parseerror(parser, "expected 'switch' operand in parenthesis");
+ return false;
+ }
+ }
+
+ if (parser->tok != '(') {
+ parseerror(parser, "expected 'switch' operand in parenthesis");
+ return false;
+ }
+
+ vec_push(parser->breaks, label);
+
+ rv = parse_switch_go(parser, block, out);
+ if (label)
+ mem_d(label);
+ if (vec_last(parser->breaks) != label) {
+ parseerror(parser, "internal error: label stack corrupted");
+ rv = false;
+ ast_delete(*out);
+ *out = NULL;
+ }
+ else {
+ vec_pop(parser->breaks);
+ }
+ return rv;
+}
+
+static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out)
+{
+ ast_expression *operand;
+ ast_value *opval;
+ ast_value *typevar;
+ ast_switch *switchnode;
+ ast_switch_case swcase;
+
+ int cvq;
+ bool noref, is_static;
+ uint32_t qflags = 0;
+
+ lex_ctx ctx = parser_ctx(parser);
+
+ (void)block; /* not touching */
+ (void)opval;
+
+ /* parse into the expression */
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected switch operand");
+ return false;
+ }
+ /* parse the operand */
+ operand = parse_expression_leave(parser, false, false, false);
+ if (!operand)
+ return false;
+
+ switchnode = ast_switch_new(ctx, operand);
+
+ /* closing paren */
+ if (parser->tok != ')') {
+ ast_delete(switchnode);
+ parseerror(parser, "expected closing paren after 'switch' operand");
+ return false;
+ }
+
+ /* parse over the opening paren */
+ if (!parser_next(parser) || parser->tok != '{') {
+ ast_delete(switchnode);
+ parseerror(parser, "expected list of cases");
+ return false;
+ }
+
+ if (!parser_next(parser)) {
+ ast_delete(switchnode);
+ parseerror(parser, "expected 'case' or 'default'");
+ return false;
+ }
+
+ /* new block; allow some variables to be declared here */
+ parser_enterblock(parser);
+ while (true) {
+ typevar = NULL;
+ if (parser->tok == TOKEN_IDENT)
+ typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
+ if (typevar || parser->tok == TOKEN_TYPENAME) {
+ if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0, NULL)) {
+ ast_delete(switchnode);
+ return false;
+ }
+ continue;
+ }
+ if (parse_qualifiers(parser, true, &cvq, &noref, &is_static, &qflags, NULL))
+ {
+ if (cvq == CV_WRONG) {
+ ast_delete(switchnode);
+ return false;
+ }
+ if (!parse_variable(parser, block, false, cvq, NULL, noref, is_static, qflags, NULL)) {
+ ast_delete(switchnode);
+ return false;
+ }
+ continue;
+ }
+ break;
+ }
+
+ /* case list! */
+ while (parser->tok != '}') {
+ ast_block *caseblock;
+
+ if (!strcmp(parser_tokval(parser), "case")) {
+ if (!parser_next(parser)) {
+ ast_delete(switchnode);
+ parseerror(parser, "expected expression for case");
+ return false;
+ }
+ swcase.value = parse_expression_leave(parser, false, false, false);
+ if (!swcase.value) {
+ ast_delete(switchnode);
+ parseerror(parser, "expected expression for case");
+ return false;
+ }
+ if (!OPTS_FLAG(RELAXED_SWITCH)) {
+ if (!ast_istype(swcase.value, ast_value)) { /* || ((ast_value*)swcase.value)->cvq != CV_CONST) { */
+ parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
+ ast_unref(operand);
+ return false;
+ }
+ }
+ }
+ else if (!strcmp(parser_tokval(parser), "default")) {
+ swcase.value = NULL;
+ if (!parser_next(parser)) {
+ ast_delete(switchnode);
+ parseerror(parser, "expected colon");
+ return false;
+ }
+ }
+ else {
+ ast_delete(switchnode);
+ parseerror(parser, "expected 'case' or 'default'");
+ return false;
+ }
+
+ /* Now the colon and body */
+ if (parser->tok != ':') {
+ if (swcase.value) ast_unref(swcase.value);
+ ast_delete(switchnode);
+ parseerror(parser, "expected colon");
+ return false;
+ }
+
+ if (!parser_next(parser)) {
+ if (swcase.value) ast_unref(swcase.value);
+ ast_delete(switchnode);
+ parseerror(parser, "expected statements or case");
+ return false;
+ }
+ caseblock = ast_block_new(parser_ctx(parser));
+ if (!caseblock) {
+ if (swcase.value) ast_unref(swcase.value);
+ ast_delete(switchnode);
+ return false;
+ }
+ swcase.code = (ast_expression*)caseblock;
+ vec_push(switchnode->cases, swcase);
+ while (true) {
+ ast_expression *expr;
+ if (parser->tok == '}')
+ break;
+ if (parser->tok == TOKEN_KEYWORD) {
+ if (!strcmp(parser_tokval(parser), "case") ||
+ !strcmp(parser_tokval(parser), "default"))
+ {
+ break;
+ }
+ }
+ if (!parse_statement(parser, caseblock, &expr, true)) {
+ ast_delete(switchnode);
+ return false;
+ }
+ if (!expr)
+ continue;
+ if (!ast_block_add_expr(caseblock, expr)) {
+ ast_delete(switchnode);
+ return false;
+ }
+ }
+ }
+
+ parser_leaveblock(parser);
+
+ /* closing paren */
+ if (parser->tok != '}') {
+ ast_delete(switchnode);
+ parseerror(parser, "expected closing paren of case list");
+ return false;
+ }
+ if (!parser_next(parser)) {
+ ast_delete(switchnode);
+ parseerror(parser, "parse error after switch");