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