-
Notifications
You must be signed in to change notification settings - Fork 30
/
parser.cpp
1895 lines (1755 loc) · 70.1 KB
/
parser.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
#include <string.h> // strcmp, memcpy
#include "parser.h"
#include "util.h"
namespace glsl {
parser::parser(const char *source, const char *fileName)
: m_ast(0)
, m_lexer(source)
, m_fileName(fileName)
{
m_oom = strnew("Out of memory");
m_error = strnew("");
}
parser::~parser() {
delete m_ast;
for (size_t i = 0; i < m_strings.size(); i++)
free(m_strings[i]);
for (size_t i = 0; i < m_memory.size(); i++)
m_memory[i].destroy();
}
#define IS_TYPE(TOKEN, TYPE) \
((TOKEN).m_type == (TYPE))
#define IS_KEYWORD(TOKEN, KEYWORD) \
(IS_TYPE((TOKEN), kType_keyword) && (TOKEN).asKeyword == (KEYWORD))
#define IS_OPERATOR(TOKEN, OPERATOR) \
(IS_TYPE((TOKEN), kType_operator) && (TOKEN).asOperator == (OPERATOR))
#define GC_NEW(X) new(&m_memory)
bool parser::isType(int type) const {
return IS_TYPE(m_token, type);
}
bool parser::isKeyword(int keyword) const {
return IS_KEYWORD(m_token, keyword);
}
bool parser::isOperator(int oper) const {
return IS_OPERATOR(m_token, oper);
}
bool parser::isEndCondition(endCondition condition) const {
return ((condition & kEndConditionSemicolon) && isType(kType_semicolon))
|| ((condition & kEndConditionParanthesis) && isOperator(kOperator_paranthesis_end))
|| ((condition & kEndConditionBracket) && isOperator(kOperator_bracket_end))
|| ((condition & kEndConditionColon) && isOperator(kOperator_colon))
|| ((condition & kEndConditionComma) && isOperator(kOperator_comma));
}
// Constant expression evaluator
bool parser::isConstant(astExpression *expression) const {
if (isConstantValue(expression))
return true;
else if (expression->type == astExpression::kVariableIdentifier) {
astVariable *reference = ((astVariableIdentifier*)expression)->variable;
if (reference->type != astVariable::kGlobal)
return false;
astExpression *initialValue = ((astGlobalVariable*)reference)->initialValue;
if (!initialValue)
return false;
return isConstant(initialValue);
} else if (expression->type == astExpression::kUnaryMinus)
return isConstant(((astUnaryExpression*)expression)->operand);
else if (expression->type == astExpression::kUnaryPlus)
return isConstant(((astUnaryExpression*)expression)->operand);
else if (expression->type == astExpression::kOperation) {
astOperationExpression *operation = (astOperationExpression*)expression;
return isConstant(operation->operand1) && isConstant(operation->operand2);
}
return false;
}
bool parser::isConstantValue(astExpression *expression) const {
return expression->type == astExpression::kIntConstant ||
expression->type == astExpression::kUIntConstant ||
expression->type == astExpression::kFloatConstant ||
expression->type == astExpression::kDoubleConstant ||
expression->type == astExpression::kBoolConstant;
}
#define ICONST(X) ((astIntConstant*)(X))
#define UCONST(X) ((astUIntConstant*)(X))
#define FCONST(X) ((astFloatConstant*)(X))
#define DCONST(X) ((astDoubleConstant*)(X))
#define BCONST(X) ((astBoolConstant*)(X))
#define ICONST_NEW(X) GC_NEW(astConstantExpression) astIntConstant(X)
#define UCONST_NEW(X) GC_NEW(astConstantExpression) astUIntConstant(X)
#define FCONST_NEW(X) GC_NEW(astConstantExpression) astFloatConstant(X)
#define DCONST_NEW(X) GC_NEW(astConstantExpression) astDoubleConstant(X)
#define BCONST_NEW(X) GC_NEW(astConstantExpression) astBoolConstant(X)
#define IVAL(X) (ICONST(X)->value)
#define UVAL(X) (UCONST(X)->value)
#define FVAL(X) (FCONST(X)->value)
#define DVAL(X) (DCONST(X)->value)
#define BVAL(X) (BCONST(X)->value)
astConstantExpression *parser::evaluate(astExpression *expression) {
if (!expression) return 0;
else if (isConstantValue(expression))
return expression;
else if (expression->type == astExpression::kVariableIdentifier)
return evaluate(((astGlobalVariable*)((astVariableIdentifier*)expression)->variable)->initialValue);
else if (expression->type == astExpression::kUnaryMinus) {
astExpression *operand = evaluate(((astUnaryExpression*)expression)->operand);
if (!operand) return 0;
switch (operand->type) {
case astExpression::kIntConstant: return ICONST_NEW(-IVAL(operand));
case astExpression::kFloatConstant: return FCONST_NEW(-FVAL(operand));
case astExpression::kDoubleConstant: return DCONST_NEW(-DVAL(operand));
default:
fatal("invalid operation in constant expression");
return 0;
}
} else if (expression->type == astExpression::kUnaryPlus) {
astExpression *operand = evaluate(((astUnaryExpression*)expression)->operand);
if (!operand) return 0;
switch (operand->type) {
case astExpression::kIntConstant:
case astExpression::kUIntConstant:
case astExpression::kFloatConstant:
case astExpression::kDoubleConstant:
return operand;
default:
fatal("invalid operation in constant expression");
return 0;
}
} else if (expression->type == astExpression::kOperation) {
int operation = ((astOperationExpression*)expression)->operation;
astExpression *lhs = evaluate(((astBinaryExpression*)expression)->operand1);
astExpression *rhs = evaluate(((astBinaryExpression*)expression)->operand2);
if (!lhs) return 0;
if (!rhs) return 0;
switch (lhs->type) {
case astExpression::kIntConstant:
switch (operation) {
case kOperator_multiply: return ICONST_NEW(IVAL(lhs) * IVAL(rhs));
case kOperator_divide: return ICONST_NEW(IVAL(lhs) / IVAL(rhs));
case kOperator_modulus: return ICONST_NEW(IVAL(lhs) % IVAL(rhs));
case kOperator_plus: return ICONST_NEW(IVAL(lhs) + IVAL(rhs));
case kOperator_minus: return ICONST_NEW(IVAL(lhs) - IVAL(rhs));
case kOperator_shift_left: return ICONST_NEW(IVAL(lhs) << IVAL(rhs));
case kOperator_shift_right: return ICONST_NEW(IVAL(lhs) >> IVAL(rhs));
case kOperator_less: return BCONST_NEW(IVAL(lhs) < IVAL(rhs));
case kOperator_greater: return BCONST_NEW(IVAL(lhs) > IVAL(rhs));
case kOperator_less_equal: return BCONST_NEW(IVAL(lhs) <= IVAL(rhs));
case kOperator_greater_equal: return BCONST_NEW(IVAL(lhs) >= IVAL(rhs));
case kOperator_equal: return BCONST_NEW(IVAL(lhs) == IVAL(rhs));
case kOperator_not_equal: return BCONST_NEW(IVAL(lhs) != IVAL(rhs));
case kOperator_bit_and: return ICONST_NEW(IVAL(lhs) & IVAL(rhs));
case kOperator_bit_xor: return ICONST_NEW(IVAL(lhs) ^ IVAL(rhs));
case kOperator_logical_and: return BCONST_NEW(IVAL(lhs) && IVAL(rhs));
case kOperator_logical_xor: return BCONST_NEW(!IVAL(lhs) != !IVAL(rhs));
case kOperator_logical_or: return BCONST_NEW(IVAL(lhs) || IVAL(rhs));
default:
fatal("invalid operation in constant expression");
return 0;
}
break;
case astExpression::kUIntConstant:
switch (operation) {
case kOperator_multiply: return UCONST_NEW(UVAL(lhs) * UVAL(rhs));
case kOperator_divide: return UCONST_NEW(UVAL(lhs) / UVAL(rhs));
case kOperator_modulus: return UCONST_NEW(UVAL(lhs) % UVAL(rhs));
case kOperator_plus: return UCONST_NEW(UVAL(lhs) + UVAL(rhs));
case kOperator_minus: return UCONST_NEW(UVAL(lhs) - UVAL(rhs));
case kOperator_shift_left: return UCONST_NEW(UVAL(lhs) << UVAL(rhs));
case kOperator_shift_right: return UCONST_NEW(UVAL(lhs) >> UVAL(rhs));
case kOperator_less: return BCONST_NEW(UVAL(lhs) < UVAL(rhs));
case kOperator_greater: return BCONST_NEW(UVAL(lhs) > UVAL(rhs));
case kOperator_less_equal: return BCONST_NEW(UVAL(lhs) <= UVAL(rhs));
case kOperator_greater_equal: return BCONST_NEW(UVAL(lhs) >= UVAL(rhs));
case kOperator_equal: return BCONST_NEW(UVAL(lhs) == UVAL(rhs));
case kOperator_not_equal: return BCONST_NEW(UVAL(lhs) != UVAL(rhs));
case kOperator_bit_and: return UCONST_NEW(UVAL(lhs) & UVAL(rhs));
case kOperator_bit_xor: return UCONST_NEW(UVAL(lhs) ^ UVAL(rhs));
case kOperator_logical_and: return BCONST_NEW(UVAL(lhs) && UVAL(rhs));
case kOperator_logical_xor: return BCONST_NEW(!UVAL(lhs) != !UVAL(rhs));
case kOperator_logical_or: return BCONST_NEW(UVAL(lhs) || UVAL(rhs));
default:
fatal("invalid operation in constant expression");
return 0;
}
break;
case astExpression::kFloatConstant:
switch (operation) {
case kOperator_multiply: return FCONST_NEW(FVAL(lhs) * FVAL(rhs));
case kOperator_divide: return FCONST_NEW(FVAL(lhs) / FVAL(rhs));
case kOperator_plus: return FCONST_NEW(FVAL(lhs) + FVAL(rhs));
case kOperator_minus: return FCONST_NEW(FVAL(lhs) - FVAL(rhs));
case kOperator_less: return BCONST_NEW(FVAL(lhs) < FVAL(rhs));
case kOperator_greater: return BCONST_NEW(FVAL(lhs) > FVAL(rhs));
case kOperator_less_equal: return BCONST_NEW(FVAL(lhs) <= FVAL(rhs));
case kOperator_greater_equal: return BCONST_NEW(FVAL(lhs) >= FVAL(rhs));
case kOperator_equal: return BCONST_NEW(FVAL(lhs) == FVAL(rhs));
case kOperator_not_equal: return BCONST_NEW(FVAL(lhs) != FVAL(rhs));
case kOperator_logical_and: return BCONST_NEW(FVAL(lhs) && FVAL(rhs));
case kOperator_logical_xor: return BCONST_NEW(!FVAL(lhs) != !FVAL(rhs));
case kOperator_logical_or: return BCONST_NEW(FVAL(lhs) || FVAL(rhs));
default:
fatal("invalid operation in constant expression");
return 0;
}
break;
case astExpression::kDoubleConstant:
switch (operation) {
case kOperator_multiply: return DCONST_NEW(DVAL(lhs) * DVAL(rhs));
case kOperator_divide: return DCONST_NEW(DVAL(lhs) / DVAL(rhs));
case kOperator_plus: return DCONST_NEW(DVAL(lhs) + DVAL(rhs));
case kOperator_minus: return DCONST_NEW(DVAL(lhs) - DVAL(rhs));
case kOperator_less: return BCONST_NEW(DVAL(lhs) < DVAL(rhs));
case kOperator_greater: return BCONST_NEW(DVAL(lhs) > DVAL(rhs));
case kOperator_less_equal: return BCONST_NEW(DVAL(lhs) <= DVAL(rhs));
case kOperator_greater_equal: return BCONST_NEW(DVAL(lhs) >= DVAL(rhs));
case kOperator_equal: return BCONST_NEW(DVAL(lhs) == DVAL(rhs));
case kOperator_not_equal: return BCONST_NEW(DVAL(lhs) != DVAL(rhs));
case kOperator_logical_and: return BCONST_NEW(DVAL(lhs) && DVAL(rhs));
case kOperator_logical_xor: return BCONST_NEW(!DVAL(lhs) != !DVAL(rhs));
case kOperator_logical_or: return BCONST_NEW(DVAL(lhs) || DVAL(rhs));
default:
fatal("invalid operation in constant expression");
return 0;
}
break;
case astExpression::kBoolConstant:
switch (operation) {
case kOperator_equal: return BCONST_NEW(BVAL(lhs) == BVAL(rhs));
case kOperator_not_equal: return BCONST_NEW(BVAL(lhs) != BVAL(rhs));
case kOperator_logical_and: return BCONST_NEW(BVAL(lhs) && BVAL(rhs));
case kOperator_logical_xor: return BCONST_NEW(!BVAL(lhs) != !BVAL(rhs));
case kOperator_logical_or: return BCONST_NEW(BVAL(lhs) || BVAL(rhs));
default:
fatal("invalid operation in constant expression");
return 0;
}
break;
}
} else {
return evaluate(expression);
}
return 0;
}
void parser::fatal(const char *fmt, ...) {
// Format banner
char *banner = 0;
int bannerLength = allocfmt(&banner, "%s:%zu:%zu: error: ", m_fileName, m_lexer.line(), m_lexer.column());
if (bannerLength == -1) {
m_error = m_oom;
return;
}
// Format message
char *message = 0;
va_list va;
va_start(va, fmt);
int messageLength = allocvfmt(&message, fmt, va);
if (messageLength == -1) {
va_end(va);
m_error = m_oom;
return;
}
va_end(va);
// Concatenate the two things
char *concat = (char *)malloc(bannerLength + messageLength + 1);
if (!concat) {
free(banner);
free(message);
m_error = m_oom;
return;
}
memcpy(concat, banner, bannerLength);
memcpy(concat + bannerLength, message, messageLength + 1); // +1 for '\0'
free(banner);
free(message);
m_error = concat;
m_strings.push_back(m_error);
}
#undef TYPENAME
#define TYPENAME(X) case kKeyword_##X:
bool parser::isBuiltin() const {
if (!isType(kType_keyword))
return false;
switch (m_token.asKeyword) {
#include "lexemes.h"
return true;
default:
break;
}
return false;
}
#undef TYPENAME
#define TYPENAME(...)
/// The parser entry point
CHECK_RETURN astTU *parser::parse(int type) {
m_ast = new astTU(type);
m_scopes.push_back(scope());
for (;;) {
m_lexer.read(m_token, true);
if (m_lexer.error()) {
fatal("%s", m_lexer.error());
return 0;
}
if (isType(kType_eof)) {
break;
}
if (isType(kType_directive)) {
if (m_token.asDirective.type == directive::kVersion) {
if (m_ast->versionDirective) {
fatal("Multiple version directives not allowed");
return 0;
}
astVersionDirective *directive = GC_NEW(astVersionDirective) astVersionDirective();
directive->version = m_token.asDirective.asVersion.version;
directive->type = m_token.asDirective.asVersion.type;
m_ast->versionDirective = directive;
} else if (m_token.asDirective.type == directive::kExtension) {
astExtensionDirective *extension = GC_NEW(astExtensionDirective) astExtensionDirective();
extension->behavior = m_token.asDirective.asExtension.behavior;
extension->name = strnew(m_token.asDirective.asExtension.name);
m_ast->extensionDirectives.push_back(extension);
}
continue;
}
vector<topLevel> items;
if (!parseTopLevel(items))
return 0;
if (isType(kType_semicolon)) {
for (size_t i = 0; i < items.size(); i++) {
topLevel &parse = items[i];
astGlobalVariable *global = GC_NEW(astVariable) astGlobalVariable();
global->storage = parse.storage;
global->auxiliary = parse.auxiliary;
global->memory = parse.memory;
global->precision = parse.precision;
global->interpolation = parse.interpolation;
global->baseType = parse.type;
global->name = strnew(parse.name);
global->isInvariant = parse.isInvariant;
global->isPrecise = parse.isPrecise;
global->layoutQualifiers = parse.layoutQualifiers;
if (parse.initialValue) {
if (!(global->initialValue = evaluate(parse.initialValue)))
return 0;
}
global->isArray = parse.isArray;
global->arraySizes = parse.arraySizes;
m_ast->globals.push_back(global);
m_scopes.back().push_back(global);
}
} else if (isOperator(kOperator_paranthesis_begin)) {
astFunction *function = parseFunction(items.front());
if (!function)
return 0;
m_ast->functions.push_back(function);
} else if (isType(kType_whitespace)) {
continue; // whitespace tokens will be used later for the preprocessor
} else {
fatal("syntax error at top level %d", m_token.asKeyword);
return 0;
}
}
return m_ast;
}
CHECK_RETURN bool parser::parseStorage(topLevel ¤t) {
// const, in, out, attribute, uniform, varying, buffer, shared
if (isKeyword(kKeyword_const)) {
current.storage = kConst;
if (!next()) return false; // skip 'const'
} else if (isKeyword(kKeyword_in)) {
current.storage = kIn;
if (!next()) return false; // skip 'in'
} else if (isKeyword(kKeyword_out)) {
current.storage = kOut;
if (!next()) return false; // skip 'out'
} else if (isKeyword(kKeyword_attribute)) {
current.storage = kAttribute;
if (!next()) return false; // skip 'attribute'
} else if (isKeyword(kKeyword_uniform)) {
current.storage = kUniform;
if (!next()) return false; // skip 'uniform'
} else if (isKeyword(kKeyword_varying)) {
current.storage = kVarying;
if (!next()) return false; // skip 'varying'
} else if (isKeyword(kKeyword_buffer)) {
current.storage = kBuffer;
if (!next()) return false; // skip 'buffer'
} else if (isKeyword(kKeyword_shared)) {
current.storage = kShared;
if (!next()) return false; // skip 'shared'
}
return true;
}
CHECK_RETURN bool parser::parseAuxiliary(topLevel ¤t) {
// centroid, sample, patch
if (isKeyword(kKeyword_centroid)) {
current.auxiliary = kCentroid;
if (!next()) return false; // skip 'centroid'
} else if (isKeyword(kKeyword_sample)) {
current.auxiliary = kSample;
if (!next()) return false; // skip 'sample'
} else if (isKeyword(kKeyword_patch)) {
current.auxiliary = kPatch;
if (!next()) return false; // skip 'patch'
}
return true;
}
CHECK_RETURN bool parser::parseInterpolation(topLevel ¤t) {
// smooth, flat, noperspective
if (isKeyword(kKeyword_smooth)) {
current.interpolation = kSmooth;
if (!next()) return false; // skip 'smooth'
} else if (isKeyword(kKeyword_flat)) {
current.interpolation = kFlat;
if (!next()) return false; // skip 'flat'
} else if (isKeyword(kKeyword_noperspective)) {
current.interpolation = kNoPerspective;
if (!next()) return false; // skip 'noperspective'
}
return true;
}
CHECK_RETURN bool parser::parsePrecision(topLevel ¤t) {
// highp, mediump, lowp
if (isKeyword(kKeyword_highp)) {
current.precision = kHighp;
if (!next()) return false; // skip 'highp'
} else if (isKeyword(kKeyword_mediump)) {
current.precision = kMediump;
if (!next()) return false; // skip 'mediump'
} else if (isKeyword(kKeyword_lowp)) {
current.precision = kLowp;
if (!next()) return false; // skip 'lowp'
}
return true;
}
CHECK_RETURN bool parser::parseInvariant(topLevel ¤t) {
// invariant
if (isKeyword(kKeyword_invariant)) {
current.isInvariant = true;
if (!next()) return false; // skip 'invariant'
}
return true;
}
CHECK_RETURN bool parser::parsePrecise(topLevel ¤t) {
// precise
if (isKeyword(kKeyword_precise)) {
current.isPrecise = true;
if (!next()) return false; // skip 'precise'
}
return true;
}
CHECK_RETURN bool parser::parseMemory(topLevel ¤t) {
// coherent, volatile, restrict, readonly, writeonly
if (isKeyword(kKeyword_coherent)) {
current.memory |= kCoherent;
if (!next()) return false; // skip 'coherent'
} else if (isKeyword(kKeyword_volatile)) {
current.memory |= kVolatile;
if (!next()) return false; // skip 'volatile'
} else if (isKeyword(kKeyword_restrict)) {
current.memory |= kRestrict;
if (!next()) return false; // skip 'restrict'
} else if (isKeyword(kKeyword_readonly)) {
current.memory |= kReadOnly;
if (!next()) return false; // skip 'readonly'
} else if (isKeyword(kKeyword_writeonly)) {
current.memory |= kWriteOnly;
if (!next()) return false; // skip 'writeonly;
}
return true;
}
static struct {
const char *qualifier;
bool isAssign;
} kLayoutQualifiers[] = {
{ "shared", false },
{ "packed", false },
{ "std140", false },
{ "row_major", false },
{ "column_major", false },
{ "binding", true },
{ "offset", true },
{ "align", true },
{ "location", true },
{ "component", true },
{ "index", true },
{ "triangles", false },
{ "quads", false },
{ "isolines", false },
{ "equal_spacing", false },
{ "fractional_even_spacing", false },
{ "fractional_odd_spacing", false },
{ "cw", false },
{ "ccw", false },
{ "point_mode", false },
{ "points", false },
{ "lines", false },
{ "lines_adjacency", false },
{ "triangles_adjacency", false },
{ "invocations", true },
{ "origin_upper_left", false },
{ "pixel_center_integer", false },
{ "early_fragment_tests", false },
{ "local_size_x", true },
{ "local_size_y", true },
{ "local_size_z", true },
{ "xfb_buffer", true },
{ "xfb_stride", true },
{ "xfb_offset", true },
{ "vertices", true },
{ "line_strip", false },
{ "triangle_strip", false },
{ "max_vertices", true },
{ "stream", true },
{ "depth_any", false },
{ "depth_greater", false },
{ "depth_less", false },
{ "depth_unchanged", false }
};
CHECK_RETURN bool parser::parseLayout(topLevel ¤t) {
vector<astLayoutQualifier*> &qualifiers = current.layoutQualifiers;
if (isKeyword(kKeyword_layout)) {
if (!next()) // skip 'layout'
return false;
if (!isOperator(kOperator_paranthesis_begin)) {
fatal("expected `(' after `layout'");
return false;
}
if (!next()) // skip '('
return false;
while (!isOperator(kOperator_paranthesis_end)) {
astLayoutQualifier *qualifier = GC_NEW(astLayoutQualifier) astLayoutQualifier();
// "The tokens used for layout-qualifier-name are identifiers,
// not keywords, however, the shared keyword is allowed as a
// layout-qualifier-id."
if (!isType(kType_identifier) && !isKeyword(kKeyword_shared))
return false;
int found = -1;
qualifier->name = strnew(isType(kType_identifier) ? m_token.asIdentifier : "shared");
for (size_t i = 0; i < sizeof(kLayoutQualifiers)/sizeof(kLayoutQualifiers[0]); i++) {
if (strcmp(qualifier->name, kLayoutQualifiers[i].qualifier))
continue;
found = int(i);
break;
}
if (found == -1) {
fatal("unknown layout qualifier `%s'", qualifier->name);
return false;
}
if (!next()) // skip identifier or 'shared' keyword
return false;
if (isOperator(kOperator_assign)) {
if (!kLayoutQualifiers[found].isAssign) {
fatal("unexpected layout qualifier value on `%s' layout qualifier", qualifier->name);
return false;
}
if (!next()) // skip '='
return false;
if (!(qualifier->initialValue = parseExpression(kEndConditionComma | kEndConditionParanthesis)))
return false;
if (!isConstant(qualifier->initialValue)) {
// TODO: check integer-constant-expression
fatal("value for layout qualifier `%s' is not a valid constant expression",
qualifier->name);
return false;
}
if (!(qualifier->initialValue = evaluate(qualifier->initialValue)))
return false;
} else if (kLayoutQualifiers[found].isAssign) {
fatal("expected layout qualifier value for `%s' layout qualifier", qualifier->name);
return false;
}
if (isOperator(kOperator_comma)) {
if (!next()) // skip ','
return false;
}
qualifiers.push_back(qualifier);
}
if (!next()) // skip ')'
return false;
}
return true;
}
static bool isInterfaceBlockStorage(int storage) {
return storage == kIn
|| storage == kOut
|| storage == kUniform
|| storage == kBuffer;
}
static bool isReservedKeyword(int keyword) {
return keyword == kKeyword_common
|| keyword == kKeyword_partition
|| keyword == kKeyword_active
|| keyword == kKeyword_asm
|| keyword == kKeyword_class
|| keyword == kKeyword_union
|| keyword == kKeyword_enum
|| keyword == kKeyword_typedef
|| keyword == kKeyword_template
|| keyword == kKeyword_this
|| keyword == kKeyword_resource
|| keyword == kKeyword_goto
|| keyword == kKeyword_inline
|| keyword == kKeyword_noinline
|| keyword == kKeyword_public
|| keyword == kKeyword_static
|| keyword == kKeyword_extern
|| keyword == kKeyword_external
|| keyword == kKeyword_interface
|| keyword == kKeyword_long
|| keyword == kKeyword_short
|| keyword == kKeyword_half
|| keyword == kKeyword_fixed
|| keyword == kKeyword_unsigned
|| keyword == kKeyword_superp
|| keyword == kKeyword_input
|| keyword == kKeyword_output
|| keyword == kKeyword_hvec2
|| keyword == kKeyword_hvec3
|| keyword == kKeyword_hvec4
|| keyword == kKeyword_fvec2
|| keyword == kKeyword_fvec3
|| keyword == kKeyword_fvec4
|| keyword == kKeyword_sampler3DRect
|| keyword == kKeyword_filter
|| keyword == kKeyword_sizeof
|| keyword == kKeyword_cast
|| keyword == kKeyword_namespace
|| keyword == kKeyword_using;
}
CHECK_RETURN bool parser::parseTopLevelItem(topLevel &level, topLevel *continuation) {
vector<topLevel> items;
while (!isBuiltin() && !isType(kType_identifier)) {
// If this is an empty file don't get caught in this loop indefinitely
token peek = m_lexer.peek();
if (IS_TYPE(peek, kType_eof))
return false;
topLevel item;
if (continuation)
item = *continuation;
if (!parseStorage(item)) return false;
if (!parseAuxiliary(item)) return false;
if (!parseInterpolation(item)) return false;
if (!parsePrecision(item)) return false;
if (!parseInvariant(item)) return false;
if (!parsePrecise(item)) return false;
if (!parseMemory(item)) return false;
if (!parseLayout(item)) return false;
if (isType(kType_keyword) && isReservedKeyword(m_token.asKeyword)) {
fatal("cannot use a reserved keyword");
return false;
}
// Check for interface block.
if (isType(kType_identifier) && isInterfaceBlockStorage(item.storage)) {
// if (!next()) return false; // skip identifier
astInterfaceBlock *unique = parseInterfaceBlock(item.storage);
if (!unique)
return false;
m_ast->interfaceBlocks.push_back(unique);
if (isType(kType_semicolon)) {
return true;
} else {
level.type = unique;
}
} else if (isKeyword(kKeyword_struct)) {
if (!next()) return 0; // skip struct
astStruct *unique = parseStruct();
if (!unique)
return false;
m_ast->structures.push_back(unique);
if (isType(kType_semicolon))
{
return true;
} else {
level.type = unique;
}
} else {
items.push_back(item);
}
}
if (continuation) {
level = *continuation;
// erase anything that is not an array size on the type, e.g
// int[2] a[2], b; should produce: int a[2][2]; int b[2];
level.arraySizes.erase(level.arraySizes.begin() + level.arrayOnTypeOffset, level.arraySizes.end());
}
for (size_t i = 0; i < items.size(); i++) {
topLevel &next = items[i];
const int storage = level.storage != -1 ? level.storage : next.storage;
if (m_ast->type == astTU::kVertex && storage == kIn) {
// "It's a compile-time error to use any auxiliary or interpolation
// qualifiers on a vertex shader input"
if (level.auxiliary != -1 || next.auxiliary != -1) {
fatal("cannot use auxiliary storage qualifier on vertex shader input");
return false;
} else if (level.interpolation != -1 || next.interpolation != -1) {
fatal("cannot use interpolation qualifier on vertex shader input");
return false;
}
}
if (m_ast->type == astTU::kFragment && storage == kOut) {
// "It's a compile-time error to use auxiliary storage qualifiers or
// interpolation qualifiers on an output in a fragment shader."
if (level.auxiliary != -1 || next.auxiliary != -1) {
fatal("cannot use auxiliary storage qualifier on fragment shader output");
return false;
} else if (level.interpolation != -1 || next.interpolation != -1) {
fatal("cannot use interpolation qualifier on fragment shader output");
return false;
}
}
if (m_ast->type != astTU::kTessEvaluation && storage == kIn) {
// "Applying the patch qualifier to inputs can only be done in tessellation
// evaluation shaders. It is a compile-time error to use patch with inputs
// in any other stage."
if (level.auxiliary == kPatch || next.auxiliary == kPatch) {
fatal("applying `patch' qualifier to input can only be done in tessellation evaluation shaders");
return false;
}
}
if (m_ast->type != astTU::kTessControl && storage == kOut) {
// "Applying patch to an output can only be done in a tessellation control
// shader. It is a compile-time errot to use patch on outputs in any
// other stage."
if (level.auxiliary == kPatch || next.auxiliary == kPatch) {
fatal("applying `patch' qualifier to output can only be done in tessellation control shaders");
return false;
}
}
if (next.storage != -1 && level.storage != -1) {
fatal("multiple storage qualifiers in declaration");
return false;
} else if (next.auxiliary != -1 && level.auxiliary != -1) {
fatal("multiple auxiliary storage qualifiers in declaration");
return false;
} else if (next.interpolation != -1 && level.interpolation != -1) {
fatal("multiple interpolation qualifiers in declaration");
return false;
} if (next.precision != -1 && level.precision != -1) {
fatal("multiple precision qualifiers in declaration");
return false;
}
level.storage = next.storage;
level.auxiliary = next.auxiliary;
level.interpolation = next.interpolation;
level.precision = next.precision;
level.memory |= next.memory;
for (size_t i = 0; i < next.layoutQualifiers.size(); i++) {
// "When the same layout-qualifier-name occurs multiple times, in a single declaration, the
// last occurrence overrides the former occurrence(s)"
for (size_t j = 0; i < level.layoutQualifiers.size(); j++) {
if (next.layoutQualifiers[i]->name == level.layoutQualifiers[j]->name)
level.layoutQualifiers.erase(level.layoutQualifiers.begin() + j);
}
level.layoutQualifiers.push_back(next.layoutQualifiers[i]);
}
}
// "It's a compile-time error to use interpolation qualifiers with patch"
if (level.auxiliary == kPatch && level.interpolation != -1) {
fatal("cannot use interpolation qualifier with auxiliary storage qualifier `patch'");
return false;
}
if (!continuation && !level.type) {
if (isType(kType_identifier)) {
level.type = findType(m_token.asIdentifier);
if (!next()) // skip identifier
return false;
} else {
level.type = parseBuiltin();
if (!next()) // skip typename
return false;
}
if (level.type) {
// Could be an array
while (isOperator(kOperator_bracket_begin)) {
level.isArray = true;
astConstantExpression *arraySize = parseArraySize();
if (!arraySize)
return false;
level.arraySizes.insert(level.arraySizes.begin(), arraySize);
level.arrayOnTypeOffset++;
if (!next()) // skip ']'
return false;
}
}
}
if (!level.type) {
fatal("expected typename");
return false;
}
if (isType(kType_identifier)) {
level.name = strnew(m_token.asIdentifier);
if (!next())// skip identifier
return false;
}
while (isOperator(kOperator_bracket_begin)) {
level.isArray = true;
level.arraySizes.push_back(parseArraySize());
if (!next()) // skip ']'
return false;
}
if (level.storage == kConst || level.storage == kUniform) {
// Can have a constant expression assignment
if (isOperator(kOperator_assign)) {
if (!next()) // skip '='
return false;
if (!(level.initialValue = parseExpression(kEndConditionSemicolon)))
return false;
if (!isConstant(level.initialValue)) {
fatal("not a valid constant expression");
return false;
}
} else if (level.storage != kUniform) {
fatal("const-qualified variable declared but not initialized");
return false;
}
}
// If it isn't a function or prototype than the use of void is not legal
if (!isOperator(kOperator_paranthesis_begin)) {
if (level.type->builtin && ((astBuiltin*)level.type)->type == kKeyword_void) {
fatal("`void' cannot be used in declaration");
return false;
}
}
// if it doesn't have a name than it's illegal
if (strnil(level.name)) {
fatal("expected name for declaration");
return false;
}
return true;
}
CHECK_RETURN bool parser::parseTopLevel(vector<topLevel> &items) {
topLevel item;
if (!parseTopLevelItem(item))
return false;
if (item.type)
items.push_back(item);
while (items.size() && isOperator(kOperator_comma)) {
if (!next())
return false; // skip ','
topLevel nextItem;
if (!parseTopLevelItem(nextItem, &items.front()))
return false;
if (nextItem.type)
items.push_back(nextItem);
}
return true;
}
template<typename T>
CHECK_RETURN T *parser::parseBlock(const char* type) {
T *unique = GC_NEW(astType) T;
if (isType(kType_identifier)) {
unique->name = strnew(m_token.asIdentifier);
if (!next()) return 0; // skip identifier
}
if (!isType(kType_scope_begin)) {
fatal("expected '{' for %s definition", type);
return 0;
}
if (!next()) return 0; // skip '{'
vector<topLevel> items;
while (!isType(kType_scope_end)) {
if (!parseTopLevel(items))
return 0;
if (!next())
return 0;
}
for (size_t i = 0; i < items.size(); i++) {
topLevel &parse = items[i];
astVariable *field = GC_NEW(astVariable) astVariable(astVariable::kField);
field->baseType = parse.type;
field->name = strnew(parse.name);
field->isPrecise = parse.isPrecise;
field->isArray = parse.isArray;
field->arraySizes = parse.arraySizes;
unique->fields.push_back(field);
}
if (!next()) return 0; // skip '}'
return unique;
}
CHECK_RETURN astStruct *parser::parseStruct() {
return parseBlock<astStruct>("structure");
}
CHECK_RETURN astInterfaceBlock *parser::parseInterfaceBlock(int storage) {
astInterfaceBlock* unique = 0;
switch (storage) {
case kIn:
unique = parseBlock<astInterfaceBlock>("input block");
break;
case kOut:
unique = parseBlock<astInterfaceBlock>("outout block");
break;
case kUniform:
unique = parseBlock<astInterfaceBlock>("uniform block");
break;
case kBuffer:
unique = parseBlock<astInterfaceBlock>("buffer block");
break;
}
if (!unique) {
return 0;
}
// When there's no identifier then implicitly declare these as globals
// in their respective places.
if (!isType(kType_identifier)) {
const size_t n_fields = unique->fields.size();
for (size_t i = 0; i < n_fields; i++) {
// Check if the variable already exists
astVariable *variable = unique->fields[i];
if (findVariable(variable->name)) {
fatal("'%s` is already declared in this scope", variable->name);
return 0;
}
m_scopes.back().push_back(unique->fields[i]);
}
}
unique->storage = storage;
return unique;
return 0;
}
CHECK_RETURN astExpression *parser::parseBinary(int lhsPrecedence, astExpression *lhs, endCondition end) {
// Precedence climbing
while (!isEndCondition(end)) {
int binaryPrecedence = m_token.precedence();
if (binaryPrecedence < lhsPrecedence)
break;
astBinaryExpression *expression = createExpression();
if (!next())
return 0;
astExpression *rhs = parseUnary(end);
if (!rhs)
return 0;