-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattribute.c
More file actions
1104 lines (987 loc) · 37.9 KB
/
attribute.c
File metadata and controls
1104 lines (987 loc) · 37.9 KB
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
enum AttributeToken {
ATTR_TK_GLOBAL = INTRO_AT_COUNT + 1,
ATTR_TK_INHERIT,
ATTR_TK_ATTRIBUTE,
ATTR_TK_APPLY_TO,
ATTR_TK_PROPAGATE,
ATTR_TK_TRANSIENT,
ATTR_TK_IMPLY,
ATTR_TK_INVALID
};
void
attribute_parse_init(ParseContext * ctx) {
static const struct { const char * key; int value; } attribute_keywords [] = {
{"flag", INTRO_AT_FLAG},
{"int", INTRO_AT_INT},
{"float", INTRO_AT_FLOAT},
{"value", INTRO_AT_VALUE},
{"member", INTRO_AT_MEMBER},
{"type", INTRO_AT_TYPE},
{"expr", INTRO_AT_EXPR},
{"__remove",INTRO_AT_REMOVE},
{"attribute", ATTR_TK_ATTRIBUTE},
{"apply_to", ATTR_TK_APPLY_TO},
{"global", ATTR_TK_GLOBAL},
{"inherit", ATTR_TK_INHERIT},
{"propagate", ATTR_TK_PROPAGATE},
{"transient", ATTR_TK_TRANSIENT},
{"imply", ATTR_TK_IMPLY},
};
shdefault(ctx->attribute_token_map, ATTR_TK_INVALID);
for (int i=0; i < LENGTH(attribute_keywords); i++) {
shput(ctx->attribute_token_map, attribute_keywords[i].key, attribute_keywords[i].value);
}
shdefault(ctx->builtin_map, -1);
for (int i=0; i < LENGTH(g_builtin_attributes); i++) {
shput(ctx->builtin_map, g_builtin_attributes[i].key, g_builtin_attributes[i].value);
}
sh_new_arena(ctx->attribute_map);
ctx->attribute_id_counter = 0;
ctx->flag_temp_id_counter = 0;
}
int
parse_global_directive(ParseContext * ctx, TokenIndex * tidx) {
Token tk;
EXPECT('(');
tk = next_token(tidx);
char temp [1024];
memcpy(temp, tk.start, tk.length);
temp[tk.length] = 0;
int a_tk = shget(ctx->attribute_token_map, temp);
if (a_tk == ATTR_TK_ATTRIBUTE) {
tk = next_token(tidx);
char * namespace = NULL;
if (tk.type == TK_IDENTIFIER) {
namespace = copy_and_terminate(ctx->arena, tk.start, tk.length);
shputs(ctx->attribute_namespace_set, (NameSet){namespace});
tk = next_token(tidx);
} else if (tk.type == TK_AT) {
tk = next_token(tidx);
memcpy(temp, tk.start, tk.length);
temp[tk.length] = 0;
a_tk = shget(ctx->attribute_token_map, temp);
if (a_tk != ATTR_TK_GLOBAL) {
parse_error(ctx, tk, "The only attribute trait allowed here is 'global'.");
return -1;
}
namespace = NULL;
tk = next_token(tidx);
} else {
parse_error(ctx, tk, "Expected namespace or @global before '('.");
return -1;
}
if (tk.type != TK_L_PARENTHESIS) {
parse_error(ctx, tk, "Expected '('.");
return -1;
}
while (1) {
AttributeParseInfo info = {0};
tk = next_token(tidx);
if (tk.type == TK_R_PARENTHESIS) {
break;
} else if (tk.type != TK_IDENTIFIER) {
parse_error(ctx, tk, "Expected an identifier for an attribute name.");
}
char namespaced [1024];
stbsp_snprintf(namespaced, sizeof namespaced, "%s%.*s", namespace ?: "", tk.length, tk.start);
if (shgeti(ctx->attribute_map, namespaced) >= 0) {
parse_error(ctx, tk, "This attribute name is reserved.");
return -1;
}
EXPECT(':');
tk = next_token(tidx);
memcpy(temp, tk.start, tk.length);
temp[tk.length] = 0;
int a_tk = shget(ctx->attribute_token_map, temp);
if (a_tk >= INTRO_AT_COUNT) {
parse_error(ctx, tk, "Invalid attribute type.");
return -1;
}
info.category = a_tk;
tk = next_token(tidx);
if (tk.type == TK_L_PARENTHESIS && a_tk == INTRO_AT_VALUE) {
tk = next_token(tidx);
if (tk.type == TK_AT) {
tk = next_token(tidx);
if (!tk_equal(&tk, "inherit")) {
parse_error(ctx, tk, "Invalid trait.");
return -1;
}
tk = next_token(tidx);
info.type_ptr = NULL;
} else {
tidx->index--;
DeclState decl = {.state = DECL_CAST};
int ret = parse_declaration(ctx, tidx, &decl);
if (ret == RET_DECL_FINISHED) {
info.type_ptr = decl.type;
} else if (ret == RET_NOT_TYPE) {
parse_error(ctx, decl.base_tk, "Type must be defined before attribute definition.");
return -1;
} else {
return -1;
}
}
tk = next_token(tidx);
}
while (tk.type == TK_AT) {
tk = next_token(tidx);
memcpy(temp, tk.start, tk.length);
temp[tk.length] = 0;
a_tk = shget(ctx->attribute_token_map, temp);
switch (a_tk) {
case ATTR_TK_GLOBAL:
if (info.category != INTRO_AT_FLAG) {
parse_error(ctx, tk, "Only flag attributes can have the trait global.");
return -1;
}
info.global = true;
info.propagate = true;
break;
case ATTR_TK_PROPAGATE:
info.propagate = true;
break;
case ATTR_TK_TRANSIENT:
info.transient = true;
break;
case ATTR_TK_IMPLY: {
EXPECT('(');
tidx->index -= 1;
int closing = find_closing(*tidx);
if (closing == 0) {
parse_error(ctx, tk_at(tidx), "No closing ')'.");
return -1;
}
AttributeDirective directive = {
.type = NULL,
.location = tidx->index,
.member_index = MIDX_IMPLY,
.namespace = namespace,
};
tidx->index = closing + 1;
info.imply_directive_index = arrlen(ctx->attribute_directives);
arrput(ctx->attribute_directives, directive);
}break;
default:
parse_error(ctx, tk, "Invalid trait.");
return -1;
}
tk = next_token(tidx);
}
if (info.category == INTRO_AT_FLAG || info.transient) {
info.id = ctx->flag_temp_id_counter++;
} else {
info.id = ctx->attribute_id_counter++;
}
shput(ctx->attribute_map, namespaced, info);
if (tk.type == TK_COMMA) {
continue;
} else if (tk.type == TK_R_PARENTHESIS) {
break;
} else {
parse_error(ctx, tk, "Expected ',' or ')'.");
return -1;
}
}
} else if (a_tk == ATTR_TK_APPLY_TO) {
EXPECT('(');
DeclState decl = {.state = DECL_CAST};
int ret = parse_declaration(ctx, tidx, &decl);
if (ret != RET_DECL_FINISHED) {
if (ret >= 0) {
parse_error(ctx, tk_last(tidx), "Invalid cast.");
}
return -1;
}
EXPECT('(');
tidx->index--;
int32_t start_directive_index = tidx->index;
tidx->index = find_closing(*tidx);
AttributeDirective directive = {
.type = decl.type,
.location = start_directive_index,
.member_index = MIDX_TYPE,
};
arrput(ctx->attribute_directives, directive);
} else {
parse_error(ctx, tk, "Invalid. Expected 'attribute' or 'apply_to'.");
return -1;
}
tk = next_token(tidx);
if (tk.type != TK_R_PARENTHESIS) {
parse_error(ctx, tk, "Missing ')'.");
return -1;
}
return 0;
}
ptrdiff_t
store_value(ParseContext * ctx, const void * value, size_t value_size) {
void * storage = arraddnptr(ctx->value_buffer, value_size);
memcpy(storage, value, value_size); // NOTE: this is only correct for LE
return (storage - (void *)ctx->value_buffer);
}
ptrdiff_t
store_ptr(ParseContext * ctx, void * data, size_t size) {
static const uint8_t nothing [sizeof(void *)] = {0};
ptrdiff_t offset = store_value(ctx, ¬hing, sizeof(void *));
PtrStore ptr_store = {0};
ptr_store.value_offset = offset;
ptr_store.data = data;
ptr_store.data_size = size;
arrput(ctx->ptr_stores, ptr_store);
return offset;
}
ptrdiff_t parse_array_value(ParseContext * ctx, const IntroType * type, TokenIndex * tidx, uint32_t * o_count);
ptrdiff_t parse_struct_value(ParseContext * ctx, const IntroType * type, TokenIndex * tidx);
// TODO: change to parse_expression
ptrdiff_t
parse_value(ParseContext * ctx, const IntroType * type, TokenIndex * tidx, uint32_t * o_count) {
char * end;
if ((type->category >= INTRO_U8 && type->category <= INTRO_S64) || type->category == INTRO_ENUM) {
intmax_t result = parse_constant_expression(ctx, tidx);
return store_value(ctx, &result, type->size);
} else if (type->category == INTRO_F32) {
float result = strtof(tk_at(tidx).start, &end);
advance_to(tidx, end);
return store_value(ctx, &result, 4);
} else if (type->category == INTRO_F64) {
double result = strtod(tk_at(tidx).start, &end);
advance_to(tidx, end);
return store_value(ctx, &result, 8);
} else if (type->category == INTRO_POINTER) {
Token tk = next_token(tidx);
if (tk.type == TK_STRING) {
if (type->of->category == INTRO_S8 && 0==strcmp(type->of->name, "char")) {
size_t length;
char * str = parse_escaped_string(&tk, &length);
if (!str) {
parse_error(ctx, tk, "Invalid string.");
return -1;
}
ptrdiff_t result = store_ptr(ctx, str, length);
return result;
}
} else {
tidx->index--;
ptrdiff_t array_value_offset = parse_array_value(ctx, type, tidx, o_count);
ptrdiff_t pointer_offset = store_value(ctx, &array_value_offset, sizeof(size_t));
return pointer_offset;
}
} else if (type->category == INTRO_ARRAY) {
if (type->of->category == INTRO_S8 && 0==strcmp(type->of->name, "char")) { // NOTE: this should probably check attributes instead
Token tk = next_token(tidx);
int32_t tk_index = tidx->index;
if (tk.type == TK_STRING) {
size_t str_length;
char * str = parse_escaped_string(&tk, &str_length);
if (!str) {
parse_error(ctx, tk, "Invalid string.");
return -1;
}
ptrdiff_t result = arrlen(ctx->value_buffer);
char * dest = (char *)arraddnptr(ctx->value_buffer, type->size);
memset(dest, 0, type->size);
strcpy(dest, str);
free(str);
return result;
}
tidx->index = tk_index;
}
ptrdiff_t result = parse_array_value(ctx, type, tidx, NULL);
return result;
} else if (intro_has_members(type)) {
ptrdiff_t result = parse_struct_value(ctx, type, tidx);
return result;
}
return -1;
}
ptrdiff_t
parse_array_value(ParseContext * ctx, const IntroType * type, TokenIndex * tidx, uint32_t * o_count) {
Token tk;
uint8_t * prev_buf = ctx->value_buffer;
ctx->value_buffer = NULL;
arrsetcap(ctx->value_buffer, type->size);
memset(ctx->value_buffer, 0, type->size);
int count_ptrs_last = arrlen(ctx->ptr_stores);
size_t array_element_size = type->of->size;
intmax_t index = 0;
intmax_t highest_index = 0;
EXPECT('{');
while (1) {
int32_t tk_index = tidx->index;
tk = next_token(tidx);
if (tk.type == TK_L_BRACKET) {
index = parse_constant_expression(ctx, tidx);
if (index < 0 || index >= type->count) {
parse_error(ctx, tk, "Invalid index.");
return -1;
}
EXPECT(']');
EXPECT('=');
} else if (tk.type == TK_COMMA) {
parse_error(ctx, tk, "Invalid symbol.");
return -1;
} else if (tk.type == TK_R_BRACE) {
break;
} else {
tidx->index = tk_index;
}
if (highest_index < index) highest_index = index;
arrsetlen(ctx->value_buffer, index * array_element_size);
ptrdiff_t parse_ret = parse_value(ctx, type->of, tidx, NULL);
if (parse_ret < 0) {
return -1;
}
tk = next_token(tidx);
if (tk.type == TK_COMMA) {
index++;
} else if (tk.type == TK_R_BRACE) {
break;
} else {
parse_error(ctx, tk, "Invalid symbol.");
return -1;
}
}
uint32_t count = highest_index + 1;
if (type->category == INTRO_ARRAY && type->count > 0) {
arrsetlen(ctx->value_buffer, type->size);
} else {
arrsetlen(ctx->value_buffer, count * array_element_size);
}
uint8_t * temp_buf = ctx->value_buffer;
ctx->value_buffer = prev_buf;
ptrdiff_t result = store_value(ctx, temp_buf, arrlen(temp_buf));
arrfree(temp_buf);
for (int i = count_ptrs_last; i < arrlen(ctx->ptr_stores); i++) {
ctx->ptr_stores[i].value_offset += result;
}
if (o_count) *o_count = count;
return result;
}
ptrdiff_t
parse_struct_value(ParseContext * ctx, const IntroType * type, TokenIndex * tidx) {
Token tk;
EXPECT('{');
uint8_t * prev_buf = ctx->value_buffer;
ctx->value_buffer = NULL;
arrsetcap(ctx->value_buffer, type->size);
memset(ctx->value_buffer, 0, type->size);
int count_ptrs_last = arrlen(ctx->ptr_stores);
int member_index = 0;
while (1) {
tk = next_token(tidx);
bool designated = false;
size_t offset = 0;
const IntroType * dtype = type;
if (tk.type == TK_R_BRACE) break;
while (tk.type == TK_PERIOD) {
if (!intro_has_members(dtype)) {
parse_error(ctx, tk, "Type does not have fields.");
return -1;
}
designated = true;
EXPECT_IDEN();
bool found_match = false;
for (int i=0; i < dtype->count; i++) {
IntroMember check = dtype->members[i];
if (tk_equal(&tk, check.name)) {
found_match = true;
offset += check.offset;
dtype = check.type;
break;
}
}
if (!found_match) {
char buf [1024];
if (dtype->name) {
stbsp_sprintf(buf, "Not a member of %s.", dtype->name);
} else {
strcpy(buf, "Invalid member name.");
}
parse_error(ctx, tk, buf);
return -1;
}
tk = next_token(tidx);
}
tidx->index--;
if (designated) {
EXPECT('=');
} else {
offset = type->members[member_index].offset;
dtype = type->members[member_index].type;
}
arrsetlen(ctx->value_buffer, offset);
ptrdiff_t parse_ret = parse_value(ctx, dtype, tidx, NULL);
if (parse_ret < 0) {
return -1;
}
tk = next_token(tidx);
if (tk.type == TK_COMMA) {
member_index += 1;
} else if (tk.type == TK_R_BRACE) {
break;
} else {
parse_error(ctx, tk, "Expected ',' or '}'.");
return -1;
}
}
uint8_t * struct_buf = ctx->value_buffer;
ctx->value_buffer = prev_buf;
ptrdiff_t result = store_value(ctx, struct_buf, arrlen(struct_buf));
arrfree(struct_buf);
for (int i = count_ptrs_last; i < arrlen(ctx->ptr_stores); i++) {
ctx->ptr_stores[i].value_offset += result;
}
return result;
}
void
store_deferred_ptrs(ParseContext * ctx) {
for (int i=0; i < arrlen(ctx->ptr_stores); i++) {
PtrStore ptr_store = ctx->ptr_stores[i];
store_value(ctx, &ptr_store.data_size, 4);
size_t offset = store_value(ctx, ptr_store.data, ptr_store.data_size);
size_t * o_offset = (size_t *)(ctx->value_buffer + ptr_store.value_offset);
memcpy(o_offset, &offset, sizeof(offset));
free(ptr_store.data);
}
arrsetlen(ctx->ptr_stores, 0);
}
int
handle_value_attribute(ParseContext * ctx, TokenIndex * tidx, IntroType * type, int member_index, AttributeData * data, Token * p_tk) {
IntroAttributeInfo attr_info = ctx->p_info->attr.available[data->id];
IntroType * v_type;
if (attr_info.id == 0) {
if (member_index >= 0) {
v_type = type->members[member_index].type;
} else {
v_type = type;
}
} else {
v_type = ctx->p_info->types[attr_info.id];
}
_assume(arrlen(ctx->ptr_stores) == 0);
uint32_t length_value = 0;
ptrdiff_t value_offset = parse_value(ctx, v_type, tidx, &length_value);
if (value_offset < 0) {
parse_error(ctx, *p_tk, "Error parsing value attribute.");
return -1;
}
if (length_value) {
DeferredDefault def = {
.type = type,
.member_index = member_index,
.attr_id = data->id,
.value = length_value,
};
arrput(ctx->deferred_length_defaults, def);
}
store_deferred_ptrs(ctx);
data->v.i = value_offset;
return 0;
}
bool
parse_attribute_name(ParseContext * ctx, TokenIndex * tidx, AttributeParseInfo * o_info) {
ptrdiff_t map_index = -1;
char name [1024];
Token tk;
EXPECT_IDEN();
if (ctx->current_namespace) {
stbsp_snprintf(name, sizeof name, "%s%.*s", ctx->current_namespace, tk.length, tk.start);
map_index = shgeti(ctx->attribute_map, name);
}
if (map_index < 0) {
memcpy(name, tk.start, tk.length);
name[tk.length] = 0;
map_index = shgeti(ctx->attribute_map, name);
}
if (map_index < 0) {
parse_error(ctx, tk, "No such attribute.");
return false;
}
*o_info = ctx->attribute_map[map_index].value;
return true;
}
int
parse_attribute(ParseContext * ctx, TokenIndex * tidx, IntroType * type, int member_index, AttributeData * o_result) {
AttributeData data = {0};
AttributeParseInfo attr_info;
Token tk;
tk = tk_at(tidx);
if (!parse_attribute_name(ctx, tidx, &attr_info)) {
return -1;
}
data.id = attr_info.final_id;
IntroAttributeCategory attribute_category = attr_info.category;
char * end;
switch(attribute_category) {
case INTRO_AT_FLAG: {
data.v.i = 0;
} break;
case INTRO_AT_INT: {
tk = next_token(tidx);
long result = strtol(tk.start, &end, 0);
if (end == tk.start) {
parse_error(ctx, tk, "Invalid integer.");
return -1;
}
advance_to(tidx, end);
data.v.i = (int32_t)result;
} break;
case INTRO_AT_FLOAT: {
tk = next_token(tidx);
float result = strtof(tk.start, &end);
advance_to(tidx, end);
if (end == tk.start) {
parse_error(ctx, tk, "Invalid floating point number.");
return -1;
}
advance_to(tidx, end);
data.v.f = result;
} break;
case INTRO_AT_VALUE: {
if (data.id == ctx->builtin.alias && tk_at(tidx).type == TK_IDENTIFIER) {
tk = next_token(tidx);
char * name = malloc(tk.length + 1);
memcpy(name, tk.start, tk.length);
name[tk.length] = 0;
ptrdiff_t result = store_ptr(ctx, name, tk.length + 1);
store_deferred_ptrs(ctx);
data.v.i = result;
} else {
if (handle_value_attribute(ctx, tidx, type, member_index, &data, &tk)) return -1;
}
} break;
case INTRO_AT_MEMBER: {
tk = next_token(tidx);
if (tk.type != TK_IDENTIFIER || is_digit(tk.start[0])) {
parse_error(ctx, tk, "Expected member name.");
return -1;
}
bool success = false;
for (int mi=0; mi < type->count; mi++) {
if (tk_equal(&tk, type->members[mi].name)) {
if (data.id == ctx->builtin.length) {
IntroType * mtype = type->members[mi].type;
uint32_t category_no_size = mtype->category & 0xf0;
if (category_no_size != INTRO_SIGNED && category_no_size != INTRO_UNSIGNED) {
parse_error(ctx, tk, "Length defining member must be of an integer type.");
return -1;
}
}
data.v.i = mi;
success = true;
break;
}
}
if (!success) {
parse_error(ctx, tk, "No such member.");
return -1;
}
} break;
case INTRO_AT_EXPR: {
ExprNode * tree = build_expression_tree2(ctx->expr_ctx, tidx);
if (!tree) {
return -1;
}
IntroContainer base_cont = {0};
AttributeDataKey key = {.type = type, .member_index = member_index};
const IntroType * header = hmget(ctx->header_map, key);
if (header) {
base_cont.type = header;
} else {
if (type == NULL) {
parse_error(ctx, tk, "Cannot imply an expression without a header.");
return -1;
}
base_cont.type = type;
IntroContainer * last_cont = &base_cont, * next;
ContainerMapValue v;
while ((v = hmget(ctx->container_map, type)).type != NULL) {
next = arena_alloc(ctx->expr_ctx->arena, sizeof(*next));
next->type = v.type;
last_cont->index = v.index;
last_cont->parent = next;
last_cont = next;
type = v.type;
}
}
uint8_t * bytecode = build_expression_procedure2(ctx->expr_ctx, tree, &base_cont);
size_t value_buf_offset = arraddnindex(ctx->value_buffer, arrlen(bytecode));
memcpy(ctx->value_buffer + value_buf_offset, bytecode, arrlen(bytecode));
arrfree(bytecode);
reset_arena(ctx->expr_ctx->arena);
data.v.i = value_buf_offset;
}break;
case INTRO_AT_REMOVE: {
AttributeParseInfo rm_attr_info;
if (!parse_attribute_name(ctx, tidx, &rm_attr_info)) return -1;
data.v.i = (int32_t)rm_attr_info.final_id;
}break;
case INTRO_AT_TYPE: {
DeclState decl = {.state = DECL_ARGS};
int ret = parse_declaration(ctx, tidx, &decl);
if (ret == RET_DECL_FINISHED || ret == RET_DECL_CONTINUE) {
tidx->index --;
if (data.id == ctx->builtin.header) {
if (type && type->category != INTRO_POINTER) {
parse_error(ctx, tk, "'header' can only be applied to a pointer.");
return -1;
}
AttributeDataKey key = {.type = type, .member_index = member_index};
hmput(ctx->header_map, key, decl.type);
}
uint32_t id = hmget(ctx->p_info->index_by_ptr_map, decl.type);
if (id == 0) {
parse_error(ctx, decl.base_tk, "Undeclared type.");
return -1;
}
data.v.i = id;
} else if (ret >= 0) {
parse_error(ctx, tk_last(tidx), "Unknown error.");
return -1;
} else {
return -1;
}
}break;
case INTRO_AT_COUNT: _assume(0);
}
if (o_result) *o_result = data;
return 0;
}
int
parse_attribute_directive(ParseContext * ctx, AttributeDirective * directive) {
TokenIndex _tidx, * tidx = &_tidx;
tidx->list = ctx->tk_list;
tidx->index = directive->location;
AttributeData * attributes = NULL;
Token tk = next_token(tidx);
if (tk.type != TK_L_PARENTHESIS) {
parse_error(ctx, tk, "Expected '('.");
return -1;
}
ctx->current_namespace = directive->namespace;
while (1) {
tk = next_token(tidx);
AttributeData data;
if (tk.type == TK_IDENTIFIER) {
if (tk_at(tidx).type == TK_COLON) {
char temp_namespace [1024];
memcpy(temp_namespace, tk.start, tk.length);
temp_namespace[tk.length] = 0;
ctx->current_namespace = shgets(ctx->attribute_namespace_set, temp_namespace).key;
if (!ctx->current_namespace) {
strcat(temp_namespace, "_");
ctx->current_namespace = shgets(ctx->attribute_namespace_set, temp_namespace).key;
if (!ctx->current_namespace) {
parse_error(ctx, tk, "Namespace does not exist.");
return -1;
}
}
tidx->index += 1;
continue;
} else {
tidx->index--;
int error = parse_attribute(ctx, tidx, directive->type, directive->member_index, &data);
if (error) return -1;
arrput(attributes, data);
}
} else if (tk.type == TK_AT) {
if (tk_equal(&tidx->list[tidx->index], "global") && tidx->list[tidx->index + 1].type == TK_COLON) {
ctx->current_namespace = NULL;
tidx->index += 2;
continue;
} else {
goto invalid_symbol;
}
} else if (tk.type == TK_NUMBER) {
data.id = ctx->builtin.id;
// @copy from above
char * end;
long result = strtol(tk.start, &end, 0);
advance_to(tidx, end);
if (end == tk.start) {
parse_error(ctx, tk, "Invalid integer.");
return -1;
}
data.v.i = (int32_t)result;
arrput(attributes, data);
} else if (tk.type == TK_EQUAL) {
data.id = ctx->builtin.fallback;
if (handle_value_attribute(ctx, tidx, directive->type, directive->member_index, &data, &tk)) return -1;
arrput(attributes, data);
} else if (tk.type == TK_TILDE) {
data.id = ctx->builtin.remove;
AttributeParseInfo rm_attr_info;
if (!parse_attribute_name(ctx, tidx, &rm_attr_info)) return -1;
data.v.i = (int32_t)rm_attr_info.final_id;
arrput(attributes, data);
} else if (tk.type == TK_R_PARENTHESIS) {
break;
} else {
invalid_symbol: ;
parse_error(ctx, tk, "Invalid symbol.");
return -1;
}
tk = next_token(tidx);
if (tk.type == TK_COMMA) {
} else if (tk.type == TK_R_PARENTHESIS) {
break;
} else {
parse_error(ctx, tk, "Expected ',' or ')'.");
return -1;
}
}
directive->count = arrlenu(attributes);
directive->attr_data = attributes;
return 0;
}
static void
add_attribute(ParseContext * ctx, ParseInfo * o_info, AttributeParseInfo * info, char * name) {
IntroAttributeInfo attribute = {
.name = name,
.category = info->category,
};
if (info->type_ptr) {
attribute.id = hmget(o_info->index_by_ptr_map, info->type_ptr);
_assume(attribute.id != 0);
} else {
attribute.id = 0;
}
int final_id = arrlen(o_info->attr.available);
arrput(o_info->attr.available, attribute);
info->final_id = final_id;
hmput(ctx->parse_info_by_id, final_id, info);
int builtin_offset = shget(ctx->builtin_map, name);
if (builtin_offset >= 0) {
*((uint8_t *)&ctx->builtin + builtin_offset) = (uint8_t)final_id;
}
if (info->global) {
AttributeData data = {
.id = final_id,
};
arrput(ctx->attribute_globals, data);
}
}
int
attribute_data_sort_callback(const void * p_a, const void * p_b) {
const AttributeData a = *(AttributeData *)p_a;
const AttributeData b = *(AttributeData *)p_b;
return a.id - b.id;
}
void
apply_attributes(ParseContext * ctx, IntroType * type, int32_t member_index, const AttributeData * data, int32_t count) {
AttributeDataKey key = {
.type = type,
.member_index = member_index,
};
AttributeDataMap * pcontent = hmgetp_null(ctx->attribute_data_map, key);
if (!pcontent) {
hmput(ctx->attribute_data_map, key, NULL);
pcontent = hmgetp_null(ctx->attribute_data_map, key);
_assume(pcontent != NULL);
}
for (int i=0; i < count; i++) {
AttributeParseInfo * info = hmget(ctx->parse_info_by_id, data[i].id);
_assume(info != NULL);
if (member_index == MIDX_TYPE_PROPAGATED && !info->propagate) {
continue;
}
if (info->imply_directive_index != 0) {
AttributeDirective implied_directive = ctx->attribute_directives[info->imply_directive_index];
apply_attributes(ctx, type, member_index, implied_directive.attr_data, implied_directive.count);
}
uint32_t check_id = (data[i].id == ctx->builtin.remove)? data[i].v.i : data[i].id;
for (int j=0; j < arrlen(pcontent->value); j++) {
if (pcontent->value[j].id == check_id) {
arrdelswap(pcontent->value, j);
break;
}
}
if (!info->transient) arrput(pcontent->value, data[i]);
}
}
static void
handle_attributes(ParseContext * ctx, ParseInfo * o_info) {
int * flags = NULL;
arrsetcap(flags, 16);
arrsetcap(o_info->attr.available, 32);
arrsetcap(ctx->value_buffer, (1 << 16));
for (int i=0; i < shlen(ctx->attribute_map); i++) {
AttributeParseInfo * info = &ctx->attribute_map[i].value;
if (info->category == INTRO_AT_FLAG) {
arrput(flags, i);
continue;
}
add_attribute(ctx, o_info, info, ctx->attribute_map[i].key);
}
o_info->attr.first_flag = arrlen(o_info->attr.available);
for (int i=0; i < arrlen(flags); i++) {
int index = flags[i];
char * name = ctx->attribute_map[index].key;
AttributeParseInfo * info = &ctx->attribute_map[index].value;
add_attribute(ctx, o_info, info, name);
}
arrfree(flags);
IntroAttributeData * empty = arraddnptr(o_info->attr.data, 1);
memset(empty, 0, sizeof(*empty));
// parse implied directives first
for (int directive_i=0; directive_i < arrlen(ctx->attribute_directives); directive_i++) {
AttributeDirective * p_directive = &ctx->attribute_directives[directive_i];
if (p_directive->type != NULL) continue;
int ret = parse_attribute_directive(ctx, p_directive);
if (ret) exit(1);
}
for (int directive_i=0; directive_i < arrlen(ctx->attribute_directives); directive_i++) {
AttributeDirective directive = ctx->attribute_directives[directive_i];
if (directive.type == NULL) continue;
if (!directive.attr_data) {
int ret = parse_attribute_directive(ctx, &directive);
if (ret) exit(1);
} else {
for (int attr_i=0; attr_i < directive.count; attr_i++) {
uint32_t old_id = directive.attr_data[attr_i].id;
directive.attr_data[attr_i].id = shget(ctx->attribute_map, g_builtin_attributes[old_id].key).final_id;
}
}
// add propagated type attributes
bool found_inheritance = false;
AttributeDataKey key = {0};
key.member_index = MIDX_TYPE_PROPAGATED;
if (directive.member_index >= 0) {
if (intro_has_members(directive.type)) {
IntroMember member = directive.type->members[directive.member_index];
key.type = member.type;
} else if (directive.type->category == INTRO_ENUM) {
key.type = directive.type; // NOTE: maybe this should be set to null instead
} else {
_assume(0 /* Vibe check failed. */);
}
} else {
if (directive.type->parent) {
key.type = directive.type->parent;
}
}
if (key.type != NULL) {
AttributeData * type_attr_data = NULL;
while (1) {
type_attr_data = hmget(ctx->attribute_data_map, key);
if (!type_attr_data && key.type->parent) {
key.type = key.type->parent;
} else {
break;
}
}
if (type_attr_data && arrlen(type_attr_data) > 0) {
apply_attributes(ctx, directive.type, directive.member_index, type_attr_data, arrlen(type_attr_data));
found_inheritance = true;
}
}
// add global flags if nothing could be inherited
if (!found_inheritance) {
apply_attributes(ctx, directive.type, directive.member_index, ctx->attribute_globals, arrlen(ctx->attribute_globals));
}
// add attributes from directive
apply_attributes(ctx, directive.type, directive.member_index, directive.attr_data, directive.count);
// add to heritable (propagated) attributes if this is for a type
if (directive.member_index == MIDX_TYPE) {
AttributeDataKey key = {.type = directive.type, .member_index = directive.member_index};
AttributeData * all_data = hmget(ctx->attribute_data_map, key);
if (arrlen(all_data) > 0) {
// function ignores non-propagated attributes
apply_attributes(ctx, directive.type, MIDX_TYPE_PROPAGATED, all_data, arrlen(all_data));
}
}
}
struct {IntroType * key; IntroAttributeDataId value;} * propagated_map = NULL;
HashTable * attr_data_set = new_table(1024);
for (int data_i=0; data_i < hmlen(ctx->attribute_data_map); data_i++) {