summaryrefslogtreecommitdiffstats
path: root/src/symbol-table.h
blob: 589b9f908c40035d5dfa91ef31b8f747d595266f (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef CCMMC_HEADER_SYMBOL_TABLE_H
#define CCMMC_HEADER_SYMBOL_TABLE_H

#include "ast.h"

#include <stdbool.h>
#include <stdint.h>

#define CCMMC_SYMBOL_SCOPE_HASH_TABLE_SIZE 256

typedef enum CcmmcSymbolKind_enum {
    CCMMC_SYMBOL_KIND_TYPE,
    CCMMC_SYMBOL_KIND_VARIABLE,
    CCMMC_SYMBOL_KIND_FUNCTION
} CcmmcSymbolKind;

typedef struct CcmmcSymbolType_struct CcmmcSymbolType;
typedef struct CcmmcSymbolType_struct {
    CcmmcAstValueType type_base;
    size_t array_dimension;
    size_t *array_size;
    bool param_valid;
    size_t param_count;
    CcmmcSymbolType *param_list;
} CcmmcSymbolType;

typedef struct CcmmcSymbolAttr_struct {
    bool is_arg;
    union {
        uint64_t addr;
        unsigned int arg_num;
    };
} CcmmcSymbolAttr;

typedef struct CcmmcSymbol_struct CcmmcSymbol;
typedef struct CcmmcSymbol_struct {
    CcmmcSymbolKind kind;
    CcmmcSymbolType type;
    CcmmcSymbolAttr attr;
    const char *name;
    CcmmcSymbol *next;
} CcmmcSymbol;

typedef struct CcmmcSymbolScope_struct CcmmcSymbolScope;
typedef struct CcmmcSymbolScope_struct {
    CcmmcSymbol *hash_table[CCMMC_SYMBOL_SCOPE_HASH_TABLE_SIZE];
    CcmmcSymbolScope *all_next;
    CcmmcSymbolScope *current_next;
} CcmmcSymbolScope;

typedef struct CcmmcSymbolTable_struct {
    // ro
    CcmmcSymbolScope *all;
    CcmmcSymbolScope *all_last;
    // rw
    CcmmcSymbolScope *this_scope;
    CcmmcSymbolScope *current;
} CcmmcSymbolTable;

static inline bool ccmmc_symbol_type_is_scalar(CcmmcSymbolType type) {
    return type.array_dimension == 0 && !type.param_valid;
}
static inline bool ccmmc_symbol_type_is_array(CcmmcSymbolType type) {
    return type.array_dimension != 0;
}
static inline bool ccmmc_symbol_type_is_function(CcmmcSymbolType type) {
    return type.param_valid;
}
static inline bool ccmmc_symbol_is_scalar(CcmmcSymbol *symbol) {
    return ccmmc_symbol_type_is_scalar(symbol->type);
}
static inline bool ccmmc_symbol_is_array(CcmmcSymbol *symbol) {
    return ccmmc_symbol_type_is_array(symbol->type);
}
static inline bool ccmmc_symbol_is_function(CcmmcSymbol *symbol) {
    return ccmmc_symbol_type_is_function(symbol->type);
}
static inline bool ccmmc_symbol_attr_is_global(CcmmcSymbolAttr *attr) {
    return !attr->is_arg && attr->addr == 0;
}
static inline bool ccmmc_symbol_attr_is_local(CcmmcSymbolAttr *attr) {
    return !ccmmc_symbol_attr_is_global(attr);
}

void             ccmmc_symbol_table_open_scope      (CcmmcSymbolTable *table);
void             ccmmc_symbol_table_reopen_scope    (CcmmcSymbolTable *table);
void             ccmmc_symbol_table_close_scope     (CcmmcSymbolTable *table);
void             ccmmc_symbol_table_init            (CcmmcSymbolTable *table);
void             ccmmc_symbol_table_insert          (CcmmcSymbolTable *table,
                                                     const char *name,
                                                     CcmmcSymbolKind kind,
                                                     CcmmcSymbolType type);
CcmmcSymbol     *ccmmc_symbol_table_retrieve        (CcmmcSymbolTable *table,
                                                     const char *name);
bool             ccmmc_symbol_scope_exist           (CcmmcSymbolScope *scope,
                                                     const char *name);


#endif
// vim: set sw=4 ts=4 sts=4 et: