glsl: Treat sampler2DRect and sampler2DRectShadow as reserved in ES2
[mesa.git] / src / compiler / glsl / glsl_lexer.ll
1 %{
2 /*
3 * Copyright © 2008, 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24 #include <ctype.h>
25 #include <limits.h>
26 #include "util/strtod.h"
27 #include "ast.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_parser.h"
30
31 static int classify_identifier(struct _mesa_glsl_parse_state *, const char *,
32 unsigned name_len, YYSTYPE *output);
33
34 #ifdef _MSC_VER
35 #define YY_NO_UNISTD_H
36 #endif
37
38 #define YY_NO_INPUT
39 #define YY_USER_ACTION \
40 do { \
41 yylloc->first_column = yycolumn + 1; \
42 yylloc->first_line = yylloc->last_line = yylineno + 1; \
43 yycolumn += yyleng; \
44 yylloc->last_column = yycolumn + 1; \
45 } while(0);
46
47 #define YY_USER_INIT yylineno = 0; yycolumn = 0; yylloc->source = 0;
48
49 /* A macro for handling reserved words and keywords across language versions.
50 *
51 * Certain words start out as identifiers, become reserved words in
52 * later language revisions, and finally become language keywords.
53 * This may happen at different times in desktop GLSL and GLSL ES.
54 *
55 * For example, consider the following lexer rule:
56 * samplerBuffer KEYWORD(130, 0, 140, 0, SAMPLERBUFFER)
57 *
58 * This means that "samplerBuffer" will be treated as:
59 * - a keyword (SAMPLERBUFFER token) ...in GLSL >= 1.40
60 * - a reserved word - error ...in GLSL >= 1.30
61 * - an identifier ...in GLSL < 1.30 or GLSL ES
62 */
63 #define KEYWORD(reserved_glsl, reserved_glsl_es, \
64 allowed_glsl, allowed_glsl_es, token) \
65 KEYWORD_WITH_ALT(reserved_glsl, reserved_glsl_es, \
66 allowed_glsl, allowed_glsl_es, false, token)
67
68 /**
69 * Like the KEYWORD macro, but the word is also treated as a keyword
70 * if the given boolean expression is true.
71 */
72 #define KEYWORD_WITH_ALT(reserved_glsl, reserved_glsl_es, \
73 allowed_glsl, allowed_glsl_es, \
74 alt_expr, token) \
75 do { \
76 if (yyextra->is_version(allowed_glsl, allowed_glsl_es) \
77 || (alt_expr)) { \
78 return token; \
79 } else if (yyextra->is_version(reserved_glsl, \
80 reserved_glsl_es)) { \
81 _mesa_glsl_error(yylloc, yyextra, \
82 "illegal use of reserved word `%s'", yytext); \
83 return ERROR_TOK; \
84 } else { \
85 return classify_identifier(yyextra, yytext, yyleng, yylval); \
86 } \
87 } while (0)
88
89 /**
90 * Like KEYWORD_WITH_ALT, but used for built-in GLSL types
91 */
92 #define TYPE_WITH_ALT(reserved_glsl, reserved_glsl_es, \
93 allowed_glsl, allowed_glsl_es, \
94 alt_expr, gtype) \
95 do { \
96 if (yyextra->is_version(allowed_glsl, allowed_glsl_es) \
97 || (alt_expr)) { \
98 yylval->type = gtype; \
99 return BASIC_TYPE_TOK; \
100 } else if (yyextra->is_version(reserved_glsl, \
101 reserved_glsl_es)) { \
102 _mesa_glsl_error(yylloc, yyextra, \
103 "illegal use of reserved word `%s'", yytext); \
104 return ERROR_TOK; \
105 } else { \
106 return classify_identifier(yyextra, yytext, yyleng, yylval); \
107 } \
108 } while (0)
109
110 #define TYPE(reserved_glsl, reserved_glsl_es, \
111 allowed_glsl, allowed_glsl_es, \
112 gtype) \
113 TYPE_WITH_ALT(reserved_glsl, reserved_glsl_es, \
114 allowed_glsl, allowed_glsl_es, \
115 false, gtype)
116
117 /**
118 * A macro for handling keywords that have been present in GLSL since
119 * its origin, but were changed into reserved words in GLSL 3.00 ES.
120 */
121 #define DEPRECATED_ES_KEYWORD(token) \
122 do { \
123 if (yyextra->is_version(0, 300)) { \
124 _mesa_glsl_error(yylloc, yyextra, \
125 "illegal use of reserved word `%s'", yytext); \
126 return ERROR_TOK; \
127 } else { \
128 return token; \
129 } \
130 } while (0)
131
132 /**
133 * Like DEPRECATED_ES_KEYWORD, but for types
134 */
135 #define DEPRECATED_ES_TYPE_WITH_ALT(alt_expr, gtype) \
136 do { \
137 if (yyextra->is_version(0, 300)) { \
138 _mesa_glsl_error(yylloc, yyextra, \
139 "illegal use of reserved word `%s'", yytext); \
140 return ERROR_TOK; \
141 } else if (alt_expr) { \
142 yylval->type = gtype; \
143 return BASIC_TYPE_TOK; \
144 } else { \
145 return classify_identifier(yyextra, yytext, yyleng, yylval); \
146 } \
147 } while (0)
148
149 #define DEPRECATED_ES_TYPE(gtype) \
150 DEPRECATED_ES_TYPE_WITH_ALT(true, gtype)
151
152 static int
153 literal_integer(char *text, int len, struct _mesa_glsl_parse_state *state,
154 YYSTYPE *lval, YYLTYPE *lloc, int base)
155 {
156 bool is_uint = (text[len - 1] == 'u' ||
157 text[len - 1] == 'U');
158 bool is_long = (text[len - 1] == 'l' || text[len - 1] == 'L');
159 const char *digits = text;
160
161 if (is_long)
162 is_uint = (text[len - 2] == 'u' && text[len - 1] == 'l') ||
163 (text[len - 2] == 'U' && text[len - 1] == 'L');
164 /* Skip "0x" */
165 if (base == 16)
166 digits += 2;
167
168 unsigned long long value = strtoull(digits, NULL, base);
169
170 if (is_long)
171 lval->n64 = (int64_t)value;
172 else
173 lval->n = (int)value;
174
175 if (is_long && !is_uint && base == 10 && value > (uint64_t)LLONG_MAX + 1) {
176 /* Tries to catch unintentionally providing a negative value. */
177 _mesa_glsl_warning(lloc, state,
178 "signed literal value `%s' is interpreted as %lld",
179 text, lval->n64);
180 } else if (!is_long && value > UINT_MAX) {
181 /* Note that signed 0xffffffff is valid, not out of range! */
182 if (state->is_version(130, 300)) {
183 _mesa_glsl_error(lloc, state,
184 "literal value `%s' out of range", text);
185 } else {
186 _mesa_glsl_warning(lloc, state,
187 "literal value `%s' out of range", text);
188 }
189 } else if (base == 10 && !is_uint && (unsigned)value > (unsigned)INT_MAX + 1) {
190 /* Tries to catch unintentionally providing a negative value.
191 * Note that -2147483648 is parsed as -(2147483648), so we don't
192 * want to warn for INT_MAX.
193 */
194 _mesa_glsl_warning(lloc, state,
195 "signed literal value `%s' is interpreted as %d",
196 text, lval->n);
197 }
198 if (is_long)
199 return is_uint ? UINT64CONSTANT : INT64CONSTANT;
200 else
201 return is_uint ? UINTCONSTANT : INTCONSTANT;
202 }
203
204 #define LITERAL_INTEGER(base) \
205 literal_integer(yytext, yyleng, yyextra, yylval, yylloc, base)
206
207 %}
208
209 %option bison-bridge bison-locations reentrant noyywrap
210 %option nounput noyy_top_state
211 %option never-interactive
212 %option prefix="_mesa_glsl_lexer_"
213 %option extra-type="struct _mesa_glsl_parse_state *"
214 %option warn nodefault
215
216 /* Note: When adding any start conditions to this list, you must also
217 * update the "Internal compiler error" catch-all rule near the end of
218 * this file. */
219 %x PP PRAGMA
220
221 DEC_INT [1-9][0-9]*
222 HEX_INT 0[xX][0-9a-fA-F]+
223 OCT_INT 0[0-7]*
224 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
225 SPC [ \t]*
226 SPCP [ \t]+
227 HASH ^{SPC}#{SPC}
228 %%
229
230 [ \r\t]+ ;
231
232 /* Preprocessor tokens. */
233 ^[ \t]*#[ \t]*$ ;
234 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION_TOK; }
235 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
236 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
237 /* Eat characters until the first digit is
238 * encountered
239 */
240 char *ptr = yytext;
241 while (!isdigit(*ptr))
242 ptr++;
243
244 /* Subtract one from the line number because
245 * yylineno is zero-based instead of
246 * one-based.
247 */
248 yylineno = strtol(ptr, &ptr, 0) - 1;
249
250 /* From GLSL 3.30 and GLSL ES on, after processing the
251 * line directive (including its new-line), the implementation
252 * will behave as if it is compiling at the line number passed
253 * as argument. It was line number + 1 in older specifications.
254 */
255 if (yyextra->is_version(330, 100))
256 yylineno--;
257
258 yylloc->source = strtol(ptr, NULL, 0);
259 }
260 {HASH}line{SPCP}{INT}{SPC}$ {
261 /* Eat characters until the first digit is
262 * encountered
263 */
264 char *ptr = yytext;
265 while (!isdigit(*ptr))
266 ptr++;
267
268 /* Subtract one from the line number because
269 * yylineno is zero-based instead of
270 * one-based.
271 */
272 yylineno = strtol(ptr, &ptr, 0) - 1;
273
274 /* From GLSL 3.30 and GLSL ES on, after processing the
275 * line directive (including its new-line), the implementation
276 * will behave as if it is compiling at the line number passed
277 * as argument. It was line number + 1 in older specifications.
278 */
279 if (yyextra->is_version(330, 100))
280 yylineno--;
281 }
282 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
283 BEGIN PP;
284 return PRAGMA_DEBUG_ON;
285 }
286 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
287 BEGIN PP;
288 return PRAGMA_DEBUG_OFF;
289 }
290 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
291 BEGIN PP;
292 return PRAGMA_OPTIMIZE_ON;
293 }
294 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
295 BEGIN PP;
296 return PRAGMA_OPTIMIZE_OFF;
297 }
298 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
299 BEGIN PP;
300 return PRAGMA_INVARIANT_ALL;
301 }
302 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
303
304 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
305 <PRAGMA>. { }
306
307 <PP>\/\/[^\n]* { }
308 <PP>[ \t\r]* { }
309 <PP>: return COLON;
310 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
311 /* We're not doing linear_strdup here, to avoid an implicit call
312 * on strlen() for the length of the string, as this is already
313 * found by flex and stored in yyleng
314 */
315 void *mem_ctx = yyextra->linalloc;
316 char *id = (char *) linear_alloc_child(mem_ctx, yyleng + 1);
317 memcpy(id, yytext, yyleng + 1);
318 yylval->identifier = id;
319 return IDENTIFIER;
320 }
321 <PP>[1-9][0-9]* {
322 yylval->n = strtol(yytext, NULL, 10);
323 return INTCONSTANT;
324 }
325 <PP>0 {
326 yylval->n = 0;
327 return INTCONSTANT;
328 }
329 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
330 <PP>. { return yytext[0]; }
331
332 \n { yylineno++; yycolumn = 0; }
333
334 attribute DEPRECATED_ES_KEYWORD(ATTRIBUTE);
335 const return CONST_TOK;
336 bool { yylval->type = glsl_type::bool_type; return BASIC_TYPE_TOK; }
337 float { yylval->type = glsl_type::float_type; return BASIC_TYPE_TOK; }
338 int { yylval->type = glsl_type::int_type; return BASIC_TYPE_TOK; }
339 uint TYPE(130, 300, 130, 300, glsl_type::uint_type);
340
341 break return BREAK;
342 continue return CONTINUE;
343 do return DO;
344 while return WHILE;
345 else return ELSE;
346 for return FOR;
347 if return IF;
348 discard return DISCARD;
349 return return RETURN;
350
351 bvec2 { yylval->type = glsl_type::bvec2_type; return BASIC_TYPE_TOK; }
352 bvec3 { yylval->type = glsl_type::bvec3_type; return BASIC_TYPE_TOK; }
353 bvec4 { yylval->type = glsl_type::bvec4_type; return BASIC_TYPE_TOK; }
354 ivec2 { yylval->type = glsl_type::ivec2_type; return BASIC_TYPE_TOK; }
355 ivec3 { yylval->type = glsl_type::ivec3_type; return BASIC_TYPE_TOK; }
356 ivec4 { yylval->type = glsl_type::ivec4_type; return BASIC_TYPE_TOK; }
357 uvec2 TYPE(130, 300, 130, 300, glsl_type::uvec2_type);
358 uvec3 TYPE(130, 300, 130, 300, glsl_type::uvec3_type);
359 uvec4 TYPE(130, 300, 130, 300, glsl_type::uvec4_type);
360 vec2 { yylval->type = glsl_type::vec2_type; return BASIC_TYPE_TOK; }
361 vec3 { yylval->type = glsl_type::vec3_type; return BASIC_TYPE_TOK; }
362 vec4 { yylval->type = glsl_type::vec4_type; return BASIC_TYPE_TOK; }
363 mat2 { yylval->type = glsl_type::mat2_type; return BASIC_TYPE_TOK; }
364 mat3 { yylval->type = glsl_type::mat3_type; return BASIC_TYPE_TOK; }
365 mat4 { yylval->type = glsl_type::mat4_type; return BASIC_TYPE_TOK; }
366 mat2x2 TYPE(120, 300, 120, 300, glsl_type::mat2_type);
367 mat2x3 TYPE(120, 300, 120, 300, glsl_type::mat2x3_type);
368 mat2x4 TYPE(120, 300, 120, 300, glsl_type::mat2x4_type);
369 mat3x2 TYPE(120, 300, 120, 300, glsl_type::mat3x2_type);
370 mat3x3 TYPE(120, 300, 120, 300, glsl_type::mat3_type);
371 mat3x4 TYPE(120, 300, 120, 300, glsl_type::mat3x4_type);
372 mat4x2 TYPE(120, 300, 120, 300, glsl_type::mat4x2_type);
373 mat4x3 TYPE(120, 300, 120, 300, glsl_type::mat4x3_type);
374 mat4x4 TYPE(120, 300, 120, 300, glsl_type::mat4_type);
375
376 in return IN_TOK;
377 out return OUT_TOK;
378 inout return INOUT_TOK;
379 uniform return UNIFORM;
380 buffer KEYWORD_WITH_ALT(0, 0, 430, 310, yyextra->ARB_shader_storage_buffer_object_enable, BUFFER);
381 varying DEPRECATED_ES_KEYWORD(VARYING);
382 centroid KEYWORD(120, 300, 120, 300, CENTROID);
383 invariant KEYWORD(120, 100, 120, 100, INVARIANT);
384 flat KEYWORD(130, 100, 130, 300, FLAT);
385 smooth KEYWORD(130, 300, 130, 300, SMOOTH);
386 noperspective KEYWORD(130, 300, 130, 0, NOPERSPECTIVE);
387 patch KEYWORD_WITH_ALT(0, 300, 400, 320, yyextra->has_tessellation_shader(), PATCH);
388
389 sampler1D DEPRECATED_ES_TYPE(glsl_type::sampler1D_type);
390 sampler2D { yylval->type = glsl_type::sampler2D_type; return BASIC_TYPE_TOK; }
391 sampler3D { yylval->type = glsl_type::sampler3D_type; return BASIC_TYPE_TOK; }
392 samplerCube { yylval->type = glsl_type::samplerCube_type; return BASIC_TYPE_TOK; }
393 sampler1DArray TYPE(130, 300, 130, 0, glsl_type::sampler1DArray_type);
394 sampler2DArray TYPE(130, 300, 130, 300, glsl_type::sampler2DArray_type);
395 sampler1DShadow DEPRECATED_ES_TYPE(glsl_type::sampler1DShadow_type);
396 sampler2DShadow { yylval->type = glsl_type::sampler2DShadow_type; return BASIC_TYPE_TOK; }
397 samplerCubeShadow TYPE(130, 300, 130, 300, glsl_type::samplerCubeShadow_type);
398 sampler1DArrayShadow TYPE(130, 300, 130, 0, glsl_type::sampler1DArrayShadow_type);
399 sampler2DArrayShadow TYPE(130, 300, 130, 300, glsl_type::sampler2DArrayShadow_type);
400 isampler1D TYPE(130, 300, 130, 0, glsl_type::isampler1D_type);
401 isampler2D TYPE(130, 300, 130, 300, glsl_type::isampler2D_type);
402 isampler3D TYPE(130, 300, 130, 300, glsl_type::isampler3D_type);
403 isamplerCube TYPE(130, 300, 130, 300, glsl_type::isamplerCube_type);
404 isampler1DArray TYPE(130, 300, 130, 0, glsl_type::isampler1DArray_type);
405 isampler2DArray TYPE(130, 300, 130, 300, glsl_type::isampler2DArray_type);
406 usampler1D TYPE(130, 300, 130, 0, glsl_type::usampler1D_type);
407 usampler2D TYPE(130, 300, 130, 300, glsl_type::usampler2D_type);
408 usampler3D TYPE(130, 300, 130, 300, glsl_type::usampler3D_type);
409 usamplerCube TYPE(130, 300, 130, 300, glsl_type::usamplerCube_type);
410 usampler1DArray TYPE(130, 300, 130, 0, glsl_type::usampler1DArray_type);
411 usampler2DArray TYPE(130, 300, 130, 300, glsl_type::usampler2DArray_type);
412
413 /* additional keywords in ARB_texture_multisample, included in GLSL 1.50 */
414 /* these are reserved but not defined in GLSL 3.00 */
415 /* [iu]sampler2DMS are defined in GLSL ES 3.10 */
416 sampler2DMS TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::sampler2DMS_type);
417 isampler2DMS TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::isampler2DMS_type);
418 usampler2DMS TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::usampler2DMS_type);
419 sampler2DMSArray TYPE_WITH_ALT(150, 300, 150, 320, yyextra->ARB_texture_multisample_enable || yyextra->OES_texture_storage_multisample_2d_array_enable, glsl_type::sampler2DMSArray_type);
420 isampler2DMSArray TYPE_WITH_ALT(150, 300, 150, 320, yyextra->ARB_texture_multisample_enable || yyextra->OES_texture_storage_multisample_2d_array_enable, glsl_type::isampler2DMSArray_type);
421 usampler2DMSArray TYPE_WITH_ALT(150, 300, 150, 320, yyextra->ARB_texture_multisample_enable || yyextra->OES_texture_storage_multisample_2d_array_enable, glsl_type::usampler2DMSArray_type);
422
423 /* keywords available with ARB_texture_cube_map_array_enable extension on desktop GLSL */
424 samplerCubeArray TYPE_WITH_ALT(400, 310, 400, 320, yyextra->ARB_texture_cube_map_array_enable || yyextra->OES_texture_cube_map_array_enable || yyextra->EXT_texture_cube_map_array_enable, glsl_type::samplerCubeArray_type);
425 isamplerCubeArray TYPE_WITH_ALT(400, 310, 400, 320, yyextra->ARB_texture_cube_map_array_enable || yyextra->OES_texture_cube_map_array_enable || yyextra->EXT_texture_cube_map_array_enable, glsl_type::isamplerCubeArray_type);
426 usamplerCubeArray TYPE_WITH_ALT(400, 310, 400, 320, yyextra->ARB_texture_cube_map_array_enable || yyextra->OES_texture_cube_map_array_enable || yyextra->EXT_texture_cube_map_array_enable, glsl_type::usamplerCubeArray_type);
427 samplerCubeArrayShadow TYPE_WITH_ALT(400, 310, 400, 320, yyextra->ARB_texture_cube_map_array_enable || yyextra->OES_texture_cube_map_array_enable || yyextra->EXT_texture_cube_map_array_enable, glsl_type::samplerCubeArrayShadow_type);
428
429 samplerExternalOES {
430 if (yyextra->OES_EGL_image_external_enable || yyextra->OES_EGL_image_external_essl3_enable) {
431 yylval->type = glsl_type::samplerExternalOES_type;
432 return BASIC_TYPE_TOK;
433 } else
434 return IDENTIFIER;
435 }
436
437 /* keywords available with ARB_gpu_shader5 */
438 precise KEYWORD_WITH_ALT(400, 310, 400, 320, yyextra->ARB_gpu_shader5_enable || yyextra->EXT_gpu_shader5_enable || yyextra->OES_gpu_shader5_enable, PRECISE);
439
440 /* keywords available with ARB_shader_image_load_store */
441 image1D TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image1D_type);
442 image2D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2D_type);
443 image3D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image3D_type);
444 image2DRect TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DRect_type);
445 imageCube TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::imageCube_type);
446 imageBuffer TYPE_WITH_ALT(130, 300, 420, 320, yyextra->ARB_shader_image_load_store_enable || yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::imageBuffer_type);
447 image1DArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image1DArray_type);
448 image2DArray TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DArray_type);
449 imageCubeArray TYPE_WITH_ALT(130, 300, 420, 320, yyextra->ARB_shader_image_load_store_enable || yyextra->OES_texture_cube_map_array_enable || yyextra->EXT_texture_cube_map_array_enable, glsl_type::imageCubeArray_type);
450 image2DMS TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DMS_type);
451 image2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DMSArray_type);
452 iimage1D TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage1D_type);
453 iimage2D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2D_type);
454 iimage3D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage3D_type);
455 iimage2DRect TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DRect_type);
456 iimageCube TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimageCube_type);
457 iimageBuffer TYPE_WITH_ALT(130, 300, 420, 320, yyextra->ARB_shader_image_load_store_enable || yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::iimageBuffer_type);
458 iimage1DArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage1DArray_type);
459 iimage2DArray TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DArray_type);
460 iimageCubeArray TYPE_WITH_ALT(130, 300, 420, 320, yyextra->ARB_shader_image_load_store_enable || yyextra->OES_texture_cube_map_array_enable || yyextra->EXT_texture_cube_map_array_enable, glsl_type::iimageCubeArray_type);
461 iimage2DMS TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DMS_type);
462 iimage2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DMSArray_type);
463 uimage1D TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage1D_type);
464 uimage2D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2D_type);
465 uimage3D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage3D_type);
466 uimage2DRect TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DRect_type);
467 uimageCube TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimageCube_type);
468 uimageBuffer TYPE_WITH_ALT(130, 300, 420, 320, yyextra->ARB_shader_image_load_store_enable || yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::uimageBuffer_type);
469 uimage1DArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage1DArray_type);
470 uimage2DArray TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DArray_type);
471 uimageCubeArray TYPE_WITH_ALT(130, 300, 420, 320, yyextra->ARB_shader_image_load_store_enable || yyextra->OES_texture_cube_map_array_enable || yyextra->EXT_texture_cube_map_array_enable, glsl_type::uimageCubeArray_type);
472 uimage2DMS TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DMS_type);
473 uimage2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DMSArray_type);
474 image1DShadow KEYWORD(130, 300, 0, 0, IMAGE1DSHADOW);
475 image2DShadow KEYWORD(130, 300, 0, 0, IMAGE2DSHADOW);
476 image1DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE1DARRAYSHADOW);
477 image2DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE2DARRAYSHADOW);
478
479 coherent KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, COHERENT);
480 volatile KEYWORD_WITH_ALT(110, 100, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, VOLATILE);
481 restrict KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, RESTRICT);
482 readonly KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, READONLY);
483 writeonly KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, WRITEONLY);
484
485 atomic_uint TYPE_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_atomic_counters_enable, glsl_type::atomic_uint_type);
486
487 shared KEYWORD_WITH_ALT(430, 310, 430, 310, yyextra->ARB_compute_shader_enable, SHARED);
488
489 struct return STRUCT;
490 void return VOID_TOK;
491
492 layout {
493 if ((yyextra->is_version(140, 300))
494 || yyextra->ARB_bindless_texture_enable
495 || yyextra->KHR_blend_equation_advanced_enable
496 || yyextra->AMD_conservative_depth_enable
497 || yyextra->ARB_conservative_depth_enable
498 || yyextra->ARB_explicit_attrib_location_enable
499 || yyextra->ARB_explicit_uniform_location_enable
500 || yyextra->has_separate_shader_objects()
501 || yyextra->ARB_uniform_buffer_object_enable
502 || yyextra->ARB_fragment_coord_conventions_enable
503 || yyextra->ARB_shading_language_420pack_enable
504 || yyextra->ARB_compute_shader_enable
505 || yyextra->ARB_tessellation_shader_enable
506 || yyextra->EXT_shader_framebuffer_fetch_non_coherent_enable) {
507 return LAYOUT_TOK;
508 } else {
509 return classify_identifier(yyextra, yytext, yyleng, yylval);
510 }
511 }
512
513 \+\+ return INC_OP;
514 -- return DEC_OP;
515 \<= return LE_OP;
516 >= return GE_OP;
517 == return EQ_OP;
518 != return NE_OP;
519 && return AND_OP;
520 \|\| return OR_OP;
521 "^^" return XOR_OP;
522 "<<" return LEFT_OP;
523 ">>" return RIGHT_OP;
524
525 \*= return MUL_ASSIGN;
526 \/= return DIV_ASSIGN;
527 \+= return ADD_ASSIGN;
528 \%= return MOD_ASSIGN;
529 \<\<= return LEFT_ASSIGN;
530 >>= return RIGHT_ASSIGN;
531 &= return AND_ASSIGN;
532 "^=" return XOR_ASSIGN;
533 \|= return OR_ASSIGN;
534 -= return SUB_ASSIGN;
535
536 [1-9][0-9]*([uU]|[lL]|ul|UL)? {
537 return LITERAL_INTEGER(10);
538 }
539 0[xX][0-9a-fA-F]+([uU]|[lL]|ul|UL)? {
540 return LITERAL_INTEGER(16);
541 }
542 0[0-7]*([uU]|[lL]|ul|UL)? {
543 return LITERAL_INTEGER(8);
544 }
545
546 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? |
547 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? |
548 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? |
549 [0-9]+[eE][+-]?[0-9]+[fF]? {
550 struct _mesa_glsl_parse_state *state = yyextra;
551 char suffix = yytext[strlen(yytext) - 1];
552 if (!state->is_version(120, 300) &&
553 (suffix == 'f' || suffix == 'F')) {
554 _mesa_glsl_warning(yylloc, state,
555 "Float suffixes are invalid in GLSL 1.10");
556 }
557 yylval->real = _mesa_strtof(yytext, NULL);
558 return FLOATCONSTANT;
559 }
560
561 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?(lf|LF) |
562 \.[0-9]+([eE][+-]?[0-9]+)?(lf|LF) |
563 [0-9]+\.([eE][+-]?[0-9]+)?(lf|LF) |
564 [0-9]+[eE][+-]?[0-9]+(lf|LF) {
565 if (!yyextra->is_version(400, 0) &&
566 !yyextra->ARB_gpu_shader_fp64_enable)
567 return ERROR_TOK;
568 yylval->dreal = _mesa_strtod(yytext, NULL);
569 return DOUBLECONSTANT;
570 }
571
572 true {
573 yylval->n = 1;
574 return BOOLCONSTANT;
575 }
576 false {
577 yylval->n = 0;
578 return BOOLCONSTANT;
579 }
580
581
582 /* Reserved words in GLSL 1.10. */
583 asm KEYWORD(110, 100, 0, 0, ASM);
584 class KEYWORD(110, 100, 0, 0, CLASS);
585 union KEYWORD(110, 100, 0, 0, UNION);
586 enum KEYWORD(110, 100, 0, 0, ENUM);
587 typedef KEYWORD(110, 100, 0, 0, TYPEDEF);
588 template KEYWORD(110, 100, 0, 0, TEMPLATE);
589 this KEYWORD(110, 100, 0, 0, THIS);
590 packed KEYWORD_WITH_ALT(110, 100, 140, 300, yyextra->ARB_uniform_buffer_object_enable, PACKED_TOK);
591 goto KEYWORD(110, 100, 0, 0, GOTO);
592 switch KEYWORD(110, 100, 130, 300, SWITCH);
593 default KEYWORD(110, 100, 130, 300, DEFAULT);
594 inline KEYWORD(110, 100, 0, 0, INLINE_TOK);
595 noinline KEYWORD(110, 100, 0, 0, NOINLINE);
596 public KEYWORD(110, 100, 0, 0, PUBLIC_TOK);
597 static KEYWORD(110, 100, 0, 0, STATIC);
598 extern KEYWORD(110, 100, 0, 0, EXTERN);
599 external KEYWORD(110, 100, 0, 0, EXTERNAL);
600 interface KEYWORD(110, 100, 0, 0, INTERFACE);
601 long KEYWORD(110, 100, 0, 0, LONG_TOK);
602 short KEYWORD(110, 100, 0, 0, SHORT_TOK);
603 double TYPE_WITH_ALT(130, 100, 130, 300, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::double_type);
604 half KEYWORD(110, 100, 0, 0, HALF);
605 fixed KEYWORD(110, 100, 0, 0, FIXED_TOK);
606 unsigned KEYWORD(110, 100, 0, 0, UNSIGNED);
607 input KEYWORD(110, 100, 0, 0, INPUT_TOK);
608 output KEYWORD(110, 100, 0, 0, OUTPUT);
609 hvec2 KEYWORD(110, 100, 0, 0, HVEC2);
610 hvec3 KEYWORD(110, 100, 0, 0, HVEC3);
611 hvec4 KEYWORD(110, 100, 0, 0, HVEC4);
612 dvec2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec2_type);
613 dvec3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec3_type);
614 dvec4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec4_type);
615 dmat2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2_type);
616 dmat3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3_type);
617 dmat4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4_type);
618 dmat2x2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2_type);
619 dmat2x3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2x3_type);
620 dmat2x4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2x4_type);
621 dmat3x2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3x2_type);
622 dmat3x3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3_type);
623 dmat3x4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3x4_type);
624 dmat4x2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4x2_type);
625 dmat4x3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4x3_type);
626 dmat4x4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4_type);
627 fvec2 KEYWORD(110, 100, 0, 0, FVEC2);
628 fvec3 KEYWORD(110, 100, 0, 0, FVEC3);
629 fvec4 KEYWORD(110, 100, 0, 0, FVEC4);
630 sampler2DRect TYPE_WITH_ALT(110, 100, 0, 0, yyextra->ARB_texture_rectangle_enable, glsl_type::sampler2DRect_type);
631 sampler3DRect KEYWORD(110, 100, 0, 0, SAMPLER3DRECT);
632 sampler2DRectShadow TYPE_WITH_ALT(110, 100, 0, 0, yyextra->ARB_texture_rectangle_enable, glsl_type::sampler2DRectShadow_type);
633 sizeof KEYWORD(110, 100, 0, 0, SIZEOF);
634 cast KEYWORD(110, 100, 0, 0, CAST);
635 namespace KEYWORD(110, 100, 0, 0, NAMESPACE);
636 using KEYWORD(110, 100, 0, 0, USING);
637
638 /* Additional reserved words in GLSL 1.20. */
639 lowp KEYWORD(120, 100, 130, 100, LOWP);
640 mediump KEYWORD(120, 100, 130, 100, MEDIUMP);
641 highp KEYWORD(120, 100, 130, 100, HIGHP);
642 precision KEYWORD(120, 100, 130, 100, PRECISION);
643
644 /* Additional reserved words in GLSL 1.30. */
645 case KEYWORD(130, 300, 130, 300, CASE);
646 common KEYWORD(130, 300, 0, 0, COMMON);
647 partition KEYWORD(130, 300, 0, 0, PARTITION);
648 active KEYWORD(130, 300, 0, 0, ACTIVE);
649 superp KEYWORD(130, 100, 0, 0, SUPERP);
650 samplerBuffer TYPE_WITH_ALT(130, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::samplerBuffer_type);
651 filter KEYWORD(130, 300, 0, 0, FILTER);
652 row_major KEYWORD_WITH_ALT(130, 0, 140, 0, yyextra->ARB_uniform_buffer_object_enable && !yyextra->es_shader, ROW_MAJOR);
653
654 /* Additional reserved words in GLSL 1.40 */
655 isampler2DRect TYPE(140, 300, 140, 0, glsl_type::isampler2DRect_type);
656 usampler2DRect TYPE(140, 300, 140, 0, glsl_type::usampler2DRect_type);
657 isamplerBuffer TYPE_WITH_ALT(140, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::isamplerBuffer_type);
658 usamplerBuffer TYPE_WITH_ALT(140, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::usamplerBuffer_type);
659
660 /* Additional reserved words in GLSL ES 3.00 */
661 resource KEYWORD(420, 300, 0, 0, RESOURCE);
662 sample KEYWORD_WITH_ALT(400, 300, 400, 320, yyextra->ARB_gpu_shader5_enable || yyextra->OES_shader_multisample_interpolation_enable, SAMPLE);
663 subroutine KEYWORD_WITH_ALT(400, 300, 400, 0, yyextra->ARB_shader_subroutine_enable, SUBROUTINE);
664
665 /* Additional words for ARB_gpu_shader_int64 */
666 int64_t TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::int64_t_type);
667 i64vec2 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec2_type);
668 i64vec3 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec3_type);
669 i64vec4 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec4_type);
670
671 uint64_t TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::uint64_t_type);
672 u64vec2 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec2_type);
673 u64vec3 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec3_type);
674 u64vec4 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec4_type);
675
676 [_a-zA-Z][_a-zA-Z0-9]* {
677 struct _mesa_glsl_parse_state *state = yyextra;
678 if (state->es_shader && yyleng > 1024) {
679 _mesa_glsl_error(yylloc, state,
680 "Identifier `%s' exceeds 1024 characters",
681 yytext);
682 }
683 return classify_identifier(state, yytext, yyleng, yylval);
684 }
685
686 \. { struct _mesa_glsl_parse_state *state = yyextra;
687 state->is_field = true;
688 return DOT_TOK; }
689
690 . { return yytext[0]; }
691
692 %%
693
694 int
695 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name,
696 unsigned name_len, YYSTYPE *output)
697 {
698 /* We're not doing linear_strdup here, to avoid an implicit call on
699 * strlen() for the length of the string, as this is already found by flex
700 * and stored in yyleng
701 */
702 char *id = (char *) linear_alloc_child(state->linalloc, name_len + 1);
703 memcpy(id, name, name_len + 1);
704 output->identifier = id;
705
706 if (state->is_field) {
707 state->is_field = false;
708 return FIELD_SELECTION;
709 }
710 if (state->symbols->get_variable(name) || state->symbols->get_function(name))
711 return IDENTIFIER;
712 else if (state->symbols->get_type(name))
713 return TYPE_IDENTIFIER;
714 else
715 return NEW_IDENTIFIER;
716 }
717
718 void
719 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
720 {
721 yylex_init_extra(state, & state->scanner);
722 yy_scan_string(string, state->scanner);
723 }
724
725 void
726 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
727 {
728 yylex_destroy(state->scanner);
729 }