nctref/src/types.h
2025-05-03 10:00:20 +03:00

123 lines
2.1 KiB
C

#ifndef NCTREF_TYPES_H
#define NCTREF_TYPES_H
#include<stddef.h>
#include<stdint.h>
#include<stdbool.h>
typedef enum {
TYPE_TYPE_PRIMITIVE, TYPE_TYPE_RECORD, TYPE_TYPE_POINTER, TYPE_TYPE_FUNCTION, TYPE_TYPE_ARRAY, TYPE_TYPE_GENERIC, TYPE_TYPE_ERROR
} TypeType;
union Type;
typedef struct TypePrimitive {
TypeType type;
const char *src;
uint16_t width;
int base;
int isFloat;
int isUnsigned;
int isNative;
int isMinimum;
int vector; /* 1 for no vector. */
struct TypePrimitive *next;
} TypePrimitive;
typedef struct TypePointer {
TypeType type;
union Type *of;
} TypePointer;
typedef struct TypeFunction {
TypeType type;
union Type *ret;
char **argNames;
union Type **args;
size_t argCount;
} TypeFunction;
typedef struct TypeArray {
TypeType type;
union Type *of;
intmax_t length;
bool lengthIsGeneric;
char *lengthGenericParamName;
size_t lengthGenericParamIdx;
} TypeArray;
typedef struct TypeRecord {
TypeType type;
char *name;
union Type **fieldTypes;
size_t *fieldOffsets;
char **fieldNames;
size_t fieldCount;
} TypeRecord;
typedef struct TypeGeneric {
TypeType type;
char *paramName;
size_t paramIdx;
} TypeGeneric;
typedef union Type {
TypeType type;
TypePrimitive primitive;
TypePointer pointer;
TypeFunction function;
TypeArray array;
TypeRecord record;
TypeGeneric generic;
} Type;
extern Type TYPE_ERROR;
Type *primitive_parse(const char*);
Type *primitive_make(uint16_t width);
size_t type_size(Type*);
int type_equal(Type*, Type*);
Type *type_pointer_wrap(Type*);
/* 0 = not castable, 1 = explicitly castable, 2 = implicitly castable */
int type_is_castable(Type *from, Type *to);
int type_is_number(Type *t);
char *type_to_string(Type*);
typedef struct Parametrization {
void *param;
struct Parametrization *next;
} Parametrization;
typedef struct {
Parametrization *typeParams;
Parametrization *intParams;
} Parametrizations;
Type *type_parametrize(Type *target, Parametrizations *parametrizations, Parametrizations *renames);
Type *type_shallow_copy(Type *t);
bool type_is_generic(Type *t);
#endif