static void parser_enterblock(parser_t *parser);
static bool parser_leaveblock(parser_t *parser);
static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
+static void parser_addlocal(parser_t *parser, const std::string &name, ast_expression *e);
static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e);
+static void parser_addglobal(parser_t *parser, const std::string &name, ast_expression *e);
static bool parse_typedef(parser_t *parser);
static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef, bool noref, bool is_static, uint32_t qflags, char *vstring);
static ast_block* parse_block(parser_t *parser);
static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef, bool *is_vararg);
-static void parseerror(parser_t *parser, const char *fmt, ...)
+static void parseerror_(parser_t *parser, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
va_end(ap);
}
-/* returns true if it counts as an error */
-static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
+template<typename... Ts>
+static inline void parseerror(parser_t *parser, const char *fmt, const Ts&... ts) {
+ return parseerror_(parser, fmt, formatNormalize(ts)...);
+}
+
+// returns true if it counts as an error
+static bool GMQCC_WARN parsewarning_(parser_t *parser, int warntype, const char *fmt, ...)
{
bool r;
va_list ap;
return r;
}
+template<typename... Ts>
+static inline bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, const Ts&... ts) {
+ return parsewarning_(parser, warntype, fmt, formatNormalize(ts)...);
+}
+
/**********************************************************************
* parsing
*/
return util_strdup(str);
}
-static ast_expression* parser_find_field(parser_t *parser, const char *name)
-{
- return ( ast_expression*)util_htget(parser->htfields, name);
+static ast_expression* parser_find_field(parser_t *parser, const char *name) {
+ return (ast_expression*)util_htget(parser->htfields, name);
+}
+static ast_expression* parser_find_field(parser_t *parser, const std::string &name) {
+ return parser_find_field(parser, name.c_str());
}
static ast_expression* parser_find_label(parser_t *parser, const char *name)
{
for (auto &it : parser->labels)
- if (!strcmp(it->name, name))
- return (ast_expression*)it;
+ if (it->m_name == name)
+ return it;
return nullptr;
}
+static inline ast_expression* parser_find_label(parser_t *parser, const std::string &name) {
+ return parser_find_label(parser, name.c_str());
+}
ast_expression* parser_find_global(parser_t *parser, const char *name)
{
return (ast_expression*)util_htget(parser->htglobals, name);
}
+ast_expression* parser_find_global(parser_t *parser, const std::string &name) {
+ return parser_find_global(parser, name.c_str());
+}
+
static ast_expression* parser_find_param(parser_t *parser, const char *name)
{
ast_value *fun;
if (!parser->function)
return nullptr;
- fun = parser->function->vtype;
- for (auto &it : fun->expression.params) {
- if (!strcmp(it->name, name))
- return (ast_expression*)it;
+ fun = parser->function->m_function_type;
+ for (auto &it : fun->m_type_params) {
+ if (it->m_name == name)
+ return it.get();
}
return nullptr;
}
hash = util_hthash(parser->htglobals, name);
*isparam = false;
- for (i = vec_size(parser->variables); i > upto;) {
+ for (i = parser->variables.size(); i > upto;) {
--i;
if ( (e = (ast_expression*)util_htgeth(parser->variables[i], name, hash)) )
return e;
return parser_find_param(parser, name);
}
+static ast_expression* parser_find_local(parser_t *parser, const std::string &name, size_t upto, bool *isparam) {
+ return parser_find_local(parser, name.c_str(), upto, isparam);
+}
+
static ast_expression* parser_find_var(parser_t *parser, const char *name)
{
bool dummy;
ast_expression *v;
- v = parser_find_local(parser, name, 0, &dummy);
+ v = parser_find_local(parser, name, PARSER_HT_LOCALS, &dummy);
if (!v) v = parser_find_global(parser, name);
return v;
}
+static inline ast_expression* parser_find_var(parser_t *parser, const std::string &name) {
+ return parser_find_var(parser, name.c_str());
+}
+
static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
{
size_t i, hash;
ast_value *e;
hash = util_hthash(parser->typedefs[0], name);
- for (i = vec_size(parser->typedefs); i > upto;) {
+ for (i = parser->typedefs.size(); i > upto;) {
--i;
if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
return e;
return nullptr;
}
+static ast_value* parser_find_typedef(parser_t *parser, const std::string &name, size_t upto) {
+ return parser_find_typedef(parser, name.c_str(), upto);
+}
+
struct sy_elem {
size_t etype; /* 0 = expression, others are operators */
bool isparen;
sy_elem e;
e.etype = 0;
e.off = 0;
- e.out = (ast_expression*)v;
+ e.out = v;
e.block = v;
e.ctx = ctx;
e.isparen = false;
ast_expression *sub;
ast_expression *entity;
- lex_ctx_t ctx = ast_ctx(*out);
+ lex_ctx_t ctx = (*out)->m_context;
if (!ast_istype(*out, ast_array_index))
return false;
index = (ast_array_index*)*out;
- if (!ast_istype(index->array, ast_entfield))
+ if (!ast_istype(index->m_array, ast_entfield))
return false;
- entfield = (ast_entfield*)index->array;
+ entfield = (ast_entfield*)index->m_array;
- if (!ast_istype(entfield->field, ast_value))
+ if (!ast_istype(entfield->m_field, ast_value))
return false;
- field = (ast_value*)entfield->field;
+ field = (ast_value*)entfield->m_field;
- sub = index->index;
- entity = entfield->entity;
+ sub = index->m_index;
+ entity = entfield->m_entity;
oldindex = index;
- index = ast_array_index_new(ctx, (ast_expression*)field, sub);
- entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
- *out = (ast_expression*)entfield;
+ index = ast_array_index::make(ctx, field, sub);
+ entfield = new ast_entfield(ctx, entity, index);
+ *out = entfield;
- oldindex->array = nullptr;
- oldindex->index = nullptr;
- ast_delete(oldindex);
+ oldindex->m_array = nullptr;
+ oldindex->m_index = nullptr;
+ delete oldindex;
return true;
}
+static int store_op_for(ast_expression* expr)
+{
+ if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) && expr->m_vtype == TYPE_FIELD && expr->m_next->m_vtype == TYPE_VECTOR) {
+ if (ast_istype(expr, ast_entfield)) {
+ return type_storep_instr[TYPE_VECTOR];
+ } else {
+ return type_store_instr[TYPE_VECTOR];
+ }
+ }
+
+ if (ast_istype(expr, ast_member) && ast_istype(((ast_member*)expr)->m_owner, ast_entfield)) {
+ return type_storep_instr[expr->m_vtype];
+ }
+
+ if (ast_istype(expr, ast_entfield)) {
+ return type_storep_instr[expr->m_vtype];
+ }
+
+ return type_store_instr[expr->m_vtype];
+}
+
static bool check_write_to(lex_ctx_t ctx, ast_expression *expr)
{
if (ast_istype(expr, ast_value)) {
ast_value *val = (ast_value*)expr;
- if (val->cvq == CV_CONST) {
- if (val->name[0] == '#') {
+ if (val->m_cvq == CV_CONST) {
+ if (val->m_name[0] == '#') {
compile_error(ctx, "invalid assignment to a literal constant");
return false;
}
* a warning instead.
*/
if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC)
- compile_error(ctx, "assignment to constant `%s`", val->name);
+ compile_error(ctx, "assignment to constant `%s`", val->m_name);
else
- (void)!compile_warning(ctx, WARN_CONST_OVERWRITE, "assignment to constant `%s`", val->name);
+ (void)!compile_warning(ctx, WARN_CONST_OVERWRITE, "assignment to constant `%s`", val->m_name);
return false;
}
}
const oper_info *op;
lex_ctx_t ctx;
ast_expression *out = nullptr;
- ast_expression *exprs[3];
+ ast_expression *exprs[3] = { 0, 0, 0 };
ast_block *blocks[3];
ast_binstore *asbinstore;
size_t i, assignop, addop, subop;
exprs[i] = sy->out[sy->out.size()+i].out;
blocks[i] = sy->out[sy->out.size()+i].block;
- if (exprs[i]->vtype == TYPE_NOEXPR &&
+ if (exprs[i]->m_vtype == TYPE_NOEXPR &&
!(i != 0 && op->id == opid2('?',':')) &&
!(i == 1 && op->id == opid1('.')))
{
if (ast_istype(exprs[i], ast_label))
- compile_error(ast_ctx(exprs[i]), "expected expression, got an unknown identifier");
+ compile_error(exprs[i]->m_context, "expected expression, got an unknown identifier");
else
- compile_error(ast_ctx(exprs[i]), "not an expression");
- (void)!compile_warning(ast_ctx(exprs[i]), WARN_DEBUG, "expression %u\n", (unsigned int)i);
+ compile_error(exprs[i]->m_context, "not an expression");
+ (void)!compile_warning(exprs[i]->m_context, WARN_DEBUG, "expression %u\n", (unsigned int)i);
}
}
- if (blocks[0] && blocks[0]->exprs.empty() && op->id != opid1(',')) {
+ if (blocks[0] && blocks[0]->m_exprs.empty() && op->id != opid1(',')) {
compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
return false;
}
#define NotSameType(T) \
- (exprs[0]->vtype != exprs[1]->vtype || \
- exprs[0]->vtype != T)
+ (exprs[0]->m_vtype != exprs[1]->m_vtype || \
+ exprs[0]->m_vtype != T)
switch (op->id)
{
return false;
case opid1('.'):
- if (exprs[0]->vtype == TYPE_VECTOR &&
- exprs[1]->vtype == TYPE_NOEXPR)
+ if (exprs[0]->m_vtype == TYPE_VECTOR &&
+ exprs[1]->m_vtype == TYPE_NOEXPR)
{
- if (exprs[1] == (ast_expression*)parser->const_vec[0])
- out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, nullptr);
- else if (exprs[1] == (ast_expression*)parser->const_vec[1])
- out = (ast_expression*)ast_member_new(ctx, exprs[0], 1, nullptr);
- else if (exprs[1] == (ast_expression*)parser->const_vec[2])
- out = (ast_expression*)ast_member_new(ctx, exprs[0], 2, nullptr);
+ if (exprs[1] == parser->const_vec[0])
+ out = ast_member::make(ctx, exprs[0], 0, "");
+ else if (exprs[1] == parser->const_vec[1])
+ out = ast_member::make(ctx, exprs[0], 1, "");
+ else if (exprs[1] == parser->const_vec[2])
+ out = ast_member::make(ctx, exprs[0], 2, "");
else {
compile_error(ctx, "access to invalid vector component");
return false;
}
}
- else if (exprs[0]->vtype == TYPE_ENTITY) {
- if (exprs[1]->vtype != TYPE_FIELD) {
- compile_error(ast_ctx(exprs[1]), "type error: right hand of member-operand should be an entity-field");
+ else if (exprs[0]->m_vtype == TYPE_ENTITY) {
+ if (exprs[1]->m_vtype != TYPE_FIELD) {
+ compile_error(exprs[1]->m_context, "type error: right hand of member-operand should be an entity-field");
return false;
}
- out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
+ out = new ast_entfield(ctx, exprs[0], exprs[1]);
}
- else if (exprs[0]->vtype == TYPE_VECTOR) {
- compile_error(ast_ctx(exprs[1]), "vectors cannot be accessed this way");
+ else if (exprs[0]->m_vtype == TYPE_VECTOR) {
+ compile_error(exprs[1]->m_context, "vectors cannot be accessed this way");
return false;
}
else {
- compile_error(ast_ctx(exprs[1]), "type error: member-of operator on something that is not an entity or vector");
+ compile_error(exprs[1]->m_context, "type error: member-of operator on something that is not an entity or vector");
return false;
}
break;
case opid1('['):
- if (exprs[0]->vtype != TYPE_ARRAY &&
- !(exprs[0]->vtype == TYPE_FIELD &&
- exprs[0]->next->vtype == TYPE_ARRAY))
+ if (exprs[0]->m_vtype != TYPE_ARRAY &&
+ !(exprs[0]->m_vtype == TYPE_FIELD &&
+ exprs[0]->m_next->m_vtype == TYPE_ARRAY))
{
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
- compile_error(ast_ctx(exprs[0]), "cannot index value of type %s", ty1);
+ compile_error(exprs[0]->m_context, "cannot index value of type %s", ty1);
return false;
}
- if (exprs[1]->vtype != TYPE_FLOAT) {
+ if (exprs[1]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
- compile_error(ast_ctx(exprs[1]), "index must be of type float, not %s", ty1);
+ compile_error(exprs[1]->m_context, "index must be of type float, not %s", ty1);
return false;
}
- out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
+ out = ast_array_index::make(ctx, exprs[0], exprs[1]);
rotate_entfield_array_index_nodes(&out);
break;
return true;
}
if (blocks[0]) {
- if (!ast_block_add_expr(blocks[0], exprs[1]))
+ if (!blocks[0]->addExpr(exprs[1]))
return false;
} else {
- blocks[0] = ast_block_new(ctx);
- if (!ast_block_add_expr(blocks[0], exprs[0]) ||
- !ast_block_add_expr(blocks[0], exprs[1]))
+ blocks[0] = new ast_block(ctx);
+ if (!blocks[0]->addExpr(exprs[0]) ||
+ !blocks[0]->addExpr(exprs[1]))
{
return false;
}
}
- ast_block_set_type(blocks[0], exprs[1]);
+ blocks[0]->setType(*exprs[1]);
sy->out.push_back(syblock(ctx, blocks[0]));
return true;
if ((out = parser->m_fold.op(op, exprs)))
break;
- if (exprs[0]->vtype != TYPE_FLOAT &&
- exprs[0]->vtype != TYPE_VECTOR) {
+ if (exprs[0]->m_vtype != TYPE_FLOAT &&
+ exprs[0]->m_vtype != TYPE_VECTOR) {
compile_error(ctx, "invalid types used in unary expression: cannot negate type %s",
- type_name[exprs[0]->vtype]);
+ type_name[exprs[0]->m_vtype]);
return false;
}
- if (exprs[0]->vtype == TYPE_FLOAT)
- out = (ast_expression*)ast_unary_new(ctx, VINSTR_NEG_F, exprs[0]);
+ if (exprs[0]->m_vtype == TYPE_FLOAT)
+ out = ast_unary::make(ctx, VINSTR_NEG_F, exprs[0]);
else
- out = (ast_expression*)ast_unary_new(ctx, VINSTR_NEG_V, exprs[0]);
+ out = ast_unary::make(ctx, VINSTR_NEG_V, exprs[0]);
break;
case opid2('!','P'):
if (!(out = parser->m_fold.op(op, exprs))) {
- switch (exprs[0]->vtype) {
+ switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
+ out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
break;
case TYPE_VECTOR:
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
+ out = ast_unary::make(ctx, INSTR_NOT_V, exprs[0]);
break;
case TYPE_STRING:
if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
+ out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
else
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
+ out = ast_unary::make(ctx, INSTR_NOT_S, exprs[0]);
break;
/* we don't constant-fold NOT for these types */
case TYPE_ENTITY:
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
+ out = ast_unary::make(ctx, INSTR_NOT_ENT, exprs[0]);
break;
case TYPE_FUNCTION:
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
+ out = ast_unary::make(ctx, INSTR_NOT_FNC, exprs[0]);
break;
default:
compile_error(ctx, "invalid types used in expression: cannot logically negate type %s",
- type_name[exprs[0]->vtype]);
+ type_name[exprs[0]->m_vtype]);
return false;
}
}
break;
case opid1('+'):
- if (exprs[0]->vtype != exprs[1]->vtype ||
- (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT) )
+ if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
+ (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT) )
{
compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
- switch (exprs[0]->vtype) {
+ switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
out = fold::binary(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
break;
break;
default:
compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
}
break;
case opid1('-'):
- if (exprs[0]->vtype != exprs[1]->vtype ||
- (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT))
+ if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
+ (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT))
{
compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
- type_name[exprs[1]->vtype],
- type_name[exprs[0]->vtype]);
+ type_name[exprs[1]->m_vtype],
+ type_name[exprs[0]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
- switch (exprs[0]->vtype) {
+ switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
out = fold::binary(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
break;
break;
default:
compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
- type_name[exprs[1]->vtype],
- type_name[exprs[0]->vtype]);
+ type_name[exprs[1]->m_vtype],
+ type_name[exprs[0]->m_vtype]);
return false;
}
}
break;
case opid1('*'):
- if (exprs[0]->vtype != exprs[1]->vtype &&
- !(exprs[0]->vtype == TYPE_VECTOR &&
- exprs[1]->vtype == TYPE_FLOAT) &&
- !(exprs[1]->vtype == TYPE_VECTOR &&
- exprs[0]->vtype == TYPE_FLOAT)
+ if (exprs[0]->m_vtype != exprs[1]->m_vtype &&
+ !(exprs[0]->m_vtype == TYPE_VECTOR &&
+ exprs[1]->m_vtype == TYPE_FLOAT) &&
+ !(exprs[1]->m_vtype == TYPE_VECTOR &&
+ exprs[0]->m_vtype == TYPE_FLOAT)
)
{
compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
- type_name[exprs[1]->vtype],
- type_name[exprs[0]->vtype]);
+ type_name[exprs[1]->m_vtype],
+ type_name[exprs[0]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
- switch (exprs[0]->vtype) {
+ switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
- if (exprs[1]->vtype == TYPE_VECTOR)
+ if (exprs[1]->m_vtype == TYPE_VECTOR)
out = fold::binary(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
else
out = fold::binary(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
break;
case TYPE_VECTOR:
- if (exprs[1]->vtype == TYPE_FLOAT)
+ if (exprs[1]->m_vtype == TYPE_FLOAT)
out = fold::binary(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
else
out = fold::binary(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
break;
default:
compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
- type_name[exprs[1]->vtype],
- type_name[exprs[0]->vtype]);
+ type_name[exprs[1]->m_vtype],
+ type_name[exprs[0]->m_vtype]);
return false;
}
}
break;
case opid1('/'):
- if (exprs[1]->vtype != TYPE_FLOAT) {
+ if (exprs[1]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
- if (exprs[0]->vtype == TYPE_FLOAT)
+ if (exprs[0]->m_vtype == TYPE_FLOAT)
out = fold::binary(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
else {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
case opid1('%'):
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform modulo operation between types %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
} else if (!(out = parser->m_fold.op(op, exprs))) {
/* generate a call to __builtin_mod */
ast_call *call = nullptr;
if (!mod) return false; /* can return null for missing floor */
- call = ast_call_new(parser_ctx(parser), mod);
- call->params.push_back(exprs[0]);
- call->params.push_back(exprs[1]);
+ call = ast_call::make(parser_ctx(parser), mod);
+ call->m_params.push_back(exprs[0]);
+ call->m_params.push_back(exprs[1]);
- out = (ast_expression*)call;
+ out = call;
}
break;
case opid1('|'):
case opid1('&'):
case opid1('^'):
- if ( !(exprs[0]->vtype == TYPE_FLOAT && exprs[1]->vtype == TYPE_FLOAT) &&
- !(exprs[0]->vtype == TYPE_VECTOR && exprs[1]->vtype == TYPE_FLOAT) &&
- !(exprs[0]->vtype == TYPE_VECTOR && exprs[1]->vtype == TYPE_VECTOR))
+ if ( !(exprs[0]->m_vtype == TYPE_FLOAT && exprs[1]->m_vtype == TYPE_FLOAT) &&
+ !(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_FLOAT) &&
+ !(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_VECTOR))
{
compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
* IF the first expression is float, the following will be too
* since scalar ^ vector is not allowed.
*/
- if (exprs[0]->vtype == TYPE_FLOAT) {
+ if (exprs[0]->m_vtype == TYPE_FLOAT) {
out = fold::binary(ctx,
(op->id == opid1('^') ? VINSTR_BITXOR : op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
exprs[0], exprs[1]);
* The first is a vector: vector is allowed to bitop with vector and
* with scalar, branch here for the second operand.
*/
- if (exprs[1]->vtype == TYPE_VECTOR) {
+ if (exprs[1]->m_vtype == TYPE_VECTOR) {
/*
* Bitop all the values of the vector components against the
* vectors components in question.
case opid2('>','>'):
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform shift between types %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
ast_expression *shift = parser->m_intrin.func((op->id == opid2('<','<')) ? "__builtin_lshift" : "__builtin_rshift");
- ast_call *call = ast_call_new(parser_ctx(parser), shift);
- call->params.push_back(exprs[0]);
- call->params.push_back(exprs[1]);
- out = (ast_expression*)call;
+ ast_call *call = ast_call::make(parser_ctx(parser), shift);
+ call->m_params.push_back(exprs[0]);
+ call->m_params.push_back(exprs[1]);
+ out = call;
}
break;
case opid3('>','>','='):
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform shift operation between types %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
if(!(out = parser->m_fold.op(op, exprs))) {
ast_expression *shift = parser->m_intrin.func((op->id == opid3('<','<','=')) ? "__builtin_lshift" : "__builtin_rshift");
- ast_call *call = ast_call_new(parser_ctx(parser), shift);
- call->params.push_back(exprs[0]);
- call->params.push_back(exprs[1]);
- out = (ast_expression*)ast_store_new(
+ ast_call *call = ast_call::make(parser_ctx(parser), shift);
+ call->m_params.push_back(exprs[0]);
+ call->m_params.push_back(exprs[1]);
+ out = new ast_store(
parser_ctx(parser),
INSTR_STORE_F,
exprs[0],
- (ast_expression*)call
+ call
);
}
case opid2('|','|'):
generated_op += 1; /* INSTR_OR */
+ [[fallthrough]];
case opid2('&','&'):
generated_op += INSTR_AND;
if (!(out = parser->m_fold.op(op, exprs))) {
- if (OPTS_FLAG(PERL_LOGIC) && !ast_compare_type(exprs[0], exprs[1])) {
+ if (OPTS_FLAG(PERL_LOGIC) && !exprs[0]->compareType(*exprs[1])) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
return false;
}
for (i = 0; i < 2; ++i) {
- if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->vtype == TYPE_VECTOR) {
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[i]);
+ if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->m_vtype == TYPE_VECTOR) {
+ out = ast_unary::make(ctx, INSTR_NOT_V, exprs[i]);
if (!out) break;
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
+ out = ast_unary::make(ctx, INSTR_NOT_F, out);
if (!out) break;
exprs[i] = out; out = nullptr;
if (OPTS_FLAG(PERL_LOGIC)) {
break;
}
}
- else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->vtype == TYPE_STRING) {
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[i]);
+ else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->m_vtype == TYPE_STRING) {
+ out = ast_unary::make(ctx, INSTR_NOT_S, exprs[i]);
if (!out) break;
- out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
+ out = ast_unary::make(ctx, INSTR_NOT_F, out);
if (!out) break;
exprs[i] = out; out = nullptr;
if (OPTS_FLAG(PERL_LOGIC)) {
return false;
}
sy->paren.pop_back();
- if (!ast_compare_type(exprs[1], exprs[2])) {
+ if (!exprs[1]->compareType(*exprs[2])) {
ast_type_to_string(exprs[1], ty1, sizeof(ty1));
ast_type_to_string(exprs[2], ty2, sizeof(ty2));
compile_error(ctx, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
- out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
+ out = new ast_ternary(ctx, exprs[0], exprs[1], exprs[2]);
break;
case opid2('*', '*'):
}
if (!(out = parser->m_fold.op(op, exprs))) {
- ast_call *gencall = ast_call_new(parser_ctx(parser), parser->m_intrin.func("pow"));
- gencall->params.push_back(exprs[0]);
- gencall->params.push_back(exprs[1]);
- out = (ast_expression*)gencall;
+ ast_call *gencall = ast_call::make(parser_ctx(parser), parser->m_intrin.func("pow"));
+ gencall->m_params.push_back(exprs[0]);
+ gencall->m_params.push_back(exprs[1]);
+ out = gencall;
}
break;
if (!(out = parser->m_fold.op(op, exprs))) {
/* This whole block is NOT fold_binary safe */
- ast_binary *eq = ast_binary_new(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
+ ast_binary *eq = new ast_binary(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
- eq->refs = AST_REF_NONE;
+ eq->m_refs = AST_REF_NONE;
/* if (lt) { */
- out = (ast_expression*)ast_ternary_new(ctx,
- (ast_expression*)ast_binary_new(ctx, INSTR_LT, exprs[0], exprs[1]),
+ out = new ast_ternary(ctx,
+ new ast_binary(ctx, INSTR_LT, exprs[0], exprs[1]),
/* out = -1 */
- (ast_expression*)parser->m_fold.imm_float(2),
+ parser->m_fold.imm_float(2),
/* } else { */
/* if (eq) { */
- (ast_expression*)ast_ternary_new(ctx, (ast_expression*)eq,
+ new ast_ternary(ctx, eq,
/* out = 0 */
- (ast_expression*)parser->m_fold.imm_float(0),
+ parser->m_fold.imm_float(0),
/* } else { */
/* out = 1 */
- (ast_expression*)parser->m_fold.imm_float(1)
+ parser->m_fold.imm_float(1)
/* } */
)
/* } */
case opid1('>'):
generated_op += 1; /* INSTR_GT */
+ [[fallthrough]];
case opid1('<'):
generated_op += 1; /* INSTR_LT */
+ [[fallthrough]];
case opid2('>', '='):
generated_op += 1; /* INSTR_GE */
+ [[fallthrough]];
case opid2('<', '='):
generated_op += INSTR_LE;
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
out = fold::binary(ctx, generated_op, exprs[0], exprs[1]);
break;
case opid2('!', '='):
- if (exprs[0]->vtype != exprs[1]->vtype) {
+ if (exprs[0]->m_vtype != exprs[1]->m_vtype) {
compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
- out = fold::binary(ctx, type_ne_instr[exprs[0]->vtype], exprs[0], exprs[1]);
+ out = fold::binary(ctx, type_ne_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
break;
case opid2('=', '='):
- if (exprs[0]->vtype != exprs[1]->vtype) {
+ if (exprs[0]->m_vtype != exprs[1]->m_vtype) {
compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
- out = fold::binary(ctx, type_eq_instr[exprs[0]->vtype], exprs[0], exprs[1]);
+ out = fold::binary(ctx, type_eq_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
break;
case opid1('='):
if (ast_istype(exprs[0], ast_entfield)) {
- ast_expression *field = ((ast_entfield*)exprs[0])->field;
- if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
- exprs[0]->vtype == TYPE_FIELD &&
- exprs[0]->next->vtype == TYPE_VECTOR)
+ ast_expression *field = ((ast_entfield*)exprs[0])->m_field;
+ assignop = store_op_for(exprs[0]);
+ if (assignop == VINSTR_END || !field->m_next->compareType(*exprs[1]))
{
- assignop = type_storep_instr[TYPE_VECTOR];
- }
- else
- assignop = type_storep_instr[exprs[0]->vtype];
- if (assignop == VINSTR_END || !ast_compare_type(field->next, exprs[1]))
- {
- ast_type_to_string(field->next, ty1, sizeof(ty1));
+ ast_type_to_string(field->m_next, ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
- field->next->vtype == TYPE_FUNCTION &&
- exprs[1]->vtype == TYPE_FUNCTION)
+ field->m_next->m_vtype == TYPE_FUNCTION &&
+ exprs[1]->m_vtype == TYPE_FUNCTION)
{
(void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
"invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
else
{
- if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
- exprs[0]->vtype == TYPE_FIELD &&
- exprs[0]->next->vtype == TYPE_VECTOR)
- {
- assignop = type_store_instr[TYPE_VECTOR];
- }
- else {
- assignop = type_store_instr[exprs[0]->vtype];
- }
+ assignop = store_op_for(exprs[0]);
if (assignop == VINSTR_END) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
- else if (!ast_compare_type(exprs[0], exprs[1]))
+ else if (!exprs[0]->compareType(*exprs[1]))
{
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
- exprs[0]->vtype == TYPE_FUNCTION &&
- exprs[1]->vtype == TYPE_FUNCTION)
+ exprs[0]->m_vtype == TYPE_FUNCTION &&
+ exprs[1]->m_vtype == TYPE_FUNCTION)
{
(void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
"invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
(void)check_write_to(ctx, exprs[0]);
/* When we're a vector of part of an entity field we use STOREP */
- if (ast_istype(exprs[0], ast_member) && ast_istype(((ast_member*)exprs[0])->owner, ast_entfield))
+ if (ast_istype(exprs[0], ast_member) && ast_istype(((ast_member*)exprs[0])->m_owner, ast_entfield))
assignop = INSTR_STOREP_F;
- out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
+ out = new ast_store(ctx, assignop, exprs[0], exprs[1]);
break;
case opid3('+','+','P'):
case opid3('-','-','P'):
/* prefix ++ */
- if (exprs[0]->vtype != TYPE_FLOAT) {
+ if (exprs[0]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
- compile_error(ast_ctx(exprs[0]), "invalid type for prefix increment: %s", ty1);
+ compile_error(exprs[0]->m_context, "invalid type for prefix increment: %s", ty1);
return false;
}
if (op->id == opid3('+','+','P'))
addop = INSTR_ADD_F;
else
addop = INSTR_SUB_F;
- (void)check_write_to(ast_ctx(exprs[0]), exprs[0]);
+ (void)check_write_to(exprs[0]->m_context, exprs[0]);
if (ast_istype(exprs[0], ast_entfield)) {
- out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
- exprs[0],
- (ast_expression*)parser->m_fold.imm_float(1));
+ out = new ast_binstore(ctx, INSTR_STOREP_F, addop,
+ exprs[0],
+ parser->m_fold.imm_float(1));
} else {
- out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
- exprs[0],
- (ast_expression*)parser->m_fold.imm_float(1));
+ out = new ast_binstore(ctx, INSTR_STORE_F, addop,
+ exprs[0],
+ parser->m_fold.imm_float(1));
}
break;
case opid3('S','+','+'):
case opid3('S','-','-'):
/* prefix ++ */
- if (exprs[0]->vtype != TYPE_FLOAT) {
+ if (exprs[0]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
- compile_error(ast_ctx(exprs[0]), "invalid type for suffix increment: %s", ty1);
+ compile_error(exprs[0]->m_context, "invalid type for suffix increment: %s", ty1);
return false;
}
if (op->id == opid3('S','+','+')) {
addop = INSTR_SUB_F;
subop = INSTR_ADD_F;
}
- (void)check_write_to(ast_ctx(exprs[0]), exprs[0]);
+ (void)check_write_to(exprs[0]->m_context, exprs[0]);
if (ast_istype(exprs[0], ast_entfield)) {
- out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
- exprs[0],
- (ast_expression*)parser->m_fold.imm_float(1));
+ out = new ast_binstore(ctx, INSTR_STOREP_F, addop,
+ exprs[0],
+ parser->m_fold.imm_float(1));
} else {
- out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
- exprs[0],
- (ast_expression*)parser->m_fold.imm_float(1));
+ out = new ast_binstore(ctx, INSTR_STORE_F, addop,
+ exprs[0],
+ parser->m_fold.imm_float(1));
}
if (!out)
return false;
out = fold::binary(ctx, subop,
out,
- (ast_expression*)parser->m_fold.imm_float(1));
+ parser->m_fold.imm_float(1));
break;
case opid2('+','='):
case opid2('-','='):
- if (exprs[0]->vtype != exprs[1]->vtype ||
- (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT) )
+ if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
+ (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT) )
{
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
return false;
}
(void)check_write_to(ctx, exprs[0]);
- if (ast_istype(exprs[0], ast_entfield))
- assignop = type_storep_instr[exprs[0]->vtype];
- else
- assignop = type_store_instr[exprs[0]->vtype];
- switch (exprs[0]->vtype) {
+ assignop = store_op_for(exprs[0]);
+ switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
- out = (ast_expression*)ast_binstore_new(ctx, assignop,
- (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
- exprs[0], exprs[1]);
+ out = new ast_binstore(ctx, assignop,
+ (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
+ exprs[0], exprs[1]);
break;
case TYPE_VECTOR:
- out = (ast_expression*)ast_binstore_new(ctx, assignop,
- (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
- exprs[0], exprs[1]);
+ out = new ast_binstore(ctx, assignop,
+ (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
+ exprs[0], exprs[1]);
break;
default:
compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
};
break;
case opid2('*','='):
case opid2('/','='):
- if (exprs[1]->vtype != TYPE_FLOAT ||
- !(exprs[0]->vtype == TYPE_FLOAT ||
- exprs[0]->vtype == TYPE_VECTOR))
+ if (exprs[1]->m_vtype != TYPE_FLOAT ||
+ !(exprs[0]->m_vtype == TYPE_FLOAT ||
+ exprs[0]->m_vtype == TYPE_VECTOR))
{
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
return false;
}
(void)check_write_to(ctx, exprs[0]);
- if (ast_istype(exprs[0], ast_entfield))
- assignop = type_storep_instr[exprs[0]->vtype];
- else
- assignop = type_store_instr[exprs[0]->vtype];
- switch (exprs[0]->vtype) {
+ assignop = store_op_for(exprs[0]);
+ switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
- out = (ast_expression*)ast_binstore_new(ctx, assignop,
- (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
- exprs[0], exprs[1]);
+ out = new ast_binstore(ctx, assignop,
+ (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
+ exprs[0], exprs[1]);
break;
case TYPE_VECTOR:
if (op->id == opid2('*','=')) {
- out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
- exprs[0], exprs[1]);
+ out = new ast_binstore(ctx, assignop, INSTR_MUL_VF,
+ exprs[0], exprs[1]);
} else {
out = fold::binary(ctx, INSTR_DIV_F,
- (ast_expression*)parser->m_fold.imm_float(1),
+ parser->m_fold.imm_float(1),
exprs[1]);
if (!out) {
compile_error(ctx, "internal error: failed to generate division");
return false;
}
- out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
- exprs[0], out);
+ out = new ast_binstore(ctx, assignop, INSTR_MUL_VF,
+ exprs[0], out);
}
break;
default:
compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
- type_name[exprs[0]->vtype],
- type_name[exprs[1]->vtype]);
+ type_name[exprs[0]->m_vtype],
+ type_name[exprs[1]->m_vtype]);
return false;
};
break;
return false;
}
(void)check_write_to(ctx, exprs[0]);
- if (ast_istype(exprs[0], ast_entfield))
- assignop = type_storep_instr[exprs[0]->vtype];
+ assignop = store_op_for(exprs[0]);
+ if (exprs[0]->m_vtype == TYPE_FLOAT)
+ out = new ast_binstore(ctx, assignop,
+ (op->id == opid2('^','=') ? VINSTR_BITXOR : op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
+ exprs[0], exprs[1]);
else
- assignop = type_store_instr[exprs[0]->vtype];
- if (exprs[0]->vtype == TYPE_FLOAT)
- out = (ast_expression*)ast_binstore_new(ctx, assignop,
- (op->id == opid2('^','=') ? VINSTR_BITXOR : op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
- exprs[0], exprs[1]);
- else
- out = (ast_expression*)ast_binstore_new(ctx, assignop,
- (op->id == opid2('^','=') ? VINSTR_BITXOR_V : op->id == opid2('&','=') ? VINSTR_BITAND_V : VINSTR_BITOR_V),
- exprs[0], exprs[1]);
+ out = new ast_binstore(ctx, assignop,
+ (op->id == opid2('^','=') ? VINSTR_BITXOR_V : op->id == opid2('&','=') ? VINSTR_BITAND_V : VINSTR_BITOR_V),
+ exprs[0], exprs[1]);
break;
case opid3('&','~','='):
/* This is like: a &= ~(b);
ty1, ty2);
return false;
}
- if (ast_istype(exprs[0], ast_entfield))
- assignop = type_storep_instr[exprs[0]->vtype];
- else
- assignop = type_store_instr[exprs[0]->vtype];
- if (exprs[0]->vtype == TYPE_FLOAT)
+ assignop = store_op_for(exprs[0]);
+ if (exprs[0]->m_vtype == TYPE_FLOAT)
out = fold::binary(ctx, INSTR_BITAND, exprs[0], exprs[1]);
else
out = fold::binary(ctx, VINSTR_BITAND_V, exprs[0], exprs[1]);
if (!out)
return false;
(void)check_write_to(ctx, exprs[0]);
- if (exprs[0]->vtype == TYPE_FLOAT)
- asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
+ if (exprs[0]->m_vtype == TYPE_FLOAT)
+ asbinstore = new ast_binstore(ctx, assignop, INSTR_SUB_F, exprs[0], out);
else
- asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_V, exprs[0], out);
- asbinstore->keep_dest = true;
- out = (ast_expression*)asbinstore;
+ asbinstore = new ast_binstore(ctx, assignop, INSTR_SUB_V, exprs[0], out);
+ asbinstore->m_keep_dest = true;
+ out = asbinstore;
break;
case opid3('l', 'e', 'n'):
- if (exprs[0]->vtype != TYPE_STRING && exprs[0]->vtype != TYPE_ARRAY) {
+ if (exprs[0]->m_vtype != TYPE_STRING && exprs[0]->m_vtype != TYPE_ARRAY) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
- compile_error(ast_ctx(exprs[0]), "invalid type for length operator: %s", ty1);
+ compile_error(exprs[0]->m_context, "invalid type for length operator: %s", ty1);
return false;
}
/* strings must be const, arrays are statically sized */
- if (exprs[0]->vtype == TYPE_STRING &&
- !(((ast_value*)exprs[0])->hasvalue && ((ast_value*)exprs[0])->cvq == CV_CONST))
+ if (exprs[0]->m_vtype == TYPE_STRING &&
+ !(((ast_value*)exprs[0])->m_hasvalue && ((ast_value*)exprs[0])->m_cvq == CV_CONST))
{
- compile_error(ast_ctx(exprs[0]), "operand of length operator not a valid constant expression");
+ compile_error(exprs[0]->m_context, "operand of length operator not a valid constant expression");
return false;
}
out = parser->m_fold.op(op, exprs);
break;
case opid2('~', 'P'):
- if (exprs[0]->vtype != TYPE_FLOAT && exprs[0]->vtype != TYPE_VECTOR) {
+ if (exprs[0]->m_vtype != TYPE_FLOAT && exprs[0]->m_vtype != TYPE_VECTOR) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
- compile_error(ast_ctx(exprs[0]), "invalid type for bit not: %s", ty1);
+ compile_error(exprs[0]->m_context, "invalid type for bit not: %s", ty1);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
- if (exprs[0]->vtype == TYPE_FLOAT) {
- out = fold::binary(ctx, INSTR_SUB_F, (ast_expression*)parser->m_fold.imm_float(2), exprs[0]);
+ if (exprs[0]->m_vtype == TYPE_FLOAT) {
+ out = fold::binary(ctx, INSTR_SUB_F, parser->m_fold.imm_float(2), exprs[0]);
} else {
- out = fold::binary(ctx, INSTR_SUB_V, (ast_expression*)parser->m_fold.imm_vector(1), exprs[0]);
+ out = fold::binary(ctx, INSTR_SUB_V, parser->m_fold.imm_vector(1), exprs[0]);
}
}
break;
}
ast_type_to_string(sy->out.back().out, ty, sizeof(ty));
ast_unref(sy->out.back().out);
- sy->out[fid] = syexp(ast_ctx(sy->out.back().out),
- (ast_expression*)parser->m_fold.constgen_string(ty, false));
+ sy->out[fid] = syexp(sy->out.back().out->m_context,
+ parser->m_fold.constgen_string(ty, false));
sy->out.pop_back();
return true;
}
* and than fruitfully fold them.
*/
#define fold_can_1(X) \
- (ast_istype(((ast_expression*)(X)), ast_value) && (X)->hasvalue && ((X)->cvq == CV_CONST) && \
- ((ast_expression*)(X))->vtype != TYPE_FUNCTION)
+ (ast_istype(((X)), ast_value) && (X)->m_hasvalue && ((X)->m_cvq == CV_CONST) && \
+ ((X))->m_vtype != TYPE_FUNCTION)
if (fid + 1 < sy->out.size())
++paramcount;
* All is well which ends well, if we make it into here we can ignore the
* intrinsic call and just evaluate it i.e constant fold it.
*/
- if (fold && ast_istype(fun, ast_value) && ((ast_value*)fun)->intrinsic) {
- ast_expression **exprs = nullptr;
+ if (fold && ast_istype(fun, ast_value) && ((ast_value*)fun)->m_intrinsic) {
+ std::vector<ast_expression*> exprs;
ast_expression *foldval = nullptr;
+ exprs.reserve(paramcount);
for (i = 0; i < paramcount; i++)
- vec_push(exprs, sy->out[fid+1 + i].out);
+ exprs.push_back(sy->out[fid+1 + i].out);
- if (!(foldval = parser->m_intrin.do_fold((ast_value*)fun, exprs))) {
- vec_free(exprs);
+ if (!(foldval = parser->m_intrin.do_fold((ast_value*)fun, exprs.data()))) {
goto fold_leave;
}
* Blub: what sorts of unreffing and resizing of
* sy->out should I be doing here?
*/
- sy->out[fid] = syexp(foldval->node.context, foldval);
+ sy->out[fid] = syexp(foldval->m_context, foldval);
sy->out.erase(sy->out.end() - paramcount, sy->out.end());
- vec_free(exprs);
return true;
}
fold_leave:
- call = ast_call_new(sy->ops[sy->ops.size()].ctx, fun);
+ call = ast_call::make(sy->ops[sy->ops.size()].ctx, fun);
if (!call)
return false;
}
for (i = 0; i < paramcount; ++i)
- call->params.push_back(sy->out[fid+1 + i].out);
+ call->m_params.push_back(sy->out[fid+1 + i].out);
sy->out.erase(sy->out.end() - paramcount, sy->out.end());
- (void)!ast_call_check_types(call, parser->function->vtype->expression.varparam);
+ (void)!call->checkTypes(parser->function->m_function_type->m_varparam);
if (parser->max_param_count < paramcount)
parser->max_param_count = paramcount;
if (ast_istype(fun, ast_value)) {
funval = (ast_value*)fun;
- if ((fun->flags & AST_FLAG_VARIADIC) &&
- !(/*funval->cvq == CV_CONST && */ funval->hasvalue && funval->constval.vfunc->builtin))
+ if ((fun->m_flags & AST_FLAG_VARIADIC) &&
+ !(/*funval->m_cvq == CV_CONST && */ funval->m_hasvalue && funval->m_constval.vfunc->m_builtin))
{
- call->va_count = (ast_expression*)parser->m_fold.constgen_float((qcfloat_t)paramcount, false);
+ call->m_va_count = parser->m_fold.constgen_float((qcfloat_t)paramcount, false);
}
}
/* overwrite fid, the function, with a call */
- sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
+ sy->out[fid] = syexp(call->m_context, call);
- if (fun->vtype != TYPE_FUNCTION) {
- parseerror(parser, "not a function (%s)", type_name[fun->vtype]);
+ if (fun->m_vtype != TYPE_FUNCTION) {
+ parseerror(parser, "not a function (%s)", type_name[fun->m_vtype]);
return false;
}
- if (!fun->next) {
+ if (!fun->m_next) {
parseerror(parser, "could not determine function return type");
return false;
} else {
ast_value *fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : nullptr);
- if (fun->flags & AST_FLAG_DEPRECATED) {
+ if (fun->m_flags & AST_FLAG_DEPRECATED) {
if (!fval) {
return !parsewarning(parser, WARN_DEPRECATED,
"call to function (which is marked deprecated)\n",
"-> it has been declared here: %s:%i",
- ast_ctx(fun).file, ast_ctx(fun).line);
+ fun->m_context.file, fun->m_context.line);
}
- if (!fval->desc) {
+ if (!fval->m_desc.length()) {
return !parsewarning(parser, WARN_DEPRECATED,
"call to `%s` (which is marked deprecated)\n"
"-> `%s` declared here: %s:%i",
- fval->name, fval->name, ast_ctx(fun).file, ast_ctx(fun).line);
+ fval->m_name, fval->m_name, fun->m_context.file, fun->m_context.line);
}
return !parsewarning(parser, WARN_DEPRECATED,
"call to `%s` (deprecated: %s)\n"
"-> `%s` declared here: %s:%i",
- fval->name, fval->desc, fval->name, ast_ctx(fun).file,
- ast_ctx(fun).line);
+ fval->m_name, fval->m_desc, fval->m_name, fun->m_context.file,
+ fun->m_context.line);
}
- if (fun->params.size() != paramcount &&
- !((fun->flags & AST_FLAG_VARIADIC) &&
- fun->params.size() < paramcount))
+ if (fun->m_type_params.size() != paramcount &&
+ !((fun->m_flags & AST_FLAG_VARIADIC) &&
+ fun->m_type_params.size() < paramcount))
{
- const char *fewmany = (fun->params.size() > paramcount) ? "few" : "many";
+ const char *fewmany = (fun->m_type_params.size() > paramcount) ? "few" : "many";
if (fval)
return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
"too %s parameters for call to %s: expected %i, got %i\n"
" -> `%s` has been declared here: %s:%i",
- fewmany, fval->name, (int)fun->params.size(), (int)paramcount,
- fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
+ fewmany, fval->m_name, (int)fun->m_type_params.size(), (int)paramcount,
+ fval->m_name, fun->m_context.file, (int)fun->m_context.line);
else
return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
"too %s parameters for function call: expected %i, got %i\n"
" -> it has been declared here: %s:%i",
- fewmany, (int)fun->params.size(), (int)paramcount,
- ast_ctx(fun).file, (int)ast_ctx(fun).line);
+ fewmany, (int)fun->m_type_params.size(), (int)paramcount,
+ fun->m_context.file, (int)fun->m_context.line);
}
}
{
ast_expression *idx, *out;
ast_value *typevar;
- ast_value *funtype = parser->function->vtype;
+ ast_value *funtype = parser->function->m_function_type;
lex_ctx_t ctx = parser_ctx(parser);
- if (!parser->function->varargs) {
+ if (!parser->function->m_varargs) {
parseerror(parser, "function has no variable argument list");
return nullptr;
}
parseerror(parser, "expected comma after parameter index");
return nullptr;
}
- /* vararg piping: ...(start) */
- out = (ast_expression*)ast_argpipe_new(ctx, idx);
+ // vararg piping: ...(start)
+ out = new ast_argpipe(ctx, idx);
return out;
}
if (parser->tok != ')') {
ast_unref(idx);
- ast_delete(typevar);
+ delete typevar;
parseerror(parser, "expected closing paren");
return nullptr;
}
- if (funtype->expression.varparam &&
- !ast_compare_type((ast_expression*)typevar, (ast_expression*)funtype->expression.varparam))
+ if (funtype->m_varparam &&
+ !typevar->compareType(*funtype->m_varparam))
{
char ty1[1024];
char ty2[1024];
- ast_type_to_string((ast_expression*)typevar, ty1, sizeof(ty1));
- ast_type_to_string((ast_expression*)funtype->expression.varparam, ty2, sizeof(ty2));
- compile_error(ast_ctx(typevar),
+ ast_type_to_string(typevar, ty1, sizeof(ty1));
+ ast_type_to_string(funtype->m_varparam, ty2, sizeof(ty2));
+ compile_error(typevar->m_context,
"function was declared to take varargs of type `%s`, requested type is: %s",
ty2, ty1);
}
- out = (ast_expression*)ast_array_index_new(ctx, (ast_expression*)(parser->function->varargs), idx);
- ast_type_adopt(out, typevar);
- ast_delete(typevar);
+ out = ast_array_index::make(ctx, parser->function->m_varargs.get(), idx);
+ out->adoptType(*typevar);
+ delete typevar;
return out;
}
val = (ast_value*)parser->m_fold.constgen_string(parser_tokval(parser), true);
if (!val)
return false;
- sy->out.push_back(syexp(parser_ctx(parser), (ast_expression*)val));
+ sy->out.push_back(syexp(parser_ctx(parser), val));
if (!parser_next(parser) || parser->tok != ')') {
parseerror(parser, "expected closing paren after translatable string");
/* When adding more intrinsics, fix the above condition */
prev = nullptr;
}
- if (prev && prev->vtype == TYPE_VECTOR && ctoken[0] >= 'x' && ctoken[0] <= 'z' && !ctoken[1])
+ if (prev && prev->m_vtype == TYPE_VECTOR && ctoken[0] >= 'x' && ctoken[0] <= 'z' && !ctoken[1])
{
- var = (ast_expression*)parser->const_vec[ctoken[0]-'x'];
+ var = parser->const_vec[ctoken[0]-'x'];
} else {
var = parser_find_var(parser, parser_tokval(parser));
if (!var)
var = parser_find_field(parser, parser_tokval(parser));
}
if (!var && with_labels) {
- var = (ast_expression*)parser_find_label(parser, parser_tokval(parser));
+ var = parser_find_label(parser, parser_tokval(parser));
if (!with_labels) {
- ast_label *lbl = ast_label_new(parser_ctx(parser), parser_tokval(parser), true);
- var = (ast_expression*)lbl;
+ ast_label *lbl = new ast_label(parser_ctx(parser), parser_tokval(parser), true);
+ var = lbl;
parser->labels.push_back(lbl);
}
}
if (!var && !strcmp(parser_tokval(parser), "__FUNC__"))
- var = (ast_expression*)parser->m_fold.constgen_string(parser->function->name, false);
+ var = parser->m_fold.constgen_string(parser->function->m_name, false);
if (!var) {
/*
* now we try for the real intrinsic hashtable. If the string
}
else
{
- if (ast_istype(var, ast_value)) {
- ((ast_value*)var)->uses++;
+ // promote these to norefs
+ if (ast_istype(var, ast_value))
+ {
+ ((ast_value *)var)->m_flags |= AST_FLAG_NOREF;
}
- else if (ast_istype(var, ast_member)) {
- ast_member *mem = (ast_member*)var;
- if (ast_istype(mem->owner, ast_value))
- ((ast_value*)(mem->owner))->uses++;
+ else if (ast_istype(var, ast_member))
+ {
+ ast_member *mem = (ast_member *)var;
+ if (ast_istype(mem->m_owner, ast_value))
+ ((ast_value *)mem->m_owner)->m_flags |= AST_FLAG_NOREF;
}
}
sy->out.push_back(syexp(parser_ctx(parser), var));
ast_expression *lexpr = sy.out.back().out;
if (ast_istype(lexpr, ast_value)) {
ast_value *last = (ast_value*)lexpr;
- if (last->isimm == true && last->cvq == CV_CONST &&
- last->hasvalue && last->expression.vtype == TYPE_STRING)
+ if (last->m_isimm == true && last->m_cvq == CV_CONST &&
+ last->m_hasvalue && last->m_vtype == TYPE_STRING)
{
char *newstr = nullptr;
- util_asprintf(&newstr, "%s%s", last->constval.vstring, parser_tokval(parser));
- sy.out.back().out = (ast_expression*)parser->m_fold.constgen_string(newstr, false);
+ util_asprintf(&newstr, "%s%s", last->m_constval.vstring, parser_tokval(parser));
+ sy.out.back().out = parser->m_fold.constgen_string(newstr, false);
mem_d(newstr);
concatenated = true;
}
static void parser_enterblock(parser_t *parser)
{
- vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
- vec_push(parser->_blocklocals, vec_size(parser->_locals));
- vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
- vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
- vec_push(parser->_block_ctx, parser_ctx(parser));
+ parser->variables.push_back(util_htnew(PARSER_HT_SIZE));
+ parser->_blocklocals.push_back(parser->_locals.size());
+ parser->typedefs.push_back(util_htnew(TYPEDEF_HT_SIZE));
+ parser->_blocktypedefs.push_back(parser->_typedefs.size());
+ parser->_block_ctx.push_back(parser_ctx(parser));
}
static bool parser_leaveblock(parser_t *parser)
bool rv = true;
size_t locals, typedefs;
- if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
+ if (parser->variables.size() <= PARSER_HT_LOCALS) {
parseerror(parser, "internal error: parser_leaveblock with no block");
return false;
}
- util_htdel(vec_last(parser->variables));
+ util_htdel(parser->variables.back());
- vec_pop(parser->variables);
- if (!vec_size(parser->_blocklocals)) {
+ parser->variables.pop_back();
+ if (!parser->_blocklocals.size()) {
parseerror(parser, "internal error: parser_leaveblock with no block (2)");
return false;
}
- locals = vec_last(parser->_blocklocals);
- vec_pop(parser->_blocklocals);
- while (vec_size(parser->_locals) != locals) {
- ast_expression *e = vec_last(parser->_locals);
- ast_value *v = (ast_value*)e;
- vec_pop(parser->_locals);
- if (ast_istype(e, ast_value) && !v->uses) {
- if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
- rv = false;
- }
- }
+ locals = parser->_blocklocals.back();
+ parser->_blocklocals.pop_back();
+ parser->_locals.resize(locals);
- typedefs = vec_last(parser->_blocktypedefs);
- while (vec_size(parser->_typedefs) != typedefs) {
- ast_delete(vec_last(parser->_typedefs));
- vec_pop(parser->_typedefs);
- }
- util_htdel(vec_last(parser->typedefs));
- vec_pop(parser->typedefs);
+ typedefs = parser->_blocktypedefs.back();
+ parser->_typedefs.resize(typedefs);
+ util_htdel(parser->typedefs.back());
+ parser->typedefs.pop_back();
- vec_pop(parser->_block_ctx);
+ parser->_block_ctx.pop_back();
return rv;
}
static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
{
- vec_push(parser->_locals, e);
- util_htset(vec_last(parser->variables), name, (void*)e);
+ parser->_locals.push_back(e);
+ util_htset(parser->variables.back(), name, (void*)e);
+}
+static void parser_addlocal(parser_t *parser, const std::string &name, ast_expression *e) {
+ return parser_addlocal(parser, name.c_str(), e);
}
static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e)
parser->globals.push_back(e);
util_htset(parser->htglobals, name, e);
}
+static void parser_addglobal(parser_t *parser, const std::string &name, ast_expression *e) {
+ return parser_addglobal(parser, name.c_str(), e);
+}
static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
{
ast_unary *unary;
ast_expression *prev;
- if (cond->vtype == TYPE_VOID || cond->vtype >= TYPE_VARIANT) {
+ if (cond->m_vtype == TYPE_VOID || cond->m_vtype >= TYPE_VARIANT) {
char ty[1024];
ast_type_to_string(cond, ty, sizeof(ty));
- compile_error(ast_ctx(cond), "invalid type for if() condition: %s", ty);
+ compile_error(cond->m_context, "invalid type for if() condition: %s", ty);
}
- if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->vtype == TYPE_STRING)
+ if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->m_vtype == TYPE_STRING)
{
prev = cond;
- cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_S, cond);
+ cond = ast_unary::make(cond->m_context, INSTR_NOT_S, cond);
if (!cond) {
ast_unref(prev);
parseerror(parser, "internal error: failed to process condition");
}
ifnot = !ifnot;
}
- else if (OPTS_FLAG(CORRECT_LOGIC) && cond->vtype == TYPE_VECTOR)
+ else if (OPTS_FLAG(CORRECT_LOGIC) && cond->m_vtype == TYPE_VECTOR)
{
/* vector types need to be cast to true booleans */
ast_binary *bin = (ast_binary*)cond;
- if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->op == INSTR_AND || bin->op == INSTR_OR))
+ if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->m_op == INSTR_AND || bin->m_op == INSTR_OR))
{
/* in perl-logic, AND and OR take care of the -fcorrect-logic */
prev = cond;
- cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_V, cond);
+ cond = ast_unary::make(cond->m_context, INSTR_NOT_V, cond);
if (!cond) {
ast_unref(prev);
parseerror(parser, "internal error: failed to process condition");
unary = (ast_unary*)cond;
/* ast_istype dereferences cond, should test here for safety */
- while (cond && ast_istype(cond, ast_unary) && unary->op == INSTR_NOT_F)
+ while (cond && ast_istype(cond, ast_unary) && unary->m_op == INSTR_NOT_F)
{
- cond = unary->operand;
- unary->operand = nullptr;
- ast_delete(unary);
+ cond = unary->m_operand;
+ unary->m_operand = nullptr;
+ delete unary;
ifnot = !ifnot;
unary = (ast_unary*)cond;
}
return false;
}
if (!ontrue)
- ontrue = (ast_expression*)ast_block_new(parser_ctx(parser));
+ ontrue = new ast_block(parser_ctx(parser));
/* check for an else */
if (!strcmp(parser_tokval(parser), "else")) {
/* parse into the 'else' branch */
if (!parser_next(parser)) {
parseerror(parser, "expected on-false branch after 'else'");
- ast_delete(ontrue);
+ delete ontrue;
ast_unref(cond);
return false;
}
if (!parse_statement_or_block(parser, &onfalse)) {
- ast_delete(ontrue);
+ delete ontrue;
ast_unref(cond);
return false;
}
cond = process_condition(parser, cond, &ifnot);
if (!cond) {
- if (ontrue) ast_delete(ontrue);
- if (onfalse) ast_delete(onfalse);
+ if (ontrue) delete ontrue;
+ if (onfalse) delete onfalse;
return false;
}
if (ifnot)
- ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
+ ifthen = new ast_ifthen(ctx, cond, onfalse, ontrue);
else
- ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
- *out = (ast_expression*)ifthen;
+ ifthen = new ast_ifthen(ctx, cond, ontrue, onfalse);
+ *out = ifthen;
return true;
}
if (parser->breaks.back() != label || parser->continues.back() != label) {
parseerror(parser, "internal error: label stack corrupted");
rv = false;
- ast_delete(*out);
+ delete *out;
*out = nullptr;
}
else {
ast_unref(ontrue);
return false;
}
- aloop = ast_loop_new(ctx, nullptr, cond, ifnot, nullptr, false, nullptr, ontrue);
- *out = (ast_expression*)aloop;
+ aloop = new ast_loop(ctx, nullptr, cond, ifnot, nullptr, false, nullptr, ontrue);
+ *out = aloop;
return true;
}
if (parser->breaks.back() != label || parser->continues.back() != label) {
parseerror(parser, "internal error: label stack corrupted");
rv = false;
- /*
- * Test for nullptr otherwise ast_delete dereferences null pointer
- * and boom.
- */
- if (*out)
- ast_delete(*out);
+ delete *out;
*out = nullptr;
}
else {
strcmp(parser_tokval(parser), "while"))
{
parseerror(parser, "expected 'while' and condition");
- ast_delete(ontrue);
+ delete ontrue;
return false;
}
/* skip the 'while' and check for opening paren */
if (!parser_next(parser) || parser->tok != '(') {
parseerror(parser, "expected 'while' condition in parenthesis");
- ast_delete(ontrue);
+ delete ontrue;
return false;
}
/* parse into the expression */
if (!parser_next(parser)) {
parseerror(parser, "expected 'while' condition after opening paren");
- ast_delete(ontrue);
+ delete ontrue;
return false;
}
/* parse the condition */
/* closing paren */
if (parser->tok != ')') {
parseerror(parser, "expected closing paren after 'while' condition");
- ast_delete(ontrue);
+ delete ontrue;
ast_unref(cond);
return false;
}
/* parse on */
if (!parser_next(parser) || parser->tok != ';') {
parseerror(parser, "expected semicolon after condition");
- ast_delete(ontrue);
+ delete ontrue;
ast_unref(cond);
return false;
}
if (!parser_next(parser)) {
parseerror(parser, "parse error");
- ast_delete(ontrue);
+ delete ontrue;
ast_unref(cond);
return false;
}
cond = process_condition(parser, cond, &ifnot);
if (!cond) {
- ast_delete(ontrue);
+ delete ontrue;
return false;
}
- aloop = ast_loop_new(ctx, nullptr, nullptr, false, cond, ifnot, nullptr, ontrue);
- *out = (ast_expression*)aloop;
+ aloop = new ast_loop(ctx, nullptr, nullptr, false, cond, ifnot, nullptr, ontrue);
+ *out = aloop;
return true;
}
if (parser->breaks.back() != label || parser->continues.back() != label) {
parseerror(parser, "internal error: label stack corrupted");
rv = false;
- ast_delete(*out);
+ delete *out;
*out = nullptr;
}
else {
initexpr = parse_expression_leave(parser, false, false, false);
if (!initexpr)
goto onerr;
-
/* move on to condition */
if (parser->tok != ';') {
parseerror(parser, "expected semicolon after for-loop initializer");
goto onerr;
}
-
if (!parser_next(parser)) {
parseerror(parser, "expected for-loop condition");
goto onerr;
}
+ } else if (!parser_next(parser)) {
+ parseerror(parser, "expected for-loop condition");
+ goto onerr;
}
/* parse the condition */
if (!cond)
goto onerr;
}
-
/* move on to incrementor */
if (parser->tok != ';') {
parseerror(parser, "expected semicolon after for-loop initializer");
increment = parse_expression_leave(parser, false, false, false);
if (!increment)
goto onerr;
- if (!ast_side_effects(increment)) {
+ if (!increment->m_side_effects) {
if (compile_warning(condctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
goto onerr;
}
if (!cond)
goto onerr;
}
- aloop = ast_loop_new(ctx, initexpr, cond, ifnot, nullptr, false, increment, ontrue);
- *out = (ast_expression*)aloop;
+ aloop = new ast_loop(ctx, initexpr, cond, ifnot, nullptr, false, increment, ontrue);
+ *out = aloop;
if (!parser_leaveblock(parser)) {
- ast_delete(aloop);
+ delete aloop;
return false;
}
return true;
ast_expression *exp = nullptr;
ast_expression *var = nullptr;
ast_return *ret = nullptr;
- ast_value *retval = parser->function->return_value;
- ast_value *expected = parser->function->vtype;
+ ast_value *retval = parser->function->m_return_value;
+ ast_value *expected = parser->function->m_function_type;
lex_ctx_t ctx = parser_ctx(parser);
return false;
}
- if (type_store_instr[expected->expression.next->vtype] == VINSTR_END) {
+ if (type_store_instr[expected->m_next->m_vtype] == VINSTR_END) {
char ty1[1024];
- ast_type_to_string(expected->expression.next, ty1, sizeof(ty1));
+ ast_type_to_string(expected->m_next, ty1, sizeof(ty1));
parseerror(parser, "invalid return type: `%s'", ty1);
return false;
}
/* prepare the return value */
if (!retval) {
- retval = ast_value_new(ctx, "#LOCAL_RETURN", TYPE_VOID);
- ast_type_adopt(retval, expected->expression.next);
- parser->function->return_value = retval;
+ retval = new ast_value(ctx, "#LOCAL_RETURN", TYPE_VOID);
+ retval->adoptType(*expected->m_next);
+ parser->function->m_return_value = retval;
+ parser->function->m_return_value->m_flags |= AST_FLAG_NOREF;
}
- if (!ast_compare_type(exp, (ast_expression*)retval)) {
+ if (!exp->compareType(*retval)) {
char ty1[1024], ty2[1024];
ast_type_to_string(exp, ty1, sizeof(ty1));
- ast_type_to_string(&retval->expression, ty2, sizeof(ty2));
+ ast_type_to_string(retval, ty2, sizeof(ty2));
parseerror(parser, "invalid type for return value: `%s', expected `%s'", ty1, ty2);
}
/* store to 'return' local variable */
- var = (ast_expression*)ast_store_new(
+ var = new ast_store(
ctx,
- type_store_instr[expected->expression.next->vtype],
- (ast_expression*)retval, exp);
+ type_store_instr[expected->m_next->m_vtype],
+ retval, exp);
if (!var) {
ast_unref(exp);
if (!exp)
return false;
- if (exp->vtype != TYPE_NIL &&
- exp->vtype != ((ast_expression*)expected)->next->vtype)
+ if (exp->m_vtype != TYPE_NIL &&
+ exp->m_vtype != (expected)->m_next->m_vtype)
{
parseerror(parser, "return with invalid expression");
}
- ret = ast_return_new(ctx, exp);
+ ret = new ast_return(ctx, exp);
if (!ret) {
ast_unref(exp);
return false;
if (!parser_next(parser))
parseerror(parser, "parse error");
- if (!retval && expected->expression.next->vtype != TYPE_VOID)
+ if (!retval && expected->m_next->m_vtype != TYPE_VOID)
{
(void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
}
- ret = ast_return_new(ctx, (ast_expression*)retval);
+ ret = new ast_return(ctx, retval);
}
- *out = (ast_expression*)ret;
+ *out = ret;
return true;
}
if (!parser_next(parser))
parseerror(parser, "parse error");
- *out = (ast_expression*)ast_breakcont_new(ctx, is_continue, levels);
+ *out = new ast_breakcont(ctx, is_continue, levels);
return true;
}
if (parser->breaks.back() != label) {
parseerror(parser, "internal error: label stack corrupted");
rv = false;
- ast_delete(*out);
+ delete *out;
*out = nullptr;
}
else {
if (!operand)
return false;
- switchnode = ast_switch_new(ctx, operand);
+ switchnode = new ast_switch(ctx, operand);
/* closing paren */
if (parser->tok != ')') {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "expected closing paren after 'switch' operand");
return false;
}
/* parse over the opening paren */
if (!parser_next(parser) || parser->tok != '{') {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "expected list of cases");
return false;
}
if (!parser_next(parser)) {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "expected 'case' or 'default'");
return false;
}
typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
if (typevar || parser->tok == TOKEN_TYPENAME) {
if (!parse_variable(parser, block, true, CV_NONE, typevar, false, false, 0, nullptr)) {
- ast_delete(switchnode);
+ delete switchnode;
return false;
}
continue;
if (parse_qualifiers(parser, true, &cvq, &noref, &is_static, &qflags, nullptr))
{
if (cvq == CV_WRONG) {
- ast_delete(switchnode);
+ delete switchnode;
return false;
}
if (!parse_variable(parser, block, true, cvq, nullptr, noref, is_static, qflags, nullptr)) {
- ast_delete(switchnode);
+ delete switchnode;
return false;
}
continue;
if (!strcmp(parser_tokval(parser), "case")) {
if (!parser_next(parser)) {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "expected expression for case");
return false;
}
- swcase.value = parse_expression_leave(parser, false, false, false);
- if (!swcase.value) {
- ast_delete(switchnode);
+ swcase.m_value = parse_expression_leave(parser, false, false, false);
+
+ if (!operand->compareType(*swcase.m_value)) {
+ char ty1[1024];
+ char ty2[1024];
+
+ ast_type_to_string(swcase.m_value, ty1, sizeof ty1);
+ ast_type_to_string(operand, ty2, sizeof ty2);
+
+ auto fnLiteral = [](ast_expression *expression) -> char* {
+ if (!ast_istype(expression, ast_value))
+ return nullptr;
+ ast_value *value = (ast_value *)expression;
+ if (!value->m_hasvalue)
+ return nullptr;
+ char *string = nullptr;
+ basic_value_t *constval = &value->m_constval;
+ switch (value->m_vtype)
+ {
+ case TYPE_FLOAT:
+ util_asprintf(&string, "%.2f", constval->vfloat);
+ return string;
+ case TYPE_VECTOR:
+ util_asprintf(&string, "'%.2f %.2f %.2f'",
+ constval->vvec.x,
+ constval->vvec.y,
+ constval->vvec.z);
+ return string;
+ case TYPE_STRING:
+ util_asprintf(&string, "\"%s\"", constval->vstring);
+ return string;
+ default:
+ break;
+ }
+ return nullptr;
+ };
+
+ char *literal = fnLiteral(swcase.m_value);
+ if (literal)
+ compile_error(parser_ctx(parser), "incompatible type `%s` for switch case `%s` expected `%s`", ty1, literal, ty2);
+ else
+ compile_error(parser_ctx(parser), "incompatible type `%s` for switch case expected `%s`", ty1, ty2);
+ mem_d(literal);
+ delete switchnode;
+ return false;
+ }
+
+ if (!swcase.m_value) {
+ delete switchnode;
parseerror(parser, "expected expression for case");
return false;
}
if (!OPTS_FLAG(RELAXED_SWITCH)) {
- if (!ast_istype(swcase.value, ast_value)) { /* || ((ast_value*)swcase.value)->cvq != CV_CONST) { */
+ if (!ast_istype(swcase.m_value, ast_value)) { /* || ((ast_value*)swcase.m_value)->m_cvq != CV_CONST) { */
+ delete switchnode;
parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
ast_unref(operand);
return false;
}
}
else if (!strcmp(parser_tokval(parser), "default")) {
- swcase.value = nullptr;
+ swcase.m_value = nullptr;
if (!parser_next(parser)) {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "expected colon");
return false;
}
}
else {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "expected 'case' or 'default'");
return false;
}
/* Now the colon and body */
if (parser->tok != ':') {
- if (swcase.value) ast_unref(swcase.value);
- ast_delete(switchnode);
+ if (swcase.m_value) ast_unref(swcase.m_value);
+ delete switchnode;
parseerror(parser, "expected colon");
return false;
}
if (!parser_next(parser)) {
- if (swcase.value) ast_unref(swcase.value);
- ast_delete(switchnode);
+ if (swcase.m_value) ast_unref(swcase.m_value);
+ delete switchnode;
parseerror(parser, "expected statements or case");
return false;
}
- caseblock = ast_block_new(parser_ctx(parser));
+ caseblock = new ast_block(parser_ctx(parser));
if (!caseblock) {
- if (swcase.value) ast_unref(swcase.value);
- ast_delete(switchnode);
+ if (swcase.m_value) ast_unref(swcase.m_value);
+ delete switchnode;
return false;
}
- swcase.code = (ast_expression*)caseblock;
- switchnode->cases.push_back(swcase);
+ swcase.m_code = caseblock;
+ switchnode->m_cases.push_back(swcase);
while (true) {
ast_expression *expr;
if (parser->tok == '}')
}
}
if (!parse_statement(parser, caseblock, &expr, true)) {
- ast_delete(switchnode);
+ delete switchnode;
return false;
}
if (!expr)
continue;
- if (!ast_block_add_expr(caseblock, expr)) {
- ast_delete(switchnode);
+ if (!caseblock->addExpr(expr)) {
+ delete switchnode;
return false;
}
}
/* closing paren */
if (parser->tok != '}') {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "expected closing paren of case list");
return false;
}
if (!parser_next(parser)) {
- ast_delete(switchnode);
+ delete switchnode;
parseerror(parser, "parse error after switch");
return false;
}
- *out = (ast_expression*)switchnode;
+ *out = switchnode;
return true;
}
if (ast_istype(*side, ast_ternary)) {
ast_ternary *tern = (ast_ternary*)*side;
- on_true = parse_goto_computed(parser, &tern->on_true);
- on_false = parse_goto_computed(parser, &tern->on_false);
+ on_true = parse_goto_computed(parser, &tern->m_on_true);
+ on_false = parse_goto_computed(parser, &tern->m_on_false);
if (!on_true || !on_false) {
parseerror(parser, "expected label or expression in ternary");
return nullptr;
}
- cond = tern->cond;
- tern->cond = nullptr;
- ast_delete(tern);
+ cond = tern->m_cond;
+ tern->m_cond = nullptr;
+ delete tern;
*side = nullptr;
- return (ast_expression*)ast_ifthen_new(parser_ctx(parser), cond, on_true, on_false);
+ return new ast_ifthen(parser_ctx(parser), cond, on_true, on_false);
} else if (ast_istype(*side, ast_label)) {
- ast_goto *gt = ast_goto_new(parser_ctx(parser), ((ast_label*)*side)->name);
- ast_goto_set_label(gt, ((ast_label*)*side));
+ ast_goto *gt = new ast_goto(parser_ctx(parser), ((ast_label*)*side)->m_name);
+ gt->setLabel(reinterpret_cast<ast_label*>(*side));
*side = nullptr;
- return (ast_expression*)gt;
+ return gt;
}
return nullptr;
}
}
/* not computed goto */
- gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
- lbl = parser_find_label(parser, gt->name);
+ gt = new ast_goto(parser_ctx(parser), parser_tokval(parser));
+ lbl = parser_find_label(parser, gt->m_name);
if (lbl) {
if (!ast_istype(lbl, ast_label)) {
parseerror(parser, "internal error: label is not an ast_label");
- ast_delete(gt);
+ delete gt;
return false;
}
- ast_goto_set_label(gt, (ast_label*)lbl);
+ gt->setLabel(reinterpret_cast<ast_label*>(lbl));
}
else
parser->gotos.push_back(gt);
return false;
}
- *out = (ast_expression*)gt;
+ *out = gt;
return true;
}
if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
{
- ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
- con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
+ ast_type_to_string(tdef, ty, sizeof(ty));
+ con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->m_name.c_str(), ty);
if (!parser_next(parser)) {
parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
return false;
inner = parse_block(parser);
if (!inner)
return false;
- *out = (ast_expression*)inner;
+ *out = inner;
return true;
}
else if (parser->tok == ':')
}
label = (ast_label*)parser_find_label(parser, parser_tokval(parser));
if (label) {
- if (!label->undefined) {
- parseerror(parser, "label `%s` already defined", label->name);
+ if (!label->m_undefined) {
+ parseerror(parser, "label `%s` already defined", label->m_name);
return false;
}
- label->undefined = false;
+ label->m_undefined = false;
}
else {
- label = ast_label_new(parser_ctx(parser), parser_tokval(parser), false);
+ label = new ast_label(parser_ctx(parser), parser_tokval(parser), false);
parser->labels.push_back(label);
}
- *out = (ast_expression*)label;
+ *out = label;
if (!parser_next(parser)) {
parseerror(parser, "parse error after label");
return false;
}
for (i = 0; i < parser->gotos.size(); ++i) {
- if (!strcmp(parser->gotos[i]->name, label->name)) {
- ast_goto_set_label(parser->gotos[i], label);
+ if (parser->gotos[i]->m_name == label->m_name) {
+ parser->gotos[i]->setLabel(label);
parser->gotos.erase(parser->gotos.begin() + i);
--i;
}
if (!exp)
return false;
*out = exp;
- if (!ast_side_effects(exp)) {
+ if (!exp->m_side_effects) {
if (compile_warning(ctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
return false;
}
bool flag = false;
bool reverse = false;
qcfloat_t num = 0;
- ast_value **values = nullptr;
ast_value *var = nullptr;
ast_value *asvalue;
+ std::vector<ast_value*> values;
ast_expression *old;
break;
}
parseerror(parser, "expected identifier or `}`");
- goto onerror;
+ return false;
}
old = parser_find_field(parser, parser_tokval(parser));
old = parser_find_global(parser, parser_tokval(parser));
if (old) {
parseerror(parser, "value `%s` has already been declared here: %s:%i",
- parser_tokval(parser), ast_ctx(old).file, ast_ctx(old).line);
- goto onerror;
+ parser_tokval(parser), old->m_context.file, old->m_context.line);
+ return false;
}
- var = ast_value_new(parser_ctx(parser), parser_tokval(parser), TYPE_FLOAT);
- vec_push(values, var);
- var->cvq = CV_CONST;
- var->hasvalue = true;
+ var = new ast_value(parser_ctx(parser), parser_tokval(parser), TYPE_FLOAT);
+ values.push_back(var);
+ var->m_cvq = CV_CONST;
+ var->m_hasvalue = true;
/* for flagged enumerations increment in POTs of TWO */
- var->constval.vfloat = (flag) ? (num *= 2) : (num ++);
- parser_addglobal(parser, var->name, (ast_expression*)var);
+ var->m_constval.vfloat = (flag) ? (num *= 2) : (num ++);
+ parser_addglobal(parser, var->m_name, var);
if (!parser_next(parser)) {
parseerror(parser, "expected `=`, `}` or comma after identifier");
- goto onerror;
+ return false;
}
if (parser->tok == ',')
break;
if (parser->tok != '=') {
parseerror(parser, "expected `=`, `}` or comma after identifier");
- goto onerror;
+ return false;
}
if (!parser_next(parser)) {
parseerror(parser, "expected expression after `=`");
- goto onerror;
+ return false;
}
/* We got a value! */
old = parse_expression_leave(parser, true, false, false);
asvalue = (ast_value*)old;
- if (!ast_istype(old, ast_value) || asvalue->cvq != CV_CONST || !asvalue->hasvalue) {
- compile_error(ast_ctx(var), "constant value or expression expected");
- goto onerror;
+ if (!ast_istype(old, ast_value) || asvalue->m_cvq != CV_CONST || !asvalue->m_hasvalue) {
+ compile_error(var->m_context, "constant value or expression expected");
+ return false;
}
- num = (var->constval.vfloat = asvalue->constval.vfloat) + 1;
+ num = (var->m_constval.vfloat = asvalue->m_constval.vfloat) + 1;
if (parser->tok == '}')
break;
if (parser->tok != ',') {
parseerror(parser, "expected `}` or comma after expression");
- goto onerror;
+ return false;
}
}
/* patch them all (for reversed attribute) */
if (reverse) {
size_t i;
- for (i = 0; i < vec_size(values); i++)
- values[i]->constval.vfloat = vec_size(values) - i - 1;
+ for (i = 0; i < values.size(); i++)
+ values[i]->m_constval.vfloat = values.size() - i - 1;
}
if (parser->tok != '}') {
parseerror(parser, "internal error: breaking without `}`");
- goto onerror;
+ return false;
}
if (!parser_next(parser) || parser->tok != ';') {
parseerror(parser, "expected semicolon after enumeration");
- goto onerror;
+ return false;
}
if (!parser_next(parser)) {
parseerror(parser, "parse error after enumeration");
- goto onerror;
+ return false;
}
- vec_free(values);
return true;
-
-onerror:
- vec_free(values);
- return false;
}
static bool parse_block_into(parser_t *parser, ast_block *block)
}
if (!expr)
continue;
- if (!ast_block_add_expr(block, expr)) {
- ast_delete(block);
+ if (!block->addExpr(expr)) {
+ delete block;
block = nullptr;
goto cleanup;
}
static ast_block* parse_block(parser_t *parser)
{
ast_block *block;
- block = ast_block_new(parser_ctx(parser));
+ block = new ast_block(parser_ctx(parser));
if (!block)
return nullptr;
if (!parse_block_into(parser, block)) {
- ast_block_delete(block);
+ delete block;
return nullptr;
}
return block;
static bool parse_statement_or_block(parser_t *parser, ast_expression **out)
{
if (parser->tok == '{') {
- *out = (ast_expression*)parse_block(parser);
+ *out = parse_block(parser);
return !!*out;
}
return parse_statement(parser, nullptr, out, false);
static bool create_vector_members(ast_value *var, ast_member **me)
{
size_t i;
- size_t len = strlen(var->name);
+ size_t len = var->m_name.length();
for (i = 0; i < 3; ++i) {
char *name = (char*)mem_a(len+3);
- memcpy(name, var->name, len);
+ memcpy(name, var->m_name.c_str(), len);
name[len+0] = '_';
name[len+1] = 'x'+i;
name[len+2] = 0;
- me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
+ me[i] = ast_member::make(var->m_context, var, i, name);
mem_d(name);
if (!me[i])
break;
return true;
/* unroll */
- do { ast_member_delete(me[--i]); } while(i);
+ do { delete me[--i]; } while(i);
return false;
}
has_frame_think = false;
old = parser->function;
- if (var->expression.flags & AST_FLAG_ALIAS) {
+ if (var->m_flags & AST_FLAG_ALIAS) {
parseerror(parser, "function aliases cannot have bodies");
return false;
}
return false;
}
- if (!OPTS_FLAG(VARIADIC_ARGS) && var->expression.flags & AST_FLAG_VARIADIC) {
+ if (!OPTS_FLAG(VARIADIC_ARGS) && var->m_flags & AST_FLAG_VARIADIC) {
if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
"variadic function with implementation will not be able to access additional parameters (try -fvariadic-args)"))
{
parseerror(parser, "expected a framenumber constant in[frame,think] notation");
return false;
}
- if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
+ if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->m_hasvalue) {
ast_unref(framenum);
parseerror(parser, "framenumber in [frame,think] notation must be a constant");
return false;
/* qc allows the use of not-yet-declared functions here
* - this automatically creates a prototype */
ast_value *thinkfunc;
- ast_expression *functype = fld_think->next;
+ ast_expression *functype = fld_think->m_next;
- thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->vtype);
- if (!thinkfunc) { /* || !ast_type_adopt(thinkfunc, functype)*/
+ thinkfunc = new ast_value(parser_ctx(parser), parser_tokval(parser), functype->m_vtype);
+ if (!thinkfunc) { /* || !thinkfunc->adoptType(*functype)*/
ast_unref(framenum);
parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
return false;
}
- ast_type_adopt(thinkfunc, functype);
+ thinkfunc->adoptType(*functype);
if (!parser_next(parser)) {
ast_unref(framenum);
- ast_delete(thinkfunc);
+ delete thinkfunc;
return false;
}
- parser_addglobal(parser, thinkfunc->name, (ast_expression*)thinkfunc);
+ parser_addglobal(parser, thinkfunc->m_name, thinkfunc);
- nextthink = (ast_expression*)thinkfunc;
+ nextthink = thinkfunc;
} else {
nextthink = parse_expression_leave(parser, true, false, false);
has_frame_think = true;
}
- block = ast_block_new(parser_ctx(parser));
+ block = new ast_block(parser_ctx(parser));
if (!block) {
parseerror(parser, "failed to allocate block");
if (has_frame_think) {
if (has_frame_think) {
if (!OPTS_FLAG(EMULATE_STATE)) {
- ast_state *state_op = ast_state_new(parser_ctx(parser), framenum, nextthink);
- if (!ast_block_add_expr(block, (ast_expression*)state_op)) {
+ ast_state *state_op = new ast_state(parser_ctx(parser), framenum, nextthink);
+ if (!block->addExpr(state_op)) {
parseerror(parser, "failed to generate state op for [frame,think]");
ast_unref(nextthink);
ast_unref(framenum);
- ast_delete(block);
+ delete block;
return false;
}
} else {
float frame_delta = 1.0f / (float)OPTS_OPTION_U32(OPTION_STATE_FPS);
ctx = parser_ctx(parser);
- self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
- self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
- self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
+ self_frame = new ast_entfield(ctx, gbl_self, fld_frame);
+ self_nextthink = new ast_entfield(ctx, gbl_self, fld_nextthink);
+ self_think = new ast_entfield(ctx, gbl_self, fld_think);
- time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
- gbl_time, (ast_expression*)parser->m_fold.constgen_float(frame_delta, false));
+ time_plus_1 = new ast_binary(ctx, INSTR_ADD_F,
+ gbl_time, parser->m_fold.constgen_float(frame_delta, false));
if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
- if (self_frame) ast_delete(self_frame);
- if (self_nextthink) ast_delete(self_nextthink);
- if (self_think) ast_delete(self_think);
- if (time_plus_1) ast_delete(time_plus_1);
+ if (self_frame) delete self_frame;
+ if (self_nextthink) delete self_nextthink;
+ if (self_think) delete self_think;
+ if (time_plus_1) delete time_plus_1;
retval = false;
}
if (retval)
{
- store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
- store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
- store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
+ store_frame = new ast_store(ctx, INSTR_STOREP_F, self_frame, framenum);
+ store_nextthink = new ast_store(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
+ store_think = new ast_store(ctx, INSTR_STOREP_FNC, self_think, nextthink);
if (!store_frame) {
- ast_delete(self_frame);
+ delete self_frame;
retval = false;
}
if (!store_nextthink) {
- ast_delete(self_nextthink);
+ delete self_nextthink;
retval = false;
}
if (!store_think) {
- ast_delete(self_think);
+ delete self_think;
retval = false;
}
if (!retval) {
- if (store_frame) ast_delete(store_frame);
- if (store_nextthink) ast_delete(store_nextthink);
- if (store_think) ast_delete(store_think);
+ if (store_frame) delete store_frame;
+ if (store_nextthink) delete store_nextthink;
+ if (store_think) delete store_think;
retval = false;
}
- if (!ast_block_add_expr(block, (ast_expression*)store_frame) ||
- !ast_block_add_expr(block, (ast_expression*)store_nextthink) ||
- !ast_block_add_expr(block, (ast_expression*)store_think))
+ if (!block->addExpr(store_frame) ||
+ !block->addExpr(store_nextthink) ||
+ !block->addExpr(store_think))
{
retval = false;
}
parseerror(parser, "failed to generate code for [frame,think]");
ast_unref(nextthink);
ast_unref(framenum);
- ast_delete(block);
+ delete block;
return false;
}
}
}
- if (var->hasvalue) {
- if (!(var->expression.flags & AST_FLAG_ACCUMULATE)) {
- parseerror(parser, "function `%s` declared with multiple bodies", var->name);
- ast_block_delete(block);
+ if (var->m_hasvalue) {
+ if (!(var->m_flags & AST_FLAG_ACCUMULATE)) {
+ parseerror(parser, "function `%s` declared with multiple bodies", var->m_name);
+ delete block;
goto enderr;
}
- func = var->constval.vfunc;
+ func = var->m_constval.vfunc;
if (!func) {
- parseerror(parser, "internal error: nullptr function: `%s`", var->name);
- ast_block_delete(block);
+ parseerror(parser, "internal error: nullptr function: `%s`", var->m_name);
+ delete block;
goto enderr;
}
} else {
- func = ast_function_new(ast_ctx(var), var->name, var);
+ func = ast_function::make(var->m_context, var->m_name, var);
if (!func) {
- parseerror(parser, "failed to allocate function for `%s`", var->name);
- ast_block_delete(block);
+ parseerror(parser, "failed to allocate function for `%s`", var->m_name);
+ delete block;
goto enderr;
}
parser->functions.push_back(func);
parser_enterblock(parser);
- for (auto &it : var->expression.params) {
+ for (auto &it : var->m_type_params) {
size_t e;
ast_member *me[3];
- if (it->expression.vtype != TYPE_VECTOR &&
- (it->expression.vtype != TYPE_FIELD ||
- it->expression.next->vtype != TYPE_VECTOR))
+ if (it->m_vtype != TYPE_VECTOR &&
+ (it->m_vtype != TYPE_FIELD ||
+ it->m_next->m_vtype != TYPE_VECTOR))
{
continue;
}
- if (!create_vector_members(it, me)) {
- ast_block_delete(block);
+ if (!create_vector_members(it.get(), me)) {
+ delete block;
goto enderrfn;
}
for (e = 0; e < 3; ++e) {
- parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
- ast_block_collect(block, (ast_expression*)me[e]);
+ parser_addlocal(parser, me[e]->m_name, me[e]);
+ block->collect(me[e]);
}
}
- if (var->argcounter && !func->argc) {
- ast_value *argc = ast_value_new(ast_ctx(var), var->argcounter, TYPE_FLOAT);
- parser_addlocal(parser, argc->name, (ast_expression*)argc);
- func->argc = argc;
+ if (var->m_argcounter && !func->m_argc) {
+ ast_value *argc = new ast_value(var->m_context, var->m_argcounter, TYPE_FLOAT);
+ parser_addlocal(parser, argc->m_name, argc);
+ func->m_argc.reset(argc);
}
- if (OPTS_FLAG(VARIADIC_ARGS) && var->expression.flags & AST_FLAG_VARIADIC && !func->varargs) {
+ if (OPTS_FLAG(VARIADIC_ARGS) && var->m_flags & AST_FLAG_VARIADIC && !func->m_varargs) {
char name[1024];
- ast_value *varargs = ast_value_new(ast_ctx(var), "reserved:va_args", TYPE_ARRAY);
- varargs->expression.flags |= AST_FLAG_IS_VARARG;
- varargs->expression.next = (ast_expression*)ast_value_new(ast_ctx(var), nullptr, TYPE_VECTOR);
- varargs->expression.count = 0;
- util_snprintf(name, sizeof(name), "%s##va##SET", var->name);
+ ast_value *varargs = new ast_value(var->m_context, "reserved:va_args", TYPE_ARRAY);
+ varargs->m_flags |= AST_FLAG_IS_VARARG;
+ varargs->m_next = new ast_value(var->m_context, "", TYPE_VECTOR);
+ varargs->m_count = 0;
+ util_snprintf(name, sizeof(name), "%s##va##SET", var->m_name.c_str());
if (!parser_create_array_setter_proto(parser, varargs, name)) {
- ast_delete(varargs);
- ast_block_delete(block);
+ delete varargs;
+ delete block;
goto enderrfn;
}
- util_snprintf(name, sizeof(name), "%s##va##GET", var->name);
- if (!parser_create_array_getter_proto(parser, varargs, varargs->expression.next, name)) {
- ast_delete(varargs);
- ast_block_delete(block);
+ util_snprintf(name, sizeof(name), "%s##va##GET", var->m_name.c_str());
+ if (!parser_create_array_getter_proto(parser, varargs, varargs->m_next, name)) {
+ delete varargs;
+ delete block;
goto enderrfn;
}
- func->varargs = varargs;
- func->fixedparams = (ast_value*)parser->m_fold.constgen_float(var->expression.params.size(), false);
+ func->m_varargs.reset(varargs);
+ func->m_fixedparams = (ast_value*)parser->m_fold.constgen_float(var->m_type_params.size(), false);
}
parser->function = func;
if (!parse_block_into(parser, block)) {
- ast_block_delete(block);
+ delete block;
goto enderrfn;
}
- func->blocks.push_back(block);
+ func->m_blocks.emplace_back(block);
parser->function = old;
if (!parser_leaveblock(parser))
retval = false;
- if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
+ if (parser->variables.size() != PARSER_HT_LOCALS) {
parseerror(parser, "internal error: local scopes left");
retval = false;
}
enderrfn:
(void)!parser_leaveblock(parser);
parser->functions.pop_back();
- ast_function_delete(func);
- var->constval.vfunc = nullptr;
+ delete func;
+ var->m_constval.vfunc = nullptr;
enderr:
parser->function = old;
ast_ifthen *ifthen;
ast_binary *cmp;
- lex_ctx_t ctx = ast_ctx(array);
+ lex_ctx_t ctx = array->m_context;
if (!left || !right) {
- if (left) ast_delete(left);
- if (right) ast_delete(right);
+ if (left) delete left;
+ if (right) delete right;
return nullptr;
}
- cmp = ast_binary_new(ctx, INSTR_LT,
- (ast_expression*)index,
- (ast_expression*)parser->m_fold.constgen_float(middle, false));
+ cmp = new ast_binary(ctx, INSTR_LT,
+ index,
+ parser->m_fold.constgen_float(middle, false));
if (!cmp) {
- ast_delete(left);
- ast_delete(right);
+ delete left;
+ delete right;
parseerror(parser, "internal error: failed to create comparison for array setter");
return nullptr;
}
- ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
+ ifthen = new ast_ifthen(ctx, cmp, left, right);
if (!ifthen) {
- ast_delete(cmp); /* will delete left and right */
+ delete cmp; /* will delete left and right */
parseerror(parser, "internal error: failed to create conditional jump for array setter");
return nullptr;
}
- return (ast_expression*)ifthen;
+ return ifthen;
}
static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
{
- lex_ctx_t ctx = ast_ctx(array);
+ lex_ctx_t ctx = array->m_context;
if (from+1 == afterend) {
/* set this value */
ast_return *ret;
ast_array_index *subscript;
ast_store *st;
- int assignop = type_store_instr[value->expression.vtype];
+ int assignop = type_store_instr[value->m_vtype];
- if (value->expression.vtype == TYPE_FIELD && value->expression.next->vtype == TYPE_VECTOR)
+ if (value->m_vtype == TYPE_FIELD && value->m_next->m_vtype == TYPE_VECTOR)
assignop = INSTR_STORE_V;
- subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser->m_fold.constgen_float(from, false));
+ subscript = ast_array_index::make(ctx, array, parser->m_fold.constgen_float(from, false));
if (!subscript)
return nullptr;
- st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
+ st = new ast_store(ctx, assignop, subscript, value);
if (!st) {
- ast_delete(subscript);
+ delete subscript;
return nullptr;
}
- block = ast_block_new(ctx);
+ block = new ast_block(ctx);
if (!block) {
- ast_delete(st);
+ delete st;
return nullptr;
}
- if (!ast_block_add_expr(block, (ast_expression*)st)) {
- ast_delete(block);
+ if (!block->addExpr(st)) {
+ delete block;
return nullptr;
}
- ret = ast_return_new(ctx, nullptr);
+ ret = new ast_return(ctx, nullptr);
if (!ret) {
- ast_delete(block);
+ delete block;
return nullptr;
}
- if (!ast_block_add_expr(block, (ast_expression*)ret)) {
- ast_delete(block);
+ if (!block->addExpr(ret)) {
+ delete block;
return nullptr;
}
- return (ast_expression*)block;
+ return block;
} else {
ast_expression *left, *right;
size_t diff = afterend - from;
size_t from,
size_t afterend)
{
- lex_ctx_t ctx = ast_ctx(array);
+ lex_ctx_t ctx = array->m_context;
if (from+1 == afterend) {
/* set this value */
ast_entfield *entfield;
ast_array_index *subscript;
ast_store *st;
- int assignop = type_storep_instr[value->expression.vtype];
+ int assignop = type_storep_instr[value->m_vtype];
- if (value->expression.vtype == TYPE_FIELD && value->expression.next->vtype == TYPE_VECTOR)
+ if (value->m_vtype == TYPE_FIELD && value->m_next->m_vtype == TYPE_VECTOR)
assignop = INSTR_STOREP_V;
- subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser->m_fold.constgen_float(from, false));
+ subscript = ast_array_index::make(ctx, array, parser->m_fold.constgen_float(from, false));
if (!subscript)
return nullptr;
- subscript->expression.next = ast_type_copy(ast_ctx(subscript), (ast_expression*)subscript);
- subscript->expression.vtype = TYPE_FIELD;
+ subscript->m_next = new ast_expression(ast_copy_type, subscript->m_context, *subscript);
+ subscript->m_vtype = TYPE_FIELD;
- entfield = ast_entfield_new_force(ctx,
- (ast_expression*)entity,
- (ast_expression*)subscript,
- (ast_expression*)subscript);
+ entfield = new ast_entfield(ctx, entity, subscript, subscript);
if (!entfield) {
- ast_delete(subscript);
+ delete subscript;
return nullptr;
}
- st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
+ st = new ast_store(ctx, assignop, entfield, value);
if (!st) {
- ast_delete(entfield);
+ delete entfield;
return nullptr;
}
- block = ast_block_new(ctx);
+ block = new ast_block(ctx);
if (!block) {
- ast_delete(st);
+ delete st;
return nullptr;
}
- if (!ast_block_add_expr(block, (ast_expression*)st)) {
- ast_delete(block);
+ if (!block->addExpr(st)) {
+ delete block;
return nullptr;
}
- ret = ast_return_new(ctx, nullptr);
+ ret = new ast_return(ctx, nullptr);
if (!ret) {
- ast_delete(block);
+ delete block;
return nullptr;
}
- if (!ast_block_add_expr(block, (ast_expression*)ret)) {
- ast_delete(block);
+ if (!block->addExpr(ret)) {
+ delete block;
return nullptr;
}
- return (ast_expression*)block;
+ return block;
} else {
ast_expression *left, *right;
size_t diff = afterend - from;
static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
{
- lex_ctx_t ctx = ast_ctx(array);
+ lex_ctx_t ctx = array->m_context;
if (from+1 == afterend) {
ast_return *ret;
ast_array_index *subscript;
- subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser->m_fold.constgen_float(from, false));
+ subscript = ast_array_index::make(ctx, array, parser->m_fold.constgen_float(from, false));
if (!subscript)
return nullptr;
- ret = ast_return_new(ctx, (ast_expression*)subscript);
+ ret = new ast_return(ctx, subscript);
if (!ret) {
- ast_delete(subscript);
+ delete subscript;
return nullptr;
}
- return (ast_expression*)ret;
+ return ret;
} else {
ast_expression *left, *right;
size_t diff = afterend - from;
ast_value *fval = nullptr;
ast_block *body = nullptr;
- fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
+ fval = new ast_value(array->m_context, funcname, TYPE_FUNCTION);
if (!fval) {
parseerror(parser, "failed to create accessor function value");
return false;
}
- fval->expression.flags &= ~(AST_FLAG_COVERAGE_MASK);
+ fval->m_flags &= ~(AST_FLAG_COVERAGE_MASK);
- func = ast_function_new(ast_ctx(array), funcname, fval);
+ func = ast_function::make(array->m_context, funcname, fval);
if (!func) {
- ast_delete(fval);
+ delete fval;
parseerror(parser, "failed to create accessor function node");
return false;
}
- body = ast_block_new(ast_ctx(array));
+ body = new ast_block(array->m_context);
if (!body) {
parseerror(parser, "failed to create block for array accessor");
- ast_delete(fval);
- ast_delete(func);
+ delete fval;
+ delete func;
return false;
}
- func->blocks.push_back(body);
+ func->m_blocks.emplace_back(body);
*out = fval;
parser->accessors.push_back(fval);
ast_function *func;
ast_value *fval;
- if (!ast_istype(array->expression.next, ast_value)) {
+ if (!ast_istype(array->m_next, ast_value)) {
parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
return nullptr;
}
if (!parser_create_array_accessor(parser, array, funcname, &fval))
return nullptr;
- func = fval->constval.vfunc;
- fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
+ func = fval->m_constval.vfunc;
+ fval->m_next = new ast_value(array->m_context, "<void>", TYPE_VOID);
- index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
- value = ast_value_copy((ast_value*)array->expression.next);
+ index = new ast_value(array->m_context, "index", TYPE_FLOAT);
+ value = new ast_value(ast_copy_type, *(ast_value*)array->m_next);
if (!index || !value) {
parseerror(parser, "failed to create locals for array accessor");
goto cleanup;
}
- (void)!ast_value_set_name(value, "value"); /* not important */
- fval->expression.params.push_back(index);
- fval->expression.params.push_back(value);
+ value->m_name = "value"; // not important
+ fval->m_type_params.emplace_back(index);
+ fval->m_type_params.emplace_back(value);
- array->setter = fval;
+ array->m_setter = fval;
return fval;
cleanup:
- if (index) ast_delete(index);
- if (value) ast_delete(value);
- ast_delete(func);
- ast_delete(fval);
+ if (index) delete index;
+ if (value) delete value;
+ delete func;
+ delete fval;
return nullptr;
}
{
ast_expression *root = nullptr;
root = array_setter_node(parser, array,
- array->setter->expression.params[0],
- array->setter->expression.params[1],
- 0, array->expression.count);
+ array->m_setter->m_type_params[0].get(),
+ array->m_setter->m_type_params[1].get(),
+ 0, array->m_count);
if (!root) {
parseerror(parser, "failed to build accessor search tree");
return false;
}
- if (!ast_block_add_expr(array->setter->constval.vfunc->blocks[0], root)) {
- ast_delete(root);
+ if (!array->m_setter->m_constval.vfunc->m_blocks[0].get()->addExpr(root)) {
+ delete root;
return false;
}
return true;
ast_function *func;
ast_value *fval;
- if (!ast_istype(array->expression.next, ast_value)) {
+ if (!ast_istype(array->m_next, ast_value)) {
parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
return false;
}
if (!parser_create_array_accessor(parser, array, funcname, &fval))
return false;
- func = fval->constval.vfunc;
- fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
+ func = fval->m_constval.vfunc;
+ fval->m_next = new ast_value(array->m_context, "<void>", TYPE_VOID);
- entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
- index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
- value = ast_value_copy((ast_value*)array->expression.next);
+ entity = new ast_value(array->m_context, "entity", TYPE_ENTITY);
+ index = new ast_value(array->m_context, "index", TYPE_FLOAT);
+ value = new ast_value(ast_copy_type, *(ast_value*)array->m_next);
if (!entity || !index || !value) {
parseerror(parser, "failed to create locals for array accessor");
goto cleanup;
}
- (void)!ast_value_set_name(value, "value"); /* not important */
- fval->expression.params.push_back(entity);
- fval->expression.params.push_back(index);
- fval->expression.params.push_back(value);
+ value->m_name = "value"; // not important
+ fval->m_type_params.emplace_back(entity);
+ fval->m_type_params.emplace_back(index);
+ fval->m_type_params.emplace_back(value);
- root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
+ root = array_field_setter_node(parser, array, entity, index, value, 0, array->m_count);
if (!root) {
parseerror(parser, "failed to build accessor search tree");
goto cleanup;
}
- array->setter = fval;
- return ast_block_add_expr(func->blocks[0], root);
+ array->m_setter = fval;
+ return func->m_blocks[0].get()->addExpr(root);
cleanup:
- if (entity) ast_delete(entity);
- if (index) ast_delete(index);
- if (value) ast_delete(value);
- if (root) ast_delete(root);
- ast_delete(func);
- ast_delete(fval);
+ if (entity) delete entity;
+ if (index) delete index;
+ if (value) delete value;
+ if (root) delete root;
+ delete func;
+ delete fval;
return false;
}
ast_value *fval;
ast_function *func;
- /* NOTE: checking array->expression.next rather than elemtype since
+ /* NOTE: checking array->m_next rather than elemtype since
* for fields elemtype is a temporary fieldtype.
*/
- if (!ast_istype(array->expression.next, ast_value)) {
+ if (!ast_istype(array->m_next, ast_value)) {
parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
return nullptr;
}
if (!parser_create_array_accessor(parser, array, funcname, &fval))
return nullptr;
- func = fval->constval.vfunc;
- fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
+ func = fval->m_constval.vfunc;
+ fval->m_next = new ast_expression(ast_copy_type, array->m_context, *elemtype);
- index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
+ index = new ast_value(array->m_context, "index", TYPE_FLOAT);
if (!index) {
parseerror(parser, "failed to create locals for array accessor");
goto cleanup;
}
- fval->expression.params.push_back(index);
+ fval->m_type_params.emplace_back(index);
- array->getter = fval;
+ array->m_getter = fval;
return fval;
cleanup:
- if (index) ast_delete(index);
- ast_delete(func);
- ast_delete(fval);
+ if (index) delete index;
+ delete func;
+ delete fval;
return nullptr;
}
{
ast_expression *root = nullptr;
- root = array_getter_node(parser, array, array->getter->expression.params[0], 0, array->expression.count);
+ root = array_getter_node(parser, array, array->m_getter->m_type_params[0].get(), 0, array->m_count);
if (!root) {
parseerror(parser, "failed to build accessor search tree");
return false;
}
- if (!ast_block_add_expr(array->getter->constval.vfunc->blocks[0], root)) {
- ast_delete(root);
+ if (!array->m_getter->m_constval.vfunc->m_blocks[0].get()->addExpr(root)) {
+ delete root;
return false;
}
return true;
static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
{
lex_ctx_t ctx = parser_ctx(parser);
- std::vector<ast_value *> params;
+ std::vector<std::unique_ptr<ast_value>> params;
ast_value *fval;
bool first = true;
bool variadic = false;
/* for the sake of less code we parse-in in this function */
if (!parser_next(parser)) {
- ast_delete(var);
+ delete var;
parseerror(parser, "expected parameter list");
return nullptr;
}
}
}
} else {
- params.push_back(param);
- if (param->expression.vtype >= TYPE_VARIANT) {
+ params.emplace_back(param);
+ if (param->m_vtype >= TYPE_VARIANT) {
char tname[1024]; /* typename is reserved in C++ */
- ast_type_to_string((ast_expression*)param, tname, sizeof(tname));
+ ast_type_to_string(param, tname, sizeof(tname));
parseerror(parser, "type not supported as part of a parameter list: %s", tname);
goto on_error;
}
/* type-restricted varargs */
if (parser->tok == TOKEN_DOTS) {
variadic = true;
- varparam = params.back();
+ varparam = params.back().release();
params.pop_back();
if (!parser_next(parser) || (parser->tok != ')' && parser->tok != TOKEN_IDENT)) {
parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
}
if (parser->tok == TOKEN_IDENT) {
argcounter = util_strdup(parser_tokval(parser));
- ast_value_set_name(param, argcounter);
+ param->m_name = argcounter;
if (!parser_next(parser) || parser->tok != ')') {
parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
goto on_error;
}
}
}
- if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC && param->name[0] == '<') {
+ if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC && param->m_name[0] == '<') {
parseerror(parser, "parameter name omitted");
goto on_error;
}
}
}
- if (params.size() == 1 && params[0]->expression.vtype == TYPE_VOID)
+ if (params.size() == 1 && params[0]->m_vtype == TYPE_VOID)
params.clear();
/* sanity check */
}
/* now turn 'var' into a function type */
- fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
- fval->expression.next = (ast_expression*)var;
+ fval = new ast_value(ctx, "<type()>", TYPE_FUNCTION);
+ fval->m_next = var;
if (variadic)
- fval->expression.flags |= AST_FLAG_VARIADIC;
+ fval->m_flags |= AST_FLAG_VARIADIC;
var = fval;
- var->expression.params = params;
- var->expression.varparam = (ast_expression*)varparam;
- var->argcounter = argcounter;
+ var->m_type_params = move(params);
+ var->m_varparam = varparam;
+ var->m_argcounter = argcounter;
return var;
if (argcounter)
mem_d(argcounter);
if (varparam)
- ast_delete(varparam);
- ast_delete(var);
- for (auto &it : params)
- ast_delete(it);
+ delete varparam;
+ delete var;
return nullptr;
}
ctx = parser_ctx(parser);
if (!parser_next(parser)) {
- ast_delete(var);
+ delete var;
parseerror(parser, "expected array-size");
return nullptr;
}
if (!cexp || !ast_istype(cexp, ast_value)) {
if (cexp)
ast_unref(cexp);
- ast_delete(var);
+ delete var;
parseerror(parser, "expected array-size as constant positive integer");
return nullptr;
}
cval = nullptr;
}
- tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
- tmp->expression.next = (ast_expression*)var;
+ tmp = new ast_value(ctx, "<type[]>", TYPE_ARRAY);
+ tmp->m_next = var;
var = tmp;
if (cval) {
- if (cval->expression.vtype == TYPE_INTEGER)
- tmp->expression.count = cval->constval.vint;
- else if (cval->expression.vtype == TYPE_FLOAT)
- tmp->expression.count = cval->constval.vfloat;
+ if (cval->m_vtype == TYPE_INTEGER)
+ tmp->m_count = cval->m_constval.vint;
+ else if (cval->m_vtype == TYPE_FLOAT)
+ tmp->m_count = cval->m_constval.vfloat;
else {
ast_unref(cexp);
- ast_delete(var);
+ delete var;
parseerror(parser, "array-size must be a positive integer constant");
return nullptr;
}
ast_unref(cexp);
} else {
- var->expression.count = -1;
- var->expression.flags |= AST_FLAG_ARRAY_INIT;
+ var->m_count = -1;
+ var->m_flags |= AST_FLAG_ARRAY_INIT;
}
if (parser->tok != ']') {
- ast_delete(var);
+ delete var;
parseerror(parser, "expected ']' after array-size");
return nullptr;
}
if (!parser_next(parser)) {
- ast_delete(var);
+ delete var;
parseerror(parser, "error after parsing array size");
return nullptr;
}
/* generate the basic type value */
if (cached_typedef) {
- var = ast_value_copy(cached_typedef);
- ast_value_set_name(var, "<type(from_def)>");
+ var = new ast_value(ast_copy_type, *cached_typedef);
+ var->m_name = "<type(from_def)>";
} else
- var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
+ var = new ast_value(ctx, "<type>", parser_token(parser)->constval.t);
for (; morefields; --morefields) {
- tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
- tmp->expression.next = (ast_expression*)var;
+ tmp = new ast_value(ctx, "<.type>", TYPE_FIELD);
+ tmp->m_next = var;
var = tmp;
}
/* parse on */
if (!parser_next(parser)) {
- ast_delete(var);
+ delete var;
parseerror(parser, "parse error after typename");
return nullptr;
}
/* store the base if requested */
if (storebase) {
- *storebase = ast_value_copy(var);
+ *storebase = new ast_value(ast_copy_type, *var);
if (isfield) {
- tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
- tmp->expression.next = (ast_expression*)*storebase;
+ tmp = new ast_value(ctx, "<type:f>", TYPE_FIELD);
+ tmp->m_next = *storebase;
*storebase = tmp;
}
}
/* parse on */
if (!parser_next(parser)) {
- ast_delete(var);
+ delete var;
mem_d(name);
parseerror(parser, "error after variable or field declaration");
return nullptr;
/* This is the point where we can turn it into a field */
if (isfield) {
/* turn it into a field if desired */
- tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
- tmp->expression.next = (ast_expression*)var;
+ tmp = new ast_value(ctx, "<type:f>", TYPE_FIELD);
+ tmp->m_next = var;
var = tmp;
}
/* finally name it */
if (name) {
- if (!ast_value_set_name(var, name)) {
- ast_delete(var);
- mem_d(name);
- parseerror(parser, "internal error: failed to set name");
- return nullptr;
- }
- /* free the name, ast_value_set_name duplicates */
+ var->m_name = name;
+ // free the name, ast_value_set_name duplicates
mem_d(name);
}
if (!typevar)
return false;
- /* while parsing types, the ast_value's get named '<something>' */
- if (!typevar->name || typevar->name[0] == '<') {
+ // while parsing types, the ast_value's get named '<something>'
+ if (!typevar->m_name.length() || typevar->m_name[0] == '<') {
parseerror(parser, "missing name in typedef");
- ast_delete(typevar);
+ delete typevar;
return false;
}
- if ( (old = parser_find_var(parser, typevar->name)) ) {
+ if ( (old = parser_find_var(parser, typevar->m_name)) ) {
parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
" -> `%s` has been declared here: %s:%i",
- typevar->name, ast_ctx(old).file, ast_ctx(old).line);
- ast_delete(typevar);
+ typevar->m_name, old->m_context.file, old->m_context.line);
+ delete typevar;
return false;
}
- if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
+ if ( (oldtype = parser_find_typedef(parser, typevar->m_name, parser->_blocktypedefs.back())) ) {
parseerror(parser, "type `%s` has already been declared here: %s:%i",
- typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
- ast_delete(typevar);
+ typevar->m_name, oldtype->m_context.file, oldtype->m_context.line);
+ delete typevar;
return false;
}
- vec_push(parser->_typedefs, typevar);
- util_htset(vec_last(parser->typedefs), typevar->name, typevar);
+ parser->_typedefs.emplace_back(typevar);
+ util_htset(parser->typedefs.back(), typevar->m_name.c_str(), typevar);
if (parser->tok != ';') {
parseerror(parser, "expected semicolon after typedef");
static bool parser_check_qualifiers(parser_t *parser, const ast_value *var, const ast_value *proto)
{
bool av, ao;
- if (proto->cvq != var->cvq) {
- if (!(proto->cvq == CV_CONST && var->cvq == CV_NONE &&
+ if (proto->m_cvq != var->m_cvq) {
+ if (!(proto->m_cvq == CV_CONST && var->m_cvq == CV_NONE &&
!OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
parser->tok == '='))
{
return !parsewarning(parser, WARN_DIFFERENT_QUALIFIERS,
"`%s` declared with different qualifiers: %s\n"
" -> previous declaration here: %s:%i uses %s",
- var->name, cvq_to_str(var->cvq),
- ast_ctx(proto).file, ast_ctx(proto).line,
- cvq_to_str(proto->cvq));
+ var->m_name, cvq_to_str(var->m_cvq),
+ proto->m_context.file, proto->m_context.line,
+ cvq_to_str(proto->m_cvq));
}
}
- av = (var ->expression.flags & AST_FLAG_NORETURN);
- ao = (proto->expression.flags & AST_FLAG_NORETURN);
+ av = (var ->m_flags & AST_FLAG_NORETURN);
+ ao = (proto->m_flags & AST_FLAG_NORETURN);
if (!av != !ao) {
return !parsewarning(parser, WARN_DIFFERENT_ATTRIBUTES,
"`%s` declared with different attributes%s\n"
" -> previous declaration here: %s:%i",
- var->name, (av ? ": noreturn" : ""),
- ast_ctx(proto).file, ast_ctx(proto).line,
+ var->m_name, (av ? ": noreturn" : ""),
+ proto->m_context.file, proto->m_context.line,
(ao ? ": noreturn" : ""));
}
return true;
static bool create_array_accessors(parser_t *parser, ast_value *var)
{
char name[1024];
- util_snprintf(name, sizeof(name), "%s##SET", var->name);
+ util_snprintf(name, sizeof(name), "%s##SET", var->m_name.c_str());
if (!parser_create_array_setter(parser, var, name))
return false;
- util_snprintf(name, sizeof(name), "%s##GET", var->name);
- if (!parser_create_array_getter(parser, var, var->expression.next, name))
+ util_snprintf(name, sizeof(name), "%s##GET", var->m_name.c_str());
+ if (!parser_create_array_getter(parser, var, var->m_next, name))
return false;
return true;
}
static bool parse_array(parser_t *parser, ast_value *array)
{
size_t i;
- if (array->initlist.size()) {
+ if (array->m_initlist.size()) {
parseerror(parser, "array already initialized elsewhere");
return false;
}
ast_value *v = (ast_value*)parse_expression_leave(parser, true, false, false);
if (!v)
return false;
- if (!ast_istype(v, ast_value) || !v->hasvalue || v->cvq != CV_CONST) {
+ if (!ast_istype(v, ast_value) || !v->m_hasvalue || v->m_cvq != CV_CONST) {
ast_unref(v);
parseerror(parser, "initializing element must be a compile time constant");
return false;
}
- array->initlist.push_back(v->constval);
- if (v->expression.vtype == TYPE_STRING) {
- array->initlist[i].vstring = util_strdupe(array->initlist[i].vstring);
+ array->m_initlist.push_back(v->m_constval);
+ if (v->m_vtype == TYPE_STRING) {
+ array->m_initlist[i].vstring = util_strdupe(array->m_initlist[i].vstring);
++i;
}
ast_unref(v);
}
*/
- if (array->expression.flags & AST_FLAG_ARRAY_INIT) {
- if (array->expression.count != (size_t)-1) {
+ if (array->m_flags & AST_FLAG_ARRAY_INIT) {
+ if (array->m_count != (size_t)-1) {
parseerror(parser, "array `%s' has already been initialized with %u elements",
- array->name, (unsigned)array->expression.count);
+ array->m_name, (unsigned)array->m_count);
}
- array->expression.count = array->initlist.size();
+ array->m_count = array->m_initlist.size();
if (!create_array_accessors(parser, array))
return false;
}
var = parse_typename(parser, &basetype, cached_typedef, nullptr);
if (!var) {
if (basetype)
- ast_delete(basetype);
+ delete basetype;
return false;
}
/* while parsing types, the ast_value's get named '<something>' */
- if (!var->name || var->name[0] == '<') {
+ if (!var->m_name.length() || var->m_name[0] == '<') {
parseerror(parser, "declaration does not declare anything");
if (basetype)
- ast_delete(basetype);
+ delete basetype;
return false;
}
}
}
- var->cvq = qualifier;
+ var->m_cvq = qualifier;
if (qflags & AST_FLAG_COVERAGE) /* specified in QC, drop our default */
- var->expression.flags &= ~(AST_FLAG_COVERAGE_MASK);
- var->expression.flags |= qflags;
+ var->m_flags &= ~(AST_FLAG_COVERAGE_MASK);
+ var->m_flags |= qflags;
/*
* store the vstring back to var for alias and
* deprecation messages.
*/
- if (var->expression.flags & AST_FLAG_DEPRECATED ||
- var->expression.flags & AST_FLAG_ALIAS)
- var->desc = vstring;
+ if (var->m_flags & AST_FLAG_DEPRECATED ||
+ var->m_flags & AST_FLAG_ALIAS)
+ var->m_desc = vstring;
- if (parser_find_global(parser, var->name) && var->expression.flags & AST_FLAG_ALIAS) {
+ if (parser_find_global(parser, var->m_name) && var->m_flags & AST_FLAG_ALIAS) {
parseerror(parser, "function aliases cannot be forward declared");
retval = false;
goto cleanup;
* Also: if there was a prototype, `var` will be deleted and set to `proto` which
* is then filled with the previous definition and the parameter-names replaced.
*/
- if (!strcmp(var->name, "nil")) {
+ if (var->m_name == "nil") {
if (OPTS_FLAG(UNTYPED_NIL)) {
if (!localblock || !OPTS_FLAG(PERMISSIVE))
parseerror(parser, "name `nil` not allowed (try -fpermissive)");
if (!localblock) {
/* Deal with end_sys_ vars */
was_end = false;
- if (!strcmp(var->name, "end_sys_globals")) {
- var->uses++;
+ if (var->m_name == "end_sys_globals") {
+ var->m_flags |= AST_FLAG_NOREF;
parser->crc_globals = parser->globals.size();
was_end = true;
}
- else if (!strcmp(var->name, "end_sys_fields")) {
- var->uses++;
+ else if (var->m_name == "end_sys_fields") {
+ var->m_flags |= AST_FLAG_NOREF;
parser->crc_fields = parser->fields.size();
was_end = true;
}
- if (was_end && var->expression.vtype == TYPE_FIELD) {
+ if (was_end && var->m_vtype == TYPE_FIELD) {
if (parsewarning(parser, WARN_END_SYS_FIELDS,
"global '%s' hint should not be a field",
parser_tokval(parser)))
}
}
- if (!nofields && var->expression.vtype == TYPE_FIELD)
+ if (!nofields && var->m_vtype == TYPE_FIELD)
{
/* deal with field declarations */
- old = parser_find_field(parser, var->name);
+ old = parser_find_field(parser, var->m_name);
if (old) {
if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
- var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
+ var->m_name, old->m_context.file, (int)old->m_context.line))
{
retval = false;
goto cleanup;
}
- ast_delete(var);
+ delete var;
var = nullptr;
goto skipvar;
/*
parseerror(parser, "field `%s` already declared here: %s:%i",
- var->name, ast_ctx(old).file, ast_ctx(old).line);
+ var->m_name, old->m_context.file, old->m_context.line);
retval = false;
goto cleanup;
*/
}
if ((OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC || OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) &&
- (old = parser_find_global(parser, var->name)))
+ (old = parser_find_global(parser, var->m_name)))
{
parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
parseerror(parser, "field `%s` already declared here: %s:%i",
- var->name, ast_ctx(old).file, ast_ctx(old).line);
+ var->m_name, old->m_context.file, old->m_context.line);
retval = false;
goto cleanup;
}
else
{
/* deal with other globals */
- old = parser_find_global(parser, var->name);
- if (old && var->expression.vtype == TYPE_FUNCTION && old->vtype == TYPE_FUNCTION)
+ old = parser_find_global(parser, var->m_name);
+ if (old && var->m_vtype == TYPE_FUNCTION && old->m_vtype == TYPE_FUNCTION)
{
/* This is a function which had a prototype */
if (!ast_istype(old, ast_value)) {
goto cleanup;
}
proto = (ast_value*)old;
- proto->desc = var->desc;
- if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
+ proto->m_desc = var->m_desc;
+ if (!proto->compareType(*var)) {
parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
- proto->name,
- ast_ctx(proto).file, ast_ctx(proto).line);
+ proto->m_name,
+ proto->m_context.file, proto->m_context.line);
retval = false;
goto cleanup;
}
/* we need the new parameter-names */
- for (i = 0; i < proto->expression.params.size(); ++i)
- ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
+ for (i = 0; i < proto->m_type_params.size(); ++i)
+ proto->m_type_params[i]->m_name = var->m_type_params[i]->m_name;
if (!parser_check_qualifiers(parser, var, proto)) {
retval = false;
- if (proto->desc)
- mem_d(proto->desc);
proto = nullptr;
goto cleanup;
}
- proto->expression.flags |= var->expression.flags;
- ast_delete(var);
+ proto->m_flags |= var->m_flags;
+ delete var;
var = proto;
}
else
if (old) {
if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
"global `%s` already declared here: %s:%i",
- var->name, ast_ctx(old).file, ast_ctx(old).line))
+ var->m_name, old->m_context.file, old->m_context.line))
{
retval = false;
goto cleanup;
}
- if (old->flags & AST_FLAG_FINAL_DECL) {
+ if (old->m_flags & AST_FLAG_FINAL_DECL) {
parseerror(parser, "cannot redeclare variable `%s`, declared final here: %s:%i",
- var->name, ast_ctx(old).file, ast_ctx(old).line);
+ var->m_name, old->m_context.file, old->m_context.line);
retval = false;
goto cleanup;
}
proto = nullptr;
goto cleanup;
}
- proto->expression.flags |= var->expression.flags;
+ proto->m_flags |= var->m_flags;
/* copy the context for finals,
* so the error can show where it was actually made 'final'
*/
- if (proto->expression.flags & AST_FLAG_FINAL_DECL)
- ast_ctx(old) = ast_ctx(var);
- ast_delete(var);
+ if (proto->m_flags & AST_FLAG_FINAL_DECL)
+ old->m_context = var->m_context;
+ delete var;
var = proto;
}
if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC &&
- (old = parser_find_field(parser, var->name)))
+ (old = parser_find_field(parser, var->m_name)))
{
parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
parseerror(parser, "global `%s` already declared here: %s:%i",
- var->name, ast_ctx(old).file, ast_ctx(old).line);
+ var->m_name, old->m_context.file, old->m_context.line);
retval = false;
goto cleanup;
}
}
else /* it's not a global */
{
- old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
+ old = parser_find_local(parser, var->m_name, parser->variables.size()-1, &isparam);
if (old && !isparam) {
parseerror(parser, "local `%s` already declared here: %s:%i",
- var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
+ var->m_name, old->m_context.file, (int)old->m_context.line);
retval = false;
goto cleanup;
}
/* doing this here as the above is just for a single scope */
- old = parser_find_local(parser, var->name, 0, &isparam);
+ old = parser_find_local(parser, var->m_name, 0, &isparam);
if (old && isparam) {
if (parsewarning(parser, WARN_LOCAL_SHADOWS,
- "local `%s` is shadowing a parameter", var->name))
+ "local `%s` is shadowing a parameter", var->m_name))
{
parseerror(parser, "local `%s` already declared here: %s:%i",
- var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
+ var->m_name, old->m_context.file, (int)old->m_context.line);
retval = false;
goto cleanup;
}
if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
- ast_delete(var);
+ delete var;
if (ast_istype(old, ast_value))
var = proto = (ast_value*)old;
else {
}
}
- /* in a noref section we simply bump the usecount */
if (noref || parser->noref)
- var->uses++;
+ var->m_flags |= AST_FLAG_NOREF;
/* Part 2:
* Create the global/local, and deal with vector types.
*/
if (!proto) {
- if (var->expression.vtype == TYPE_VECTOR)
+ if (var->m_vtype == TYPE_VECTOR)
isvector = true;
- else if (var->expression.vtype == TYPE_FIELD &&
- var->expression.next->vtype == TYPE_VECTOR)
+ else if (var->m_vtype == TYPE_FIELD &&
+ var->m_next->m_vtype == TYPE_VECTOR)
isvector = true;
if (isvector) {
if (!localblock) {
/* deal with global variables, fields, functions */
- if (!nofields && var->expression.vtype == TYPE_FIELD && parser->tok != '=') {
- var->isfield = true;
- parser->fields.push_back((ast_expression*)var);
- util_htset(parser->htfields, var->name, var);
+ if (!nofields && var->m_vtype == TYPE_FIELD && parser->tok != '=') {
+ var->m_isfield = true;
+ parser->fields.push_back(var);
+ util_htset(parser->htfields, var->m_name.c_str(), var);
if (isvector) {
for (i = 0; i < 3; ++i) {
- parser->fields.push_back((ast_expression*)me[i]);
- util_htset(parser->htfields, me[i]->name, me[i]);
+ parser->fields.push_back(me[i]);
+ util_htset(parser->htfields, me[i]->m_name.c_str(), me[i]);
}
}
}
else {
- if (!(var->expression.flags & AST_FLAG_ALIAS)) {
- parser_addglobal(parser, var->name, (ast_expression*)var);
+ if (!(var->m_flags & AST_FLAG_ALIAS)) {
+ parser_addglobal(parser, var->m_name, var);
if (isvector) {
for (i = 0; i < 3; ++i) {
- parser_addglobal(parser, me[i]->name, (ast_expression*)me[i]);
+ parser_addglobal(parser, me[i]->m_name.c_str(), me[i]);
}
}
} else {
- ast_expression *find = parser_find_global(parser, var->desc);
+ ast_expression *find = parser_find_global(parser, var->m_desc);
if (!find) {
- compile_error(parser_ctx(parser), "undeclared variable `%s` for alias `%s`", var->desc, var->name);
+ compile_error(parser_ctx(parser), "undeclared variable `%s` for alias `%s`", var->m_desc, var->m_name);
return false;
}
- if (!ast_compare_type((ast_expression*)var, find)) {
+ if (!var->compareType(*find)) {
char ty1[1024];
char ty2[1024];
- ast_type_to_string(find, ty1, sizeof(ty1));
- ast_type_to_string((ast_expression*)var, ty2, sizeof(ty2));
+ ast_type_to_string(find, ty1, sizeof(ty1));
+ ast_type_to_string(var, ty2, sizeof(ty2));
compile_error(parser_ctx(parser), "incompatible types `%s` and `%s` for alias `%s`",
- ty1, ty2, var->name
+ ty1, ty2, var->m_name
);
return false;
}
- util_htset(parser->aliases, var->name, find);
+ util_htset(parser->aliases, var->m_name.c_str(), find);
/* generate aliases for vector components */
if (isvector) {
char *buffer[3];
- util_asprintf(&buffer[0], "%s_x", var->desc);
- util_asprintf(&buffer[1], "%s_y", var->desc);
- util_asprintf(&buffer[2], "%s_z", var->desc);
+ util_asprintf(&buffer[0], "%s_x", var->m_desc.c_str());
+ util_asprintf(&buffer[1], "%s_y", var->m_desc.c_str());
+ util_asprintf(&buffer[2], "%s_z", var->m_desc.c_str());
- util_htset(parser->aliases, me[0]->name, parser_find_global(parser, buffer[0]));
- util_htset(parser->aliases, me[1]->name, parser_find_global(parser, buffer[1]));
- util_htset(parser->aliases, me[2]->name, parser_find_global(parser, buffer[2]));
+ util_htset(parser->aliases, me[0]->m_name.c_str(), parser_find_global(parser, buffer[0]));
+ util_htset(parser->aliases, me[1]->m_name.c_str(), parser_find_global(parser, buffer[1]));
+ util_htset(parser->aliases, me[2]->m_name.c_str(), parser_find_global(parser, buffer[2]));
mem_d(buffer[0]);
mem_d(buffer[1]);
}
} else {
if (is_static) {
- /* a static adds itself to be generated like any other global
- * but is added to the local namespace instead
- */
- char *defname = nullptr;
- size_t prefix_len, ln;
+ // a static adds itself to be generated like any other global
+ // but is added to the local namespace instead
+ std::string defname;
+ size_t prefix_len;
size_t sn, sn_size;
- ln = strlen(parser->function->name);
- vec_append(defname, ln, parser->function->name);
+ defname = parser->function->m_name;
+ defname.append(2, ':');
- vec_append(defname, 2, "::");
- /* remember the length up to here */
- prefix_len = vec_size(defname);
+ // remember the length up to here
+ prefix_len = defname.length();
- /* Add it to the local scope */
- util_htset(vec_last(parser->variables), var->name, (void*)var);
+ // Add it to the local scope
+ util_htset(parser->variables.back(), var->m_name.c_str(), (void*)var);
- /* now rename the global */
- ln = strlen(var->name);
- vec_append(defname, ln, var->name);
- /* if a variable of that name already existed, add the
- * counter value.
- * The counter is incremented either way.
- */
- sn_size = parser->function->static_names.size();
+ // now rename the global
+ defname.append(var->m_name);
+ // if a variable of that name already existed, add the
+ // counter value.
+ // The counter is incremented either way.
+ sn_size = parser->function->m_static_names.size();
for (sn = 0; sn != sn_size; ++sn) {
- if (strcmp(parser->function->static_names[sn], var->name) == 0)
+ if (parser->function->m_static_names[sn] == var->m_name.c_str())
break;
}
if (sn != sn_size) {
char *num = nullptr;
- int len = util_asprintf(&num, "#%u", parser->function->static_count);
- vec_append(defname, len, num);
+ int len = util_asprintf(&num, "#%u", parser->function->m_static_count);
+ defname.append(num, 0, len);
mem_d(num);
}
else
- parser->function->static_names.push_back(util_strdup(var->name));
- parser->function->static_count++;
- ast_value_set_name(var, defname);
+ parser->function->m_static_names.emplace_back(var->m_name);
+ parser->function->m_static_count++;
+ var->m_name = defname;
- /* push it to the to-be-generated globals */
- parser->globals.push_back((ast_expression*)var);
+ // push it to the to-be-generated globals
+ parser->globals.push_back(var);
- /* same game for the vector members */
+ // same game for the vector members
if (isvector) {
+ defname.erase(prefix_len);
for (i = 0; i < 3; ++i) {
- util_htset(vec_last(parser->variables), me[i]->name, (void*)(me[i]));
-
- vec_shrinkto(defname, prefix_len);
- ln = strlen(me[i]->name);
- vec_append(defname, ln, me[i]->name);
- ast_member_set_name(me[i], defname);
-
- parser->globals.push_back((ast_expression*)me[i]);
+ util_htset(parser->variables.back(), me[i]->m_name.c_str(), (void*)(me[i]));
+ me[i]->m_name = defname + me[i]->m_name;
+ parser->globals.push_back(me[i]);
}
}
- vec_free(defname);
} else {
- localblock->locals.push_back(var);
- parser_addlocal(parser, var->name, (ast_expression*)var);
+ localblock->m_locals.push_back(var);
+ parser_addlocal(parser, var->m_name, var);
if (isvector) {
for (i = 0; i < 3; ++i) {
- parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
- ast_block_collect(localblock, (ast_expression*)me[i]);
+ parser_addlocal(parser, me[i]->m_name, me[i]);
+ localblock->collect(me[i]);
}
}
}
/* Part 2.2
* deal with arrays
*/
- if (var->expression.vtype == TYPE_ARRAY) {
- if (var->expression.count != (size_t)-1) {
+ if (var->m_vtype == TYPE_ARRAY) {
+ if (var->m_count != (size_t)-1) {
if (!create_array_accessors(parser, var))
goto cleanup;
}
}
else if (!localblock && !nofields &&
- var->expression.vtype == TYPE_FIELD &&
- var->expression.next->vtype == TYPE_ARRAY)
+ var->m_vtype == TYPE_FIELD &&
+ var->m_next->m_vtype == TYPE_ARRAY)
{
char name[1024];
ast_expression *telem;
ast_value *tfield;
- ast_value *array = (ast_value*)var->expression.next;
+ ast_value *array = (ast_value*)var->m_next;
- if (!ast_istype(var->expression.next, ast_value)) {
+ if (!ast_istype(var->m_next, ast_value)) {
parseerror(parser, "internal error: field element type must be an ast_value");
goto cleanup;
}
- util_snprintf(name, sizeof(name), "%s##SETF", var->name);
+ util_snprintf(name, sizeof(name), "%s##SETF", var->m_name.c_str());
if (!parser_create_array_field_setter(parser, array, name))
goto cleanup;
- telem = ast_type_copy(ast_ctx(var), array->expression.next);
- tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
- tfield->expression.next = telem;
- util_snprintf(name, sizeof(name), "%s##GETFP", var->name);
- if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
- ast_delete(tfield);
+ telem = new ast_expression(ast_copy_type, var->m_context, *array->m_next);
+ tfield = new ast_value(var->m_context, "<.type>", TYPE_FIELD);
+ tfield->m_next = telem;
+ util_snprintf(name, sizeof(name), "%s##GETFP", var->m_name.c_str());
+ if (!parser_create_array_getter(parser, array, tfield, name)) {
+ delete tfield;
goto cleanup;
}
- ast_delete(tfield);
+ delete tfield;
}
skipvar:
if (parser->tok == ';') {
- ast_delete(basetype);
+ delete basetype;
if (!parser_next(parser)) {
parseerror(parser, "error after variable declaration");
return false;
goto another;
/*
- if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
+ if (!var || (!localblock && !nofields && basetype->m_vtype == TYPE_FIELD)) {
*/
if (!var) {
parseerror(parser, "missing comma or semicolon while parsing variables");
if (localblock && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
"initializing expression turns variable `%s` into a constant in this standard",
- var->name) )
+ var->m_name) )
{
break;
}
}
- if (parser->tok != '{' || var->expression.vtype != TYPE_FUNCTION) {
+ if (parser->tok != '{' || var->m_vtype != TYPE_FUNCTION) {
if (parser->tok != '=') {
parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
break;
parseerror(parser, "cannot declare builtins within functions");
break;
}
- if (var->expression.vtype != TYPE_FUNCTION) {
- parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
+ if (var->m_vtype != TYPE_FUNCTION) {
+ parseerror(parser, "unexpected builtin number, '%s' is not a function", var->m_name);
break;
}
if (!parser_next(parser)) {
parseerror(parser, "builtin number expected");
break;
}
- if (!ast_istype(number, ast_value) || !number->hasvalue || number->cvq != CV_CONST)
+ if (!ast_istype(number, ast_value) || !number->m_hasvalue || number->m_cvq != CV_CONST)
{
ast_unref(number);
parseerror(parser, "builtin number must be a compile time constant");
break;
}
- if (number->expression.vtype == TYPE_INTEGER)
- builtin_num = number->constval.vint;
- else if (number->expression.vtype == TYPE_FLOAT)
- builtin_num = number->constval.vfloat;
+ if (number->m_vtype == TYPE_INTEGER)
+ builtin_num = number->m_constval.vint;
+ else if (number->m_vtype == TYPE_FLOAT)
+ builtin_num = number->m_constval.vfloat;
else {
ast_unref(number);
parseerror(parser, "builtin number must be an integer constant");
break;
}
- if (var->hasvalue) {
+ if (var->m_hasvalue) {
(void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
"builtin `%s` has already been defined\n"
" -> previous declaration here: %s:%i",
- var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
+ var->m_name, var->m_context.file, (int)var->m_context.line);
}
else
{
- func = ast_function_new(ast_ctx(var), var->name, var);
+ func = ast_function::make(var->m_context, var->m_name, var);
if (!func) {
- parseerror(parser, "failed to allocate function for `%s`", var->name);
+ parseerror(parser, "failed to allocate function for `%s`", var->m_name);
break;
}
parser->functions.push_back(func);
- func->builtin = -builtin_num-1;
+ func->m_builtin = -builtin_num-1;
}
if (OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS)
: (!parser_next(parser)))
{
parseerror(parser, "expected comma or semicolon");
- if (func)
- ast_function_delete(func);
- var->constval.vfunc = nullptr;
+ delete func;
+ var->m_constval.vfunc = nullptr;
break;
}
}
- else if (var->expression.vtype == TYPE_ARRAY && parser->tok == '{')
+ else if (var->m_vtype == TYPE_ARRAY && parser->tok == '{')
{
if (localblock) {
/* Note that fteqcc and most others don't even *have*
break;
}
- var->hasvalue = true;
+ var->m_hasvalue = true;
if (!parse_array(parser, var))
break;
}
- else if (var->expression.vtype == TYPE_FUNCTION && (parser->tok == '{' || parser->tok == '['))
+ else if (var->m_vtype == TYPE_FUNCTION && (parser->tok == '{' || parser->tok == '['))
{
if (localblock) {
parseerror(parser, "cannot declare functions within functions");
}
if (proto)
- ast_ctx(proto) = parser_ctx(parser);
+ proto->m_context = parser_ctx(parser);
if (!parse_function_body(parser, var))
break;
- ast_delete(basetype);
+ delete basetype;
for (auto &it : parser->gotos)
- parseerror(parser, "undefined label: `%s`", it->name);
+ parseerror(parser, "undefined label: `%s`", it->m_name);
+ parser->gotos.clear();
+ parser->labels.clear();
return true;
} else {
ast_expression *cexp;
/* deal with foldable constants: */
if (localblock &&
- var->cvq == CV_CONST && cval && cval->hasvalue && cval->cvq == CV_CONST && !cval->isfield)
+ var->m_cvq == CV_CONST && cval && cval->m_hasvalue && cval->m_cvq == CV_CONST && !cval->m_isfield)
{
/* remove it from the current locals */
if (isvector) {
for (i = 0; i < 3; ++i) {
- vec_pop(parser->_locals);
- localblock->collect.pop_back();
+ parser->_locals.pop_back();
+ localblock->m_collect.pop_back();
}
}
/* do sanity checking, this function really needs refactoring */
- if (vec_last(parser->_locals) != (ast_expression*)var)
+ if (parser->_locals.back() != var)
parseerror(parser, "internal error: unexpected change in local variable handling");
else
- vec_pop(parser->_locals);
- if (localblock->locals.back() != var)
+ parser->_locals.pop_back();
+ if (localblock->m_locals.back() != var)
parseerror(parser, "internal error: unexpected change in local variable handling (2)");
else
- localblock->locals.pop_back();
+ localblock->m_locals.pop_back();
/* push it to the to-be-generated globals */
- parser->globals.push_back((ast_expression*)var);
+ parser->globals.push_back(var);
if (isvector)
for (i = 0; i < 3; ++i)
- parser->globals.push_back((ast_expression*)last_me[i]);
+ parser->globals.push_back(last_me[i]);
folded_const = true;
}
if (folded_const || !localblock || is_static) {
if (cval != parser->nil &&
- (!cval || ((!cval->hasvalue || cval->cvq != CV_CONST) && !cval->isfield))
+ (!cval || ((!cval->m_hasvalue || cval->m_cvq != CV_CONST) && !cval->m_isfield))
)
{
parseerror(parser, "initializer is non constant");
!OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
qualifier != CV_VAR)
{
- var->cvq = CV_CONST;
+ var->m_cvq = CV_CONST;
}
if (cval == parser->nil)
- var->expression.flags |= AST_FLAG_INITIALIZED;
+ {
+ var->m_flags |= AST_FLAG_INITIALIZED;
+ var->m_flags |= AST_FLAG_NOREF;
+ }
else
{
- var->hasvalue = true;
- if (cval->expression.vtype == TYPE_STRING)
- var->constval.vstring = parser_strdup(cval->constval.vstring);
- else if (cval->expression.vtype == TYPE_FIELD)
- var->constval.vfield = cval;
+ var->m_hasvalue = true;
+ if (cval->m_vtype == TYPE_STRING)
+ var->m_constval.vstring = parser_strdup(cval->m_constval.vstring);
+ else if (cval->m_vtype == TYPE_FIELD)
+ var->m_constval.vfield = cval;
else
- memcpy(&var->constval, &cval->constval, sizeof(var->constval));
+ memcpy(&var->m_constval, &cval->m_constval, sizeof(var->m_constval));
ast_unref(cval);
}
}
} else {
int cvq;
shunt sy;
- cvq = var->cvq;
- var->cvq = CV_NONE;
- sy.out.push_back(syexp(ast_ctx(var), (ast_expression*)var));
- sy.out.push_back(syexp(ast_ctx(cexp), (ast_expression*)cexp));
- sy.ops.push_back(syop(ast_ctx(var), parser->assign_op));
+ cvq = var->m_cvq;
+ var->m_cvq = CV_NONE;
+ sy.out.push_back(syexp(var->m_context, var));
+ sy.out.push_back(syexp(cexp->m_context, cexp));
+ sy.ops.push_back(syop(var->m_context, parser->assign_op));
if (!parser_sy_apply_operator(parser, &sy))
ast_unref(cexp);
else {
if (sy.out.size() != 1 && sy.ops.size() != 0)
parseerror(parser, "internal error: leaked operands");
- if (!ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out))
+ if (!localblock->addExpr(sy.out[0].out))
break;
}
- var->cvq = cvq;
+ var->m_cvq = cvq;
}
/* a constant initialized to an inexact value should be marked inexact:
* const float x = <inexact>; should propagate the inexact flag
*/
- if (var->cvq == CV_CONST && var->expression.vtype == TYPE_FLOAT) {
- if (cval && cval->hasvalue && cval->cvq == CV_CONST)
- var->inexact = cval->inexact;
+ if (var->m_cvq == CV_CONST && var->m_vtype == TYPE_FLOAT) {
+ if (cval && cval->m_hasvalue && cval->m_cvq == CV_CONST)
+ var->m_inexact = cval->m_inexact;
}
}
parseerror(parser, "expected another variable");
break;
}
- var = ast_value_copy(basetype);
+ var = new ast_value(ast_copy_type, *basetype);
cleanvar = true;
- ast_value_set_name(var, parser_tokval(parser));
+ var->m_name = parser_tokval(parser);
if (!parser_next(parser)) {
parseerror(parser, "error parsing variable declaration");
break;
break;
}
- ast_delete(basetype);
+ delete basetype;
return true;
}
if (cleanvar && var)
- ast_delete(var);
- ast_delete(basetype);
+ delete var;
+ delete basetype;
return false;
cleanup:
- ast_delete(basetype);
+ delete basetype;
if (cleanvar && var)
- ast_delete(var);
- if (me[0]) ast_member_delete(me[0]);
- if (me[1]) ast_member_delete(me[1]);
- if (me[2]) ast_member_delete(me[2]);
+ delete var;
+ delete me[0];
+ delete me[1];
+ delete me[2];
return retval;
}
if (!ast_istype(parser->globals[i], ast_value))
continue;
value = (ast_value*)(parser->globals[i]);
- switch (value->expression.vtype) {
+ switch (value->m_vtype) {
case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
crc = progdefs_crc_both(crc, "\tint\t");
break;
}
- crc = progdefs_crc_both(crc, value->name);
+ crc = progdefs_crc_both(crc, value->m_name.c_str());
crc = progdefs_crc_both(crc, ";\n");
}
crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
if (!ast_istype(parser->fields[i], ast_value))
continue;
value = (ast_value*)(parser->fields[i]);
- switch (value->expression.next->vtype) {
+ switch (value->m_next->m_vtype) {
case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
crc = progdefs_crc_both(crc, "\tint\t");
break;
}
- crc = progdefs_crc_both(crc, value->name);
+ crc = progdefs_crc_both(crc, value->m_name.c_str());
crc = progdefs_crc_both(crc, ";\n");
}
crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
- ir->code->crc = crc;
+ ir->m_code->crc = crc;
+}
+
+parser_t::parser_t()
+ : lex(nullptr)
+ , tok(0)
+ , ast_cleaned(false)
+ , translated(0)
+ , crc_globals(0)
+ , crc_fields(0)
+ , function(nullptr)
+ , aliases(util_htnew(PARSER_HT_SIZE))
+ , htfields(util_htnew(PARSER_HT_SIZE))
+ , htglobals(util_htnew(PARSER_HT_SIZE))
+ , assign_op(nullptr)
+ , noref(false)
+ , max_param_count(1)
+ // finish initializing the rest of the parser before initializing
+ // m_fold and m_intrin with the parser passed along
+ , m_fold()
+ , m_intrin()
+{
+ variables.push_back(htfields);
+ variables.push_back(htglobals);
+ typedefs.push_back(util_htnew(TYPEDEF_HT_SIZE));
+ _blocktypedefs.push_back(0);
+
+ lex_ctx_t empty_ctx;
+ empty_ctx.file = "<internal>";
+ empty_ctx.line = 0;
+ empty_ctx.column = 0;
+ nil = new ast_value(empty_ctx, "nil", TYPE_NIL);
+ nil->m_cvq = CV_CONST;
+ if (OPTS_FLAG(UNTYPED_NIL))
+ util_htset(htglobals, "nil", (void*)nil);
+
+ const_vec[0] = new ast_value(empty_ctx, "<vector.x>", TYPE_NOEXPR);
+ const_vec[1] = new ast_value(empty_ctx, "<vector.y>", TYPE_NOEXPR);
+ const_vec[2] = new ast_value(empty_ctx, "<vector.z>", TYPE_NOEXPR);
+
+ if (OPTS_OPTION_BOOL(OPTION_ADD_INFO)) {
+ reserved_version = new ast_value(empty_ctx, "reserved:version", TYPE_STRING);
+ reserved_version->m_cvq = CV_CONST;
+ reserved_version->m_hasvalue = true;
+ reserved_version->m_flags |= AST_FLAG_INCLUDE_DEF;
+ reserved_version->m_flags |= AST_FLAG_NOREF;
+ reserved_version->m_constval.vstring = util_strdup(GMQCC_FULL_VERSION_STRING);
+ } else {
+ reserved_version = nullptr;
+ }
+
+ m_fold = fold(this);
+ m_intrin = intrin(this);
+}
+
+parser_t::~parser_t()
+{
+ remove_ast();
}
parser_t *parser_create()
{
parser_t *parser;
- lex_ctx_t empty_ctx;
size_t i;
- parser = (parser_t*)mem_a(sizeof(parser_t));
+ parser = new parser_t;
if (!parser)
return nullptr;
- memset(parser, 0, sizeof(*parser));
-
- // TODO: remove
- new (parser) parser_t();
-
for (i = 0; i < operator_count; ++i) {
if (operators[i].id == opid1('=')) {
parser->assign_op = operators+i;
}
if (!parser->assign_op) {
con_err("internal error: initializing parser: failed to find assign operator\n");
- mem_d(parser);
+ delete parser;
return nullptr;
}
- vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
- vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
- vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
- vec_push(parser->_blocktypedefs, 0);
-
- parser->aliases = util_htnew(PARSER_HT_SIZE);
-
- empty_ctx.file = "<internal>";
- empty_ctx.line = 0;
- empty_ctx.column = 0;
- parser->nil = ast_value_new(empty_ctx, "nil", TYPE_NIL);
- parser->nil->cvq = CV_CONST;
- if (OPTS_FLAG(UNTYPED_NIL))
- util_htset(parser->htglobals, "nil", (void*)parser->nil);
-
- parser->max_param_count = 1;
-
- parser->const_vec[0] = ast_value_new(empty_ctx, "<vector.x>", TYPE_NOEXPR);
- parser->const_vec[1] = ast_value_new(empty_ctx, "<vector.y>", TYPE_NOEXPR);
- parser->const_vec[2] = ast_value_new(empty_ctx, "<vector.z>", TYPE_NOEXPR);
-
- if (OPTS_OPTION_BOOL(OPTION_ADD_INFO)) {
- parser->reserved_version = ast_value_new(empty_ctx, "reserved:version", TYPE_STRING);
- parser->reserved_version->cvq = CV_CONST;
- parser->reserved_version->hasvalue = true;
- parser->reserved_version->expression.flags |= AST_FLAG_INCLUDE_DEF;
- parser->reserved_version->constval.vstring = util_strdup(GMQCC_FULL_VERSION_STRING);
- } else {
- parser->reserved_version = nullptr;
- }
-
- parser->m_fold = fold(parser);
- parser->m_intrin = intrin(parser);
return parser;
}
return parser_compile(parser);
}
-static void parser_remove_ast(parser_t *parser)
+void parser_t::remove_ast()
{
- size_t i;
- if (parser->ast_cleaned)
+ if (ast_cleaned)
return;
- parser->ast_cleaned = true;
- for (auto &it : parser->accessors) {
- ast_delete(it->constval.vfunc);
- it->constval.vfunc = nullptr;
- ast_delete(it);
- }
- 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]);
- vec_free(parser->variables);
- vec_free(parser->_blocklocals);
- vec_free(parser->_locals);
-
- for (i = 0; i < vec_size(parser->_typedefs); ++i)
- ast_delete(parser->_typedefs[i]);
- vec_free(parser->_typedefs);
- for (i = 0; i < vec_size(parser->typedefs); ++i)
- util_htdel(parser->typedefs[i]);
- vec_free(parser->typedefs);
- vec_free(parser->_blocktypedefs);
-
- vec_free(parser->_block_ctx);
-
- ast_value_delete(parser->nil);
-
- ast_value_delete(parser->const_vec[0]);
- ast_value_delete(parser->const_vec[1]);
- ast_value_delete(parser->const_vec[2]);
-
- if (parser->reserved_version)
- ast_value_delete(parser->reserved_version);
-
- util_htdel(parser->aliases);
-}
+ ast_cleaned = true;
+ for (auto &it : accessors) {
+ delete it->m_constval.vfunc;
+ it->m_constval.vfunc = nullptr;
+ delete it;
+ }
+ for (auto &it : functions) delete it;
+ for (auto &it : globals) delete it;
+ for (auto &it : fields) delete it;
-void parser_cleanup(parser_t *parser)
-{
- parser_remove_ast(parser);
- mem_d(parser);
+ for (auto &it : variables) util_htdel(it);
+ variables.clear();
+ _blocklocals.clear();
+ _locals.clear();
+
+ _typedefs.clear();
+ for (auto &it : typedefs) util_htdel(it);
+ typedefs.clear();
+ _blocktypedefs.clear();
+
+ _block_ctx.clear();
+
+ delete nil;
+
+ delete const_vec[0];
+ delete const_vec[1];
+ delete const_vec[2];
+
+ if (reserved_version)
+ delete reserved_version;
+
+ util_htdel(aliases);
}
static bool parser_set_coverage_func(parser_t *parser, ir_builder *ir) {
func = nullptr;
for (auto &it : parser->functions) {
- if (!strcmp(it->name, "coverage")) {
+ if (it->m_name == "coverage") {
func = it;
break;
}
if (!func) {
if (OPTS_OPTION_BOOL(OPTION_COVERAGE)) {
con_out("coverage support requested but no coverage() builtin declared\n");
- ir_builder_delete(ir);
+ delete ir;
return false;
}
return true;
}
- cov = func->vtype;
- expr = (ast_expression*)cov;
+ cov = func->m_function_type;
+ expr = cov;
- if (expr->vtype != TYPE_FUNCTION || expr->params.size()) {
+ if (expr->m_vtype != TYPE_FUNCTION || expr->m_type_params.size()) {
char ty[1024];
ast_type_to_string(expr, ty, sizeof(ty));
con_out("invalid type for coverage(): %s\n", ty);
- ir_builder_delete(ir);
+ delete ir;
return false;
}
- ir->coverage_func = func->ir_func->value;
+ ir->m_coverage_func = func->m_ir_func->m_value;
return true;
}
return false;
}
- ir = ir_builder_new("gmqcc_out");
+ ir = new ir_builder("gmqcc_out");
if (!ir) {
con_out("failed to allocate builder\n");
return false;
if (!ast_istype(it, ast_value))
continue;
ast_value *field = (ast_value*)it;
- hasvalue = field->hasvalue;
- field->hasvalue = false;
- if (!ast_global_codegen((ast_value*)field, ir, true)) {
- con_out("failed to generate field %s\n", field->name);
- ir_builder_delete(ir);
+ hasvalue = field->m_hasvalue;
+ field->m_hasvalue = false;
+ if (!reinterpret_cast<ast_value*>(field)->generateGlobal(ir, true)) {
+ con_out("failed to generate field %s\n", field->m_name.c_str());
+ delete ir;
return false;
}
if (hasvalue) {
ir_value *ifld;
ast_expression *subtype;
- field->hasvalue = true;
- subtype = field->expression.next;
- ifld = ir_builder_create_field(ir, field->name, subtype->vtype);
- if (subtype->vtype == TYPE_FIELD)
- ifld->fieldtype = subtype->next->vtype;
- else if (subtype->vtype == TYPE_FUNCTION)
- ifld->outtype = subtype->next->vtype;
- (void)!ir_value_set_field(field->ir_v, ifld);
+ field->m_hasvalue = true;
+ subtype = field->m_next;
+ ifld = ir->createField(field->m_name, subtype->m_vtype);
+ if (subtype->m_vtype == TYPE_FIELD)
+ ifld->m_fieldtype = subtype->m_next->m_vtype;
+ else if (subtype->m_vtype == TYPE_FUNCTION)
+ ifld->m_outtype = subtype->m_next->m_vtype;
+ (void)!field->m_ir_v->setField(ifld);
}
}
for (auto &it : parser->globals) {
if (!ast_istype(it, ast_value))
continue;
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);
+ if (!(asvalue->m_flags & AST_FLAG_NOREF) && asvalue->m_cvq != CV_CONST && asvalue->m_vtype != TYPE_FUNCTION) {
+ retval = retval && !compile_warning(asvalue->m_context, WARN_UNUSED_VARIABLE,
+ "unused global: `%s`", asvalue->m_name);
}
- if (!ast_global_codegen(asvalue, ir, false)) {
- con_out("failed to generate global %s\n", asvalue->name);
- ir_builder_delete(ir);
+ if (!asvalue->generateGlobal(ir, false)) {
+ con_out("failed to generate global %s\n", asvalue->m_name.c_str());
+ delete ir;
return false;
}
}
* immediates, because the accessors may add new immediates
*/
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();
- if (!parser_create_array_setter_impl(parser, f->varargs)) {
- con_out("failed to generate vararg setter for %s\n", f->name);
- ir_builder_delete(ir);
+ if (f->m_varargs) {
+ if (parser->max_param_count > f->m_function_type->m_type_params.size()) {
+ f->m_varargs->m_count = parser->max_param_count - f->m_function_type->m_type_params.size();
+ if (!parser_create_array_setter_impl(parser, f->m_varargs.get())) {
+ con_out("failed to generate vararg setter for %s\n", f->m_name.c_str());
+ delete ir;
return false;
}
- if (!parser_create_array_getter_impl(parser, f->varargs)) {
- con_out("failed to generate vararg getter for %s\n", f->name);
- ir_builder_delete(ir);
+ if (!parser_create_array_getter_impl(parser, f->m_varargs.get())) {
+ con_out("failed to generate vararg getter for %s\n", f->m_name.c_str());
+ delete ir;
return false;
}
} else {
- ast_delete(f->varargs);
- f->varargs = nullptr;
+ f->m_varargs = nullptr;
}
}
}
if (!ast_istype(it, ast_value))
continue;
ast_value *asvalue = (ast_value*)it;
- if (!(asvalue->expression.flags & AST_FLAG_INITIALIZED))
+ if (!(asvalue->m_flags & AST_FLAG_INITIALIZED))
{
- if (asvalue->cvq == CV_CONST && !asvalue->hasvalue)
- (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_CONSTANT,
+ if (asvalue->m_cvq == CV_CONST && !asvalue->m_hasvalue)
+ (void)!compile_warning(asvalue->m_context, WARN_UNINITIALIZED_CONSTANT,
"uninitialized constant: `%s`",
- asvalue->name);
- else if ((asvalue->cvq == CV_NONE || asvalue->cvq == CV_CONST) && !asvalue->hasvalue)
- (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_GLOBAL,
+ asvalue->m_name);
+ else if ((asvalue->m_cvq == CV_NONE || asvalue->m_cvq == CV_CONST) && !asvalue->m_hasvalue)
+ (void)!compile_warning(asvalue->m_context, WARN_UNINITIALIZED_GLOBAL,
"uninitialized global: `%s`",
- asvalue->name);
+ asvalue->m_name);
}
- if (!ast_generate_accessors(asvalue, ir)) {
- ir_builder_delete(ir);
+ if (!asvalue->generateAccessors(ir)) {
+ delete ir;
return false;
}
}
for (auto &it : parser->fields) {
- ast_value *asvalue = (ast_value*)it->next;
- if (!ast_istype((ast_expression*)asvalue, ast_value))
+ ast_value *asvalue = (ast_value*)it->m_next;
+ if (!ast_istype(asvalue, ast_value))
continue;
- if (asvalue->expression.vtype != TYPE_ARRAY)
+ if (asvalue->m_vtype != TYPE_ARRAY)
continue;
- if (!ast_generate_accessors(asvalue, ir)) {
- ir_builder_delete(ir);
+ if (!asvalue->generateAccessors(ir)) {
+ delete ir;
return false;
}
}
if (parser->reserved_version &&
- !ast_global_codegen(parser->reserved_version, ir, false))
+ !parser->reserved_version->generateGlobal(ir, false))
{
con_out("failed to generate reserved::version");
- ir_builder_delete(ir);
+ delete ir;
return false;
}
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);
+ if (!f->generateFunction(ir)) {
+ con_out("failed to generate function %s\n", f->m_name.c_str());
+ delete ir;
return false;
}
}
generate_checksum(parser, ir);
if (OPTS_OPTION_BOOL(OPTION_DUMP))
- ir_builder_dump(ir, con_out);
+ ir->dump(con_out);
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);
+ if (!ir_function_finalize(it->m_ir_func)) {
+ con_out("failed to finalize function %s\n", it->m_name.c_str());
+ delete ir;
return false;
}
}
- parser_remove_ast(parser);
+ parser->remove_ast();
- if (compile_Werrors) {
- con_out("*** there were warnings treated as errors\n");
- compile_show_werrors();
- retval = false;
- }
+ auto fnCheckWErrors = [&retval]() {
+ if (compile_Werrors) {
+ con_out("*** there were warnings treated as errors\n");
+ compile_show_werrors();
+ retval = false;
+ }
+ };
+
+ fnCheckWErrors();
if (retval) {
if (OPTS_OPTION_BOOL(OPTION_DUMPFIN))
- ir_builder_dump(ir, con_out);
+ ir->dump(con_out);
- if (!ir_builder_generate(ir, output)) {
+ if (!ir->generate(output)) {
con_out("*** failed to generate output file\n");
- ir_builder_delete(ir);
+ delete ir;
return false;
}
+
+ // ir->generate can generate compiler warnings
+ fnCheckWErrors();
}
- ir_builder_delete(ir);
+ delete ir;
return retval;
}