+#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++] = ',';
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)
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)
char texp[1024];
char tgot[1024];
size_t i;
- bool retval = true;
- const ast_expression *func = self->func;
+ 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);
+ if (count > func->params.size())
+ count = func->params.size();
for (i = 0; i < count; ++i) {
if (ast_istype(self->params[i], ast_argpipe)) {
}
}
count = vec_size(self->params);
- if (count > vec_size(func->params) && func->varparam) {
+ 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);
}
/* 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;
}
}
*/
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;
}
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;
}