• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
31static 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
152static int
153literal_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
221DEC_INT		[1-9][0-9]*
222HEX_INT		0[xX][0-9a-fA-F]+
223OCT_INT		0[0-7]*
224INT		({DEC_INT}|{HEX_INT}|{OCT_INT})
225SPC		[ \t]*
226SPCP		[ \t]+
227HASH		^{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
334attribute	DEPRECATED_ES_KEYWORD(ATTRIBUTE);
335const		return CONST_TOK;
336bool		{ yylval->type = glsl_type::bool_type; return BASIC_TYPE_TOK; }
337float		{ yylval->type = glsl_type::float_type; return BASIC_TYPE_TOK; }
338int		{ yylval->type = glsl_type::int_type; return BASIC_TYPE_TOK; }
339uint		TYPE(130, 300, 130, 300, glsl_type::uint_type);
340
341break		return BREAK;
342continue	return CONTINUE;
343do		return DO;
344while		return WHILE;
345else		return ELSE;
346for		return FOR;
347if		return IF;
348discard		return DISCARD;
349return		return RETURN;
350
351bvec2		{ yylval->type = glsl_type::bvec2_type; return BASIC_TYPE_TOK; }
352bvec3		{ yylval->type = glsl_type::bvec3_type; return BASIC_TYPE_TOK; }
353bvec4		{ yylval->type = glsl_type::bvec4_type; return BASIC_TYPE_TOK; }
354ivec2		{ yylval->type = glsl_type::ivec2_type; return BASIC_TYPE_TOK; }
355ivec3		{ yylval->type = glsl_type::ivec3_type; return BASIC_TYPE_TOK; }
356ivec4		{ yylval->type = glsl_type::ivec4_type; return BASIC_TYPE_TOK; }
357uvec2		TYPE(130, 300, 130, 300, glsl_type::uvec2_type);
358uvec3		TYPE(130, 300, 130, 300, glsl_type::uvec3_type);
359uvec4		TYPE(130, 300, 130, 300, glsl_type::uvec4_type);
360vec2		{ yylval->type = glsl_type::vec2_type; return BASIC_TYPE_TOK; }
361vec3		{ yylval->type = glsl_type::vec3_type; return BASIC_TYPE_TOK; }
362vec4		{ yylval->type = glsl_type::vec4_type; return BASIC_TYPE_TOK; }
363mat2		{ yylval->type = glsl_type::mat2_type; return BASIC_TYPE_TOK; }
364mat3		{ yylval->type = glsl_type::mat3_type; return BASIC_TYPE_TOK; }
365mat4		{ yylval->type = glsl_type::mat4_type; return BASIC_TYPE_TOK; }
366mat2x2		TYPE(120, 300, 120, 300, glsl_type::mat2_type);
367mat2x3		TYPE(120, 300, 120, 300, glsl_type::mat2x3_type);
368mat2x4		TYPE(120, 300, 120, 300, glsl_type::mat2x4_type);
369mat3x2		TYPE(120, 300, 120, 300, glsl_type::mat3x2_type);
370mat3x3		TYPE(120, 300, 120, 300, glsl_type::mat3_type);
371mat3x4		TYPE(120, 300, 120, 300, glsl_type::mat3x4_type);
372mat4x2		TYPE(120, 300, 120, 300, glsl_type::mat4x2_type);
373mat4x3		TYPE(120, 300, 120, 300, glsl_type::mat4x3_type);
374mat4x4		TYPE(120, 300, 120, 300, glsl_type::mat4_type);
375
376in		return IN_TOK;
377out		return OUT_TOK;
378inout		return INOUT_TOK;
379uniform		return UNIFORM;
380buffer		KEYWORD_WITH_ALT(0, 0, 430, 310, yyextra->ARB_shader_storage_buffer_object_enable, BUFFER);
381varying		DEPRECATED_ES_KEYWORD(VARYING);
382centroid	KEYWORD(120, 300, 120, 300, CENTROID);
383invariant	KEYWORD(120, 100, 120, 100, INVARIANT);
384flat		KEYWORD(130, 100, 130, 300, FLAT);
385smooth		KEYWORD(130, 300, 130, 300, SMOOTH);
386noperspective	KEYWORD(130, 300, 130, 0, NOPERSPECTIVE);
387patch		KEYWORD_WITH_ALT(0, 300, 400, 320, yyextra->has_tessellation_shader(), PATCH);
388
389sampler1D	DEPRECATED_ES_TYPE(glsl_type::sampler1D_type);
390sampler2D	{ yylval->type = glsl_type::sampler2D_type; return BASIC_TYPE_TOK; }
391sampler3D	{ yylval->type = glsl_type::sampler3D_type; return BASIC_TYPE_TOK; }
392samplerCube	{ yylval->type = glsl_type::samplerCube_type; return BASIC_TYPE_TOK; }
393sampler1DArray	TYPE(130, 300, 130, 0, glsl_type::sampler1DArray_type);
394sampler2DArray	TYPE(130, 300, 130, 300, glsl_type::sampler2DArray_type);
395sampler1DShadow	DEPRECATED_ES_TYPE(glsl_type::sampler1DShadow_type);
396sampler2DShadow	{ yylval->type = glsl_type::sampler2DShadow_type; return BASIC_TYPE_TOK; }
397samplerCubeShadow	TYPE(130, 300, 130, 300, glsl_type::samplerCubeShadow_type);
398sampler1DArrayShadow	TYPE(130, 300, 130, 0, glsl_type::sampler1DArrayShadow_type);
399sampler2DArrayShadow	TYPE(130, 300, 130, 300, glsl_type::sampler2DArrayShadow_type);
400isampler1D		TYPE(130, 300, 130, 0, glsl_type::isampler1D_type);
401isampler2D		TYPE(130, 300, 130, 300, glsl_type::isampler2D_type);
402isampler3D		TYPE(130, 300, 130, 300, glsl_type::isampler3D_type);
403isamplerCube		TYPE(130, 300, 130, 300, glsl_type::isamplerCube_type);
404isampler1DArray		TYPE(130, 300, 130, 0, glsl_type::isampler1DArray_type);
405isampler2DArray		TYPE(130, 300, 130, 300, glsl_type::isampler2DArray_type);
406usampler1D		TYPE(130, 300, 130, 0, glsl_type::usampler1D_type);
407usampler2D		TYPE(130, 300, 130, 300, glsl_type::usampler2D_type);
408usampler3D		TYPE(130, 300, 130, 300, glsl_type::usampler3D_type);
409usamplerCube		TYPE(130, 300, 130, 300, glsl_type::usamplerCube_type);
410usampler1DArray		TYPE(130, 300, 130, 0, glsl_type::usampler1DArray_type);
411usampler2DArray		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 */
416sampler2DMS        TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::sampler2DMS_type);
417isampler2DMS       TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::isampler2DMS_type);
418usampler2DMS       TYPE_WITH_ALT(150, 300, 150, 310, yyextra->ARB_texture_multisample_enable, glsl_type::usampler2DMS_type);
419sampler2DMSArray   TYPE_WITH_ALT(150, 300, 150, 320, yyextra->ARB_texture_multisample_enable || yyextra->OES_texture_storage_multisample_2d_array_enable, glsl_type::sampler2DMSArray_type);
420isampler2DMSArray  TYPE_WITH_ALT(150, 300, 150, 320, yyextra->ARB_texture_multisample_enable || yyextra->OES_texture_storage_multisample_2d_array_enable, glsl_type::isampler2DMSArray_type);
421usampler2DMSArray  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 */
424samplerCubeArray   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);
425isamplerCubeArray 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);
426usamplerCubeArray 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);
427samplerCubeArrayShadow   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
429samplerExternalOES		{
430			  if (yyextra->OES_EGL_image_external_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 */
438precise		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 */
441image1D         TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image1D_type);
442image2D         TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2D_type);
443image3D         TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image3D_type);
444image2DRect     TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DRect_type);
445imageCube       TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::imageCube_type);
446imageBuffer     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);
447image1DArray    TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image1DArray_type);
448image2DArray    TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DArray_type);
449imageCubeArray  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);
450image2DMS       TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DMS_type);
451image2DMSArray  TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::image2DMSArray_type);
452iimage1D        TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage1D_type);
453iimage2D        TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2D_type);
454iimage3D        TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage3D_type);
455iimage2DRect    TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DRect_type);
456iimageCube      TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimageCube_type);
457iimageBuffer    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);
458iimage1DArray   TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage1DArray_type);
459iimage2DArray   TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DArray_type);
460iimageCubeArray 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);
461iimage2DMS      TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DMS_type);
462iimage2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::iimage2DMSArray_type);
463uimage1D        TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage1D_type);
464uimage2D        TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2D_type);
465uimage3D        TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage3D_type);
466uimage2DRect    TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DRect_type);
467uimageCube      TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimageCube_type);
468uimageBuffer    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);
469uimage1DArray   TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage1DArray_type);
470uimage2DArray   TYPE_WITH_ALT(130, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DArray_type);
471uimageCubeArray 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);
472uimage2DMS      TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DMS_type);
473uimage2DMSArray TYPE_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, glsl_type::uimage2DMSArray_type);
474image1DShadow           KEYWORD(130, 300, 0, 0, IMAGE1DSHADOW);
475image2DShadow           KEYWORD(130, 300, 0, 0, IMAGE2DSHADOW);
476image1DArrayShadow      KEYWORD(130, 300, 0, 0, IMAGE1DARRAYSHADOW);
477image2DArrayShadow      KEYWORD(130, 300, 0, 0, IMAGE2DARRAYSHADOW);
478
479coherent       KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, COHERENT);
480volatile       KEYWORD_WITH_ALT(110, 100, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, VOLATILE);
481restrict       KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, RESTRICT);
482readonly       KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, READONLY);
483writeonly      KEYWORD_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_image_load_store_enable || yyextra->ARB_shader_storage_buffer_object_enable, WRITEONLY);
484
485atomic_uint     TYPE_WITH_ALT(420, 300, 420, 310, yyextra->ARB_shader_atomic_counters_enable, glsl_type::atomic_uint_type);
486
487shared          KEYWORD_WITH_ALT(430, 310, 430, 310, yyextra->ARB_compute_shader_enable, SHARED);
488
489struct		return STRUCT;
490void		return VOID_TOK;
491
492layout		{
493		  if ((yyextra->is_version(140, 300))
494		      || yyextra->AMD_conservative_depth_enable
495		      || yyextra->ARB_conservative_depth_enable
496		      || yyextra->ARB_explicit_attrib_location_enable
497		      || yyextra->ARB_explicit_uniform_location_enable
498                      || yyextra->has_separate_shader_objects()
499		      || yyextra->ARB_uniform_buffer_object_enable
500		      || yyextra->ARB_fragment_coord_conventions_enable
501                      || yyextra->ARB_shading_language_420pack_enable
502                      || yyextra->ARB_compute_shader_enable
503                      || yyextra->ARB_tessellation_shader_enable) {
504		      return LAYOUT_TOK;
505		   } else {
506		      return classify_identifier(yyextra, yytext, yyleng, yylval);
507		   }
508		}
509
510\+\+		return INC_OP;
511--		return DEC_OP;
512\<=		return LE_OP;
513>=		return GE_OP;
514==		return EQ_OP;
515!=		return NE_OP;
516&&		return AND_OP;
517\|\|		return OR_OP;
518"^^"		return XOR_OP;
519"<<"		return LEFT_OP;
520">>"		return RIGHT_OP;
521
522\*=		return MUL_ASSIGN;
523\/=		return DIV_ASSIGN;
524\+=		return ADD_ASSIGN;
525\%=		return MOD_ASSIGN;
526\<\<=		return LEFT_ASSIGN;
527>>=		return RIGHT_ASSIGN;
528&=		return AND_ASSIGN;
529"^="		return XOR_ASSIGN;
530\|=		return OR_ASSIGN;
531-=		return SUB_ASSIGN;
532
533[1-9][0-9]*([uU]|[lL]|ul|UL)?	{
534			    return LITERAL_INTEGER(10);
535			}
5360[xX][0-9a-fA-F]+([uU]|[lL]|ul|UL)?	{
537			    return LITERAL_INTEGER(16);
538			}
5390[0-7]*([uU]|[lL]|ul|UL)?		{
540			    return LITERAL_INTEGER(8);
541			}
542
543[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]?	|
544\.[0-9]+([eE][+-]?[0-9]+)?[fF]?		|
545[0-9]+\.([eE][+-]?[0-9]+)?[fF]?		|
546[0-9]+[eE][+-]?[0-9]+[fF]?		{
547			    struct _mesa_glsl_parse_state *state = yyextra;
548			    char suffix = yytext[strlen(yytext) - 1];
549			    if (!state->is_version(120, 300) &&
550			        (suffix == 'f' || suffix == 'F')) {
551			        _mesa_glsl_warning(yylloc, state,
552			                           "Float suffixes are invalid in GLSL 1.10");
553			    }
554			    yylval->real = _mesa_strtof(yytext, NULL);
555			    return FLOATCONSTANT;
556			}
557
558[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?(lf|LF)	|
559\.[0-9]+([eE][+-]?[0-9]+)?(lf|LF)	|
560[0-9]+\.([eE][+-]?[0-9]+)?(lf|LF)	|
561[0-9]+[eE][+-]?[0-9]+(lf|LF)		{
562			    if (!yyextra->is_version(400, 0) &&
563			        !yyextra->ARB_gpu_shader_fp64_enable)
564			        return ERROR_TOK;
565			    yylval->dreal = _mesa_strtod(yytext, NULL);
566			    return DOUBLECONSTANT;
567			}
568
569true			{
570			    yylval->n = 1;
571			    return BOOLCONSTANT;
572			}
573false			{
574			    yylval->n = 0;
575			    return BOOLCONSTANT;
576			}
577
578
579    /* Reserved words in GLSL 1.10. */
580asm		KEYWORD(110, 100, 0, 0, ASM);
581class		KEYWORD(110, 100, 0, 0, CLASS);
582union		KEYWORD(110, 100, 0, 0, UNION);
583enum		KEYWORD(110, 100, 0, 0, ENUM);
584typedef		KEYWORD(110, 100, 0, 0, TYPEDEF);
585template	KEYWORD(110, 100, 0, 0, TEMPLATE);
586this		KEYWORD(110, 100, 0, 0, THIS);
587packed		KEYWORD_WITH_ALT(110, 100, 140, 300, yyextra->ARB_uniform_buffer_object_enable, PACKED_TOK);
588goto		KEYWORD(110, 100, 0, 0, GOTO);
589switch		KEYWORD(110, 100, 130, 300, SWITCH);
590default		KEYWORD(110, 100, 130, 300, DEFAULT);
591inline		KEYWORD(110, 100, 0, 0, INLINE_TOK);
592noinline	KEYWORD(110, 100, 0, 0, NOINLINE);
593public		KEYWORD(110, 100, 0, 0, PUBLIC_TOK);
594static		KEYWORD(110, 100, 0, 0, STATIC);
595extern		KEYWORD(110, 100, 0, 0, EXTERN);
596external	KEYWORD(110, 100, 0, 0, EXTERNAL);
597interface	KEYWORD(110, 100, 0, 0, INTERFACE);
598long		KEYWORD(110, 100, 0, 0, LONG_TOK);
599short		KEYWORD(110, 100, 0, 0, SHORT_TOK);
600double		TYPE_WITH_ALT(130, 300, 130, 300, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::double_type);
601half		KEYWORD(110, 100, 0, 0, HALF);
602fixed		KEYWORD(110, 100, 0, 0, FIXED_TOK);
603unsigned	KEYWORD(110, 100, 0, 0, UNSIGNED);
604input		KEYWORD(110, 100, 0, 0, INPUT_TOK);
605output		KEYWORD(110, 100, 0, 0, OUTPUT);
606hvec2		KEYWORD(110, 100, 0, 0, HVEC2);
607hvec3		KEYWORD(110, 100, 0, 0, HVEC3);
608hvec4		KEYWORD(110, 100, 0, 0, HVEC4);
609dvec2		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec2_type);
610dvec3		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec3_type);
611dvec4		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dvec4_type);
612dmat2		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2_type);
613dmat3		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3_type);
614dmat4		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4_type);
615dmat2x2		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2_type);
616dmat2x3		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2x3_type);
617dmat2x4		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat2x4_type);
618dmat3x2		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3x2_type);
619dmat3x3		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3_type);
620dmat3x4		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat3x4_type);
621dmat4x2		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4x2_type);
622dmat4x3		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4x3_type);
623dmat4x4		TYPE_WITH_ALT(110, 100, 400, 0, yyextra->ARB_gpu_shader_fp64_enable, glsl_type::dmat4_type);
624fvec2		KEYWORD(110, 100, 0, 0, FVEC2);
625fvec3		KEYWORD(110, 100, 0, 0, FVEC3);
626fvec4		KEYWORD(110, 100, 0, 0, FVEC4);
627sampler2DRect		DEPRECATED_ES_TYPE_WITH_ALT(yyextra->ARB_texture_rectangle_enable, glsl_type::sampler2DRect_type);
628sampler3DRect		KEYWORD(110, 100, 0, 0, SAMPLER3DRECT);
629sampler2DRectShadow	DEPRECATED_ES_TYPE_WITH_ALT(yyextra->ARB_texture_rectangle_enable, glsl_type::sampler2DRectShadow_type);
630sizeof		KEYWORD(110, 100, 0, 0, SIZEOF);
631cast		KEYWORD(110, 100, 0, 0, CAST);
632namespace	KEYWORD(110, 100, 0, 0, NAMESPACE);
633using		KEYWORD(110, 100, 0, 0, USING);
634
635    /* Additional reserved words in GLSL 1.20. */
636lowp		KEYWORD(120, 100, 130, 100, LOWP);
637mediump		KEYWORD(120, 100, 130, 100, MEDIUMP);
638highp		KEYWORD(120, 100, 130, 100, HIGHP);
639precision	KEYWORD(120, 100, 130, 100, PRECISION);
640
641    /* Additional reserved words in GLSL 1.30. */
642case		KEYWORD(130, 300, 130, 300, CASE);
643common		KEYWORD(130, 300, 0, 0, COMMON);
644partition	KEYWORD(130, 300, 0, 0, PARTITION);
645active		KEYWORD(130, 300, 0, 0, ACTIVE);
646superp		KEYWORD(130, 100, 0, 0, SUPERP);
647samplerBuffer	TYPE_WITH_ALT(130, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::samplerBuffer_type);
648filter		KEYWORD(130, 300, 0, 0, FILTER);
649row_major	KEYWORD_WITH_ALT(130, 0, 140, 0, yyextra->ARB_uniform_buffer_object_enable && !yyextra->es_shader, ROW_MAJOR);
650
651    /* Additional reserved words in GLSL 1.40 */
652isampler2DRect	TYPE(140, 300, 140, 0, glsl_type::isampler2DRect_type);
653usampler2DRect	TYPE(140, 300, 140, 0, glsl_type::usampler2DRect_type);
654isamplerBuffer	TYPE_WITH_ALT(140, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::isamplerBuffer_type);
655usamplerBuffer	TYPE_WITH_ALT(140, 300, 140, 320, yyextra->EXT_texture_buffer_enable || yyextra->OES_texture_buffer_enable, glsl_type::usamplerBuffer_type);
656
657    /* Additional reserved words in GLSL ES 3.00 */
658resource	KEYWORD(420, 300, 0, 0, RESOURCE);
659sample		KEYWORD_WITH_ALT(400, 300, 400, 320, yyextra->ARB_gpu_shader5_enable || yyextra->OES_shader_multisample_interpolation_enable, SAMPLE);
660subroutine	KEYWORD_WITH_ALT(400, 300, 400, 0, yyextra->ARB_shader_subroutine_enable, SUBROUTINE);
661
662    /* Additional words for ARB_gpu_shader_int64 */
663int64_t		TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::int64_t_type);
664i64vec2		TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec2_type);
665i64vec3		TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec3_type);
666i64vec4		TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::i64vec4_type);
667
668uint64_t	TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::uint64_t_type);
669u64vec2		TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec2_type);
670u64vec3		TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec3_type);
671u64vec4		TYPE_WITH_ALT(0, 0, 0, 0, yyextra->ARB_gpu_shader_int64_enable, glsl_type::u64vec4_type);
672
673[_a-zA-Z][_a-zA-Z0-9]*	{
674			    struct _mesa_glsl_parse_state *state = yyextra;
675			    if (state->es_shader && yyleng > 1024) {
676			       _mesa_glsl_error(yylloc, state,
677			                        "Identifier `%s' exceeds 1024 characters",
678			                        yytext);
679			    }
680			    return classify_identifier(state, yytext, yyleng, yylval);
681			}
682
683\.			{ struct _mesa_glsl_parse_state *state = yyextra;
684			  state->is_field = true;
685			  return DOT_TOK; }
686
687.			{ return yytext[0]; }
688
689%%
690
691int
692classify_identifier(struct _mesa_glsl_parse_state *state, const char *name,
693                    unsigned name_len, YYSTYPE *output)
694{
695   /* We're not doing linear_strdup here, to avoid an implicit call on
696    * strlen() for the length of the string, as this is already found by flex
697    * and stored in yyleng
698    */
699   char *id = (char *) linear_alloc_child(state->linalloc, name_len + 1);
700   memcpy(id, name, name_len + 1);
701   output->identifier = id;
702
703   if (state->is_field) {
704      state->is_field = false;
705      return FIELD_SELECTION;
706   }
707   if (state->symbols->get_variable(name) || state->symbols->get_function(name))
708      return IDENTIFIER;
709   else if (state->symbols->get_type(name))
710      return TYPE_IDENTIFIER;
711   else
712      return NEW_IDENTIFIER;
713}
714
715void
716_mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
717{
718   yylex_init_extra(state, & state->scanner);
719   yy_scan_string(string, state->scanner);
720}
721
722void
723_mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
724{
725   yylex_destroy(state->scanner);
726}
727