+#include <new>
+
#include <stdlib.h>
#include <string.h>
if (!self) { \
return NULL; \
} \
+ new (self) T(); \
ast_node_init((ast_node*)self, ctx, TYPE_##T); \
( (ast_node*)self )->destroy = (ast_node_delete*)destroyfn
self->next = NULL;
self->outl = NULL;
self->outr = NULL;
- self->params = NULL;
self->count = 0;
self->varparam = NULL;
self->flags = 0;
static void ast_expression_delete(ast_expression *self)
{
- size_t i;
if (self->next)
ast_delete(self->next);
- for (i = 0; i < vec_size(self->params); ++i) {
- ast_delete(self->params[i]);
- }
- vec_free(self->params);
+ for (auto &it : self->params)
+ ast_delete(it);
if (self->varparam)
ast_delete(self->varparam);
}
ast_value* ast_value_copy(const ast_value *self)
{
- size_t i;
const ast_expression *fromex;
- ast_expression *selfex;
+ ast_expression *selfex;
ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
if (self->expression.next) {
cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
}
- fromex = &self->expression;
+ fromex = &self->expression;
selfex = &cp->expression;
- selfex->count = fromex->count;
- selfex->flags = fromex->flags;
- for (i = 0; i < vec_size(fromex->params); ++i) {
- ast_value *v = ast_value_copy(fromex->params[i]);
- vec_push(selfex->params, v);
+ selfex->count = fromex->count;
+ selfex->flags = fromex->flags;
+ for (auto &it : fromex->params) {
+ ast_value *v = ast_value_copy(it);
+ selfex->params.push_back(v);
}
return cp;
}
void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
{
- size_t i;
const ast_expression *fromex;
- ast_expression *selfex;
+ ast_expression *selfex;
self->vtype = other->vtype;
if (other->next) {
self->next = (ast_expression*)ast_type_copy(ast_ctx(self), other->next);
}
fromex = other;
selfex = self;
- selfex->count = fromex->count;
- selfex->flags = fromex->flags;
- for (i = 0; i < vec_size(fromex->params); ++i) {
- ast_value *v = ast_value_copy(fromex->params[i]);
- vec_push(selfex->params, v);
+ selfex->count = fromex->count;
+ selfex->flags = fromex->flags;
+ for (auto &it : fromex->params) {
+ ast_value *v = ast_value_copy(it);
+ selfex->params.push_back(v);
}
}
ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
{
- size_t i;
const ast_expression *fromex;
ast_expression *selfex;
else
selfex->next = NULL;
- selfex->count = fromex->count;
- selfex->flags = fromex->flags;
- for (i = 0; i < vec_size(fromex->params); ++i) {
- ast_value *v = ast_value_copy(fromex->params[i]);
- vec_push(selfex->params, v);
+ selfex->count = fromex->count;
+ selfex->flags = fromex->flags;
+ for (auto &it : fromex->params) {
+ ast_value *v = ast_value_copy(it);
+ selfex->params.push_back(v);
}
return self;
return false;
if (!a->next != !b->next)
return false;
- if (vec_size(a->params) != vec_size(b->params))
+ if (a->params.size() != b->params.size())
return false;
if ((a->flags & AST_FLAG_TYPE_MASK) !=
(b->flags & AST_FLAG_TYPE_MASK) )
{
return false;
}
- if (vec_size(a->params)) {
+ if (a->params.size()) {
size_t i;
- for (i = 0; i < vec_size(a->params); ++i) {
+ for (i = 0; i < a->params.size(); ++i) {
if (!ast_compare_type((ast_expression*)a->params[i],
(ast_expression*)b->params[i]))
return false;
pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
if (pos + 2 >= bufsize)
goto full;
- if (!vec_size(e->params)) {
+ if (e->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 < vec_size(e->params); ++i) {
+ for (i = 1; i < e->params.size(); ++i) {
if (pos + 2 >= bufsize)
goto full;
buf[pos++] = ',';
self->inexact = false;
self->uses = 0;
memset(&self->constval, 0, sizeof(self->constval));
- self->initlist = NULL;
self->ir_v = NULL;
self->ir_values = NULL;
if (self->desc)
mem_d(self->desc);
- if (self->initlist) {
- if (self->expression.next->vtype == TYPE_STRING) {
- /* strings are allocated, free them */
- size_t i, len = vec_size(self->initlist);
- /* in theory, len should be expression.count
- * but let's not take any chances */
- for (i = 0; i < len; ++i) {
- if (self->initlist[i].vstring)
- mem_d(self->initlist[i].vstring);
- }
- }
- vec_free(self->initlist);
+ // initlist imples an array which implies .next in the expression exists.
+ if (self->initlist.size() && self->expression.next->vtype == TYPE_STRING) {
+ for (auto &it : self->initlist)
+ if (it.vstring)
+ mem_d(it.vstring);
}
ast_expression_delete((ast_expression*)self);
void ast_value_params_add(ast_value *self, ast_value *p)
{
- vec_push(self->expression.params, p);
+ self->expression.params.push_back(p);
}
bool ast_value_set_name(ast_value *self, const char *name)
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
self->operand = op;
- self->cases = NULL;
ast_propagate_effects(self, op);
void ast_switch_delete(ast_switch *self)
{
- size_t i;
ast_unref(self->operand);
- for (i = 0; i < vec_size(self->cases); ++i) {
- if (self->cases[i].value)
- ast_unref(self->cases[i].value);
- ast_unref(self->cases[i].code);
+ for (auto &it : self->cases) {
+ if (it.value)
+ ast_unref(it.value);
+ ast_unref(it.code);
}
- vec_free(self->cases);
ast_expression_delete((ast_expression*)self);
mem_d(self);
self->name = util_strdup(name);
self->irblock = NULL;
- self->gotos = NULL;
self->undefined = undefined;
return self;
void ast_label_delete(ast_label *self)
{
mem_d((void*)self->name);
- vec_free(self->gotos);
ast_expression_delete((ast_expression*)self);
mem_d(self);
}
static void ast_label_register_goto(ast_label *self, ast_goto *g)
{
- vec_push(self->gotos, g);
+ self->gotos.push_back(g);
}
ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name)
ast_side_effects(self) = true;
- self->params = NULL;
self->func = funcexpr;
self->va_count = NULL;
void ast_call_delete(ast_call *self)
{
- size_t i;
- for (i = 0; i < vec_size(self->params); ++i)
- ast_unref(self->params[i]);
- vec_free(self->params);
+ for (auto &it : self->params)
+ ast_unref(it);
if (self->func)
ast_unref(self->func);
char texp[1024];
char tgot[1024];
size_t i;
- bool retval = true;
- const ast_expression *func = self->func;
- size_t count = vec_size(self->params);
- if (count > vec_size(func->params))
- count = vec_size(func->params);
+ bool retval = true;
+ const ast_expression *func = self->func;
+ size_t count = self->params.size();
+ if (count > func->params.size())
+ count = func->params.size();
for (i = 0; i < count; ++i) {
if (ast_istype(self->params[i], ast_argpipe)) {
retval = false;
}
}
- count = vec_size(self->params);
- if (count > vec_size(func->params) && func->varparam) {
+ count = self->params.size();
+ if (count > func->params.size() && func->varparam) {
for (; i < count; ++i) {
if (ast_istype(self->params[i], ast_argpipe)) {
/* warn about type safety instead */
ast_instantiate(ast_block, ctx, ast_block_delete);
ast_expression_init((ast_expression*)self,
(ast_expression_codegen*)&ast_block_codegen);
-
- self->locals = NULL;
- self->exprs = NULL;
- self->collect = NULL;
-
return self;
}
bool ast_block_add_expr(ast_block *self, ast_expression *e)
{
ast_propagate_effects(self, e);
- vec_push(self->exprs, e);
+ self->exprs.push_back(e);
if (self->expression.next) {
ast_delete(self->expression.next);
self->expression.next = NULL;
void ast_block_collect(ast_block *self, ast_expression *expr)
{
- vec_push(self->collect, expr);
+ self->collect.push_back(expr);
expr->node.keep = true;
}
void ast_block_delete(ast_block *self)
{
- size_t i;
- for (i = 0; i < vec_size(self->exprs); ++i)
- ast_unref(self->exprs[i]);
- vec_free(self->exprs);
- for (i = 0; i < vec_size(self->locals); ++i)
- ast_delete(self->locals[i]);
- vec_free(self->locals);
- for (i = 0; i < vec_size(self->collect); ++i)
- ast_delete(self->collect[i]);
- vec_free(self->collect);
+ for (auto &it : self->exprs) ast_unref(it);
+ for (auto &it : self->locals) ast_delete(it);
+ for (auto &it : self->collect) ast_delete(it);
ast_expression_delete((ast_expression*)self);
mem_d(self);
}
self->vtype = vtype;
self->name = name ? util_strdup(name) : NULL;
- self->blocks = NULL;
self->labelcount = 0;
self->builtin = 0;
for (i = 0; i < vec_size(self->static_names); ++i)
mem_d(self->static_names[i]);
vec_free(self->static_names);
- for (i = 0; i < vec_size(self->blocks); ++i)
- ast_delete(self->blocks[i]);
- vec_free(self->blocks);
+ for (auto &it : self->blocks)
+ ast_delete(it);
vec_free(self->breakblocks);
vec_free(self->continueblocks);
if (self->varargs)
static bool ast_global_array_set(ast_value *self)
{
- size_t count = vec_size(self->initlist);
+ size_t count = self->initlist.size();
size_t i;
if (count > self->expression.count) {
static bool check_array(ast_value *self, ast_value *array)
{
- if (array->expression.flags & AST_FLAG_ARRAY_INIT && !array->initlist) {
+ if (array->expression.flags & AST_FLAG_ARRAY_INIT && array->initlist.empty()) {
compile_error(ast_ctx(self), "array without size: %s", self->name);
return false;
}
ast_expression *ec;
ast_expression_codegen *cgen;
- size_t i;
-
(void)ir;
irf = self->ir_func;
/* fill the parameter list */
ec = &self->vtype->expression;
- for (i = 0; i < vec_size(ec->params); ++i)
- {
- if (ec->params[i]->expression.vtype == TYPE_FIELD)
- vec_push(irf->params, ec->params[i]->expression.next->vtype);
+ for (auto &it : ec->params) {
+ if (it->expression.vtype == TYPE_FIELD)
+ vec_push(irf->params, it->expression.next->vtype);
else
- vec_push(irf->params, ec->params[i]->expression.vtype);
+ vec_push(irf->params, it->expression.vtype);
if (!self->builtin) {
- if (!ast_local_codegen(ec->params[i], self->ir_func, true))
+ if (!ast_local_codegen(it, self->ir_func, true))
return false;
}
}
return false;
}
- if (!vec_size(self->blocks)) {
+ if (self->blocks.empty()) {
compile_error(ast_ctx(self), "function `%s` has no body", self->name);
return false;
}
}
}
- for (i = 0; i < vec_size(self->blocks); ++i) {
- cgen = self->blocks[i]->expression.codegen;
- if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
+ for (auto &it : self->blocks) {
+ cgen = it->expression.codegen;
+ if (!(*cgen)((ast_expression*)it, self, false, &dummy))
return false;
}
*/
bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
{
- size_t i;
-
/* We don't use this
* Note: an ast-representation using the comma-operator
* of the form: (a, b, c) = x should not assign to c...
*out = NULL;
/* generate locals */
- for (i = 0; i < vec_size(self->locals); ++i)
- {
- if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
+ for (auto &it : self->locals) {
+ if (!ast_local_codegen(it, func->ir_func, false)) {
if (OPTS_OPTION_BOOL(OPTION_DEBUG))
- compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
+ compile_error(ast_ctx(self), "failed to generate local `%s`", it->name);
return false;
}
}
- for (i = 0; i < vec_size(self->exprs); ++i)
- {
+ for (auto &it : self->exprs) {
ast_expression_codegen *gen;
- if (func->curblock->final && !starts_a_label(self->exprs[i])) {
- if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
+ if (func->curblock->final && !starts_a_label(it)) {
+ if (compile_warning(ast_ctx(it), WARN_UNREACHABLE_CODE, "unreachable statement"))
return false;
continue;
}
- gen = self->exprs[i]->codegen;
- if (!(*gen)(self->exprs[i], func, false, out))
+ gen = it->codegen;
+ if (!(*gen)(it, func, false, out))
return false;
}
ir_block *bout = NULL;
ir_block *bfall = NULL;
size_t bout_id;
- size_t c;
char typestr[1024];
uint16_t cmpinstr;
if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
return false;
- if (!vec_size(self->cases))
+ if (self->cases.empty())
return true;
cmpinstr = type_eq_instr[irop->vtype];
vec_push(func->breakblocks, bout);
/* Now create all cases */
- for (c = 0; c < vec_size(self->cases); ++c) {
+ for (auto &it : self->cases) {
ir_value *cond, *val;
ir_block *bcase, *bnot;
size_t bnot_id;
- ast_switch_case *swcase = &self->cases[c];
+ ast_switch_case *swcase = ⁢
if (swcase->value) {
/* A regular case */
bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
{
- size_t i;
ir_value *dummy;
if (self->undefined) {
func->curblock = self->irblock;
/* Generate all the leftover gotos */
- for (i = 0; i < vec_size(self->gotos); ++i) {
- if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
+ for (auto &it : self->gotos) {
+ if (!ast_goto_codegen(it, func, false, &dummy))
return false;
}
params = NULL;
/* parameters */
- for (i = 0; i < vec_size(self->params); ++i)
- {
+ for (auto &it : self->params) {
ir_value *param;
- ast_expression *expr = self->params[i];
-
- cgen = expr->codegen;
- if (!(*cgen)(expr, func, false, ¶m))
+ cgen = it->codegen;
+ if (!(*cgen)(it, func, false, ¶m))
goto error;
if (!param)
goto error;