Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

glsl_parser.ypp 29KB

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