2
0

hlslGrammar.cpp 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170
  1. //
  2. // Copyright (C) 2016-2018 Google, Inc.
  3. // Copyright (C) 2016 LunarG, Inc.
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions
  9. // are met:
  10. //
  11. // Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. //
  14. // Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following
  16. // disclaimer in the documentation and/or other materials provided
  17. // with the distribution.
  18. //
  19. // Neither the name of Google, Inc., nor the names of its
  20. // contributors may be used to endorse or promote products derived
  21. // from this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //
  37. // This is a set of mutually recursive methods implementing the HLSL grammar.
  38. // Generally, each returns
  39. // - through an argument: a type specifically appropriate to which rule it
  40. // recognized
  41. // - through the return value: true/false to indicate whether or not it
  42. // recognized its rule
  43. //
  44. // As much as possible, only grammar recognition should happen in this file,
  45. // with all other work being farmed out to hlslParseHelper.cpp, which in turn
  46. // will build the AST.
  47. //
  48. // The next token, yet to be "accepted" is always sitting in 'token'.
  49. // When a method says it accepts a rule, that means all tokens involved
  50. // in the rule will have been consumed, and none left in 'token'.
  51. //
  52. #include "hlslTokens.h"
  53. #include "hlslGrammar.h"
  54. #include "hlslAttributes.h"
  55. namespace glslang {
  56. // Root entry point to this recursive decent parser.
  57. // Return true if compilation unit was successfully accepted.
  58. bool HlslGrammar::parse()
  59. {
  60. advanceToken();
  61. return acceptCompilationUnit();
  62. }
  63. void HlslGrammar::expected(const char* syntax)
  64. {
  65. parseContext.error(token.loc, "Expected", syntax, "");
  66. }
  67. void HlslGrammar::unimplemented(const char* error)
  68. {
  69. parseContext.error(token.loc, "Unimplemented", error, "");
  70. }
  71. // IDENTIFIER
  72. // THIS
  73. // type that can be used as IDENTIFIER
  74. //
  75. // Only process the next token if it is an identifier.
  76. // Return true if it was an identifier.
  77. bool HlslGrammar::acceptIdentifier(HlslToken& idToken)
  78. {
  79. // IDENTIFIER
  80. if (peekTokenClass(EHTokIdentifier)) {
  81. idToken = token;
  82. advanceToken();
  83. return true;
  84. }
  85. // THIS
  86. // -> maps to the IDENTIFIER spelled with the internal special name for 'this'
  87. if (peekTokenClass(EHTokThis)) {
  88. idToken = token;
  89. advanceToken();
  90. idToken.tokenClass = EHTokIdentifier;
  91. idToken.string = NewPoolTString(intermediate.implicitThisName);
  92. return true;
  93. }
  94. // type that can be used as IDENTIFIER
  95. // Even though "sample", "bool", "float", etc keywords (for types, interpolation modifiers),
  96. // they ARE still accepted as identifiers. This is not a dense space: e.g, "void" is not a
  97. // valid identifier, nor is "linear". This code special cases the known instances of this, so
  98. // e.g, "int sample;" or "float float;" is accepted. Other cases can be added here if needed.
  99. const char* idString = getTypeString(peek());
  100. if (idString == nullptr)
  101. return false;
  102. token.string = NewPoolTString(idString);
  103. token.tokenClass = EHTokIdentifier;
  104. idToken = token;
  105. typeIdentifiers = true;
  106. advanceToken();
  107. return true;
  108. }
  109. // compilationUnit
  110. // : declaration_list EOF
  111. //
  112. bool HlslGrammar::acceptCompilationUnit()
  113. {
  114. if (! acceptDeclarationList(unitNode))
  115. return false;
  116. if (! peekTokenClass(EHTokNone))
  117. return false;
  118. // set root of AST
  119. if (unitNode && !unitNode->getAsAggregate())
  120. unitNode = intermediate.growAggregate(nullptr, unitNode);
  121. intermediate.setTreeRoot(unitNode);
  122. return true;
  123. }
  124. // Recognize the following, but with the extra condition that it can be
  125. // successfully terminated by EOF or '}'.
  126. //
  127. // declaration_list
  128. // : list of declaration_or_semicolon followed by EOF or RIGHT_BRACE
  129. //
  130. // declaration_or_semicolon
  131. // : declaration
  132. // : SEMICOLON
  133. //
  134. bool HlslGrammar::acceptDeclarationList(TIntermNode*& nodeList)
  135. {
  136. do {
  137. // HLSL allows extra semicolons between global declarations
  138. do { } while (acceptTokenClass(EHTokSemicolon));
  139. // EOF or RIGHT_BRACE
  140. if (peekTokenClass(EHTokNone) || peekTokenClass(EHTokRightBrace))
  141. return true;
  142. // declaration
  143. if (! acceptDeclaration(nodeList))
  144. return false;
  145. } while (true);
  146. return true;
  147. }
  148. // sampler_state
  149. // : LEFT_BRACE [sampler_state_assignment ... ] RIGHT_BRACE
  150. //
  151. // sampler_state_assignment
  152. // : sampler_state_identifier EQUAL value SEMICOLON
  153. //
  154. // sampler_state_identifier
  155. // : ADDRESSU
  156. // | ADDRESSV
  157. // | ADDRESSW
  158. // | BORDERCOLOR
  159. // | FILTER
  160. // | MAXANISOTROPY
  161. // | MAXLOD
  162. // | MINLOD
  163. // | MIPLODBIAS
  164. //
  165. bool HlslGrammar::acceptSamplerState()
  166. {
  167. // TODO: this should be genericized to accept a list of valid tokens and
  168. // return token/value pairs. Presently it is specific to texture values.
  169. if (! acceptTokenClass(EHTokLeftBrace))
  170. return true;
  171. parseContext.warn(token.loc, "unimplemented", "immediate sampler state", "");
  172. do {
  173. // read state name
  174. HlslToken state;
  175. if (! acceptIdentifier(state))
  176. break; // end of list
  177. // FXC accepts any case
  178. TString stateName = *state.string;
  179. std::transform(stateName.begin(), stateName.end(), stateName.begin(), ::tolower);
  180. if (! acceptTokenClass(EHTokAssign)) {
  181. expected("assign");
  182. return false;
  183. }
  184. if (stateName == "minlod" || stateName == "maxlod") {
  185. if (! peekTokenClass(EHTokIntConstant)) {
  186. expected("integer");
  187. return false;
  188. }
  189. TIntermTyped* lod = nullptr;
  190. if (! acceptLiteral(lod)) // should never fail, since we just looked for an integer
  191. return false;
  192. } else if (stateName == "maxanisotropy") {
  193. if (! peekTokenClass(EHTokIntConstant)) {
  194. expected("integer");
  195. return false;
  196. }
  197. TIntermTyped* maxAnisotropy = nullptr;
  198. if (! acceptLiteral(maxAnisotropy)) // should never fail, since we just looked for an integer
  199. return false;
  200. } else if (stateName == "filter") {
  201. HlslToken filterMode;
  202. if (! acceptIdentifier(filterMode)) {
  203. expected("filter mode");
  204. return false;
  205. }
  206. } else if (stateName == "addressu" || stateName == "addressv" || stateName == "addressw") {
  207. HlslToken addrMode;
  208. if (! acceptIdentifier(addrMode)) {
  209. expected("texture address mode");
  210. return false;
  211. }
  212. } else if (stateName == "miplodbias") {
  213. TIntermTyped* lodBias = nullptr;
  214. if (! acceptLiteral(lodBias)) {
  215. expected("lod bias");
  216. return false;
  217. }
  218. } else if (stateName == "bordercolor") {
  219. return false;
  220. } else {
  221. expected("texture state");
  222. return false;
  223. }
  224. // SEMICOLON
  225. if (! acceptTokenClass(EHTokSemicolon)) {
  226. expected("semicolon");
  227. return false;
  228. }
  229. } while (true);
  230. if (! acceptTokenClass(EHTokRightBrace))
  231. return false;
  232. return true;
  233. }
  234. // sampler_declaration_dx9
  235. // : SAMPLER identifier EQUAL sampler_type sampler_state
  236. //
  237. bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/)
  238. {
  239. if (! acceptTokenClass(EHTokSampler))
  240. return false;
  241. // TODO: remove this when DX9 style declarations are implemented.
  242. unimplemented("Direct3D 9 sampler declaration");
  243. // read sampler name
  244. HlslToken name;
  245. if (! acceptIdentifier(name)) {
  246. expected("sampler name");
  247. return false;
  248. }
  249. if (! acceptTokenClass(EHTokAssign)) {
  250. expected("=");
  251. return false;
  252. }
  253. return false;
  254. }
  255. // declaration
  256. // : attributes attributed_declaration
  257. // | NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
  258. //
  259. // attributed_declaration
  260. // : sampler_declaration_dx9 post_decls SEMICOLON
  261. // | fully_specified_type // for cbuffer/tbuffer
  262. // | fully_specified_type declarator_list SEMICOLON // for non cbuffer/tbuffer
  263. // | fully_specified_type identifier function_parameters post_decls compound_statement // function definition
  264. // | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition
  265. // | typedef declaration
  266. //
  267. // declarator_list
  268. // : declarator COMMA declarator COMMA declarator... // zero or more declarators
  269. //
  270. // declarator
  271. // : identifier array_specifier post_decls
  272. // | identifier array_specifier post_decls EQUAL assignment_expression
  273. // | identifier function_parameters post_decls // function prototype
  274. //
  275. // Parsing has to go pretty far in to know whether it's a variable, prototype, or
  276. // function definition, so the implementation below doesn't perfectly divide up the grammar
  277. // as above. (The 'identifier' in the first item in init_declarator list is the
  278. // same as 'identifier' for function declarations.)
  279. //
  280. // This can generate more than one subtree, one per initializer or a function body.
  281. // All initializer subtrees are put in their own aggregate node, making one top-level
  282. // node for all the initializers. Each function created is a top-level node to grow
  283. // into the passed-in nodeList.
  284. //
  285. // If 'nodeList' is passed in as non-null, it must be an aggregate to extend for
  286. // each top-level node the declaration creates. Otherwise, if only one top-level
  287. // node in generated here, that is want is returned in nodeList.
  288. //
  289. bool HlslGrammar::acceptDeclaration(TIntermNode*& nodeList)
  290. {
  291. // NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
  292. if (acceptTokenClass(EHTokNamespace)) {
  293. HlslToken namespaceToken;
  294. if (!acceptIdentifier(namespaceToken)) {
  295. expected("namespace name");
  296. return false;
  297. }
  298. parseContext.pushNamespace(*namespaceToken.string);
  299. if (!acceptTokenClass(EHTokLeftBrace)) {
  300. expected("{");
  301. return false;
  302. }
  303. if (!acceptDeclarationList(nodeList)) {
  304. expected("declaration list");
  305. return false;
  306. }
  307. if (!acceptTokenClass(EHTokRightBrace)) {
  308. expected("}");
  309. return false;
  310. }
  311. parseContext.popNamespace();
  312. return true;
  313. }
  314. bool declarator_list = false; // true when processing comma separation
  315. // attributes
  316. TFunctionDeclarator declarator;
  317. acceptAttributes(declarator.attributes);
  318. // typedef
  319. bool typedefDecl = acceptTokenClass(EHTokTypedef);
  320. TType declaredType;
  321. // DX9 sampler declaration use a different syntax
  322. // DX9 shaders need to run through HLSL compiler (fxc) via a back compat mode, it isn't going to
  323. // be possible to simultaneously compile D3D10+ style shaders and DX9 shaders. If we want to compile DX9
  324. // HLSL shaders, this will have to be a master level switch
  325. // As such, the sampler keyword in D3D10+ turns into an automatic sampler type, and is commonly used
  326. // For that reason, this line is commented out
  327. // if (acceptSamplerDeclarationDX9(declaredType))
  328. // return true;
  329. bool forbidDeclarators = (peekTokenClass(EHTokCBuffer) || peekTokenClass(EHTokTBuffer));
  330. // fully_specified_type
  331. if (! acceptFullySpecifiedType(declaredType, nodeList, declarator.attributes, forbidDeclarators))
  332. return false;
  333. // cbuffer and tbuffer end with the closing '}'.
  334. // No semicolon is included.
  335. if (forbidDeclarators)
  336. return true;
  337. // declarator_list
  338. // : declarator
  339. // : identifier
  340. HlslToken idToken;
  341. TIntermAggregate* initializers = nullptr;
  342. while (acceptIdentifier(idToken)) {
  343. TString *fullName = idToken.string;
  344. if (parseContext.symbolTable.atGlobalLevel())
  345. parseContext.getFullNamespaceName(fullName);
  346. if (peekTokenClass(EHTokLeftParen)) {
  347. // looks like function parameters
  348. // merge in the attributes into the return type
  349. parseContext.transferTypeAttributes(token.loc, declarator.attributes, declaredType, true);
  350. // Potentially rename shader entry point function. No-op most of the time.
  351. parseContext.renameShaderFunction(fullName);
  352. // function_parameters
  353. declarator.function = new TFunction(fullName, declaredType);
  354. if (!acceptFunctionParameters(*declarator.function)) {
  355. expected("function parameter list");
  356. return false;
  357. }
  358. // post_decls
  359. acceptPostDecls(declarator.function->getWritableType().getQualifier());
  360. // compound_statement (function body definition) or just a prototype?
  361. declarator.loc = token.loc;
  362. if (peekTokenClass(EHTokLeftBrace)) {
  363. if (declarator_list)
  364. parseContext.error(idToken.loc, "function body can't be in a declarator list", "{", "");
  365. if (typedefDecl)
  366. parseContext.error(idToken.loc, "function body can't be in a typedef", "{", "");
  367. return acceptFunctionDefinition(declarator, nodeList, nullptr);
  368. } else {
  369. if (typedefDecl)
  370. parseContext.error(idToken.loc, "function typedefs not implemented", "{", "");
  371. parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, true);
  372. }
  373. } else {
  374. // A variable declaration.
  375. // merge in the attributes, the first time around, into the shared type
  376. if (! declarator_list)
  377. parseContext.transferTypeAttributes(token.loc, declarator.attributes, declaredType);
  378. // Fix the storage qualifier if it's a global.
  379. if (declaredType.getQualifier().storage == EvqTemporary && parseContext.symbolTable.atGlobalLevel())
  380. declaredType.getQualifier().storage = EvqUniform;
  381. // recognize array_specifier
  382. TArraySizes* arraySizes = nullptr;
  383. acceptArraySpecifier(arraySizes);
  384. // We can handle multiple variables per type declaration, so
  385. // the number of types can expand when arrayness is different.
  386. TType variableType;
  387. variableType.shallowCopy(declaredType);
  388. // In the most general case, arrayness is potentially coming both from the
  389. // declared type and from the variable: "int[] a[];" or just one or the other.
  390. // Merge it all to the variableType, so all arrayness is part of the variableType.
  391. variableType.transferArraySizes(arraySizes);
  392. variableType.copyArrayInnerSizes(declaredType.getArraySizes());
  393. // samplers accept immediate sampler state
  394. if (variableType.getBasicType() == EbtSampler) {
  395. if (! acceptSamplerState())
  396. return false;
  397. }
  398. // post_decls
  399. acceptPostDecls(variableType.getQualifier());
  400. // EQUAL assignment_expression
  401. TIntermTyped* expressionNode = nullptr;
  402. if (acceptTokenClass(EHTokAssign)) {
  403. if (typedefDecl)
  404. parseContext.error(idToken.loc, "can't have an initializer", "typedef", "");
  405. if (! acceptAssignmentExpression(expressionNode)) {
  406. expected("initializer");
  407. return false;
  408. }
  409. }
  410. // TODO: things scoped within an annotation need their own name space;
  411. // TODO: strings are not yet handled.
  412. if (variableType.getBasicType() != EbtString && parseContext.getAnnotationNestingLevel() == 0) {
  413. if (typedefDecl)
  414. parseContext.declareTypedef(idToken.loc, *fullName, variableType);
  415. else if (variableType.getBasicType() == EbtBlock) {
  416. if (expressionNode)
  417. parseContext.error(idToken.loc, "buffer aliasing not yet supported", "block initializer", "");
  418. parseContext.declareBlock(idToken.loc, variableType, fullName);
  419. parseContext.declareStructBufferCounter(idToken.loc, variableType, *fullName);
  420. } else {
  421. if (variableType.getQualifier().storage == EvqUniform && ! variableType.containsOpaque()) {
  422. // this isn't really an individual variable, but a member of the $Global buffer
  423. parseContext.growGlobalUniformBlock(idToken.loc, variableType, *fullName);
  424. } else {
  425. // Declare the variable and add any initializer code to the AST.
  426. // The top-level node is always made into an aggregate, as that's
  427. // historically how the AST has been.
  428. initializers = intermediate.growAggregate(initializers,
  429. parseContext.declareVariable(idToken.loc, *fullName, variableType, expressionNode),
  430. idToken.loc);
  431. }
  432. }
  433. }
  434. }
  435. // COMMA
  436. if (acceptTokenClass(EHTokComma))
  437. declarator_list = true;
  438. }
  439. // The top-level initializer node is a sequence.
  440. if (initializers != nullptr)
  441. initializers->setOperator(EOpSequence);
  442. // if we have a locally scoped static, it needs a globally scoped initializer
  443. if (declaredType.getQualifier().storage == EvqGlobal && !parseContext.symbolTable.atGlobalLevel()) {
  444. unitNode = intermediate.growAggregate(unitNode, initializers, idToken.loc);
  445. } else {
  446. // Add the initializers' aggregate to the nodeList we were handed.
  447. if (nodeList)
  448. nodeList = intermediate.growAggregate(nodeList, initializers);
  449. else
  450. nodeList = initializers;
  451. }
  452. // SEMICOLON
  453. if (! acceptTokenClass(EHTokSemicolon)) {
  454. // This may have been a false detection of what appeared to be a declaration, but
  455. // was actually an assignment such as "float = 4", where "float" is an identifier.
  456. // We put the token back to let further parsing happen for cases where that may
  457. // happen. This errors on the side of caution, and mostly triggers the error.
  458. if (peek() == EHTokAssign || peek() == EHTokLeftBracket || peek() == EHTokDot || peek() == EHTokComma)
  459. recedeToken();
  460. else
  461. expected(";");
  462. return false;
  463. }
  464. return true;
  465. }
  466. // control_declaration
  467. // : fully_specified_type identifier EQUAL expression
  468. //
  469. bool HlslGrammar::acceptControlDeclaration(TIntermNode*& node)
  470. {
  471. node = nullptr;
  472. TAttributes attributes;
  473. // fully_specified_type
  474. TType type;
  475. if (! acceptFullySpecifiedType(type, attributes))
  476. return false;
  477. if (attributes.size() > 0)
  478. parseContext.warn(token.loc, "attributes don't apply to control declaration", "", "");
  479. // filter out type casts
  480. if (peekTokenClass(EHTokLeftParen)) {
  481. recedeToken();
  482. return false;
  483. }
  484. // identifier
  485. HlslToken idToken;
  486. if (! acceptIdentifier(idToken)) {
  487. expected("identifier");
  488. return false;
  489. }
  490. // EQUAL
  491. TIntermTyped* expressionNode = nullptr;
  492. if (! acceptTokenClass(EHTokAssign)) {
  493. expected("=");
  494. return false;
  495. }
  496. // expression
  497. if (! acceptExpression(expressionNode)) {
  498. expected("initializer");
  499. return false;
  500. }
  501. node = parseContext.declareVariable(idToken.loc, *idToken.string, type, expressionNode);
  502. return true;
  503. }
  504. // fully_specified_type
  505. // : type_specifier
  506. // | type_qualifier type_specifier
  507. //
  508. bool HlslGrammar::acceptFullySpecifiedType(TType& type, const TAttributes& attributes)
  509. {
  510. TIntermNode* nodeList = nullptr;
  511. return acceptFullySpecifiedType(type, nodeList, attributes);
  512. }
  513. bool HlslGrammar::acceptFullySpecifiedType(TType& type, TIntermNode*& nodeList, const TAttributes& attributes, bool forbidDeclarators)
  514. {
  515. // type_qualifier
  516. TQualifier qualifier;
  517. qualifier.clear();
  518. if (! acceptQualifier(qualifier))
  519. return false;
  520. TSourceLoc loc = token.loc;
  521. // type_specifier
  522. if (! acceptType(type, nodeList)) {
  523. // If this is not a type, we may have inadvertently gone down a wrong path
  524. // by parsing "sample", which can be treated like either an identifier or a
  525. // qualifier. Back it out, if we did.
  526. if (qualifier.sample)
  527. recedeToken();
  528. return false;
  529. }
  530. if (type.getBasicType() == EbtBlock) {
  531. // the type was a block, which set some parts of the qualifier
  532. parseContext.mergeQualifiers(type.getQualifier(), qualifier);
  533. // merge in the attributes
  534. parseContext.transferTypeAttributes(token.loc, attributes, type);
  535. // further, it can create an anonymous instance of the block
  536. // (cbuffer and tbuffer don't consume the next identifier, and
  537. // should set forbidDeclarators)
  538. if (forbidDeclarators || peek() != EHTokIdentifier)
  539. parseContext.declareBlock(loc, type);
  540. } else {
  541. // Some qualifiers are set when parsing the type. Merge those with
  542. // whatever comes from acceptQualifier.
  543. assert(qualifier.layoutFormat == ElfNone);
  544. qualifier.layoutFormat = type.getQualifier().layoutFormat;
  545. qualifier.precision = type.getQualifier().precision;
  546. if (type.getQualifier().storage == EvqOut ||
  547. type.getQualifier().storage == EvqBuffer) {
  548. qualifier.storage = type.getQualifier().storage;
  549. qualifier.readonly = type.getQualifier().readonly;
  550. }
  551. if (type.isBuiltIn())
  552. qualifier.builtIn = type.getQualifier().builtIn;
  553. type.getQualifier() = qualifier;
  554. }
  555. return true;
  556. }
  557. // type_qualifier
  558. // : qualifier qualifier ...
  559. //
  560. // Zero or more of these, so this can't return false.
  561. //
  562. bool HlslGrammar::acceptQualifier(TQualifier& qualifier)
  563. {
  564. do {
  565. switch (peek()) {
  566. case EHTokStatic:
  567. qualifier.storage = EvqGlobal;
  568. break;
  569. case EHTokExtern:
  570. // TODO: no meaning in glslang?
  571. break;
  572. case EHTokShared:
  573. // TODO: hint
  574. break;
  575. case EHTokGroupShared:
  576. qualifier.storage = EvqShared;
  577. break;
  578. case EHTokUniform:
  579. qualifier.storage = EvqUniform;
  580. break;
  581. case EHTokConst:
  582. qualifier.storage = EvqConst;
  583. break;
  584. case EHTokVolatile:
  585. qualifier.volatil = true;
  586. break;
  587. case EHTokLinear:
  588. qualifier.smooth = true;
  589. break;
  590. case EHTokCentroid:
  591. qualifier.centroid = true;
  592. break;
  593. case EHTokNointerpolation:
  594. qualifier.flat = true;
  595. break;
  596. case EHTokNoperspective:
  597. qualifier.nopersp = true;
  598. break;
  599. case EHTokSample:
  600. qualifier.sample = true;
  601. break;
  602. case EHTokRowMajor:
  603. qualifier.layoutMatrix = ElmColumnMajor;
  604. break;
  605. case EHTokColumnMajor:
  606. qualifier.layoutMatrix = ElmRowMajor;
  607. break;
  608. case EHTokPrecise:
  609. qualifier.noContraction = true;
  610. break;
  611. case EHTokIn:
  612. qualifier.storage = (qualifier.storage == EvqOut) ? EvqInOut : EvqIn;
  613. break;
  614. case EHTokOut:
  615. qualifier.storage = (qualifier.storage == EvqIn) ? EvqInOut : EvqOut;
  616. break;
  617. case EHTokInOut:
  618. qualifier.storage = EvqInOut;
  619. break;
  620. case EHTokLayout:
  621. if (! acceptLayoutQualifierList(qualifier))
  622. return false;
  623. continue;
  624. case EHTokGloballyCoherent:
  625. qualifier.coherent = true;
  626. break;
  627. case EHTokInline:
  628. // TODO: map this to SPIR-V function control
  629. break;
  630. // GS geometries: these are specified on stage input variables, and are an error (not verified here)
  631. // for output variables.
  632. case EHTokPoint:
  633. qualifier.storage = EvqIn;
  634. if (!parseContext.handleInputGeometry(token.loc, ElgPoints))
  635. return false;
  636. break;
  637. case EHTokLine:
  638. qualifier.storage = EvqIn;
  639. if (!parseContext.handleInputGeometry(token.loc, ElgLines))
  640. return false;
  641. break;
  642. case EHTokTriangle:
  643. qualifier.storage = EvqIn;
  644. if (!parseContext.handleInputGeometry(token.loc, ElgTriangles))
  645. return false;
  646. break;
  647. case EHTokLineAdj:
  648. qualifier.storage = EvqIn;
  649. if (!parseContext.handleInputGeometry(token.loc, ElgLinesAdjacency))
  650. return false;
  651. break;
  652. case EHTokTriangleAdj:
  653. qualifier.storage = EvqIn;
  654. if (!parseContext.handleInputGeometry(token.loc, ElgTrianglesAdjacency))
  655. return false;
  656. break;
  657. default:
  658. return true;
  659. }
  660. advanceToken();
  661. } while (true);
  662. }
  663. // layout_qualifier_list
  664. // : LAYOUT LEFT_PAREN layout_qualifier COMMA layout_qualifier ... RIGHT_PAREN
  665. //
  666. // layout_qualifier
  667. // : identifier
  668. // | identifier EQUAL expression
  669. //
  670. // Zero or more of these, so this can't return false.
  671. //
  672. bool HlslGrammar::acceptLayoutQualifierList(TQualifier& qualifier)
  673. {
  674. if (! acceptTokenClass(EHTokLayout))
  675. return false;
  676. // LEFT_PAREN
  677. if (! acceptTokenClass(EHTokLeftParen))
  678. return false;
  679. do {
  680. // identifier
  681. HlslToken idToken;
  682. if (! acceptIdentifier(idToken))
  683. break;
  684. // EQUAL expression
  685. if (acceptTokenClass(EHTokAssign)) {
  686. TIntermTyped* expr;
  687. if (! acceptConditionalExpression(expr)) {
  688. expected("expression");
  689. return false;
  690. }
  691. parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string, expr);
  692. } else
  693. parseContext.setLayoutQualifier(idToken.loc, qualifier, *idToken.string);
  694. // COMMA
  695. if (! acceptTokenClass(EHTokComma))
  696. break;
  697. } while (true);
  698. // RIGHT_PAREN
  699. if (! acceptTokenClass(EHTokRightParen)) {
  700. expected(")");
  701. return false;
  702. }
  703. return true;
  704. }
  705. // template_type
  706. // : FLOAT
  707. // | DOUBLE
  708. // | INT
  709. // | DWORD
  710. // | UINT
  711. // | BOOL
  712. //
  713. bool HlslGrammar::acceptTemplateVecMatBasicType(TBasicType& basicType)
  714. {
  715. switch (peek()) {
  716. case EHTokFloat:
  717. basicType = EbtFloat;
  718. break;
  719. case EHTokDouble:
  720. basicType = EbtDouble;
  721. break;
  722. case EHTokInt:
  723. case EHTokDword:
  724. basicType = EbtInt;
  725. break;
  726. case EHTokUint:
  727. basicType = EbtUint;
  728. break;
  729. case EHTokBool:
  730. basicType = EbtBool;
  731. break;
  732. default:
  733. return false;
  734. }
  735. advanceToken();
  736. return true;
  737. }
  738. // vector_template_type
  739. // : VECTOR
  740. // | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
  741. //
  742. bool HlslGrammar::acceptVectorTemplateType(TType& type)
  743. {
  744. if (! acceptTokenClass(EHTokVector))
  745. return false;
  746. if (! acceptTokenClass(EHTokLeftAngle)) {
  747. // in HLSL, 'vector' alone means float4.
  748. new(&type) TType(EbtFloat, EvqTemporary, 4);
  749. return true;
  750. }
  751. TBasicType basicType;
  752. if (! acceptTemplateVecMatBasicType(basicType)) {
  753. expected("scalar type");
  754. return false;
  755. }
  756. // COMMA
  757. if (! acceptTokenClass(EHTokComma)) {
  758. expected(",");
  759. return false;
  760. }
  761. // integer
  762. if (! peekTokenClass(EHTokIntConstant)) {
  763. expected("literal integer");
  764. return false;
  765. }
  766. TIntermTyped* vecSize;
  767. if (! acceptLiteral(vecSize))
  768. return false;
  769. const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
  770. new(&type) TType(basicType, EvqTemporary, vecSizeI);
  771. if (vecSizeI == 1)
  772. type.makeVector();
  773. if (!acceptTokenClass(EHTokRightAngle)) {
  774. expected("right angle bracket");
  775. return false;
  776. }
  777. return true;
  778. }
  779. // matrix_template_type
  780. // : MATRIX
  781. // | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
  782. //
  783. bool HlslGrammar::acceptMatrixTemplateType(TType& type)
  784. {
  785. if (! acceptTokenClass(EHTokMatrix))
  786. return false;
  787. if (! acceptTokenClass(EHTokLeftAngle)) {
  788. // in HLSL, 'matrix' alone means float4x4.
  789. new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
  790. return true;
  791. }
  792. TBasicType basicType;
  793. if (! acceptTemplateVecMatBasicType(basicType)) {
  794. expected("scalar type");
  795. return false;
  796. }
  797. // COMMA
  798. if (! acceptTokenClass(EHTokComma)) {
  799. expected(",");
  800. return false;
  801. }
  802. // integer rows
  803. if (! peekTokenClass(EHTokIntConstant)) {
  804. expected("literal integer");
  805. return false;
  806. }
  807. TIntermTyped* rows;
  808. if (! acceptLiteral(rows))
  809. return false;
  810. // COMMA
  811. if (! acceptTokenClass(EHTokComma)) {
  812. expected(",");
  813. return false;
  814. }
  815. // integer cols
  816. if (! peekTokenClass(EHTokIntConstant)) {
  817. expected("literal integer");
  818. return false;
  819. }
  820. TIntermTyped* cols;
  821. if (! acceptLiteral(cols))
  822. return false;
  823. new(&type) TType(basicType, EvqTemporary, 0,
  824. rows->getAsConstantUnion()->getConstArray()[0].getIConst(),
  825. cols->getAsConstantUnion()->getConstArray()[0].getIConst());
  826. if (!acceptTokenClass(EHTokRightAngle)) {
  827. expected("right angle bracket");
  828. return false;
  829. }
  830. return true;
  831. }
  832. // layout_geometry
  833. // : LINESTREAM
  834. // | POINTSTREAM
  835. // | TRIANGLESTREAM
  836. //
  837. bool HlslGrammar::acceptOutputPrimitiveGeometry(TLayoutGeometry& geometry)
  838. {
  839. // read geometry type
  840. const EHlslTokenClass geometryType = peek();
  841. switch (geometryType) {
  842. case EHTokPointStream: geometry = ElgPoints; break;
  843. case EHTokLineStream: geometry = ElgLineStrip; break;
  844. case EHTokTriangleStream: geometry = ElgTriangleStrip; break;
  845. default:
  846. return false; // not a layout geometry
  847. }
  848. advanceToken(); // consume the layout keyword
  849. return true;
  850. }
  851. // tessellation_decl_type
  852. // : INPUTPATCH
  853. // | OUTPUTPATCH
  854. //
  855. bool HlslGrammar::acceptTessellationDeclType(TBuiltInVariable& patchType)
  856. {
  857. // read geometry type
  858. const EHlslTokenClass tessType = peek();
  859. switch (tessType) {
  860. case EHTokInputPatch: patchType = EbvInputPatch; break;
  861. case EHTokOutputPatch: patchType = EbvOutputPatch; break;
  862. default:
  863. return false; // not a tessellation decl
  864. }
  865. advanceToken(); // consume the keyword
  866. return true;
  867. }
  868. // tessellation_patch_template_type
  869. // : tessellation_decl_type LEFT_ANGLE type comma integer_literal RIGHT_ANGLE
  870. //
  871. bool HlslGrammar::acceptTessellationPatchTemplateType(TType& type)
  872. {
  873. TBuiltInVariable patchType;
  874. if (! acceptTessellationDeclType(patchType))
  875. return false;
  876. if (! acceptTokenClass(EHTokLeftAngle))
  877. return false;
  878. if (! acceptType(type)) {
  879. expected("tessellation patch type");
  880. return false;
  881. }
  882. if (! acceptTokenClass(EHTokComma))
  883. return false;
  884. // integer size
  885. if (! peekTokenClass(EHTokIntConstant)) {
  886. expected("literal integer");
  887. return false;
  888. }
  889. TIntermTyped* size;
  890. if (! acceptLiteral(size))
  891. return false;
  892. TArraySizes* arraySizes = new TArraySizes;
  893. arraySizes->addInnerSize(size->getAsConstantUnion()->getConstArray()[0].getIConst());
  894. type.transferArraySizes(arraySizes);
  895. type.getQualifier().builtIn = patchType;
  896. if (! acceptTokenClass(EHTokRightAngle)) {
  897. expected("right angle bracket");
  898. return false;
  899. }
  900. return true;
  901. }
  902. // stream_out_template_type
  903. // : output_primitive_geometry_type LEFT_ANGLE type RIGHT_ANGLE
  904. //
  905. bool HlslGrammar::acceptStreamOutTemplateType(TType& type, TLayoutGeometry& geometry)
  906. {
  907. geometry = ElgNone;
  908. if (! acceptOutputPrimitiveGeometry(geometry))
  909. return false;
  910. if (! acceptTokenClass(EHTokLeftAngle))
  911. return false;
  912. if (! acceptType(type)) {
  913. expected("stream output type");
  914. return false;
  915. }
  916. type.getQualifier().storage = EvqOut;
  917. type.getQualifier().builtIn = EbvGsOutputStream;
  918. if (! acceptTokenClass(EHTokRightAngle)) {
  919. expected("right angle bracket");
  920. return false;
  921. }
  922. return true;
  923. }
  924. // annotations
  925. // : LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
  926. //
  927. bool HlslGrammar::acceptAnnotations(TQualifier&)
  928. {
  929. if (! acceptTokenClass(EHTokLeftAngle))
  930. return false;
  931. // note that we are nesting a name space
  932. parseContext.nestAnnotations();
  933. // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE
  934. do {
  935. // eat any extra SEMI_COLON; don't know if the grammar calls for this or not
  936. while (acceptTokenClass(EHTokSemicolon))
  937. ;
  938. if (acceptTokenClass(EHTokRightAngle))
  939. break;
  940. // declaration
  941. TIntermNode* node = nullptr;
  942. if (! acceptDeclaration(node)) {
  943. expected("declaration in annotation");
  944. return false;
  945. }
  946. } while (true);
  947. parseContext.unnestAnnotations();
  948. return true;
  949. }
  950. // subpass input type
  951. // : SUBPASSINPUT
  952. // | SUBPASSINPUT VECTOR LEFT_ANGLE template_type RIGHT_ANGLE
  953. // | SUBPASSINPUTMS
  954. // | SUBPASSINPUTMS VECTOR LEFT_ANGLE template_type RIGHT_ANGLE
  955. bool HlslGrammar::acceptSubpassInputType(TType& type)
  956. {
  957. // read subpass type
  958. const EHlslTokenClass subpassInputType = peek();
  959. bool multisample;
  960. switch (subpassInputType) {
  961. case EHTokSubpassInput: multisample = false; break;
  962. case EHTokSubpassInputMS: multisample = true; break;
  963. default:
  964. return false; // not a subpass input declaration
  965. }
  966. advanceToken(); // consume the sampler type keyword
  967. TType subpassType(EbtFloat, EvqUniform, 4); // default type is float4
  968. if (acceptTokenClass(EHTokLeftAngle)) {
  969. if (! acceptType(subpassType)) {
  970. expected("scalar or vector type");
  971. return false;
  972. }
  973. const TBasicType basicRetType = subpassType.getBasicType() ;
  974. switch (basicRetType) {
  975. case EbtFloat:
  976. case EbtUint:
  977. case EbtInt:
  978. case EbtStruct:
  979. break;
  980. default:
  981. unimplemented("basic type in subpass input");
  982. return false;
  983. }
  984. if (! acceptTokenClass(EHTokRightAngle)) {
  985. expected("right angle bracket");
  986. return false;
  987. }
  988. }
  989. const TBasicType subpassBasicType = subpassType.isStruct() ? (*subpassType.getStruct())[0].type->getBasicType()
  990. : subpassType.getBasicType();
  991. TSampler sampler;
  992. sampler.setSubpass(subpassBasicType, multisample);
  993. // Remember the declared return type. Function returns false on error.
  994. if (!parseContext.setTextureReturnType(sampler, subpassType, token.loc))
  995. return false;
  996. type.shallowCopy(TType(sampler, EvqUniform));
  997. return true;
  998. }
  999. // sampler_type for DX9 compatibility
  1000. // : SAMPLER
  1001. // | SAMPLER1D
  1002. // | SAMPLER2D
  1003. // | SAMPLER3D
  1004. // | SAMPLERCUBE
  1005. bool HlslGrammar::acceptSamplerTypeDX9(TType &type)
  1006. {
  1007. // read sampler type
  1008. const EHlslTokenClass samplerType = peek();
  1009. TSamplerDim dim = EsdNone;
  1010. TType txType(EbtFloat, EvqUniform, 4); // default type is float4
  1011. bool isShadow = false;
  1012. switch (samplerType)
  1013. {
  1014. case EHTokSampler: dim = Esd2D; break;
  1015. case EHTokSampler1d: dim = Esd1D; break;
  1016. case EHTokSampler2d: dim = Esd2D; break;
  1017. case EHTokSampler3d: dim = Esd3D; break;
  1018. case EHTokSamplerCube: dim = EsdCube; break;
  1019. default:
  1020. return false; // not a dx9 sampler declaration
  1021. }
  1022. advanceToken(); // consume the sampler type keyword
  1023. TArraySizes *arraySizes = nullptr; // TODO: array
  1024. TSampler sampler;
  1025. sampler.set(txType.getBasicType(), dim, false, isShadow, false);
  1026. if (!parseContext.setTextureReturnType(sampler, txType, token.loc))
  1027. return false;
  1028. type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
  1029. type.getQualifier().layoutFormat = ElfNone;
  1030. return true;
  1031. }
  1032. // sampler_type
  1033. // : SAMPLER
  1034. // | SAMPLER1D
  1035. // | SAMPLER2D
  1036. // | SAMPLER3D
  1037. // | SAMPLERCUBE
  1038. // | SAMPLERSTATE
  1039. // | SAMPLERCOMPARISONSTATE
  1040. bool HlslGrammar::acceptSamplerType(TType& type)
  1041. {
  1042. // read sampler type
  1043. const EHlslTokenClass samplerType = peek();
  1044. // TODO: for DX9
  1045. // TSamplerDim dim = EsdNone;
  1046. bool isShadow = false;
  1047. switch (samplerType) {
  1048. case EHTokSampler: break;
  1049. case EHTokSampler1d: /*dim = Esd1D*/; break;
  1050. case EHTokSampler2d: /*dim = Esd2D*/; break;
  1051. case EHTokSampler3d: /*dim = Esd3D*/; break;
  1052. case EHTokSamplerCube: /*dim = EsdCube*/; break;
  1053. case EHTokSamplerState: break;
  1054. case EHTokSamplerComparisonState: isShadow = true; break;
  1055. default:
  1056. return false; // not a sampler declaration
  1057. }
  1058. advanceToken(); // consume the sampler type keyword
  1059. TArraySizes* arraySizes = nullptr; // TODO: array
  1060. TSampler sampler;
  1061. sampler.setPureSampler(isShadow);
  1062. type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
  1063. return true;
  1064. }
  1065. // texture_type
  1066. // | BUFFER
  1067. // | TEXTURE1D
  1068. // | TEXTURE1DARRAY
  1069. // | TEXTURE2D
  1070. // | TEXTURE2DARRAY
  1071. // | TEXTURE3D
  1072. // | TEXTURECUBE
  1073. // | TEXTURECUBEARRAY
  1074. // | TEXTURE2DMS
  1075. // | TEXTURE2DMSARRAY
  1076. // | RWBUFFER
  1077. // | RWTEXTURE1D
  1078. // | RWTEXTURE1DARRAY
  1079. // | RWTEXTURE2D
  1080. // | RWTEXTURE2DARRAY
  1081. // | RWTEXTURE3D
  1082. bool HlslGrammar::acceptTextureType(TType& type)
  1083. {
  1084. const EHlslTokenClass textureType = peek();
  1085. TSamplerDim dim = EsdNone;
  1086. bool array = false;
  1087. bool ms = false;
  1088. bool image = false;
  1089. bool combined = true;
  1090. switch (textureType) {
  1091. case EHTokBuffer: dim = EsdBuffer; combined = false; break;
  1092. case EHTokTexture1d: dim = Esd1D; break;
  1093. case EHTokTexture1darray: dim = Esd1D; array = true; break;
  1094. case EHTokTexture2d: dim = Esd2D; break;
  1095. case EHTokTexture2darray: dim = Esd2D; array = true; break;
  1096. case EHTokTexture3d: dim = Esd3D; break;
  1097. case EHTokTextureCube: dim = EsdCube; break;
  1098. case EHTokTextureCubearray: dim = EsdCube; array = true; break;
  1099. case EHTokTexture2DMS: dim = Esd2D; ms = true; break;
  1100. case EHTokTexture2DMSarray: dim = Esd2D; array = true; ms = true; break;
  1101. case EHTokRWBuffer: dim = EsdBuffer; image=true; break;
  1102. case EHTokRWTexture1d: dim = Esd1D; array=false; image=true; break;
  1103. case EHTokRWTexture1darray: dim = Esd1D; array=true; image=true; break;
  1104. case EHTokRWTexture2d: dim = Esd2D; array=false; image=true; break;
  1105. case EHTokRWTexture2darray: dim = Esd2D; array=true; image=true; break;
  1106. case EHTokRWTexture3d: dim = Esd3D; array=false; image=true; break;
  1107. default:
  1108. return false; // not a texture declaration
  1109. }
  1110. advanceToken(); // consume the texture object keyword
  1111. TType txType(EbtFloat, EvqUniform, 4); // default type is float4
  1112. TIntermTyped* msCount = nullptr;
  1113. // texture type: required for multisample types and RWBuffer/RWTextures!
  1114. if (acceptTokenClass(EHTokLeftAngle)) {
  1115. if (! acceptType(txType)) {
  1116. expected("scalar or vector type");
  1117. return false;
  1118. }
  1119. const TBasicType basicRetType = txType.getBasicType() ;
  1120. switch (basicRetType) {
  1121. case EbtFloat:
  1122. case EbtUint:
  1123. case EbtInt:
  1124. case EbtStruct:
  1125. break;
  1126. default:
  1127. unimplemented("basic type in texture");
  1128. return false;
  1129. }
  1130. // Buffers can handle small mats if they fit in 4 components
  1131. if (dim == EsdBuffer && txType.isMatrix()) {
  1132. if ((txType.getMatrixCols() * txType.getMatrixRows()) > 4) {
  1133. expected("components < 4 in matrix buffer type");
  1134. return false;
  1135. }
  1136. // TODO: except we don't handle it yet...
  1137. unimplemented("matrix type in buffer");
  1138. return false;
  1139. }
  1140. if (!txType.isScalar() && !txType.isVector() && !txType.isStruct()) {
  1141. expected("scalar, vector, or struct type");
  1142. return false;
  1143. }
  1144. if (ms && acceptTokenClass(EHTokComma)) {
  1145. // read sample count for multisample types, if given
  1146. if (! peekTokenClass(EHTokIntConstant)) {
  1147. expected("multisample count");
  1148. return false;
  1149. }
  1150. if (! acceptLiteral(msCount)) // should never fail, since we just found an integer
  1151. return false;
  1152. }
  1153. if (! acceptTokenClass(EHTokRightAngle)) {
  1154. expected("right angle bracket");
  1155. return false;
  1156. }
  1157. } else if (ms) {
  1158. expected("texture type for multisample");
  1159. return false;
  1160. } else if (image) {
  1161. expected("type for RWTexture/RWBuffer");
  1162. return false;
  1163. }
  1164. TArraySizes* arraySizes = nullptr;
  1165. const bool shadow = false; // declared on the sampler
  1166. TSampler sampler;
  1167. TLayoutFormat format = ElfNone;
  1168. // Buffer, RWBuffer and RWTexture (images) require a TLayoutFormat. We handle only a limit set.
  1169. if (image || dim == EsdBuffer)
  1170. format = parseContext.getLayoutFromTxType(token.loc, txType);
  1171. const TBasicType txBasicType = txType.isStruct() ? (*txType.getStruct())[0].type->getBasicType()
  1172. : txType.getBasicType();
  1173. // Non-image Buffers are combined
  1174. if (dim == EsdBuffer && !image) {
  1175. sampler.set(txType.getBasicType(), dim, array);
  1176. } else {
  1177. // DX10 textures are separated. TODO: DX9.
  1178. if (image) {
  1179. sampler.setImage(txBasicType, dim, array, shadow, ms);
  1180. } else {
  1181. sampler.setTexture(txBasicType, dim, array, shadow, ms);
  1182. }
  1183. }
  1184. // Remember the declared return type. Function returns false on error.
  1185. if (!parseContext.setTextureReturnType(sampler, txType, token.loc))
  1186. return false;
  1187. // Force uncombined, if necessary
  1188. if (!combined)
  1189. sampler.combined = false;
  1190. type.shallowCopy(TType(sampler, EvqUniform, arraySizes));
  1191. type.getQualifier().layoutFormat = format;
  1192. return true;
  1193. }
  1194. // If token is for a type, update 'type' with the type information,
  1195. // and return true and advance.
  1196. // Otherwise, return false, and don't advance
  1197. bool HlslGrammar::acceptType(TType& type)
  1198. {
  1199. TIntermNode* nodeList = nullptr;
  1200. return acceptType(type, nodeList);
  1201. }
  1202. bool HlslGrammar::acceptType(TType& type, TIntermNode*& nodeList)
  1203. {
  1204. // Basic types for min* types, use native halfs if the option allows them.
  1205. bool enable16BitTypes = parseContext.hlslEnable16BitTypes();
  1206. const TBasicType min16float_bt = enable16BitTypes ? EbtFloat16 : EbtFloat;
  1207. const TBasicType min10float_bt = enable16BitTypes ? EbtFloat16 : EbtFloat;
  1208. const TBasicType half_bt = enable16BitTypes ? EbtFloat16 : EbtFloat;
  1209. const TBasicType min16int_bt = enable16BitTypes ? EbtInt16 : EbtInt;
  1210. const TBasicType min12int_bt = enable16BitTypes ? EbtInt16 : EbtInt;
  1211. const TBasicType min16uint_bt = enable16BitTypes ? EbtUint16 : EbtUint;
  1212. // Some types might have turned into identifiers. Take the hit for checking
  1213. // when this has happened.
  1214. if (typeIdentifiers) {
  1215. const char* identifierString = getTypeString(peek());
  1216. if (identifierString != nullptr) {
  1217. TString name = identifierString;
  1218. // if it's an identifier, it's not a type
  1219. if (parseContext.symbolTable.find(name) != nullptr)
  1220. return false;
  1221. }
  1222. }
  1223. bool isUnorm = false;
  1224. bool isSnorm = false;
  1225. // Accept snorm and unorm. Presently, this is ignored, save for an error check below.
  1226. switch (peek()) {
  1227. case EHTokUnorm:
  1228. isUnorm = true;
  1229. advanceToken(); // eat the token
  1230. break;
  1231. case EHTokSNorm:
  1232. isSnorm = true;
  1233. advanceToken(); // eat the token
  1234. break;
  1235. default:
  1236. break;
  1237. }
  1238. switch (peek()) {
  1239. case EHTokVector:
  1240. return acceptVectorTemplateType(type);
  1241. break;
  1242. case EHTokMatrix:
  1243. return acceptMatrixTemplateType(type);
  1244. break;
  1245. case EHTokPointStream: // fall through
  1246. case EHTokLineStream: // ...
  1247. case EHTokTriangleStream: // ...
  1248. {
  1249. TLayoutGeometry geometry;
  1250. if (! acceptStreamOutTemplateType(type, geometry))
  1251. return false;
  1252. if (! parseContext.handleOutputGeometry(token.loc, geometry))
  1253. return false;
  1254. return true;
  1255. }
  1256. case EHTokInputPatch: // fall through
  1257. case EHTokOutputPatch: // ...
  1258. {
  1259. if (! acceptTessellationPatchTemplateType(type))
  1260. return false;
  1261. return true;
  1262. }
  1263. case EHTokSampler: // fall through
  1264. case EHTokSampler1d: // ...
  1265. case EHTokSampler2d: // ...
  1266. case EHTokSampler3d: // ...
  1267. case EHTokSamplerCube: // ...
  1268. if (parseContext.hlslDX9Compatible())
  1269. return acceptSamplerTypeDX9(type);
  1270. else
  1271. return acceptSamplerType(type);
  1272. break;
  1273. case EHTokSamplerState: // fall through
  1274. case EHTokSamplerComparisonState: // ...
  1275. return acceptSamplerType(type);
  1276. break;
  1277. case EHTokSubpassInput: // fall through
  1278. case EHTokSubpassInputMS: // ...
  1279. return acceptSubpassInputType(type);
  1280. break;
  1281. case EHTokBuffer: // fall through
  1282. case EHTokTexture1d: // ...
  1283. case EHTokTexture1darray: // ...
  1284. case EHTokTexture2d: // ...
  1285. case EHTokTexture2darray: // ...
  1286. case EHTokTexture3d: // ...
  1287. case EHTokTextureCube: // ...
  1288. case EHTokTextureCubearray: // ...
  1289. case EHTokTexture2DMS: // ...
  1290. case EHTokTexture2DMSarray: // ...
  1291. case EHTokRWTexture1d: // ...
  1292. case EHTokRWTexture1darray: // ...
  1293. case EHTokRWTexture2d: // ...
  1294. case EHTokRWTexture2darray: // ...
  1295. case EHTokRWTexture3d: // ...
  1296. case EHTokRWBuffer: // ...
  1297. return acceptTextureType(type);
  1298. break;
  1299. case EHTokAppendStructuredBuffer:
  1300. case EHTokByteAddressBuffer:
  1301. case EHTokConsumeStructuredBuffer:
  1302. case EHTokRWByteAddressBuffer:
  1303. case EHTokRWStructuredBuffer:
  1304. case EHTokStructuredBuffer:
  1305. return acceptStructBufferType(type);
  1306. break;
  1307. case EHTokTextureBuffer:
  1308. return acceptTextureBufferType(type);
  1309. break;
  1310. case EHTokConstantBuffer:
  1311. return acceptConstantBufferType(type);
  1312. case EHTokClass:
  1313. case EHTokStruct:
  1314. case EHTokCBuffer:
  1315. case EHTokTBuffer:
  1316. return acceptStruct(type, nodeList);
  1317. case EHTokIdentifier:
  1318. // An identifier could be for a user-defined type.
  1319. // Note we cache the symbol table lookup, to save for a later rule
  1320. // when this is not a type.
  1321. if (parseContext.lookupUserType(*token.string, type) != nullptr) {
  1322. advanceToken();
  1323. return true;
  1324. } else
  1325. return false;
  1326. case EHTokVoid:
  1327. new(&type) TType(EbtVoid);
  1328. break;
  1329. case EHTokString:
  1330. new(&type) TType(EbtString);
  1331. break;
  1332. case EHTokFloat:
  1333. new(&type) TType(EbtFloat);
  1334. break;
  1335. case EHTokFloat1:
  1336. new(&type) TType(EbtFloat);
  1337. type.makeVector();
  1338. break;
  1339. case EHTokFloat2:
  1340. new(&type) TType(EbtFloat, EvqTemporary, 2);
  1341. break;
  1342. case EHTokFloat3:
  1343. new(&type) TType(EbtFloat, EvqTemporary, 3);
  1344. break;
  1345. case EHTokFloat4:
  1346. new(&type) TType(EbtFloat, EvqTemporary, 4);
  1347. break;
  1348. case EHTokDouble:
  1349. new(&type) TType(EbtDouble);
  1350. break;
  1351. case EHTokDouble1:
  1352. new(&type) TType(EbtDouble);
  1353. type.makeVector();
  1354. break;
  1355. case EHTokDouble2:
  1356. new(&type) TType(EbtDouble, EvqTemporary, 2);
  1357. break;
  1358. case EHTokDouble3:
  1359. new(&type) TType(EbtDouble, EvqTemporary, 3);
  1360. break;
  1361. case EHTokDouble4:
  1362. new(&type) TType(EbtDouble, EvqTemporary, 4);
  1363. break;
  1364. case EHTokInt:
  1365. case EHTokDword:
  1366. new(&type) TType(EbtInt);
  1367. break;
  1368. case EHTokInt1:
  1369. new(&type) TType(EbtInt);
  1370. type.makeVector();
  1371. break;
  1372. case EHTokInt2:
  1373. new(&type) TType(EbtInt, EvqTemporary, 2);
  1374. break;
  1375. case EHTokInt3:
  1376. new(&type) TType(EbtInt, EvqTemporary, 3);
  1377. break;
  1378. case EHTokInt4:
  1379. new(&type) TType(EbtInt, EvqTemporary, 4);
  1380. break;
  1381. case EHTokUint:
  1382. new(&type) TType(EbtUint);
  1383. break;
  1384. case EHTokUint1:
  1385. new(&type) TType(EbtUint);
  1386. type.makeVector();
  1387. break;
  1388. case EHTokUint2:
  1389. new(&type) TType(EbtUint, EvqTemporary, 2);
  1390. break;
  1391. case EHTokUint3:
  1392. new(&type) TType(EbtUint, EvqTemporary, 3);
  1393. break;
  1394. case EHTokUint4:
  1395. new(&type) TType(EbtUint, EvqTemporary, 4);
  1396. break;
  1397. case EHTokUint64:
  1398. new(&type) TType(EbtUint64);
  1399. break;
  1400. case EHTokBool:
  1401. new(&type) TType(EbtBool);
  1402. break;
  1403. case EHTokBool1:
  1404. new(&type) TType(EbtBool);
  1405. type.makeVector();
  1406. break;
  1407. case EHTokBool2:
  1408. new(&type) TType(EbtBool, EvqTemporary, 2);
  1409. break;
  1410. case EHTokBool3:
  1411. new(&type) TType(EbtBool, EvqTemporary, 3);
  1412. break;
  1413. case EHTokBool4:
  1414. new(&type) TType(EbtBool, EvqTemporary, 4);
  1415. break;
  1416. case EHTokHalf:
  1417. new(&type) TType(half_bt, EvqTemporary);
  1418. break;
  1419. case EHTokHalf1:
  1420. new(&type) TType(half_bt, EvqTemporary);
  1421. type.makeVector();
  1422. break;
  1423. case EHTokHalf2:
  1424. new(&type) TType(half_bt, EvqTemporary, 2);
  1425. break;
  1426. case EHTokHalf3:
  1427. new(&type) TType(half_bt, EvqTemporary, 3);
  1428. break;
  1429. case EHTokHalf4:
  1430. new(&type) TType(half_bt, EvqTemporary, 4);
  1431. break;
  1432. case EHTokMin16float:
  1433. new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
  1434. break;
  1435. case EHTokMin16float1:
  1436. new(&type) TType(min16float_bt, EvqTemporary, EpqMedium);
  1437. type.makeVector();
  1438. break;
  1439. case EHTokMin16float2:
  1440. new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 2);
  1441. break;
  1442. case EHTokMin16float3:
  1443. new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 3);
  1444. break;
  1445. case EHTokMin16float4:
  1446. new(&type) TType(min16float_bt, EvqTemporary, EpqMedium, 4);
  1447. break;
  1448. case EHTokMin10float:
  1449. new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
  1450. break;
  1451. case EHTokMin10float1:
  1452. new(&type) TType(min10float_bt, EvqTemporary, EpqMedium);
  1453. type.makeVector();
  1454. break;
  1455. case EHTokMin10float2:
  1456. new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 2);
  1457. break;
  1458. case EHTokMin10float3:
  1459. new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 3);
  1460. break;
  1461. case EHTokMin10float4:
  1462. new(&type) TType(min10float_bt, EvqTemporary, EpqMedium, 4);
  1463. break;
  1464. case EHTokMin16int:
  1465. new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
  1466. break;
  1467. case EHTokMin16int1:
  1468. new(&type) TType(min16int_bt, EvqTemporary, EpqMedium);
  1469. type.makeVector();
  1470. break;
  1471. case EHTokMin16int2:
  1472. new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 2);
  1473. break;
  1474. case EHTokMin16int3:
  1475. new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 3);
  1476. break;
  1477. case EHTokMin16int4:
  1478. new(&type) TType(min16int_bt, EvqTemporary, EpqMedium, 4);
  1479. break;
  1480. case EHTokMin12int:
  1481. new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
  1482. break;
  1483. case EHTokMin12int1:
  1484. new(&type) TType(min12int_bt, EvqTemporary, EpqMedium);
  1485. type.makeVector();
  1486. break;
  1487. case EHTokMin12int2:
  1488. new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 2);
  1489. break;
  1490. case EHTokMin12int3:
  1491. new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 3);
  1492. break;
  1493. case EHTokMin12int4:
  1494. new(&type) TType(min12int_bt, EvqTemporary, EpqMedium, 4);
  1495. break;
  1496. case EHTokMin16uint:
  1497. new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
  1498. break;
  1499. case EHTokMin16uint1:
  1500. new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium);
  1501. type.makeVector();
  1502. break;
  1503. case EHTokMin16uint2:
  1504. new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 2);
  1505. break;
  1506. case EHTokMin16uint3:
  1507. new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 3);
  1508. break;
  1509. case EHTokMin16uint4:
  1510. new(&type) TType(min16uint_bt, EvqTemporary, EpqMedium, 4);
  1511. break;
  1512. case EHTokInt1x1:
  1513. new(&type) TType(EbtInt, EvqTemporary, 0, 1, 1);
  1514. break;
  1515. case EHTokInt1x2:
  1516. new(&type) TType(EbtInt, EvqTemporary, 0, 1, 2);
  1517. break;
  1518. case EHTokInt1x3:
  1519. new(&type) TType(EbtInt, EvqTemporary, 0, 1, 3);
  1520. break;
  1521. case EHTokInt1x4:
  1522. new(&type) TType(EbtInt, EvqTemporary, 0, 1, 4);
  1523. break;
  1524. case EHTokInt2x1:
  1525. new(&type) TType(EbtInt, EvqTemporary, 0, 2, 1);
  1526. break;
  1527. case EHTokInt2x2:
  1528. new(&type) TType(EbtInt, EvqTemporary, 0, 2, 2);
  1529. break;
  1530. case EHTokInt2x3:
  1531. new(&type) TType(EbtInt, EvqTemporary, 0, 2, 3);
  1532. break;
  1533. case EHTokInt2x4:
  1534. new(&type) TType(EbtInt, EvqTemporary, 0, 2, 4);
  1535. break;
  1536. case EHTokInt3x1:
  1537. new(&type) TType(EbtInt, EvqTemporary, 0, 3, 1);
  1538. break;
  1539. case EHTokInt3x2:
  1540. new(&type) TType(EbtInt, EvqTemporary, 0, 3, 2);
  1541. break;
  1542. case EHTokInt3x3:
  1543. new(&type) TType(EbtInt, EvqTemporary, 0, 3, 3);
  1544. break;
  1545. case EHTokInt3x4:
  1546. new(&type) TType(EbtInt, EvqTemporary, 0, 3, 4);
  1547. break;
  1548. case EHTokInt4x1:
  1549. new(&type) TType(EbtInt, EvqTemporary, 0, 4, 1);
  1550. break;
  1551. case EHTokInt4x2:
  1552. new(&type) TType(EbtInt, EvqTemporary, 0, 4, 2);
  1553. break;
  1554. case EHTokInt4x3:
  1555. new(&type) TType(EbtInt, EvqTemporary, 0, 4, 3);
  1556. break;
  1557. case EHTokInt4x4:
  1558. new(&type) TType(EbtInt, EvqTemporary, 0, 4, 4);
  1559. break;
  1560. case EHTokUint1x1:
  1561. new(&type) TType(EbtUint, EvqTemporary, 0, 1, 1);
  1562. break;
  1563. case EHTokUint1x2:
  1564. new(&type) TType(EbtUint, EvqTemporary, 0, 1, 2);
  1565. break;
  1566. case EHTokUint1x3:
  1567. new(&type) TType(EbtUint, EvqTemporary, 0, 1, 3);
  1568. break;
  1569. case EHTokUint1x4:
  1570. new(&type) TType(EbtUint, EvqTemporary, 0, 1, 4);
  1571. break;
  1572. case EHTokUint2x1:
  1573. new(&type) TType(EbtUint, EvqTemporary, 0, 2, 1);
  1574. break;
  1575. case EHTokUint2x2:
  1576. new(&type) TType(EbtUint, EvqTemporary, 0, 2, 2);
  1577. break;
  1578. case EHTokUint2x3:
  1579. new(&type) TType(EbtUint, EvqTemporary, 0, 2, 3);
  1580. break;
  1581. case EHTokUint2x4:
  1582. new(&type) TType(EbtUint, EvqTemporary, 0, 2, 4);
  1583. break;
  1584. case EHTokUint3x1:
  1585. new(&type) TType(EbtUint, EvqTemporary, 0, 3, 1);
  1586. break;
  1587. case EHTokUint3x2:
  1588. new(&type) TType(EbtUint, EvqTemporary, 0, 3, 2);
  1589. break;
  1590. case EHTokUint3x3:
  1591. new(&type) TType(EbtUint, EvqTemporary, 0, 3, 3);
  1592. break;
  1593. case EHTokUint3x4:
  1594. new(&type) TType(EbtUint, EvqTemporary, 0, 3, 4);
  1595. break;
  1596. case EHTokUint4x1:
  1597. new(&type) TType(EbtUint, EvqTemporary, 0, 4, 1);
  1598. break;
  1599. case EHTokUint4x2:
  1600. new(&type) TType(EbtUint, EvqTemporary, 0, 4, 2);
  1601. break;
  1602. case EHTokUint4x3:
  1603. new(&type) TType(EbtUint, EvqTemporary, 0, 4, 3);
  1604. break;
  1605. case EHTokUint4x4:
  1606. new(&type) TType(EbtUint, EvqTemporary, 0, 4, 4);
  1607. break;
  1608. case EHTokBool1x1:
  1609. new(&type) TType(EbtBool, EvqTemporary, 0, 1, 1);
  1610. break;
  1611. case EHTokBool1x2:
  1612. new(&type) TType(EbtBool, EvqTemporary, 0, 1, 2);
  1613. break;
  1614. case EHTokBool1x3:
  1615. new(&type) TType(EbtBool, EvqTemporary, 0, 1, 3);
  1616. break;
  1617. case EHTokBool1x4:
  1618. new(&type) TType(EbtBool, EvqTemporary, 0, 1, 4);
  1619. break;
  1620. case EHTokBool2x1:
  1621. new(&type) TType(EbtBool, EvqTemporary, 0, 2, 1);
  1622. break;
  1623. case EHTokBool2x2:
  1624. new(&type) TType(EbtBool, EvqTemporary, 0, 2, 2);
  1625. break;
  1626. case EHTokBool2x3:
  1627. new(&type) TType(EbtBool, EvqTemporary, 0, 2, 3);
  1628. break;
  1629. case EHTokBool2x4:
  1630. new(&type) TType(EbtBool, EvqTemporary, 0, 2, 4);
  1631. break;
  1632. case EHTokBool3x1:
  1633. new(&type) TType(EbtBool, EvqTemporary, 0, 3, 1);
  1634. break;
  1635. case EHTokBool3x2:
  1636. new(&type) TType(EbtBool, EvqTemporary, 0, 3, 2);
  1637. break;
  1638. case EHTokBool3x3:
  1639. new(&type) TType(EbtBool, EvqTemporary, 0, 3, 3);
  1640. break;
  1641. case EHTokBool3x4:
  1642. new(&type) TType(EbtBool, EvqTemporary, 0, 3, 4);
  1643. break;
  1644. case EHTokBool4x1:
  1645. new(&type) TType(EbtBool, EvqTemporary, 0, 4, 1);
  1646. break;
  1647. case EHTokBool4x2:
  1648. new(&type) TType(EbtBool, EvqTemporary, 0, 4, 2);
  1649. break;
  1650. case EHTokBool4x3:
  1651. new(&type) TType(EbtBool, EvqTemporary, 0, 4, 3);
  1652. break;
  1653. case EHTokBool4x4:
  1654. new(&type) TType(EbtBool, EvqTemporary, 0, 4, 4);
  1655. break;
  1656. case EHTokFloat1x1:
  1657. new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 1);
  1658. break;
  1659. case EHTokFloat1x2:
  1660. new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 2);
  1661. break;
  1662. case EHTokFloat1x3:
  1663. new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 3);
  1664. break;
  1665. case EHTokFloat1x4:
  1666. new(&type) TType(EbtFloat, EvqTemporary, 0, 1, 4);
  1667. break;
  1668. case EHTokFloat2x1:
  1669. new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 1);
  1670. break;
  1671. case EHTokFloat2x2:
  1672. new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 2);
  1673. break;
  1674. case EHTokFloat2x3:
  1675. new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 3);
  1676. break;
  1677. case EHTokFloat2x4:
  1678. new(&type) TType(EbtFloat, EvqTemporary, 0, 2, 4);
  1679. break;
  1680. case EHTokFloat3x1:
  1681. new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 1);
  1682. break;
  1683. case EHTokFloat3x2:
  1684. new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 2);
  1685. break;
  1686. case EHTokFloat3x3:
  1687. new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 3);
  1688. break;
  1689. case EHTokFloat3x4:
  1690. new(&type) TType(EbtFloat, EvqTemporary, 0, 3, 4);
  1691. break;
  1692. case EHTokFloat4x1:
  1693. new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 1);
  1694. break;
  1695. case EHTokFloat4x2:
  1696. new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 2);
  1697. break;
  1698. case EHTokFloat4x3:
  1699. new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 3);
  1700. break;
  1701. case EHTokFloat4x4:
  1702. new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
  1703. break;
  1704. case EHTokHalf1x1:
  1705. new(&type) TType(half_bt, EvqTemporary, 0, 1, 1);
  1706. break;
  1707. case EHTokHalf1x2:
  1708. new(&type) TType(half_bt, EvqTemporary, 0, 1, 2);
  1709. break;
  1710. case EHTokHalf1x3:
  1711. new(&type) TType(half_bt, EvqTemporary, 0, 1, 3);
  1712. break;
  1713. case EHTokHalf1x4:
  1714. new(&type) TType(half_bt, EvqTemporary, 0, 1, 4);
  1715. break;
  1716. case EHTokHalf2x1:
  1717. new(&type) TType(half_bt, EvqTemporary, 0, 2, 1);
  1718. break;
  1719. case EHTokHalf2x2:
  1720. new(&type) TType(half_bt, EvqTemporary, 0, 2, 2);
  1721. break;
  1722. case EHTokHalf2x3:
  1723. new(&type) TType(half_bt, EvqTemporary, 0, 2, 3);
  1724. break;
  1725. case EHTokHalf2x4:
  1726. new(&type) TType(half_bt, EvqTemporary, 0, 2, 4);
  1727. break;
  1728. case EHTokHalf3x1:
  1729. new(&type) TType(half_bt, EvqTemporary, 0, 3, 1);
  1730. break;
  1731. case EHTokHalf3x2:
  1732. new(&type) TType(half_bt, EvqTemporary, 0, 3, 2);
  1733. break;
  1734. case EHTokHalf3x3:
  1735. new(&type) TType(half_bt, EvqTemporary, 0, 3, 3);
  1736. break;
  1737. case EHTokHalf3x4:
  1738. new(&type) TType(half_bt, EvqTemporary, 0, 3, 4);
  1739. break;
  1740. case EHTokHalf4x1:
  1741. new(&type) TType(half_bt, EvqTemporary, 0, 4, 1);
  1742. break;
  1743. case EHTokHalf4x2:
  1744. new(&type) TType(half_bt, EvqTemporary, 0, 4, 2);
  1745. break;
  1746. case EHTokHalf4x3:
  1747. new(&type) TType(half_bt, EvqTemporary, 0, 4, 3);
  1748. break;
  1749. case EHTokHalf4x4:
  1750. new(&type) TType(half_bt, EvqTemporary, 0, 4, 4);
  1751. break;
  1752. case EHTokDouble1x1:
  1753. new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 1);
  1754. break;
  1755. case EHTokDouble1x2:
  1756. new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 2);
  1757. break;
  1758. case EHTokDouble1x3:
  1759. new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 3);
  1760. break;
  1761. case EHTokDouble1x4:
  1762. new(&type) TType(EbtDouble, EvqTemporary, 0, 1, 4);
  1763. break;
  1764. case EHTokDouble2x1:
  1765. new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 1);
  1766. break;
  1767. case EHTokDouble2x2:
  1768. new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 2);
  1769. break;
  1770. case EHTokDouble2x3:
  1771. new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 3);
  1772. break;
  1773. case EHTokDouble2x4:
  1774. new(&type) TType(EbtDouble, EvqTemporary, 0, 2, 4);
  1775. break;
  1776. case EHTokDouble3x1:
  1777. new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 1);
  1778. break;
  1779. case EHTokDouble3x2:
  1780. new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 2);
  1781. break;
  1782. case EHTokDouble3x3:
  1783. new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 3);
  1784. break;
  1785. case EHTokDouble3x4:
  1786. new(&type) TType(EbtDouble, EvqTemporary, 0, 3, 4);
  1787. break;
  1788. case EHTokDouble4x1:
  1789. new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 1);
  1790. break;
  1791. case EHTokDouble4x2:
  1792. new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 2);
  1793. break;
  1794. case EHTokDouble4x3:
  1795. new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 3);
  1796. break;
  1797. case EHTokDouble4x4:
  1798. new(&type) TType(EbtDouble, EvqTemporary, 0, 4, 4);
  1799. break;
  1800. default:
  1801. return false;
  1802. }
  1803. advanceToken();
  1804. if ((isUnorm || isSnorm) && !type.isFloatingDomain()) {
  1805. parseContext.error(token.loc, "unorm and snorm only valid in floating point domain", "", "");
  1806. return false;
  1807. }
  1808. return true;
  1809. }
  1810. // struct
  1811. // : struct_type IDENTIFIER post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
  1812. // | struct_type post_decls LEFT_BRACE struct_declaration_list RIGHT_BRACE
  1813. // | struct_type IDENTIFIER // use of previously declared struct type
  1814. //
  1815. // struct_type
  1816. // : STRUCT
  1817. // | CLASS
  1818. // | CBUFFER
  1819. // | TBUFFER
  1820. //
  1821. bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
  1822. {
  1823. // This storage qualifier will tell us whether it's an AST
  1824. // block type or just a generic structure type.
  1825. TStorageQualifier storageQualifier = EvqTemporary;
  1826. bool readonly = false;
  1827. if (acceptTokenClass(EHTokCBuffer)) {
  1828. // CBUFFER
  1829. storageQualifier = EvqUniform;
  1830. } else if (acceptTokenClass(EHTokTBuffer)) {
  1831. // TBUFFER
  1832. storageQualifier = EvqBuffer;
  1833. readonly = true;
  1834. } else if (! acceptTokenClass(EHTokClass) && ! acceptTokenClass(EHTokStruct)) {
  1835. // Neither CLASS nor STRUCT
  1836. return false;
  1837. }
  1838. // Now known to be one of CBUFFER, TBUFFER, CLASS, or STRUCT
  1839. // IDENTIFIER. It might also be a keyword which can double as an identifier.
  1840. // For example: 'cbuffer ConstantBuffer' or 'struct ConstantBuffer' is legal.
  1841. // 'cbuffer int' is also legal, and 'struct int' appears rejected only because
  1842. // it attempts to redefine the 'int' type.
  1843. const char* idString = getTypeString(peek());
  1844. TString structName = "";
  1845. if (peekTokenClass(EHTokIdentifier) || idString != nullptr) {
  1846. if (idString != nullptr)
  1847. structName = *idString;
  1848. else
  1849. structName = *token.string;
  1850. advanceToken();
  1851. }
  1852. // post_decls
  1853. TQualifier postDeclQualifier;
  1854. postDeclQualifier.clear();
  1855. bool postDeclsFound = acceptPostDecls(postDeclQualifier);
  1856. // LEFT_BRACE, or
  1857. // struct_type IDENTIFIER
  1858. if (! acceptTokenClass(EHTokLeftBrace)) {
  1859. if (structName.size() > 0 && !postDeclsFound && parseContext.lookupUserType(structName, type) != nullptr) {
  1860. // struct_type IDENTIFIER
  1861. return true;
  1862. } else {
  1863. expected("{");
  1864. return false;
  1865. }
  1866. }
  1867. // struct_declaration_list
  1868. TTypeList* typeList;
  1869. // Save each member function so they can be processed after we have a fully formed 'this'.
  1870. TVector<TFunctionDeclarator> functionDeclarators;
  1871. parseContext.pushNamespace(structName);
  1872. bool acceptedList = acceptStructDeclarationList(typeList, nodeList, functionDeclarators);
  1873. parseContext.popNamespace();
  1874. if (! acceptedList) {
  1875. expected("struct member declarations");
  1876. return false;
  1877. }
  1878. // RIGHT_BRACE
  1879. if (! acceptTokenClass(EHTokRightBrace)) {
  1880. expected("}");
  1881. return false;
  1882. }
  1883. // create the user-defined type
  1884. if (storageQualifier == EvqTemporary)
  1885. new(&type) TType(typeList, structName);
  1886. else {
  1887. postDeclQualifier.storage = storageQualifier;
  1888. postDeclQualifier.readonly = readonly;
  1889. new(&type) TType(typeList, structName, postDeclQualifier); // sets EbtBlock
  1890. }
  1891. parseContext.declareStruct(token.loc, structName, type);
  1892. // For member functions: now that we know the type of 'this', go back and
  1893. // - add their implicit argument with 'this' (not to the mangling, just the argument list)
  1894. // - parse the functions, their tokens were saved for deferred parsing (now)
  1895. for (int b = 0; b < (int)functionDeclarators.size(); ++b) {
  1896. // update signature
  1897. if (functionDeclarators[b].function->hasImplicitThis())
  1898. functionDeclarators[b].function->addThisParameter(type, intermediate.implicitThisName);
  1899. }
  1900. // All member functions get parsed inside the class/struct namespace and with the
  1901. // class/struct members in a symbol-table level.
  1902. parseContext.pushNamespace(structName);
  1903. parseContext.pushThisScope(type, functionDeclarators);
  1904. bool deferredSuccess = true;
  1905. for (int b = 0; b < (int)functionDeclarators.size() && deferredSuccess; ++b) {
  1906. // parse body
  1907. pushTokenStream(functionDeclarators[b].body);
  1908. if (! acceptFunctionBody(functionDeclarators[b], nodeList))
  1909. deferredSuccess = false;
  1910. popTokenStream();
  1911. }
  1912. parseContext.popThisScope();
  1913. parseContext.popNamespace();
  1914. return deferredSuccess;
  1915. }
  1916. // constantbuffer
  1917. // : CONSTANTBUFFER LEFT_ANGLE type RIGHT_ANGLE
  1918. bool HlslGrammar::acceptConstantBufferType(TType& type)
  1919. {
  1920. if (! acceptTokenClass(EHTokConstantBuffer))
  1921. return false;
  1922. if (! acceptTokenClass(EHTokLeftAngle)) {
  1923. expected("left angle bracket");
  1924. return false;
  1925. }
  1926. TType templateType;
  1927. if (! acceptType(templateType)) {
  1928. expected("type");
  1929. return false;
  1930. }
  1931. if (! acceptTokenClass(EHTokRightAngle)) {
  1932. expected("right angle bracket");
  1933. return false;
  1934. }
  1935. TQualifier postDeclQualifier;
  1936. postDeclQualifier.clear();
  1937. postDeclQualifier.storage = EvqUniform;
  1938. if (templateType.isStruct()) {
  1939. // Make a block from the type parsed as the template argument
  1940. TTypeList* typeList = templateType.getWritableStruct();
  1941. new(&type) TType(typeList, "", postDeclQualifier); // sets EbtBlock
  1942. type.getQualifier().storage = EvqUniform;
  1943. return true;
  1944. } else {
  1945. parseContext.error(token.loc, "non-structure type in ConstantBuffer", "", "");
  1946. return false;
  1947. }
  1948. }
  1949. // texture_buffer
  1950. // : TEXTUREBUFFER LEFT_ANGLE type RIGHT_ANGLE
  1951. bool HlslGrammar::acceptTextureBufferType(TType& type)
  1952. {
  1953. if (! acceptTokenClass(EHTokTextureBuffer))
  1954. return false;
  1955. if (! acceptTokenClass(EHTokLeftAngle)) {
  1956. expected("left angle bracket");
  1957. return false;
  1958. }
  1959. TType templateType;
  1960. if (! acceptType(templateType)) {
  1961. expected("type");
  1962. return false;
  1963. }
  1964. if (! acceptTokenClass(EHTokRightAngle)) {
  1965. expected("right angle bracket");
  1966. return false;
  1967. }
  1968. templateType.getQualifier().storage = EvqBuffer;
  1969. templateType.getQualifier().readonly = true;
  1970. TType blockType(templateType.getWritableStruct(), "", templateType.getQualifier());
  1971. blockType.getQualifier().storage = EvqBuffer;
  1972. blockType.getQualifier().readonly = true;
  1973. type.shallowCopy(blockType);
  1974. return true;
  1975. }
  1976. // struct_buffer
  1977. // : APPENDSTRUCTUREDBUFFER
  1978. // | BYTEADDRESSBUFFER
  1979. // | CONSUMESTRUCTUREDBUFFER
  1980. // | RWBYTEADDRESSBUFFER
  1981. // | RWSTRUCTUREDBUFFER
  1982. // | STRUCTUREDBUFFER
  1983. bool HlslGrammar::acceptStructBufferType(TType& type)
  1984. {
  1985. const EHlslTokenClass structBuffType = peek();
  1986. // TODO: globallycoherent
  1987. bool hasTemplateType = true;
  1988. bool readonly = false;
  1989. TStorageQualifier storage = EvqBuffer;
  1990. TBuiltInVariable builtinType = EbvNone;
  1991. switch (structBuffType) {
  1992. case EHTokAppendStructuredBuffer:
  1993. builtinType = EbvAppendConsume;
  1994. break;
  1995. case EHTokByteAddressBuffer:
  1996. hasTemplateType = false;
  1997. readonly = true;
  1998. builtinType = EbvByteAddressBuffer;
  1999. break;
  2000. case EHTokConsumeStructuredBuffer:
  2001. builtinType = EbvAppendConsume;
  2002. break;
  2003. case EHTokRWByteAddressBuffer:
  2004. hasTemplateType = false;
  2005. builtinType = EbvRWByteAddressBuffer;
  2006. break;
  2007. case EHTokRWStructuredBuffer:
  2008. builtinType = EbvRWStructuredBuffer;
  2009. break;
  2010. case EHTokStructuredBuffer:
  2011. builtinType = EbvStructuredBuffer;
  2012. readonly = true;
  2013. break;
  2014. default:
  2015. return false; // not a structure buffer type
  2016. }
  2017. advanceToken(); // consume the structure keyword
  2018. // type on which this StructedBuffer is templatized. E.g, StructedBuffer<MyStruct> ==> MyStruct
  2019. TType* templateType = new TType;
  2020. if (hasTemplateType) {
  2021. if (! acceptTokenClass(EHTokLeftAngle)) {
  2022. expected("left angle bracket");
  2023. return false;
  2024. }
  2025. if (! acceptType(*templateType)) {
  2026. expected("type");
  2027. return false;
  2028. }
  2029. if (! acceptTokenClass(EHTokRightAngle)) {
  2030. expected("right angle bracket");
  2031. return false;
  2032. }
  2033. } else {
  2034. // byte address buffers have no explicit type.
  2035. TType uintType(EbtUint, storage);
  2036. templateType->shallowCopy(uintType);
  2037. }
  2038. // Create an unsized array out of that type.
  2039. // TODO: does this work if it's already an array type?
  2040. TArraySizes* unsizedArray = new TArraySizes;
  2041. unsizedArray->addInnerSize(UnsizedArraySize);
  2042. templateType->transferArraySizes(unsizedArray);
  2043. templateType->getQualifier().storage = storage;
  2044. // field name is canonical for all structbuffers
  2045. templateType->setFieldName("@data");
  2046. TTypeList* blockStruct = new TTypeList;
  2047. TTypeLoc member = { templateType, token.loc };
  2048. blockStruct->push_back(member);
  2049. // This is the type of the buffer block (SSBO)
  2050. TType blockType(blockStruct, "", templateType->getQualifier());
  2051. blockType.getQualifier().storage = storage;
  2052. blockType.getQualifier().readonly = readonly;
  2053. blockType.getQualifier().builtIn = builtinType;
  2054. // We may have created an equivalent type before, in which case we should use its
  2055. // deep structure.
  2056. parseContext.shareStructBufferType(blockType);
  2057. type.shallowCopy(blockType);
  2058. return true;
  2059. }
  2060. // struct_declaration_list
  2061. // : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
  2062. //
  2063. // struct_declaration
  2064. // : attributes fully_specified_type struct_declarator COMMA struct_declarator ...
  2065. // | attributes fully_specified_type IDENTIFIER function_parameters post_decls compound_statement // member-function definition
  2066. //
  2067. // struct_declarator
  2068. // : IDENTIFIER post_decls
  2069. // | IDENTIFIER array_specifier post_decls
  2070. // | IDENTIFIER function_parameters post_decls // member-function prototype
  2071. //
  2072. bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList, TIntermNode*& nodeList,
  2073. TVector<TFunctionDeclarator>& declarators)
  2074. {
  2075. typeList = new TTypeList();
  2076. HlslToken idToken;
  2077. do {
  2078. // success on seeing the RIGHT_BRACE coming up
  2079. if (peekTokenClass(EHTokRightBrace))
  2080. break;
  2081. // struct_declaration
  2082. // attributes
  2083. TAttributes attributes;
  2084. acceptAttributes(attributes);
  2085. bool declarator_list = false;
  2086. // fully_specified_type
  2087. TType memberType;
  2088. if (! acceptFullySpecifiedType(memberType, nodeList, attributes)) {
  2089. expected("member type");
  2090. return false;
  2091. }
  2092. // merge in the attributes
  2093. parseContext.transferTypeAttributes(token.loc, attributes, memberType);
  2094. // struct_declarator COMMA struct_declarator ...
  2095. bool functionDefinitionAccepted = false;
  2096. do {
  2097. if (! acceptIdentifier(idToken)) {
  2098. expected("member name");
  2099. return false;
  2100. }
  2101. if (peekTokenClass(EHTokLeftParen)) {
  2102. // function_parameters
  2103. if (!declarator_list) {
  2104. declarators.resize(declarators.size() + 1);
  2105. // request a token stream for deferred processing
  2106. functionDefinitionAccepted = acceptMemberFunctionDefinition(nodeList, memberType, *idToken.string,
  2107. declarators.back());
  2108. if (functionDefinitionAccepted)
  2109. break;
  2110. }
  2111. expected("member-function definition");
  2112. return false;
  2113. } else {
  2114. // add it to the list of members
  2115. TTypeLoc member = { new TType(EbtVoid), token.loc };
  2116. member.type->shallowCopy(memberType);
  2117. member.type->setFieldName(*idToken.string);
  2118. typeList->push_back(member);
  2119. // array_specifier
  2120. TArraySizes* arraySizes = nullptr;
  2121. acceptArraySpecifier(arraySizes);
  2122. if (arraySizes)
  2123. typeList->back().type->transferArraySizes(arraySizes);
  2124. acceptPostDecls(member.type->getQualifier());
  2125. // EQUAL assignment_expression
  2126. if (acceptTokenClass(EHTokAssign)) {
  2127. parseContext.warn(idToken.loc, "struct-member initializers ignored", "typedef", "");
  2128. TIntermTyped* expressionNode = nullptr;
  2129. if (! acceptAssignmentExpression(expressionNode)) {
  2130. expected("initializer");
  2131. return false;
  2132. }
  2133. }
  2134. }
  2135. // success on seeing the SEMICOLON coming up
  2136. if (peekTokenClass(EHTokSemicolon))
  2137. break;
  2138. // COMMA
  2139. if (acceptTokenClass(EHTokComma))
  2140. declarator_list = true;
  2141. else {
  2142. expected(",");
  2143. return false;
  2144. }
  2145. } while (true);
  2146. // SEMI_COLON
  2147. if (! functionDefinitionAccepted && ! acceptTokenClass(EHTokSemicolon)) {
  2148. expected(";");
  2149. return false;
  2150. }
  2151. } while (true);
  2152. return true;
  2153. }
  2154. // member_function_definition
  2155. // | function_parameters post_decls compound_statement
  2156. //
  2157. // Expects type to have EvqGlobal for a static member and
  2158. // EvqTemporary for non-static member.
  2159. bool HlslGrammar::acceptMemberFunctionDefinition(TIntermNode*& nodeList, const TType& type, TString& memberName,
  2160. TFunctionDeclarator& declarator)
  2161. {
  2162. bool accepted = false;
  2163. TString* functionName = &memberName;
  2164. parseContext.getFullNamespaceName(functionName);
  2165. declarator.function = new TFunction(functionName, type);
  2166. if (type.getQualifier().storage == EvqTemporary)
  2167. declarator.function->setImplicitThis();
  2168. else
  2169. declarator.function->setIllegalImplicitThis();
  2170. // function_parameters
  2171. if (acceptFunctionParameters(*declarator.function)) {
  2172. // post_decls
  2173. acceptPostDecls(declarator.function->getWritableType().getQualifier());
  2174. // compound_statement (function body definition)
  2175. if (peekTokenClass(EHTokLeftBrace)) {
  2176. declarator.loc = token.loc;
  2177. declarator.body = new TVector<HlslToken>;
  2178. accepted = acceptFunctionDefinition(declarator, nodeList, declarator.body);
  2179. }
  2180. } else
  2181. expected("function parameter list");
  2182. return accepted;
  2183. }
  2184. // function_parameters
  2185. // : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
  2186. // | LEFT_PAREN VOID RIGHT_PAREN
  2187. //
  2188. bool HlslGrammar::acceptFunctionParameters(TFunction& function)
  2189. {
  2190. // LEFT_PAREN
  2191. if (! acceptTokenClass(EHTokLeftParen))
  2192. return false;
  2193. // VOID RIGHT_PAREN
  2194. if (! acceptTokenClass(EHTokVoid)) {
  2195. do {
  2196. // parameter_declaration
  2197. if (! acceptParameterDeclaration(function))
  2198. break;
  2199. // COMMA
  2200. if (! acceptTokenClass(EHTokComma))
  2201. break;
  2202. } while (true);
  2203. }
  2204. // RIGHT_PAREN
  2205. if (! acceptTokenClass(EHTokRightParen)) {
  2206. expected(")");
  2207. return false;
  2208. }
  2209. return true;
  2210. }
  2211. // default_parameter_declaration
  2212. // : EQUAL conditional_expression
  2213. // : EQUAL initializer
  2214. bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTyped*& node)
  2215. {
  2216. node = nullptr;
  2217. // Valid not to have a default_parameter_declaration
  2218. if (!acceptTokenClass(EHTokAssign))
  2219. return true;
  2220. if (!acceptConditionalExpression(node)) {
  2221. if (!acceptInitializer(node))
  2222. return false;
  2223. // For initializer lists, we have to const-fold into a constructor for the type, so build
  2224. // that.
  2225. TFunction* constructor = parseContext.makeConstructorCall(token.loc, type);
  2226. if (constructor == nullptr) // cannot construct
  2227. return false;
  2228. TIntermTyped* arguments = nullptr;
  2229. for (int i = 0; i < int(node->getAsAggregate()->getSequence().size()); i++)
  2230. parseContext.handleFunctionArgument(constructor, arguments, node->getAsAggregate()->getSequence()[i]->getAsTyped());
  2231. node = parseContext.handleFunctionCall(token.loc, constructor, node);
  2232. }
  2233. if (node == nullptr)
  2234. return false;
  2235. // If this is simply a constant, we can use it directly.
  2236. if (node->getAsConstantUnion())
  2237. return true;
  2238. // Otherwise, it has to be const-foldable.
  2239. TIntermTyped* origNode = node;
  2240. node = intermediate.fold(node->getAsAggregate());
  2241. if (node != nullptr && origNode != node)
  2242. return true;
  2243. parseContext.error(token.loc, "invalid default parameter value", "", "");
  2244. return false;
  2245. }
  2246. // parameter_declaration
  2247. // : attributes attributed_declaration
  2248. //
  2249. // attributed_declaration
  2250. // : fully_specified_type post_decls [ = default_parameter_declaration ]
  2251. // | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
  2252. //
  2253. bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
  2254. {
  2255. // attributes
  2256. TAttributes attributes;
  2257. acceptAttributes(attributes);
  2258. // fully_specified_type
  2259. TType* type = new TType;
  2260. if (! acceptFullySpecifiedType(*type, attributes))
  2261. return false;
  2262. // merge in the attributes
  2263. parseContext.transferTypeAttributes(token.loc, attributes, *type);
  2264. // identifier
  2265. HlslToken idToken;
  2266. acceptIdentifier(idToken);
  2267. // array_specifier
  2268. TArraySizes* arraySizes = nullptr;
  2269. acceptArraySpecifier(arraySizes);
  2270. if (arraySizes) {
  2271. if (arraySizes->hasUnsized()) {
  2272. parseContext.error(token.loc, "function parameter requires array size", "[]", "");
  2273. return false;
  2274. }
  2275. type->transferArraySizes(arraySizes);
  2276. }
  2277. // post_decls
  2278. acceptPostDecls(type->getQualifier());
  2279. TIntermTyped* defaultValue;
  2280. if (!acceptDefaultParameterDeclaration(*type, defaultValue))
  2281. return false;
  2282. parseContext.paramFix(*type);
  2283. // If any prior parameters have default values, all the parameters after that must as well.
  2284. if (defaultValue == nullptr && function.getDefaultParamCount() > 0) {
  2285. parseContext.error(idToken.loc, "invalid parameter after default value parameters", idToken.string->c_str(), "");
  2286. return false;
  2287. }
  2288. TParameter param = { idToken.string, type, defaultValue };
  2289. function.addParameter(param);
  2290. return true;
  2291. }
  2292. // Do the work to create the function definition in addition to
  2293. // parsing the body (compound_statement).
  2294. //
  2295. // If 'deferredTokens' are passed in, just get the token stream,
  2296. // don't process.
  2297. //
  2298. bool HlslGrammar::acceptFunctionDefinition(TFunctionDeclarator& declarator, TIntermNode*& nodeList,
  2299. TVector<HlslToken>* deferredTokens)
  2300. {
  2301. parseContext.handleFunctionDeclarator(declarator.loc, *declarator.function, false /* not prototype */);
  2302. if (deferredTokens)
  2303. return captureBlockTokens(*deferredTokens);
  2304. else
  2305. return acceptFunctionBody(declarator, nodeList);
  2306. }
  2307. bool HlslGrammar::acceptFunctionBody(TFunctionDeclarator& declarator, TIntermNode*& nodeList)
  2308. {
  2309. // we might get back an entry-point
  2310. TIntermNode* entryPointNode = nullptr;
  2311. // This does a pushScope()
  2312. TIntermNode* functionNode = parseContext.handleFunctionDefinition(declarator.loc, *declarator.function,
  2313. declarator.attributes, entryPointNode);
  2314. // compound_statement
  2315. TIntermNode* functionBody = nullptr;
  2316. if (! acceptCompoundStatement(functionBody))
  2317. return false;
  2318. // this does a popScope()
  2319. parseContext.handleFunctionBody(declarator.loc, *declarator.function, functionBody, functionNode);
  2320. // Hook up the 1 or 2 function definitions.
  2321. nodeList = intermediate.growAggregate(nodeList, functionNode);
  2322. nodeList = intermediate.growAggregate(nodeList, entryPointNode);
  2323. return true;
  2324. }
  2325. // Accept an expression with parenthesis around it, where
  2326. // the parenthesis ARE NOT expression parenthesis, but the
  2327. // syntactically required ones like in "if ( expression )".
  2328. //
  2329. // Also accepts a declaration expression; "if (int a = expression)".
  2330. //
  2331. // Note this one is not set up to be speculative; as it gives
  2332. // errors if not found.
  2333. //
  2334. bool HlslGrammar::acceptParenExpression(TIntermTyped*& expression)
  2335. {
  2336. expression = nullptr;
  2337. // LEFT_PAREN
  2338. if (! acceptTokenClass(EHTokLeftParen))
  2339. expected("(");
  2340. bool decl = false;
  2341. TIntermNode* declNode = nullptr;
  2342. decl = acceptControlDeclaration(declNode);
  2343. if (decl) {
  2344. if (declNode == nullptr || declNode->getAsTyped() == nullptr) {
  2345. expected("initialized declaration");
  2346. return false;
  2347. } else
  2348. expression = declNode->getAsTyped();
  2349. } else {
  2350. // no declaration
  2351. if (! acceptExpression(expression)) {
  2352. expected("expression");
  2353. return false;
  2354. }
  2355. }
  2356. // RIGHT_PAREN
  2357. if (! acceptTokenClass(EHTokRightParen))
  2358. expected(")");
  2359. return true;
  2360. }
  2361. // The top-level full expression recognizer.
  2362. //
  2363. // expression
  2364. // : assignment_expression COMMA assignment_expression COMMA assignment_expression ...
  2365. //
  2366. bool HlslGrammar::acceptExpression(TIntermTyped*& node)
  2367. {
  2368. node = nullptr;
  2369. // assignment_expression
  2370. if (! acceptAssignmentExpression(node))
  2371. return false;
  2372. if (! peekTokenClass(EHTokComma))
  2373. return true;
  2374. do {
  2375. // ... COMMA
  2376. TSourceLoc loc = token.loc;
  2377. advanceToken();
  2378. // ... assignment_expression
  2379. TIntermTyped* rightNode = nullptr;
  2380. if (! acceptAssignmentExpression(rightNode)) {
  2381. expected("assignment expression");
  2382. return false;
  2383. }
  2384. node = intermediate.addComma(node, rightNode, loc);
  2385. if (! peekTokenClass(EHTokComma))
  2386. return true;
  2387. } while (true);
  2388. }
  2389. // initializer
  2390. // : LEFT_BRACE RIGHT_BRACE
  2391. // | LEFT_BRACE initializer_list RIGHT_BRACE
  2392. //
  2393. // initializer_list
  2394. // : assignment_expression COMMA assignment_expression COMMA ...
  2395. //
  2396. bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
  2397. {
  2398. // LEFT_BRACE
  2399. if (! acceptTokenClass(EHTokLeftBrace))
  2400. return false;
  2401. // RIGHT_BRACE
  2402. TSourceLoc loc = token.loc;
  2403. if (acceptTokenClass(EHTokRightBrace)) {
  2404. // a zero-length initializer list
  2405. node = intermediate.makeAggregate(loc);
  2406. return true;
  2407. }
  2408. // initializer_list
  2409. node = nullptr;
  2410. do {
  2411. // assignment_expression
  2412. TIntermTyped* expr;
  2413. if (! acceptAssignmentExpression(expr)) {
  2414. expected("assignment expression in initializer list");
  2415. return false;
  2416. }
  2417. const bool firstNode = (node == nullptr);
  2418. node = intermediate.growAggregate(node, expr, loc);
  2419. // If every sub-node in the list has qualifier EvqConst, the returned node becomes
  2420. // EvqConst. Otherwise, it becomes EvqTemporary. That doesn't happen with e.g.
  2421. // EvqIn or EvqPosition, since the collection isn't EvqPosition if all the members are.
  2422. if (firstNode && expr->getQualifier().storage == EvqConst)
  2423. node->getQualifier().storage = EvqConst;
  2424. else if (expr->getQualifier().storage != EvqConst)
  2425. node->getQualifier().storage = EvqTemporary;
  2426. // COMMA
  2427. if (acceptTokenClass(EHTokComma)) {
  2428. if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
  2429. return true;
  2430. continue;
  2431. }
  2432. // RIGHT_BRACE
  2433. if (acceptTokenClass(EHTokRightBrace))
  2434. return true;
  2435. expected(", or }");
  2436. return false;
  2437. } while (true);
  2438. }
  2439. // Accept an assignment expression, where assignment operations
  2440. // associate right-to-left. That is, it is implicit, for example
  2441. //
  2442. // a op (b op (c op d))
  2443. //
  2444. // assigment_expression
  2445. // : initializer
  2446. // | conditional_expression
  2447. // | conditional_expression assign_op conditional_expression assign_op conditional_expression ...
  2448. //
  2449. bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
  2450. {
  2451. // initializer
  2452. if (peekTokenClass(EHTokLeftBrace)) {
  2453. if (acceptInitializer(node))
  2454. return true;
  2455. expected("initializer");
  2456. return false;
  2457. }
  2458. // conditional_expression
  2459. if (! acceptConditionalExpression(node))
  2460. return false;
  2461. // assignment operation?
  2462. TOperator assignOp = HlslOpMap::assignment(peek());
  2463. if (assignOp == EOpNull)
  2464. return true;
  2465. // assign_op
  2466. TSourceLoc loc = token.loc;
  2467. advanceToken();
  2468. // conditional_expression assign_op conditional_expression ...
  2469. // Done by recursing this function, which automatically
  2470. // gets the right-to-left associativity.
  2471. TIntermTyped* rightNode = nullptr;
  2472. if (! acceptAssignmentExpression(rightNode)) {
  2473. expected("assignment expression");
  2474. return false;
  2475. }
  2476. node = parseContext.handleAssign(loc, assignOp, node, rightNode);
  2477. node = parseContext.handleLvalue(loc, "assign", node);
  2478. if (node == nullptr) {
  2479. parseContext.error(loc, "could not create assignment", "", "");
  2480. return false;
  2481. }
  2482. if (! peekTokenClass(EHTokComma))
  2483. return true;
  2484. return true;
  2485. }
  2486. // Accept a conditional expression, which associates right-to-left,
  2487. // accomplished by the "true" expression calling down to lower
  2488. // precedence levels than this level.
  2489. //
  2490. // conditional_expression
  2491. // : binary_expression
  2492. // | binary_expression QUESTION expression COLON assignment_expression
  2493. //
  2494. bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
  2495. {
  2496. // binary_expression
  2497. if (! acceptBinaryExpression(node, PlLogicalOr))
  2498. return false;
  2499. if (! acceptTokenClass(EHTokQuestion))
  2500. return true;
  2501. node = parseContext.convertConditionalExpression(token.loc, node, false);
  2502. if (node == nullptr)
  2503. return false;
  2504. ++parseContext.controlFlowNestingLevel; // this only needs to work right if no errors
  2505. TIntermTyped* trueNode = nullptr;
  2506. if (! acceptExpression(trueNode)) {
  2507. expected("expression after ?");
  2508. return false;
  2509. }
  2510. TSourceLoc loc = token.loc;
  2511. if (! acceptTokenClass(EHTokColon)) {
  2512. expected(":");
  2513. return false;
  2514. }
  2515. TIntermTyped* falseNode = nullptr;
  2516. if (! acceptAssignmentExpression(falseNode)) {
  2517. expected("expression after :");
  2518. return false;
  2519. }
  2520. --parseContext.controlFlowNestingLevel;
  2521. node = intermediate.addSelection(node, trueNode, falseNode, loc);
  2522. return true;
  2523. }
  2524. // Accept a binary expression, for binary operations that
  2525. // associate left-to-right. This is, it is implicit, for example
  2526. //
  2527. // ((a op b) op c) op d
  2528. //
  2529. // binary_expression
  2530. // : expression op expression op expression ...
  2531. //
  2532. // where 'expression' is the next higher level in precedence.
  2533. //
  2534. bool HlslGrammar::acceptBinaryExpression(TIntermTyped*& node, PrecedenceLevel precedenceLevel)
  2535. {
  2536. if (precedenceLevel > PlMul)
  2537. return acceptUnaryExpression(node);
  2538. // assignment_expression
  2539. if (! acceptBinaryExpression(node, (PrecedenceLevel)(precedenceLevel + 1)))
  2540. return false;
  2541. do {
  2542. TOperator op = HlslOpMap::binary(peek());
  2543. PrecedenceLevel tokenLevel = HlslOpMap::precedenceLevel(op);
  2544. if (tokenLevel < precedenceLevel)
  2545. return true;
  2546. // ... op
  2547. TSourceLoc loc = token.loc;
  2548. advanceToken();
  2549. // ... expression
  2550. TIntermTyped* rightNode = nullptr;
  2551. if (! acceptBinaryExpression(rightNode, (PrecedenceLevel)(precedenceLevel + 1))) {
  2552. expected("expression");
  2553. return false;
  2554. }
  2555. node = intermediate.addBinaryMath(op, node, rightNode, loc);
  2556. if (node == nullptr) {
  2557. parseContext.error(loc, "Could not perform requested binary operation", "", "");
  2558. return false;
  2559. }
  2560. } while (true);
  2561. }
  2562. // unary_expression
  2563. // : (type) unary_expression
  2564. // | + unary_expression
  2565. // | - unary_expression
  2566. // | ! unary_expression
  2567. // | ~ unary_expression
  2568. // | ++ unary_expression
  2569. // | -- unary_expression
  2570. // | postfix_expression
  2571. //
  2572. bool HlslGrammar::acceptUnaryExpression(TIntermTyped*& node)
  2573. {
  2574. // (type) unary_expression
  2575. // Have to look two steps ahead, because this could be, e.g., a
  2576. // postfix_expression instead, since that also starts with at "(".
  2577. if (acceptTokenClass(EHTokLeftParen)) {
  2578. TType castType;
  2579. if (acceptType(castType)) {
  2580. // recognize any array_specifier as part of the type
  2581. TArraySizes* arraySizes = nullptr;
  2582. acceptArraySpecifier(arraySizes);
  2583. if (arraySizes != nullptr)
  2584. castType.transferArraySizes(arraySizes);
  2585. TSourceLoc loc = token.loc;
  2586. if (acceptTokenClass(EHTokRightParen)) {
  2587. // We've matched "(type)" now, get the expression to cast
  2588. if (! acceptUnaryExpression(node))
  2589. return false;
  2590. // Hook it up like a constructor
  2591. TFunction* constructorFunction = parseContext.makeConstructorCall(loc, castType);
  2592. if (constructorFunction == nullptr) {
  2593. expected("type that can be constructed");
  2594. return false;
  2595. }
  2596. TIntermTyped* arguments = nullptr;
  2597. parseContext.handleFunctionArgument(constructorFunction, arguments, node);
  2598. node = parseContext.handleFunctionCall(loc, constructorFunction, arguments);
  2599. return node != nullptr;
  2600. } else {
  2601. // This could be a parenthesized constructor, ala (int(3)), and we just accepted
  2602. // the '(int' part. We must back up twice.
  2603. recedeToken();
  2604. recedeToken();
  2605. // Note, there are no array constructors like
  2606. // (float[2](...))
  2607. if (arraySizes != nullptr)
  2608. parseContext.error(loc, "parenthesized array constructor not allowed", "([]())", "", "");
  2609. }
  2610. } else {
  2611. // This isn't a type cast, but it still started "(", so if it is a
  2612. // unary expression, it can only be a postfix_expression, so try that.
  2613. // Back it up first.
  2614. recedeToken();
  2615. return acceptPostfixExpression(node);
  2616. }
  2617. }
  2618. // peek for "op unary_expression"
  2619. TOperator unaryOp = HlslOpMap::preUnary(peek());
  2620. // postfix_expression (if no unary operator)
  2621. if (unaryOp == EOpNull)
  2622. return acceptPostfixExpression(node);
  2623. // op unary_expression
  2624. TSourceLoc loc = token.loc;
  2625. advanceToken();
  2626. if (! acceptUnaryExpression(node))
  2627. return false;
  2628. // + is a no-op
  2629. if (unaryOp == EOpAdd)
  2630. return true;
  2631. node = intermediate.addUnaryMath(unaryOp, node, loc);
  2632. // These unary ops require lvalues
  2633. if (unaryOp == EOpPreIncrement || unaryOp == EOpPreDecrement)
  2634. node = parseContext.handleLvalue(loc, "unary operator", node);
  2635. return node != nullptr;
  2636. }
  2637. // postfix_expression
  2638. // : LEFT_PAREN expression RIGHT_PAREN
  2639. // | literal
  2640. // | constructor
  2641. // | IDENTIFIER [ COLONCOLON IDENTIFIER [ COLONCOLON IDENTIFIER ... ] ]
  2642. // | function_call
  2643. // | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET
  2644. // | postfix_expression DOT IDENTIFIER
  2645. // | postfix_expression DOT IDENTIFIER arguments
  2646. // | postfix_expression arguments
  2647. // | postfix_expression INC_OP
  2648. // | postfix_expression DEC_OP
  2649. //
  2650. bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
  2651. {
  2652. // Not implemented as self-recursive:
  2653. // The logical "right recursion" is done with a loop at the end
  2654. // idToken will pick up either a variable or a function name in a function call
  2655. HlslToken idToken;
  2656. // Find something before the postfix operations, as they can't operate
  2657. // on nothing. So, no "return true", they fall through, only "return false".
  2658. if (acceptTokenClass(EHTokLeftParen)) {
  2659. // LEFT_PAREN expression RIGHT_PAREN
  2660. if (! acceptExpression(node)) {
  2661. expected("expression");
  2662. return false;
  2663. }
  2664. if (! acceptTokenClass(EHTokRightParen)) {
  2665. expected(")");
  2666. return false;
  2667. }
  2668. } else if (acceptLiteral(node)) {
  2669. // literal (nothing else to do yet)
  2670. } else if (acceptConstructor(node)) {
  2671. // constructor (nothing else to do yet)
  2672. } else if (acceptIdentifier(idToken)) {
  2673. // user-type, namespace name, variable, or function name
  2674. TString* fullName = idToken.string;
  2675. while (acceptTokenClass(EHTokColonColon)) {
  2676. // user-type or namespace name
  2677. fullName = NewPoolTString(fullName->c_str());
  2678. fullName->append(parseContext.scopeMangler);
  2679. if (acceptIdentifier(idToken))
  2680. fullName->append(*idToken.string);
  2681. else {
  2682. expected("identifier after ::");
  2683. return false;
  2684. }
  2685. }
  2686. if (! peekTokenClass(EHTokLeftParen)) {
  2687. node = parseContext.handleVariable(idToken.loc, fullName);
  2688. if (node == nullptr)
  2689. return false;
  2690. } else if (acceptFunctionCall(idToken.loc, *fullName, node, nullptr)) {
  2691. // function_call (nothing else to do yet)
  2692. } else {
  2693. expected("function call arguments");
  2694. return false;
  2695. }
  2696. } else {
  2697. // nothing found, can't post operate
  2698. return false;
  2699. }
  2700. // Something was found, chain as many postfix operations as exist.
  2701. do {
  2702. TSourceLoc loc = token.loc;
  2703. TOperator postOp = HlslOpMap::postUnary(peek());
  2704. // Consume only a valid post-unary operator, otherwise we are done.
  2705. switch (postOp) {
  2706. case EOpIndexDirectStruct:
  2707. case EOpIndexIndirect:
  2708. case EOpPostIncrement:
  2709. case EOpPostDecrement:
  2710. case EOpScoping:
  2711. advanceToken();
  2712. break;
  2713. default:
  2714. return true;
  2715. }
  2716. // We have a valid post-unary operator, process it.
  2717. switch (postOp) {
  2718. case EOpScoping:
  2719. case EOpIndexDirectStruct:
  2720. {
  2721. // DOT IDENTIFIER
  2722. // includes swizzles, member variables, and member functions
  2723. HlslToken field;
  2724. if (! acceptIdentifier(field)) {
  2725. expected("swizzle or member");
  2726. return false;
  2727. }
  2728. if (peekTokenClass(EHTokLeftParen)) {
  2729. // member function
  2730. TIntermTyped* thisNode = node;
  2731. // arguments
  2732. if (! acceptFunctionCall(field.loc, *field.string, node, thisNode)) {
  2733. expected("function parameters");
  2734. return false;
  2735. }
  2736. } else
  2737. node = parseContext.handleDotDereference(field.loc, node, *field.string);
  2738. break;
  2739. }
  2740. case EOpIndexIndirect:
  2741. {
  2742. // LEFT_BRACKET integer_expression RIGHT_BRACKET
  2743. TIntermTyped* indexNode = nullptr;
  2744. if (! acceptExpression(indexNode) ||
  2745. ! peekTokenClass(EHTokRightBracket)) {
  2746. expected("expression followed by ']'");
  2747. return false;
  2748. }
  2749. advanceToken();
  2750. node = parseContext.handleBracketDereference(indexNode->getLoc(), node, indexNode);
  2751. if (node == nullptr)
  2752. return false;
  2753. break;
  2754. }
  2755. case EOpPostIncrement:
  2756. // INC_OP
  2757. // fall through
  2758. case EOpPostDecrement:
  2759. // DEC_OP
  2760. node = intermediate.addUnaryMath(postOp, node, loc);
  2761. node = parseContext.handleLvalue(loc, "unary operator", node);
  2762. break;
  2763. default:
  2764. assert(0);
  2765. break;
  2766. }
  2767. } while (true);
  2768. }
  2769. // constructor
  2770. // : type argument_list
  2771. //
  2772. bool HlslGrammar::acceptConstructor(TIntermTyped*& node)
  2773. {
  2774. // type
  2775. TType type;
  2776. if (acceptType(type)) {
  2777. TFunction* constructorFunction = parseContext.makeConstructorCall(token.loc, type);
  2778. if (constructorFunction == nullptr)
  2779. return false;
  2780. // arguments
  2781. TIntermTyped* arguments = nullptr;
  2782. if (! acceptArguments(constructorFunction, arguments)) {
  2783. // It's possible this is a type keyword used as an identifier. Put the token back
  2784. // for later use.
  2785. recedeToken();
  2786. return false;
  2787. }
  2788. // hook it up
  2789. node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
  2790. return node != nullptr;
  2791. }
  2792. return false;
  2793. }
  2794. // The function_call identifier was already recognized, and passed in as idToken.
  2795. //
  2796. // function_call
  2797. // : [idToken] arguments
  2798. //
  2799. bool HlslGrammar::acceptFunctionCall(const TSourceLoc& loc, TString& name, TIntermTyped*& node, TIntermTyped* baseObject)
  2800. {
  2801. // name
  2802. TString* functionName = nullptr;
  2803. if (baseObject == nullptr) {
  2804. functionName = &name;
  2805. } else if (parseContext.isBuiltInMethod(loc, baseObject, name)) {
  2806. // Built-in methods are not in the symbol table as methods, but as global functions
  2807. // taking an explicit 'this' as the first argument.
  2808. functionName = NewPoolTString(BUILTIN_PREFIX);
  2809. functionName->append(name);
  2810. } else {
  2811. if (! baseObject->getType().isStruct()) {
  2812. expected("structure");
  2813. return false;
  2814. }
  2815. functionName = NewPoolTString("");
  2816. functionName->append(baseObject->getType().getTypeName());
  2817. parseContext.addScopeMangler(*functionName);
  2818. functionName->append(name);
  2819. }
  2820. // function
  2821. TFunction* function = new TFunction(functionName, TType(EbtVoid));
  2822. // arguments
  2823. TIntermTyped* arguments = nullptr;
  2824. if (baseObject != nullptr) {
  2825. // Non-static member functions have an implicit first argument of the base object.
  2826. parseContext.handleFunctionArgument(function, arguments, baseObject);
  2827. }
  2828. if (! acceptArguments(function, arguments))
  2829. return false;
  2830. // call
  2831. node = parseContext.handleFunctionCall(loc, function, arguments);
  2832. return node != nullptr;
  2833. }
  2834. // arguments
  2835. // : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
  2836. //
  2837. // The arguments are pushed onto the 'function' argument list and
  2838. // onto the 'arguments' aggregate.
  2839. //
  2840. bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
  2841. {
  2842. // LEFT_PAREN
  2843. if (! acceptTokenClass(EHTokLeftParen))
  2844. return false;
  2845. // RIGHT_PAREN
  2846. if (acceptTokenClass(EHTokRightParen))
  2847. return true;
  2848. // must now be at least one expression...
  2849. do {
  2850. // expression
  2851. TIntermTyped* arg;
  2852. if (! acceptAssignmentExpression(arg))
  2853. return false;
  2854. // hook it up
  2855. parseContext.handleFunctionArgument(function, arguments, arg);
  2856. // COMMA
  2857. if (! acceptTokenClass(EHTokComma))
  2858. break;
  2859. } while (true);
  2860. // RIGHT_PAREN
  2861. if (! acceptTokenClass(EHTokRightParen)) {
  2862. expected(")");
  2863. return false;
  2864. }
  2865. return true;
  2866. }
  2867. bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
  2868. {
  2869. switch (token.tokenClass) {
  2870. case EHTokIntConstant:
  2871. node = intermediate.addConstantUnion(token.i, token.loc, true);
  2872. break;
  2873. case EHTokUintConstant:
  2874. node = intermediate.addConstantUnion(token.u, token.loc, true);
  2875. break;
  2876. case EHTokFloat16Constant:
  2877. node = intermediate.addConstantUnion(token.d, EbtFloat16, token.loc, true);
  2878. break;
  2879. case EHTokFloatConstant:
  2880. node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
  2881. break;
  2882. case EHTokDoubleConstant:
  2883. node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
  2884. break;
  2885. case EHTokBoolConstant:
  2886. node = intermediate.addConstantUnion(token.b, token.loc, true);
  2887. break;
  2888. case EHTokStringConstant:
  2889. node = intermediate.addConstantUnion(token.string, token.loc, true);
  2890. break;
  2891. default:
  2892. return false;
  2893. }
  2894. advanceToken();
  2895. return true;
  2896. }
  2897. // simple_statement
  2898. // : SEMICOLON
  2899. // | declaration_statement
  2900. // | expression SEMICOLON
  2901. //
  2902. bool HlslGrammar::acceptSimpleStatement(TIntermNode*& statement)
  2903. {
  2904. // SEMICOLON
  2905. if (acceptTokenClass(EHTokSemicolon))
  2906. return true;
  2907. // declaration
  2908. if (acceptDeclaration(statement))
  2909. return true;
  2910. // expression
  2911. TIntermTyped* node;
  2912. if (acceptExpression(node))
  2913. statement = node;
  2914. else
  2915. return false;
  2916. // SEMICOLON (following an expression)
  2917. if (acceptTokenClass(EHTokSemicolon))
  2918. return true;
  2919. else {
  2920. expected(";");
  2921. return false;
  2922. }
  2923. }
  2924. // compound_statement
  2925. // : LEFT_CURLY statement statement ... RIGHT_CURLY
  2926. //
  2927. bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
  2928. {
  2929. TIntermAggregate* compoundStatement = nullptr;
  2930. // LEFT_CURLY
  2931. if (! acceptTokenClass(EHTokLeftBrace))
  2932. return false;
  2933. // statement statement ...
  2934. TIntermNode* statement = nullptr;
  2935. while (acceptStatement(statement)) {
  2936. TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
  2937. if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
  2938. branch->getFlowOp() == EOpDefault)) {
  2939. // hook up individual subsequences within a switch statement
  2940. parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
  2941. compoundStatement = nullptr;
  2942. } else {
  2943. // hook it up to the growing compound statement
  2944. compoundStatement = intermediate.growAggregate(compoundStatement, statement);
  2945. }
  2946. }
  2947. if (compoundStatement)
  2948. compoundStatement->setOperator(EOpSequence);
  2949. retStatement = compoundStatement;
  2950. // RIGHT_CURLY
  2951. return acceptTokenClass(EHTokRightBrace);
  2952. }
  2953. bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
  2954. {
  2955. parseContext.pushScope();
  2956. bool result = acceptStatement(statement);
  2957. parseContext.popScope();
  2958. return result;
  2959. }
  2960. bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
  2961. {
  2962. parseContext.pushScope();
  2963. bool result = acceptCompoundStatement(statement);
  2964. parseContext.popScope();
  2965. return result;
  2966. }
  2967. // statement
  2968. // : attributes attributed_statement
  2969. //
  2970. // attributed_statement
  2971. // : compound_statement
  2972. // | simple_statement
  2973. // | selection_statement
  2974. // | switch_statement
  2975. // | case_label
  2976. // | default_label
  2977. // | iteration_statement
  2978. // | jump_statement
  2979. //
  2980. bool HlslGrammar::acceptStatement(TIntermNode*& statement)
  2981. {
  2982. statement = nullptr;
  2983. // attributes
  2984. TAttributes attributes;
  2985. acceptAttributes(attributes);
  2986. // attributed_statement
  2987. switch (peek()) {
  2988. case EHTokLeftBrace:
  2989. return acceptScopedCompoundStatement(statement);
  2990. case EHTokIf:
  2991. return acceptSelectionStatement(statement, attributes);
  2992. case EHTokSwitch:
  2993. return acceptSwitchStatement(statement, attributes);
  2994. case EHTokFor:
  2995. case EHTokDo:
  2996. case EHTokWhile:
  2997. return acceptIterationStatement(statement, attributes);
  2998. case EHTokContinue:
  2999. case EHTokBreak:
  3000. case EHTokDiscard:
  3001. case EHTokReturn:
  3002. return acceptJumpStatement(statement);
  3003. case EHTokCase:
  3004. return acceptCaseLabel(statement);
  3005. case EHTokDefault:
  3006. return acceptDefaultLabel(statement);
  3007. case EHTokRightBrace:
  3008. // Performance: not strictly necessary, but stops a bunch of hunting early,
  3009. // and is how sequences of statements end.
  3010. return false;
  3011. default:
  3012. return acceptSimpleStatement(statement);
  3013. }
  3014. return true;
  3015. }
  3016. // attributes
  3017. // : [zero or more:] bracketed-attribute
  3018. //
  3019. // bracketed-attribute:
  3020. // : LEFT_BRACKET scoped-attribute RIGHT_BRACKET
  3021. // : LEFT_BRACKET LEFT_BRACKET scoped-attribute RIGHT_BRACKET RIGHT_BRACKET
  3022. //
  3023. // scoped-attribute:
  3024. // : attribute
  3025. // | namespace COLON COLON attribute
  3026. //
  3027. // attribute:
  3028. // : UNROLL
  3029. // | UNROLL LEFT_PAREN literal RIGHT_PAREN
  3030. // | FASTOPT
  3031. // | ALLOW_UAV_CONDITION
  3032. // | BRANCH
  3033. // | FLATTEN
  3034. // | FORCECASE
  3035. // | CALL
  3036. // | DOMAIN
  3037. // | EARLYDEPTHSTENCIL
  3038. // | INSTANCE
  3039. // | MAXTESSFACTOR
  3040. // | OUTPUTCONTROLPOINTS
  3041. // | OUTPUTTOPOLOGY
  3042. // | PARTITIONING
  3043. // | PATCHCONSTANTFUNC
  3044. // | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
  3045. //
  3046. void HlslGrammar::acceptAttributes(TAttributes& attributes)
  3047. {
  3048. // For now, accept the [ XXX(X) ] syntax, but drop all but
  3049. // numthreads, which is used to set the CS local size.
  3050. // TODO: subset to correct set? Pass on?
  3051. do {
  3052. HlslToken attributeToken;
  3053. // LEFT_BRACKET?
  3054. if (! acceptTokenClass(EHTokLeftBracket))
  3055. return;
  3056. // another LEFT_BRACKET?
  3057. bool doubleBrackets = false;
  3058. if (acceptTokenClass(EHTokLeftBracket))
  3059. doubleBrackets = true;
  3060. // attribute? (could be namespace; will adjust later)
  3061. if (!acceptIdentifier(attributeToken)) {
  3062. if (!peekTokenClass(EHTokRightBracket)) {
  3063. expected("namespace or attribute identifier");
  3064. advanceToken();
  3065. }
  3066. }
  3067. TString nameSpace;
  3068. if (acceptTokenClass(EHTokColonColon)) {
  3069. // namespace COLON COLON
  3070. nameSpace = *attributeToken.string;
  3071. // attribute
  3072. if (!acceptIdentifier(attributeToken)) {
  3073. expected("attribute identifier");
  3074. return;
  3075. }
  3076. }
  3077. TIntermAggregate* expressions = nullptr;
  3078. // (x, ...)
  3079. if (acceptTokenClass(EHTokLeftParen)) {
  3080. expressions = new TIntermAggregate;
  3081. TIntermTyped* node;
  3082. bool expectingExpression = false;
  3083. while (acceptAssignmentExpression(node)) {
  3084. expectingExpression = false;
  3085. expressions->getSequence().push_back(node);
  3086. if (acceptTokenClass(EHTokComma))
  3087. expectingExpression = true;
  3088. }
  3089. // 'expressions' is an aggregate with the expressions in it
  3090. if (! acceptTokenClass(EHTokRightParen))
  3091. expected(")");
  3092. // Error for partial or missing expression
  3093. if (expectingExpression || expressions->getSequence().empty())
  3094. expected("expression");
  3095. }
  3096. // RIGHT_BRACKET
  3097. if (!acceptTokenClass(EHTokRightBracket)) {
  3098. expected("]");
  3099. return;
  3100. }
  3101. // another RIGHT_BRACKET?
  3102. if (doubleBrackets && !acceptTokenClass(EHTokRightBracket)) {
  3103. expected("]]");
  3104. return;
  3105. }
  3106. // Add any values we found into the attribute map.
  3107. if (attributeToken.string != nullptr) {
  3108. TAttributeType attributeType = parseContext.attributeFromName(nameSpace, *attributeToken.string);
  3109. if (attributeType == EatNone)
  3110. parseContext.warn(attributeToken.loc, "unrecognized attribute", attributeToken.string->c_str(), "");
  3111. else {
  3112. TAttributeArgs attributeArgs = { attributeType, expressions };
  3113. attributes.push_back(attributeArgs);
  3114. }
  3115. }
  3116. } while (true);
  3117. }
  3118. // selection_statement
  3119. // : IF LEFT_PAREN expression RIGHT_PAREN statement
  3120. // : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
  3121. //
  3122. bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement, const TAttributes& attributes)
  3123. {
  3124. TSourceLoc loc = token.loc;
  3125. // IF
  3126. if (! acceptTokenClass(EHTokIf))
  3127. return false;
  3128. // so that something declared in the condition is scoped to the lifetimes
  3129. // of the then-else statements
  3130. parseContext.pushScope();
  3131. // LEFT_PAREN expression RIGHT_PAREN
  3132. TIntermTyped* condition;
  3133. if (! acceptParenExpression(condition))
  3134. return false;
  3135. condition = parseContext.convertConditionalExpression(loc, condition);
  3136. if (condition == nullptr)
  3137. return false;
  3138. // create the child statements
  3139. TIntermNodePair thenElse = { nullptr, nullptr };
  3140. ++parseContext.controlFlowNestingLevel; // this only needs to work right if no errors
  3141. // then statement
  3142. if (! acceptScopedStatement(thenElse.node1)) {
  3143. expected("then statement");
  3144. return false;
  3145. }
  3146. // ELSE
  3147. if (acceptTokenClass(EHTokElse)) {
  3148. // else statement
  3149. if (! acceptScopedStatement(thenElse.node2)) {
  3150. expected("else statement");
  3151. return false;
  3152. }
  3153. }
  3154. // Put the pieces together
  3155. statement = intermediate.addSelection(condition, thenElse, loc);
  3156. parseContext.handleSelectionAttributes(loc, statement->getAsSelectionNode(), attributes);
  3157. parseContext.popScope();
  3158. --parseContext.controlFlowNestingLevel;
  3159. return true;
  3160. }
  3161. // switch_statement
  3162. // : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
  3163. //
  3164. bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement, const TAttributes& attributes)
  3165. {
  3166. // SWITCH
  3167. TSourceLoc loc = token.loc;
  3168. if (! acceptTokenClass(EHTokSwitch))
  3169. return false;
  3170. // LEFT_PAREN expression RIGHT_PAREN
  3171. parseContext.pushScope();
  3172. TIntermTyped* switchExpression;
  3173. if (! acceptParenExpression(switchExpression)) {
  3174. parseContext.popScope();
  3175. return false;
  3176. }
  3177. // compound_statement
  3178. parseContext.pushSwitchSequence(new TIntermSequence);
  3179. ++parseContext.controlFlowNestingLevel;
  3180. bool statementOkay = acceptCompoundStatement(statement);
  3181. --parseContext.controlFlowNestingLevel;
  3182. if (statementOkay)
  3183. statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr,
  3184. attributes);
  3185. parseContext.popSwitchSequence();
  3186. parseContext.popScope();
  3187. return statementOkay;
  3188. }
  3189. // iteration_statement
  3190. // : WHILE LEFT_PAREN condition RIGHT_PAREN statement
  3191. // | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
  3192. // | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
  3193. //
  3194. // Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
  3195. bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement, const TAttributes& attributes)
  3196. {
  3197. TSourceLoc loc = token.loc;
  3198. TIntermTyped* condition = nullptr;
  3199. EHlslTokenClass loop = peek();
  3200. assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
  3201. // WHILE or DO or FOR
  3202. advanceToken();
  3203. TIntermLoop* loopNode = nullptr;
  3204. switch (loop) {
  3205. case EHTokWhile:
  3206. // so that something declared in the condition is scoped to the lifetime
  3207. // of the while sub-statement
  3208. parseContext.pushScope(); // this only needs to work right if no errors
  3209. parseContext.nestLooping();
  3210. ++parseContext.controlFlowNestingLevel;
  3211. // LEFT_PAREN condition RIGHT_PAREN
  3212. if (! acceptParenExpression(condition))
  3213. return false;
  3214. condition = parseContext.convertConditionalExpression(loc, condition);
  3215. if (condition == nullptr)
  3216. return false;
  3217. // statement
  3218. if (! acceptScopedStatement(statement)) {
  3219. expected("while sub-statement");
  3220. return false;
  3221. }
  3222. parseContext.unnestLooping();
  3223. parseContext.popScope();
  3224. --parseContext.controlFlowNestingLevel;
  3225. loopNode = intermediate.addLoop(statement, condition, nullptr, true, loc);
  3226. statement = loopNode;
  3227. break;
  3228. case EHTokDo:
  3229. parseContext.nestLooping(); // this only needs to work right if no errors
  3230. ++parseContext.controlFlowNestingLevel;
  3231. // statement
  3232. if (! acceptScopedStatement(statement)) {
  3233. expected("do sub-statement");
  3234. return false;
  3235. }
  3236. // WHILE
  3237. if (! acceptTokenClass(EHTokWhile)) {
  3238. expected("while");
  3239. return false;
  3240. }
  3241. // LEFT_PAREN condition RIGHT_PAREN
  3242. if (! acceptParenExpression(condition))
  3243. return false;
  3244. condition = parseContext.convertConditionalExpression(loc, condition);
  3245. if (condition == nullptr)
  3246. return false;
  3247. if (! acceptTokenClass(EHTokSemicolon))
  3248. expected(";");
  3249. parseContext.unnestLooping();
  3250. --parseContext.controlFlowNestingLevel;
  3251. loopNode = intermediate.addLoop(statement, condition, 0, false, loc);
  3252. statement = loopNode;
  3253. break;
  3254. case EHTokFor:
  3255. {
  3256. // LEFT_PAREN
  3257. if (! acceptTokenClass(EHTokLeftParen))
  3258. expected("(");
  3259. // so that something declared in the condition is scoped to the lifetime
  3260. // of the for sub-statement
  3261. parseContext.pushScope();
  3262. // initializer
  3263. TIntermNode* initNode = nullptr;
  3264. if (! acceptSimpleStatement(initNode))
  3265. expected("for-loop initializer statement");
  3266. parseContext.nestLooping(); // this only needs to work right if no errors
  3267. ++parseContext.controlFlowNestingLevel;
  3268. // condition SEMI_COLON
  3269. acceptExpression(condition);
  3270. if (! acceptTokenClass(EHTokSemicolon))
  3271. expected(";");
  3272. if (condition != nullptr) {
  3273. condition = parseContext.convertConditionalExpression(loc, condition);
  3274. if (condition == nullptr)
  3275. return false;
  3276. }
  3277. // iterator SEMI_COLON
  3278. TIntermTyped* iterator = nullptr;
  3279. acceptExpression(iterator);
  3280. if (! acceptTokenClass(EHTokRightParen))
  3281. expected(")");
  3282. // statement
  3283. if (! acceptScopedStatement(statement)) {
  3284. expected("for sub-statement");
  3285. return false;
  3286. }
  3287. statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc, loopNode);
  3288. parseContext.popScope();
  3289. parseContext.unnestLooping();
  3290. --parseContext.controlFlowNestingLevel;
  3291. break;
  3292. }
  3293. default:
  3294. return false;
  3295. }
  3296. parseContext.handleLoopAttributes(loc, loopNode, attributes);
  3297. return true;
  3298. }
  3299. // jump_statement
  3300. // : CONTINUE SEMICOLON
  3301. // | BREAK SEMICOLON
  3302. // | DISCARD SEMICOLON
  3303. // | RETURN SEMICOLON
  3304. // | RETURN expression SEMICOLON
  3305. //
  3306. bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
  3307. {
  3308. EHlslTokenClass jump = peek();
  3309. switch (jump) {
  3310. case EHTokContinue:
  3311. case EHTokBreak:
  3312. case EHTokDiscard:
  3313. case EHTokReturn:
  3314. advanceToken();
  3315. break;
  3316. default:
  3317. // not something we handle in this function
  3318. return false;
  3319. }
  3320. switch (jump) {
  3321. case EHTokContinue:
  3322. statement = intermediate.addBranch(EOpContinue, token.loc);
  3323. if (parseContext.loopNestingLevel == 0) {
  3324. expected("loop");
  3325. return false;
  3326. }
  3327. break;
  3328. case EHTokBreak:
  3329. statement = intermediate.addBranch(EOpBreak, token.loc);
  3330. if (parseContext.loopNestingLevel == 0 && parseContext.switchSequenceStack.size() == 0) {
  3331. expected("loop or switch");
  3332. return false;
  3333. }
  3334. break;
  3335. case EHTokDiscard:
  3336. statement = intermediate.addBranch(EOpKill, token.loc);
  3337. break;
  3338. case EHTokReturn:
  3339. {
  3340. // expression
  3341. TIntermTyped* node;
  3342. if (acceptExpression(node)) {
  3343. // hook it up
  3344. statement = parseContext.handleReturnValue(token.loc, node);
  3345. } else
  3346. statement = intermediate.addBranch(EOpReturn, token.loc);
  3347. break;
  3348. }
  3349. default:
  3350. assert(0);
  3351. return false;
  3352. }
  3353. // SEMICOLON
  3354. if (! acceptTokenClass(EHTokSemicolon))
  3355. expected(";");
  3356. return true;
  3357. }
  3358. // case_label
  3359. // : CASE expression COLON
  3360. //
  3361. bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
  3362. {
  3363. TSourceLoc loc = token.loc;
  3364. if (! acceptTokenClass(EHTokCase))
  3365. return false;
  3366. TIntermTyped* expression;
  3367. if (! acceptExpression(expression)) {
  3368. expected("case expression");
  3369. return false;
  3370. }
  3371. if (! acceptTokenClass(EHTokColon)) {
  3372. expected(":");
  3373. return false;
  3374. }
  3375. statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
  3376. return true;
  3377. }
  3378. // default_label
  3379. // : DEFAULT COLON
  3380. //
  3381. bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
  3382. {
  3383. TSourceLoc loc = token.loc;
  3384. if (! acceptTokenClass(EHTokDefault))
  3385. return false;
  3386. if (! acceptTokenClass(EHTokColon)) {
  3387. expected(":");
  3388. return false;
  3389. }
  3390. statement = parseContext.intermediate.addBranch(EOpDefault, loc);
  3391. return true;
  3392. }
  3393. // array_specifier
  3394. // : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
  3395. // : LEFT_BRACKET RGHT_BRACKET // optional
  3396. //
  3397. void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
  3398. {
  3399. arraySizes = nullptr;
  3400. // Early-out if there aren't any array dimensions
  3401. if (!peekTokenClass(EHTokLeftBracket))
  3402. return;
  3403. // If we get here, we have at least one array dimension. This will track the sizes we find.
  3404. arraySizes = new TArraySizes;
  3405. // Collect each array dimension.
  3406. while (acceptTokenClass(EHTokLeftBracket)) {
  3407. TSourceLoc loc = token.loc;
  3408. TIntermTyped* sizeExpr = nullptr;
  3409. // Array sizing expression is optional. If omitted, array will be later sized by initializer list.
  3410. const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
  3411. if (! acceptTokenClass(EHTokRightBracket)) {
  3412. expected("]");
  3413. return;
  3414. }
  3415. if (hasArraySize) {
  3416. TArraySize arraySize;
  3417. parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
  3418. arraySizes->addInnerSize(arraySize);
  3419. } else {
  3420. arraySizes->addInnerSize(0); // sized by initializers.
  3421. }
  3422. }
  3423. }
  3424. // post_decls
  3425. // : COLON semantic // optional
  3426. // COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
  3427. // COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
  3428. // COLON LAYOUT layout_qualifier_list
  3429. // annotations // optional
  3430. //
  3431. // Return true if any tokens were accepted. That is,
  3432. // false can be returned on successfully recognizing nothing,
  3433. // not necessarily meaning bad syntax.
  3434. //
  3435. bool HlslGrammar::acceptPostDecls(TQualifier& qualifier)
  3436. {
  3437. bool found = false;
  3438. do {
  3439. // COLON
  3440. if (acceptTokenClass(EHTokColon)) {
  3441. found = true;
  3442. HlslToken idToken;
  3443. if (peekTokenClass(EHTokLayout))
  3444. acceptLayoutQualifierList(qualifier);
  3445. else if (acceptTokenClass(EHTokPackOffset)) {
  3446. // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
  3447. if (! acceptTokenClass(EHTokLeftParen)) {
  3448. expected("(");
  3449. return false;
  3450. }
  3451. HlslToken locationToken;
  3452. if (! acceptIdentifier(locationToken)) {
  3453. expected("c[subcomponent][.component]");
  3454. return false;
  3455. }
  3456. HlslToken componentToken;
  3457. if (acceptTokenClass(EHTokDot)) {
  3458. if (! acceptIdentifier(componentToken)) {
  3459. expected("component");
  3460. return false;
  3461. }
  3462. }
  3463. if (! acceptTokenClass(EHTokRightParen)) {
  3464. expected(")");
  3465. break;
  3466. }
  3467. parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
  3468. } else if (! acceptIdentifier(idToken)) {
  3469. expected("layout, semantic, packoffset, or register");
  3470. return false;
  3471. } else if (*idToken.string == "register") {
  3472. // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
  3473. // LEFT_PAREN
  3474. if (! acceptTokenClass(EHTokLeftParen)) {
  3475. expected("(");
  3476. return false;
  3477. }
  3478. HlslToken registerDesc; // for Type#
  3479. HlslToken profile;
  3480. if (! acceptIdentifier(registerDesc)) {
  3481. expected("register number description");
  3482. return false;
  3483. }
  3484. if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
  3485. acceptTokenClass(EHTokComma)) {
  3486. // Then we didn't really see the registerDesc yet, it was
  3487. // actually the profile. Adjust...
  3488. profile = registerDesc;
  3489. if (! acceptIdentifier(registerDesc)) {
  3490. expected("register number description");
  3491. return false;
  3492. }
  3493. }
  3494. int subComponent = 0;
  3495. if (acceptTokenClass(EHTokLeftBracket)) {
  3496. // LEFT_BRACKET subcomponent RIGHT_BRACKET
  3497. if (! peekTokenClass(EHTokIntConstant)) {
  3498. expected("literal integer");
  3499. return false;
  3500. }
  3501. subComponent = token.i;
  3502. advanceToken();
  3503. if (! acceptTokenClass(EHTokRightBracket)) {
  3504. expected("]");
  3505. break;
  3506. }
  3507. }
  3508. // (COMMA SPACEN)opt
  3509. HlslToken spaceDesc;
  3510. if (acceptTokenClass(EHTokComma)) {
  3511. if (! acceptIdentifier(spaceDesc)) {
  3512. expected ("space identifier");
  3513. return false;
  3514. }
  3515. }
  3516. // RIGHT_PAREN
  3517. if (! acceptTokenClass(EHTokRightParen)) {
  3518. expected(")");
  3519. break;
  3520. }
  3521. parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
  3522. } else {
  3523. // semantic, in idToken.string
  3524. TString semanticUpperCase = *idToken.string;
  3525. std::transform(semanticUpperCase.begin(), semanticUpperCase.end(), semanticUpperCase.begin(), ::toupper);
  3526. parseContext.handleSemantic(idToken.loc, qualifier, mapSemantic(semanticUpperCase.c_str()), semanticUpperCase);
  3527. }
  3528. } else if (peekTokenClass(EHTokLeftAngle)) {
  3529. found = true;
  3530. acceptAnnotations(qualifier);
  3531. } else
  3532. break;
  3533. } while (true);
  3534. return found;
  3535. }
  3536. //
  3537. // Get the stream of tokens from the scanner, but skip all syntactic/semantic
  3538. // processing.
  3539. //
  3540. bool HlslGrammar::captureBlockTokens(TVector<HlslToken>& tokens)
  3541. {
  3542. if (! peekTokenClass(EHTokLeftBrace))
  3543. return false;
  3544. int braceCount = 0;
  3545. do {
  3546. switch (peek()) {
  3547. case EHTokLeftBrace:
  3548. ++braceCount;
  3549. break;
  3550. case EHTokRightBrace:
  3551. --braceCount;
  3552. break;
  3553. case EHTokNone:
  3554. // End of input before balance { } is bad...
  3555. return false;
  3556. default:
  3557. break;
  3558. }
  3559. tokens.push_back(token);
  3560. advanceToken();
  3561. } while (braceCount > 0);
  3562. return true;
  3563. }
  3564. // Return a string for just the types that can also be declared as an identifier.
  3565. const char* HlslGrammar::getTypeString(EHlslTokenClass tokenClass) const
  3566. {
  3567. switch (tokenClass) {
  3568. case EHTokSample: return "sample";
  3569. case EHTokHalf: return "half";
  3570. case EHTokHalf1x1: return "half1x1";
  3571. case EHTokHalf1x2: return "half1x2";
  3572. case EHTokHalf1x3: return "half1x3";
  3573. case EHTokHalf1x4: return "half1x4";
  3574. case EHTokHalf2x1: return "half2x1";
  3575. case EHTokHalf2x2: return "half2x2";
  3576. case EHTokHalf2x3: return "half2x3";
  3577. case EHTokHalf2x4: return "half2x4";
  3578. case EHTokHalf3x1: return "half3x1";
  3579. case EHTokHalf3x2: return "half3x2";
  3580. case EHTokHalf3x3: return "half3x3";
  3581. case EHTokHalf3x4: return "half3x4";
  3582. case EHTokHalf4x1: return "half4x1";
  3583. case EHTokHalf4x2: return "half4x2";
  3584. case EHTokHalf4x3: return "half4x3";
  3585. case EHTokHalf4x4: return "half4x4";
  3586. case EHTokBool: return "bool";
  3587. case EHTokFloat: return "float";
  3588. case EHTokDouble: return "double";
  3589. case EHTokInt: return "int";
  3590. case EHTokUint: return "uint";
  3591. case EHTokMin16float: return "min16float";
  3592. case EHTokMin10float: return "min10float";
  3593. case EHTokMin16int: return "min16int";
  3594. case EHTokMin12int: return "min12int";
  3595. case EHTokConstantBuffer: return "ConstantBuffer";
  3596. case EHTokLayout: return "layout";
  3597. default:
  3598. return nullptr;
  3599. }
  3600. }
  3601. } // end namespace glslang