]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.cpp
ast_expression: params -> type_params
[xonotic/gmqcc.git] / ast.cpp
diff --git a/ast.cpp b/ast.cpp
index 366d2a9ecd750069a9aaa0ff9824cbdb69df3af9..d71c4715d7190ddd99915e544a497e358786ca89 100644 (file)
--- a/ast.cpp
+++ b/ast.cpp
@@ -5,7 +5,8 @@
 
 #include "gmqcc.h"
 #include "ast.h"
-#include "parser.h"
+#include "fold.h"
+//#include "parser.h"
 
 #define ast_instantiate(T, ctx, destroyfn)                          \
     T* self = (T*)mem_a(sizeof(T));                                 \
@@ -66,12 +67,12 @@ static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
 }
 
 /* Initialize main ast node aprts */
-static void ast_node_init(ast_node *self, lex_ctx_t ctx, int nodetype)
+static void ast_node_init(ast_node *self, lex_ctx_t ctx, int node_type)
 {
     self->context = ctx;
-    self->destroy = &_ast_node_destroy;
-    self->keep    = false;
-    self->nodetype = nodetype;
+    self->destroy      = &_ast_node_destroy;
+    self->keep_node    = false;
+    self->node_type    = node_type;
     self->side_effects = false;
 }
 
@@ -103,7 +104,7 @@ static void ast_expression_delete(ast_expression *self)
 {
     if (self->next)
         ast_delete(self->next);
-    for (auto &it : self->params)
+    for (auto &it : self->type_params)
         ast_delete(it);
     if (self->varparam)
         ast_delete(self->varparam);
@@ -119,17 +120,17 @@ ast_value* ast_value_copy(const ast_value *self)
 {
     const ast_expression *fromex;
     ast_expression *selfex;
-    ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
+    ast_value *cp = ast_value_new(self->expression.context, self->name, self->expression.vtype);
     if (self->expression.next) {
-        cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
+        cp->expression.next = ast_type_copy(self->expression.context, self->expression.next);
     }
     fromex = &self->expression;
     selfex = &cp->expression;
     selfex->count = fromex->count;
     selfex->flags = fromex->flags;
-    for (auto &it : fromex->params) {
+    for (auto &it : fromex->type_params) {
         ast_value *v = ast_value_copy(it);
-        selfex->params.push_back(v);
+        selfex->type_params.push_back(v);
     }
     return cp;
 }
@@ -146,9 +147,9 @@ void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
     selfex = self;
     selfex->count = fromex->count;
     selfex->flags = fromex->flags;
-    for (auto &it : fromex->params) {
+    for (auto &it : fromex->type_params) {
         ast_value *v = ast_value_copy(it);
-        selfex->params.push_back(v);
+        selfex->type_params.push_back(v);
     }
 }
 
@@ -188,9 +189,9 @@ ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
 
         selfex->count = fromex->count;
         selfex->flags = fromex->flags;
-        for (auto &it : fromex->params) {
+        for (auto &it : fromex->type_params) {
             ast_value *v = ast_value_copy(it);
-            selfex->params.push_back(v);
+            selfex->type_params.push_back(v);
         }
 
         return self;
@@ -206,18 +207,18 @@ bool ast_compare_type(ast_expression *a, ast_expression *b)
         return false;
     if (!a->next != !b->next)
         return false;
-    if (a->params.size() != b->params.size())
+    if (a->type_params.size() != b->type_params.size())
         return false;
     if ((a->flags & AST_FLAG_TYPE_MASK) !=
         (b->flags & AST_FLAG_TYPE_MASK) )
     {
         return false;
     }
-    if (a->params.size()) {
+    if (a->type_params.size()) {
         size_t i;
-        for (i = 0; i < a->params.size(); ++i) {
-            if (!ast_compare_type((ast_expression*)a->params[i],
-                                  (ast_expression*)b->params[i]))
+        for (i = 0; i < a->type_params.size(); ++i) {
+            if (!ast_compare_type((ast_expression*)a->type_params[i],
+                                  (ast_expression*)b->type_params[i]))
                 return false;
         }
     }
@@ -266,19 +267,19 @@ static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsi
             pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
             if (pos + 2 >= bufsize)
                 goto full;
-            if (e->params.empty()) {
+            if (e->type_params.empty()) {
                 buf[pos++] = '(';
                 buf[pos++] = ')';
                 return pos;
             }
             buf[pos++] = '(';
-            pos = ast_type_to_string_impl((ast_expression*)(e->params[0]), buf, bufsize, pos);
-            for (i = 1; i < e->params.size(); ++i) {
+            pos = ast_type_to_string_impl((ast_expression*)(e->type_params[0]), buf, bufsize, pos);
+            for (i = 1; i < e->type_params.size(); ++i) {
                 if (pos + 2 >= bufsize)
                     goto full;
                 buf[pos++] = ',';
                 buf[pos++] = ' ';
-                pos = ast_type_to_string_impl((ast_expression*)(e->params[i]), buf, bufsize, pos);
+                pos = ast_type_to_string_impl((ast_expression*)(e->type_params[i]), buf, bufsize, pos);
             }
             if (pos + 1 >= bufsize)
                 goto full;
@@ -324,7 +325,7 @@ ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
     ast_instantiate(ast_value, ctx, ast_value_delete);
     ast_expression_init((ast_expression*)self,
                         (ast_expression_codegen*)&ast_value_codegen);
-    self->expression.node.keep = true; /* keep */
+    self->expression.keep_node = true; /* keep */
 
     self->name = name ? util_strdup(name) : nullptr;
     self->expression.vtype = t;
@@ -365,7 +366,7 @@ void ast_value_delete(ast_value* self)
             break;
         case TYPE_FUNCTION:
             /* unlink us from the function node */
-            self->constval.vfunc->vtype = nullptr;
+            self->constval.vfunc->function_type = nullptr;
             break;
         /* NOTE: delete function? currently collected in
          * the parser structure
@@ -393,7 +394,7 @@ void ast_value_delete(ast_value* self)
 
 void ast_value_params_add(ast_value *self, ast_value *p)
 {
-    self->expression.params.push_back(p);
+    self->expression.type_params.push_back(p);
 }
 
 bool ast_value_set_name(ast_value *self, const char *name)
@@ -616,7 +617,7 @@ ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int fi
     }
 
     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
-    self->expression.node.keep = true; /* keep */
+    self->expression.keep_node = true; /* keep */
 
     if (owner->vtype == TYPE_VECTOR) {
         self->expression.vtype = TYPE_FLOAT;
@@ -641,7 +642,7 @@ ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int fi
 
 void ast_member_delete(ast_member *self)
 {
-    /* The owner is always an ast_value, which has .keep=true,
+    /* The owner is always an ast_value, which has .keep_node=true,
      * also: ast_members are usually deleted after the owner, thus
      * this will cause invalid access
     ast_unref(self->owner);
@@ -1041,8 +1042,8 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
     bool retval = true;
     const ast_expression *func = self->func;
     size_t count = self->params.size();
-    if (count > func->params.size())
-        count = func->params.size();
+    if (count > func->type_params.size())
+        count = func->type_params.size();
 
     for (i = 0; i < count; ++i) {
         if (ast_istype(self->params[i], ast_argpipe)) {
@@ -1051,13 +1052,13 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
                 compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
                 return false;
             }
-            if (!ast_call_check_vararg(self, va_type, (ast_expression*)func->params[i]))
+            if (!ast_call_check_vararg(self, va_type, (ast_expression*)func->type_params[i]))
                 retval = false;
         }
-        else if (!ast_compare_type(self->params[i], (ast_expression*)(func->params[i])))
+        else if (!ast_compare_type(self->params[i], (ast_expression*)(func->type_params[i])))
         {
             ast_type_to_string(self->params[i], tgot, sizeof(tgot));
-            ast_type_to_string((ast_expression*)func->params[i], texp, sizeof(texp));
+            ast_type_to_string((ast_expression*)func->type_params[i], texp, sizeof(texp));
             compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
                      (unsigned int)(i+1), texp, tgot);
             /* we don't immediately return */
@@ -1065,7 +1066,7 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
         }
     }
     count = self->params.size();
-    if (count > func->params.size() && func->varparam) {
+    if (count > func->type_params.size() && func->varparam) {
         for (; i < count; ++i) {
             if (ast_istype(self->params[i], ast_argpipe)) {
                 /* warn about type safety instead */
@@ -1138,7 +1139,7 @@ bool ast_block_add_expr(ast_block *self, ast_expression *e)
 void ast_block_collect(ast_block *self, ast_expression *expr)
 {
     self->collect.push_back(expr);
-    expr->node.keep = true;
+    expr->keep_node = true;
 }
 
 void ast_block_delete(ast_block *self)
@@ -1172,8 +1173,8 @@ ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype
         goto cleanup;
     }
 
-    self->vtype  = vtype;
-    self->name   = name ? util_strdup(name) : nullptr;
+    self->function_type = vtype;
+    self->name          = name ? util_strdup(name) : nullptr;
 
     self->labelcount = 0;
     self->builtin = 0;
@@ -1201,14 +1202,14 @@ void ast_function_delete(ast_function *self)
 {
     if (self->name)
         mem_d((void*)self->name);
-    if (self->vtype) {
-        /* ast_value_delete(self->vtype); */
-        self->vtype->hasvalue = false;
-        self->vtype->constval.vfunc = nullptr;
+    if (self->function_type) {
+        /* ast_value_delete(self->function_type); */
+        self->function_type->hasvalue = false;
+        self->function_type->constval.vfunc = nullptr;
         /* We use unref - if it was stored in a global table it is supposed
          * to be deleted from *there*
          */
-        ast_unref(self->vtype);
+        ast_unref(self->function_type);
     }
     for (auto &it : self->static_names)
         mem_d(it);
@@ -1780,8 +1781,8 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
     }
 
     /* fill the parameter list */
-    ec = &self->vtype->expression;
-    for (auto &it : ec->params) {
+    ec = &self->function_type->expression;
+    for (auto &it : ec->type_params) {
         if (it->expression.vtype == TYPE_FIELD)
             vec_push(irf->params, it->expression.next->vtype);
         else
@@ -1853,8 +1854,8 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
     /* TODO: check return types */
     if (!self->curblock->final)
     {
-        if (!self->vtype->expression.next ||
-            self->vtype->expression.next->vtype == TYPE_VOID)
+        if (!self->function_type->expression.next ||
+            self->function_type->expression.next->vtype == TYPE_VOID)
         {
             return ir_block_create_return(self->curblock, ast_ctx(self), nullptr);
         }
@@ -2560,7 +2561,7 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
     ir_block *ontrue_endblock = nullptr;
     ir_block *onfalse_endblock = nullptr;
     ir_block *merge = nullptr;
-    int       fold  = 0;
+    int folded = 0;
 
     /* We don't output any value, thus also don't care about r/lvalue */
     (void)out;
@@ -2580,8 +2581,8 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
     cond = func->curblock;
 
     /* try constant folding away the condition */
-    if ((fold = fold_cond_ifthen(condval, func, self)) != -1)
-        return fold;
+    if ((folded = fold::cond_ifthen(condval, func, self)) != -1)
+        return folded;
 
     if (self->on_true) {
         /* create on-true block */
@@ -2663,7 +2664,7 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
     ir_block *ontrue, *ontrue_out = nullptr;
     ir_block *onfalse, *onfalse_out = nullptr;
     ir_block *merge;
-    int       fold  = 0;
+    int folded = 0;
 
     /* Ternary can never create an lvalue... */
     if (lvalue)
@@ -2689,8 +2690,8 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
     cond_out = func->curblock;
 
     /* try constant folding away the condition */
-    if ((fold = fold_cond_ternary(condval, func, self)) != -1)
-        return fold;
+    if ((folded = fold::cond_ternary(condval, func, self)) != -1)
+        return folded;
 
     /* create on-true block */
     ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));