aboutsummaryrefslogtreecommitdiff
path: root/src/ast.c
blob: f0bb528d67ed60c1949787299ab2ad5612af0658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
#include "../include/ast.h"
#include "../include/parser.h"
#include "../include/compiler.h"
#include "../include/std/log.h"
#include "../include/std/hash.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>

#define throw(result) do { throw_debug_msg; longjmp(context->env, (result)); } while(0)
#define throw_if_error(result) \
    do { \
        int return_if_result; \
        return_if_result = (result); \
        if((return_if_result) != 0) \
            throw(return_if_result); \
    } while(0)

static void variable_type_resolve(VariableType *self, AstCompilerContext *context, AstResolvedType *resolved_type);
static void ast_resolve(Ast *self, AstCompilerContext *context);

static void scope_named_object_init(ScopeNamedObject *self) {
    self->type = NAMED_OBJECT_NONE;
    self->value.data = NULL;
    self->resolve_data = NULL;
}

/* TODO: Remove these? */
#if 0
static bool scope_named_object_equals(ScopeNamedObject *self, ScopeNamedObject *other) {
    /* This should be fine, without checking ScopeNamedObject type, since they are only equal if the types are equal as well */
    return self->value.lhs_expr == other->value.lhs_expr;
}

static BufferView scope_named_object_get_type_name(ScopeNamedObject *self) {
    BufferView result;
    switch(self->type) {
        case NAMED_OBJECT_NONE:
            result = create_buffer_view("", 0);
            break;
        case NAMED_OBJECT_LHS_EXPR:
            result = variable_type_get_name(&self->value.lhs_expr->type);
            break;
        case NAMED_OBJECT_FUNC_PARAM:
            result = variable_type_get_name(&self->value.func_param->type);
            break;
    }
    return result;
}
#endif

static BufferView ast_resolved_type_get_name(AstResolvedType *self) {
    BufferView result;
    switch(self->type) {
        case RESOLVED_TYPE_NONE:
            result = create_buffer_view("", 0);
            break;
        case RESOLVED_TYPE_LHS_EXPR:
            result = self->value.lhs_expr->var_name;
            break;
        case RESOLVED_TYPE_FUNC_SIG: {
            if(!self->value.func_sig->func_decl || !self->value.func_sig->func_decl->lhs_expr) {
                /*
                    TODO: Use function signature string from the source file, which will also help with error reference.
                    Currently this would point to an invalid location in the source file
                */
                result = create_buffer_view("fn()", 0);
                break;
            }
            result = self->value.func_sig->func_decl->lhs_expr->var_name;
            break;
        }
    }
    return result;
}

static void ast_resolved_type_init(AstResolvedType *self) {
    self->value.data = NULL;
    self->type = RESOLVED_TYPE_NONE;
}

static bool ast_resolved_type_equals(AstResolvedType *self, AstResolvedType *other) {
    if(self->type != other->type)
        return bool_false;

    switch(self->type) {
        case RESOLVED_TYPE_NONE:
            return bool_true;
        case RESOLVED_TYPE_LHS_EXPR:
            return self->value.lhs_expr == other->value.lhs_expr;
        case RESOLVED_TYPE_FUNC_SIG:
            return function_signature_equals(self->value.func_sig, other->value.func_sig);
    }
    return bool_false;
}

static AstResolvedType lhs_expr_get_resolved_type(LhsExpr *self, AstCompilerContext *context) {
    AstResolvedType result;
    variable_type_resolve(&self->type, context, &result);
    if(result.type != RESOLVED_TYPE_NONE)
        return result;

    assert(self->rhs_expr);
    ast_resolve(self->rhs_expr, context);
    return self->rhs_expr->resolve_data.type;
}

/* TODO: Detect recursive dependency? is it even possible for function parameters? (it would normally be a recursive dependency on the function declaration) */
static void function_parameter_resolve(FunctionParameter *self, AstCompilerContext *context) {
    if(self->resolve_data.status == AST_RESOLVED)
        return;
    self->resolve_data.status = AST_RESOLVING;
    variable_type_resolve(&self->type, context, &self->resolve_data.type);
    self->resolve_data.status = AST_RESOLVED;
}

static AstResolvedType scope_named_object_get_resolved_type(ScopeNamedObject *self, AstCompilerContext *context) {
    AstResolvedType result;
    switch(self->type) {
        case NAMED_OBJECT_NONE:
            /* Type not resolved */
            assert(bool_false);
            ast_resolved_type_init(&result);
            return result;
        case NAMED_OBJECT_LHS_EXPR:
            result = lhs_expr_get_resolved_type(self->value.lhs_expr, context);
            break;
        case NAMED_OBJECT_FUNC_PARAM:
            function_parameter_resolve(self->value.func_param, context);
            result = self->value.func_param->resolve_data.type;
            break;
    }
    return result;
}

static void resolve_data_init(AstResolveData *self) {
    self->status = AST_NOT_RESOLVED;
    ast_resolved_type_init(&self->type);
    self->ir_reg = 0;
}

int ast_create(ArenaAllocator *allocator, void *value, AstType type, Ast **result) {
    return_if_error(arena_allocator_alloc(allocator, sizeof(Ast), (void**)result));
    (*result)->value.data = value;
    (*result)->type = type;
    resolve_data_init(&(*result)->resolve_data);
    (*result)->parser = NULL;
    return 0;
}

static bool ast_is_decl(Ast *self) {
    /* TODO: Add more types as they are introduced */
    return self->type == AST_FUNCTION_DECL || self->type == AST_STRUCT_DECL;
}

BufferView ast_get_name(Ast *self) {
    BufferView name;
    switch(self->type) {
        case AST_FUNCTION_DECL:
        case AST_STRUCT_DECL:
        case AST_IMPORT:
        case AST_STRING:
        case AST_BINOP:
        case AST_IF_STATEMENT:
        case AST_WHILE_STATEMENT:
        case AST_RETURN:
            name = create_buffer_view_null();
            break;
        case AST_NUMBER:
            name = self->value.number->code_ref;
            break;
        case AST_BOOL:
            name = self->value.bool->code_ref;
            break;
        case AST_LHS:
            name = self->value.lhs_expr->var_name;
            break;
        case AST_ASSIGN:
            name = ast_get_name(self->value.assign_expr->lhs_expr);
            break;
        case AST_FUNCTION_CALL:
            name = self->value.func_call->func.name;
            break;
        case AST_VARIABLE:
            name = self->value.variable->name;
            break;
        case AST_STRUCT_FIELD:
            name = self->value.struct_field->name;
            break;
    }
    return name;
}

static BufferView ast_get_code_reference(Ast *self) {
    return ast_get_name(self);
}

int function_signature_init(FunctionSignature *self, ArenaAllocator *allocator) {
    self->resolved = bool_false;
    self->func_decl = NULL;
    return_if_error(buffer_init(&self->parameters, allocator));
    return buffer_init(&self->return_types, allocator);
}

static FunctionParameter* function_signature_get_parameter_by_name(FunctionSignature *self, BufferView name) {
    FunctionParameter *param, *param_end;
    param = buffer_begin(&self->parameters);
    param_end = buffer_end(&self->parameters);
    for(; param != param_end; ++param) {
        if(buffer_view_equals(&name, &param->name))
            return param;
    }
    return NULL;
}

int function_signature_add_parameter(FunctionSignature *self, const FunctionParameter *new_param) {
    const FunctionParameter *existing_param = function_signature_get_parameter_by_name(self, new_param->name);
    if(existing_param)
        return AST_ERR_DEF_DUP;
    return buffer_append(&self->parameters, new_param, sizeof(FunctionParameter));
}

int function_signature_add_return_type(FunctionSignature *self, const VariableType *var_type) {
    FunctionReturnType return_type;
    return_type.type = *var_type;
    ast_resolved_type_init(&return_type.resolved_type);
    return buffer_append(&self->return_types, &return_type, sizeof(return_type));
}

static CHECK_RESULT bool function_parameter_equals(FunctionParameter *self, FunctionParameter *other) {
    /* It's fine if the name of the parameter is different. Only the type matters */
    return ast_resolved_type_equals(&self->resolve_data.type, &other->resolve_data.type);
}

static CHECK_RESULT bool function_parameters_equals(Buffer *func_params, Buffer *other_func_params) {
    FunctionParameter *func_param, *func_param_end;
    FunctionParameter *other_func_param, *other_func_param_end;

    func_param = buffer_begin(func_params);
    func_param_end = buffer_end(func_params);
    other_func_param = buffer_begin(other_func_params);
    other_func_param_end = buffer_end(other_func_params);
    /* Different number of arguments */
    if(func_param_end - func_param != other_func_param_end - other_func_param)
        return bool_false;

    for(; func_param != func_param_end; ++func_param, ++other_func_param) {
        if(!function_parameter_equals(func_param, other_func_param))
            return bool_false;
    }
    return bool_true;
}

static CHECK_RESULT bool function_return_type_equals(FunctionReturnType *self, FunctionReturnType *other) {
    return ast_resolved_type_equals(&self->resolved_type, &other->resolved_type);
}

static CHECK_RESULT bool function_return_types_equals(Buffer *func_return_types, Buffer *other_func_return_types) {
    FunctionReturnType *func_return_type, *func_return_type_end;
    FunctionReturnType *other_func_return_type, *other_func_return_type_end;

    func_return_type = buffer_begin(func_return_types);
    func_return_type_end = buffer_end(func_return_types);
    other_func_return_type = buffer_begin(other_func_return_types);
    other_func_return_type_end = buffer_end(other_func_return_types);
    /* Different number of arguments */
    if(func_return_type_end - func_return_type != other_func_return_type_end - other_func_return_type)
        return bool_false;

    for(; func_return_type != func_return_type_end; ++func_return_type, ++other_func_return_type) {
        if(!function_return_type_equals(func_return_type, other_func_return_type))
            return bool_false;
    }
    return bool_true;
}

bool function_signature_equals(FunctionSignature *self, FunctionSignature *other) {
    if(!function_parameters_equals(&self->parameters, &other->parameters))
        return bool_false;
    return function_return_types_equals(&self->return_types, &other->return_types);
}

void function_parameter_init(FunctionParameter *self) {
    self->name = create_buffer_view_null();
    self->type.type = VARIABLE_TYPE_NONE;
    self->type.value.variable = NULL;
    resolve_data_init(&self->resolve_data);
}

int funcdecl_init(FunctionDecl *self, FunctionSignature *signature, Scope *parent, ArenaAllocator *allocator) {
    self->lhs_expr = NULL;
    self->signature = signature;
    self->ir_func_index = 0;
    return scope_init(&self->body, parent, allocator);
}

int funccall_init(FunctionCall *self, BufferView name, ArenaAllocator *allocator) {
    variable_init(&self->func, name);
    return buffer_init(&self->args, allocator);
}

int structdecl_init(StructDecl *self, Scope *parent, ArenaAllocator *allocator) {
    self->fields_num_pointers = 0;
    self->fields_fixed_size_bytes = 0;
    self->is_signed = bool_false;
    return scope_init(&self->body, parent, allocator);
}

int structdecl_add_field(StructDecl *self, StructField *field, ArenaAllocator *allocator) {
    Ast *body_obj;
    return_if_error(ast_create(allocator, field, AST_STRUCT_FIELD, &body_obj));
    return scope_add_child(&self->body, body_obj);
}

LhsExpr* structdecl_get_field_by_name(StructDecl *self, BufferView field_name) {
    Ast* result;
    if(!hash_map_get(&self->body.named_objects, field_name, &result))
        return NULL;
    return result->value.lhs_expr;
}

void structfield_init(StructField *self, BufferView name, VariableType *type) {
    self->name = name;
    self->type = *type;
}

void lhsexpr_init(LhsExpr *self, DeclFlag decl_flag, BufferView var_name) {
    assert(!((decl_flag & DECL_FLAG_EXTERN) && (decl_flag & DECL_FLAG_EXPORT)) && "Expression cant be both extern and export");
    self->decl_flags = decl_flag;
    self->type.type = VARIABLE_TYPE_NONE;
    self->type.value.variable = NULL;
    self->var_name = var_name;
    self->rhs_expr = NULL;
    self->extern_index = 0;
}

void assignmentexpr_init(AssignmentExpr *self, Ast *lhs_expr, Ast *rhs_expr) {
    self->lhs_expr = lhs_expr;
    self->rhs_expr = rhs_expr;
}

void import_init(Import *self, BufferView path) {
    self->path = path;
    self->file_scope = NULL;
}

int string_init(String *self, BufferView str) {
    /* TODO: Convert special characters. For example \n should be converted to binary newline etc */
    self->str = str;
    return 0;
}

void number_init(Number *self, AmalNumber *value, BufferView code_ref) {
    self->value = *value;
    self->code_ref = code_ref;
}

void ast_bool_init(AstBool *self, bool value, BufferView code_ref) {
    self->value = value;
    self->code_ref = code_ref;
}

void variable_init(Variable *self, BufferView name) {
    self->name = name;
    scope_named_object_init(&self->resolved_var);
}

void binop_init(Binop *self) {
    self->lhs = NULL;
    self->rhs = NULL;
    self->type = BINOP_ADD;
    self->grouped = bool_false;
}

int if_statement_init(IfStatement *self, Scope *parent, ArenaAllocator *allocator) {
    self->condition = NULL;
    self->else_if_stmt = NULL;
    return scope_init(&self->body, parent, allocator);
}

int else_if_statement_init(ElseIfStatement *self, Scope *parent, ArenaAllocator *allocator) {
    self->condition = NULL;
    self->next_else_if_stmt = NULL;
    return scope_init(&self->body, parent, allocator);
}

int while_statement_init(WhileStatement *self, Scope *parent, ArenaAllocator *allocator) {
    self->condition = NULL;
    return scope_init(&self->body, parent, allocator);
}

void return_expr_init(ReturnExpr *self, Ast *rhs_expr) {
    self->rhs_expr = rhs_expr;
}

int scope_init(Scope *self, Scope *parent, ArenaAllocator *allocator) {
    return_if_error(buffer_init(&self->ast_objects, allocator));
    return_if_error(hash_map_init(&self->named_objects, allocator, sizeof(Ast*), hash_map_compare_string, amal_hash_string));
    self->parent = parent;
    self->function_signature = NULL;
    return 0;
}

int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ArenaAllocator *allocator) {
    char null_terminator;
    null_terminator = '\0';
    self->parser = NULL;

    return_if_error(buffer_init(&self->canonical_path, allocator));
    return_if_error(buffer_append(&self->canonical_path, canonical_path.data, canonical_path.size));
    return_if_error(buffer_append(&self->canonical_path, &null_terminator, 1));
    /* To exclude null-terminator character from size but not from data */
    self->canonical_path.size -= 1;
    return 0;
}

int scope_add_child(Scope *self, Ast *child) {
    Ast *existing_child;
    bool child_already_exists;

    if(child->type == AST_LHS) {
        BufferView var_name;
        var_name = child->value.lhs_expr->var_name;
        assert(var_name.data);
        child_already_exists = hash_map_get(&self->named_objects, var_name, &existing_child);
        if(child_already_exists)
            return AST_ERR_DEF_DUP;

        cleanup_if_error(hash_map_insert(&self->named_objects, var_name, &child));
    }
    cleanup_if_error(buffer_append(&self->ast_objects, &child, sizeof(Ast*)));
    return 0;

    cleanup:
    return AST_ERR;
}

void scope_resolve(Scope *self, AstCompilerContext *context) {
    Ast **ast = buffer_begin(&self->ast_objects);
    Ast **ast_end = buffer_end(&self->ast_objects);
    Scope *prev_scope = context->scope;

    context->scope = self;
    for(; ast != ast_end; ++ast) {
        ast_resolve(*ast, context);
    }
    context->scope = prev_scope;
}

static void compiler_print_error(amal_compiler *compiler, const char *ref, const char *fmt, ...) {
    Tokenizer *tokenizer;
    va_list args;
    va_start(args, fmt);

    tokenizer = amal_compiler_find_tokenizer_by_code_reference(compiler, ref);
    if(!tokenizer) {
        amal_log_error("Failed to find tokenizer for code reference %p. Is it an invalid reference?", ref ? ref : "(null)");
        vfprintf(stderr, fmt, args);
        fputc('\n', stderr);
        return;
    }
    tokenizer_print_error_args(tokenizer, tokenizer_get_code_reference_index(tokenizer, ref), fmt, args);
    va_end(args);
}

static void __scope_get_resolved_variable(Scope *self, Scope *start, AstCompilerContext *context, BufferView name, ScopeNamedObject *result) {
    Ast *ast_result;
    bool exists;
    Scope *prev_scope;

    assert(self);
    exists = hash_map_get(&self->named_objects, name, &ast_result);
    if(!exists) {
        if(self->function_signature) {
            FunctionParameter *func_param = function_signature_get_parameter_by_name(self->function_signature, name);
            if(func_param) {
                prev_scope = context->scope;
                context->scope = self;
                function_parameter_resolve(func_param, context);
                context->scope = prev_scope;

                result->type = NAMED_OBJECT_FUNC_PARAM;
                result->value.func_param = func_param;
                result->resolve_data = &func_param->resolve_data;
                return;
            }

            /* TODO: Remove this when closures can capture variables */
            assert(self->parent == &context->parser->struct_decl.body);
        }

        if(self->parent) {
            __scope_get_resolved_variable(self->parent, start, context, name, result);
            return;
        }

        compiler_print_error(context->compiler, name.data, "Undefined reference to variable \"%.*s\"", name.size, name.data);
        throw(AST_ERR);
    }

    /*
        Need to change scope here because we are changing the visible scope
        and the ast object may be in another scope than the current
        resolving ast.
    */
    prev_scope = context->scope;
    context->scope = self;
    ast_resolve(ast_result, context);
    context->scope = prev_scope;

    assert(ast_result->type == AST_LHS);
    result->type = NAMED_OBJECT_LHS_EXPR;
    result->value.lhs_expr = ast_result->value.lhs_expr;
    result->resolve_data = &ast_result->resolve_data;
}

static void scope_get_resolved_variable(Scope *self, AstCompilerContext *context, BufferView name, ScopeNamedObject *result) {
    __scope_get_resolved_variable(self, self, context, name, result);
}

static void function_return_type_resolve(FunctionReturnType *self, AstCompilerContext *context) {
    variable_type_resolve(&self->type, context, &self->resolved_type);
}

static void function_signature_resolve(FunctionSignature *self, AstCompilerContext *context) {
    if(self->resolved)
        return;

    {
        FunctionParameter *param, *param_end;
        param = buffer_begin(&self->parameters);
        param_end = buffer_end(&self->parameters);
        for(; param != param_end; ++param) {
            function_parameter_resolve(param, context);
        }
    }
    {
        FunctionReturnType *return_type, *return_type_end;
        return_type = buffer_begin(&self->return_types);
        return_type_end = buffer_end(&self->return_types);
        for(; return_type != return_type_end; ++return_type) {
            function_return_type_resolve(return_type, context);
        }
    }

    self->resolved = bool_true;
}

static void variable_resolve(Variable *self, AstCompilerContext *context, AstResolvedType *resolved_type) {
    if(self->resolved_var.type == NAMED_OBJECT_NONE)
        scope_get_resolved_variable(context->scope, context, self->name, &self->resolved_var);
    *resolved_type = scope_named_object_get_resolved_type(&self->resolved_var, context);
}

void variable_type_resolve(VariableType *self, AstCompilerContext *context, AstResolvedType *resolved_type) {
    switch(self->type) {
        case VARIABLE_TYPE_NONE:
            ast_resolved_type_init(resolved_type);
            return;
        case VARIABLE_TYPE_VARIABLE:
            variable_resolve(self->value.variable, context, resolved_type);
            break;
        case VARIABLE_TYPE_SIGNATURE:
            function_signature_resolve(self->value.signature, context);
            resolved_type->type = RESOLVED_TYPE_FUNC_SIG;
            resolved_type->value.func_sig = self->value.signature;
            break;
    }
}

static void lhsexpr_resolve_rhs(LhsExpr *self, AstCompilerContext *context, AstResolvedType *result) {
    ast_resolve(self->rhs_expr, context);
    if(ast_is_decl(self->rhs_expr)) {
        result->type = RESOLVED_TYPE_LHS_EXPR;
        result->value.lhs_expr = self;
        /* Structs resolved type becomes the lhs while functions resolved type becomes the function signature they own */
        if (self->rhs_expr->type == AST_STRUCT_DECL)
            self->rhs_expr->resolve_data.type = *result;
    } else {
        *result = self->rhs_expr->resolve_data.type;
    }
}

static void lhsexpr_resolve(Ast *ast, AstCompilerContext *context) {
    LhsExpr *self;

    assert(ast->type == AST_LHS);
    self = ast->value.lhs_expr;

    variable_type_resolve(&self->type, context, &ast->resolve_data.type);

    /*
    TODO: When parameters and return types are implemented, AST_RESOLVE_END should be set after
    the parameters and return types have been resolved as recursive function calls should
    be allowed but recursive function calls still require parameters and return types to be known.
    */
    if(self->rhs_expr) {
        AstResolvedType rhs_resolve_type;
        if(self->rhs_expr->type == AST_FUNCTION_DECL) {
            /*
                The function declaration itself always resolves the signature, but we also do it here because we
                want to have the signature solved before setting the lhs expr as solved. Also function signatures can exist
                without lhs expr (anonymous function).
            */
            function_signature_resolve(self->rhs_expr->value.func_decl->signature, context);
            ast->resolve_data.status = AST_RESOLVED;
            /*
                If rhs is a function declaration then there is no need to wait until it has been resolved before setting the type.
                We still need to continue after this, so rhs can be resolved.
            */
            if(ast->resolve_data.type.type == RESOLVED_TYPE_NONE) {
                ast->resolve_data.type.type = RESOLVED_TYPE_FUNC_SIG;
                ast->resolve_data.type.value.func_sig = self->rhs_expr->value.func_decl->signature;
            }
        }

        lhsexpr_resolve_rhs(self, context, &rhs_resolve_type);

        /* TODO: Add casting */
        if(ast->resolve_data.type.type == RESOLVED_TYPE_LHS_EXPR && !ast_resolved_type_equals(&ast->resolve_data.type, &rhs_resolve_type)) {
            /* 
                TODO: Instead of using self->var_name, use type name. This cant be done right now because
                type can be function signature.
            */
            compiler_print_error(context->compiler, self->var_name.data, "Variable type and variable assignment type (right-hand side) do not match");
            throw(AST_ERR);
        }
        ast->resolve_data.type = rhs_resolve_type;
    }
}

static LhsExpr* binop_get_lhs_expr(Binop *self) {
    if(self->rhs) {
        if(self->rhs->type == AST_LHS)
            return self->rhs->value.lhs_expr;
        else if(self->rhs->type == AST_BINOP)
            return binop_get_lhs_expr(self->rhs->value.binop);
    } else {
        if(self->lhs->type == AST_LHS)
            return self->lhs->value.lhs_expr;
        else if(self->lhs->type == AST_BINOP)
            return binop_get_lhs_expr(self->lhs->value.binop);
    }
    return NULL;
}

static void assignmentexpr_resolve(Ast *ast, AstCompilerContext *context) {
    AssignmentExpr *self;
    bool is_lhs_const;
    is_lhs_const = bool_false;

    assert(ast->type == AST_ASSIGN);
    self = ast->value.assign_expr;

    ast_resolve(self->lhs_expr, context);
    ast_resolve(self->rhs_expr, context);

    if(self->lhs_expr->type == AST_VARIABLE) {
        /* TODO: Allow non-const function param */
        const ScopeNamedObject *resolved_var = &self->lhs_expr->value.variable->resolved_var;
        if(resolved_var->type == NAMED_OBJECT_FUNC_PARAM || LHS_EXPR_IS_CONST(resolved_var->value.lhs_expr))
            is_lhs_const = bool_true;
    } else if(self->lhs_expr->type == AST_BINOP) {
        LhsExpr *lhs_expr;
        lhs_expr = binop_get_lhs_expr(self->lhs_expr->value.binop);
        is_lhs_const = LHS_EXPR_IS_CONST(lhs_expr);
    }

    /* This also covers extern variables, since extern variables are always const */
    /* TODO: var.field type expressions should also be checked */
    if(is_lhs_const) {
        compiler_print_error(context->compiler, ast_get_code_reference(self->lhs_expr).data, "Can't assign to a const variable");
        throw(AST_ERR);
    }

    /* TODO: Add casting */
    if(!ast_resolved_type_equals(&self->lhs_expr->resolve_data.type, &self->rhs_expr->resolve_data.type)) {
        BufferView rhs_type_name = ast_resolved_type_get_name(&self->rhs_expr->resolve_data.type);
        BufferView lhs_type_name = ast_resolved_type_get_name(&self->lhs_expr->resolve_data.type);
        /* 
            TODO: Instead of using self->var_name, use type name. This cant be done right now because
            type can be function signature.
        */
        compiler_print_error(context->compiler, ast_get_code_reference(self->lhs_expr).data,
            "Can't cast data of type %.*s to type %.*s", rhs_type_name.size, rhs_type_name.data, lhs_type_name.size, lhs_type_name.data);
        throw(AST_ERR);
    }

    ast->resolve_data.type = self->lhs_expr->resolve_data.type;
}

static void import_resolve(Ast *ast, AstCompilerContext *context) {
    Import *self;
    assert(ast->type == AST_IMPORT);
    (void)context;
    self = ast->value.import;
    ast->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR;
    ast->resolve_data.type.value.lhs_expr = &self->file_scope->file_scope_ref->parser->file_decl;
}

static Scope* lhsexpr_get_scope(LhsExpr *self) {
    AstValue value = self->rhs_expr->value;
    switch(self->rhs_expr->type) {
        case AST_FUNCTION_DECL:
            return &value.func_decl->body;
        case AST_STRUCT_DECL:
            return &value.struct_decl->body;
        case AST_IMPORT:
            /* *import_index = 1 + value.import->file_scope->import_index;*/
            assert(bool_false);
            return &value.import->file_scope->file_scope_ref->parser->struct_decl.body;
        default:
            break;
    }
    assert(bool_false && "Expected lhsexpr_get_scope to only be called for non-extern function declaration, struct declaration and import");
    return NULL;
}

static Scope* ast_resolved_type_get_scope(AstResolvedType *self) {
    switch(self->type) {
        case RESOLVED_TYPE_NONE:
            assert(bool_false && "Expected ast_resolved_type_get_scope to only be called for a resolved object");
            return NULL;
        case RESOLVED_TYPE_LHS_EXPR:
            return lhsexpr_get_scope(self->value.lhs_expr);
        case RESOLVED_TYPE_FUNC_SIG:
            assert(self->value.func_sig->func_decl);
            return &self->value.func_sig->func_decl->body;
    }
    return NULL;
}

static void funcdecl_resolve(Ast *self, AstCompilerContext *context) {
    FunctionDecl *func_decl = self->value.func_decl;
    function_signature_resolve(func_decl->signature, context);
    scope_resolve(&func_decl->body, context);
    self->resolve_data.type.type = RESOLVED_TYPE_FUNC_SIG;
    self->resolve_data.type.value.func_sig = self->value.func_decl->signature;
}

static usize min(usize a, usize b) {
    return a < b ? a : b;
}

static bool is_c_pointer_compatible(VariableType *self) {
    return self->variable_type_flags & VARIABLE_TYPE_FLAG_BORROW;
}

static bool is_arg_str_and_param_c_str(AstResolvedType *arg_type, VariableType *param_type, AstCompilerContext *context) {
    return arg_type->value.data == &context->compiler->default_types.str->lhs_expr &&
        param_type->type == VARIABLE_TYPE_VARIABLE &&
        param_type->value.variable->resolved_var.value.data == context->compiler->default_types.c_char &&
        is_c_pointer_compatible(param_type);
}

static bool resolve_data_type_equals(AstResolvedType *self, AstResolvedType *other) {
    if(self->type != other->type)
        return bool_false;

    switch(self->type) {
        case RESOLVED_TYPE_NONE:
        case RESOLVED_TYPE_LHS_EXPR:
            return self->value.data == other->value.data;
        case RESOLVED_TYPE_FUNC_SIG:
            return function_signature_equals(self->value.func_sig, other->value.func_sig);
    }
    assert(bool_false);
}

static bool function_parameter_is_c_vararg(FunctionParameter *self, AstCompilerContext *context) {
    amal_default_type *vararg_type = context->compiler->default_types.c_varargs;
    return self->resolve_data.type.value.data == &vararg_type->lhs_expr;
}

static bool is_function_arg_compatible_with_parameter(AstResolvedType *arg, FunctionParameter *param, AstCompilerContext *context) {
    return resolve_data_type_equals(arg, &param->resolve_data.type) ||
           is_arg_str_and_param_c_str(arg, &param->type, context) ||
           function_parameter_is_c_vararg(param, context);
}

/* Pointers, isize and usize are returned with size 4, as that is the smallest possible size for them */
static int arithmetic_type_get_size(StructDecl *self) {
    assert(sizeof(usize) >= 4 && "Make this work when size of pointer is less than 4");
    return self->fields_num_pointers * 4 + self->fields_fixed_size_bytes;
}

static bool is_implicit_cast_possible(AstResolvedType *from, FunctionParameter *to, AstCompilerContext *context) {
    if(from->type == RESOLVED_TYPE_LHS_EXPR && to->resolve_data.type.type == RESOLVED_TYPE_LHS_EXPR) {
        LhsExpr *from_lhs_expr = from->value.lhs_expr;
        LhsExpr *to_lhs_expr = to->resolve_data.type.value.lhs_expr;
        assert(to_lhs_expr->rhs_expr && to_lhs_expr->rhs_expr->type == AST_STRUCT_DECL);
        /* TODO: Optimize, dont use is_arithmetic_type */
        if(is_arithmetic_type(from_lhs_expr, context->compiler) && is_arithmetic_type(to_lhs_expr, context->compiler)) {
            StructDecl *from_struct_type = from_lhs_expr->rhs_expr->value.struct_decl;
            StructDecl *to_struct_type = to_lhs_expr->rhs_expr->value.struct_decl;
            assert(from_lhs_expr->rhs_expr->type == AST_STRUCT_DECL);
            /* TODO: Also allow implicit cast if @from variable is const and if the const value can be cast to @to without data loss */
            /* TODO: Allow cast between signed<->unsigned if the value is known at compile-time and if it can be cast without data loss */
            return arithmetic_type_get_size(from_struct_type) <= arithmetic_type_get_size(to_struct_type) && from_struct_type->is_signed == to_struct_type->is_signed;
        }
    }
    return bool_false;
}

static void funccall_resolve_signature_types(FunctionCall *func_call, FunctionSignature *func_sig, AstCompilerContext *context) {
    Ast **arg = buffer_begin(&func_call->args);
    Ast **arg_end = buffer_end(&func_call->args);

    FunctionParameter *func_param = buffer_begin(&func_sig->parameters);
    FunctionParameter *func_param_end = buffer_end(&func_sig->parameters);
    
    usize num_args = arg_end - arg;
    usize num_params = func_param_end - func_param;
    isize num_missing_args = (isize)num_params - (isize)num_args;
    usize num_check = min(num_args, num_params);
    usize i = 0;
    for(; i < num_check; ++i) {
        if(!is_function_arg_compatible_with_parameter(&(*arg)->resolve_data.type, func_param, context)) {
            /* TODO: Cast data to larger size? */
            if(!is_implicit_cast_possible(&(*arg)->resolve_data.type, func_param, context)) {
                BufferView arg_name = ast_resolved_type_get_name(&(*arg)->resolve_data.type);
                BufferView param_name = ast_resolved_type_get_name(&func_param->resolve_data.type);
                /*
                    TODO: Use arg as tokenizer reference, but the name is generated right now so it doesn't belong to the tokenizer;
                    so the reference cant be used.
                */
                compiler_print_error(context->compiler, func_call->func.name.data,
                    "Can't implicitly cast argument of type \"%.*s\" to parameter of type \"%.*s\"", arg_name.size, arg_name.data, param_name.size, param_name.data);
                throw(AST_ERR);
            }
        }
        ++arg;
        ++func_param;
    }

    if(num_missing_args > 0) {
        FunctionParameter *vararg_param = buffer_begin(&func_sig->parameters);
        bool has_vararg = num_params > 0 && function_parameter_is_c_vararg(&vararg_param[num_params - 1], context);
        if (has_vararg)
            num_missing_args -= 1;
        if(num_missing_args > 0) {
            compiler_print_error(context->compiler, func_call->func.name.data,
                "Missing %d argument(s) to closure \"%.*s\"", num_missing_args, func_call->func.name.size, func_call->func.name.data);
            throw(AST_ERR);
        }
    }
}

static void funccall_resolve(Ast *self, AstCompilerContext *context) {
    Ast **ast;
    Ast **ast_end;
    FunctionSignature *func_sig;

    FunctionCall *func_call = self->value.func_call;
    variable_resolve(&func_call->func, context, &self->resolve_data.type);
    /* Attemping to use call syntax (variable_name ( ) ) with a variable that is not a function */
    if(self->resolve_data.type.type != RESOLVED_TYPE_FUNC_SIG) {
        BufferView callee_code_ref = ast_resolved_type_get_name(&self->resolve_data.type);
        compiler_print_error(context->compiler, func_call->func.name.data,
            "\"%.*s\" is not a function. Only functions can be called", func_call->func.name.size, func_call->func.name.data);
        /* TODO: use tokenizer_print_note, once it has been added */
        /* TODO: Print type */
        compiler_print_error(context->compiler, callee_code_ref.data, "Type was declared here");
        throw(AST_ERR);
    }

    {
        func_sig = self->resolve_data.type.value.func_sig;
        self->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR;
        if(func_sig->return_types.size > 0) {
            FunctionReturnType *return_type = buffer_begin(&func_sig->return_types);
            self->resolve_data.type = return_type->resolved_type;
        } else {
            self->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR;
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.void_type->lhs_expr;
        }
    }

    ast = buffer_begin(&func_call->args);
    ast_end = buffer_end(&func_call->args);
    for(; ast != ast_end; ++ast) {
        ast_resolve(*ast, context);
    }

    funccall_resolve_signature_types(func_call, func_sig, context);
}

static TypeSize variable_type_get_byte_size(VariableType *self) {
    TypeSize type_size;
    type_size.num_pointers = 0;
    type_size.fixed_size = 0;
    switch(self->type) {
        case VARIABLE_TYPE_NONE:
            assert(bool_false && "Variable type not resolved!");
            break;
        case VARIABLE_TYPE_VARIABLE: {
            if(self->variable_type_flags & VARIABLE_TYPE_FLAG_BORROW)
                type_size.num_pointers = 1;
            else
                type_size = resolved_type_get_byte_size(&self->value.variable->resolved_var.resolve_data->type);
            break;
        }
        case VARIABLE_TYPE_SIGNATURE:
            type_size.num_pointers = 1;
            break;
    }
    return type_size;
}

static void structdecl_resolve(Ast *self, AstCompilerContext *context) {
    StructDecl *struct_decl = self->value.struct_decl;
    Scope *body = &struct_decl->body;
    scope_resolve(body, context);

    {
        /*
            Sum the size of all the fields into the struct, so the struct can know it's full size
            without searching for it.
            TODO: Exclude functions, but not function pointers.
        */
        Ast **ast = buffer_begin(&body->ast_objects);
        Ast **ast_end = buffer_end(&body->ast_objects);
        for(; ast != ast_end; ++ast) {
            StructField *struct_field = (*ast)->value.struct_field;
            TypeSize type_size = variable_type_get_byte_size(&struct_field->type);
            struct_decl->fields_num_pointers += type_size.num_pointers;
            struct_decl->fields_fixed_size_bytes += type_size.fixed_size;
        }
    }
}

static void structfield_resolve(Ast *self, AstCompilerContext *context) {
    StructField *struct_field = self->value.struct_field;
    variable_type_resolve(&struct_field->type, context, &self->resolve_data.type);
}

static bool is_struct_decl(Ast *self) {
    LhsExpr *resolved_type;
    if(self->resolve_data.type.type != RESOLVED_TYPE_LHS_EXPR)
        return bool_false;
    resolved_type = self->resolve_data.type.value.lhs_expr;
    assert(self->resolve_data.status == AST_RESOLVED);
    return resolved_type->rhs_expr && resolved_type->rhs_expr->type == AST_STRUCT_DECL;
}

static void binop_resolve_dot_access(Ast *ast, AstCompilerContext *context, ScopeNamedObject *rhs_resolved_var) {
    Binop *self;
    Scope *lhs_scope;
    BufferView caller_code_ref;
    BufferView callee_code_ref;

    assert(ast->type == AST_BINOP);
    self = ast->value.binop;

    if(self->lhs->type != AST_VARIABLE) {
        /* TODO: Allow field access for numbers and string as well */
        compiler_print_error(context->compiler, ast_get_code_reference(self->lhs).data, "Accessing fields is only applicable for variables");
        throw(AST_ERR);
    }
    
    lhs_scope = ast_resolved_type_get_scope(&self->lhs->resolve_data.type);
    scope_get_resolved_variable(lhs_scope, context, self->rhs->value.variable->name, rhs_resolved_var);
    self->rhs->resolve_data.type = scope_named_object_get_resolved_type(rhs_resolved_var, context);

    caller_code_ref = ast_get_code_reference(self->rhs);
    callee_code_ref = ast_resolved_type_get_name(&self->rhs->resolve_data.type);

    if(!is_struct_decl(self->lhs)) {
        compiler_print_error(context->compiler, caller_code_ref.data, "Can only access field of structs");
        /* TODO: use tokenizer_print_note, once it has been added */
        /* TODO: Print type */
        compiler_print_error(context->compiler, callee_code_ref.data, "Type was declared here");
        throw(AST_ERR);
    }

    {
        bool invalid_dot_access = bool_true;
        switch(self->rhs->resolve_data.type.type) {
            case RESOLVED_TYPE_NONE:
                assert(bool_false);
                break;
            case RESOLVED_TYPE_FUNC_SIG: {
                FunctionSignature *func_sig = self->rhs->resolve_data.type.value.func_sig;
                if(func_sig->func_decl && func_sig->func_decl->lhs_expr && LHS_EXPR_IS_PUB(func_sig->func_decl->lhs_expr))
                    invalid_dot_access = bool_false;
                break;
            }
            case RESOLVED_TYPE_LHS_EXPR:
                invalid_dot_access = !LHS_EXPR_IS_PUB(self->rhs->resolve_data.type.value.lhs_expr);
                break;
        }

        if(invalid_dot_access) {
            compiler_print_error(context->compiler, caller_code_ref.data, "Can't access non-public field \"%.*s\"", caller_code_ref.size, caller_code_ref.data);
            /* TODO: use tokenizer_print_note, once it has been added */
            /* TODO: Print type */
            compiler_print_error(context->compiler, callee_code_ref.data, "Type was declared non-public here");
            throw(AST_ERR);
        }
    }
}

static void binop_resolve(Ast *ast, AstCompilerContext *context) {
    Binop *self;
    assert(ast->type == AST_BINOP);
    self = ast->value.binop;
    ast_resolve(self->lhs, context);
    if(self->type == BINOP_DOT && (self->rhs->type == AST_VARIABLE || self->rhs->type == AST_FUNCTION_CALL)) {
        ScopeNamedObject rhs_resolved_var;
        binop_resolve_dot_access(ast, context, &rhs_resolved_var);
        /* Only function call has extra data that needs to be resolved (args) */
        if(self->rhs->type == AST_FUNCTION_CALL) {
            /*Scope *prev_scope = context->scope;*/
            /*context->scope = ast_resolved_type_get_scope(&self->lhs->resolve_data.type);*/
            self->rhs->value.func_call->func.resolved_var = rhs_resolved_var;
            ast_resolve(self->rhs, context);
            /*context->scope = prev_scope;*/
        }
        self->rhs->resolve_data.status = AST_RESOLVED;
        ast->resolve_data.type = self->rhs->resolve_data.type;
    } else {
        ast_resolve(self->rhs, context);
        /* TODO: Convert types that can be safely converted */
        assert(self->lhs->resolve_data.type.type != RESOLVED_TYPE_NONE);
        assert(self->rhs->resolve_data.type.type != RESOLVED_TYPE_NONE);
        if(!ast_resolved_type_equals(&self->rhs->resolve_data.type, &self->lhs->resolve_data.type)) {
            /*
            TODO: For this first error, only print the line without a reference to code.
            This requires change in tokenizer_print_error to be able to take a line as reference.

            TODO: Use note for the additional information instead of error.
            */
            BufferView lhs_type_name = ast_resolved_type_get_name(&self->lhs->resolve_data.type);
            BufferView rhs_type_name = ast_resolved_type_get_name(&self->rhs->resolve_data.type);
            compiler_print_error(context->compiler, ast_get_code_reference(self->rhs).data,
                "Can't cast type %.*s to type %.*s",
                    rhs_type_name.size, rhs_type_name.data,
                    lhs_type_name.size, lhs_type_name.data);
            compiler_print_error(context->compiler, ast_get_code_reference(self->lhs).data,
                "Left-hand side is of type %.*s",
                    lhs_type_name.size, lhs_type_name.data);
            compiler_print_error(context->compiler, ast_get_code_reference(self->rhs).data,
                "Right-hand side is of type %.*s",
                    rhs_type_name.size, rhs_type_name.data);
            throw(AST_ERR);
            /* TODO: Optimize this? store arithmetic type in the LhsExpr itself? */
        } else if(self->lhs->resolve_data.type.type != RESOLVED_TYPE_LHS_EXPR || !is_arithmetic_type(self->lhs->resolve_data.type.value.lhs_expr, context->compiler)) {
            /* TODO: Point the error at the binop instead of LHS */
            BufferView lhs_type_name = ast_resolved_type_get_name(&self->lhs->resolve_data.type);
            compiler_print_error(context->compiler, ast_get_code_reference(self->lhs).data,
                "Arithmetic operation can only be performed with the types i8, u8, i16, u16, i32, u32, i64, u64, isize and usize",
                    lhs_type_name.size, lhs_type_name.data);
            throw(AST_ERR);
        }
        ast->resolve_data.type = self->lhs->resolve_data.type;
    }
}

static void else_if_statement_resolve(ElseIfStatement *else_if_stmt, AstCompilerContext *context) {
    if(else_if_stmt->condition)
        ast_resolve(else_if_stmt->condition, context);

    scope_resolve(&else_if_stmt->body, context);
    if(else_if_stmt->next_else_if_stmt)
        else_if_statement_resolve(else_if_stmt->next_else_if_stmt, context);
}

static void if_statement_resolve(IfStatement *if_stmt, AstCompilerContext *context) {
    assert(if_stmt->condition);
    ast_resolve(if_stmt->condition, context);
    scope_resolve(&if_stmt->body, context);
    if(if_stmt->else_if_stmt)
        else_if_statement_resolve(if_stmt->else_if_stmt, context);
}

static void while_statement_resolve(WhileStatement *while_stmt, AstCompilerContext *context) {
    ast_resolve(while_stmt->condition, context);
    scope_resolve(&while_stmt->body, context);
}

static void return_expr_resolve(ReturnExpr *self, AstCompilerContext *context) {
    ast_resolve(self->rhs_expr, context);
}

TypeSize resolved_type_get_byte_size(AstResolvedType *self) {
    TypeSize type_size;
    type_size.num_pointers = 0;
    type_size.fixed_size = 0;
    switch(self->type) {
        case RESOLVED_TYPE_NONE:
            assert(bool_false && "Type not resolved!");
            break;
        case RESOLVED_TYPE_LHS_EXPR: {
            /* Resolved type until rhs is StructDecl or FunctionSignature */
            LhsExpr *lhs_expr = self->value.lhs_expr;
            if(lhs_expr->type.type != VARIABLE_TYPE_NONE)
                type_size = variable_type_get_byte_size(&lhs_expr->type);
            else {
                assert(lhs_expr->rhs_expr);
                if(lhs_expr->rhs_expr->type == AST_STRUCT_DECL) {
                    StructDecl *struct_decl = lhs_expr->rhs_expr->value.struct_decl;
                    type_size.num_pointers = struct_decl->fields_num_pointers;
                    type_size.fixed_size = struct_decl->fields_fixed_size_bytes;
                } else {
                    type_size = resolved_type_get_byte_size(&lhs_expr->rhs_expr->resolve_data.type);
                }
            }
            break;
        }
        case RESOLVED_TYPE_FUNC_SIG:
            type_size.num_pointers = 1;
            break;
    }
    return type_size;
}

static void signed_integer_resolve(Ast *self, AstCompilerContext *context) {
    Number *number = self->value.number;
    assert(number->value.type == AMAL_NUMBER_SIGNED_INTEGER);
    switch(number->value.bits) {
        case 8:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.i8->lhs_expr;
            break;
        case 16:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.i16->lhs_expr;
            break;
        case 32:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.i32->lhs_expr;
            break;
        case 64:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.i64->lhs_expr;
            break;
        default: {
            compiler_print_error(context->compiler, ast_get_code_reference(self).data, "Currently only 8, 16, 32 and 64 bit integers are supported. TODO: Support arbitrary size integer");
            throw(AST_ERR);
            break;
        }
    }
}

static void unsigned_integer_resolve(Ast *self, AstCompilerContext *context) {
    Number *number = self->value.number;
    assert(number->value.type == AMAL_NUMBER_UNSIGNED_INTEGER);
    switch(number->value.bits) {
        case 8:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.u8->lhs_expr;
            break;
        case 16:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.u16->lhs_expr;
            break;
        case 32:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.u32->lhs_expr;
            break;
        case 64:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.u64->lhs_expr;
            break;
        default: {
            compiler_print_error(context->compiler, ast_get_code_reference(self).data, "Currently only 8, 16, 32 and 64 bit integers are supported. TODO: Support arbitrary size integer");
            throw(AST_ERR);
            break;
        }
    }
}

static void float_resolve(Ast *self, AstCompilerContext *context) {
    Number *number = self->value.number;
    assert(number->value.type == AMAL_NUMBER_FLOAT);
    switch(number->value.bits) {
        case 32:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.f32->lhs_expr;
            break;
        case 64:
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.f64->lhs_expr;
            break;
        default: {
            compiler_print_error(context->compiler, ast_get_code_reference(self).data, "Currently only 32 and 64 bit floats are supported. TODO: Support arbitrary size float");
            throw(AST_ERR);
            break;
        }
    }
}

static void number_resolve(Ast *self, AstCompilerContext *context) {
    Number *number = self->value.number;
    self->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR;
    switch(number->value.type) {
        case AMAL_NUMBER_SIGNED_INTEGER:
            signed_integer_resolve(self, context);
            break;
        case AMAL_NUMBER_UNSIGNED_INTEGER:
            unsigned_integer_resolve(self, context);
            break;
        case AMAL_NUMBER_FLOAT:
            float_resolve(self, context);
            break;
    }
}

void ast_resolve(Ast *self, AstCompilerContext *context) {
    assert(self);
    assert(context->parser);
    /* 
        TODO: Move these to the types that need checks for recursive dependency (function declaration, struct declaration)
        For function declaration, it should be marked as resolved when the signature has been resolved
        instead of the whole function declaration including the body
        because the body can have function call that calls functions that are resolving
        or even recursive function call, which should be allowed.
    */
    /*
        This check is outside lhs_expr mutex for optimization purpose as most times there wont be
        a race in multiple threads to resolve an AST expression.
    */
    if(self->resolve_data.status == AST_RESOLVED) {
        return;
        /*
            NOTE!!!: There is a data race here and it's fine!
            A recursive dependency can be missed here in one thread, but it's fine because that
            can only happen if another thread is also resolving the same ast, in which case that thread will finally
            report the recursive dependency. Note that this can mean that different files can report the
            recursive dependency everytime you compile code with recursive dependency. However this can happen
            even without a data race since files are compile in parallel so one file might resolve the ast with the issue
            before another file has done it, and it can be different everytime the files are compiled.
        */
    } else if(self->resolve_data.status == AST_RESOLVING && self->parser == context->parser) {
        compiler_print_error(context->compiler, ast_get_code_reference(self).data, "Found recursive dependency");
        throw(AST_ERR);
    }

    /*
        TODO: This could cause a deadlock if two different threads resolve the same expression at the same time
        and self->parser is overwritten by one of the threads, and then if both of the threads try to
        resolve another expression at the same time that this expression depends on,
        then the same thread could get ownership of that expression (self->parser would be assigned to that thread)
        or another thread steals it.
        Then the other thread that had its self->parser stolen would be stuck in a recursive dependency
        deadlock since in the above code, an error is only thrown if the parser belongs to the current thread.
        A possible fix for this may be to add a check above that if any of the other thread has failed,
        then this thread should fail as well.
    */

    self->resolve_data.status = AST_RESOLVING;
    self->parser = context->parser;
    switch(self->type) {
        case AST_NUMBER:
            number_resolve(self, context);
            break;
        case AST_BOOL: {
            self->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR;
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.bool->lhs_expr;
            break;
        }
        case AST_FUNCTION_DECL:
            funcdecl_resolve(self, context);
            break;
        case AST_FUNCTION_CALL:
            funccall_resolve(self, context);
            break;
        case AST_STRUCT_DECL:
            structdecl_resolve(self, context);
            break;
        case AST_STRUCT_FIELD:
            structfield_resolve(self, context);
            break;
        case AST_LHS:
            lhsexpr_resolve(self, context);
            break;
        case AST_ASSIGN:
            assignmentexpr_resolve(self, context);
            break;
        case AST_IMPORT:
            /* TODO: When @import(...).data syntax is added, implement the resolve for it */
            import_resolve(self, context);
            break;
        case AST_STRING:
            /* TODO: Convert special combinations. For example \n to newline */
            self->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR;
            self->resolve_data.type.value.lhs_expr = &context->compiler->default_types.str->lhs_expr;
            break;
        case AST_VARIABLE:
            variable_resolve(self->value.variable, context, &self->resolve_data.type);
            break;
        case AST_BINOP:
            binop_resolve(self, context);
            break;
        case AST_IF_STATEMENT:
            if_statement_resolve(self->value.if_stmt, context);
            break;
        case AST_WHILE_STATEMENT:
            while_statement_resolve(self->value.while_stmt, context);
            break;
        case AST_RETURN:
            return_expr_resolve(self->value.return_expr, context);
            break;
    }
    /* TODO: See comment at the top of this function */
    /*assert(self->resolve_data.type.type != RESOLVED_TYPE_NONE);*/
    self->resolve_data.status = AST_RESOLVED;
}