]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
cache filenames as such instead of using code_cachedstring
[xonotic/gmqcc.git] / parser.c
index 06f6d7a37705f29dd8a217c56ec10e90924411bb..f11684bb3bb139325a598b8114374db641a0444b 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -44,27 +44,27 @@ MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_vector)
 MEM_VEC_FUNCTIONS(parser_t, varentry_t, locals)
 MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions)
 
-static void parser_pop_local(parser_t *parser);
-static bool parser_variable(parser_t *parser, ast_block *localblock);
-static ast_block* parser_parse_block(parser_t *parser, bool warnreturn);
-static bool parser_parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
-static ast_expression* parser_parse_statement_or_block(parser_t *parser);
-static ast_expression* parser_expression_leave(parser_t *parser, bool stopatcomma);
-static ast_expression* parser_expression(parser_t *parser, bool stopatcomma);
-
-void parseerror(parser_t *parser, const char *fmt, ...)
+static bool GMQCC_WARN parser_pop_local(parser_t *parser);
+static bool parse_variable(parser_t *parser, ast_block *localblock);
+static ast_block* parse_block(parser_t *parser, bool warnreturn);
+static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
+static ast_expression* parse_statement_or_block(parser_t *parser);
+static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
+static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
+
+static void parseerror(parser_t *parser, const char *fmt, ...)
 {
        va_list ap;
 
        parser->errors++;
 
        va_start(ap, fmt);
-    vprintmsg(LVL_ERROR, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "parse error", fmt, ap);
+    vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
        va_end(ap);
 }
 
 /* returns true if it counts as an error */
-bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
+static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
 {
        va_list ap;
        int lvl = LVL_WARNING;
@@ -78,7 +78,25 @@ bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ..
        }
 
        va_start(ap, fmt);
-    vprintmsg(lvl, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "warning", fmt, ap);
+    vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
+       va_end(ap);
+
+       return opts_werror;
+}
+
+static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
+{
+       va_list ap;
+       int lvl = LVL_WARNING;
+
+    if (!OPTS_WARN(warntype))
+        return false;
+
+    if (opts_werror)
+           lvl = LVL_ERROR;
+
+       va_start(ap, fmt);
+    vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
        va_end(ap);
 
        return opts_werror;
@@ -129,7 +147,7 @@ bool parser_next(parser_t *parser)
     /* lex_do kills the previous token */
     parser->tok = lex_do(parser->lex);
     if (parser->tok == TOKEN_EOF)
-        return false;
+        return true;
     if (parser->tok >= TOKEN_ERROR) {
         parseerror(parser, "lex error");
         return false;
@@ -137,19 +155,11 @@ bool parser_next(parser_t *parser)
     return true;
 }
 
-/* lift a token out of the parser so it's not destroyed by parser_next */
-token *parser_lift(parser_t *parser)
-{
-    token *tok = parser->lex->tok;
-    parser->lex->tok = NULL;
-    return tok;
-}
-
-#define parser_tokval(p) (p->lex->tok->value)
-#define parser_token(p)  (p->lex->tok)
-#define parser_ctx(p)    (p->lex->tok->ctx)
+#define parser_tokval(p) ((p)->lex->tok.value)
+#define parser_token(p)  (&((p)->lex->tok))
+#define parser_ctx(p)    ((p)->lex->tok.ctx)
 
-ast_value* parser_const_float(parser_t *parser, double d)
+static ast_value* parser_const_float(parser_t *parser, double d)
 {
     size_t i;
     ast_value *out;
@@ -167,14 +177,14 @@ ast_value* parser_const_float(parser_t *parser, double d)
     return out;
 }
 
-ast_value* parser_const_float_0(parser_t *parser)
+static ast_value* parser_const_float_0(parser_t *parser)
 {
     if (!parser->imm_float_zero)
         parser->imm_float_zero = parser_const_float(parser, 0);
     return parser->imm_float_zero;
 }
 
-char *parser_strdup(const char *str)
+static char *parser_strdup(const char *str)
 {
     if (str && !*str) {
         /* actually dup empty strings */
@@ -185,7 +195,7 @@ char *parser_strdup(const char *str)
     return util_strdup(str);
 }
 
-ast_value* parser_const_string(parser_t *parser, const char *str)
+static ast_value* parser_const_string(parser_t *parser, const char *str)
 {
     size_t i;
     ast_value *out;
@@ -203,7 +213,7 @@ ast_value* parser_const_string(parser_t *parser, const char *str)
     return out;
 }
 
-ast_value* parser_const_vector(parser_t *parser, vector v)
+static ast_value* parser_const_vector(parser_t *parser, vector v)
 {
     size_t i;
     ast_value *out;
@@ -221,7 +231,7 @@ ast_value* parser_const_vector(parser_t *parser, vector v)
     return out;
 }
 
-ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
+static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
 {
     vector v;
     v.x = x;
@@ -230,14 +240,14 @@ ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
     return parser_const_vector(parser, v);
 }
 
-ast_value* parser_const_vector_0(parser_t *parser)
+static ast_value* parser_const_vector_0(parser_t *parser)
 {
     if (!parser->imm_vector_zero)
         parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
     return parser->imm_vector_zero;
 }
 
-ast_expression* parser_find_field(parser_t *parser, const char *name)
+static ast_expression* parser_find_field(parser_t *parser, const char *name)
 {
     size_t i;
     for (i = 0; i < parser->fields_count; ++i) {
@@ -247,7 +257,7 @@ ast_expression* parser_find_field(parser_t *parser, const char *name)
     return NULL;
 }
 
-ast_expression* parser_find_global(parser_t *parser, const char *name)
+static ast_expression* parser_find_global(parser_t *parser, const char *name)
 {
     size_t i;
     for (i = 0; i < parser->globals_count; ++i) {
@@ -257,7 +267,7 @@ ast_expression* parser_find_global(parser_t *parser, const char *name)
     return NULL;
 }
 
-ast_expression* parser_find_param(parser_t *parser, const char *name)
+static ast_expression* parser_find_param(parser_t *parser, const char *name)
 {
     size_t i;
     ast_value *fun;
@@ -271,7 +281,7 @@ ast_expression* parser_find_param(parser_t *parser, const char *name)
     return NULL;
 }
 
-ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
+static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
 {
     size_t i;
     *isparam = false;
@@ -284,7 +294,7 @@ ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upt
     return parser_find_param(parser, name);
 }
 
-ast_expression* parser_find_var(parser_t *parser, const char *name)
+static ast_expression* parser_find_var(parser_t *parser, const char *name)
 {
     bool dummy;
     ast_expression *v;
@@ -298,7 +308,7 @@ typedef struct {
 } paramlist_t;
 MEM_VEC_FUNCTIONS(paramlist_t, ast_value*, p)
 
-static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc)
+static ast_value *parse_type(parser_t *parser, int basetype, bool *isfunc)
 {
     paramlist_t params;
     ast_value *var;
@@ -306,6 +316,7 @@ static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc
     int vtype = basetype;
     int temptype;
     size_t i;
+    bool variadic = false;
 
     MEM_VECTOR_INIT(&params, p);
 
@@ -317,7 +328,7 @@ static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc
             ast_value *param;
             ast_value *fld;
             bool isfield = false;
-            bool dummy;
+            bool isfuncparam = false;
 
             if (!parser_next(parser))
                 goto on_error;
@@ -333,12 +344,27 @@ static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc
                 }
             }
 
+            if (parser->tok == TOKEN_DOTS) {
+                /* variadic args */
+                variadic = true;
+                if (!parser_next(parser))
+                    goto on_error;
+                if (parser->tok != ')') {
+                    parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
+                    goto on_error;
+                }
+                if (opts_standard == COMPILER_QCC) {
+                    if (parsewarning(parser, WARN_EXTENSIONS, "variadic functions are not available in this standard"))
+                        goto on_error;
+                }
+                break;
+            }
+
             temptype = parser_token(parser)->constval.t;
             if (!parser_next(parser))
                 goto on_error;
 
-            param = parser_parse_type(parser, temptype, &dummy);
-            (void)dummy;
+            param = parse_type(parser, temptype, &isfuncparam);
 
             if (!param)
                 goto on_error;
@@ -351,6 +377,19 @@ static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc
                     goto on_error;
             }
 
+            /* This comes before the isfield part! */
+            if (isfuncparam) {
+                ast_value *fval = ast_value_new(ast_ctx(param), param->name, TYPE_FUNCTION);
+                if (!fval) {
+                    ast_delete(param);
+                    goto on_error;
+                }
+                fval->expression.next = (ast_expression*)param;
+                MEM_VECTOR_MOVE(&param->expression, params, &fval->expression, params);
+                fval->expression.variadic = param->expression.variadic;
+                param = fval;
+            }
+
             if (isfield) {
                 fld = ast_value_new(ctx, param->name, TYPE_FIELD);
                 fld->expression.next = (ast_expression*)param;
@@ -376,6 +415,7 @@ static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc
     var = ast_value_new(ctx, "<unnamed>", vtype);
     if (!var)
         goto on_error;
+    var->expression.variadic = variadic;
     MEM_VECTOR_MOVE(&params, p, &var->expression, params);
     return var;
 on_error:
@@ -941,29 +981,42 @@ static bool parser_close_call(parser_t *parser, shunt *sy)
         parseerror(parser, "could not determine function return type");
         return false;
     } else {
-        if (fun->expression.params_count != paramcount) {
+        if (fun->expression.params_count != paramcount &&
+            !(fun->expression.variadic &&
+              fun->expression.params_count < paramcount))
+        {
             ast_value *fval;
+            const char *fewmany = (fun->expression.params_count > paramcount) ? "few" : "many";
+
             fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
             if (opts_standard == COMPILER_GMQCC)
             {
                 if (fval)
-                    parseerror(parser, "too few parameters for call to %s: expected %i, got %i",
-                               fval->name, (int)fun->expression.params_count, paramcount);
+                    parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
+                               " -> `%s` has been declared here: %s:%i",
+                               fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
+                               fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
                 else
-                    parseerror(parser, "too few parameters for function call: expected %i, got %i",
-                               (int)fun->expression.params_count, paramcount);
+                    parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
+                               " -> `%s` has been declared here: %s:%i",
+                               fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
+                               fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
                 return false;
             }
             else
             {
                 if (fval)
                     return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
-                                         "too few parameters for call to %s: expected %i, got %i",
-                                         fval->name, (int)fun->expression.params_count, paramcount);
+                                         "too %s parameters for call to %s: expected %i, got %i\n"
+                                         " -> `%s` has been declared here: %s:%i",
+                                         fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
+                                         fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
                 else
                     return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
-                                         "too few parameters for function call: expected %i, got %i",
-                                         (int)fun->expression.params_count, paramcount);
+                                         "too %s parameters for function call: expected %i, got %i\n"
+                                         " -> `%s` has been declared here: %s:%i",
+                                         fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
+                                         fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
             }
         }
     }
@@ -1010,7 +1063,7 @@ static void parser_reclassify_token(parser_t *parser)
     }
 }
 
-static ast_expression* parser_expression_leave(parser_t *parser, bool stopatcomma)
+static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
 {
     ast_expression *expr = NULL;
     shunt sy;
@@ -1073,6 +1126,8 @@ static ast_expression* parser_expression_leave(parser_t *parser, bool stopatcomm
                 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
                 goto onerr;
             }
+            if (ast_istype(var, ast_value))
+                ((ast_value*)var)->uses++;
             if (!shunt_out_add(&sy, syexp(parser_ctx(parser), var))) {
                 parseerror(parser, "out of memory");
                 goto onerr;
@@ -1296,9 +1351,9 @@ onerr:
     return NULL;
 }
 
-static ast_expression* parser_expression(parser_t *parser, bool stopatcomma)
+static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
 {
-    ast_expression *e = parser_expression_leave(parser, stopatcomma);
+    ast_expression *e = parse_expression_leave(parser, stopatcomma);
     if (!e)
         return NULL;
     if (!parser_next(parser)) {
@@ -1308,7 +1363,7 @@ static ast_expression* parser_expression(parser_t *parser, bool stopatcomma)
     return e;
 }
 
-static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression **out)
+static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
 {
     ast_ifthen *ifthen;
     ast_expression *cond, *ontrue, *onfalse = NULL;
@@ -1326,7 +1381,7 @@ static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression *
         return false;
     }
     /* parse the condition */
-    cond = parser_expression_leave(parser, false);
+    cond = parse_expression_leave(parser, false);
     if (!cond)
         return false;
     /* closing paren */
@@ -1341,7 +1396,7 @@ static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression *
         ast_delete(cond);
         return false;
     }
-    ontrue = parser_parse_statement_or_block(parser);
+    ontrue = parse_statement_or_block(parser);
     if (!ontrue) {
         ast_delete(cond);
         return false;
@@ -1355,7 +1410,7 @@ static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression *
             ast_delete(cond);
             return false;
         }
-        onfalse = parser_parse_statement_or_block(parser);
+        onfalse = parse_statement_or_block(parser);
         if (!onfalse) {
             ast_delete(ontrue);
             ast_delete(cond);
@@ -1368,7 +1423,7 @@ static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression *
     return true;
 }
 
-static bool parser_parse_while(parser_t *parser, ast_block *block, ast_expression **out)
+static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
 {
     ast_loop *aloop;
     ast_expression *cond, *ontrue;
@@ -1386,7 +1441,7 @@ static bool parser_parse_while(parser_t *parser, ast_block *block, ast_expressio
         return false;
     }
     /* parse the condition */
-    cond = parser_expression_leave(parser, false);
+    cond = parse_expression_leave(parser, false);
     if (!cond)
         return false;
     /* closing paren */
@@ -1401,7 +1456,7 @@ static bool parser_parse_while(parser_t *parser, ast_block *block, ast_expressio
         ast_delete(cond);
         return false;
     }
-    ontrue = parser_parse_statement_or_block(parser);
+    ontrue = parse_statement_or_block(parser);
     if (!ontrue) {
         ast_delete(cond);
         return false;
@@ -1412,7 +1467,7 @@ static bool parser_parse_while(parser_t *parser, ast_block *block, ast_expressio
     return true;
 }
 
-static bool parser_parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
+static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
 {
     ast_loop *aloop;
     ast_expression *cond, *ontrue;
@@ -1424,7 +1479,7 @@ static bool parser_parse_dowhile(parser_t *parser, ast_block *block, ast_express
         parseerror(parser, "expected loop body");
         return false;
     }
-    ontrue = parser_parse_statement_or_block(parser);
+    ontrue = parse_statement_or_block(parser);
     if (!ontrue)
         return false;
 
@@ -1450,7 +1505,7 @@ static bool parser_parse_dowhile(parser_t *parser, ast_block *block, ast_express
         return false;
     }
     /* parse the condition */
-    cond = parser_expression_leave(parser, false);
+    cond = parse_expression_leave(parser, false);
     if (!cond)
         return false;
     /* closing paren */
@@ -1480,11 +1535,12 @@ static bool parser_parse_dowhile(parser_t *parser, ast_block *block, ast_express
     return true;
 }
 
-static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression **out)
+static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
 {
     ast_loop *aloop;
     ast_expression *initexpr, *cond, *increment, *ontrue;
     size_t oldblocklocal;
+    bool   retval = true;
 
     lex_ctx ctx = parser_ctx(parser);
 
@@ -1516,12 +1572,12 @@ static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression
 
         parseerror(parser, "TODO: assignment of new variables to be non-const");
         goto onerr;
-        if (!parser_variable(parser, block))
+        if (!parse_variable(parser, block))
             goto onerr;
     }
     else if (parser->tok != ';')
     {
-        initexpr = parser_expression_leave(parser, false);
+        initexpr = parse_expression_leave(parser, false);
         if (!initexpr)
             goto onerr;
     }
@@ -1538,7 +1594,7 @@ static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression
 
     /* parse the condition */
     if (parser->tok != ';') {
-        cond = parser_expression_leave(parser, false);
+        cond = parse_expression_leave(parser, false);
         if (!cond)
             goto onerr;
     }
@@ -1555,9 +1611,16 @@ static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression
 
     /* parse the incrementor */
     if (parser->tok != ')') {
-        increment = parser_expression_leave(parser, false);
+        increment = parse_expression_leave(parser, false);
         if (!increment)
             goto onerr;
+        if (!ast_istype(increment, ast_store) &&
+            !ast_istype(increment, ast_call) &&
+            !ast_istype(increment, ast_binstore))
+        {
+            if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
+                goto onerr;
+        }
     }
 
     /* closing paren */
@@ -1570,7 +1633,7 @@ static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression
         parseerror(parser, "expected for-loop body");
         goto onerr;
     }
-    ontrue = parser_parse_statement_or_block(parser);
+    ontrue = parse_statement_or_block(parser);
     if (!ontrue) {
         goto onerr;
     }
@@ -1579,20 +1642,20 @@ static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression
     *out = (ast_expression*)aloop;
 
     while (parser->locals_count > parser->blocklocal)
-        parser_pop_local(parser);
+        retval = retval && parser_pop_local(parser);
     parser->blocklocal = oldblocklocal;
-    return true;
+    return retval;
 onerr:
     if (initexpr)  ast_delete(initexpr);
     if (cond)      ast_delete(cond);
     if (increment) ast_delete(increment);
     while (parser->locals_count > parser->blocklocal)
-        parser_pop_local(parser);
+        (void)!parser_pop_local(parser);
     parser->blocklocal = oldblocklocal;
     return false;
 }
 
-static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
+static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
 {
     if (parser->tok == TOKEN_TYPENAME)
     {
@@ -1605,7 +1668,7 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
             if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
                 return false;
         }
-        if (!parser_variable(parser, block))
+        if (!parse_variable(parser, block))
             return false;
         *out = NULL;
         return true;
@@ -1622,7 +1685,7 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
                 parseerror(parser, "expected variable declaration");
                 return false;
             }
-            if (!parser_variable(parser, block))
+            if (!parse_variable(parser, block))
                 return false;
             *out = NULL;
             return true;
@@ -1639,7 +1702,7 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
             }
 
             if (parser->tok != ';') {
-                exp = parser_expression(parser, false);
+                exp = parse_expression(parser, false);
                 if (!exp)
                     return false;
 
@@ -1668,15 +1731,15 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
         }
         else if (!strcmp(parser_tokval(parser), "if"))
         {
-            return parser_parse_if(parser, block, out);
+            return parse_if(parser, block, out);
         }
         else if (!strcmp(parser_tokval(parser), "while"))
         {
-            return parser_parse_while(parser, block, out);
+            return parse_while(parser, block, out);
         }
         else if (!strcmp(parser_tokval(parser), "do"))
         {
-            return parser_parse_dowhile(parser, block, out);
+            return parse_dowhile(parser, block, out);
         }
         else if (!strcmp(parser_tokval(parser), "for"))
         {
@@ -1684,7 +1747,7 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
                 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
                     return false;
             }
-            return parser_parse_for(parser, block, out);
+            return parse_for(parser, block, out);
         }
         parseerror(parser, "Unexpected keyword");
         return false;
@@ -1692,7 +1755,7 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
     else if (parser->tok == '{')
     {
         ast_block *inner;
-        inner = parser_parse_block(parser, false);
+        inner = parse_block(parser, false);
         if (!inner)
             return false;
         *out = (ast_expression*)inner;
@@ -1700,23 +1763,39 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
     }
     else
     {
-        ast_expression *exp = parser_expression(parser, false);
+        ast_expression *exp = parse_expression(parser, false);
         if (!exp)
             return false;
         *out = exp;
+        if (!ast_istype(exp, ast_store) &&
+            !ast_istype(exp, ast_call) &&
+            !ast_istype(exp, ast_binstore))
+        {
+            if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
+                return false;
+        }
         return true;
     }
 }
 
-static void parser_pop_local(parser_t *parser)
+static bool GMQCC_WARN parser_pop_local(parser_t *parser)
 {
+    varentry_t *ve;
     parser->locals_count--;
+
+    ve = &parser->locals[parser->locals_count];
+    if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
+        if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
+            return false;
+    }
     mem_d(parser->locals[parser->locals_count].name);
+    return true;
 }
 
-static bool parser_parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
+static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
 {
     size_t oldblocklocal;
+    bool   retval = true;
 
     oldblocklocal = parser->blocklocal;
     parser->blocklocal = parser->locals_count;
@@ -1732,7 +1811,7 @@ static bool parser_parse_block_into(parser_t *parser, ast_block *block, bool war
         if (parser->tok == '}')
             break;
 
-        if (!parser_parse_statement(parser, block, &expr)) {
+        if (!parse_statement(parser, block, &expr)) {
             parseerror(parser, "parse error");
             block = NULL;
             goto cleanup;
@@ -1765,139 +1844,452 @@ static bool parser_parse_block_into(parser_t *parser, ast_block *block, bool war
 
 cleanup:
     while (parser->locals_count > parser->blocklocal)
-        parser_pop_local(parser);
+        retval = retval && parser_pop_local(parser);
     parser->blocklocal = oldblocklocal;
     return !!block;
 }
 
-static ast_block* parser_parse_block(parser_t *parser, bool warnreturn)
+static ast_block* parse_block(parser_t *parser, bool warnreturn)
 {
     ast_block *block;
     block = ast_block_new(parser_ctx(parser));
     if (!block)
         return NULL;
-    if (!parser_parse_block_into(parser, block, warnreturn)) {
+    if (!parse_block_into(parser, block, warnreturn)) {
         ast_block_delete(block);
         return NULL;
     }
     return block;
 }
 
-static ast_expression* parser_parse_statement_or_block(parser_t *parser)
+static ast_expression* parse_statement_or_block(parser_t *parser)
 {
     ast_expression *expr = NULL;
     if (parser->tok == '{')
-        return (ast_expression*)parser_parse_block(parser, false);
-    if (!parser_parse_statement(parser, NULL, &expr))
+        return (ast_expression*)parse_block(parser, false);
+    if (!parse_statement(parser, NULL, &expr))
         return NULL;
     return expr;
 }
 
-static bool create_vector_members(parser_t *parser, ast_value *var,
-                                  varentry_t *vx, varentry_t *vy, varentry_t *vz)
+/* loop method */
+static bool create_vector_members(parser_t *parser, ast_value *var, varentry_t *ve)
 {
+    size_t i;
     size_t len = strlen(var->name);
-    vx->var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, 0);
-    if (!vx->var) {
-        parseerror(parser, "failed to create vector members (out of memory)");
-        return false;
+
+    for (i = 0; i < 3; ++i) {
+        ve[i].var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, i);
+        if (!ve[i].var)
+            break;
+
+        ve[i].name = (char*)mem_a(len+3);
+        if (!ve[i].name) {
+            ast_delete(ve[i].var);
+            break;
+        }
+
+        memcpy(ve[i].name, var->name, len);
+        ve[i].name[len]   = '_';
+        ve[i].name[len+1] = 'x'+i;
+        ve[i].name[len+2] = 0;
     }
+    if (i == 3)
+        return true;
 
-    vy->var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, 1);
-    if (!vy->var) {
-        ast_delete(vx->var);
-        parseerror(parser, "failed to create vector members (out of memory)");
-        return false;
+    /* unroll */
+    do {
+        --i;
+        mem_d(ve[i].name);
+        ast_delete(ve[i].var);
+        ve[i].name = NULL;
+        ve[i].var  = NULL;
+    } while (i);
+    return false;
+}
+
+static bool parse_function_body(parser_t *parser, ast_value *var)
+{
+    ast_block      *block = NULL;
+    ast_function   *func;
+    ast_function   *old;
+    size_t          parami;
+
+    ast_expression *framenum  = NULL;
+    ast_expression *nextthink = NULL;
+    /* None of the following have to be deleted */
+    ast_expression *fld_think, *fld_nextthink, *fld_frame;
+    ast_expression *gbl_time, *gbl_self;
+    bool            has_frame_think;
+
+    bool retval = true;
+
+    has_frame_think = false;
+    old = parser->function;
+
+    if (var->expression.variadic) {
+        if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
+                         "variadic function with implementation will not be able to access additional parameters"))
+        {
+            return false;
+        }
     }
 
-    vz->var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, 2);
-    if (!vz->var) {
-        ast_delete(vy->var);
-        ast_delete(vx->var);
-        parseerror(parser, "failed to create vector members (out of memory)");
-        return false;
+    if (parser->tok == '[') {
+        /* got a frame definition: [ framenum, nextthink ]
+         * this translates to:
+         * self.frame = framenum;
+         * self.nextthink = time + 0.1;
+         * self.think = nextthink;
+         */
+        nextthink = NULL;
+
+        fld_think     = parser_find_field(parser, "think");
+        fld_nextthink = parser_find_field(parser, "nextthink");
+        fld_frame     = parser_find_field(parser, "frame");
+        if (!fld_think || !fld_nextthink || !fld_frame) {
+            parseerror(parser, "cannot use [frame,think] notation without the required fields");
+            parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
+            return false;
+        }
+        gbl_time      = parser_find_global(parser, "time");
+        gbl_self      = parser_find_global(parser, "self");
+        if (!gbl_time || !gbl_self) {
+            parseerror(parser, "cannot use [frame,think] notation without the required globals");
+            parseerror(parser, "please declare the following globals: `time`, `self`");
+            return false;
+        }
+
+        if (!parser_next(parser))
+            return false;
+
+        framenum = parse_expression_leave(parser, true);
+        if (!framenum) {
+            parseerror(parser, "expected a framenumber constant in[frame,think] notation");
+            return false;
+        }
+        if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
+            ast_unref(framenum);
+            parseerror(parser, "framenumber in [frame,think] notation must be a constant");
+            return false;
+        }
+
+        if (parser->tok != ',') {
+            ast_unref(framenum);
+            parseerror(parser, "expected comma after frame number in [frame,think] notation");
+            parseerror(parser, "Got a %i\n", parser->tok);
+            return false;
+        }
+
+        if (!parser_next(parser)) {
+            ast_unref(framenum);
+            return false;
+        }
+
+        if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
+        {
+            /* qc allows the use of not-yet-declared functions here
+             * - this automatically creates a prototype */
+            varentry_t      varent;
+            ast_value      *thinkfunc;
+            ast_expression *functype = fld_think->expression.next;
+
+            thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
+            if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
+                ast_unref(framenum);
+                parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
+                return false;
+            }
+
+            if (!parser_next(parser)) {
+                ast_unref(framenum);
+                ast_delete(thinkfunc);
+                return false;
+            }
+
+            varent.var = (ast_expression*)thinkfunc;
+            varent.name = util_strdup(thinkfunc->name);
+            if (!parser_t_globals_add(parser, varent)) {
+                ast_unref(framenum);
+                ast_delete(thinkfunc);
+                return false;
+            }
+            nextthink = (ast_expression*)thinkfunc;
+
+        } else {
+            nextthink = parse_expression_leave(parser, true);
+            if (!nextthink) {
+                ast_unref(framenum);
+                parseerror(parser, "expected a think-function in [frame,think] notation");
+                return false;
+            }
+        }
+
+        if (!ast_istype(nextthink, ast_value)) {
+            parseerror(parser, "think-function in [frame,think] notation must be a constant");
+            retval = false;
+        }
+
+        if (retval && parser->tok != ']') {
+            parseerror(parser, "expected closing `]` for [frame,think] notation");
+            retval = false;
+        }
+
+        if (retval && !parser_next(parser)) {
+            retval = false;
+        }
+
+        if (retval && parser->tok != '{') {
+            parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
+            retval = false;
+        }
+
+        if (!retval) {
+            ast_unref(nextthink);
+            ast_unref(framenum);
+            return false;
+        }
+
+        has_frame_think = true;
     }
 
-    if ( !(vx->name = (char*)mem_a(len+3)) ) {
-        ast_delete(vz->var);
-        ast_delete(vy->var);
-        ast_delete(vx->var);
-        parseerror(parser, "failed to create vector members (out of memory)");
+    block = ast_block_new(parser_ctx(parser));
+    if (!block) {
+        parseerror(parser, "failed to allocate block");
+        if (has_frame_think) {
+            ast_unref(nextthink);
+            ast_unref(framenum);
+        }
         return false;
     }
-    if ( !(vy->name = (char*)mem_a(len+3)) ) {
-        mem_d(vx->name);
-        ast_delete(vz->var);
-        ast_delete(vy->var);
-        ast_delete(vx->var);
-        parseerror(parser, "failed to create vector members (out of memory)");
-        return false;
+
+    if (has_frame_think) {
+        lex_ctx ctx;
+        ast_expression *self_frame;
+        ast_expression *self_nextthink;
+        ast_expression *self_think;
+        ast_expression *time_plus_1;
+        ast_store *store_frame;
+        ast_store *store_nextthink;
+        ast_store *store_think;
+
+        ctx = parser_ctx(parser);
+        self_frame     = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
+        self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
+        self_think     = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
+
+        time_plus_1    = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
+                         gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
+
+        if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
+            if (self_frame)     ast_delete(self_frame);
+            if (self_nextthink) ast_delete(self_nextthink);
+            if (self_think)     ast_delete(self_think);
+            if (time_plus_1)    ast_delete(time_plus_1);
+            retval = false;
+        }
+
+        if (retval)
+        {
+            store_frame     = ast_store_new(ctx, INSTR_STOREP_F,   self_frame,     framenum);
+            store_nextthink = ast_store_new(ctx, INSTR_STOREP_F,   self_nextthink, time_plus_1);
+            store_think     = ast_store_new(ctx, INSTR_STOREP_FNC, self_think,     nextthink);
+
+            if (!store_frame) {
+                ast_delete(self_frame);
+                retval = false;
+            }
+            if (!store_nextthink) {
+                ast_delete(self_nextthink);
+                retval = false;
+            }
+            if (!store_think) {
+                ast_delete(self_think);
+                retval = false;
+            }
+            if (!retval) {
+                if (store_frame)     ast_delete(store_frame);
+                if (store_nextthink) ast_delete(store_nextthink);
+                if (store_think)     ast_delete(store_think);
+                retval = false;
+            }
+            if (retval && !ast_block_exprs_add(block, (ast_expression*)store_frame)) {
+                ast_delete(store_frame);
+                ast_delete(store_nextthink);
+                ast_delete(store_think);
+                retval = false;
+            }
+
+            if (retval && !ast_block_exprs_add(block, (ast_expression*)store_nextthink)) {
+                ast_delete(store_nextthink);
+                ast_delete(store_think);
+                retval = false;
+            }
+
+            if (retval && !ast_block_exprs_add(block, (ast_expression*)store_think) )
+            {
+                ast_delete(store_think);
+                retval = false;
+            }
+        }
+
+        if (!retval) {
+            parseerror(parser, "failed to generate code for [frame,think]");
+            ast_unref(nextthink);
+            ast_unref(framenum);
+            ast_delete(block);
+            return false;
+        }
     }
-    if ( !(vz->name = (char*)mem_a(len+3)) ) {
-        mem_d(vy->name);
-        mem_d(vx->name);
-        ast_delete(vz->var);
-        ast_delete(vy->var);
-        ast_delete(vx->var);
-        parseerror(parser, "failed to create vector members (out of memory)");
-        return false;
+
+    for (parami = 0; parami < var->expression.params_count; ++parami) {
+        size_t     e;
+        varentry_t ve[3];
+        ast_value *param = var->expression.params[parami];
+
+        if (param->expression.vtype != TYPE_VECTOR &&
+            (param->expression.vtype != TYPE_FIELD ||
+             param->expression.next->expression.vtype != TYPE_VECTOR))
+        {
+            continue;
+        }
+
+        if (!create_vector_members(parser, param, ve)) {
+            ast_block_delete(block);
+            return false;
+        }
+
+        for (e = 0; e < 3; ++e) {
+            if (!parser_t_locals_add(parser, ve[e]))
+                break;
+            if (!ast_block_collect(block, ve[e].var)) {
+                parser->locals_count--;
+                break;
+            }
+            ve[e].var = NULL; /* collected */
+        }
+        if (e != e) {
+            parser->locals -= e;
+            do {
+                mem_d(ve[e].name);
+                --e;
+            } while (e);
+            ast_block_delete(block);
+            return false;
+        }
     }
 
-    memcpy(vx->name, var->name, len);
-    memcpy(vy->name, var->name, len);
-    memcpy(vz->name, var->name, len);
-    vx->name[len] = vy->name[len] = vz->name[len] = '_';
-    vx->name[len+1] = 'x';
-    vy->name[len+1] = 'y';
-    vz->name[len+1] = 'z';
-    vx->name[len+2] = vy->name[len+2] = vz->name[len+2] = 0;
-    return true;
+    func = ast_function_new(ast_ctx(var), var->name, var);
+    if (!func) {
+        parseerror(parser, "failed to allocate function for `%s`", var->name);
+        ast_block_delete(block);
+        goto enderr;
+    }
+    if (!parser_t_functions_add(parser, func)) {
+        parseerror(parser, "failed to allocate slot for function `%s`", var->name);
+        ast_block_delete(block);
+        goto enderrfn;
+    }
+
+    parser->function = func;
+    if (!parse_block_into(parser, block, true)) {
+        ast_block_delete(block);
+        goto enderrfn;
+    }
+
+    if (!ast_function_blocks_add(func, block)) {
+        ast_block_delete(block);
+        goto enderrfn;
+    }
+
+    parser->function = old;
+    while (parser->locals_count)
+        retval = retval && parser_pop_local(parser);
+
+    if (parser->tok == ';')
+        return parser_next(parser);
+    else if (opts_standard == COMPILER_QCC)
+        parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
+    return retval;
+
+enderrfn:
+    ast_function_delete(func);
+    var->constval.vfunc = NULL;
+
+enderr:
+    while (parser->locals_count) {
+        parser->locals_count--;
+        mem_d(parser->locals[parser->locals_count].name);
+    }
+    parser->function = old;
+    return false;
 }
 
-static bool parser_variable(parser_t *parser, ast_block *localblock)
+static bool parse_variable(parser_t *parser, ast_block *localblock)
 {
-    bool          isfunc = false;
-    ast_function *func = NULL;
-    lex_ctx       ctx;
-    ast_value    *var;
-    varentry_t    varent;
+    bool            isfunc = false;
+    lex_ctx         ctx;
+
+    ast_value      *var = NULL;
+    bool cleanvar = false;
+
+    varentry_t      varent;
+    varentry_t      ve[3];
+
     ast_expression *olddecl;
 
+    ast_value      *typevar;
+
     bool hadproto;
     bool isparam;
 
+    bool retval = true;
+
+    /* go */
+
     int basetype = parser_token(parser)->constval.t;
 
+    if (!parser_next(parser)) {
+        parseerror(parser, "expected variable definition");
+        return false;
+    }
+
+    typevar = parse_type(parser, basetype, &isfunc);
+    if (!typevar)
+        return false;
+
     while (true)
     {
-        hadproto = false;
+        hadproto    = false;
+        olddecl     = NULL;
+        isparam     = false;
+        varent.name = NULL;
 
-        if (!parser_next(parser)) { /* skip basetype or comma */
-            parseerror(parser, "expected variable declaration");
-            return false;
-        }
+        ve[0].name = ve[1].name = ve[2].name = NULL;
+        ve[0].var  = ve[1].var  = ve[2].var  = NULL;
 
-        olddecl = NULL;
-        isfunc  = false;
-        func    = NULL;
-        isparam = false;
         ctx = parser_ctx(parser);
-        var = parser_parse_type(parser, basetype, &isfunc);
+        var = ast_value_copy(typevar);
+        cleanvar = true;
 
-        if (!var)
-            return false;
+        if (!var) {
+            parseerror(parser, "failed to create variable");
+            retval = false;
+            goto cleanup;
+        }
 
         if (parser->tok != TOKEN_IDENT) {
-            parseerror(parser, "expected variable name\n");
-            return false;
+            parseerror(parser, "expected variable name");
+            retval = false;
+            goto cleanup;
         }
 
         if (!isfunc) {
             if (!localblock && (olddecl = parser_find_global(parser, parser_tokval(parser)))) {
-                ast_value_delete(var);
                 parseerror(parser, "global `%s` already declared here: %s:%i",
                            parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
-                return false;
+                retval = false;
+                goto cleanup;
             }
 
             if (localblock) {
@@ -1907,22 +2299,24 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
                     if (olddecl)
                     {
                         if (!isparam) {
-                            ast_value_delete(var);
                             parseerror(parser, "local `%s` already declared here: %s:%i",
                                        parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
-                            return false;
+                            retval = false;
+                            goto cleanup;
                         }
                     }
 
-                    if( (!isparam && olddecl) || (olddecl = parser_find_local(parser, parser_tokval(parser), 0, &isparam)) )
+                    if( (!isparam && olddecl) ||
+                        (olddecl = parser_find_local(parser, parser_tokval(parser), 0, &isparam))
+                      )
                     {
                         if (parsewarning(parser, WARN_LOCAL_SHADOWS,
                                          "local `%s` is shadowing a parameter", parser_tokval(parser)))
                         {
-                            ast_value_delete(var);
                             parseerror(parser, "local `%s` already declared here: %s:%i",
                                        parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
-                            return false;
+                            retval = false;
+                            goto cleanup;
                         }
                     }
                 }
@@ -1931,17 +2325,20 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
                     if (olddecl)
                     {
                         ast_value_delete(var);
+                        var = NULL;
                         if (isparam &&
                             parsewarning(parser, WARN_LOCAL_SHADOWS,
                                          "a parameter is shadowing local `%s`", parser_tokval(parser)))
                         {
-                            return false;
+                            retval = false;
+                            goto cleanup;
                         }
                         else if (!isparam)
                         {
                             parseerror(parser, "local `%s` already declared here: %s:%i",
                                        parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
-                            return false;
+                            retval = false;
+                            goto cleanup;
                         }
                         goto nextvar;
                     }
@@ -1951,8 +2348,8 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
 
         if (!ast_value_set_name(var, parser_tokval(parser))) {
             parseerror(parser, "failed to set variable name\n");
-            ast_value_delete(var);
-            return false;
+            retval = false;
+            goto cleanup;
         }
 
         if (isfunc) {
@@ -1969,16 +2366,13 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
             if (olddecl) {
                 /* we had a prototype */
                 if (!ast_istype(olddecl, ast_value)) {
-                    /* theoretically not possible you think?
-                     * well:
-                     * vector v;
+                    /* vector v;
                      * void() v_x = {}
-                     * got it?
                      */
                     parseerror(parser, "cannot declare a function with the same name as a vector's member: %s",
                                parser_tokval(parser));
-                    ast_value_delete(var);
-                    return false;
+                    retval = false;
+                    goto cleanup;
                 }
 
                 proto = (ast_value*)olddecl;
@@ -1988,16 +2382,15 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
              * as return type
              */
             fval = ast_value_new(ctx, var->name, TYPE_FUNCTION);
-            func = ast_function_new(ctx, var->name, fval);
-            if (!fval || !func) {
-                ast_value_delete(var);
-                if (fval) ast_value_delete(fval);
-                if (func) ast_function_delete(func);
-                return false;
+            if (!fval) {
+                retval = false;
+                goto cleanup;
             }
 
             fval->expression.next = (ast_expression*)var;
             MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params);
+            fval->expression.variadic = var->expression.variadic;
+            var = NULL;
 
             /* we compare the type late here, but it's easier than
              * messing with the parameter-vector etc. earlier
@@ -2008,9 +2401,9 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
                     parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
                                proto->name,
                                ast_ctx(proto).file, ast_ctx(proto).line);
-                    ast_function_delete(func);
                     ast_value_delete(fval);
-                    return false;
+                    retval = false;
+                    goto cleanup;
                 }
                 /* copy over the parameter names */
                 for (param = 0; param < fval->expression.params_count; ++param)
@@ -2019,20 +2412,10 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
                 ast_ctx(proto) = ast_ctx(fval);
 
                 /* now ditch the rest of the new data */
-                ast_function_delete(func);
                 ast_value_delete(fval);
                 fval = proto;
-                func = proto->constval.vfunc;
                 hadproto = true;
             }
-            else
-            {
-                if (!parser_t_functions_add(parser, func)) {
-                    ast_function_delete(func);
-                    ast_value_delete(fval);
-                    return false;
-                }
-            }
 
             var = fval;
         }
@@ -2040,358 +2423,182 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
         if (!hadproto) {
             varent.name = util_strdup(var->name);
             varent.var = (ast_expression*)var;
+
+            if (!localblock) {
+                if (!(retval = parser_t_globals_add(parser, varent)))
+                    goto cleanup;
+            } else {
+                if (!(retval = parser_t_locals_add(parser, varent)))
+                    goto cleanup;
+                if (!(retval = ast_block_locals_add(localblock, var))) {
+                    parser->locals_count--;
+                    goto cleanup;
+                }
+            }
+
             if (var->expression.vtype == TYPE_VECTOR)
             {
-                varentry_t vx, vy, vz;
-                if (!create_vector_members(parser, var, &vx, &vy, &vz)) {
-                    ast_delete(var);
-                    return false;
+                size_t e;
+                if (!create_vector_members(parser, var, ve)) {
+                    retval = false;
+                    goto cleanup;
                 }
 
                 if (!localblock) {
-                    (void)!parser_t_globals_add(parser, varent);
-                    (void)!parser_t_globals_add(parser, vx);
-                    (void)!parser_t_globals_add(parser, vy);
-                    (void)!parser_t_globals_add(parser, vz);
+                    for (e = 0; e < 3; ++e) {
+                        if (!(retval = parser_t_globals_add(parser, ve[e])))
+                            break;
+                    }
+                    if (!retval) {
+                        parser->globals_count -= e+1;
+                        goto cleanup;
+                    }
                 } else {
-                    (void)!parser_t_locals_add(parser, varent);
-                    (void)!parser_t_locals_add(parser, vx);
-                    (void)!parser_t_locals_add(parser, vy);
-                    (void)!parser_t_locals_add(parser, vz);
-                    if (!ast_block_locals_add(localblock, var) ||
-                        !ast_block_collect(localblock, vx.var) ||
-                        !ast_block_collect(localblock, vy.var) ||
-                        !ast_block_collect(localblock, vz.var))
-                    {
-                        parser_pop_local(parser);
-                        parser_pop_local(parser);
-                        parser_pop_local(parser);
-                        parser_pop_local(parser);
-                        ast_value_delete(var);
-                        return false;
+                    for (e = 0; e < 3; ++e) {
+                        if (!(retval = parser_t_locals_add(parser, ve[e])))
+                            break;
+                        if (!(retval = ast_block_collect(localblock, ve[e].var)))
+                            break;
+                        ve[e].var = NULL; /* from here it's being collected in the block */
+                    }
+                    if (!retval) {
+                        parser->locals_count -= e+1;
+                        localblock->locals_count--;
+                        goto cleanup;
                     }
                 }
+                ve[0].name = ve[1].name = ve[2].name = NULL;
+                ve[0].var  = ve[1].var  = ve[2].var  = NULL;
             }
-            else
-            {
-                if ( (!localblock && !parser_t_globals_add(parser, varent)) ||
-                     ( localblock && !parser_t_locals_add(parser, varent)) )
-                {
-                    ast_value_delete(var);
-                    return false;
-                }
-                if (localblock && !ast_block_locals_add(localblock, var))
-                {
-                    parser_pop_local(parser);
-                    ast_value_delete(var);
-                    return false;
-                }
-            }
+            cleanvar = false;
+            varent.name = NULL;
         }
 
 nextvar:
-        if (!parser_next(parser)) {
-            ast_value_delete(var);
-            return false;
-        }
+        if (!(retval = parser_next(parser)))
+            goto cleanup;
 
         if (parser->tok == ';') {
-            if (!parser_next(parser))
-                return parser->tok == TOKEN_EOF;
-            return true;
+            ast_value_delete(typevar);
+            return parser_next(parser);
         }
 
         if (parser->tok == ',') {
             /* another var */
+            if (!(retval = parser_next(parser)))
+                goto cleanup;
             continue;
         }
 
+        /* NOTE: only 'typevar' needs to be deleted from here on, so 'cleanup' won't be used
+         * to avoid having too many gotos
+         */
+        if (localblock && opts_standard == COMPILER_QCC) {
+            if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
+                             "initializing expression turns variable `%s` into a constant in this standard",
+                             var->name) )
+            {
+                ast_value_delete(typevar);
+                return false;
+            }
+        }
+
         if (parser->tok != '=') {
-            parseerror(parser, "expected '=' or ';'");
+            if (opts_standard == COMPILER_QCC)
+                parseerror(parser, "missing semicolon");
+            else
+                parseerror(parser, "missing semicolon or initializer");
+            ast_value_delete(typevar);
             return false;
         }
 
-        if (!parser_next(parser))
+        if (!parser_next(parser)) {
+            ast_value_delete(typevar);
             return false;
+        }
 
         if (parser->tok == '#') {
+            ast_function *func;
+
             if (localblock) {
                 parseerror(parser, "cannot declare builtins within functions");
+                ast_value_delete(typevar);
                 return false;
             }
-            if (!isfunc || !func) {
+            if (!isfunc) {
                 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
+                ast_value_delete(typevar);
                 return false;
             }
             if (!parser_next(parser)) {
                 parseerror(parser, "expected builtin number");
+                ast_value_delete(typevar);
                 return false;
             }
             if (parser->tok != TOKEN_INTCONST) {
                 parseerror(parser, "builtin number must be an integer constant");
+                ast_value_delete(typevar);
                 return false;
             }
             if (parser_token(parser)->constval.i <= 0) {
                 parseerror(parser, "builtin number must be positive integer greater than zero");
+                ast_value_delete(typevar);
                 return false;
             }
 
-            func->builtin = -parser_token(parser)->constval.i;
-
-            if (!parser_next(parser))
+            func = ast_function_new(ast_ctx(var), var->name, var);
+            if (!func) {
+                parseerror(parser, "failed to allocate function for `%s`", var->name);
+                ast_value_delete(typevar);
                 return false;
-        } else if (parser->tok == '{' || parser->tok == '[') {
-            /* function body */
-            ast_function *old;
-            ast_block *block;
-            size_t     parami;
-
-            ast_expression *fld_think, *fld_nextthink, *fld_frame;
-            ast_expression *gbl_time, *gbl_self;
-            ast_expression *framenum, *nextthink;
-            bool            has_frame_think;
-
-            has_frame_think = false;
-            old = parser->function;
-
-            if (localblock) {
-                parseerror(parser, "cannot declare functions within functions");
-                return false;
-            }
-
-            if (parser->tok == '[') {
-                /* got a frame definition: [ framenum, nextthink ]
-                 * this translates to:
-                 * self.frame = framenum;
-                 * self.nextthink = time + 0.1;
-                 * self.think = nextthink;
-                 */
-                nextthink = NULL;
-
-                fld_think     = parser_find_field(parser, "think");
-                fld_nextthink = parser_find_field(parser, "nextthink");
-                fld_frame     = parser_find_field(parser, "frame");
-                if (!fld_think || !fld_nextthink || !fld_frame) {
-                    parseerror(parser, "cannot use [frame,think] notation without the required fields");
-                    parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
-                    return false;
-                }
-                gbl_time      = parser_find_global(parser, "time");
-                gbl_self      = parser_find_global(parser, "self");
-                if (!gbl_time || !gbl_self) {
-                    parseerror(parser, "cannot use [frame,think] notation without the required globals");
-                    parseerror(parser, "please declare the following globals: `time`, `self`");
-                    return false;
-                }
-
-                if (!parser_next(parser)) {
-                    return false;
-                }
-
-                framenum = parser_expression_leave(parser, true);
-                if (!framenum) {
-                    parseerror(parser, "expected a framenumber constant in[frame,think] notation");
-                    return false;
-                }
-                if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
-                    ast_unref(framenum);
-                    parseerror(parser, "framenumber in [frame,think] notation must be a constant");
-                }
-
-                if (parser->tok != ',') {
-                    ast_unref(framenum);
-                    parseerror(parser, "expected comma after frame number in [frame,think] notation");
-                    parseerror(parser, "Got a %i\n", parser->tok);
-                    return false;
-                }
-
-                if (!parser_next(parser)) {
-                    ast_unref(framenum);
-                    return false;
-                }
-
-                if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
-                {
-                    /* qc allows the use of not-yet-declared functions here
-                     * - this automatically creates a prototype */
-                    varentry_t      varent;
-                    ast_value      *thinkfunc;
-                    ast_expression *functype = fld_think->expression.next;
-
-                    thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
-                    if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
-                        ast_unref(framenum);
-                        parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
-                        return false;
-                    }
-
-                    if (!parser_next(parser)) {
-                        ast_unref(framenum);
-                        return false;
-                    }
-
-                    varent.var = (ast_expression*)thinkfunc;
-                    varent.name = util_strdup(thinkfunc->name);
-                    if (thinkfunc->expression.vtype == TYPE_FUNCTION)
-                    {
-                        ast_function *func;
-
-                        func = ast_function_new(parser_ctx(parser), thinkfunc->name, thinkfunc);
-                        if (!func) {
-                            ast_delete(thinkfunc);
-                            ast_unref(framenum);
-                            parseerror(parser, "failed to create function for implicit prototype for `%s`",
-                                       thinkfunc->name);
-                            return false;
-                        }
-                        (void)!parser_t_functions_add(parser, func);
-                        (void)!parser_t_globals_add(parser, varent);
-                    }
-                    else
-                        (void)!parser_t_globals_add(parser, varent);
-                    nextthink = (ast_expression*)thinkfunc;
-
-                } else {
-                    nextthink = parser_expression_leave(parser, true);
-                    if (!nextthink) {
-                        ast_unref(framenum);
-                        parseerror(parser, "expected a think-function in [frame,think] notation");
-                        return false;
-                    }
-                }
-
-                if (!ast_istype(nextthink, ast_value) || !( (ast_value*)nextthink )->isconst) {
-                    ast_unref(nextthink);
-                    ast_unref(framenum);
-                    parseerror(parser, "think-function in [frame,think] notation must be a constant");
-                }
-
-                if (parser->tok != ']') {
-                    parseerror(parser, "expected closing `]` for [frame,think] notation");
-                    ast_unref(nextthink);
-                    ast_unref(framenum);
-                    return false;
-                }
-
-                if (!parser_next(parser)) {
-                    ast_unref(nextthink);
-                    ast_unref(framenum);
-                    return false;
-                }
-
-                if (parser->tok != '{') {
-                    parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
-                    ast_unref(nextthink);
-                    ast_unref(framenum);
-                    return false;
-                }
-
-                has_frame_think = true;
             }
-
-            block = ast_block_new(parser_ctx(parser));
-            if (!block) {
-                parseerror(parser, "failed to allocate block");
+            if (!parser_t_functions_add(parser, func)) {
+                parseerror(parser, "failed to allocate slot for function `%s`", var->name);
+                ast_function_delete(func);
+                var->constval.vfunc = NULL;
+                ast_value_delete(typevar);
                 return false;
             }
 
-            if (has_frame_think) {
-                lex_ctx ctx;
-                ast_expression *self_frame;
-                ast_expression *self_nextthink;
-                ast_expression *self_think;
-                ast_expression *time_plus_1;
-                ast_store *store_frame;
-                ast_store *store_nextthink;
-                ast_store *store_think;
-
-                ctx = parser_ctx(parser);
-                self_frame     = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
-                self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
-                self_think     = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
-
-                time_plus_1    = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
-                                 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
-
-                store_frame     = ast_store_new(ctx, INSTR_STOREP_F,   self_frame,     framenum);
-                store_nextthink = ast_store_new(ctx, INSTR_STOREP_F,   self_nextthink, time_plus_1);
-                store_think     = ast_store_new(ctx, INSTR_STOREP_FNC, self_think,     nextthink);
-
-                if (!ast_block_exprs_add(block, (ast_expression*)store_frame)     ||
-                    !ast_block_exprs_add(block, (ast_expression*)store_nextthink) ||
-                    !ast_block_exprs_add(block, (ast_expression*)store_think) )
-                {
-                    parseerror(parser, "failed to generate code for [frame,think]");
-                    ast_block_delete(block);
-                    return false;
-                }
-            }
-
-            for (parami = 0; parami < var->expression.params_count; ++parami) {
-                ast_value *param = var->expression.params[parami];
-                varentry_t vx, vy, vz;
-
-                if (param->expression.vtype != TYPE_VECTOR &&
-                    (param->expression.vtype != TYPE_FIELD ||
-                     param->expression.next->expression.vtype != TYPE_VECTOR))
-                {
-                    continue;
-                }
-
-                if (!create_vector_members(parser, param, &vx, &vy, &vz)) {
-                    ast_block_delete(block);
-                    return false;
-                }
-
-                (void)!parser_t_locals_add(parser, vx);
-                (void)!parser_t_locals_add(parser, vy);
-                (void)!parser_t_locals_add(parser, vz);
-                if (!ast_block_collect(block, vx.var) ||
-                    !ast_block_collect(block, vy.var) ||
-                    !ast_block_collect(block, vz.var) )
-                {
-                    parser_pop_local(parser);
-                    parser_pop_local(parser);
-                    parser_pop_local(parser);
-                    ast_block_delete(block);
-                    return false;
-                }
-            }
+            func->builtin = -parser_token(parser)->constval.i;
 
-            parser->function = func;
-            if (!parser_parse_block_into(parser, block, true)) {
-                ast_block_delete(block);
-                parser->function = old;
+            if (!parser_next(parser)) {
+                ast_value_delete(typevar);
                 return false;
             }
-            parser->function = old;
-
-            if (!block)
+        }
+        else if (parser->tok == '{' || parser->tok == '[')
+        {
+            ast_value_delete(typevar);
+            if (localblock) {
+                parseerror(parser, "cannot declare functions within functions");
                 return false;
+            }
 
-            if (!ast_function_blocks_add(func, block)) {
-                ast_block_delete(block);
+            if (!parse_function_body(parser, var)) {
                 return false;
             }
-
-            if (parser->tok == ';')
-                return parser_next(parser) || parser->tok == TOKEN_EOF;
-            else if (opts_standard == COMPILER_QCC)
-                parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
             return true;
         } else {
             ast_expression *cexp;
             ast_value      *cval;
 
-            cexp = parser_expression_leave(parser, true);
+            cexp = parse_expression_leave(parser, true);
+            if (!cexp) {
+                ast_value_delete(typevar);
+                return false;
+            }
+
             cval = (ast_value*)cexp;
             if (!ast_istype(cval, ast_value) || !cval->isconst)
                 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
             else
             {
                 var->isconst = true;
-                memcpy(&var->constval, &cval->constval, sizeof(var->constval));
-                memset(&cval->constval, 0, sizeof(cval->constval));
+                if (cval->expression.vtype == TYPE_STRING)
+                    var->constval.vstring = parser_strdup(cval->constval.vstring);
+                else
+                    memcpy(&var->constval, &cval->constval, sizeof(var->constval));
                 ast_unref(cval);
             }
         }
@@ -2403,20 +2610,35 @@ nextvar:
 
         if (parser->tok != ';') {
             parseerror(parser, "missing semicolon");
+            ast_value_delete(typevar);
             return false;
         }
 
         (void)parser_next(parser);
 
+        ast_value_delete(typevar);
         return true;
     }
+
+cleanup:
+    ast_delete(typevar);
+    if (var && cleanvar) ast_delete(var);
+    if (varent.name) mem_d(varent.name);
+    if (ve[0].name)  mem_d(ve[0].name);
+    if (ve[1].name)  mem_d(ve[1].name);
+    if (ve[2].name)  mem_d(ve[2].name);
+    if (ve[0].var)   mem_d(ve[0].var);
+    if (ve[1].var)   mem_d(ve[1].var);
+    if (ve[2].var)   mem_d(ve[2].var);
+
+    return retval;
 }
 
-static bool parser_do(parser_t *parser)
+static bool parser_global_statement(parser_t *parser)
 {
     if (parser->tok == TOKEN_TYPENAME)
     {
-        return parser_variable(parser, NULL);
+        return parse_variable(parser, NULL);
     }
     else if (parser->tok == TOKEN_KEYWORD)
     {
@@ -2445,12 +2667,12 @@ static bool parser_do(parser_t *parser)
 
         /* parse into the declaration */
         if (!parser_next(parser)) {
-            parseerror(parser, "expected field def");
+            parseerror(parser, "expected field definition");
             return false;
         }
 
         /* parse the field type fully */
-        typevar = var = parser_parse_type(parser, basetype, &isfunc);
+        typevar = var = parse_type(parser, basetype, &isfunc);
         if (!var)
             return false;
 
@@ -2484,6 +2706,7 @@ static bool parser_do(parser_t *parser)
                 }
                 fval->expression.next = (ast_expression*)var;
                 MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params);
+                fval->expression.variadic = var->expression.variadic;
                 var = fval;
             }
 
@@ -2511,7 +2734,7 @@ static bool parser_do(parser_t *parser)
                     ast_delete(fld);
                     return false;
                 } else {
-                    if (parsewarning(parser, WARN_FIELD_REDECLARED, "field %s has already been declared here: %s:%i",
+                    if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` has already been declared here: %s:%i",
                                      parser_tokval(parser), ast_ctx(oldex).file, (int)ast_ctx(oldex).line))
                     {
                         ast_delete(fld);
@@ -2530,14 +2753,14 @@ static bool parser_do(parser_t *parser)
             if (var->expression.vtype == TYPE_VECTOR)
             {
                 /* create _x, _y and _z fields as well */
-                varentry_t vx, vy, vz;
-                if (!create_vector_members(parser, fld, &vx, &vy, &vz)) {
+                varentry_t ve[3];
+                if (!create_vector_members(parser, fld, ve)) {
                     ast_delete(fld);
                     return false;
                 }
-                (void)!parser_t_fields_add(parser, vx);
-                (void)!parser_t_fields_add(parser, vy);
-                (void)!parser_t_fields_add(parser, vz);
+                (void)!parser_t_fields_add(parser, ve[0]);
+                (void)!parser_t_fields_add(parser, ve[1]);
+                (void)!parser_t_fields_add(parser, ve[2]);
             }
 
 nextfield:
@@ -2569,7 +2792,7 @@ nextfield:
     }
     else
     {
-        parseerror(parser, "unexpected token: %s", parser->lex->tok->value);
+        parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
         return false;
     }
     return true;
@@ -2602,16 +2825,21 @@ bool parser_compile(const char *filename)
     {
         while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
         {
-            if (!parser_do(parser)) {
+            if (!parser_global_statement(parser)) {
                 if (parser->tok == TOKEN_EOF)
                     parseerror(parser, "unexpected eof");
                 else if (!parser->errors)
-                    parseerror(parser, "parse error\n");
+                    parseerror(parser, "parse error");
                 lex_close(parser->lex);
                 parser->lex = NULL;
                 return false;
             }
         }
+    } else {
+        parseerror(parser, "parse error");
+        lex_close(parser->lex);
+        parser->lex = NULL;
+        return false;
     }
 
     lex_close(parser->lex);
@@ -2658,6 +2886,7 @@ bool parser_finish(const char *output)
 {
     size_t i;
     ir_builder *ir;
+    bool retval = true;
 
     if (!parser->errors)
     {
@@ -2667,27 +2896,6 @@ bool parser_finish(const char *output)
             return false;
         }
 
-        for (i = 0; i < parser->imm_float_count; ++i) {
-            if (!ast_global_codegen(parser->imm_float[i], ir)) {
-                printf("failed to generate global %s\n", parser->imm_float[i]->name);
-                ir_builder_delete(ir);
-                return false;
-            }
-        }
-        for (i = 0; i < parser->imm_string_count; ++i) {
-            if (!ast_global_codegen(parser->imm_string[i], ir)) {
-                printf("failed to generate global %s\n", parser->imm_string[i]->name);
-                ir_builder_delete(ir);
-                return false;
-            }
-        }
-        for (i = 0; i < parser->imm_vector_count; ++i) {
-            if (!ast_global_codegen(parser->imm_vector[i], ir)) {
-                printf("failed to generate global %s\n", parser->imm_vector[i]->name);
-                ir_builder_delete(ir);
-                return false;
-            }
-        }
         for (i = 0; i < parser->fields_count; ++i) {
             ast_value *field;
             bool isconst;
@@ -2715,14 +2923,41 @@ bool parser_finish(const char *output)
             }
         }
         for (i = 0; i < parser->globals_count; ++i) {
+            ast_value *asvalue;
             if (!ast_istype(parser->globals[i].var, ast_value))
                 continue;
-            if (!ast_global_codegen((ast_value*)(parser->globals[i].var), ir)) {
+            asvalue = (ast_value*)(parser->globals[i].var);
+            if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) {
+                retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
+                                               "unused global: `%s`", asvalue->name);
+            }
+            if (!ast_global_codegen(asvalue, ir)) {
                 printf("failed to generate global %s\n", parser->globals[i].name);
                 ir_builder_delete(ir);
                 return false;
             }
         }
+        for (i = 0; i < parser->imm_float_count; ++i) {
+            if (!ast_global_codegen(parser->imm_float[i], ir)) {
+                printf("failed to generate global %s\n", parser->imm_float[i]->name);
+                ir_builder_delete(ir);
+                return false;
+            }
+        }
+        for (i = 0; i < parser->imm_string_count; ++i) {
+            if (!ast_global_codegen(parser->imm_string[i], ir)) {
+                printf("failed to generate global %s\n", parser->imm_string[i]->name);
+                ir_builder_delete(ir);
+                return false;
+            }
+        }
+        for (i = 0; i < parser->imm_vector_count; ++i) {
+            if (!ast_global_codegen(parser->imm_vector[i], ir)) {
+                printf("failed to generate global %s\n", parser->imm_vector[i]->name);
+                ir_builder_delete(ir);
+                return false;
+            }
+        }
         for (i = 0; i < parser->functions_count; ++i) {
             if (!ast_function_codegen(parser->functions[i], ir)) {
                 printf("failed to generate function %s\n", parser->functions[i]->name);
@@ -2736,17 +2971,19 @@ bool parser_finish(const char *output)
             }
         }
 
-        if (opts_dump)
-            ir_builder_dump(ir, printf);
+        if (retval) {
+            if (opts_dump)
+                ir_builder_dump(ir, printf);
 
-        if (!ir_builder_generate(ir, output)) {
-            printf("*** failed to generate output file\n");
-            ir_builder_delete(ir);
-            return false;
+            if (!ir_builder_generate(ir, output)) {
+                printf("*** failed to generate output file\n");
+                ir_builder_delete(ir);
+                return false;
+            }
         }
 
         ir_builder_delete(ir);
-        return true;
+        return retval;
     }
 
     printf("*** there were compile errors\n");