Non-functional: Use better token names for the preprocessor.
[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 // Fill this in when doing glslang-level scanning, to hand back to the parser.
234 class TParserToken {
235 public:
236     explicit TParserToken(YYSTYPE& b) : sType(b) { }
237
238     YYSTYPE& sType;
239 };
240
241 } // end namespace glslang
242
243 // This is the function the glslang parser (i.e., bison) calls to get its next token
244 int yylex(YYSTYPE* glslangTokenDesc, glslang::TParseContext& parseContext)
245 {
246     glslang::TParserToken token(*glslangTokenDesc);
247
248     return parseContext.getScanContext()->tokenize(parseContext.getPpContext(), token);
249 }
250
251 namespace {
252
253 // A single global usable by all threads, by all versions, by all languages.
254 // After a single process-level initialization, this is read only and thread safe
255 std::map<std::string, int>* KeywordMap = 0;
256 std::set<std::string>* ReservedSet = 0;
257
258 };
259
260 namespace glslang {
261
262 void TScanContext::fillInKeywordMap()
263 {
264     if (KeywordMap != 0) {
265         // this is really an error, as this should called only once per process
266         // but, the only risk is if two threads called simultaneously
267         return;
268     }
269     KeywordMap = new std::map<std::string, int>;
270
271     (*KeywordMap)["const"] =                   CONST;
272     (*KeywordMap)["uniform"] =                 UNIFORM;
273     (*KeywordMap)["in"] =                      IN;
274     (*KeywordMap)["out"] =                     OUT;
275     (*KeywordMap)["inout"] =                   INOUT;
276     (*KeywordMap)["struct"] =                  STRUCT;
277     (*KeywordMap)["break"] =                   BREAK;
278     (*KeywordMap)["continue"] =                CONTINUE;
279     (*KeywordMap)["do"] =                      DO;
280     (*KeywordMap)["for"] =                     FOR;
281     (*KeywordMap)["while"] =                   WHILE;
282     (*KeywordMap)["switch"] =                  SWITCH;
283     (*KeywordMap)["case"] =                    CASE;
284     (*KeywordMap)["default"] =                 DEFAULT;
285     (*KeywordMap)["if"] =                      IF;
286     (*KeywordMap)["else"] =                    ELSE;
287     (*KeywordMap)["discard"] =                 DISCARD;
288     (*KeywordMap)["return"] =                  RETURN;
289     (*KeywordMap)["void"] =                    VOID;
290     (*KeywordMap)["bool"] =                    BOOL;
291     (*KeywordMap)["float"] =                   FLOAT;
292     (*KeywordMap)["int"] =                     INT;
293     (*KeywordMap)["bvec2"] =                   BVEC2;
294     (*KeywordMap)["bvec3"] =                   BVEC3;
295     (*KeywordMap)["bvec4"] =                   BVEC4;
296     (*KeywordMap)["vec2"] =                    VEC2;
297     (*KeywordMap)["vec3"] =                    VEC3;
298     (*KeywordMap)["vec4"] =                    VEC4;
299     (*KeywordMap)["ivec2"] =                   IVEC2;
300     (*KeywordMap)["ivec3"] =                   IVEC3;
301     (*KeywordMap)["ivec4"] =                   IVEC4;
302     (*KeywordMap)["mat2"] =                    MAT2;
303     (*KeywordMap)["mat3"] =                    MAT3;
304     (*KeywordMap)["mat4"] =                    MAT4;
305     (*KeywordMap)["sampler2D"] =               SAMPLER2D;
306     (*KeywordMap)["samplerCube"] =             SAMPLERCUBE;
307     (*KeywordMap)["true"] =                    BOOLCONSTANT;
308     (*KeywordMap)["false"] =                   BOOLCONSTANT;
309     (*KeywordMap)["attribute"] =               ATTRIBUTE;
310     (*KeywordMap)["varying"] =                 VARYING;
311     (*KeywordMap)["buffer"] =                  BUFFER;
312     (*KeywordMap)["coherent"] =                COHERENT;
313     (*KeywordMap)["restrict"] =                RESTRICT;
314     (*KeywordMap)["readonly"] =                READONLY;
315     (*KeywordMap)["writeonly"] =               WRITEONLY;
316     (*KeywordMap)["atomic_uint"] =             ATOMIC_UINT;
317     (*KeywordMap)["volatile"] =                VOLATILE;
318     (*KeywordMap)["layout"] =                  LAYOUT;
319     (*KeywordMap)["shared"] =                  SHARED;
320     (*KeywordMap)["patch"] =                   PATCH;
321     (*KeywordMap)["sample"] =                  SAMPLE;
322     (*KeywordMap)["subroutine"] =              SUBROUTINE;
323     (*KeywordMap)["highp"] =                   HIGH_PRECISION;
324     (*KeywordMap)["mediump"] =                 MEDIUM_PRECISION;
325     (*KeywordMap)["lowp"] =                    LOW_PRECISION;
326     (*KeywordMap)["precision"] =               PRECISION;
327     (*KeywordMap)["mat2x2"] =                  MAT2X2;
328     (*KeywordMap)["mat2x3"] =                  MAT2X3;
329     (*KeywordMap)["mat2x4"] =                  MAT2X4;
330     (*KeywordMap)["mat3x2"] =                  MAT3X2;
331     (*KeywordMap)["mat3x3"] =                  MAT3X3;
332     (*KeywordMap)["mat3x4"] =                  MAT3X4;
333     (*KeywordMap)["mat4x2"] =                  MAT4X2;
334     (*KeywordMap)["mat4x3"] =                  MAT4X3;
335     (*KeywordMap)["mat4x4"] =                  MAT4X4;
336     (*KeywordMap)["dmat2"] =                   DMAT2;
337     (*KeywordMap)["dmat3"] =                   DMAT3;
338     (*KeywordMap)["dmat4"] =                   DMAT4;
339     (*KeywordMap)["dmat2x2"] =                 DMAT2X2;
340     (*KeywordMap)["dmat2x3"] =                 DMAT2X3;
341     (*KeywordMap)["dmat2x4"] =                 DMAT2X4;
342     (*KeywordMap)["dmat3x2"] =                 DMAT3X2;
343     (*KeywordMap)["dmat3x3"] =                 DMAT3X3;
344     (*KeywordMap)["dmat3x4"] =                 DMAT3X4;
345     (*KeywordMap)["dmat4x2"] =                 DMAT4X2;
346     (*KeywordMap)["dmat4x3"] =                 DMAT4X3;
347     (*KeywordMap)["dmat4x4"] =                 DMAT4X4;
348     (*KeywordMap)["image1D"] =                 IMAGE1D;
349     (*KeywordMap)["iimage1D"] =                IIMAGE1D;
350     (*KeywordMap)["uimage1D"] =                UIMAGE1D;
351     (*KeywordMap)["image2D"] =                 IMAGE2D;
352     (*KeywordMap)["iimage2D"] =                IIMAGE2D;
353     (*KeywordMap)["uimage2D"] =                UIMAGE2D;
354     (*KeywordMap)["image3D"] =                 IMAGE3D;
355     (*KeywordMap)["iimage3D"] =                IIMAGE3D;
356     (*KeywordMap)["uimage3D"] =                UIMAGE3D;
357     (*KeywordMap)["image2DRect"] =             IMAGE2DRECT;
358     (*KeywordMap)["iimage2DRect"] =            IIMAGE2DRECT;
359     (*KeywordMap)["uimage2DRect"] =            UIMAGE2DRECT;
360     (*KeywordMap)["imageCube"] =               IMAGECUBE;
361     (*KeywordMap)["iimageCube"] =              IIMAGECUBE;
362     (*KeywordMap)["uimageCube"] =              UIMAGECUBE;
363     (*KeywordMap)["imageBuffer"] =             IMAGEBUFFER;
364     (*KeywordMap)["iimageBuffer"] =            IIMAGEBUFFER;
365     (*KeywordMap)["uimageBuffer"] =            UIMAGEBUFFER;
366     (*KeywordMap)["image1DArray"] =            IMAGE1DARRAY;
367     (*KeywordMap)["iimage1DArray"] =           IIMAGE1DARRAY;
368     (*KeywordMap)["uimage1DArray"] =           UIMAGE1DARRAY;
369     (*KeywordMap)["image2DArray"] =            IMAGE2DARRAY;
370     (*KeywordMap)["iimage2DArray"] =           IIMAGE2DARRAY;
371     (*KeywordMap)["uimage2DArray"] =           UIMAGE2DARRAY;
372     (*KeywordMap)["imageCubeArray"] =          IMAGECUBEARRAY;
373     (*KeywordMap)["iimageCubeArray"] =         IIMAGECUBEARRAY;
374     (*KeywordMap)["uimageCubeArray"] =         UIMAGECUBEARRAY;
375     (*KeywordMap)["image2DMS"] =               IMAGE2DMS;
376     (*KeywordMap)["iimage2DMS"] =              IIMAGE2DMS;
377     (*KeywordMap)["uimage2DMS"] =              UIMAGE2DMS;
378     (*KeywordMap)["image2DMSArray"] =          IMAGE2DMSARRAY;
379     (*KeywordMap)["iimage2DMSArray"] =         IIMAGE2DMSARRAY;
380     (*KeywordMap)["uimage2DMSArray"] =         UIMAGE2DMSARRAY;
381     (*KeywordMap)["double"] =                  DOUBLE;
382     (*KeywordMap)["dvec2"] =                   DVEC2;
383     (*KeywordMap)["dvec3"] =                   DVEC3;
384     (*KeywordMap)["dvec4"] =                   DVEC4;
385     (*KeywordMap)["samplerCubeArray"] =        SAMPLERCUBEARRAY;
386     (*KeywordMap)["samplerCubeArrayShadow"] =  SAMPLERCUBEARRAYSHADOW;
387     (*KeywordMap)["isamplerCubeArray"] =       ISAMPLERCUBEARRAY;
388     (*KeywordMap)["usamplerCubeArray"] =       USAMPLERCUBEARRAY;
389     (*KeywordMap)["sampler1DArrayShadow"] =    SAMPLER1DARRAYSHADOW;
390     (*KeywordMap)["isampler1DArray"] =         ISAMPLER1DARRAY;
391     (*KeywordMap)["usampler1D"] =              USAMPLER1D;
392     (*KeywordMap)["isampler1D"] =              ISAMPLER1D;
393     (*KeywordMap)["usampler1DArray"] =         USAMPLER1DARRAY;
394     (*KeywordMap)["samplerBuffer"] =           SAMPLERBUFFER;
395     (*KeywordMap)["uint"] =                    UINT;
396     (*KeywordMap)["uvec2"] =                   UVEC2;
397     (*KeywordMap)["uvec3"] =                   UVEC3;
398     (*KeywordMap)["uvec4"] =                   UVEC4;
399     (*KeywordMap)["samplerCubeShadow"] =       SAMPLERCUBESHADOW;
400     (*KeywordMap)["sampler2DArray"] =          SAMPLER2DARRAY;
401     (*KeywordMap)["sampler2DArrayShadow"] =    SAMPLER2DARRAYSHADOW;
402     (*KeywordMap)["isampler2D"] =              ISAMPLER2D;
403     (*KeywordMap)["isampler3D"] =              ISAMPLER3D;
404     (*KeywordMap)["isamplerCube"] =            ISAMPLERCUBE;
405     (*KeywordMap)["isampler2DArray"] =         ISAMPLER2DARRAY;
406     (*KeywordMap)["usampler2D"] =              USAMPLER2D;
407     (*KeywordMap)["usampler3D"] =              USAMPLER3D;
408     (*KeywordMap)["usamplerCube"] =            USAMPLERCUBE;
409     (*KeywordMap)["usampler2DArray"] =         USAMPLER2DARRAY;
410     (*KeywordMap)["isampler2DRect"] =          ISAMPLER2DRECT;
411     (*KeywordMap)["usampler2DRect"] =          USAMPLER2DRECT;
412     (*KeywordMap)["isamplerBuffer"] =          ISAMPLERBUFFER;
413     (*KeywordMap)["usamplerBuffer"] =          USAMPLERBUFFER;
414     (*KeywordMap)["sampler2DMS"] =             SAMPLER2DMS;
415     (*KeywordMap)["isampler2DMS"] =            ISAMPLER2DMS;
416     (*KeywordMap)["usampler2DMS"] =            USAMPLER2DMS;
417     (*KeywordMap)["sampler2DMSArray"] =        SAMPLER2DMSARRAY;
418     (*KeywordMap)["isampler2DMSArray"] =       ISAMPLER2DMSARRAY;
419     (*KeywordMap)["usampler2DMSArray"] =       USAMPLER2DMSARRAY;
420     (*KeywordMap)["sampler1D"] =               SAMPLER1D;
421     (*KeywordMap)["sampler1DShadow"] =         SAMPLER1DSHADOW;
422     (*KeywordMap)["sampler3D"] =               SAMPLER3D;
423     (*KeywordMap)["sampler2DShadow"] =         SAMPLER2DSHADOW;
424     (*KeywordMap)["sampler2DRect"] =           SAMPLER2DRECT;
425     (*KeywordMap)["sampler2DRectShadow"] =     SAMPLER2DRECTSHADOW;
426     (*KeywordMap)["sampler1DArray"] =          SAMPLER1DARRAY;
427     (*KeywordMap)["noperspective"] =           NOPERSPECTIVE;
428     (*KeywordMap)["smooth"] =                  SMOOTH;
429     (*KeywordMap)["flat"] =                    FLAT;
430     (*KeywordMap)["centroid"] =                CENTROID;
431     (*KeywordMap)["precise"] =                 PRECISE;
432     (*KeywordMap)["invariant"] =               INVARIANT;
433     (*KeywordMap)["packed"] =                  PACKED;
434     (*KeywordMap)["resource"] =                RESOURCE;
435     (*KeywordMap)["superp"] =                  SUPERP;
436
437     ReservedSet = new std::set<std::string>;
438     
439     ReservedSet->insert("common");
440     ReservedSet->insert("partition");
441     ReservedSet->insert("active");
442     ReservedSet->insert("asm");
443     ReservedSet->insert("class");
444     ReservedSet->insert("union");
445     ReservedSet->insert("enum");
446     ReservedSet->insert("typedef");
447     ReservedSet->insert("template");
448     ReservedSet->insert("this");
449     ReservedSet->insert("goto");
450     ReservedSet->insert("inline");
451     ReservedSet->insert("noinline");
452     ReservedSet->insert("public");
453     ReservedSet->insert("static");
454     ReservedSet->insert("extern");
455     ReservedSet->insert("external");
456     ReservedSet->insert("interface");
457     ReservedSet->insert("long");
458     ReservedSet->insert("short");
459     ReservedSet->insert("half");
460     ReservedSet->insert("fixed");
461     ReservedSet->insert("unsigned");
462     ReservedSet->insert("input");
463     ReservedSet->insert("output");
464     ReservedSet->insert("hvec2");
465     ReservedSet->insert("hvec3");
466     ReservedSet->insert("hvec4");
467     ReservedSet->insert("fvec2");
468     ReservedSet->insert("fvec3");
469     ReservedSet->insert("fvec4");
470     ReservedSet->insert("sampler3DRect");
471     ReservedSet->insert("filter");
472     ReservedSet->insert("sizeof");
473     ReservedSet->insert("cast");
474     ReservedSet->insert("namespace");
475     ReservedSet->insert("using");
476 }
477
478 int TScanContext::tokenize(TPpContext* pp, TParserToken& token)
479 {
480     parserToken = &token;
481     TPpToken ppToken;
482     tokenText = pp->tokenize(&ppToken);
483
484     loc = ppToken.loc;
485     parserToken->sType.lex.loc = loc;
486     switch (ppToken.token) {
487     case ';':  afterType = false;   return SEMICOLON;
488     case ',':  afterType = false;   return COMMA;
489     case ':':                       return COLON;
490     case '=':  afterType = false;   return EQUAL;
491     case '(':  afterType = false;   return LEFT_PAREN;
492     case ')':  afterType = false;   return RIGHT_PAREN;
493     case '.':  field = true;        return DOT;
494     case '!':                       return BANG;
495     case '-':                       return DASH;
496     case '~':                       return TILDE;
497     case '+':                       return PLUS;
498     case '*':                       return STAR;
499     case '/':                       return SLASH;
500     case '%':                       return PERCENT;
501     case '<':                       return LEFT_ANGLE;
502     case '>':                       return RIGHT_ANGLE;
503     case '|':                       return VERTICAL_BAR;
504     case '^':                       return CARET;
505     case '&':                       return AMPERSAND;
506     case '?':                       return QUESTION;
507     case '[':                       return LEFT_BRACKET;
508     case ']':                       return RIGHT_BRACKET;
509     case '{':                       return LEFT_BRACE;
510     case '}':                       return RIGHT_BRACE;
511
512     case CPP_AND_OP:                return AND_OP;
513     case CPP_SUB_ASSIGN:            return SUB_ASSIGN;
514     case CPP_MOD_ASSIGN:            return MOD_ASSIGN;
515     case CPP_ADD_ASSIGN:            return ADD_ASSIGN;
516     case CPP_DIV_ASSIGN:            return DIV_ASSIGN;
517     case CPP_MUL_ASSIGN:            return MUL_ASSIGN;
518     case CPP_EQ_OP:                 return EQ_OP;
519     case CPP_XOR_OP:                return XOR_OP;
520     case CPP_GE_OP:                 return GE_OP;
521     case CPP_RIGHT_OP:              return RIGHT_OP;
522     case CPP_LE_OP:                 return LE_OP;
523     case CPP_LEFT_OP:               return LEFT_OP;
524     case CPP_DEC_OP:                return DEC_OP;
525     case CPP_NE_OP:                 return NE_OP;
526     case CPP_OR_OP:                 return OR_OP;
527     case CPP_INC_OP:                return INC_OP;
528     case CPP_RIGHT_ASSIGN:          return RIGHT_ASSIGN;
529     case CPP_LEFT_ASSIGN:           return LEFT_ASSIGN;
530     case CPP_AND_ASSIGN:            return AND_ASSIGN;
531     case CPP_OR_ASSIGN:             return OR_ASSIGN;
532     case CPP_XOR_ASSIGN:            return XOR_ASSIGN;
533                                    
534     case CPP_INTCONSTANT:           parserToken->sType.lex.i = ppToken.ival;        return INTCONSTANT;
535     case CPP_UINTCONSTANT:          parserToken->sType.lex.i = ppToken.ival;        return UINTCONSTANT;
536     case CPP_FLOATCONSTANT:         parserToken->sType.lex.d = ppToken.dval;       return FLOATCONSTANT;
537     case CPP_DOUBLECONSTANT:        parserToken->sType.lex.d = ppToken.dval;       return DOUBLECONSTANT;
538     case CPP_IDENTIFIER:            return tokenizeIdentifier();
539
540     case EOF:                       return 0;
541                                    
542     default:
543         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown PP token", loc);
544         return 0;
545     }
546 }
547
548 int TScanContext::tokenizeIdentifier()
549 {
550     if (ReservedSet->find(tokenText) != ReservedSet->end())
551         return reservedWord();
552
553     std::map<std::string, int>::const_iterator it = KeywordMap->find(tokenText);
554     if (it == KeywordMap->end()) {
555         // Should have an identifier of some sort
556         return identifierOrType();
557     }
558     keyword = it->second;
559     field = false;
560
561     switch (keyword) {
562     case CONST:
563     case UNIFORM:
564     case IN:
565     case OUT:
566     case INOUT:
567     case STRUCT:
568     case BREAK:
569     case CONTINUE:
570     case DO:
571     case FOR:
572     case WHILE:
573     case IF:
574     case ELSE:
575     case DISCARD:
576     case RETURN:
577     case CASE:
578         return keyword;
579
580     case SWITCH:
581     case DEFAULT:
582         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
583             (parseContext.profile != EEsProfile && parseContext.version < 130))
584             reservedWord();
585         return keyword;
586
587     case VOID:
588     case BOOL:
589     case FLOAT:
590     case INT:
591     case BVEC2:
592     case BVEC3:
593     case BVEC4:
594     case VEC2:
595     case VEC3:
596     case VEC4:
597     case IVEC2:
598     case IVEC3:
599     case IVEC4:
600     case MAT2:
601     case MAT3:
602     case MAT4:
603     case SAMPLER2D:
604     case SAMPLERCUBE:
605         afterType = true;
606         return keyword;
607
608     case BOOLCONSTANT:
609         if (strcmp("true", tokenText) == 0)
610             parserToken->sType.lex.b = true;
611         else
612             parserToken->sType.lex.b = false;
613         return keyword;
614
615     case ATTRIBUTE:
616     case VARYING:
617         if (parseContext.profile == EEsProfile && parseContext.version >= 300)
618             reservedWord();
619         return keyword;
620
621     case BUFFER:
622         if (parseContext.version < 430)
623             return identifierOrType();
624         return keyword;
625
626     case COHERENT:
627     case RESTRICT:
628     case READONLY:
629     case WRITEONLY:
630     case ATOMIC_UINT:
631         return es30ReservedFromGLSL(420);
632
633     case VOLATILE:
634         if (parseContext.profile == EEsProfile || parseContext.version < 420)
635             reservedWord();
636         return keyword;
637
638     case LAYOUT:
639     case SHARED:
640         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
641             (parseContext.profile != EEsProfile && parseContext.version < 140))
642             return identifierOrType();
643         return keyword;
644
645     case PATCH:
646     case SAMPLE:
647     case SUBROUTINE:
648         return es30ReservedFromGLSL(400);
649
650     case HIGH_PRECISION:
651     case MEDIUM_PRECISION:
652     case LOW_PRECISION:
653     case PRECISION:
654         return precisionKeyword();
655
656     case MAT2X2:
657     case MAT2X3:
658     case MAT2X4:
659     case MAT3X2:
660     case MAT3X3:
661     case MAT3X4:
662     case MAT4X2:
663     case MAT4X3:
664     case MAT4X4:        
665         return matNxM();
666
667     case DMAT2:
668     case DMAT3:
669     case DMAT4:
670     case DMAT2X2:
671     case DMAT2X3:
672     case DMAT2X4:
673     case DMAT3X2:
674     case DMAT3X3:
675     case DMAT3X4:
676     case DMAT4X2:
677     case DMAT4X3:
678     case DMAT4X4:
679         return dMat();
680
681     case IMAGE1D:
682     case IIMAGE1D:
683     case UIMAGE1D:
684     case IMAGE2D:
685     case IIMAGE2D:
686     case UIMAGE2D:
687     case IMAGE3D:
688     case IIMAGE3D:
689     case UIMAGE3D:
690     case IMAGE2DRECT:
691     case IIMAGE2DRECT:
692     case UIMAGE2DRECT:
693     case IMAGECUBE:
694     case IIMAGECUBE:
695     case UIMAGECUBE:
696     case IMAGEBUFFER:
697     case IIMAGEBUFFER:
698     case UIMAGEBUFFER:
699     case IMAGE1DARRAY:
700     case IIMAGE1DARRAY:
701     case UIMAGE1DARRAY:
702     case IMAGE2DARRAY:
703     case IIMAGE2DARRAY:
704     case UIMAGE2DARRAY:
705         return firstGenerationImage();
706
707     case IMAGECUBEARRAY:
708     case IIMAGECUBEARRAY:
709     case UIMAGECUBEARRAY:
710     case IMAGE2DMS:
711     case IIMAGE2DMS:
712     case UIMAGE2DMS:
713     case IMAGE2DMSARRAY:
714     case IIMAGE2DMSARRAY:
715     case UIMAGE2DMSARRAY:
716         return secondGenerationImage();
717
718     case DOUBLE:
719     case DVEC2:
720     case DVEC3:
721     case DVEC4:
722     case SAMPLERCUBEARRAY:
723     case SAMPLERCUBEARRAYSHADOW:
724     case ISAMPLERCUBEARRAY:
725     case USAMPLERCUBEARRAY:
726         afterType = true;
727         if (parseContext.profile == EEsProfile || parseContext.version < 400)
728             reservedWord();
729         return keyword;
730
731     case ISAMPLER1D:
732     case ISAMPLER1DARRAY:
733     case SAMPLER1DARRAYSHADOW:
734     case USAMPLER1D:
735     case USAMPLER1DARRAY:
736     case SAMPLERBUFFER:
737         afterType = true;
738         return es30ReservedFromGLSL(130);
739
740     case UINT:
741     case UVEC2:
742     case UVEC3:
743     case UVEC4:
744     case SAMPLERCUBESHADOW:
745     case SAMPLER2DARRAY:
746     case SAMPLER2DARRAYSHADOW:
747     case ISAMPLER2D:
748     case ISAMPLER3D:
749     case ISAMPLERCUBE:
750     case ISAMPLER2DARRAY:
751     case USAMPLER2D:
752     case USAMPLER3D:
753     case USAMPLERCUBE:
754     case USAMPLER2DARRAY:
755         afterType = true;
756         return nonreservedKeyword(300, 130);
757         
758     case ISAMPLER2DRECT:
759     case USAMPLER2DRECT:
760     case ISAMPLERBUFFER:
761     case USAMPLERBUFFER:
762         afterType = true;
763         return es30ReservedFromGLSL(140);
764         
765     case SAMPLER2DMS:
766     case ISAMPLER2DMS:
767     case USAMPLER2DMS:
768     case SAMPLER2DMSARRAY:
769     case ISAMPLER2DMSARRAY:
770     case USAMPLER2DMSARRAY:
771         afterType = true;
772         return es30ReservedFromGLSL(150);
773
774     case SAMPLER1D:
775     case SAMPLER1DSHADOW:
776         afterType = true;
777         if (parseContext.profile == EEsProfile)
778             reservedWord();
779         return keyword;
780
781     case SAMPLER3D:
782     case SAMPLER2DSHADOW:
783         afterType = true;
784         if (parseContext.profile == EEsProfile && parseContext.version < 300)
785             reservedWord();
786         return keyword;
787
788     case SAMPLER2DRECT:
789     case SAMPLER2DRECTSHADOW:
790         afterType = true;
791         if (parseContext.profile == EEsProfile ||
792             (parseContext.profile != EEsProfile && parseContext.version < 140))
793             reservedWord();
794         return keyword;
795
796     case SAMPLER1DARRAY:
797         afterType = true;
798         if (parseContext.profile == EEsProfile && parseContext.version == 300)
799             reservedWord();
800         else if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
801                  (parseContext.profile != EEsProfile && parseContext.version < 130))
802             return identifierOrType();
803         return keyword;
804
805     case NOPERSPECTIVE:
806         return es30ReservedFromGLSL(130);
807         
808     case SMOOTH:
809         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
810             (parseContext.profile != EEsProfile && parseContext.version < 130))
811             return identifierOrType();
812         return keyword;
813
814     case FLAT:
815         if (parseContext.profile == EEsProfile && parseContext.version < 300)
816             reservedWord();
817         else if (parseContext.profile != EEsProfile && parseContext.version < 130)
818             return identifierOrType();
819         return keyword;
820
821     case CENTROID:
822         if (parseContext.version < 120)
823             return identifierOrType();
824         return keyword;
825
826     case PRECISE:
827         if (parseContext.profile == EEsProfile ||
828             (parseContext.profile != EEsProfile && parseContext.version < 400))
829             return identifierOrType();
830         return keyword;
831
832     case INVARIANT:
833         if (parseContext.profile != EEsProfile && parseContext.version < 120)
834             return identifierOrType();
835         return keyword;
836
837     case PACKED:
838         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
839             (parseContext.profile != EEsProfile && parseContext.version < 330))
840             return reservedWord();
841         return identifierOrType();
842
843     case RESOURCE:
844     {
845         bool reserved = (parseContext.profile == EEsProfile && parseContext.version >= 300) ||
846                         (parseContext.profile != EEsProfile && parseContext.version >= 420);
847         return identifierOrReserved(reserved);
848     }
849     case SUPERP:
850     {
851         bool reserved = parseContext.profile == EEsProfile || parseContext.version >= 130;
852         return identifierOrReserved(reserved);
853     }
854     
855     default:
856         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown glslang keyword", loc);
857         return 0;
858     }
859 }
860
861 int TScanContext::identifierOrType()
862 {
863     parserToken->sType.lex.string = NewPoolTString(tokenText);
864     if (field) {
865         field = false;
866  
867         return FIELD_SELECTION;
868     }
869
870     parserToken->sType.lex.symbol = parseContext.symbolTable.find(*parserToken->sType.lex.string);
871     if (afterType == false && parserToken->sType.lex.symbol) {
872         if (const TVariable* variable = parserToken->sType.lex.symbol->getAsVariable()) {
873             if (variable->isUserType()) {
874                 afterType = true;
875
876                 return TYPE_NAME;
877             }
878         }
879     }
880
881     return IDENTIFIER;
882 }
883
884 int TScanContext::reservedWord()
885 {
886     parseContext.error(loc, "Reserved word.", tokenText, "", "");
887
888     return 0;
889 }
890
891 int TScanContext::identifierOrReserved(bool reserved)
892 {
893     if (reserved) {
894         reservedWord();
895
896         return 0;
897     }
898
899     if (parseContext.forwardCompatible)
900         parseContext.warn(loc, "using future reserved keyword", tokenText, "");
901
902     return identifierOrType();
903 }
904
905 // For keywords that suddenly showed up on non-ES (not previously reserved)
906 // but then got reserved by ES 3.0.
907 int TScanContext::es30ReservedFromGLSL(int version)
908 {
909     if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
910         (parseContext.profile != EEsProfile && parseContext.version < version)) {
911             if (parseContext.forwardCompatible)
912                 parseContext.warn(loc, "future reserved word in ES 300 and keyword in GLSL", tokenText, "");
913
914             return identifierOrType();
915     } else if (parseContext.profile == EEsProfile && parseContext.version >= 300)
916         reservedWord();
917
918     return keyword;
919 }
920
921 // For a keyword that was never reserved, until it suddenly
922 // showed up, both in an es version and a non-ES version.
923 int TScanContext::nonreservedKeyword(int esVersion, int nonEsVersion)
924 {
925     if ((parseContext.profile == EEsProfile && parseContext.version < esVersion) ||
926         (parseContext.profile != EEsProfile && parseContext.version < nonEsVersion)) {
927         if (parseContext.forwardCompatible)
928             parseContext.warn(loc, "using future keyword", tokenText, "");
929
930         return identifierOrType();
931     }
932
933     return keyword;
934 }
935
936 int TScanContext::precisionKeyword()
937 {
938     if (parseContext.profile == EEsProfile || parseContext.version >= 130)
939         return keyword;
940
941     if (parseContext.forwardCompatible)
942         parseContext.warn(loc, "using ES precision qualifier keyword", tokenText, "");
943
944     return identifierOrType();
945 }
946
947 int TScanContext::matNxM()
948 {
949     afterType = true;
950
951     if (parseContext.version > 110)
952         return keyword;
953
954     if (parseContext.forwardCompatible)
955         parseContext.warn(loc, "using future non-square matrix type keyword", tokenText, "");
956
957     return identifierOrType();
958 }
959
960 int TScanContext::dMat()
961 {
962     afterType = true;
963
964     if (parseContext.profile == EEsProfile && parseContext.version >= 300) {
965         reservedWord();
966
967         return keyword;
968     }
969
970     if (parseContext.profile != EEsProfile && parseContext.version >= 400)
971         return keyword;
972
973     if (parseContext.forwardCompatible)
974         parseContext.warn(loc, "using future type keyword", tokenText, "");
975
976     return identifierOrType();
977 }
978
979 int TScanContext::firstGenerationImage()
980 {
981     afterType = true;
982
983     if (parseContext.profile != EEsProfile && parseContext.version >= 420)
984         return keyword;
985
986     if ((parseContext.profile == EEsProfile && parseContext.version >= 300) ||
987         (parseContext.profile != EEsProfile && parseContext.version >= 130)) {
988         reservedWord();
989
990         return keyword;
991     }
992
993     if (parseContext.forwardCompatible)
994         parseContext.warn(loc, "using future type keyword", tokenText, "");
995
996     return identifierOrType();
997 }
998
999 int TScanContext::secondGenerationImage()
1000 {
1001     afterType = true;
1002
1003     if (parseContext.profile != EEsProfile && parseContext.version >= 420)
1004         return keyword;
1005
1006     if (parseContext.forwardCompatible)
1007         parseContext.warn(loc, "using future type keyword", tokenText, "");
1008
1009     return identifierOrType();
1010 }
1011
1012 } // end namespace glslang