Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

glsl_parser.ypp 29KB

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