/* types.c -- Handling of types Copyright © 2020 Samuel Lidén Borell 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; }