1 //
2 // Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
4 // Copyright (C) 2017 ARM Limited.
5 // Copyright (C) 2020 Google, Inc.
6 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
12 // are met:
13 //
14 //    Redistributions of source code must retain the above copyright
15 //    notice, this list of conditions and the following disclaimer.
16 //
17 //    Redistributions in binary form must reproduce the above
18 //    copyright notice, this list of conditions and the following
19 //    disclaimer in the documentation and/or other materials provided
20 //    with the distribution.
21 //
22 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
23 //    contributors may be used to endorse or promote products derived
24 //    from this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 // POSSIBILITY OF SUCH DAMAGE.
38 //
39 
40 //
41 // GLSL scanning, leveraging the scanning done by the preprocessor.
42 //
43 
44 #include <cstring>
45 #include <unordered_map>
46 #include <unordered_set>
47 
48 #include "../Include/Types.h"
49 #include "SymbolTable.h"
50 #include "ParseHelper.h"
51 #include "attribute.h"
52 #include "glslang_tab.cpp.h"
53 #include "ScanContext.h"
54 #include "Scan.h"
55 
56 // preprocessor includes
57 #include "preprocessor/PpContext.h"
58 #include "preprocessor/PpTokens.h"
59 
60 // Required to avoid missing prototype warnings for some compilers
61 int yylex(YYSTYPE*, glslang::TParseContext&);
62 
63 namespace glslang {
64 
65 // read past any white space
consumeWhiteSpace(bool & foundNonSpaceTab)66 void TInputScanner::consumeWhiteSpace(bool& foundNonSpaceTab)
67 {
68     int c = peek();  // don't accidentally consume anything other than whitespace
69     while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
70         if (c == '\r' || c == '\n')
71             foundNonSpaceTab = true;
72         get();
73         c = peek();
74     }
75 }
76 
77 // return true if a comment was actually consumed
consumeComment()78 bool TInputScanner::consumeComment()
79 {
80     if (peek() != '/')
81         return false;
82 
83     get();  // consume the '/'
84     int c = peek();
85     if (c == '/') {
86 
87         // a '//' style comment
88         get();  // consume the second '/'
89         c = get();
90         do {
91             while (c != EndOfInput && c != '\\' && c != '\r' && c != '\n')
92                 c = get();
93 
94             if (c == EndOfInput || c == '\r' || c == '\n') {
95                 while (c == '\r' || c == '\n')
96                     c = get();
97 
98                 // we reached the end of the comment
99                 break;
100             } else {
101                 // it's a '\', so we need to keep going, after skipping what's escaped
102 
103                 // read the skipped character
104                 c = get();
105 
106                 // if it's a two-character newline, skip both characters
107                 if (c == '\r' && peek() == '\n')
108                     get();
109                 c = get();
110             }
111         } while (true);
112 
113         // put back the last non-comment character
114         if (c != EndOfInput)
115             unget();
116 
117         return true;
118     } else if (c == '*') {
119 
120         // a '/*' style comment
121         get();  // consume the '*'
122         c = get();
123         do {
124             while (c != EndOfInput && c != '*')
125                 c = get();
126             if (c == '*') {
127                 c = get();
128                 if (c == '/')
129                     break;  // end of comment
130                 // not end of comment
131             } else // end of input
132                 break;
133         } while (true);
134 
135         return true;
136     } else {
137         // it's not a comment, put the '/' back
138         unget();
139 
140         return false;
141     }
142 }
143 
144 // skip whitespace, then skip a comment, rinse, repeat
consumeWhitespaceComment(bool & foundNonSpaceTab)145 void TInputScanner::consumeWhitespaceComment(bool& foundNonSpaceTab)
146 {
147     do {
148         consumeWhiteSpace(foundNonSpaceTab);
149 
150         // if not starting a comment now, then done
151         int c = peek();
152         if (c != '/' || c == EndOfInput)
153             return;
154 
155         // skip potential comment
156         foundNonSpaceTab = true;
157         if (! consumeComment())
158             return;
159 
160     } while (true);
161 }
162 
163 // Returns true if there was non-white space (e.g., a comment, newline) before the #version
164 // or no #version was found; otherwise, returns false.  There is no error case, it always
165 // succeeds, but will leave version == 0 if no #version was found.
166 //
167 // Sets notFirstToken based on whether tokens (beyond white space and comments)
168 // appeared before the #version.
169 //
170 // N.B. does not attempt to leave input in any particular known state.  The assumption
171 // is that scanning will start anew, following the rules for the chosen version/profile,
172 // and with a corresponding parsing context.
173 //
scanVersion(int & version,EProfile & profile,bool & notFirstToken)174 bool TInputScanner::scanVersion(int& version, EProfile& profile, bool& notFirstToken)
175 {
176     // This function doesn't have to get all the semantics correct,
177     // just find the #version if there is a correct one present.
178     // The preprocessor will have the responsibility of getting all the semantics right.
179 
180     bool versionNotFirst = false;  // means not first WRT comments and white space, nothing more
181     notFirstToken = false;         // means not first WRT to real tokens
182     version = 0;                   // means not found
183     profile = ENoProfile;
184 
185     bool foundNonSpaceTab = false;
186     bool lookingInMiddle = false;
187     int c;
188     do {
189         if (lookingInMiddle) {
190             notFirstToken = true;
191             // make forward progress by finishing off the current line plus extra new lines
192             if (peek() != '\n' && peek() != '\r') {
193                 do {
194                     c = get();
195                 } while (c != EndOfInput && c != '\n' && c != '\r');
196             }
197             while (peek() == '\n' || peek() == '\r')
198                 get();
199             if (peek() == EndOfInput)
200                 return true;
201         }
202         lookingInMiddle = true;
203 
204         // Nominal start, skipping the desktop allowed comments and white space, but tracking if
205         // something else was found for ES:
206         consumeWhitespaceComment(foundNonSpaceTab);
207         if (foundNonSpaceTab)
208             versionNotFirst = true;
209 
210         // "#"
211         if (get() != '#') {
212             versionNotFirst = true;
213             continue;
214         }
215 
216         // whitespace
217         do {
218             c = get();
219         } while (c == ' ' || c == '\t');
220 
221         // "version"
222         if (    c != 'v' ||
223             get() != 'e' ||
224             get() != 'r' ||
225             get() != 's' ||
226             get() != 'i' ||
227             get() != 'o' ||
228             get() != 'n') {
229             versionNotFirst = true;
230             continue;
231         }
232 
233         // whitespace
234         do {
235             c = get();
236         } while (c == ' ' || c == '\t');
237 
238         // version number
239         while (c >= '0' && c <= '9') {
240             version = 10 * version + (c - '0');
241             c = get();
242         }
243         if (version == 0) {
244             versionNotFirst = true;
245             continue;
246         }
247 
248         // whitespace
249         while (c == ' ' || c == '\t')
250             c = get();
251 
252         // profile
253         const int maxProfileLength = 13;  // not including any 0
254         char profileString[maxProfileLength];
255         int profileLength;
256         for (profileLength = 0; profileLength < maxProfileLength; ++profileLength) {
257             if (c == EndOfInput || c == ' ' || c == '\t' || c == '\n' || c == '\r')
258                 break;
259             profileString[profileLength] = (char)c;
260             c = get();
261         }
262         if (c != EndOfInput && c != ' ' && c != '\t' && c != '\n' && c != '\r') {
263             versionNotFirst = true;
264             continue;
265         }
266 
267         if (profileLength == 2 && strncmp(profileString, "es", profileLength) == 0)
268             profile = EEsProfile;
269         else if (profileLength == 4 && strncmp(profileString, "core", profileLength) == 0)
270             profile = ECoreProfile;
271         else if (profileLength == 13 && strncmp(profileString, "compatibility", profileLength) == 0)
272             profile = ECompatibilityProfile;
273 
274         return versionNotFirst;
275     } while (true);
276 }
277 
278 // Fill this in when doing glslang-level scanning, to hand back to the parser.
279 class TParserToken {
280 public:
TParserToken(YYSTYPE & b)281     explicit TParserToken(YYSTYPE& b) : sType(b) { }
282 
283     YYSTYPE& sType;
284 protected:
285     TParserToken(TParserToken&);
286     TParserToken& operator=(TParserToken&);
287 };
288 
289 } // end namespace glslang
290 
291 // This is the function the glslang parser (i.e., bison) calls to get its next token
yylex(YYSTYPE * glslangTokenDesc,glslang::TParseContext & parseContext)292 int yylex(YYSTYPE* glslangTokenDesc, glslang::TParseContext& parseContext)
293 {
294     glslang::TParserToken token(*glslangTokenDesc);
295 
296     return parseContext.getScanContext()->tokenize(parseContext.getPpContext(), token);
297 }
298 
299 namespace {
300 
301 struct str_eq
302 {
operator ()__anondecfe2e40111::str_eq303     bool operator()(const char* lhs, const char* rhs) const
304     {
305         return strcmp(lhs, rhs) == 0;
306     }
307 };
308 
309 struct str_hash
310 {
operator ()__anondecfe2e40111::str_hash311     size_t operator()(const char* str) const
312     {
313         // djb2
314         unsigned long hash = 5381;
315         int c;
316 
317         while ((c = *str++) != 0)
318             hash = ((hash << 5) + hash) + c;
319 
320         return hash;
321     }
322 };
323 
324 // A single global usable by all threads, by all versions, by all languages.
325 // After a single process-level initialization, this is read only and thread safe
326 std::unordered_map<const char*, int, str_hash, str_eq>* KeywordMap = nullptr;
327 #ifndef GLSLANG_WEB
328 std::unordered_set<const char*, str_hash, str_eq>* ReservedSet = nullptr;
329 #endif
330 
331 };
332 
333 namespace glslang {
334 
fillInKeywordMap()335 void TScanContext::fillInKeywordMap()
336 {
337     if (KeywordMap != nullptr) {
338         // this is really an error, as this should called only once per process
339         // but, the only risk is if two threads called simultaneously
340         return;
341     }
342     KeywordMap = new std::unordered_map<const char*, int, str_hash, str_eq>;
343 
344     (*KeywordMap)["const"] =                   CONST;
345     (*KeywordMap)["uniform"] =                 UNIFORM;
346     (*KeywordMap)["buffer"] =                  BUFFER;
347     (*KeywordMap)["in"] =                      IN;
348     (*KeywordMap)["out"] =                     OUT;
349     (*KeywordMap)["smooth"] =                  SMOOTH;
350     (*KeywordMap)["flat"] =                    FLAT;
351     (*KeywordMap)["centroid"] =                CENTROID;
352     (*KeywordMap)["invariant"] =               INVARIANT;
353     (*KeywordMap)["packed"] =                  PACKED;
354     (*KeywordMap)["resource"] =                RESOURCE;
355     (*KeywordMap)["inout"] =                   INOUT;
356     (*KeywordMap)["struct"] =                  STRUCT;
357     (*KeywordMap)["break"] =                   BREAK;
358     (*KeywordMap)["continue"] =                CONTINUE;
359     (*KeywordMap)["do"] =                      DO;
360     (*KeywordMap)["for"] =                     FOR;
361     (*KeywordMap)["while"] =                   WHILE;
362     (*KeywordMap)["switch"] =                  SWITCH;
363     (*KeywordMap)["case"] =                    CASE;
364     (*KeywordMap)["default"] =                 DEFAULT;
365     (*KeywordMap)["if"] =                      IF;
366     (*KeywordMap)["else"] =                    ELSE;
367     (*KeywordMap)["discard"] =                 DISCARD;
368     (*KeywordMap)["terminateInvocation"] =     TERMINATE_INVOCATION;
369     (*KeywordMap)["terminateRayEXT"] =         TERMINATE_RAY;
370     (*KeywordMap)["ignoreIntersectionEXT"] =   IGNORE_INTERSECTION;
371     (*KeywordMap)["return"] =                  RETURN;
372     (*KeywordMap)["void"] =                    VOID;
373     (*KeywordMap)["bool"] =                    BOOL;
374     (*KeywordMap)["float"] =                   FLOAT;
375     (*KeywordMap)["int"] =                     INT;
376     (*KeywordMap)["bvec2"] =                   BVEC2;
377     (*KeywordMap)["bvec3"] =                   BVEC3;
378     (*KeywordMap)["bvec4"] =                   BVEC4;
379     (*KeywordMap)["vec2"] =                    VEC2;
380     (*KeywordMap)["vec3"] =                    VEC3;
381     (*KeywordMap)["vec4"] =                    VEC4;
382     (*KeywordMap)["ivec2"] =                   IVEC2;
383     (*KeywordMap)["ivec3"] =                   IVEC3;
384     (*KeywordMap)["ivec4"] =                   IVEC4;
385     (*KeywordMap)["mat2"] =                    MAT2;
386     (*KeywordMap)["mat3"] =                    MAT3;
387     (*KeywordMap)["mat4"] =                    MAT4;
388     (*KeywordMap)["true"] =                    BOOLCONSTANT;
389     (*KeywordMap)["false"] =                   BOOLCONSTANT;
390     (*KeywordMap)["layout"] =                  LAYOUT;
391     (*KeywordMap)["shared"] =                  SHARED;
392     (*KeywordMap)["highp"] =                   HIGH_PRECISION;
393     (*KeywordMap)["mediump"] =                 MEDIUM_PRECISION;
394     (*KeywordMap)["lowp"] =                    LOW_PRECISION;
395     (*KeywordMap)["superp"] =                  SUPERP;
396     (*KeywordMap)["precision"] =               PRECISION;
397     (*KeywordMap)["mat2x2"] =                  MAT2X2;
398     (*KeywordMap)["mat2x3"] =                  MAT2X3;
399     (*KeywordMap)["mat2x4"] =                  MAT2X4;
400     (*KeywordMap)["mat3x2"] =                  MAT3X2;
401     (*KeywordMap)["mat3x3"] =                  MAT3X3;
402     (*KeywordMap)["mat3x4"] =                  MAT3X4;
403     (*KeywordMap)["mat4x2"] =                  MAT4X2;
404     (*KeywordMap)["mat4x3"] =                  MAT4X3;
405     (*KeywordMap)["mat4x4"] =                  MAT4X4;
406     (*KeywordMap)["uint"] =                    UINT;
407     (*KeywordMap)["uvec2"] =                   UVEC2;
408     (*KeywordMap)["uvec3"] =                   UVEC3;
409     (*KeywordMap)["uvec4"] =                   UVEC4;
410 
411 #ifndef GLSLANG_WEB
412     (*KeywordMap)["nonuniformEXT"] =           NONUNIFORM;
413     (*KeywordMap)["demote"] =                  DEMOTE;
414     (*KeywordMap)["attribute"] =               ATTRIBUTE;
415     (*KeywordMap)["varying"] =                 VARYING;
416     (*KeywordMap)["noperspective"] =           NOPERSPECTIVE;
417     (*KeywordMap)["coherent"] =                COHERENT;
418     (*KeywordMap)["devicecoherent"] =          DEVICECOHERENT;
419     (*KeywordMap)["queuefamilycoherent"] =     QUEUEFAMILYCOHERENT;
420     (*KeywordMap)["workgroupcoherent"] =       WORKGROUPCOHERENT;
421     (*KeywordMap)["subgroupcoherent"] =        SUBGROUPCOHERENT;
422     (*KeywordMap)["shadercallcoherent"] =      SHADERCALLCOHERENT;
423     (*KeywordMap)["nonprivate"] =              NONPRIVATE;
424     (*KeywordMap)["restrict"] =                RESTRICT;
425     (*KeywordMap)["readonly"] =                READONLY;
426     (*KeywordMap)["writeonly"] =               WRITEONLY;
427     (*KeywordMap)["atomic_uint"] =             ATOMIC_UINT;
428     (*KeywordMap)["volatile"] =                VOLATILE;
429     (*KeywordMap)["patch"] =                   PATCH;
430     (*KeywordMap)["sample"] =                  SAMPLE;
431     (*KeywordMap)["subroutine"] =              SUBROUTINE;
432     (*KeywordMap)["dmat2"] =                   DMAT2;
433     (*KeywordMap)["dmat3"] =                   DMAT3;
434     (*KeywordMap)["dmat4"] =                   DMAT4;
435     (*KeywordMap)["dmat2x2"] =                 DMAT2X2;
436     (*KeywordMap)["dmat2x3"] =                 DMAT2X3;
437     (*KeywordMap)["dmat2x4"] =                 DMAT2X4;
438     (*KeywordMap)["dmat3x2"] =                 DMAT3X2;
439     (*KeywordMap)["dmat3x3"] =                 DMAT3X3;
440     (*KeywordMap)["dmat3x4"] =                 DMAT3X4;
441     (*KeywordMap)["dmat4x2"] =                 DMAT4X2;
442     (*KeywordMap)["dmat4x3"] =                 DMAT4X3;
443     (*KeywordMap)["dmat4x4"] =                 DMAT4X4;
444     (*KeywordMap)["image1D"] =                 IMAGE1D;
445     (*KeywordMap)["iimage1D"] =                IIMAGE1D;
446     (*KeywordMap)["uimage1D"] =                UIMAGE1D;
447     (*KeywordMap)["image2D"] =                 IMAGE2D;
448     (*KeywordMap)["iimage2D"] =                IIMAGE2D;
449     (*KeywordMap)["uimage2D"] =                UIMAGE2D;
450     (*KeywordMap)["image3D"] =                 IMAGE3D;
451     (*KeywordMap)["iimage3D"] =                IIMAGE3D;
452     (*KeywordMap)["uimage3D"] =                UIMAGE3D;
453     (*KeywordMap)["image2DRect"] =             IMAGE2DRECT;
454     (*KeywordMap)["iimage2DRect"] =            IIMAGE2DRECT;
455     (*KeywordMap)["uimage2DRect"] =            UIMAGE2DRECT;
456     (*KeywordMap)["imageCube"] =               IMAGECUBE;
457     (*KeywordMap)["iimageCube"] =              IIMAGECUBE;
458     (*KeywordMap)["uimageCube"] =              UIMAGECUBE;
459     (*KeywordMap)["imageBuffer"] =             IMAGEBUFFER;
460     (*KeywordMap)["iimageBuffer"] =            IIMAGEBUFFER;
461     (*KeywordMap)["uimageBuffer"] =            UIMAGEBUFFER;
462     (*KeywordMap)["image1DArray"] =            IMAGE1DARRAY;
463     (*KeywordMap)["iimage1DArray"] =           IIMAGE1DARRAY;
464     (*KeywordMap)["uimage1DArray"] =           UIMAGE1DARRAY;
465     (*KeywordMap)["image2DArray"] =            IMAGE2DARRAY;
466     (*KeywordMap)["iimage2DArray"] =           IIMAGE2DARRAY;
467     (*KeywordMap)["uimage2DArray"] =           UIMAGE2DARRAY;
468     (*KeywordMap)["imageCubeArray"] =          IMAGECUBEARRAY;
469     (*KeywordMap)["iimageCubeArray"] =         IIMAGECUBEARRAY;
470     (*KeywordMap)["uimageCubeArray"] =         UIMAGECUBEARRAY;
471     (*KeywordMap)["image2DMS"] =               IMAGE2DMS;
472     (*KeywordMap)["iimage2DMS"] =              IIMAGE2DMS;
473     (*KeywordMap)["uimage2DMS"] =              UIMAGE2DMS;
474     (*KeywordMap)["image2DMSArray"] =          IMAGE2DMSARRAY;
475     (*KeywordMap)["iimage2DMSArray"] =         IIMAGE2DMSARRAY;
476     (*KeywordMap)["uimage2DMSArray"] =         UIMAGE2DMSARRAY;
477     (*KeywordMap)["i64image1D"] =              I64IMAGE1D;
478     (*KeywordMap)["u64image1D"] =              U64IMAGE1D;
479     (*KeywordMap)["i64image2D"] =              I64IMAGE2D;
480     (*KeywordMap)["u64image2D"] =              U64IMAGE2D;
481     (*KeywordMap)["i64image3D"] =              I64IMAGE3D;
482     (*KeywordMap)["u64image3D"] =              U64IMAGE3D;
483     (*KeywordMap)["i64image2DRect"] =          I64IMAGE2DRECT;
484     (*KeywordMap)["u64image2DRect"] =          U64IMAGE2DRECT;
485     (*KeywordMap)["i64imageCube"] =            I64IMAGECUBE;
486     (*KeywordMap)["u64imageCube"] =            U64IMAGECUBE;
487     (*KeywordMap)["i64imageBuffer"] =          I64IMAGEBUFFER;
488     (*KeywordMap)["u64imageBuffer"] =          U64IMAGEBUFFER;
489     (*KeywordMap)["i64image1DArray"] =         I64IMAGE1DARRAY;
490     (*KeywordMap)["u64image1DArray"] =         U64IMAGE1DARRAY;
491     (*KeywordMap)["i64image2DArray"] =         I64IMAGE2DARRAY;
492     (*KeywordMap)["u64image2DArray"] =         U64IMAGE2DARRAY;
493     (*KeywordMap)["i64imageCubeArray"] =       I64IMAGECUBEARRAY;
494     (*KeywordMap)["u64imageCubeArray"] =       U64IMAGECUBEARRAY;
495     (*KeywordMap)["i64image2DMS"] =            I64IMAGE2DMS;
496     (*KeywordMap)["u64image2DMS"] =            U64IMAGE2DMS;
497     (*KeywordMap)["i64image2DMSArray"] =       I64IMAGE2DMSARRAY;
498     (*KeywordMap)["u64image2DMSArray"] =       U64IMAGE2DMSARRAY;
499     (*KeywordMap)["double"] =                  DOUBLE;
500     (*KeywordMap)["dvec2"] =                   DVEC2;
501     (*KeywordMap)["dvec3"] =                   DVEC3;
502     (*KeywordMap)["dvec4"] =                   DVEC4;
503     (*KeywordMap)["int64_t"] =                 INT64_T;
504     (*KeywordMap)["uint64_t"] =                UINT64_T;
505     (*KeywordMap)["i64vec2"] =                 I64VEC2;
506     (*KeywordMap)["i64vec3"] =                 I64VEC3;
507     (*KeywordMap)["i64vec4"] =                 I64VEC4;
508     (*KeywordMap)["u64vec2"] =                 U64VEC2;
509     (*KeywordMap)["u64vec3"] =                 U64VEC3;
510     (*KeywordMap)["u64vec4"] =                 U64VEC4;
511 
512     // GL_EXT_shader_explicit_arithmetic_types
513     (*KeywordMap)["int8_t"] =                  INT8_T;
514     (*KeywordMap)["i8vec2"] =                  I8VEC2;
515     (*KeywordMap)["i8vec3"] =                  I8VEC3;
516     (*KeywordMap)["i8vec4"] =                  I8VEC4;
517     (*KeywordMap)["uint8_t"] =                 UINT8_T;
518     (*KeywordMap)["u8vec2"] =                  U8VEC2;
519     (*KeywordMap)["u8vec3"] =                  U8VEC3;
520     (*KeywordMap)["u8vec4"] =                  U8VEC4;
521 
522     (*KeywordMap)["int16_t"] =                 INT16_T;
523     (*KeywordMap)["i16vec2"] =                 I16VEC2;
524     (*KeywordMap)["i16vec3"] =                 I16VEC3;
525     (*KeywordMap)["i16vec4"] =                 I16VEC4;
526     (*KeywordMap)["uint16_t"] =                UINT16_T;
527     (*KeywordMap)["u16vec2"] =                 U16VEC2;
528     (*KeywordMap)["u16vec3"] =                 U16VEC3;
529     (*KeywordMap)["u16vec4"] =                 U16VEC4;
530 
531     (*KeywordMap)["int32_t"] =                 INT32_T;
532     (*KeywordMap)["i32vec2"] =                 I32VEC2;
533     (*KeywordMap)["i32vec3"] =                 I32VEC3;
534     (*KeywordMap)["i32vec4"] =                 I32VEC4;
535     (*KeywordMap)["uint32_t"] =                UINT32_T;
536     (*KeywordMap)["u32vec2"] =                 U32VEC2;
537     (*KeywordMap)["u32vec3"] =                 U32VEC3;
538     (*KeywordMap)["u32vec4"] =                 U32VEC4;
539 
540     (*KeywordMap)["float16_t"] =               FLOAT16_T;
541     (*KeywordMap)["f16vec2"] =                 F16VEC2;
542     (*KeywordMap)["f16vec3"] =                 F16VEC3;
543     (*KeywordMap)["f16vec4"] =                 F16VEC4;
544     (*KeywordMap)["f16mat2"] =                 F16MAT2;
545     (*KeywordMap)["f16mat3"] =                 F16MAT3;
546     (*KeywordMap)["f16mat4"] =                 F16MAT4;
547     (*KeywordMap)["f16mat2x2"] =               F16MAT2X2;
548     (*KeywordMap)["f16mat2x3"] =               F16MAT2X3;
549     (*KeywordMap)["f16mat2x4"] =               F16MAT2X4;
550     (*KeywordMap)["f16mat3x2"] =               F16MAT3X2;
551     (*KeywordMap)["f16mat3x3"] =               F16MAT3X3;
552     (*KeywordMap)["f16mat3x4"] =               F16MAT3X4;
553     (*KeywordMap)["f16mat4x2"] =               F16MAT4X2;
554     (*KeywordMap)["f16mat4x3"] =               F16MAT4X3;
555     (*KeywordMap)["f16mat4x4"] =               F16MAT4X4;
556 
557     (*KeywordMap)["float32_t"] =               FLOAT32_T;
558     (*KeywordMap)["f32vec2"] =                 F32VEC2;
559     (*KeywordMap)["f32vec3"] =                 F32VEC3;
560     (*KeywordMap)["f32vec4"] =                 F32VEC4;
561     (*KeywordMap)["f32mat2"] =                 F32MAT2;
562     (*KeywordMap)["f32mat3"] =                 F32MAT3;
563     (*KeywordMap)["f32mat4"] =                 F32MAT4;
564     (*KeywordMap)["f32mat2x2"] =               F32MAT2X2;
565     (*KeywordMap)["f32mat2x3"] =               F32MAT2X3;
566     (*KeywordMap)["f32mat2x4"] =               F32MAT2X4;
567     (*KeywordMap)["f32mat3x2"] =               F32MAT3X2;
568     (*KeywordMap)["f32mat3x3"] =               F32MAT3X3;
569     (*KeywordMap)["f32mat3x4"] =               F32MAT3X4;
570     (*KeywordMap)["f32mat4x2"] =               F32MAT4X2;
571     (*KeywordMap)["f32mat4x3"] =               F32MAT4X3;
572     (*KeywordMap)["f32mat4x4"] =               F32MAT4X4;
573     (*KeywordMap)["float64_t"] =               FLOAT64_T;
574     (*KeywordMap)["f64vec2"] =                 F64VEC2;
575     (*KeywordMap)["f64vec3"] =                 F64VEC3;
576     (*KeywordMap)["f64vec4"] =                 F64VEC4;
577     (*KeywordMap)["f64mat2"] =                 F64MAT2;
578     (*KeywordMap)["f64mat3"] =                 F64MAT3;
579     (*KeywordMap)["f64mat4"] =                 F64MAT4;
580     (*KeywordMap)["f64mat2x2"] =               F64MAT2X2;
581     (*KeywordMap)["f64mat2x3"] =               F64MAT2X3;
582     (*KeywordMap)["f64mat2x4"] =               F64MAT2X4;
583     (*KeywordMap)["f64mat3x2"] =               F64MAT3X2;
584     (*KeywordMap)["f64mat3x3"] =               F64MAT3X3;
585     (*KeywordMap)["f64mat3x4"] =               F64MAT3X4;
586     (*KeywordMap)["f64mat4x2"] =               F64MAT4X2;
587     (*KeywordMap)["f64mat4x3"] =               F64MAT4X3;
588     (*KeywordMap)["f64mat4x4"] =               F64MAT4X4;
589 #endif
590 
591     (*KeywordMap)["sampler2D"] =               SAMPLER2D;
592     (*KeywordMap)["samplerCube"] =             SAMPLERCUBE;
593     (*KeywordMap)["samplerCubeShadow"] =       SAMPLERCUBESHADOW;
594     (*KeywordMap)["sampler2DArray"] =          SAMPLER2DARRAY;
595     (*KeywordMap)["sampler2DArrayShadow"] =    SAMPLER2DARRAYSHADOW;
596     (*KeywordMap)["isampler2D"] =              ISAMPLER2D;
597     (*KeywordMap)["isampler3D"] =              ISAMPLER3D;
598     (*KeywordMap)["isamplerCube"] =            ISAMPLERCUBE;
599     (*KeywordMap)["isampler2DArray"] =         ISAMPLER2DARRAY;
600     (*KeywordMap)["usampler2D"] =              USAMPLER2D;
601     (*KeywordMap)["usampler3D"] =              USAMPLER3D;
602     (*KeywordMap)["usamplerCube"] =            USAMPLERCUBE;
603     (*KeywordMap)["usampler2DArray"] =         USAMPLER2DARRAY;
604     (*KeywordMap)["sampler3D"] =               SAMPLER3D;
605     (*KeywordMap)["sampler2DShadow"] =         SAMPLER2DSHADOW;
606 
607     (*KeywordMap)["texture2D"] =               TEXTURE2D;
608     (*KeywordMap)["textureCube"] =             TEXTURECUBE;
609     (*KeywordMap)["texture2DArray"] =          TEXTURE2DARRAY;
610     (*KeywordMap)["itexture2D"] =              ITEXTURE2D;
611     (*KeywordMap)["itexture3D"] =              ITEXTURE3D;
612     (*KeywordMap)["itextureCube"] =            ITEXTURECUBE;
613     (*KeywordMap)["itexture2DArray"] =         ITEXTURE2DARRAY;
614     (*KeywordMap)["utexture2D"] =              UTEXTURE2D;
615     (*KeywordMap)["utexture3D"] =              UTEXTURE3D;
616     (*KeywordMap)["utextureCube"] =            UTEXTURECUBE;
617     (*KeywordMap)["utexture2DArray"] =         UTEXTURE2DARRAY;
618     (*KeywordMap)["texture3D"] =               TEXTURE3D;
619 
620     (*KeywordMap)["sampler"] =                 SAMPLER;
621     (*KeywordMap)["samplerShadow"] =           SAMPLERSHADOW;
622 
623 #ifndef GLSLANG_WEB
624     (*KeywordMap)["textureCubeArray"] =        TEXTURECUBEARRAY;
625     (*KeywordMap)["itextureCubeArray"] =       ITEXTURECUBEARRAY;
626     (*KeywordMap)["utextureCubeArray"] =       UTEXTURECUBEARRAY;
627     (*KeywordMap)["samplerCubeArray"] =        SAMPLERCUBEARRAY;
628     (*KeywordMap)["samplerCubeArrayShadow"] =  SAMPLERCUBEARRAYSHADOW;
629     (*KeywordMap)["isamplerCubeArray"] =       ISAMPLERCUBEARRAY;
630     (*KeywordMap)["usamplerCubeArray"] =       USAMPLERCUBEARRAY;
631     (*KeywordMap)["sampler1DArrayShadow"] =    SAMPLER1DARRAYSHADOW;
632     (*KeywordMap)["isampler1DArray"] =         ISAMPLER1DARRAY;
633     (*KeywordMap)["usampler1D"] =              USAMPLER1D;
634     (*KeywordMap)["isampler1D"] =              ISAMPLER1D;
635     (*KeywordMap)["usampler1DArray"] =         USAMPLER1DARRAY;
636     (*KeywordMap)["samplerBuffer"] =           SAMPLERBUFFER;
637     (*KeywordMap)["isampler2DRect"] =          ISAMPLER2DRECT;
638     (*KeywordMap)["usampler2DRect"] =          USAMPLER2DRECT;
639     (*KeywordMap)["isamplerBuffer"] =          ISAMPLERBUFFER;
640     (*KeywordMap)["usamplerBuffer"] =          USAMPLERBUFFER;
641     (*KeywordMap)["sampler2DMS"] =             SAMPLER2DMS;
642     (*KeywordMap)["isampler2DMS"] =            ISAMPLER2DMS;
643     (*KeywordMap)["usampler2DMS"] =            USAMPLER2DMS;
644     (*KeywordMap)["sampler2DMSArray"] =        SAMPLER2DMSARRAY;
645     (*KeywordMap)["isampler2DMSArray"] =       ISAMPLER2DMSARRAY;
646     (*KeywordMap)["usampler2DMSArray"] =       USAMPLER2DMSARRAY;
647     (*KeywordMap)["sampler1D"] =               SAMPLER1D;
648     (*KeywordMap)["sampler1DShadow"] =         SAMPLER1DSHADOW;
649     (*KeywordMap)["sampler2DRect"] =           SAMPLER2DRECT;
650     (*KeywordMap)["sampler2DRectShadow"] =     SAMPLER2DRECTSHADOW;
651     (*KeywordMap)["sampler1DArray"] =          SAMPLER1DARRAY;
652 
653     (*KeywordMap)["samplerExternalOES"] =      SAMPLEREXTERNALOES; // GL_OES_EGL_image_external
654 
655     (*KeywordMap)["__samplerExternal2DY2YEXT"] = SAMPLEREXTERNAL2DY2YEXT; // GL_EXT_YUV_target
656 
657     (*KeywordMap)["itexture1DArray"] =         ITEXTURE1DARRAY;
658     (*KeywordMap)["utexture1D"] =              UTEXTURE1D;
659     (*KeywordMap)["itexture1D"] =              ITEXTURE1D;
660     (*KeywordMap)["utexture1DArray"] =         UTEXTURE1DARRAY;
661     (*KeywordMap)["textureBuffer"] =           TEXTUREBUFFER;
662     (*KeywordMap)["itexture2DRect"] =          ITEXTURE2DRECT;
663     (*KeywordMap)["utexture2DRect"] =          UTEXTURE2DRECT;
664     (*KeywordMap)["itextureBuffer"] =          ITEXTUREBUFFER;
665     (*KeywordMap)["utextureBuffer"] =          UTEXTUREBUFFER;
666     (*KeywordMap)["texture2DMS"] =             TEXTURE2DMS;
667     (*KeywordMap)["itexture2DMS"] =            ITEXTURE2DMS;
668     (*KeywordMap)["utexture2DMS"] =            UTEXTURE2DMS;
669     (*KeywordMap)["texture2DMSArray"] =        TEXTURE2DMSARRAY;
670     (*KeywordMap)["itexture2DMSArray"] =       ITEXTURE2DMSARRAY;
671     (*KeywordMap)["utexture2DMSArray"] =       UTEXTURE2DMSARRAY;
672     (*KeywordMap)["texture1D"] =               TEXTURE1D;
673     (*KeywordMap)["texture2DRect"] =           TEXTURE2DRECT;
674     (*KeywordMap)["texture1DArray"] =          TEXTURE1DARRAY;
675 
676     (*KeywordMap)["subpassInput"] =            SUBPASSINPUT;
677     (*KeywordMap)["subpassInputMS"] =          SUBPASSINPUTMS;
678     (*KeywordMap)["isubpassInput"] =           ISUBPASSINPUT;
679     (*KeywordMap)["isubpassInputMS"] =         ISUBPASSINPUTMS;
680     (*KeywordMap)["usubpassInput"] =           USUBPASSINPUT;
681     (*KeywordMap)["usubpassInputMS"] =         USUBPASSINPUTMS;
682 
683     (*KeywordMap)["f16sampler1D"] =                 F16SAMPLER1D;
684     (*KeywordMap)["f16sampler2D"] =                 F16SAMPLER2D;
685     (*KeywordMap)["f16sampler3D"] =                 F16SAMPLER3D;
686     (*KeywordMap)["f16sampler2DRect"] =             F16SAMPLER2DRECT;
687     (*KeywordMap)["f16samplerCube"] =               F16SAMPLERCUBE;
688     (*KeywordMap)["f16sampler1DArray"] =            F16SAMPLER1DARRAY;
689     (*KeywordMap)["f16sampler2DArray"] =            F16SAMPLER2DARRAY;
690     (*KeywordMap)["f16samplerCubeArray"] =          F16SAMPLERCUBEARRAY;
691     (*KeywordMap)["f16samplerBuffer"] =             F16SAMPLERBUFFER;
692     (*KeywordMap)["f16sampler2DMS"] =               F16SAMPLER2DMS;
693     (*KeywordMap)["f16sampler2DMSArray"] =          F16SAMPLER2DMSARRAY;
694     (*KeywordMap)["f16sampler1DShadow"] =           F16SAMPLER1DSHADOW;
695     (*KeywordMap)["f16sampler2DShadow"] =           F16SAMPLER2DSHADOW;
696     (*KeywordMap)["f16sampler2DRectShadow"] =       F16SAMPLER2DRECTSHADOW;
697     (*KeywordMap)["f16samplerCubeShadow"] =         F16SAMPLERCUBESHADOW;
698     (*KeywordMap)["f16sampler1DArrayShadow"] =      F16SAMPLER1DARRAYSHADOW;
699     (*KeywordMap)["f16sampler2DArrayShadow"] =      F16SAMPLER2DARRAYSHADOW;
700     (*KeywordMap)["f16samplerCubeArrayShadow"] =    F16SAMPLERCUBEARRAYSHADOW;
701 
702     (*KeywordMap)["f16image1D"] =                   F16IMAGE1D;
703     (*KeywordMap)["f16image2D"] =                   F16IMAGE2D;
704     (*KeywordMap)["f16image3D"] =                   F16IMAGE3D;
705     (*KeywordMap)["f16image2DRect"] =               F16IMAGE2DRECT;
706     (*KeywordMap)["f16imageCube"] =                 F16IMAGECUBE;
707     (*KeywordMap)["f16image1DArray"] =              F16IMAGE1DARRAY;
708     (*KeywordMap)["f16image2DArray"] =              F16IMAGE2DARRAY;
709     (*KeywordMap)["f16imageCubeArray"] =            F16IMAGECUBEARRAY;
710     (*KeywordMap)["f16imageBuffer"] =               F16IMAGEBUFFER;
711     (*KeywordMap)["f16image2DMS"] =                 F16IMAGE2DMS;
712     (*KeywordMap)["f16image2DMSArray"] =            F16IMAGE2DMSARRAY;
713 
714     (*KeywordMap)["f16texture1D"] =                 F16TEXTURE1D;
715     (*KeywordMap)["f16texture2D"] =                 F16TEXTURE2D;
716     (*KeywordMap)["f16texture3D"] =                 F16TEXTURE3D;
717     (*KeywordMap)["f16texture2DRect"] =             F16TEXTURE2DRECT;
718     (*KeywordMap)["f16textureCube"] =               F16TEXTURECUBE;
719     (*KeywordMap)["f16texture1DArray"] =            F16TEXTURE1DARRAY;
720     (*KeywordMap)["f16texture2DArray"] =            F16TEXTURE2DARRAY;
721     (*KeywordMap)["f16textureCubeArray"] =          F16TEXTURECUBEARRAY;
722     (*KeywordMap)["f16textureBuffer"] =             F16TEXTUREBUFFER;
723     (*KeywordMap)["f16texture2DMS"] =               F16TEXTURE2DMS;
724     (*KeywordMap)["f16texture2DMSArray"] =          F16TEXTURE2DMSARRAY;
725 
726     (*KeywordMap)["f16subpassInput"] =              F16SUBPASSINPUT;
727     (*KeywordMap)["f16subpassInputMS"] =            F16SUBPASSINPUTMS;
728     (*KeywordMap)["__explicitInterpAMD"] =     EXPLICITINTERPAMD;
729     (*KeywordMap)["pervertexNV"] =             PERVERTEXNV;
730     (*KeywordMap)["precise"] =                 PRECISE;
731 
732     (*KeywordMap)["rayPayloadNV"] =            PAYLOADNV;
733     (*KeywordMap)["rayPayloadEXT"] =           PAYLOADEXT;
734     (*KeywordMap)["rayPayloadInNV"] =          PAYLOADINNV;
735     (*KeywordMap)["rayPayloadInEXT"] =         PAYLOADINEXT;
736     (*KeywordMap)["hitAttributeNV"] =          HITATTRNV;
737     (*KeywordMap)["hitAttributeEXT"] =         HITATTREXT;
738     (*KeywordMap)["callableDataNV"] =          CALLDATANV;
739     (*KeywordMap)["callableDataEXT"] =         CALLDATAEXT;
740     (*KeywordMap)["callableDataInNV"] =        CALLDATAINNV;
741     (*KeywordMap)["callableDataInEXT"] =       CALLDATAINEXT;
742     (*KeywordMap)["accelerationStructureNV"] = ACCSTRUCTNV;
743     (*KeywordMap)["accelerationStructureEXT"]   = ACCSTRUCTEXT;
744     (*KeywordMap)["rayQueryEXT"] =              RAYQUERYEXT;
745     (*KeywordMap)["perprimitiveNV"] =          PERPRIMITIVENV;
746     (*KeywordMap)["perviewNV"] =               PERVIEWNV;
747     (*KeywordMap)["taskNV"] =                  PERTASKNV;
748 
749     (*KeywordMap)["fcoopmatNV"] =              FCOOPMATNV;
750     (*KeywordMap)["icoopmatNV"] =              ICOOPMATNV;
751     (*KeywordMap)["ucoopmatNV"] =              UCOOPMATNV;
752 
753     ReservedSet = new std::unordered_set<const char*, str_hash, str_eq>;
754 
755     ReservedSet->insert("common");
756     ReservedSet->insert("partition");
757     ReservedSet->insert("active");
758     ReservedSet->insert("asm");
759     ReservedSet->insert("class");
760     ReservedSet->insert("union");
761     ReservedSet->insert("enum");
762     ReservedSet->insert("typedef");
763     ReservedSet->insert("template");
764     ReservedSet->insert("this");
765     ReservedSet->insert("goto");
766     ReservedSet->insert("inline");
767     ReservedSet->insert("noinline");
768     ReservedSet->insert("public");
769     ReservedSet->insert("static");
770     ReservedSet->insert("extern");
771     ReservedSet->insert("external");
772     ReservedSet->insert("interface");
773     ReservedSet->insert("long");
774     ReservedSet->insert("short");
775     ReservedSet->insert("half");
776     ReservedSet->insert("fixed");
777     ReservedSet->insert("unsigned");
778     ReservedSet->insert("input");
779     ReservedSet->insert("output");
780     ReservedSet->insert("hvec2");
781     ReservedSet->insert("hvec3");
782     ReservedSet->insert("hvec4");
783     ReservedSet->insert("fvec2");
784     ReservedSet->insert("fvec3");
785     ReservedSet->insert("fvec4");
786     ReservedSet->insert("sampler3DRect");
787     ReservedSet->insert("filter");
788     ReservedSet->insert("sizeof");
789     ReservedSet->insert("cast");
790     ReservedSet->insert("namespace");
791     ReservedSet->insert("using");
792 #endif
793 }
794 
deleteKeywordMap()795 void TScanContext::deleteKeywordMap()
796 {
797     delete KeywordMap;
798     KeywordMap = nullptr;
799 #ifndef GLSLANG_WEB
800     delete ReservedSet;
801     ReservedSet = nullptr;
802 #endif
803 }
804 
805 // Called by yylex to get the next token.
806 // Returning 0 implies end of input.
tokenize(TPpContext * pp,TParserToken & token)807 int TScanContext::tokenize(TPpContext* pp, TParserToken& token)
808 {
809     do {
810         parserToken = &token;
811         TPpToken ppToken;
812         int token = pp->tokenize(ppToken);
813         if (token == EndOfInput)
814             return 0;
815 
816         tokenText = ppToken.name;
817         loc = ppToken.loc;
818         parserToken->sType.lex.loc = loc;
819         switch (token) {
820         case ';':  afterType = false; afterBuffer = false; return SEMICOLON;
821         case ',':  afterType = false;   return COMMA;
822         case ':':                       return COLON;
823         case '=':  afterType = false;   return EQUAL;
824         case '(':  afterType = false;   return LEFT_PAREN;
825         case ')':  afterType = false;   return RIGHT_PAREN;
826         case '.':  field = true;        return DOT;
827         case '!':                       return BANG;
828         case '-':                       return DASH;
829         case '~':                       return TILDE;
830         case '+':                       return PLUS;
831         case '*':                       return STAR;
832         case '/':                       return SLASH;
833         case '%':                       return PERCENT;
834         case '<':                       return LEFT_ANGLE;
835         case '>':                       return RIGHT_ANGLE;
836         case '|':                       return VERTICAL_BAR;
837         case '^':                       return CARET;
838         case '&':                       return AMPERSAND;
839         case '?':                       return QUESTION;
840         case '[':                       return LEFT_BRACKET;
841         case ']':                       return RIGHT_BRACKET;
842         case '{':  afterStruct = false; afterBuffer = false; return LEFT_BRACE;
843         case '}':                       return RIGHT_BRACE;
844         case '\\':
845             parseContext.error(loc, "illegal use of escape character", "\\", "");
846             break;
847 
848         case PPAtomAddAssign:          return ADD_ASSIGN;
849         case PPAtomSubAssign:          return SUB_ASSIGN;
850         case PPAtomMulAssign:          return MUL_ASSIGN;
851         case PPAtomDivAssign:          return DIV_ASSIGN;
852         case PPAtomModAssign:          return MOD_ASSIGN;
853 
854         case PpAtomRight:              return RIGHT_OP;
855         case PpAtomLeft:               return LEFT_OP;
856 
857         case PpAtomRightAssign:        return RIGHT_ASSIGN;
858         case PpAtomLeftAssign:         return LEFT_ASSIGN;
859         case PpAtomAndAssign:          return AND_ASSIGN;
860         case PpAtomOrAssign:           return OR_ASSIGN;
861         case PpAtomXorAssign:          return XOR_ASSIGN;
862 
863         case PpAtomAnd:                return AND_OP;
864         case PpAtomOr:                 return OR_OP;
865         case PpAtomXor:                return XOR_OP;
866 
867         case PpAtomEQ:                 return EQ_OP;
868         case PpAtomGE:                 return GE_OP;
869         case PpAtomNE:                 return NE_OP;
870         case PpAtomLE:                 return LE_OP;
871 
872         case PpAtomDecrement:          return DEC_OP;
873         case PpAtomIncrement:          return INC_OP;
874 
875         case PpAtomColonColon:
876             parseContext.error(loc, "not supported", "::", "");
877             break;
878 
879         case PpAtomConstString:        parserToken->sType.lex.string = NewPoolTString(tokenText);     return STRING_LITERAL;
880         case PpAtomConstInt:           parserToken->sType.lex.i    = ppToken.ival;       return INTCONSTANT;
881         case PpAtomConstUint:          parserToken->sType.lex.i    = ppToken.ival;       return UINTCONSTANT;
882         case PpAtomConstFloat:         parserToken->sType.lex.d    = ppToken.dval;       return FLOATCONSTANT;
883 #ifndef GLSLANG_WEB
884         case PpAtomConstInt16:         parserToken->sType.lex.i    = ppToken.ival;       return INT16CONSTANT;
885         case PpAtomConstUint16:        parserToken->sType.lex.i    = ppToken.ival;       return UINT16CONSTANT;
886         case PpAtomConstInt64:         parserToken->sType.lex.i64  = ppToken.i64val;     return INT64CONSTANT;
887         case PpAtomConstUint64:        parserToken->sType.lex.i64  = ppToken.i64val;     return UINT64CONSTANT;
888         case PpAtomConstDouble:        parserToken->sType.lex.d    = ppToken.dval;       return DOUBLECONSTANT;
889         case PpAtomConstFloat16:       parserToken->sType.lex.d    = ppToken.dval;       return FLOAT16CONSTANT;
890 #endif
891         case PpAtomIdentifier:
892         {
893             int token = tokenizeIdentifier();
894             field = false;
895             return token;
896         }
897 
898         case EndOfInput:               return 0;
899 
900         default:
901             char buf[2];
902             buf[0] = (char)token;
903             buf[1] = 0;
904             parseContext.error(loc, "unexpected token", buf, "");
905             break;
906         }
907     } while (true);
908 }
909 
tokenizeIdentifier()910 int TScanContext::tokenizeIdentifier()
911 {
912 #ifndef GLSLANG_WEB
913     if (ReservedSet->find(tokenText) != ReservedSet->end())
914         return reservedWord();
915 #endif
916 
917     auto it = KeywordMap->find(tokenText);
918     if (it == KeywordMap->end()) {
919         // Should have an identifier of some sort
920         return identifierOrType();
921     }
922     keyword = it->second;
923 
924     switch (keyword) {
925     case CONST:
926     case UNIFORM:
927     case IN:
928     case OUT:
929     case INOUT:
930     case BREAK:
931     case CONTINUE:
932     case DO:
933     case FOR:
934     case WHILE:
935     case IF:
936     case ELSE:
937     case DISCARD:
938     case RETURN:
939     case CASE:
940         return keyword;
941 
942     case TERMINATE_INVOCATION:
943         if (!parseContext.extensionTurnedOn(E_GL_EXT_terminate_invocation))
944             return identifierOrType();
945         return keyword;
946 
947     case TERMINATE_RAY:
948     case IGNORE_INTERSECTION:
949         if (!parseContext.extensionTurnedOn(E_GL_EXT_ray_tracing))
950             return identifierOrType();
951         return keyword;
952 
953     case BUFFER:
954         afterBuffer = true;
955         if ((parseContext.isEsProfile() && parseContext.version < 310) ||
956             (!parseContext.isEsProfile() && (parseContext.version < 430 &&
957             !parseContext.extensionTurnedOn(E_GL_ARB_shader_storage_buffer_object))))
958             return identifierOrType();
959         return keyword;
960 
961     case STRUCT:
962         afterStruct = true;
963         return keyword;
964 
965     case SWITCH:
966     case DEFAULT:
967         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
968             (!parseContext.isEsProfile() && parseContext.version < 130))
969             reservedWord();
970         return keyword;
971 
972     case VOID:
973     case BOOL:
974     case FLOAT:
975     case INT:
976     case BVEC2:
977     case BVEC3:
978     case BVEC4:
979     case VEC2:
980     case VEC3:
981     case VEC4:
982     case IVEC2:
983     case IVEC3:
984     case IVEC4:
985     case MAT2:
986     case MAT3:
987     case MAT4:
988     case SAMPLER2D:
989     case SAMPLERCUBE:
990         afterType = true;
991         return keyword;
992 
993     case BOOLCONSTANT:
994         if (strcmp("true", tokenText) == 0)
995             parserToken->sType.lex.b = true;
996         else
997             parserToken->sType.lex.b = false;
998         return keyword;
999 
1000     case SMOOTH:
1001         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1002             (!parseContext.isEsProfile() && parseContext.version < 130))
1003             return identifierOrType();
1004         return keyword;
1005     case FLAT:
1006         if (parseContext.isEsProfile() && parseContext.version < 300)
1007             reservedWord();
1008         else if (!parseContext.isEsProfile() && parseContext.version < 130)
1009             return identifierOrType();
1010         return keyword;
1011     case CENTROID:
1012         if (parseContext.version < 120)
1013             return identifierOrType();
1014         return keyword;
1015     case INVARIANT:
1016         if (!parseContext.isEsProfile() && parseContext.version < 120)
1017             return identifierOrType();
1018         return keyword;
1019     case PACKED:
1020         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1021             (!parseContext.isEsProfile() && parseContext.version < 140))
1022             return reservedWord();
1023         return identifierOrType();
1024 
1025     case RESOURCE:
1026     {
1027         bool reserved = (parseContext.isEsProfile() && parseContext.version >= 300) ||
1028                         (!parseContext.isEsProfile() && parseContext.version >= 420);
1029         return identifierOrReserved(reserved);
1030     }
1031     case SUPERP:
1032     {
1033         bool reserved = parseContext.isEsProfile() || parseContext.version >= 130;
1034         return identifierOrReserved(reserved);
1035     }
1036 
1037 #ifndef GLSLANG_WEB
1038     case NOPERSPECTIVE:
1039         if (parseContext.extensionTurnedOn(E_GL_NV_shader_noperspective_interpolation))
1040             return keyword;
1041         return es30ReservedFromGLSL(130);
1042 
1043     case NONUNIFORM:
1044         if (parseContext.extensionTurnedOn(E_GL_EXT_nonuniform_qualifier))
1045             return keyword;
1046         else
1047             return identifierOrType();
1048     case ATTRIBUTE:
1049     case VARYING:
1050         if (parseContext.isEsProfile() && parseContext.version >= 300)
1051             reservedWord();
1052         return keyword;
1053     case PAYLOADNV:
1054     case PAYLOADINNV:
1055     case HITATTRNV:
1056     case CALLDATANV:
1057     case CALLDATAINNV:
1058     case ACCSTRUCTNV:
1059         if (parseContext.symbolTable.atBuiltInLevel() ||
1060             parseContext.extensionTurnedOn(E_GL_NV_ray_tracing))
1061             return keyword;
1062         return identifierOrType();
1063     case PAYLOADEXT:
1064     case PAYLOADINEXT:
1065     case HITATTREXT:
1066     case CALLDATAEXT:
1067     case CALLDATAINEXT:
1068     case ACCSTRUCTEXT:
1069         if (parseContext.symbolTable.atBuiltInLevel() ||
1070             parseContext.extensionTurnedOn(E_GL_EXT_ray_tracing) ||
1071             parseContext.extensionTurnedOn(E_GL_EXT_ray_query))
1072             return keyword;
1073         return identifierOrType();
1074     case RAYQUERYEXT:
1075         if (parseContext.symbolTable.atBuiltInLevel() ||
1076             (!parseContext.isEsProfile() && parseContext.version >= 460
1077                  && parseContext.extensionTurnedOn(E_GL_EXT_ray_query)))
1078             return keyword;
1079         return identifierOrType();
1080     case ATOMIC_UINT:
1081         if ((parseContext.isEsProfile() && parseContext.version >= 310) ||
1082             parseContext.extensionTurnedOn(E_GL_ARB_shader_atomic_counters))
1083             return keyword;
1084         return es30ReservedFromGLSL(420);
1085 
1086     case COHERENT:
1087     case DEVICECOHERENT:
1088     case QUEUEFAMILYCOHERENT:
1089     case WORKGROUPCOHERENT:
1090     case SUBGROUPCOHERENT:
1091     case SHADERCALLCOHERENT:
1092     case NONPRIVATE:
1093     case RESTRICT:
1094     case READONLY:
1095     case WRITEONLY:
1096         if (parseContext.isEsProfile() && parseContext.version >= 310)
1097             return keyword;
1098         return es30ReservedFromGLSL(parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store) ? 130 : 420);
1099     case VOLATILE:
1100         if (parseContext.isEsProfile() && parseContext.version >= 310)
1101             return keyword;
1102         if (! parseContext.symbolTable.atBuiltInLevel() && (parseContext.isEsProfile() ||
1103             (parseContext.version < 420 && ! parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
1104             reservedWord();
1105         return keyword;
1106     case PATCH:
1107         if (parseContext.symbolTable.atBuiltInLevel() ||
1108             (parseContext.isEsProfile() &&
1109              (parseContext.version >= 320 ||
1110               parseContext.extensionsTurnedOn(Num_AEP_tessellation_shader, AEP_tessellation_shader))) ||
1111             (!parseContext.isEsProfile() && parseContext.extensionTurnedOn(E_GL_ARB_tessellation_shader)))
1112             return keyword;
1113 
1114         return es30ReservedFromGLSL(400);
1115 
1116     case SAMPLE:
1117         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1118             parseContext.extensionsTurnedOn(1, &E_GL_OES_shader_multisample_interpolation))
1119             return keyword;
1120         return es30ReservedFromGLSL(400);
1121 
1122     case SUBROUTINE:
1123         return es30ReservedFromGLSL(400);
1124 #endif
1125     case SHARED:
1126         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1127             (!parseContext.isEsProfile() && parseContext.version < 140))
1128             return identifierOrType();
1129         return keyword;
1130     case LAYOUT:
1131     {
1132         const int numLayoutExts = 2;
1133         const char* layoutExts[numLayoutExts] = { E_GL_ARB_shading_language_420pack,
1134                                                   E_GL_ARB_explicit_attrib_location };
1135         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1136             (!parseContext.isEsProfile() && parseContext.version < 140 &&
1137             ! parseContext.extensionsTurnedOn(numLayoutExts, layoutExts)))
1138             return identifierOrType();
1139         return keyword;
1140     }
1141 
1142     case HIGH_PRECISION:
1143     case MEDIUM_PRECISION:
1144     case LOW_PRECISION:
1145     case PRECISION:
1146         return precisionKeyword();
1147 
1148     case MAT2X2:
1149     case MAT2X3:
1150     case MAT2X4:
1151     case MAT3X2:
1152     case MAT3X3:
1153     case MAT3X4:
1154     case MAT4X2:
1155     case MAT4X3:
1156     case MAT4X4:
1157         return matNxM();
1158 
1159 #ifndef GLSLANG_WEB
1160     case DMAT2:
1161     case DMAT3:
1162     case DMAT4:
1163     case DMAT2X2:
1164     case DMAT2X3:
1165     case DMAT2X4:
1166     case DMAT3X2:
1167     case DMAT3X3:
1168     case DMAT3X4:
1169     case DMAT4X2:
1170     case DMAT4X3:
1171     case DMAT4X4:
1172         return dMat();
1173 
1174     case IMAGE1D:
1175     case IIMAGE1D:
1176     case UIMAGE1D:
1177     case IMAGE1DARRAY:
1178     case IIMAGE1DARRAY:
1179     case UIMAGE1DARRAY:
1180     case IMAGE2DRECT:
1181     case IIMAGE2DRECT:
1182     case UIMAGE2DRECT:
1183         afterType = true;
1184         return firstGenerationImage(false);
1185 
1186     case I64IMAGE1D:
1187     case U64IMAGE1D:
1188     case I64IMAGE1DARRAY:
1189     case U64IMAGE1DARRAY:
1190     case I64IMAGE2DRECT:
1191     case U64IMAGE2DRECT:
1192         afterType = true;
1193         if (parseContext.symbolTable.atBuiltInLevel() ||
1194             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1195             return firstGenerationImage(false);
1196         }
1197         return identifierOrType();
1198 
1199     case IMAGEBUFFER:
1200     case IIMAGEBUFFER:
1201     case UIMAGEBUFFER:
1202         afterType = true;
1203         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1204             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1205             return keyword;
1206         return firstGenerationImage(false);
1207 
1208     case I64IMAGEBUFFER:
1209     case U64IMAGEBUFFER:
1210         afterType = true;
1211         if (parseContext.symbolTable.atBuiltInLevel() ||
1212             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1213             if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1214                 parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1215                 return keyword;
1216             return firstGenerationImage(false);
1217         }
1218         return identifierOrType();
1219 
1220     case IMAGE2D:
1221     case IIMAGE2D:
1222     case UIMAGE2D:
1223     case IMAGE3D:
1224     case IIMAGE3D:
1225     case UIMAGE3D:
1226     case IMAGECUBE:
1227     case IIMAGECUBE:
1228     case UIMAGECUBE:
1229     case IMAGE2DARRAY:
1230     case IIMAGE2DARRAY:
1231     case UIMAGE2DARRAY:
1232         afterType = true;
1233         return firstGenerationImage(true);
1234 
1235     case I64IMAGE2D:
1236     case U64IMAGE2D:
1237     case I64IMAGE3D:
1238     case U64IMAGE3D:
1239     case I64IMAGECUBE:
1240     case U64IMAGECUBE:
1241     case I64IMAGE2DARRAY:
1242     case U64IMAGE2DARRAY:
1243         afterType = true;
1244         if (parseContext.symbolTable.atBuiltInLevel() ||
1245             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64))
1246             return firstGenerationImage(true);
1247         return identifierOrType();
1248 
1249     case IMAGECUBEARRAY:
1250     case IIMAGECUBEARRAY:
1251     case UIMAGECUBEARRAY:
1252         afterType = true;
1253         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1254             parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1255             return keyword;
1256         return secondGenerationImage();
1257 
1258     case I64IMAGECUBEARRAY:
1259     case U64IMAGECUBEARRAY:
1260         afterType = true;
1261         if (parseContext.symbolTable.atBuiltInLevel() ||
1262             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1263             if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1264                 parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1265                 return keyword;
1266             return secondGenerationImage();
1267         }
1268         return identifierOrType();
1269 
1270     case IMAGE2DMS:
1271     case IIMAGE2DMS:
1272     case UIMAGE2DMS:
1273     case IMAGE2DMSARRAY:
1274     case IIMAGE2DMSARRAY:
1275     case UIMAGE2DMSARRAY:
1276         afterType = true;
1277         return secondGenerationImage();
1278 
1279     case I64IMAGE2DMS:
1280     case U64IMAGE2DMS:
1281     case I64IMAGE2DMSARRAY:
1282     case U64IMAGE2DMSARRAY:
1283         afterType = true;
1284         if (parseContext.symbolTable.atBuiltInLevel() ||
1285             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1286             return secondGenerationImage();
1287         }
1288         return identifierOrType();
1289 
1290     case DOUBLE:
1291     case DVEC2:
1292     case DVEC3:
1293     case DVEC4:
1294         afterType = true;
1295         if (parseContext.isEsProfile() || parseContext.version < 150 ||
1296             (!parseContext.symbolTable.atBuiltInLevel() &&
1297               (parseContext.version < 400 && !parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_fp64) &&
1298               (parseContext.version < 410 && !parseContext.extensionTurnedOn(E_GL_ARB_vertex_attrib_64bit)))))
1299             reservedWord();
1300         return keyword;
1301 
1302     case INT64_T:
1303     case UINT64_T:
1304     case I64VEC2:
1305     case I64VEC3:
1306     case I64VEC4:
1307     case U64VEC2:
1308     case U64VEC3:
1309     case U64VEC4:
1310         afterType = true;
1311         if (parseContext.symbolTable.atBuiltInLevel() ||
1312             parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_int64) ||
1313             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1314             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int64))
1315             return keyword;
1316         return identifierOrType();
1317 
1318     case INT8_T:
1319     case UINT8_T:
1320     case I8VEC2:
1321     case I8VEC3:
1322     case I8VEC4:
1323     case U8VEC2:
1324     case U8VEC3:
1325     case U8VEC4:
1326         afterType = true;
1327         if (parseContext.symbolTable.atBuiltInLevel() ||
1328             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1329             parseContext.extensionTurnedOn(E_GL_EXT_shader_8bit_storage) ||
1330             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int8))
1331             return keyword;
1332         return identifierOrType();
1333 
1334     case INT16_T:
1335     case UINT16_T:
1336     case I16VEC2:
1337     case I16VEC3:
1338     case I16VEC4:
1339     case U16VEC2:
1340     case U16VEC3:
1341     case U16VEC4:
1342         afterType = true;
1343         if (parseContext.symbolTable.atBuiltInLevel() ||
1344             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_int16) ||
1345             parseContext.extensionTurnedOn(E_GL_EXT_shader_16bit_storage) ||
1346             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1347             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int16))
1348             return keyword;
1349         return identifierOrType();
1350     case INT32_T:
1351     case UINT32_T:
1352     case I32VEC2:
1353     case I32VEC3:
1354     case I32VEC4:
1355     case U32VEC2:
1356     case U32VEC3:
1357     case U32VEC4:
1358         afterType = true;
1359         if (parseContext.symbolTable.atBuiltInLevel() ||
1360             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1361             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int32))
1362             return keyword;
1363         return identifierOrType();
1364     case FLOAT32_T:
1365     case F32VEC2:
1366     case F32VEC3:
1367     case F32VEC4:
1368     case F32MAT2:
1369     case F32MAT3:
1370     case F32MAT4:
1371     case F32MAT2X2:
1372     case F32MAT2X3:
1373     case F32MAT2X4:
1374     case F32MAT3X2:
1375     case F32MAT3X3:
1376     case F32MAT3X4:
1377     case F32MAT4X2:
1378     case F32MAT4X3:
1379     case F32MAT4X4:
1380         afterType = true;
1381         if (parseContext.symbolTable.atBuiltInLevel() ||
1382             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1383             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float32))
1384             return keyword;
1385         return identifierOrType();
1386 
1387     case FLOAT64_T:
1388     case F64VEC2:
1389     case F64VEC3:
1390     case F64VEC4:
1391     case F64MAT2:
1392     case F64MAT3:
1393     case F64MAT4:
1394     case F64MAT2X2:
1395     case F64MAT2X3:
1396     case F64MAT2X4:
1397     case F64MAT3X2:
1398     case F64MAT3X3:
1399     case F64MAT3X4:
1400     case F64MAT4X2:
1401     case F64MAT4X3:
1402     case F64MAT4X4:
1403         afterType = true;
1404         if (parseContext.symbolTable.atBuiltInLevel() ||
1405             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1406             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float64))
1407             return keyword;
1408         return identifierOrType();
1409 
1410     case FLOAT16_T:
1411     case F16VEC2:
1412     case F16VEC3:
1413     case F16VEC4:
1414         afterType = true;
1415         if (parseContext.symbolTable.atBuiltInLevel() ||
1416             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float) ||
1417             parseContext.extensionTurnedOn(E_GL_EXT_shader_16bit_storage) ||
1418             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1419             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float16))
1420             return keyword;
1421 
1422         return identifierOrType();
1423 
1424     case F16MAT2:
1425     case F16MAT3:
1426     case F16MAT4:
1427     case F16MAT2X2:
1428     case F16MAT2X3:
1429     case F16MAT2X4:
1430     case F16MAT3X2:
1431     case F16MAT3X3:
1432     case F16MAT3X4:
1433     case F16MAT4X2:
1434     case F16MAT4X3:
1435     case F16MAT4X4:
1436         afterType = true;
1437         if (parseContext.symbolTable.atBuiltInLevel() ||
1438             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float) ||
1439             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1440             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float16))
1441             return keyword;
1442 
1443         return identifierOrType();
1444 
1445     case SAMPLERCUBEARRAY:
1446     case SAMPLERCUBEARRAYSHADOW:
1447     case ISAMPLERCUBEARRAY:
1448     case USAMPLERCUBEARRAY:
1449         afterType = true;
1450         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1451             parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1452             return keyword;
1453         if (parseContext.isEsProfile() || (parseContext.version < 400 && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_cube_map_array)))
1454             reservedWord();
1455         return keyword;
1456 
1457     case TEXTURECUBEARRAY:
1458     case ITEXTURECUBEARRAY:
1459     case UTEXTURECUBEARRAY:
1460         if (parseContext.spvVersion.vulkan > 0)
1461             return keyword;
1462         else
1463             return identifierOrType();
1464 #endif
1465 
1466     case UINT:
1467     case UVEC2:
1468     case UVEC3:
1469     case UVEC4:
1470     case SAMPLERCUBESHADOW:
1471     case SAMPLER2DARRAY:
1472     case SAMPLER2DARRAYSHADOW:
1473     case ISAMPLER2D:
1474     case ISAMPLER3D:
1475     case ISAMPLERCUBE:
1476     case ISAMPLER2DARRAY:
1477     case USAMPLER2D:
1478     case USAMPLER3D:
1479     case USAMPLERCUBE:
1480     case USAMPLER2DARRAY:
1481         afterType = true;
1482         return nonreservedKeyword(300, 130);
1483 
1484     case SAMPLER3D:
1485         afterType = true;
1486         if (parseContext.isEsProfile() && parseContext.version < 300) {
1487             if (!parseContext.extensionTurnedOn(E_GL_OES_texture_3D))
1488                 reservedWord();
1489         }
1490         return keyword;
1491 
1492     case SAMPLER2DSHADOW:
1493         afterType = true;
1494         if (parseContext.isEsProfile() && parseContext.version < 300) {
1495             if (!parseContext.extensionTurnedOn(E_GL_EXT_shadow_samplers))
1496                 reservedWord();
1497         }
1498         return keyword;
1499 
1500     case TEXTURE2D:
1501     case TEXTURECUBE:
1502     case TEXTURE2DARRAY:
1503     case ITEXTURE2D:
1504     case ITEXTURE3D:
1505     case ITEXTURECUBE:
1506     case ITEXTURE2DARRAY:
1507     case UTEXTURE2D:
1508     case UTEXTURE3D:
1509     case UTEXTURECUBE:
1510     case UTEXTURE2DARRAY:
1511     case TEXTURE3D:
1512     case SAMPLER:
1513     case SAMPLERSHADOW:
1514         if (parseContext.spvVersion.vulkan > 0)
1515             return keyword;
1516         else
1517             return identifierOrType();
1518 
1519 #ifndef GLSLANG_WEB
1520     case ISAMPLER1D:
1521     case ISAMPLER1DARRAY:
1522     case SAMPLER1DARRAYSHADOW:
1523     case USAMPLER1D:
1524     case USAMPLER1DARRAY:
1525         afterType = true;
1526         return es30ReservedFromGLSL(130);
1527     case ISAMPLER2DRECT:
1528     case USAMPLER2DRECT:
1529         afterType = true;
1530         return es30ReservedFromGLSL(140);
1531 
1532     case SAMPLERBUFFER:
1533         afterType = true;
1534         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1535             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1536             return keyword;
1537         return es30ReservedFromGLSL(130);
1538 
1539     case ISAMPLERBUFFER:
1540     case USAMPLERBUFFER:
1541         afterType = true;
1542         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1543             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1544             return keyword;
1545         return es30ReservedFromGLSL(140);
1546 
1547     case SAMPLER2DMS:
1548     case ISAMPLER2DMS:
1549     case USAMPLER2DMS:
1550         afterType = true;
1551         if (parseContext.isEsProfile() && parseContext.version >= 310)
1552             return keyword;
1553         if (!parseContext.isEsProfile() && (parseContext.version > 140 ||
1554             (parseContext.version == 140 && parseContext.extensionsTurnedOn(1, &E_GL_ARB_texture_multisample))))
1555             return keyword;
1556         return es30ReservedFromGLSL(150);
1557 
1558     case SAMPLER2DMSARRAY:
1559     case ISAMPLER2DMSARRAY:
1560     case USAMPLER2DMSARRAY:
1561         afterType = true;
1562         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1563             parseContext.extensionsTurnedOn(1, &E_GL_OES_texture_storage_multisample_2d_array))
1564             return keyword;
1565         if (!parseContext.isEsProfile() && (parseContext.version > 140 ||
1566             (parseContext.version == 140 && parseContext.extensionsTurnedOn(1, &E_GL_ARB_texture_multisample))))
1567             return keyword;
1568         return es30ReservedFromGLSL(150);
1569 
1570     case SAMPLER1D:
1571     case SAMPLER1DSHADOW:
1572         afterType = true;
1573         if (parseContext.isEsProfile())
1574             reservedWord();
1575         return keyword;
1576 
1577     case SAMPLER2DRECT:
1578     case SAMPLER2DRECTSHADOW:
1579         afterType = true;
1580         if (parseContext.isEsProfile())
1581             reservedWord();
1582         else if (parseContext.version < 140 && ! parseContext.symbolTable.atBuiltInLevel() && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_rectangle)) {
1583             if (parseContext.relaxedErrors())
1584                 parseContext.requireExtensions(loc, 1, &E_GL_ARB_texture_rectangle, "texture-rectangle sampler keyword");
1585             else
1586                 reservedWord();
1587         }
1588         return keyword;
1589 
1590     case SAMPLER1DARRAY:
1591         afterType = true;
1592         if (parseContext.isEsProfile() && parseContext.version == 300)
1593             reservedWord();
1594         else if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1595                  (!parseContext.isEsProfile() && parseContext.version < 130))
1596             return identifierOrType();
1597         return keyword;
1598 
1599     case SAMPLEREXTERNALOES:
1600         afterType = true;
1601         if (parseContext.symbolTable.atBuiltInLevel() ||
1602             parseContext.extensionTurnedOn(E_GL_OES_EGL_image_external) ||
1603             parseContext.extensionTurnedOn(E_GL_OES_EGL_image_external_essl3))
1604             return keyword;
1605         return identifierOrType();
1606 
1607     case SAMPLEREXTERNAL2DY2YEXT:
1608         afterType = true;
1609         if (parseContext.symbolTable.atBuiltInLevel() ||
1610             parseContext.extensionTurnedOn(E_GL_EXT_YUV_target))
1611             return keyword;
1612         return identifierOrType();
1613 
1614     case ITEXTURE1DARRAY:
1615     case UTEXTURE1D:
1616     case ITEXTURE1D:
1617     case UTEXTURE1DARRAY:
1618     case TEXTUREBUFFER:
1619     case ITEXTURE2DRECT:
1620     case UTEXTURE2DRECT:
1621     case ITEXTUREBUFFER:
1622     case UTEXTUREBUFFER:
1623     case TEXTURE2DMS:
1624     case ITEXTURE2DMS:
1625     case UTEXTURE2DMS:
1626     case TEXTURE2DMSARRAY:
1627     case ITEXTURE2DMSARRAY:
1628     case UTEXTURE2DMSARRAY:
1629     case TEXTURE1D:
1630     case TEXTURE2DRECT:
1631     case TEXTURE1DARRAY:
1632         if (parseContext.spvVersion.vulkan > 0)
1633             return keyword;
1634         else
1635             return identifierOrType();
1636 
1637     case SUBPASSINPUT:
1638     case SUBPASSINPUTMS:
1639     case ISUBPASSINPUT:
1640     case ISUBPASSINPUTMS:
1641     case USUBPASSINPUT:
1642     case USUBPASSINPUTMS:
1643         if (parseContext.spvVersion.vulkan > 0)
1644             return keyword;
1645         else
1646             return identifierOrType();
1647 
1648     case F16SAMPLER1D:
1649     case F16SAMPLER2D:
1650     case F16SAMPLER3D:
1651     case F16SAMPLER2DRECT:
1652     case F16SAMPLERCUBE:
1653     case F16SAMPLER1DARRAY:
1654     case F16SAMPLER2DARRAY:
1655     case F16SAMPLERCUBEARRAY:
1656     case F16SAMPLERBUFFER:
1657     case F16SAMPLER2DMS:
1658     case F16SAMPLER2DMSARRAY:
1659     case F16SAMPLER1DSHADOW:
1660     case F16SAMPLER2DSHADOW:
1661     case F16SAMPLER1DARRAYSHADOW:
1662     case F16SAMPLER2DARRAYSHADOW:
1663     case F16SAMPLER2DRECTSHADOW:
1664     case F16SAMPLERCUBESHADOW:
1665     case F16SAMPLERCUBEARRAYSHADOW:
1666 
1667     case F16IMAGE1D:
1668     case F16IMAGE2D:
1669     case F16IMAGE3D:
1670     case F16IMAGE2DRECT:
1671     case F16IMAGECUBE:
1672     case F16IMAGE1DARRAY:
1673     case F16IMAGE2DARRAY:
1674     case F16IMAGECUBEARRAY:
1675     case F16IMAGEBUFFER:
1676     case F16IMAGE2DMS:
1677     case F16IMAGE2DMSARRAY:
1678 
1679     case F16TEXTURE1D:
1680     case F16TEXTURE2D:
1681     case F16TEXTURE3D:
1682     case F16TEXTURE2DRECT:
1683     case F16TEXTURECUBE:
1684     case F16TEXTURE1DARRAY:
1685     case F16TEXTURE2DARRAY:
1686     case F16TEXTURECUBEARRAY:
1687     case F16TEXTUREBUFFER:
1688     case F16TEXTURE2DMS:
1689     case F16TEXTURE2DMSARRAY:
1690 
1691     case F16SUBPASSINPUT:
1692     case F16SUBPASSINPUTMS:
1693         afterType = true;
1694         if (parseContext.symbolTable.atBuiltInLevel() ||
1695             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float_fetch))
1696             return keyword;
1697         return identifierOrType();
1698 
1699     case EXPLICITINTERPAMD:
1700         if (parseContext.extensionTurnedOn(E_GL_AMD_shader_explicit_vertex_parameter))
1701             return keyword;
1702         return identifierOrType();
1703 
1704     case PERVERTEXNV:
1705         if ((!parseContext.isEsProfile() && parseContext.version >= 450) ||
1706             parseContext.extensionTurnedOn(E_GL_NV_fragment_shader_barycentric))
1707             return keyword;
1708         return identifierOrType();
1709 
1710     case PRECISE:
1711         if ((parseContext.isEsProfile() &&
1712              (parseContext.version >= 320 || parseContext.extensionsTurnedOn(Num_AEP_gpu_shader5, AEP_gpu_shader5))) ||
1713             (!parseContext.isEsProfile() && parseContext.version >= 400))
1714             return keyword;
1715         if (parseContext.isEsProfile() && parseContext.version == 310) {
1716             reservedWord();
1717             return keyword;
1718         }
1719         return identifierOrType();
1720 
1721     case PERPRIMITIVENV:
1722     case PERVIEWNV:
1723     case PERTASKNV:
1724         if ((!parseContext.isEsProfile() && parseContext.version >= 450) ||
1725             (parseContext.isEsProfile() && parseContext.version >= 320) ||
1726             parseContext.extensionTurnedOn(E_GL_NV_mesh_shader))
1727             return keyword;
1728         return identifierOrType();
1729 
1730     case FCOOPMATNV:
1731         afterType = true;
1732         if (parseContext.symbolTable.atBuiltInLevel() ||
1733             parseContext.extensionTurnedOn(E_GL_NV_cooperative_matrix))
1734             return keyword;
1735         return identifierOrType();
1736 
1737     case UCOOPMATNV:
1738     case ICOOPMATNV:
1739         afterType = true;
1740         if (parseContext.symbolTable.atBuiltInLevel() ||
1741             parseContext.extensionTurnedOn(E_GL_NV_integer_cooperative_matrix))
1742             return keyword;
1743         return identifierOrType();
1744 
1745     case DEMOTE:
1746         if (parseContext.extensionTurnedOn(E_GL_EXT_demote_to_helper_invocation))
1747             return keyword;
1748         else
1749             return identifierOrType();
1750 #endif
1751 
1752     default:
1753         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown glslang keyword", loc);
1754         return 0;
1755     }
1756 }
1757 
identifierOrType()1758 int TScanContext::identifierOrType()
1759 {
1760     parserToken->sType.lex.string = NewPoolTString(tokenText);
1761     if (field)
1762         return IDENTIFIER;
1763 
1764     parserToken->sType.lex.symbol = parseContext.symbolTable.find(*parserToken->sType.lex.string);
1765     if ((afterType == false && afterStruct == false) && parserToken->sType.lex.symbol != nullptr) {
1766         if (const TVariable* variable = parserToken->sType.lex.symbol->getAsVariable()) {
1767             if (variable->isUserType() &&
1768                 // treat redeclaration of forward-declared buffer/uniform reference as an identifier
1769                 !(variable->getType().isReference() && afterBuffer)) {
1770                 afterType = true;
1771 
1772                 return TYPE_NAME;
1773             }
1774         }
1775     }
1776 
1777     return IDENTIFIER;
1778 }
1779 
1780 // Give an error for use of a reserved symbol.
1781 // However, allow built-in declarations to use reserved words, to allow
1782 // extension support before the extension is enabled.
reservedWord()1783 int TScanContext::reservedWord()
1784 {
1785     if (! parseContext.symbolTable.atBuiltInLevel())
1786         parseContext.error(loc, "Reserved word.", tokenText, "", "");
1787 
1788     return 0;
1789 }
1790 
identifierOrReserved(bool reserved)1791 int TScanContext::identifierOrReserved(bool reserved)
1792 {
1793     if (reserved) {
1794         reservedWord();
1795 
1796         return 0;
1797     }
1798 
1799     if (parseContext.isForwardCompatible())
1800         parseContext.warn(loc, "using future reserved keyword", tokenText, "");
1801 
1802     return identifierOrType();
1803 }
1804 
1805 // For keywords that suddenly showed up on non-ES (not previously reserved)
1806 // but then got reserved by ES 3.0.
es30ReservedFromGLSL(int version)1807 int TScanContext::es30ReservedFromGLSL(int version)
1808 {
1809     if (parseContext.symbolTable.atBuiltInLevel())
1810         return keyword;
1811 
1812     if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1813         (!parseContext.isEsProfile() && parseContext.version < version)) {
1814             if (parseContext.isForwardCompatible())
1815                 parseContext.warn(loc, "future reserved word in ES 300 and keyword in GLSL", tokenText, "");
1816 
1817             return identifierOrType();
1818     } else if (parseContext.isEsProfile() && parseContext.version >= 300)
1819         reservedWord();
1820 
1821     return keyword;
1822 }
1823 
1824 // For a keyword that was never reserved, until it suddenly
1825 // showed up, both in an es version and a non-ES version.
nonreservedKeyword(int esVersion,int nonEsVersion)1826 int TScanContext::nonreservedKeyword(int esVersion, int nonEsVersion)
1827 {
1828     if ((parseContext.isEsProfile() && parseContext.version < esVersion) ||
1829         (!parseContext.isEsProfile() && parseContext.version < nonEsVersion)) {
1830         if (parseContext.isForwardCompatible())
1831             parseContext.warn(loc, "using future keyword", tokenText, "");
1832 
1833         return identifierOrType();
1834     }
1835 
1836     return keyword;
1837 }
1838 
precisionKeyword()1839 int TScanContext::precisionKeyword()
1840 {
1841     if (parseContext.isEsProfile() || parseContext.version >= 130)
1842         return keyword;
1843 
1844     if (parseContext.isForwardCompatible())
1845         parseContext.warn(loc, "using ES precision qualifier keyword", tokenText, "");
1846 
1847     return identifierOrType();
1848 }
1849 
matNxM()1850 int TScanContext::matNxM()
1851 {
1852     afterType = true;
1853 
1854     if (parseContext.version > 110)
1855         return keyword;
1856 
1857     if (parseContext.isForwardCompatible())
1858         parseContext.warn(loc, "using future non-square matrix type keyword", tokenText, "");
1859 
1860     return identifierOrType();
1861 }
1862 
dMat()1863 int TScanContext::dMat()
1864 {
1865     afterType = true;
1866 
1867     if (parseContext.isEsProfile() && parseContext.version >= 300) {
1868         reservedWord();
1869 
1870         return keyword;
1871     }
1872 
1873     if (!parseContext.isEsProfile() && (parseContext.version >= 400 ||
1874         parseContext.symbolTable.atBuiltInLevel() ||
1875         (parseContext.version >= 150 && parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_fp64)) ||
1876         (parseContext.version >= 150 && parseContext.extensionTurnedOn(E_GL_ARB_vertex_attrib_64bit)
1877          && parseContext.language == EShLangVertex)))
1878         return keyword;
1879 
1880     if (parseContext.isForwardCompatible())
1881         parseContext.warn(loc, "using future type keyword", tokenText, "");
1882 
1883     return identifierOrType();
1884 }
1885 
firstGenerationImage(bool inEs310)1886 int TScanContext::firstGenerationImage(bool inEs310)
1887 {
1888     if (parseContext.symbolTable.atBuiltInLevel() ||
1889         (!parseContext.isEsProfile() && (parseContext.version >= 420 ||
1890          parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))) ||
1891         (inEs310 && parseContext.isEsProfile() && parseContext.version >= 310))
1892         return keyword;
1893 
1894     if ((parseContext.isEsProfile() && parseContext.version >= 300) ||
1895         (!parseContext.isEsProfile() && parseContext.version >= 130)) {
1896         reservedWord();
1897 
1898         return keyword;
1899     }
1900 
1901     if (parseContext.isForwardCompatible())
1902         parseContext.warn(loc, "using future type keyword", tokenText, "");
1903 
1904     return identifierOrType();
1905 }
1906 
secondGenerationImage()1907 int TScanContext::secondGenerationImage()
1908 {
1909     if (parseContext.isEsProfile() && parseContext.version >= 310) {
1910         reservedWord();
1911         return keyword;
1912     }
1913 
1914     if (parseContext.symbolTable.atBuiltInLevel() ||
1915         (!parseContext.isEsProfile() &&
1916          (parseContext.version >= 420 || parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
1917         return keyword;
1918 
1919     if (parseContext.isForwardCompatible())
1920         parseContext.warn(loc, "using future type keyword", tokenText, "");
1921 
1922     return identifierOrType();
1923 }
1924 
1925 } // end namespace glslang
1926