* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static bool ast_member_codegen(ast_member*, ast_function*, bool lvalue, ir_value**);
static void ast_array_index_delete(ast_array_index*);
static bool ast_array_index_codegen(ast_array_index*, ast_function*, bool lvalue, ir_value**);
+static void ast_argpipe_delete(ast_argpipe*);
+static bool ast_argpipe_codegen(ast_argpipe*, ast_function*, bool lvalue, ir_value**);
static void ast_store_delete(ast_store*);
static bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
static void ast_ifthen_delete(ast_ifthen*);
mem_d(self);
}
+ast_argpipe* ast_argpipe_new(lex_ctx ctx, ast_expression *index)
+{
+ ast_instantiate(ast_argpipe, ctx, ast_argpipe_delete);
+ ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_argpipe_codegen);
+ self->index = index;
+ self->expression.vtype = TYPE_NOEXPR;
+ return self;
+}
+
+void ast_argpipe_delete(ast_argpipe *self)
+{
+ if (self->index)
+ ast_unref(self->index);
+ ast_expression_delete((ast_expression*)self);
+ mem_d(self);
+}
+
ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
{
ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
mem_d(self);
}
-bool ast_call_check_types(ast_call *self)
+static bool ast_call_check_vararg(ast_call *self, ast_expression *va_type, ast_expression *exp_type)
+{
+ char texp[1024];
+ char tgot[1024];
+ if (!exp_type)
+ return true;
+ if (!va_type || !ast_compare_type(va_type, exp_type))
+ {
+ if (va_type && exp_type)
+ {
+ ast_type_to_string(va_type, tgot, sizeof(tgot));
+ ast_type_to_string(exp_type, texp, sizeof(texp));
+ if (OPTS_FLAG(UNSAFE_VARARGS)) {
+ if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
+ "piped variadic argument differs in type: constrained to type %s, expected type %s",
+ tgot, texp))
+ return false;
+ } else {
+ compile_error(ast_ctx(self),
+ "piped variadic argument differs in type: constrained to type %s, expected type %s",
+ tgot, texp);
+ return false;
+ }
+ }
+ else
+ {
+ ast_type_to_string(exp_type, texp, sizeof(texp));
+ if (OPTS_FLAG(UNSAFE_VARARGS)) {
+ if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
+ "piped variadic argument may differ in type: expected type %s",
+ texp))
+ return false;
+ } else {
+ compile_error(ast_ctx(self),
+ "piped variadic argument may differ in type: expected type %s",
+ texp);
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+bool ast_call_check_types(ast_call *self, ast_expression *va_type)
{
char texp[1024];
char tgot[1024];
count = vec_size(func->params);
for (i = 0; i < count; ++i) {
- if (!ast_compare_type(self->params[i], (ast_expression*)(func->params[i])))
+ if (ast_istype(self->params[i], ast_argpipe)) {
+ /* warn about type safety instead */
+ if (i+1 != count) {
+ compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
+ return false;
+ }
+ if (!ast_call_check_vararg(self, va_type, (ast_expression*)func->params[i]))
+ retval = false;
+ }
+ else if (!ast_compare_type(self->params[i], (ast_expression*)(func->params[i])))
{
ast_type_to_string(self->params[i], tgot, sizeof(tgot));
ast_type_to_string((ast_expression*)func->params[i], texp, sizeof(texp));
count = vec_size(self->params);
if (count > vec_size(func->params) && func->varparam) {
for (; i < count; ++i) {
- if (!ast_compare_type(self->params[i], func->varparam))
+ if (ast_istype(self->params[i], ast_argpipe)) {
+ /* warn about type safety instead */
+ if (i+1 != count) {
+ compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
+ return false;
+ }
+ if (!ast_call_check_vararg(self, va_type, func->varparam))
+ retval = false;
+ }
+ else if (!ast_compare_type(self->params[i], func->varparam))
{
ast_type_to_string(self->params[i], tgot, sizeof(tgot));
ast_type_to_string(func->varparam, texp, sizeof(texp));
- compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
+ compile_error(ast_ctx(self), "invalid type for variadic parameter %u in function call: expected %s, got %s",
(unsigned int)(i+1), texp, tgot);
/* we don't immediately return */
retval = false;
vtype->hasvalue = true;
vtype->constval.vfunc = self;
- self->varargs = NULL;
- self->argc = NULL;
- self->fixedparams = NULL;
+ self->varargs = NULL;
+ self->argc = NULL;
+ self->fixedparams = NULL;
+ self->return_value = NULL;
return self;
}
ast_delete(self->argc);
if (self->fixedparams)
ast_unref(self->fixedparams);
+ if (self->return_value)
+ ast_unref(self->return_value);
mem_d(self);
}
return true;
}
+static bool ast_global_array_set(ast_value *self)
+{
+ size_t count = vec_size(self->initlist);
+ size_t i;
+
+ if (count > self->expression.count) {
+ compile_error(ast_ctx(self), "too many elements in initializer");
+ count = self->expression.count;
+ }
+ else if (count < self->expression.count) {
+ /* add this?
+ compile_warning(ast_ctx(self), "not all elements are initialized");
+ */
+ }
+
+ for (i = 0; i != count; ++i) {
+ switch (self->expression.next->vtype) {
+ case TYPE_FLOAT:
+ if (!ir_value_set_float(self->ir_values[i], self->initlist[i].vfloat))
+ return false;
+ break;
+ case TYPE_VECTOR:
+ if (!ir_value_set_vector(self->ir_values[i], self->initlist[i].vvec))
+ return false;
+ break;
+ case TYPE_STRING:
+ if (!ir_value_set_string(self->ir_values[i], self->initlist[i].vstring))
+ return false;
+ break;
+ case TYPE_ARRAY:
+ /* we don't support them in any other place yet either */
+ compile_error(ast_ctx(self), "TODO: nested arrays");
+ return false;
+ case TYPE_FUNCTION:
+ /* this requiers a bit more work - similar to the fields I suppose */
+ compile_error(ast_ctx(self), "global of type function not properly generated");
+ return false;
+ case TYPE_FIELD:
+ if (!self->initlist[i].vfield) {
+ compile_error(ast_ctx(self), "field constant without vfield set");
+ return false;
+ }
+ if (!self->initlist[i].vfield->ir_v) {
+ compile_error(ast_ctx(self), "field constant generated before its field");
+ return false;
+ }
+ if (!ir_value_set_field(self->ir_values[i], self->initlist[i].vfield->ir_v))
+ return false;
+ break;
+ default:
+ compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
+ break;
+ }
+ }
+ return true;
+}
+
bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
{
ir_value *v = NULL;
ast_expression *elemtype = self->expression.next;
int vtype = elemtype->vtype;
+ if (self->expression.flags & AST_FLAG_ARRAY_INIT && !self->expression.count) {
+ compile_error(ast_ctx(self), "array `%s' has no size", self->name);
+ return false;
+ }
+
/* same as with field arrays */
if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
v->context = ast_ctx(self);
}
+ /* link us to the ir_value */
+ v->cvq = self->cvq;
+ self->ir_v = v;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
+
+ /* initialize */
if (self->hasvalue) {
switch (self->expression.vtype)
{
goto error;
break;
case TYPE_ARRAY:
- compile_error(ast_ctx(self), "TODO: global constant array");
+ ast_global_array_set(self);
break;
case TYPE_FUNCTION:
compile_error(ast_ctx(self), "global of type function not properly generated");
break;
}
}
-
- /* link us to the ir_value */
- v->cvq = self->cvq;
- self->ir_v = v;
- if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
- self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
return true;
error: /* clean up */
return true;
}
+ /* have a local return value variable? */
+ if (self->return_value) {
+ if (!ast_local_codegen(self->return_value, self->ir_func, false))
+ return false;
+ }
+
if (!vec_size(self->blocks)) {
compile_error(ast_ctx(self), "function `%s` has no body", self->name);
return false;
}
else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
{
- /* error("missing return"); */
- if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
+ if (self->return_value) {
+ cgen = self->return_value->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->return_value), self, false, &dummy))
+ return false;
+ return ir_block_create_return(self->curblock, ast_ctx(self), dummy);
+ }
+ else if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
"control reaches end of non-void function (`%s`) via %s",
self->name, self->curblock->label))
{
return true;
}
+bool ast_argpipe_codegen(ast_argpipe *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ *out = NULL;
+ if (lvalue) {
+ compile_error(ast_ctx(self), "argpipe node: not an lvalue");
+ return false;
+ }
+ (void)func;
+ (void)out;
+ compile_error(ast_ctx(self), "TODO: argpipe codegen not implemented");
+ return false;
+}
+
bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
{
ast_expression_codegen *cgen;