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