nctref/src/types.h

113 lines
1.8 KiB
C
Raw Permalink Normal View History

2023-08-27 19:48:06 +03:00
#ifndef NCTREF_TYPES_H
#define NCTREF_TYPES_H
#include<stddef.h>
#include<stdint.h>
2025-05-03 09:59:30 +03:00
#include<stdbool.h>
2023-08-27 19:48:06 +03:00
typedef enum {
2025-05-03 09:59:30 +03:00
TYPE_TYPE_PRIMITIVE, TYPE_TYPE_RECORD, TYPE_TYPE_POINTER, TYPE_TYPE_FUNCTION, TYPE_TYPE_ARRAY, TYPE_TYPE_GENERIC, TYPE_TYPE_ERROR
2023-08-27 19:48:06 +03:00
} 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;
2024-12-14 18:13:33 +02:00
char **argNames;
2023-08-27 19:48:06 +03:00
union Type **args;
size_t argCount;
} TypeFunction;
typedef struct TypeArray {
TypeType type;
union Type *of;
2025-05-03 09:59:30 +03:00
intmax_t length;
bool lengthIsGeneric;
char *lengthGenericParamName;
size_t lengthGenericParamIdx;
2023-08-27 19:48:06 +03:00
} TypeArray;
2025-02-27 20:10:02 +02:00
typedef struct TypeRecord {
TypeType type;
char *name;
union Type **fieldTypes;
size_t *fieldOffsets;
char **fieldNames;
size_t fieldCount;
} TypeRecord;
2025-05-03 09:59:30 +03:00
typedef struct TypeGeneric {
TypeType type;
char *paramName;
size_t paramIdx;
} TypeGeneric;
2023-08-27 19:48:06 +03:00
typedef union Type {
TypeType type;
TypePrimitive primitive;
TypePointer pointer;
TypeFunction function;
TypeArray array;
2025-02-27 20:10:02 +02:00
TypeRecord record;
2025-05-03 09:59:30 +03:00
TypeGeneric generic;
2023-08-27 19:48:06 +03:00
} Type;
extern Type TYPE_ERROR;
Type *primitive_parse(const char*);
2024-02-13 21:33:49 +02:00
Type *primitive_make(uint16_t width);
2023-08-27 19:48:06 +03:00
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);
2024-11-20 16:36:17 +02:00
int type_is_number(Type *t);
char *type_to_string(Type*);
struct Scope;
Type *type_parametrize(Type *target, struct Scope *scope);
2025-05-03 09:59:30 +03:00
Type *type_shallow_copy(Type *t);
bool type_is_generic(Type *t);
2023-08-27 19:48:06 +03:00
#endif