]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
lex_ctx of a return will be at the return keyword now
[xonotic/gmqcc.git] / parser.c
index afe1f11220bab851f02cc15cc1409e546dfbe735..c13fb8927decb5e12b8602665f5a734f9e265aac 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -75,6 +75,7 @@ typedef struct {
     size_t          *_blocklocals;
     ast_value      **_typedefs;
     size_t          *_blocktypedefs;
+    lex_ctx         *_block_ctx;
 
     size_t errors;
 
@@ -92,7 +93,7 @@ static void parser_enterblock(parser_t *parser);
 static bool parser_leaveblock(parser_t *parser);
 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
 static bool parse_typedef(parser_t *parser);
-static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef);
+static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef);
 static ast_block* parse_block(parser_t *parser);
 static bool parse_block_into(parser_t *parser, ast_block *block);
 static ast_expression* parse_statement_or_block(parser_t *parser);
@@ -1412,8 +1413,14 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
                 goto onerr;
             }
-            if (ast_istype(var, ast_value))
+            if (ast_istype(var, ast_value)) {
                 ((ast_value*)var)->uses++;
+            }
+            else if (ast_istype(var, ast_member)) {
+                ast_member *mem = (ast_member*)var;
+                if (ast_istype(mem->owner, ast_value))
+                    ((ast_value*)(mem->owner))->uses++;
+            }
             vec_push(sy.out, syexp(parser_ctx(parser), var));
             DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
         }
@@ -1674,6 +1681,7 @@ static void parser_enterblock(parser_t *parser)
     vec_push(parser->_blocklocals, vec_size(parser->_locals));
     vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
     vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
+    vec_push(parser->_block_ctx, parser_ctx(parser));
 }
 
 static bool parser_leaveblock(parser_t *parser)
@@ -1700,8 +1708,10 @@ static bool parser_leaveblock(parser_t *parser)
         ast_value      *v = (ast_value*)e;
         vec_pop(parser->_locals);
         if (ast_istype(e, ast_value) && !v->uses) {
-            if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
+            if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name)) {
+                parser->errors++;
                 rv = false;
+            }
         }
     }
 
@@ -1713,6 +1723,7 @@ static bool parser_leaveblock(parser_t *parser)
     util_htdel(vec_last(parser->typedefs));
     vec_pop(parser->typedefs);
 
+    vec_pop(parser->_block_ctx);
     return rv;
 }
 
@@ -2035,6 +2046,8 @@ static bool parse_return(parser_t *parser, ast_block *block, ast_expression **ou
     ast_return     *ret = NULL;
     ast_value      *expected = parser->function->vtype;
 
+    lex_ctx ctx = parser_ctx(parser);
+
     (void)block; /* not touching */
 
     if (!parser_next(parser)) {
@@ -2065,7 +2078,7 @@ static bool parse_return(parser_t *parser, ast_block *block, ast_expression **ou
             else
                 parseerror(parser, "return without value");
         }
-        ret = ast_return_new(parser_ctx(parser), NULL);
+        ret = ast_return_new(ctx, NULL);
     }
     *out = (ast_expression*)ret;
     return true;
@@ -3562,7 +3575,7 @@ static bool parse_typedef(parser_t *parser)
     return true;
 }
 
-static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef)
+static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef)
 {
     ast_value *var;
     ast_value *proto;
@@ -3625,8 +3638,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
             }
         }
 
-        if (is_const_var == CV_CONST)
-            var->cvq = CV_CONST;
+        var->cvq = qualifier;
 
         /* Part 1:
          * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
@@ -3999,7 +4011,7 @@ skipvar:
                 {
                     if (opts_standard != COMPILER_GMQCC &&
                         !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
-                        is_const_var != CV_VAR)
+                        qualifier != CV_VAR)
                     {
                         var->cvq = CV_CONST;
                     }
@@ -4359,6 +4371,8 @@ void parser_cleanup()
     vec_free(parser->typedefs);
     vec_free(parser->_blocktypedefs);
 
+    vec_free(parser->_block_ctx);
+
     vec_free(parser->labels);
     vec_free(parser->gotos);