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