Cthulhu  0.2.10
Cthulhu compiler collection
ast.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #pragma once
4 
5 #include "scan/scan.h"
6 #include "scan/node.h"
7 
8 #include "cthulhu/tree/ops.h"
9 
10 #include <gmp.h>
11 
12 typedef struct ctu_t ctu_t;
13 typedef struct vector_t vector_t;
14 
15 typedef struct ctu_integer_t {
16  mpz_t value;
20 
21 typedef enum ctu_kind_t {
22  /* expressions */
30 
38 
41 
42  /* builtins */
46 
47  /* statements */
56 
57  /* types */
63 
64  /* real decls */
67 
68  /* type decls */
73 
74  /* intermediates */
80 
81  /* modules */
85 
86 typedef struct ctu_t {
88  const node_t *node;
89 
90  union {
91  struct {
92  char *name;
93  bool exported;
94  const vector_t *attribs;
95 
96  union {
97  /* eCtuImport */
99 
100  /* eCtuStmtWhile */
101  struct {
105  };
106 
107  /* eCtuGlobal|eCtuStmtLocal */
108  struct {
111  bool mut;
112  };
113 
114  /* eCtuDeclFunction|eCtuTypeFunction */
115  struct {
116  const vector_t *params;
117  char *variadic;
120  };
121 
122  /* eCtuDeclTypeAlias */
123  struct {
124  bool newtype;
126  };
127 
128  /* eCtuDeclStruct|eCtuDeclUnion */
129  const vector_t *fields;
130 
131  /* eCtuField */
133 
134  /* eCtuParam */
136 
137  /* eCtuDeclVariant */
138  struct {
140  const vector_t *cases;
141  };
142 
143  /* eCtuVariantCase */
144  struct {
147  };
148  };
149  };
150 
151  /* eCtuExprInt */
153 
154  /* eCtuExprBool */
156 
157  /* eCtuExprString */
158  struct {
159  char *text;
160  size_t length;
161  };
162 
163  /* eCtuExprInit */
164  const vector_t *inits;
165 
166  /* eCtuExprName */
167  const vector_t *path;
168 
169  struct {
170  union {
171  /* eCtuExprBinary */
173 
174  /* eCtuExprCompare */
176  };
177 
180  };
181 
182  struct {
183  union {
184  /* eCtuExprIndex */
186 
187  /* eCtuExprUnary */
189 
190  /* eCtuExprField|eCtuExprFieldIndirect|eCtuFieldInit */
191  char *field;
192 
193  /* eCtuExprCast */
195  };
196 
198  };
199 
200  /* eCtuExprCall */
201  struct {
203  const vector_t *args;
204  };
205 
206  /* eCtuStmtList */
207  const vector_t *stmts;
208 
209  /* eCtuStmtReturn */
211 
212  /* eCtuStmtAssign */
213  struct {
216  };
217 
218  /* eCtuStmtBreak|eCtuStmtContinue */
219  char *label;
220 
221  /* eCtuTypeName */
223 
224  /* eCtuTypePointer */
225  struct {
227  bool array;
228  };
229 
230  /* eCtuTypeArray */
231  struct {
234  };
235 
236  /* eCtuAttrib */
237  struct {
240  };
241 
242  /* eCtuModule */
243  struct {
246  const vector_t *decls;
247  };
248  };
249 } ctu_t;
250 
254 
255 ctu_t *ctu_module(scan_t *scan, where_t where, const vector_t *modspec, const vector_t *imports, const vector_t *decls);
256 ctu_t *ctu_import(scan_t *scan, where_t where, vector_t *path, char *name);
257 
261 
262 ctu_t *ctu_attrib(scan_t *scan, where_t where, const vector_t *path, const vector_t *args);
263 ctu_t *ctu_apply(ctu_t *decl, const vector_t *attribs);
264 
268 
269 ctu_t *ctu_stmt_list(scan_t *scan, where_t where, vector_t *stmts);
270 ctu_t *ctu_stmt_local(scan_t *scan, where_t where, bool mutable, char *name, ctu_t *type, ctu_t *value);
271 ctu_t *ctu_stmt_return(scan_t *scan, where_t where, ctu_t *value);
272 ctu_t *ctu_stmt_while(scan_t *scan, where_t where, char *name, ctu_t *cond, ctu_t *then, ctu_t *other);
273 ctu_t *ctu_stmt_assign(scan_t *scan, where_t where, ctu_t *dst, ctu_t *src);
274 ctu_t *ctu_stmt_break(scan_t *scan, where_t where, char *label);
275 ctu_t *ctu_stmt_continue(scan_t *scan, where_t where, char *label);
276 
277 ctu_t *ctu_stmt_branch(scan_t *scan, where_t where, ctu_t *cond, ctu_t *then, ctu_t *other);
278 
282 
283 ctu_t *ctu_expr_int(scan_t *scan, where_t where, ctu_integer_t value);
284 ctu_t *ctu_expr_bool(scan_t *scan, where_t where, bool value);
285 ctu_t *ctu_expr_string(scan_t *scan, where_t where, char *text, size_t length);
286 ctu_t *ctu_expr_char(scan_t *scan, where_t where, char *text, size_t length);
287 ctu_t *ctu_expr_init(scan_t *scan, where_t where, const vector_t *inits);
288 
289 ctu_t *ctu_expr_call(scan_t *scan, where_t where, ctu_t *callee, const vector_t *args);
290 ctu_t *ctu_expr_name(scan_t *scan, where_t where, const vector_t *path);
291 ctu_t *ctu_expr_cast(scan_t *scan, where_t where, ctu_t *expr, ctu_t *type);
292 
293 ctu_t *ctu_expr_ref(scan_t *scan, where_t where, ctu_t *expr);
294 ctu_t *ctu_expr_deref(scan_t *scan, where_t where, ctu_t *expr);
295 ctu_t *ctu_expr_index(scan_t *scan, where_t where, ctu_t *expr, ctu_t *index);
296 ctu_t *ctu_expr_field(scan_t *scan, where_t where, ctu_t *expr, char *field);
297 ctu_t *ctu_expr_field_indirect(scan_t *scan, where_t where, ctu_t *expr, char *field);
298 
299 ctu_t *ctu_expr_unary(scan_t *scan, where_t where, unary_t unary, ctu_t *expr);
300 ctu_t *ctu_expr_binary(scan_t *scan, where_t where, binary_t binary, ctu_t *lhs, ctu_t *rhs);
301 ctu_t *ctu_expr_compare(scan_t *scan, where_t where, compare_t compare, ctu_t *lhs, ctu_t *rhs);
302 
303 ctu_t *ctu_builtin_sizeof(scan_t *scan, where_t where, ctu_t *type);
304 ctu_t *ctu_builtin_alignof(scan_t *scan, where_t where, ctu_t *type);
305 ctu_t *ctu_builtin_offsetof(scan_t *scan, where_t where, ctu_t *type, char *field);
306 
310 
311 ctu_t *ctu_type_name(scan_t *scan, where_t where, vector_t *path);
312 ctu_t *ctu_type_pointer(scan_t *scan, where_t where, ctu_t *pointer, bool array);
313 ctu_t *ctu_type_array(scan_t *scan, where_t where, ctu_t *array, ctu_t *length);
314 ctu_t *ctu_type_function(scan_t *scan, where_t where, const vector_t *params, ctu_t *return_type);
315 ctu_t *ctu_type_const(scan_t *scan, where_t where, ctu_t *type);
316 
320 
321 ctu_t *ctu_decl_global(scan_t *scan, where_t where, bool exported, bool mutable, char *name, ctu_t *type, ctu_t *value);
322 ctu_t *ctu_decl_function(scan_t *scan, where_t where, bool exported, char *name, const vector_t *params, char *variadic, ctu_t *return_type, ctu_t *body);
323 
327 
328 ctu_t *ctu_decl_typealias(scan_t *scan, where_t where, bool exported, char *name, bool newtype, ctu_t *type);
329 
330 ctu_t *ctu_decl_union(scan_t *scan, where_t where, bool exported, char *name, vector_t *fields);
331 ctu_t *ctu_decl_struct(scan_t *scan, where_t where, bool exported, char *name, vector_t *fields);
332 
333 ctu_t *ctu_decl_variant(scan_t *scan, where_t where, bool exported, char *name, ctu_t *underlying, const vector_t *cases);
334 
338 
339 ctu_t *ctu_field(scan_t *scan, where_t where, char *name, ctu_t *type);
340 ctu_t *ctu_param(scan_t *scan, where_t where, char *name, ctu_t *type);
341 ctu_t *ctu_field_init(scan_t *scan, where_t where, char *name, ctu_t *value);
342 ctu_t *ctu_variant_case(scan_t *scan, where_t where, char *name, bool is_default, ctu_t *expr);
343 
347 
348 typedef struct ctu_params_t {
349  const vector_t *params;
350  char *variadic;
351 } ctu_params_t;
352 
353 ctu_params_t ctu_params_new(const vector_t *params, char *variadic);
ctu_t * ctu_stmt_return(scan_t *scan, where_t where, ctu_t *value)
Definition: ast.c:83
ctu_t * ctu_expr_name(scan_t *scan, where_t where, const vector_t *path)
Definition: ast.c:177
ctu_t * ctu_expr_init(scan_t *scan, where_t where, const vector_t *inits)
Definition: ast.c:162
ctu_t * ctu_expr_char(scan_t *scan, where_t where, char *text, size_t length)
Definition: ast.c:154
ctu_t * ctu_expr_field_indirect(scan_t *scan, where_t where, ctu_t *expr, char *field)
Definition: ast.c:222
ctu_t * ctu_builtin_alignof(scan_t *scan, where_t where, ctu_t *type)
Definition: ast.c:265
ctu_t * ctu_stmt_branch(scan_t *scan, where_t where, ctu_t *cond, ctu_t *then, ctu_t *other)
Definition: ast.c:121
ctu_t * ctu_expr_deref(scan_t *scan, where_t where, ctu_t *expr)
Definition: ast.c:199
ctu_t * ctu_variant_case(scan_t *scan, where_t where, char *name, bool is_default, ctu_t *expr)
Definition: ast.c:397
ctu_t * ctu_stmt_break(scan_t *scan, where_t where, char *label)
Definition: ast.c:107
ctu_t * ctu_expr_cast(scan_t *scan, where_t where, ctu_t *expr, ctu_t *type)
Definition: ast.c:184
ctu_t * ctu_expr_ref(scan_t *scan, where_t where, ctu_t *expr)
Definition: ast.c:192
ctu_t * ctu_import(scan_t *scan, where_t where, vector_t *path, char *name)
Definition: ast.c:42
ctu_t * ctu_expr_binary(scan_t *scan, where_t where, binary_t binary, ctu_t *lhs, ctu_t *rhs)
Definition: ast.c:238
ctu_t * ctu_decl_struct(scan_t *scan, where_t where, bool exported, char *name, vector_t *fields)
Definition: ast.c:358
ctu_t * ctu_apply(ctu_t *decl, const vector_t *attribs)
Definition: ast.c:59
ctu_t * ctu_expr_compare(scan_t *scan, where_t where, compare_t compare, ctu_t *lhs, ctu_t *rhs)
Definition: ast.c:247
ctu_t * ctu_type_array(scan_t *scan, where_t where, ctu_t *array, ctu_t *length)
Definition: ast.c:297
ctu_t * ctu_expr_index(scan_t *scan, where_t where, ctu_t *expr, ctu_t *index)
Definition: ast.c:206
ctu_t * ctu_decl_variant(scan_t *scan, where_t where, bool exported, char *name, ctu_t *underlying, const vector_t *cases)
Definition: ast.c:365
ctu_t * ctu_type_name(scan_t *scan, where_t where, vector_t *path)
Definition: ast.c:282
ctu_t * ctu_expr_string(scan_t *scan, where_t where, char *text, size_t length)
Definition: ast.c:146
ctu_t * ctu_field_init(scan_t *scan, where_t where, char *name, ctu_t *value)
Definition: ast.c:389
ctu_t * ctu_expr_int(scan_t *scan, where_t where, ctu_integer_t value)
Definition: ast.c:132
ctu_t * ctu_type_const(scan_t *scan, where_t where, ctu_t *type)
Definition: ast.c:313
ctu_t * ctu_type_pointer(scan_t *scan, where_t where, ctu_t *pointer, bool array)
Definition: ast.c:289
ctu_t * ctu_module(scan_t *scan, where_t where, const vector_t *modspec, const vector_t *imports, const vector_t *decls)
Definition: ast.c:33
ctu_t * ctu_param(scan_t *scan, where_t where, char *name, ctu_t *type)
Definition: ast.c:381
ctu_t * ctu_builtin_sizeof(scan_t *scan, where_t where, ctu_t *type)
Definition: ast.c:258
ctu_t * ctu_field(scan_t *scan, where_t where, char *name, ctu_t *type)
Definition: ast.c:373
ctu_t * ctu_stmt_continue(scan_t *scan, where_t where, char *label)
Definition: ast.c:114
ctu_t * ctu_stmt_assign(scan_t *scan, where_t where, ctu_t *dst, ctu_t *src)
Definition: ast.c:99
ctu_t * ctu_stmt_local(scan_t *scan, where_t where, bool mutable, char *name, ctu_t *type, ctu_t *value)
Definition: ast.c:74
ctu_t * ctu_stmt_list(scan_t *scan, where_t where, vector_t *stmts)
Definition: ast.c:67
ctu_t * ctu_builtin_offsetof(scan_t *scan, where_t where, ctu_t *type, char *field)
Definition: ast.c:272
ctu_t * ctu_expr_unary(scan_t *scan, where_t where, unary_t unary, ctu_t *expr)
Definition: ast.c:230
ctu_kind_t
Definition: ast.h:21
@ eCtuField
Definition: ast.h:75
@ eCtuExprField
Definition: ast.h:36
@ eCtuImport
Definition: ast.h:82
@ eCtuExprAlignOf
Definition: ast.h:44
@ eCtuDeclStruct
Definition: ast.h:72
@ eCtuStmtBranch
Definition: ast.h:55
@ eCtuTypeArray
Definition: ast.h:61
@ eCtuStmtWhile
Definition: ast.h:51
@ eCtuTypePointer
Definition: ast.h:59
@ eCtuTypeConst
Definition: ast.h:62
@ eCtuStmtContinue
Definition: ast.h:54
@ eCtuAttrib
Definition: ast.h:77
@ eCtuExprBinary
Definition: ast.h:32
@ eCtuParam
Definition: ast.h:76
@ eCtuModule
Definition: ast.h:83
@ eCtuDeclUnion
Definition: ast.h:71
@ eCtuStmtAssign
Definition: ast.h:52
@ eCtuExprChar
Definition: ast.h:26
@ eCtuDeclVariant
Definition: ast.h:70
@ eCtuExprFieldIndirect
Definition: ast.h:37
@ eCtuDeclTypeAlias
Definition: ast.h:69
@ eCtuExprName
Definition: ast.h:27
@ eCtuDeclGlobal
Definition: ast.h:65
@ eCtuExprCast
Definition: ast.h:28
@ eCtuVariantCase
Definition: ast.h:79
@ eCtuExprDeref
Definition: ast.h:40
@ eCtuStmtBreak
Definition: ast.h:53
@ eCtuTypeName
Definition: ast.h:58
@ eCtuExprCompare
Definition: ast.h:31
@ eCtuExprSizeOf
Definition: ast.h:43
@ eCtuStmtList
Definition: ast.h:48
@ eCtuStmtLocal
Definition: ast.h:49
@ eCtuStmtReturn
Definition: ast.h:50
@ eCtuExprCall
Definition: ast.h:34
@ eCtuExprInt
Definition: ast.h:23
@ eCtuDeclFunction
Definition: ast.h:66
@ eCtuExprString
Definition: ast.h:25
@ eCtuExprOffsetOf
Definition: ast.h:45
@ eCtuExprInit
Definition: ast.h:29
@ eCtuExprBool
Definition: ast.h:24
@ eCtuFieldInit
Definition: ast.h:78
@ eCtuExprRef
Definition: ast.h:39
@ eCtuExprUnary
Definition: ast.h:33
@ eCtuTypeFunction
Definition: ast.h:60
@ eCtuExprIndex
Definition: ast.h:35
ctu_t * ctu_expr_call(scan_t *scan, where_t where, ctu_t *callee, const vector_t *args)
Definition: ast.c:169
ctu_t * ctu_expr_bool(scan_t *scan, where_t where, bool value)
Definition: ast.c:139
ctu_t * ctu_decl_union(scan_t *scan, where_t where, bool exported, char *name, vector_t *fields)
Definition: ast.c:351
ctu_t * ctu_type_function(scan_t *scan, where_t where, const vector_t *params, ctu_t *return_type)
Definition: ast.c:305
ctu_t * ctu_decl_function(scan_t *scan, where_t where, bool exported, char *name, const vector_t *params, char *variadic, ctu_t *return_type, ctu_t *body)
Definition: ast.c:331
ctu_t * ctu_attrib(scan_t *scan, where_t where, const vector_t *path, const vector_t *args)
Definition: ast.c:51
ctu_t * ctu_decl_global(scan_t *scan, where_t where, bool exported, bool mutable, char *name, ctu_t *type, ctu_t *value)
Definition: ast.c:322
ctu_params_t ctu_params_new(const vector_t *params, char *variadic)
Definition: ast.c:410
ctu_t * ctu_decl_typealias(scan_t *scan, where_t where, bool exported, char *name, bool newtype, ctu_t *type)
Definition: ast.c:343
ctu_t * ctu_expr_field(scan_t *scan, where_t where, ctu_t *expr, char *field)
Definition: ast.c:214
ctu_t * ctu_stmt_while(scan_t *scan, where_t where, char *name, ctu_t *cond, ctu_t *then, ctu_t *other)
Definition: ast.c:90
sign_t
integer sign
Definition: ops.h:104
binary_t
all binary operators
Definition: ops.h:32
digit_t
digit width
Definition: ops.h:96
unary_t
all unary operators
Definition: ops.h:48
compare_t
all comparison operators
Definition: ops.h:40
digit_t digit
Definition: ast.h:17
sign_t sign
Definition: ast.h:18
mpz_t value
Definition: ast.h:16
const vector_t * params
Definition: ast.h:349
char * variadic
Definition: ast.h:350
Definition: ast.h:86
ctu_t * then
Definition: ast.h:103
ctu_t * array_type
Definition: ast.h:232
const vector_t * attrib_path
Definition: ast.h:238
bool array
Definition: ast.h:227
ctu_t * body
Definition: ast.h:119
ctu_t * cond
Definition: ast.h:102
ctu_t * other
Definition: ast.h:104
ctu_kind_t kind
Definition: ast.h:87
const vector_t * attribs
Definition: ast.h:94
binary_t binary
Definition: ast.h:172
ctu_t * callee
Definition: ast.h:202
size_t length
Definition: ast.h:160
ctu_t * index
Definition: ast.h:185
compare_t compare
Definition: ast.h:175
const vector_t * import_path
Definition: ast.h:98
const vector_t * fields
Definition: ast.h:129
const vector_t * attrib_args
Definition: ast.h:239
ctu_t * value
Definition: ast.h:110
const vector_t * inits
Definition: ast.h:164
ctu_t * underlying
Definition: ast.h:139
char * field
Definition: ast.h:191
ctu_t * case_value
Definition: ast.h:146
char * text
Definition: ast.h:159
const vector_t * imports
Definition: ast.h:245
const vector_t * decls
Definition: ast.h:246
char * label
Definition: ast.h:219
const vector_t * params
Definition: ast.h:116
vector_t * type_name
Definition: ast.h:222
ctu_t * param_type
Definition: ast.h:135
bool newtype
Definition: ast.h:124
ctu_integer_t integer
Definition: ast.h:152
bool mut
Definition: ast.h:111
const vector_t * args
Definition: ast.h:203
ctu_t * src
Definition: ast.h:215
bool default_case
Definition: ast.h:145
ctu_t * return_type
Definition: ast.h:118
ctu_t * rhs
Definition: ast.h:179
const node_t * node
Definition: ast.h:88
ctu_t * lhs
Definition: ast.h:178
ctu_t * array_length
Definition: ast.h:233
const vector_t * modspec
Definition: ast.h:244
ctu_t * cast
Definition: ast.h:194
const vector_t * stmts
Definition: ast.h:207
char * variadic
Definition: ast.h:117
ctu_t * type
Definition: ast.h:109
ctu_t * field_type
Definition: ast.h:132
ctu_t * expr
Definition: ast.h:197
ctu_t * pointer
Definition: ast.h:226
ctu_t * result
Definition: ast.h:210
const vector_t * path
Definition: ast.h:167
ctu_t * type_alias
Definition: ast.h:125
unary_t unary
Definition: ast.h:188
const vector_t * cases
Definition: ast.h:140
bool bool_value
Definition: ast.h:155
char * name
Definition: ast.h:92
bool exported
Definition: ast.h:93
ctu_t * dst
Definition: ast.h:214
a position in a source file
Definition: node.h:23
a source file scanner
Definition: scan.h:24
a generic vector of pointers
Definition: vector.c:16
a location inside a scanner locations are inclusive and 0-based
Definition: where.h:23