aboutsummaryrefslogtreecommitdiff
path: root/src/types.c
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-04 07:38:53 -0500
committerMustafa Quraish <[email protected]>2022-02-05 08:56:15 -0500
commit6a376cfd6f89a6ceb39a8c425cf8095647170c7e (patch)
tree0f5b6e0dee971222824c71e8ec207bbdbd1eb3ec /src/types.c
parentAllow `builtins.c` to inject constants into program, use for syscalls (diff)
downloadcup-6a376cfd6f89a6ceb39a8c425cf8095647170c7e.tar.xz
cup-6a376cfd6f89a6ceb39a8c425cf8095647170c7e.zip
Add `void` type and allow void* to be assigned to other ptr types
Diffstat (limited to 'src/types.c')
-rw-r--r--src/types.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/types.c b/src/types.c
index 8ac4c77..c44f88d 100644
--- a/src/types.c
+++ b/src/types.c
@@ -21,6 +21,12 @@ bool is_convertible(Type *from, Type *to)
{
if (from->type == TYPE_ANY || to->type == TYPE_ANY)
return true;
+
+ // Allow converstions to and from void* to any other pointer type
+ if (from->type == TYPE_PTR && to->type == TYPE_PTR)
+ if (from->ptr->type == TYPE_VOID || to->ptr->type == TYPE_VOID)
+ return true;
+
// TODO: Should we rause a warning if the target type is narrower
// than the source type?
if (is_int_type(from) && is_int_type(to))
@@ -52,9 +58,11 @@ Type *type_new(DataType type)
static Type type_int = {.type = TYPE_INT, .ptr = NULL};
static Type type_char = {.type = TYPE_CHAR, .ptr = NULL};
static Type type_any = {.type = TYPE_ANY, .ptr = NULL};
+ static Type type_void = {.type = TYPE_VOID, .ptr = NULL};
if (type == TYPE_INT) return &type_int;
if (type == TYPE_CHAR) return &type_char;
if (type == TYPE_ANY) return &type_any;
+ if (type == TYPE_VOID) return &type_void;
Type *self = calloc(sizeof(Type), 1);
self->type = type;
@@ -102,7 +110,7 @@ static char *data_type_to_str(Type *type)
{
switch (type->type)
{
- case TYPE_NONE: return "void";
+ case TYPE_VOID: return "void";
case TYPE_INT: return "int";
case TYPE_PTR: return "*";
case TYPE_ARRAY: return "array";