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