Reframe the preprocessor as a C++ class, with instances, removing all C code, removin...
[platform/upstream/glslang.git] / glslang / MachineIndependent / Scan.cpp
1 //
2 //Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 //Copyright (C) 2013 LunarG, Inc.
4 //
5 //All rights reserved.
6 //
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
9 //are met:
10 //
11 //    Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 //
14 //    Redistributions in binary form must reproduce the above
15 //    copyright notice, this list of conditions and the following
16 //    disclaimer in the documentation and/or other materials provided
17 //    with the distribution.
18 //
19 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 //    contributors may be used to endorse or promote products derived
21 //    from this software without specific prior written permission.
22 //
23 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //POSSIBILITY OF SUCH DAMAGE.
35 //
36
37 //
38 // GLSL scanning, leveraging the scanning done by the preprocessor.
39 //
40
41 #include <string.h>
42
43 #include "Scan.h"
44 #include "Include/Types.h"
45 #include "SymbolTable.h"
46 #include "glslang_tab.cpp.h"
47 #include "ParseHelper.h"
48 #include "ScanContext.h"
49
50 // preprocessor includes
51 #include "preprocessor/PpContext.h"
52 #include "preprocessor/PpTokens.h"
53
54 namespace glslang {
55     
56 // read past any white space
57 void ConsumeWhiteSpace(TInputScanner& input, bool& foundNonSpaceTab)
58 {
59     char c = input.peek();  // don't accidentally consume anything other than whitespace
60     while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
61         if (c == '\r' || c == '\n')
62             foundNonSpaceTab = true;
63         input.get();
64         c = input.peek();
65     }
66 }
67
68 // return true if a comment was actually consumed
69 bool ConsumeComment(TInputScanner& input)
70 {
71     if (input.peek() != '/')
72         return false;
73
74     input.get();  // consume the '/'
75     char c = input.peek();
76     if (c == '/') {
77
78         // a '//' style comment
79         input.get();  // consume the second '/'
80         c = input.get();
81         do {
82             while (c > 0 && c != '\\' && c != '\r' && c != '\n')
83                 c = input.get();
84
85             if (c <= 0 || c == '\r' || c == '\n') {
86                 while (c == '\r' || c == '\n')
87                     c = input.get();
88
89                 // we reached the end of the comment
90                 break;
91             } else {
92                 // it's a '\', so we need to keep going, after skipping what's escaped
93                     
94                 // read the skipped character
95                 c = input.get();
96
97                 // if it's a two-character newline, skip both characters
98                 if (c == '\r' && input.peek() == '\n')
99                     input.get();
100                 c = input.get();
101             }
102         } while (true);
103
104         // put back the last non-comment character
105         if (c > 0)
106             input.unget();
107
108         return true;
109     } else if (c == '*') {
110
111         // a '/*' style comment
112         input.get();  // consume the '*'
113         c = input.get();
114         do {
115             while (c > 0 && c != '*')
116                 c = input.get();
117             if (c == '*') {
118                 c = input.get();
119                 if (c == '/')
120                     break;  // end of comment
121                 // not end of comment
122             } else // end of input
123                 break;
124         } while (true);
125
126         return true;
127     } else {
128         // it's not a comment, put the '/' back
129         input.unget();
130
131         return false;
132     }
133 }
134
135 // skip whitespace, then skip a comment, rinse, repeat
136 void ConsumeWhitespaceComment(TInputScanner& input, bool& foundNonSpaceTab)
137 {
138     do {
139         ConsumeWhiteSpace(input, foundNonSpaceTab);
140  
141         // if not starting a comment now, then done
142         char c = input.peek();
143         if (c != '/' || c < 0)
144             return;
145
146         // skip potential comment 
147         foundNonSpaceTab = true;
148         if (! ConsumeComment(input))
149             return;
150
151     } while (true);
152 }
153
154 // Returns true if there was non-white space (e.g., a comment, newline) before the #version
155 // or no #version was found; otherwise, returns false.  There is no error case, it always
156 // succeeds, but will leave version == 0 if no #version was found.
157 //
158 // N.B. does not attempt to leave input in any particular known state.  The assumption
159 // is that scanning will start anew, following the rules for the chosen version/profile,
160 // and with a corresponding parsing context.
161 //
162 bool ScanVersion(TInputScanner& input, int& version, EProfile& profile)
163 {
164     // This function doesn't have to get all the semantics correct, 
165     // just find the #version if there is a correct one present.
166     // The preprocessor will have the responsibility of getting all the semantics right.
167
168     version = 0;  // means not found
169     profile = ENoProfile;
170
171     bool foundNonSpaceTab = false;
172     ConsumeWhitespaceComment(input, foundNonSpaceTab);
173
174     // #
175     if (input.get() != '#')
176         return true;
177
178     // whitespace
179     char c;
180     do {
181         c = input.get();
182     } while (c == ' ' || c == '\t');
183
184     if (          c != 'v' ||
185         input.get() != 'e' ||
186         input.get() != 'r' ||
187         input.get() != 's' ||
188         input.get() != 'i' ||
189         input.get() != 'o' ||
190         input.get() != 'n')
191         return true;
192
193     // whitespace
194     do {
195         c = input.get();
196     } while (c == ' ' || c == '\t');
197
198     // version number
199     while (c >= '0' && c <= '9') {
200         version = 10 * version + (c - '0');
201         c = input.get();
202     }
203     if (version == 0)
204         return true;
205     
206     // whitespace
207     while (c == ' ' || c == '\t')
208         c = input.get();
209
210     // profile
211     const int maxProfileLength = 13;  // not including any 0
212     char profileString[maxProfileLength];
213     int profileLength;
214     for (profileLength = 0; profileLength < maxProfileLength; ++profileLength) {
215         if (c < 0 || c == ' ' || c == '\t' || c == '\n' || c == '\r')
216             break;
217         profileString[profileLength] = c;
218         c = input.get();
219     }
220     if (c > 0 && c != ' ' && c != '\t' && c != '\n' && c != '\r')
221         return true;
222
223     if (profileLength == 2 && strncmp(profileString, "es", profileLength) == 0)
224         profile = EEsProfile;
225     else if (profileLength == 4 && strncmp(profileString, "core", profileLength) == 0)
226         profile = ECoreProfile;
227     else if (profileLength == 13 && strncmp(profileString, "compatibility", profileLength) == 0)
228         profile = ECompatibilityProfile;
229
230     return foundNonSpaceTab;
231 }
232
233 }; // end glslang namespace
234
235 namespace glslang {
236
237 // Fill this in when doing glslang-level scanning, to hand back to the parser.
238 class TParserToken {
239 public:
240     explicit TParserToken(YYSTYPE& b) : sType(b) { }
241
242     YYSTYPE& sType;
243 };
244
245 };  // end namespace glslang
246
247 // This is the function the glslang parser (i.e., bison) calls to get its next token
248 int yylex(YYSTYPE* glslangTokenDesc, TParseContext& parseContext)
249 {
250     glslang::TParserToken token(*glslangTokenDesc);
251
252     return parseContext.scanContext->tokenize(parseContext.ppContext, token);
253 }
254
255 namespace {
256
257 // A single global usable by all threads, by all versions, by all languages.
258 // After a single process-level initialization, this is read only and thread safe
259 std::map<std::string, int>* KeywordMap = 0;
260 std::set<std::string>* ReservedSet = 0;
261
262 };
263
264 namespace glslang {
265
266 void TScanContext::fillInKeywordMap()
267 {
268     if (KeywordMap != 0) {
269         // this is really an error, as this should called only once per process
270         // but, the only risk is if two threads called simultaneously
271         return;
272     }
273     KeywordMap = new std::map<std::string, int>;
274
275     (*KeywordMap)["const"] =                   CONST;
276     (*KeywordMap)["uniform"] =                 UNIFORM;
277     (*KeywordMap)["in"] =                      IN;
278     (*KeywordMap)["out"] =                     OUT;
279     (*KeywordMap)["inout"] =                   INOUT;
280     (*KeywordMap)["struct"] =                  STRUCT;
281     (*KeywordMap)["break"] =                   BREAK;
282     (*KeywordMap)["continue"] =                CONTINUE;
283     (*KeywordMap)["do"] =                      DO;
284     (*KeywordMap)["for"] =                     FOR;
285     (*KeywordMap)["while"] =                   WHILE;
286     (*KeywordMap)["switch"] =                  SWITCH;
287     (*KeywordMap)["case"] =                    CASE;
288     (*KeywordMap)["default"] =                 DEFAULT;
289     (*KeywordMap)["if"] =                      IF;
290     (*KeywordMap)["else"] =                    ELSE;
291     (*KeywordMap)["discard"] =                 DISCARD;
292     (*KeywordMap)["return"] =                  RETURN;
293     (*KeywordMap)["void"] =                    VOID;
294     (*KeywordMap)["bool"] =                    BOOL;
295     (*KeywordMap)["float"] =                   FLOAT;
296     (*KeywordMap)["int"] =                     INT;
297     (*KeywordMap)["bvec2"] =                   BVEC2;
298     (*KeywordMap)["bvec3"] =                   BVEC3;
299     (*KeywordMap)["bvec4"] =                   BVEC4;
300     (*KeywordMap)["vec2"] =                    VEC2;
301     (*KeywordMap)["vec3"] =                    VEC3;
302     (*KeywordMap)["vec4"] =                    VEC4;
303     (*KeywordMap)["ivec2"] =                   IVEC2;
304     (*KeywordMap)["ivec3"] =                   IVEC3;
305     (*KeywordMap)["ivec4"] =                   IVEC4;
306     (*KeywordMap)["mat2"] =                    MAT2;
307     (*KeywordMap)["mat3"] =                    MAT3;
308     (*KeywordMap)["mat4"] =                    MAT4;
309     (*KeywordMap)["sampler2D"] =               SAMPLER2D;
310     (*KeywordMap)["samplerCube"] =             SAMPLERCUBE;
311     (*KeywordMap)["true"] =                    BOOLCONSTANT;
312     (*KeywordMap)["false"] =                   BOOLCONSTANT;
313     (*KeywordMap)["attribute"] =               ATTRIBUTE;
314     (*KeywordMap)["varying"] =                 VARYING;
315     (*KeywordMap)["buffer"] =                  BUFFER;
316     (*KeywordMap)["coherent"] =                COHERENT;
317     (*KeywordMap)["restrict"] =                RESTRICT;
318     (*KeywordMap)["readonly"] =                READONLY;
319     (*KeywordMap)["writeonly"] =               WRITEONLY;
320     (*KeywordMap)["atomic_uint"] =             ATOMIC_UINT;
321     (*KeywordMap)["volatile"] =                VOLATILE;
322     (*KeywordMap)["layout"] =                  LAYOUT;
323     (*KeywordMap)["shared"] =                  SHARED;
324     (*KeywordMap)["patch"] =                   PATCH;
325     (*KeywordMap)["sample"] =                  SAMPLE;
326     (*KeywordMap)["subroutine"] =              SUBROUTINE;
327     (*KeywordMap)["highp"] =                   HIGH_PRECISION;
328     (*KeywordMap)["mediump"] =                 MEDIUM_PRECISION;
329     (*KeywordMap)["lowp"] =                    LOW_PRECISION;
330     (*KeywordMap)["precision"] =               PRECISION;
331     (*KeywordMap)["mat2x2"] =                  MAT2X2;
332     (*KeywordMap)["mat2x3"] =                  MAT2X3;
333     (*KeywordMap)["mat2x4"] =                  MAT2X4;
334     (*KeywordMap)["mat3x2"] =                  MAT3X2;
335     (*KeywordMap)["mat3x3"] =                  MAT3X3;
336     (*KeywordMap)["mat3x4"] =                  MAT3X4;
337     (*KeywordMap)["mat4x2"] =                  MAT4X2;
338     (*KeywordMap)["mat4x3"] =                  MAT4X3;
339     (*KeywordMap)["mat4x4"] =                  MAT4X4;
340     (*KeywordMap)["dmat2"] =                   DMAT2;
341     (*KeywordMap)["dmat3"] =                   DMAT3;
342     (*KeywordMap)["dmat4"] =                   DMAT4;
343     (*KeywordMap)["dmat2x2"] =                 DMAT2X2;
344     (*KeywordMap)["dmat2x3"] =                 DMAT2X3;
345     (*KeywordMap)["dmat2x4"] =                 DMAT2X4;
346     (*KeywordMap)["dmat3x2"] =                 DMAT3X2;
347     (*KeywordMap)["dmat3x3"] =                 DMAT3X3;
348     (*KeywordMap)["dmat3x4"] =                 DMAT3X4;
349     (*KeywordMap)["dmat4x2"] =                 DMAT4X2;
350     (*KeywordMap)["dmat4x3"] =                 DMAT4X3;
351     (*KeywordMap)["dmat4x4"] =                 DMAT4X4;
352     (*KeywordMap)["image1D"] =                 IMAGE1D;
353     (*KeywordMap)["iimage1D"] =                IIMAGE1D;
354     (*KeywordMap)["uimage1D"] =                UIMAGE1D;
355     (*KeywordMap)["image2D"] =                 IMAGE2D;
356     (*KeywordMap)["iimage2D"] =                IIMAGE2D;
357     (*KeywordMap)["uimage2D"] =                UIMAGE2D;
358     (*KeywordMap)["image3D"] =                 IMAGE3D;
359     (*KeywordMap)["iimage3D"] =                IIMAGE3D;
360     (*KeywordMap)["uimage3D"] =                UIMAGE3D;
361     (*KeywordMap)["image2DRect"] =             IMAGE2DRECT;
362     (*KeywordMap)["iimage2DRect"] =            IIMAGE2DRECT;
363     (*KeywordMap)["uimage2DRect"] =            UIMAGE2DRECT;
364     (*KeywordMap)["imageCube"] =               IMAGECUBE;
365     (*KeywordMap)["iimageCube"] =              IIMAGECUBE;
366     (*KeywordMap)["uimageCube"] =              UIMAGECUBE;
367     (*KeywordMap)["imageBuffer"] =             IMAGEBUFFER;
368     (*KeywordMap)["iimageBuffer"] =            IIMAGEBUFFER;
369     (*KeywordMap)["uimageBuffer"] =            UIMAGEBUFFER;
370     (*KeywordMap)["image1DArray"] =            IMAGE1DARRAY;
371     (*KeywordMap)["iimage1DArray"] =           IIMAGE1DARRAY;
372     (*KeywordMap)["uimage1DArray"] =           UIMAGE1DARRAY;
373     (*KeywordMap)["image2DArray"] =            IMAGE2DARRAY;
374     (*KeywordMap)["iimage2DArray"] =           IIMAGE2DARRAY;
375     (*KeywordMap)["uimage2DArray"] =           UIMAGE2DARRAY;
376     (*KeywordMap)["imageCubeArray"] =          IMAGECUBEARRAY;
377     (*KeywordMap)["iimageCubeArray"] =         IIMAGECUBEARRAY;
378     (*KeywordMap)["uimageCubeArray"] =         UIMAGECUBEARRAY;
379     (*KeywordMap)["image2DMS"] =               IMAGE2DMS;
380     (*KeywordMap)["iimage2DMS"] =              IIMAGE2DMS;
381     (*KeywordMap)["uimage2DMS"] =              UIMAGE2DMS;
382     (*KeywordMap)["image2DMSArray"] =          IMAGE2DMSARRAY;
383     (*KeywordMap)["iimage2DMSArray"] =         IIMAGE2DMSARRAY;
384     (*KeywordMap)["uimage2DMSArray"] =         UIMAGE2DMSARRAY;
385     (*KeywordMap)["double"] =                  DOUBLE;
386     (*KeywordMap)["dvec2"] =                   DVEC2;
387     (*KeywordMap)["dvec3"] =                   DVEC3;
388     (*KeywordMap)["dvec4"] =                   DVEC4;
389     (*KeywordMap)["samplerCubeArray"] =        SAMPLERCUBEARRAY;
390     (*KeywordMap)["samplerCubeArrayShadow"] =  SAMPLERCUBEARRAYSHADOW;
391     (*KeywordMap)["isamplerCubeArray"] =       ISAMPLERCUBEARRAY;
392     (*KeywordMap)["usamplerCubeArray"] =       USAMPLERCUBEARRAY;
393     (*KeywordMap)["sampler1DArrayShadow"] =    SAMPLER1DARRAYSHADOW;
394     (*KeywordMap)["isampler1DArray"] =         ISAMPLER1DARRAY;
395     (*KeywordMap)["usampler1D"] =              USAMPLER1D;
396     (*KeywordMap)["isampler1D"] =              ISAMPLER1D;
397     (*KeywordMap)["usampler1DArray"] =         USAMPLER1DARRAY;
398     (*KeywordMap)["samplerBuffer"] =           SAMPLERBUFFER;
399     (*KeywordMap)["uint"] =                    UINT;
400     (*KeywordMap)["uvec2"] =                   UVEC2;
401     (*KeywordMap)["uvec3"] =                   UVEC3;
402     (*KeywordMap)["uvec4"] =                   UVEC4;
403     (*KeywordMap)["samplerCubeShadow"] =       SAMPLERCUBESHADOW;
404     (*KeywordMap)["sampler2DArray"] =          SAMPLER2DARRAY;
405     (*KeywordMap)["sampler2DArrayShadow"] =    SAMPLER2DARRAYSHADOW;
406     (*KeywordMap)["isampler2D"] =              ISAMPLER2D;
407     (*KeywordMap)["isampler3D"] =              ISAMPLER3D;
408     (*KeywordMap)["isamplerCube"] =            ISAMPLERCUBE;
409     (*KeywordMap)["isampler2DArray"] =         ISAMPLER2DARRAY;
410     (*KeywordMap)["usampler2D"] =              USAMPLER2D;
411     (*KeywordMap)["usampler3D"] =              USAMPLER3D;
412     (*KeywordMap)["usamplerCube"] =            USAMPLERCUBE;
413     (*KeywordMap)["usampler2DArray"] =         USAMPLER2DARRAY;
414     (*KeywordMap)["isampler2DRect"] =          ISAMPLER2DRECT;
415     (*KeywordMap)["usampler2DRect"] =          USAMPLER2DRECT;
416     (*KeywordMap)["isamplerBuffer"] =          ISAMPLERBUFFER;
417     (*KeywordMap)["usamplerBuffer"] =          USAMPLERBUFFER;
418     (*KeywordMap)["sampler2DMS"] =             SAMPLER2DMS;
419     (*KeywordMap)["isampler2DMS"] =            ISAMPLER2DMS;
420     (*KeywordMap)["usampler2DMS"] =            USAMPLER2DMS;
421     (*KeywordMap)["sampler2DMSArray"] =        SAMPLER2DMSARRAY;
422     (*KeywordMap)["isampler2DMSArray"] =       ISAMPLER2DMSARRAY;
423     (*KeywordMap)["usampler2DMSArray"] =       USAMPLER2DMSARRAY;
424     (*KeywordMap)["sampler1D"] =               SAMPLER1D;
425     (*KeywordMap)["sampler1DShadow"] =         SAMPLER1DSHADOW;
426     (*KeywordMap)["sampler3D"] =               SAMPLER3D;
427     (*KeywordMap)["sampler2DShadow"] =         SAMPLER2DSHADOW;
428     (*KeywordMap)["sampler2DRect"] =           SAMPLER2DRECT;
429     (*KeywordMap)["sampler2DRectShadow"] =     SAMPLER2DRECTSHADOW;
430     (*KeywordMap)["sampler1DArray"] =          SAMPLER1DARRAY;
431     (*KeywordMap)["noperspective"] =           NOPERSPECTIVE;
432     (*KeywordMap)["smooth"] =                  SMOOTH;
433     (*KeywordMap)["flat"] =                    FLAT;
434     (*KeywordMap)["centroid"] =                CENTROID;
435     (*KeywordMap)["precise"] =                 PRECISE;
436     (*KeywordMap)["invariant"] =               INVARIANT;
437     (*KeywordMap)["packed"] =                  PACKED;
438     (*KeywordMap)["resource"] =                RESOURCE;
439     (*KeywordMap)["superp"] =                  SUPERP;
440
441     ReservedSet = new std::set<std::string>;
442     
443     ReservedSet->insert("common");
444     ReservedSet->insert("partition");
445     ReservedSet->insert("active");
446     ReservedSet->insert("asm");
447     ReservedSet->insert("class");
448     ReservedSet->insert("union");
449     ReservedSet->insert("enum");
450     ReservedSet->insert("typedef");
451     ReservedSet->insert("template");
452     ReservedSet->insert("this");
453     ReservedSet->insert("goto");
454     ReservedSet->insert("inline");
455     ReservedSet->insert("noinline");
456     ReservedSet->insert("public");
457     ReservedSet->insert("static");
458     ReservedSet->insert("extern");
459     ReservedSet->insert("external");
460     ReservedSet->insert("interface");
461     ReservedSet->insert("long");
462     ReservedSet->insert("short");
463     ReservedSet->insert("half");
464     ReservedSet->insert("fixed");
465     ReservedSet->insert("unsigned");
466     ReservedSet->insert("input");
467     ReservedSet->insert("output");
468     ReservedSet->insert("hvec2");
469     ReservedSet->insert("hvec3");
470     ReservedSet->insert("hvec4");
471     ReservedSet->insert("fvec2");
472     ReservedSet->insert("fvec3");
473     ReservedSet->insert("fvec4");
474     ReservedSet->insert("sampler3DRect");
475     ReservedSet->insert("filter");
476     ReservedSet->insert("sizeof");
477     ReservedSet->insert("cast");
478     ReservedSet->insert("namespace");
479     ReservedSet->insert("using");
480 }
481
482 int TScanContext::tokenize(TPpContext* pp, TParserToken& token)
483 {
484     parserToken = &token;
485     TPpToken ppToken;
486     tokenText = pp->tokenize(&ppToken);
487
488     loc = ppToken.loc;
489     parserToken->sType.lex.loc = loc;
490     switch (ppToken.ppToken) {
491     case ';':  afterType = false;   return SEMICOLON;
492     case ',':  afterType = false;   return COMMA;
493     case ':':                       return COLON;
494     case '=':  afterType = false;   return EQUAL;
495     case '(':  afterType = false;   return LEFT_PAREN;
496     case ')':  afterType = false;   return RIGHT_PAREN;
497     case '.':  field = true;        return DOT;
498     case '!':                       return BANG;
499     case '-':                       return DASH;
500     case '~':                       return TILDE;
501     case '+':                       return PLUS;
502     case '*':                       return STAR;
503     case '/':                       return SLASH;
504     case '%':                       return PERCENT;
505     case '<':                       return LEFT_ANGLE;
506     case '>':                       return RIGHT_ANGLE;
507     case '|':                       return VERTICAL_BAR;
508     case '^':                       return CARET;
509     case '&':                       return AMPERSAND;
510     case '?':                       return QUESTION;
511     case '[':                       return LEFT_BRACKET;
512     case ']':                       return RIGHT_BRACKET;
513     case '{':                       return LEFT_BRACE;
514     case '}':                       return RIGHT_BRACE;
515
516     case CPP_AND_OP:                return AND_OP;
517     case CPP_SUB_ASSIGN:            return SUB_ASSIGN;
518     case CPP_MOD_ASSIGN:            return MOD_ASSIGN;
519     case CPP_ADD_ASSIGN:            return ADD_ASSIGN;
520     case CPP_DIV_ASSIGN:            return DIV_ASSIGN;
521     case CPP_MUL_ASSIGN:            return MUL_ASSIGN;
522     case CPP_EQ_OP:                 return EQ_OP;
523     case CPP_XOR_OP:                return XOR_OP;
524     case CPP_GE_OP:                 return GE_OP;
525     case CPP_RIGHT_OP:              return RIGHT_OP;
526     case CPP_LE_OP:                 return LE_OP;
527     case CPP_LEFT_OP:               return LEFT_OP;
528     case CPP_DEC_OP:                return DEC_OP;
529     case CPP_NE_OP:                 return NE_OP;
530     case CPP_OR_OP:                 return OR_OP;
531     case CPP_INC_OP:                return INC_OP;
532     case CPP_RIGHT_ASSIGN:          return RIGHT_ASSIGN;
533     case CPP_LEFT_ASSIGN:           return LEFT_ASSIGN;
534     case CPP_AND_ASSIGN:            return AND_ASSIGN;
535     case CPP_OR_ASSIGN:             return OR_ASSIGN;
536     case CPP_XOR_ASSIGN:            return XOR_ASSIGN;
537                                    
538     case CPP_INTCONSTANT:           parserToken->sType.lex.i = ppToken.ival;        return INTCONSTANT;
539     case CPP_UINTCONSTANT:          parserToken->sType.lex.i = ppToken.ival;        return UINTCONSTANT;
540     case CPP_FLOATCONSTANT:         parserToken->sType.lex.d = ppToken.dval;       return FLOATCONSTANT;
541     case CPP_DOUBLECONSTANT:        parserToken->sType.lex.d = ppToken.dval;       return DOUBLECONSTANT;
542     case CPP_IDENTIFIER:            return tokenizeIdentifier();
543
544     case EOF:                       return 0;
545                                    
546     default:
547         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown PP token", loc);
548         return 0;
549     }
550 }
551
552 int TScanContext::tokenizeIdentifier()
553 {
554     if (ReservedSet->find(tokenText) != ReservedSet->end())
555         return reservedWord();
556
557     keyword = (*KeywordMap)[tokenText];
558     if (keyword == 0) {
559         // Should have an identifier of some sort
560         return identifierOrType();
561     }
562     field = false;
563
564     switch (keyword) {
565     case CONST:
566     case UNIFORM:
567     case IN:
568     case OUT:
569     case INOUT:
570     case STRUCT:
571     case BREAK:
572     case CONTINUE:
573     case DO:
574     case FOR:
575     case WHILE:
576     case IF:
577     case ELSE:
578     case DISCARD:
579     case RETURN:
580     case CASE:
581         return keyword;
582
583     case SWITCH:
584     case DEFAULT:
585         if (parseContext.profile == EEsProfile && parseContext.version < 300 ||
586             parseContext.profile != EEsProfile && parseContext.version < 130)
587             reservedWord();
588         return keyword;
589
590     case VOID:
591     case BOOL:
592     case FLOAT:
593     case INT:
594     case BVEC2:
595     case BVEC3:
596     case BVEC4:
597     case VEC2:
598     case VEC3:
599     case VEC4:
600     case IVEC2:
601     case IVEC3:
602     case IVEC4:
603     case MAT2:
604     case MAT3:
605     case MAT4:
606     case SAMPLER2D:
607     case SAMPLERCUBE:
608         afterType = true;
609         return keyword;
610
611     case BOOLCONSTANT:
612         if (strcmp("true", tokenText) == 0)
613             parserToken->sType.lex.b = true;
614         else
615             parserToken->sType.lex.b = false;
616         return keyword;
617
618     case ATTRIBUTE:
619     case VARYING:
620         if (parseContext.profile == EEsProfile && parseContext.version >= 300)
621             reservedWord();
622         return keyword;
623
624     case BUFFER:
625         if (parseContext.version < 430)
626             return identifierOrType();
627         return keyword;
628
629     case COHERENT:
630     case RESTRICT:
631     case READONLY:
632     case WRITEONLY:
633     case ATOMIC_UINT:
634         return es30ReservedFromGLSL(420);
635
636     case VOLATILE:
637         if (parseContext.profile == EEsProfile || parseContext.version < 420)
638             reservedWord();
639         return keyword;
640
641     case LAYOUT:
642     case SHARED:
643         if (parseContext.profile == EEsProfile && parseContext.version < 300 ||
644             parseContext.profile != EEsProfile && parseContext.version < 140)
645             return identifierOrType();
646         return keyword;
647
648     case PATCH:
649     case SAMPLE:
650     case SUBROUTINE:
651         return es30ReservedFromGLSL(400);
652
653     case HIGH_PRECISION:
654     case MEDIUM_PRECISION:
655     case LOW_PRECISION:
656     case PRECISION:
657         return precisionKeyword();
658
659     case MAT2X2:
660     case MAT2X3:
661     case MAT2X4:
662     case MAT3X2:
663     case MAT3X3:
664     case MAT3X4:
665     case MAT4X2:
666     case MAT4X3:
667     case MAT4X4:        
668         return matNxM();
669
670     case DMAT2:
671     case DMAT3:
672     case DMAT4:
673     case DMAT2X2:
674     case DMAT2X3:
675     case DMAT2X4:
676     case DMAT3X2:
677     case DMAT3X3:
678     case DMAT3X4:
679     case DMAT4X2:
680     case DMAT4X3:
681     case DMAT4X4:
682         return dMat();
683
684     case IMAGE1D:
685     case IIMAGE1D:
686     case UIMAGE1D:
687     case IMAGE2D:
688     case IIMAGE2D:
689     case UIMAGE2D:
690     case IMAGE3D:
691     case IIMAGE3D:
692     case UIMAGE3D:
693     case IMAGE2DRECT:
694     case IIMAGE2DRECT:
695     case UIMAGE2DRECT:
696     case IMAGECUBE:
697     case IIMAGECUBE:
698     case UIMAGECUBE:
699     case IMAGEBUFFER:
700     case IIMAGEBUFFER:
701     case UIMAGEBUFFER:
702     case IMAGE1DARRAY:
703     case IIMAGE1DARRAY:
704     case UIMAGE1DARRAY:
705     case IMAGE2DARRAY:
706     case IIMAGE2DARRAY:
707     case UIMAGE2DARRAY:
708         return firstGenerationImage();
709
710     case IMAGECUBEARRAY:
711     case IIMAGECUBEARRAY:
712     case UIMAGECUBEARRAY:
713     case IMAGE2DMS:
714     case IIMAGE2DMS:
715     case UIMAGE2DMS:
716     case IMAGE2DMSARRAY:
717     case IIMAGE2DMSARRAY:
718     case UIMAGE2DMSARRAY:
719         return secondGenerationImage();
720
721     case DOUBLE:
722     case DVEC2:
723     case DVEC3:
724     case DVEC4:
725     case SAMPLERCUBEARRAY:
726     case SAMPLERCUBEARRAYSHADOW:
727     case ISAMPLERCUBEARRAY:
728     case USAMPLERCUBEARRAY:
729         afterType = true;
730         if (parseContext.profile == EEsProfile || parseContext.version < 400)
731             reservedWord();
732         return keyword;
733
734     case ISAMPLER1D:
735     case ISAMPLER1DARRAY:
736     case SAMPLER1DARRAYSHADOW:
737     case USAMPLER1D:
738     case USAMPLER1DARRAY:
739     case SAMPLERBUFFER:
740         afterType = true;
741         return es30ReservedFromGLSL(130);
742
743     case UINT:
744     case UVEC2:
745     case UVEC3:
746     case UVEC4:
747     case SAMPLERCUBESHADOW:
748     case SAMPLER2DARRAY:
749     case SAMPLER2DARRAYSHADOW:
750     case ISAMPLER2D:
751     case ISAMPLER3D:
752     case ISAMPLERCUBE:
753     case ISAMPLER2DARRAY:
754     case USAMPLER2D:
755     case USAMPLER3D:
756     case USAMPLERCUBE:
757     case USAMPLER2DARRAY:
758         afterType = true;
759         return nonreservedKeyword(300, 130);
760         
761     case ISAMPLER2DRECT:
762     case USAMPLER2DRECT:
763     case ISAMPLERBUFFER:
764     case USAMPLERBUFFER:
765         afterType = true;
766         return es30ReservedFromGLSL(140);
767         
768     case SAMPLER2DMS:
769     case ISAMPLER2DMS:
770     case USAMPLER2DMS:
771     case SAMPLER2DMSARRAY:
772     case ISAMPLER2DMSARRAY:
773     case USAMPLER2DMSARRAY:
774         afterType = true;
775         return es30ReservedFromGLSL(150);
776
777     case SAMPLER1D:
778     case SAMPLER1DSHADOW:
779         afterType = true;
780         if (parseContext.profile == EEsProfile)
781             reservedWord();
782         return keyword;
783
784     case SAMPLER3D:
785     case SAMPLER2DSHADOW:
786         afterType = true;
787         if (parseContext.profile == EEsProfile && parseContext.version < 300)
788             reservedWord();
789         return keyword;
790
791     case SAMPLER2DRECT:
792     case SAMPLER2DRECTSHADOW:
793         afterType = true;
794         if (parseContext.profile == EEsProfile ||
795             parseContext.profile != EEsProfile && parseContext.version < 140)
796             reservedWord();
797         return keyword;
798
799     case SAMPLER1DARRAY:
800         afterType = true;
801         if (parseContext.profile == EEsProfile && parseContext.version == 300)
802             reservedWord();
803         else if (parseContext.profile == EEsProfile && parseContext.version < 300 ||
804                  parseContext.profile != EEsProfile && parseContext.version < 130)
805             return identifierOrType();
806         return keyword;
807
808     case NOPERSPECTIVE:
809         return es30ReservedFromGLSL(130);
810         
811     case SMOOTH:
812         if (parseContext.profile == EEsProfile && parseContext.version < 300 ||
813             parseContext.profile != EEsProfile && parseContext.version < 130)
814             return identifierOrType();
815         return keyword;
816
817     case FLAT:
818         if (parseContext.profile == EEsProfile && parseContext.version < 300)
819             reservedWord();
820         else if (parseContext.profile != EEsProfile && parseContext.version < 130)
821             return identifierOrType();
822         return keyword;
823
824     case CENTROID:
825         if (parseContext.version < 120)
826             return identifierOrType();
827         return keyword;
828
829     case PRECISE:
830         if (parseContext.profile == EEsProfile ||
831             parseContext.profile != EEsProfile && parseContext.version < 400)
832             return identifierOrType();
833         return keyword;
834
835     case INVARIANT:
836         if (parseContext.profile != EEsProfile && parseContext.version < 120)
837             return identifierOrType();
838         return keyword;
839
840     case PACKED:
841         if (parseContext.profile == EEsProfile && parseContext.version < 300 ||
842             parseContext.profile != EEsProfile && parseContext.version < 330)
843             return reservedWord();
844         return identifierOrType();
845
846     case RESOURCE:
847     {
848         bool reserved = parseContext.profile == EEsProfile && parseContext.version >= 300 ||
849                         parseContext.profile != EEsProfile && parseContext.version >= 420;
850         return identifierOrReserved(reserved);
851     }
852     case SUPERP:
853     {
854         bool reserved = parseContext.profile == EEsProfile || parseContext.version >= 130;
855         return identifierOrReserved(reserved);
856     }
857     
858     default:        
859         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown glslang keyword", loc);
860         return 0;
861     }
862 }
863
864 int TScanContext::identifierOrType()
865 {
866     parserToken->sType.lex.string = NewPoolTString(tokenText);
867     if (field) {
868         field = false;
869  
870         return FIELD_SELECTION;
871     }
872
873     parserToken->sType.lex.symbol = parseContext.symbolTable.find(*parserToken->sType.lex.string);
874     if (afterType == false && parserToken->sType.lex.symbol) {
875         if (TVariable* variable = parserToken->sType.lex.symbol->getAsVariable()) {
876             if (variable->isUserType()) {
877                 afterType = true;
878
879                 return TYPE_NAME;
880             }
881         }
882     }
883
884     return IDENTIFIER;
885 }
886
887 int TScanContext::reservedWord()
888 {
889     parseContext.error(loc, "Reserved word.", tokenText, "", "");
890
891     return 0;
892 }
893
894 int TScanContext::identifierOrReserved(bool reserved)
895 {
896     if (reserved) {
897         reservedWord();
898
899         return 0;
900     }
901
902     if (parseContext.forwardCompatible)
903         parseContext.warn(loc, "using future reserved keyword", tokenText, "");
904
905     return identifierOrType();
906 }
907
908 // For keywords that suddenly showed up on non-ES (not previously reserved)
909 // but then got reserved by ES 3.0.
910 int TScanContext::es30ReservedFromGLSL(int version)
911 {
912     if (parseContext.profile == EEsProfile && parseContext.version < 300 ||
913         parseContext.profile != EEsProfile && parseContext.version < version) {
914             if (parseContext.forwardCompatible)
915                 parseContext.warn(loc, "future reserved word in ES 300 and keyword in GLSL", tokenText, "");
916
917             return identifierOrType();
918     } else if (parseContext.profile == EEsProfile && parseContext.version >= 300)
919         reservedWord();
920
921     return keyword;
922 }
923
924 // For a keyword that was never reserved, until it suddenly
925 // showed up, both in an es version and a non-ES version.
926 int TScanContext::nonreservedKeyword(int esVersion, int nonEsVersion)
927 {
928     if (parseContext.profile == EEsProfile && parseContext.version < esVersion ||
929         parseContext.profile != EEsProfile && parseContext.version < nonEsVersion) {
930         if (parseContext.forwardCompatible)
931             parseContext.warn(loc, "using future keyword", tokenText, "");
932
933         return identifierOrType();
934     }
935
936     return keyword;
937 }
938
939 int TScanContext::precisionKeyword()
940 {
941     if (parseContext.profile == EEsProfile || parseContext.version >= 130)
942         return keyword;
943
944     if (parseContext.forwardCompatible)
945         parseContext.warn(loc, "using ES precision qualifier keyword", tokenText, "");
946
947     return identifierOrType();
948 }
949
950 int TScanContext::matNxM()
951 {
952     afterType = true;
953
954     if (parseContext.version > 110)
955         return keyword;
956
957     if (parseContext.forwardCompatible)
958         parseContext.warn(loc, "using future non-square matrix type keyword", tokenText, "");
959
960     return identifierOrType();
961 }
962
963 int TScanContext::dMat()
964 {
965     afterType = true;
966
967     if (parseContext.profile == EEsProfile && parseContext.version >= 300) {
968         reservedWord();
969
970         return keyword;
971     }
972
973     if (parseContext.profile != EEsProfile && parseContext.version >= 400)
974         return keyword;
975
976     if (parseContext.forwardCompatible)
977         parseContext.warn(loc, "using future type keyword", tokenText, "");
978
979     return identifierOrType();
980 }
981
982 int TScanContext::firstGenerationImage()
983 {
984     afterType = true;
985
986     if (parseContext.profile != EEsProfile && parseContext.version >= 420)
987         return keyword;
988
989     if (parseContext.profile == EEsProfile && parseContext.version >= 300 ||
990         parseContext.profile != EEsProfile && parseContext.version >= 130) {
991         reservedWord();
992
993         return keyword;
994     }
995
996     if (parseContext.forwardCompatible)
997         parseContext.warn(loc, "using future type keyword", tokenText, "");
998
999     return identifierOrType();
1000 }
1001
1002 int TScanContext::secondGenerationImage()
1003 {
1004     afterType = true;
1005
1006     if (parseContext.profile != EEsProfile && parseContext.version >= 420)
1007         return keyword;
1008
1009     if (parseContext.forwardCompatible)
1010         parseContext.warn(loc, "using future type keyword", tokenText, "");
1011
1012     return identifierOrType();
1013 }
1014
1015 };