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