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