-
-
Notifications
You must be signed in to change notification settings - Fork 548
/
effect_parser_stmt.cpp
2662 lines (2258 loc) · 85.6 KB
/
effect_parser_stmt.cpp
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
/*
* Copyright (C) 2014 Patrick Mours
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "effect_lexer.hpp"
#include "effect_parser.hpp"
#include "effect_codegen.hpp"
#include <cctype> // std::toupper
#include <cassert>
#include <iterator> // std::back_inserter
#include <algorithm> // std::max, std::replace, std::transform
#include <string_view>
template <typename ENTER_TYPE, typename LEAVE_TYPE>
struct scope_guard
{
explicit scope_guard(ENTER_TYPE &&enter_lambda, LEAVE_TYPE &&leave_lambda) :
leave_lambda(std::forward<LEAVE_TYPE>(leave_lambda)) { enter_lambda(); }
~scope_guard() { leave_lambda(); }
private:
LEAVE_TYPE leave_lambda;
};
bool reshadefx::parser::parse(std::string input, codegen *backend)
{
_lexer = std::make_unique<lexer>(std::move(input));
// Set backend for subsequent code-generation
_codegen = backend;
assert(backend != nullptr);
consume();
bool parse_success = true;
bool current_success = true;
while (!peek(tokenid::end_of_file))
{
if (!parse_top(current_success))
return false;
if (!current_success)
parse_success = false;
}
if (parse_success)
backend->optimize_bindings();
return parse_success;
}
bool reshadefx::parser::parse_top(bool &parse_success)
{
if (accept(tokenid::namespace_))
{
// Anonymous namespaces are not supported right now, so an identifier is a must
if (!expect(tokenid::identifier))
return false;
const std::string name = std::move(_token.literal_as_string);
if (!expect('{'))
return false;
enter_namespace(name);
bool current_success = true;
bool parse_success_namespace = true;
// Recursively parse top level statements until the namespace is closed again
while (!peek('}')) // Empty namespaces are valid
{
if (!parse_top(current_success))
return false;
if (!current_success)
parse_success_namespace = false;
}
leave_namespace();
parse_success = expect('}') && parse_success_namespace;
}
else if (accept(tokenid::struct_)) // Structure keyword found, parse the structure definition
{
// Structure definitions are terminated with a semicolon
parse_success = parse_struct() && expect(';');
}
else if (accept(tokenid::technique)) // Technique keyword found, parse the technique definition
{
parse_success = parse_technique();
}
else
{
location attribute_location;
shader_type stype = shader_type::unknown;
int num_threads[3] = { 0, 0, 0 };
// Read any function attributes first
while (accept('['))
{
if (!expect(tokenid::identifier))
return false;
const std::string attribute = std::move(_token.literal_as_string);
if (attribute == "shader")
{
attribute_location = _token_next.location;
if (!expect('(') || !expect(tokenid::string_literal))
return false;
if (_token.literal_as_string == "vertex")
stype = shader_type::vertex;
else if (_token.literal_as_string == "pixel")
stype = shader_type::pixel;
else if (_token.literal_as_string == "compute")
stype = shader_type::compute;
if (!expect(')'))
return false;
}
else if (attribute == "numthreads")
{
attribute_location = _token_next.location;
expression x, y, z;
if (!expect('(') || !parse_expression_multary(x, 8) || !expect(',') || !parse_expression_multary(y, 8) || !expect(',') || !parse_expression_multary(z, 8) || !expect(')'))
return false;
if (!x.is_constant)
{
error(x.location, 3011, "value must be a literal expression");
parse_success = false;
}
if (!y.is_constant)
{
error(y.location, 3011, "value must be a literal expression");
parse_success = false;
}
if (!z.is_constant)
{
error(z.location, 3011, "value must be a literal expression");
parse_success = false;
}
x.add_cast_operation({ type::t_int, 1, 1 });
y.add_cast_operation({ type::t_int, 1, 1 });
z.add_cast_operation({ type::t_int, 1, 1 });
num_threads[0] = x.constant.as_int[0];
num_threads[1] = y.constant.as_int[0];
num_threads[2] = z.constant.as_int[0];
}
else
{
warning(_token.location, 0, "unknown attribute '" + attribute + "'");
}
if (!expect(']'))
return false;
}
if (type type = {}; parse_type(type)) // Type found, this can be either a variable or a function declaration
{
parse_success = expect(tokenid::identifier);
if (!parse_success)
return true;
if (peek('('))
{
const std::string name = std::move(_token.literal_as_string);
// This is definitely a function declaration, so parse it
if (!parse_function(type, name, stype, num_threads))
{
// Insert dummy function into symbol table, so later references can be resolved despite the error
insert_symbol(name, { symbol_type::function, UINT32_MAX, { type::t_function } }, true);
parse_success = false;
return true;
}
}
else
{
if (!attribute_location.source.empty())
{
error(attribute_location, 0, "attribute is valid only on functions");
parse_success = false;
}
// There may be multiple variable names after the type, handle them all
unsigned int count = 0;
do
{
if (count++ > 0 && !(expect(',') && expect(tokenid::identifier)))
{
parse_success = false;
return false;
}
const std::string name = std::move(_token.literal_as_string);
if (!parse_variable(type, name, true))
{
// Insert dummy variable into symbol table, so later references can be resolved despite the error
insert_symbol(name, { symbol_type::variable, UINT32_MAX, type }, true);
// Skip the rest of the statement
consume_until(';');
parse_success = false;
return true;
}
}
while (!peek(';'));
// Variable declarations are terminated with a semicolon
parse_success = expect(';');
}
}
else if (accept(';')) // Ignore single semicolons in the source
{
parse_success = true;
}
else
{
// Unexpected token in source stream, consume and report an error about it
consume();
// Only add another error message if succeeded parsing previously
// This is done to avoid walls of error messages because of consequential errors following a top-level syntax mistake
if (parse_success)
error(_token.location, 3000, "syntax error: unexpected '" + token::id_to_name(_token.id) + '\'');
parse_success = false;
}
}
return true;
}
bool reshadefx::parser::parse_statement(bool scoped)
{
if (!_codegen->is_in_block())
{
error(_token_next.location, 0, "unreachable code");
return false;
}
unsigned int loop_control = 0;
unsigned int selection_control = 0;
// Read any loop and branch control attributes first
while (accept('['))
{
enum control_mask
{
unroll = 0x1,
dont_unroll = 0x2,
flatten = (0x1 << 4),
dont_flatten = (0x2 << 4),
switch_force_case = (0x4 << 4),
switch_call = (0x8 << 4)
};
const std::string attribute = std::move(_token_next.literal_as_string);
if (!expect(tokenid::identifier) || !expect(']'))
return false;
if (attribute == "unroll")
loop_control |= unroll;
else if (attribute == "loop" || attribute == "fastopt")
loop_control |= dont_unroll;
else if (attribute == "flatten")
selection_control |= flatten;
else if (attribute == "branch")
selection_control |= dont_flatten;
else if (attribute == "forcecase")
selection_control |= switch_force_case;
else if (attribute == "call")
selection_control |= switch_call;
else
warning(_token.location, 0, "unknown attribute '" + attribute + "'");
if ((loop_control & (unroll | dont_unroll)) == (unroll | dont_unroll))
{
error(_token.location, 3524, "can't use loop and unroll attributes together");
return false;
}
if ((selection_control & (flatten | dont_flatten)) == (flatten | dont_flatten))
{
error(_token.location, 3524, "can't use branch and flatten attributes together");
return false;
}
}
// Shift by two so that the possible values are 0x01 for 'flatten' and 0x02 for 'dont_flatten', equivalent to 'unroll' and 'dont_unroll'
selection_control >>= 4;
if (peek('{')) // Parse statement block
return parse_statement_block(scoped);
if (accept(';')) // Ignore empty statements
return true;
// Most statements with the exception of declarations are only valid inside functions
if (_codegen->is_in_function())
{
const location statement_location = _token_next.location;
if (accept(tokenid::if_))
{
codegen::id true_block = _codegen->create_block(); // Block which contains the statements executed when the condition is true
codegen::id false_block = _codegen->create_block(); // Block which contains the statements executed when the condition is false
const codegen::id merge_block = _codegen->create_block(); // Block that is executed after the branch re-merged with the current control flow
expression condition_exp;
if (!expect('(') || !parse_expression(condition_exp) || !expect(')'))
return false;
if (!condition_exp.type.is_scalar())
{
error(condition_exp.location, 3019, "if statement conditional expressions must evaluate to a scalar");
return false;
}
// Load condition and convert to boolean value as required by 'OpBranchConditional' in SPIR-V
condition_exp.add_cast_operation({ type::t_bool, 1, 1 });
const codegen::id condition_value = _codegen->emit_load(condition_exp);
const codegen::id condition_block = _codegen->leave_block_and_branch_conditional(condition_value, true_block, false_block);
{ // Then block of the if statement
_codegen->enter_block(true_block);
if (!parse_statement(true))
return false;
true_block = _codegen->leave_block_and_branch(merge_block);
}
{ // Else block of the if statement
_codegen->enter_block(false_block);
if (accept(tokenid::else_) && !parse_statement(true))
return false;
false_block = _codegen->leave_block_and_branch(merge_block);
}
_codegen->enter_block(merge_block);
// Emit structured control flow for an if statement and connect all basic blocks
_codegen->emit_if(statement_location, condition_value, condition_block, true_block, false_block, selection_control);
return true;
}
if (accept(tokenid::switch_))
{
const codegen::id merge_block = _codegen->create_block(); // Block that is executed after the switch re-merged with the current control flow
expression selector_exp;
if (!expect('(') || !parse_expression(selector_exp) || !expect(')'))
return false;
if (!selector_exp.type.is_scalar())
{
error(selector_exp.location, 3019, "switch statement expression must evaluate to a scalar");
return false;
}
// Load selector and convert to integral value as required by switch instruction
selector_exp.add_cast_operation({ type::t_int, 1, 1 });
const codegen::id selector_value = _codegen->emit_load(selector_exp);
const codegen::id selector_block = _codegen->leave_block_and_switch(selector_value, merge_block);
if (!expect('{'))
return false;
scope_guard _(
[this, merge_block]() {
_loop_break_target_stack.push_back(merge_block);
},
[this]() {
_loop_break_target_stack.pop_back();
});
bool parse_success = true;
// The default case jumps to the end of the switch statement if not overwritten
codegen::id default_label = merge_block, default_block = merge_block;
codegen::id current_label = _codegen->create_block();
std::vector<codegen::id> case_literal_and_labels, case_blocks;
size_t last_case_label_index = 0;
// Enter first switch statement body block
_codegen->enter_block(current_label);
while (!peek(tokenid::end_of_file))
{
while (accept(tokenid::case_) || accept(tokenid::default_))
{
if (_token.id == tokenid::case_)
{
expression case_label;
if (!parse_expression(case_label))
{
consume_until('}');
return false;
}
if (!case_label.type.is_scalar() || !case_label.type.is_integral() || !case_label.is_constant)
{
error(case_label.location, 3020, "invalid type for case expression - value must be an integer scalar");
consume_until('}');
return false;
}
// Check for duplicate case values
for (size_t i = 0; i < case_literal_and_labels.size(); i += 2)
{
if (case_literal_and_labels[i] == case_label.constant.as_uint[0])
{
parse_success = false;
error(case_label.location, 3532, "duplicate case " + std::to_string(case_label.constant.as_uint[0]));
break;
}
}
case_blocks.emplace_back(); // This is set to the actual block below
case_literal_and_labels.push_back(case_label.constant.as_uint[0]);
case_literal_and_labels.push_back(current_label);
}
else
{
// Check if the default label was already changed by a previous 'default' statement
if (default_label != merge_block)
{
parse_success = false;
error(_token.location, 3532, "duplicate default in switch statement");
}
default_label = current_label;
default_block = 0; // This is set to the actual block below
}
if (!expect(':'))
{
consume_until('}');
return false;
}
}
// It is valid for no statement to follow if this is the last label in the switch body
const bool end_of_switch = peek('}');
if (!end_of_switch && !parse_statement(true))
{
consume_until('}');
return false;
}
// Handle fall-through case and end of switch statement
if (peek(tokenid::case_) || peek(tokenid::default_) || end_of_switch)
{
if (_codegen->is_in_block()) // Disallow fall-through for now
{
parse_success = false;
error(_token_next.location, 3533, "non-empty case statements must have break or return");
}
const codegen::id next_label = end_of_switch ? merge_block : _codegen->create_block();
// This is different from 'current_label', since there may have been branching logic inside the case, which would have changed the active block
const codegen::id current_block = _codegen->leave_block_and_branch(next_label);
if (0 == default_block)
default_block = current_block;
for (size_t i = last_case_label_index; i < case_blocks.size(); ++i)
// Need to use the initial label for the switch table, but the current block to merge all the block data
case_blocks[i] = current_block;
current_label = next_label;
_codegen->enter_block(current_label);
if (end_of_switch) // We reached the end, nothing more to do
break;
last_case_label_index = case_blocks.size();
}
}
if (case_literal_and_labels.empty() && default_label == merge_block)
warning(statement_location, 5002, "switch statement contains no 'case' or 'default' labels");
// Emit structured control flow for a switch statement and connect all basic blocks
_codegen->emit_switch(statement_location, selector_value, selector_block, default_label, default_block, case_literal_and_labels, case_blocks, selection_control);
return expect('}') && parse_success;
}
if (accept(tokenid::for_))
{
if (!expect('('))
return false;
scope_guard _(
[this]() { enter_scope(); },
[this]() { leave_scope(); });
// Parse initializer first
if (type type = {}; parse_type(type))
{
unsigned int count = 0;
do
{
// There may be multiple declarations behind a type, so loop through them
if (count++ > 0 && !expect(','))
return false;
if (!expect(tokenid::identifier) || !parse_variable(type, std::move(_token.literal_as_string)))
return false;
}
while (!peek(';'));
}
else
{
// Initializer can also contain an expression if not a variable declaration list and not empty
if (!peek(';'))
{
expression initializer_exp;
if (!parse_expression(initializer_exp))
return false;
}
}
if (!expect(';'))
return false;
const codegen::id merge_block = _codegen->create_block(); // Block that is executed after the loop
const codegen::id header_label = _codegen->create_block(); // Pointer to the loop merge instruction
const codegen::id continue_label = _codegen->create_block(); // Pointer to the continue block
codegen::id loop_block = _codegen->create_block(); // Pointer to the main loop body block
codegen::id condition_block = _codegen->create_block(); // Pointer to the condition check
codegen::id condition_value = 0;
// End current block by branching to the next label
const codegen::id prev_block = _codegen->leave_block_and_branch(header_label);
{ // Begin loop block (this header is used for explicit structured control flow)
_codegen->enter_block(header_label);
_codegen->leave_block_and_branch(condition_block);
}
{ // Parse condition block
_codegen->enter_block(condition_block);
if (!peek(';'))
{
expression condition_exp;
if (!parse_expression(condition_exp))
return false;
if (!condition_exp.type.is_scalar())
{
error(condition_exp.location, 3019, "scalar value expected");
return false;
}
// Evaluate condition and branch to the right target
condition_exp.add_cast_operation({ type::t_bool, 1, 1 });
condition_value = _codegen->emit_load(condition_exp);
condition_block = _codegen->leave_block_and_branch_conditional(condition_value, loop_block, merge_block);
}
else // It is valid for there to be no condition expression
{
condition_block = _codegen->leave_block_and_branch(loop_block);
}
if (!expect(';'))
return false;
}
{ // Parse loop continue block into separate block so it can be appended to the end down the line
_codegen->enter_block(continue_label);
if (!peek(')'))
{
expression continue_exp;
if (!parse_expression(continue_exp))
return false;
}
if (!expect(')'))
return false;
// Branch back to the loop header at the end of the continue block
_codegen->leave_block_and_branch(header_label);
}
{ // Parse loop body block
_codegen->enter_block(loop_block);
_loop_break_target_stack.push_back(merge_block);
_loop_continue_target_stack.push_back(continue_label);
const bool parse_success = parse_statement(false);
_loop_break_target_stack.pop_back();
_loop_continue_target_stack.pop_back();
if (!parse_success)
return false;
loop_block = _codegen->leave_block_and_branch(continue_label);
}
// Add merge block label to the end of the loop
_codegen->enter_block(merge_block);
// Emit structured control flow for a loop statement and connect all basic blocks
_codegen->emit_loop(statement_location, condition_value, prev_block, header_label, condition_block, loop_block, continue_label, loop_control);
return true;
}
if (accept(tokenid::while_))
{
scope_guard _(
[this]() { enter_scope(); },
[this]() { leave_scope(); });
const codegen::id merge_block = _codegen->create_block();
const codegen::id header_label = _codegen->create_block();
const codegen::id continue_label = _codegen->create_block();
codegen::id loop_block = _codegen->create_block();
codegen::id condition_block = _codegen->create_block();
codegen::id condition_value = 0;
// End current block by branching to the next label
const codegen::id prev_block = _codegen->leave_block_and_branch(header_label);
{ // Begin loop block
_codegen->enter_block(header_label);
_codegen->leave_block_and_branch(condition_block);
}
{ // Parse condition block
_codegen->enter_block(condition_block);
expression condition_exp;
if (!expect('(') || !parse_expression(condition_exp) || !expect(')'))
return false;
if (!condition_exp.type.is_scalar())
{
error(condition_exp.location, 3019, "scalar value expected");
return false;
}
// Evaluate condition and branch to the right target
condition_exp.add_cast_operation({ type::t_bool, 1, 1 });
condition_value = _codegen->emit_load(condition_exp);
condition_block = _codegen->leave_block_and_branch_conditional(condition_value, loop_block, merge_block);
}
{ // Parse loop body block
_codegen->enter_block(loop_block);
_loop_break_target_stack.push_back(merge_block);
_loop_continue_target_stack.push_back(continue_label);
const bool parse_success = parse_statement(false);
_loop_break_target_stack.pop_back();
_loop_continue_target_stack.pop_back();
if (!parse_success)
return false;
loop_block = _codegen->leave_block_and_branch(continue_label);
}
{ // Branch back to the loop header in empty continue block
_codegen->enter_block(continue_label);
_codegen->leave_block_and_branch(header_label);
}
// Add merge block label to the end of the loop
_codegen->enter_block(merge_block);
// Emit structured control flow for a loop statement and connect all basic blocks
_codegen->emit_loop(statement_location, condition_value, prev_block, header_label, condition_block, loop_block, continue_label, loop_control);
return true;
}
if (accept(tokenid::do_))
{
const codegen::id merge_block = _codegen->create_block();
const codegen::id header_label = _codegen->create_block();
const codegen::id continue_label = _codegen->create_block();
codegen::id loop_block = _codegen->create_block();
codegen::id condition_value = 0;
// End current block by branching to the next label
const codegen::id prev_block = _codegen->leave_block_and_branch(header_label);
{ // Begin loop block
_codegen->enter_block(header_label);
_codegen->leave_block_and_branch(loop_block);
}
{ // Parse loop body block
_codegen->enter_block(loop_block);
_loop_break_target_stack.push_back(merge_block);
_loop_continue_target_stack.push_back(continue_label);
const bool parse_success = parse_statement(true);
_loop_break_target_stack.pop_back();
_loop_continue_target_stack.pop_back();
if (!parse_success)
return false;
loop_block = _codegen->leave_block_and_branch(continue_label);
}
{ // Continue block does the condition evaluation
_codegen->enter_block(continue_label);
expression condition_exp;
if (!expect(tokenid::while_) || !expect('(') || !parse_expression(condition_exp) || !expect(')') || !expect(';'))
return false;
if (!condition_exp.type.is_scalar())
{
error(condition_exp.location, 3019, "scalar value expected");
return false;
}
// Evaluate condition and branch to the right target
condition_exp.add_cast_operation({ type::t_bool, 1, 1 });
condition_value = _codegen->emit_load(condition_exp);
_codegen->leave_block_and_branch_conditional(condition_value, header_label, merge_block);
}
// Add merge block label to the end of the loop
_codegen->enter_block(merge_block);
// Emit structured control flow for a loop statement and connect all basic blocks
_codegen->emit_loop(statement_location, condition_value, prev_block, header_label, 0, loop_block, continue_label, loop_control);
return true;
}
if (accept(tokenid::break_))
{
if (_loop_break_target_stack.empty())
{
error(statement_location, 3518, "break must be inside loop");
return false;
}
// Branch to the break target of the inner most loop on the stack
_codegen->leave_block_and_branch(_loop_break_target_stack.back(), 1);
return expect(';');
}
if (accept(tokenid::continue_))
{
if (_loop_continue_target_stack.empty())
{
error(statement_location, 3519, "continue must be inside loop");
return false;
}
// Branch to the continue target of the inner most loop on the stack
_codegen->leave_block_and_branch(_loop_continue_target_stack.back(), 2);
return expect(';');
}
if (accept(tokenid::return_))
{
const type &return_type = _codegen->_current_function->return_type;
if (!peek(';'))
{
expression return_exp;
if (!parse_expression(return_exp))
{
consume_until(';');
return false;
}
// Cannot return to void
if (return_type.is_void())
{
error(statement_location, 3079, "void functions cannot return a value");
// Consume the semicolon that follows the return expression so that parsing may continue
accept(';');
return false;
}
// Cannot return arrays from a function
if (return_exp.type.is_array() || !type::rank(return_exp.type, return_type))
{
error(statement_location, 3017, "expression (" + return_exp.type.description() + ") does not match function return type (" + return_type.description() + ')');
accept(';');
return false;
}
// Load return value and perform implicit cast to function return type
if (return_exp.type.components() > return_type.components())
warning(return_exp.location, 3206, "implicit truncation of vector type");
return_exp.add_cast_operation(return_type);
const codegen::id return_value = _codegen->emit_load(return_exp);
_codegen->leave_block_and_return(return_value);
}
else if (!return_type.is_void())
{
// No return value was found, but the function expects one
error(statement_location, 3080, "function must return a value");
// Consume the semicolon that follows the return expression so that parsing may continue
accept(';');
return false;
}
else
{
_codegen->leave_block_and_return();
}
return expect(';');
}
if (accept(tokenid::discard_))
{
// Leave the current function block
_codegen->leave_block_and_kill();
return expect(';');
}
}
// Handle variable declarations
if (type type = {}; parse_type(type))
{
unsigned int count = 0;
do
{
// There may be multiple declarations behind a type, so loop through them
if (count++ > 0 && !expect(','))
{
// Try to consume the rest of the declaration so that parsing may continue despite the error
consume_until(';');
return false;
}
if (!expect(tokenid::identifier) || !parse_variable(type, std::move(_token.literal_as_string)))
{
consume_until(';');
return false;
}
}
while (!peek(';'));
return expect(';');
}
// Handle expression statements
expression statement_exp;
if (parse_expression(statement_exp))
return expect(';'); // A statement has to be terminated with a semicolon
// Gracefully consume any remaining characters until the statement would usually end, so that parsing may continue despite the error
consume_until(';');
return false;
}
bool reshadefx::parser::parse_statement_block(bool scoped)
{
if (!expect('{'))
return false;
if (scoped)
enter_scope();
// Parse statements until the end of the block is reached
while (!peek('}') && !peek(tokenid::end_of_file))
{
if (!parse_statement(true))
{
if (scoped)
leave_scope();
// Ignore the rest of this block
unsigned int level = 0;
while (!peek(tokenid::end_of_file))
{
if (accept('{'))
{
++level;
}
else if (accept('}'))
{
if (level-- == 0)
break;
} // These braces are necessary to match the 'else' to the correct 'if' statement
else
{
consume();
}
}
return false;
}
}
if (scoped)
leave_scope();
return expect('}');
}
bool reshadefx::parser::parse_type(type &type)
{
type.qualifiers = 0;
accept_type_qualifiers(type);
if (!accept_type_class(type))
return false;
if (type.is_integral() && (type.has(type::q_centroid) || type.has(type::q_noperspective)))
{
error(_token.location, 4576, "signature specifies invalid interpolation mode for integer component type");
return false;
}
if (type.has(type::q_centroid) && !type.has(type::q_noperspective))
type.qualifiers |= type::q_linear;
return true;
}
bool reshadefx::parser::parse_array_length(type &type)
{
// Reset array length to zero before checking if one exists
type.array_length = 0;
if (accept('['))
{
if (accept(']'))
{
// No length expression, so this is an unbounded array
type.array_length = 0xFFFFFFFF;
}
else if (expression length_exp; parse_expression(length_exp) && expect(']'))
{
if (!length_exp.is_constant || !(length_exp.type.is_scalar() && length_exp.type.is_integral()))
{
error(length_exp.location, 3058, "array dimensions must be literal scalar expressions");
return false;
}
type.array_length = length_exp.constant.as_uint[0];
if (type.array_length < 1 || type.array_length > 65536)
{
error(length_exp.location, 3059, "array dimension must be between 1 and 65536");
return false;
}
}
else
{
return false;
}
}
// Multi-dimensional arrays are not supported
if (peek('['))
{
error(_token_next.location, 3119, "arrays cannot be multi-dimensional");
return false;
}
return true;
}