#include "assert.h"
#include "validate_constraints.h"
#include "ctinfo.h"
#include "dbug.h"
#include "memory.h"
#include "mymacros.h"
#include "symbol_table.h"
#include "traverse.h"
#include "tree_basic.h"
#include <string.h>

node* VCfundef(node* arg_node, info* arg_info)
{
    DBUG_ENTER("VCfundef");

    struct st_entry_fn* ent = FUNDEF_STLINK(arg_node);

    if (strcmp(ent->name, "main") == 0 && (ent->num_params != 0 || ent->type != TY_int))
    {
        ERR("Invalid declaration for main function (must be int main())");
    }

    if (ent->num_params > 255)
    {
        ERR("Functions can't have more than 255 parameters.");
    }

    TO(FUNDEF_BODY);

    DBUG_RETURN(arg_node);
}

node* VCfor(node* arg_node, info* arg_info)
{
    /* Really trivial check now, but we could run this again after constant prop/folding stuff and
     * it might catch more. Not required, though. */
    DBUG_ENTER("VCfor");

    node *step = FOR_VARSTEPVAL(arg_node);
    if (NODE_TYPE(step) == N_int && INT_VALUE(step) == 0)
    {
        ERR("For loop step value can't be zero.");
    }

    TO(FOR_VARINITVAL);
    TO(FOR_VARLIMVAL);
    TO(FOR_VARSTEPVAL);
    TO(FOR_BLOCK);

    DBUG_RETURN(arg_node);
}

node* VCdoValidate(node* syntaxtree)
{
    DBUG_ENTER("VCdoValidate");

    TRAVpush(TR_vc);
    syntaxtree = TRAVdo(syntaxtree, NULL);
    TRAVpop();

    DBUG_RETURN(syntaxtree);
}