summaryrefslogtreecommitdiff
path: root/bootstrap/types.c
blob: 7e67389d6f801645836d3f65c083681743ab4f4d (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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

/*

  types.c -- Handling of types

  Copyright © 2020 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "bootstrap.h"
#include "hash.h"

/* TODO add all builtins */
static struct Type type_boolident = { D_TYPEDEF, T_IDENT, 0, 0, { NULL } };
static struct Type type_boolenum = { D_TYPEDEF, T_ENUM, 0, 0, { NULL } };
static struct Type type_usize = { D_TYPEDEF, T_ELMNTRY, 0, 0, { NULL } };
static struct Type type_int = { D_TYPEDEF, T_ELMNTRY, 0, 0, { NULL } };

struct TypeRef builtin_tr[BT_NUMTYPES];

struct Ident ident_bool;
struct EnumType enumtype_bool;
struct EnumValue ev_bool[2];
struct Expr value_0;
struct Expr value_1;
struct LiteralValue literal_0;
struct LiteralValue literal_1;

void init_builtins(void)
{
    builtin_tr[0].type = &type_boolident;
    builtin_tr[1].type = &type_usize;
    builtin_tr[2].type = &type_int;
    type_usize.kind.builtin = BT_USIZE;
    type_int.kind.builtin = BT_USIZE;
    type_boolident.kind.ident = &ident_bool;
    type_boolenum.kind.enu = &enumtype_bool;
    enumtype_bool.values = &ev_bool[0];
    enumtype_bool.base = NULL;
    ev_bool[0].next = &ev_bool[1];
    ev_bool[0].hash = HASH5('f','a','l','s','e');
    ev_bool[0].name = "false";
    ev_bool[0].value = &value_0;
    ev_bool[1].hash = HASH4('t','r','u','e');
    ev_bool[1].name = "true";
    ev_bool[1].value = &value_1;
    value_0.type = E_NUMBER;
    value_0.a.value = &literal_0;
    value_0.tmp.tr.type = &type_int;
    value_1.type = E_NUMBER;
    value_1.a.value = &literal_1;
    value_1.tmp.tr.type = &type_int;
    literal_0.length = 1;
    literal_0.data = "0";
    literal_1.length = 1;
    literal_1.data = "1";
}

void require_tr(const struct Expr *expr, const char *errmsg)
{
    if (!expr->tmp.tr.type) {
        error_linecol(-1, -1, errmsg);
    }
}

struct TypeRef root_tr(struct Type *type)
{
    struct TypeRef tr;
    tr.type = type;
    tr.quals = 0;
    tr.prm = NULL;
    return tr;
}

struct TypeRef nested_tr(struct Type *type, struct TypeRef *wrappedby)
{
    struct TypeRef tr;
    tr.type = type;
    tr.quals = wrappedby->quals;
    tr.prm = wrappedby->prm;
    return tr;
}

struct TypeRef get_elem_tr(struct TypeRef *tr)
{
    struct TypeRef elemtr;
    elemtr.prm = tr->prm;
    elemtr.quals = tr->quals;
    if (tr->type && tr->type->type == T_ARRAY) {
        elemtr.type = ARR_ELEM_TYPE(tr->type);
    } else {
        elemtr.type = NULL;
    }
    return elemtr;
}


/**
 * Checks two type references for compatibility,
 * and reports an error if not.
 */
int check_compat(const char *itemname,
                 struct TypeRef *tr, struct TypeRef *target)
{
    /* TODO */
    return 1;
}


/**
 * Determines the actual type of the given typeref, and checks that it is
 * of the a given type.
 *
 * @param expr Expression containing a typeref (i.e. must have been
 *             processed by print_expr_temps).
 * @param type One of the T_ constants
 * @return The actual type. The type field will be null on error.
 */
struct TypeRef require_expr_type(struct Expr *expr, unsigned type)
{
    struct TypeRef tr;
    /* TODO */
    return tr;
}