accelerationStructureEXT - issue #2152
[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)["return"] =                  RETURN;
369     (*KeywordMap)["void"] =                    VOID;
370     (*KeywordMap)["bool"] =                    BOOL;
371     (*KeywordMap)["float"] =                   FLOAT;
372     (*KeywordMap)["int"] =                     INT;
373     (*KeywordMap)["bvec2"] =                   BVEC2;
374     (*KeywordMap)["bvec3"] =                   BVEC3;
375     (*KeywordMap)["bvec4"] =                   BVEC4;
376     (*KeywordMap)["vec2"] =                    VEC2;
377     (*KeywordMap)["vec3"] =                    VEC3;
378     (*KeywordMap)["vec4"] =                    VEC4;
379     (*KeywordMap)["ivec2"] =                   IVEC2;
380     (*KeywordMap)["ivec3"] =                   IVEC3;
381     (*KeywordMap)["ivec4"] =                   IVEC4;
382     (*KeywordMap)["mat2"] =                    MAT2;
383     (*KeywordMap)["mat3"] =                    MAT3;
384     (*KeywordMap)["mat4"] =                    MAT4;
385     (*KeywordMap)["true"] =                    BOOLCONSTANT;
386     (*KeywordMap)["false"] =                   BOOLCONSTANT;
387     (*KeywordMap)["layout"] =                  LAYOUT;
388     (*KeywordMap)["shared"] =                  SHARED;
389     (*KeywordMap)["highp"] =                   HIGH_PRECISION;
390     (*KeywordMap)["mediump"] =                 MEDIUM_PRECISION;
391     (*KeywordMap)["lowp"] =                    LOW_PRECISION;
392     (*KeywordMap)["superp"] =                  SUPERP;
393     (*KeywordMap)["precision"] =               PRECISION;
394     (*KeywordMap)["mat2x2"] =                  MAT2X2;
395     (*KeywordMap)["mat2x3"] =                  MAT2X3;
396     (*KeywordMap)["mat2x4"] =                  MAT2X4;
397     (*KeywordMap)["mat3x2"] =                  MAT3X2;
398     (*KeywordMap)["mat3x3"] =                  MAT3X3;
399     (*KeywordMap)["mat3x4"] =                  MAT3X4;
400     (*KeywordMap)["mat4x2"] =                  MAT4X2;
401     (*KeywordMap)["mat4x3"] =                  MAT4X3;
402     (*KeywordMap)["mat4x4"] =                  MAT4X4;
403     (*KeywordMap)["uint"] =                    UINT;
404     (*KeywordMap)["uvec2"] =                   UVEC2;
405     (*KeywordMap)["uvec3"] =                   UVEC3;
406     (*KeywordMap)["uvec4"] =                   UVEC4;
407
408 #ifndef GLSLANG_WEB
409     (*KeywordMap)["nonuniformEXT"] =           NONUNIFORM;
410     (*KeywordMap)["demote"] =                  DEMOTE;
411     (*KeywordMap)["attribute"] =               ATTRIBUTE;
412     (*KeywordMap)["varying"] =                 VARYING;
413     (*KeywordMap)["noperspective"] =           NOPERSPECTIVE;
414     (*KeywordMap)["coherent"] =                COHERENT;
415     (*KeywordMap)["devicecoherent"] =          DEVICECOHERENT;
416     (*KeywordMap)["queuefamilycoherent"] =     QUEUEFAMILYCOHERENT;
417     (*KeywordMap)["workgroupcoherent"] =       WORKGROUPCOHERENT;
418     (*KeywordMap)["subgroupcoherent"] =        SUBGROUPCOHERENT;
419     (*KeywordMap)["shadercallcoherent"] =      SHADERCALLCOHERENT;
420     (*KeywordMap)["nonprivate"] =              NONPRIVATE;
421     (*KeywordMap)["restrict"] =                RESTRICT;
422     (*KeywordMap)["readonly"] =                READONLY;
423     (*KeywordMap)["writeonly"] =               WRITEONLY;
424     (*KeywordMap)["atomic_uint"] =             ATOMIC_UINT;
425     (*KeywordMap)["volatile"] =                VOLATILE;
426     (*KeywordMap)["patch"] =                   PATCH;
427     (*KeywordMap)["sample"] =                  SAMPLE;
428     (*KeywordMap)["subroutine"] =              SUBROUTINE;
429     (*KeywordMap)["dmat2"] =                   DMAT2;
430     (*KeywordMap)["dmat3"] =                   DMAT3;
431     (*KeywordMap)["dmat4"] =                   DMAT4;
432     (*KeywordMap)["dmat2x2"] =                 DMAT2X2;
433     (*KeywordMap)["dmat2x3"] =                 DMAT2X3;
434     (*KeywordMap)["dmat2x4"] =                 DMAT2X4;
435     (*KeywordMap)["dmat3x2"] =                 DMAT3X2;
436     (*KeywordMap)["dmat3x3"] =                 DMAT3X3;
437     (*KeywordMap)["dmat3x4"] =                 DMAT3X4;
438     (*KeywordMap)["dmat4x2"] =                 DMAT4X2;
439     (*KeywordMap)["dmat4x3"] =                 DMAT4X3;
440     (*KeywordMap)["dmat4x4"] =                 DMAT4X4;
441     (*KeywordMap)["image1D"] =                 IMAGE1D;
442     (*KeywordMap)["iimage1D"] =                IIMAGE1D;
443     (*KeywordMap)["uimage1D"] =                UIMAGE1D;
444     (*KeywordMap)["image2D"] =                 IMAGE2D;
445     (*KeywordMap)["iimage2D"] =                IIMAGE2D;
446     (*KeywordMap)["uimage2D"] =                UIMAGE2D;
447     (*KeywordMap)["image3D"] =                 IMAGE3D;
448     (*KeywordMap)["iimage3D"] =                IIMAGE3D;
449     (*KeywordMap)["uimage3D"] =                UIMAGE3D;
450     (*KeywordMap)["image2DRect"] =             IMAGE2DRECT;
451     (*KeywordMap)["iimage2DRect"] =            IIMAGE2DRECT;
452     (*KeywordMap)["uimage2DRect"] =            UIMAGE2DRECT;
453     (*KeywordMap)["imageCube"] =               IMAGECUBE;
454     (*KeywordMap)["iimageCube"] =              IIMAGECUBE;
455     (*KeywordMap)["uimageCube"] =              UIMAGECUBE;
456     (*KeywordMap)["imageBuffer"] =             IMAGEBUFFER;
457     (*KeywordMap)["iimageBuffer"] =            IIMAGEBUFFER;
458     (*KeywordMap)["uimageBuffer"] =            UIMAGEBUFFER;
459     (*KeywordMap)["image1DArray"] =            IMAGE1DARRAY;
460     (*KeywordMap)["iimage1DArray"] =           IIMAGE1DARRAY;
461     (*KeywordMap)["uimage1DArray"] =           UIMAGE1DARRAY;
462     (*KeywordMap)["image2DArray"] =            IMAGE2DARRAY;
463     (*KeywordMap)["iimage2DArray"] =           IIMAGE2DARRAY;
464     (*KeywordMap)["uimage2DArray"] =           UIMAGE2DARRAY;
465     (*KeywordMap)["imageCubeArray"] =          IMAGECUBEARRAY;
466     (*KeywordMap)["iimageCubeArray"] =         IIMAGECUBEARRAY;
467     (*KeywordMap)["uimageCubeArray"] =         UIMAGECUBEARRAY;
468     (*KeywordMap)["image2DMS"] =               IMAGE2DMS;
469     (*KeywordMap)["iimage2DMS"] =              IIMAGE2DMS;
470     (*KeywordMap)["uimage2DMS"] =              UIMAGE2DMS;
471     (*KeywordMap)["image2DMSArray"] =          IMAGE2DMSARRAY;
472     (*KeywordMap)["iimage2DMSArray"] =         IIMAGE2DMSARRAY;
473     (*KeywordMap)["uimage2DMSArray"] =         UIMAGE2DMSARRAY;
474     (*KeywordMap)["double"] =                  DOUBLE;
475     (*KeywordMap)["dvec2"] =                   DVEC2;
476     (*KeywordMap)["dvec3"] =                   DVEC3;
477     (*KeywordMap)["dvec4"] =                   DVEC4;
478     (*KeywordMap)["int64_t"] =                 INT64_T;
479     (*KeywordMap)["uint64_t"] =                UINT64_T;
480     (*KeywordMap)["i64vec2"] =                 I64VEC2;
481     (*KeywordMap)["i64vec3"] =                 I64VEC3;
482     (*KeywordMap)["i64vec4"] =                 I64VEC4;
483     (*KeywordMap)["u64vec2"] =                 U64VEC2;
484     (*KeywordMap)["u64vec3"] =                 U64VEC3;
485     (*KeywordMap)["u64vec4"] =                 U64VEC4;
486
487     // GL_EXT_shader_explicit_arithmetic_types
488     (*KeywordMap)["int8_t"] =                  INT8_T;
489     (*KeywordMap)["i8vec2"] =                  I8VEC2;
490     (*KeywordMap)["i8vec3"] =                  I8VEC3;
491     (*KeywordMap)["i8vec4"] =                  I8VEC4;
492     (*KeywordMap)["uint8_t"] =                 UINT8_T;
493     (*KeywordMap)["u8vec2"] =                  U8VEC2;
494     (*KeywordMap)["u8vec3"] =                  U8VEC3;
495     (*KeywordMap)["u8vec4"] =                  U8VEC4;
496
497     (*KeywordMap)["int16_t"] =                 INT16_T;
498     (*KeywordMap)["i16vec2"] =                 I16VEC2;
499     (*KeywordMap)["i16vec3"] =                 I16VEC3;
500     (*KeywordMap)["i16vec4"] =                 I16VEC4;
501     (*KeywordMap)["uint16_t"] =                UINT16_T;
502     (*KeywordMap)["u16vec2"] =                 U16VEC2;
503     (*KeywordMap)["u16vec3"] =                 U16VEC3;
504     (*KeywordMap)["u16vec4"] =                 U16VEC4;
505
506     (*KeywordMap)["int32_t"] =                 INT32_T;
507     (*KeywordMap)["i32vec2"] =                 I32VEC2;
508     (*KeywordMap)["i32vec3"] =                 I32VEC3;
509     (*KeywordMap)["i32vec4"] =                 I32VEC4;
510     (*KeywordMap)["uint32_t"] =                UINT32_T;
511     (*KeywordMap)["u32vec2"] =                 U32VEC2;
512     (*KeywordMap)["u32vec3"] =                 U32VEC3;
513     (*KeywordMap)["u32vec4"] =                 U32VEC4;
514
515     (*KeywordMap)["float16_t"] =               FLOAT16_T;
516     (*KeywordMap)["f16vec2"] =                 F16VEC2;
517     (*KeywordMap)["f16vec3"] =                 F16VEC3;
518     (*KeywordMap)["f16vec4"] =                 F16VEC4;
519     (*KeywordMap)["f16mat2"] =                 F16MAT2;
520     (*KeywordMap)["f16mat3"] =                 F16MAT3;
521     (*KeywordMap)["f16mat4"] =                 F16MAT4;
522     (*KeywordMap)["f16mat2x2"] =               F16MAT2X2;
523     (*KeywordMap)["f16mat2x3"] =               F16MAT2X3;
524     (*KeywordMap)["f16mat2x4"] =               F16MAT2X4;
525     (*KeywordMap)["f16mat3x2"] =               F16MAT3X2;
526     (*KeywordMap)["f16mat3x3"] =               F16MAT3X3;
527     (*KeywordMap)["f16mat3x4"] =               F16MAT3X4;
528     (*KeywordMap)["f16mat4x2"] =               F16MAT4X2;
529     (*KeywordMap)["f16mat4x3"] =               F16MAT4X3;
530     (*KeywordMap)["f16mat4x4"] =               F16MAT4X4;
531
532     (*KeywordMap)["float32_t"] =               FLOAT32_T;
533     (*KeywordMap)["f32vec2"] =                 F32VEC2;
534     (*KeywordMap)["f32vec3"] =                 F32VEC3;
535     (*KeywordMap)["f32vec4"] =                 F32VEC4;
536     (*KeywordMap)["f32mat2"] =                 F32MAT2;
537     (*KeywordMap)["f32mat3"] =                 F32MAT3;
538     (*KeywordMap)["f32mat4"] =                 F32MAT4;
539     (*KeywordMap)["f32mat2x2"] =               F32MAT2X2;
540     (*KeywordMap)["f32mat2x3"] =               F32MAT2X3;
541     (*KeywordMap)["f32mat2x4"] =               F32MAT2X4;
542     (*KeywordMap)["f32mat3x2"] =               F32MAT3X2;
543     (*KeywordMap)["f32mat3x3"] =               F32MAT3X3;
544     (*KeywordMap)["f32mat3x4"] =               F32MAT3X4;
545     (*KeywordMap)["f32mat4x2"] =               F32MAT4X2;
546     (*KeywordMap)["f32mat4x3"] =               F32MAT4X3;
547     (*KeywordMap)["f32mat4x4"] =               F32MAT4X4;
548     (*KeywordMap)["float64_t"] =               FLOAT64_T;
549     (*KeywordMap)["f64vec2"] =                 F64VEC2;
550     (*KeywordMap)["f64vec3"] =                 F64VEC3;
551     (*KeywordMap)["f64vec4"] =                 F64VEC4;
552     (*KeywordMap)["f64mat2"] =                 F64MAT2;
553     (*KeywordMap)["f64mat3"] =                 F64MAT3;
554     (*KeywordMap)["f64mat4"] =                 F64MAT4;
555     (*KeywordMap)["f64mat2x2"] =               F64MAT2X2;
556     (*KeywordMap)["f64mat2x3"] =               F64MAT2X3;
557     (*KeywordMap)["f64mat2x4"] =               F64MAT2X4;
558     (*KeywordMap)["f64mat3x2"] =               F64MAT3X2;
559     (*KeywordMap)["f64mat3x3"] =               F64MAT3X3;
560     (*KeywordMap)["f64mat3x4"] =               F64MAT3X4;
561     (*KeywordMap)["f64mat4x2"] =               F64MAT4X2;
562     (*KeywordMap)["f64mat4x3"] =               F64MAT4X3;
563     (*KeywordMap)["f64mat4x4"] =               F64MAT4X4;
564 #endif
565
566     (*KeywordMap)["sampler2D"] =               SAMPLER2D;
567     (*KeywordMap)["samplerCube"] =             SAMPLERCUBE;
568     (*KeywordMap)["samplerCubeShadow"] =       SAMPLERCUBESHADOW;
569     (*KeywordMap)["sampler2DArray"] =          SAMPLER2DARRAY;
570     (*KeywordMap)["sampler2DArrayShadow"] =    SAMPLER2DARRAYSHADOW;
571     (*KeywordMap)["isampler2D"] =              ISAMPLER2D;
572     (*KeywordMap)["isampler3D"] =              ISAMPLER3D;
573     (*KeywordMap)["isamplerCube"] =            ISAMPLERCUBE;
574     (*KeywordMap)["isampler2DArray"] =         ISAMPLER2DARRAY;
575     (*KeywordMap)["usampler2D"] =              USAMPLER2D;
576     (*KeywordMap)["usampler3D"] =              USAMPLER3D;
577     (*KeywordMap)["usamplerCube"] =            USAMPLERCUBE;
578     (*KeywordMap)["usampler2DArray"] =         USAMPLER2DARRAY;
579     (*KeywordMap)["sampler3D"] =               SAMPLER3D;
580     (*KeywordMap)["sampler2DShadow"] =         SAMPLER2DSHADOW;
581
582     (*KeywordMap)["texture2D"] =               TEXTURE2D;
583     (*KeywordMap)["textureCube"] =             TEXTURECUBE;
584     (*KeywordMap)["texture2DArray"] =          TEXTURE2DARRAY;
585     (*KeywordMap)["itexture2D"] =              ITEXTURE2D;
586     (*KeywordMap)["itexture3D"] =              ITEXTURE3D;
587     (*KeywordMap)["itextureCube"] =            ITEXTURECUBE;
588     (*KeywordMap)["itexture2DArray"] =         ITEXTURE2DARRAY;
589     (*KeywordMap)["utexture2D"] =              UTEXTURE2D;
590     (*KeywordMap)["utexture3D"] =              UTEXTURE3D;
591     (*KeywordMap)["utextureCube"] =            UTEXTURECUBE;
592     (*KeywordMap)["utexture2DArray"] =         UTEXTURE2DARRAY;
593     (*KeywordMap)["texture3D"] =               TEXTURE3D;
594
595     (*KeywordMap)["sampler"] =                 SAMPLER;
596     (*KeywordMap)["samplerShadow"] =           SAMPLERSHADOW;
597
598 #ifndef GLSLANG_WEB
599     (*KeywordMap)["textureCubeArray"] =        TEXTURECUBEARRAY;
600     (*KeywordMap)["itextureCubeArray"] =       ITEXTURECUBEARRAY;
601     (*KeywordMap)["utextureCubeArray"] =       UTEXTURECUBEARRAY;
602     (*KeywordMap)["samplerCubeArray"] =        SAMPLERCUBEARRAY;
603     (*KeywordMap)["samplerCubeArrayShadow"] =  SAMPLERCUBEARRAYSHADOW;
604     (*KeywordMap)["isamplerCubeArray"] =       ISAMPLERCUBEARRAY;
605     (*KeywordMap)["usamplerCubeArray"] =       USAMPLERCUBEARRAY;
606     (*KeywordMap)["sampler1DArrayShadow"] =    SAMPLER1DARRAYSHADOW;
607     (*KeywordMap)["isampler1DArray"] =         ISAMPLER1DARRAY;
608     (*KeywordMap)["usampler1D"] =              USAMPLER1D;
609     (*KeywordMap)["isampler1D"] =              ISAMPLER1D;
610     (*KeywordMap)["usampler1DArray"] =         USAMPLER1DARRAY;
611     (*KeywordMap)["samplerBuffer"] =           SAMPLERBUFFER;
612     (*KeywordMap)["isampler2DRect"] =          ISAMPLER2DRECT;
613     (*KeywordMap)["usampler2DRect"] =          USAMPLER2DRECT;
614     (*KeywordMap)["isamplerBuffer"] =          ISAMPLERBUFFER;
615     (*KeywordMap)["usamplerBuffer"] =          USAMPLERBUFFER;
616     (*KeywordMap)["sampler2DMS"] =             SAMPLER2DMS;
617     (*KeywordMap)["isampler2DMS"] =            ISAMPLER2DMS;
618     (*KeywordMap)["usampler2DMS"] =            USAMPLER2DMS;
619     (*KeywordMap)["sampler2DMSArray"] =        SAMPLER2DMSARRAY;
620     (*KeywordMap)["isampler2DMSArray"] =       ISAMPLER2DMSARRAY;
621     (*KeywordMap)["usampler2DMSArray"] =       USAMPLER2DMSARRAY;
622     (*KeywordMap)["sampler1D"] =               SAMPLER1D;
623     (*KeywordMap)["sampler1DShadow"] =         SAMPLER1DSHADOW;
624     (*KeywordMap)["sampler2DRect"] =           SAMPLER2DRECT;
625     (*KeywordMap)["sampler2DRectShadow"] =     SAMPLER2DRECTSHADOW;
626     (*KeywordMap)["sampler1DArray"] =          SAMPLER1DARRAY;
627
628     (*KeywordMap)["samplerExternalOES"] =      SAMPLEREXTERNALOES; // GL_OES_EGL_image_external
629
630     (*KeywordMap)["__samplerExternal2DY2YEXT"] = SAMPLEREXTERNAL2DY2YEXT; // GL_EXT_YUV_target
631
632     (*KeywordMap)["itexture1DArray"] =         ITEXTURE1DARRAY;
633     (*KeywordMap)["utexture1D"] =              UTEXTURE1D;
634     (*KeywordMap)["itexture1D"] =              ITEXTURE1D;
635     (*KeywordMap)["utexture1DArray"] =         UTEXTURE1DARRAY;
636     (*KeywordMap)["textureBuffer"] =           TEXTUREBUFFER;
637     (*KeywordMap)["itexture2DRect"] =          ITEXTURE2DRECT;
638     (*KeywordMap)["utexture2DRect"] =          UTEXTURE2DRECT;
639     (*KeywordMap)["itextureBuffer"] =          ITEXTUREBUFFER;
640     (*KeywordMap)["utextureBuffer"] =          UTEXTUREBUFFER;
641     (*KeywordMap)["texture2DMS"] =             TEXTURE2DMS;
642     (*KeywordMap)["itexture2DMS"] =            ITEXTURE2DMS;
643     (*KeywordMap)["utexture2DMS"] =            UTEXTURE2DMS;
644     (*KeywordMap)["texture2DMSArray"] =        TEXTURE2DMSARRAY;
645     (*KeywordMap)["itexture2DMSArray"] =       ITEXTURE2DMSARRAY;
646     (*KeywordMap)["utexture2DMSArray"] =       UTEXTURE2DMSARRAY;
647     (*KeywordMap)["texture1D"] =               TEXTURE1D;
648     (*KeywordMap)["texture2DRect"] =           TEXTURE2DRECT;
649     (*KeywordMap)["texture1DArray"] =          TEXTURE1DARRAY;
650
651     (*KeywordMap)["subpassInput"] =            SUBPASSINPUT;
652     (*KeywordMap)["subpassInputMS"] =          SUBPASSINPUTMS;
653     (*KeywordMap)["isubpassInput"] =           ISUBPASSINPUT;
654     (*KeywordMap)["isubpassInputMS"] =         ISUBPASSINPUTMS;
655     (*KeywordMap)["usubpassInput"] =           USUBPASSINPUT;
656     (*KeywordMap)["usubpassInputMS"] =         USUBPASSINPUTMS;
657
658     (*KeywordMap)["f16sampler1D"] =                 F16SAMPLER1D;
659     (*KeywordMap)["f16sampler2D"] =                 F16SAMPLER2D;
660     (*KeywordMap)["f16sampler3D"] =                 F16SAMPLER3D;
661     (*KeywordMap)["f16sampler2DRect"] =             F16SAMPLER2DRECT;
662     (*KeywordMap)["f16samplerCube"] =               F16SAMPLERCUBE;
663     (*KeywordMap)["f16sampler1DArray"] =            F16SAMPLER1DARRAY;
664     (*KeywordMap)["f16sampler2DArray"] =            F16SAMPLER2DARRAY;
665     (*KeywordMap)["f16samplerCubeArray"] =          F16SAMPLERCUBEARRAY;
666     (*KeywordMap)["f16samplerBuffer"] =             F16SAMPLERBUFFER;
667     (*KeywordMap)["f16sampler2DMS"] =               F16SAMPLER2DMS;
668     (*KeywordMap)["f16sampler2DMSArray"] =          F16SAMPLER2DMSARRAY;
669     (*KeywordMap)["f16sampler1DShadow"] =           F16SAMPLER1DSHADOW;
670     (*KeywordMap)["f16sampler2DShadow"] =           F16SAMPLER2DSHADOW;
671     (*KeywordMap)["f16sampler2DRectShadow"] =       F16SAMPLER2DRECTSHADOW;
672     (*KeywordMap)["f16samplerCubeShadow"] =         F16SAMPLERCUBESHADOW;
673     (*KeywordMap)["f16sampler1DArrayShadow"] =      F16SAMPLER1DARRAYSHADOW;
674     (*KeywordMap)["f16sampler2DArrayShadow"] =      F16SAMPLER2DARRAYSHADOW;
675     (*KeywordMap)["f16samplerCubeArrayShadow"] =    F16SAMPLERCUBEARRAYSHADOW;
676
677     (*KeywordMap)["f16image1D"] =                   F16IMAGE1D;
678     (*KeywordMap)["f16image2D"] =                   F16IMAGE2D;
679     (*KeywordMap)["f16image3D"] =                   F16IMAGE3D;
680     (*KeywordMap)["f16image2DRect"] =               F16IMAGE2DRECT;
681     (*KeywordMap)["f16imageCube"] =                 F16IMAGECUBE;
682     (*KeywordMap)["f16image1DArray"] =              F16IMAGE1DARRAY;
683     (*KeywordMap)["f16image2DArray"] =              F16IMAGE2DARRAY;
684     (*KeywordMap)["f16imageCubeArray"] =            F16IMAGECUBEARRAY;
685     (*KeywordMap)["f16imageBuffer"] =               F16IMAGEBUFFER;
686     (*KeywordMap)["f16image2DMS"] =                 F16IMAGE2DMS;
687     (*KeywordMap)["f16image2DMSArray"] =            F16IMAGE2DMSARRAY;
688
689     (*KeywordMap)["f16texture1D"] =                 F16TEXTURE1D;
690     (*KeywordMap)["f16texture2D"] =                 F16TEXTURE2D;
691     (*KeywordMap)["f16texture3D"] =                 F16TEXTURE3D;
692     (*KeywordMap)["f16texture2DRect"] =             F16TEXTURE2DRECT;
693     (*KeywordMap)["f16textureCube"] =               F16TEXTURECUBE;
694     (*KeywordMap)["f16texture1DArray"] =            F16TEXTURE1DARRAY;
695     (*KeywordMap)["f16texture2DArray"] =            F16TEXTURE2DARRAY;
696     (*KeywordMap)["f16textureCubeArray"] =          F16TEXTURECUBEARRAY;
697     (*KeywordMap)["f16textureBuffer"] =             F16TEXTUREBUFFER;
698     (*KeywordMap)["f16texture2DMS"] =               F16TEXTURE2DMS;
699     (*KeywordMap)["f16texture2DMSArray"] =          F16TEXTURE2DMSARRAY;
700
701     (*KeywordMap)["f16subpassInput"] =              F16SUBPASSINPUT;
702     (*KeywordMap)["f16subpassInputMS"] =            F16SUBPASSINPUTMS;
703     (*KeywordMap)["__explicitInterpAMD"] =     EXPLICITINTERPAMD;
704     (*KeywordMap)["pervertexNV"] =             PERVERTEXNV;
705     (*KeywordMap)["precise"] =                 PRECISE;
706
707     (*KeywordMap)["rayPayloadNV"] =            PAYLOADNV;
708     (*KeywordMap)["rayPayloadEXT"] =           PAYLOADEXT;
709     (*KeywordMap)["rayPayloadInNV"] =          PAYLOADINNV;
710     (*KeywordMap)["rayPayloadInEXT"] =         PAYLOADINEXT;
711     (*KeywordMap)["hitAttributeNV"] =          HITATTRNV;
712     (*KeywordMap)["hitAttributeEXT"] =         HITATTREXT;
713     (*KeywordMap)["callableDataNV"] =          CALLDATANV;
714     (*KeywordMap)["callableDataEXT"] =         CALLDATAEXT;
715     (*KeywordMap)["callableDataInNV"] =        CALLDATAINNV;
716     (*KeywordMap)["callableDataInEXT"] =       CALLDATAINEXT;
717     (*KeywordMap)["accelerationStructureNV"] = ACCSTRUCTNV;
718     (*KeywordMap)["accelerationStructureEXT"]   = ACCSTRUCTEXT;
719     (*KeywordMap)["rayQueryEXT"] =              RAYQUERYEXT;
720     (*KeywordMap)["perprimitiveNV"] =          PERPRIMITIVENV;
721     (*KeywordMap)["perviewNV"] =               PERVIEWNV;
722     (*KeywordMap)["taskNV"] =                  PERTASKNV;
723
724     (*KeywordMap)["fcoopmatNV"] =              FCOOPMATNV;
725     (*KeywordMap)["icoopmatNV"] =              ICOOPMATNV;
726     (*KeywordMap)["ucoopmatNV"] =              UCOOPMATNV;
727
728     ReservedSet = new std::unordered_set<const char*, str_hash, str_eq>;
729
730     ReservedSet->insert("common");
731     ReservedSet->insert("partition");
732     ReservedSet->insert("active");
733     ReservedSet->insert("asm");
734     ReservedSet->insert("class");
735     ReservedSet->insert("union");
736     ReservedSet->insert("enum");
737     ReservedSet->insert("typedef");
738     ReservedSet->insert("template");
739     ReservedSet->insert("this");
740     ReservedSet->insert("goto");
741     ReservedSet->insert("inline");
742     ReservedSet->insert("noinline");
743     ReservedSet->insert("public");
744     ReservedSet->insert("static");
745     ReservedSet->insert("extern");
746     ReservedSet->insert("external");
747     ReservedSet->insert("interface");
748     ReservedSet->insert("long");
749     ReservedSet->insert("short");
750     ReservedSet->insert("half");
751     ReservedSet->insert("fixed");
752     ReservedSet->insert("unsigned");
753     ReservedSet->insert("input");
754     ReservedSet->insert("output");
755     ReservedSet->insert("hvec2");
756     ReservedSet->insert("hvec3");
757     ReservedSet->insert("hvec4");
758     ReservedSet->insert("fvec2");
759     ReservedSet->insert("fvec3");
760     ReservedSet->insert("fvec4");
761     ReservedSet->insert("sampler3DRect");
762     ReservedSet->insert("filter");
763     ReservedSet->insert("sizeof");
764     ReservedSet->insert("cast");
765     ReservedSet->insert("namespace");
766     ReservedSet->insert("using");
767 #endif
768 }
769
770 void TScanContext::deleteKeywordMap()
771 {
772     delete KeywordMap;
773     KeywordMap = nullptr;
774 #ifndef GLSLANG_WEB
775     delete ReservedSet;
776     ReservedSet = nullptr;
777 #endif
778 }
779
780 // Called by yylex to get the next token.
781 // Returning 0 implies end of input.
782 int TScanContext::tokenize(TPpContext* pp, TParserToken& token)
783 {
784     do {
785         parserToken = &token;
786         TPpToken ppToken;
787         int token = pp->tokenize(ppToken);
788         if (token == EndOfInput)
789             return 0;
790
791         tokenText = ppToken.name;
792         loc = ppToken.loc;
793         parserToken->sType.lex.loc = loc;
794         switch (token) {
795         case ';':  afterType = false; afterBuffer = false; return SEMICOLON;
796         case ',':  afterType = false;   return COMMA;
797         case ':':                       return COLON;
798         case '=':  afterType = false;   return EQUAL;
799         case '(':  afterType = false;   return LEFT_PAREN;
800         case ')':  afterType = false;   return RIGHT_PAREN;
801         case '.':  field = true;        return DOT;
802         case '!':                       return BANG;
803         case '-':                       return DASH;
804         case '~':                       return TILDE;
805         case '+':                       return PLUS;
806         case '*':                       return STAR;
807         case '/':                       return SLASH;
808         case '%':                       return PERCENT;
809         case '<':                       return LEFT_ANGLE;
810         case '>':                       return RIGHT_ANGLE;
811         case '|':                       return VERTICAL_BAR;
812         case '^':                       return CARET;
813         case '&':                       return AMPERSAND;
814         case '?':                       return QUESTION;
815         case '[':                       return LEFT_BRACKET;
816         case ']':                       return RIGHT_BRACKET;
817         case '{':  afterStruct = false; afterBuffer = false; return LEFT_BRACE;
818         case '}':                       return RIGHT_BRACE;
819         case '\\':
820             parseContext.error(loc, "illegal use of escape character", "\\", "");
821             break;
822
823         case PPAtomAddAssign:          return ADD_ASSIGN;
824         case PPAtomSubAssign:          return SUB_ASSIGN;
825         case PPAtomMulAssign:          return MUL_ASSIGN;
826         case PPAtomDivAssign:          return DIV_ASSIGN;
827         case PPAtomModAssign:          return MOD_ASSIGN;
828
829         case PpAtomRight:              return RIGHT_OP;
830         case PpAtomLeft:               return LEFT_OP;
831
832         case PpAtomRightAssign:        return RIGHT_ASSIGN;
833         case PpAtomLeftAssign:         return LEFT_ASSIGN;
834         case PpAtomAndAssign:          return AND_ASSIGN;
835         case PpAtomOrAssign:           return OR_ASSIGN;
836         case PpAtomXorAssign:          return XOR_ASSIGN;
837
838         case PpAtomAnd:                return AND_OP;
839         case PpAtomOr:                 return OR_OP;
840         case PpAtomXor:                return XOR_OP;
841
842         case PpAtomEQ:                 return EQ_OP;
843         case PpAtomGE:                 return GE_OP;
844         case PpAtomNE:                 return NE_OP;
845         case PpAtomLE:                 return LE_OP;
846
847         case PpAtomDecrement:          return DEC_OP;
848         case PpAtomIncrement:          return INC_OP;
849
850         case PpAtomColonColon:
851             parseContext.error(loc, "not supported", "::", "");
852             break;
853
854         case PpAtomConstString:        parserToken->sType.lex.string = NewPoolTString(tokenText);     return STRING_LITERAL;
855         case PpAtomConstInt:           parserToken->sType.lex.i    = ppToken.ival;       return INTCONSTANT;
856         case PpAtomConstUint:          parserToken->sType.lex.i    = ppToken.ival;       return UINTCONSTANT;
857         case PpAtomConstFloat:         parserToken->sType.lex.d    = ppToken.dval;       return FLOATCONSTANT;
858 #ifndef GLSLANG_WEB
859         case PpAtomConstInt16:         parserToken->sType.lex.i    = ppToken.ival;       return INT16CONSTANT;
860         case PpAtomConstUint16:        parserToken->sType.lex.i    = ppToken.ival;       return UINT16CONSTANT;
861         case PpAtomConstInt64:         parserToken->sType.lex.i64  = ppToken.i64val;     return INT64CONSTANT;
862         case PpAtomConstUint64:        parserToken->sType.lex.i64  = ppToken.i64val;     return UINT64CONSTANT;
863         case PpAtomConstDouble:        parserToken->sType.lex.d    = ppToken.dval;       return DOUBLECONSTANT;
864         case PpAtomConstFloat16:       parserToken->sType.lex.d    = ppToken.dval;       return FLOAT16CONSTANT;
865 #endif
866         case PpAtomIdentifier:
867         {
868             int token = tokenizeIdentifier();
869             field = false;
870             return token;
871         }
872
873         case EndOfInput:               return 0;
874
875         default:
876             char buf[2];
877             buf[0] = (char)token;
878             buf[1] = 0;
879             parseContext.error(loc, "unexpected token", buf, "");
880             break;
881         }
882     } while (true);
883 }
884
885 int TScanContext::tokenizeIdentifier()
886 {
887 #ifndef GLSLANG_WEB
888     if (ReservedSet->find(tokenText) != ReservedSet->end())
889         return reservedWord();
890 #endif
891
892     auto it = KeywordMap->find(tokenText);
893     if (it == KeywordMap->end()) {
894         // Should have an identifier of some sort
895         return identifierOrType();
896     }
897     keyword = it->second;
898
899     switch (keyword) {
900     case CONST:
901     case UNIFORM:
902     case IN:
903     case OUT:
904     case INOUT:
905     case BREAK:
906     case CONTINUE:
907     case DO:
908     case FOR:
909     case WHILE:
910     case IF:
911     case ELSE:
912     case DISCARD:
913     case RETURN:
914     case CASE:
915         return keyword;
916
917     case BUFFER:
918         afterBuffer = true;
919         if ((parseContext.isEsProfile() && parseContext.version < 310) ||
920             (!parseContext.isEsProfile() && parseContext.version < 430))
921             return identifierOrType();
922         return keyword;
923
924     case STRUCT:
925         afterStruct = true;
926         return keyword;
927
928     case SWITCH:
929     case DEFAULT:
930         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
931             (!parseContext.isEsProfile() && parseContext.version < 130))
932             reservedWord();
933         return keyword;
934
935     case VOID:
936     case BOOL:
937     case FLOAT:
938     case INT:
939     case BVEC2:
940     case BVEC3:
941     case BVEC4:
942     case VEC2:
943     case VEC3:
944     case VEC4:
945     case IVEC2:
946     case IVEC3:
947     case IVEC4:
948     case MAT2:
949     case MAT3:
950     case MAT4:
951     case SAMPLER2D:
952     case SAMPLERCUBE:
953         afterType = true;
954         return keyword;
955
956     case BOOLCONSTANT:
957         if (strcmp("true", tokenText) == 0)
958             parserToken->sType.lex.b = true;
959         else
960             parserToken->sType.lex.b = false;
961         return keyword;
962
963     case SMOOTH:
964         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
965             (!parseContext.isEsProfile() && parseContext.version < 130))
966             return identifierOrType();
967         return keyword;
968     case FLAT:
969         if (parseContext.isEsProfile() && parseContext.version < 300)
970             reservedWord();
971         else if (!parseContext.isEsProfile() && parseContext.version < 130)
972             return identifierOrType();
973         return keyword;
974     case CENTROID:
975         if (parseContext.version < 120)
976             return identifierOrType();
977         return keyword;
978     case INVARIANT:
979         if (!parseContext.isEsProfile() && parseContext.version < 120)
980             return identifierOrType();
981         return keyword;
982     case PACKED:
983         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
984             (!parseContext.isEsProfile() && parseContext.version < 330))
985             return reservedWord();
986         return identifierOrType();
987
988     case RESOURCE:
989     {
990         bool reserved = (parseContext.isEsProfile() && parseContext.version >= 300) ||
991                         (!parseContext.isEsProfile() && parseContext.version >= 420);
992         return identifierOrReserved(reserved);
993     }
994     case SUPERP:
995     {
996         bool reserved = parseContext.isEsProfile() || parseContext.version >= 130;
997         return identifierOrReserved(reserved);
998     }
999
1000 #ifndef GLSLANG_WEB
1001     case NOPERSPECTIVE:
1002         if (parseContext.extensionTurnedOn(E_GL_NV_shader_noperspective_interpolation))
1003             return keyword;
1004         return es30ReservedFromGLSL(130);
1005
1006     case NONUNIFORM:
1007         if (parseContext.extensionTurnedOn(E_GL_EXT_nonuniform_qualifier))
1008             return keyword;
1009         else
1010             return identifierOrType();
1011     case ATTRIBUTE:
1012     case VARYING:
1013         if (parseContext.isEsProfile() && parseContext.version >= 300)
1014             reservedWord();
1015         return keyword;
1016     case PAYLOADNV:
1017     case PAYLOADINNV:
1018     case HITATTRNV:
1019     case CALLDATANV:
1020     case CALLDATAINNV:
1021     case ACCSTRUCTNV:
1022         if (parseContext.symbolTable.atBuiltInLevel() ||
1023             parseContext.extensionTurnedOn(E_GL_NV_ray_tracing))
1024             return keyword;
1025         return identifierOrType();
1026     case PAYLOADEXT:
1027     case PAYLOADINEXT:
1028     case HITATTREXT:
1029     case CALLDATAEXT:
1030     case CALLDATAINEXT:
1031     case ACCSTRUCTEXT:
1032         if (parseContext.symbolTable.atBuiltInLevel() ||
1033             parseContext.extensionTurnedOn(E_GL_EXT_ray_query))
1034             return keyword;
1035         return identifierOrType();
1036     case RAYQUERYEXT:
1037         if (parseContext.symbolTable.atBuiltInLevel() ||
1038             (!parseContext.isEsProfile() && parseContext.version >= 460
1039                  && parseContext.extensionTurnedOn(E_GL_EXT_ray_query)))
1040             return keyword;
1041         return identifierOrType();
1042     case ATOMIC_UINT:
1043         if ((parseContext.isEsProfile() && parseContext.version >= 310) ||
1044             parseContext.extensionTurnedOn(E_GL_ARB_shader_atomic_counters))
1045             return keyword;
1046         return es30ReservedFromGLSL(420);
1047
1048     case COHERENT:
1049     case DEVICECOHERENT:
1050     case QUEUEFAMILYCOHERENT:
1051     case WORKGROUPCOHERENT:
1052     case SUBGROUPCOHERENT:
1053     case SHADERCALLCOHERENT:
1054     case NONPRIVATE:
1055     case RESTRICT:
1056     case READONLY:
1057     case WRITEONLY:
1058         if (parseContext.isEsProfile() && parseContext.version >= 310)
1059             return keyword;
1060         return es30ReservedFromGLSL(parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store) ? 130 : 420);
1061     case VOLATILE:
1062         if (parseContext.isEsProfile() && parseContext.version >= 310)
1063             return keyword;
1064         if (! parseContext.symbolTable.atBuiltInLevel() && (parseContext.isEsProfile() ||
1065             (parseContext.version < 420 && ! parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
1066             reservedWord();
1067         return keyword;
1068     case PATCH:
1069         if (parseContext.symbolTable.atBuiltInLevel() ||
1070             (parseContext.isEsProfile() &&
1071              (parseContext.version >= 320 ||
1072               parseContext.extensionsTurnedOn(Num_AEP_tessellation_shader, AEP_tessellation_shader))) ||
1073             (!parseContext.isEsProfile() && parseContext.extensionTurnedOn(E_GL_ARB_tessellation_shader)))
1074             return keyword;
1075
1076         return es30ReservedFromGLSL(400);
1077
1078     case SAMPLE:
1079         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1080             parseContext.extensionsTurnedOn(1, &E_GL_OES_shader_multisample_interpolation))
1081             return keyword;
1082         return es30ReservedFromGLSL(400);
1083
1084     case SUBROUTINE:
1085         return es30ReservedFromGLSL(400);
1086 #endif
1087     case SHARED:
1088         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1089             (!parseContext.isEsProfile() && parseContext.version < 140))
1090             return identifierOrType();
1091         return keyword;
1092     case LAYOUT:
1093     {
1094         const int numLayoutExts = 2;
1095         const char* layoutExts[numLayoutExts] = { E_GL_ARB_shading_language_420pack,
1096                                                   E_GL_ARB_explicit_attrib_location };
1097         if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1098             (!parseContext.isEsProfile() && parseContext.version < 140 &&
1099             ! parseContext.extensionsTurnedOn(numLayoutExts, layoutExts)))
1100             return identifierOrType();
1101         return keyword;
1102     }
1103
1104     case HIGH_PRECISION:
1105     case MEDIUM_PRECISION:
1106     case LOW_PRECISION:
1107     case PRECISION:
1108         return precisionKeyword();
1109
1110     case MAT2X2:
1111     case MAT2X3:
1112     case MAT2X4:
1113     case MAT3X2:
1114     case MAT3X3:
1115     case MAT3X4:
1116     case MAT4X2:
1117     case MAT4X3:
1118     case MAT4X4:
1119         return matNxM();
1120
1121 #ifndef GLSLANG_WEB
1122     case DMAT2:
1123     case DMAT3:
1124     case DMAT4:
1125     case DMAT2X2:
1126     case DMAT2X3:
1127     case DMAT2X4:
1128     case DMAT3X2:
1129     case DMAT3X3:
1130     case DMAT3X4:
1131     case DMAT4X2:
1132     case DMAT4X3:
1133     case DMAT4X4:
1134         return dMat();
1135
1136     case IMAGE1D:
1137     case IIMAGE1D:
1138     case UIMAGE1D:
1139     case IMAGE1DARRAY:
1140     case IIMAGE1DARRAY:
1141     case UIMAGE1DARRAY:
1142     case IMAGE2DRECT:
1143     case IIMAGE2DRECT:
1144     case UIMAGE2DRECT:
1145         afterType = true;
1146         return firstGenerationImage(false);
1147
1148     case IMAGEBUFFER:
1149     case IIMAGEBUFFER:
1150     case UIMAGEBUFFER:
1151         afterType = true;
1152         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1153             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1154             return keyword;
1155         return firstGenerationImage(false);
1156
1157     case IMAGE2D:
1158     case IIMAGE2D:
1159     case UIMAGE2D:
1160     case IMAGE3D:
1161     case IIMAGE3D:
1162     case UIMAGE3D:
1163     case IMAGECUBE:
1164     case IIMAGECUBE:
1165     case UIMAGECUBE:
1166     case IMAGE2DARRAY:
1167     case IIMAGE2DARRAY:
1168     case UIMAGE2DARRAY:
1169         afterType = true;
1170         return firstGenerationImage(true);
1171
1172     case IMAGECUBEARRAY:
1173     case IIMAGECUBEARRAY:
1174     case UIMAGECUBEARRAY:
1175         afterType = true;
1176         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1177             parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1178             return keyword;
1179         return secondGenerationImage();
1180
1181     case IMAGE2DMS:
1182     case IIMAGE2DMS:
1183     case UIMAGE2DMS:
1184     case IMAGE2DMSARRAY:
1185     case IIMAGE2DMSARRAY:
1186     case UIMAGE2DMSARRAY:
1187         afterType = true;
1188         return secondGenerationImage();
1189
1190     case DOUBLE:
1191     case DVEC2:
1192     case DVEC3:
1193     case DVEC4:
1194         afterType = true;
1195         if (parseContext.isEsProfile() || parseContext.version < 150 ||
1196             (!parseContext.symbolTable.atBuiltInLevel() &&
1197               parseContext.version < 400 &&
1198              !parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_fp64)))
1199             reservedWord();
1200         return keyword;
1201
1202     case INT64_T:
1203     case UINT64_T:
1204     case I64VEC2:
1205     case I64VEC3:
1206     case I64VEC4:
1207     case U64VEC2:
1208     case U64VEC3:
1209     case U64VEC4:
1210         afterType = true;
1211         if (parseContext.symbolTable.atBuiltInLevel() ||
1212             parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_int64) ||
1213             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1214             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int64))
1215             return keyword;
1216         return identifierOrType();
1217
1218     case INT8_T:
1219     case UINT8_T:
1220     case I8VEC2:
1221     case I8VEC3:
1222     case I8VEC4:
1223     case U8VEC2:
1224     case U8VEC3:
1225     case U8VEC4:
1226         afterType = true;
1227         if (parseContext.symbolTable.atBuiltInLevel() ||
1228             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1229             parseContext.extensionTurnedOn(E_GL_EXT_shader_8bit_storage) ||
1230             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int8))
1231             return keyword;
1232         return identifierOrType();
1233
1234     case INT16_T:
1235     case UINT16_T:
1236     case I16VEC2:
1237     case I16VEC3:
1238     case I16VEC4:
1239     case U16VEC2:
1240     case U16VEC3:
1241     case U16VEC4:
1242         afterType = true;
1243         if (parseContext.symbolTable.atBuiltInLevel() ||
1244             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_int16) ||
1245             parseContext.extensionTurnedOn(E_GL_EXT_shader_16bit_storage) ||
1246             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1247             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int16))
1248             return keyword;
1249         return identifierOrType();
1250     case INT32_T:
1251     case UINT32_T:
1252     case I32VEC2:
1253     case I32VEC3:
1254     case I32VEC4:
1255     case U32VEC2:
1256     case U32VEC3:
1257     case U32VEC4:
1258         afterType = true;
1259         if (parseContext.symbolTable.atBuiltInLevel() ||
1260             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1261             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int32))
1262             return keyword;
1263         return identifierOrType();
1264     case FLOAT32_T:
1265     case F32VEC2:
1266     case F32VEC3:
1267     case F32VEC4:
1268     case F32MAT2:
1269     case F32MAT3:
1270     case F32MAT4:
1271     case F32MAT2X2:
1272     case F32MAT2X3:
1273     case F32MAT2X4:
1274     case F32MAT3X2:
1275     case F32MAT3X3:
1276     case F32MAT3X4:
1277     case F32MAT4X2:
1278     case F32MAT4X3:
1279     case F32MAT4X4:
1280         afterType = true;
1281         if (parseContext.symbolTable.atBuiltInLevel() ||
1282             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1283             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float32))
1284             return keyword;
1285         return identifierOrType();
1286
1287     case FLOAT64_T:
1288     case F64VEC2:
1289     case F64VEC3:
1290     case F64VEC4:
1291     case F64MAT2:
1292     case F64MAT3:
1293     case F64MAT4:
1294     case F64MAT2X2:
1295     case F64MAT2X3:
1296     case F64MAT2X4:
1297     case F64MAT3X2:
1298     case F64MAT3X3:
1299     case F64MAT3X4:
1300     case F64MAT4X2:
1301     case F64MAT4X3:
1302     case F64MAT4X4:
1303         afterType = true;
1304         if (parseContext.symbolTable.atBuiltInLevel() ||
1305             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1306             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float64))
1307             return keyword;
1308         return identifierOrType();
1309
1310     case FLOAT16_T:
1311     case F16VEC2:
1312     case F16VEC3:
1313     case F16VEC4:
1314         afterType = true;
1315         if (parseContext.symbolTable.atBuiltInLevel() ||
1316             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float) ||
1317             parseContext.extensionTurnedOn(E_GL_EXT_shader_16bit_storage) ||
1318             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1319             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float16))
1320             return keyword;
1321
1322         return identifierOrType();
1323
1324     case F16MAT2:
1325     case F16MAT3:
1326     case F16MAT4:
1327     case F16MAT2X2:
1328     case F16MAT2X3:
1329     case F16MAT2X4:
1330     case F16MAT3X2:
1331     case F16MAT3X3:
1332     case F16MAT3X4:
1333     case F16MAT4X2:
1334     case F16MAT4X3:
1335     case F16MAT4X4:
1336         afterType = true;
1337         if (parseContext.symbolTable.atBuiltInLevel() ||
1338             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float) ||
1339             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||
1340             parseContext.extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float16))
1341             return keyword;
1342
1343         return identifierOrType();
1344
1345     case SAMPLERCUBEARRAY:
1346     case SAMPLERCUBEARRAYSHADOW:
1347     case ISAMPLERCUBEARRAY:
1348     case USAMPLERCUBEARRAY:
1349         afterType = true;
1350         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1351             parseContext.extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))
1352             return keyword;
1353         if (parseContext.isEsProfile() || (parseContext.version < 400 && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_cube_map_array)))
1354             reservedWord();
1355         return keyword;
1356
1357     case TEXTURECUBEARRAY:
1358     case ITEXTURECUBEARRAY:
1359     case UTEXTURECUBEARRAY:
1360         if (parseContext.spvVersion.vulkan > 0)
1361             return keyword;
1362         else
1363             return identifierOrType();
1364 #endif
1365
1366     case UINT:
1367     case UVEC2:
1368     case UVEC3:
1369     case UVEC4:
1370     case SAMPLERCUBESHADOW:
1371     case SAMPLER2DARRAY:
1372     case SAMPLER2DARRAYSHADOW:
1373     case ISAMPLER2D:
1374     case ISAMPLER3D:
1375     case ISAMPLERCUBE:
1376     case ISAMPLER2DARRAY:
1377     case USAMPLER2D:
1378     case USAMPLER3D:
1379     case USAMPLERCUBE:
1380     case USAMPLER2DARRAY:
1381         afterType = true;
1382         return nonreservedKeyword(300, 130);
1383
1384     case SAMPLER3D:
1385         afterType = true;
1386         if (parseContext.isEsProfile() && parseContext.version < 300) {
1387             if (!parseContext.extensionTurnedOn(E_GL_OES_texture_3D))
1388                 reservedWord();
1389         }
1390         return keyword;
1391
1392     case SAMPLER2DSHADOW:
1393         afterType = true;
1394         if (parseContext.isEsProfile() && parseContext.version < 300) {
1395             if (!parseContext.extensionTurnedOn(E_GL_EXT_shadow_samplers))
1396                 reservedWord();
1397         }
1398         return keyword;
1399
1400     case TEXTURE2D:
1401     case TEXTURECUBE:
1402     case TEXTURE2DARRAY:
1403     case ITEXTURE2D:
1404     case ITEXTURE3D:
1405     case ITEXTURECUBE:
1406     case ITEXTURE2DARRAY:
1407     case UTEXTURE2D:
1408     case UTEXTURE3D:
1409     case UTEXTURECUBE:
1410     case UTEXTURE2DARRAY:
1411     case TEXTURE3D:
1412     case SAMPLER:
1413     case SAMPLERSHADOW:
1414         if (parseContext.spvVersion.vulkan > 0)
1415             return keyword;
1416         else
1417             return identifierOrType();
1418
1419 #ifndef GLSLANG_WEB
1420     case ISAMPLER1D:
1421     case ISAMPLER1DARRAY:
1422     case SAMPLER1DARRAYSHADOW:
1423     case USAMPLER1D:
1424     case USAMPLER1DARRAY:
1425         afterType = true;
1426         return es30ReservedFromGLSL(130);
1427     case ISAMPLER2DRECT:
1428     case USAMPLER2DRECT:
1429         afterType = true;
1430         return es30ReservedFromGLSL(140);
1431
1432     case SAMPLERBUFFER:
1433         afterType = true;
1434         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1435             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1436             return keyword;
1437         return es30ReservedFromGLSL(130);
1438
1439     case ISAMPLERBUFFER:
1440     case USAMPLERBUFFER:
1441         afterType = true;
1442         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1443             parseContext.extensionsTurnedOn(Num_AEP_texture_buffer, AEP_texture_buffer))
1444             return keyword;
1445         return es30ReservedFromGLSL(140);
1446
1447     case SAMPLER2DMS:
1448     case ISAMPLER2DMS:
1449     case USAMPLER2DMS:
1450         afterType = true;
1451         if (parseContext.isEsProfile() && parseContext.version >= 310)
1452             return keyword;
1453         if (!parseContext.isEsProfile() && (parseContext.version > 140 ||
1454             (parseContext.version == 140 && parseContext.extensionsTurnedOn(1, &E_GL_ARB_texture_multisample))))
1455             return keyword;
1456         return es30ReservedFromGLSL(150);
1457
1458     case SAMPLER2DMSARRAY:
1459     case ISAMPLER2DMSARRAY:
1460     case USAMPLER2DMSARRAY:
1461         afterType = true;
1462         if ((parseContext.isEsProfile() && parseContext.version >= 320) ||
1463             parseContext.extensionsTurnedOn(1, &E_GL_OES_texture_storage_multisample_2d_array))
1464             return keyword;
1465         if (!parseContext.isEsProfile() && (parseContext.version > 140 ||
1466             (parseContext.version == 140 && parseContext.extensionsTurnedOn(1, &E_GL_ARB_texture_multisample))))
1467             return keyword;
1468         return es30ReservedFromGLSL(150);
1469
1470     case SAMPLER1D:
1471     case SAMPLER1DSHADOW:
1472         afterType = true;
1473         if (parseContext.isEsProfile())
1474             reservedWord();
1475         return keyword;
1476
1477     case SAMPLER2DRECT:
1478     case SAMPLER2DRECTSHADOW:
1479         afterType = true;
1480         if (parseContext.isEsProfile())
1481             reservedWord();
1482         else if (parseContext.version < 140 && ! parseContext.symbolTable.atBuiltInLevel() && ! parseContext.extensionTurnedOn(E_GL_ARB_texture_rectangle)) {
1483             if (parseContext.relaxedErrors())
1484                 parseContext.requireExtensions(loc, 1, &E_GL_ARB_texture_rectangle, "texture-rectangle sampler keyword");
1485             else
1486                 reservedWord();
1487         }
1488         return keyword;
1489
1490     case SAMPLER1DARRAY:
1491         afterType = true;
1492         if (parseContext.isEsProfile() && parseContext.version == 300)
1493             reservedWord();
1494         else if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1495                  (!parseContext.isEsProfile() && parseContext.version < 130))
1496             return identifierOrType();
1497         return keyword;
1498
1499     case SAMPLEREXTERNALOES:
1500         afterType = true;
1501         if (parseContext.symbolTable.atBuiltInLevel() ||
1502             parseContext.extensionTurnedOn(E_GL_OES_EGL_image_external) ||
1503             parseContext.extensionTurnedOn(E_GL_OES_EGL_image_external_essl3))
1504             return keyword;
1505         return identifierOrType();
1506
1507     case SAMPLEREXTERNAL2DY2YEXT:
1508         afterType = true;
1509         if (parseContext.symbolTable.atBuiltInLevel() ||
1510             parseContext.extensionTurnedOn(E_GL_EXT_YUV_target))
1511             return keyword;
1512         return identifierOrType();
1513
1514     case ITEXTURE1DARRAY:
1515     case UTEXTURE1D:
1516     case ITEXTURE1D:
1517     case UTEXTURE1DARRAY:
1518     case TEXTUREBUFFER:
1519     case ITEXTURE2DRECT:
1520     case UTEXTURE2DRECT:
1521     case ITEXTUREBUFFER:
1522     case UTEXTUREBUFFER:
1523     case TEXTURE2DMS:
1524     case ITEXTURE2DMS:
1525     case UTEXTURE2DMS:
1526     case TEXTURE2DMSARRAY:
1527     case ITEXTURE2DMSARRAY:
1528     case UTEXTURE2DMSARRAY:
1529     case TEXTURE1D:
1530     case TEXTURE2DRECT:
1531     case TEXTURE1DARRAY:
1532         if (parseContext.spvVersion.vulkan > 0)
1533             return keyword;
1534         else
1535             return identifierOrType();
1536
1537     case SUBPASSINPUT:
1538     case SUBPASSINPUTMS:
1539     case ISUBPASSINPUT:
1540     case ISUBPASSINPUTMS:
1541     case USUBPASSINPUT:
1542     case USUBPASSINPUTMS:
1543         if (parseContext.spvVersion.vulkan > 0)
1544             return keyword;
1545         else
1546             return identifierOrType();
1547
1548     case F16SAMPLER1D:
1549     case F16SAMPLER2D:
1550     case F16SAMPLER3D:
1551     case F16SAMPLER2DRECT:
1552     case F16SAMPLERCUBE:
1553     case F16SAMPLER1DARRAY:
1554     case F16SAMPLER2DARRAY:
1555     case F16SAMPLERCUBEARRAY:
1556     case F16SAMPLERBUFFER:
1557     case F16SAMPLER2DMS:
1558     case F16SAMPLER2DMSARRAY:
1559     case F16SAMPLER1DSHADOW:
1560     case F16SAMPLER2DSHADOW:
1561     case F16SAMPLER1DARRAYSHADOW:
1562     case F16SAMPLER2DARRAYSHADOW:
1563     case F16SAMPLER2DRECTSHADOW:
1564     case F16SAMPLERCUBESHADOW:
1565     case F16SAMPLERCUBEARRAYSHADOW:
1566
1567     case F16IMAGE1D:
1568     case F16IMAGE2D:
1569     case F16IMAGE3D:
1570     case F16IMAGE2DRECT:
1571     case F16IMAGECUBE:
1572     case F16IMAGE1DARRAY:
1573     case F16IMAGE2DARRAY:
1574     case F16IMAGECUBEARRAY:
1575     case F16IMAGEBUFFER:
1576     case F16IMAGE2DMS:
1577     case F16IMAGE2DMSARRAY:
1578
1579     case F16TEXTURE1D:
1580     case F16TEXTURE2D:
1581     case F16TEXTURE3D:
1582     case F16TEXTURE2DRECT:
1583     case F16TEXTURECUBE:
1584     case F16TEXTURE1DARRAY:
1585     case F16TEXTURE2DARRAY:
1586     case F16TEXTURECUBEARRAY:
1587     case F16TEXTUREBUFFER:
1588     case F16TEXTURE2DMS:
1589     case F16TEXTURE2DMSARRAY:
1590
1591     case F16SUBPASSINPUT:
1592     case F16SUBPASSINPUTMS:
1593         afterType = true;
1594         if (parseContext.symbolTable.atBuiltInLevel() ||
1595             parseContext.extensionTurnedOn(E_GL_AMD_gpu_shader_half_float_fetch))
1596             return keyword;
1597         return identifierOrType();
1598
1599     case EXPLICITINTERPAMD:
1600         if (parseContext.extensionTurnedOn(E_GL_AMD_shader_explicit_vertex_parameter))
1601             return keyword;
1602         return identifierOrType();
1603
1604     case PERVERTEXNV:
1605         if ((!parseContext.isEsProfile() && parseContext.version >= 450) ||
1606             parseContext.extensionTurnedOn(E_GL_NV_fragment_shader_barycentric))
1607             return keyword;
1608         return identifierOrType();
1609
1610     case PRECISE:
1611         if ((parseContext.isEsProfile() &&
1612              (parseContext.version >= 320 || parseContext.extensionsTurnedOn(Num_AEP_gpu_shader5, AEP_gpu_shader5))) ||
1613             (!parseContext.isEsProfile() && parseContext.version >= 400))
1614             return keyword;
1615         if (parseContext.isEsProfile() && parseContext.version == 310) {
1616             reservedWord();
1617             return keyword;
1618         }
1619         return identifierOrType();
1620
1621     case PERPRIMITIVENV:
1622     case PERVIEWNV:
1623     case PERTASKNV:
1624         if ((!parseContext.isEsProfile() && parseContext.version >= 450) ||
1625             (parseContext.isEsProfile() && parseContext.version >= 320) ||
1626             parseContext.extensionTurnedOn(E_GL_NV_mesh_shader))
1627             return keyword;
1628         return identifierOrType();
1629
1630     case FCOOPMATNV:
1631         afterType = true;
1632         if (parseContext.symbolTable.atBuiltInLevel() ||
1633             parseContext.extensionTurnedOn(E_GL_NV_cooperative_matrix))
1634             return keyword;
1635         return identifierOrType();
1636
1637     case UCOOPMATNV:
1638     case ICOOPMATNV:
1639         afterType = true;
1640         if (parseContext.symbolTable.atBuiltInLevel() ||
1641             parseContext.extensionTurnedOn(E_GL_NV_integer_cooperative_matrix))
1642             return keyword;
1643         return identifierOrType();
1644
1645     case DEMOTE:
1646         if (parseContext.extensionTurnedOn(E_GL_EXT_demote_to_helper_invocation))
1647             return keyword;
1648         else
1649             return identifierOrType();
1650 #endif
1651
1652     default:
1653         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown glslang keyword", loc);
1654         return 0;
1655     }
1656 }
1657
1658 int TScanContext::identifierOrType()
1659 {
1660     parserToken->sType.lex.string = NewPoolTString(tokenText);
1661     if (field)
1662         return IDENTIFIER;
1663
1664     parserToken->sType.lex.symbol = parseContext.symbolTable.find(*parserToken->sType.lex.string);
1665     if ((afterType == false && afterStruct == false) && parserToken->sType.lex.symbol != nullptr) {
1666         if (const TVariable* variable = parserToken->sType.lex.symbol->getAsVariable()) {
1667             if (variable->isUserType() &&
1668                 // treat redeclaration of forward-declared buffer/uniform reference as an identifier
1669                 !(variable->getType().isReference() && afterBuffer)) {
1670                 afterType = true;
1671
1672                 return TYPE_NAME;
1673             }
1674         }
1675     }
1676
1677     return IDENTIFIER;
1678 }
1679
1680 // Give an error for use of a reserved symbol.
1681 // However, allow built-in declarations to use reserved words, to allow
1682 // extension support before the extension is enabled.
1683 int TScanContext::reservedWord()
1684 {
1685     if (! parseContext.symbolTable.atBuiltInLevel())
1686         parseContext.error(loc, "Reserved word.", tokenText, "", "");
1687
1688     return 0;
1689 }
1690
1691 int TScanContext::identifierOrReserved(bool reserved)
1692 {
1693     if (reserved) {
1694         reservedWord();
1695
1696         return 0;
1697     }
1698
1699     if (parseContext.isForwardCompatible())
1700         parseContext.warn(loc, "using future reserved keyword", tokenText, "");
1701
1702     return identifierOrType();
1703 }
1704
1705 // For keywords that suddenly showed up on non-ES (not previously reserved)
1706 // but then got reserved by ES 3.0.
1707 int TScanContext::es30ReservedFromGLSL(int version)
1708 {
1709     if (parseContext.symbolTable.atBuiltInLevel())
1710         return keyword;
1711
1712     if ((parseContext.isEsProfile() && parseContext.version < 300) ||
1713         (!parseContext.isEsProfile() && parseContext.version < version)) {
1714             if (parseContext.isForwardCompatible())
1715                 parseContext.warn(loc, "future reserved word in ES 300 and keyword in GLSL", tokenText, "");
1716
1717             return identifierOrType();
1718     } else if (parseContext.isEsProfile() && parseContext.version >= 300)
1719         reservedWord();
1720
1721     return keyword;
1722 }
1723
1724 // For a keyword that was never reserved, until it suddenly
1725 // showed up, both in an es version and a non-ES version.
1726 int TScanContext::nonreservedKeyword(int esVersion, int nonEsVersion)
1727 {
1728     if ((parseContext.isEsProfile() && parseContext.version < esVersion) ||
1729         (!parseContext.isEsProfile() && parseContext.version < nonEsVersion)) {
1730         if (parseContext.isForwardCompatible())
1731             parseContext.warn(loc, "using future keyword", tokenText, "");
1732
1733         return identifierOrType();
1734     }
1735
1736     return keyword;
1737 }
1738
1739 int TScanContext::precisionKeyword()
1740 {
1741     if (parseContext.isEsProfile() || parseContext.version >= 130)
1742         return keyword;
1743
1744     if (parseContext.isForwardCompatible())
1745         parseContext.warn(loc, "using ES precision qualifier keyword", tokenText, "");
1746
1747     return identifierOrType();
1748 }
1749
1750 int TScanContext::matNxM()
1751 {
1752     afterType = true;
1753
1754     if (parseContext.version > 110)
1755         return keyword;
1756
1757     if (parseContext.isForwardCompatible())
1758         parseContext.warn(loc, "using future non-square matrix type keyword", tokenText, "");
1759
1760     return identifierOrType();
1761 }
1762
1763 int TScanContext::dMat()
1764 {
1765     afterType = true;
1766
1767     if (parseContext.isEsProfile() && parseContext.version >= 300) {
1768         reservedWord();
1769
1770         return keyword;
1771     }
1772
1773     if (!parseContext.isEsProfile() && (parseContext.version >= 400 ||
1774         parseContext.symbolTable.atBuiltInLevel() ||
1775         (parseContext.version >= 150 && parseContext.extensionTurnedOn(E_GL_ARB_gpu_shader_fp64))))
1776         return keyword;
1777
1778     if (parseContext.isForwardCompatible())
1779         parseContext.warn(loc, "using future type keyword", tokenText, "");
1780
1781     return identifierOrType();
1782 }
1783
1784 int TScanContext::firstGenerationImage(bool inEs310)
1785 {
1786     if (parseContext.symbolTable.atBuiltInLevel() ||
1787         (!parseContext.isEsProfile() && (parseContext.version >= 420 ||
1788          parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))) ||
1789         (inEs310 && parseContext.isEsProfile() && parseContext.version >= 310))
1790         return keyword;
1791
1792     if ((parseContext.isEsProfile() && parseContext.version >= 300) ||
1793         (!parseContext.isEsProfile() && parseContext.version >= 130)) {
1794         reservedWord();
1795
1796         return keyword;
1797     }
1798
1799     if (parseContext.isForwardCompatible())
1800         parseContext.warn(loc, "using future type keyword", tokenText, "");
1801
1802     return identifierOrType();
1803 }
1804
1805 int TScanContext::secondGenerationImage()
1806 {
1807     if (parseContext.isEsProfile() && parseContext.version >= 310) {
1808         reservedWord();
1809         return keyword;
1810     }
1811
1812     if (parseContext.symbolTable.atBuiltInLevel() ||
1813         (!parseContext.isEsProfile() &&
1814          (parseContext.version >= 420 || parseContext.extensionTurnedOn(E_GL_ARB_shader_image_load_store))))
1815         return keyword;
1816
1817     if (parseContext.isForwardCompatible())
1818         parseContext.warn(loc, "using future type keyword", tokenText, "");
1819
1820     return identifierOrType();
1821 }
1822
1823 } // end namespace glslang