Add support for GL_NV_shader_invocation_reorder. (#3054)
[platform/upstream/glslang.git] / glslang / MachineIndependent / Scan.cpp
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
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
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
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 //
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:
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
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 {
303     bool operator()(const char* lhs, const char* rhs) const
304     {
305         return strcmp(lhs, rhs) == 0;
306     }
307 };
308
309 struct str_hash
310 {
311     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
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
590     // GL_EXT_spirv_intrinsics
591     (*KeywordMap)["spirv_instruction"] =       SPIRV_INSTRUCTION;
592     (*KeywordMap)["spirv_execution_mode"] =    SPIRV_EXECUTION_MODE;
593     (*KeywordMap)["spirv_execution_mode_id"] = SPIRV_EXECUTION_MODE_ID;
594     (*KeywordMap)["spirv_decorate"] =          SPIRV_DECORATE;
595     (*KeywordMap)["spirv_decorate_id"] =       SPIRV_DECORATE_ID;
596     (*KeywordMap)["spirv_decorate_string"] =   SPIRV_DECORATE_STRING;
597     (*KeywordMap)["spirv_type"] =              SPIRV_TYPE;
598     (*KeywordMap)["spirv_storage_class"] =     SPIRV_STORAGE_CLASS;
599     (*KeywordMap)["spirv_by_reference"] =      SPIRV_BY_REFERENCE;
600     (*KeywordMap)["spirv_literal"] =           SPIRV_LITERAL;
601 #endif
602
603     (*KeywordMap)["sampler2D"] =               SAMPLER2D;
604     (*KeywordMap)["samplerCube"] =             SAMPLERCUBE;
605     (*KeywordMap)["samplerCubeShadow"] =       SAMPLERCUBESHADOW;
606     (*KeywordMap)["sampler2DArray"] =          SAMPLER2DARRAY;
607     (*KeywordMap)["sampler2DArrayShadow"] =    SAMPLER2DARRAYSHADOW;
608     (*KeywordMap)["isampler2D"] =              ISAMPLER2D;
609     (*KeywordMap)["isampler3D"] =              ISAMPLER3D;
610     (*KeywordMap)["isamplerCube"] =            ISAMPLERCUBE;
611     (*KeywordMap)["isampler2DArray"] =         ISAMPLER2DARRAY;
612     (*KeywordMap)["usampler2D"] =              USAMPLER2D;
613     (*KeywordMap)["usampler3D"] =              USAMPLER3D;
614     (*KeywordMap)["usamplerCube"] =            USAMPLERCUBE;
615     (*KeywordMap)["usampler2DArray"] =         USAMPLER2DARRAY;
616     (*KeywordMap)["sampler3D"] =               SAMPLER3D;
617     (*KeywordMap)["sampler2DShadow"] =         SAMPLER2DSHADOW;
618
619     (*KeywordMap)["texture2D"] =               TEXTURE2D;
620     (*KeywordMap)["textureCube"] =             TEXTURECUBE;
621     (*KeywordMap)["texture2DArray"] =          TEXTURE2DARRAY;
622     (*KeywordMap)["itexture2D"] =              ITEXTURE2D;
623     (*KeywordMap)["itexture3D"] =              ITEXTURE3D;
624     (*KeywordMap)["itextureCube"] =            ITEXTURECUBE;
625     (*KeywordMap)["itexture2DArray"] =         ITEXTURE2DARRAY;
626     (*KeywordMap)["utexture2D"] =              UTEXTURE2D;
627     (*KeywordMap)["utexture3D"] =              UTEXTURE3D;
628     (*KeywordMap)["utextureCube"] =            UTEXTURECUBE;
629     (*KeywordMap)["utexture2DArray"] =         UTEXTURE2DARRAY;
630     (*KeywordMap)["texture3D"] =               TEXTURE3D;
631
632     (*KeywordMap)["sampler"] =                 SAMPLER;
633     (*KeywordMap)["samplerShadow"] =           SAMPLERSHADOW;
634
635 #ifndef GLSLANG_WEB
636     (*KeywordMap)["textureCubeArray"] =        TEXTURECUBEARRAY;
637     (*KeywordMap)["itextureCubeArray"] =       ITEXTURECUBEARRAY;
638     (*KeywordMap)["utextureCubeArray"] =       UTEXTURECUBEARRAY;
639     (*KeywordMap)["samplerCubeArray"] =        SAMPLERCUBEARRAY;
640     (*KeywordMap)["samplerCubeArrayShadow"] =  SAMPLERCUBEARRAYSHADOW;
641     (*KeywordMap)["isamplerCubeArray"] =       ISAMPLERCUBEARRAY;
642     (*KeywordMap)["usamplerCubeArray"] =       USAMPLERCUBEARRAY;
643     (*KeywordMap)["sampler1DArrayShadow"] =    SAMPLER1DARRAYSHADOW;
644     (*KeywordMap)["isampler1DArray"] =         ISAMPLER1DARRAY;
645     (*KeywordMap)["usampler1D"] =              USAMPLER1D;
646     (*KeywordMap)["isampler1D"] =              ISAMPLER1D;
647     (*KeywordMap)["usampler1DArray"] =         USAMPLER1DARRAY;
648     (*KeywordMap)["samplerBuffer"] =           SAMPLERBUFFER;
649     (*KeywordMap)["isampler2DRect"] =          ISAMPLER2DRECT;
650     (*KeywordMap)["usampler2DRect"] =          USAMPLER2DRECT;
651     (*KeywordMap)["isamplerBuffer"] =          ISAMPLERBUFFER;
652     (*KeywordMap)["usamplerBuffer"] =          USAMPLERBUFFER;
653     (*KeywordMap)["sampler2DMS"] =             SAMPLER2DMS;
654     (*KeywordMap)["isampler2DMS"] =            ISAMPLER2DMS;
655     (*KeywordMap)["usampler2DMS"] =            USAMPLER2DMS;
656     (*KeywordMap)["sampler2DMSArray"] =        SAMPLER2DMSARRAY;
657     (*KeywordMap)["isampler2DMSArray"] =       ISAMPLER2DMSARRAY;
658     (*KeywordMap)["usampler2DMSArray"] =       USAMPLER2DMSARRAY;
659     (*KeywordMap)["sampler1D"] =               SAMPLER1D;
660     (*KeywordMap)["sampler1DShadow"] =         SAMPLER1DSHADOW;
661     (*KeywordMap)["sampler2DRect"] =           SAMPLER2DRECT;
662     (*KeywordMap)["sampler2DRectShadow"] =     SAMPLER2DRECTSHADOW;
663     (*KeywordMap)["sampler1DArray"] =          SAMPLER1DARRAY;
664
665     (*KeywordMap)["samplerExternalOES"] =      SAMPLEREXTERNALOES; // GL_OES_EGL_image_external
666
667     (*KeywordMap)["__samplerExternal2DY2YEXT"] = SAMPLEREXTERNAL2DY2YEXT; // GL_EXT_YUV_target
668
669     (*KeywordMap)["itexture1DArray"] =         ITEXTURE1DARRAY;
670     (*KeywordMap)["utexture1D"] =              UTEXTURE1D;
671     (*KeywordMap)["itexture1D"] =              ITEXTURE1D;
672     (*KeywordMap)["utexture1DArray"] =         UTEXTURE1DARRAY;
673     (*KeywordMap)["textureBuffer"] =           TEXTUREBUFFER;
674     (*KeywordMap)["itexture2DRect"] =          ITEXTURE2DRECT;
675     (*KeywordMap)["utexture2DRect"] =          UTEXTURE2DRECT;
676     (*KeywordMap)["itextureBuffer"] =          ITEXTUREBUFFER;
677     (*KeywordMap)["utextureBuffer"] =          UTEXTUREBUFFER;
678     (*KeywordMap)["texture2DMS"] =             TEXTURE2DMS;
679     (*KeywordMap)["itexture2DMS"] =            ITEXTURE2DMS;
680     (*KeywordMap)["utexture2DMS"] =            UTEXTURE2DMS;
681     (*KeywordMap)["texture2DMSArray"] =        TEXTURE2DMSARRAY;
682     (*KeywordMap)["itexture2DMSArray"] =       ITEXTURE2DMSARRAY;
683     (*KeywordMap)["utexture2DMSArray"] =       UTEXTURE2DMSARRAY;
684     (*KeywordMap)["texture1D"] =               TEXTURE1D;
685     (*KeywordMap)["texture2DRect"] =           TEXTURE2DRECT;
686     (*KeywordMap)["texture1DArray"] =          TEXTURE1DARRAY;
687
688     (*KeywordMap)["subpassInput"] =            SUBPASSINPUT;
689     (*KeywordMap)["subpassInputMS"] =          SUBPASSINPUTMS;
690     (*KeywordMap)["isubpassInput"] =           ISUBPASSINPUT;
691     (*KeywordMap)["isubpassInputMS"] =         ISUBPASSINPUTMS;
692     (*KeywordMap)["usubpassInput"] =           USUBPASSINPUT;
693     (*KeywordMap)["usubpassInputMS"] =         USUBPASSINPUTMS;
694
695     (*KeywordMap)["f16sampler1D"] =                 F16SAMPLER1D;
696     (*KeywordMap)["f16sampler2D"] =                 F16SAMPLER2D;
697     (*KeywordMap)["f16sampler3D"] =                 F16SAMPLER3D;
698     (*KeywordMap)["f16sampler2DRect"] =             F16SAMPLER2DRECT;
699     (*KeywordMap)["f16samplerCube"] =               F16SAMPLERCUBE;
700     (*KeywordMap)["f16sampler1DArray"] =            F16SAMPLER1DARRAY;
701     (*KeywordMap)["f16sampler2DArray"] =            F16SAMPLER2DARRAY;
702     (*KeywordMap)["f16samplerCubeArray"] =          F16SAMPLERCUBEARRAY;
703     (*KeywordMap)["f16samplerBuffer"] =             F16SAMPLERBUFFER;
704     (*KeywordMap)["f16sampler2DMS"] =               F16SAMPLER2DMS;
705     (*KeywordMap)["f16sampler2DMSArray"] =          F16SAMPLER2DMSARRAY;
706     (*KeywordMap)["f16sampler1DShadow"] =           F16SAMPLER1DSHADOW;
707     (*KeywordMap)["f16sampler2DShadow"] =           F16SAMPLER2DSHADOW;
708     (*KeywordMap)["f16sampler2DRectShadow"] =       F16SAMPLER2DRECTSHADOW;
709     (*KeywordMap)["f16samplerCubeShadow"] =         F16SAMPLERCUBESHADOW;
710     (*KeywordMap)["f16sampler1DArrayShadow"] =      F16SAMPLER1DARRAYSHADOW;
711     (*KeywordMap)["f16sampler2DArrayShadow"] =      F16SAMPLER2DARRAYSHADOW;
712     (*KeywordMap)["f16samplerCubeArrayShadow"] =    F16SAMPLERCUBEARRAYSHADOW;
713
714     (*KeywordMap)["f16image1D"] =                   F16IMAGE1D;
715     (*KeywordMap)["f16image2D"] =                   F16IMAGE2D;
716     (*KeywordMap)["f16image3D"] =                   F16IMAGE3D;
717     (*KeywordMap)["f16image2DRect"] =               F16IMAGE2DRECT;
718     (*KeywordMap)["f16imageCube"] =                 F16IMAGECUBE;
719     (*KeywordMap)["f16image1DArray"] =              F16IMAGE1DARRAY;
720     (*KeywordMap)["f16image2DArray"] =              F16IMAGE2DARRAY;
721     (*KeywordMap)["f16imageCubeArray"] =            F16IMAGECUBEARRAY;
722     (*KeywordMap)["f16imageBuffer"] =               F16IMAGEBUFFER;
723     (*KeywordMap)["f16image2DMS"] =                 F16IMAGE2DMS;
724     (*KeywordMap)["f16image2DMSArray"] =            F16IMAGE2DMSARRAY;
725
726     (*KeywordMap)["f16texture1D"] =                 F16TEXTURE1D;
727     (*KeywordMap)["f16texture2D"] =                 F16TEXTURE2D;
728     (*KeywordMap)["f16texture3D"] =                 F16TEXTURE3D;
729     (*KeywordMap)["f16texture2DRect"] =             F16TEXTURE2DRECT;
730     (*KeywordMap)["f16textureCube"] =               F16TEXTURECUBE;
731     (*KeywordMap)["f16texture1DArray"] =            F16TEXTURE1DARRAY;
732     (*KeywordMap)["f16texture2DArray"] =            F16TEXTURE2DARRAY;
733     (*KeywordMap)["f16textureCubeArray"] =          F16TEXTURECUBEARRAY;
734     (*KeywordMap)["f16textureBuffer"] =             F16TEXTUREBUFFER;
735     (*KeywordMap)["f16texture2DMS"] =               F16TEXTURE2DMS;
736     (*KeywordMap)["f16texture2DMSArray"] =          F16TEXTURE2DMSARRAY;
737
738     (*KeywordMap)["f16subpassInput"] =              F16SUBPASSINPUT;
739     (*KeywordMap)["f16subpassInputMS"] =            F16SUBPASSINPUTMS;
740     (*KeywordMap)["__explicitInterpAMD"] =     EXPLICITINTERPAMD;
741     (*KeywordMap)["pervertexNV"] =             PERVERTEXNV;
742     (*KeywordMap)["pervertexEXT"] =            PERVERTEXEXT;
743     (*KeywordMap)["precise"] =                 PRECISE;
744
745     (*KeywordMap)["rayPayloadNV"] =            PAYLOADNV;
746     (*KeywordMap)["rayPayloadEXT"] =           PAYLOADEXT;
747     (*KeywordMap)["rayPayloadInNV"] =          PAYLOADINNV;
748     (*KeywordMap)["rayPayloadInEXT"] =         PAYLOADINEXT;
749     (*KeywordMap)["hitAttributeNV"] =          HITATTRNV;
750     (*KeywordMap)["hitAttributeEXT"] =         HITATTREXT;
751     (*KeywordMap)["callableDataNV"] =          CALLDATANV;
752     (*KeywordMap)["callableDataEXT"] =         CALLDATAEXT;
753     (*KeywordMap)["callableDataInNV"] =        CALLDATAINNV;
754     (*KeywordMap)["callableDataInEXT"] =       CALLDATAINEXT;
755     (*KeywordMap)["accelerationStructureNV"] = ACCSTRUCTNV;
756     (*KeywordMap)["accelerationStructureEXT"]   = ACCSTRUCTEXT;
757     (*KeywordMap)["rayQueryEXT"] =              RAYQUERYEXT;
758     (*KeywordMap)["perprimitiveNV"] =          PERPRIMITIVENV;
759     (*KeywordMap)["perviewNV"] =               PERVIEWNV;
760     (*KeywordMap)["taskNV"] =                  PERTASKNV;
761     (*KeywordMap)["perprimitiveEXT"] =         PERPRIMITIVEEXT;
762     (*KeywordMap)["taskPayloadSharedEXT"] =    TASKPAYLOADWORKGROUPEXT;
763
764     (*KeywordMap)["fcoopmatNV"] =              FCOOPMATNV;
765     (*KeywordMap)["icoopmatNV"] =              ICOOPMATNV;
766     (*KeywordMap)["ucoopmatNV"] =              UCOOPMATNV;
767
768     (*KeywordMap)["hitObjectNV"] =             HITOBJECTNV;
769     (*KeywordMap)["hitObjectAttributeNV"] =    HITOBJECTATTRNV;
770
771     ReservedSet = new std::unordered_set<const char*, str_hash, str_eq>;
772
773     ReservedSet->insert("common");
774     ReservedSet->insert("partition");
775     ReservedSet->insert("active");
776     ReservedSet->insert("asm");
777     ReservedSet->insert("class");
778     ReservedSet->insert("union");
779     ReservedSet->insert("enum");
780     ReservedSet->insert("typedef");
781     ReservedSet->insert("template");
782     ReservedSet->insert("this");
783     ReservedSet->insert("goto");
784     ReservedSet->insert("inline");
785     ReservedSet->insert("noinline");
786     ReservedSet->insert("public");
787     ReservedSet->insert("static");
788     ReservedSet->insert("extern");
789     ReservedSet->insert("external");
790     ReservedSet->insert("interface");
791     ReservedSet->insert("long");
792     ReservedSet->insert("short");
793     ReservedSet->insert("half");
794     ReservedSet->insert("fixed");
795     ReservedSet->insert("unsigned");
796     ReservedSet->insert("input");
797     ReservedSet->insert("output");
798     ReservedSet->insert("hvec2");
799     ReservedSet->insert("hvec3");
800     ReservedSet->insert("hvec4");
801     ReservedSet->insert("fvec2");
802     ReservedSet->insert("fvec3");
803     ReservedSet->insert("fvec4");
804     ReservedSet->insert("sampler3DRect");
805     ReservedSet->insert("filter");
806     ReservedSet->insert("sizeof");
807     ReservedSet->insert("cast");
808     ReservedSet->insert("namespace");
809     ReservedSet->insert("using");
810 #endif
811 }
812
813 void TScanContext::deleteKeywordMap()
814 {
815     delete KeywordMap;
816     KeywordMap = nullptr;
817 #ifndef GLSLANG_WEB
818     delete ReservedSet;
819     ReservedSet = nullptr;
820 #endif
821 }
822
823 // Called by yylex to get the next token.
824 // Returning 0 implies end of input.
825 int TScanContext::tokenize(TPpContext* pp, TParserToken& token)
826 {
827     do {
828         parserToken = &token;
829         TPpToken ppToken;
830         int token = pp->tokenize(ppToken);
831         if (token == EndOfInput)
832             return 0;
833
834         tokenText = ppToken.name;
835         loc = ppToken.loc;
836         parserToken->sType.lex.loc = loc;
837         switch (token) {
838         case ';':  afterType = false; afterBuffer = false; return SEMICOLON;
839         case ',':  afterType = false;   return COMMA;
840         case ':':                       return COLON;
841         case '=':  afterType = false;   return EQUAL;
842         case '(':  afterType = false;   return LEFT_PAREN;
843         case ')':  afterType = false;   return RIGHT_PAREN;
844         case '.':  field = true;        return DOT;
845         case '!':                       return BANG;
846         case '-':                       return DASH;
847         case '~':                       return TILDE;
848         case '+':                       return PLUS;
849         case '*':                       return STAR;
850         case '/':                       return SLASH;
851         case '%':                       return PERCENT;
852         case '<':                       return LEFT_ANGLE;
853         case '>':                       return RIGHT_ANGLE;
854         case '|':                       return VERTICAL_BAR;
855         case '^':                       return CARET;
856         case '&':                       return AMPERSAND;
857         case '?':                       return QUESTION;
858         case '[':                       return LEFT_BRACKET;
859         case ']':                       return RIGHT_BRACKET;
860         case '{':  afterStruct = false; afterBuffer = false; return LEFT_BRACE;
861         case '}':                       return RIGHT_BRACE;
862         case '\\':
863             parseContext.error(loc, "illegal use of escape character", "\\", "");
864             break;
865
866         case PPAtomAddAssign:          return ADD_ASSIGN;
867         case PPAtomSubAssign:          return SUB_ASSIGN;
868         case PPAtomMulAssign:          return MUL_ASSIGN;
869         case PPAtomDivAssign:          return DIV_ASSIGN;
870         case PPAtomModAssign:          return MOD_ASSIGN;
871
872         case PpAtomRight:              return RIGHT_OP;
873         case PpAtomLeft:               return LEFT_OP;
874
875         case PpAtomRightAssign:        return RIGHT_ASSIGN;
876         case PpAtomLeftAssign:         return LEFT_ASSIGN;
877         case PpAtomAndAssign:          return AND_ASSIGN;
878         case PpAtomOrAssign:           return OR_ASSIGN;
879         case PpAtomXorAssign:          return XOR_ASSIGN;
880
881         case PpAtomAnd:                return AND_OP;
882         case PpAtomOr:                 return OR_OP;
883         case PpAtomXor:                return XOR_OP;
884
885         case PpAtomEQ:                 return EQ_OP;
886         case PpAtomGE:                 return GE_OP;
887         case PpAtomNE:                 return NE_OP;
888         case PpAtomLE:                 return LE_OP;
889
890         case PpAtomDecrement:          return DEC_OP;
891         case PpAtomIncrement:          return INC_OP;
892
893         case PpAtomColonColon:
894             parseContext.error(loc, "not supported", "::", "");
895             break;
896
897         case PpAtomConstString:        parserToken->sType.lex.string = NewPoolTString(tokenText);     return STRING_LITERAL;
898         case PpAtomConstInt:           parserToken->sType.lex.i    = ppToken.ival;       return INTCONSTANT;
899         case PpAtomConstUint:          parserToken->sType.lex.i    = ppToken.ival;       return UINTCONSTANT;
900         case PpAtomConstFloat:         parserToken->sType.lex.d    = ppToken.dval;       return FLOATCONSTANT;
901 #ifndef GLSLANG_WEB
902         case PpAtomConstInt16:         parserToken->sType.lex.i    = ppToken.ival;       return INT16CONSTANT;
903         case PpAtomConstUint16:        parserToken->sType.lex.i    = ppToken.ival;       return UINT16CONSTANT;
904         case PpAtomConstInt64:         parserToken->sType.lex.i64  = ppToken.i64val;     return INT64CONSTANT;
905         case PpAtomConstUint64:        parserToken->sType.lex.i64  = ppToken.i64val;     return UINT64CONSTANT;
906         case PpAtomConstDouble:        parserToken->sType.lex.d    = ppToken.dval;       return DOUBLECONSTANT;
907         case PpAtomConstFloat16:       parserToken->sType.lex.d    = ppToken.dval;       return FLOAT16CONSTANT;
908 #endif
909         case PpAtomIdentifier:
910         {
911             int token = tokenizeIdentifier();
912             field = false;
913             return token;
914         }
915
916         case EndOfInput:               return 0;
917
918         default:
919             char buf[2];
920             buf[0] = (char)token;
921             buf[1] = 0;
922             parseContext.error(loc, "unexpected token", buf, "");
923             break;
924         }
925     } while (true);
926 }
927
928 int TScanContext::tokenizeIdentifier()
929 {
930 #ifndef GLSLANG_WEB
931     if (ReservedSet->find(tokenText) != ReservedSet->end())
932         return reservedWord();
933 #endif
934
935     auto it = KeywordMap->find(tokenText);
936     if (it == KeywordMap->end()) {
937         // Should have an identifier of some sort
938         return identifierOrType();
939     }
940     keyword = it->second;
941
942     switch (keyword) {
943     case CONST:
944     case UNIFORM:
945     case IN:
946     case OUT:
947     case INOUT:
948     case BREAK:
949     case CONTINUE:
950     case DO:
951     case FOR:
952     case WHILE:
953     case IF:
954     case ELSE:
955     case DISCARD:
956     case RETURN:
957     case CASE:
958         return keyword;
959
960     case TERMINATE_INVOCATION:
961         if (!parseContext.extensionTurnedOn(E_GL_EXT_terminate_invocation))
962             return identifierOrType();
963         return keyword;
964
965     case TERMINATE_RAY:
966     case IGNORE_INTERSECTION:
967         if (!parseContext.extensionTurnedOn(E_GL_EXT_ray_tracing))
968             return identifierOrType();
969         return keyword;
970
971     case BUFFER:
972         afterBuffer = true;
973         if ((parseContext.isEsProfile() && parseContext.version < 310) ||
974             (!parseContext.isEsProfile() && (parseContext.version < 430 &&
975             !parseContext.extensionTurnedOn(E_GL_ARB_shader_storage_buffer_object))))
976             return identifierOrType();
977         return keyword;
978
979     case STRUCT:
980         afterStruct = true;
981         return keyword;
982
983     case SWITCH:
984     case DEFAULT:
985         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
986             (!parseContext.isEsProfile() && parseContext.version < 130))
987             reservedWord();
988         return keyword;
989
990     case VOID:
991     case BOOL:
992     case FLOAT:
993     case INT:
994     case BVEC2:
995     case BVEC3:
996     case BVEC4:
997     case VEC2:
998     case VEC3:
999     case VEC4:
1000     case IVEC2:
1001     case IVEC3:
1002     case IVEC4:
1003     case MAT2:
1004     case MAT3:
1005     case MAT4:
1006     case SAMPLER2D:
1007     case SAMPLERCUBE:
1008         afterType = true;
1009         return keyword;
1010
1011     case BOOLCONSTANT:
1012         if (strcmp("true", tokenText) == 0)
1013             parserToken->sType.lex.b = true;
1014         else
1015             parserToken->sType.lex.b = false;
1016         return keyword;
1017
1018     case SMOOTH:
1019         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1020             (!parseContext.isEsProfile() && parseContext.version < 130))
1021             return identifierOrType();
1022         return keyword;
1023     case FLAT:
1024         if (parseContext.isEsProfile() && parseContext.version < 300)
1025             reservedWord();
1026         else if (!parseContext.isEsProfile() && parseContext.version < 130)
1027             return identifierOrType();
1028         return keyword;
1029     case CENTROID:
1030         if (parseContext.version < 120)
1031             return identifierOrType();
1032         return keyword;
1033     case INVARIANT:
1034         if (!parseContext.isEsProfile() && parseContext.version < 120)
1035             return identifierOrType();
1036         return keyword;
1037     case PACKED:
1038         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1039             (!parseContext.isEsProfile() && parseContext.version < 140))
1040             return reservedWord();
1041         return identifierOrType();
1042
1043     case RESOURCE:
1044     {
1045         bool reserved = (parseContext.isEsProfile() && parseContext.version >= 300) ||
1046                         (!parseContext.isEsProfile() && parseContext.version >= 420);
1047         return identifierOrReserved(reserved);
1048     }
1049     case SUPERP:
1050     {
1051         bool reserved = parseContext.isEsProfile() || parseContext.version >= 130;
1052         return identifierOrReserved(reserved);
1053     }
1054
1055 #ifndef GLSLANG_WEB
1056     case NOPERSPECTIVE:
1057         if (parseContext.extensionTurnedOn(E_GL_NV_shader_noperspective_interpolation))
1058             return keyword;
1059         return es30ReservedFromGLSL(130);
1060
1061     case NONUNIFORM:
1062         if (parseContext.extensionTurnedOn(E_GL_EXT_nonuniform_qualifier))
1063             return keyword;
1064         else
1065             return identifierOrType();
1066     case ATTRIBUTE:
1067     case VARYING:
1068         if (parseContext.isEsProfile() && parseContext.version >= 300)
1069             reservedWord();
1070         return keyword;
1071     case PAYLOADNV:
1072     case PAYLOADINNV:
1073     case HITATTRNV:
1074     case CALLDATANV:
1075     case CALLDATAINNV:
1076     case ACCSTRUCTNV:
1077         if (parseContext.symbolTable.atBuiltInLevel() ||
1078             parseContext.extensionTurnedOn(E_GL_NV_ray_tracing))
1079             return keyword;
1080         return identifierOrType();
1081     case PAYLOADEXT:
1082     case PAYLOADINEXT:
1083     case HITATTREXT:
1084     case CALLDATAEXT:
1085     case CALLDATAINEXT:
1086     case ACCSTRUCTEXT:
1087         if (parseContext.symbolTable.atBuiltInLevel() ||
1088             parseContext.extensionTurnedOn(E_GL_EXT_ray_tracing) ||
1089             parseContext.extensionTurnedOn(E_GL_EXT_ray_query))
1090             return keyword;
1091         return identifierOrType();
1092     case RAYQUERYEXT:
1093         if (parseContext.symbolTable.atBuiltInLevel() ||
1094             (!parseContext.isEsProfile() && parseContext.version >= 460
1095                  && parseContext.extensionTurnedOn(E_GL_EXT_ray_query)))
1096             return keyword;
1097         return identifierOrType();
1098     case ATOMIC_UINT:
1099         if ((parseContext.isEsProfile() && parseContext.version >= 310) ||
1100             parseContext.extensionTurnedOn(E_GL_ARB_shader_atomic_counters))
1101             return keyword;
1102         return es30ReservedFromGLSL(420);
1103
1104     case COHERENT:
1105     case DEVICECOHERENT:
1106     case QUEUEFAMILYCOHERENT:
1107     case WORKGROUPCOHERENT:
1108     case SUBGROUPCOHERENT:
1109     case SHADERCALLCOHERENT:
1110     case NONPRIVATE:
1111     case RESTRICT:
1112     case READONLY:
1113     case WRITEONLY:
1114         if (parseContext.isEsProfile() && parseContext.version >= 310)
1115             return keyword;
1116         return es30ReservedFromGLSL(parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store) ? 130 : 420);
1117     case VOLATILE:
1118         if (parseContext.isEsProfile() && parseContext.version >= 310)
1119             return keyword;
1120         if (! parseContext.symbolTable.atBuiltInLevel() && (parseContext.isEsProfile() ||
1121             (parseContext.version < 420 && ! parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
1122             reservedWord();
1123         return keyword;
1124     case PATCH:
1125         if (parseContext.symbolTable.atBuiltInLevel() ||
1126             (parseContext.isEsProfile() &&
1127              (parseContext.version >= 320 ||
1128               parseContext.extensionsTurnedOn(Num_AEP_tessellation_shader, AEP_tessellation_shader))) ||
1129             (!parseContext.isEsProfile() && parseContext.extensionTurnedOn(E_GL_ARB_tessellation_shader)))
1130             return keyword;
1131
1132         return es30ReservedFromGLSL(400);
1133
1134     case SAMPLE:
1135         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1136             parseContext.extensionsTurnedOn(1, &E_GL_OES_shader_multisample_interpolation))
1137             return keyword;
1138         return es30ReservedFromGLSL(400);
1139
1140     case SUBROUTINE:
1141         return es30ReservedFromGLSL(400);
1142 #endif
1143     case SHARED:
1144         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1145             (!parseContext.isEsProfile() && parseContext.version < 140))
1146             return identifierOrType();
1147         return keyword;
1148     case LAYOUT:
1149     {
1150         const int numLayoutExts = 2;
1151         const char* layoutExts[numLayoutExts] = { E_GL_ARB_shading_language_420pack,
1152                                                   E_GL_ARB_explicit_attrib_location };
1153         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1154             (!parseContext.isEsProfile() && parseContext.version < 140 &&
1155             ! parseContext.extensionsTurnedOn(numLayoutExts, layoutExts)))
1156             return identifierOrType();
1157         return keyword;
1158     }
1159
1160     case HIGH_PRECISION:
1161     case MEDIUM_PRECISION:
1162     case LOW_PRECISION:
1163     case PRECISION:
1164         return precisionKeyword();
1165
1166     case MAT2X2:
1167     case MAT2X3:
1168     case MAT2X4:
1169     case MAT3X2:
1170     case MAT3X3:
1171     case MAT3X4:
1172     case MAT4X2:
1173     case MAT4X3:
1174     case MAT4X4:
1175         return matNxM();
1176
1177 #ifndef GLSLANG_WEB
1178     case DMAT2:
1179     case DMAT3:
1180     case DMAT4:
1181     case DMAT2X2:
1182     case DMAT2X3:
1183     case DMAT2X4:
1184     case DMAT3X2:
1185     case DMAT3X3:
1186     case DMAT3X4:
1187     case DMAT4X2:
1188     case DMAT4X3:
1189     case DMAT4X4:
1190         return dMat();
1191
1192     case IMAGE1D:
1193     case IIMAGE1D:
1194     case UIMAGE1D:
1195     case IMAGE1DARRAY:
1196     case IIMAGE1DARRAY:
1197     case UIMAGE1DARRAY:
1198     case IMAGE2DRECT:
1199     case IIMAGE2DRECT:
1200     case UIMAGE2DRECT:
1201         afterType = true;
1202         return firstGenerationImage(false);
1203
1204     case I64IMAGE1D:
1205     case U64IMAGE1D:
1206     case I64IMAGE1DARRAY:
1207     case U64IMAGE1DARRAY:
1208     case I64IMAGE2DRECT:
1209     case U64IMAGE2DRECT:
1210         afterType = true;
1211         if (parseContext.symbolTable.atBuiltInLevel() ||
1212             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1213             return firstGenerationImage(false);
1214         }
1215         return identifierOrType();
1216
1217     case IMAGEBUFFER:
1218     case IIMAGEBUFFER:
1219     case UIMAGEBUFFER:
1220         afterType = true;
1221         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1222             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1223             return keyword;
1224         return firstGenerationImage(false);
1225         
1226     case I64IMAGEBUFFER:
1227     case U64IMAGEBUFFER:
1228         afterType = true;        
1229         if (parseContext.symbolTable.atBuiltInLevel() ||
1230             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1231             if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1232                 parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1233                 return keyword;
1234             return firstGenerationImage(false);
1235         }
1236         return identifierOrType();
1237
1238     case IMAGE2D:
1239     case IIMAGE2D:
1240     case UIMAGE2D:
1241     case IMAGE3D:
1242     case IIMAGE3D:
1243     case UIMAGE3D:
1244     case IMAGECUBE:
1245     case IIMAGECUBE:
1246     case UIMAGECUBE:
1247     case IMAGE2DARRAY:
1248     case IIMAGE2DARRAY:
1249     case UIMAGE2DARRAY:
1250         afterType = true;
1251         return firstGenerationImage(true);
1252
1253     case I64IMAGE2D:
1254     case U64IMAGE2D:
1255     case I64IMAGE3D:
1256     case U64IMAGE3D:
1257     case I64IMAGECUBE:
1258     case U64IMAGECUBE:
1259     case I64IMAGE2DARRAY:
1260     case U64IMAGE2DARRAY:
1261         afterType = true;
1262         if (parseContext.symbolTable.atBuiltInLevel() ||
1263             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64))
1264             return firstGenerationImage(true);
1265         return identifierOrType();
1266         
1267     case IMAGECUBEARRAY:
1268     case IIMAGECUBEARRAY:
1269     case UIMAGECUBEARRAY:
1270         afterType = true;
1271         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1272             parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1273             return keyword;
1274         return secondGenerationImage();
1275         
1276     case I64IMAGECUBEARRAY:
1277     case U64IMAGECUBEARRAY:
1278         afterType = true;
1279         if (parseContext.symbolTable.atBuiltInLevel() ||
1280             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1281             if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1282                 parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1283                 return keyword;
1284             return secondGenerationImage();
1285         }
1286         return identifierOrType();
1287
1288     case IMAGE2DMS:
1289     case IIMAGE2DMS:
1290     case UIMAGE2DMS:
1291     case IMAGE2DMSARRAY:
1292     case IIMAGE2DMSARRAY:
1293     case UIMAGE2DMSARRAY:
1294         afterType = true;
1295         return secondGenerationImage();
1296         
1297     case I64IMAGE2DMS:
1298     case U64IMAGE2DMS:
1299     case I64IMAGE2DMSARRAY:
1300     case U64IMAGE2DMSARRAY:
1301         afterType = true;
1302         if (parseContext.symbolTable.atBuiltInLevel() ||
1303             parseContext.extensionTurnedOn(E_GL_EXT_shader_image_int64)) {
1304             return secondGenerationImage();
1305         }
1306         return identifierOrType();
1307
1308     case DOUBLE:
1309     case DVEC2:
1310     case DVEC3:
1311     case DVEC4:
1312         afterType = true;
1313         if (parseContext.isEsProfile() || parseContext.version < 150 ||
1314             (!parseContext.symbolTable.atBuiltInLevel() &&
1315               (parseContext.version < 400 && !parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_fp64) &&
1316               (parseContext.version < 410 && !parseContext.extensionTurnedOn(E_GL_ARB_vertex_attrib_64bit)))))
1317             reservedWord();
1318         return keyword;
1319
1320     case INT64_T:
1321     case UINT64_T:
1322     case I64VEC2:
1323     case I64VEC3:
1324     case I64VEC4:
1325     case U64VEC2:
1326     case U64VEC3:
1327     case U64VEC4:
1328         afterType = true;
1329         if (parseContext.symbolTable.atBuiltInLevel() ||
1330             parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_int64) ||
1331             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1332             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int64))
1333             return keyword;
1334         return identifierOrType();
1335
1336     case INT8_T:
1337     case UINT8_T:
1338     case I8VEC2:
1339     case I8VEC3:
1340     case I8VEC4:
1341     case U8VEC2:
1342     case U8VEC3:
1343     case U8VEC4:
1344         afterType = true;
1345         if (parseContext.symbolTable.atBuiltInLevel() ||
1346             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1347             parseContext.extensionTurnedOn(E_GL_EXT_shader_8bit_storage) ||
1348             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int8))
1349             return keyword;
1350         return identifierOrType();
1351
1352     case INT16_T:
1353     case UINT16_T:
1354     case I16VEC2:
1355     case I16VEC3:
1356     case I16VEC4:
1357     case U16VEC2:
1358     case U16VEC3:
1359     case U16VEC4:
1360         afterType = true;
1361         if (parseContext.symbolTable.atBuiltInLevel() ||
1362             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_int16) ||
1363             parseContext.extensionTurnedOn(E_GL_EXT_shader_16bit_storage) ||
1364             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1365             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int16))
1366             return keyword;
1367         return identifierOrType();
1368     case INT32_T:
1369     case UINT32_T:
1370     case I32VEC2:
1371     case I32VEC3:
1372     case I32VEC4:
1373     case U32VEC2:
1374     case U32VEC3:
1375     case U32VEC4:
1376         afterType = true;
1377         if (parseContext.symbolTable.atBuiltInLevel() ||
1378             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1379             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int32))
1380             return keyword;
1381         return identifierOrType();
1382     case FLOAT32_T:
1383     case F32VEC2:
1384     case F32VEC3:
1385     case F32VEC4:
1386     case F32MAT2:
1387     case F32MAT3:
1388     case F32MAT4:
1389     case F32MAT2X2:
1390     case F32MAT2X3:
1391     case F32MAT2X4:
1392     case F32MAT3X2:
1393     case F32MAT3X3:
1394     case F32MAT3X4:
1395     case F32MAT4X2:
1396     case F32MAT4X3:
1397     case F32MAT4X4:
1398         afterType = true;
1399         if (parseContext.symbolTable.atBuiltInLevel() ||
1400             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1401             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float32))
1402             return keyword;
1403         return identifierOrType();
1404
1405     case FLOAT64_T:
1406     case F64VEC2:
1407     case F64VEC3:
1408     case F64VEC4:
1409     case F64MAT2:
1410     case F64MAT3:
1411     case F64MAT4:
1412     case F64MAT2X2:
1413     case F64MAT2X3:
1414     case F64MAT2X4:
1415     case F64MAT3X2:
1416     case F64MAT3X3:
1417     case F64MAT3X4:
1418     case F64MAT4X2:
1419     case F64MAT4X3:
1420     case F64MAT4X4:
1421         afterType = true;
1422         if (parseContext.symbolTable.atBuiltInLevel() ||
1423             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1424             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float64))
1425             return keyword;
1426         return identifierOrType();
1427
1428     case FLOAT16_T:
1429     case F16VEC2:
1430     case F16VEC3:
1431     case F16VEC4:
1432         afterType = true;
1433         if (parseContext.symbolTable.atBuiltInLevel() ||
1434             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float) ||
1435             parseContext.extensionTurnedOn(E_GL_EXT_shader_16bit_storage) ||
1436             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1437             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float16))
1438             return keyword;
1439
1440         return identifierOrType();
1441
1442     case F16MAT2:
1443     case F16MAT3:
1444     case F16MAT4:
1445     case F16MAT2X2:
1446     case F16MAT2X3:
1447     case F16MAT2X4:
1448     case F16MAT3X2:
1449     case F16MAT3X3:
1450     case F16MAT3X4:
1451     case F16MAT4X2:
1452     case F16MAT4X3:
1453     case F16MAT4X4:
1454         afterType = true;
1455         if (parseContext.symbolTable.atBuiltInLevel() ||
1456             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float) ||
1457             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1458             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float16))
1459             return keyword;
1460
1461         return identifierOrType();
1462
1463     case SAMPLERCUBEARRAY:
1464     case SAMPLERCUBEARRAYSHADOW:
1465     case ISAMPLERCUBEARRAY:
1466     case USAMPLERCUBEARRAY:
1467         afterType = true;
1468         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1469             parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1470             return keyword;
1471         if (parseContext.isEsProfile() || (parseContext.version < 400 && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_cube_map_array)))
1472             reservedWord();
1473         return keyword;
1474
1475     case TEXTURECUBEARRAY:
1476     case ITEXTURECUBEARRAY:
1477     case UTEXTURECUBEARRAY:
1478         if (parseContext.spvVersion.vulkan > 0)
1479             return keyword;
1480         else
1481             return identifierOrType();
1482 #endif
1483
1484     case UINT:
1485     case UVEC2:
1486     case UVEC3:
1487     case UVEC4:
1488     case SAMPLERCUBESHADOW:
1489     case SAMPLER2DARRAY:
1490     case SAMPLER2DARRAYSHADOW:
1491     case ISAMPLER2D:
1492     case ISAMPLER3D:
1493     case ISAMPLERCUBE:
1494     case ISAMPLER2DARRAY:
1495     case USAMPLER2D:
1496     case USAMPLER3D:
1497     case USAMPLERCUBE:
1498     case USAMPLER2DARRAY:
1499         afterType = true;
1500         return nonreservedKeyword(300, 130);
1501
1502     case SAMPLER3D:
1503         afterType = true;
1504         if (parseContext.isEsProfile() && parseContext.version < 300) {
1505             if (!parseContext.extensionTurnedOn(E_GL_OES_texture_3D))
1506                 reservedWord();
1507         }
1508         return keyword;
1509
1510     case SAMPLER2DSHADOW:
1511         afterType = true;
1512         if (parseContext.isEsProfile() && parseContext.version < 300) {
1513             if (!parseContext.extensionTurnedOn(E_GL_EXT_shadow_samplers))
1514                 reservedWord();
1515         }
1516         return keyword;
1517
1518     case TEXTURE2D:
1519     case TEXTURECUBE:
1520     case TEXTURE2DARRAY:
1521     case ITEXTURE2D:
1522     case ITEXTURE3D:
1523     case ITEXTURECUBE:
1524     case ITEXTURE2DARRAY:
1525     case UTEXTURE2D:
1526     case UTEXTURE3D:
1527     case UTEXTURECUBE:
1528     case UTEXTURE2DARRAY:
1529     case TEXTURE3D:
1530     case SAMPLER:
1531     case SAMPLERSHADOW:
1532         if (parseContext.spvVersion.vulkan > 0)
1533             return keyword;
1534         else
1535             return identifierOrType();
1536
1537 #ifndef GLSLANG_WEB
1538     case ISAMPLER1D:
1539     case ISAMPLER1DARRAY:
1540     case SAMPLER1DARRAYSHADOW:
1541     case USAMPLER1D:
1542     case USAMPLER1DARRAY:
1543         afterType = true;
1544         return es30ReservedFromGLSL(130);
1545     case ISAMPLER2DRECT:
1546     case USAMPLER2DRECT:
1547         afterType = true;
1548         return es30ReservedFromGLSL(140);
1549
1550     case SAMPLERBUFFER:
1551         afterType = true;
1552         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1553             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1554             return keyword;
1555         return es30ReservedFromGLSL(130);
1556
1557     case ISAMPLERBUFFER:
1558     case USAMPLERBUFFER:
1559         afterType = true;
1560         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1561             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1562             return keyword;
1563         return es30ReservedFromGLSL(140);
1564
1565     case SAMPLER2DMS:
1566     case ISAMPLER2DMS:
1567     case USAMPLER2DMS:
1568         afterType = true;
1569         if (parseContext.isEsProfile() && parseContext.version >= 310)
1570             return keyword;
1571         if (!parseContext.isEsProfile() && (parseContext.version > 140 ||
1572             (parseContext.version == 140 && parseContext.extensionsTurnedOn(1, &E_GL_ARB_texture_multisample))))
1573             return keyword;
1574         return es30ReservedFromGLSL(150);
1575
1576     case SAMPLER2DMSARRAY:
1577     case ISAMPLER2DMSARRAY:
1578     case USAMPLER2DMSARRAY:
1579         afterType = true;
1580         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1581             parseContext.extensionsTurnedOn(1, &E_GL_OES_texture_storage_multisample_2d_array))
1582             return keyword;
1583         if (!parseContext.isEsProfile() && (parseContext.version > 140 ||
1584             (parseContext.version == 140 && parseContext.extensionsTurnedOn(1, &E_GL_ARB_texture_multisample))))
1585             return keyword;
1586         return es30ReservedFromGLSL(150);
1587
1588     case SAMPLER1D:
1589     case SAMPLER1DSHADOW:
1590         afterType = true;
1591         if (parseContext.isEsProfile())
1592             reservedWord();
1593         return keyword;
1594
1595     case SAMPLER2DRECT:
1596     case SAMPLER2DRECTSHADOW:
1597         afterType = true;
1598         if (parseContext.isEsProfile())
1599             reservedWord();
1600         else if (parseContext.version < 140 && ! parseContext.symbolTable.atBuiltInLevel() && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_rectangle)) {
1601             if (parseContext.relaxedErrors())
1602                 parseContext.requireExtensions(loc, 1, &E_GL_ARB_texture_rectangle, "texture-rectangle sampler keyword");
1603             else
1604                 reservedWord();
1605         }
1606         return keyword;
1607
1608     case SAMPLER1DARRAY:
1609         afterType = true;
1610         if (parseContext.isEsProfile() && parseContext.version == 300)
1611             reservedWord();
1612         else if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1613                  (!parseContext.isEsProfile() && parseContext.version < 130))
1614             return identifierOrType();
1615         return keyword;
1616
1617     case SAMPLEREXTERNALOES:
1618         afterType = true;
1619         if (parseContext.symbolTable.atBuiltInLevel() ||
1620             parseContext.extensionTurnedOn(E_GL_OES_EGL_image_external) ||
1621             parseContext.extensionTurnedOn(E_GL_OES_EGL_image_external_essl3))
1622             return keyword;
1623         return identifierOrType();
1624
1625     case SAMPLEREXTERNAL2DY2YEXT:
1626         afterType = true;
1627         if (parseContext.symbolTable.atBuiltInLevel() ||
1628             parseContext.extensionTurnedOn(E_GL_EXT_YUV_target))
1629             return keyword;
1630         return identifierOrType();
1631
1632     case ITEXTURE1DARRAY:
1633     case UTEXTURE1D:
1634     case ITEXTURE1D:
1635     case UTEXTURE1DARRAY:
1636     case TEXTUREBUFFER:
1637     case ITEXTURE2DRECT:
1638     case UTEXTURE2DRECT:
1639     case ITEXTUREBUFFER:
1640     case UTEXTUREBUFFER:
1641     case TEXTURE2DMS:
1642     case ITEXTURE2DMS:
1643     case UTEXTURE2DMS:
1644     case TEXTURE2DMSARRAY:
1645     case ITEXTURE2DMSARRAY:
1646     case UTEXTURE2DMSARRAY:
1647     case TEXTURE1D:
1648     case TEXTURE2DRECT:
1649     case TEXTURE1DARRAY:
1650         if (parseContext.spvVersion.vulkan > 0)
1651             return keyword;
1652         else
1653             return identifierOrType();
1654
1655     case SUBPASSINPUT:
1656     case SUBPASSINPUTMS:
1657     case ISUBPASSINPUT:
1658     case ISUBPASSINPUTMS:
1659     case USUBPASSINPUT:
1660     case USUBPASSINPUTMS:
1661         if (parseContext.spvVersion.vulkan > 0)
1662             return keyword;
1663         else
1664             return identifierOrType();
1665
1666     case F16SAMPLER1D:
1667     case F16SAMPLER2D:
1668     case F16SAMPLER3D:
1669     case F16SAMPLER2DRECT:
1670     case F16SAMPLERCUBE:
1671     case F16SAMPLER1DARRAY:
1672     case F16SAMPLER2DARRAY:
1673     case F16SAMPLERCUBEARRAY:
1674     case F16SAMPLERBUFFER:
1675     case F16SAMPLER2DMS:
1676     case F16SAMPLER2DMSARRAY:
1677     case F16SAMPLER1DSHADOW:
1678     case F16SAMPLER2DSHADOW:
1679     case F16SAMPLER1DARRAYSHADOW:
1680     case F16SAMPLER2DARRAYSHADOW:
1681     case F16SAMPLER2DRECTSHADOW:
1682     case F16SAMPLERCUBESHADOW:
1683     case F16SAMPLERCUBEARRAYSHADOW:
1684
1685     case F16IMAGE1D:
1686     case F16IMAGE2D:
1687     case F16IMAGE3D:
1688     case F16IMAGE2DRECT:
1689     case F16IMAGECUBE:
1690     case F16IMAGE1DARRAY:
1691     case F16IMAGE2DARRAY:
1692     case F16IMAGECUBEARRAY:
1693     case F16IMAGEBUFFER:
1694     case F16IMAGE2DMS:
1695     case F16IMAGE2DMSARRAY:
1696
1697     case F16TEXTURE1D:
1698     case F16TEXTURE2D:
1699     case F16TEXTURE3D:
1700     case F16TEXTURE2DRECT:
1701     case F16TEXTURECUBE:
1702     case F16TEXTURE1DARRAY:
1703     case F16TEXTURE2DARRAY:
1704     case F16TEXTURECUBEARRAY:
1705     case F16TEXTUREBUFFER:
1706     case F16TEXTURE2DMS:
1707     case F16TEXTURE2DMSARRAY:
1708
1709     case F16SUBPASSINPUT:
1710     case F16SUBPASSINPUTMS:
1711         afterType = true;
1712         if (parseContext.symbolTable.atBuiltInLevel() ||
1713             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float_fetch))
1714             return keyword;
1715         return identifierOrType();
1716
1717     case EXPLICITINTERPAMD:
1718         if (parseContext.extensionTurnedOn(E_GL_AMD_shader_explicit_vertex_parameter))
1719             return keyword;
1720         return identifierOrType();
1721
1722     case PERVERTEXNV:
1723         if ((!parseContext.isEsProfile() && parseContext.version >= 450) ||
1724             parseContext.extensionTurnedOn(E_GL_NV_fragment_shader_barycentric))
1725             return keyword;
1726         return identifierOrType();
1727
1728     case PERVERTEXEXT:
1729         if ((!parseContext.isEsProfile() && parseContext.version >= 450) ||
1730             parseContext.extensionTurnedOn(E_GL_EXT_fragment_shader_barycentric))
1731             return keyword;
1732         return identifierOrType();
1733
1734     case PRECISE:
1735         if ((parseContext.isEsProfile() &&
1736              (parseContext.version >= 320 || parseContext.extensionsTurnedOn(Num_AEP_gpu_shader5, AEP_gpu_shader5))) ||
1737             (!parseContext.isEsProfile() && parseContext.version >= 400))
1738             return keyword;
1739         if (parseContext.isEsProfile() && parseContext.version == 310) {
1740             reservedWord();
1741             return keyword;
1742         }
1743         return identifierOrType();
1744
1745     case PERPRIMITIVENV:
1746     case PERVIEWNV:
1747     case PERTASKNV:
1748         if (parseContext.symbolTable.atBuiltInLevel() ||
1749             parseContext.extensionTurnedOn(E_GL_NV_mesh_shader))
1750             return keyword;
1751         return identifierOrType();
1752
1753     case PERPRIMITIVEEXT:
1754     case TASKPAYLOADWORKGROUPEXT:
1755         if (parseContext.symbolTable.atBuiltInLevel() ||
1756             parseContext.extensionTurnedOn(E_GL_EXT_mesh_shader))
1757             return keyword;
1758         return identifierOrType();
1759
1760     case FCOOPMATNV:
1761         afterType = true;
1762         if (parseContext.symbolTable.atBuiltInLevel() ||
1763             parseContext.extensionTurnedOn(E_GL_NV_cooperative_matrix))
1764             return keyword;
1765         return identifierOrType();
1766
1767     case UCOOPMATNV:
1768     case ICOOPMATNV:
1769         afterType = true;
1770         if (parseContext.symbolTable.atBuiltInLevel() ||
1771             parseContext.extensionTurnedOn(E_GL_NV_integer_cooperative_matrix))
1772             return keyword;
1773         return identifierOrType();
1774
1775     case DEMOTE:
1776         if (parseContext.extensionTurnedOn(E_GL_EXT_demote_to_helper_invocation))
1777             return keyword;
1778         else
1779             return identifierOrType();
1780
1781     case SPIRV_INSTRUCTION:
1782     case SPIRV_EXECUTION_MODE:
1783     case SPIRV_EXECUTION_MODE_ID:
1784     case SPIRV_DECORATE:
1785     case SPIRV_DECORATE_ID:
1786     case SPIRV_DECORATE_STRING:
1787     case SPIRV_TYPE:
1788     case SPIRV_STORAGE_CLASS:
1789     case SPIRV_BY_REFERENCE:
1790     case SPIRV_LITERAL:
1791         if (parseContext.symbolTable.atBuiltInLevel() ||
1792             parseContext.extensionTurnedOn(E_GL_EXT_spirv_intrinsics))
1793             return keyword;
1794         return identifierOrType();
1795
1796     case HITOBJECTNV:
1797         if (parseContext.symbolTable.atBuiltInLevel() ||
1798             (!parseContext.isEsProfile() && parseContext.version >= 460
1799                  && parseContext.extensionTurnedOn(E_GL_NV_shader_invocation_reorder)))
1800             return keyword;
1801         return identifierOrType();
1802
1803     case HITOBJECTATTRNV:
1804         if (parseContext.symbolTable.atBuiltInLevel() ||
1805             (!parseContext.isEsProfile() && parseContext.version >= 460
1806                  && parseContext.extensionTurnedOn(E_GL_NV_shader_invocation_reorder)))
1807             return keyword;
1808         return identifierOrType();
1809 #endif
1810
1811     default:
1812         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown glslang keyword", loc);
1813         return 0;
1814     }
1815 }
1816
1817 int TScanContext::identifierOrType()
1818 {
1819     parserToken->sType.lex.string = NewPoolTString(tokenText);
1820     if (field)
1821         return IDENTIFIER;
1822
1823     parserToken->sType.lex.symbol = parseContext.symbolTable.find(*parserToken->sType.lex.string);
1824     if ((afterType == false && afterStruct == false) && parserToken->sType.lex.symbol != nullptr) {
1825         if (const TVariable* variable = parserToken->sType.lex.symbol->getAsVariable()) {
1826             if (variable->isUserType() &&
1827                 // treat redeclaration of forward-declared buffer/uniform reference as an identifier
1828                 !(variable->getType().isReference() && afterBuffer)) {
1829                 afterType = true;
1830
1831                 return TYPE_NAME;
1832             }
1833         }
1834     }
1835
1836     return IDENTIFIER;
1837 }
1838
1839 // Give an error for use of a reserved symbol.
1840 // However, allow built-in declarations to use reserved words, to allow
1841 // extension support before the extension is enabled.
1842 int TScanContext::reservedWord()
1843 {
1844     if (! parseContext.symbolTable.atBuiltInLevel())
1845         parseContext.error(loc, "Reserved word.", tokenText, "", "");
1846
1847     return 0;
1848 }
1849
1850 int TScanContext::identifierOrReserved(bool reserved)
1851 {
1852     if (reserved) {
1853         reservedWord();
1854
1855         return 0;
1856     }
1857
1858     if (parseContext.isForwardCompatible())
1859         parseContext.warn(loc, "using future reserved keyword", tokenText, "");
1860
1861     return identifierOrType();
1862 }
1863
1864 // For keywords that suddenly showed up on non-ES (not previously reserved)
1865 // but then got reserved by ES 3.0.
1866 int TScanContext::es30ReservedFromGLSL(int version)
1867 {
1868     if (parseContext.symbolTable.atBuiltInLevel())
1869         return keyword;
1870
1871     if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1872         (!parseContext.isEsProfile() && parseContext.version < version)) {
1873             if (parseContext.isForwardCompatible())
1874                 parseContext.warn(loc, "future reserved word in ES 300 and keyword in GLSL", tokenText, "");
1875
1876             return identifierOrType();
1877     } else if (parseContext.isEsProfile() && parseContext.version >= 300)
1878         reservedWord();
1879
1880     return keyword;
1881 }
1882
1883 // For a keyword that was never reserved, until it suddenly
1884 // showed up, both in an es version and a non-ES version.
1885 int TScanContext::nonreservedKeyword(int esVersion, int nonEsVersion)
1886 {
1887     if ((parseContext.isEsProfile() && parseContext.version < esVersion) ||
1888         (!parseContext.isEsProfile() && parseContext.version < nonEsVersion)) {
1889         if (parseContext.isForwardCompatible())
1890             parseContext.warn(loc, "using future keyword", tokenText, "");
1891
1892         return identifierOrType();
1893     }
1894
1895     return keyword;
1896 }
1897
1898 int TScanContext::precisionKeyword()
1899 {
1900     if (parseContext.isEsProfile() || parseContext.version >= 130)
1901         return keyword;
1902
1903     if (parseContext.isForwardCompatible())
1904         parseContext.warn(loc, "using ES precision qualifier keyword", tokenText, "");
1905
1906     return identifierOrType();
1907 }
1908
1909 int TScanContext::matNxM()
1910 {
1911     afterType = true;
1912
1913     if (parseContext.version > 110)
1914         return keyword;
1915
1916     if (parseContext.isForwardCompatible())
1917         parseContext.warn(loc, "using future non-square matrix type keyword", tokenText, "");
1918
1919     return identifierOrType();
1920 }
1921
1922 int TScanContext::dMat()
1923 {
1924     afterType = true;
1925
1926     if (parseContext.isEsProfile() && parseContext.version >= 300) {
1927         reservedWord();
1928
1929         return keyword;
1930     }
1931
1932     if (!parseContext.isEsProfile() && (parseContext.version >= 400 ||
1933         parseContext.symbolTable.atBuiltInLevel() ||
1934         (parseContext.version >= 150 && parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_fp64)) ||
1935         (parseContext.version >= 150 && parseContext.extensionTurnedOn(E_GL_ARB_vertex_attrib_64bit)
1936          && parseContext.language == EShLangVertex)))
1937         return keyword;
1938
1939     if (parseContext.isForwardCompatible())
1940         parseContext.warn(loc, "using future type keyword", tokenText, "");
1941
1942     return identifierOrType();
1943 }
1944
1945 int TScanContext::firstGenerationImage(bool inEs310)
1946 {
1947     if (parseContext.symbolTable.atBuiltInLevel() ||
1948         (!parseContext.isEsProfile() && (parseContext.version >= 420 ||
1949          parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))) ||
1950         (inEs310 && parseContext.isEsProfile() && parseContext.version >= 310))
1951         return keyword;
1952
1953     if ((parseContext.isEsProfile() && parseContext.version >= 300) ||
1954         (!parseContext.isEsProfile() && parseContext.version >= 130)) {
1955         reservedWord();
1956
1957         return keyword;
1958     }
1959
1960     if (parseContext.isForwardCompatible())
1961         parseContext.warn(loc, "using future type keyword", tokenText, "");
1962
1963     return identifierOrType();
1964 }
1965
1966 int TScanContext::secondGenerationImage()
1967 {
1968     if (parseContext.isEsProfile() && parseContext.version >= 310) {
1969         reservedWord();
1970         return keyword;
1971     }
1972
1973     if (parseContext.symbolTable.atBuiltInLevel() ||
1974         (!parseContext.isEsProfile() &&
1975          (parseContext.version >= 420 || parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
1976         return keyword;
1977
1978     if (parseContext.isForwardCompatible())
1979         parseContext.warn(loc, "using future type keyword", tokenText, "");
1980
1981     return identifierOrType();
1982 }
1983
1984 } // end namespace glslang