]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
improve the warning
[xonotic/gmqcc.git] / parser.c
index de0bc52023a18a1e034e8c050ef521f5fe1f75b5..194f224080787a26369e7757f2082147ab391451 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -380,6 +380,44 @@ static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
 # define DEBUGSHUNTDO(x)
 #endif
 
+/* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
+ * so we need to rotate it to become ent.(foo[n]).
+ */
+static bool rotate_entfield_array_index_nodes(ast_expression **out)
+{
+    ast_array_index *index;
+    ast_entfield    *entfield;
+
+    ast_value       *field;
+    ast_expression  *sub;
+    ast_expression  *entity;
+
+    lex_ctx ctx = ast_ctx(*out);
+
+    if (!ast_istype(*out, ast_array_index))
+        return false;
+    index = (ast_array_index*)*out;
+
+    if (!ast_istype(index->array, ast_entfield))
+        return false;
+    entfield = (ast_entfield*)index->array;
+
+    if (!ast_istype(entfield->field, ast_value))
+        return false;
+    field = (ast_value*)entfield->field;
+
+    sub    = index->index;
+    entity = entfield->entity;
+
+    ast_delete(index);
+
+    index = ast_array_index_new(ctx, (ast_expression*)field, sub);
+    entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
+    *out = (ast_expression*)entfield;
+
+    return true;
+}
+
 static bool parser_sy_pop(parser_t *parser, shunt *sy)
 {
     const oper_info *op;
@@ -464,7 +502,10 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
             break;
 
         case opid1('['):
-            if (exprs[0]->expression.vtype != TYPE_ARRAY) {
+            if (exprs[0]->expression.vtype != TYPE_ARRAY &&
+                !(exprs[0]->expression.vtype == TYPE_FIELD &&
+                  exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
+            {
                 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
                 parseerror(parser, "cannot index value of type %s", ty1);
                 return false;
@@ -475,6 +516,15 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
                 return false;
             }
             out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
+            if (rotate_entfield_array_index_nodes(&out))
+            {
+                if (opts_standard != COMPILER_GMQCC) {
+                    /* this error doesn't need to make us bail out */
+                    (void)!parsewarning(parser, WARN_EXTENSIONS,
+                                        "accessing array-field members of an entity without parenthesis\n"
+                                        " -> this is an extension from -std=gmqcc");
+                }
+            }
             break;
 
         case opid1(','):
@@ -2344,16 +2394,19 @@ static ast_expression *array_field_setter_node(
         ast_return      *ret;
         ast_entfield    *entfield;
         ast_array_index *subscript;
-        int assignop = type_store_instr[value->expression.vtype];
+        int assignop = type_storep_instr[value->expression.vtype];
 
         if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
-            assignop = INSTR_STORE_V;
+            assignop = INSTR_STOREP_V;
 
         subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
         if (!subscript)
             return NULL;
 
-        entfield = ast_entfield_new(ctx, (ast_expression*)entity, (ast_expression*)subscript);
+        entfield = ast_entfield_new_force(ctx,
+                                          (ast_expression*)entity,
+                                          (ast_expression*)subscript,
+                                          (ast_expression*)subscript);
         if (!entfield) {
             ast_delete(subscript);
             return NULL;
@@ -3740,7 +3793,7 @@ bool parser_finish(const char *output)
                     !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
                     !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
                 {
-                    printf("failed to generate setter for %s\n", parser->globals[i].name);
+                    printf("failed to generate setter for %s\n", parser->fields[i].name);
                     ir_builder_delete(ir);
                     return false;
                 }
@@ -3750,7 +3803,7 @@ bool parser_finish(const char *output)
                     !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
                     !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
                 {
-                    printf("failed to generate getter for %s\n", parser->globals[i].name);
+                    printf("failed to generate getter for %s\n", parser->fields[i].name);
                     ir_builder_delete(ir);
                     return false;
                 }