summaryrefslogtreecommitdiff
path: root/scope.h
blob: c0c3d3bd6348ce3689d5cf0e973d4ba01a632a51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef SCOPE_H
#define SCOPE_H

#include "ccc.h"
#include "type.h"
#include "hashmap.h"

struct type_alias {
    const char* name;
    struct type type;
};

struct storage_location {
    enum {
        STO_REG,
        STO_LABEL,
        STO_STACK,
        STO_IMM,
    } type;
    union {
        struct reg* reg;
        const char* label;
        sintegral_t bp_offset;
        integral_t value;
    };
};

struct fn_def {
    const struct fn_decl_node* decl;
    bool resolved;
};

struct var_def {
    const struct type* type;
    char* name;
    struct fn_def fn;
    struct storage_location storage;
};

struct scope {
    struct hash_map types;
    struct hash_map vars;
    struct scope* next_out;
    integral_t bp_offset;
};

void scope_push(struct scope** p_scope);
void scope_pop(struct scope** p_scope);
void scope_destroy(struct scope* scope);
bool scope_get_type(
    const struct scope* scope,
    const struct type_alias** p_entry,
    const char* name);
const struct type_alias* scope_define_type(
    struct scope* scope,
    struct type_alias 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