#ifndef SCOPE_H
#define SCOPE_H

struct storage_location {
    enum {
        STO_REG,
        STO_LABEL,
        STO_STACK,
        STO_IMM,
        /* I would like to solve functions using the type system
         * and kill STO_FN and STO_UNRESOLVED in favor of STO_LABEL. */
        STO_FN,
        STO_UNRESOLVED,
    } type;
    union {
        const struct reg* reg;
        const char* label;
        long long bp_offset;
        unsigned long long value;
        struct fn_decl_node* decl;
    };
};

struct type_ref {
    const struct type_def* raw_type;
    unsigned char ptr_level;
};

struct type_key {
    char* name;
    unsigned char how_long;
    bool marked_unsigned;
    bool marked_signed;
};

struct type_def {
    struct type_key key;
    bool is_signed;
    bool is_floating;
    unsigned long long sz;
};

struct var_def {
    const struct type_ref* type;
    char* name;
    struct storage_location loc;
    const struct type_def* resolved_type;
};

struct scope {
    struct type_def** types;
    unsigned long long type_sz;
    unsigned long long type_cap;

    struct var_def** vars;
    unsigned long long var_sz;
    unsigned long long var_cap;

    struct scope* next_out;
    unsigned long long bp_offset;
};

struct ast;

void scope_push(struct scope** p_scope);
void scope_pop(struct scope** p_scope);
void scope_destroy(struct scope* scope);
void scope_install_default_types(struct ast* ast);
bool scope_get_type(
    const struct scope* scope,
    const struct type_def** p_entry,
    const struct type_key* key);
const struct type_def* scope_define_type(
    struct scope* scope,
    struct type_def type);
bool scope_get_var(
    const struct scope* scope,
    struct var_def** p_entry,
    const char* name);
struct var_def* scope_define_var(struct scope* scope, struct var_def var);

#endif