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

glsl_parser.yy 86KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885
  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. #ifndef _MSC_VER
  28. #include <strings.h>
  29. #endif
  30. #include <assert.h>
  31. #include "ast.h"
  32. #include "glsl_parser_extras.h"
  33. #include "glsl_types.h"
  34. #include "main/context.h"
  35. #ifdef _MSC_VER
  36. #pragma warning( disable : 4065 ) // switch statement contains 'default' but no 'case' labels
  37. #endif
  38. #undef yyerror
  39. static void yyerror(YYLTYPE *loc, _mesa_glsl_parse_state *st, const char *msg)
  40. {
  41. _mesa_glsl_error(loc, st, "%s", msg);
  42. }
  43. static int
  44. _mesa_glsl_lex(YYSTYPE *val, YYLTYPE *loc, _mesa_glsl_parse_state *state)
  45. {
  46. return _mesa_glsl_lexer_lex(val, loc, state->scanner);
  47. }
  48. static bool match_layout_qualifier(const char *s1, const char *s2,
  49. _mesa_glsl_parse_state *state)
  50. {
  51. /* From the GLSL 1.50 spec, section 4.3.8 (Layout Qualifiers):
  52. *
  53. * "The tokens in any layout-qualifier-id-list ... are not case
  54. * sensitive, unless explicitly noted otherwise."
  55. *
  56. * The text "unless explicitly noted otherwise" appears to be
  57. * vacuous--no desktop GLSL spec (up through GLSL 4.40) notes
  58. * otherwise.
  59. *
  60. * However, the GLSL ES 3.00 spec says, in section 4.3.8 (Layout
  61. * Qualifiers):
  62. *
  63. * "As for other identifiers, they are case sensitive."
  64. *
  65. * So we need to do a case-sensitive or a case-insensitive match,
  66. * depending on whether we are compiling for GLSL ES.
  67. */
  68. if (state->es_shader)
  69. return strcmp(s1, s2);
  70. else
  71. return strcasecmp(s1, s2);
  72. }
  73. %}
  74. %expect 0
  75. %pure-parser
  76. %error-verbose
  77. %locations
  78. %initial-action {
  79. @$.first_line = 1;
  80. @$.first_column = 1;
  81. @$.last_line = 1;
  82. @$.last_column = 1;
  83. @$.source = 0;
  84. }
  85. %lex-param {struct _mesa_glsl_parse_state *state}
  86. %parse-param {struct _mesa_glsl_parse_state *state}
  87. %union {
  88. int n;
  89. float real;
  90. double dreal;
  91. const char *identifier;
  92. struct ast_type_qualifier type_qualifier;
  93. ast_node *node;
  94. ast_type_specifier *type_specifier;
  95. ast_array_specifier *array_specifier;
  96. ast_fully_specified_type *fully_specified_type;
  97. ast_function *function;
  98. ast_parameter_declarator *parameter_declarator;
  99. ast_function_definition *function_definition;
  100. ast_compound_statement *compound_statement;
  101. ast_expression *expression;
  102. ast_declarator_list *declarator_list;
  103. ast_struct_specifier *struct_specifier;
  104. ast_declaration *declaration;
  105. ast_switch_body *switch_body;
  106. ast_case_label *case_label;
  107. ast_case_label_list *case_label_list;
  108. ast_case_statement *case_statement;
  109. ast_case_statement_list *case_statement_list;
  110. ast_interface_block *interface_block;
  111. struct {
  112. ast_node *cond;
  113. ast_expression *rest;
  114. } for_rest_statement;
  115. struct {
  116. ast_node *then_statement;
  117. ast_node *else_statement;
  118. } selection_rest_statement;
  119. }
  120. %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK DOUBLE_TOK
  121. %token BREAK BUFFER CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
  122. %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4 DVEC2 DVEC3 DVEC4
  123. %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING SAMPLE
  124. %token NOPERSPECTIVE FLAT SMOOTH
  125. %token MAT2X2 MAT2X3 MAT2X4
  126. %token MAT3X2 MAT3X3 MAT3X4
  127. %token MAT4X2 MAT4X3 MAT4X4
  128. %token DMAT2X2 DMAT2X3 DMAT2X4
  129. %token DMAT3X2 DMAT3X3 DMAT3X4
  130. %token DMAT4X2 DMAT4X3 DMAT4X4
  131. %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
  132. %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
  133. %token SAMPLER2DARRAYSHADOW SAMPLERCUBEARRAY SAMPLERCUBEARRAYSHADOW
  134. %token ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
  135. %token ISAMPLER1DARRAY ISAMPLER2DARRAY ISAMPLERCUBEARRAY
  136. %token USAMPLER1D USAMPLER2D USAMPLER3D USAMPLERCUBE USAMPLER1DARRAY
  137. %token USAMPLER2DARRAY USAMPLERCUBEARRAY
  138. %token SAMPLER2DRECT ISAMPLER2DRECT USAMPLER2DRECT SAMPLER2DRECTSHADOW
  139. %token SAMPLERBUFFER ISAMPLERBUFFER USAMPLERBUFFER
  140. %token SAMPLER2DMS ISAMPLER2DMS USAMPLER2DMS
  141. %token SAMPLER2DMSARRAY ISAMPLER2DMSARRAY USAMPLER2DMSARRAY
  142. %token SAMPLEREXTERNALOES
  143. %token IMAGE1D IMAGE2D IMAGE3D IMAGE2DRECT IMAGECUBE IMAGEBUFFER
  144. %token IMAGE1DARRAY IMAGE2DARRAY IMAGECUBEARRAY IMAGE2DMS IMAGE2DMSARRAY
  145. %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGE2DRECT IIMAGECUBE IIMAGEBUFFER
  146. %token IIMAGE1DARRAY IIMAGE2DARRAY IIMAGECUBEARRAY IIMAGE2DMS IIMAGE2DMSARRAY
  147. %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGE2DRECT UIMAGECUBE UIMAGEBUFFER
  148. %token UIMAGE1DARRAY UIMAGE2DARRAY UIMAGECUBEARRAY UIMAGE2DMS UIMAGE2DMSARRAY
  149. %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
  150. %token COHERENT VOLATILE RESTRICT READONLY WRITEONLY
  151. %token ATOMIC_UINT
  152. %token STRUCT VOID_TOK WHILE
  153. %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
  154. %type <identifier> any_identifier
  155. %type <interface_block> instance_name_opt
  156. %token <real> FLOATCONSTANT
  157. %token <dreal> DOUBLECONSTANT
  158. %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
  159. %token <identifier> FIELD_SELECTION
  160. %token LEFT_OP RIGHT_OP
  161. %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
  162. %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
  163. %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
  164. %token SUB_ASSIGN
  165. %token INVARIANT PRECISE
  166. %token LOWP MEDIUMP HIGHP SUPERP PRECISION
  167. %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
  168. %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
  169. %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
  170. %token PRAGMA_INVARIANT_ALL
  171. %token LAYOUT_TOK
  172. /* Reserved words that are not actually used in the grammar.
  173. */
  174. %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
  175. %token INLINE_TOK NOINLINE PUBLIC_TOK STATIC EXTERN EXTERNAL
  176. %token LONG_TOK SHORT_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK
  177. %token HVEC2 HVEC3 HVEC4 FVEC2 FVEC3 FVEC4
  178. %token SAMPLER3DRECT
  179. %token SIZEOF CAST NAMESPACE USING
  180. %token RESOURCE PATCH
  181. %token SUBROUTINE
  182. %token ERROR_TOK
  183. %token COMMON PARTITION ACTIVE FILTER ROW_MAJOR
  184. %type <identifier> variable_identifier
  185. %type <node> statement
  186. %type <node> statement_list
  187. %type <node> simple_statement
  188. %type <n> precision_qualifier
  189. %type <type_qualifier> type_qualifier
  190. %type <type_qualifier> auxiliary_storage_qualifier
  191. %type <type_qualifier> storage_qualifier
  192. %type <type_qualifier> interpolation_qualifier
  193. %type <type_qualifier> layout_qualifier
  194. %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
  195. %type <type_qualifier> interface_block_layout_qualifier
  196. %type <type_qualifier> memory_qualifier
  197. %type <type_qualifier> interface_qualifier
  198. %type <type_specifier> type_specifier
  199. %type <type_specifier> type_specifier_nonarray
  200. %type <array_specifier> array_specifier
  201. %type <identifier> basic_type_specifier_nonarray
  202. %type <fully_specified_type> fully_specified_type
  203. %type <function> function_prototype
  204. %type <function> function_header
  205. %type <function> function_header_with_parameters
  206. %type <function> function_declarator
  207. %type <parameter_declarator> parameter_declarator
  208. %type <parameter_declarator> parameter_declaration
  209. %type <type_qualifier> parameter_qualifier
  210. %type <type_qualifier> parameter_direction_qualifier
  211. %type <type_specifier> parameter_type_specifier
  212. %type <function_definition> function_definition
  213. %type <compound_statement> compound_statement_no_new_scope
  214. %type <compound_statement> compound_statement
  215. %type <node> statement_no_new_scope
  216. %type <node> expression_statement
  217. %type <expression> expression
  218. %type <expression> primary_expression
  219. %type <expression> assignment_expression
  220. %type <expression> conditional_expression
  221. %type <expression> logical_or_expression
  222. %type <expression> logical_xor_expression
  223. %type <expression> logical_and_expression
  224. %type <expression> inclusive_or_expression
  225. %type <expression> exclusive_or_expression
  226. %type <expression> and_expression
  227. %type <expression> equality_expression
  228. %type <expression> relational_expression
  229. %type <expression> shift_expression
  230. %type <expression> additive_expression
  231. %type <expression> multiplicative_expression
  232. %type <expression> unary_expression
  233. %type <expression> constant_expression
  234. %type <expression> integer_expression
  235. %type <expression> postfix_expression
  236. %type <expression> function_call_header_with_parameters
  237. %type <expression> function_call_header_no_parameters
  238. %type <expression> function_call_header
  239. %type <expression> function_call_generic
  240. %type <expression> function_call_or_method
  241. %type <expression> function_call
  242. %type <expression> method_call_generic
  243. %type <expression> method_call_header_with_parameters
  244. %type <expression> method_call_header_no_parameters
  245. %type <expression> method_call_header
  246. %type <n> assignment_operator
  247. %type <n> unary_operator
  248. %type <expression> function_identifier
  249. %type <node> external_declaration
  250. %type <declarator_list> init_declarator_list
  251. %type <declarator_list> single_declaration
  252. %type <expression> initializer
  253. %type <expression> initializer_list
  254. %type <node> declaration
  255. %type <node> declaration_statement
  256. %type <node> jump_statement
  257. %type <node> interface_block
  258. %type <interface_block> basic_interface_block
  259. %type <struct_specifier> struct_specifier
  260. %type <declarator_list> struct_declaration_list
  261. %type <declarator_list> struct_declaration
  262. %type <declaration> struct_declarator
  263. %type <declaration> struct_declarator_list
  264. %type <declarator_list> member_list
  265. %type <declarator_list> member_declaration
  266. %type <node> selection_statement
  267. %type <selection_rest_statement> selection_rest_statement
  268. %type <node> switch_statement
  269. %type <switch_body> switch_body
  270. %type <case_label_list> case_label_list
  271. %type <case_label> case_label
  272. %type <case_statement> case_statement
  273. %type <case_statement_list> case_statement_list
  274. %type <node> iteration_statement
  275. %type <node> condition
  276. %type <node> conditionopt
  277. %type <node> for_init_statement
  278. %type <for_rest_statement> for_rest_statement
  279. %type <n> integer_constant
  280. %type <node> layout_defaults
  281. %right THEN ELSE
  282. %%
  283. translation_unit:
  284. version_statement extension_statement_list
  285. {
  286. _mesa_glsl_initialize_types(state);
  287. }
  288. external_declaration_list
  289. {
  290. delete state->symbols;
  291. state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
  292. _mesa_glsl_initialize_types(state);
  293. }
  294. ;
  295. version_statement:
  296. /* blank - no #version specified: defaults are already set */
  297. | VERSION_TOK INTCONSTANT EOL
  298. {
  299. state->process_version_directive(&@2, $2, NULL);
  300. if (state->error) {
  301. YYERROR;
  302. }
  303. }
  304. | VERSION_TOK INTCONSTANT any_identifier EOL
  305. {
  306. state->process_version_directive(&@2, $2, $3);
  307. if (state->error) {
  308. YYERROR;
  309. }
  310. }
  311. ;
  312. pragma_statement:
  313. PRAGMA_DEBUG_ON EOL
  314. | PRAGMA_DEBUG_OFF EOL
  315. | PRAGMA_OPTIMIZE_ON EOL
  316. | PRAGMA_OPTIMIZE_OFF EOL
  317. | PRAGMA_INVARIANT_ALL EOL
  318. {
  319. /* Pragma invariant(all) cannot be used in a fragment shader.
  320. *
  321. * Page 27 of the GLSL 1.20 spec, Page 53 of the GLSL ES 3.00 spec:
  322. *
  323. * "It is an error to use this pragma in a fragment shader."
  324. */
  325. if (state->is_version(120, 300) &&
  326. state->stage == MESA_SHADER_FRAGMENT) {
  327. _mesa_glsl_error(& @1, state,
  328. "pragma `invariant(all)' cannot be used "
  329. "in a fragment shader.");
  330. } else if (!state->is_version(120, 100)) {
  331. _mesa_glsl_warning(& @1, state,
  332. "pragma `invariant(all)' not supported in %s "
  333. "(GLSL ES 1.00 or GLSL 1.20 required)",
  334. state->get_version_string());
  335. } else {
  336. state->all_invariant = true;
  337. }
  338. }
  339. ;
  340. extension_statement_list:
  341. | extension_statement_list extension_statement
  342. ;
  343. any_identifier:
  344. IDENTIFIER
  345. | TYPE_IDENTIFIER
  346. | NEW_IDENTIFIER
  347. ;
  348. extension_statement:
  349. EXTENSION any_identifier COLON any_identifier EOL
  350. {
  351. if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
  352. YYERROR;
  353. }
  354. }
  355. ;
  356. external_declaration_list:
  357. external_declaration
  358. {
  359. /* FINISHME: The NULL test is required because pragmas are set to
  360. * FINISHME: NULL. (See production rule for external_declaration.)
  361. */
  362. if ($1 != NULL)
  363. state->translation_unit.push_tail(& $1->link);
  364. }
  365. | external_declaration_list external_declaration
  366. {
  367. /* FINISHME: The NULL test is required because pragmas are set to
  368. * FINISHME: NULL. (See production rule for external_declaration.)
  369. */
  370. if ($2 != NULL)
  371. state->translation_unit.push_tail(& $2->link);
  372. }
  373. | external_declaration_list extension_statement {
  374. if (!state->allow_extension_directive_midshader) {
  375. _mesa_glsl_error(& @2, state,
  376. "#extension directive is not allowed "
  377. "in the middle of a shader");
  378. YYERROR;
  379. }
  380. }
  381. ;
  382. variable_identifier:
  383. IDENTIFIER
  384. | NEW_IDENTIFIER
  385. ;
  386. primary_expression:
  387. variable_identifier
  388. {
  389. void *ctx = state;
  390. $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
  391. $$->set_location(@1);
  392. $$->primary_expression.identifier = $1;
  393. }
  394. | INTCONSTANT
  395. {
  396. void *ctx = state;
  397. $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
  398. $$->set_location(@1);
  399. $$->primary_expression.int_constant = $1;
  400. }
  401. | UINTCONSTANT
  402. {
  403. void *ctx = state;
  404. $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
  405. $$->set_location(@1);
  406. $$->primary_expression.uint_constant = $1;
  407. }
  408. | FLOATCONSTANT
  409. {
  410. void *ctx = state;
  411. $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
  412. $$->set_location(@1);
  413. $$->primary_expression.float_constant = $1;
  414. }
  415. | DOUBLECONSTANT
  416. {
  417. void *ctx = state;
  418. $$ = new(ctx) ast_expression(ast_double_constant, NULL, NULL, NULL);
  419. $$->set_location(@1);
  420. $$->primary_expression.double_constant = $1;
  421. }
  422. | BOOLCONSTANT
  423. {
  424. void *ctx = state;
  425. $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
  426. $$->set_location(@1);
  427. $$->primary_expression.bool_constant = $1;
  428. }
  429. | '(' expression ')'
  430. {
  431. $$ = $2;
  432. }
  433. ;
  434. postfix_expression:
  435. primary_expression
  436. | postfix_expression '[' integer_expression ']'
  437. {
  438. void *ctx = state;
  439. $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
  440. $$->set_location_range(@1, @4);
  441. }
  442. | function_call
  443. {
  444. $$ = $1;
  445. }
  446. | postfix_expression '.' any_identifier
  447. {
  448. void *ctx = state;
  449. $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
  450. $$->set_location_range(@1, @3);
  451. $$->primary_expression.identifier = $3;
  452. }
  453. | postfix_expression INC_OP
  454. {
  455. void *ctx = state;
  456. $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
  457. $$->set_location_range(@1, @2);
  458. }
  459. | postfix_expression DEC_OP
  460. {
  461. void *ctx = state;
  462. $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
  463. $$->set_location_range(@1, @2);
  464. }
  465. ;
  466. integer_expression:
  467. expression
  468. ;
  469. function_call:
  470. function_call_or_method
  471. ;
  472. function_call_or_method:
  473. function_call_generic
  474. | postfix_expression '.' method_call_generic
  475. {
  476. void *ctx = state;
  477. $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
  478. $$->set_location_range(@1, @3);
  479. }
  480. ;
  481. function_call_generic:
  482. function_call_header_with_parameters ')'
  483. | function_call_header_no_parameters ')'
  484. ;
  485. function_call_header_no_parameters:
  486. function_call_header VOID_TOK
  487. | function_call_header
  488. ;
  489. function_call_header_with_parameters:
  490. function_call_header assignment_expression
  491. {
  492. $$ = $1;
  493. $$->set_location(@1);
  494. $$->expressions.push_tail(& $2->link);
  495. }
  496. | function_call_header_with_parameters ',' assignment_expression
  497. {
  498. $$ = $1;
  499. $$->set_location(@1);
  500. $$->expressions.push_tail(& $3->link);
  501. }
  502. ;
  503. // Grammar Note: Constructors look like functions, but lexical
  504. // analysis recognized most of them as keywords. They are now
  505. // recognized through "type_specifier".
  506. function_call_header:
  507. function_identifier '('
  508. ;
  509. function_identifier:
  510. type_specifier
  511. {
  512. void *ctx = state;
  513. $$ = new(ctx) ast_function_expression($1);
  514. $$->set_location(@1);
  515. }
  516. | variable_identifier
  517. {
  518. void *ctx = state;
  519. ast_expression *callee = new(ctx) ast_expression($1);
  520. callee->set_location(@1);
  521. $$ = new(ctx) ast_function_expression(callee);
  522. $$->set_location(@1);
  523. }
  524. | FIELD_SELECTION
  525. {
  526. void *ctx = state;
  527. ast_expression *callee = new(ctx) ast_expression($1);
  528. callee->set_location(@1);
  529. $$ = new(ctx) ast_function_expression(callee);
  530. $$->set_location(@1);
  531. }
  532. ;
  533. method_call_generic:
  534. method_call_header_with_parameters ')'
  535. | method_call_header_no_parameters ')'
  536. ;
  537. method_call_header_no_parameters:
  538. method_call_header VOID_TOK
  539. | method_call_header
  540. ;
  541. method_call_header_with_parameters:
  542. method_call_header assignment_expression
  543. {
  544. $$ = $1;
  545. $$->set_location(@1);
  546. $$->expressions.push_tail(& $2->link);
  547. }
  548. | method_call_header_with_parameters ',' assignment_expression
  549. {
  550. $$ = $1;
  551. $$->set_location(@1);
  552. $$->expressions.push_tail(& $3->link);
  553. }
  554. ;
  555. // Grammar Note: Constructors look like methods, but lexical
  556. // analysis recognized most of them as keywords. They are now
  557. // recognized through "type_specifier".
  558. method_call_header:
  559. variable_identifier '('
  560. {
  561. void *ctx = state;
  562. ast_expression *callee = new(ctx) ast_expression($1);
  563. callee->set_location(@1);
  564. $$ = new(ctx) ast_function_expression(callee);
  565. $$->set_location(@1);
  566. }
  567. ;
  568. // Grammar Note: No traditional style type casts.
  569. unary_expression:
  570. postfix_expression
  571. | INC_OP unary_expression
  572. {
  573. void *ctx = state;
  574. $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
  575. $$->set_location(@1);
  576. }
  577. | DEC_OP unary_expression
  578. {
  579. void *ctx = state;
  580. $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
  581. $$->set_location(@1);
  582. }
  583. | unary_operator unary_expression
  584. {
  585. void *ctx = state;
  586. $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
  587. $$->set_location_range(@1, @2);
  588. }
  589. ;
  590. // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
  591. unary_operator:
  592. '+' { $$ = ast_plus; }
  593. | '-' { $$ = ast_neg; }
  594. | '!' { $$ = ast_logic_not; }
  595. | '~' { $$ = ast_bit_not; }
  596. ;
  597. multiplicative_expression:
  598. unary_expression
  599. | multiplicative_expression '*' unary_expression
  600. {
  601. void *ctx = state;
  602. $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
  603. $$->set_location_range(@1, @3);
  604. }
  605. | multiplicative_expression '/' unary_expression
  606. {
  607. void *ctx = state;
  608. $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
  609. $$->set_location_range(@1, @3);
  610. }
  611. | multiplicative_expression '%' unary_expression
  612. {
  613. void *ctx = state;
  614. $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
  615. $$->set_location_range(@1, @3);
  616. }
  617. ;
  618. additive_expression:
  619. multiplicative_expression
  620. | additive_expression '+' multiplicative_expression
  621. {
  622. void *ctx = state;
  623. $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
  624. $$->set_location_range(@1, @3);
  625. }
  626. | additive_expression '-' multiplicative_expression
  627. {
  628. void *ctx = state;
  629. $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
  630. $$->set_location_range(@1, @3);
  631. }
  632. ;
  633. shift_expression:
  634. additive_expression
  635. | shift_expression LEFT_OP additive_expression
  636. {
  637. void *ctx = state;
  638. $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
  639. $$->set_location_range(@1, @3);
  640. }
  641. | shift_expression RIGHT_OP additive_expression
  642. {
  643. void *ctx = state;
  644. $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
  645. $$->set_location_range(@1, @3);
  646. }
  647. ;
  648. relational_expression:
  649. shift_expression
  650. | relational_expression '<' shift_expression
  651. {
  652. void *ctx = state;
  653. $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
  654. $$->set_location_range(@1, @3);
  655. }
  656. | relational_expression '>' shift_expression
  657. {
  658. void *ctx = state;
  659. $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
  660. $$->set_location_range(@1, @3);
  661. }
  662. | relational_expression LE_OP shift_expression
  663. {
  664. void *ctx = state;
  665. $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
  666. $$->set_location_range(@1, @3);
  667. }
  668. | relational_expression GE_OP shift_expression
  669. {
  670. void *ctx = state;
  671. $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
  672. $$->set_location_range(@1, @3);
  673. }
  674. ;
  675. equality_expression:
  676. relational_expression
  677. | equality_expression EQ_OP relational_expression
  678. {
  679. void *ctx = state;
  680. $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
  681. $$->set_location_range(@1, @3);
  682. }
  683. | equality_expression NE_OP relational_expression
  684. {
  685. void *ctx = state;
  686. $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
  687. $$->set_location_range(@1, @3);
  688. }
  689. ;
  690. and_expression:
  691. equality_expression
  692. | and_expression '&' equality_expression
  693. {
  694. void *ctx = state;
  695. $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
  696. $$->set_location_range(@1, @3);
  697. }
  698. ;
  699. exclusive_or_expression:
  700. and_expression
  701. | exclusive_or_expression '^' and_expression
  702. {
  703. void *ctx = state;
  704. $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
  705. $$->set_location_range(@1, @3);
  706. }
  707. ;
  708. inclusive_or_expression:
  709. exclusive_or_expression
  710. | inclusive_or_expression '|' exclusive_or_expression
  711. {
  712. void *ctx = state;
  713. $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
  714. $$->set_location_range(@1, @3);
  715. }
  716. ;
  717. logical_and_expression:
  718. inclusive_or_expression
  719. | logical_and_expression AND_OP inclusive_or_expression
  720. {
  721. void *ctx = state;
  722. $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
  723. $$->set_location_range(@1, @3);
  724. }
  725. ;
  726. logical_xor_expression:
  727. logical_and_expression
  728. | logical_xor_expression XOR_OP logical_and_expression
  729. {
  730. void *ctx = state;
  731. $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
  732. $$->set_location_range(@1, @3);
  733. }
  734. ;
  735. logical_or_expression:
  736. logical_xor_expression
  737. | logical_or_expression OR_OP logical_xor_expression
  738. {
  739. void *ctx = state;
  740. $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
  741. $$->set_location_range(@1, @3);
  742. }
  743. ;
  744. conditional_expression:
  745. logical_or_expression
  746. | logical_or_expression '?' expression ':' assignment_expression
  747. {
  748. void *ctx = state;
  749. $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
  750. $$->set_location_range(@1, @5);
  751. }
  752. ;
  753. assignment_expression:
  754. conditional_expression
  755. | unary_expression assignment_operator assignment_expression
  756. {
  757. void *ctx = state;
  758. $$ = new(ctx) ast_expression($2, $1, $3, NULL);
  759. $$->set_location_range(@1, @3);
  760. }
  761. ;
  762. assignment_operator:
  763. '=' { $$ = ast_assign; }
  764. | MUL_ASSIGN { $$ = ast_mul_assign; }
  765. | DIV_ASSIGN { $$ = ast_div_assign; }
  766. | MOD_ASSIGN { $$ = ast_mod_assign; }
  767. | ADD_ASSIGN { $$ = ast_add_assign; }
  768. | SUB_ASSIGN { $$ = ast_sub_assign; }
  769. | LEFT_ASSIGN { $$ = ast_ls_assign; }
  770. | RIGHT_ASSIGN { $$ = ast_rs_assign; }
  771. | AND_ASSIGN { $$ = ast_and_assign; }
  772. | XOR_ASSIGN { $$ = ast_xor_assign; }
  773. | OR_ASSIGN { $$ = ast_or_assign; }
  774. ;
  775. expression:
  776. assignment_expression
  777. {
  778. $$ = $1;
  779. }
  780. | expression ',' assignment_expression
  781. {
  782. void *ctx = state;
  783. if ($1->oper != ast_sequence) {
  784. $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
  785. $$->set_location_range(@1, @3);
  786. $$->expressions.push_tail(& $1->link);
  787. } else {
  788. $$ = $1;
  789. }
  790. $$->expressions.push_tail(& $3->link);
  791. }
  792. ;
  793. constant_expression:
  794. conditional_expression
  795. ;
  796. declaration:
  797. function_prototype ';'
  798. {
  799. state->symbols->pop_scope();
  800. $$ = $1;
  801. }
  802. | init_declarator_list ';'
  803. {
  804. $$ = $1;
  805. }
  806. | PRECISION precision_qualifier type_specifier ';'
  807. {
  808. $3->default_precision = $2;
  809. $$ = $3;
  810. }
  811. | interface_block
  812. {
  813. $$ = $1;
  814. }
  815. ;
  816. function_prototype:
  817. function_declarator ')'
  818. ;
  819. function_declarator:
  820. function_header
  821. | function_header_with_parameters
  822. ;
  823. function_header_with_parameters:
  824. function_header parameter_declaration
  825. {
  826. $$ = $1;
  827. $$->parameters.push_tail(& $2->link);
  828. }
  829. | function_header_with_parameters ',' parameter_declaration
  830. {
  831. $$ = $1;
  832. $$->parameters.push_tail(& $3->link);
  833. }
  834. ;
  835. function_header:
  836. fully_specified_type variable_identifier '('
  837. {
  838. void *ctx = state;
  839. $$ = new(ctx) ast_function();
  840. $$->set_location(@2);
  841. $$->return_type = $1;
  842. $$->identifier = $2;
  843. state->symbols->add_function(new(state) ir_function($2));
  844. state->symbols->push_scope();
  845. }
  846. ;
  847. parameter_declarator:
  848. type_specifier any_identifier
  849. {
  850. void *ctx = state;
  851. $$ = new(ctx) ast_parameter_declarator();
  852. $$->set_location_range(@1, @2);
  853. $$->type = new(ctx) ast_fully_specified_type();
  854. $$->type->set_location(@1);
  855. $$->type->specifier = $1;
  856. $$->identifier = $2;
  857. }
  858. | type_specifier any_identifier array_specifier
  859. {
  860. void *ctx = state;
  861. $$ = new(ctx) ast_parameter_declarator();
  862. $$->set_location_range(@1, @3);
  863. $$->type = new(ctx) ast_fully_specified_type();
  864. $$->type->set_location(@1);
  865. $$->type->specifier = $1;
  866. $$->identifier = $2;
  867. $$->array_specifier = $3;
  868. }
  869. ;
  870. parameter_declaration:
  871. parameter_qualifier parameter_declarator
  872. {
  873. $$ = $2;
  874. $$->type->qualifier = $1;
  875. }
  876. | parameter_qualifier parameter_type_specifier
  877. {
  878. void *ctx = state;
  879. $$ = new(ctx) ast_parameter_declarator();
  880. $$->set_location(@2);
  881. $$->type = new(ctx) ast_fully_specified_type();
  882. $$->type->set_location_range(@1, @2);
  883. $$->type->qualifier = $1;
  884. $$->type->specifier = $2;
  885. }
  886. ;
  887. parameter_qualifier:
  888. /* empty */
  889. {
  890. memset(& $$, 0, sizeof($$));
  891. }
  892. | CONST_TOK parameter_qualifier
  893. {
  894. if ($2.flags.q.constant)
  895. _mesa_glsl_error(&@1, state, "duplicate const qualifier");
  896. $$ = $2;
  897. $$.flags.q.constant = 1;
  898. }
  899. | PRECISE parameter_qualifier
  900. {
  901. if ($2.flags.q.precise)
  902. _mesa_glsl_error(&@1, state, "duplicate precise qualifier");
  903. $$ = $2;
  904. $$.flags.q.precise = 1;
  905. }
  906. | parameter_direction_qualifier parameter_qualifier
  907. {
  908. if (($1.flags.q.in || $1.flags.q.out) && ($2.flags.q.in || $2.flags.q.out))
  909. _mesa_glsl_error(&@1, state, "duplicate in/out/inout qualifier");
  910. if (!state->ARB_shading_language_420pack_enable && $2.flags.q.constant)
  911. _mesa_glsl_error(&@1, state, "in/out/inout must come after const "
  912. "or precise");
  913. $$ = $1;
  914. $$.merge_qualifier(&@1, state, $2);
  915. }
  916. | precision_qualifier parameter_qualifier
  917. {
  918. if ($2.precision != ast_precision_none)
  919. _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
  920. if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
  921. _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
  922. $$ = $2;
  923. $$.precision = $1;
  924. }
  925. | memory_qualifier parameter_qualifier
  926. {
  927. $$ = $1;
  928. $$.merge_qualifier(&@1, state, $2);
  929. }
  930. parameter_direction_qualifier:
  931. IN_TOK
  932. {
  933. memset(& $$, 0, sizeof($$));
  934. $$.flags.q.in = 1;
  935. }
  936. | OUT_TOK
  937. {
  938. memset(& $$, 0, sizeof($$));
  939. $$.flags.q.out = 1;
  940. }
  941. | INOUT_TOK
  942. {
  943. memset(& $$, 0, sizeof($$));
  944. $$.flags.q.in = 1;
  945. $$.flags.q.out = 1;
  946. }
  947. ;
  948. parameter_type_specifier:
  949. type_specifier
  950. ;
  951. init_declarator_list:
  952. single_declaration
  953. | init_declarator_list ',' any_identifier
  954. {
  955. void *ctx = state;
  956. ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
  957. decl->set_location(@3);
  958. $$ = $1;
  959. $$->declarations.push_tail(&decl->link);
  960. state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
  961. }
  962. | init_declarator_list ',' any_identifier array_specifier
  963. {
  964. void *ctx = state;
  965. ast_declaration *decl = new(ctx) ast_declaration($3, $4, NULL);
  966. decl->set_location_range(@3, @4);
  967. $$ = $1;
  968. $$->declarations.push_tail(&decl->link);
  969. state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
  970. }
  971. | init_declarator_list ',' any_identifier array_specifier '=' initializer
  972. {
  973. void *ctx = state;
  974. ast_declaration *decl = new(ctx) ast_declaration($3, $4, $6);
  975. decl->set_location_range(@3, @4);
  976. $$ = $1;
  977. $$->declarations.push_tail(&decl->link);
  978. state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
  979. }
  980. | init_declarator_list ',' any_identifier '=' initializer
  981. {
  982. void *ctx = state;
  983. ast_declaration *decl = new(ctx) ast_declaration($3, NULL, $5);
  984. decl->set_location(@3);
  985. $$ = $1;
  986. $$->declarations.push_tail(&decl->link);
  987. state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
  988. }
  989. ;
  990. // Grammar Note: No 'enum', or 'typedef'.
  991. single_declaration:
  992. fully_specified_type
  993. {
  994. void *ctx = state;
  995. /* Empty declaration list is valid. */
  996. $$ = new(ctx) ast_declarator_list($1);
  997. $$->set_location(@1);
  998. }
  999. | fully_specified_type any_identifier
  1000. {
  1001. void *ctx = state;
  1002. ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
  1003. decl->set_location(@2);
  1004. $$ = new(ctx) ast_declarator_list($1);
  1005. $$->set_location_range(@1, @2);
  1006. $$->declarations.push_tail(&decl->link);
  1007. }
  1008. | fully_specified_type any_identifier array_specifier
  1009. {
  1010. void *ctx = state;
  1011. ast_declaration *decl = new(ctx) ast_declaration($2, $3, NULL);
  1012. decl->set_location_range(@2, @3);
  1013. $$ = new(ctx) ast_declarator_list($1);
  1014. $$->set_location_range(@1, @3);
  1015. $$->declarations.push_tail(&decl->link);
  1016. }
  1017. | fully_specified_type any_identifier array_specifier '=' initializer
  1018. {
  1019. void *ctx = state;
  1020. ast_declaration *decl = new(ctx) ast_declaration($2, $3, $5);
  1021. decl->set_location_range(@2, @3);
  1022. $$ = new(ctx) ast_declarator_list($1);
  1023. $$->set_location_range(@1, @3);
  1024. $$->declarations.push_tail(&decl->link);
  1025. }
  1026. | fully_specified_type any_identifier '=' initializer
  1027. {
  1028. void *ctx = state;
  1029. ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
  1030. decl->set_location(@2);
  1031. $$ = new(ctx) ast_declarator_list($1);
  1032. $$->set_location_range(@1, @2);
  1033. $$->declarations.push_tail(&decl->link);
  1034. }
  1035. | INVARIANT variable_identifier
  1036. {
  1037. void *ctx = state;
  1038. ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
  1039. decl->set_location(@2);
  1040. $$ = new(ctx) ast_declarator_list(NULL);
  1041. $$->set_location_range(@1, @2);
  1042. $$->invariant = true;
  1043. $$->declarations.push_tail(&decl->link);
  1044. }
  1045. | PRECISE variable_identifier
  1046. {
  1047. void *ctx = state;
  1048. ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
  1049. decl->set_location(@2);
  1050. $$ = new(ctx) ast_declarator_list(NULL);
  1051. $$->set_location_range(@1, @2);
  1052. $$->precise = true;
  1053. $$->declarations.push_tail(&decl->link);
  1054. }
  1055. ;
  1056. fully_specified_type:
  1057. type_specifier
  1058. {
  1059. void *ctx = state;
  1060. $$ = new(ctx) ast_fully_specified_type();
  1061. $$->set_location(@1);
  1062. $$->specifier = $1;
  1063. }
  1064. | type_qualifier type_specifier
  1065. {
  1066. void *ctx = state;
  1067. $$ = new(ctx) ast_fully_specified_type();
  1068. $$->set_location_range(@1, @2);
  1069. $$->qualifier = $1;
  1070. $$->specifier = $2;
  1071. }
  1072. ;
  1073. layout_qualifier:
  1074. LAYOUT_TOK '(' layout_qualifier_id_list ')'
  1075. {
  1076. $$ = $3;
  1077. }
  1078. ;
  1079. layout_qualifier_id_list:
  1080. layout_qualifier_id
  1081. | layout_qualifier_id_list ',' layout_qualifier_id
  1082. {
  1083. $$ = $1;
  1084. if (!$$.merge_qualifier(& @3, state, $3)) {
  1085. YYERROR;
  1086. }
  1087. }
  1088. ;
  1089. integer_constant:
  1090. INTCONSTANT { $$ = $1; }
  1091. | UINTCONSTANT { $$ = $1; }
  1092. ;
  1093. layout_qualifier_id:
  1094. any_identifier
  1095. {
  1096. memset(& $$, 0, sizeof($$));
  1097. /* Layout qualifiers for ARB_fragment_coord_conventions. */
  1098. if (!$$.flags.i && (state->ARB_fragment_coord_conventions_enable ||
  1099. state->is_version(150, 0))) {
  1100. if (match_layout_qualifier($1, "origin_upper_left", state) == 0) {
  1101. $$.flags.q.origin_upper_left = 1;
  1102. } else if (match_layout_qualifier($1, "pixel_center_integer",
  1103. state) == 0) {
  1104. $$.flags.q.pixel_center_integer = 1;
  1105. }
  1106. if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
  1107. _mesa_glsl_warning(& @1, state,
  1108. "GL_ARB_fragment_coord_conventions layout "
  1109. "identifier `%s' used", $1);
  1110. }
  1111. }
  1112. /* Layout qualifiers for AMD/ARB_conservative_depth. */
  1113. if (!$$.flags.i &&
  1114. (state->AMD_conservative_depth_enable ||
  1115. state->ARB_conservative_depth_enable)) {
  1116. if (match_layout_qualifier($1, "depth_any", state) == 0) {
  1117. $$.flags.q.depth_any = 1;
  1118. } else if (match_layout_qualifier($1, "depth_greater", state) == 0) {
  1119. $$.flags.q.depth_greater = 1;
  1120. } else if (match_layout_qualifier($1, "depth_less", state) == 0) {
  1121. $$.flags.q.depth_less = 1;
  1122. } else if (match_layout_qualifier($1, "depth_unchanged",
  1123. state) == 0) {
  1124. $$.flags.q.depth_unchanged = 1;
  1125. }
  1126. if ($$.flags.i && state->AMD_conservative_depth_warn) {
  1127. _mesa_glsl_warning(& @1, state,
  1128. "GL_AMD_conservative_depth "
  1129. "layout qualifier `%s' is used", $1);
  1130. }
  1131. if ($$.flags.i && state->ARB_conservative_depth_warn) {
  1132. _mesa_glsl_warning(& @1, state,
  1133. "GL_ARB_conservative_depth "
  1134. "layout qualifier `%s' is used", $1);
  1135. }
  1136. }
  1137. /* See also interface_block_layout_qualifier. */
  1138. if (!$$.flags.i && state->has_uniform_buffer_objects()) {
  1139. if (match_layout_qualifier($1, "std140", state) == 0) {
  1140. $$.flags.q.std140 = 1;
  1141. } else if (match_layout_qualifier($1, "shared", state) == 0) {
  1142. $$.flags.q.shared = 1;
  1143. } else if (match_layout_qualifier($1, "column_major", state) == 0) {
  1144. $$.flags.q.column_major = 1;
  1145. /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
  1146. * below in the interface_block_layout_qualifier rule.
  1147. *
  1148. * It is not a reserved word in GLSL ES 3.00, so it's handled here as
  1149. * an identifier.
  1150. *
  1151. * Also, this takes care of alternate capitalizations of
  1152. * "row_major" (which is necessary because layout qualifiers
  1153. * are case-insensitive in desktop GLSL).
  1154. */
  1155. } else if (match_layout_qualifier($1, "row_major", state) == 0) {
  1156. $$.flags.q.row_major = 1;
  1157. /* "packed" is a reserved word in GLSL, and its token is
  1158. * parsed below in the interface_block_layout_qualifier rule.
  1159. * However, we must take care of alternate capitalizations of
  1160. * "packed", because layout qualifiers are case-insensitive
  1161. * in desktop GLSL.
  1162. */
  1163. } else if (match_layout_qualifier($1, "packed", state) == 0) {
  1164. $$.flags.q.packed = 1;
  1165. }
  1166. if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
  1167. _mesa_glsl_warning(& @1, state,
  1168. "#version 140 / GL_ARB_uniform_buffer_object "
  1169. "layout qualifier `%s' is used", $1);
  1170. }
  1171. }
  1172. /* Layout qualifiers for GLSL 1.50 geometry shaders. */
  1173. if (!$$.flags.i) {
  1174. static const struct {
  1175. const char *s;
  1176. GLenum e;
  1177. } map[] = {
  1178. { "points", GL_POINTS },
  1179. { "lines", GL_LINES },
  1180. { "lines_adjacency", GL_LINES_ADJACENCY },
  1181. { "line_strip", GL_LINE_STRIP },
  1182. { "triangles", GL_TRIANGLES },
  1183. { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
  1184. { "triangle_strip", GL_TRIANGLE_STRIP },
  1185. };
  1186. for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
  1187. if (match_layout_qualifier($1, map[i].s, state) == 0) {
  1188. $$.flags.q.prim_type = 1;
  1189. $$.prim_type = map[i].e;
  1190. break;
  1191. }
  1192. }
  1193. if ($$.flags.i && !state->is_version(150, 0)) {
  1194. _mesa_glsl_error(& @1, state, "#version 150 layout "
  1195. "qualifier `%s' used", $1);
  1196. }
  1197. }
  1198. /* Layout qualifiers for ARB_shader_image_load_store. */
  1199. if (state->ARB_shader_image_load_store_enable ||
  1200. state->is_version(420, 0)) {
  1201. if (!$$.flags.i) {
  1202. static const struct {
  1203. const char *name;
  1204. GLenum format;
  1205. glsl_base_type base_type;
  1206. } map[] = {
  1207. { "rgba32f", GL_RGBA32F, GLSL_TYPE_FLOAT },
  1208. { "rgba16f", GL_RGBA16F, GLSL_TYPE_FLOAT },
  1209. { "rg32f", GL_RG32F, GLSL_TYPE_FLOAT },
  1210. { "rg16f", GL_RG16F, GLSL_TYPE_FLOAT },
  1211. { "r11f_g11f_b10f", GL_R11F_G11F_B10F, GLSL_TYPE_FLOAT },
  1212. { "r32f", GL_R32F, GLSL_TYPE_FLOAT },
  1213. { "r16f", GL_R16F, GLSL_TYPE_FLOAT },
  1214. { "rgba32ui", GL_RGBA32UI, GLSL_TYPE_UINT },
  1215. { "rgba16ui", GL_RGBA16UI, GLSL_TYPE_UINT },
  1216. { "rgb10_a2ui", GL_RGB10_A2UI, GLSL_TYPE_UINT },
  1217. { "rgba8ui", GL_RGBA8UI, GLSL_TYPE_UINT },
  1218. { "rg32ui", GL_RG32UI, GLSL_TYPE_UINT },
  1219. { "rg16ui", GL_RG16UI, GLSL_TYPE_UINT },
  1220. { "rg8ui", GL_RG8UI, GLSL_TYPE_UINT },
  1221. { "r32ui", GL_R32UI, GLSL_TYPE_UINT },
  1222. { "r16ui", GL_R16UI, GLSL_TYPE_UINT },
  1223. { "r8ui", GL_R8UI, GLSL_TYPE_UINT },
  1224. { "rgba32i", GL_RGBA32I, GLSL_TYPE_INT },
  1225. { "rgba16i", GL_RGBA16I, GLSL_TYPE_INT },
  1226. { "rgba8i", GL_RGBA8I, GLSL_TYPE_INT },
  1227. { "rg32i", GL_RG32I, GLSL_TYPE_INT },
  1228. { "rg16i", GL_RG16I, GLSL_TYPE_INT },
  1229. { "rg8i", GL_RG8I, GLSL_TYPE_INT },
  1230. { "r32i", GL_R32I, GLSL_TYPE_INT },
  1231. { "r16i", GL_R16I, GLSL_TYPE_INT },
  1232. { "r8i", GL_R8I, GLSL_TYPE_INT },
  1233. { "rgba16", GL_RGBA16, GLSL_TYPE_FLOAT },
  1234. { "rgb10_a2", GL_RGB10_A2, GLSL_TYPE_FLOAT },
  1235. { "rgba8", GL_RGBA8, GLSL_TYPE_FLOAT },
  1236. { "rg16", GL_RG16, GLSL_TYPE_FLOAT },
  1237. { "rg8", GL_RG8, GLSL_TYPE_FLOAT },
  1238. { "r16", GL_R16, GLSL_TYPE_FLOAT },
  1239. { "r8", GL_R8, GLSL_TYPE_FLOAT },
  1240. { "rgba16_snorm", GL_RGBA16_SNORM, GLSL_TYPE_FLOAT },
  1241. { "rgba8_snorm", GL_RGBA8_SNORM, GLSL_TYPE_FLOAT },
  1242. { "rg16_snorm", GL_RG16_SNORM, GLSL_TYPE_FLOAT },
  1243. { "rg8_snorm", GL_RG8_SNORM, GLSL_TYPE_FLOAT },
  1244. { "r16_snorm", GL_R16_SNORM, GLSL_TYPE_FLOAT },
  1245. { "r8_snorm", GL_R8_SNORM, GLSL_TYPE_FLOAT }
  1246. };
  1247. for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
  1248. if (match_layout_qualifier($1, map[i].name, state) == 0) {
  1249. $$.flags.q.explicit_image_format = 1;
  1250. $$.image_format = map[i].format;
  1251. $$.image_base_type = map[i].base_type;
  1252. break;
  1253. }
  1254. }
  1255. }
  1256. if (!$$.flags.i &&
  1257. match_layout_qualifier($1, "early_fragment_tests", state) == 0) {
  1258. /* From section 4.4.1.3 of the GLSL 4.50 specification
  1259. * (Fragment Shader Inputs):
  1260. *
  1261. * "Fragment shaders also allow the following layout
  1262. * qualifier on in only (not with variable declarations)
  1263. * layout-qualifier-id
  1264. * early_fragment_tests
  1265. * [...]"
  1266. */
  1267. if (state->stage != MESA_SHADER_FRAGMENT) {
  1268. _mesa_glsl_error(& @1, state,
  1269. "early_fragment_tests layout qualifier only "
  1270. "valid in fragment shaders");
  1271. }
  1272. $$.flags.q.early_fragment_tests = 1;
  1273. }
  1274. }
  1275. /* Layout qualifiers for tessellation evaluation shaders. */
  1276. if (!$$.flags.i) {
  1277. struct {
  1278. const char *s;
  1279. GLenum e;
  1280. } map[] = {
  1281. /* triangles already parsed by gs-specific code */
  1282. { "quads", GL_QUADS },
  1283. { "isolines", GL_ISOLINES },
  1284. };
  1285. for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
  1286. if (match_layout_qualifier($1, map[i].s, state) == 0) {
  1287. $$.flags.q.prim_type = 1;
  1288. $$.prim_type = map[i].e;
  1289. break;
  1290. }
  1291. }
  1292. if ($$.flags.i &&
  1293. !state->ARB_tessellation_shader_enable &&
  1294. !state->is_version(400, 0)) {
  1295. _mesa_glsl_error(& @1, state,
  1296. "primitive mode qualifier `%s' requires "
  1297. "GLSL 4.00 or ARB_tessellation_shader", $1);
  1298. }
  1299. }
  1300. if (!$$.flags.i) {
  1301. struct {
  1302. const char *s;
  1303. GLenum e;
  1304. } map[] = {
  1305. { "equal_spacing", GL_EQUAL },
  1306. { "fractional_odd_spacing", GL_FRACTIONAL_ODD },
  1307. { "fractional_even_spacing", GL_FRACTIONAL_EVEN },
  1308. };
  1309. for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
  1310. if (match_layout_qualifier($1, map[i].s, state) == 0) {
  1311. $$.flags.q.vertex_spacing = 1;
  1312. $$.vertex_spacing = map[i].e;
  1313. break;
  1314. }
  1315. }
  1316. if ($$.flags.i &&
  1317. !state->ARB_tessellation_shader_enable &&
  1318. !state->is_version(400, 0)) {
  1319. _mesa_glsl_error(& @1, state,
  1320. "vertex spacing qualifier `%s' requires "
  1321. "GLSL 4.00 or ARB_tessellation_shader", $1);
  1322. }
  1323. }
  1324. if (!$$.flags.i) {
  1325. if (match_layout_qualifier($1, "cw", state) == 0) {
  1326. $$.flags.q.ordering = 1;
  1327. $$.ordering = GL_CW;
  1328. } else if (match_layout_qualifier($1, "ccw", state) == 0) {
  1329. $$.flags.q.ordering = 1;
  1330. $$.ordering = GL_CCW;
  1331. }
  1332. if ($$.flags.i &&
  1333. !state->ARB_tessellation_shader_enable &&
  1334. !state->is_version(400, 0)) {
  1335. _mesa_glsl_error(& @1, state,
  1336. "ordering qualifier `%s' requires "
  1337. "GLSL 4.00 or ARB_tessellation_shader", $1);
  1338. }
  1339. }
  1340. if (!$$.flags.i) {
  1341. if (match_layout_qualifier($1, "point_mode", state) == 0) {
  1342. $$.flags.q.point_mode = 1;
  1343. $$.point_mode = true;
  1344. }
  1345. if ($$.flags.i &&
  1346. !state->ARB_tessellation_shader_enable &&
  1347. !state->is_version(400, 0)) {
  1348. _mesa_glsl_error(& @1, state,
  1349. "qualifier `point_mode' requires "
  1350. "GLSL 4.00 or ARB_tessellation_shader");
  1351. }
  1352. }
  1353. if (!$$.flags.i) {
  1354. _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
  1355. "`%s'", $1);
  1356. YYERROR;
  1357. }
  1358. }
  1359. | any_identifier '=' integer_constant
  1360. {
  1361. memset(& $$, 0, sizeof($$));
  1362. if (match_layout_qualifier("location", $1, state) == 0) {
  1363. $$.flags.q.explicit_location = 1;
  1364. if ($$.flags.q.attribute == 1 &&
  1365. state->ARB_explicit_attrib_location_warn) {
  1366. _mesa_glsl_warning(& @1, state,
  1367. "GL_ARB_explicit_attrib_location layout "
  1368. "identifier `%s' used", $1);
  1369. }
  1370. if ($3 >= 0) {
  1371. $$.location = $3;
  1372. } else {
  1373. _mesa_glsl_error(& @3, state, "invalid location %d specified", $3);
  1374. YYERROR;
  1375. }
  1376. }
  1377. if (match_layout_qualifier("index", $1, state) == 0) {
  1378. $$.flags.q.explicit_index = 1;
  1379. if ($3 >= 0) {
  1380. $$.index = $3;
  1381. } else {
  1382. _mesa_glsl_error(& @3, state, "invalid index %d specified", $3);
  1383. YYERROR;
  1384. }
  1385. }
  1386. if ((state->ARB_shading_language_420pack_enable ||
  1387. state->has_atomic_counters() ||
  1388. state->ARB_shader_storage_buffer_object_enable) &&
  1389. match_layout_qualifier("binding", $1, state) == 0) {
  1390. $$.flags.q.explicit_binding = 1;
  1391. $$.binding = $3;
  1392. }
  1393. if (state->has_atomic_counters() &&
  1394. match_layout_qualifier("offset", $1, state) == 0) {
  1395. $$.flags.q.explicit_offset = 1;
  1396. $$.offset = $3;
  1397. }
  1398. if (match_layout_qualifier("max_vertices", $1, state) == 0) {
  1399. $$.flags.q.max_vertices = 1;
  1400. if ($3 < 0) {
  1401. _mesa_glsl_error(& @3, state,
  1402. "invalid max_vertices %d specified", $3);
  1403. YYERROR;
  1404. } else {
  1405. $$.max_vertices = $3;
  1406. if (!state->is_version(150, 0)) {
  1407. _mesa_glsl_error(& @3, state,
  1408. "#version 150 max_vertices qualifier "
  1409. "specified", $3);
  1410. }
  1411. }
  1412. }
  1413. if (state->stage == MESA_SHADER_GEOMETRY) {
  1414. if (match_layout_qualifier("stream", $1, state) == 0 &&
  1415. state->check_explicit_attrib_stream_allowed(& @3)) {
  1416. $$.flags.q.stream = 1;
  1417. if ($3 < 0) {
  1418. _mesa_glsl_error(& @3, state,
  1419. "invalid stream %d specified", $3);
  1420. YYERROR;
  1421. } else {
  1422. $$.flags.q.explicit_stream = 1;
  1423. $$.stream = $3;
  1424. }
  1425. }
  1426. }
  1427. static const char * const local_size_qualifiers[3] = {
  1428. "local_size_x",
  1429. "local_size_y",
  1430. "local_size_z",
  1431. };
  1432. for (int i = 0; i < 3; i++) {
  1433. if (match_layout_qualifier(local_size_qualifiers[i], $1,
  1434. state) == 0) {
  1435. if ($3 <= 0) {
  1436. _mesa_glsl_error(& @3, state,
  1437. "invalid %s of %d specified",
  1438. local_size_qualifiers[i], $3);
  1439. YYERROR;
  1440. } else if (!state->is_version(430, 0) &&
  1441. !state->ARB_compute_shader_enable) {
  1442. _mesa_glsl_error(& @3, state,
  1443. "%s qualifier requires GLSL 4.30 or "
  1444. "ARB_compute_shader",
  1445. local_size_qualifiers[i]);
  1446. YYERROR;
  1447. } else {
  1448. $$.flags.q.local_size |= (1 << i);
  1449. $$.local_size[i] = $3;
  1450. }
  1451. break;
  1452. }
  1453. }
  1454. if (match_layout_qualifier("invocations", $1, state) == 0) {
  1455. $$.flags.q.invocations = 1;
  1456. if ($3 <= 0) {
  1457. _mesa_glsl_error(& @3, state,
  1458. "invalid invocations %d specified", $3);
  1459. YYERROR;
  1460. } else if ($3 > MAX_GEOMETRY_SHADER_INVOCATIONS) {
  1461. _mesa_glsl_error(& @3, state,
  1462. "invocations (%d) exceeds "
  1463. "GL_MAX_GEOMETRY_SHADER_INVOCATIONS", $3);
  1464. YYERROR;
  1465. } else {
  1466. $$.invocations = $3;
  1467. if (!state->is_version(400, 0) &&
  1468. !state->ARB_gpu_shader5_enable) {
  1469. _mesa_glsl_error(& @3, state,
  1470. "GL_ARB_gpu_shader5 invocations "
  1471. "qualifier specified", $3);
  1472. }
  1473. }
  1474. }
  1475. /* Layout qualifiers for tessellation control shaders. */
  1476. if (match_layout_qualifier("vertices", $1, state) == 0) {
  1477. $$.flags.q.vertices = 1;
  1478. if ($3 <= 0) {
  1479. _mesa_glsl_error(& @3, state,
  1480. "invalid vertices (%d) specified", $3);
  1481. YYERROR;
  1482. } else if ($3 > (int)state->Const.MaxPatchVertices) {
  1483. _mesa_glsl_error(& @3, state,
  1484. "vertices (%d) exceeds "
  1485. "GL_MAX_PATCH_VERTICES", $3);
  1486. YYERROR;
  1487. } else {
  1488. $$.vertices = $3;
  1489. if (!state->ARB_tessellation_shader_enable &&
  1490. !state->is_version(400, 0)) {
  1491. _mesa_glsl_error(& @1, state,
  1492. "vertices qualifier requires GLSL 4.00 or "
  1493. "ARB_tessellation_shader");
  1494. }
  1495. }
  1496. }
  1497. /* If the identifier didn't match any known layout identifiers,
  1498. * emit an error.
  1499. */
  1500. if (!$$.flags.i) {
  1501. _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
  1502. "`%s'", $1);
  1503. YYERROR;
  1504. }
  1505. }
  1506. | interface_block_layout_qualifier
  1507. {
  1508. $$ = $1;
  1509. /* Layout qualifiers for ARB_uniform_buffer_object. */
  1510. if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
  1511. _mesa_glsl_error(& @1, state,
  1512. "#version 140 / GL_ARB_uniform_buffer_object "
  1513. "layout qualifier `%s' is used", $1);
  1514. } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
  1515. _mesa_glsl_warning(& @1, state,
  1516. "#version 140 / GL_ARB_uniform_buffer_object "
  1517. "layout qualifier `%s' is used", $1);
  1518. }
  1519. }
  1520. ;
  1521. /* This is a separate language rule because we parse these as tokens
  1522. * (due to them being reserved keywords) instead of identifiers like
  1523. * most qualifiers. See the any_identifier path of
  1524. * layout_qualifier_id for the others.
  1525. *
  1526. * Note that since layout qualifiers are case-insensitive in desktop
  1527. * GLSL, all of these qualifiers need to be handled as identifiers as
  1528. * well (by the any_identifier path of layout_qualifier_id).
  1529. */
  1530. interface_block_layout_qualifier:
  1531. ROW_MAJOR
  1532. {
  1533. memset(& $$, 0, sizeof($$));
  1534. $$.flags.q.row_major = 1;
  1535. }
  1536. | PACKED_TOK
  1537. {
  1538. memset(& $$, 0, sizeof($$));
  1539. $$.flags.q.packed = 1;
  1540. }
  1541. ;
  1542. interpolation_qualifier:
  1543. SMOOTH
  1544. {
  1545. memset(& $$, 0, sizeof($$));
  1546. $$.flags.q.smooth = 1;
  1547. }
  1548. | FLAT
  1549. {
  1550. memset(& $$, 0, sizeof($$));
  1551. $$.flags.q.flat = 1;
  1552. }
  1553. | NOPERSPECTIVE
  1554. {
  1555. memset(& $$, 0, sizeof($$));
  1556. $$.flags.q.noperspective = 1;
  1557. }
  1558. ;
  1559. type_qualifier:
  1560. /* Single qualifiers */
  1561. INVARIANT
  1562. {
  1563. memset(& $$, 0, sizeof($$));
  1564. $$.flags.q.invariant = 1;
  1565. }
  1566. | PRECISE
  1567. {
  1568. memset(& $$, 0, sizeof($$));
  1569. $$.flags.q.precise = 1;
  1570. }
  1571. | auxiliary_storage_qualifier
  1572. | storage_qualifier
  1573. | interpolation_qualifier
  1574. | layout_qualifier
  1575. | memory_qualifier
  1576. | precision_qualifier
  1577. {
  1578. memset(&$$, 0, sizeof($$));
  1579. $$.precision = $1;
  1580. }
  1581. /* Multiple qualifiers:
  1582. * In GLSL 4.20, these can be specified in any order. In earlier versions,
  1583. * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
  1584. *
  1585. * invariant interpolation auxiliary storage precision ...or...
  1586. * layout storage precision
  1587. *
  1588. * Each qualifier's rule ensures that the accumulated qualifiers on the right
  1589. * side don't contain any that must appear on the left hand side.
  1590. * For example, when processing a storage qualifier, we check that there are
  1591. * no auxiliary, interpolation, layout, invariant, or precise qualifiers to the right.
  1592. */
  1593. | PRECISE type_qualifier
  1594. {
  1595. if ($2.flags.q.precise)
  1596. _mesa_glsl_error(&@1, state, "duplicate \"precise\" qualifier");
  1597. $$ = $2;
  1598. $$.flags.q.precise = 1;
  1599. }
  1600. | INVARIANT type_qualifier
  1601. {
  1602. if ($2.flags.q.invariant)
  1603. _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
  1604. if (!state->ARB_shading_language_420pack_enable && $2.flags.q.precise)
  1605. _mesa_glsl_error(&@1, state,
  1606. "\"invariant\" must come after \"precise\"");
  1607. $$ = $2;
  1608. $$.flags.q.invariant = 1;
  1609. /* GLSL ES 3.00 spec, section 4.6.1 "The Invariant Qualifier":
  1610. *
  1611. * "Only variables output from a shader can be candidates for invariance.
  1612. * This includes user-defined output variables and the built-in output
  1613. * variables. As only outputs can be declared as invariant, an invariant
  1614. * output from one shader stage will still match an input of a subsequent
  1615. * stage without the input being declared as invariant."
  1616. */
  1617. if (state->es_shader && state->language_version >= 300 && $$.flags.q.in)
  1618. _mesa_glsl_error(&@1, state, "invariant qualifiers cannot be used with shader inputs");
  1619. }
  1620. | interpolation_qualifier type_qualifier
  1621. {
  1622. /* Section 4.3 of the GLSL 1.40 specification states:
  1623. * "...qualified with one of these interpolation qualifiers"
  1624. *
  1625. * GLSL 1.30 claims to allow "one or more", but insists that:
  1626. * "These interpolation qualifiers may only precede the qualifiers in,
  1627. * centroid in, out, or centroid out in a declaration."
  1628. *
  1629. * ...which means that e.g. smooth can't precede smooth, so there can be
  1630. * only one after all, and the 1.40 text is a clarification, not a change.
  1631. */
  1632. if ($2.has_interpolation())
  1633. _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
  1634. if (!state->ARB_shading_language_420pack_enable &&
  1635. ($2.flags.q.precise || $2.flags.q.invariant)) {
  1636. _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
  1637. "after \"precise\" or \"invariant\"");
  1638. }
  1639. $$ = $1;
  1640. $$.merge_qualifier(&@1, state, $2);
  1641. }
  1642. | layout_qualifier type_qualifier
  1643. {
  1644. /* In the absence of ARB_shading_language_420pack, layout qualifiers may
  1645. * appear no later than auxiliary storage qualifiers. There is no
  1646. * particularly clear spec language mandating this, but in all examples
  1647. * the layout qualifier precedes the storage qualifier.
  1648. *
  1649. * We allow combinations of layout with interpolation, invariant or
  1650. * precise qualifiers since these are useful in ARB_separate_shader_objects.
  1651. * There is no clear spec guidance on this either.
  1652. */
  1653. if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
  1654. _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
  1655. $$ = $1;
  1656. $$.merge_qualifier(&@1, state, $2);
  1657. }
  1658. | auxiliary_storage_qualifier type_qualifier
  1659. {
  1660. if ($2.has_auxiliary_storage()) {
  1661. _mesa_glsl_error(&@1, state,
  1662. "duplicate auxiliary storage qualifier (centroid or sample)");
  1663. }
  1664. if (!state->ARB_shading_language_420pack_enable &&
  1665. ($2.flags.q.precise || $2.flags.q.invariant ||
  1666. $2.has_interpolation() || $2.has_layout())) {
  1667. _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
  1668. "just before storage qualifiers");
  1669. }
  1670. $$ = $1;
  1671. $$.merge_qualifier(&@1, state, $2);
  1672. }
  1673. | storage_qualifier type_qualifier
  1674. {
  1675. /* Section 4.3 of the GLSL 1.20 specification states:
  1676. * "Variable declarations may have a storage qualifier specified..."
  1677. * 1.30 clarifies this to "may have one storage qualifier".
  1678. */
  1679. if ($2.has_storage())
  1680. _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
  1681. if (!state->ARB_shading_language_420pack_enable &&
  1682. ($2.flags.q.precise || $2.flags.q.invariant || $2.has_interpolation() ||
  1683. $2.has_layout() || $2.has_auxiliary_storage())) {
  1684. _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
  1685. "precise, invariant, interpolation, layout and auxiliary "
  1686. "storage qualifiers");
  1687. }
  1688. $$ = $1;
  1689. $$.merge_qualifier(&@1, state, $2);
  1690. }
  1691. | precision_qualifier type_qualifier
  1692. {
  1693. if ($2.precision != ast_precision_none)
  1694. _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
  1695. if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
  1696. _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
  1697. $$ = $2;
  1698. $$.precision = $1;
  1699. }
  1700. | memory_qualifier type_qualifier
  1701. {
  1702. $$ = $1;
  1703. $$.merge_qualifier(&@1, state, $2);
  1704. }
  1705. ;
  1706. auxiliary_storage_qualifier:
  1707. CENTROID
  1708. {
  1709. memset(& $$, 0, sizeof($$));
  1710. $$.flags.q.centroid = 1;
  1711. }
  1712. | SAMPLE
  1713. {
  1714. memset(& $$, 0, sizeof($$));
  1715. $$.flags.q.sample = 1;
  1716. }
  1717. | PATCH
  1718. {
  1719. memset(& $$, 0, sizeof($$));
  1720. $$.flags.q.patch = 1;
  1721. }
  1722. storage_qualifier:
  1723. CONST_TOK
  1724. {
  1725. memset(& $$, 0, sizeof($$));
  1726. $$.flags.q.constant = 1;
  1727. }
  1728. | ATTRIBUTE
  1729. {
  1730. memset(& $$, 0, sizeof($$));
  1731. $$.flags.q.attribute = 1;
  1732. }
  1733. | VARYING
  1734. {
  1735. memset(& $$, 0, sizeof($$));
  1736. $$.flags.q.varying = 1;
  1737. }
  1738. | IN_TOK
  1739. {
  1740. memset(& $$, 0, sizeof($$));
  1741. $$.flags.q.in = 1;
  1742. }
  1743. | OUT_TOK
  1744. {
  1745. memset(& $$, 0, sizeof($$));
  1746. $$.flags.q.out = 1;
  1747. if (state->stage == MESA_SHADER_GEOMETRY &&
  1748. state->has_explicit_attrib_stream()) {
  1749. /* Section 4.3.8.2 (Output Layout Qualifiers) of the GLSL 4.00
  1750. * spec says:
  1751. *
  1752. * "If the block or variable is declared with the stream
  1753. * identifier, it is associated with the specified stream;
  1754. * otherwise, it is associated with the current default stream."
  1755. */
  1756. $$.flags.q.stream = 1;
  1757. $$.flags.q.explicit_stream = 0;
  1758. $$.stream = state->out_qualifier->stream;
  1759. }
  1760. }
  1761. | UNIFORM
  1762. {
  1763. memset(& $$, 0, sizeof($$));
  1764. $$.flags.q.uniform = 1;
  1765. }
  1766. | BUFFER
  1767. {
  1768. memset(& $$, 0, sizeof($$));
  1769. $$.flags.q.buffer = 1;
  1770. }
  1771. ;
  1772. memory_qualifier:
  1773. COHERENT
  1774. {
  1775. memset(& $$, 0, sizeof($$));
  1776. $$.flags.q.coherent = 1;
  1777. }
  1778. | VOLATILE
  1779. {
  1780. memset(& $$, 0, sizeof($$));
  1781. $$.flags.q._volatile = 1;
  1782. }
  1783. | RESTRICT
  1784. {
  1785. STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
  1786. memset(& $$, 0, sizeof($$));
  1787. $$.flags.q.restrict_flag = 1;
  1788. }
  1789. | READONLY
  1790. {
  1791. memset(& $$, 0, sizeof($$));
  1792. $$.flags.q.read_only = 1;
  1793. }
  1794. | WRITEONLY
  1795. {
  1796. memset(& $$, 0, sizeof($$));
  1797. $$.flags.q.write_only = 1;
  1798. }
  1799. ;
  1800. array_specifier:
  1801. '[' ']'
  1802. {
  1803. void *ctx = state;
  1804. $$ = new(ctx) ast_array_specifier(@1);
  1805. $$->set_location_range(@1, @2);
  1806. }
  1807. | '[' constant_expression ']'
  1808. {
  1809. void *ctx = state;
  1810. $$ = new(ctx) ast_array_specifier(@1, $2);
  1811. $$->set_location_range(@1, @3);
  1812. }
  1813. | array_specifier '[' ']'
  1814. {
  1815. $$ = $1;
  1816. if (!state->ARB_arrays_of_arrays_enable) {
  1817. _mesa_glsl_error(& @1, state,
  1818. "GL_ARB_arrays_of_arrays "
  1819. "required for defining arrays of arrays");
  1820. } else {
  1821. _mesa_glsl_error(& @1, state,
  1822. "only the outermost array dimension can "
  1823. "be unsized");
  1824. }
  1825. }
  1826. | array_specifier '[' constant_expression ']'
  1827. {
  1828. $$ = $1;
  1829. if (!state->ARB_arrays_of_arrays_enable) {
  1830. _mesa_glsl_error(& @1, state,
  1831. "GL_ARB_arrays_of_arrays "
  1832. "required for defining arrays of arrays");
  1833. }
  1834. $$->add_dimension($3);
  1835. }
  1836. ;
  1837. type_specifier:
  1838. type_specifier_nonarray
  1839. | type_specifier_nonarray array_specifier
  1840. {
  1841. $$ = $1;
  1842. $$->array_specifier = $2;
  1843. }
  1844. ;
  1845. type_specifier_nonarray:
  1846. basic_type_specifier_nonarray
  1847. {
  1848. void *ctx = state;
  1849. $$ = new(ctx) ast_type_specifier($1);
  1850. $$->set_location(@1);
  1851. }
  1852. | struct_specifier
  1853. {
  1854. void *ctx = state;
  1855. $$ = new(ctx) ast_type_specifier($1);
  1856. $$->set_location(@1);
  1857. }
  1858. | TYPE_IDENTIFIER
  1859. {
  1860. void *ctx = state;
  1861. $$ = new(ctx) ast_type_specifier($1);
  1862. $$->set_location(@1);
  1863. }
  1864. ;
  1865. basic_type_specifier_nonarray:
  1866. VOID_TOK { $$ = "void"; }
  1867. | FLOAT_TOK { $$ = "float"; }
  1868. | DOUBLE_TOK { $$ = "double"; }
  1869. | INT_TOK { $$ = "int"; }
  1870. | UINT_TOK { $$ = "uint"; }
  1871. | BOOL_TOK { $$ = "bool"; }
  1872. | VEC2 { $$ = "vec2"; }
  1873. | VEC3 { $$ = "vec3"; }
  1874. | VEC4 { $$ = "vec4"; }
  1875. | BVEC2 { $$ = "bvec2"; }
  1876. | BVEC3 { $$ = "bvec3"; }
  1877. | BVEC4 { $$ = "bvec4"; }
  1878. | IVEC2 { $$ = "ivec2"; }
  1879. | IVEC3 { $$ = "ivec3"; }
  1880. | IVEC4 { $$ = "ivec4"; }
  1881. | UVEC2 { $$ = "uvec2"; }
  1882. | UVEC3 { $$ = "uvec3"; }
  1883. | UVEC4 { $$ = "uvec4"; }
  1884. | DVEC2 { $$ = "dvec2"; }
  1885. | DVEC3 { $$ = "dvec3"; }
  1886. | DVEC4 { $$ = "dvec4"; }
  1887. | MAT2X2 { $$ = "mat2"; }
  1888. | MAT2X3 { $$ = "mat2x3"; }
  1889. | MAT2X4 { $$ = "mat2x4"; }
  1890. | MAT3X2 { $$ = "mat3x2"; }
  1891. | MAT3X3 { $$ = "mat3"; }
  1892. | MAT3X4 { $$ = "mat3x4"; }
  1893. | MAT4X2 { $$ = "mat4x2"; }
  1894. | MAT4X3 { $$ = "mat4x3"; }
  1895. | MAT4X4 { $$ = "mat4"; }
  1896. | DMAT2X2 { $$ = "dmat2"; }
  1897. | DMAT2X3 { $$ = "dmat2x3"; }
  1898. | DMAT2X4 { $$ = "dmat2x4"; }
  1899. | DMAT3X2 { $$ = "dmat3x2"; }
  1900. | DMAT3X3 { $$ = "dmat3"; }
  1901. | DMAT3X4 { $$ = "dmat3x4"; }
  1902. | DMAT4X2 { $$ = "dmat4x2"; }
  1903. | DMAT4X3 { $$ = "dmat4x3"; }
  1904. | DMAT4X4 { $$ = "dmat4"; }
  1905. | SAMPLER1D { $$ = "sampler1D"; }
  1906. | SAMPLER2D { $$ = "sampler2D"; }
  1907. | SAMPLER2DRECT { $$ = "sampler2DRect"; }
  1908. | SAMPLER3D { $$ = "sampler3D"; }
  1909. | SAMPLERCUBE { $$ = "samplerCube"; }
  1910. | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
  1911. | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
  1912. | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
  1913. | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
  1914. | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
  1915. | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
  1916. | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
  1917. | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
  1918. | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
  1919. | SAMPLERBUFFER { $$ = "samplerBuffer"; }
  1920. | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
  1921. | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
  1922. | ISAMPLER1D { $$ = "isampler1D"; }
  1923. | ISAMPLER2D { $$ = "isampler2D"; }
  1924. | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
  1925. | ISAMPLER3D { $$ = "isampler3D"; }
  1926. | ISAMPLERCUBE { $$ = "isamplerCube"; }
  1927. | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
  1928. | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
  1929. | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
  1930. | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
  1931. | USAMPLER1D { $$ = "usampler1D"; }
  1932. | USAMPLER2D { $$ = "usampler2D"; }
  1933. | USAMPLER2DRECT { $$ = "usampler2DRect"; }
  1934. | USAMPLER3D { $$ = "usampler3D"; }
  1935. | USAMPLERCUBE { $$ = "usamplerCube"; }
  1936. | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
  1937. | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
  1938. | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
  1939. | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
  1940. | SAMPLER2DMS { $$ = "sampler2DMS"; }
  1941. | ISAMPLER2DMS { $$ = "isampler2DMS"; }
  1942. | USAMPLER2DMS { $$ = "usampler2DMS"; }
  1943. | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
  1944. | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
  1945. | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
  1946. | IMAGE1D { $$ = "image1D"; }
  1947. | IMAGE2D { $$ = "image2D"; }
  1948. | IMAGE3D { $$ = "image3D"; }
  1949. | IMAGE2DRECT { $$ = "image2DRect"; }
  1950. | IMAGECUBE { $$ = "imageCube"; }
  1951. | IMAGEBUFFER { $$ = "imageBuffer"; }
  1952. | IMAGE1DARRAY { $$ = "image1DArray"; }
  1953. | IMAGE2DARRAY { $$ = "image2DArray"; }
  1954. | IMAGECUBEARRAY { $$ = "imageCubeArray"; }
  1955. | IMAGE2DMS { $$ = "image2DMS"; }
  1956. | IMAGE2DMSARRAY { $$ = "image2DMSArray"; }
  1957. | IIMAGE1D { $$ = "iimage1D"; }
  1958. | IIMAGE2D { $$ = "iimage2D"; }
  1959. | IIMAGE3D { $$ = "iimage3D"; }
  1960. | IIMAGE2DRECT { $$ = "iimage2DRect"; }
  1961. | IIMAGECUBE { $$ = "iimageCube"; }
  1962. | IIMAGEBUFFER { $$ = "iimageBuffer"; }
  1963. | IIMAGE1DARRAY { $$ = "iimage1DArray"; }
  1964. | IIMAGE2DARRAY { $$ = "iimage2DArray"; }
  1965. | IIMAGECUBEARRAY { $$ = "iimageCubeArray"; }
  1966. | IIMAGE2DMS { $$ = "iimage2DMS"; }
  1967. | IIMAGE2DMSARRAY { $$ = "iimage2DMSArray"; }
  1968. | UIMAGE1D { $$ = "uimage1D"; }
  1969. | UIMAGE2D { $$ = "uimage2D"; }
  1970. | UIMAGE3D { $$ = "uimage3D"; }
  1971. | UIMAGE2DRECT { $$ = "uimage2DRect"; }
  1972. | UIMAGECUBE { $$ = "uimageCube"; }
  1973. | UIMAGEBUFFER { $$ = "uimageBuffer"; }
  1974. | UIMAGE1DARRAY { $$ = "uimage1DArray"; }
  1975. | UIMAGE2DARRAY { $$ = "uimage2DArray"; }
  1976. | UIMAGECUBEARRAY { $$ = "uimageCubeArray"; }
  1977. | UIMAGE2DMS { $$ = "uimage2DMS"; }
  1978. | UIMAGE2DMSARRAY { $$ = "uimage2DMSArray"; }
  1979. | ATOMIC_UINT { $$ = "atomic_uint"; }
  1980. ;
  1981. precision_qualifier:
  1982. HIGHP
  1983. {
  1984. state->check_precision_qualifiers_allowed(&@1);
  1985. $$ = ast_precision_high;
  1986. }
  1987. | MEDIUMP
  1988. {
  1989. state->check_precision_qualifiers_allowed(&@1);
  1990. $$ = ast_precision_medium;
  1991. }
  1992. | LOWP
  1993. {
  1994. state->check_precision_qualifiers_allowed(&@1);
  1995. $$ = ast_precision_low;
  1996. }
  1997. ;
  1998. struct_specifier:
  1999. STRUCT any_identifier '{' struct_declaration_list '}'
  2000. {
  2001. void *ctx = state;
  2002. $$ = new(ctx) ast_struct_specifier($2, $4);
  2003. $$->set_location_range(@2, @5);
  2004. state->symbols->add_type($2, glsl_type::void_type);
  2005. }
  2006. | STRUCT '{' struct_declaration_list '}'
  2007. {
  2008. void *ctx = state;
  2009. $$ = new(ctx) ast_struct_specifier(NULL, $3);
  2010. $$->set_location_range(@2, @4);
  2011. }
  2012. ;
  2013. struct_declaration_list:
  2014. struct_declaration
  2015. {
  2016. $$ = $1;
  2017. $1->link.self_link();
  2018. }
  2019. | struct_declaration_list struct_declaration
  2020. {
  2021. $$ = $1;
  2022. $$->link.insert_before(& $2->link);
  2023. }
  2024. ;
  2025. struct_declaration:
  2026. fully_specified_type struct_declarator_list ';'
  2027. {
  2028. void *ctx = state;
  2029. ast_fully_specified_type *const type = $1;
  2030. type->set_location(@1);
  2031. if (type->qualifier.flags.i != 0)
  2032. _mesa_glsl_error(&@1, state,
  2033. "only precision qualifiers may be applied to "
  2034. "structure members");
  2035. $$ = new(ctx) ast_declarator_list(type);
  2036. $$->set_location(@2);
  2037. $$->declarations.push_degenerate_list_at_head(& $2->link);
  2038. }
  2039. ;
  2040. struct_declarator_list:
  2041. struct_declarator
  2042. {
  2043. $$ = $1;
  2044. $1->link.self_link();
  2045. }
  2046. | struct_declarator_list ',' struct_declarator
  2047. {
  2048. $$ = $1;
  2049. $$->link.insert_before(& $3->link);
  2050. }
  2051. ;
  2052. struct_declarator:
  2053. any_identifier
  2054. {
  2055. void *ctx = state;
  2056. $$ = new(ctx) ast_declaration($1, NULL, NULL);
  2057. $$->set_location(@1);
  2058. }
  2059. | any_identifier array_specifier
  2060. {
  2061. void *ctx = state;
  2062. $$ = new(ctx) ast_declaration($1, $2, NULL);
  2063. $$->set_location_range(@1, @2);
  2064. }
  2065. ;
  2066. initializer:
  2067. assignment_expression
  2068. | '{' initializer_list '}'
  2069. {
  2070. $$ = $2;
  2071. }
  2072. | '{' initializer_list ',' '}'
  2073. {
  2074. $$ = $2;
  2075. }
  2076. ;
  2077. initializer_list:
  2078. initializer
  2079. {
  2080. void *ctx = state;
  2081. $$ = new(ctx) ast_aggregate_initializer();
  2082. $$->set_location(@1);
  2083. $$->expressions.push_tail(& $1->link);
  2084. }
  2085. | initializer_list ',' initializer
  2086. {
  2087. $1->expressions.push_tail(& $3->link);
  2088. }
  2089. ;
  2090. declaration_statement:
  2091. declaration
  2092. ;
  2093. // Grammar Note: labeled statements for SWITCH only; 'goto' is not
  2094. // supported.
  2095. statement:
  2096. compound_statement { $$ = (ast_node *) $1; }
  2097. | simple_statement
  2098. ;
  2099. simple_statement:
  2100. declaration_statement
  2101. | expression_statement
  2102. | selection_statement
  2103. | switch_statement
  2104. | iteration_statement
  2105. | jump_statement
  2106. ;
  2107. compound_statement:
  2108. '{' '}'
  2109. {
  2110. void *ctx = state;
  2111. $$ = new(ctx) ast_compound_statement(true, NULL);
  2112. $$->set_location_range(@1, @2);
  2113. }
  2114. | '{'
  2115. {
  2116. state->symbols->push_scope();
  2117. }
  2118. statement_list '}'
  2119. {
  2120. void *ctx = state;
  2121. $$ = new(ctx) ast_compound_statement(true, $3);
  2122. $$->set_location_range(@1, @4);
  2123. state->symbols->pop_scope();
  2124. }
  2125. ;
  2126. statement_no_new_scope:
  2127. compound_statement_no_new_scope { $$ = (ast_node *) $1; }
  2128. | simple_statement
  2129. ;
  2130. compound_statement_no_new_scope:
  2131. '{' '}'
  2132. {
  2133. void *ctx = state;
  2134. $$ = new(ctx) ast_compound_statement(false, NULL);
  2135. $$->set_location_range(@1, @2);
  2136. }
  2137. | '{' statement_list '}'
  2138. {
  2139. void *ctx = state;
  2140. $$ = new(ctx) ast_compound_statement(false, $2);
  2141. $$->set_location_range(@1, @3);
  2142. }
  2143. ;
  2144. statement_list:
  2145. statement
  2146. {
  2147. if ($1 == NULL) {
  2148. _mesa_glsl_error(& @1, state, "<nil> statement");
  2149. assert($1 != NULL);
  2150. }
  2151. $$ = $1;
  2152. $$->link.self_link();
  2153. }
  2154. | statement_list statement
  2155. {
  2156. if ($2 == NULL) {
  2157. _mesa_glsl_error(& @2, state, "<nil> statement");
  2158. assert($2 != NULL);
  2159. }
  2160. $$ = $1;
  2161. $$->link.insert_before(& $2->link);
  2162. }
  2163. ;
  2164. expression_statement:
  2165. ';'
  2166. {
  2167. void *ctx = state;
  2168. $$ = new(ctx) ast_expression_statement(NULL);
  2169. $$->set_location(@1);
  2170. }
  2171. | expression ';'
  2172. {
  2173. void *ctx = state;
  2174. $$ = new(ctx) ast_expression_statement($1);
  2175. $$->set_location(@1);
  2176. }
  2177. ;
  2178. selection_statement:
  2179. IF '(' expression ')' selection_rest_statement
  2180. {
  2181. $$ = new(state) ast_selection_statement($3, $5.then_statement,
  2182. $5.else_statement);
  2183. $$->set_location_range(@1, @5);
  2184. }
  2185. ;
  2186. selection_rest_statement:
  2187. statement ELSE statement
  2188. {
  2189. $$.then_statement = $1;
  2190. $$.else_statement = $3;
  2191. }
  2192. | statement %prec THEN
  2193. {
  2194. $$.then_statement = $1;
  2195. $$.else_statement = NULL;
  2196. }
  2197. ;
  2198. condition:
  2199. expression
  2200. {
  2201. $$ = (ast_node *) $1;
  2202. }
  2203. | fully_specified_type any_identifier '=' initializer
  2204. {
  2205. void *ctx = state;
  2206. ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
  2207. ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
  2208. decl->set_location_range(@2, @4);
  2209. declarator->set_location(@1);
  2210. declarator->declarations.push_tail(&decl->link);
  2211. $$ = declarator;
  2212. }
  2213. ;
  2214. /*
  2215. * switch_statement grammar is based on the syntax described in the body
  2216. * of the GLSL spec, not in it's appendix!!!
  2217. */
  2218. switch_statement:
  2219. SWITCH '(' expression ')' switch_body
  2220. {
  2221. $$ = new(state) ast_switch_statement($3, $5);
  2222. $$->set_location_range(@1, @5);
  2223. }
  2224. ;
  2225. switch_body:
  2226. '{' '}'
  2227. {
  2228. $$ = new(state) ast_switch_body(NULL);
  2229. $$->set_location_range(@1, @2);
  2230. }
  2231. | '{' case_statement_list '}'
  2232. {
  2233. $$ = new(state) ast_switch_body($2);
  2234. $$->set_location_range(@1, @3);
  2235. }
  2236. ;
  2237. case_label:
  2238. CASE expression ':'
  2239. {
  2240. $$ = new(state) ast_case_label($2);
  2241. $$->set_location(@2);
  2242. }
  2243. | DEFAULT ':'
  2244. {
  2245. $$ = new(state) ast_case_label(NULL);
  2246. $$->set_location(@2);
  2247. }
  2248. ;
  2249. case_label_list:
  2250. case_label
  2251. {
  2252. ast_case_label_list *labels = new(state) ast_case_label_list();
  2253. labels->labels.push_tail(& $1->link);
  2254. $$ = labels;
  2255. $$->set_location(@1);
  2256. }
  2257. | case_label_list case_label
  2258. {
  2259. $$ = $1;
  2260. $$->labels.push_tail(& $2->link);
  2261. }
  2262. ;
  2263. case_statement:
  2264. case_label_list statement
  2265. {
  2266. ast_case_statement *stmts = new(state) ast_case_statement($1);
  2267. stmts->set_location(@2);
  2268. stmts->stmts.push_tail(& $2->link);
  2269. $$ = stmts;
  2270. }
  2271. | case_statement statement
  2272. {
  2273. $$ = $1;
  2274. $$->stmts.push_tail(& $2->link);
  2275. }
  2276. ;
  2277. case_statement_list:
  2278. case_statement
  2279. {
  2280. ast_case_statement_list *cases= new(state) ast_case_statement_list();
  2281. cases->set_location(@1);
  2282. cases->cases.push_tail(& $1->link);
  2283. $$ = cases;
  2284. }
  2285. | case_statement_list case_statement
  2286. {
  2287. $$ = $1;
  2288. $$->cases.push_tail(& $2->link);
  2289. }
  2290. ;
  2291. iteration_statement:
  2292. WHILE '(' condition ')' statement_no_new_scope
  2293. {
  2294. void *ctx = state;
  2295. $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
  2296. NULL, $3, NULL, $5);
  2297. $$->set_location_range(@1, @4);
  2298. }
  2299. | DO statement WHILE '(' expression ')' ';'
  2300. {
  2301. void *ctx = state;
  2302. $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
  2303. NULL, $5, NULL, $2);
  2304. $$->set_location_range(@1, @6);
  2305. }
  2306. | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
  2307. {
  2308. void *ctx = state;
  2309. $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
  2310. $3, $4.cond, $4.rest, $6);
  2311. $$->set_location_range(@1, @6);
  2312. }
  2313. ;
  2314. for_init_statement:
  2315. expression_statement
  2316. | declaration_statement
  2317. ;
  2318. conditionopt:
  2319. condition
  2320. | /* empty */
  2321. {
  2322. $$ = NULL;
  2323. }
  2324. ;
  2325. for_rest_statement:
  2326. conditionopt ';'
  2327. {
  2328. $$.cond = $1;
  2329. $$.rest = NULL;
  2330. }
  2331. | conditionopt ';' expression
  2332. {
  2333. $$.cond = $1;
  2334. $$.rest = $3;
  2335. }
  2336. ;
  2337. // Grammar Note: No 'goto'. Gotos are not supported.
  2338. jump_statement:
  2339. CONTINUE ';'
  2340. {
  2341. void *ctx = state;
  2342. $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
  2343. $$->set_location(@1);
  2344. }
  2345. | BREAK ';'
  2346. {
  2347. void *ctx = state;
  2348. $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
  2349. $$->set_location(@1);
  2350. }
  2351. | RETURN ';'
  2352. {
  2353. void *ctx = state;
  2354. $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
  2355. $$->set_location(@1);
  2356. }
  2357. | RETURN expression ';'
  2358. {
  2359. void *ctx = state;
  2360. $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
  2361. $$->set_location_range(@1, @2);
  2362. }
  2363. | DISCARD ';' // Fragment shader only.
  2364. {
  2365. void *ctx = state;
  2366. $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
  2367. $$->set_location(@1);
  2368. }
  2369. ;
  2370. external_declaration:
  2371. function_definition { $$ = $1; }
  2372. | declaration { $$ = $1; }
  2373. | pragma_statement { $$ = NULL; }
  2374. | layout_defaults { $$ = $1; }
  2375. ;
  2376. function_definition:
  2377. function_prototype compound_statement_no_new_scope
  2378. {
  2379. void *ctx = state;
  2380. $$ = new(ctx) ast_function_definition();
  2381. $$->set_location_range(@1, @2);
  2382. $$->prototype = $1;
  2383. $$->body = $2;
  2384. state->symbols->pop_scope();
  2385. }
  2386. ;
  2387. /* layout_qualifieropt is packed into this rule */
  2388. interface_block:
  2389. basic_interface_block
  2390. {
  2391. $$ = $1;
  2392. }
  2393. | layout_qualifier basic_interface_block
  2394. {
  2395. ast_interface_block *block = $2;
  2396. if (!block->layout.merge_qualifier(& @1, state, $1)) {
  2397. YYERROR;
  2398. }
  2399. foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
  2400. ast_type_qualifier& qualifier = member->type->qualifier;
  2401. if (qualifier.flags.q.stream && qualifier.stream != block->layout.stream) {
  2402. _mesa_glsl_error(& @1, state,
  2403. "stream layout qualifier on "
  2404. "interface block member does not match "
  2405. "the interface block (%d vs %d)",
  2406. qualifier.stream, block->layout.stream);
  2407. YYERROR;
  2408. }
  2409. }
  2410. $$ = block;
  2411. }
  2412. ;
  2413. basic_interface_block:
  2414. interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
  2415. {
  2416. ast_interface_block *const block = $6;
  2417. block->block_name = $2;
  2418. block->declarations.push_degenerate_list_at_head(& $4->link);
  2419. if ($1.flags.q.buffer) {
  2420. if (!state->has_shader_storage_buffer_objects()) {
  2421. _mesa_glsl_error(& @1, state,
  2422. "#version 430 / GL_ARB_shader_storage_buffer_object "
  2423. "required for defining shader storage blocks");
  2424. } else if (state->ARB_shader_storage_buffer_object_warn) {
  2425. _mesa_glsl_warning(& @1, state,
  2426. "#version 430 / GL_ARB_shader_storage_buffer_object "
  2427. "required for defining shader storage blocks");
  2428. }
  2429. } else if ($1.flags.q.uniform) {
  2430. if (!state->has_uniform_buffer_objects()) {
  2431. _mesa_glsl_error(& @1, state,
  2432. "#version 140 / GL_ARB_uniform_buffer_object "
  2433. "required for defining uniform blocks");
  2434. } else if (state->ARB_uniform_buffer_object_warn) {
  2435. _mesa_glsl_warning(& @1, state,
  2436. "#version 140 / GL_ARB_uniform_buffer_object "
  2437. "required for defining uniform blocks");
  2438. }
  2439. } else {
  2440. if (state->es_shader || state->language_version < 150) {
  2441. _mesa_glsl_error(& @1, state,
  2442. "#version 150 required for using "
  2443. "interface blocks");
  2444. }
  2445. }
  2446. /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
  2447. * "It is illegal to have an input block in a vertex shader
  2448. * or an output block in a fragment shader"
  2449. */
  2450. if ((state->stage == MESA_SHADER_VERTEX) && $1.flags.q.in) {
  2451. _mesa_glsl_error(& @1, state,
  2452. "`in' interface block is not allowed for "
  2453. "a vertex shader");
  2454. } else if ((state->stage == MESA_SHADER_FRAGMENT) && $1.flags.q.out) {
  2455. _mesa_glsl_error(& @1, state,
  2456. "`out' interface block is not allowed for "
  2457. "a fragment shader");
  2458. }
  2459. /* Since block arrays require names, and both features are added in
  2460. * the same language versions, we don't have to explicitly
  2461. * version-check both things.
  2462. */
  2463. if (block->instance_name != NULL) {
  2464. state->check_version(150, 300, & @1, "interface blocks with "
  2465. "an instance name are not allowed");
  2466. }
  2467. uint64_t interface_type_mask;
  2468. struct ast_type_qualifier temp_type_qualifier;
  2469. /* Get a bitmask containing only the in/out/uniform/buffer
  2470. * flags, allowing us to ignore other irrelevant flags like
  2471. * interpolation qualifiers.
  2472. */
  2473. temp_type_qualifier.flags.i = 0;
  2474. temp_type_qualifier.flags.q.uniform = true;
  2475. temp_type_qualifier.flags.q.buffer = true;
  2476. temp_type_qualifier.flags.q.in = true;
  2477. temp_type_qualifier.flags.q.out = true;
  2478. interface_type_mask = temp_type_qualifier.flags.i;
  2479. /* Get the block's interface qualifier. The interface_qualifier
  2480. * production rule guarantees that only one bit will be set (and
  2481. * it will be in/out/uniform).
  2482. */
  2483. uint64_t block_interface_qualifier = $1.flags.i;
  2484. block->layout.flags.i |= block_interface_qualifier;
  2485. if (state->stage == MESA_SHADER_GEOMETRY &&
  2486. state->has_explicit_attrib_stream()) {
  2487. /* Assign global layout's stream value. */
  2488. block->layout.flags.q.stream = 1;
  2489. block->layout.flags.q.explicit_stream = 0;
  2490. block->layout.stream = state->out_qualifier->stream;
  2491. }
  2492. foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
  2493. ast_type_qualifier& qualifier = member->type->qualifier;
  2494. if ((qualifier.flags.i & interface_type_mask) == 0) {
  2495. /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
  2496. * "If no optional qualifier is used in a member declaration, the
  2497. * qualifier of the variable is just in, out, or uniform as declared
  2498. * by interface-qualifier."
  2499. */
  2500. qualifier.flags.i |= block_interface_qualifier;
  2501. } else if ((qualifier.flags.i & interface_type_mask) !=
  2502. block_interface_qualifier) {
  2503. /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
  2504. * "If optional qualifiers are used, they can include interpolation
  2505. * and storage qualifiers and they must declare an input, output,
  2506. * or uniform variable consistent with the interface qualifier of
  2507. * the block."
  2508. */
  2509. _mesa_glsl_error(& @1, state,
  2510. "uniform/in/out qualifier on "
  2511. "interface block member does not match "
  2512. "the interface block");
  2513. }
  2514. /* From GLSL ES 3.0, chapter 4.3.7 "Interface Blocks":
  2515. *
  2516. * "GLSL ES 3.0 does not support interface blocks for shader inputs or
  2517. * outputs."
  2518. *
  2519. * And from GLSL ES 3.0, chapter 4.6.1 "The invariant qualifier":.
  2520. *
  2521. * "Only variables output from a shader can be candidates for
  2522. * invariance."
  2523. *
  2524. * From GLSL 4.40 and GLSL 1.50, section "Interface Blocks":
  2525. *
  2526. * "If optional qualifiers are used, they can include interpolation
  2527. * qualifiers, auxiliary storage qualifiers, and storage qualifiers
  2528. * and they must declare an input, output, or uniform member
  2529. * consistent with the interface qualifier of the block"
  2530. */
  2531. if (qualifier.flags.q.invariant)
  2532. _mesa_glsl_error(&@1, state,
  2533. "invariant qualifiers cannot be used "
  2534. "with interface blocks members");
  2535. }
  2536. $$ = block;
  2537. }
  2538. ;
  2539. interface_qualifier:
  2540. IN_TOK
  2541. {
  2542. memset(& $$, 0, sizeof($$));
  2543. $$.flags.q.in = 1;
  2544. }
  2545. | OUT_TOK
  2546. {
  2547. memset(& $$, 0, sizeof($$));
  2548. $$.flags.q.out = 1;
  2549. }
  2550. | UNIFORM
  2551. {
  2552. memset(& $$, 0, sizeof($$));
  2553. $$.flags.q.uniform = 1;
  2554. }
  2555. | BUFFER
  2556. {
  2557. memset(& $$, 0, sizeof($$));
  2558. $$.flags.q.buffer = 1;
  2559. }
  2560. ;
  2561. instance_name_opt:
  2562. /* empty */
  2563. {
  2564. $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
  2565. NULL, NULL);
  2566. }
  2567. | NEW_IDENTIFIER
  2568. {
  2569. $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
  2570. $1, NULL);
  2571. $$->set_location(@1);
  2572. }
  2573. | NEW_IDENTIFIER array_specifier
  2574. {
  2575. $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
  2576. $1, $2);
  2577. $$->set_location_range(@1, @2);
  2578. }
  2579. ;
  2580. member_list:
  2581. member_declaration
  2582. {
  2583. $$ = $1;
  2584. $1->link.self_link();
  2585. }
  2586. | member_declaration member_list
  2587. {
  2588. $$ = $1;
  2589. $2->link.insert_before(& $$->link);
  2590. }
  2591. ;
  2592. member_declaration:
  2593. fully_specified_type struct_declarator_list ';'
  2594. {
  2595. void *ctx = state;
  2596. ast_fully_specified_type *type = $1;
  2597. type->set_location(@1);
  2598. if (type->qualifier.flags.q.attribute) {
  2599. _mesa_glsl_error(& @1, state,
  2600. "keyword 'attribute' cannot be used with "
  2601. "interface block member");
  2602. } else if (type->qualifier.flags.q.varying) {
  2603. _mesa_glsl_error(& @1, state,
  2604. "keyword 'varying' cannot be used with "
  2605. "interface block member");
  2606. }
  2607. $$ = new(ctx) ast_declarator_list(type);
  2608. $$->set_location(@2);
  2609. $$->declarations.push_degenerate_list_at_head(& $2->link);
  2610. }
  2611. ;
  2612. layout_defaults:
  2613. layout_qualifier UNIFORM ';'
  2614. {
  2615. if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
  2616. YYERROR;
  2617. }
  2618. $$ = NULL;
  2619. }
  2620. | layout_qualifier IN_TOK ';'
  2621. {
  2622. $$ = NULL;
  2623. if (!state->in_qualifier->merge_in_qualifier(& @1, state, $1, $$)) {
  2624. YYERROR;
  2625. }
  2626. }
  2627. | layout_qualifier OUT_TOK ';'
  2628. {
  2629. $$ = NULL;
  2630. if (state->stage == MESA_SHADER_GEOMETRY) {
  2631. if ($1.flags.q.prim_type) {
  2632. /* Make sure this is a valid output primitive type. */
  2633. switch ($1.prim_type) {
  2634. case GL_POINTS:
  2635. case GL_LINE_STRIP:
  2636. case GL_TRIANGLE_STRIP:
  2637. break;
  2638. default:
  2639. _mesa_glsl_error(&@1, state, "invalid geometry shader output "
  2640. "primitive type");
  2641. break;
  2642. }
  2643. }
  2644. if (!state->out_qualifier->merge_qualifier(& @1, state, $1))
  2645. YYERROR;
  2646. /* Allow future assigments of global out's stream id value */
  2647. state->out_qualifier->flags.q.explicit_stream = 0;
  2648. } else if (state->stage == MESA_SHADER_TESS_CTRL) {
  2649. if (!state->out_qualifier->merge_out_qualifier(& @1, state, $1, $$))
  2650. YYERROR;
  2651. } else {
  2652. _mesa_glsl_error(& @1, state,
  2653. "out layout qualifiers only valid in "
  2654. "tessellation control or geometry shaders");
  2655. }
  2656. }