glsl.y 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. %{
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <stddef.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include "glsl_parser.h" //For context struct
  11. #include "glsl.parser.h" //For GLSL_STYPE and GLSL_LTYPE
  12. #include "glsl.lexer.h" //For glsl_lex()
  13. static void glsl_error(GLSL_LTYPE *loc, struct glsl_parse_context *c, const char *s);
  14. #define GLSL_STACK_BUFFER_SIZE (1024*1024)
  15. #define GLSL_STACK_BUFFER_PAYLOAD_SIZE (GLSL_STACK_BUFFER_SIZE - sizeof(intptr_t))
  16. uint8_t *glsl_parse_alloc(struct glsl_parse_context *context, size_t size, int align)
  17. {
  18. uint8_t *ret;
  19. if (size + align > (context->cur_buffer_end - context->cur_buffer)) {
  20. uint8_t *next_buffer = (uint8_t *)malloc(GLSL_STACK_BUFFER_SIZE);
  21. if (context->cur_buffer) {
  22. uint8_t **pnext = (uint8_t **)context->cur_buffer_end;
  23. *pnext = next_buffer;
  24. }
  25. context->cur_buffer_start = next_buffer;
  26. context->cur_buffer = next_buffer;
  27. context->cur_buffer_end = next_buffer + GLSL_STACK_BUFFER_PAYLOAD_SIZE;
  28. if (!context->first_buffer) {
  29. context->first_buffer = context->cur_buffer;
  30. }
  31. *((uint8_t **)context->cur_buffer_end) = NULL;
  32. }
  33. ret = context->cur_buffer;
  34. uint8_t *trunc = (uint8_t *)((~((intptr_t)align - 1)) & ((intptr_t)ret));
  35. if (trunc != ret) {
  36. ret = trunc + align;
  37. }
  38. context->cur_buffer = ret + size;
  39. return ret;
  40. }
  41. void glsl_parse_dealloc(struct glsl_parse_context *context)
  42. {
  43. uint8_t *buffer = context->first_buffer;
  44. while (buffer) {
  45. uint8_t *next = *((uint8_t **)(buffer + GLSL_STACK_BUFFER_PAYLOAD_SIZE));
  46. free(buffer);
  47. buffer = next;
  48. }
  49. }
  50. static char *glsl_parse_strdup(struct glsl_parse_context *context, const char *c)
  51. {
  52. int len = strlen(c);
  53. char *ret = (char *)glsl_parse_alloc(context, len + 1, 1);
  54. strcpy(ret, c);
  55. return ret;
  56. }
  57. struct glsl_node *new_glsl_node(struct glsl_parse_context *context, int code, ...)
  58. {
  59. struct glsl_node *temp;
  60. int i;
  61. int n = 0;
  62. va_list vl;
  63. va_start(vl, code);
  64. while (1) {
  65. temp = va_arg(vl, struct glsl_node *);
  66. if (temp)
  67. n++;
  68. else
  69. break;
  70. }
  71. va_end(vl);
  72. struct glsl_node *g = (struct glsl_node *)glsl_parse_alloc(context, offsetof(struct glsl_node, children[n]), 8);
  73. g->code = code;
  74. g->child_count = n;
  75. va_start(vl, code);
  76. for (i = 0; i < n; i++) {
  77. temp = va_arg(vl, struct glsl_node *);
  78. g->children[i] = temp;
  79. }
  80. va_end(vl);
  81. return g;
  82. }
  83. static struct glsl_node *new_glsl_identifier(struct glsl_parse_context *context, const char *str)
  84. {
  85. struct glsl_node *n = new_glsl_node(context, IDENTIFIER, NULL);
  86. if (!str)
  87. n->data.str = NULL;
  88. else
  89. n->data.str = glsl_parse_strdup(context, str);
  90. return n;
  91. }
  92. static struct glsl_node *new_glsl_string(struct glsl_parse_context *context, int code, const char *str)
  93. {
  94. struct glsl_node *n = new_glsl_node(context, code, NULL);
  95. n->data.str = glsl_parse_strdup(context, str);
  96. return n;
  97. }
  98. #define scanner context->scanner //To allow the scanner to find it's context
  99. %}
  100. %defines
  101. %define api.prefix {glsl_}
  102. %define api.value.type union
  103. %pure-parser
  104. %parse-param { struct glsl_parse_context * context }
  105. %lex-param { void * scanner }
  106. %locations
  107. %type <struct glsl_node *> translation_unit
  108. %type <struct glsl_node *> external_declaration
  109. %type <struct glsl_node *> function_definition
  110. %type <struct glsl_node *> compound_statement_no_new_scope
  111. %type <struct glsl_node *> statement
  112. %type <struct glsl_node *> statement_list
  113. %type <struct glsl_node *> compound_statement
  114. %type <struct glsl_node *> simple_statement
  115. %type <struct glsl_node *> declaration
  116. %type <struct glsl_node *> identifier_list
  117. %type <struct glsl_node *> init_declarator_list
  118. %type <struct glsl_node *> single_declaration
  119. %type <struct glsl_node *> initializer
  120. %type <struct glsl_node *> initializer_list
  121. %type <struct glsl_node *> expression_statement
  122. %type <struct glsl_node *> selection_statement
  123. %type <struct glsl_node *> switch_statement
  124. %type <struct glsl_node *> switch_statement_list
  125. %type <struct glsl_node *> case_label
  126. %type <struct glsl_node *> iteration_statement
  127. %type <struct glsl_node *> statement_no_new_scope
  128. %type <struct glsl_node *> for_init_statement
  129. %type <struct glsl_node *> conditionopt
  130. %type <struct glsl_node *> condition
  131. %type <struct glsl_node *> for_rest_statement
  132. %type <struct glsl_node *> jump_statement
  133. %type <struct glsl_node *> function_prototype
  134. %type <struct glsl_node *> function_declarator
  135. %type <struct glsl_node *> parameter_declaration
  136. %type <struct glsl_node *> parameter_declarator
  137. %type <struct glsl_node *> function_header
  138. %type <struct glsl_node *> function_parameter_list
  139. %type <struct glsl_node *> fully_specified_type
  140. %type <struct glsl_node *> parameter_type_specifier
  141. %type <struct glsl_node *> primary_expression
  142. %type <struct glsl_node *> expression
  143. %type <struct glsl_node *> assignment_expression
  144. %type <struct glsl_node *> conditional_expression
  145. %type <struct glsl_node *> logical_or_expression
  146. %type <struct glsl_node *> logical_xor_expression
  147. %type <struct glsl_node *> logical_and_expression
  148. %type <struct glsl_node *> exclusive_or_expression
  149. %type <struct glsl_node *> constant_expression
  150. %type <struct glsl_node *> and_expression
  151. %type <struct glsl_node *> equality_expression
  152. %type <struct glsl_node *> relational_expression
  153. %type <struct glsl_node *> shift_expression
  154. %type <struct glsl_node *> additive_expression
  155. %type <struct glsl_node *> multiplicative_expression
  156. %type <struct glsl_node *> unary_expression
  157. %type <struct glsl_node *> postfix_expression
  158. %type <struct glsl_node *> integer_expression
  159. %type <struct glsl_node *> inclusive_or_expression
  160. %type <struct glsl_node *> function_call
  161. %type <struct glsl_node *> function_call_or_method
  162. %type <struct glsl_node *> function_call_generic
  163. %type <struct glsl_node *> function_call_parameter_list
  164. %type <struct glsl_node *> function_identifier
  165. %type <struct glsl_node *> type_specifier
  166. %type <struct glsl_node *> type_specifier_nonarray
  167. %type <struct glsl_node *> struct_specifier
  168. %type <struct glsl_node *> array_specifier
  169. %type <struct glsl_node *> array_specifier_list
  170. %type <struct glsl_node *> struct_declaration_list
  171. %type <struct glsl_node *> struct_declaration
  172. %type <struct glsl_node *> struct_declarator_list
  173. %type <struct glsl_node *> struct_declarator
  174. %type <struct glsl_node *> type_qualifier
  175. %type <struct glsl_node *> single_type_qualifier
  176. %type <struct glsl_node *> layout_qualifier
  177. %type <struct glsl_node *> layout_qualifier_id_list
  178. %type <struct glsl_node *> layout_qualifier_id
  179. %type <struct glsl_node *> precision_qualifier
  180. %type <struct glsl_node *> invariant_qualifier
  181. %type <struct glsl_node *> precise_qualifier
  182. %type <struct glsl_node *> storage_qualifier
  183. %type <struct glsl_node *> interpolation_qualifier
  184. %type <struct glsl_node *> type_name_list
  185. %type <struct glsl_node *> variable_identifier
  186. %type <struct glsl_node *> decl_identifier
  187. %type <struct glsl_node *> block_identifier
  188. %type <struct glsl_node *> struct_name
  189. %type <struct glsl_node *> type_name
  190. %type <struct glsl_node *> param_name
  191. %type <struct glsl_node *> function_name
  192. %type <struct glsl_node *> field_identifier
  193. %type <struct glsl_node *> type_specifier_identifier
  194. %type <struct glsl_node *> layout_identifier
  195. %type <int> assignment_operator
  196. %type <int> unary_operator
  197. %token CONST
  198. %token BOOL
  199. %token FLOAT
  200. %token DOUBLE
  201. %token INT
  202. %token UINT
  203. %token BREAK
  204. %token CONTINUE
  205. %token DO
  206. %token ELSE
  207. %token FOR
  208. %token IF
  209. %token DISCARD
  210. %token RETURN
  211. %token RETURN_VALUE
  212. %token SWITCH
  213. %token CASE
  214. %token DEFAULT
  215. %token SUBROUTINE
  216. %token BVEC2
  217. %token BVEC3
  218. %token BVEC4
  219. %token IVEC2
  220. %token IVEC3
  221. %token IVEC4
  222. %token UVEC2
  223. %token UVEC3
  224. %token UVEC4
  225. %token VEC2
  226. %token VEC3
  227. %token VEC4
  228. %token MAT2
  229. %token MAT3
  230. %token MAT4
  231. %token CENTROID
  232. %token IN
  233. %token OUT
  234. %token INOUT
  235. %token UNIFORM
  236. %token PATCH
  237. %token SAMPLE
  238. %token BUFFER
  239. %token SHARED
  240. %token COHERENT
  241. %token VOLATILE
  242. %token RESTRICT
  243. %token READONLY
  244. %token WRITEONLY
  245. %token DVEC2
  246. %token DVEC3
  247. %token DVEC4
  248. %token DMAT2
  249. %token DMAT3
  250. %token DMAT4
  251. %token NOPERSPECTIVE
  252. %token FLAT
  253. %token SMOOTH
  254. %token LAYOUT
  255. %token MAT2X2
  256. %token MAT2X3
  257. %token MAT2X4
  258. %token MAT3X2
  259. %token MAT3X3
  260. %token MAT3X4
  261. %token MAT4X2
  262. %token MAT4X3
  263. %token MAT4X4
  264. %token DMAT2X2
  265. %token DMAT2X3
  266. %token DMAT2X4
  267. %token DMAT3X2
  268. %token DMAT3X3
  269. %token DMAT3X4
  270. %token DMAT4X2
  271. %token DMAT4X3
  272. %token DMAT4X4
  273. %token ATOMIC_UINT
  274. %token SAMPLER1D
  275. %token SAMPLER2D
  276. %token SAMPLER3D
  277. %token SAMPLERCUBE
  278. %token SAMPLER1DSHADOW
  279. %token SAMPLER2DSHADOW
  280. %token SAMPLERCUBESHADOW
  281. %token SAMPLER1DARRAY
  282. %token SAMPLER2DARRAY
  283. %token SAMPLER1DARRAYSHADOW
  284. %token SAMPLER2DARRAYSHADOW
  285. %token ISAMPLER1D
  286. %token ISAMPLER2D
  287. %token ISAMPLER3D
  288. %token ISAMPLERCUBE
  289. %token ISAMPLER1DARRAY
  290. %token ISAMPLER2DARRAY
  291. %token USAMPLER1D
  292. %token USAMPLER2D
  293. %token USAMPLER3D
  294. %token USAMPLERCUBE
  295. %token USAMPLER1DARRAY
  296. %token USAMPLER2DARRAY
  297. %token SAMPLER2DRECT
  298. %token SAMPLER2DRECTSHADOW
  299. %token ISAMPLER2DRECT
  300. %token USAMPLER2DRECT
  301. %token SAMPLERBUFFER
  302. %token ISAMPLERBUFFER
  303. %token USAMPLERBUFFER
  304. %token SAMPLERCUBEARRAY
  305. %token SAMPLERCUBEARRAYSHADOW
  306. %token ISAMPLERCUBEARRAY
  307. %token USAMPLERCUBEARRAY
  308. %token SAMPLER2DMS
  309. %token ISAMPLER2DMS
  310. %token USAMPLER2DMS
  311. %token SAMPLER2DMSARRAY
  312. %token ISAMPLER2DMSARRAY
  313. %token USAMPLER2DMSARRAY
  314. %token IMAGE1D
  315. %token IIMAGE1D
  316. %token UIMAGE1D
  317. %token IMAGE2D
  318. %token IIMAGE2D
  319. %token UIMAGE2D
  320. %token IMAGE3D
  321. %token IIMAGE3D
  322. %token UIMAGE3D
  323. %token IMAGE2DRECT
  324. %token IIMAGE2DRECT
  325. %token UIMAGE2DRECT
  326. %token IMAGECUBE
  327. %token IIMAGECUBE
  328. %token UIMAGECUBE
  329. %token IMAGEBUFFER
  330. %token IIMAGEBUFFER
  331. %token UIMAGEBUFFER
  332. %token IMAGE1DARRAY
  333. %token IIMAGE1DARRAY
  334. %token UIMAGE1DARRAY
  335. %token IMAGE2DARRAY
  336. %token IIMAGE2DARRAY
  337. %token UIMAGE2DARRAY
  338. %token IMAGECUBEARRAY
  339. %token IIMAGECUBEARRAY
  340. %token UIMAGECUBEARRAY
  341. %token IMAGE2DMS
  342. %token IIMAGE2DMS
  343. %token UIMAGE2DMS
  344. %token IMAGE2DMSARRAY
  345. %token IIMAGE2DMSARRAY
  346. %token UIMAGE2DMSARRAY
  347. %token STRUCT
  348. %token VOID
  349. %token WHILE
  350. %token <char *> IDENTIFIER
  351. %token <float> FLOATCONSTANT
  352. %token <double> DOUBLECONSTANT
  353. %token <int> INTCONSTANT
  354. %token <unsigned int> UINTCONSTANT
  355. %token TRUE_VALUE
  356. %token FALSE_VALUE
  357. %token LEFT_OP
  358. %token RIGHT_OP
  359. %token INC_OP
  360. %token DEC_OP
  361. %token LE_OP
  362. %token GE_OP
  363. %token EQ_OP
  364. %token NE_OP
  365. %token AND_OP
  366. %token OR_OP
  367. %token XOR_OP
  368. %token MUL_ASSIGN
  369. %token DIV_ASSIGN
  370. %token ADD_ASSIGN
  371. %token MOD_ASSIGN
  372. %token LEFT_ASSIGN
  373. %token RIGHT_ASSIGN
  374. %token AND_ASSIGN
  375. %token XOR_ASSIGN
  376. %token OR_ASSIGN
  377. %token SUB_ASSIGN
  378. %token LEFT_PAREN
  379. %token RIGHT_PAREN
  380. %token LEFT_BRACKET
  381. %token RIGHT_BRACKET
  382. %token LEFT_BRACE
  383. %token RIGHT_BRACE
  384. %token DOT
  385. %token COMMA
  386. %token COLON
  387. %token EQUAL
  388. %token SEMICOLON
  389. %token BANG
  390. %token DASH
  391. %token TILDE
  392. %token PLUS
  393. %token STAR
  394. %token SLASH
  395. %token PERCENT
  396. %token LEFT_ANGLE
  397. %token RIGHT_ANGLE
  398. %token VERTICAL_BAR
  399. %token CARET
  400. %token AMPERSAND
  401. %token QUESTION
  402. %token INVARIANT
  403. %token PRECISE
  404. %token HIGHP
  405. %token MEDIUMP
  406. %token LOWP
  407. %token PRECISION
  408. %token AT
  409. %token UNARY_PLUS
  410. %token UNARY_DASH
  411. %token PRE_INC_OP
  412. %token PRE_DEC_OP
  413. %token POST_DEC_OP
  414. %token POST_INC_OP
  415. %token ARRAY_REF_OP
  416. %token FUNCTION_CALL
  417. %token TYPE_NAME_LIST
  418. %token TYPE_SPECIFIER
  419. %token POSTFIX_EXPRESSION
  420. %token TYPE_QUALIFIER_LIST
  421. %token STRUCT_DECLARATION
  422. %token STRUCT_DECLARATOR
  423. %token STRUCT_SPECIFIER
  424. %token FUNCTION_DEFINITION
  425. %token DECLARATION
  426. %token STATEMENT_LIST
  427. %token TRANSLATION_UNIT
  428. %token PRECISION_DECLARATION
  429. %token BLOCK_DECLARATION
  430. %token TYPE_QUALIFIER_DECLARATION
  431. %token IDENTIFIER_LIST
  432. %token INIT_DECLARATOR_LIST
  433. %token FULLY_SPECIFIED_TYPE
  434. %token SINGLE_DECLARATION
  435. %token SINGLE_INIT_DECLARATION
  436. %token INITIALIZER_LIST
  437. %token EXPRESSION_STATEMENT
  438. %token SELECTION_STATEMENT
  439. %token SELECTION_STATEMENT_ELSE
  440. %token SWITCH_STATEMENT
  441. %token FOR_REST_STATEMENT
  442. %token WHILE_STATEMENT
  443. %token DO_STATEMENT
  444. %token FOR_STATEMENT
  445. %token CASE_LABEL
  446. %token CONDITION_OPT
  447. %token ASSIGNMENT_CONDITION
  448. %token EXPRESSION_CONDITION
  449. %token FUNCTION_HEADER
  450. %token FUNCTION_DECLARATION
  451. %token FUNCTION_PARAMETER_LIST
  452. %token PARAMETER_DECLARATION
  453. %token PARAMETER_DECLARATOR
  454. %token UNINITIALIZED_DECLARATION
  455. %token ARRAY_SPECIFIER
  456. %token ARRAY_SPECIFIER_LIST
  457. %token STRUCT_DECLARATOR_LIST
  458. %token FUNCTION_CALL_PARAMETER_LIST
  459. %token STRUCT_DECLARATION_LIST
  460. %token LAYOUT_QUALIFIER_ID
  461. %token LAYOUT_QUALIFIER_ID_LIST
  462. %token SUBROUTINE_TYPE
  463. %token PAREN_EXPRESSION
  464. %token INIT_DECLARATOR
  465. %token INITIALIZER
  466. %token TERNARY_EXPRESSION
  467. %token FIELD_IDENTIFIER
  468. %token NUM_GLSL_TOKEN
  469. %%
  470. root : { context->root = new_glsl_node(context, TRANSLATION_UNIT, NULL); }
  471. | translation_unit { context->root = $1; }
  472. ;
  473. translation_unit : external_declaration
  474. { $$ = new_glsl_node(context, TRANSLATION_UNIT, $1, NULL); }
  475. | translation_unit external_declaration
  476. { $$ = new_glsl_node(context, TRANSLATION_UNIT, $1, $2, NULL); }
  477. ;
  478. block_identifier : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  479. ;
  480. decl_identifier : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  481. ;
  482. struct_name : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  483. ;
  484. type_name : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  485. ;
  486. param_name : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  487. ;
  488. function_name : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  489. ;
  490. field_identifier : IDENTIFIER { $$ = new_glsl_string(context, FIELD_IDENTIFIER, $1); }
  491. ;
  492. variable_identifier : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  493. ;
  494. layout_identifier : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  495. ;
  496. type_specifier_identifier : IDENTIFIER { $$ = new_glsl_identifier(context, $1); }
  497. ;
  498. external_declaration : function_definition { $$ = $1; }
  499. | declaration { $$ = $1; }
  500. ;
  501. function_definition : function_prototype compound_statement_no_new_scope
  502. { $$ = new_glsl_node(context, FUNCTION_DEFINITION,
  503. $1,
  504. $2,
  505. NULL); }
  506. | function_prototype
  507. { $$ = new_glsl_node(context, FUNCTION_DEFINITION,
  508. $1,
  509. new_glsl_node(context, STATEMENT_LIST, NULL),
  510. NULL); }
  511. ;
  512. compound_statement_no_new_scope : LEFT_BRACE RIGHT_BRACE { $$ = new_glsl_node(context, STATEMENT_LIST, NULL); }
  513. | LEFT_BRACE statement_list RIGHT_BRACE { $$ = $2; }
  514. ;
  515. statement : compound_statement { $$ = $1; }
  516. | simple_statement { $$ = $1; }
  517. ;
  518. statement_list : statement { $$ = new_glsl_node(context, STATEMENT_LIST, $1, NULL); }
  519. | statement_list statement { $$ = new_glsl_node(context, STATEMENT_LIST, $1, $2, NULL); }
  520. ;
  521. compound_statement : LEFT_BRACE RIGHT_BRACE { $$ = new_glsl_node(context, STATEMENT_LIST, NULL); }
  522. | LEFT_BRACE statement_list RIGHT_BRACE { $$ = $2; }
  523. ;
  524. simple_statement : declaration { $$ = $1; }
  525. | expression_statement { $$ = $1; }
  526. | selection_statement { $$ = $1; }
  527. | switch_statement { $$ = $1; }
  528. | case_label { $$= $1; }
  529. | iteration_statement { $$ = $1; }
  530. | jump_statement { $$ = $1; }
  531. ;
  532. declaration : function_prototype SEMICOLON { $$ = new_glsl_node(context, DECLARATION, $1, NULL); }
  533. | init_declarator_list SEMICOLON { $$ = new_glsl_node(context, DECLARATION, $1, NULL); }
  534. | PRECISION precision_qualifier type_specifier SEMICOLON
  535. { $$ = new_glsl_node(context, DECLARATION,
  536. new_glsl_node(context, PRECISION_DECLARATION,
  537. $2,
  538. $3,
  539. NULL),
  540. NULL); }
  541. | type_qualifier block_identifier LEFT_BRACE struct_declaration_list RIGHT_BRACE SEMICOLON
  542. { $$ = new_glsl_node(context, DECLARATION,
  543. new_glsl_node(context, BLOCK_DECLARATION,
  544. $1,
  545. $2,
  546. $4,
  547. new_glsl_identifier(context, NULL),
  548. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  549. NULL),
  550. NULL); }
  551. | type_qualifier block_identifier LEFT_BRACE struct_declaration_list RIGHT_BRACE decl_identifier SEMICOLON
  552. { $$ = new_glsl_node(context, DECLARATION,
  553. new_glsl_node(context, BLOCK_DECLARATION,
  554. $1,
  555. $2,
  556. $4,
  557. $6,
  558. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  559. NULL),
  560. NULL); }
  561. | type_qualifier block_identifier LEFT_BRACE struct_declaration_list RIGHT_BRACE decl_identifier array_specifier_list SEMICOLON
  562. { $$ = new_glsl_node(context, DECLARATION,
  563. new_glsl_node(context, BLOCK_DECLARATION,
  564. $1,
  565. $2,
  566. $4,
  567. $6,
  568. $7,
  569. NULL),
  570. NULL); }
  571. | type_qualifier SEMICOLON
  572. { $$ = new_glsl_node(context, DECLARATION,
  573. new_glsl_node(context, UNINITIALIZED_DECLARATION,
  574. $1,
  575. new_glsl_identifier(context, NULL),
  576. NULL),
  577. NULL); }
  578. | type_qualifier type_name SEMICOLON
  579. { $$ = new_glsl_node(context, DECLARATION,
  580. new_glsl_node(context, UNINITIALIZED_DECLARATION,
  581. $1,
  582. $2,
  583. new_glsl_node(context, IDENTIFIER_LIST, NULL),
  584. NULL),
  585. NULL); }
  586. | type_qualifier type_name identifier_list SEMICOLON
  587. { $$ = new_glsl_node(context, DECLARATION,
  588. new_glsl_node(context, UNINITIALIZED_DECLARATION,
  589. $1,
  590. $2,
  591. $3,
  592. NULL),
  593. NULL); }
  594. ;
  595. identifier_list : COMMA decl_identifier { $$ = new_glsl_node(context, IDENTIFIER_LIST, $2, NULL); }
  596. | identifier_list COMMA decl_identifier
  597. { $$ = new_glsl_node(context, IDENTIFIER_LIST, $1, $3, NULL); }
  598. ;
  599. init_declarator_list : single_declaration { $$ = new_glsl_node(context, INIT_DECLARATOR_LIST, $1, NULL); }
  600. | init_declarator_list COMMA decl_identifier
  601. { $$ = new_glsl_node(context, INIT_DECLARATOR_LIST,
  602. $1,
  603. new_glsl_node(context, INIT_DECLARATOR,
  604. $3,
  605. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  606. NULL),
  607. NULL); }
  608. | init_declarator_list COMMA decl_identifier array_specifier_list
  609. { $$ = new_glsl_node(context, INIT_DECLARATOR_LIST,
  610. $1,
  611. new_glsl_node(context, INIT_DECLARATOR,
  612. $3,
  613. $4,
  614. NULL),
  615. NULL); }
  616. | init_declarator_list COMMA decl_identifier array_specifier_list EQUAL initializer
  617. { $$ = new_glsl_node(context, INIT_DECLARATOR_LIST,
  618. $1,
  619. new_glsl_node(context, INIT_DECLARATOR,
  620. $3,
  621. $4,
  622. $6,
  623. NULL),
  624. NULL); }
  625. | init_declarator_list COMMA decl_identifier EQUAL initializer
  626. { $$ = new_glsl_node(context, INIT_DECLARATOR_LIST,
  627. $1,
  628. new_glsl_node(context, INIT_DECLARATOR,
  629. $3,
  630. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  631. $5,
  632. NULL),
  633. NULL); }
  634. ;
  635. single_declaration : fully_specified_type
  636. { $$ = new_glsl_node(context, SINGLE_DECLARATION,
  637. $1,
  638. new_glsl_identifier(context, NULL),
  639. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  640. NULL); }
  641. | fully_specified_type decl_identifier
  642. { $$ = new_glsl_node(context, SINGLE_DECLARATION,
  643. $1,
  644. $2,
  645. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  646. NULL); }
  647. | fully_specified_type decl_identifier array_specifier_list
  648. { $$ = new_glsl_node(context, SINGLE_DECLARATION, $1, $2, $3, NULL); }
  649. | fully_specified_type decl_identifier array_specifier_list EQUAL initializer
  650. { $$ = new_glsl_node(context, SINGLE_INIT_DECLARATION, $1, $2, $3, $5, NULL); }
  651. | fully_specified_type decl_identifier EQUAL initializer
  652. { $$ = new_glsl_node(context, SINGLE_INIT_DECLARATION,
  653. $1,
  654. $2,
  655. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  656. $4,
  657. NULL); }
  658. ;
  659. initializer : assignment_expression { $$ = new_glsl_node(context, INITIALIZER, $1, NULL); }
  660. | LEFT_BRACE initializer_list RIGHT_BRACE { $$ = new_glsl_node(context, INITIALIZER, $2, NULL); }
  661. | LEFT_BRACE initializer_list COMMA RIGHT_BRACE { $$ = new_glsl_node(context, INITIALIZER, $2, NULL); }
  662. ;
  663. initializer_list : initializer
  664. { $$ = new_glsl_node(context, INITIALIZER_LIST, $1, NULL); }
  665. | initializer_list COMMA initializer
  666. { $$ = new_glsl_node(context, INITIALIZER_LIST, $1, $3, NULL); }
  667. ;
  668. expression_statement : SEMICOLON { $$ = new_glsl_node(context, EXPRESSION_STATEMENT, NULL); }
  669. | expression SEMICOLON { $$ = new_glsl_node(context, EXPRESSION_STATEMENT, $1, NULL); }
  670. ;
  671. selection_statement : IF LEFT_PAREN expression RIGHT_PAREN statement
  672. { $$ = new_glsl_node(context, SELECTION_STATEMENT, $3, $5, NULL); }
  673. | IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
  674. { $$ = new_glsl_node(context, SELECTION_STATEMENT_ELSE, $3, $5, $7, NULL); }
  675. ;
  676. switch_statement : SWITCH LEFT_PAREN expression RIGHT_PAREN LEFT_BRACE switch_statement_list RIGHT_BRACE
  677. { $$ = new_glsl_node(context, SWITCH_STATEMENT, $3, $6, NULL); }
  678. ;
  679. switch_statement_list : { $$ = new_glsl_node(context, STATEMENT_LIST, NULL); }
  680. | statement_list { $$ = $1; }
  681. ;
  682. case_label : CASE expression COLON { $$ = new_glsl_node(context, CASE_LABEL, $2, NULL); }
  683. | DEFAULT COLON { $$ = new_glsl_node(context, CASE_LABEL, NULL); }
  684. ;
  685. iteration_statement : WHILE LEFT_PAREN condition RIGHT_PAREN statement_no_new_scope
  686. { $$ = new_glsl_node(context, WHILE_STATEMENT, $3, $5, NULL); }
  687. | DO statement WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
  688. { $$ = new_glsl_node(context, DO_STATEMENT, $2, $5, NULL); }
  689. | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope
  690. { $$ = new_glsl_node(context, FOR_STATEMENT, $3, $4, $6, NULL); }
  691. ;
  692. statement_no_new_scope : compound_statement_no_new_scope { $$ = $1; }
  693. | simple_statement { $$ = $1; }
  694. ;
  695. for_init_statement : expression_statement { $$ = $1; }
  696. | declaration { $$ = $1; }
  697. ;
  698. conditionopt : condition { $$ = new_glsl_node(context, CONDITION_OPT, $1, NULL); }
  699. | { $$ = new_glsl_node(context, CONDITION_OPT, NULL); }
  700. ;
  701. condition : expression
  702. { $$ = new_glsl_node(context, EXPRESSION_CONDITION, $1, NULL); }
  703. | fully_specified_type variable_identifier EQUAL initializer
  704. { $$ = new_glsl_node(context, ASSIGNMENT_CONDITION, $1, $2, $4, NULL); }
  705. ;
  706. for_rest_statement : conditionopt SEMICOLON
  707. { $$ = new_glsl_node(context, FOR_REST_STATEMENT, $1, NULL); }
  708. | conditionopt SEMICOLON expression
  709. { $$ = new_glsl_node(context, FOR_REST_STATEMENT, $1, $3, NULL); }
  710. ;
  711. jump_statement : CONTINUE SEMICOLON
  712. { $$ = new_glsl_node(context, CONTINUE, NULL); }
  713. | BREAK SEMICOLON
  714. { $$ = new_glsl_node(context, BREAK, NULL); }
  715. | RETURN SEMICOLON
  716. { $$ = new_glsl_node(context, RETURN, NULL); }
  717. | RETURN expression SEMICOLON
  718. { $$ = new_glsl_node(context, RETURN_VALUE, $2, NULL); }
  719. | DISCARD SEMICOLON
  720. { $$ = new_glsl_node(context, DISCARD, NULL); }
  721. ;
  722. function_prototype : function_declarator RIGHT_PAREN { $$ = $1; }
  723. ;
  724. function_declarator : function_header
  725. { $$ = new_glsl_node(context, FUNCTION_DECLARATION,
  726. $1,
  727. new_glsl_node(context, FUNCTION_PARAMETER_LIST, NULL),
  728. NULL); }
  729. | function_header function_parameter_list
  730. { $$ = new_glsl_node(context, FUNCTION_DECLARATION,
  731. $1,
  732. $2,
  733. NULL); }
  734. ;
  735. function_parameter_list : parameter_declaration
  736. { $$ = new_glsl_node(context, FUNCTION_PARAMETER_LIST, $1, NULL); }
  737. | function_parameter_list COMMA parameter_declaration
  738. { $$ = new_glsl_node(context, FUNCTION_PARAMETER_LIST, $1, $3, NULL); }
  739. ;
  740. parameter_declaration : type_qualifier parameter_declarator
  741. { $$ = new_glsl_node(context, PARAMETER_DECLARATION, $1, $2, NULL); }
  742. | parameter_declarator
  743. { $$ = new_glsl_node(context, PARAMETER_DECLARATION,
  744. new_glsl_node(context, TYPE_QUALIFIER_LIST, NULL),
  745. $1,
  746. NULL); }
  747. | type_qualifier parameter_type_specifier
  748. { $$ = new_glsl_node(context, PARAMETER_DECLARATION, $1, $2, NULL); }
  749. | parameter_type_specifier
  750. { $$ = new_glsl_node(context, PARAMETER_DECLARATION,
  751. new_glsl_node(context, TYPE_QUALIFIER_LIST, NULL),
  752. $1,
  753. NULL); }
  754. ;
  755. parameter_declarator : type_specifier param_name
  756. { $$ = new_glsl_node(context, PARAMETER_DECLARATOR, $1, $2, NULL); }
  757. | type_specifier param_name array_specifier_list
  758. { $$ = new_glsl_node(context, PARAMETER_DECLARATOR, $1, $2, $3, NULL);}
  759. ;
  760. function_header : fully_specified_type function_name LEFT_PAREN
  761. { $$ = new_glsl_node(context, FUNCTION_HEADER, $1, $2, NULL); }
  762. ;
  763. fully_specified_type : type_specifier
  764. { $$ = new_glsl_node(context, FULLY_SPECIFIED_TYPE,
  765. new_glsl_node(context, TYPE_QUALIFIER_LIST, NULL),
  766. $1,
  767. NULL); }
  768. | type_qualifier type_specifier
  769. { $$ = new_glsl_node(context, FULLY_SPECIFIED_TYPE, $1, $2, NULL); }
  770. ;
  771. parameter_type_specifier : type_specifier
  772. { $$ = new_glsl_node(context, PARAMETER_DECLARATOR, $1, NULL); }
  773. ;
  774. type_specifier : type_specifier_nonarray
  775. { $$ = new_glsl_node(context, TYPE_SPECIFIER,
  776. $1,
  777. new_glsl_node(context, ARRAY_SPECIFIER_LIST, NULL),
  778. NULL); }
  779. | type_specifier_nonarray array_specifier_list
  780. { $$ = new_glsl_node(context, TYPE_SPECIFIER, $1, $2, NULL); }
  781. ;
  782. array_specifier_list : array_specifier
  783. { $$ = new_glsl_node(context, ARRAY_SPECIFIER_LIST, $1, NULL); }
  784. | array_specifier_list array_specifier
  785. { $$ = new_glsl_node(context, ARRAY_SPECIFIER_LIST, $1, $2, NULL); }
  786. ;
  787. array_specifier : LEFT_BRACKET RIGHT_BRACKET
  788. { $$ = new_glsl_node(context, ARRAY_SPECIFIER, NULL); }
  789. | LEFT_BRACKET constant_expression RIGHT_BRACKET
  790. { $$ = new_glsl_node(context, ARRAY_SPECIFIER, $2, NULL); }
  791. ;
  792. type_specifier_nonarray : VOID { $$ = new_glsl_node(context, VOID, NULL); }
  793. | FLOAT { $$ = new_glsl_node(context, FLOAT, NULL); }
  794. | DOUBLE { $$ = new_glsl_node(context, DOUBLE, NULL); }
  795. | INT { $$ = new_glsl_node(context, INT, NULL); }
  796. | UINT { $$ = new_glsl_node(context, UINT, NULL); }
  797. | BOOL { $$ = new_glsl_node(context, BOOL, NULL); }
  798. | VEC2 { $$ = new_glsl_node(context, VEC2, NULL); }
  799. | VEC3 { $$ = new_glsl_node(context, VEC3, NULL); }
  800. | VEC4 { $$ = new_glsl_node(context, VEC4, NULL); }
  801. | DVEC2 { $$ = new_glsl_node(context, DVEC2, NULL); }
  802. | DVEC3 { $$ = new_glsl_node(context, DVEC3, NULL); }
  803. | DVEC4 { $$ = new_glsl_node(context, DVEC4, NULL); }
  804. | BVEC2 { $$ = new_glsl_node(context, BVEC2, NULL); }
  805. | BVEC3 { $$ = new_glsl_node(context, BVEC3, NULL); }
  806. | BVEC4 { $$ = new_glsl_node(context, BVEC4, NULL); }
  807. | IVEC2 { $$ = new_glsl_node(context, IVEC2, NULL); }
  808. | IVEC3 { $$ = new_glsl_node(context, IVEC3, NULL); }
  809. | IVEC4 { $$ = new_glsl_node(context, IVEC4, NULL); }
  810. | UVEC2 { $$ = new_glsl_node(context, UVEC2, NULL); }
  811. | UVEC3 { $$ = new_glsl_node(context, UVEC3, NULL); }
  812. | UVEC4 { $$ = new_glsl_node(context, UVEC4, NULL); }
  813. | MAT2 { $$ = new_glsl_node(context, MAT2, NULL); }
  814. | MAT3 { $$ = new_glsl_node(context, MAT3, NULL); }
  815. | MAT4 { $$ = new_glsl_node(context, MAT4, NULL); }
  816. | MAT2X2 { $$ = new_glsl_node(context, MAT2X2, NULL); }
  817. | MAT2X3 { $$ = new_glsl_node(context, MAT2X3, NULL); }
  818. | MAT2X4 { $$ = new_glsl_node(context, MAT2X4, NULL); }
  819. | MAT3X2 { $$ = new_glsl_node(context, MAT3X2, NULL); }
  820. | MAT3X3 { $$ = new_glsl_node(context, MAT3X3, NULL); }
  821. | MAT3X4 { $$ = new_glsl_node(context, MAT3X4, NULL); }
  822. | MAT4X2 { $$ = new_glsl_node(context, MAT4X2, NULL); }
  823. | MAT4X3 { $$ = new_glsl_node(context, MAT4X3, NULL); }
  824. | MAT4X4 { $$ = new_glsl_node(context, MAT4X4, NULL); }
  825. | DMAT2 { $$ = new_glsl_node(context, DMAT2, NULL); }
  826. | DMAT3 { $$ = new_glsl_node(context, DMAT3, NULL); }
  827. | DMAT4 { $$ = new_glsl_node(context, DMAT4, NULL); }
  828. | DMAT2X2 { $$ = new_glsl_node(context, DMAT2X2, NULL); }
  829. | DMAT2X3 { $$ = new_glsl_node(context, DMAT2X3, NULL); }
  830. | DMAT2X4 { $$ = new_glsl_node(context, DMAT2X4, NULL); }
  831. | DMAT3X2 { $$ = new_glsl_node(context, DMAT3X2, NULL); }
  832. | DMAT3X3 { $$ = new_glsl_node(context, DMAT3X3, NULL); }
  833. | DMAT3X4 { $$ = new_glsl_node(context, DMAT3X4, NULL); }
  834. | DMAT4X2 { $$ = new_glsl_node(context, DMAT4X2, NULL); }
  835. | DMAT4X3 { $$ = new_glsl_node(context, DMAT4X3, NULL); }
  836. | DMAT4X4 { $$ = new_glsl_node(context, DMAT4X4, NULL); }
  837. | ATOMIC_UINT { $$ = new_glsl_node(context, UINT, NULL); }
  838. | SAMPLER1D { $$ = new_glsl_node(context, SAMPLER1D, NULL); }
  839. | SAMPLER2D { $$ = new_glsl_node(context, SAMPLER2D, NULL); }
  840. | SAMPLER3D { $$ = new_glsl_node(context, SAMPLER3D, NULL); }
  841. | SAMPLERCUBE { $$ = new_glsl_node(context, SAMPLERCUBE, NULL); }
  842. | SAMPLER1DSHADOW { $$ = new_glsl_node(context, SAMPLER1DSHADOW, NULL); }
  843. | SAMPLER2DSHADOW { $$ = new_glsl_node(context, SAMPLER2DSHADOW, NULL); }
  844. | SAMPLERCUBESHADOW { $$ = new_glsl_node(context, SAMPLERCUBESHADOW, NULL); }
  845. | SAMPLER1DARRAY { $$ = new_glsl_node(context, SAMPLER1DARRAY, NULL); }
  846. | SAMPLER2DARRAY { $$ = new_glsl_node(context, SAMPLER2DARRAY, NULL); }
  847. | SAMPLER1DARRAYSHADOW { $$ = new_glsl_node(context, SAMPLER1DARRAYSHADOW, NULL); }
  848. | SAMPLER2DARRAYSHADOW { $$ = new_glsl_node(context, SAMPLER2DARRAYSHADOW, NULL); }
  849. | SAMPLERCUBEARRAY { $$ = new_glsl_node(context, SAMPLERCUBEARRAY, NULL); }
  850. | SAMPLERCUBEARRAYSHADOW { $$ = new_glsl_node(context, SAMPLERCUBEARRAYSHADOW, NULL); }
  851. | ISAMPLER1D { $$ = new_glsl_node(context, ISAMPLER1D, NULL); }
  852. | ISAMPLER2D { $$ = new_glsl_node(context, ISAMPLER2D, NULL); }
  853. | ISAMPLER3D { $$ = new_glsl_node(context, ISAMPLER3D, NULL); }
  854. | ISAMPLERCUBE { $$ = new_glsl_node(context, ISAMPLERCUBE, NULL); }
  855. | ISAMPLER1DARRAY { $$ = new_glsl_node(context, ISAMPLER1DARRAY, NULL); }
  856. | ISAMPLER2DARRAY { $$ = new_glsl_node(context, ISAMPLER2DARRAY, NULL); }
  857. | ISAMPLERCUBEARRAY { $$ = new_glsl_node(context, ISAMPLERCUBEARRAY, NULL); }
  858. | USAMPLER1D { $$ = new_glsl_node(context, USAMPLER1D, NULL); }
  859. | USAMPLER2D { $$ = new_glsl_node(context, USAMPLER2D, NULL); }
  860. | USAMPLER3D { $$ = new_glsl_node(context, USAMPLER3D, NULL); }
  861. | USAMPLERCUBE { $$ = new_glsl_node(context, USAMPLERCUBE, NULL); }
  862. | USAMPLER1DARRAY { $$ = new_glsl_node(context, USAMPLER1DARRAY, NULL); }
  863. | USAMPLER2DARRAY { $$ = new_glsl_node(context, USAMPLER2DARRAY, NULL); }
  864. | USAMPLERCUBEARRAY { $$ = new_glsl_node(context, USAMPLERCUBEARRAY, NULL); }
  865. | SAMPLER2DRECT { $$ = new_glsl_node(context, SAMPLER2DRECT, NULL); }
  866. | SAMPLER2DRECTSHADOW { $$ = new_glsl_node(context, SAMPLER2DRECTSHADOW, NULL); }
  867. | ISAMPLER2DRECT { $$ = new_glsl_node(context, ISAMPLER2DRECT, NULL); }
  868. | USAMPLER2DRECT { $$ = new_glsl_node(context, USAMPLER2DRECT, NULL); }
  869. | SAMPLERBUFFER { $$ = new_glsl_node(context, SAMPLERBUFFER, NULL); }
  870. | ISAMPLERBUFFER { $$ = new_glsl_node(context, ISAMPLERBUFFER, NULL); }
  871. | USAMPLERBUFFER { $$ = new_glsl_node(context, USAMPLERBUFFER, NULL); }
  872. | SAMPLER2DMS { $$ = new_glsl_node(context, SAMPLER2DMS, NULL); }
  873. | ISAMPLER2DMS { $$ = new_glsl_node(context, ISAMPLER2DMS, NULL); }
  874. | USAMPLER2DMS { $$ = new_glsl_node(context, USAMPLER2DMS, NULL); }
  875. | SAMPLER2DMSARRAY { $$ = new_glsl_node(context, SAMPLER2DMSARRAY, NULL); }
  876. | ISAMPLER2DMSARRAY { $$ = new_glsl_node(context, ISAMPLER2DMSARRAY, NULL); }
  877. | USAMPLER2DMSARRAY { $$ = new_glsl_node(context, USAMPLER2DMSARRAY, NULL); }
  878. | IMAGE1D { $$ = new_glsl_node(context, IMAGE1D, NULL); }
  879. | IIMAGE1D { $$ = new_glsl_node(context, IIMAGE1D, NULL); }
  880. | UIMAGE1D { $$ = new_glsl_node(context, UIMAGE1D, NULL); }
  881. | IMAGE2D { $$ = new_glsl_node(context, IMAGE2D, NULL); }
  882. | IIMAGE2D { $$ = new_glsl_node(context, IIMAGE2D, NULL); }
  883. | UIMAGE2D { $$ = new_glsl_node(context, UIMAGE2D, NULL); }
  884. | IMAGE3D { $$ = new_glsl_node(context, IMAGE3D, NULL); }
  885. | IIMAGE3D { $$ = new_glsl_node(context, IIMAGE3D, NULL); }
  886. | UIMAGE3D { $$ = new_glsl_node(context, UIMAGE3D, NULL); }
  887. | IMAGE2DRECT { $$ = new_glsl_node(context, IMAGE2DRECT, NULL); }
  888. | IIMAGE2DRECT { $$ = new_glsl_node(context, IIMAGE2DRECT, NULL); }
  889. | UIMAGE2DRECT { $$ = new_glsl_node(context, UIMAGE2DRECT, NULL); }
  890. | IMAGECUBE { $$ = new_glsl_node(context, IMAGECUBE, NULL); }
  891. | IIMAGECUBE { $$ = new_glsl_node(context, IIMAGECUBE, NULL); }
  892. | UIMAGECUBE { $$ = new_glsl_node(context, UIMAGECUBE, NULL); }
  893. | IMAGEBUFFER { $$ = new_glsl_node(context, IMAGEBUFFER, NULL); }
  894. | IIMAGEBUFFER { $$ = new_glsl_node(context, IIMAGEBUFFER, NULL); }
  895. | UIMAGEBUFFER { $$ = new_glsl_node(context, UIMAGEBUFFER, NULL); }
  896. | IMAGE1DARRAY { $$ = new_glsl_node(context, IMAGE1DARRAY, NULL); }
  897. | IIMAGE1DARRAY { $$ = new_glsl_node(context, IIMAGE1DARRAY, NULL); }
  898. | UIMAGE1DARRAY { $$ = new_glsl_node(context, UIMAGE1DARRAY, NULL); }
  899. | IMAGE2DARRAY { $$ = new_glsl_node(context, IMAGE2DARRAY, NULL); }
  900. | IIMAGE2DARRAY { $$ = new_glsl_node(context, IIMAGE2DARRAY, NULL); }
  901. | UIMAGE2DARRAY { $$ = new_glsl_node(context, UIMAGE2DARRAY, NULL); }
  902. | IMAGECUBEARRAY { $$ = new_glsl_node(context, IMAGECUBEARRAY, NULL); }
  903. | IIMAGECUBEARRAY { $$ = new_glsl_node(context, IIMAGECUBEARRAY, NULL); }
  904. | UIMAGECUBEARRAY { $$ = new_glsl_node(context, UIMAGECUBEARRAY, NULL); }
  905. | IMAGE2DMS { $$ = new_glsl_node(context, IMAGE2DMS, NULL); }
  906. | IIMAGE2DMS { $$ = new_glsl_node(context, IIMAGE2DMS, NULL); }
  907. | UIMAGE2DMS { $$ = new_glsl_node(context, UIMAGE2DMS, NULL); }
  908. | IMAGE2DMSARRAY { $$ = new_glsl_node(context, IMAGE2DMSARRAY, NULL); }
  909. | IIMAGE2DMSARRAY { $$ = new_glsl_node(context, IIMAGE2DMSARRAY, NULL); }
  910. | UIMAGE2DMSARRAY { $$ = new_glsl_node(context, UIMAGE2DMSARRAY, NULL); }
  911. | struct_specifier { $$ = $1; }
  912. | type_specifier_identifier { $$ = $1; }
  913. ;
  914. struct_specifier : STRUCT struct_name LEFT_BRACE struct_declaration_list RIGHT_BRACE
  915. { $$ = new_glsl_node(context, STRUCT_SPECIFIER, $2, $4, NULL);}
  916. | STRUCT LEFT_BRACE struct_declaration_list RIGHT_BRACE
  917. { $$ = new_glsl_node(context, STRUCT_SPECIFIER,
  918. new_glsl_identifier(context, NULL),
  919. $3,
  920. NULL); }
  921. ;
  922. struct_declaration_list : struct_declaration
  923. { $$ = new_glsl_node(context, STRUCT_DECLARATION_LIST, $1, NULL); }
  924. | struct_declaration_list struct_declaration
  925. { $$ = new_glsl_node(context, STRUCT_DECLARATION_LIST, $1, $2, NULL); }
  926. ;
  927. struct_declaration : type_specifier struct_declarator_list SEMICOLON
  928. { $$ = new_glsl_node(context, STRUCT_DECLARATION,
  929. new_glsl_node(context, TYPE_QUALIFIER_LIST, NULL),
  930. $1,
  931. $2,
  932. NULL); }
  933. | type_qualifier type_specifier struct_declarator_list SEMICOLON
  934. { $$ = new_glsl_node(context, STRUCT_DECLARATION, $1, $2, $3, NULL); }
  935. ;
  936. struct_declarator_list : struct_declarator
  937. { $$ = new_glsl_node(context, STRUCT_DECLARATOR_LIST, $1, NULL); }
  938. | struct_declarator_list COMMA struct_declarator
  939. { $$ = new_glsl_node(context, STRUCT_DECLARATOR_LIST, $1, $3, NULL); }
  940. ;
  941. struct_declarator : field_identifier
  942. { $$ = new_glsl_node(context, STRUCT_DECLARATOR, $1, NULL); }
  943. | field_identifier array_specifier_list
  944. { $$ = new_glsl_node(context, STRUCT_DECLARATOR, $1, $2, NULL); }
  945. ;
  946. type_qualifier : single_type_qualifier
  947. { $$ = new_glsl_node(context, TYPE_QUALIFIER_LIST, $1, NULL); }
  948. | type_qualifier single_type_qualifier
  949. { $$ = new_glsl_node(context, TYPE_QUALIFIER_LIST, $1, $2, NULL); }
  950. ;
  951. single_type_qualifier : storage_qualifier { $$ = $1; }
  952. | layout_qualifier { $$ = $1; }
  953. | precision_qualifier { $$ = $1; }
  954. | interpolation_qualifier { $$ = $1; }
  955. | invariant_qualifier { $$ = $1; }
  956. | precise_qualifier { $$ = $1; }
  957. ;
  958. layout_qualifier : LAYOUT LEFT_PAREN layout_qualifier_id_list RIGHT_PAREN { $$ = $3; }
  959. ;
  960. layout_qualifier_id_list: layout_qualifier_id { $$ = $1; }
  961. | layout_qualifier_id_list COMMA layout_qualifier_id
  962. { $$ = new_glsl_node(context, LAYOUT_QUALIFIER_ID_LIST, $1, $3, NULL); }
  963. ;
  964. layout_qualifier_id : layout_identifier
  965. { $$ = new_glsl_node(context, LAYOUT_QUALIFIER_ID, $1, NULL); }
  966. | layout_identifier EQUAL constant_expression
  967. { $$ = new_glsl_node(context, LAYOUT_QUALIFIER_ID, $1, $3, NULL);}
  968. | SHARED
  969. { $$ = new_glsl_node(context, SHARED, NULL); }
  970. ;
  971. precision_qualifier : HIGHP { $$ = new_glsl_node(context, HIGHP, NULL); }
  972. | MEDIUMP { $$ = new_glsl_node(context, MEDIUMP, NULL); }
  973. | LOWP { $$ = new_glsl_node(context, LOWP, NULL); }
  974. ;
  975. interpolation_qualifier : SMOOTH { $$ = new_glsl_node(context, SMOOTH, NULL); }
  976. | FLAT { $$ = new_glsl_node(context, FLAT, NULL); }
  977. | NOPERSPECTIVE { $$ = new_glsl_node(context, NOPERSPECTIVE, NULL); }
  978. ;
  979. invariant_qualifier : INVARIANT { $$ = new_glsl_node(context, INVARIANT, NULL); }
  980. ;
  981. precise_qualifier : PRECISE { $$ = new_glsl_node(context, PRECISE, NULL); }
  982. ;
  983. storage_qualifier : CONST { $$ = new_glsl_node(context, CONST, NULL); }
  984. | INOUT { $$ = new_glsl_node(context, INOUT, NULL); }
  985. | IN { $$ = new_glsl_node(context, IN, NULL); }
  986. | OUT { $$ = new_glsl_node(context, OUT, NULL); }
  987. | CENTROID { $$ = new_glsl_node(context, CENTROID, NULL); }
  988. | PATCH { $$ = new_glsl_node(context, PATCH, NULL); }
  989. | SAMPLE { $$ = new_glsl_node(context, SAMPLE, NULL); }
  990. | UNIFORM { $$ = new_glsl_node(context, UNIFORM, NULL); }
  991. | BUFFER { $$ = new_glsl_node(context, BUFFER, NULL); }
  992. | SHARED { $$ = new_glsl_node(context, SHARED, NULL); }
  993. | COHERENT { $$ = new_glsl_node(context, COHERENT, NULL); }
  994. | VOLATILE { $$ = new_glsl_node(context, VOLATILE, NULL); }
  995. | RESTRICT { $$ = new_glsl_node(context, RESTRICT, NULL); }
  996. | READONLY { $$ = new_glsl_node(context, READONLY, NULL); }
  997. | WRITEONLY { $$ = new_glsl_node(context, WRITEONLY, NULL); }
  998. | SUBROUTINE { $$ = new_glsl_node(context, SUBROUTINE, NULL); }
  999. | SUBROUTINE LEFT_PAREN type_name_list RIGHT_PAREN
  1000. { $$ = new_glsl_node(context, SUBROUTINE_TYPE,
  1001. new_glsl_node(context, TYPE_NAME_LIST, $3, NULL),
  1002. NULL); }
  1003. ;
  1004. type_name_list : type_name { $$ = $1; }
  1005. | type_name_list COMMA type_name
  1006. { $$ = new_glsl_node(context, TYPE_NAME_LIST, $1, $3, NULL); }
  1007. ;
  1008. expression : assignment_expression { $$ = $1; }
  1009. | expression COMMA assignment_expression
  1010. { $$ = new_glsl_node(context, COMMA, $1, $3, NULL); }
  1011. ;
  1012. assignment_expression : conditional_expression { $$ = $1; }
  1013. | unary_expression assignment_operator assignment_expression
  1014. { $$ = new_glsl_node(context,$2, $1, $3, NULL); }
  1015. ;
  1016. assignment_operator : EQUAL { $$ = EQUAL; }
  1017. | MUL_ASSIGN { $$ = MUL_ASSIGN; }
  1018. | DIV_ASSIGN { $$ = DIV_ASSIGN; }
  1019. | MOD_ASSIGN { $$ = MOD_ASSIGN; }
  1020. | ADD_ASSIGN { $$ = ADD_ASSIGN; }
  1021. | SUB_ASSIGN { $$ = SUB_ASSIGN; }
  1022. | LEFT_ASSIGN { $$ = LEFT_ASSIGN; }
  1023. | RIGHT_ASSIGN { $$ = RIGHT_ASSIGN; }
  1024. | AND_ASSIGN { $$ = AND_ASSIGN; }
  1025. | XOR_ASSIGN { $$ = XOR_ASSIGN; }
  1026. | OR_ASSIGN { $$ = OR_ASSIGN; }
  1027. ;
  1028. constant_expression : conditional_expression { $$ = $1; }
  1029. ;
  1030. conditional_expression : logical_or_expression { $$ = $1; }
  1031. | logical_or_expression QUESTION expression COLON assignment_expression
  1032. { $$ = new_glsl_node(context, TERNARY_EXPRESSION, $1, $3, $5, NULL); }
  1033. ;
  1034. logical_or_expression : logical_xor_expression { $$ = $1; }
  1035. | logical_or_expression OR_OP logical_xor_expression
  1036. { $$ = new_glsl_node(context, OR_OP, $1, $3, NULL); }
  1037. ;
  1038. logical_xor_expression : logical_and_expression { $$ = $1; }
  1039. | logical_xor_expression XOR_OP logical_and_expression
  1040. { $$ = new_glsl_node(context, XOR_OP, $1, $3, NULL); }
  1041. ;
  1042. logical_and_expression : inclusive_or_expression { $$ = $1; }
  1043. | logical_and_expression AND_OP inclusive_or_expression
  1044. { $$ = new_glsl_node(context, AND_OP, $1, $3, NULL); }
  1045. ;
  1046. inclusive_or_expression : exclusive_or_expression { $$ = $1; }
  1047. | inclusive_or_expression VERTICAL_BAR exclusive_or_expression
  1048. { $$ = new_glsl_node(context, VERTICAL_BAR, $1, $3, NULL); }
  1049. ;
  1050. exclusive_or_expression : and_expression { $$ = $1; }
  1051. | exclusive_or_expression CARET and_expression
  1052. { $$ = new_glsl_node(context, CARET, $1, $3, NULL); }
  1053. ;
  1054. and_expression : equality_expression { $$ = $1; }
  1055. | and_expression AMPERSAND equality_expression
  1056. { $$ = new_glsl_node(context, AMPERSAND, $1, $3, NULL); }
  1057. ;
  1058. equality_expression : relational_expression { $$ = $1; }
  1059. | equality_expression EQ_OP relational_expression
  1060. { $$ = new_glsl_node(context, EQ_OP, $1, $3, NULL); }
  1061. | equality_expression NE_OP relational_expression
  1062. { $$ = new_glsl_node(context, NE_OP, $1, $3, NULL); }
  1063. ;
  1064. relational_expression : shift_expression { $$ = $1; }
  1065. | relational_expression LEFT_ANGLE shift_expression
  1066. { $$ = new_glsl_node(context, LEFT_ANGLE, $1, $3, NULL); }
  1067. | relational_expression RIGHT_ANGLE shift_expression
  1068. { $$ = new_glsl_node(context, RIGHT_ANGLE, $1, $3, NULL); }
  1069. | relational_expression LE_OP shift_expression
  1070. { $$ = new_glsl_node(context, LE_OP, $1, $3, NULL); }
  1071. | relational_expression GE_OP shift_expression
  1072. { $$ = new_glsl_node(context, GE_OP, $1, $3, NULL); }
  1073. ;
  1074. shift_expression : additive_expression { $$ = $1; }
  1075. | shift_expression LEFT_OP additive_expression
  1076. { $$ = new_glsl_node(context, LEFT_OP, $1, $3, NULL); }
  1077. | shift_expression RIGHT_OP additive_expression
  1078. { $$ = new_glsl_node(context, RIGHT_OP, $1, $3, NULL); }
  1079. ;
  1080. additive_expression : multiplicative_expression { $$ = $1; }
  1081. | additive_expression PLUS multiplicative_expression
  1082. { $$ = new_glsl_node(context, PLUS, $1, $3, NULL); }
  1083. | additive_expression DASH multiplicative_expression
  1084. { $$ = new_glsl_node(context, DASH, $1, $3, NULL); }
  1085. ;
  1086. multiplicative_expression : unary_expression { $$ = $1; }
  1087. | multiplicative_expression STAR unary_expression
  1088. { $$ = new_glsl_node(context, STAR, $1, $3, NULL); }
  1089. | multiplicative_expression SLASH unary_expression
  1090. { $$ = new_glsl_node(context, SLASH, $1, $3, NULL); }
  1091. | multiplicative_expression PERCENT unary_expression
  1092. { $$ = new_glsl_node(context, PERCENT, $1, $3, NULL); }
  1093. ;
  1094. unary_expression : postfix_expression { $$ = $1; }
  1095. | INC_OP unary_expression
  1096. { $$ = new_glsl_node(context, PRE_INC_OP, $2, NULL); }
  1097. | DEC_OP unary_expression
  1098. { $$ = new_glsl_node(context, PRE_DEC_OP, $2, NULL); }
  1099. | unary_operator unary_expression
  1100. { $$ = new_glsl_node(context,$1, $2, NULL); }
  1101. ;
  1102. unary_operator : PLUS { $$ = UNARY_PLUS; }
  1103. | DASH { $$ = UNARY_DASH; }
  1104. | BANG { $$ = BANG; }
  1105. | TILDE { $$ = TILDE; }
  1106. ;
  1107. postfix_expression : primary_expression { $$ = $1; }
  1108. | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
  1109. { $$ = new_glsl_node(context, ARRAY_REF_OP, $1, $3, NULL); }
  1110. | function_call { $$ = $1; }
  1111. | postfix_expression DOT field_identifier
  1112. { $$ = new_glsl_node(context, DOT, $1, $3, NULL);}
  1113. | postfix_expression INC_OP
  1114. { $$ = new_glsl_node(context, POST_INC_OP, $1, NULL); }
  1115. | postfix_expression DEC_OP
  1116. { $$ = new_glsl_node(context, POST_DEC_OP, $1, NULL); }
  1117. ;
  1118. integer_expression : expression { $$ = $1; }
  1119. ;
  1120. function_call : function_call_or_method { $$ = $1; }
  1121. ;
  1122. function_call_or_method : function_call_generic { $$ = $1; }
  1123. ;
  1124. function_call_generic : function_identifier LEFT_PAREN function_call_parameter_list RIGHT_PAREN
  1125. { $$ = new_glsl_node(context, FUNCTION_CALL, $1, $3, NULL); }
  1126. | function_identifier LEFT_PAREN LEFT_PAREN
  1127. { $$ = new_glsl_node(context, FUNCTION_CALL,
  1128. $1,
  1129. new_glsl_node(context, FUNCTION_CALL_PARAMETER_LIST, NULL),
  1130. NULL); }
  1131. | function_identifier LEFT_PAREN VOID RIGHT_PAREN
  1132. { $$ = new_glsl_node(context, FUNCTION_CALL,
  1133. $1,
  1134. new_glsl_node(context, FUNCTION_CALL_PARAMETER_LIST, NULL),
  1135. NULL); }
  1136. ;
  1137. function_call_parameter_list : assignment_expression
  1138. { $$ = new_glsl_node(context, FUNCTION_CALL_PARAMETER_LIST, $1, NULL); }
  1139. | function_call_parameter_list COMMA assignment_expression
  1140. { $$ = new_glsl_node(context, FUNCTION_CALL_PARAMETER_LIST, $1, $3, NULL); }
  1141. ;
  1142. function_identifier : type_specifier { $$ = $1; }
  1143. | postfix_expression
  1144. { $$ = new_glsl_node(context, POSTFIX_EXPRESSION, $1, NULL); }
  1145. ;
  1146. primary_expression : variable_identifier { $$ = $1; }
  1147. | INTCONSTANT
  1148. { $$ = new_glsl_node(context, INTCONSTANT, NULL); $$->data.i = $1; }
  1149. | UINTCONSTANT
  1150. { $$ = new_glsl_node(context, UINTCONSTANT, NULL); $$->data.ui = $1; }
  1151. | FLOATCONSTANT
  1152. { $$ = new_glsl_node(context, FLOATCONSTANT, NULL); $$->data.f = $1; }
  1153. | TRUE_VALUE
  1154. { $$ = new_glsl_node(context, TRUE_VALUE, NULL); }
  1155. | FALSE_VALUE
  1156. { $$ = new_glsl_node(context, FALSE_VALUE, NULL); }
  1157. | DOUBLECONSTANT
  1158. { $$ = new_glsl_node(context, DOUBLECONSTANT, NULL); $$->data.d = $1; }
  1159. | LEFT_PAREN expression RIGHT_PAREN
  1160. { $$ = new_glsl_node(context, PAREN_EXPRESSION, $2, NULL); }
  1161. ;
  1162. %%
  1163. #include "glsl_ast.h"
  1164. //The scanner macro, needed for integration with flex, causes problems below
  1165. #undef scanner
  1166. static void glsl_error(GLSL_LTYPE *loc, struct glsl_parse_context *c, const char *s)
  1167. {
  1168. c->error = true;
  1169. if (c->error_cb)
  1170. c->error_cb(s, loc->first_line, loc->first_column, loc->last_column);
  1171. }
  1172. int list_length(struct glsl_node *n, int list_token)
  1173. {
  1174. if (n->code != list_token) {
  1175. return 1;
  1176. } else {
  1177. int i;
  1178. int count = 0;
  1179. for (i = 0; i < n->child_count; i++) {
  1180. count += list_length(n->children[i], list_token);
  1181. }
  1182. return count;
  1183. }
  1184. }
  1185. static void list_gather(struct glsl_node *n, struct glsl_node *new_list, int list_token)
  1186. {
  1187. int i;
  1188. for (i = 0; i < n->child_count; i++) {
  1189. struct glsl_node *child = n->children[i];
  1190. if (child->code != list_token)
  1191. new_list->children[new_list->child_count++] = child;
  1192. else
  1193. list_gather(child, new_list, list_token);
  1194. }
  1195. }
  1196. static void list_collapse(struct glsl_parse_context *context, struct glsl_node *n)
  1197. {
  1198. int i;
  1199. for (i = 0; i < n->child_count; i++) {
  1200. struct glsl_node *child = n->children[i];
  1201. if (glsl_ast_is_list_node(child)) {
  1202. int list_token = child->code;
  1203. int length = list_length(child, list_token);
  1204. struct glsl_node *g = (struct glsl_node *)glsl_parse_alloc(context, offsetof(struct glsl_node, children[length]), 8);
  1205. g->code = list_token;
  1206. g->child_count = 0;
  1207. list_gather(child, g, list_token);
  1208. n->children[i] = g;
  1209. child = g;
  1210. }
  1211. list_collapse(context, child);
  1212. }
  1213. }
  1214. static bool parse_internal(struct glsl_parse_context *context)
  1215. {
  1216. context->error = false;
  1217. glsl_parse(context);
  1218. if (context->root) {
  1219. if (glsl_ast_is_list_node(context->root)) {
  1220. //
  1221. // list_collapse() can't combine all the TRANSLATION_UNIT nodes
  1222. // since it would need to replace g_glsl_node_root so we combine
  1223. // the TRANSLATION_UNIT nodes here.
  1224. //
  1225. int list_code = context->root->code;
  1226. int length = list_length(context->root, list_code);
  1227. struct glsl_node *new_root = (struct glsl_node *)glsl_parse_alloc(context, offsetof(struct glsl_node, children[length]), 8);
  1228. new_root->code = TRANSLATION_UNIT;
  1229. new_root->child_count = 0;
  1230. list_gather(context->root, new_root, list_code);
  1231. assert(new_root->child_count == length);
  1232. context->root = new_root;
  1233. }
  1234. //
  1235. // Collapse other list nodes
  1236. //
  1237. list_collapse(context, context->root);
  1238. }
  1239. return context->error;
  1240. }
  1241. bool glsl_parse_file(struct glsl_parse_context *context, FILE *file)
  1242. {
  1243. glsl_lex_init(&(context->scanner));
  1244. glsl_set_in(file, context->scanner);
  1245. bool error;
  1246. error = parse_internal(context);
  1247. glsl_lex_destroy(context->scanner);
  1248. context->scanner = NULL;
  1249. return error;
  1250. }
  1251. bool glsl_parse_string(struct glsl_parse_context *context, const char *str)
  1252. {
  1253. char *text;
  1254. size_t sz;
  1255. bool error;
  1256. glsl_lex_init(&(context->scanner));
  1257. sz = strlen(str);
  1258. text = malloc(sz + 2);
  1259. strcpy(text, str);
  1260. text[sz + 1] = 0;
  1261. glsl__scan_buffer(text, sz + 2, context->scanner);
  1262. error = parse_internal(context);
  1263. free(text);
  1264. glsl_lex_destroy(context->scanner);
  1265. context->scanner = NULL;
  1266. return error;
  1267. }
  1268. void glsl_parse_context_init(struct glsl_parse_context *context)
  1269. {
  1270. context->root = NULL;
  1271. context->scanner = NULL;
  1272. context->first_buffer = NULL;
  1273. context->cur_buffer_start = NULL;
  1274. context->cur_buffer = NULL;
  1275. context->cur_buffer_end = NULL;
  1276. context->error_cb = NULL;
  1277. context->error = false;
  1278. }
  1279. void glsl_parse_set_error_cb(struct glsl_parse_context *context, glsl_parse_error_cb_t error_cb)
  1280. {
  1281. context->error_cb = error_cb;
  1282. }
  1283. void glsl_parse_context_destroy(struct glsl_parse_context *context)
  1284. {
  1285. glsl_parse_dealloc(context);
  1286. }