forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 12
/
effect_preprocessor.cpp
1186 lines (1027 loc) · 28.9 KB
/
effect_preprocessor.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. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#include "effect_lexer.hpp"
#include "effect_preprocessor.hpp"
#include <cassert>
enum op_type
{
op_none = -1,
op_or,
op_and,
op_bitor,
op_bitxor,
op_bitand,
op_not_equal,
op_equal,
op_less,
op_greater,
op_less_equal,
op_greater_equal,
op_leftshift,
op_rightshift,
op_add,
op_subtract,
op_modulo,
op_divide,
op_multiply,
op_plus,
op_negate,
op_not,
op_bitnot,
op_parentheses
};
enum macro_replacement
{
macro_replacement_start = '\x00',
macro_replacement_argument = '\xFA',
macro_replacement_concat = '\xFF',
macro_replacement_stringize = '\xFE',
macro_replacement_space = '\xFD',
macro_replacement_break = '\xFC',
macro_replacement_expand = '\xFB',
};
static const int precedence_lookup[] = {
0, 1, 2, 3, 4, // bitwise operators
5, 6, 7, 7, 7, 7, // logical operators
8, 8, // left shift, right shift
9, 9, // add, subtract
10, 10, 10, // modulo, divide, multiply
11, 11, 11, 11 // unary operators
};
static bool read_file(const std::filesystem::path &path, std::string &data)
{
#ifdef _WIN32
FILE *file = nullptr;
if (_wfopen_s(&file, path.c_str(), L"rb") != 0)
return false;
#else
FILE *const file = fopen(path.c_str(), "rb");
if (file == nullptr)
return false;
#endif
// Read file contents into memory
std::vector<char> mem(static_cast<size_t>(std::filesystem::file_size(path) + 1));
const size_t eof = fread(mem.data(), 1, mem.size() - 1, file);
// Append a new line feed to the end of the input string to avoid issues with parsing
mem[eof] = '\n';
// No longer need to have a handle open to the file, since all data was read, so can safely close it
fclose(file);
std::string_view filedata(mem.data(), mem.size());
// Remove BOM (0xefbbbf means 0xfeff)
if (filedata.size() >= 3 &&
static_cast<unsigned char>(filedata[0]) == 0xef &&
static_cast<unsigned char>(filedata[1]) == 0xbb &&
static_cast<unsigned char>(filedata[2]) == 0xbf)
filedata = std::string_view(filedata.data() + 3, filedata.size() - 3);
data = filedata;
return true;
}
static std::string escape_string(std::string s)
{
for (size_t offset = 0; (offset = s.find('\\', offset)) != std::string::npos; offset += 2)
s.insert(offset, "\\", 1);
return s;
}
reshadefx::preprocessor::preprocessor()
{
}
reshadefx::preprocessor::~preprocessor()
{
}
void reshadefx::preprocessor::add_include_path(const std::filesystem::path &path)
{
assert(!path.empty());
_include_paths.push_back(path);
}
bool reshadefx::preprocessor::add_macro_definition(const std::string &name, const macro ¯o)
{
assert(!name.empty());
return _macros.emplace(name, macro).second;
}
bool reshadefx::preprocessor::append_file(const std::filesystem::path &path)
{
std::string data;
if (!read_file(path, data))
return false;
_success = true; // Clear success flag before parsing a new file
#ifdef _WIN32
push(std::move(data), path.u8string());
#else
push(std::move(data), path.string());
#endif
parse();
return _success;
}
bool reshadefx::preprocessor::append_string(const std::string &source_code)
{
// Enforce all input strings to end with a line feed
assert(!source_code.empty() && source_code.back() == '\n');
_success = true; // Clear success flag before parsing a new string
push(source_code);
parse();
return _success;
}
std::vector<std::filesystem::path> reshadefx::preprocessor::included_files() const
{
std::vector<std::filesystem::path> files;
files.reserve(_filecache.size());
for (const auto &it : _filecache)
files.push_back(std::filesystem::u8path(it.first));
return files;
}
std::vector<std::pair<std::string, std::string>> reshadefx::preprocessor::used_macro_definitions() const
{
std::vector<std::pair<std::string, std::string>> defines;
defines.reserve(_used_macros.size());
for (const auto &name : _used_macros)
// Do not include function-like macros, since they are more likely to contain a complex replacement list
if (const auto it = _macros.find(name); it != _macros.end() && !it->second.is_function_like)
defines.push_back({ name, it->second.replacement_list });
return defines;
}
void reshadefx::preprocessor::error(const location &location, const std::string &message)
{
_errors += location.source + '(' + std::to_string(location.line) + ", " + std::to_string(location.column) + ')' + ": preprocessor error: " + message + '\n';
_success = false; // Unset success flag
}
void reshadefx::preprocessor::warning(const location &location, const std::string &message)
{
_errors += location.source + '(' + std::to_string(location.line) + ", " + std::to_string(location.column) + ')' + ": preprocessor warning: " + message + '\n';
}
void reshadefx::preprocessor::push(std::string input, const std::string &name)
{
input_level level = {};
level.lexer.reset(new lexer(std::move(input), true, false, false, false, true, false));
level.next_token.id = tokenid::unknown;
// Initialize location information
if (!name.empty())
{
level.name = name;
_output += "#line 1 \"" + name + "\"\n";
_output_location.source = name;
}
// Inherit hidden macros from parent
if (!_input_stack.empty())
{
level.hidden_macros = _input_stack.back().hidden_macros;
}
_input_stack.push_back(std::move(level));
_next_input_index = _input_stack.size() - 1;
consume();
}
bool reshadefx::preprocessor::peek(tokenid token) const
{
return _input_stack[_next_input_index].next_token == token;
}
bool reshadefx::preprocessor::consume()
{
_current_input_index = _next_input_index;
if (_input_stack.empty())
{
// End of input has been reached already (this can happen when the input text is not terminated with a new line)
assert(_current_input_index == 0);
return false;
}
// Clear out input stack, now that the current token is overwritten
while (_input_stack.size() > (_current_input_index + 1))
_input_stack.pop_back();
// Update location information after switching input levels
input_level &input = _input_stack[_current_input_index];
if (!input.name.empty() && input.name != _output_location.source)
{
_output += "#line " + std::to_string(input.next_token.location.line) + " \"" + input.name + "\"\n";
_output_location.line = _token.location.line;
_output_location.source = input.name;
}
// Set current token
_token = std::move(input.next_token);
_token.location.source = _output_location.source;
_current_token_raw_data = input.lexer->input_string().substr(_token.offset, _token.length);
// Get the next token
input.next_token = input.lexer->lex();
// Pop input level if lexical analysis has reached the end of it
// This ensures the EOF token is not consumed until the very last file
while (peek(tokenid::end_of_file))
{
// Remove any unterminated blocks from the stack
for (; !_if_stack.empty() && _if_stack.back().input_index >= _next_input_index; _if_stack.pop_back())
{
error(_if_stack.back().token.location, "unterminated #if");
}
if (_next_input_index == 0)
{
// End of input has been reached, so cannot pop further and this is the last token
_input_stack.pop_back();
return false;
}
_next_input_index--;
}
return true;
}
void reshadefx::preprocessor::consume_until(tokenid token)
{
while (!accept(token) && !peek(tokenid::end_of_file))
{
consume();
}
}
bool reshadefx::preprocessor::accept(tokenid token)
{
while (peek(tokenid::space))
{
consume();
}
if (peek(token))
{
consume();
return true;
}
return false;
}
bool reshadefx::preprocessor::expect(tokenid token)
{
if (!accept(token))
{
auto actual_token = _input_stack[_next_input_index].next_token;
actual_token.location.source = _output_location.source;
error(actual_token.location, "syntax error: unexpected token '" +
_input_stack[_next_input_index].lexer->input_string().substr(actual_token.offset, actual_token.length) + '\'');
return false;
}
return true;
}
void reshadefx::preprocessor::parse()
{
std::string line;
while (consume())
{
_recursion_count = 0;
const bool skip = !_if_stack.empty() && _if_stack.back().skipping;
switch (_token)
{
case tokenid::hash_if:
parse_if();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_ifdef:
parse_ifdef();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_ifndef:
parse_ifndef();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_else:
parse_else();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_elif:
parse_elif();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_endif:
parse_endif();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
}
if (skip)
continue;
switch (_token)
{
case tokenid::hash_def:
parse_def();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_undef:
parse_undef();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_error:
parse_error();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_warning:
parse_warning();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_pragma:
parse_pragma();
if (!expect(tokenid::end_of_line))
consume_until(tokenid::end_of_line);
continue;
case tokenid::hash_include:
parse_include();
continue;
case tokenid::hash_unknown:
error(_token.location, "unrecognized preprocessing directive '" + _token.literal_as_string + '\'');
consume_until(tokenid::end_of_line);
continue;
case tokenid::end_of_line:
if (line.empty())
continue;
_output_location.line++;
if (_output_location.line != _token.location.line)
{
_output += "#line " + std::to_string(_token.location.line) + '\n';
_output_location.line = _token.location.line;
}
_output += line;
_output += '\n';
line.clear();
continue;
case tokenid::identifier:
if (evaluate_identifier_as_macro())
continue;
// fall through
default:
line += _current_token_raw_data;
break;
}
}
// Append the last line after the EOF was reached to the output
_output += line;
}
void reshadefx::preprocessor::parse_def()
{
if (!expect(tokenid::identifier))
return;
else if (_token.literal_as_string == "defined")
return warning(_token.location, "macro name 'defined' is reserved");
macro m;
const auto location = std::move(_token.location);
const auto macro_name = std::move(_token.literal_as_string);
const auto macro_name_end_offset = _token.offset + _token.length;
if (_input_stack[_current_input_index].lexer->input_string()[macro_name_end_offset] == '(')
{
accept(tokenid::parenthesis_open);
m.is_function_like = true;
while (accept(tokenid::identifier))
{
m.parameters.push_back(_token.literal_as_string);
if (!accept(tokenid::comma))
break;
}
if (accept(tokenid::ellipsis))
{
m.is_variadic = true;
m.parameters.push_back("__VA_ARGS__");
// TODO: Implement variadic macros
error(_token.location, "variadic macros are not currently supported");
return;
}
if (!expect(tokenid::parenthesis_close))
return;
}
create_macro_replacement_list(m);
if (!add_macro_definition(macro_name, m))
return error(location, "redefinition of '" + macro_name + "'");
}
void reshadefx::preprocessor::parse_undef()
{
if (!expect(tokenid::identifier))
return;
else if (_token.literal_as_string == "defined")
return warning(_token.location, "macro name 'defined' is reserved");
_macros.erase(_token.literal_as_string);
}
void reshadefx::preprocessor::parse_if()
{
if_level level;
level.token = _token;
level.input_index = _current_input_index;
level.value = evaluate_expression();
level.skipping = (!_if_stack.empty() && _if_stack.back().skipping) || !level.value;
_if_stack.push_back(std::move(level));
}
void reshadefx::preprocessor::parse_ifdef()
{
if_level level;
level.token = _token;
level.input_index = _current_input_index;
if (!expect(tokenid::identifier))
return;
level.value = _macros.find(_token.literal_as_string) != _macros.end();
level.skipping = (!_if_stack.empty() && _if_stack.back().skipping) || !level.value;
_if_stack.push_back(std::move(level));
_used_macros.emplace(_token.literal_as_string);
}
void reshadefx::preprocessor::parse_ifndef()
{
if_level level;
level.token = _token;
level.input_index = _current_input_index;
if (!expect(tokenid::identifier))
return;
level.value = _macros.find(_token.literal_as_string) == _macros.end();
level.skipping = (!_if_stack.empty() && _if_stack.back().skipping) || !level.value;
_if_stack.push_back(std::move(level));
_used_macros.emplace(_token.literal_as_string);
}
void reshadefx::preprocessor::parse_elif()
{
if (_if_stack.empty())
return error(_token.location, "missing #if for #elif");
if_level &level = _if_stack.back();
if (level.token == tokenid::hash_else)
return error(_token.location, "#elif is not allowed after #else");
const bool condition_result = evaluate_expression();
level.token = _token;
level.input_index = _current_input_index;
level.skipping = (_if_stack.size() > 1 && _if_stack[_if_stack.size() - 2].skipping) || level.value || !condition_result;
if (!level.value) level.value = condition_result;
}
void reshadefx::preprocessor::parse_else()
{
if (_if_stack.empty())
return error(_token.location, "missing #if for #else");
if_level &level = _if_stack.back();
if (level.token == tokenid::hash_else)
return error(_token.location, "#else is not allowed after #else");
level.token = _token;
level.input_index = _current_input_index;
level.skipping = (_if_stack.size() > 1 && _if_stack[_if_stack.size() - 2].skipping) || level.value;
if (!level.value) level.value = true;
}
void reshadefx::preprocessor::parse_endif()
{
if (_if_stack.empty())
error(_token.location, "missing #if for #endif");
else
_if_stack.pop_back();
}
void reshadefx::preprocessor::parse_error()
{
const auto keyword_location = std::move(_token.location);
if (!expect(tokenid::string_literal))
return;
error(keyword_location, _token.literal_as_string);
}
void reshadefx::preprocessor::parse_warning()
{
const auto keyword_location = std::move(_token.location);
if (!expect(tokenid::string_literal))
return;
warning(keyword_location, _token.literal_as_string);
}
void reshadefx::preprocessor::parse_pragma()
{
const auto keyword_location = std::move(_token.location);
if (!expect(tokenid::identifier))
return;
std::string pragma = std::move(_token.literal_as_string);
while (!peek(tokenid::end_of_line) && !peek(tokenid::end_of_file))
{
consume();
if (_token == tokenid::identifier && evaluate_identifier_as_macro())
continue;
pragma += _current_token_raw_data;
}
if (pragma == "once")
{
if (const auto it = _filecache.find(_output_location.source); it != _filecache.end())
it->second.clear();
return;
}
warning(keyword_location, "unknown pragma ignored");
}
void reshadefx::preprocessor::parse_include()
{
const auto keyword_location = std::move(_token.location);
while (accept(tokenid::identifier))
{
if (evaluate_identifier_as_macro())
continue;
error(_token.location, "syntax error: unexpected identifier in #include");
consume_until(tokenid::end_of_line);
return;
}
if (!expect(tokenid::string_literal))
{
consume_until(tokenid::end_of_line);
return;
}
const std::filesystem::path filename = std::filesystem::u8path(_token.literal_as_string);
std::error_code ec;
std::filesystem::path filepath = std::filesystem::u8path(_output_location.source);
filepath.replace_filename(filename);
if (!std::filesystem::exists(filepath, ec))
for (const auto &include_path : _include_paths)
if (std::filesystem::exists(filepath = include_path / filename, ec))
break;
#ifdef _WIN32
const std::string filepath_string = filepath.u8string();
#else
const std::string filepath_string = filepath.string();
#endif
// Detect recursive include and abort to avoid infinite loop
if (std::find_if(_input_stack.begin(), _input_stack.end(),
[&filepath_string](const auto &level) { return level.name == filepath_string; }) != _input_stack.end())
{
error(_token.location, "recursive #include");
return;
}
std::string data;
if (auto it = _filecache.find(filepath_string);
it != _filecache.end())
{
data = it->second;
}
else
{
if (!read_file(filepath, data))
{
error(keyword_location, "could not open included file '" + filepath_string + '\'');
consume_until(tokenid::end_of_line);
return;
}
_filecache.emplace(filepath_string, data);
}
push(std::move(data), filepath_string);
}
bool reshadefx::preprocessor::evaluate_expression()
{
struct rpn_token
{
int value;
bool is_op;
};
size_t rpn_index = 0;
size_t stack_index = 0;
const size_t STACK_SIZE = 128;
rpn_token rpn[STACK_SIZE];
int stack[STACK_SIZE];
// Keep track of previous token to figure out data type of expression
tokenid previous_token = _token;
// Run shunting-yard algorithm
while (!peek(tokenid::end_of_line))
{
if (stack_index >= STACK_SIZE || rpn_index >= STACK_SIZE)
{
error(_token.location, "expression evaluator ran out of stack space");
return false;
}
int op = op_none;
bool is_left_associative = true;
bool parenthesis_matched = false;
consume();
switch (_token)
{
case tokenid::space:
continue;
case tokenid::backslash:
// Skip to next line if the line ends with a backslash
if (accept(tokenid::end_of_line))
continue;
else // Otherwise continue on processing the token (it is not valid here, but make that an error below)
break;
case tokenid::exclaim:
op = op_not;
is_left_associative = false;
break;
case tokenid::percent:
op = op_modulo;
break;
case tokenid::ampersand:
op = op_bitand;
break;
case tokenid::star:
op = op_multiply;
break;
case tokenid::plus:
is_left_associative =
previous_token == tokenid::int_literal ||
previous_token == tokenid::uint_literal ||
previous_token == tokenid::identifier ||
previous_token == tokenid::parenthesis_close;
op = is_left_associative ? op_add : op_plus;
break;
case tokenid::minus:
is_left_associative =
previous_token == tokenid::int_literal ||
previous_token == tokenid::uint_literal ||
previous_token == tokenid::identifier ||
previous_token == tokenid::parenthesis_close;
op = is_left_associative ? op_subtract : op_negate;
break;
case tokenid::slash:
op = op_divide;
break;
case tokenid::less:
op = op_less;
break;
case tokenid::greater:
op = op_greater;
break;
case tokenid::caret:
op = op_bitxor;
break;
case tokenid::pipe:
op = op_bitor;
break;
case tokenid::tilde:
op = op_bitnot;
is_left_associative = false;
break;
case tokenid::exclaim_equal:
op = op_not_equal;
break;
case tokenid::ampersand_ampersand:
op = op_and;
break;
case tokenid::less_less:
op = op_leftshift;
break;
case tokenid::less_equal:
op = op_less_equal;
break;
case tokenid::equal_equal:
op = op_equal;
break;
case tokenid::greater_greater:
op = op_rightshift;
break;
case tokenid::greater_equal:
op = op_greater_equal;
break;
case tokenid::pipe_pipe:
op = op_or;
break;
}
switch (_token)
{
case tokenid::parenthesis_open:
stack[stack_index++] = op_parentheses;
break;
case tokenid::parenthesis_close:
parenthesis_matched = false;
while (stack_index > 0)
{
const int op2 = stack[--stack_index];
if (op2 == op_parentheses)
{
parenthesis_matched = true;
break;
}
rpn[rpn_index++] = { op2, true };
}
if (!parenthesis_matched)
{
error(_token.location, "unmatched ')'");
return false;
}
break;
case tokenid::identifier:
if (evaluate_identifier_as_macro())
continue;
if (_token.literal_as_string == "exists")
{
const bool has_parentheses = accept(tokenid::parenthesis_open);
while (accept(tokenid::identifier))
{
if (!evaluate_identifier_as_macro())
{
error(_token.location, "syntax error: unexpected identifier after 'exists'");
return false;
}
}
if (!expect(tokenid::string_literal))
return false;
const std::filesystem::path filename = std::filesystem::u8path(_token.literal_as_string);
if (has_parentheses && !expect(tokenid::parenthesis_close))
return false;
std::error_code ec;
std::filesystem::path filepath = std::filesystem::u8path(_output_location.source);
filepath.replace_filename(filename);
if (!std::filesystem::exists(filepath, ec))
for (const auto &include_path : _include_paths)
if (std::filesystem::exists(filepath = include_path / filename, ec))
break;
rpn[rpn_index++] = { std::filesystem::exists(filepath, ec) ? 1 : 0, false };
continue;
}
if (_token.literal_as_string == "defined")
{
const bool has_parentheses = accept(tokenid::parenthesis_open);
if (!expect(tokenid::identifier))
return false;
const std::string macro_name = std::move(_token.literal_as_string);
if (has_parentheses && !expect(tokenid::parenthesis_close))
return false;
rpn[rpn_index++] = { _macros.find(macro_name) != _macros.end() ? 1 : 0, false };
continue;
}
// An identifier that cannot be replaced with a number becomes zero
rpn[rpn_index++] = { 0, false };
break;
case tokenid::int_literal:
case tokenid::uint_literal:
rpn[rpn_index++] = { _token.literal_as_int, false };
break;
default:
if (op == op_none)
{
error(_token.location, "invalid expression");
return false;
}
while (stack_index > 0)
{
const int prev_op = stack[stack_index - 1];
if (prev_op == op_parentheses)
break;
if (is_left_associative ? (precedence_lookup[op] > precedence_lookup[prev_op]) : (precedence_lookup[op] >= precedence_lookup[prev_op]))
break;
stack_index--;
rpn[rpn_index++] = { prev_op, true };
}
stack[stack_index++] = op;
break;
}
previous_token = _token;
}
while (stack_index > 0)
{
const int op = stack[--stack_index];
if (op == op_parentheses)
{
error(_token.location, "unmatched ')'");
return false;
}
rpn[rpn_index++] = { op, true };
}
#define UNARY_OPERATION(op) { \
if (stack_index < 1) \
return error(_token.location, "invalid expression"), 0; \
stack[stack_index - 1] = op stack[stack_index - 1]; \
}
#define BINARY_OPERATION(op) { \
if (stack_index < 2) \
return error(_token.location, "invalid expression"), 0; \
stack[stack_index - 2] = stack[stack_index - 2] op stack[stack_index - 1]; \
stack_index--; \
}
// Evaluate reverse polish notation output
for (auto *token = rpn; rpn_index--; token++)
{
if (token->is_op)
{
switch (token->value)
{
case op_or:
BINARY_OPERATION(||);
break;
case op_and:
BINARY_OPERATION(&&);
break;
case op_bitor:
BINARY_OPERATION(|);
break;
case op_bitxor:
BINARY_OPERATION(^);
break;
case op_bitand:
BINARY_OPERATION(&);
break;
case op_not_equal:
BINARY_OPERATION(!=);
break;
case op_equal:
BINARY_OPERATION(==);
break;
case op_less:
BINARY_OPERATION(<);
break;
case op_greater:
BINARY_OPERATION(>);
break;
case op_less_equal:
BINARY_OPERATION(<=);
break;
case op_greater_equal:
BINARY_OPERATION(>=);
break;
case op_leftshift:
BINARY_OPERATION(<<);
break;
case op_rightshift:
BINARY_OPERATION(>>);
break;
case op_add:
BINARY_OPERATION(+);
break;
case op_subtract:
BINARY_OPERATION(-);
break;
case op_modulo:
BINARY_OPERATION(%);
break;
case op_divide:
BINARY_OPERATION(/);
break;
case op_multiply:
BINARY_OPERATION(*);
break;
case op_plus:
UNARY_OPERATION(+);
break;
case op_negate:
UNARY_OPERATION(-);
break;
case op_not:
UNARY_OPERATION(!);
break;
case op_bitnot:
UNARY_OPERATION(~);
break;
}
}
else
{
stack[stack_index++] = token->value;
}
}
if (stack_index != 1)
{
error(_token.location, "invalid expression");
return false;
}
return stack[0] != 0;
}
bool reshadefx::preprocessor::evaluate_identifier_as_macro()
{
if (_token.literal_as_string == "__FILE__")
{
push('\"' + escape_string(_token.location.source) + '\"');
return true;
}
if (_token.literal_as_string == "__LINE__")
{