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.

ast_to_hir.cpp 189KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file ast_to_hir.c
  25. * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
  26. *
  27. * During the conversion to HIR, the majority of the symantic checking is
  28. * preformed on the program. This includes:
  29. *
  30. * * Symbol table management
  31. * * Type checking
  32. * * Function binding
  33. *
  34. * The majority of this work could be done during parsing, and the parser could
  35. * probably generate HIR directly. However, this results in frequent changes
  36. * to the parser code. Since we do not assume that every system this complier
  37. * is built on will have Flex and Bison installed, we have to store the code
  38. * generated by these tools in our version control system. In other parts of
  39. * the system we've seen problems where a parser was changed but the generated
  40. * code was not committed, merge conflicts where created because two developers
  41. * had slightly different versions of Bison installed, etc.
  42. *
  43. * I have also noticed that running Bison generated parsers in GDB is very
  44. * irritating. When you get a segfault on '$$ = $1->foo', you can't very
  45. * well 'print $1' in GDB.
  46. *
  47. * As a result, my preference is to put as little C code as possible in the
  48. * parser (and lexer) sources.
  49. */
  50. #include "main/core.h" /* for struct gl_extensions */
  51. #include "glsl_symbol_table.h"
  52. #include "glsl_parser_extras.h"
  53. #include "ast.h"
  54. #include "glsl_types.h"
  55. #include "program/hash_table.h"
  56. #include "ir.h"
  57. static void
  58. detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
  59. exec_list *instructions);
  60. static void
  61. remove_per_vertex_blocks(exec_list *instructions,
  62. _mesa_glsl_parse_state *state, ir_variable_mode mode);
  63. void
  64. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
  65. {
  66. _mesa_glsl_initialize_variables(instructions, state);
  67. state->symbols->separate_function_namespace = state->language_version == 110;
  68. state->current_function = NULL;
  69. state->toplevel_ir = instructions;
  70. state->gs_input_prim_type_specified = false;
  71. /* Section 4.2 of the GLSL 1.20 specification states:
  72. * "The built-in functions are scoped in a scope outside the global scope
  73. * users declare global variables in. That is, a shader's global scope,
  74. * available for user-defined functions and global variables, is nested
  75. * inside the scope containing the built-in functions."
  76. *
  77. * Since built-in functions like ftransform() access built-in variables,
  78. * it follows that those must be in the outer scope as well.
  79. *
  80. * We push scope here to create this nesting effect...but don't pop.
  81. * This way, a shader's globals are still in the symbol table for use
  82. * by the linker.
  83. */
  84. state->symbols->push_scope();
  85. foreach_list_typed (ast_node, ast, link, & state->translation_unit)
  86. ast->hir(instructions, state);
  87. detect_recursion_unlinked(state, instructions);
  88. detect_conflicting_assignments(state, instructions);
  89. state->toplevel_ir = NULL;
  90. /* Move all of the variable declarations to the front of the IR list, and
  91. * reverse the order. This has the (intended!) side effect that vertex
  92. * shader inputs and fragment shader outputs will appear in the IR in the
  93. * same order that they appeared in the shader code. This results in the
  94. * locations being assigned in the declared order. Many (arguably buggy)
  95. * applications depend on this behavior, and it matches what nearly all
  96. * other drivers do.
  97. */
  98. foreach_list_safe(node, instructions) {
  99. ir_variable *const var = ((ir_instruction *) node)->as_variable();
  100. if (var == NULL)
  101. continue;
  102. var->remove();
  103. instructions->push_head(var);
  104. }
  105. /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
  106. *
  107. * If multiple shaders using members of a built-in block belonging to
  108. * the same interface are linked together in the same program, they
  109. * must all redeclare the built-in block in the same way, as described
  110. * in section 4.3.7 "Interface Blocks" for interface block matching, or
  111. * a link error will result.
  112. *
  113. * The phrase "using members of a built-in block" implies that if two
  114. * shaders are linked together and one of them *does not use* any members
  115. * of the built-in block, then that shader does not need to have a matching
  116. * redeclaration of the built-in block.
  117. *
  118. * This appears to be a clarification to the behaviour established for
  119. * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
  120. * version.
  121. *
  122. * The definition of "interface" in section 4.3.7 that applies here is as
  123. * follows:
  124. *
  125. * The boundary between adjacent programmable pipeline stages: This
  126. * spans all the outputs in all compilation units of the first stage
  127. * and all the inputs in all compilation units of the second stage.
  128. *
  129. * Therefore this rule applies to both inter- and intra-stage linking.
  130. *
  131. * The easiest way to implement this is to check whether the shader uses
  132. * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
  133. * remove all the relevant variable declaration from the IR, so that the
  134. * linker won't see them and complain about mismatches.
  135. */
  136. remove_per_vertex_blocks(instructions, state, ir_var_shader_in);
  137. remove_per_vertex_blocks(instructions, state, ir_var_shader_out);
  138. }
  139. /**
  140. * If a conversion is available, convert one operand to a different type
  141. *
  142. * The \c from \c ir_rvalue is converted "in place".
  143. *
  144. * \param to Type that the operand it to be converted to
  145. * \param from Operand that is being converted
  146. * \param state GLSL compiler state
  147. *
  148. * \return
  149. * If a conversion is possible (or unnecessary), \c true is returned.
  150. * Otherwise \c false is returned.
  151. */
  152. bool
  153. apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
  154. struct _mesa_glsl_parse_state *state)
  155. {
  156. void *ctx = state;
  157. if (to->base_type == from->type->base_type)
  158. return true;
  159. /* This conversion was added in GLSL 1.20. If the compilation mode is
  160. * GLSL 1.10, the conversion is skipped.
  161. */
  162. if (!state->is_version(120, 0))
  163. return false;
  164. /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
  165. *
  166. * "There are no implicit array or structure conversions. For
  167. * example, an array of int cannot be implicitly converted to an
  168. * array of float. There are no implicit conversions between
  169. * signed and unsigned integers."
  170. */
  171. /* FINISHME: The above comment is partially a lie. There is int/uint
  172. * FINISHME: conversion for immediate constants.
  173. */
  174. if (!to->is_float() || !from->type->is_numeric())
  175. return false;
  176. /* Convert to a floating point type with the same number of components
  177. * as the original type - i.e. int to float, not int to vec4.
  178. */
  179. to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
  180. from->type->matrix_columns);
  181. switch (from->type->base_type) {
  182. case GLSL_TYPE_INT:
  183. from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
  184. break;
  185. case GLSL_TYPE_UINT:
  186. from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
  187. break;
  188. case GLSL_TYPE_BOOL:
  189. from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
  190. break;
  191. default:
  192. assert(0);
  193. }
  194. return true;
  195. }
  196. static const struct glsl_type *
  197. arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  198. bool multiply,
  199. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  200. {
  201. const glsl_type *type_a = value_a->type;
  202. const glsl_type *type_b = value_b->type;
  203. /* From GLSL 1.50 spec, page 56:
  204. *
  205. * "The arithmetic binary operators add (+), subtract (-),
  206. * multiply (*), and divide (/) operate on integer and
  207. * floating-point scalars, vectors, and matrices."
  208. */
  209. if (!type_a->is_numeric() || !type_b->is_numeric()) {
  210. _mesa_glsl_error(loc, state,
  211. "operands to arithmetic operators must be numeric");
  212. return glsl_type::error_type;
  213. }
  214. /* "If one operand is floating-point based and the other is
  215. * not, then the conversions from Section 4.1.10 "Implicit
  216. * Conversions" are applied to the non-floating-point-based operand."
  217. */
  218. if (!apply_implicit_conversion(type_a, value_b, state)
  219. && !apply_implicit_conversion(type_b, value_a, state)) {
  220. _mesa_glsl_error(loc, state,
  221. "could not implicitly convert operands to "
  222. "arithmetic operator");
  223. return glsl_type::error_type;
  224. }
  225. type_a = value_a->type;
  226. type_b = value_b->type;
  227. /* "If the operands are integer types, they must both be signed or
  228. * both be unsigned."
  229. *
  230. * From this rule and the preceeding conversion it can be inferred that
  231. * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
  232. * The is_numeric check above already filtered out the case where either
  233. * type is not one of these, so now the base types need only be tested for
  234. * equality.
  235. */
  236. if (type_a->base_type != type_b->base_type) {
  237. _mesa_glsl_error(loc, state,
  238. "base type mismatch for arithmetic operator");
  239. return glsl_type::error_type;
  240. }
  241. /* "All arithmetic binary operators result in the same fundamental type
  242. * (signed integer, unsigned integer, or floating-point) as the
  243. * operands they operate on, after operand type conversion. After
  244. * conversion, the following cases are valid
  245. *
  246. * * The two operands are scalars. In this case the operation is
  247. * applied, resulting in a scalar."
  248. */
  249. if (type_a->is_scalar() && type_b->is_scalar())
  250. return type_a;
  251. /* "* One operand is a scalar, and the other is a vector or matrix.
  252. * In this case, the scalar operation is applied independently to each
  253. * component of the vector or matrix, resulting in the same size
  254. * vector or matrix."
  255. */
  256. if (type_a->is_scalar()) {
  257. if (!type_b->is_scalar())
  258. return type_b;
  259. } else if (type_b->is_scalar()) {
  260. return type_a;
  261. }
  262. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  263. * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
  264. * handled.
  265. */
  266. assert(!type_a->is_scalar());
  267. assert(!type_b->is_scalar());
  268. /* "* The two operands are vectors of the same size. In this case, the
  269. * operation is done component-wise resulting in the same size
  270. * vector."
  271. */
  272. if (type_a->is_vector() && type_b->is_vector()) {
  273. if (type_a == type_b) {
  274. return type_a;
  275. } else {
  276. _mesa_glsl_error(loc, state,
  277. "vector size mismatch for arithmetic operator");
  278. return glsl_type::error_type;
  279. }
  280. }
  281. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  282. * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
  283. * <vector, vector> have been handled. At least one of the operands must
  284. * be matrix. Further, since there are no integer matrix types, the base
  285. * type of both operands must be float.
  286. */
  287. assert(type_a->is_matrix() || type_b->is_matrix());
  288. assert(type_a->base_type == GLSL_TYPE_FLOAT);
  289. assert(type_b->base_type == GLSL_TYPE_FLOAT);
  290. /* "* The operator is add (+), subtract (-), or divide (/), and the
  291. * operands are matrices with the same number of rows and the same
  292. * number of columns. In this case, the operation is done component-
  293. * wise resulting in the same size matrix."
  294. * * The operator is multiply (*), where both operands are matrices or
  295. * one operand is a vector and the other a matrix. A right vector
  296. * operand is treated as a column vector and a left vector operand as a
  297. * row vector. In all these cases, it is required that the number of
  298. * columns of the left operand is equal to the number of rows of the
  299. * right operand. Then, the multiply (*) operation does a linear
  300. * algebraic multiply, yielding an object that has the same number of
  301. * rows as the left operand and the same number of columns as the right
  302. * operand. Section 5.10 "Vector and Matrix Operations" explains in
  303. * more detail how vectors and matrices are operated on."
  304. */
  305. if (! multiply) {
  306. if (type_a == type_b)
  307. return type_a;
  308. } else {
  309. if (type_a->is_matrix() && type_b->is_matrix()) {
  310. /* Matrix multiply. The columns of A must match the rows of B. Given
  311. * the other previously tested constraints, this means the vector type
  312. * of a row from A must be the same as the vector type of a column from
  313. * B.
  314. */
  315. if (type_a->row_type() == type_b->column_type()) {
  316. /* The resulting matrix has the number of columns of matrix B and
  317. * the number of rows of matrix A. We get the row count of A by
  318. * looking at the size of a vector that makes up a column. The
  319. * transpose (size of a row) is done for B.
  320. */
  321. const glsl_type *const type =
  322. glsl_type::get_instance(type_a->base_type,
  323. type_a->column_type()->vector_elements,
  324. type_b->row_type()->vector_elements);
  325. assert(type != glsl_type::error_type);
  326. return type;
  327. }
  328. } else if (type_a->is_matrix()) {
  329. /* A is a matrix and B is a column vector. Columns of A must match
  330. * rows of B. Given the other previously tested constraints, this
  331. * means the vector type of a row from A must be the same as the
  332. * vector the type of B.
  333. */
  334. if (type_a->row_type() == type_b) {
  335. /* The resulting vector has a number of elements equal to
  336. * the number of rows of matrix A. */
  337. const glsl_type *const type =
  338. glsl_type::get_instance(type_a->base_type,
  339. type_a->column_type()->vector_elements,
  340. 1);
  341. assert(type != glsl_type::error_type);
  342. return type;
  343. }
  344. } else {
  345. assert(type_b->is_matrix());
  346. /* A is a row vector and B is a matrix. Columns of A must match rows
  347. * of B. Given the other previously tested constraints, this means
  348. * the type of A must be the same as the vector type of a column from
  349. * B.
  350. */
  351. if (type_a == type_b->column_type()) {
  352. /* The resulting vector has a number of elements equal to
  353. * the number of columns of matrix B. */
  354. const glsl_type *const type =
  355. glsl_type::get_instance(type_a->base_type,
  356. type_b->row_type()->vector_elements,
  357. 1);
  358. assert(type != glsl_type::error_type);
  359. return type;
  360. }
  361. }
  362. _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
  363. return glsl_type::error_type;
  364. }
  365. /* "All other cases are illegal."
  366. */
  367. _mesa_glsl_error(loc, state, "type mismatch");
  368. return glsl_type::error_type;
  369. }
  370. static const struct glsl_type *
  371. unary_arithmetic_result_type(const struct glsl_type *type,
  372. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  373. {
  374. /* From GLSL 1.50 spec, page 57:
  375. *
  376. * "The arithmetic unary operators negate (-), post- and pre-increment
  377. * and decrement (-- and ++) operate on integer or floating-point
  378. * values (including vectors and matrices). All unary operators work
  379. * component-wise on their operands. These result with the same type
  380. * they operated on."
  381. */
  382. if (!type->is_numeric()) {
  383. _mesa_glsl_error(loc, state,
  384. "operands to arithmetic operators must be numeric");
  385. return glsl_type::error_type;
  386. }
  387. return type;
  388. }
  389. /**
  390. * \brief Return the result type of a bit-logic operation.
  391. *
  392. * If the given types to the bit-logic operator are invalid, return
  393. * glsl_type::error_type.
  394. *
  395. * \param type_a Type of LHS of bit-logic op
  396. * \param type_b Type of RHS of bit-logic op
  397. */
  398. static const struct glsl_type *
  399. bit_logic_result_type(const struct glsl_type *type_a,
  400. const struct glsl_type *type_b,
  401. ast_operators op,
  402. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  403. {
  404. if (!state->check_bitwise_operations_allowed(loc)) {
  405. return glsl_type::error_type;
  406. }
  407. /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
  408. *
  409. * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
  410. * (|). The operands must be of type signed or unsigned integers or
  411. * integer vectors."
  412. */
  413. if (!type_a->is_integer()) {
  414. _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
  415. ast_expression::operator_string(op));
  416. return glsl_type::error_type;
  417. }
  418. if (!type_b->is_integer()) {
  419. _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
  420. ast_expression::operator_string(op));
  421. return glsl_type::error_type;
  422. }
  423. /* "The fundamental types of the operands (signed or unsigned) must
  424. * match,"
  425. */
  426. if (type_a->base_type != type_b->base_type) {
  427. _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
  428. "base type", ast_expression::operator_string(op));
  429. return glsl_type::error_type;
  430. }
  431. /* "The operands cannot be vectors of differing size." */
  432. if (type_a->is_vector() &&
  433. type_b->is_vector() &&
  434. type_a->vector_elements != type_b->vector_elements) {
  435. _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
  436. "different sizes", ast_expression::operator_string(op));
  437. return glsl_type::error_type;
  438. }
  439. /* "If one operand is a scalar and the other a vector, the scalar is
  440. * applied component-wise to the vector, resulting in the same type as
  441. * the vector. The fundamental types of the operands [...] will be the
  442. * resulting fundamental type."
  443. */
  444. if (type_a->is_scalar())
  445. return type_b;
  446. else
  447. return type_a;
  448. }
  449. static const struct glsl_type *
  450. modulus_result_type(const struct glsl_type *type_a,
  451. const struct glsl_type *type_b,
  452. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  453. {
  454. if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) {
  455. return glsl_type::error_type;
  456. }
  457. /* From GLSL 1.50 spec, page 56:
  458. * "The operator modulus (%) operates on signed or unsigned integers or
  459. * integer vectors. The operand types must both be signed or both be
  460. * unsigned."
  461. */
  462. if (!type_a->is_integer()) {
  463. _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer");
  464. return glsl_type::error_type;
  465. }
  466. if (!type_b->is_integer()) {
  467. _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer");
  468. return glsl_type::error_type;
  469. }
  470. if (type_a->base_type != type_b->base_type) {
  471. _mesa_glsl_error(loc, state,
  472. "operands of %% must have the same base type");
  473. return glsl_type::error_type;
  474. }
  475. /* "The operands cannot be vectors of differing size. If one operand is
  476. * a scalar and the other vector, then the scalar is applied component-
  477. * wise to the vector, resulting in the same type as the vector. If both
  478. * are vectors of the same size, the result is computed component-wise."
  479. */
  480. if (type_a->is_vector()) {
  481. if (!type_b->is_vector()
  482. || (type_a->vector_elements == type_b->vector_elements))
  483. return type_a;
  484. } else
  485. return type_b;
  486. /* "The operator modulus (%) is not defined for any other data types
  487. * (non-integer types)."
  488. */
  489. _mesa_glsl_error(loc, state, "type mismatch");
  490. return glsl_type::error_type;
  491. }
  492. static const struct glsl_type *
  493. relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  494. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  495. {
  496. const glsl_type *type_a = value_a->type;
  497. const glsl_type *type_b = value_b->type;
  498. /* From GLSL 1.50 spec, page 56:
  499. * "The relational operators greater than (>), less than (<), greater
  500. * than or equal (>=), and less than or equal (<=) operate only on
  501. * scalar integer and scalar floating-point expressions."
  502. */
  503. if (!type_a->is_numeric()
  504. || !type_b->is_numeric()
  505. || !type_a->is_scalar()
  506. || !type_b->is_scalar()) {
  507. _mesa_glsl_error(loc, state,
  508. "operands to relational operators must be scalar and "
  509. "numeric");
  510. return glsl_type::error_type;
  511. }
  512. /* "Either the operands' types must match, or the conversions from
  513. * Section 4.1.10 "Implicit Conversions" will be applied to the integer
  514. * operand, after which the types must match."
  515. */
  516. if (!apply_implicit_conversion(type_a, value_b, state)
  517. && !apply_implicit_conversion(type_b, value_a, state)) {
  518. _mesa_glsl_error(loc, state,
  519. "could not implicitly convert operands to "
  520. "relational operator");
  521. return glsl_type::error_type;
  522. }
  523. type_a = value_a->type;
  524. type_b = value_b->type;
  525. if (type_a->base_type != type_b->base_type) {
  526. _mesa_glsl_error(loc, state, "base type mismatch");
  527. return glsl_type::error_type;
  528. }
  529. /* "The result is scalar Boolean."
  530. */
  531. return glsl_type::bool_type;
  532. }
  533. /**
  534. * \brief Return the result type of a bit-shift operation.
  535. *
  536. * If the given types to the bit-shift operator are invalid, return
  537. * glsl_type::error_type.
  538. *
  539. * \param type_a Type of LHS of bit-shift op
  540. * \param type_b Type of RHS of bit-shift op
  541. */
  542. static const struct glsl_type *
  543. shift_result_type(const struct glsl_type *type_a,
  544. const struct glsl_type *type_b,
  545. ast_operators op,
  546. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  547. {
  548. if (!state->check_bitwise_operations_allowed(loc)) {
  549. return glsl_type::error_type;
  550. }
  551. /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
  552. *
  553. * "The shift operators (<<) and (>>). For both operators, the operands
  554. * must be signed or unsigned integers or integer vectors. One operand
  555. * can be signed while the other is unsigned."
  556. */
  557. if (!type_a->is_integer()) {
  558. _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
  559. "integer vector", ast_expression::operator_string(op));
  560. return glsl_type::error_type;
  561. }
  562. if (!type_b->is_integer()) {
  563. _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
  564. "integer vector", ast_expression::operator_string(op));
  565. return glsl_type::error_type;
  566. }
  567. /* "If the first operand is a scalar, the second operand has to be
  568. * a scalar as well."
  569. */
  570. if (type_a->is_scalar() && !type_b->is_scalar()) {
  571. _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the "
  572. "second must be scalar as well",
  573. ast_expression::operator_string(op));
  574. return glsl_type::error_type;
  575. }
  576. /* If both operands are vectors, check that they have same number of
  577. * elements.
  578. */
  579. if (type_a->is_vector() &&
  580. type_b->is_vector() &&
  581. type_a->vector_elements != type_b->vector_elements) {
  582. _mesa_glsl_error(loc, state, "vector operands to operator %s must "
  583. "have same number of elements",
  584. ast_expression::operator_string(op));
  585. return glsl_type::error_type;
  586. }
  587. /* "In all cases, the resulting type will be the same type as the left
  588. * operand."
  589. */
  590. return type_a;
  591. }
  592. /**
  593. * Validates that a value can be assigned to a location with a specified type
  594. *
  595. * Validates that \c rhs can be assigned to some location. If the types are
  596. * not an exact match but an automatic conversion is possible, \c rhs will be
  597. * converted.
  598. *
  599. * \return
  600. * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
  601. * Otherwise the actual RHS to be assigned will be returned. This may be
  602. * \c rhs, or it may be \c rhs after some type conversion.
  603. *
  604. * \note
  605. * In addition to being used for assignments, this function is used to
  606. * type-check return values.
  607. */
  608. ir_rvalue *
  609. validate_assignment(struct _mesa_glsl_parse_state *state,
  610. YYLTYPE loc, const glsl_type *lhs_type,
  611. ir_rvalue *rhs, bool is_initializer)
  612. {
  613. /* If there is already some error in the RHS, just return it. Anything
  614. * else will lead to an avalanche of error message back to the user.
  615. */
  616. if (rhs->type->is_error())
  617. return rhs;
  618. /* If the types are identical, the assignment can trivially proceed.
  619. */
  620. if (rhs->type == lhs_type)
  621. return rhs;
  622. /* If the array element types are the same and the LHS is unsized,
  623. * the assignment is okay for initializers embedded in variable
  624. * declarations.
  625. *
  626. * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
  627. * is handled by ir_dereference::is_lvalue.
  628. */
  629. if (lhs_type->is_unsized_array() && rhs->type->is_array()
  630. && (lhs_type->element_type() == rhs->type->element_type())) {
  631. if (is_initializer) {
  632. return rhs;
  633. } else {
  634. _mesa_glsl_error(&loc, state,
  635. "implicitly sized arrays cannot be assigned");
  636. return NULL;
  637. }
  638. }
  639. /* Check for implicit conversion in GLSL 1.20 */
  640. if (apply_implicit_conversion(lhs_type, rhs, state)) {
  641. if (rhs->type == lhs_type)
  642. return rhs;
  643. }
  644. _mesa_glsl_error(&loc, state,
  645. "%s of type %s cannot be assigned to "
  646. "variable of type %s",
  647. is_initializer ? "initializer" : "value",
  648. rhs->type->name, lhs_type->name);
  649. return NULL;
  650. }
  651. static void
  652. mark_whole_array_access(ir_rvalue *access)
  653. {
  654. ir_dereference_variable *deref = access->as_dereference_variable();
  655. if (deref && deref->var) {
  656. deref->var->data.max_array_access = deref->type->length - 1;
  657. }
  658. }
  659. ir_rvalue *
  660. do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
  661. const char *non_lvalue_description,
  662. ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
  663. YYLTYPE lhs_loc)
  664. {
  665. void *ctx = state;
  666. bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
  667. /* If the assignment LHS comes back as an ir_binop_vector_extract
  668. * expression, move it to the RHS as an ir_triop_vector_insert.
  669. */
  670. if (lhs->ir_type == ir_type_expression) {
  671. ir_expression *const expr = lhs->as_expression();
  672. if (unlikely(expr->operation == ir_binop_vector_extract)) {
  673. ir_rvalue *new_rhs =
  674. validate_assignment(state, lhs_loc, lhs->type,
  675. rhs, is_initializer);
  676. if (new_rhs == NULL) {
  677. return lhs;
  678. } else {
  679. rhs = new(ctx) ir_expression(ir_triop_vector_insert,
  680. expr->operands[0]->type,
  681. expr->operands[0],
  682. new_rhs,
  683. expr->operands[1]);
  684. lhs = expr->operands[0]->clone(ctx, NULL);
  685. }
  686. }
  687. }
  688. ir_variable *lhs_var = lhs->variable_referenced();
  689. if (lhs_var)
  690. lhs_var->data.assigned = true;
  691. if (!error_emitted) {
  692. if (non_lvalue_description != NULL) {
  693. _mesa_glsl_error(&lhs_loc, state,
  694. "assignment to %s",
  695. non_lvalue_description);
  696. error_emitted = true;
  697. } else if (lhs->variable_referenced() != NULL
  698. && lhs->variable_referenced()->data.read_only) {
  699. _mesa_glsl_error(&lhs_loc, state,
  700. "assignment to read-only variable '%s'",
  701. lhs->variable_referenced()->name);
  702. error_emitted = true;
  703. } else if (lhs->type->is_array() &&
  704. !state->check_version(120, 300, &lhs_loc,
  705. "whole array assignment forbidden")) {
  706. /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
  707. *
  708. * "Other binary or unary expressions, non-dereferenced
  709. * arrays, function names, swizzles with repeated fields,
  710. * and constants cannot be l-values."
  711. *
  712. * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
  713. */
  714. error_emitted = true;
  715. } else if (!lhs->is_lvalue()) {
  716. _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
  717. error_emitted = true;
  718. }
  719. }
  720. ir_rvalue *new_rhs =
  721. validate_assignment(state, lhs_loc, lhs->type, rhs, is_initializer);
  722. if (new_rhs != NULL) {
  723. rhs = new_rhs;
  724. /* If the LHS array was not declared with a size, it takes it size from
  725. * the RHS. If the LHS is an l-value and a whole array, it must be a
  726. * dereference of a variable. Any other case would require that the LHS
  727. * is either not an l-value or not a whole array.
  728. */
  729. if (lhs->type->is_unsized_array()) {
  730. ir_dereference *const d = lhs->as_dereference();
  731. assert(d != NULL);
  732. ir_variable *const var = d->variable_referenced();
  733. assert(var != NULL);
  734. if (var->data.max_array_access >= unsigned(rhs->type->array_size())) {
  735. /* FINISHME: This should actually log the location of the RHS. */
  736. _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
  737. "previous access",
  738. var->data.max_array_access);
  739. }
  740. var->type = glsl_type::get_array_instance(lhs->type->element_type(),
  741. rhs->type->array_size());
  742. d->type = var->type;
  743. }
  744. mark_whole_array_access(rhs);
  745. mark_whole_array_access(lhs);
  746. }
  747. /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
  748. * but not post_inc) need the converted assigned value as an rvalue
  749. * to handle things like:
  750. *
  751. * i = j += 1;
  752. *
  753. * So we always just store the computed value being assigned to a
  754. * temporary and return a deref of that temporary. If the rvalue
  755. * ends up not being used, the temp will get copy-propagated out.
  756. */
  757. ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
  758. ir_var_temporary);
  759. ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
  760. instructions->push_tail(var);
  761. instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs));
  762. deref_var = new(ctx) ir_dereference_variable(var);
  763. if (!error_emitted)
  764. instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
  765. return new(ctx) ir_dereference_variable(var);
  766. }
  767. static ir_rvalue *
  768. get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
  769. {
  770. void *ctx = ralloc_parent(lvalue);
  771. ir_variable *var;
  772. var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
  773. ir_var_temporary);
  774. instructions->push_tail(var);
  775. var->data.mode = ir_var_auto;
  776. instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
  777. lvalue));
  778. return new(ctx) ir_dereference_variable(var);
  779. }
  780. ir_rvalue *
  781. ast_node::hir(exec_list *instructions,
  782. struct _mesa_glsl_parse_state *state)
  783. {
  784. (void) instructions;
  785. (void) state;
  786. return NULL;
  787. }
  788. static ir_rvalue *
  789. do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
  790. {
  791. int join_op;
  792. ir_rvalue *cmp = NULL;
  793. if (operation == ir_binop_all_equal)
  794. join_op = ir_binop_logic_and;
  795. else
  796. join_op = ir_binop_logic_or;
  797. switch (op0->type->base_type) {
  798. case GLSL_TYPE_FLOAT:
  799. case GLSL_TYPE_UINT:
  800. case GLSL_TYPE_INT:
  801. case GLSL_TYPE_BOOL:
  802. return new(mem_ctx) ir_expression(operation, op0, op1);
  803. case GLSL_TYPE_ARRAY: {
  804. for (unsigned int i = 0; i < op0->type->length; i++) {
  805. ir_rvalue *e0, *e1, *result;
  806. e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
  807. new(mem_ctx) ir_constant(i));
  808. e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
  809. new(mem_ctx) ir_constant(i));
  810. result = do_comparison(mem_ctx, operation, e0, e1);
  811. if (cmp) {
  812. cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  813. } else {
  814. cmp = result;
  815. }
  816. }
  817. mark_whole_array_access(op0);
  818. mark_whole_array_access(op1);
  819. break;
  820. }
  821. case GLSL_TYPE_STRUCT: {
  822. for (unsigned int i = 0; i < op0->type->length; i++) {
  823. ir_rvalue *e0, *e1, *result;
  824. const char *field_name = op0->type->fields.structure[i].name;
  825. e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
  826. field_name);
  827. e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
  828. field_name);
  829. result = do_comparison(mem_ctx, operation, e0, e1);
  830. if (cmp) {
  831. cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  832. } else {
  833. cmp = result;
  834. }
  835. }
  836. break;
  837. }
  838. case GLSL_TYPE_ERROR:
  839. case GLSL_TYPE_VOID:
  840. case GLSL_TYPE_SAMPLER:
  841. case GLSL_TYPE_INTERFACE:
  842. case GLSL_TYPE_ATOMIC_UINT:
  843. /* I assume a comparison of a struct containing a sampler just
  844. * ignores the sampler present in the type.
  845. */
  846. break;
  847. }
  848. if (cmp == NULL)
  849. cmp = new(mem_ctx) ir_constant(true);
  850. return cmp;
  851. }
  852. /* For logical operations, we want to ensure that the operands are
  853. * scalar booleans. If it isn't, emit an error and return a constant
  854. * boolean to avoid triggering cascading error messages.
  855. */
  856. ir_rvalue *
  857. get_scalar_boolean_operand(exec_list *instructions,
  858. struct _mesa_glsl_parse_state *state,
  859. ast_expression *parent_expr,
  860. int operand,
  861. const char *operand_name,
  862. bool *error_emitted)
  863. {
  864. ast_expression *expr = parent_expr->subexpressions[operand];
  865. void *ctx = state;
  866. ir_rvalue *val = expr->hir(instructions, state);
  867. if (val->type->is_boolean() && val->type->is_scalar())
  868. return val;
  869. if (!*error_emitted) {
  870. YYLTYPE loc = expr->get_location();
  871. _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
  872. operand_name,
  873. parent_expr->operator_string(parent_expr->oper));
  874. *error_emitted = true;
  875. }
  876. return new(ctx) ir_constant(true);
  877. }
  878. /**
  879. * If name refers to a builtin array whose maximum allowed size is less than
  880. * size, report an error and return true. Otherwise return false.
  881. */
  882. void
  883. check_builtin_array_max_size(const char *name, unsigned size,
  884. YYLTYPE loc, struct _mesa_glsl_parse_state *state)
  885. {
  886. if ((strcmp("gl_TexCoord", name) == 0)
  887. && (size > state->Const.MaxTextureCoords)) {
  888. /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
  889. *
  890. * "The size [of gl_TexCoord] can be at most
  891. * gl_MaxTextureCoords."
  892. */
  893. _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
  894. "be larger than gl_MaxTextureCoords (%u)",
  895. state->Const.MaxTextureCoords);
  896. } else if (strcmp("gl_ClipDistance", name) == 0
  897. && size > state->Const.MaxClipPlanes) {
  898. /* From section 7.1 (Vertex Shader Special Variables) of the
  899. * GLSL 1.30 spec:
  900. *
  901. * "The gl_ClipDistance array is predeclared as unsized and
  902. * must be sized by the shader either redeclaring it with a
  903. * size or indexing it only with integral constant
  904. * expressions. ... The size can be at most
  905. * gl_MaxClipDistances."
  906. */
  907. _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
  908. "be larger than gl_MaxClipDistances (%u)",
  909. state->Const.MaxClipPlanes);
  910. }
  911. }
  912. /**
  913. * Create the constant 1, of a which is appropriate for incrementing and
  914. * decrementing values of the given GLSL type. For example, if type is vec4,
  915. * this creates a constant value of 1.0 having type float.
  916. *
  917. * If the given type is invalid for increment and decrement operators, return
  918. * a floating point 1--the error will be detected later.
  919. */
  920. static ir_rvalue *
  921. constant_one_for_inc_dec(void *ctx, const glsl_type *type)
  922. {
  923. switch (type->base_type) {
  924. case GLSL_TYPE_UINT:
  925. return new(ctx) ir_constant((unsigned) 1);
  926. case GLSL_TYPE_INT:
  927. return new(ctx) ir_constant(1);
  928. default:
  929. case GLSL_TYPE_FLOAT:
  930. return new(ctx) ir_constant(1.0f);
  931. }
  932. }
  933. ir_rvalue *
  934. ast_expression::hir(exec_list *instructions,
  935. struct _mesa_glsl_parse_state *state)
  936. {
  937. void *ctx = state;
  938. static const int operations[AST_NUM_OPERATORS] = {
  939. -1, /* ast_assign doesn't convert to ir_expression. */
  940. -1, /* ast_plus doesn't convert to ir_expression. */
  941. ir_unop_neg,
  942. ir_binop_add,
  943. ir_binop_sub,
  944. ir_binop_mul,
  945. ir_binop_div,
  946. ir_binop_mod,
  947. ir_binop_lshift,
  948. ir_binop_rshift,
  949. ir_binop_less,
  950. ir_binop_greater,
  951. ir_binop_lequal,
  952. ir_binop_gequal,
  953. ir_binop_all_equal,
  954. ir_binop_any_nequal,
  955. ir_binop_bit_and,
  956. ir_binop_bit_xor,
  957. ir_binop_bit_or,
  958. ir_unop_bit_not,
  959. ir_binop_logic_and,
  960. ir_binop_logic_xor,
  961. ir_binop_logic_or,
  962. ir_unop_logic_not,
  963. /* Note: The following block of expression types actually convert
  964. * to multiple IR instructions.
  965. */
  966. ir_binop_mul, /* ast_mul_assign */
  967. ir_binop_div, /* ast_div_assign */
  968. ir_binop_mod, /* ast_mod_assign */
  969. ir_binop_add, /* ast_add_assign */
  970. ir_binop_sub, /* ast_sub_assign */
  971. ir_binop_lshift, /* ast_ls_assign */
  972. ir_binop_rshift, /* ast_rs_assign */
  973. ir_binop_bit_and, /* ast_and_assign */
  974. ir_binop_bit_xor, /* ast_xor_assign */
  975. ir_binop_bit_or, /* ast_or_assign */
  976. -1, /* ast_conditional doesn't convert to ir_expression. */
  977. ir_binop_add, /* ast_pre_inc. */
  978. ir_binop_sub, /* ast_pre_dec. */
  979. ir_binop_add, /* ast_post_inc. */
  980. ir_binop_sub, /* ast_post_dec. */
  981. -1, /* ast_field_selection doesn't conv to ir_expression. */
  982. -1, /* ast_array_index doesn't convert to ir_expression. */
  983. -1, /* ast_function_call doesn't conv to ir_expression. */
  984. -1, /* ast_identifier doesn't convert to ir_expression. */
  985. -1, /* ast_int_constant doesn't convert to ir_expression. */
  986. -1, /* ast_uint_constant doesn't conv to ir_expression. */
  987. -1, /* ast_float_constant doesn't conv to ir_expression. */
  988. -1, /* ast_bool_constant doesn't conv to ir_expression. */
  989. -1, /* ast_sequence doesn't convert to ir_expression. */
  990. };
  991. ir_rvalue *result = NULL;
  992. ir_rvalue *op[3];
  993. const struct glsl_type *type; /* a temporary variable for switch cases */
  994. bool error_emitted = false;
  995. YYLTYPE loc;
  996. loc = this->get_location();
  997. switch (this->oper) {
  998. case ast_aggregate:
  999. assert(!"ast_aggregate: Should never get here.");
  1000. break;
  1001. case ast_assign: {
  1002. op[0] = this->subexpressions[0]->hir(instructions, state);
  1003. op[1] = this->subexpressions[1]->hir(instructions, state);
  1004. result = do_assignment(instructions, state,
  1005. this->subexpressions[0]->non_lvalue_description,
  1006. op[0], op[1], false,
  1007. this->subexpressions[0]->get_location());
  1008. error_emitted = result->type->is_error();
  1009. break;
  1010. }
  1011. case ast_plus:
  1012. op[0] = this->subexpressions[0]->hir(instructions, state);
  1013. type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  1014. error_emitted = type->is_error();
  1015. result = op[0];
  1016. break;
  1017. case ast_neg:
  1018. op[0] = this->subexpressions[0]->hir(instructions, state);
  1019. type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  1020. error_emitted = type->is_error();
  1021. result = new(ctx) ir_expression(operations[this->oper], type,
  1022. op[0], NULL);
  1023. break;
  1024. case ast_add:
  1025. case ast_sub:
  1026. case ast_mul:
  1027. case ast_div:
  1028. op[0] = this->subexpressions[0]->hir(instructions, state);
  1029. op[1] = this->subexpressions[1]->hir(instructions, state);
  1030. type = arithmetic_result_type(op[0], op[1],
  1031. (this->oper == ast_mul),
  1032. state, & loc);
  1033. error_emitted = type->is_error();
  1034. result = new(ctx) ir_expression(operations[this->oper], type,
  1035. op[0], op[1]);
  1036. break;
  1037. case ast_mod:
  1038. op[0] = this->subexpressions[0]->hir(instructions, state);
  1039. op[1] = this->subexpressions[1]->hir(instructions, state);
  1040. type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  1041. assert(operations[this->oper] == ir_binop_mod);
  1042. result = new(ctx) ir_expression(operations[this->oper], type,
  1043. op[0], op[1]);
  1044. error_emitted = type->is_error();
  1045. break;
  1046. case ast_lshift:
  1047. case ast_rshift:
  1048. if (!state->check_bitwise_operations_allowed(&loc)) {
  1049. error_emitted = true;
  1050. }
  1051. op[0] = this->subexpressions[0]->hir(instructions, state);
  1052. op[1] = this->subexpressions[1]->hir(instructions, state);
  1053. type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  1054. &loc);
  1055. result = new(ctx) ir_expression(operations[this->oper], type,
  1056. op[0], op[1]);
  1057. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1058. break;
  1059. case ast_less:
  1060. case ast_greater:
  1061. case ast_lequal:
  1062. case ast_gequal:
  1063. op[0] = this->subexpressions[0]->hir(instructions, state);
  1064. op[1] = this->subexpressions[1]->hir(instructions, state);
  1065. type = relational_result_type(op[0], op[1], state, & loc);
  1066. /* The relational operators must either generate an error or result
  1067. * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
  1068. */
  1069. assert(type->is_error()
  1070. || ((type->base_type == GLSL_TYPE_BOOL)
  1071. && type->is_scalar()));
  1072. result = new(ctx) ir_expression(operations[this->oper], type,
  1073. op[0], op[1]);
  1074. error_emitted = type->is_error();
  1075. break;
  1076. case ast_nequal:
  1077. case ast_equal:
  1078. op[0] = this->subexpressions[0]->hir(instructions, state);
  1079. op[1] = this->subexpressions[1]->hir(instructions, state);
  1080. /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
  1081. *
  1082. * "The equality operators equal (==), and not equal (!=)
  1083. * operate on all types. They result in a scalar Boolean. If
  1084. * the operand types do not match, then there must be a
  1085. * conversion from Section 4.1.10 "Implicit Conversions"
  1086. * applied to one operand that can make them match, in which
  1087. * case this conversion is done."
  1088. */
  1089. if ((!apply_implicit_conversion(op[0]->type, op[1], state)
  1090. && !apply_implicit_conversion(op[1]->type, op[0], state))
  1091. || (op[0]->type != op[1]->type)) {
  1092. _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
  1093. "type", (this->oper == ast_equal) ? "==" : "!=");
  1094. error_emitted = true;
  1095. } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
  1096. !state->check_version(120, 300, &loc,
  1097. "array comparisons forbidden")) {
  1098. error_emitted = true;
  1099. } else if ((op[0]->type->contains_opaque() ||
  1100. op[1]->type->contains_opaque())) {
  1101. _mesa_glsl_error(&loc, state, "opaque type comparisons forbidden");
  1102. error_emitted = true;
  1103. }
  1104. if (error_emitted) {
  1105. result = new(ctx) ir_constant(false);
  1106. } else {
  1107. result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
  1108. assert(result->type == glsl_type::bool_type);
  1109. }
  1110. break;
  1111. case ast_bit_and:
  1112. case ast_bit_xor:
  1113. case ast_bit_or:
  1114. op[0] = this->subexpressions[0]->hir(instructions, state);
  1115. op[1] = this->subexpressions[1]->hir(instructions, state);
  1116. type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1117. state, &loc);
  1118. result = new(ctx) ir_expression(operations[this->oper], type,
  1119. op[0], op[1]);
  1120. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1121. break;
  1122. case ast_bit_not:
  1123. op[0] = this->subexpressions[0]->hir(instructions, state);
  1124. if (!state->check_bitwise_operations_allowed(&loc)) {
  1125. error_emitted = true;
  1126. }
  1127. if (!op[0]->type->is_integer()) {
  1128. _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
  1129. error_emitted = true;
  1130. }
  1131. type = error_emitted ? glsl_type::error_type : op[0]->type;
  1132. result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
  1133. break;
  1134. case ast_logic_and: {
  1135. exec_list rhs_instructions;
  1136. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1137. "LHS", &error_emitted);
  1138. op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
  1139. "RHS", &error_emitted);
  1140. if (rhs_instructions.is_empty()) {
  1141. result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
  1142. type = result->type;
  1143. } else {
  1144. ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1145. "and_tmp",
  1146. ir_var_temporary);
  1147. instructions->push_tail(tmp);
  1148. ir_if *const stmt = new(ctx) ir_if(op[0]);
  1149. instructions->push_tail(stmt);
  1150. stmt->then_instructions.append_list(&rhs_instructions);
  1151. ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1152. ir_assignment *const then_assign =
  1153. new(ctx) ir_assignment(then_deref, op[1]);
  1154. stmt->then_instructions.push_tail(then_assign);
  1155. ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1156. ir_assignment *const else_assign =
  1157. new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
  1158. stmt->else_instructions.push_tail(else_assign);
  1159. result = new(ctx) ir_dereference_variable(tmp);
  1160. type = tmp->type;
  1161. }
  1162. break;
  1163. }
  1164. case ast_logic_or: {
  1165. exec_list rhs_instructions;
  1166. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1167. "LHS", &error_emitted);
  1168. op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
  1169. "RHS", &error_emitted);
  1170. if (rhs_instructions.is_empty()) {
  1171. result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
  1172. type = result->type;
  1173. } else {
  1174. ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1175. "or_tmp",
  1176. ir_var_temporary);
  1177. instructions->push_tail(tmp);
  1178. ir_if *const stmt = new(ctx) ir_if(op[0]);
  1179. instructions->push_tail(stmt);
  1180. ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1181. ir_assignment *const then_assign =
  1182. new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
  1183. stmt->then_instructions.push_tail(then_assign);
  1184. stmt->else_instructions.append_list(&rhs_instructions);
  1185. ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1186. ir_assignment *const else_assign =
  1187. new(ctx) ir_assignment(else_deref, op[1]);
  1188. stmt->else_instructions.push_tail(else_assign);
  1189. result = new(ctx) ir_dereference_variable(tmp);
  1190. type = tmp->type;
  1191. }
  1192. break;
  1193. }
  1194. case ast_logic_xor:
  1195. /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
  1196. *
  1197. * "The logical binary operators and (&&), or ( | | ), and
  1198. * exclusive or (^^). They operate only on two Boolean
  1199. * expressions and result in a Boolean expression."
  1200. */
  1201. op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
  1202. &error_emitted);
  1203. op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
  1204. &error_emitted);
  1205. result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1206. op[0], op[1]);
  1207. break;
  1208. case ast_logic_not:
  1209. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1210. "operand", &error_emitted);
  1211. result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1212. op[0], NULL);
  1213. break;
  1214. case ast_mul_assign:
  1215. case ast_div_assign:
  1216. case ast_add_assign:
  1217. case ast_sub_assign: {
  1218. op[0] = this->subexpressions[0]->hir(instructions, state);
  1219. op[1] = this->subexpressions[1]->hir(instructions, state);
  1220. type = arithmetic_result_type(op[0], op[1],
  1221. (this->oper == ast_mul_assign),
  1222. state, & loc);
  1223. ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1224. op[0], op[1]);
  1225. result = do_assignment(instructions, state,
  1226. this->subexpressions[0]->non_lvalue_description,
  1227. op[0]->clone(ctx, NULL), temp_rhs, false,
  1228. this->subexpressions[0]->get_location());
  1229. error_emitted = (op[0]->type->is_error());
  1230. /* GLSL 1.10 does not allow array assignment. However, we don't have to
  1231. * explicitly test for this because none of the binary expression
  1232. * operators allow array operands either.
  1233. */
  1234. break;
  1235. }
  1236. case ast_mod_assign: {
  1237. op[0] = this->subexpressions[0]->hir(instructions, state);
  1238. op[1] = this->subexpressions[1]->hir(instructions, state);
  1239. type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  1240. assert(operations[this->oper] == ir_binop_mod);
  1241. ir_rvalue *temp_rhs;
  1242. temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1243. op[0], op[1]);
  1244. result = do_assignment(instructions, state,
  1245. this->subexpressions[0]->non_lvalue_description,
  1246. op[0]->clone(ctx, NULL), temp_rhs, false,
  1247. this->subexpressions[0]->get_location());
  1248. error_emitted = type->is_error();
  1249. break;
  1250. }
  1251. case ast_ls_assign:
  1252. case ast_rs_assign: {
  1253. op[0] = this->subexpressions[0]->hir(instructions, state);
  1254. op[1] = this->subexpressions[1]->hir(instructions, state);
  1255. type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  1256. &loc);
  1257. ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1258. type, op[0], op[1]);
  1259. result = do_assignment(instructions, state,
  1260. this->subexpressions[0]->non_lvalue_description,
  1261. op[0]->clone(ctx, NULL), temp_rhs, false,
  1262. this->subexpressions[0]->get_location());
  1263. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1264. break;
  1265. }
  1266. case ast_and_assign:
  1267. case ast_xor_assign:
  1268. case ast_or_assign: {
  1269. op[0] = this->subexpressions[0]->hir(instructions, state);
  1270. op[1] = this->subexpressions[1]->hir(instructions, state);
  1271. type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1272. state, &loc);
  1273. ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1274. type, op[0], op[1]);
  1275. result = do_assignment(instructions, state,
  1276. this->subexpressions[0]->non_lvalue_description,
  1277. op[0]->clone(ctx, NULL), temp_rhs, false,
  1278. this->subexpressions[0]->get_location());
  1279. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1280. break;
  1281. }
  1282. case ast_conditional: {
  1283. /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1284. *
  1285. * "The ternary selection operator (?:). It operates on three
  1286. * expressions (exp1 ? exp2 : exp3). This operator evaluates the
  1287. * first expression, which must result in a scalar Boolean."
  1288. */
  1289. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1290. "condition", &error_emitted);
  1291. /* The :? operator is implemented by generating an anonymous temporary
  1292. * followed by an if-statement. The last instruction in each branch of
  1293. * the if-statement assigns a value to the anonymous temporary. This
  1294. * temporary is the r-value of the expression.
  1295. */
  1296. exec_list then_instructions;
  1297. exec_list else_instructions;
  1298. op[1] = this->subexpressions[1]->hir(&then_instructions, state);
  1299. op[2] = this->subexpressions[2]->hir(&else_instructions, state);
  1300. /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1301. *
  1302. * "The second and third expressions can be any type, as
  1303. * long their types match, or there is a conversion in
  1304. * Section 4.1.10 "Implicit Conversions" that can be applied
  1305. * to one of the expressions to make their types match. This
  1306. * resulting matching type is the type of the entire
  1307. * expression."
  1308. */
  1309. if ((!apply_implicit_conversion(op[1]->type, op[2], state)
  1310. && !apply_implicit_conversion(op[2]->type, op[1], state))
  1311. || (op[1]->type != op[2]->type)) {
  1312. YYLTYPE loc = this->subexpressions[1]->get_location();
  1313. _mesa_glsl_error(& loc, state, "second and third operands of ?: "
  1314. "operator must have matching types");
  1315. error_emitted = true;
  1316. type = glsl_type::error_type;
  1317. } else {
  1318. type = op[1]->type;
  1319. }
  1320. /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
  1321. *
  1322. * "The second and third expressions must be the same type, but can
  1323. * be of any type other than an array."
  1324. */
  1325. if (type->is_array() &&
  1326. !state->check_version(120, 300, &loc,
  1327. "second and third operands of ?: operator "
  1328. "cannot be arrays")) {
  1329. error_emitted = true;
  1330. }
  1331. ir_constant *cond_val = op[0]->constant_expression_value();
  1332. ir_constant *then_val = op[1]->constant_expression_value();
  1333. ir_constant *else_val = op[2]->constant_expression_value();
  1334. if (then_instructions.is_empty()
  1335. && else_instructions.is_empty()
  1336. && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
  1337. result = (cond_val->value.b[0]) ? then_val : else_val;
  1338. } else {
  1339. ir_variable *const tmp =
  1340. new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
  1341. instructions->push_tail(tmp);
  1342. ir_if *const stmt = new(ctx) ir_if(op[0]);
  1343. instructions->push_tail(stmt);
  1344. then_instructions.move_nodes_to(& stmt->then_instructions);
  1345. ir_dereference *const then_deref =
  1346. new(ctx) ir_dereference_variable(tmp);
  1347. ir_assignment *const then_assign =
  1348. new(ctx) ir_assignment(then_deref, op[1]);
  1349. stmt->then_instructions.push_tail(then_assign);
  1350. else_instructions.move_nodes_to(& stmt->else_instructions);
  1351. ir_dereference *const else_deref =
  1352. new(ctx) ir_dereference_variable(tmp);
  1353. ir_assignment *const else_assign =
  1354. new(ctx) ir_assignment(else_deref, op[2]);
  1355. stmt->else_instructions.push_tail(else_assign);
  1356. result = new(ctx) ir_dereference_variable(tmp);
  1357. }
  1358. break;
  1359. }
  1360. case ast_pre_inc:
  1361. case ast_pre_dec: {
  1362. this->non_lvalue_description = (this->oper == ast_pre_inc)
  1363. ? "pre-increment operation" : "pre-decrement operation";
  1364. op[0] = this->subexpressions[0]->hir(instructions, state);
  1365. op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
  1366. type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1367. ir_rvalue *temp_rhs;
  1368. temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1369. op[0], op[1]);
  1370. result = do_assignment(instructions, state,
  1371. this->subexpressions[0]->non_lvalue_description,
  1372. op[0]->clone(ctx, NULL), temp_rhs, false,
  1373. this->subexpressions[0]->get_location());
  1374. error_emitted = op[0]->type->is_error();
  1375. break;
  1376. }
  1377. case ast_post_inc:
  1378. case ast_post_dec: {
  1379. this->non_lvalue_description = (this->oper == ast_post_inc)
  1380. ? "post-increment operation" : "post-decrement operation";
  1381. op[0] = this->subexpressions[0]->hir(instructions, state);
  1382. op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
  1383. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1384. type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1385. ir_rvalue *temp_rhs;
  1386. temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1387. op[0], op[1]);
  1388. /* Get a temporary of a copy of the lvalue before it's modified.
  1389. * This may get thrown away later.
  1390. */
  1391. result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
  1392. (void)do_assignment(instructions, state,
  1393. this->subexpressions[0]->non_lvalue_description,
  1394. op[0]->clone(ctx, NULL), temp_rhs, false,
  1395. this->subexpressions[0]->get_location());
  1396. error_emitted = op[0]->type->is_error();
  1397. break;
  1398. }
  1399. case ast_field_selection:
  1400. result = _mesa_ast_field_selection_to_hir(this, instructions, state);
  1401. break;
  1402. case ast_array_index: {
  1403. YYLTYPE index_loc = subexpressions[1]->get_location();
  1404. op[0] = subexpressions[0]->hir(instructions, state);
  1405. op[1] = subexpressions[1]->hir(instructions, state);
  1406. result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
  1407. loc, index_loc);
  1408. if (result->type->is_error())
  1409. error_emitted = true;
  1410. break;
  1411. }
  1412. case ast_function_call:
  1413. /* Should *NEVER* get here. ast_function_call should always be handled
  1414. * by ast_function_expression::hir.
  1415. */
  1416. assert(0);
  1417. break;
  1418. case ast_identifier: {
  1419. /* ast_identifier can appear several places in a full abstract syntax
  1420. * tree. This particular use must be at location specified in the grammar
  1421. * as 'variable_identifier'.
  1422. */
  1423. ir_variable *var =
  1424. state->symbols->get_variable(this->primary_expression.identifier);
  1425. if (var != NULL) {
  1426. var->data.used = true;
  1427. result = new(ctx) ir_dereference_variable(var);
  1428. } else {
  1429. _mesa_glsl_error(& loc, state, "`%s' undeclared",
  1430. this->primary_expression.identifier);
  1431. result = ir_rvalue::error_value(ctx);
  1432. error_emitted = true;
  1433. }
  1434. break;
  1435. }
  1436. case ast_int_constant:
  1437. result = new(ctx) ir_constant(this->primary_expression.int_constant);
  1438. break;
  1439. case ast_uint_constant:
  1440. result = new(ctx) ir_constant(this->primary_expression.uint_constant);
  1441. break;
  1442. case ast_float_constant:
  1443. result = new(ctx) ir_constant(this->primary_expression.float_constant);
  1444. break;
  1445. case ast_bool_constant:
  1446. result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
  1447. break;
  1448. case ast_sequence: {
  1449. /* It should not be possible to generate a sequence in the AST without
  1450. * any expressions in it.
  1451. */
  1452. assert(!this->expressions.is_empty());
  1453. /* The r-value of a sequence is the last expression in the sequence. If
  1454. * the other expressions in the sequence do not have side-effects (and
  1455. * therefore add instructions to the instruction list), they get dropped
  1456. * on the floor.
  1457. */
  1458. exec_node *previous_tail_pred = NULL;
  1459. YYLTYPE previous_operand_loc = loc;
  1460. foreach_list_typed (ast_node, ast, link, &this->expressions) {
  1461. /* If one of the operands of comma operator does not generate any
  1462. * code, we want to emit a warning. At each pass through the loop
  1463. * previous_tail_pred will point to the last instruction in the
  1464. * stream *before* processing the previous operand. Naturally,
  1465. * instructions->tail_pred will point to the last instruction in the
  1466. * stream *after* processing the previous operand. If the two
  1467. * pointers match, then the previous operand had no effect.
  1468. *
  1469. * The warning behavior here differs slightly from GCC. GCC will
  1470. * only emit a warning if none of the left-hand operands have an
  1471. * effect. However, it will emit a warning for each. I believe that
  1472. * there are some cases in C (especially with GCC extensions) where
  1473. * it is useful to have an intermediate step in a sequence have no
  1474. * effect, but I don't think these cases exist in GLSL. Either way,
  1475. * it would be a giant hassle to replicate that behavior.
  1476. */
  1477. if (previous_tail_pred == instructions->tail_pred) {
  1478. _mesa_glsl_warning(&previous_operand_loc, state,
  1479. "left-hand operand of comma expression has "
  1480. "no effect");
  1481. }
  1482. /* tail_pred is directly accessed instead of using the get_tail()
  1483. * method for performance reasons. get_tail() has extra code to
  1484. * return NULL when the list is empty. We don't care about that
  1485. * here, so using tail_pred directly is fine.
  1486. */
  1487. previous_tail_pred = instructions->tail_pred;
  1488. previous_operand_loc = ast->get_location();
  1489. result = ast->hir(instructions, state);
  1490. }
  1491. /* Any errors should have already been emitted in the loop above.
  1492. */
  1493. error_emitted = true;
  1494. break;
  1495. }
  1496. }
  1497. type = NULL; /* use result->type, not type. */
  1498. assert(result != NULL);
  1499. if (result->type->is_error() && !error_emitted)
  1500. _mesa_glsl_error(& loc, state, "type mismatch");
  1501. return result;
  1502. }
  1503. ir_rvalue *
  1504. ast_expression_statement::hir(exec_list *instructions,
  1505. struct _mesa_glsl_parse_state *state)
  1506. {
  1507. /* It is possible to have expression statements that don't have an
  1508. * expression. This is the solitary semicolon:
  1509. *
  1510. * for (i = 0; i < 5; i++)
  1511. * ;
  1512. *
  1513. * In this case the expression will be NULL. Test for NULL and don't do
  1514. * anything in that case.
  1515. */
  1516. if (expression != NULL)
  1517. expression->hir(instructions, state);
  1518. /* Statements do not have r-values.
  1519. */
  1520. return NULL;
  1521. }
  1522. ir_rvalue *
  1523. ast_compound_statement::hir(exec_list *instructions,
  1524. struct _mesa_glsl_parse_state *state)
  1525. {
  1526. if (new_scope)
  1527. state->symbols->push_scope();
  1528. foreach_list_typed (ast_node, ast, link, &this->statements)
  1529. ast->hir(instructions, state);
  1530. if (new_scope)
  1531. state->symbols->pop_scope();
  1532. /* Compound statements do not have r-values.
  1533. */
  1534. return NULL;
  1535. }
  1536. static const glsl_type *
  1537. process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
  1538. struct _mesa_glsl_parse_state *state)
  1539. {
  1540. unsigned length = 0;
  1541. if (base == NULL)
  1542. return glsl_type::error_type;
  1543. /* From page 19 (page 25) of the GLSL 1.20 spec:
  1544. *
  1545. * "Only one-dimensional arrays may be declared."
  1546. */
  1547. if (base->is_array()) {
  1548. _mesa_glsl_error(loc, state,
  1549. "invalid array of `%s' (only one-dimensional arrays "
  1550. "may be declared)",
  1551. base->name);
  1552. return glsl_type::error_type;
  1553. }
  1554. if (array_size != NULL) {
  1555. exec_list dummy_instructions;
  1556. ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
  1557. YYLTYPE loc = array_size->get_location();
  1558. if (ir != NULL) {
  1559. if (!ir->type->is_integer()) {
  1560. _mesa_glsl_error(& loc, state, "array size must be integer type");
  1561. } else if (!ir->type->is_scalar()) {
  1562. _mesa_glsl_error(& loc, state, "array size must be scalar type");
  1563. } else {
  1564. ir_constant *const size = ir->constant_expression_value();
  1565. if (size == NULL) {
  1566. _mesa_glsl_error(& loc, state, "array size must be a "
  1567. "constant valued expression");
  1568. } else if (size->value.i[0] <= 0) {
  1569. _mesa_glsl_error(& loc, state, "array size must be > 0");
  1570. } else {
  1571. assert(size->type == ir->type);
  1572. length = size->value.u[0];
  1573. /* If the array size is const (and we've verified that
  1574. * it is) then no instructions should have been emitted
  1575. * when we converted it to HIR. If they were emitted,
  1576. * then either the array size isn't const after all, or
  1577. * we are emitting unnecessary instructions.
  1578. */
  1579. assert(dummy_instructions.is_empty());
  1580. }
  1581. }
  1582. }
  1583. }
  1584. const glsl_type *array_type = glsl_type::get_array_instance(base, length);
  1585. return array_type != NULL ? array_type : glsl_type::error_type;
  1586. }
  1587. const glsl_type *
  1588. ast_type_specifier::glsl_type(const char **name,
  1589. struct _mesa_glsl_parse_state *state) const
  1590. {
  1591. const struct glsl_type *type;
  1592. type = state->symbols->get_type(this->type_name);
  1593. *name = this->type_name;
  1594. if (this->is_array) {
  1595. YYLTYPE loc = this->get_location();
  1596. type = process_array_type(&loc, type, this->array_size, state);
  1597. }
  1598. return type;
  1599. }
  1600. const glsl_type *
  1601. ast_fully_specified_type::glsl_type(const char **name,
  1602. struct _mesa_glsl_parse_state *state) const
  1603. {
  1604. const struct glsl_type *type = this->specifier->glsl_type(name, state);
  1605. if (type == NULL)
  1606. return NULL;
  1607. if (type->base_type == GLSL_TYPE_FLOAT
  1608. && state->es_shader
  1609. && state->stage == MESA_SHADER_FRAGMENT
  1610. && this->qualifier.precision == ast_precision_none
  1611. && state->symbols->get_variable("#default precision") == NULL) {
  1612. YYLTYPE loc = this->get_location();
  1613. _mesa_glsl_error(&loc, state,
  1614. "no precision specified this scope for type `%s'",
  1615. type->name);
  1616. }
  1617. return type;
  1618. }
  1619. /**
  1620. * Determine whether a toplevel variable declaration declares a varying. This
  1621. * function operates by examining the variable's mode and the shader target,
  1622. * so it correctly identifies linkage variables regardless of whether they are
  1623. * declared using the deprecated "varying" syntax or the new "in/out" syntax.
  1624. *
  1625. * Passing a non-toplevel variable declaration (e.g. a function parameter) to
  1626. * this function will produce undefined results.
  1627. */
  1628. static bool
  1629. is_varying_var(ir_variable *var, gl_shader_stage target)
  1630. {
  1631. switch (target) {
  1632. case MESA_SHADER_VERTEX:
  1633. return var->data.mode == ir_var_shader_out;
  1634. case MESA_SHADER_FRAGMENT:
  1635. return var->data.mode == ir_var_shader_in;
  1636. default:
  1637. return var->data.mode == ir_var_shader_out || var->data.mode == ir_var_shader_in;
  1638. }
  1639. }
  1640. /**
  1641. * Matrix layout qualifiers are only allowed on certain types
  1642. */
  1643. static void
  1644. validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
  1645. YYLTYPE *loc,
  1646. const glsl_type *type,
  1647. ir_variable *var)
  1648. {
  1649. if (var && !var->is_in_uniform_block()) {
  1650. /* Layout qualifiers may only apply to interface blocks and fields in
  1651. * them.
  1652. */
  1653. _mesa_glsl_error(loc, state,
  1654. "uniform block layout qualifiers row_major and "
  1655. "column_major may not be applied to variables "
  1656. "outside of uniform blocks");
  1657. } else if (!type->is_matrix()) {
  1658. /* The OpenGL ES 3.0 conformance tests did not originally allow
  1659. * matrix layout qualifiers on non-matrices. However, the OpenGL
  1660. * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
  1661. * amended to specifically allow these layouts on all types. Emit
  1662. * a warning so that people know their code may not be portable.
  1663. */
  1664. _mesa_glsl_warning(loc, state,
  1665. "uniform block layout qualifiers row_major and "
  1666. "column_major applied to non-matrix types may "
  1667. "be rejected by older compilers");
  1668. } else if (type->is_record()) {
  1669. /* We allow 'layout(row_major)' on structure types because it's the only
  1670. * way to get row-major layouts on matrices contained in structures.
  1671. */
  1672. _mesa_glsl_warning(loc, state,
  1673. "uniform block layout qualifiers row_major and "
  1674. "column_major applied to structure types is not "
  1675. "strictly conformant and may be rejected by other "
  1676. "compilers");
  1677. }
  1678. }
  1679. static bool
  1680. validate_binding_qualifier(struct _mesa_glsl_parse_state *state,
  1681. YYLTYPE *loc,
  1682. ir_variable *var,
  1683. const ast_type_qualifier *qual)
  1684. {
  1685. if (var->data.mode != ir_var_uniform) {
  1686. _mesa_glsl_error(loc, state,
  1687. "the \"binding\" qualifier only applies to uniforms");
  1688. return false;
  1689. }
  1690. if (qual->binding < 0) {
  1691. _mesa_glsl_error(loc, state, "binding values must be >= 0");
  1692. return false;
  1693. }
  1694. const struct gl_context *const ctx = state->ctx;
  1695. unsigned elements = var->type->is_array() ? var->type->length : 1;
  1696. unsigned max_index = qual->binding + elements - 1;
  1697. if (var->type->is_interface()) {
  1698. /* UBOs. From page 60 of the GLSL 4.20 specification:
  1699. * "If the binding point for any uniform block instance is less than zero,
  1700. * or greater than or equal to the implementation-dependent maximum
  1701. * number of uniform buffer bindings, a compilation error will occur.
  1702. * When the binding identifier is used with a uniform block instanced as
  1703. * an array of size N, all elements of the array from binding through
  1704. * binding + N – 1 must be within this range."
  1705. *
  1706. * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
  1707. */
  1708. if (max_index >= ctx->Const.MaxUniformBufferBindings) {
  1709. _mesa_glsl_error(loc, state, "layout(binding = %d) for %d UBOs exceeds "
  1710. "the maximum number of UBO binding points (%d)",
  1711. qual->binding, elements,
  1712. ctx->Const.MaxUniformBufferBindings);
  1713. return false;
  1714. }
  1715. } else if (var->type->is_sampler() ||
  1716. (var->type->is_array() && var->type->fields.array->is_sampler())) {
  1717. /* Samplers. From page 63 of the GLSL 4.20 specification:
  1718. * "If the binding is less than zero, or greater than or equal to the
  1719. * implementation-dependent maximum supported number of units, a
  1720. * compilation error will occur. When the binding identifier is used
  1721. * with an array of size N, all elements of the array from binding
  1722. * through binding + N - 1 must be within this range."
  1723. */
  1724. unsigned limit = 0;
  1725. switch (state->stage) {
  1726. case MESA_SHADER_VERTEX:
  1727. limit = ctx->Const.VertexProgram.MaxTextureImageUnits;
  1728. break;
  1729. case MESA_SHADER_GEOMETRY:
  1730. limit = ctx->Const.GeometryProgram.MaxTextureImageUnits;
  1731. break;
  1732. case MESA_SHADER_FRAGMENT:
  1733. limit = ctx->Const.FragmentProgram.MaxTextureImageUnits;
  1734. break;
  1735. }
  1736. if (max_index >= limit) {
  1737. _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
  1738. "exceeds the maximum number of texture image units "
  1739. "(%d)", qual->binding, elements, limit);
  1740. return false;
  1741. }
  1742. } else if (var->type->contains_atomic()) {
  1743. assert(ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS);
  1744. if (unsigned(qual->binding) >= ctx->Const.MaxAtomicBufferBindings) {
  1745. _mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the "
  1746. " maximum number of atomic counter buffer bindings"
  1747. "(%d)", qual->binding,
  1748. ctx->Const.MaxAtomicBufferBindings);
  1749. return false;
  1750. }
  1751. } else {
  1752. _mesa_glsl_error(loc, state,
  1753. "the \"binding\" qualifier only applies to uniform "
  1754. "blocks, samplers, atomic counters, or arrays thereof");
  1755. return false;
  1756. }
  1757. return true;
  1758. }
  1759. static glsl_interp_qualifier
  1760. interpret_interpolation_qualifier(const struct ast_type_qualifier *qual,
  1761. ir_variable_mode mode,
  1762. struct _mesa_glsl_parse_state *state,
  1763. YYLTYPE *loc)
  1764. {
  1765. glsl_interp_qualifier interpolation;
  1766. if (qual->flags.q.flat)
  1767. interpolation = INTERP_QUALIFIER_FLAT;
  1768. else if (qual->flags.q.noperspective)
  1769. interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
  1770. else if (qual->flags.q.smooth)
  1771. interpolation = INTERP_QUALIFIER_SMOOTH;
  1772. else
  1773. interpolation = INTERP_QUALIFIER_NONE;
  1774. if (interpolation != INTERP_QUALIFIER_NONE) {
  1775. if (mode != ir_var_shader_in && mode != ir_var_shader_out) {
  1776. _mesa_glsl_error(loc, state,
  1777. "interpolation qualifier `%s' can only be applied to "
  1778. "shader inputs or outputs.",
  1779. interpolation_string(interpolation));
  1780. }
  1781. if ((state->stage == MESA_SHADER_VERTEX && mode == ir_var_shader_in) ||
  1782. (state->stage == MESA_SHADER_FRAGMENT && mode == ir_var_shader_out)) {
  1783. _mesa_glsl_error(loc, state,
  1784. "interpolation qualifier `%s' cannot be applied to "
  1785. "vertex shader inputs or fragment shader outputs",
  1786. interpolation_string(interpolation));
  1787. }
  1788. }
  1789. return interpolation;
  1790. }
  1791. static void
  1792. validate_explicit_location(const struct ast_type_qualifier *qual,
  1793. ir_variable *var,
  1794. struct _mesa_glsl_parse_state *state,
  1795. YYLTYPE *loc)
  1796. {
  1797. bool fail = false;
  1798. /* In the vertex shader only shader inputs can be given explicit
  1799. * locations.
  1800. *
  1801. * In the fragment shader only shader outputs can be given explicit
  1802. * locations.
  1803. */
  1804. switch (state->stage) {
  1805. case MESA_SHADER_VERTEX:
  1806. if (var->data.mode == ir_var_shader_in) {
  1807. if (!state->check_explicit_attrib_location_allowed(loc, var))
  1808. return;
  1809. break;
  1810. }
  1811. fail = true;
  1812. break;
  1813. case MESA_SHADER_GEOMETRY:
  1814. _mesa_glsl_error(loc, state,
  1815. "geometry shader variables cannot be given "
  1816. "explicit locations");
  1817. return;
  1818. case MESA_SHADER_FRAGMENT:
  1819. if (var->data.mode == ir_var_shader_out) {
  1820. if (!state->check_explicit_attrib_location_allowed(loc, var))
  1821. return;
  1822. break;
  1823. }
  1824. fail = true;
  1825. break;
  1826. };
  1827. if (fail) {
  1828. _mesa_glsl_error(loc, state,
  1829. "%s cannot be given an explicit location in %s shader",
  1830. mode_string(var),
  1831. _mesa_shader_stage_to_string(state->stage));
  1832. } else {
  1833. var->data.explicit_location = true;
  1834. /* This bit of silliness is needed because invalid explicit locations
  1835. * are supposed to be flagged during linking. Small negative values
  1836. * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
  1837. * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
  1838. * The linker needs to be able to differentiate these cases. This
  1839. * ensures that negative values stay negative.
  1840. */
  1841. if (qual->location >= 0) {
  1842. var->data.location = (state->stage == MESA_SHADER_VERTEX)
  1843. ? (qual->location + VERT_ATTRIB_GENERIC0)
  1844. : (qual->location + FRAG_RESULT_DATA0);
  1845. } else {
  1846. var->data.location = qual->location;
  1847. }
  1848. if (qual->flags.q.explicit_index) {
  1849. /* From the GLSL 4.30 specification, section 4.4.2 (Output
  1850. * Layout Qualifiers):
  1851. *
  1852. * "It is also a compile-time error if a fragment shader
  1853. * sets a layout index to less than 0 or greater than 1."
  1854. *
  1855. * Older specifications don't mandate a behavior; we take
  1856. * this as a clarification and always generate the error.
  1857. */
  1858. if (qual->index < 0 || qual->index > 1) {
  1859. _mesa_glsl_error(loc, state,
  1860. "explicit index may only be 0 or 1");
  1861. } else {
  1862. var->data.explicit_index = true;
  1863. var->data.index = qual->index;
  1864. }
  1865. }
  1866. }
  1867. return;
  1868. }
  1869. static void
  1870. apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
  1871. ir_variable *var,
  1872. struct _mesa_glsl_parse_state *state,
  1873. YYLTYPE *loc,
  1874. bool is_parameter)
  1875. {
  1876. STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i));
  1877. if (qual->flags.q.invariant) {
  1878. if (var->data.used) {
  1879. _mesa_glsl_error(loc, state,
  1880. "variable `%s' may not be redeclared "
  1881. "`invariant' after being used",
  1882. var->name);
  1883. } else {
  1884. var->data.invariant = 1;
  1885. }
  1886. }
  1887. if (qual->flags.q.constant || qual->flags.q.attribute
  1888. || qual->flags.q.uniform
  1889. || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
  1890. var->data.read_only = 1;
  1891. if (qual->flags.q.centroid)
  1892. var->data.centroid = 1;
  1893. if (qual->flags.q.sample)
  1894. var->data.sample = 1;
  1895. if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) {
  1896. var->type = glsl_type::error_type;
  1897. _mesa_glsl_error(loc, state,
  1898. "`attribute' variables may not be declared in the "
  1899. "%s shader",
  1900. _mesa_shader_stage_to_string(state->stage));
  1901. }
  1902. /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
  1903. *
  1904. * "However, the const qualifier cannot be used with out or inout."
  1905. *
  1906. * The same section of the GLSL 4.40 spec further clarifies this saying:
  1907. *
  1908. * "The const qualifier cannot be used with out or inout, or a
  1909. * compile-time error results."
  1910. */
  1911. if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
  1912. _mesa_glsl_error(loc, state,
  1913. "`const' may not be applied to `out' or `inout' "
  1914. "function parameters");
  1915. }
  1916. /* If there is no qualifier that changes the mode of the variable, leave
  1917. * the setting alone.
  1918. */
  1919. if (qual->flags.q.in && qual->flags.q.out)
  1920. var->data.mode = ir_var_function_inout;
  1921. else if (qual->flags.q.in)
  1922. var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
  1923. else if (qual->flags.q.attribute
  1924. || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
  1925. var->data.mode = ir_var_shader_in;
  1926. else if (qual->flags.q.out)
  1927. var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
  1928. else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX))
  1929. var->data.mode = ir_var_shader_out;
  1930. else if (qual->flags.q.uniform)
  1931. var->data.mode = ir_var_uniform;
  1932. if (!is_parameter && is_varying_var(var, state->stage)) {
  1933. /* This variable is being used to link data between shader stages (in
  1934. * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
  1935. * that is allowed for such purposes.
  1936. *
  1937. * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
  1938. *
  1939. * "The varying qualifier can be used only with the data types
  1940. * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
  1941. * these."
  1942. *
  1943. * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
  1944. * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
  1945. *
  1946. * "Fragment inputs can only be signed and unsigned integers and
  1947. * integer vectors, float, floating-point vectors, matrices, or
  1948. * arrays of these. Structures cannot be input.
  1949. *
  1950. * Similar text exists in the section on vertex shader outputs.
  1951. *
  1952. * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
  1953. * 3.00 spec allows structs as well. Varying structs are also allowed
  1954. * in GLSL 1.50.
  1955. */
  1956. switch (var->type->get_scalar_type()->base_type) {
  1957. case GLSL_TYPE_FLOAT:
  1958. /* Ok in all GLSL versions */
  1959. break;
  1960. case GLSL_TYPE_UINT:
  1961. case GLSL_TYPE_INT:
  1962. if (state->is_version(130, 300))
  1963. break;
  1964. _mesa_glsl_error(loc, state,
  1965. "varying variables must be of base type float in %s",
  1966. state->get_version_string());
  1967. break;
  1968. case GLSL_TYPE_STRUCT:
  1969. if (state->is_version(150, 300))
  1970. break;
  1971. _mesa_glsl_error(loc, state,
  1972. "varying variables may not be of type struct");
  1973. break;
  1974. default:
  1975. _mesa_glsl_error(loc, state, "illegal type for a varying variable");
  1976. break;
  1977. }
  1978. }
  1979. if (state->all_invariant && (state->current_function == NULL)) {
  1980. switch (state->stage) {
  1981. case MESA_SHADER_VERTEX:
  1982. if (var->data.mode == ir_var_shader_out)
  1983. var->data.invariant = true;
  1984. break;
  1985. case MESA_SHADER_GEOMETRY:
  1986. if ((var->data.mode == ir_var_shader_in)
  1987. || (var->data.mode == ir_var_shader_out))
  1988. var->data.invariant = true;
  1989. break;
  1990. case MESA_SHADER_FRAGMENT:
  1991. if (var->data.mode == ir_var_shader_in)
  1992. var->data.invariant = true;
  1993. break;
  1994. }
  1995. }
  1996. var->data.interpolation =
  1997. interpret_interpolation_qualifier(qual, (ir_variable_mode) var->data.mode,
  1998. state, loc);
  1999. var->data.pixel_center_integer = qual->flags.q.pixel_center_integer;
  2000. var->data.origin_upper_left = qual->flags.q.origin_upper_left;
  2001. if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
  2002. && (strcmp(var->name, "gl_FragCoord") != 0)) {
  2003. const char *const qual_string = (qual->flags.q.origin_upper_left)
  2004. ? "origin_upper_left" : "pixel_center_integer";
  2005. _mesa_glsl_error(loc, state,
  2006. "layout qualifier `%s' can only be applied to "
  2007. "fragment shader input `gl_FragCoord'",
  2008. qual_string);
  2009. }
  2010. if (qual->flags.q.explicit_location) {
  2011. validate_explicit_location(qual, var, state, loc);
  2012. } else if (qual->flags.q.explicit_index) {
  2013. _mesa_glsl_error(loc, state,
  2014. "explicit index requires explicit location");
  2015. }
  2016. if (qual->flags.q.explicit_binding &&
  2017. validate_binding_qualifier(state, loc, var, qual)) {
  2018. var->data.explicit_binding = true;
  2019. var->data.binding = qual->binding;
  2020. }
  2021. if (var->type->contains_atomic()) {
  2022. if (var->data.mode == ir_var_uniform) {
  2023. if (var->data.explicit_binding) {
  2024. unsigned *offset =
  2025. &state->atomic_counter_offsets[var->data.binding];
  2026. if (*offset % ATOMIC_COUNTER_SIZE)
  2027. _mesa_glsl_error(loc, state,
  2028. "misaligned atomic counter offset");
  2029. var->data.atomic.offset = *offset;
  2030. *offset += var->type->atomic_size();
  2031. } else {
  2032. _mesa_glsl_error(loc, state,
  2033. "atomic counters require explicit binding point");
  2034. }
  2035. } else if (var->data.mode != ir_var_function_in) {
  2036. _mesa_glsl_error(loc, state, "atomic counters may only be declared as "
  2037. "function parameters or uniform-qualified "
  2038. "global variables");
  2039. }
  2040. }
  2041. /* Does the declaration use the deprecated 'attribute' or 'varying'
  2042. * keywords?
  2043. */
  2044. const bool uses_deprecated_qualifier = qual->flags.q.attribute
  2045. || qual->flags.q.varying;
  2046. /* Is the 'layout' keyword used with parameters that allow relaxed checking.
  2047. * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
  2048. * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
  2049. * allowed the layout qualifier to be used with 'varying' and 'attribute'.
  2050. * These extensions and all following extensions that add the 'layout'
  2051. * keyword have been modified to require the use of 'in' or 'out'.
  2052. *
  2053. * The following extension do not allow the deprecated keywords:
  2054. *
  2055. * GL_AMD_conservative_depth
  2056. * GL_ARB_conservative_depth
  2057. * GL_ARB_gpu_shader5
  2058. * GL_ARB_separate_shader_objects
  2059. * GL_ARB_tesselation_shader
  2060. * GL_ARB_transform_feedback3
  2061. * GL_ARB_uniform_buffer_object
  2062. *
  2063. * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
  2064. * allow layout with the deprecated keywords.
  2065. */
  2066. const bool relaxed_layout_qualifier_checking =
  2067. state->ARB_fragment_coord_conventions_enable;
  2068. if (qual->has_layout() && uses_deprecated_qualifier) {
  2069. if (relaxed_layout_qualifier_checking) {
  2070. _mesa_glsl_warning(loc, state,
  2071. "`layout' qualifier may not be used with "
  2072. "`attribute' or `varying'");
  2073. } else {
  2074. _mesa_glsl_error(loc, state,
  2075. "`layout' qualifier may not be used with "
  2076. "`attribute' or `varying'");
  2077. }
  2078. }
  2079. /* Layout qualifiers for gl_FragDepth, which are enabled by extension
  2080. * AMD_conservative_depth.
  2081. */
  2082. int depth_layout_count = qual->flags.q.depth_any
  2083. + qual->flags.q.depth_greater
  2084. + qual->flags.q.depth_less
  2085. + qual->flags.q.depth_unchanged;
  2086. if (depth_layout_count > 0
  2087. && !state->AMD_conservative_depth_enable
  2088. && !state->ARB_conservative_depth_enable) {
  2089. _mesa_glsl_error(loc, state,
  2090. "extension GL_AMD_conservative_depth or "
  2091. "GL_ARB_conservative_depth must be enabled "
  2092. "to use depth layout qualifiers");
  2093. } else if (depth_layout_count > 0
  2094. && strcmp(var->name, "gl_FragDepth") != 0) {
  2095. _mesa_glsl_error(loc, state,
  2096. "depth layout qualifiers can be applied only to "
  2097. "gl_FragDepth");
  2098. } else if (depth_layout_count > 1
  2099. && strcmp(var->name, "gl_FragDepth") == 0) {
  2100. _mesa_glsl_error(loc, state,
  2101. "at most one depth layout qualifier can be applied to "
  2102. "gl_FragDepth");
  2103. }
  2104. if (qual->flags.q.depth_any)
  2105. var->data.depth_layout = ir_depth_layout_any;
  2106. else if (qual->flags.q.depth_greater)
  2107. var->data.depth_layout = ir_depth_layout_greater;
  2108. else if (qual->flags.q.depth_less)
  2109. var->data.depth_layout = ir_depth_layout_less;
  2110. else if (qual->flags.q.depth_unchanged)
  2111. var->data.depth_layout = ir_depth_layout_unchanged;
  2112. else
  2113. var->data.depth_layout = ir_depth_layout_none;
  2114. if (qual->flags.q.std140 ||
  2115. qual->flags.q.packed ||
  2116. qual->flags.q.shared) {
  2117. _mesa_glsl_error(loc, state,
  2118. "uniform block layout qualifiers std140, packed, and "
  2119. "shared can only be applied to uniform blocks, not "
  2120. "members");
  2121. }
  2122. if (qual->flags.q.row_major || qual->flags.q.column_major) {
  2123. validate_matrix_layout_for_type(state, loc, var->type, var);
  2124. }
  2125. }
  2126. /**
  2127. * Get the variable that is being redeclared by this declaration
  2128. *
  2129. * Semantic checks to verify the validity of the redeclaration are also
  2130. * performed. If semantic checks fail, compilation error will be emitted via
  2131. * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
  2132. *
  2133. * \returns
  2134. * A pointer to an existing variable in the current scope if the declaration
  2135. * is a redeclaration, \c NULL otherwise.
  2136. */
  2137. static ir_variable *
  2138. get_variable_being_redeclared(ir_variable *var, YYLTYPE loc,
  2139. struct _mesa_glsl_parse_state *state,
  2140. bool allow_all_redeclarations)
  2141. {
  2142. /* Check if this declaration is actually a re-declaration, either to
  2143. * resize an array or add qualifiers to an existing variable.
  2144. *
  2145. * This is allowed for variables in the current scope, or when at
  2146. * global scope (for built-ins in the implicit outer scope).
  2147. */
  2148. ir_variable *earlier = state->symbols->get_variable(var->name);
  2149. if (earlier == NULL ||
  2150. (state->current_function != NULL &&
  2151. !state->symbols->name_declared_this_scope(var->name))) {
  2152. return NULL;
  2153. }
  2154. /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
  2155. *
  2156. * "It is legal to declare an array without a size and then
  2157. * later re-declare the same name as an array of the same
  2158. * type and specify a size."
  2159. */
  2160. if (earlier->type->is_unsized_array() && var->type->is_array()
  2161. && (var->type->element_type() == earlier->type->element_type())) {
  2162. /* FINISHME: This doesn't match the qualifiers on the two
  2163. * FINISHME: declarations. It's not 100% clear whether this is
  2164. * FINISHME: required or not.
  2165. */
  2166. const unsigned size = unsigned(var->type->array_size());
  2167. check_builtin_array_max_size(var->name, size, loc, state);
  2168. if ((size > 0) && (size <= earlier->data.max_array_access)) {
  2169. _mesa_glsl_error(& loc, state, "array size must be > %u due to "
  2170. "previous access",
  2171. earlier->data.max_array_access);
  2172. }
  2173. earlier->type = var->type;
  2174. delete var;
  2175. var = NULL;
  2176. } else if ((state->ARB_fragment_coord_conventions_enable ||
  2177. state->is_version(150, 0))
  2178. && strcmp(var->name, "gl_FragCoord") == 0
  2179. && earlier->type == var->type
  2180. && earlier->data.mode == var->data.mode) {
  2181. /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
  2182. * qualifiers.
  2183. */
  2184. earlier->data.origin_upper_left = var->data.origin_upper_left;
  2185. earlier->data.pixel_center_integer = var->data.pixel_center_integer;
  2186. /* According to section 4.3.7 of the GLSL 1.30 spec,
  2187. * the following built-in varaibles can be redeclared with an
  2188. * interpolation qualifier:
  2189. * * gl_FrontColor
  2190. * * gl_BackColor
  2191. * * gl_FrontSecondaryColor
  2192. * * gl_BackSecondaryColor
  2193. * * gl_Color
  2194. * * gl_SecondaryColor
  2195. */
  2196. } else if (state->is_version(130, 0)
  2197. && (strcmp(var->name, "gl_FrontColor") == 0
  2198. || strcmp(var->name, "gl_BackColor") == 0
  2199. || strcmp(var->name, "gl_FrontSecondaryColor") == 0
  2200. || strcmp(var->name, "gl_BackSecondaryColor") == 0
  2201. || strcmp(var->name, "gl_Color") == 0
  2202. || strcmp(var->name, "gl_SecondaryColor") == 0)
  2203. && earlier->type == var->type
  2204. && earlier->data.mode == var->data.mode) {
  2205. earlier->data.interpolation = var->data.interpolation;
  2206. /* Layout qualifiers for gl_FragDepth. */
  2207. } else if ((state->AMD_conservative_depth_enable ||
  2208. state->ARB_conservative_depth_enable)
  2209. && strcmp(var->name, "gl_FragDepth") == 0
  2210. && earlier->type == var->type
  2211. && earlier->data.mode == var->data.mode) {
  2212. /** From the AMD_conservative_depth spec:
  2213. * Within any shader, the first redeclarations of gl_FragDepth
  2214. * must appear before any use of gl_FragDepth.
  2215. */
  2216. if (earlier->data.used) {
  2217. _mesa_glsl_error(&loc, state,
  2218. "the first redeclaration of gl_FragDepth "
  2219. "must appear before any use of gl_FragDepth");
  2220. }
  2221. /* Prevent inconsistent redeclaration of depth layout qualifier. */
  2222. if (earlier->data.depth_layout != ir_depth_layout_none
  2223. && earlier->data.depth_layout != var->data.depth_layout) {
  2224. _mesa_glsl_error(&loc, state,
  2225. "gl_FragDepth: depth layout is declared here "
  2226. "as '%s, but it was previously declared as "
  2227. "'%s'",
  2228. depth_layout_string(var->data.depth_layout),
  2229. depth_layout_string(earlier->data.depth_layout));
  2230. }
  2231. earlier->data.depth_layout = var->data.depth_layout;
  2232. } else if (allow_all_redeclarations) {
  2233. if (earlier->data.mode != var->data.mode) {
  2234. _mesa_glsl_error(&loc, state,
  2235. "redeclaration of `%s' with incorrect qualifiers",
  2236. var->name);
  2237. } else if (earlier->type != var->type) {
  2238. _mesa_glsl_error(&loc, state,
  2239. "redeclaration of `%s' has incorrect type",
  2240. var->name);
  2241. }
  2242. } else {
  2243. _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
  2244. }
  2245. return earlier;
  2246. }
  2247. /**
  2248. * Generate the IR for an initializer in a variable declaration
  2249. */
  2250. ir_rvalue *
  2251. process_initializer(ir_variable *var, ast_declaration *decl,
  2252. ast_fully_specified_type *type,
  2253. exec_list *initializer_instructions,
  2254. struct _mesa_glsl_parse_state *state)
  2255. {
  2256. ir_rvalue *result = NULL;
  2257. YYLTYPE initializer_loc = decl->initializer->get_location();
  2258. /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
  2259. *
  2260. * "All uniform variables are read-only and are initialized either
  2261. * directly by an application via API commands, or indirectly by
  2262. * OpenGL."
  2263. */
  2264. if (var->data.mode == ir_var_uniform) {
  2265. state->check_version(120, 0, &initializer_loc,
  2266. "cannot initialize uniforms");
  2267. }
  2268. if (var->type->is_sampler()) {
  2269. _mesa_glsl_error(& initializer_loc, state,
  2270. "cannot initialize samplers");
  2271. }
  2272. if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL)) {
  2273. _mesa_glsl_error(& initializer_loc, state,
  2274. "cannot initialize %s shader input / %s",
  2275. _mesa_shader_stage_to_string(state->stage),
  2276. (state->stage == MESA_SHADER_VERTEX)
  2277. ? "attribute" : "varying");
  2278. }
  2279. ir_dereference *const lhs = new(state) ir_dereference_variable(var);
  2280. ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
  2281. state);
  2282. /* Calculate the constant value if this is a const or uniform
  2283. * declaration.
  2284. */
  2285. if (type->qualifier.flags.q.constant
  2286. || type->qualifier.flags.q.uniform) {
  2287. ir_rvalue *new_rhs = validate_assignment(state, initializer_loc,
  2288. var->type, rhs, true);
  2289. if (new_rhs != NULL) {
  2290. rhs = new_rhs;
  2291. ir_constant *constant_value = rhs->constant_expression_value();
  2292. if (!constant_value) {
  2293. /* If ARB_shading_language_420pack is enabled, initializers of
  2294. * const-qualified local variables do not have to be constant
  2295. * expressions. Const-qualified global variables must still be
  2296. * initialized with constant expressions.
  2297. */
  2298. if (!state->ARB_shading_language_420pack_enable
  2299. || state->current_function == NULL) {
  2300. _mesa_glsl_error(& initializer_loc, state,
  2301. "initializer of %s variable `%s' must be a "
  2302. "constant expression",
  2303. (type->qualifier.flags.q.constant)
  2304. ? "const" : "uniform",
  2305. decl->identifier);
  2306. if (var->type->is_numeric()) {
  2307. /* Reduce cascading errors. */
  2308. var->constant_value = ir_constant::zero(state, var->type);
  2309. }
  2310. }
  2311. } else {
  2312. rhs = constant_value;
  2313. var->constant_value = constant_value;
  2314. }
  2315. } else {
  2316. if (var->type->is_numeric()) {
  2317. /* Reduce cascading errors. */
  2318. var->constant_value = ir_constant::zero(state, var->type);
  2319. }
  2320. }
  2321. }
  2322. if (rhs && !rhs->type->is_error()) {
  2323. bool temp = var->data.read_only;
  2324. if (type->qualifier.flags.q.constant)
  2325. var->data.read_only = false;
  2326. /* Never emit code to initialize a uniform.
  2327. */
  2328. const glsl_type *initializer_type;
  2329. if (!type->qualifier.flags.q.uniform) {
  2330. result = do_assignment(initializer_instructions, state,
  2331. NULL,
  2332. lhs, rhs, true,
  2333. type->get_location());
  2334. initializer_type = result->type;
  2335. } else
  2336. initializer_type = rhs->type;
  2337. var->constant_initializer = rhs->constant_expression_value();
  2338. var->data.has_initializer = true;
  2339. /* If the declared variable is an unsized array, it must inherrit
  2340. * its full type from the initializer. A declaration such as
  2341. *
  2342. * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
  2343. *
  2344. * becomes
  2345. *
  2346. * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
  2347. *
  2348. * The assignment generated in the if-statement (below) will also
  2349. * automatically handle this case for non-uniforms.
  2350. *
  2351. * If the declared variable is not an array, the types must
  2352. * already match exactly. As a result, the type assignment
  2353. * here can be done unconditionally. For non-uniforms the call
  2354. * to do_assignment can change the type of the initializer (via
  2355. * the implicit conversion rules). For uniforms the initializer
  2356. * must be a constant expression, and the type of that expression
  2357. * was validated above.
  2358. */
  2359. var->type = initializer_type;
  2360. var->data.read_only = temp;
  2361. }
  2362. return result;
  2363. }
  2364. /**
  2365. * Do additional processing necessary for geometry shader input declarations
  2366. * (this covers both interface blocks arrays and bare input variables).
  2367. */
  2368. static void
  2369. handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
  2370. YYLTYPE loc, ir_variable *var)
  2371. {
  2372. unsigned num_vertices = 0;
  2373. if (state->gs_input_prim_type_specified) {
  2374. num_vertices = vertices_per_prim(state->gs_input_prim_type);
  2375. }
  2376. /* Geometry shader input variables must be arrays. Caller should have
  2377. * reported an error for this.
  2378. */
  2379. if (!var->type->is_array()) {
  2380. assert(state->error);
  2381. /* To avoid cascading failures, short circuit the checks below. */
  2382. return;
  2383. }
  2384. if (var->type->is_unsized_array()) {
  2385. /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
  2386. *
  2387. * All geometry shader input unsized array declarations will be
  2388. * sized by an earlier input layout qualifier, when present, as per
  2389. * the following table.
  2390. *
  2391. * Followed by a table mapping each allowed input layout qualifier to
  2392. * the corresponding input length.
  2393. */
  2394. if (num_vertices != 0)
  2395. var->type = glsl_type::get_array_instance(var->type->fields.array,
  2396. num_vertices);
  2397. } else {
  2398. /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
  2399. * includes the following examples of compile-time errors:
  2400. *
  2401. * // code sequence within one shader...
  2402. * in vec4 Color1[]; // size unknown
  2403. * ...Color1.length()...// illegal, length() unknown
  2404. * in vec4 Color2[2]; // size is 2
  2405. * ...Color1.length()...// illegal, Color1 still has no size
  2406. * in vec4 Color3[3]; // illegal, input sizes are inconsistent
  2407. * layout(lines) in; // legal, input size is 2, matching
  2408. * in vec4 Color4[3]; // illegal, contradicts layout
  2409. * ...
  2410. *
  2411. * To detect the case illustrated by Color3, we verify that the size of
  2412. * an explicitly-sized array matches the size of any previously declared
  2413. * explicitly-sized array. To detect the case illustrated by Color4, we
  2414. * verify that the size of an explicitly-sized array is consistent with
  2415. * any previously declared input layout.
  2416. */
  2417. if (num_vertices != 0 && var->type->length != num_vertices) {
  2418. _mesa_glsl_error(&loc, state,
  2419. "geometry shader input size contradicts previously"
  2420. " declared layout (size is %u, but layout requires a"
  2421. " size of %u)", var->type->length, num_vertices);
  2422. } else if (state->gs_input_size != 0 &&
  2423. var->type->length != state->gs_input_size) {
  2424. _mesa_glsl_error(&loc, state,
  2425. "geometry shader input sizes are "
  2426. "inconsistent (size is %u, but a previous "
  2427. "declaration has size %u)",
  2428. var->type->length, state->gs_input_size);
  2429. } else {
  2430. state->gs_input_size = var->type->length;
  2431. }
  2432. }
  2433. }
  2434. void
  2435. validate_identifier(const char *identifier, YYLTYPE loc,
  2436. struct _mesa_glsl_parse_state *state)
  2437. {
  2438. /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  2439. *
  2440. * "Identifiers starting with "gl_" are reserved for use by
  2441. * OpenGL, and may not be declared in a shader as either a
  2442. * variable or a function."
  2443. */
  2444. if (strncmp(identifier, "gl_", 3) == 0) {
  2445. _mesa_glsl_error(&loc, state,
  2446. "identifier `%s' uses reserved `gl_' prefix",
  2447. identifier);
  2448. } else if (strstr(identifier, "__")) {
  2449. /* From page 14 (page 20 of the PDF) of the GLSL 1.10
  2450. * spec:
  2451. *
  2452. * "In addition, all identifiers containing two
  2453. * consecutive underscores (__) are reserved as
  2454. * possible future keywords."
  2455. */
  2456. _mesa_glsl_error(&loc, state,
  2457. "identifier `%s' uses reserved `__' string",
  2458. identifier);
  2459. }
  2460. }
  2461. ir_rvalue *
  2462. ast_declarator_list::hir(exec_list *instructions,
  2463. struct _mesa_glsl_parse_state *state)
  2464. {
  2465. void *ctx = state;
  2466. const struct glsl_type *decl_type;
  2467. const char *type_name = NULL;
  2468. ir_rvalue *result = NULL;
  2469. YYLTYPE loc = this->get_location();
  2470. /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
  2471. *
  2472. * "To ensure that a particular output variable is invariant, it is
  2473. * necessary to use the invariant qualifier. It can either be used to
  2474. * qualify a previously declared variable as being invariant
  2475. *
  2476. * invariant gl_Position; // make existing gl_Position be invariant"
  2477. *
  2478. * In these cases the parser will set the 'invariant' flag in the declarator
  2479. * list, and the type will be NULL.
  2480. */
  2481. if (this->invariant) {
  2482. assert(this->type == NULL);
  2483. if (state->current_function != NULL) {
  2484. _mesa_glsl_error(& loc, state,
  2485. "all uses of `invariant' keyword must be at global "
  2486. "scope");
  2487. }
  2488. foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2489. assert(!decl->is_array);
  2490. assert(decl->array_size == NULL);
  2491. assert(decl->initializer == NULL);
  2492. ir_variable *const earlier =
  2493. state->symbols->get_variable(decl->identifier);
  2494. if (earlier == NULL) {
  2495. _mesa_glsl_error(& loc, state,
  2496. "undeclared variable `%s' cannot be marked "
  2497. "invariant", decl->identifier);
  2498. } else if ((state->stage == MESA_SHADER_VERTEX)
  2499. && (earlier->data.mode != ir_var_shader_out)) {
  2500. _mesa_glsl_error(& loc, state,
  2501. "`%s' cannot be marked invariant, vertex shader "
  2502. "outputs only", decl->identifier);
  2503. } else if ((state->stage == MESA_SHADER_FRAGMENT)
  2504. && (earlier->data.mode != ir_var_shader_in)) {
  2505. _mesa_glsl_error(& loc, state,
  2506. "`%s' cannot be marked invariant, fragment shader "
  2507. "inputs only", decl->identifier);
  2508. } else if (earlier->data.used) {
  2509. _mesa_glsl_error(& loc, state,
  2510. "variable `%s' may not be redeclared "
  2511. "`invariant' after being used",
  2512. earlier->name);
  2513. } else {
  2514. earlier->data.invariant = true;
  2515. }
  2516. }
  2517. /* Invariant redeclarations do not have r-values.
  2518. */
  2519. return NULL;
  2520. }
  2521. assert(this->type != NULL);
  2522. assert(!this->invariant);
  2523. /* The type specifier may contain a structure definition. Process that
  2524. * before any of the variable declarations.
  2525. */
  2526. (void) this->type->specifier->hir(instructions, state);
  2527. decl_type = this->type->glsl_type(& type_name, state);
  2528. /* An offset-qualified atomic counter declaration sets the default
  2529. * offset for the next declaration within the same atomic counter
  2530. * buffer.
  2531. */
  2532. if (decl_type && decl_type->contains_atomic()) {
  2533. if (type->qualifier.flags.q.explicit_binding &&
  2534. type->qualifier.flags.q.explicit_offset)
  2535. state->atomic_counter_offsets[type->qualifier.binding] =
  2536. type->qualifier.offset;
  2537. }
  2538. if (this->declarations.is_empty()) {
  2539. /* If there is no structure involved in the program text, there are two
  2540. * possible scenarios:
  2541. *
  2542. * - The program text contained something like 'vec4;'. This is an
  2543. * empty declaration. It is valid but weird. Emit a warning.
  2544. *
  2545. * - The program text contained something like 'S;' and 'S' is not the
  2546. * name of a known structure type. This is both invalid and weird.
  2547. * Emit an error.
  2548. *
  2549. * - The program text contained something like 'mediump float;'
  2550. * when the programmer probably meant 'precision mediump
  2551. * float;' Emit a warning with a description of what they
  2552. * probably meant to do.
  2553. *
  2554. * Note that if decl_type is NULL and there is a structure involved,
  2555. * there must have been some sort of error with the structure. In this
  2556. * case we assume that an error was already generated on this line of
  2557. * code for the structure. There is no need to generate an additional,
  2558. * confusing error.
  2559. */
  2560. assert(this->type->specifier->structure == NULL || decl_type != NULL
  2561. || state->error);
  2562. if (decl_type == NULL) {
  2563. _mesa_glsl_error(&loc, state,
  2564. "invalid type `%s' in empty declaration",
  2565. type_name);
  2566. } else if (decl_type->base_type == GLSL_TYPE_ATOMIC_UINT) {
  2567. /* Empty atomic counter declarations are allowed and useful
  2568. * to set the default offset qualifier.
  2569. */
  2570. return NULL;
  2571. } else if (this->type->qualifier.precision != ast_precision_none) {
  2572. if (this->type->specifier->structure != NULL) {
  2573. _mesa_glsl_error(&loc, state,
  2574. "precision qualifiers can't be applied "
  2575. "to structures");
  2576. } else {
  2577. static const char *const precision_names[] = {
  2578. "highp",
  2579. "highp",
  2580. "mediump",
  2581. "lowp"
  2582. };
  2583. _mesa_glsl_warning(&loc, state,
  2584. "empty declaration with precision qualifier, "
  2585. "to set the default precision, use "
  2586. "`precision %s %s;'",
  2587. precision_names[this->type->qualifier.precision],
  2588. type_name);
  2589. }
  2590. } else if (this->type->specifier->structure == NULL) {
  2591. _mesa_glsl_warning(&loc, state, "empty declaration");
  2592. }
  2593. }
  2594. foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2595. const struct glsl_type *var_type;
  2596. ir_variable *var;
  2597. /* FINISHME: Emit a warning if a variable declaration shadows a
  2598. * FINISHME: declaration at a higher scope.
  2599. */
  2600. if ((decl_type == NULL) || decl_type->is_void()) {
  2601. if (type_name != NULL) {
  2602. _mesa_glsl_error(& loc, state,
  2603. "invalid type `%s' in declaration of `%s'",
  2604. type_name, decl->identifier);
  2605. } else {
  2606. _mesa_glsl_error(& loc, state,
  2607. "invalid type in declaration of `%s'",
  2608. decl->identifier);
  2609. }
  2610. continue;
  2611. }
  2612. if (decl->is_array) {
  2613. var_type = process_array_type(&loc, decl_type, decl->array_size,
  2614. state);
  2615. if (var_type->is_error())
  2616. continue;
  2617. } else {
  2618. var_type = decl_type;
  2619. }
  2620. var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
  2621. /* The 'varying in' and 'varying out' qualifiers can only be used with
  2622. * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
  2623. * yet.
  2624. */
  2625. if (this->type->qualifier.flags.q.varying) {
  2626. if (this->type->qualifier.flags.q.in) {
  2627. _mesa_glsl_error(& loc, state,
  2628. "`varying in' qualifier in declaration of "
  2629. "`%s' only valid for geometry shaders using "
  2630. "ARB_geometry_shader4 or EXT_geometry_shader4",
  2631. decl->identifier);
  2632. } else if (this->type->qualifier.flags.q.out) {
  2633. _mesa_glsl_error(& loc, state,
  2634. "`varying out' qualifier in declaration of "
  2635. "`%s' only valid for geometry shaders using "
  2636. "ARB_geometry_shader4 or EXT_geometry_shader4",
  2637. decl->identifier);
  2638. }
  2639. }
  2640. /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
  2641. *
  2642. * "Global variables can only use the qualifiers const,
  2643. * attribute, uni form, or varying. Only one may be
  2644. * specified.
  2645. *
  2646. * Local variables can only use the qualifier const."
  2647. *
  2648. * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
  2649. * any extension that adds the 'layout' keyword.
  2650. */
  2651. if (!state->is_version(130, 300)
  2652. && !state->has_explicit_attrib_location()
  2653. && !state->ARB_fragment_coord_conventions_enable) {
  2654. if (this->type->qualifier.flags.q.out) {
  2655. _mesa_glsl_error(& loc, state,
  2656. "`out' qualifier in declaration of `%s' "
  2657. "only valid for function parameters in %s",
  2658. decl->identifier, state->get_version_string());
  2659. }
  2660. if (this->type->qualifier.flags.q.in) {
  2661. _mesa_glsl_error(& loc, state,
  2662. "`in' qualifier in declaration of `%s' "
  2663. "only valid for function parameters in %s",
  2664. decl->identifier, state->get_version_string());
  2665. }
  2666. /* FINISHME: Test for other invalid qualifiers. */
  2667. }
  2668. apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
  2669. & loc, false);
  2670. if (this->type->qualifier.flags.q.invariant) {
  2671. if ((state->stage == MESA_SHADER_VERTEX) &&
  2672. var->data.mode != ir_var_shader_out) {
  2673. _mesa_glsl_error(& loc, state,
  2674. "`%s' cannot be marked invariant, vertex shader "
  2675. "outputs only", var->name);
  2676. } else if ((state->stage == MESA_SHADER_FRAGMENT) &&
  2677. var->data.mode != ir_var_shader_in) {
  2678. /* FINISHME: Note that this doesn't work for invariant on
  2679. * a function signature inval
  2680. */
  2681. _mesa_glsl_error(& loc, state,
  2682. "`%s' cannot be marked invariant, fragment shader "
  2683. "inputs only", var->name);
  2684. }
  2685. }
  2686. if (state->current_function != NULL) {
  2687. const char *mode = NULL;
  2688. const char *extra = "";
  2689. /* There is no need to check for 'inout' here because the parser will
  2690. * only allow that in function parameter lists.
  2691. */
  2692. if (this->type->qualifier.flags.q.attribute) {
  2693. mode = "attribute";
  2694. } else if (this->type->qualifier.flags.q.uniform) {
  2695. mode = "uniform";
  2696. } else if (this->type->qualifier.flags.q.varying) {
  2697. mode = "varying";
  2698. } else if (this->type->qualifier.flags.q.in) {
  2699. mode = "in";
  2700. extra = " or in function parameter list";
  2701. } else if (this->type->qualifier.flags.q.out) {
  2702. mode = "out";
  2703. extra = " or in function parameter list";
  2704. }
  2705. if (mode) {
  2706. _mesa_glsl_error(& loc, state,
  2707. "%s variable `%s' must be declared at "
  2708. "global scope%s",
  2709. mode, var->name, extra);
  2710. }
  2711. } else if (var->data.mode == ir_var_shader_in) {
  2712. var->data.read_only = true;
  2713. if (state->stage == MESA_SHADER_VERTEX) {
  2714. bool error_emitted = false;
  2715. /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  2716. *
  2717. * "Vertex shader inputs can only be float, floating-point
  2718. * vectors, matrices, signed and unsigned integers and integer
  2719. * vectors. Vertex shader inputs can also form arrays of these
  2720. * types, but not structures."
  2721. *
  2722. * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
  2723. *
  2724. * "Vertex shader inputs can only be float, floating-point
  2725. * vectors, matrices, signed and unsigned integers and integer
  2726. * vectors. They cannot be arrays or structures."
  2727. *
  2728. * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
  2729. *
  2730. * "The attribute qualifier can be used only with float,
  2731. * floating-point vectors, and matrices. Attribute variables
  2732. * cannot be declared as arrays or structures."
  2733. *
  2734. * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
  2735. *
  2736. * "Vertex shader inputs can only be float, floating-point
  2737. * vectors, matrices, signed and unsigned integers and integer
  2738. * vectors. Vertex shader inputs cannot be arrays or
  2739. * structures."
  2740. */
  2741. const glsl_type *check_type = var->type->is_array()
  2742. ? var->type->fields.array : var->type;
  2743. switch (check_type->base_type) {
  2744. case GLSL_TYPE_FLOAT:
  2745. break;
  2746. case GLSL_TYPE_UINT:
  2747. case GLSL_TYPE_INT:
  2748. if (state->is_version(120, 300))
  2749. break;
  2750. /* FALLTHROUGH */
  2751. default:
  2752. _mesa_glsl_error(& loc, state,
  2753. "vertex shader input / attribute cannot have "
  2754. "type %s`%s'",
  2755. var->type->is_array() ? "array of " : "",
  2756. check_type->name);
  2757. error_emitted = true;
  2758. }
  2759. if (!error_emitted && var->type->is_array() &&
  2760. !state->check_version(150, 0, &loc,
  2761. "vertex shader input / attribute "
  2762. "cannot have array type")) {
  2763. error_emitted = true;
  2764. }
  2765. } else if (state->stage == MESA_SHADER_GEOMETRY) {
  2766. /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
  2767. *
  2768. * Geometry shader input variables get the per-vertex values
  2769. * written out by vertex shader output variables of the same
  2770. * names. Since a geometry shader operates on a set of
  2771. * vertices, each input varying variable (or input block, see
  2772. * interface blocks below) needs to be declared as an array.
  2773. */
  2774. if (!var->type->is_array()) {
  2775. _mesa_glsl_error(&loc, state,
  2776. "geometry shader inputs must be arrays");
  2777. }
  2778. handle_geometry_shader_input_decl(state, loc, var);
  2779. }
  2780. }
  2781. /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
  2782. * so must integer vertex outputs.
  2783. *
  2784. * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
  2785. * "Fragment shader inputs that are signed or unsigned integers or
  2786. * integer vectors must be qualified with the interpolation qualifier
  2787. * flat."
  2788. *
  2789. * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
  2790. * "Fragment shader inputs that are, or contain, signed or unsigned
  2791. * integers or integer vectors must be qualified with the
  2792. * interpolation qualifier flat."
  2793. *
  2794. * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
  2795. * "Vertex shader outputs that are, or contain, signed or unsigned
  2796. * integers or integer vectors must be qualified with the
  2797. * interpolation qualifier flat."
  2798. *
  2799. * Note that prior to GLSL 1.50, this requirement applied to vertex
  2800. * outputs rather than fragment inputs. That creates problems in the
  2801. * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
  2802. * desktop GL shaders. For GLSL ES shaders, we follow the spec and
  2803. * apply the restriction to both vertex outputs and fragment inputs.
  2804. *
  2805. * Note also that the desktop GLSL specs are missing the text "or
  2806. * contain"; this is presumably an oversight, since there is no
  2807. * reasonable way to interpolate a fragment shader input that contains
  2808. * an integer.
  2809. */
  2810. if (state->is_version(130, 300) &&
  2811. var->type->contains_integer() &&
  2812. var->data.interpolation != INTERP_QUALIFIER_FLAT &&
  2813. ((state->stage == MESA_SHADER_FRAGMENT && var->data.mode == ir_var_shader_in)
  2814. || (state->stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_out
  2815. && state->es_shader))) {
  2816. const char *var_type = (state->stage == MESA_SHADER_VERTEX) ?
  2817. "vertex output" : "fragment input";
  2818. _mesa_glsl_error(&loc, state, "if a %s is (or contains) "
  2819. "an integer, then it must be qualified with 'flat'",
  2820. var_type);
  2821. }
  2822. /* Interpolation qualifiers cannot be applied to 'centroid' and
  2823. * 'centroid varying'.
  2824. *
  2825. * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
  2826. * "interpolation qualifiers may only precede the qualifiers in,
  2827. * centroid in, out, or centroid out in a declaration. They do not apply
  2828. * to the deprecated storage qualifiers varying or centroid varying."
  2829. *
  2830. * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
  2831. */
  2832. if (state->is_version(130, 0)
  2833. && this->type->qualifier.has_interpolation()
  2834. && this->type->qualifier.flags.q.varying) {
  2835. const char *i = this->type->qualifier.interpolation_string();
  2836. assert(i != NULL);
  2837. const char *s;
  2838. if (this->type->qualifier.flags.q.centroid)
  2839. s = "centroid varying";
  2840. else
  2841. s = "varying";
  2842. _mesa_glsl_error(&loc, state,
  2843. "qualifier '%s' cannot be applied to the "
  2844. "deprecated storage qualifier '%s'", i, s);
  2845. }
  2846. /* Interpolation qualifiers can only apply to vertex shader outputs and
  2847. * fragment shader inputs.
  2848. *
  2849. * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
  2850. * "Outputs from a vertex shader (out) and inputs to a fragment
  2851. * shader (in) can be further qualified with one or more of these
  2852. * interpolation qualifiers"
  2853. *
  2854. * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
  2855. * "These interpolation qualifiers may only precede the qualifiers
  2856. * in, centroid in, out, or centroid out in a declaration. They do
  2857. * not apply to inputs into a vertex shader or outputs from a
  2858. * fragment shader."
  2859. */
  2860. if (state->is_version(130, 300)
  2861. && this->type->qualifier.has_interpolation()) {
  2862. const char *i = this->type->qualifier.interpolation_string();
  2863. assert(i != NULL);
  2864. switch (state->stage) {
  2865. case MESA_SHADER_VERTEX:
  2866. if (this->type->qualifier.flags.q.in) {
  2867. _mesa_glsl_error(&loc, state,
  2868. "qualifier '%s' cannot be applied to vertex "
  2869. "shader inputs", i);
  2870. }
  2871. break;
  2872. case MESA_SHADER_FRAGMENT:
  2873. if (this->type->qualifier.flags.q.out) {
  2874. _mesa_glsl_error(&loc, state,
  2875. "qualifier '%s' cannot be applied to fragment "
  2876. "shader outputs", i);
  2877. }
  2878. break;
  2879. default:
  2880. break;
  2881. }
  2882. }
  2883. /* From section 4.3.4 of the GLSL 1.30 spec:
  2884. * "It is an error to use centroid in in a vertex shader."
  2885. *
  2886. * From section 4.3.4 of the GLSL ES 3.00 spec:
  2887. * "It is an error to use centroid in or interpolation qualifiers in
  2888. * a vertex shader input."
  2889. */
  2890. if (state->is_version(130, 300)
  2891. && this->type->qualifier.flags.q.centroid
  2892. && this->type->qualifier.flags.q.in
  2893. && state->stage == MESA_SHADER_VERTEX) {
  2894. _mesa_glsl_error(&loc, state,
  2895. "'centroid in' cannot be used in a vertex shader");
  2896. }
  2897. if (state->stage == MESA_SHADER_VERTEX
  2898. && this->type->qualifier.flags.q.sample
  2899. && this->type->qualifier.flags.q.in) {
  2900. _mesa_glsl_error(&loc, state,
  2901. "'sample in' cannot be used in a vertex shader");
  2902. }
  2903. /* Section 4.3.6 of the GLSL 1.30 specification states:
  2904. * "It is an error to use centroid out in a fragment shader."
  2905. *
  2906. * The GL_ARB_shading_language_420pack extension specification states:
  2907. * "It is an error to use auxiliary storage qualifiers or interpolation
  2908. * qualifiers on an output in a fragment shader."
  2909. */
  2910. if (state->stage == MESA_SHADER_FRAGMENT &&
  2911. this->type->qualifier.flags.q.out &&
  2912. this->type->qualifier.has_auxiliary_storage()) {
  2913. _mesa_glsl_error(&loc, state,
  2914. "auxiliary storage qualifiers cannot be used on "
  2915. "fragment shader outputs");
  2916. }
  2917. /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
  2918. */
  2919. if (this->type->qualifier.precision != ast_precision_none) {
  2920. state->check_precision_qualifiers_allowed(&loc);
  2921. }
  2922. /* Precision qualifiers apply to floating point, integer and sampler
  2923. * types.
  2924. *
  2925. * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
  2926. * "Any floating point or any integer declaration can have the type
  2927. * preceded by one of these precision qualifiers [...] Literal
  2928. * constants do not have precision qualifiers. Neither do Boolean
  2929. * variables.
  2930. *
  2931. * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
  2932. * spec also says:
  2933. *
  2934. * "Precision qualifiers are added for code portability with OpenGL
  2935. * ES, not for functionality. They have the same syntax as in OpenGL
  2936. * ES."
  2937. *
  2938. * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
  2939. *
  2940. * "uniform lowp sampler2D sampler;
  2941. * highp vec2 coord;
  2942. * ...
  2943. * lowp vec4 col = texture2D (sampler, coord);
  2944. * // texture2D returns lowp"
  2945. *
  2946. * From this, we infer that GLSL 1.30 (and later) should allow precision
  2947. * qualifiers on sampler types just like float and integer types.
  2948. */
  2949. if (this->type->qualifier.precision != ast_precision_none
  2950. && !var->type->is_float()
  2951. && !var->type->is_integer()
  2952. && !var->type->is_record()
  2953. && !var->type->is_sampler()
  2954. && !(var->type->is_array()
  2955. && (var->type->fields.array->is_float()
  2956. || var->type->fields.array->is_integer()))) {
  2957. _mesa_glsl_error(&loc, state,
  2958. "precision qualifiers apply only to floating point"
  2959. ", integer and sampler types");
  2960. }
  2961. /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  2962. *
  2963. * "[Sampler types] can only be declared as function
  2964. * parameters or uniform variables (see Section 4.3.5
  2965. * "Uniform")".
  2966. */
  2967. if (var_type->contains_sampler() &&
  2968. !this->type->qualifier.flags.q.uniform) {
  2969. _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
  2970. }
  2971. /* Process the initializer and add its instructions to a temporary
  2972. * list. This list will be added to the instruction stream (below) after
  2973. * the declaration is added. This is done because in some cases (such as
  2974. * redeclarations) the declaration may not actually be added to the
  2975. * instruction stream.
  2976. */
  2977. exec_list initializer_instructions;
  2978. ir_variable *earlier =
  2979. get_variable_being_redeclared(var, decl->get_location(), state,
  2980. false /* allow_all_redeclarations */);
  2981. if (earlier != NULL) {
  2982. if (strncmp(var->name, "gl_", 3) == 0 &&
  2983. earlier->data.how_declared == ir_var_declared_in_block) {
  2984. _mesa_glsl_error(&loc, state,
  2985. "`%s' has already been redeclared using "
  2986. "gl_PerVertex", var->name);
  2987. }
  2988. earlier->data.how_declared = ir_var_declared_normally;
  2989. }
  2990. if (decl->initializer != NULL) {
  2991. result = process_initializer((earlier == NULL) ? var : earlier,
  2992. decl, this->type,
  2993. &initializer_instructions, state);
  2994. }
  2995. /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
  2996. *
  2997. * "It is an error to write to a const variable outside of
  2998. * its declaration, so they must be initialized when
  2999. * declared."
  3000. */
  3001. if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
  3002. _mesa_glsl_error(& loc, state,
  3003. "const declaration of `%s' must be initialized",
  3004. decl->identifier);
  3005. }
  3006. if (state->es_shader) {
  3007. const glsl_type *const t = (earlier == NULL)
  3008. ? var->type : earlier->type;
  3009. if (t->is_unsized_array())
  3010. /* Section 10.17 of the GLSL ES 1.00 specification states that
  3011. * unsized array declarations have been removed from the language.
  3012. * Arrays that are sized using an initializer are still explicitly
  3013. * sized. However, GLSL ES 1.00 does not allow array
  3014. * initializers. That is only allowed in GLSL ES 3.00.
  3015. *
  3016. * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
  3017. *
  3018. * "An array type can also be formed without specifying a size
  3019. * if the definition includes an initializer:
  3020. *
  3021. * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
  3022. * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
  3023. *
  3024. * float a[5];
  3025. * float b[] = a;"
  3026. */
  3027. _mesa_glsl_error(& loc, state,
  3028. "unsized array declarations are not allowed in "
  3029. "GLSL ES");
  3030. }
  3031. /* If the declaration is not a redeclaration, there are a few additional
  3032. * semantic checks that must be applied. In addition, variable that was
  3033. * created for the declaration should be added to the IR stream.
  3034. */
  3035. if (earlier == NULL) {
  3036. validate_identifier(decl->identifier, loc, state);
  3037. /* Add the variable to the symbol table. Note that the initializer's
  3038. * IR was already processed earlier (though it hasn't been emitted
  3039. * yet), without the variable in scope.
  3040. *
  3041. * This differs from most C-like languages, but it follows the GLSL
  3042. * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
  3043. * spec:
  3044. *
  3045. * "Within a declaration, the scope of a name starts immediately
  3046. * after the initializer if present or immediately after the name
  3047. * being declared if not."
  3048. */
  3049. if (!state->symbols->add_variable(var)) {
  3050. YYLTYPE loc = this->get_location();
  3051. _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
  3052. "current scope", decl->identifier);
  3053. continue;
  3054. }
  3055. /* Push the variable declaration to the top. It means that all the
  3056. * variable declarations will appear in a funny last-to-first order,
  3057. * but otherwise we run into trouble if a function is prototyped, a
  3058. * global var is decled, then the function is defined with usage of
  3059. * the global var. See glslparsertest's CorrectModule.frag.
  3060. */
  3061. instructions->push_head(var);
  3062. }
  3063. instructions->append_list(&initializer_instructions);
  3064. }
  3065. /* Generally, variable declarations do not have r-values. However,
  3066. * one is used for the declaration in
  3067. *
  3068. * while (bool b = some_condition()) {
  3069. * ...
  3070. * }
  3071. *
  3072. * so we return the rvalue from the last seen declaration here.
  3073. */
  3074. return result;
  3075. }
  3076. ir_rvalue *
  3077. ast_parameter_declarator::hir(exec_list *instructions,
  3078. struct _mesa_glsl_parse_state *state)
  3079. {
  3080. void *ctx = state;
  3081. const struct glsl_type *type;
  3082. const char *name = NULL;
  3083. YYLTYPE loc = this->get_location();
  3084. type = this->type->glsl_type(& name, state);
  3085. if (type == NULL) {
  3086. if (name != NULL) {
  3087. _mesa_glsl_error(& loc, state,
  3088. "invalid type `%s' in declaration of `%s'",
  3089. name, this->identifier);
  3090. } else {
  3091. _mesa_glsl_error(& loc, state,
  3092. "invalid type in declaration of `%s'",
  3093. this->identifier);
  3094. }
  3095. type = glsl_type::error_type;
  3096. }
  3097. /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
  3098. *
  3099. * "Functions that accept no input arguments need not use void in the
  3100. * argument list because prototypes (or definitions) are required and
  3101. * therefore there is no ambiguity when an empty argument list "( )" is
  3102. * declared. The idiom "(void)" as a parameter list is provided for
  3103. * convenience."
  3104. *
  3105. * Placing this check here prevents a void parameter being set up
  3106. * for a function, which avoids tripping up checks for main taking
  3107. * parameters and lookups of an unnamed symbol.
  3108. */
  3109. if (type->is_void()) {
  3110. if (this->identifier != NULL)
  3111. _mesa_glsl_error(& loc, state,
  3112. "named parameter cannot have type `void'");
  3113. is_void = true;
  3114. return NULL;
  3115. }
  3116. if (formal_parameter && (this->identifier == NULL)) {
  3117. _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
  3118. return NULL;
  3119. }
  3120. /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
  3121. * call already handled the "vec4[..] foo" case.
  3122. */
  3123. if (this->is_array) {
  3124. type = process_array_type(&loc, type, this->array_size, state);
  3125. }
  3126. if (!type->is_error() && type->is_unsized_array()) {
  3127. _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
  3128. "a declared size");
  3129. type = glsl_type::error_type;
  3130. }
  3131. is_void = false;
  3132. ir_variable *var = new(ctx)
  3133. ir_variable(type, this->identifier, ir_var_function_in);
  3134. /* Apply any specified qualifiers to the parameter declaration. Note that
  3135. * for function parameters the default mode is 'in'.
  3136. */
  3137. apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
  3138. true);
  3139. /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  3140. *
  3141. * "Samplers cannot be treated as l-values; hence cannot be used
  3142. * as out or inout function parameters, nor can they be assigned
  3143. * into."
  3144. */
  3145. if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
  3146. && type->contains_sampler()) {
  3147. _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
  3148. type = glsl_type::error_type;
  3149. }
  3150. /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
  3151. *
  3152. * "When calling a function, expressions that do not evaluate to
  3153. * l-values cannot be passed to parameters declared as out or inout."
  3154. *
  3155. * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
  3156. *
  3157. * "Other binary or unary expressions, non-dereferenced arrays,
  3158. * function names, swizzles with repeated fields, and constants
  3159. * cannot be l-values."
  3160. *
  3161. * So for GLSL 1.10, passing an array as an out or inout parameter is not
  3162. * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
  3163. */
  3164. if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
  3165. && type->is_array()
  3166. && !state->check_version(120, 100, &loc,
  3167. "arrays cannot be out or inout parameters")) {
  3168. type = glsl_type::error_type;
  3169. }
  3170. instructions->push_tail(var);
  3171. /* Parameter declarations do not have r-values.
  3172. */
  3173. return NULL;
  3174. }
  3175. void
  3176. ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
  3177. bool formal,
  3178. exec_list *ir_parameters,
  3179. _mesa_glsl_parse_state *state)
  3180. {
  3181. ast_parameter_declarator *void_param = NULL;
  3182. unsigned count = 0;
  3183. foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
  3184. param->formal_parameter = formal;
  3185. param->hir(ir_parameters, state);
  3186. if (param->is_void)
  3187. void_param = param;
  3188. count++;
  3189. }
  3190. if ((void_param != NULL) && (count > 1)) {
  3191. YYLTYPE loc = void_param->get_location();
  3192. _mesa_glsl_error(& loc, state,
  3193. "`void' parameter must be only parameter");
  3194. }
  3195. }
  3196. void
  3197. emit_function(_mesa_glsl_parse_state *state, ir_function *f)
  3198. {
  3199. /* IR invariants disallow function declarations or definitions
  3200. * nested within other function definitions. But there is no
  3201. * requirement about the relative order of function declarations
  3202. * and definitions with respect to one another. So simply insert
  3203. * the new ir_function block at the end of the toplevel instruction
  3204. * list.
  3205. */
  3206. state->toplevel_ir->push_tail(f);
  3207. }
  3208. ir_rvalue *
  3209. ast_function::hir(exec_list *instructions,
  3210. struct _mesa_glsl_parse_state *state)
  3211. {
  3212. void *ctx = state;
  3213. ir_function *f = NULL;
  3214. ir_function_signature *sig = NULL;
  3215. exec_list hir_parameters;
  3216. const char *const name = identifier;
  3217. /* New functions are always added to the top-level IR instruction stream,
  3218. * so this instruction list pointer is ignored. See also emit_function
  3219. * (called below).
  3220. */
  3221. (void) instructions;
  3222. /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
  3223. *
  3224. * "Function declarations (prototypes) cannot occur inside of functions;
  3225. * they must be at global scope, or for the built-in functions, outside
  3226. * the global scope."
  3227. *
  3228. * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
  3229. *
  3230. * "User defined functions may only be defined within the global scope."
  3231. *
  3232. * Note that this language does not appear in GLSL 1.10.
  3233. */
  3234. if ((state->current_function != NULL) &&
  3235. state->is_version(120, 100)) {
  3236. YYLTYPE loc = this->get_location();
  3237. _mesa_glsl_error(&loc, state,
  3238. "declaration of function `%s' not allowed within "
  3239. "function body", name);
  3240. }
  3241. validate_identifier(name, this->get_location(), state);
  3242. /* Convert the list of function parameters to HIR now so that they can be
  3243. * used below to compare this function's signature with previously seen
  3244. * signatures for functions with the same name.
  3245. */
  3246. ast_parameter_declarator::parameters_to_hir(& this->parameters,
  3247. is_definition,
  3248. & hir_parameters, state);
  3249. const char *return_type_name;
  3250. const glsl_type *return_type =
  3251. this->return_type->glsl_type(& return_type_name, state);
  3252. if (!return_type) {
  3253. YYLTYPE loc = this->get_location();
  3254. _mesa_glsl_error(&loc, state,
  3255. "function `%s' has undeclared return type `%s'",
  3256. name, return_type_name);
  3257. return_type = glsl_type::error_type;
  3258. }
  3259. /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
  3260. * "No qualifier is allowed on the return type of a function."
  3261. */
  3262. if (this->return_type->has_qualifiers()) {
  3263. YYLTYPE loc = this->get_location();
  3264. _mesa_glsl_error(& loc, state,
  3265. "function `%s' return type has qualifiers", name);
  3266. }
  3267. /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
  3268. *
  3269. * "Arrays are allowed as arguments and as the return type. In both
  3270. * cases, the array must be explicitly sized."
  3271. */
  3272. if (return_type->is_unsized_array()) {
  3273. YYLTYPE loc = this->get_location();
  3274. _mesa_glsl_error(& loc, state,
  3275. "function `%s' return type array must be explicitly "
  3276. "sized", name);
  3277. }
  3278. /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  3279. *
  3280. * "[Sampler types] can only be declared as function parameters
  3281. * or uniform variables (see Section 4.3.5 "Uniform")".
  3282. */
  3283. if (return_type->contains_sampler()) {
  3284. YYLTYPE loc = this->get_location();
  3285. _mesa_glsl_error(&loc, state,
  3286. "function `%s' return type can't contain a sampler",
  3287. name);
  3288. }
  3289. /* Verify that this function's signature either doesn't match a previously
  3290. * seen signature for a function with the same name, or, if a match is found,
  3291. * that the previously seen signature does not have an associated definition.
  3292. */
  3293. f = state->symbols->get_function(name);
  3294. if (f != NULL && (state->es_shader || f->has_user_signature())) {
  3295. sig = f->exact_matching_signature(state, &hir_parameters);
  3296. if (sig != NULL) {
  3297. const char *badvar = sig->qualifiers_match(&hir_parameters);
  3298. if (badvar != NULL) {
  3299. YYLTYPE loc = this->get_location();
  3300. _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
  3301. "qualifiers don't match prototype", name, badvar);
  3302. }
  3303. if (sig->return_type != return_type) {
  3304. YYLTYPE loc = this->get_location();
  3305. _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
  3306. "match prototype", name);
  3307. }
  3308. if (sig->is_defined) {
  3309. if (is_definition) {
  3310. YYLTYPE loc = this->get_location();
  3311. _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
  3312. } else {
  3313. /* We just encountered a prototype that exactly matches a
  3314. * function that's already been defined. This is redundant,
  3315. * and we should ignore it.
  3316. */
  3317. return NULL;
  3318. }
  3319. }
  3320. }
  3321. } else {
  3322. f = new(ctx) ir_function(name);
  3323. if (!state->symbols->add_function(f)) {
  3324. /* This function name shadows a non-function use of the same name. */
  3325. YYLTYPE loc = this->get_location();
  3326. _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
  3327. "non-function", name);
  3328. return NULL;
  3329. }
  3330. emit_function(state, f);
  3331. }
  3332. /* Verify the return type of main() */
  3333. if (strcmp(name, "main") == 0) {
  3334. if (! return_type->is_void()) {
  3335. YYLTYPE loc = this->get_location();
  3336. _mesa_glsl_error(& loc, state, "main() must return void");
  3337. }
  3338. if (!hir_parameters.is_empty()) {
  3339. YYLTYPE loc = this->get_location();
  3340. _mesa_glsl_error(& loc, state, "main() must not take any parameters");
  3341. }
  3342. }
  3343. /* Finish storing the information about this new function in its signature.
  3344. */
  3345. if (sig == NULL) {
  3346. sig = new(ctx) ir_function_signature(return_type);
  3347. f->add_signature(sig);
  3348. }
  3349. sig->replace_parameters(&hir_parameters);
  3350. signature = sig;
  3351. /* Function declarations (prototypes) do not have r-values.
  3352. */
  3353. return NULL;
  3354. }
  3355. ir_rvalue *
  3356. ast_function_definition::hir(exec_list *instructions,
  3357. struct _mesa_glsl_parse_state *state)
  3358. {
  3359. prototype->is_definition = true;
  3360. prototype->hir(instructions, state);
  3361. ir_function_signature *signature = prototype->signature;
  3362. if (signature == NULL)
  3363. return NULL;
  3364. assert(state->current_function == NULL);
  3365. state->current_function = signature;
  3366. state->found_return = false;
  3367. /* Duplicate parameters declared in the prototype as concrete variables.
  3368. * Add these to the symbol table.
  3369. */
  3370. state->symbols->push_scope();
  3371. foreach_iter(exec_list_iterator, iter, signature->parameters) {
  3372. ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
  3373. assert(var != NULL);
  3374. /* The only way a parameter would "exist" is if two parameters have
  3375. * the same name.
  3376. */
  3377. if (state->symbols->name_declared_this_scope(var->name)) {
  3378. YYLTYPE loc = this->get_location();
  3379. _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
  3380. } else {
  3381. state->symbols->add_variable(var);
  3382. }
  3383. }
  3384. /* Convert the body of the function to HIR. */
  3385. this->body->hir(&signature->body, state);
  3386. signature->is_defined = true;
  3387. state->symbols->pop_scope();
  3388. assert(state->current_function == signature);
  3389. state->current_function = NULL;
  3390. if (!signature->return_type->is_void() && !state->found_return) {
  3391. YYLTYPE loc = this->get_location();
  3392. _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
  3393. "%s, but no return statement",
  3394. signature->function_name(),
  3395. signature->return_type->name);
  3396. }
  3397. /* Function definitions do not have r-values.
  3398. */
  3399. return NULL;
  3400. }
  3401. ir_rvalue *
  3402. ast_jump_statement::hir(exec_list *instructions,
  3403. struct _mesa_glsl_parse_state *state)
  3404. {
  3405. void *ctx = state;
  3406. switch (mode) {
  3407. case ast_return: {
  3408. ir_return *inst;
  3409. assert(state->current_function);
  3410. if (opt_return_value) {
  3411. ir_rvalue *ret = opt_return_value->hir(instructions, state);
  3412. /* The value of the return type can be NULL if the shader says
  3413. * 'return foo();' and foo() is a function that returns void.
  3414. *
  3415. * NOTE: The GLSL spec doesn't say that this is an error. The type
  3416. * of the return value is void. If the return type of the function is
  3417. * also void, then this should compile without error. Seriously.
  3418. */
  3419. const glsl_type *const ret_type =
  3420. (ret == NULL) ? glsl_type::void_type : ret->type;
  3421. /* Implicit conversions are not allowed for return values prior to
  3422. * ARB_shading_language_420pack.
  3423. */
  3424. if (state->current_function->return_type != ret_type) {
  3425. YYLTYPE loc = this->get_location();
  3426. if (state->ARB_shading_language_420pack_enable) {
  3427. if (!apply_implicit_conversion(state->current_function->return_type,
  3428. ret, state)) {
  3429. _mesa_glsl_error(& loc, state,
  3430. "could not implicitly convert return value "
  3431. "to %s, in function `%s'",
  3432. state->current_function->return_type->name,
  3433. state->current_function->function_name());
  3434. }
  3435. } else {
  3436. _mesa_glsl_error(& loc, state,
  3437. "`return' with wrong type %s, in function `%s' "
  3438. "returning %s",
  3439. ret_type->name,
  3440. state->current_function->function_name(),
  3441. state->current_function->return_type->name);
  3442. }
  3443. } else if (state->current_function->return_type->base_type ==
  3444. GLSL_TYPE_VOID) {
  3445. YYLTYPE loc = this->get_location();
  3446. /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
  3447. * specs add a clarification:
  3448. *
  3449. * "A void function can only use return without a return argument, even if
  3450. * the return argument has void type. Return statements only accept values:
  3451. *
  3452. * void func1() { }
  3453. * void func2() { return func1(); } // illegal return statement"
  3454. */
  3455. _mesa_glsl_error(& loc, state,
  3456. "void functions can only use `return' without a "
  3457. "return argument");
  3458. }
  3459. inst = new(ctx) ir_return(ret);
  3460. } else {
  3461. if (state->current_function->return_type->base_type !=
  3462. GLSL_TYPE_VOID) {
  3463. YYLTYPE loc = this->get_location();
  3464. _mesa_glsl_error(& loc, state,
  3465. "`return' with no value, in function %s returning "
  3466. "non-void",
  3467. state->current_function->function_name());
  3468. }
  3469. inst = new(ctx) ir_return;
  3470. }
  3471. state->found_return = true;
  3472. instructions->push_tail(inst);
  3473. break;
  3474. }
  3475. case ast_discard:
  3476. if (state->stage != MESA_SHADER_FRAGMENT) {
  3477. YYLTYPE loc = this->get_location();
  3478. _mesa_glsl_error(& loc, state,
  3479. "`discard' may only appear in a fragment shader");
  3480. }
  3481. instructions->push_tail(new(ctx) ir_discard);
  3482. break;
  3483. case ast_break:
  3484. case ast_continue:
  3485. if (mode == ast_continue &&
  3486. state->loop_nesting_ast == NULL) {
  3487. YYLTYPE loc = this->get_location();
  3488. _mesa_glsl_error(& loc, state,
  3489. "continue may only appear in a loop");
  3490. } else if (mode == ast_break &&
  3491. state->loop_nesting_ast == NULL &&
  3492. state->switch_state.switch_nesting_ast == NULL) {
  3493. YYLTYPE loc = this->get_location();
  3494. _mesa_glsl_error(& loc, state,
  3495. "break may only appear in a loop or a switch");
  3496. } else {
  3497. /* For a loop, inline the for loop expression again,
  3498. * since we don't know where near the end of
  3499. * the loop body the normal copy of it
  3500. * is going to be placed.
  3501. */
  3502. if (state->loop_nesting_ast != NULL &&
  3503. mode == ast_continue &&
  3504. state->loop_nesting_ast->rest_expression) {
  3505. state->loop_nesting_ast->rest_expression->hir(instructions,
  3506. state);
  3507. }
  3508. if (state->switch_state.is_switch_innermost &&
  3509. mode == ast_break) {
  3510. /* Force break out of switch by setting is_break switch state.
  3511. */
  3512. ir_variable *const is_break_var = state->switch_state.is_break_var;
  3513. ir_dereference_variable *const deref_is_break_var =
  3514. new(ctx) ir_dereference_variable(is_break_var);
  3515. ir_constant *const true_val = new(ctx) ir_constant(true);
  3516. ir_assignment *const set_break_var =
  3517. new(ctx) ir_assignment(deref_is_break_var, true_val);
  3518. instructions->push_tail(set_break_var);
  3519. }
  3520. else {
  3521. ir_loop_jump *const jump =
  3522. new(ctx) ir_loop_jump((mode == ast_break)
  3523. ? ir_loop_jump::jump_break
  3524. : ir_loop_jump::jump_continue);
  3525. instructions->push_tail(jump);
  3526. }
  3527. }
  3528. break;
  3529. }
  3530. /* Jump instructions do not have r-values.
  3531. */
  3532. return NULL;
  3533. }
  3534. ir_rvalue *
  3535. ast_selection_statement::hir(exec_list *instructions,
  3536. struct _mesa_glsl_parse_state *state)
  3537. {
  3538. void *ctx = state;
  3539. ir_rvalue *const condition = this->condition->hir(instructions, state);
  3540. /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
  3541. *
  3542. * "Any expression whose type evaluates to a Boolean can be used as the
  3543. * conditional expression bool-expression. Vector types are not accepted
  3544. * as the expression to if."
  3545. *
  3546. * The checks are separated so that higher quality diagnostics can be
  3547. * generated for cases where both rules are violated.
  3548. */
  3549. if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
  3550. YYLTYPE loc = this->condition->get_location();
  3551. _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
  3552. "boolean");
  3553. }
  3554. ir_if *const stmt = new(ctx) ir_if(condition);
  3555. if (then_statement != NULL) {
  3556. state->symbols->push_scope();
  3557. then_statement->hir(& stmt->then_instructions, state);
  3558. state->symbols->pop_scope();
  3559. }
  3560. if (else_statement != NULL) {
  3561. state->symbols->push_scope();
  3562. else_statement->hir(& stmt->else_instructions, state);
  3563. state->symbols->pop_scope();
  3564. }
  3565. instructions->push_tail(stmt);
  3566. /* if-statements do not have r-values.
  3567. */
  3568. return NULL;
  3569. }
  3570. ir_rvalue *
  3571. ast_switch_statement::hir(exec_list *instructions,
  3572. struct _mesa_glsl_parse_state *state)
  3573. {
  3574. void *ctx = state;
  3575. ir_rvalue *const test_expression =
  3576. this->test_expression->hir(instructions, state);
  3577. /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
  3578. *
  3579. * "The type of init-expression in a switch statement must be a
  3580. * scalar integer."
  3581. */
  3582. if (!test_expression->type->is_scalar() ||
  3583. !test_expression->type->is_integer()) {
  3584. YYLTYPE loc = this->test_expression->get_location();
  3585. _mesa_glsl_error(& loc,
  3586. state,
  3587. "switch-statement expression must be scalar "
  3588. "integer");
  3589. }
  3590. /* Track the switch-statement nesting in a stack-like manner.
  3591. */
  3592. struct glsl_switch_state saved = state->switch_state;
  3593. state->switch_state.is_switch_innermost = true;
  3594. state->switch_state.switch_nesting_ast = this;
  3595. state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
  3596. hash_table_pointer_compare);
  3597. state->switch_state.previous_default = NULL;
  3598. /* Initalize is_fallthru state to false.
  3599. */
  3600. ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
  3601. state->switch_state.is_fallthru_var =
  3602. new(ctx) ir_variable(glsl_type::bool_type,
  3603. "switch_is_fallthru_tmp",
  3604. ir_var_temporary);
  3605. instructions->push_tail(state->switch_state.is_fallthru_var);
  3606. ir_dereference_variable *deref_is_fallthru_var =
  3607. new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3608. instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
  3609. is_fallthru_val));
  3610. /* Initalize is_break state to false.
  3611. */
  3612. ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
  3613. state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
  3614. "switch_is_break_tmp",
  3615. ir_var_temporary);
  3616. instructions->push_tail(state->switch_state.is_break_var);
  3617. ir_dereference_variable *deref_is_break_var =
  3618. new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
  3619. instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
  3620. is_break_val));
  3621. /* Cache test expression.
  3622. */
  3623. test_to_hir(instructions, state);
  3624. /* Emit code for body of switch stmt.
  3625. */
  3626. body->hir(instructions, state);
  3627. hash_table_dtor(state->switch_state.labels_ht);
  3628. state->switch_state = saved;
  3629. /* Switch statements do not have r-values. */
  3630. return NULL;
  3631. }
  3632. void
  3633. ast_switch_statement::test_to_hir(exec_list *instructions,
  3634. struct _mesa_glsl_parse_state *state)
  3635. {
  3636. void *ctx = state;
  3637. /* Cache value of test expression. */
  3638. ir_rvalue *const test_val =
  3639. test_expression->hir(instructions,
  3640. state);
  3641. state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
  3642. "switch_test_tmp",
  3643. ir_var_temporary);
  3644. ir_dereference_variable *deref_test_var =
  3645. new(ctx) ir_dereference_variable(state->switch_state.test_var);
  3646. instructions->push_tail(state->switch_state.test_var);
  3647. instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
  3648. }
  3649. ir_rvalue *
  3650. ast_switch_body::hir(exec_list *instructions,
  3651. struct _mesa_glsl_parse_state *state)
  3652. {
  3653. if (stmts != NULL)
  3654. stmts->hir(instructions, state);
  3655. /* Switch bodies do not have r-values. */
  3656. return NULL;
  3657. }
  3658. ir_rvalue *
  3659. ast_case_statement_list::hir(exec_list *instructions,
  3660. struct _mesa_glsl_parse_state *state)
  3661. {
  3662. foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
  3663. case_stmt->hir(instructions, state);
  3664. /* Case statements do not have r-values. */
  3665. return NULL;
  3666. }
  3667. ir_rvalue *
  3668. ast_case_statement::hir(exec_list *instructions,
  3669. struct _mesa_glsl_parse_state *state)
  3670. {
  3671. labels->hir(instructions, state);
  3672. /* Conditionally set fallthru state based on break state. */
  3673. ir_constant *const false_val = new(state) ir_constant(false);
  3674. ir_dereference_variable *const deref_is_fallthru_var =
  3675. new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3676. ir_dereference_variable *const deref_is_break_var =
  3677. new(state) ir_dereference_variable(state->switch_state.is_break_var);
  3678. ir_assignment *const reset_fallthru_on_break =
  3679. new(state) ir_assignment(deref_is_fallthru_var,
  3680. false_val,
  3681. deref_is_break_var);
  3682. instructions->push_tail(reset_fallthru_on_break);
  3683. /* Guard case statements depending on fallthru state. */
  3684. ir_dereference_variable *const deref_fallthru_guard =
  3685. new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3686. ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
  3687. foreach_list_typed (ast_node, stmt, link, & this->stmts)
  3688. stmt->hir(& test_fallthru->then_instructions, state);
  3689. instructions->push_tail(test_fallthru);
  3690. /* Case statements do not have r-values. */
  3691. return NULL;
  3692. }
  3693. ir_rvalue *
  3694. ast_case_label_list::hir(exec_list *instructions,
  3695. struct _mesa_glsl_parse_state *state)
  3696. {
  3697. foreach_list_typed (ast_case_label, label, link, & this->labels)
  3698. label->hir(instructions, state);
  3699. /* Case labels do not have r-values. */
  3700. return NULL;
  3701. }
  3702. ir_rvalue *
  3703. ast_case_label::hir(exec_list *instructions,
  3704. struct _mesa_glsl_parse_state *state)
  3705. {
  3706. void *ctx = state;
  3707. ir_dereference_variable *deref_fallthru_var =
  3708. new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3709. ir_rvalue *const true_val = new(ctx) ir_constant(true);
  3710. /* If not default case, ... */
  3711. if (this->test_value != NULL) {
  3712. /* Conditionally set fallthru state based on
  3713. * comparison of cached test expression value to case label.
  3714. */
  3715. ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
  3716. ir_constant *label_const = label_rval->constant_expression_value();
  3717. if (!label_const) {
  3718. YYLTYPE loc = this->test_value->get_location();
  3719. _mesa_glsl_error(& loc, state,
  3720. "switch statement case label must be a "
  3721. "constant expression");
  3722. /* Stuff a dummy value in to allow processing to continue. */
  3723. label_const = new(ctx) ir_constant(0);
  3724. } else {
  3725. ast_expression *previous_label = (ast_expression *)
  3726. hash_table_find(state->switch_state.labels_ht,
  3727. (void *)(uintptr_t)label_const->value.u[0]);
  3728. if (previous_label) {
  3729. YYLTYPE loc = this->test_value->get_location();
  3730. _mesa_glsl_error(& loc, state,
  3731. "duplicate case value");
  3732. loc = previous_label->get_location();
  3733. _mesa_glsl_error(& loc, state,
  3734. "this is the previous case label");
  3735. } else {
  3736. hash_table_insert(state->switch_state.labels_ht,
  3737. this->test_value,
  3738. (void *)(uintptr_t)label_const->value.u[0]);
  3739. }
  3740. }
  3741. ir_dereference_variable *deref_test_var =
  3742. new(ctx) ir_dereference_variable(state->switch_state.test_var);
  3743. ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
  3744. label_const,
  3745. deref_test_var);
  3746. ir_assignment *set_fallthru_on_test =
  3747. new(ctx) ir_assignment(deref_fallthru_var,
  3748. true_val,
  3749. test_cond);
  3750. instructions->push_tail(set_fallthru_on_test);
  3751. } else { /* default case */
  3752. if (state->switch_state.previous_default) {
  3753. YYLTYPE loc = this->get_location();
  3754. _mesa_glsl_error(& loc, state,
  3755. "multiple default labels in one switch");
  3756. loc = state->switch_state.previous_default->get_location();
  3757. _mesa_glsl_error(& loc, state,
  3758. "this is the first default label");
  3759. }
  3760. state->switch_state.previous_default = this;
  3761. /* Set falltrhu state. */
  3762. ir_assignment *set_fallthru =
  3763. new(ctx) ir_assignment(deref_fallthru_var, true_val);
  3764. instructions->push_tail(set_fallthru);
  3765. }
  3766. /* Case statements do not have r-values. */
  3767. return NULL;
  3768. }
  3769. void
  3770. ast_iteration_statement::condition_to_hir(ir_loop *stmt,
  3771. struct _mesa_glsl_parse_state *state)
  3772. {
  3773. void *ctx = state;
  3774. if (condition != NULL) {
  3775. ir_rvalue *const cond =
  3776. condition->hir(& stmt->body_instructions, state);
  3777. if ((cond == NULL)
  3778. || !cond->type->is_boolean() || !cond->type->is_scalar()) {
  3779. YYLTYPE loc = condition->get_location();
  3780. _mesa_glsl_error(& loc, state,
  3781. "loop condition must be scalar boolean");
  3782. } else {
  3783. /* As the first code in the loop body, generate a block that looks
  3784. * like 'if (!condition) break;' as the loop termination condition.
  3785. */
  3786. ir_rvalue *const not_cond =
  3787. new(ctx) ir_expression(ir_unop_logic_not, cond);
  3788. ir_if *const if_stmt = new(ctx) ir_if(not_cond);
  3789. ir_jump *const break_stmt =
  3790. new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
  3791. if_stmt->then_instructions.push_tail(break_stmt);
  3792. stmt->body_instructions.push_tail(if_stmt);
  3793. }
  3794. }
  3795. }
  3796. ir_rvalue *
  3797. ast_iteration_statement::hir(exec_list *instructions,
  3798. struct _mesa_glsl_parse_state *state)
  3799. {
  3800. void *ctx = state;
  3801. /* For-loops and while-loops start a new scope, but do-while loops do not.
  3802. */
  3803. if (mode != ast_do_while)
  3804. state->symbols->push_scope();
  3805. if (init_statement != NULL)
  3806. init_statement->hir(instructions, state);
  3807. ir_loop *const stmt = new(ctx) ir_loop();
  3808. instructions->push_tail(stmt);
  3809. /* Track the current loop nesting. */
  3810. ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
  3811. state->loop_nesting_ast = this;
  3812. /* Likewise, indicate that following code is closest to a loop,
  3813. * NOT closest to a switch.
  3814. */
  3815. bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
  3816. state->switch_state.is_switch_innermost = false;
  3817. if (mode != ast_do_while)
  3818. condition_to_hir(stmt, state);
  3819. if (body != NULL)
  3820. body->hir(& stmt->body_instructions, state);
  3821. if (rest_expression != NULL)
  3822. rest_expression->hir(& stmt->body_instructions, state);
  3823. if (mode == ast_do_while)
  3824. condition_to_hir(stmt, state);
  3825. if (mode != ast_do_while)
  3826. state->symbols->pop_scope();
  3827. /* Restore previous nesting before returning. */
  3828. state->loop_nesting_ast = nesting_ast;
  3829. state->switch_state.is_switch_innermost = saved_is_switch_innermost;
  3830. /* Loops do not have r-values.
  3831. */
  3832. return NULL;
  3833. }
  3834. /**
  3835. * Determine if the given type is valid for establishing a default precision
  3836. * qualifier.
  3837. *
  3838. * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
  3839. *
  3840. * "The precision statement
  3841. *
  3842. * precision precision-qualifier type;
  3843. *
  3844. * can be used to establish a default precision qualifier. The type field
  3845. * can be either int or float or any of the sampler types, and the
  3846. * precision-qualifier can be lowp, mediump, or highp."
  3847. *
  3848. * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
  3849. * qualifiers on sampler types, but this seems like an oversight (since the
  3850. * intention of including these in GLSL 1.30 is to allow compatibility with ES
  3851. * shaders). So we allow int, float, and all sampler types regardless of GLSL
  3852. * version.
  3853. */
  3854. static bool
  3855. is_valid_default_precision_type(const struct glsl_type *const type)
  3856. {
  3857. if (type == NULL)
  3858. return false;
  3859. switch (type->base_type) {
  3860. case GLSL_TYPE_INT:
  3861. case GLSL_TYPE_FLOAT:
  3862. /* "int" and "float" are valid, but vectors and matrices are not. */
  3863. return type->vector_elements == 1 && type->matrix_columns == 1;
  3864. case GLSL_TYPE_SAMPLER:
  3865. return true;
  3866. default:
  3867. return false;
  3868. }
  3869. }
  3870. ir_rvalue *
  3871. ast_type_specifier::hir(exec_list *instructions,
  3872. struct _mesa_glsl_parse_state *state)
  3873. {
  3874. if (this->default_precision == ast_precision_none && this->structure == NULL)
  3875. return NULL;
  3876. YYLTYPE loc = this->get_location();
  3877. /* If this is a precision statement, check that the type to which it is
  3878. * applied is either float or int.
  3879. *
  3880. * From section 4.5.3 of the GLSL 1.30 spec:
  3881. * "The precision statement
  3882. * precision precision-qualifier type;
  3883. * can be used to establish a default precision qualifier. The type
  3884. * field can be either int or float [...]. Any other types or
  3885. * qualifiers will result in an error.
  3886. */
  3887. if (this->default_precision != ast_precision_none) {
  3888. if (!state->check_precision_qualifiers_allowed(&loc))
  3889. return NULL;
  3890. if (this->structure != NULL) {
  3891. _mesa_glsl_error(&loc, state,
  3892. "precision qualifiers do not apply to structures");
  3893. return NULL;
  3894. }
  3895. if (this->is_array) {
  3896. _mesa_glsl_error(&loc, state,
  3897. "default precision statements do not apply to "
  3898. "arrays");
  3899. return NULL;
  3900. }
  3901. const struct glsl_type *const type =
  3902. state->symbols->get_type(this->type_name);
  3903. if (!is_valid_default_precision_type(type)) {
  3904. _mesa_glsl_error(&loc, state,
  3905. "default precision statements apply only to "
  3906. "float, int, and sampler types");
  3907. return NULL;
  3908. }
  3909. if (type->base_type == GLSL_TYPE_FLOAT
  3910. && state->es_shader
  3911. && state->stage == MESA_SHADER_FRAGMENT) {
  3912. /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
  3913. * spec says:
  3914. *
  3915. * "The fragment language has no default precision qualifier for
  3916. * floating point types."
  3917. *
  3918. * As a result, we have to track whether or not default precision has
  3919. * been specified for float in GLSL ES fragment shaders.
  3920. *
  3921. * Earlier in that same section, the spec says:
  3922. *
  3923. * "Non-precision qualified declarations will use the precision
  3924. * qualifier specified in the most recent precision statement
  3925. * that is still in scope. The precision statement has the same
  3926. * scoping rules as variable declarations. If it is declared
  3927. * inside a compound statement, its effect stops at the end of
  3928. * the innermost statement it was declared in. Precision
  3929. * statements in nested scopes override precision statements in
  3930. * outer scopes. Multiple precision statements for the same basic
  3931. * type can appear inside the same scope, with later statements
  3932. * overriding earlier statements within that scope."
  3933. *
  3934. * Default precision specifications follow the same scope rules as
  3935. * variables. So, we can track the state of the default float
  3936. * precision in the symbol table, and the rules will just work. This
  3937. * is a slight abuse of the symbol table, but it has the semantics
  3938. * that we want.
  3939. */
  3940. ir_variable *const junk =
  3941. new(state) ir_variable(type, "#default precision",
  3942. ir_var_temporary);
  3943. state->symbols->add_variable(junk);
  3944. }
  3945. /* FINISHME: Translate precision statements into IR. */
  3946. return NULL;
  3947. }
  3948. /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
  3949. * process_record_constructor() can do type-checking on C-style initializer
  3950. * expressions of structs, but ast_struct_specifier should only be translated
  3951. * to HIR if it is declaring the type of a structure.
  3952. *
  3953. * The ->is_declaration field is false for initializers of variables
  3954. * declared separately from the struct's type definition.
  3955. *
  3956. * struct S { ... }; (is_declaration = true)
  3957. * struct T { ... } t = { ... }; (is_declaration = true)
  3958. * S s = { ... }; (is_declaration = false)
  3959. */
  3960. if (this->structure != NULL && this->structure->is_declaration)
  3961. return this->structure->hir(instructions, state);
  3962. return NULL;
  3963. }
  3964. /**
  3965. * Process a structure or interface block tree into an array of structure fields
  3966. *
  3967. * After parsing, where there are some syntax differnces, structures and
  3968. * interface blocks are almost identical. They are similar enough that the
  3969. * AST for each can be processed the same way into a set of
  3970. * \c glsl_struct_field to describe the members.
  3971. *
  3972. * If we're processing an interface block, var_mode should be the type of the
  3973. * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
  3974. * If we're processing a structure, var_mode should be ir_var_auto.
  3975. *
  3976. * \return
  3977. * The number of fields processed. A pointer to the array structure fields is
  3978. * stored in \c *fields_ret.
  3979. */
  3980. unsigned
  3981. ast_process_structure_or_interface_block(exec_list *instructions,
  3982. struct _mesa_glsl_parse_state *state,
  3983. exec_list *declarations,
  3984. YYLTYPE &loc,
  3985. glsl_struct_field **fields_ret,
  3986. bool is_interface,
  3987. bool block_row_major,
  3988. bool allow_reserved_names,
  3989. ir_variable_mode var_mode)
  3990. {
  3991. unsigned decl_count = 0;
  3992. /* Make an initial pass over the list of fields to determine how
  3993. * many there are. Each element in this list is an ast_declarator_list.
  3994. * This means that we actually need to count the number of elements in the
  3995. * 'declarations' list in each of the elements.
  3996. */
  3997. foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
  3998. foreach_list_const (decl_ptr, & decl_list->declarations) {
  3999. decl_count++;
  4000. }
  4001. }
  4002. /* Allocate storage for the fields and process the field
  4003. * declarations. As the declarations are processed, try to also convert
  4004. * the types to HIR. This ensures that structure definitions embedded in
  4005. * other structure definitions or in interface blocks are processed.
  4006. */
  4007. glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
  4008. decl_count);
  4009. unsigned i = 0;
  4010. foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
  4011. const char *type_name;
  4012. decl_list->type->specifier->hir(instructions, state);
  4013. /* Section 10.9 of the GLSL ES 1.00 specification states that
  4014. * embedded structure definitions have been removed from the language.
  4015. */
  4016. if (state->es_shader && decl_list->type->specifier->structure != NULL) {
  4017. _mesa_glsl_error(&loc, state, "embedded structure definitions are "
  4018. "not allowed in GLSL ES 1.00");
  4019. }
  4020. const glsl_type *decl_type =
  4021. decl_list->type->glsl_type(& type_name, state);
  4022. foreach_list_typed (ast_declaration, decl, link,
  4023. &decl_list->declarations) {
  4024. if (!allow_reserved_names)
  4025. validate_identifier(decl->identifier, loc, state);
  4026. /* From the GL_ARB_uniform_buffer_object spec:
  4027. *
  4028. * "Sampler types are not allowed inside of uniform
  4029. * blocks. All other types, arrays, and structures
  4030. * allowed for uniforms are allowed within a uniform
  4031. * block."
  4032. *
  4033. * It should be impossible for decl_type to be NULL here. Cases that
  4034. * might naturally lead to decl_type being NULL, especially for the
  4035. * is_interface case, will have resulted in compilation having
  4036. * already halted due to a syntax error.
  4037. */
  4038. const struct glsl_type *field_type =
  4039. decl_type != NULL ? decl_type : glsl_type::error_type;
  4040. if (is_interface && field_type->contains_sampler()) {
  4041. YYLTYPE loc = decl_list->get_location();
  4042. _mesa_glsl_error(&loc, state,
  4043. "uniform in non-default uniform block contains sampler");
  4044. }
  4045. if (field_type->contains_atomic()) {
  4046. /* FINISHME: Add a spec quotation here once updated spec
  4047. * FINISHME: language is available. See Khronos bug #10903
  4048. * FINISHME: on whether atomic counters are allowed in
  4049. * FINISHME: structures.
  4050. */
  4051. YYLTYPE loc = decl_list->get_location();
  4052. _mesa_glsl_error(&loc, state, "atomic counter in structure or "
  4053. "uniform block");
  4054. }
  4055. const struct ast_type_qualifier *const qual =
  4056. & decl_list->type->qualifier;
  4057. if (qual->flags.q.std140 ||
  4058. qual->flags.q.packed ||
  4059. qual->flags.q.shared) {
  4060. _mesa_glsl_error(&loc, state,
  4061. "uniform block layout qualifiers std140, packed, and "
  4062. "shared can only be applied to uniform blocks, not "
  4063. "members");
  4064. }
  4065. if (decl->is_array) {
  4066. field_type = process_array_type(&loc, decl_type, decl->array_size,
  4067. state);
  4068. }
  4069. fields[i].type = field_type;
  4070. fields[i].name = decl->identifier;
  4071. fields[i].location = -1;
  4072. fields[i].interpolation =
  4073. interpret_interpolation_qualifier(qual, var_mode, state, &loc);
  4074. fields[i].centroid = qual->flags.q.centroid ? 1 : 0;
  4075. fields[i].sample = qual->flags.q.sample ? 1 : 0;
  4076. if (qual->flags.q.row_major || qual->flags.q.column_major) {
  4077. if (!qual->flags.q.uniform) {
  4078. _mesa_glsl_error(&loc, state,
  4079. "row_major and column_major can only be "
  4080. "applied to uniform interface blocks");
  4081. } else
  4082. validate_matrix_layout_for_type(state, &loc, field_type, NULL);
  4083. }
  4084. if (qual->flags.q.uniform && qual->has_interpolation()) {
  4085. _mesa_glsl_error(&loc, state,
  4086. "interpolation qualifiers cannot be used "
  4087. "with uniform interface blocks");
  4088. }
  4089. if (field_type->is_matrix() ||
  4090. (field_type->is_array() && field_type->fields.array->is_matrix())) {
  4091. fields[i].row_major = block_row_major;
  4092. if (qual->flags.q.row_major)
  4093. fields[i].row_major = true;
  4094. else if (qual->flags.q.column_major)
  4095. fields[i].row_major = false;
  4096. }
  4097. i++;
  4098. }
  4099. }
  4100. assert(i == decl_count);
  4101. *fields_ret = fields;
  4102. return decl_count;
  4103. }
  4104. ir_rvalue *
  4105. ast_struct_specifier::hir(exec_list *instructions,
  4106. struct _mesa_glsl_parse_state *state)
  4107. {
  4108. YYLTYPE loc = this->get_location();
  4109. /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
  4110. *
  4111. * "Anonymous structures are not supported; so embedded structures must
  4112. * have a declarator. A name given to an embedded struct is scoped at
  4113. * the same level as the struct it is embedded in."
  4114. *
  4115. * The same section of the GLSL 1.20 spec says:
  4116. *
  4117. * "Anonymous structures are not supported. Embedded structures are not
  4118. * supported.
  4119. *
  4120. * struct S { float f; };
  4121. * struct T {
  4122. * S; // Error: anonymous structures disallowed
  4123. * struct { ... }; // Error: embedded structures disallowed
  4124. * S s; // Okay: nested structures with name are allowed
  4125. * };"
  4126. *
  4127. * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
  4128. * we allow embedded structures in 1.10 only.
  4129. */
  4130. if (state->language_version != 110 && state->struct_specifier_depth != 0)
  4131. _mesa_glsl_error(&loc, state,
  4132. "embedded structure declartions are not allowed");
  4133. state->struct_specifier_depth++;
  4134. glsl_struct_field *fields;
  4135. unsigned decl_count =
  4136. ast_process_structure_or_interface_block(instructions,
  4137. state,
  4138. &this->declarations,
  4139. loc,
  4140. &fields,
  4141. false,
  4142. false,
  4143. false /* allow_reserved_names */,
  4144. ir_var_auto);
  4145. validate_identifier(this->name, loc, state);
  4146. const glsl_type *t =
  4147. glsl_type::get_record_instance(fields, decl_count, this->name);
  4148. if (!state->symbols->add_type(name, t)) {
  4149. _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
  4150. } else {
  4151. const glsl_type **s = reralloc(state, state->user_structures,
  4152. const glsl_type *,
  4153. state->num_user_structures + 1);
  4154. if (s != NULL) {
  4155. s[state->num_user_structures] = t;
  4156. state->user_structures = s;
  4157. state->num_user_structures++;
  4158. }
  4159. }
  4160. state->struct_specifier_depth--;
  4161. /* Structure type definitions do not have r-values.
  4162. */
  4163. return NULL;
  4164. }
  4165. /**
  4166. * Visitor class which detects whether a given interface block has been used.
  4167. */
  4168. class interface_block_usage_visitor : public ir_hierarchical_visitor
  4169. {
  4170. public:
  4171. interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block)
  4172. : mode(mode), block(block), found(false)
  4173. {
  4174. }
  4175. virtual ir_visitor_status visit(ir_dereference_variable *ir)
  4176. {
  4177. if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) {
  4178. found = true;
  4179. return visit_stop;
  4180. }
  4181. return visit_continue;
  4182. }
  4183. bool usage_found() const
  4184. {
  4185. return this->found;
  4186. }
  4187. private:
  4188. ir_variable_mode mode;
  4189. const glsl_type *block;
  4190. bool found;
  4191. };
  4192. ir_rvalue *
  4193. ast_interface_block::hir(exec_list *instructions,
  4194. struct _mesa_glsl_parse_state *state)
  4195. {
  4196. YYLTYPE loc = this->get_location();
  4197. /* The ast_interface_block has a list of ast_declarator_lists. We
  4198. * need to turn those into ir_variables with an association
  4199. * with this uniform block.
  4200. */
  4201. enum glsl_interface_packing packing;
  4202. if (this->layout.flags.q.shared) {
  4203. packing = GLSL_INTERFACE_PACKING_SHARED;
  4204. } else if (this->layout.flags.q.packed) {
  4205. packing = GLSL_INTERFACE_PACKING_PACKED;
  4206. } else {
  4207. /* The default layout is std140.
  4208. */
  4209. packing = GLSL_INTERFACE_PACKING_STD140;
  4210. }
  4211. ir_variable_mode var_mode;
  4212. const char *iface_type_name;
  4213. if (this->layout.flags.q.in) {
  4214. var_mode = ir_var_shader_in;
  4215. iface_type_name = "in";
  4216. } else if (this->layout.flags.q.out) {
  4217. var_mode = ir_var_shader_out;
  4218. iface_type_name = "out";
  4219. } else if (this->layout.flags.q.uniform) {
  4220. var_mode = ir_var_uniform;
  4221. iface_type_name = "uniform";
  4222. } else {
  4223. var_mode = ir_var_auto;
  4224. iface_type_name = "UNKNOWN";
  4225. assert(!"interface block layout qualifier not found!");
  4226. }
  4227. bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0;
  4228. bool block_row_major = this->layout.flags.q.row_major;
  4229. exec_list declared_variables;
  4230. glsl_struct_field *fields;
  4231. unsigned int num_variables =
  4232. ast_process_structure_or_interface_block(&declared_variables,
  4233. state,
  4234. &this->declarations,
  4235. loc,
  4236. &fields,
  4237. true,
  4238. block_row_major,
  4239. redeclaring_per_vertex,
  4240. var_mode);
  4241. if (!redeclaring_per_vertex)
  4242. validate_identifier(this->block_name, loc, state);
  4243. const glsl_type *earlier_per_vertex = NULL;
  4244. if (redeclaring_per_vertex) {
  4245. /* Find the previous declaration of gl_PerVertex. If we're redeclaring
  4246. * the named interface block gl_in, we can find it by looking at the
  4247. * previous declaration of gl_in. Otherwise we can find it by looking
  4248. * at the previous decalartion of any of the built-in outputs,
  4249. * e.g. gl_Position.
  4250. *
  4251. * Also check that the instance name and array-ness of the redeclaration
  4252. * are correct.
  4253. */
  4254. switch (var_mode) {
  4255. case ir_var_shader_in:
  4256. if (ir_variable *earlier_gl_in =
  4257. state->symbols->get_variable("gl_in")) {
  4258. earlier_per_vertex = earlier_gl_in->get_interface_type();
  4259. } else {
  4260. _mesa_glsl_error(&loc, state,
  4261. "redeclaration of gl_PerVertex input not allowed "
  4262. "in the %s shader",
  4263. _mesa_shader_stage_to_string(state->stage));
  4264. }
  4265. if (this->instance_name == NULL ||
  4266. strcmp(this->instance_name, "gl_in") != 0 || !this->is_array) {
  4267. _mesa_glsl_error(&loc, state,
  4268. "gl_PerVertex input must be redeclared as "
  4269. "gl_in[]");
  4270. }
  4271. break;
  4272. case ir_var_shader_out:
  4273. if (ir_variable *earlier_gl_Position =
  4274. state->symbols->get_variable("gl_Position")) {
  4275. earlier_per_vertex = earlier_gl_Position->get_interface_type();
  4276. } else {
  4277. _mesa_glsl_error(&loc, state,
  4278. "redeclaration of gl_PerVertex output not "
  4279. "allowed in the %s shader",
  4280. _mesa_shader_stage_to_string(state->stage));
  4281. }
  4282. if (this->instance_name != NULL) {
  4283. _mesa_glsl_error(&loc, state,
  4284. "gl_PerVertex input may not be redeclared with "
  4285. "an instance name");
  4286. }
  4287. break;
  4288. default:
  4289. _mesa_glsl_error(&loc, state,
  4290. "gl_PerVertex must be declared as an input or an "
  4291. "output");
  4292. break;
  4293. }
  4294. if (earlier_per_vertex == NULL) {
  4295. /* An error has already been reported. Bail out to avoid null
  4296. * dereferences later in this function.
  4297. */
  4298. return NULL;
  4299. }
  4300. /* Copy locations from the old gl_PerVertex interface block. */
  4301. for (unsigned i = 0; i < num_variables; i++) {
  4302. int j = earlier_per_vertex->field_index(fields[i].name);
  4303. if (j == -1) {
  4304. _mesa_glsl_error(&loc, state,
  4305. "redeclaration of gl_PerVertex must be a subset "
  4306. "of the built-in members of gl_PerVertex");
  4307. } else {
  4308. fields[i].location =
  4309. earlier_per_vertex->fields.structure[j].location;
  4310. fields[i].interpolation =
  4311. earlier_per_vertex->fields.structure[j].interpolation;
  4312. fields[i].centroid =
  4313. earlier_per_vertex->fields.structure[j].centroid;
  4314. fields[i].sample =
  4315. earlier_per_vertex->fields.structure[j].sample;
  4316. }
  4317. }
  4318. /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
  4319. * spec:
  4320. *
  4321. * If a built-in interface block is redeclared, it must appear in
  4322. * the shader before any use of any member included in the built-in
  4323. * declaration, or a compilation error will result.
  4324. *
  4325. * This appears to be a clarification to the behaviour established for
  4326. * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
  4327. * regardless of GLSL version.
  4328. */
  4329. interface_block_usage_visitor v(var_mode, earlier_per_vertex);
  4330. v.run(instructions);
  4331. if (v.usage_found()) {
  4332. _mesa_glsl_error(&loc, state,
  4333. "redeclaration of a built-in interface block must "
  4334. "appear before any use of any member of the "
  4335. "interface block");
  4336. }
  4337. }
  4338. const glsl_type *block_type =
  4339. glsl_type::get_interface_instance(fields,
  4340. num_variables,
  4341. packing,
  4342. this->block_name);
  4343. if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
  4344. YYLTYPE loc = this->get_location();
  4345. _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
  4346. "already taken in the current scope",
  4347. this->block_name, iface_type_name);
  4348. }
  4349. /* Since interface blocks cannot contain statements, it should be
  4350. * impossible for the block to generate any instructions.
  4351. */
  4352. assert(declared_variables.is_empty());
  4353. /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
  4354. *
  4355. * Geometry shader input variables get the per-vertex values written
  4356. * out by vertex shader output variables of the same names. Since a
  4357. * geometry shader operates on a set of vertices, each input varying
  4358. * variable (or input block, see interface blocks below) needs to be
  4359. * declared as an array.
  4360. */
  4361. if (state->stage == MESA_SHADER_GEOMETRY && !this->is_array &&
  4362. var_mode == ir_var_shader_in) {
  4363. _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
  4364. }
  4365. /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
  4366. * says:
  4367. *
  4368. * "If an instance name (instance-name) is used, then it puts all the
  4369. * members inside a scope within its own name space, accessed with the
  4370. * field selector ( . ) operator (analogously to structures)."
  4371. */
  4372. if (this->instance_name) {
  4373. if (redeclaring_per_vertex) {
  4374. /* When a built-in in an unnamed interface block is redeclared,
  4375. * get_variable_being_redeclared() calls
  4376. * check_builtin_array_max_size() to make sure that built-in array
  4377. * variables aren't redeclared to illegal sizes. But we're looking
  4378. * at a redeclaration of a named built-in interface block. So we
  4379. * have to manually call check_builtin_array_max_size() for all parts
  4380. * of the interface that are arrays.
  4381. */
  4382. for (unsigned i = 0; i < num_variables; i++) {
  4383. if (fields[i].type->is_array()) {
  4384. const unsigned size = fields[i].type->array_size();
  4385. check_builtin_array_max_size(fields[i].name, size, loc, state);
  4386. }
  4387. }
  4388. } else {
  4389. validate_identifier(this->instance_name, loc, state);
  4390. }
  4391. ir_variable *var;
  4392. if (this->is_array) {
  4393. /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
  4394. *
  4395. * For uniform blocks declared an array, each individual array
  4396. * element corresponds to a separate buffer object backing one
  4397. * instance of the block. As the array size indicates the number
  4398. * of buffer objects needed, uniform block array declarations
  4399. * must specify an array size.
  4400. *
  4401. * And a few paragraphs later:
  4402. *
  4403. * Geometry shader input blocks must be declared as arrays and
  4404. * follow the array declaration and linking rules for all
  4405. * geometry shader inputs. All other input and output block
  4406. * arrays must specify an array size.
  4407. *
  4408. * The upshot of this is that the only circumstance where an
  4409. * interface array size *doesn't* need to be specified is on a
  4410. * geometry shader input.
  4411. */
  4412. if (this->array_size == NULL &&
  4413. (state->stage != MESA_SHADER_GEOMETRY || !this->layout.flags.q.in)) {
  4414. _mesa_glsl_error(&loc, state,
  4415. "only geometry shader inputs may be unsized "
  4416. "instance block arrays");
  4417. }
  4418. const glsl_type *block_array_type =
  4419. process_array_type(&loc, block_type, this->array_size, state);
  4420. var = new(state) ir_variable(block_array_type,
  4421. this->instance_name,
  4422. var_mode);
  4423. } else {
  4424. var = new(state) ir_variable(block_type,
  4425. this->instance_name,
  4426. var_mode);
  4427. }
  4428. if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in)
  4429. handle_geometry_shader_input_decl(state, loc, var);
  4430. if (ir_variable *earlier =
  4431. state->symbols->get_variable(this->instance_name)) {
  4432. if (!redeclaring_per_vertex) {
  4433. _mesa_glsl_error(&loc, state, "`%s' redeclared",
  4434. this->instance_name);
  4435. }
  4436. earlier->data.how_declared = ir_var_declared_normally;
  4437. earlier->type = var->type;
  4438. earlier->reinit_interface_type(block_type);
  4439. delete var;
  4440. } else {
  4441. state->symbols->add_variable(var);
  4442. instructions->push_tail(var);
  4443. }
  4444. } else {
  4445. /* In order to have an array size, the block must also be declared with
  4446. * an instane name.
  4447. */
  4448. assert(!this->is_array);
  4449. for (unsigned i = 0; i < num_variables; i++) {
  4450. ir_variable *var =
  4451. new(state) ir_variable(fields[i].type,
  4452. ralloc_strdup(state, fields[i].name),
  4453. var_mode);
  4454. var->data.interpolation = fields[i].interpolation;
  4455. var->data.centroid = fields[i].centroid;
  4456. var->data.sample = fields[i].sample;
  4457. var->init_interface_type(block_type);
  4458. if (redeclaring_per_vertex) {
  4459. ir_variable *earlier =
  4460. get_variable_being_redeclared(var, loc, state,
  4461. true /* allow_all_redeclarations */);
  4462. if (strncmp(var->name, "gl_", 3) != 0 || earlier == NULL) {
  4463. _mesa_glsl_error(&loc, state,
  4464. "redeclaration of gl_PerVertex can only "
  4465. "include built-in variables");
  4466. } else if (earlier->data.how_declared == ir_var_declared_normally) {
  4467. _mesa_glsl_error(&loc, state,
  4468. "`%s' has already been redeclared", var->name);
  4469. } else {
  4470. earlier->data.how_declared = ir_var_declared_in_block;
  4471. earlier->reinit_interface_type(block_type);
  4472. }
  4473. continue;
  4474. }
  4475. if (state->symbols->get_variable(var->name) != NULL)
  4476. _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
  4477. /* Propagate the "binding" keyword into this UBO's fields;
  4478. * the UBO declaration itself doesn't get an ir_variable unless it
  4479. * has an instance name. This is ugly.
  4480. */
  4481. var->data.explicit_binding = this->layout.flags.q.explicit_binding;
  4482. var->data.binding = this->layout.binding;
  4483. state->symbols->add_variable(var);
  4484. instructions->push_tail(var);
  4485. }
  4486. if (redeclaring_per_vertex && block_type != earlier_per_vertex) {
  4487. /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
  4488. *
  4489. * It is also a compilation error ... to redeclare a built-in
  4490. * block and then use a member from that built-in block that was
  4491. * not included in the redeclaration.
  4492. *
  4493. * This appears to be a clarification to the behaviour established
  4494. * for gl_PerVertex by GLSL 1.50, therefore we implement this
  4495. * behaviour regardless of GLSL version.
  4496. *
  4497. * To prevent the shader from using a member that was not included in
  4498. * the redeclaration, we disable any ir_variables that are still
  4499. * associated with the old declaration of gl_PerVertex (since we've
  4500. * already updated all of the variables contained in the new
  4501. * gl_PerVertex to point to it).
  4502. *
  4503. * As a side effect this will prevent
  4504. * validate_intrastage_interface_blocks() from getting confused and
  4505. * thinking there are conflicting definitions of gl_PerVertex in the
  4506. * shader.
  4507. */
  4508. foreach_list_safe(node, instructions) {
  4509. ir_variable *const var = ((ir_instruction *) node)->as_variable();
  4510. if (var != NULL &&
  4511. var->get_interface_type() == earlier_per_vertex &&
  4512. var->data.mode == var_mode) {
  4513. if (var->data.how_declared == ir_var_declared_normally) {
  4514. _mesa_glsl_error(&loc, state,
  4515. "redeclaration of gl_PerVertex cannot "
  4516. "follow a redeclaration of `%s'",
  4517. var->name);
  4518. }
  4519. state->symbols->disable_variable(var->name);
  4520. var->remove();
  4521. }
  4522. }
  4523. }
  4524. }
  4525. return NULL;
  4526. }
  4527. ir_rvalue *
  4528. ast_gs_input_layout::hir(exec_list *instructions,
  4529. struct _mesa_glsl_parse_state *state)
  4530. {
  4531. YYLTYPE loc = this->get_location();
  4532. /* If any geometry input layout declaration preceded this one, make sure it
  4533. * was consistent with this one.
  4534. */
  4535. if (state->gs_input_prim_type_specified &&
  4536. state->gs_input_prim_type != this->prim_type) {
  4537. _mesa_glsl_error(&loc, state,
  4538. "geometry shader input layout does not match"
  4539. " previous declaration");
  4540. return NULL;
  4541. }
  4542. /* If any shader inputs occurred before this declaration and specified an
  4543. * array size, make sure the size they specified is consistent with the
  4544. * primitive type.
  4545. */
  4546. unsigned num_vertices = vertices_per_prim(this->prim_type);
  4547. if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
  4548. _mesa_glsl_error(&loc, state,
  4549. "this geometry shader input layout implies %u vertices"
  4550. " per primitive, but a previous input is declared"
  4551. " with size %u", num_vertices, state->gs_input_size);
  4552. return NULL;
  4553. }
  4554. state->gs_input_prim_type_specified = true;
  4555. state->gs_input_prim_type = this->prim_type;
  4556. /* If any shader inputs occurred before this declaration and did not
  4557. * specify an array size, their size is determined now.
  4558. */
  4559. foreach_list (node, instructions) {
  4560. ir_variable *var = ((ir_instruction *) node)->as_variable();
  4561. if (var == NULL || var->data.mode != ir_var_shader_in)
  4562. continue;
  4563. /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
  4564. * array; skip it.
  4565. */
  4566. if (var->type->is_unsized_array()) {
  4567. if (var->data.max_array_access >= num_vertices) {
  4568. _mesa_glsl_error(&loc, state,
  4569. "this geometry shader input layout implies %u"
  4570. " vertices, but an access to element %u of input"
  4571. " `%s' already exists", num_vertices,
  4572. var->data.max_array_access, var->name);
  4573. } else {
  4574. var->type = glsl_type::get_array_instance(var->type->fields.array,
  4575. num_vertices);
  4576. }
  4577. }
  4578. }
  4579. return NULL;
  4580. }
  4581. static void
  4582. detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
  4583. exec_list *instructions)
  4584. {
  4585. bool gl_FragColor_assigned = false;
  4586. bool gl_FragData_assigned = false;
  4587. bool user_defined_fs_output_assigned = false;
  4588. ir_variable *user_defined_fs_output = NULL;
  4589. /* It would be nice to have proper location information. */
  4590. YYLTYPE loc;
  4591. memset(&loc, 0, sizeof(loc));
  4592. foreach_list(node, instructions) {
  4593. ir_variable *var = ((ir_instruction *)node)->as_variable();
  4594. if (!var || !var->data.assigned)
  4595. continue;
  4596. if (strcmp(var->name, "gl_FragColor") == 0)
  4597. gl_FragColor_assigned = true;
  4598. else if (strcmp(var->name, "gl_FragData") == 0)
  4599. gl_FragData_assigned = true;
  4600. else if (strncmp(var->name, "gl_", 3) != 0) {
  4601. if (state->stage == MESA_SHADER_FRAGMENT &&
  4602. var->data.mode == ir_var_shader_out) {
  4603. user_defined_fs_output_assigned = true;
  4604. user_defined_fs_output = var;
  4605. }
  4606. }
  4607. }
  4608. /* From the GLSL 1.30 spec:
  4609. *
  4610. * "If a shader statically assigns a value to gl_FragColor, it
  4611. * may not assign a value to any element of gl_FragData. If a
  4612. * shader statically writes a value to any element of
  4613. * gl_FragData, it may not assign a value to
  4614. * gl_FragColor. That is, a shader may assign values to either
  4615. * gl_FragColor or gl_FragData, but not both. Multiple shaders
  4616. * linked together must also consistently write just one of
  4617. * these variables. Similarly, if user declared output
  4618. * variables are in use (statically assigned to), then the
  4619. * built-in variables gl_FragColor and gl_FragData may not be
  4620. * assigned to. These incorrect usages all generate compile
  4621. * time errors."
  4622. */
  4623. if (gl_FragColor_assigned && gl_FragData_assigned) {
  4624. _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  4625. "`gl_FragColor' and `gl_FragData'");
  4626. } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
  4627. _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  4628. "`gl_FragColor' and `%s'",
  4629. user_defined_fs_output->name);
  4630. } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
  4631. _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  4632. "`gl_FragData' and `%s'",
  4633. user_defined_fs_output->name);
  4634. }
  4635. }
  4636. static void
  4637. remove_per_vertex_blocks(exec_list *instructions,
  4638. _mesa_glsl_parse_state *state, ir_variable_mode mode)
  4639. {
  4640. /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
  4641. * if it exists in this shader type.
  4642. */
  4643. const glsl_type *per_vertex = NULL;
  4644. switch (mode) {
  4645. case ir_var_shader_in:
  4646. if (ir_variable *gl_in = state->symbols->get_variable("gl_in"))
  4647. per_vertex = gl_in->get_interface_type();
  4648. break;
  4649. case ir_var_shader_out:
  4650. if (ir_variable *gl_Position =
  4651. state->symbols->get_variable("gl_Position")) {
  4652. per_vertex = gl_Position->get_interface_type();
  4653. }
  4654. break;
  4655. default:
  4656. assert(!"Unexpected mode");
  4657. break;
  4658. }
  4659. /* If we didn't find a built-in gl_PerVertex interface block, then we don't
  4660. * need to do anything.
  4661. */
  4662. if (per_vertex == NULL)
  4663. return;
  4664. /* If the interface block is used by the shader, then we don't need to do
  4665. * anything.
  4666. */
  4667. interface_block_usage_visitor v(mode, per_vertex);
  4668. v.run(instructions);
  4669. if (v.usage_found())
  4670. return;
  4671. /* Remove any ir_variable declarations that refer to the interface block
  4672. * we're removing.
  4673. */
  4674. foreach_list_safe(node, instructions) {
  4675. ir_variable *const var = ((ir_instruction *) node)->as_variable();
  4676. if (var != NULL && var->get_interface_type() == per_vertex &&
  4677. var->data.mode == mode) {
  4678. state->symbols->disable_variable(var->name);
  4679. var->remove();
  4680. }
  4681. }
  4682. }