Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glsl_parser.ypp 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "ast.h"
  29. #include "glsl_parser_extras.h"
  30. #include "glsl_types.h"
  31. #define YYLEX_PARAM state->scanner
  32. %}
  33. %pure-parser
  34. %locations
  35. %error-verbose
  36. %lex-param {void *scanner}
  37. %parse-param {struct _mesa_glsl_parse_state *state}
  38. %name-prefix "_mesa_glsl_"
  39. %union {
  40. int n;
  41. float real;
  42. char *identifier;
  43. union {
  44. struct ast_type_qualifier q;
  45. unsigned i;
  46. } type_qualifier;
  47. struct ast_node *node;
  48. struct ast_type_specifier *type_specifier;
  49. struct ast_fully_specified_type *fully_specified_type;
  50. struct ast_function *function;
  51. struct ast_parameter_declarator *parameter_declarator;
  52. struct ast_function_definition *function_definition;
  53. struct ast_compound_statement *compound_statement;
  54. struct ast_expression *expression;
  55. struct ast_declarator_list *declarator_list;
  56. struct ast_struct_specifier *struct_specifier;
  57. struct ast_declaration *declaration;
  58. struct {
  59. struct ast_node *cond;
  60. struct ast_expression *rest;
  61. } for_rest_statement;
  62. }
  63. %token ATTRIBUTE CONST BOOL FLOAT INT UINT
  64. %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
  65. %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
  66. %token MAT2 MAT3 MAT4 CENTROID IN OUT INOUT UNIFORM VARYING
  67. %token NOPERSPECTIVE FLAT SMOOTH
  68. %token MAT2X2 MAT2X3 MAT2X4
  69. %token MAT3X2 MAT3X3 MAT3X4
  70. %token MAT4X2 MAT4X3 MAT4X4
  71. %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
  72. %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
  73. %token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
  74. %token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D
  75. %token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY
  76. %token STRUCT VOID WHILE
  77. %token <identifier> IDENTIFIER TYPE_NAME
  78. %token <real> FLOATCONSTANT
  79. %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
  80. %token <identifier> FIELD_SELECTION
  81. %token LEFT_OP RIGHT_OP
  82. %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
  83. %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
  84. %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
  85. %token SUB_ASSIGN
  86. %token INVARIANT
  87. %token HIGH_PRECISION MEDIUM_PRECISION LOW_PRECISION PRECISION
  88. %token VERSION EXTENSION LINE PRAGMA COLON EOL INTERFACE OUTPUT
  89. /* Reserved words that are not actually used in the grammar.
  90. */
  91. %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED GOTO
  92. %token INLINE NOINLINE VOLATILE PUBLIC STATIC EXTERN EXTERNAL
  93. %token LONG SHORT DOUBLE HALF FIXED UNSIGNED INPUT OUPTUT
  94. %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
  95. %token SAMPLER2DRECT SAMPLER3DRECT SAMPLER2DRECTSHADOW
  96. %token SIZEOF CAST NAMESPACE USING LOWP MEDIUMP HIGHP
  97. %type <identifier> variable_identifier
  98. %type <node> statement
  99. %type <node> statement_list
  100. %type <node> simple_statement
  101. %type <node> statement_matched
  102. %type <node> statement_unmatched
  103. %type <n> precision_qualifier
  104. %type <type_qualifier> type_qualifier
  105. %type <type_qualifier> storage_qualifier
  106. %type <type_qualifier> interpolation_qualifier
  107. %type <type_specifier> type_specifier
  108. %type <type_specifier> type_specifier_no_prec
  109. %type <type_specifier> type_specifier_nonarray
  110. %type <n> basic_type_specifier_nonarray
  111. %type <fully_specified_type> fully_specified_type
  112. %type <function> function_prototype
  113. %type <function> function_header
  114. %type <function> function_header_with_parameters
  115. %type <function> function_declarator
  116. %type <parameter_declarator> parameter_declarator
  117. %type <parameter_declarator> parameter_declaration
  118. %type <type_qualifier> parameter_qualifier
  119. %type <type_qualifier> parameter_type_qualifier
  120. %type <type_specifier> parameter_type_specifier
  121. %type <function_definition> function_definition
  122. %type <compound_statement> compound_statement_no_new_scope
  123. %type <compound_statement> compound_statement
  124. %type <node> statement_no_new_scope
  125. %type <node> expression_statement
  126. %type <expression> expression
  127. %type <expression> primary_expression
  128. %type <expression> assignment_expression
  129. %type <expression> conditional_expression
  130. %type <expression> logical_or_expression
  131. %type <expression> logical_xor_expression
  132. %type <expression> logical_and_expression
  133. %type <expression> inclusive_or_expression
  134. %type <expression> exclusive_or_expression
  135. %type <expression> and_expression
  136. %type <expression> equality_expression
  137. %type <expression> relational_expression
  138. %type <expression> shift_expression
  139. %type <expression> additive_expression
  140. %type <expression> multiplicative_expression
  141. %type <expression> unary_expression
  142. %type <expression> constant_expression
  143. %type <expression> integer_expression
  144. %type <expression> postfix_expression
  145. %type <expression> function_call_header_with_parameters
  146. %type <expression> function_call_header_no_parameters
  147. %type <expression> function_call_header
  148. %type <expression> function_call_generic
  149. %type <expression> function_call_or_method
  150. %type <expression> function_call
  151. %type <n> assignment_operator
  152. %type <n> unary_operator
  153. %type <expression> function_identifier
  154. %type <node> external_declaration
  155. %type <declarator_list> init_declarator_list
  156. %type <declarator_list> single_declaration
  157. %type <expression> initializer
  158. %type <node> declaration
  159. %type <node> declaration_statement
  160. %type <node> jump_statement
  161. %type <struct_specifier> struct_specifier
  162. %type <node> struct_declaration_list
  163. %type <declarator_list> struct_declaration
  164. %type <declaration> struct_declarator
  165. %type <declaration> struct_declarator_list
  166. %type <node> selection_statement_matched
  167. %type <node> selection_statement_unmatched
  168. %type <node> iteration_statement
  169. %type <node> condition
  170. %type <node> conditionopt
  171. %type <node> for_init_statement
  172. %type <for_rest_statement> for_rest_statement
  173. %%
  174. translation_unit:
  175. version_statement
  176. {
  177. _mesa_glsl_initialize_types(state);
  178. }
  179. external_declaration_list
  180. |
  181. {
  182. state->language_version = 110;
  183. _mesa_glsl_initialize_types(state);
  184. }
  185. external_declaration_list
  186. ;
  187. version_statement:
  188. VERSION INTCONSTANT EOL
  189. {
  190. switch ($2) {
  191. case 110:
  192. case 120:
  193. case 130:
  194. /* FINISHME: Check against implementation support versions. */
  195. state->language_version = $2;
  196. break;
  197. default:
  198. _mesa_glsl_error(& @2, state, "Shading language version"
  199. "%u is not supported\n", $2);
  200. break;
  201. }
  202. }
  203. ;
  204. external_declaration_list:
  205. external_declaration
  206. {
  207. insert_at_tail(& state->translation_unit,
  208. (struct simple_node *) $1);
  209. }
  210. | external_declaration_list external_declaration
  211. {
  212. insert_at_tail(& state->translation_unit,
  213. (struct simple_node *) $2);
  214. }
  215. ;
  216. variable_identifier:
  217. IDENTIFIER
  218. ;
  219. primary_expression:
  220. variable_identifier
  221. {
  222. $$ = new ast_expression(ast_identifier, NULL, NULL, NULL);
  223. $$->set_location(yylloc);
  224. $$->primary_expression.identifier = $1;
  225. }
  226. | INTCONSTANT
  227. {
  228. $$ = new ast_expression(ast_int_constant, NULL, NULL, NULL);
  229. $$->set_location(yylloc);
  230. $$->primary_expression.int_constant = $1;
  231. }
  232. | UINTCONSTANT
  233. {
  234. $$ = new ast_expression(ast_uint_constant, NULL, NULL, NULL);
  235. $$->set_location(yylloc);
  236. $$->primary_expression.uint_constant = $1;
  237. }
  238. | FLOATCONSTANT
  239. {
  240. $$ = new ast_expression(ast_float_constant, NULL, NULL, NULL);
  241. $$->set_location(yylloc);
  242. $$->primary_expression.float_constant = $1;
  243. }
  244. | BOOLCONSTANT
  245. {
  246. $$ = new ast_expression(ast_bool_constant, NULL, NULL, NULL);
  247. $$->set_location(yylloc);
  248. $$->primary_expression.bool_constant = $1;
  249. }
  250. | '(' expression ')'
  251. {
  252. $$ = $2;
  253. }
  254. ;
  255. postfix_expression:
  256. primary_expression
  257. | postfix_expression '[' integer_expression ']'
  258. {
  259. $$ = new ast_expression(ast_array_index, $1, $3, NULL);
  260. $$->set_location(yylloc);
  261. }
  262. | function_call
  263. {
  264. $$ = $1;
  265. }
  266. | postfix_expression '.' IDENTIFIER
  267. {
  268. $$ = new ast_expression(ast_field_selection, $1, NULL, NULL);
  269. $$->set_location(yylloc);
  270. $$->primary_expression.identifier = $3;
  271. }
  272. | postfix_expression INC_OP
  273. {
  274. $$ = new ast_expression(ast_post_inc, $1, NULL, NULL);
  275. $$->set_location(yylloc);
  276. }
  277. | postfix_expression DEC_OP
  278. {
  279. $$ = new ast_expression(ast_post_dec, $1, NULL, NULL);
  280. $$->set_location(yylloc);
  281. }
  282. ;
  283. integer_expression:
  284. expression
  285. ;
  286. function_call:
  287. function_call_or_method
  288. ;
  289. function_call_or_method:
  290. function_call_generic
  291. | postfix_expression '.' function_call_generic
  292. {
  293. $$ = new ast_expression(ast_field_selection, $1, $3, NULL);
  294. $$->set_location(yylloc);
  295. }
  296. ;
  297. function_call_generic:
  298. function_call_header_with_parameters ')'
  299. | function_call_header_no_parameters ')'
  300. ;
  301. function_call_header_no_parameters:
  302. function_call_header VOID
  303. | function_call_header
  304. ;
  305. function_call_header_with_parameters:
  306. function_call_header assignment_expression
  307. {
  308. $$ = $1;
  309. $$->set_location(yylloc);
  310. $$->subexpressions[1] = $2;
  311. }
  312. | function_call_header_with_parameters ',' assignment_expression
  313. {
  314. $$ = $1;
  315. $$->set_location(yylloc);
  316. insert_at_tail((struct simple_node *) $$->subexpressions[1],
  317. (struct simple_node *) $3);
  318. }
  319. ;
  320. // Grammar Note: Constructors look like functions, but lexical
  321. // analysis recognized most of them as keywords. They are now
  322. // recognized through "type_specifier".
  323. function_call_header:
  324. function_identifier '('
  325. ;
  326. function_identifier:
  327. type_specifier
  328. {
  329. $$ = new ast_function_expression($1);
  330. $$->set_location(yylloc);
  331. }
  332. | IDENTIFIER
  333. {
  334. ast_expression *callee = new ast_expression($1);
  335. $$ = new ast_function_expression(callee);
  336. $$->set_location(yylloc);
  337. }
  338. | FIELD_SELECTION
  339. {
  340. ast_expression *callee = new ast_expression($1);
  341. $$ = new ast_function_expression(callee);
  342. $$->set_location(yylloc);
  343. }
  344. ;
  345. // Grammar Note: No traditional style type casts.
  346. unary_expression:
  347. postfix_expression
  348. | INC_OP unary_expression
  349. {
  350. $$ = new ast_expression(ast_pre_inc, $2, NULL, NULL);
  351. $$->set_location(yylloc);
  352. }
  353. | DEC_OP unary_expression
  354. {
  355. $$ = new ast_expression(ast_pre_dec, $2, NULL, NULL);
  356. $$->set_location(yylloc);
  357. }
  358. | unary_operator unary_expression
  359. {
  360. $$ = new ast_expression($1, $2, NULL, NULL);
  361. $$->set_location(yylloc);
  362. }
  363. ;
  364. // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
  365. unary_operator:
  366. '+' { $$ = ast_plus; }
  367. | '-' { $$ = ast_neg; }
  368. | '!' { $$ = ast_logic_not; }
  369. | '~' { $$ = ast_bit_not; }
  370. ;
  371. multiplicative_expression:
  372. unary_expression
  373. | multiplicative_expression '*' unary_expression
  374. {
  375. $$ = new ast_expression_bin(ast_mul, $1, $3);
  376. $$->set_location(yylloc);
  377. }
  378. | multiplicative_expression '/' unary_expression
  379. {
  380. $$ = new ast_expression_bin(ast_div, $1, $3);
  381. $$->set_location(yylloc);
  382. }
  383. | multiplicative_expression '%' unary_expression
  384. {
  385. $$ = new ast_expression_bin(ast_mod, $1, $3);
  386. $$->set_location(yylloc);
  387. }
  388. ;
  389. additive_expression:
  390. multiplicative_expression
  391. | additive_expression '+' multiplicative_expression
  392. {
  393. $$ = new ast_expression_bin(ast_add, $1, $3);
  394. $$->set_location(yylloc);
  395. }
  396. | additive_expression '-' multiplicative_expression
  397. {
  398. $$ = new ast_expression_bin(ast_sub, $1, $3);
  399. $$->set_location(yylloc);
  400. }
  401. ;
  402. shift_expression:
  403. additive_expression
  404. | shift_expression LEFT_OP additive_expression
  405. {
  406. $$ = new ast_expression_bin(ast_lshift, $1, $3);
  407. $$->set_location(yylloc);
  408. }
  409. | shift_expression RIGHT_OP additive_expression
  410. {
  411. $$ = new ast_expression_bin(ast_rshift, $1, $3);
  412. $$->set_location(yylloc);
  413. }
  414. ;
  415. relational_expression:
  416. shift_expression
  417. | relational_expression '<' shift_expression
  418. {
  419. $$ = new ast_expression_bin(ast_less, $1, $3);
  420. $$->set_location(yylloc);
  421. }
  422. | relational_expression '>' shift_expression
  423. {
  424. $$ = new ast_expression_bin(ast_greater, $1, $3);
  425. $$->set_location(yylloc);
  426. }
  427. | relational_expression LE_OP shift_expression
  428. {
  429. $$ = new ast_expression_bin(ast_lequal, $1, $3);
  430. $$->set_location(yylloc);
  431. }
  432. | relational_expression GE_OP shift_expression
  433. {
  434. $$ = new ast_expression_bin(ast_gequal, $1, $3);
  435. $$->set_location(yylloc);
  436. }
  437. ;
  438. equality_expression:
  439. relational_expression
  440. | equality_expression EQ_OP relational_expression
  441. {
  442. $$ = new ast_expression_bin(ast_equal, $1, $3);
  443. $$->set_location(yylloc);
  444. }
  445. | equality_expression NE_OP relational_expression
  446. {
  447. $$ = new ast_expression_bin(ast_nequal, $1, $3);
  448. $$->set_location(yylloc);
  449. }
  450. ;
  451. and_expression:
  452. equality_expression
  453. | and_expression '&' equality_expression
  454. {
  455. $$ = new ast_expression_bin(ast_bit_or, $1, $3);
  456. $$->set_location(yylloc);
  457. }
  458. ;
  459. exclusive_or_expression:
  460. and_expression
  461. | exclusive_or_expression '^' and_expression
  462. {
  463. $$ = new ast_expression_bin(ast_bit_xor, $1, $3);
  464. $$->set_location(yylloc);
  465. }
  466. ;
  467. inclusive_or_expression:
  468. exclusive_or_expression
  469. | inclusive_or_expression '|' exclusive_or_expression
  470. {
  471. $$ = new ast_expression_bin(ast_bit_or, $1, $3);
  472. $$->set_location(yylloc);
  473. }
  474. ;
  475. logical_and_expression:
  476. inclusive_or_expression
  477. | logical_and_expression AND_OP inclusive_or_expression
  478. {
  479. $$ = new ast_expression_bin(ast_logic_and, $1, $3);
  480. $$->set_location(yylloc);
  481. }
  482. ;
  483. logical_xor_expression:
  484. logical_and_expression
  485. | logical_xor_expression XOR_OP logical_and_expression
  486. {
  487. $$ = new ast_expression_bin(ast_logic_xor, $1, $3);
  488. $$->set_location(yylloc);
  489. }
  490. ;
  491. logical_or_expression:
  492. logical_xor_expression
  493. | logical_or_expression OR_OP logical_xor_expression
  494. {
  495. $$ = new ast_expression_bin(ast_logic_or, $1, $3);
  496. $$->set_location(yylloc);
  497. }
  498. ;
  499. conditional_expression:
  500. logical_or_expression
  501. | logical_or_expression '?' expression ':' assignment_expression
  502. {
  503. $$ = new ast_expression(ast_conditional, $1, $3, $5);
  504. $$->set_location(yylloc);
  505. }
  506. ;
  507. assignment_expression:
  508. conditional_expression
  509. | unary_expression assignment_operator assignment_expression
  510. {
  511. $$ = new ast_expression($2, $1, $3, NULL);
  512. $$->set_location(yylloc);
  513. }
  514. ;
  515. assignment_operator:
  516. '=' { $$ = ast_assign; }
  517. | MUL_ASSIGN { $$ = ast_mul_assign; }
  518. | DIV_ASSIGN { $$ = ast_div_assign; }
  519. | MOD_ASSIGN { $$ = ast_mod_assign; }
  520. | ADD_ASSIGN { $$ = ast_add_assign; }
  521. | SUB_ASSIGN { $$ = ast_sub_assign; }
  522. | LEFT_ASSIGN { $$ = ast_ls_assign; }
  523. | RIGHT_ASSIGN { $$ = ast_rs_assign; }
  524. | AND_ASSIGN { $$ = ast_and_assign; }
  525. | XOR_ASSIGN { $$ = ast_xor_assign; }
  526. | OR_ASSIGN { $$ = ast_or_assign; }
  527. ;
  528. expression:
  529. assignment_expression
  530. {
  531. $$ = $1;
  532. }
  533. | expression ',' assignment_expression
  534. {
  535. if ($1->oper != ast_sequence) {
  536. $$ = new ast_expression(ast_sequence, NULL, NULL, NULL);
  537. $$->set_location(yylloc);
  538. insert_at_tail(& $$->expressions, $1);
  539. } else {
  540. $$ = $1;
  541. }
  542. insert_at_tail(& $$->expressions, $3);
  543. }
  544. ;
  545. constant_expression:
  546. conditional_expression
  547. ;
  548. declaration:
  549. function_prototype ';'
  550. {
  551. $$ = $1;
  552. }
  553. | init_declarator_list ';'
  554. {
  555. $$ = $1;
  556. }
  557. | PRECISION precision_qualifier type_specifier_no_prec ';'
  558. {
  559. $$ = NULL; /* FINISHME */
  560. }
  561. ;
  562. function_prototype:
  563. function_declarator ')'
  564. ;
  565. function_declarator:
  566. function_header
  567. | function_header_with_parameters
  568. ;
  569. function_header_with_parameters:
  570. function_header parameter_declaration
  571. {
  572. $$ = $1;
  573. insert_at_tail(& $$->parameters,
  574. (struct simple_node *) $2);
  575. }
  576. | function_header_with_parameters ',' parameter_declaration
  577. {
  578. $$ = $1;
  579. insert_at_tail(& $$->parameters,
  580. (struct simple_node *) $3);
  581. }
  582. ;
  583. function_header:
  584. fully_specified_type IDENTIFIER '('
  585. {
  586. $$ = new ast_function();
  587. $$->set_location(yylloc);
  588. $$->return_type = $1;
  589. $$->identifier = $2;
  590. }
  591. ;
  592. parameter_declarator:
  593. type_specifier IDENTIFIER
  594. {
  595. $$ = new ast_parameter_declarator();
  596. $$->set_location(yylloc);
  597. $$->type = new ast_fully_specified_type();
  598. $$->type->set_location(yylloc);
  599. $$->type->specifier = $1;
  600. $$->identifier = $2;
  601. }
  602. | type_specifier IDENTIFIER '[' constant_expression ']'
  603. {
  604. $$ = new ast_parameter_declarator();
  605. $$->set_location(yylloc);
  606. $$->type = new ast_fully_specified_type();
  607. $$->type->set_location(yylloc);
  608. $$->type->specifier = $1;
  609. $$->identifier = $2;
  610. $$->is_array = true;
  611. $$->array_size = $4;
  612. }
  613. ;
  614. parameter_declaration:
  615. parameter_type_qualifier parameter_qualifier parameter_declarator
  616. {
  617. $1.i |= $2.i;
  618. $$ = $3;
  619. $$->type->qualifier = $1.q;
  620. }
  621. | parameter_qualifier parameter_declarator
  622. {
  623. $$ = $2;
  624. $$->type->qualifier = $1.q;
  625. }
  626. | parameter_type_qualifier parameter_qualifier parameter_type_specifier
  627. {
  628. $1.i |= $2.i;
  629. $$ = new ast_parameter_declarator();
  630. $$->set_location(yylloc);
  631. $$->type = new ast_fully_specified_type();
  632. $$->type->qualifier = $1.q;
  633. $$->type->specifier = $3;
  634. }
  635. | parameter_qualifier parameter_type_specifier
  636. {
  637. $$ = new ast_parameter_declarator();
  638. $$->set_location(yylloc);
  639. $$->type = new ast_fully_specified_type();
  640. $$->type->qualifier = $1.q;
  641. $$->type->specifier = $2;
  642. }
  643. ;
  644. parameter_qualifier:
  645. /* empty */ { $$.i = 0; }
  646. | IN { $$.i = 0; $$.q.in = 1; }
  647. | OUT { $$.i = 0; $$.q.out = 1; }
  648. | INOUT { $$.i = 0; $$.q.in = 1; $$.q.out = 1; }
  649. ;
  650. parameter_type_specifier:
  651. type_specifier
  652. ;
  653. init_declarator_list:
  654. single_declaration
  655. | init_declarator_list ',' IDENTIFIER
  656. {
  657. ast_declaration *decl = new ast_declaration($3, false, NULL, NULL);
  658. decl->set_location(yylloc);
  659. $$ = $1;
  660. insert_at_tail(& $$->declarations,
  661. (struct simple_node *) decl);
  662. }
  663. | init_declarator_list ',' IDENTIFIER '[' ']'
  664. {
  665. ast_declaration *decl = new ast_declaration($3, true, NULL, NULL);
  666. decl->set_location(yylloc);
  667. $$ = $1;
  668. insert_at_tail(& $$->declarations,
  669. (struct simple_node *) decl);
  670. }
  671. | init_declarator_list ',' IDENTIFIER '[' constant_expression ']'
  672. {
  673. ast_declaration *decl = new ast_declaration($3, true, $5, NULL);
  674. decl->set_location(yylloc);
  675. $$ = $1;
  676. insert_at_tail(& $$->declarations,
  677. (struct simple_node *) decl);
  678. }
  679. | init_declarator_list ',' IDENTIFIER '[' ']' '=' initializer
  680. {
  681. ast_declaration *decl = new ast_declaration($3, true, NULL, $7);
  682. decl->set_location(yylloc);
  683. $$ = $1;
  684. insert_at_tail(& $$->declarations,
  685. (struct simple_node *) decl);
  686. }
  687. | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' '=' initializer
  688. {
  689. ast_declaration *decl = new ast_declaration($3, true, $5, $8);
  690. decl->set_location(yylloc);
  691. $$ = $1;
  692. insert_at_tail(& $$->declarations,
  693. (struct simple_node *) decl);
  694. }
  695. | init_declarator_list ',' IDENTIFIER '=' initializer
  696. {
  697. ast_declaration *decl = new ast_declaration($3, false, NULL, $5);
  698. decl->set_location(yylloc);
  699. $$ = $1;
  700. insert_at_tail(& $$->declarations,
  701. (struct simple_node *) decl);
  702. }
  703. ;
  704. // Grammar Note: No 'enum', or 'typedef'.
  705. single_declaration:
  706. fully_specified_type
  707. {
  708. $$ = new ast_declarator_list($1);
  709. $$->set_location(yylloc);
  710. }
  711. | fully_specified_type IDENTIFIER
  712. {
  713. ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
  714. $$ = new ast_declarator_list($1);
  715. $$->set_location(yylloc);
  716. insert_at_tail(& $$->declarations,
  717. (struct simple_node *) decl);
  718. }
  719. | fully_specified_type IDENTIFIER '[' ']'
  720. {
  721. ast_declaration *decl = new ast_declaration($2, true, NULL, NULL);
  722. $$ = new ast_declarator_list($1);
  723. $$->set_location(yylloc);
  724. insert_at_tail(& $$->declarations,
  725. (struct simple_node *) decl);
  726. }
  727. | fully_specified_type IDENTIFIER '[' constant_expression ']'
  728. {
  729. ast_declaration *decl = new ast_declaration($2, true, $4, NULL);
  730. $$ = new ast_declarator_list($1);
  731. $$->set_location(yylloc);
  732. insert_at_tail(& $$->declarations,
  733. (struct simple_node *) decl);
  734. }
  735. | fully_specified_type IDENTIFIER '[' ']' '=' initializer
  736. {
  737. ast_declaration *decl = new ast_declaration($2, true, NULL, $6);
  738. $$ = new ast_declarator_list($1);
  739. $$->set_location(yylloc);
  740. insert_at_tail(& $$->declarations,
  741. (struct simple_node *) decl);
  742. }
  743. | fully_specified_type IDENTIFIER '[' constant_expression ']' '=' initializer
  744. {
  745. ast_declaration *decl = new ast_declaration($2, true, $4, $7);
  746. $$ = new ast_declarator_list($1);
  747. $$->set_location(yylloc);
  748. insert_at_tail(& $$->declarations,
  749. (struct simple_node *) decl);
  750. }
  751. | fully_specified_type IDENTIFIER '=' initializer
  752. {
  753. ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
  754. $$ = new ast_declarator_list($1);
  755. $$->set_location(yylloc);
  756. insert_at_tail(& $$->declarations,
  757. (struct simple_node *) decl);
  758. }
  759. | INVARIANT IDENTIFIER // Vertex only.
  760. {
  761. ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
  762. $$ = new ast_declarator_list(NULL);
  763. $$->set_location(yylloc);
  764. $$->invariant = true;
  765. insert_at_tail(& $$->declarations,
  766. (struct simple_node *) decl);
  767. }
  768. ;
  769. fully_specified_type:
  770. type_specifier
  771. {
  772. $$ = new ast_fully_specified_type();
  773. $$->set_location(yylloc);
  774. $$->specifier = $1;
  775. }
  776. | type_qualifier type_specifier
  777. {
  778. $$ = new ast_fully_specified_type();
  779. $$->set_location(yylloc);
  780. $$->qualifier = $1.q;
  781. $$->specifier = $2;
  782. }
  783. ;
  784. interpolation_qualifier:
  785. SMOOTH { $$.i = 0; $$.q.smooth = 1; }
  786. | FLAT { $$.i = 0; $$.q.flat = 1; }
  787. | NOPERSPECTIVE { $$.i = 0; $$.q.noperspective = 1; }
  788. ;
  789. parameter_type_qualifier:
  790. CONST { $$.i = 0; $$.q.constant = 1; }
  791. ;
  792. type_qualifier:
  793. storage_qualifier
  794. | interpolation_qualifier type_qualifier
  795. {
  796. $$.i = $1.i | $2.i;
  797. }
  798. | INVARIANT type_qualifier
  799. {
  800. $$ = $2;
  801. $$.q.invariant = 1;
  802. }
  803. ;
  804. storage_qualifier:
  805. CONST { $$.i = 0; $$.q.constant = 1; }
  806. | ATTRIBUTE { $$.i = 0; $$.q.attribute = 1; }
  807. | VARYING { $$.i = 0; $$.q.varying = 1; }
  808. | CENTROID VARYING { $$.i = 0; $$.q.centroid = 1; $$.q.varying = 1; }
  809. | IN { $$.i = 0; $$.q.in = 1; }
  810. | OUT { $$.i = 0; $$.q.out = 1; }
  811. | CENTROID IN { $$.i = 0; $$.q.centroid = 1; $$.q.in = 1; }
  812. | CENTROID OUT { $$.i = 0; $$.q.centroid = 1; $$.q.out = 1; }
  813. | UNIFORM { $$.i = 0; $$.q.uniform = 1; }
  814. ;
  815. type_specifier:
  816. type_specifier_no_prec
  817. | precision_qualifier type_specifier_no_prec
  818. {
  819. $$ = $2;
  820. $$->precision = $1;
  821. }
  822. ;
  823. type_specifier_no_prec:
  824. type_specifier_nonarray
  825. | type_specifier_nonarray '[' ']'
  826. {
  827. $$ = $1;
  828. $$->is_array = true;
  829. $$->array_size = NULL;
  830. }
  831. | type_specifier_nonarray '[' constant_expression ']'
  832. {
  833. $$ = $1;
  834. $$->is_array = true;
  835. $$->array_size = $3;
  836. }
  837. ;
  838. type_specifier_nonarray:
  839. basic_type_specifier_nonarray
  840. {
  841. $$ = new ast_type_specifier($1);
  842. $$->set_location(yylloc);
  843. }
  844. | struct_specifier
  845. {
  846. $$ = new ast_type_specifier($1);
  847. $$->set_location(yylloc);
  848. }
  849. | TYPE_NAME
  850. {
  851. $$ = new ast_type_specifier($1);
  852. $$->set_location(yylloc);
  853. }
  854. ;
  855. basic_type_specifier_nonarray:
  856. VOID { $$ = ast_void; }
  857. | FLOAT { $$ = ast_float; }
  858. | INT { $$ = ast_int; }
  859. | UINT { $$ = ast_uint; }
  860. | BOOL { $$ = ast_bool; }
  861. | VEC2 { $$ = ast_vec2; }
  862. | VEC3 { $$ = ast_vec3; }
  863. | VEC4 { $$ = ast_vec4; }
  864. | BVEC2 { $$ = ast_bvec2; }
  865. | BVEC3 { $$ = ast_bvec3; }
  866. | BVEC4 { $$ = ast_bvec4; }
  867. | IVEC2 { $$ = ast_ivec2; }
  868. | IVEC3 { $$ = ast_ivec3; }
  869. | IVEC4 { $$ = ast_ivec4; }
  870. | UVEC2 { $$ = ast_uvec2; }
  871. | UVEC3 { $$ = ast_uvec3; }
  872. | UVEC4 { $$ = ast_uvec4; }
  873. | MAT2 { $$ = ast_mat2; }
  874. | MAT3 { $$ = ast_mat3; }
  875. | MAT4 { $$ = ast_mat4; }
  876. | MAT2X2 { $$ = ast_mat2; }
  877. | MAT2X3 { $$ = ast_mat2x3; }
  878. | MAT2X4 { $$ = ast_mat2x4; }
  879. | MAT3X2 { $$ = ast_mat3x2; }
  880. | MAT3X3 { $$ = ast_mat3; }
  881. | MAT3X4 { $$ = ast_mat3x4; }
  882. | MAT4X2 { $$ = ast_mat4x2; }
  883. | MAT4X3 { $$ = ast_mat4x3; }
  884. | MAT4X4 { $$ = ast_mat4; }
  885. | SAMPLER1D { $$ = ast_sampler1d; }
  886. | SAMPLER2D { $$ = ast_sampler2d; }
  887. | SAMPLER2DRECT { $$ = ast_sampler2drect; }
  888. | SAMPLER3D { $$ = ast_sampler3d; }
  889. | SAMPLERCUBE { $$ = ast_samplercube; }
  890. | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
  891. | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
  892. | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
  893. | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
  894. | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
  895. | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
  896. | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
  897. | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
  898. | ISAMPLER1D { $$ = ast_isampler1d; }
  899. | ISAMPLER2D { $$ = ast_isampler2d; }
  900. | ISAMPLER3D { $$ = ast_isampler3d; }
  901. | ISAMPLERCUBE { $$ = ast_isamplercube; }
  902. | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
  903. | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
  904. | USAMPLER1D { $$ = ast_usampler1d; }
  905. | USAMPLER2D { $$ = ast_usampler2d; }
  906. | USAMPLER3D { $$ = ast_usampler3d; }
  907. | USAMPLERCUBE { $$ = ast_usamplercube; }
  908. | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
  909. | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
  910. ;
  911. precision_qualifier:
  912. HIGH_PRECISION { $$ = ast_precision_high; }
  913. | MEDIUM_PRECISION { $$ = ast_precision_medium; }
  914. | LOW_PRECISION { $$ = ast_precision_low; }
  915. ;
  916. struct_specifier:
  917. STRUCT IDENTIFIER '{' struct_declaration_list '}'
  918. {
  919. $$ = new ast_struct_specifier($2, $4);
  920. $$->set_location(yylloc);
  921. }
  922. | STRUCT '{' struct_declaration_list '}'
  923. {
  924. $$ = new ast_struct_specifier(NULL, $3);
  925. $$->set_location(yylloc);
  926. }
  927. ;
  928. struct_declaration_list:
  929. struct_declaration
  930. {
  931. $$ = (struct ast_node *) $1;
  932. }
  933. | struct_declaration_list struct_declaration
  934. {
  935. $$ = (struct ast_node *) $1;
  936. insert_at_tail((struct simple_node *) $$,
  937. (struct simple_node *) $2);
  938. }
  939. ;
  940. struct_declaration:
  941. type_specifier struct_declarator_list ';'
  942. {
  943. ast_fully_specified_type *type = new ast_fully_specified_type();
  944. type->set_location(yylloc);
  945. type->specifier = $1;
  946. $$ = new ast_declarator_list(type);
  947. $$->set_location(yylloc);
  948. insert_at_tail((struct simple_node *) $2,
  949. & $$->declarations);
  950. }
  951. ;
  952. struct_declarator_list:
  953. struct_declarator
  954. | struct_declarator_list ',' struct_declarator
  955. {
  956. $$ = $1;
  957. insert_at_tail((struct simple_node *) $$,
  958. (struct simple_node *) $3);
  959. }
  960. ;
  961. struct_declarator:
  962. IDENTIFIER
  963. {
  964. $$ = new ast_declaration($1, false, NULL, NULL);
  965. $$->set_location(yylloc);
  966. }
  967. | IDENTIFIER '[' constant_expression ']'
  968. {
  969. $$ = new ast_declaration($1, true, $3, NULL);
  970. $$->set_location(yylloc);
  971. }
  972. ;
  973. initializer:
  974. assignment_expression
  975. ;
  976. declaration_statement:
  977. declaration
  978. ;
  979. // Grammar Note: labeled statements for SWITCH only; 'goto' is not
  980. // supported.
  981. statement:
  982. statement_matched
  983. | statement_unmatched
  984. ;
  985. statement_matched:
  986. compound_statement { $$ = (struct ast_node *) $1; }
  987. | simple_statement
  988. ;
  989. statement_unmatched:
  990. selection_statement_unmatched
  991. ;
  992. simple_statement:
  993. declaration_statement
  994. | expression_statement
  995. | selection_statement_matched
  996. | switch_statement { $$ = NULL; }
  997. | case_label { $$ = NULL; }
  998. | iteration_statement
  999. | jump_statement
  1000. ;
  1001. compound_statement:
  1002. '{' '}'
  1003. {
  1004. $$ = new ast_compound_statement(true, NULL);
  1005. $$->set_location(yylloc);
  1006. }
  1007. | '{' statement_list '}'
  1008. {
  1009. $$ = new ast_compound_statement(true, $2);
  1010. $$->set_location(yylloc);
  1011. }
  1012. ;
  1013. statement_no_new_scope:
  1014. compound_statement_no_new_scope { $$ = (struct ast_node *) $1; }
  1015. | simple_statement
  1016. ;
  1017. compound_statement_no_new_scope:
  1018. '{' '}'
  1019. {
  1020. $$ = new ast_compound_statement(false, NULL);
  1021. $$->set_location(yylloc);
  1022. }
  1023. | '{' statement_list '}'
  1024. {
  1025. $$ = new ast_compound_statement(false, $2);
  1026. $$->set_location(yylloc);
  1027. }
  1028. ;
  1029. statement_list:
  1030. statement
  1031. {
  1032. if ($1 == NULL) {
  1033. _mesa_glsl_error(& @1, state, "<nil> statement\n");
  1034. assert($1 != NULL);
  1035. }
  1036. $$ = $1;
  1037. make_empty_list((struct simple_node *) $$);
  1038. }
  1039. | statement_list statement
  1040. {
  1041. if ($2 == NULL) {
  1042. _mesa_glsl_error(& @2, state, "<nil> statement\n");
  1043. assert($2 != NULL);
  1044. }
  1045. $$ = $1;
  1046. insert_at_tail((struct simple_node *) $$,
  1047. (struct simple_node *) $2);
  1048. }
  1049. ;
  1050. expression_statement:
  1051. ';'
  1052. {
  1053. $$ = new ast_expression_statement(NULL);
  1054. $$->set_location(yylloc);
  1055. }
  1056. | expression ';'
  1057. {
  1058. $$ = new ast_expression_statement($1);
  1059. $$->set_location(yylloc);
  1060. }
  1061. ;
  1062. selection_statement_matched:
  1063. IF '(' expression ')' statement_matched ELSE statement_matched
  1064. {
  1065. $$ = new ast_selection_statement($3, $5, $7);
  1066. $$->set_location(yylloc);
  1067. }
  1068. ;
  1069. selection_statement_unmatched:
  1070. IF '(' expression ')' statement_matched
  1071. {
  1072. $$ = new ast_selection_statement($3, $5, NULL);
  1073. $$->set_location(yylloc);
  1074. }
  1075. | IF '(' expression ')' statement_unmatched
  1076. {
  1077. $$ = new ast_selection_statement($3, $5, NULL);
  1078. $$->set_location(yylloc);
  1079. }
  1080. | IF '(' expression ')' statement_matched ELSE statement_unmatched
  1081. {
  1082. $$ = new ast_selection_statement($3, $5, $7);
  1083. $$->set_location(yylloc);
  1084. }
  1085. ;
  1086. condition:
  1087. expression
  1088. {
  1089. $$ = (struct ast_node *) $1;
  1090. }
  1091. | fully_specified_type IDENTIFIER '=' initializer
  1092. {
  1093. ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
  1094. ast_declarator_list *declarator = new ast_declarator_list($1);
  1095. decl->set_location(yylloc);
  1096. declarator->set_location(yylloc);
  1097. insert_at_tail(& declarator->declarations,
  1098. (struct simple_node *) decl);
  1099. $$ = declarator;
  1100. }
  1101. ;
  1102. switch_statement:
  1103. SWITCH '(' expression ')' compound_statement
  1104. ;
  1105. case_label:
  1106. CASE expression ':'
  1107. | DEFAULT ':'
  1108. ;
  1109. iteration_statement:
  1110. WHILE '(' condition ')' statement_no_new_scope
  1111. {
  1112. $$ = new ast_iteration_statement(ast_iteration_statement::ast_while,
  1113. NULL, $3, NULL, $5);
  1114. $$->set_location(yylloc);
  1115. }
  1116. | DO statement WHILE '(' expression ')' ';'
  1117. {
  1118. $$ = new ast_iteration_statement(ast_iteration_statement::ast_do_while,
  1119. NULL, $5, NULL, $2);
  1120. $$->set_location(yylloc);
  1121. }
  1122. | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
  1123. {
  1124. $$ = new ast_iteration_statement(ast_iteration_statement::ast_for,
  1125. $3, $4.cond, $4.rest, $6);
  1126. $$->set_location(yylloc);
  1127. }
  1128. ;
  1129. for_init_statement:
  1130. expression_statement
  1131. | declaration_statement
  1132. ;
  1133. conditionopt:
  1134. condition
  1135. | /* empty */
  1136. {
  1137. $$ = NULL;
  1138. }
  1139. ;
  1140. for_rest_statement:
  1141. conditionopt ';'
  1142. {
  1143. $$.cond = $1;
  1144. $$.rest = NULL;
  1145. }
  1146. | conditionopt ';' expression
  1147. {
  1148. $$.cond = $1;
  1149. $$.rest = $3;
  1150. }
  1151. ;
  1152. // Grammar Note: No 'goto'. Gotos are not supported.
  1153. jump_statement:
  1154. CONTINUE ';'
  1155. {
  1156. $$ = new ast_jump_statement(ast_jump_statement::ast_continue, NULL);
  1157. $$->set_location(yylloc);
  1158. }
  1159. | BREAK ';'
  1160. {
  1161. $$ = new ast_jump_statement(ast_jump_statement::ast_break, NULL);
  1162. $$->set_location(yylloc);
  1163. }
  1164. | RETURN ';'
  1165. {
  1166. $$ = new ast_jump_statement(ast_jump_statement::ast_return, NULL);
  1167. $$->set_location(yylloc);
  1168. }
  1169. | RETURN expression ';'
  1170. {
  1171. $$ = new ast_jump_statement(ast_jump_statement::ast_return, $2);
  1172. $$->set_location(yylloc);
  1173. }
  1174. | DISCARD ';' // Fragment shader only.
  1175. {
  1176. $$ = new ast_jump_statement(ast_jump_statement::ast_discard, NULL);
  1177. $$->set_location(yylloc);
  1178. }
  1179. ;
  1180. external_declaration:
  1181. function_definition { $$ = $1; }
  1182. | declaration { $$ = $1; }
  1183. ;
  1184. function_definition:
  1185. function_prototype compound_statement_no_new_scope
  1186. {
  1187. $$ = new ast_function_definition();
  1188. $$->set_location(yylloc);
  1189. $$->prototype = $1;
  1190. $$->body = $2;
  1191. }
  1192. ;