]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.cpp
Use std::vector for ast switch cases
[xonotic/gmqcc.git] / parser.cpp
index ed84b618e04f818381e87003c65f5fa53d1d262f..86535fbf0f5d15179eca79fec9b06d06538a0598 100644 (file)
@@ -147,15 +147,14 @@ static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t
     return NULL;
 }
 
-typedef struct
-{
+struct sy_elem {
     size_t etype; /* 0 = expression, others are operators */
-    bool            isparen;
-    size_t          off;
+    bool isparen;
+    size_t off;
     ast_expression *out;
-    ast_block      *block; /* for commas and function calls */
+    ast_block *block; /* for commas and function calls */
     lex_ctx_t ctx;
-} sy_elem;
+};
 
 enum {
     PAREN_EXPR,
@@ -164,13 +163,13 @@ enum {
     PAREN_TERNARY1,
     PAREN_TERNARY2
 };
-typedef struct
-{
+
+struct shunt {
     sy_elem        *out;
     sy_elem        *ops;
     size_t         *argc;
     unsigned int   *paren;
-} shunt;
+};
 
 static sy_elem syexp(lex_ctx_t ctx, ast_expression *v) {
     sy_elem e;
@@ -2027,7 +2026,7 @@ static void parser_addlocal(parser_t *parser, const char *name, ast_expression *
 
 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e)
 {
-    vec_push(parser->globals, e);
+    parser->globals.push_back(e);
     util_htset(parser->htglobals, name, e);
 }
 
@@ -2724,10 +2723,10 @@ static bool parse_break_continue(parser_t *parser, ast_block *block, ast_express
 /* returns true when it was a variable qualifier, false otherwise!
  * on error, cvq is set to CV_WRONG
  */
-typedef struct {
+struct attribute_t {
     const char *name;
     size_t      flag;
-} attribute_t;
+};
 
 static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags, char **message)
 {
@@ -3143,7 +3142,7 @@ static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression *
             return false;
         }
         swcase.code = (ast_expression*)caseblock;
-        vec_push(switchnode->cases, swcase);
+        switchnode->cases.push_back(swcase);
         while (true) {
             ast_expression *expr;
             if (parser->tok == '}')
@@ -4036,7 +4035,7 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
             ast_block_delete(block);
             goto enderr;
         }
-        vec_push(parser->functions, func);
+        parser->functions.push_back(func);
     }
 
     parser_enterblock(parser);
@@ -4097,7 +4096,7 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
         goto enderrfn;
     }
 
-    vec_push(func->blocks, block);
+    func->blocks.push_back(block);
 
     parser->function = old;
     if (!parser_leaveblock(parser))
@@ -4115,7 +4114,7 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
 
 enderrfn:
     (void)!parser_leaveblock(parser);
-    vec_pop(parser->functions);
+    parser->functions.pop_back();
     ast_function_delete(func);
     var->constval.vfunc = NULL;
 
@@ -4357,10 +4356,10 @@ static bool parser_create_array_accessor(parser_t *parser, ast_value *array, con
         return false;
     }
 
-    vec_push(func->blocks, body);
+    func->blocks.push_back(body);
     *out = fval;
 
-    vec_push(parser->accessors, fval);
+    parser->accessors.push_back(fval);
 
     return true;
 }
@@ -5006,7 +5005,7 @@ static bool create_array_accessors(parser_t *parser, ast_value *var)
 static bool parse_array(parser_t *parser, ast_value *array)
 {
     size_t i;
-    if (array->initlist) {
+    if (array->initlist.size()) {
         parseerror(parser, "array already initialized elsewhere");
         return false;
     }
@@ -5024,7 +5023,7 @@ static bool parse_array(parser_t *parser, ast_value *array)
             parseerror(parser, "initializing element must be a compile time constant");
             return false;
         }
-        vec_push(array->initlist, v->constval);
+        array->initlist.push_back(v->constval);
         if (v->expression.vtype == TYPE_STRING) {
             array->initlist[i].vstring = util_strdupe(array->initlist[i].vstring);
             ++i;
@@ -5053,7 +5052,7 @@ static bool parse_array(parser_t *parser, ast_value *array)
             parseerror(parser, "array `%s' has already been initialized with %u elements",
                        array->name, (unsigned)array->expression.count);
         }
-        array->expression.count = vec_size(array->initlist);
+        array->expression.count = array->initlist.size();
         if (!create_array_accessors(parser, array))
             return false;
     }
@@ -5172,12 +5171,12 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
             was_end = false;
             if (!strcmp(var->name, "end_sys_globals")) {
                 var->uses++;
-                parser->crc_globals = vec_size(parser->globals);
+                parser->crc_globals = parser->globals.size();
                 was_end = true;
             }
             else if (!strcmp(var->name, "end_sys_fields")) {
                 var->uses++;
-                parser->crc_fields = vec_size(parser->fields);
+                parser->crc_fields = parser->fields.size();
                 was_end = true;
             }
             if (was_end && var->expression.vtype == TYPE_FIELD) {
@@ -5363,11 +5362,11 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                 /* deal with global variables, fields, functions */
                 if (!nofields && var->expression.vtype == TYPE_FIELD && parser->tok != '=') {
                     var->isfield = true;
-                    vec_push(parser->fields, (ast_expression*)var);
+                    parser->fields.push_back((ast_expression*)var);
                     util_htset(parser->htfields, var->name, var);
                     if (isvector) {
                         for (i = 0; i < 3; ++i) {
-                            vec_push(parser->fields, (ast_expression*)me[i]);
+                            parser->fields.push_back((ast_expression*)me[i]);
                             util_htset(parser->htfields, me[i]->name, me[i]);
                         }
                     }
@@ -5464,7 +5463,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                     ast_value_set_name(var, defname);
 
                     /* push it to the to-be-generated globals */
-                    vec_push(parser->globals, (ast_expression*)var);
+                    parser->globals.push_back((ast_expression*)var);
 
                     /* same game for the vector members */
                     if (isvector) {
@@ -5476,7 +5475,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                             vec_append(defname, ln, me[i]->name);
                             ast_member_set_name(me[i], defname);
 
-                            vec_push(parser->globals, (ast_expression*)me[i]);
+                            parser->globals.push_back((ast_expression*)me[i]);
                         }
                     }
                     vec_free(defname);
@@ -5649,7 +5648,7 @@ skipvar:
                     parseerror(parser, "failed to allocate function for `%s`", var->name);
                     break;
                 }
-                vec_push(parser->functions, func);
+                parser->functions.push_back(func);
 
                 func->builtin = -builtin_num-1;
             }
@@ -5726,10 +5725,10 @@ skipvar:
                 else
                     localblock->locals.pop_back();
                 /* push it to the to-be-generated globals */
-                vec_push(parser->globals, (ast_expression*)var);
+                parser->globals.push_back((ast_expression*)var);
                 if (isvector)
                     for (i = 0; i < 3; ++i)
-                        vec_push(parser->globals, (ast_expression*)last_me[i]);
+                        parser->globals.push_back((ast_expression*)last_me[i]);
                 folded_const = true;
             }
 
@@ -6095,24 +6094,14 @@ static void parser_remove_ast(parser_t *parser)
     if (parser->ast_cleaned)
         return;
     parser->ast_cleaned = true;
-    for (i = 0; i < vec_size(parser->accessors); ++i) {
-        ast_delete(parser->accessors[i]->constval.vfunc);
-        parser->accessors[i]->constval.vfunc = NULL;
-        ast_delete(parser->accessors[i]);
-    }
-    for (i = 0; i < vec_size(parser->functions); ++i) {
-        ast_delete(parser->functions[i]);
-    }
-    for (i = 0; i < vec_size(parser->fields); ++i) {
-        ast_delete(parser->fields[i]);
-    }
-    for (i = 0; i < vec_size(parser->globals); ++i) {
-        ast_delete(parser->globals[i]);
+    for (auto &it : parser->accessors) {
+        ast_delete(it->constval.vfunc);
+        it->constval.vfunc = nullptr;
+        ast_delete(it);
     }
-    vec_free(parser->accessors);
-    vec_free(parser->functions);
-    vec_free(parser->globals);
-    vec_free(parser->fields);
+    for (auto &it : parser->functions) ast_delete(it);
+    for (auto &it : parser->globals) ast_delete(it);
+    for (auto &it : parser->fields) ast_delete(it);
 
     for (i = 0; i < vec_size(parser->variables); ++i)
         util_htdel(parser->variables[i]);
@@ -6151,7 +6140,6 @@ void parser_cleanup(parser_t *parser)
 }
 
 static bool parser_set_coverage_func(parser_t *parser, ir_builder *ir) {
-    size_t          i;
     ast_expression *expr;
     ast_value      *cov;
     ast_function   *func;
@@ -6159,10 +6147,10 @@ static bool parser_set_coverage_func(parser_t *parser, ir_builder *ir) {
     if (!OPTS_OPTION_BOOL(OPTION_COVERAGE))
         return true;
 
-    func = NULL;
-    for (i = 0; i != vec_size(parser->functions); ++i) {
-        if (!strcmp(parser->functions[i]->name, "coverage")) {
-            func = parser->functions[i];
+    func = nullptr;
+    for (auto &it : parser->functions) {
+        if (!strcmp(it->name, "coverage")) {
+            func = it;
             break;
         }
     }
@@ -6192,9 +6180,8 @@ static bool parser_set_coverage_func(parser_t *parser, ir_builder *ir) {
 
 bool parser_finish(parser_t *parser, const char *output)
 {
-    size_t          i;
-    ir_builder     *ir;
-    bool            retval = true;
+    ir_builder *ir;
+    bool retval = true;
 
     if (compile_errors) {
         con_out("*** there were compile errors\n");
@@ -6207,12 +6194,11 @@ bool parser_finish(parser_t *parser, const char *output)
         return false;
     }
 
-    for (i = 0; i < vec_size(parser->fields); ++i) {
-        ast_value *field;
+    for (auto &it : parser->fields) {
         bool hasvalue;
-        if (!ast_istype(parser->fields[i], ast_value))
+        if (!ast_istype(it, ast_value))
             continue;
-        field = (ast_value*)parser->fields[i];
+        ast_value *field = (ast_value*)it;
         hasvalue = field->hasvalue;
         field->hasvalue = false;
         if (!ast_global_codegen((ast_value*)field, ir, true)) {
@@ -6233,11 +6219,11 @@ bool parser_finish(parser_t *parser, const char *output)
             (void)!ir_value_set_field(field->ir_v, ifld);
         }
     }
-    for (i = 0; i < vec_size(parser->globals); ++i) {
+    for (auto &it : parser->globals) {
         ast_value *asvalue;
-        if (!ast_istype(parser->globals[i], ast_value))
+        if (!ast_istype(it, ast_value))
             continue;
-        asvalue = (ast_value*)(parser->globals[i]);
+        asvalue = (ast_value*)it;
         if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
             retval = retval && !compile_warning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
                                                 "unused global: `%s`", asvalue->name);
@@ -6251,8 +6237,7 @@ bool parser_finish(parser_t *parser, const char *output)
     /* Build function vararg accessor ast tree now before generating
      * immediates, because the accessors may add new immediates
      */
-    for (i = 0; i < vec_size(parser->functions); ++i) {
-        ast_function *f = parser->functions[i];
+    for (auto &f : parser->functions) {
         if (f->varargs) {
             if (parser->max_param_count > f->vtype->expression.params.size()) {
                 f->varargs->expression.count = parser->max_param_count - f->vtype->expression.params.size();
@@ -6279,12 +6264,10 @@ bool parser_finish(parser_t *parser, const char *output)
     /* before generating any functions we need to set the coverage_func */
     if (!parser_set_coverage_func(parser, ir))
         return false;
-
-    for (i = 0; i < vec_size(parser->globals); ++i) {
-        ast_value *asvalue;
-        if (!ast_istype(parser->globals[i], ast_value))
+    for (auto &it : parser->globals) {
+        if (!ast_istype(it, ast_value))
             continue;
-        asvalue = (ast_value*)(parser->globals[i]);
+        ast_value *asvalue = (ast_value*)it;
         if (!(asvalue->expression.flags & AST_FLAG_INITIALIZED))
         {
             if (asvalue->cvq == CV_CONST && !asvalue->hasvalue)
@@ -6301,10 +6284,8 @@ bool parser_finish(parser_t *parser, const char *output)
             return false;
         }
     }
-    for (i = 0; i < vec_size(parser->fields); ++i) {
-        ast_value *asvalue;
-        asvalue = (ast_value*)(parser->fields[i]->next);
-
+    for (auto &it : parser->fields) {
+        ast_value *asvalue = (ast_value*)it->next;
         if (!ast_istype((ast_expression*)asvalue, ast_value))
             continue;
         if (asvalue->expression.vtype != TYPE_ARRAY)
@@ -6321,8 +6302,7 @@ bool parser_finish(parser_t *parser, const char *output)
         ir_builder_delete(ir);
         return false;
     }
-    for (i = 0; i < vec_size(parser->functions); ++i) {
-        ast_function *f = parser->functions[i];
+    for (auto &f : parser->functions) {
         if (!ast_function_codegen(f, ir)) {
             con_out("failed to generate function %s\n", f->name);
             ir_builder_delete(ir);
@@ -6334,9 +6314,9 @@ bool parser_finish(parser_t *parser, const char *output)
 
     if (OPTS_OPTION_BOOL(OPTION_DUMP))
         ir_builder_dump(ir, con_out);
-    for (i = 0; i < vec_size(parser->functions); ++i) {
-        if (!ir_function_finalize(parser->functions[i]->ir_func)) {
-            con_out("failed to finalize function %s\n", parser->functions[i]->name);
+    for (auto &it : parser->functions) {
+        if (!ir_function_finalize(it->ir_func)) {
+            con_out("failed to finalize function %s\n", it->name);
             ir_builder_delete(ir);
             return false;
         }