tomo/types.h

161 lines
4.2 KiB
C
Raw Normal View History

2024-02-07 21:52:18 -08:00
#pragma once
2024-03-18 09:49:38 -07:00
// Logic for defining and working with types
2024-02-07 21:52:18 -08:00
#include <printf.h>
#include <stdlib.h>
#include "ast.h"
2024-09-13 17:18:08 -07:00
#include "stdlib/arrays.h"
2024-02-07 21:52:18 -08:00
typedef struct type_s type_t;
typedef struct arg_s {
const char *name;
type_t *type;
ast_t *default_val;
struct arg_s *next;
} arg_t;
2024-03-08 11:23:16 -08:00
#define ARG_LIST(...) ({\
arg_t *args[] = {__VA_ARGS__}; \
for (size_t i = 0; i < sizeof(args)/sizeof(args[0])-1; i++) \
args[i]->next = args[i+1]; \
args[0]; })
2024-02-07 21:52:18 -08:00
typedef struct tag_s {
const char *name;
int64_t tag_value;
type_t *type;
struct tag_s *next;
} tag_t;
typedef struct type_list_s {
type_t *type;
struct type_list_s *next;
} type_list_t;
struct type_s {
enum {
UnknownType,
AbortType,
ReturnType,
2024-02-07 21:52:18 -08:00
VoidType,
MemoryType,
BoolType,
2024-09-15 12:33:47 -07:00
ByteType,
BigIntType,
2024-02-07 21:52:18 -08:00
IntType,
NumType,
2024-05-18 11:38:41 -07:00
CStringType,
2024-11-17 11:49:03 -08:00
MomentType,
2024-03-03 15:15:45 -08:00
TextType,
2024-02-07 21:52:18 -08:00
ArrayType,
2024-08-10 12:15:38 -07:00
SetType,
2024-02-07 21:52:18 -08:00
TableType,
FunctionType,
2024-02-17 13:56:19 -08:00
ClosureType,
2024-02-07 21:52:18 -08:00
PointerType,
StructType,
EnumType,
2024-09-10 22:31:31 -07:00
OptionalType,
2024-02-07 21:52:18 -08:00
TypeInfoType,
MutexedType,
2024-03-19 11:22:03 -07:00
ModuleType,
2024-02-07 21:52:18 -08:00
} tag;
union {
struct {
2024-02-11 21:41:49 -08:00
} UnknownType, AbortType, VoidType, MemoryType, BoolType;
struct {
type_t *ret;
} ReturnType;
struct {} BigIntType;
2024-02-07 21:52:18 -08:00
struct {
enum { TYPE_IBITS8=8, TYPE_IBITS16=16, TYPE_IBITS32=32, TYPE_IBITS64=64 } bits;
2024-02-07 21:52:18 -08:00
} IntType;
2024-09-15 12:33:47 -07:00
struct {} ByteType;
2024-02-07 21:52:18 -08:00
struct {
enum { TYPE_NBITS32=32, TYPE_NBITS64=64 } bits;
2024-02-07 21:52:18 -08:00
} NumType;
2024-11-17 11:49:03 -08:00
struct {} CStringType, MomentType;
2024-02-15 10:43:46 -08:00
struct {
2024-03-09 15:22:12 -08:00
const char *lang;
struct env_s *env;
2024-03-03 15:15:45 -08:00
} TextType;
2024-02-07 21:52:18 -08:00
struct {
type_t *item_type;
} ArrayType;
2024-02-07 21:52:18 -08:00
struct {
2024-08-10 12:15:38 -07:00
type_t *item_type;
} SetType;
struct {
2024-02-07 21:52:18 -08:00
type_t *key_type, *value_type;
ast_t *default_value;
2024-02-07 21:52:18 -08:00
} TableType;
struct {
arg_t *args;
type_t *ret;
} FunctionType;
2024-02-17 13:56:19 -08:00
struct {
type_t *fn;
} ClosureType;
2024-02-07 21:52:18 -08:00
struct {
type_t *pointed;
bool is_stack:1;
2024-02-07 21:52:18 -08:00
} PointerType;
struct {
2024-02-14 10:43:23 -08:00
const char *name;
2024-02-07 21:52:18 -08:00
arg_t *fields;
2024-03-05 09:49:13 -08:00
bool opaque;
struct env_s *env;
2024-02-07 21:52:18 -08:00
} StructType;
struct {
2024-02-14 10:43:23 -08:00
const char *name;
2024-02-07 21:52:18 -08:00
tag_t *tags;
2024-03-05 09:49:13 -08:00
bool opaque;
struct env_s *env;
2024-02-07 21:52:18 -08:00
} EnumType;
2024-09-10 22:31:31 -07:00
struct {
type_t *type;
} OptionalType, MutexedType;
2024-03-03 10:04:50 -08:00
struct {
const char *name;
type_t *type;
2024-03-21 10:33:10 -07:00
struct env_s *env;
2024-03-03 10:04:50 -08:00
} TypeInfoType;
2024-03-19 11:22:03 -07:00
struct {
const char *name;
} ModuleType;
2024-02-07 21:52:18 -08:00
} __data;
};
#define Type(typetag, ...) new(type_t, .tag=typetag, .__data.typetag={__VA_ARGS__})
#define INT_TYPE Type(BigIntType)
#define NUM_TYPE Type(NumType, .bits=TYPE_NBITS64)
2024-02-07 21:52:18 -08:00
int printf_pointer_size(const struct printf_info *info, size_t n, int argtypes[n], int size[n]);
int printf_type(FILE *stream, const struct printf_info *info, const void *const args[]);
CORD type_to_cord(type_t *t);
PUREFUNC bool type_eq(type_t *a, type_t *b);
PUREFUNC bool type_is_a(type_t *t, type_t *req);
2024-02-07 21:52:18 -08:00
type_t *type_or_type(type_t *a, type_t *b);
type_t *value_type(type_t *a);
typedef enum {NUM_PRECISION_EQUAL, NUM_PRECISION_LESS, NUM_PRECISION_MORE, NUM_PRECISION_INCOMPARABLE} precision_cmp_e;
PUREFUNC precision_cmp_e compare_precision(type_t *a, type_t *b);
PUREFUNC bool has_heap_memory(type_t *t);
PUREFUNC bool has_stack_memory(type_t *t);
PUREFUNC bool can_promote(type_t *actual, type_t *needed);
PUREFUNC const char *enum_single_value_tag(type_t *enum_type, type_t *t);
PUREFUNC bool is_int_type(type_t *t);
PUREFUNC bool is_numeric_type(type_t *t);
PUREFUNC bool is_packed_data(type_t *t);
PUREFUNC size_t type_size(type_t *t);
PUREFUNC size_t type_align(type_t *t);
PUREFUNC size_t unpadded_struct_size(type_t *t);
2024-11-25 11:57:58 -08:00
PUREFUNC type_t *non_optional(type_t *t);
2024-02-17 13:56:19 -08:00
type_t *get_field_type(type_t *t, const char *field_name);
PUREFUNC type_t *get_iterated_type(type_t *t);
2024-02-07 21:52:18 -08:00
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0