2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
38 // GLSL scanning, leveraging the scanning done by the preprocessor.
42 #include <unordered_map>
43 #include <unordered_set>
45 #include "../Include/Types.h"
46 #include "SymbolTable.h"
47 #include "ParseHelper.h"
48 #include "glslang_tab.cpp.h"
49 #include "ScanContext.h"
52 // preprocessor includes
53 #include "preprocessor/PpContext.h"
54 #include "preprocessor/PpTokens.h"
56 // Required to avoid missing prototype warnings for some compilers
57 int yylex(YYSTYPE*, glslang::TParseContext&);
61 // read past any white space
62 void TInputScanner::consumeWhiteSpace(bool& foundNonSpaceTab)
64 int c = peek(); // don't accidentally consume anything other than whitespace
65 while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
66 if (c == '\r' || c == '\n')
67 foundNonSpaceTab = true;
73 // return true if a comment was actually consumed
74 bool TInputScanner::consumeComment()
79 get(); // consume the '/'
83 // a '//' style comment
84 get(); // consume the second '/'
87 while (c != EndOfInput && c != '\\' && c != '\r' && c != '\n')
90 if (c == EndOfInput || c == '\r' || c == '\n') {
91 while (c == '\r' || c == '\n')
94 // we reached the end of the comment
97 // it's a '\', so we need to keep going, after skipping what's escaped
99 // read the skipped character
102 // if it's a two-character newline, skip both characters
103 if (c == '\r' && peek() == '\n')
109 // put back the last non-comment character
114 } else if (c == '*') {
116 // a '/*' style comment
117 get(); // consume the '*'
120 while (c != EndOfInput && c != '*')
125 break; // end of comment
126 // not end of comment
127 } else // end of input
133 // it's not a comment, put the '/' back
140 // skip whitespace, then skip a comment, rinse, repeat
141 void TInputScanner::consumeWhitespaceComment(bool& foundNonSpaceTab)
144 consumeWhiteSpace(foundNonSpaceTab);
146 // if not starting a comment now, then done
148 if (c != '/' || c == EndOfInput)
151 // skip potential comment
152 foundNonSpaceTab = true;
153 if (! consumeComment())
159 // Returns true if there was non-white space (e.g., a comment, newline) before the #version
160 // or no #version was found; otherwise, returns false. There is no error case, it always
161 // succeeds, but will leave version == 0 if no #version was found.
163 // Sets notFirstToken based on whether tokens (beyond white space and comments)
164 // appeared before the #version.
166 // N.B. does not attempt to leave input in any particular known state. The assumption
167 // is that scanning will start anew, following the rules for the chosen version/profile,
168 // and with a corresponding parsing context.
170 bool TInputScanner::scanVersion(int& version, EProfile& profile, bool& notFirstToken)
172 // This function doesn't have to get all the semantics correct,
173 // just find the #version if there is a correct one present.
174 // The preprocessor will have the responsibility of getting all the semantics right.
176 bool versionNotFirst = false; // means not first WRT comments and white space, nothing more
177 notFirstToken = false; // means not first WRT to real tokens
178 version = 0; // means not found
179 profile = ENoProfile;
181 bool foundNonSpaceTab = false;
182 bool lookingInMiddle = false;
185 if (lookingInMiddle) {
186 notFirstToken = true;
187 // make forward progress by finishing off the current line plus extra new lines
188 if (peek() == '\n' || peek() == '\r') {
189 while (peek() == '\n' || peek() == '\r')
194 } while (c != EndOfInput && c != '\n' && c != '\r');
195 while (peek() == '\n' || peek() == '\r')
197 if (peek() == EndOfInput)
200 lookingInMiddle = true;
202 // Nominal start, skipping the desktop allowed comments and white space, but tracking if
203 // something else was found for ES:
204 consumeWhitespaceComment(foundNonSpaceTab);
205 if (foundNonSpaceTab)
206 versionNotFirst = true;
210 versionNotFirst = true;
217 } while (c == ' ' || c == '\t');
227 versionNotFirst = true;
234 } while (c == ' ' || c == '\t');
237 while (c >= '0' && c <= '9') {
238 version = 10 * version + (c - '0');
242 versionNotFirst = true;
247 while (c == ' ' || c == '\t')
251 const int maxProfileLength = 13; // not including any 0
252 char profileString[maxProfileLength];
254 for (profileLength = 0; profileLength < maxProfileLength; ++profileLength) {
255 if (c == EndOfInput || c == ' ' || c == '\t' || c == '\n' || c == '\r')
257 profileString[profileLength] = (char)c;
260 if (c != EndOfInput && c != ' ' && c != '\t' && c != '\n' && c != '\r') {
261 versionNotFirst = true;
265 if (profileLength == 2 && strncmp(profileString, "es", profileLength) == 0)
266 profile = EEsProfile;
267 else if (profileLength == 4 && strncmp(profileString, "core", profileLength) == 0)
268 profile = ECoreProfile;
269 else if (profileLength == 13 && strncmp(profileString, "compatibility", profileLength) == 0)
270 profile = ECompatibilityProfile;
272 return versionNotFirst;
276 // Fill this in when doing glslang-level scanning, to hand back to the parser.
279 explicit TParserToken(YYSTYPE& b) : sType(b) { }
283 TParserToken(TParserToken&);
284 TParserToken& operator=(TParserToken&);
287 } // end namespace glslang
289 // This is the function the glslang parser (i.e., bison) calls to get its next token
290 int yylex(YYSTYPE* glslangTokenDesc, glslang::TParseContext& parseContext)
292 glslang::TParserToken token(*glslangTokenDesc);
294 return parseContext.getScanContext()->tokenize(parseContext.getPpContext(), token);
301 bool operator()(const char* lhs, const char* rhs) const
303 return strcmp(lhs, rhs) == 0;
309 size_t operator()(const char* str) const
312 unsigned long hash = 5381;
315 while ((c = *str++) != 0)
316 hash = ((hash << 5) + hash) + c;
322 // A single global usable by all threads, by all versions, by all languages.
323 // After a single process-level initialization, this is read only and thread safe
324 std::unordered_map<const char*, int, str_hash, str_eq>* KeywordMap = nullptr;
325 std::unordered_set<const char*, str_hash, str_eq>* ReservedSet = nullptr;
331 void TScanContext::fillInKeywordMap()
333 if (KeywordMap != nullptr) {
334 // this is really an error, as this should called only once per process
335 // but, the only risk is if two threads called simultaneously
338 KeywordMap = new std::unordered_map<const char*, int, str_hash, str_eq>;
340 (*KeywordMap)["const"] = CONST;
341 (*KeywordMap)["uniform"] = UNIFORM;
342 (*KeywordMap)["in"] = IN;
343 (*KeywordMap)["out"] = OUT;
344 (*KeywordMap)["inout"] = INOUT;
345 (*KeywordMap)["struct"] = STRUCT;
346 (*KeywordMap)["break"] = BREAK;
347 (*KeywordMap)["continue"] = CONTINUE;
348 (*KeywordMap)["do"] = DO;
349 (*KeywordMap)["for"] = FOR;
350 (*KeywordMap)["while"] = WHILE;
351 (*KeywordMap)["switch"] = SWITCH;
352 (*KeywordMap)["case"] = CASE;
353 (*KeywordMap)["default"] = DEFAULT;
354 (*KeywordMap)["if"] = IF;
355 (*KeywordMap)["else"] = ELSE;
356 (*KeywordMap)["discard"] = DISCARD;
357 (*KeywordMap)["return"] = RETURN;
358 (*KeywordMap)["void"] = VOID;
359 (*KeywordMap)["bool"] = BOOL;
360 (*KeywordMap)["float"] = FLOAT;
361 (*KeywordMap)["int"] = INT;
362 (*KeywordMap)["bvec2"] = BVEC2;
363 (*KeywordMap)["bvec3"] = BVEC3;
364 (*KeywordMap)["bvec4"] = BVEC4;
365 (*KeywordMap)["vec2"] = VEC2;
366 (*KeywordMap)["vec3"] = VEC3;
367 (*KeywordMap)["vec4"] = VEC4;
368 (*KeywordMap)["ivec2"] = IVEC2;
369 (*KeywordMap)["ivec3"] = IVEC3;
370 (*KeywordMap)["ivec4"] = IVEC4;
371 (*KeywordMap)["mat2"] = MAT2;
372 (*KeywordMap)["mat3"] = MAT3;
373 (*KeywordMap)["mat4"] = MAT4;
374 (*KeywordMap)["true"] = BOOLCONSTANT;
375 (*KeywordMap)["false"] = BOOLCONSTANT;
376 (*KeywordMap)["attribute"] = ATTRIBUTE;
377 (*KeywordMap)["varying"] = VARYING;
378 (*KeywordMap)["buffer"] = BUFFER;
379 (*KeywordMap)["coherent"] = COHERENT;
380 (*KeywordMap)["restrict"] = RESTRICT;
381 (*KeywordMap)["readonly"] = READONLY;
382 (*KeywordMap)["writeonly"] = WRITEONLY;
383 (*KeywordMap)["atomic_uint"] = ATOMIC_UINT;
384 (*KeywordMap)["volatile"] = VOLATILE;
385 (*KeywordMap)["layout"] = LAYOUT;
386 (*KeywordMap)["shared"] = SHARED;
387 (*KeywordMap)["patch"] = PATCH;
388 (*KeywordMap)["sample"] = SAMPLE;
389 (*KeywordMap)["subroutine"] = SUBROUTINE;
390 (*KeywordMap)["highp"] = HIGH_PRECISION;
391 (*KeywordMap)["mediump"] = MEDIUM_PRECISION;
392 (*KeywordMap)["lowp"] = LOW_PRECISION;
393 (*KeywordMap)["precision"] = PRECISION;
394 (*KeywordMap)["mat2x2"] = MAT2X2;
395 (*KeywordMap)["mat2x3"] = MAT2X3;
396 (*KeywordMap)["mat2x4"] = MAT2X4;
397 (*KeywordMap)["mat3x2"] = MAT3X2;
398 (*KeywordMap)["mat3x3"] = MAT3X3;
399 (*KeywordMap)["mat3x4"] = MAT3X4;
400 (*KeywordMap)["mat4x2"] = MAT4X2;
401 (*KeywordMap)["mat4x3"] = MAT4X3;
402 (*KeywordMap)["mat4x4"] = MAT4X4;
403 (*KeywordMap)["dmat2"] = DMAT2;
404 (*KeywordMap)["dmat3"] = DMAT3;
405 (*KeywordMap)["dmat4"] = DMAT4;
406 (*KeywordMap)["dmat2x2"] = DMAT2X2;
407 (*KeywordMap)["dmat2x3"] = DMAT2X3;
408 (*KeywordMap)["dmat2x4"] = DMAT2X4;
409 (*KeywordMap)["dmat3x2"] = DMAT3X2;
410 (*KeywordMap)["dmat3x3"] = DMAT3X3;
411 (*KeywordMap)["dmat3x4"] = DMAT3X4;
412 (*KeywordMap)["dmat4x2"] = DMAT4X2;
413 (*KeywordMap)["dmat4x3"] = DMAT4X3;
414 (*KeywordMap)["dmat4x4"] = DMAT4X4;
415 (*KeywordMap)["image1D"] = IMAGE1D;
416 (*KeywordMap)["iimage1D"] = IIMAGE1D;
417 (*KeywordMap)["uimage1D"] = UIMAGE1D;
418 (*KeywordMap)["image2D"] = IMAGE2D;
419 (*KeywordMap)["iimage2D"] = IIMAGE2D;
420 (*KeywordMap)["uimage2D"] = UIMAGE2D;
421 (*KeywordMap)["image3D"] = IMAGE3D;
422 (*KeywordMap)["iimage3D"] = IIMAGE3D;
423 (*KeywordMap)["uimage3D"] = UIMAGE3D;
424 (*KeywordMap)["image2DRect"] = IMAGE2DRECT;
425 (*KeywordMap)["iimage2DRect"] = IIMAGE2DRECT;
426 (*KeywordMap)["uimage2DRect"] = UIMAGE2DRECT;
427 (*KeywordMap)["imageCube"] = IMAGECUBE;
428 (*KeywordMap)["iimageCube"] = IIMAGECUBE;
429 (*KeywordMap)["uimageCube"] = UIMAGECUBE;
430 (*KeywordMap)["imageBuffer"] = IMAGEBUFFER;
431 (*KeywordMap)["iimageBuffer"] = IIMAGEBUFFER;
432 (*KeywordMap)["uimageBuffer"] = UIMAGEBUFFER;
433 (*KeywordMap)["image1DArray"] = IMAGE1DARRAY;
434 (*KeywordMap)["iimage1DArray"] = IIMAGE1DARRAY;
435 (*KeywordMap)["uimage1DArray"] = UIMAGE1DARRAY;
436 (*KeywordMap)["image2DArray"] = IMAGE2DARRAY;
437 (*KeywordMap)["iimage2DArray"] = IIMAGE2DARRAY;
438 (*KeywordMap)["uimage2DArray"] = UIMAGE2DARRAY;
439 (*KeywordMap)["imageCubeArray"] = IMAGECUBEARRAY;
440 (*KeywordMap)["iimageCubeArray"] = IIMAGECUBEARRAY;
441 (*KeywordMap)["uimageCubeArray"] = UIMAGECUBEARRAY;
442 (*KeywordMap)["image2DMS"] = IMAGE2DMS;
443 (*KeywordMap)["iimage2DMS"] = IIMAGE2DMS;
444 (*KeywordMap)["uimage2DMS"] = UIMAGE2DMS;
445 (*KeywordMap)["image2DMSArray"] = IMAGE2DMSARRAY;
446 (*KeywordMap)["iimage2DMSArray"] = IIMAGE2DMSARRAY;
447 (*KeywordMap)["uimage2DMSArray"] = UIMAGE2DMSARRAY;
448 (*KeywordMap)["double"] = DOUBLE;
449 (*KeywordMap)["dvec2"] = DVEC2;
450 (*KeywordMap)["dvec3"] = DVEC3;
451 (*KeywordMap)["dvec4"] = DVEC4;
452 (*KeywordMap)["uint"] = UINT;
453 (*KeywordMap)["uvec2"] = UVEC2;
454 (*KeywordMap)["uvec3"] = UVEC3;
455 (*KeywordMap)["uvec4"] = UVEC4;
457 (*KeywordMap)["int64_t"] = INT64_T;
458 (*KeywordMap)["uint64_t"] = UINT64_T;
459 (*KeywordMap)["i64vec2"] = I64VEC2;
460 (*KeywordMap)["i64vec3"] = I64VEC3;
461 (*KeywordMap)["i64vec4"] = I64VEC4;
462 (*KeywordMap)["u64vec2"] = U64VEC2;
463 (*KeywordMap)["u64vec3"] = U64VEC3;
464 (*KeywordMap)["u64vec4"] = U64VEC4;
466 #ifdef AMD_EXTENSIONS
467 (*KeywordMap)["float16_t"] = FLOAT16_T;
468 (*KeywordMap)["f16vec2"] = F16VEC2;
469 (*KeywordMap)["f16vec3"] = F16VEC3;
470 (*KeywordMap)["f16vec4"] = F16VEC4;
471 (*KeywordMap)["f16mat2"] = F16MAT2;
472 (*KeywordMap)["f16mat3"] = F16MAT3;
473 (*KeywordMap)["f16mat4"] = F16MAT4;
474 (*KeywordMap)["f16mat2x2"] = F16MAT2X2;
475 (*KeywordMap)["f16mat2x3"] = F16MAT2X3;
476 (*KeywordMap)["f16mat2x4"] = F16MAT2X4;
477 (*KeywordMap)["f16mat3x2"] = F16MAT3X2;
478 (*KeywordMap)["f16mat3x3"] = F16MAT3X3;
479 (*KeywordMap)["f16mat3x4"] = F16MAT3X4;
480 (*KeywordMap)["f16mat4x2"] = F16MAT4X2;
481 (*KeywordMap)["f16mat4x3"] = F16MAT4X3;
482 (*KeywordMap)["f16mat4x4"] = F16MAT4X4;
485 (*KeywordMap)["sampler2D"] = SAMPLER2D;
486 (*KeywordMap)["samplerCube"] = SAMPLERCUBE;
487 (*KeywordMap)["samplerCubeArray"] = SAMPLERCUBEARRAY;
488 (*KeywordMap)["samplerCubeArrayShadow"] = SAMPLERCUBEARRAYSHADOW;
489 (*KeywordMap)["isamplerCubeArray"] = ISAMPLERCUBEARRAY;
490 (*KeywordMap)["usamplerCubeArray"] = USAMPLERCUBEARRAY;
491 (*KeywordMap)["sampler1DArrayShadow"] = SAMPLER1DARRAYSHADOW;
492 (*KeywordMap)["isampler1DArray"] = ISAMPLER1DARRAY;
493 (*KeywordMap)["usampler1D"] = USAMPLER1D;
494 (*KeywordMap)["isampler1D"] = ISAMPLER1D;
495 (*KeywordMap)["usampler1DArray"] = USAMPLER1DARRAY;
496 (*KeywordMap)["samplerBuffer"] = SAMPLERBUFFER;
497 (*KeywordMap)["samplerCubeShadow"] = SAMPLERCUBESHADOW;
498 (*KeywordMap)["sampler2DArray"] = SAMPLER2DARRAY;
499 (*KeywordMap)["sampler2DArrayShadow"] = SAMPLER2DARRAYSHADOW;
500 (*KeywordMap)["isampler2D"] = ISAMPLER2D;
501 (*KeywordMap)["isampler3D"] = ISAMPLER3D;
502 (*KeywordMap)["isamplerCube"] = ISAMPLERCUBE;
503 (*KeywordMap)["isampler2DArray"] = ISAMPLER2DARRAY;
504 (*KeywordMap)["usampler2D"] = USAMPLER2D;
505 (*KeywordMap)["usampler3D"] = USAMPLER3D;
506 (*KeywordMap)["usamplerCube"] = USAMPLERCUBE;
507 (*KeywordMap)["usampler2DArray"] = USAMPLER2DARRAY;
508 (*KeywordMap)["isampler2DRect"] = ISAMPLER2DRECT;
509 (*KeywordMap)["usampler2DRect"] = USAMPLER2DRECT;
510 (*KeywordMap)["isamplerBuffer"] = ISAMPLERBUFFER;
511 (*KeywordMap)["usamplerBuffer"] = USAMPLERBUFFER;
512 (*KeywordMap)["sampler2DMS"] = SAMPLER2DMS;
513 (*KeywordMap)["isampler2DMS"] = ISAMPLER2DMS;
514 (*KeywordMap)["usampler2DMS"] = USAMPLER2DMS;
515 (*KeywordMap)["sampler2DMSArray"] = SAMPLER2DMSARRAY;
516 (*KeywordMap)["isampler2DMSArray"] = ISAMPLER2DMSARRAY;
517 (*KeywordMap)["usampler2DMSArray"] = USAMPLER2DMSARRAY;
518 (*KeywordMap)["sampler1D"] = SAMPLER1D;
519 (*KeywordMap)["sampler1DShadow"] = SAMPLER1DSHADOW;
520 (*KeywordMap)["sampler3D"] = SAMPLER3D;
521 (*KeywordMap)["sampler2DShadow"] = SAMPLER2DSHADOW;
522 (*KeywordMap)["sampler2DRect"] = SAMPLER2DRECT;
523 (*KeywordMap)["sampler2DRectShadow"] = SAMPLER2DRECTSHADOW;
524 (*KeywordMap)["sampler1DArray"] = SAMPLER1DARRAY;
526 (*KeywordMap)["samplerExternalOES"] = SAMPLEREXTERNALOES; // GL_OES_EGL_image_external
528 (*KeywordMap)["sampler"] = SAMPLER;
529 (*KeywordMap)["samplerShadow"] = SAMPLERSHADOW;
531 (*KeywordMap)["texture2D"] = TEXTURE2D;
532 (*KeywordMap)["textureCube"] = TEXTURECUBE;
533 (*KeywordMap)["textureCubeArray"] = TEXTURECUBEARRAY;
534 (*KeywordMap)["itextureCubeArray"] = ITEXTURECUBEARRAY;
535 (*KeywordMap)["utextureCubeArray"] = UTEXTURECUBEARRAY;
536 (*KeywordMap)["itexture1DArray"] = ITEXTURE1DARRAY;
537 (*KeywordMap)["utexture1D"] = UTEXTURE1D;
538 (*KeywordMap)["itexture1D"] = ITEXTURE1D;
539 (*KeywordMap)["utexture1DArray"] = UTEXTURE1DARRAY;
540 (*KeywordMap)["textureBuffer"] = TEXTUREBUFFER;
541 (*KeywordMap)["texture2DArray"] = TEXTURE2DARRAY;
542 (*KeywordMap)["itexture2D"] = ITEXTURE2D;
543 (*KeywordMap)["itexture3D"] = ITEXTURE3D;
544 (*KeywordMap)["itextureCube"] = ITEXTURECUBE;
545 (*KeywordMap)["itexture2DArray"] = ITEXTURE2DARRAY;
546 (*KeywordMap)["utexture2D"] = UTEXTURE2D;
547 (*KeywordMap)["utexture3D"] = UTEXTURE3D;
548 (*KeywordMap)["utextureCube"] = UTEXTURECUBE;
549 (*KeywordMap)["utexture2DArray"] = UTEXTURE2DARRAY;
550 (*KeywordMap)["itexture2DRect"] = ITEXTURE2DRECT;
551 (*KeywordMap)["utexture2DRect"] = UTEXTURE2DRECT;
552 (*KeywordMap)["itextureBuffer"] = ITEXTUREBUFFER;
553 (*KeywordMap)["utextureBuffer"] = UTEXTUREBUFFER;
554 (*KeywordMap)["texture2DMS"] = TEXTURE2DMS;
555 (*KeywordMap)["itexture2DMS"] = ITEXTURE2DMS;
556 (*KeywordMap)["utexture2DMS"] = UTEXTURE2DMS;
557 (*KeywordMap)["texture2DMSArray"] = TEXTURE2DMSARRAY;
558 (*KeywordMap)["itexture2DMSArray"] = ITEXTURE2DMSARRAY;
559 (*KeywordMap)["utexture2DMSArray"] = UTEXTURE2DMSARRAY;
560 (*KeywordMap)["texture1D"] = TEXTURE1D;
561 (*KeywordMap)["texture3D"] = TEXTURE3D;
562 (*KeywordMap)["texture2DRect"] = TEXTURE2DRECT;
563 (*KeywordMap)["texture1DArray"] = TEXTURE1DARRAY;
565 (*KeywordMap)["subpassInput"] = SUBPASSINPUT;
566 (*KeywordMap)["subpassInputMS"] = SUBPASSINPUTMS;
567 (*KeywordMap)["isubpassInput"] = ISUBPASSINPUT;
568 (*KeywordMap)["isubpassInputMS"] = ISUBPASSINPUTMS;
569 (*KeywordMap)["usubpassInput"] = USUBPASSINPUT;
570 (*KeywordMap)["usubpassInputMS"] = USUBPASSINPUTMS;
572 (*KeywordMap)["noperspective"] = NOPERSPECTIVE;
573 (*KeywordMap)["smooth"] = SMOOTH;
574 (*KeywordMap)["flat"] = FLAT;
575 #ifdef AMD_EXTENSIONS
576 (*KeywordMap)["__explicitInterpAMD"] = __EXPLICITINTERPAMD;
578 (*KeywordMap)["centroid"] = CENTROID;
579 (*KeywordMap)["precise"] = PRECISE;
580 (*KeywordMap)["invariant"] = INVARIANT;
581 (*KeywordMap)["packed"] = PACKED;
582 (*KeywordMap)["resource"] = RESOURCE;
583 (*KeywordMap)["superp"] = SUPERP;
585 ReservedSet = new std::unordered_set<const char*, str_hash, str_eq>;
587 ReservedSet->insert("common");
588 ReservedSet->insert("partition");
589 ReservedSet->insert("active");
590 ReservedSet->insert("asm");
591 ReservedSet->insert("class");
592 ReservedSet->insert("union");
593 ReservedSet->insert("enum");
594 ReservedSet->insert("typedef");
595 ReservedSet->insert("template");
596 ReservedSet->insert("this");
597 ReservedSet->insert("goto");
598 ReservedSet->insert("inline");
599 ReservedSet->insert("noinline");
600 ReservedSet->insert("public");
601 ReservedSet->insert("static");
602 ReservedSet->insert("extern");
603 ReservedSet->insert("external");
604 ReservedSet->insert("interface");
605 ReservedSet->insert("long");
606 ReservedSet->insert("short");
607 ReservedSet->insert("half");
608 ReservedSet->insert("fixed");
609 ReservedSet->insert("unsigned");
610 ReservedSet->insert("input");
611 ReservedSet->insert("output");
612 ReservedSet->insert("hvec2");
613 ReservedSet->insert("hvec3");
614 ReservedSet->insert("hvec4");
615 ReservedSet->insert("fvec2");
616 ReservedSet->insert("fvec3");
617 ReservedSet->insert("fvec4");
618 ReservedSet->insert("sampler3DRect");
619 ReservedSet->insert("filter");
620 ReservedSet->insert("sizeof");
621 ReservedSet->insert("cast");
622 ReservedSet->insert("namespace");
623 ReservedSet->insert("using");
626 void TScanContext::deleteKeywordMap()
629 KeywordMap = nullptr;
631 ReservedSet = nullptr;
634 // Called by yylex to get the next token.
635 // Returning 0 implies end of input.
636 int TScanContext::tokenize(TPpContext* pp, TParserToken& token)
639 parserToken = &token;
641 int token = pp->tokenize(ppToken);
642 if (token == EndOfInput)
645 tokenText = ppToken.name;
647 parserToken->sType.lex.loc = loc;
649 case ';': afterType = false; return SEMICOLON;
650 case ',': afterType = false; return COMMA;
651 case ':': return COLON;
652 case '=': afterType = false; return EQUAL;
653 case '(': afterType = false; return LEFT_PAREN;
654 case ')': afterType = false; return RIGHT_PAREN;
655 case '.': field = true; return DOT;
656 case '!': return BANG;
657 case '-': return DASH;
658 case '~': return TILDE;
659 case '+': return PLUS;
660 case '*': return STAR;
661 case '/': return SLASH;
662 case '%': return PERCENT;
663 case '<': return LEFT_ANGLE;
664 case '>': return RIGHT_ANGLE;
665 case '|': return VERTICAL_BAR;
666 case '^': return CARET;
667 case '&': return AMPERSAND;
668 case '?': return QUESTION;
669 case '[': return LEFT_BRACKET;
670 case ']': return RIGHT_BRACKET;
671 case '{': return LEFT_BRACE;
672 case '}': return RIGHT_BRACE;
674 parseContext.error(loc, "illegal use of escape character", "\\", "");
677 case PPAtomAddAssign: return ADD_ASSIGN;
678 case PPAtomSubAssign: return SUB_ASSIGN;
679 case PPAtomMulAssign: return MUL_ASSIGN;
680 case PPAtomDivAssign: return DIV_ASSIGN;
681 case PPAtomModAssign: return MOD_ASSIGN;
683 case PpAtomRight: return RIGHT_OP;
684 case PpAtomLeft: return LEFT_OP;
686 case PpAtomRightAssign: return RIGHT_ASSIGN;
687 case PpAtomLeftAssign: return LEFT_ASSIGN;
688 case PpAtomAndAssign: return AND_ASSIGN;
689 case PpAtomOrAssign: return OR_ASSIGN;
690 case PpAtomXorAssign: return XOR_ASSIGN;
692 case PpAtomAnd: return AND_OP;
693 case PpAtomOr: return OR_OP;
694 case PpAtomXor: return XOR_OP;
696 case PpAtomEQ: return EQ_OP;
697 case PpAtomGE: return GE_OP;
698 case PpAtomNE: return NE_OP;
699 case PpAtomLE: return LE_OP;
701 case PpAtomDecrement: return DEC_OP;
702 case PpAtomIncrement: return INC_OP;
704 case PpAtomColonColon:
705 parseContext.error(loc, "not supported", "::", "");
708 case PpAtomConstInt: parserToken->sType.lex.i = ppToken.ival; return INTCONSTANT;
709 case PpAtomConstUint: parserToken->sType.lex.i = ppToken.ival; return UINTCONSTANT;
710 case PpAtomConstInt64: parserToken->sType.lex.i64 = ppToken.i64val; return INT64CONSTANT;
711 case PpAtomConstUint64: parserToken->sType.lex.i64 = ppToken.i64val; return UINT64CONSTANT;
712 case PpAtomConstFloat: parserToken->sType.lex.d = ppToken.dval; return FLOATCONSTANT;
713 case PpAtomConstDouble: parserToken->sType.lex.d = ppToken.dval; return DOUBLECONSTANT;
714 #ifdef AMD_EXTENSIONS
715 case PpAtomConstFloat16: parserToken->sType.lex.d = ppToken.dval; return FLOAT16CONSTANT;
717 case PpAtomIdentifier:
719 int token = tokenizeIdentifier();
724 case EndOfInput: return 0;
728 buf[0] = (char)token;
730 parseContext.error(loc, "unexpected token", buf, "");
736 int TScanContext::tokenizeIdentifier()
738 if (ReservedSet->find(tokenText) != ReservedSet->end())
739 return reservedWord();
741 auto it = KeywordMap->find(tokenText);
742 if (it == KeywordMap->end()) {
743 // Should have an identifier of some sort
744 return identifierOrType();
746 keyword = it->second;
769 if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
770 (parseContext.profile != EEsProfile && parseContext.version < 130))
796 if (strcmp("true", tokenText) == 0)
797 parserToken->sType.lex.b = true;
799 parserToken->sType.lex.b = false;
804 if (parseContext.profile == EEsProfile && parseContext.version >= 300)
809 if ((parseContext.profile == EEsProfile && parseContext.version < 310) ||
810 (parseContext.profile != EEsProfile && parseContext.version < 430))
811 return identifierOrType();
815 if ((parseContext.profile == EEsProfile && parseContext.version >= 310) ||
816 parseContext.extensionTurnedOn(E_GL_ARB_shader_atomic_counters))
818 return es30ReservedFromGLSL(420);
824 if (parseContext.profile == EEsProfile && parseContext.version >= 310)
826 return es30ReservedFromGLSL(parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store) ? 130 : 420);
829 if (parseContext.profile == EEsProfile && parseContext.version >= 310)
831 if (! parseContext.symbolTable.atBuiltInLevel() && (parseContext.profile == EEsProfile || (parseContext.version < 420 && ! parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
837 const int numLayoutExts = 2;
838 const char* layoutExts[numLayoutExts] = { E_GL_ARB_shading_language_420pack,
839 E_GL_ARB_explicit_attrib_location };
840 if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
841 (parseContext.profile != EEsProfile && parseContext.version < 140 &&
842 ! parseContext.extensionsTurnedOn(numLayoutExts, layoutExts)))
843 return identifierOrType();
847 if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
848 (parseContext.profile != EEsProfile && parseContext.version < 140))
849 return identifierOrType();
853 if (parseContext.symbolTable.atBuiltInLevel() ||
854 (parseContext.profile == EEsProfile && parseContext.extensionsTurnedOn(Num_AEP_tessellation_shader, AEP_tessellation_shader)) ||
855 (parseContext.profile != EEsProfile && parseContext.extensionTurnedOn(E_GL_ARB_tessellation_shader)))
858 return es30ReservedFromGLSL(400);
861 if (parseContext.extensionsTurnedOn(1, &E_GL_OES_shader_multisample_interpolation))
863 return es30ReservedFromGLSL(400);
866 return es30ReservedFromGLSL(400);
869 case MEDIUM_PRECISION:
872 return precisionKeyword();
909 return firstGenerationImage(false);
915 if (parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
917 return firstGenerationImage(false);
932 return firstGenerationImage(true);
935 case IIMAGECUBEARRAY:
936 case UIMAGECUBEARRAY:
938 if (parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
940 return secondGenerationImage();
946 case IIMAGE2DMSARRAY:
947 case UIMAGE2DMSARRAY:
949 return secondGenerationImage();
956 if (parseContext.profile == EEsProfile || parseContext.version < 400)
969 if (parseContext.symbolTable.atBuiltInLevel() ||
970 (parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_int64) &&
971 parseContext.profile != EEsProfile && parseContext.version >= 450))
973 return identifierOrType();
975 #ifdef AMD_EXTENSIONS
993 if (parseContext.symbolTable.atBuiltInLevel() ||
994 (parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float) &&
995 parseContext.profile != EEsProfile && parseContext.version >= 450))
997 return identifierOrType();
1000 case SAMPLERCUBEARRAY:
1001 case SAMPLERCUBEARRAYSHADOW:
1002 case ISAMPLERCUBEARRAY:
1003 case USAMPLERCUBEARRAY:
1005 if (parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1007 if (parseContext.profile == EEsProfile || (parseContext.version < 400 && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_cube_map_array)))
1012 case ISAMPLER1DARRAY:
1013 case SAMPLER1DARRAYSHADOW:
1015 case USAMPLER1DARRAY:
1017 return es30ReservedFromGLSL(130);
1023 case SAMPLERCUBESHADOW:
1024 case SAMPLER2DARRAY:
1025 case SAMPLER2DARRAYSHADOW:
1029 case ISAMPLER2DARRAY:
1033 case USAMPLER2DARRAY:
1035 return nonreservedKeyword(300, 130);
1037 case ISAMPLER2DRECT:
1038 case USAMPLER2DRECT:
1040 return es30ReservedFromGLSL(140);
1044 if (parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1046 return es30ReservedFromGLSL(130);
1048 case ISAMPLERBUFFER:
1049 case USAMPLERBUFFER:
1051 if (parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1053 return es30ReservedFromGLSL(140);
1059 if (parseContext.profile == EEsProfile && parseContext.version >= 310)
1061 return es30ReservedFromGLSL(150);
1063 case SAMPLER2DMSARRAY:
1064 case ISAMPLER2DMSARRAY:
1065 case USAMPLER2DMSARRAY:
1067 if (parseContext.extensionsTurnedOn(1, &E_GL_OES_texture_storage_multisample_2d_array))
1069 return es30ReservedFromGLSL(150);
1072 case SAMPLER1DSHADOW:
1074 if (parseContext.profile == EEsProfile)
1080 if (parseContext.profile == EEsProfile && parseContext.version < 300) {
1081 if (! parseContext.extensionTurnedOn(E_GL_OES_texture_3D))
1086 case SAMPLER2DSHADOW:
1088 if (parseContext.profile == EEsProfile && parseContext.version < 300)
1093 case SAMPLER2DRECTSHADOW:
1095 if (parseContext.profile == EEsProfile)
1097 else if (parseContext.version < 140 && ! parseContext.symbolTable.atBuiltInLevel() && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_rectangle)) {
1098 if (parseContext.relaxedErrors())
1099 parseContext.requireExtensions(loc, 1, &E_GL_ARB_texture_rectangle, "texture-rectangle sampler keyword");
1105 case SAMPLER1DARRAY:
1107 if (parseContext.profile == EEsProfile && parseContext.version == 300)
1109 else if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
1110 (parseContext.profile != EEsProfile && parseContext.version < 130))
1111 return identifierOrType();
1114 case SAMPLEREXTERNALOES:
1116 if (parseContext.symbolTable.atBuiltInLevel() || parseContext.extensionTurnedOn(E_GL_OES_EGL_image_external))
1118 return identifierOrType();
1122 case TEXTURECUBEARRAY:
1123 case ITEXTURECUBEARRAY:
1124 case UTEXTURECUBEARRAY:
1125 case ITEXTURE1DARRAY:
1128 case UTEXTURE1DARRAY:
1130 case TEXTURE2DARRAY:
1134 case ITEXTURE2DARRAY:
1138 case UTEXTURE2DARRAY:
1139 case ITEXTURE2DRECT:
1140 case UTEXTURE2DRECT:
1141 case ITEXTUREBUFFER:
1142 case UTEXTUREBUFFER:
1146 case TEXTURE2DMSARRAY:
1147 case ITEXTURE2DMSARRAY:
1148 case UTEXTURE2DMSARRAY:
1152 case TEXTURE1DARRAY:
1155 if (parseContext.spvVersion.vulkan >= 100)
1158 return identifierOrType();
1161 case SUBPASSINPUTMS:
1163 case ISUBPASSINPUTMS:
1165 case USUBPASSINPUTMS:
1166 if (parseContext.spvVersion.vulkan >= 100)
1169 return identifierOrType();
1172 return es30ReservedFromGLSL(130);
1175 if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
1176 (parseContext.profile != EEsProfile && parseContext.version < 130))
1177 return identifierOrType();
1180 #ifdef AMD_EXTENSIONS
1181 case __EXPLICITINTERPAMD:
1182 if (parseContext.profile != EEsProfile && parseContext.version >= 450 &&
1183 parseContext.extensionTurnedOn(E_GL_AMD_shader_explicit_vertex_parameter))
1185 return identifierOrType();
1189 if (parseContext.profile == EEsProfile && parseContext.version < 300)
1191 else if (parseContext.profile != EEsProfile && parseContext.version < 130)
1192 return identifierOrType();
1196 if (parseContext.version < 120)
1197 return identifierOrType();
1201 if ((parseContext.profile == EEsProfile && parseContext.extensionsTurnedOn(Num_AEP_gpu_shader5, AEP_gpu_shader5)) ||
1202 (parseContext.profile != EEsProfile && parseContext.version >= 400))
1204 if (parseContext.profile == EEsProfile && parseContext.version == 310) {
1208 return identifierOrType();
1211 if (parseContext.profile != EEsProfile && parseContext.version < 120)
1212 return identifierOrType();
1216 if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
1217 (parseContext.profile != EEsProfile && parseContext.version < 330))
1218 return reservedWord();
1219 return identifierOrType();
1223 bool reserved = (parseContext.profile == EEsProfile && parseContext.version >= 300) ||
1224 (parseContext.profile != EEsProfile && parseContext.version >= 420);
1225 return identifierOrReserved(reserved);
1229 bool reserved = parseContext.profile == EEsProfile || parseContext.version >= 130;
1230 return identifierOrReserved(reserved);
1234 parseContext.infoSink.info.message(EPrefixInternalError, "Unknown glslang keyword", loc);
1239 int TScanContext::identifierOrType()
1241 parserToken->sType.lex.string = NewPoolTString(tokenText);
1245 parserToken->sType.lex.symbol = parseContext.symbolTable.find(*parserToken->sType.lex.string);
1246 if (afterType == false && parserToken->sType.lex.symbol) {
1247 if (const TVariable* variable = parserToken->sType.lex.symbol->getAsVariable()) {
1248 if (variable->isUserType()) {
1259 // Give an error for use of a reserved symbol.
1260 // However, allow built-in declarations to use reserved words, to allow
1261 // extension support before the extension is enabled.
1262 int TScanContext::reservedWord()
1264 if (! parseContext.symbolTable.atBuiltInLevel())
1265 parseContext.error(loc, "Reserved word.", tokenText, "", "");
1270 int TScanContext::identifierOrReserved(bool reserved)
1278 if (parseContext.forwardCompatible)
1279 parseContext.warn(loc, "using future reserved keyword", tokenText, "");
1281 return identifierOrType();
1284 // For keywords that suddenly showed up on non-ES (not previously reserved)
1285 // but then got reserved by ES 3.0.
1286 int TScanContext::es30ReservedFromGLSL(int version)
1288 if (parseContext.symbolTable.atBuiltInLevel())
1291 if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
1292 (parseContext.profile != EEsProfile && parseContext.version < version)) {
1293 if (parseContext.forwardCompatible)
1294 parseContext.warn(loc, "future reserved word in ES 300 and keyword in GLSL", tokenText, "");
1296 return identifierOrType();
1297 } else if (parseContext.profile == EEsProfile && parseContext.version >= 300)
1303 // For a keyword that was never reserved, until it suddenly
1304 // showed up, both in an es version and a non-ES version.
1305 int TScanContext::nonreservedKeyword(int esVersion, int nonEsVersion)
1307 if ((parseContext.profile == EEsProfile && parseContext.version < esVersion) ||
1308 (parseContext.profile != EEsProfile && parseContext.version < nonEsVersion)) {
1309 if (parseContext.forwardCompatible)
1310 parseContext.warn(loc, "using future keyword", tokenText, "");
1312 return identifierOrType();
1318 int TScanContext::precisionKeyword()
1320 if (parseContext.profile == EEsProfile || parseContext.version >= 130)
1323 if (parseContext.forwardCompatible)
1324 parseContext.warn(loc, "using ES precision qualifier keyword", tokenText, "");
1326 return identifierOrType();
1329 int TScanContext::matNxM()
1333 if (parseContext.version > 110)
1336 if (parseContext.forwardCompatible)
1337 parseContext.warn(loc, "using future non-square matrix type keyword", tokenText, "");
1339 return identifierOrType();
1342 int TScanContext::dMat()
1346 if (parseContext.profile == EEsProfile && parseContext.version >= 300) {
1352 if (parseContext.profile != EEsProfile && parseContext.version >= 400)
1355 if (parseContext.forwardCompatible)
1356 parseContext.warn(loc, "using future type keyword", tokenText, "");
1358 return identifierOrType();
1361 int TScanContext::firstGenerationImage(bool inEs310)
1363 if (parseContext.symbolTable.atBuiltInLevel() ||
1364 (parseContext.profile != EEsProfile && (parseContext.version >= 420 || parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))) ||
1365 (inEs310 && parseContext.profile == EEsProfile && parseContext.version >= 310))
1368 if ((parseContext.profile == EEsProfile && parseContext.version >= 300) ||
1369 (parseContext.profile != EEsProfile && parseContext.version >= 130)) {
1375 if (parseContext.forwardCompatible)
1376 parseContext.warn(loc, "using future type keyword", tokenText, "");
1378 return identifierOrType();
1381 int TScanContext::secondGenerationImage()
1383 if (parseContext.profile == EEsProfile && parseContext.version >= 310) {
1388 if (parseContext.symbolTable.atBuiltInLevel() ||
1389 (parseContext.profile != EEsProfile &&
1390 (parseContext.version >= 420 || parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
1393 if (parseContext.forwardCompatible)
1394 parseContext.warn(loc, "using future type keyword", tokenText, "");
1396 return identifierOrType();
1399 } // end namespace glslang