glsl/parser: Return the glsl_type object from the lexer
[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(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 { \
142 yylval->type = gtype; \
143 return BASIC_TYPE_TOK; \
144 } \
145 } while (0)
146
147 static int
148 literal_integer(char *text, int len, struct _mesa_glsl_parse_state *state,
149 YYSTYPE *lval, YYLTYPE *lloc, int base)
150 {
151 bool is_uint = (text[len - 1] == 'u' ||
152 text[len - 1] == 'U');
153 bool is_long = (text[len - 1] == 'l' || text[len - 1] == 'L');
154 const char *digits = text;
155
156 if (is_long)
157 is_uint = (text[len - 2] == 'u' && text[len - 1] == 'l') ||
158 (text[len - 2] == 'U' && text[len - 1] == 'L');
159 /* Skip "0x" */
160 if (base == 16)
161 digits += 2;
162
163 unsigned long long value = strtoull(digits, NULL, base);
164
165 if (is_long)
166 lval->n64 = (int64_t)value;
167 else
168 lval->n = (int)value;
169
170 if (is_long && !is_uint && base == 10 && value > (uint64_t)LLONG_MAX + 1) {
171 /* Tries to catch unintentionally providing a negative value. */
172 _mesa_glsl_warning(lloc, state,
173 "signed literal value `%s' is interpreted as %lld",
174 text, lval->n64);
175 } else if (!is_long && value > UINT_MAX) {
176 /* Note that signed 0xffffffff is valid, not out of range! */
177 if (state->is_version(130, 300)) {
178 _mesa_glsl_error(lloc, state,
179 "literal value `%s' out of range", text);
180 } else {
181 _mesa_glsl_warning(lloc, state,
182 "literal value `%s' out of range", text);
183 }
184 } else if (base == 10 && !is_uint && (unsigned)value > (unsigned)INT_MAX + 1) {
185 /* Tries to catch unintentionally providing a negative value.
186 * Note that -2147483648 is parsed as -(2147483648), so we don't
187 * want to warn for INT_MAX.
188 */
189 _mesa_glsl_warning(lloc, state,
190 "signed literal value `%s' is interpreted as %d",
191 text, lval->n);
192 }
193 if (is_long)
194 return is_uint ? UINT64CONSTANT : INT64CONSTANT;
195 else
196 return is_uint ? UINTCONSTANT : INTCONSTANT;
197 }
198
199 #define LITERAL_INTEGER(base) \
200 literal_integer(yytext, yyleng, yyextra, yylval, yylloc, base)
201
202 %}
203
204 %option bison-bridge bison-locations reentrant noyywrap
205 %option nounput noyy_top_state
206 %option never-interactive
207 %option prefix="_mesa_glsl_lexer_"
208 %option extra-type="struct _mesa_glsl_parse_state *"
209 %option warn nodefault
210
211 /* Note: When adding any start conditions to this list, you must also
212 * update the "Internal compiler error" catch-all rule near the end of
213 * this file. */
214 %x PP PRAGMA
215
216 DEC_INT [1-9][0-9]*
217 HEX_INT 0[xX][0-9a-fA-F]+
218 OCT_INT 0[0-7]*
219 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
220 SPC [ \t]*
221 SPCP [ \t]+
222 HASH ^{SPC}#{SPC}
223 %%
224
225 [ \r\t]+ ;
226
227 /* Preprocessor tokens. */
228 ^[ \t]*#[ \t]*$ ;
229 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION_TOK; }
230 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
231 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
232 /* Eat characters until the first digit is
233 * encountered
234 */
235 char *ptr = yytext;
236 while (!isdigit(*ptr))
237 ptr++;
238
239 /* Subtract one from the line number because
240 * yylineno is zero-based instead of
241 * one-based.
242 */
243 yylineno = strtol(ptr, &ptr, 0) - 1;
244
245 /* From GLSL 3.30 and GLSL ES on, after processing the
246 * line directive (including its new-line), the implementation
247 * will behave as if it is compiling at the line number passed
248 * as argument. It was line number + 1 in older specifications.
249 */
250 if (yyextra->is_version(330, 100))
251 yylineno--;
252
253 yylloc->source = strtol(ptr, NULL, 0);
254 }
255 {HASH}line{SPCP}{INT}{SPC}$ {
256 /* Eat characters until the first digit is
257 * encountered
258 */
259 char *ptr = yytext;
260 while (!isdigit(*ptr))
261 ptr++;
262
263 /* Subtract one from the line number because
264 * yylineno is zero-based instead of
265 * one-based.
266 */
267 yylineno = strtol(ptr, &ptr, 0) - 1;
268
269 /* From GLSL 3.30 and GLSL ES on, after processing the
270 * line directive (including its new-line), the implementation
271 * will behave as if it is compiling at the line number passed
272 * as argument. It was line number + 1 in older specifications.
273 */
274 if (yyextra->is_version(330, 100))
275 yylineno--;
276 }
277 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
278 BEGIN PP;
279 return PRAGMA_DEBUG_ON;
280 }
281 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
282 BEGIN PP;
283 return PRAGMA_DEBUG_OFF;
284 }
285 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
286 BEGIN PP;
287 return PRAGMA_OPTIMIZE_ON;
288 }
289 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
290 BEGIN PP;
291 return PRAGMA_OPTIMIZE_OFF;
292 }
293 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
294 BEGIN PP;
295 return PRAGMA_INVARIANT_ALL;
296 }
297 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
298
299 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
300 <PRAGMA>. { }
301
302 <PP>\/\/[^\n]* { }
303 <PP>[ \t\r]* { }
304 <PP>: return COLON;
305 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
306 /* We're not doing linear_strdup here, to avoid an implicit call
307 * on strlen() for the length of the string, as this is already
308 * found by flex and stored in yyleng
309 */
310 void *mem_ctx = yyextra->linalloc;
311 char *id = (char *) linear_alloc_child(mem_ctx, yyleng + 1);
312 memcpy(id, yytext, yyleng + 1);
313 yylval->identifier = id;
314 return IDENTIFIER;
315 }
316 <PP>[1-9][0-9]* {
317 yylval->n = strtol(yytext, NULL, 10);
318 return INTCONSTANT;
319 }
320 <PP>0 {
321 yylval->n = 0;
322 return INTCONSTANT;
323 }
324 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
325 <PP>. { return yytext[0]; }
326
327 \n { yylineno++; yycolumn = 0; }
328
329 attribute DEPRECATED_ES_KEYWORD(ATTRIBUTE);
330 const return CONST_TOK;
331 bool { yylval->type = glsl_type::bool_type; return BASIC_TYPE_TOK; }
332 float { yylval->type = glsl_type::float_type; return BASIC_TYPE_TOK; }
333 int { yylval->type = glsl_type::int_type; return BASIC_TYPE_TOK; }
334 uint TYPE(130, 300, 130, 300, glsl_type::uint_type);
335
336 break return BREAK;
337 continue return CONTINUE;
338 do return DO;
339 while return WHILE;
340 else return ELSE;
341 for return FOR;
342 if return IF;
343 discard return DISCARD;
344 return return RETURN;
345
346 bvec2 { yylval->type = glsl_type::bvec2_type; return BASIC_TYPE_TOK; }
347 bvec3 { yylval->type = glsl_type::bvec3_type; return BASIC_TYPE_TOK; }
348 bvec4 { yylval->type = glsl_type::bvec4_type; return BASIC_TYPE_TOK; }
349 ivec2 { yylval->type = glsl_type::ivec2_type; return BASIC_TYPE_TOK; }
350 ivec3 { yylval->type = glsl_type::ivec3_type; return BASIC_TYPE_TOK; }
351 ivec4 { yylval->type = glsl_type::ivec4_type; return BASIC_TYPE_TOK; }
352 uvec2 TYPE(130, 300, 130, 300, glsl_type::uvec2_type);
353 uvec3 TYPE(130, 300, 130, 300, glsl_type::uvec3_type);
354 uvec4 TYPE(130, 300, 130, 300, glsl_type::uvec4_type);
355 vec2 { yylval->type = glsl_type::vec2_type; return BASIC_TYPE_TOK; }
356 vec3 { yylval->type = glsl_type::vec3_type; return BASIC_TYPE_TOK; }
357 vec4 { yylval->type = glsl_type::vec4_type; return BASIC_TYPE_TOK; }
358 mat2 { yylval->type = glsl_type::mat2_type; return BASIC_TYPE_TOK; }
359 mat3 { yylval->type = glsl_type::mat3_type; return BASIC_TYPE_TOK; }
360 mat4 { yylval->type = glsl_type::mat4_type; return BASIC_TYPE_TOK; }
361 mat2x2 TYPE(120, 300, 120, 300, glsl_type::mat2_type);
362 mat2x3 TYPE(120, 300, 120, 300, glsl_type::mat2x3_type);
363 mat2x4 TYPE(120, 300, 120, 300, glsl_type::mat2x4_type);
364 mat3x2 TYPE(120, 300, 120, 300, glsl_type::mat3x2_type);
365 mat3x3 TYPE(120, 300, 120, 300, glsl_type::mat3_type);
366 mat3x4 TYPE(120, 300, 120, 300, glsl_type::mat3x4_type);
367 mat4x2 TYPE(120, 300, 120, 300, glsl_type::mat4x2_type);
368 mat4x3 TYPE(120, 300, 120, 300, glsl_type::mat4x3_type);
369 mat4x4 TYPE(120, 300, 120, 300, glsl_type::mat4_type);
370
371 in return IN_TOK;
372 out return OUT_TOK;
373 inout return INOUT_TOK;
374 uniform return UNIFORM;
375 buffer KEYWORD_WITH_ALT(0, 0, 430, 310, yyextra->ARB_shader_storage_buffer_object_enable, BUFFER);
376 varying DEPRECATED_ES_KEYWORD(VARYING);
377 centroid KEYWORD(120, 300, 120, 300, CENTROID);
378 invariant KEYWORD(120, 100, 120, 100, INVARIANT);
379 flat KEYWORD(130, 100, 130, 300, FLAT);
380 smooth KEYWORD(130, 300, 130, 300, SMOOTH);
381 noperspective KEYWORD(130, 300, 130, 0, NOPERSPECTIVE);
382 patch KEYWORD_WITH_ALT(0, 300, 400, 320, yyextra->has_tessellation_shader(), PATCH);
383
384 sampler1D DEPRECATED_ES_TYPE(glsl_type::sampler1D_type);
385 sampler2D { yylval->type = glsl_type::sampler2D_type; return BASIC_TYPE_TOK; }
386 sampler3D { yylval->type = glsl_type::sampler3D_type; return BASIC_TYPE_TOK; }
387 samplerCube { yylval->type = glsl_type::samplerCube_type; return BASIC_TYPE_TOK; }
388 sampler1DArray TYPE(130, 300, 130, 0, glsl_type::sampler1DArray_type);
389 sampler2DArray TYPE(130, 300, 130, 300, glsl_type::sampler2DArray_type);
390 sampler1DShadow DEPRECATED_ES_TYPE(glsl_type::sampler1DShadow_type);
391 sampler2DShadow { yylval->type = glsl_type::sampler2DShadow_type; return BASIC_TYPE_TOK; }
392 samplerCubeShadow TYPE(130, 300, 130, 300, glsl_type::samplerCubeShadow_type);
393 sampler1DArrayShadow TYPE(130, 300, 130, 0, glsl_type::sampler1DArrayShadow_type);
394 sampler2DArrayShadow TYPE(130, 300, 130, 300, glsl_type::sampler2DArrayShadow_type);
395 isampler1D TYPE(130, 300, 130, 0, glsl_type::isampler1D_type);
396 isampler2D TYPE(130, 300, 130, 300, glsl_type::isampler2D_type);
397 isampler3D TYPE(130, 300, 130, 300, glsl_type::isampler3D_type);
398 isamplerCube TYPE(130, 300, 130, 300, glsl_type::isamplerCube_type);
399 isampler1DArray TYPE(130, 300, 130, 0, glsl_type::isampler1DArray_type);
400 isampler2DArray TYPE(130, 300, 130, 300, glsl_type::isampler2DArray_type);
401 usampler1D TYPE(130, 300, 130, 0, glsl_type::usampler1D_type);
402 usampler2D TYPE(130, 300, 130, 300, glsl_type::usampler2D_type);
403 usampler3D TYPE(130, 300, 130, 300, glsl_type::usampler3D_type);
404 usamplerCube TYPE(130, 300, 130, 300, glsl_type::usamplerCube_type);
405 usampler1DArray TYPE(130, 300, 130, 0, glsl_type::usampler1DArray_type);
406 usampler2DArray TYPE(130, 300, 130, 300, glsl_type::usampler2DArray_type);
407
408 /* additional keywords in ARB_texture_multisample, included in GLSL 1.50 */
409 /* these are reserved but not defined in GLSL 3.00 */
410 /* [iu]sampler2DMS are defined in GLSL ES 3.10 */
411 sampler2DMS TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::sampler2DMS_type);
412 isampler2DMS TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::isampler2DMS_type);
413 usampler2DMS TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::usampler2DMS_type);
414 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);
415 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);
416 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);
417
418 /* keywords available with ARB_texture_cube_map_array_enable extension on desktop GLSL */
419 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);
420 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);
421 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);
422 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);
423
424 samplerExternalOES {
425 if (yyextra->OES_EGL_image_external_enable) {
426 yylval->type = glsl_type::samplerExternalOES_type;
427 return BASIC_TYPE_TOK;
428 } else
429 return IDENTIFIER;
430 }
431
432 /* keywords available with ARB_gpu_shader5 */
433 precise KEYWORD_WITH_ALT(400, 310, 400, 320, yyextra->ARB_gpu_shader5_enable || yyextra->EXT_gpu_shader5_enable || yyextra->OES_gpu_shader5_enable, PRECISE);
434
435 /* keywords available with ARB_shader_image_load_store */
436 image1D TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image1D_type);
437 image2D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2D_type);
438 image3D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image3D_type);
439 image2DRect TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DRect_type);
440 imageCube TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::imageCube_type);
441 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);
442 image1DArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image1DArray_type);
443 image2DArray TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DArray_type);
444 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);
445 image2DMS TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DMS_type);
446 image2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DMSArray_type);
447 iimage1D TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage1D_type);
448 iimage2D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2D_type);
449 iimage3D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage3D_type);
450 iimage2DRect TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DRect_type);
451 iimageCube TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimageCube_type);
452 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);
453 iimage1DArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage1DArray_type);
454 iimage2DArray TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DArray_type);
455 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);
456 iimage2DMS TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DMS_type);
457 iimage2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DMSArray_type);
458 uimage1D TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage1D_type);
459 uimage2D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2D_type);
460 uimage3D TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage3D_type);
461 uimage2DRect TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DRect_type);
462 uimageCube TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimageCube_type);
463 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);
464 uimage1DArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage1DArray_type);
465 uimage2DArray TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DArray_type);
466 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);
467 uimage2DMS TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DMS_type);
468 uimage2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DMSArray_type);
469 image1DShadow KEYWORD(130, 300, 0, 0, IMAGE1DSHADOW);
470 image2DShadow KEYWORD(130, 300, 0, 0, IMAGE2DSHADOW);
471 image1DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE1DARRAYSHADOW);
472 image2DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE2DARRAYSHADOW);
473
474 coherent KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, COHERENT);
475 volatile KEYWORD_WITH_ALT(110, 100, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, VOLATILE);
476 restrict KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, RESTRICT);
477 readonly KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, READONLY);
478 writeonly KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, WRITEONLY);
479
480 atomic_uint TYPE_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_atomic_counters_enable, glsl_type::atomic_uint_type);
481
482 shared KEYWORD_WITH_ALT(430, 310, 430, 310, yyextra->ARB_compute_shader_enable, SHARED);
483
484 struct return STRUCT;
485 void return VOID_TOK;
486
487 layout {
488 if ((yyextra->is_version(140, 300))
489 || yyextra->AMD_conservative_depth_enable
490 || yyextra->ARB_conservative_depth_enable
491 || yyextra->ARB_explicit_attrib_location_enable
492 || yyextra->ARB_explicit_uniform_location_enable
493 || yyextra->has_separate_shader_objects()
494 || yyextra->ARB_uniform_buffer_object_enable
495 || yyextra->ARB_fragment_coord_conventions_enable
496 || yyextra->ARB_shading_language_420pack_enable
497 || yyextra->ARB_compute_shader_enable
498 || yyextra->ARB_tessellation_shader_enable) {
499 return LAYOUT_TOK;
500 } else {
501 return classify_identifier(yyextra, yytext, yyleng, yylval);
502 }
503 }
504
505 \+\+ return INC_OP;
506 -- return DEC_OP;
507 \<= return LE_OP;
508 >= return GE_OP;
509 == return EQ_OP;
510 != return NE_OP;
511 && return AND_OP;
512 \|\| return OR_OP;
513 "^^" return XOR_OP;
514 "<<" return LEFT_OP;
515 ">>" return RIGHT_OP;
516
517 \*= return MUL_ASSIGN;
518 \/= return DIV_ASSIGN;
519 \+= return ADD_ASSIGN;
520 \%= return MOD_ASSIGN;
521 \<\<= return LEFT_ASSIGN;
522 >>= return RIGHT_ASSIGN;
523 &= return AND_ASSIGN;
524 "^=" return XOR_ASSIGN;
525 \|= return OR_ASSIGN;
526 -= return SUB_ASSIGN;
527
528 [1-9][0-9]*([uU]|[lL]|ul|UL)? {
529 return LITERAL_INTEGER(10);
530 }
531 0[xX][0-9a-fA-F]+([uU]|[lL]|ul|UL)? {
532 return LITERAL_INTEGER(16);
533 }
534 0[0-7]*([uU]|[lL]|ul|UL)? {
535 return LITERAL_INTEGER(8);
536 }
537
538 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? |
539 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? |
540 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? |
541 [0-9]+[eE][+-]?[0-9]+[fF]? {
542 struct _mesa_glsl_parse_state *state = yyextra;
543 char suffix = yytext[strlen(yytext) - 1];
544 if (!state->is_version(120, 300) &&
545 (suffix == 'f' || suffix == 'F')) {
546 _mesa_glsl_warning(yylloc, state,
547 "Float suffixes are invalid in GLSL 1.10");
548 }
549 yylval->real = _mesa_strtof(yytext, NULL);
550 return FLOATCONSTANT;
551 }
552
553 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?(lf|LF) |
554 \.[0-9]+([eE][+-]?[0-9]+)?(lf|LF) |
555 [0-9]+\.([eE][+-]?[0-9]+)?(lf|LF) |
556 [0-9]+[eE][+-]?[0-9]+(lf|LF) {
557 if (!yyextra->is_version(400, 0) &&
558 !yyextra->ARB_gpu_shader_fp64_enable)
559 return ERROR_TOK;
560 yylval->dreal = _mesa_strtod(yytext, NULL);
561 return DOUBLECONSTANT;
562 }
563
564 true {
565 yylval->n = 1;
566 return BOOLCONSTANT;
567 }
568 false {
569 yylval->n = 0;
570 return BOOLCONSTANT;
571 }
572
573
574 /* Reserved words in GLSL 1.10. */
575 asm KEYWORD(110, 100, 0, 0, ASM);
576 class KEYWORD(110, 100, 0, 0, CLASS);
577 union KEYWORD(110, 100, 0, 0, UNION);
578 enum KEYWORD(110, 100, 0, 0, ENUM);
579 typedef KEYWORD(110, 100, 0, 0, TYPEDEF);
580 template KEYWORD(110, 100, 0, 0, TEMPLATE);
581 this KEYWORD(110, 100, 0, 0, THIS);
582 packed KEYWORD_WITH_ALT(110, 100, 140, 300, yyextra->ARB_uniform_buffer_object_enable, PACKED_TOK);
583 goto KEYWORD(110, 100, 0, 0, GOTO);
584 switch KEYWORD(110, 100, 130, 300, SWITCH);
585 default KEYWORD(110, 100, 130, 300, DEFAULT);
586 inline KEYWORD(110, 100, 0, 0, INLINE_TOK);
587 noinline KEYWORD(110, 100, 0, 0, NOINLINE);
588 public KEYWORD(110, 100, 0, 0, PUBLIC_TOK);
589 static KEYWORD(110, 100, 0, 0, STATIC);
590 extern KEYWORD(110, 100, 0, 0, EXTERN);
591 external KEYWORD(110, 100, 0, 0, EXTERNAL);
592 interface KEYWORD(110, 100, 0, 0, INTERFACE);
593 long KEYWORD(110, 100, 0, 0, LONG_TOK);
594 short KEYWORD(110, 100, 0, 0, SHORT_TOK);
595 double TYPE_WITH_ALT(130, 300, 130, 300, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::double_type);
596 half KEYWORD(110, 100, 0, 0, HALF);
597 fixed KEYWORD(110, 100, 0, 0, FIXED_TOK);
598 unsigned KEYWORD(110, 100, 0, 0, UNSIGNED);
599 input KEYWORD(110, 100, 0, 0, INPUT_TOK);
600 output KEYWORD(110, 100, 0, 0, OUTPUT);
601 hvec2 KEYWORD(110, 100, 0, 0, HVEC2);
602 hvec3 KEYWORD(110, 100, 0, 0, HVEC3);
603 hvec4 KEYWORD(110, 100, 0, 0, HVEC4);
604 dvec2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec2_type);
605 dvec3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec3_type);
606 dvec4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec4_type);
607 dmat2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2_type);
608 dmat3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3_type);
609 dmat4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4_type);
610 dmat2x2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2_type);
611 dmat2x3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2x3_type);
612 dmat2x4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2x4_type);
613 dmat3x2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3x2_type);
614 dmat3x3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3_type);
615 dmat3x4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3x4_type);
616 dmat4x2 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4x2_type);
617 dmat4x3 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4x3_type);
618 dmat4x4 TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4_type);
619 fvec2 KEYWORD(110, 100, 0, 0, FVEC2);
620 fvec3 KEYWORD(110, 100, 0, 0, FVEC3);
621 fvec4 KEYWORD(110, 100, 0, 0, FVEC4);
622 sampler2DRect DEPRECATED_ES_TYPE(glsl_type::sampler2DRect_type);
623 sampler3DRect KEYWORD(110, 100, 0, 0, SAMPLER3DRECT);
624 sampler2DRectShadow DEPRECATED_ES_TYPE(glsl_type::sampler2DRectShadow_type);
625 sizeof KEYWORD(110, 100, 0, 0, SIZEOF);
626 cast KEYWORD(110, 100, 0, 0, CAST);
627 namespace KEYWORD(110, 100, 0, 0, NAMESPACE);
628 using KEYWORD(110, 100, 0, 0, USING);
629
630 /* Additional reserved words in GLSL 1.20. */
631 lowp KEYWORD(120, 100, 130, 100, LOWP);
632 mediump KEYWORD(120, 100, 130, 100, MEDIUMP);
633 highp KEYWORD(120, 100, 130, 100, HIGHP);
634 precision KEYWORD(120, 100, 130, 100, PRECISION);
635
636 /* Additional reserved words in GLSL 1.30. */
637 case KEYWORD(130, 300, 130, 300, CASE);
638 common KEYWORD(130, 300, 0, 0, COMMON);
639 partition KEYWORD(130, 300, 0, 0, PARTITION);
640 active KEYWORD(130, 300, 0, 0, ACTIVE);
641 superp KEYWORD(130, 100, 0, 0, SUPERP);
642 samplerBuffer TYPE_WITH_ALT(130, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::samplerBuffer_type);
643 filter KEYWORD(130, 300, 0, 0, FILTER);
644 row_major KEYWORD_WITH_ALT(130, 0, 140, 0, yyextra->ARB_uniform_buffer_object_enable && !yyextra->es_shader, ROW_MAJOR);
645
646 /* Additional reserved words in GLSL 1.40 */
647 isampler2DRect TYPE(140, 300, 140, 0, glsl_type::isampler2DRect_type);
648 usampler2DRect TYPE(140, 300, 140, 0, glsl_type::usampler2DRect_type);
649 isamplerBuffer TYPE_WITH_ALT(140, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::isamplerBuffer_type);
650 usamplerBuffer TYPE_WITH_ALT(140, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::usamplerBuffer_type);
651
652 /* Additional reserved words in GLSL ES 3.00 */
653 resource KEYWORD(420, 300, 0, 0, RESOURCE);
654 sample KEYWORD_WITH_ALT(400, 300, 400, 320, yyextra->ARB_gpu_shader5_enable || yyextra->OES_shader_multisample_interpolation_enable, SAMPLE);
655 subroutine KEYWORD_WITH_ALT(400, 300, 400, 0, yyextra->ARB_shader_subroutine_enable, SUBROUTINE);
656
657 /* Additional words for ARB_gpu_shader_int64 */
658 int64_t TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::int64_t_type);
659 i64vec2 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec2_type);
660 i64vec3 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec3_type);
661 i64vec4 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec4_type);
662
663 uint64_t TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::uint64_t_type);
664 u64vec2 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec2_type);
665 u64vec3 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec3_type);
666 u64vec4 TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec4_type);
667
668 [_a-zA-Z][_a-zA-Z0-9]* {
669 struct _mesa_glsl_parse_state *state = yyextra;
670 if (state->es_shader && yyleng > 1024) {
671 _mesa_glsl_error(yylloc, state,
672 "Identifier `%s' exceeds 1024 characters",
673 yytext);
674 }
675 return classify_identifier(state, yytext, yyleng, yylval);
676 }
677
678 \. { struct _mesa_glsl_parse_state *state = yyextra;
679 state->is_field = true;
680 return DOT_TOK; }
681
682 . { return yytext[0]; }
683
684 %%
685
686 int
687 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name,
688 unsigned name_len, YYSTYPE *output)
689 {
690 /* We're not doing linear_strdup here, to avoid an implicit call on
691 * strlen() for the length of the string, as this is already found by flex
692 * and stored in yyleng
693 */
694 char *id = (char *) linear_alloc_child(state->linalloc, name_len + 1);
695 memcpy(id, name, name_len + 1);
696 output->identifier = id;
697
698 if (state->is_field) {
699 state->is_field = false;
700 return FIELD_SELECTION;
701 }
702 if (state->symbols->get_variable(name) || state->symbols->get_function(name))
703 return IDENTIFIER;
704 else if (state->symbols->get_type(name))
705 return TYPE_IDENTIFIER;
706 else
707 return NEW_IDENTIFIER;
708 }
709
710 void
711 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
712 {
713 yylex_init_extra(state, & state->scanner);
714 yy_scan_string(string, state->scanner);
715 }
716
717 void
718 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
719 {
720 yylex_destroy(state->scanner);
721 }