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.

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