hlslGrammar.cpp 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175
  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. if (arguments == nullptr) {
  2789. expected("one or more arguments");
  2790. return false;
  2791. }
  2792. // hook it up
  2793. node = parseContext.handleFunctionCall(arguments->getLoc(), constructorFunction, arguments);
  2794. return node != nullptr;
  2795. }
  2796. return false;
  2797. }
  2798. // The function_call identifier was already recognized, and passed in as idToken.
  2799. //
  2800. // function_call
  2801. // : [idToken] arguments
  2802. //
  2803. bool HlslGrammar::acceptFunctionCall(const TSourceLoc& loc, TString& name, TIntermTyped*& node, TIntermTyped* baseObject)
  2804. {
  2805. // name
  2806. TString* functionName = nullptr;
  2807. if (baseObject == nullptr) {
  2808. functionName = &name;
  2809. } else if (parseContext.isBuiltInMethod(loc, baseObject, name)) {
  2810. // Built-in methods are not in the symbol table as methods, but as global functions
  2811. // taking an explicit 'this' as the first argument.
  2812. functionName = NewPoolTString(BUILTIN_PREFIX);
  2813. functionName->append(name);
  2814. } else {
  2815. if (! baseObject->getType().isStruct()) {
  2816. expected("structure");
  2817. return false;
  2818. }
  2819. functionName = NewPoolTString("");
  2820. functionName->append(baseObject->getType().getTypeName());
  2821. parseContext.addScopeMangler(*functionName);
  2822. functionName->append(name);
  2823. }
  2824. // function
  2825. TFunction* function = new TFunction(functionName, TType(EbtVoid));
  2826. // arguments
  2827. TIntermTyped* arguments = nullptr;
  2828. if (baseObject != nullptr) {
  2829. // Non-static member functions have an implicit first argument of the base object.
  2830. parseContext.handleFunctionArgument(function, arguments, baseObject);
  2831. }
  2832. if (! acceptArguments(function, arguments))
  2833. return false;
  2834. // call
  2835. node = parseContext.handleFunctionCall(loc, function, arguments);
  2836. return node != nullptr;
  2837. }
  2838. // arguments
  2839. // : LEFT_PAREN expression COMMA expression COMMA ... RIGHT_PAREN
  2840. //
  2841. // The arguments are pushed onto the 'function' argument list and
  2842. // onto the 'arguments' aggregate.
  2843. //
  2844. bool HlslGrammar::acceptArguments(TFunction* function, TIntermTyped*& arguments)
  2845. {
  2846. // LEFT_PAREN
  2847. if (! acceptTokenClass(EHTokLeftParen))
  2848. return false;
  2849. // RIGHT_PAREN
  2850. if (acceptTokenClass(EHTokRightParen))
  2851. return true;
  2852. // must now be at least one expression...
  2853. do {
  2854. // expression
  2855. TIntermTyped* arg;
  2856. if (! acceptAssignmentExpression(arg))
  2857. return false;
  2858. // hook it up
  2859. parseContext.handleFunctionArgument(function, arguments, arg);
  2860. // COMMA
  2861. if (! acceptTokenClass(EHTokComma))
  2862. break;
  2863. } while (true);
  2864. // RIGHT_PAREN
  2865. if (! acceptTokenClass(EHTokRightParen)) {
  2866. expected(")");
  2867. return false;
  2868. }
  2869. return true;
  2870. }
  2871. bool HlslGrammar::acceptLiteral(TIntermTyped*& node)
  2872. {
  2873. switch (token.tokenClass) {
  2874. case EHTokIntConstant:
  2875. node = intermediate.addConstantUnion(token.i, token.loc, true);
  2876. break;
  2877. case EHTokUintConstant:
  2878. node = intermediate.addConstantUnion(token.u, token.loc, true);
  2879. break;
  2880. case EHTokFloat16Constant:
  2881. node = intermediate.addConstantUnion(token.d, EbtFloat16, token.loc, true);
  2882. break;
  2883. case EHTokFloatConstant:
  2884. node = intermediate.addConstantUnion(token.d, EbtFloat, token.loc, true);
  2885. break;
  2886. case EHTokDoubleConstant:
  2887. node = intermediate.addConstantUnion(token.d, EbtDouble, token.loc, true);
  2888. break;
  2889. case EHTokBoolConstant:
  2890. node = intermediate.addConstantUnion(token.b, token.loc, true);
  2891. break;
  2892. case EHTokStringConstant:
  2893. node = intermediate.addConstantUnion(token.string, token.loc, true);
  2894. break;
  2895. default:
  2896. return false;
  2897. }
  2898. advanceToken();
  2899. return true;
  2900. }
  2901. // simple_statement
  2902. // : SEMICOLON
  2903. // | declaration_statement
  2904. // | expression SEMICOLON
  2905. //
  2906. bool HlslGrammar::acceptSimpleStatement(TIntermNode*& statement)
  2907. {
  2908. // SEMICOLON
  2909. if (acceptTokenClass(EHTokSemicolon))
  2910. return true;
  2911. // declaration
  2912. if (acceptDeclaration(statement))
  2913. return true;
  2914. // expression
  2915. TIntermTyped* node;
  2916. if (acceptExpression(node))
  2917. statement = node;
  2918. else
  2919. return false;
  2920. // SEMICOLON (following an expression)
  2921. if (acceptTokenClass(EHTokSemicolon))
  2922. return true;
  2923. else {
  2924. expected(";");
  2925. return false;
  2926. }
  2927. }
  2928. // compound_statement
  2929. // : LEFT_CURLY statement statement ... RIGHT_CURLY
  2930. //
  2931. bool HlslGrammar::acceptCompoundStatement(TIntermNode*& retStatement)
  2932. {
  2933. TIntermAggregate* compoundStatement = nullptr;
  2934. // LEFT_CURLY
  2935. if (! acceptTokenClass(EHTokLeftBrace))
  2936. return false;
  2937. // statement statement ...
  2938. TIntermNode* statement = nullptr;
  2939. while (acceptStatement(statement)) {
  2940. TIntermBranch* branch = statement ? statement->getAsBranchNode() : nullptr;
  2941. if (branch != nullptr && (branch->getFlowOp() == EOpCase ||
  2942. branch->getFlowOp() == EOpDefault)) {
  2943. // hook up individual subsequences within a switch statement
  2944. parseContext.wrapupSwitchSubsequence(compoundStatement, statement);
  2945. compoundStatement = nullptr;
  2946. } else {
  2947. // hook it up to the growing compound statement
  2948. compoundStatement = intermediate.growAggregate(compoundStatement, statement);
  2949. }
  2950. }
  2951. if (compoundStatement)
  2952. compoundStatement->setOperator(EOpSequence);
  2953. retStatement = compoundStatement;
  2954. // RIGHT_CURLY
  2955. return acceptTokenClass(EHTokRightBrace);
  2956. }
  2957. bool HlslGrammar::acceptScopedStatement(TIntermNode*& statement)
  2958. {
  2959. parseContext.pushScope();
  2960. bool result = acceptStatement(statement);
  2961. parseContext.popScope();
  2962. return result;
  2963. }
  2964. bool HlslGrammar::acceptScopedCompoundStatement(TIntermNode*& statement)
  2965. {
  2966. parseContext.pushScope();
  2967. bool result = acceptCompoundStatement(statement);
  2968. parseContext.popScope();
  2969. return result;
  2970. }
  2971. // statement
  2972. // : attributes attributed_statement
  2973. //
  2974. // attributed_statement
  2975. // : compound_statement
  2976. // | simple_statement
  2977. // | selection_statement
  2978. // | switch_statement
  2979. // | case_label
  2980. // | default_label
  2981. // | iteration_statement
  2982. // | jump_statement
  2983. //
  2984. bool HlslGrammar::acceptStatement(TIntermNode*& statement)
  2985. {
  2986. statement = nullptr;
  2987. // attributes
  2988. TAttributes attributes;
  2989. acceptAttributes(attributes);
  2990. // attributed_statement
  2991. switch (peek()) {
  2992. case EHTokLeftBrace:
  2993. return acceptScopedCompoundStatement(statement);
  2994. case EHTokIf:
  2995. return acceptSelectionStatement(statement, attributes);
  2996. case EHTokSwitch:
  2997. return acceptSwitchStatement(statement, attributes);
  2998. case EHTokFor:
  2999. case EHTokDo:
  3000. case EHTokWhile:
  3001. return acceptIterationStatement(statement, attributes);
  3002. case EHTokContinue:
  3003. case EHTokBreak:
  3004. case EHTokDiscard:
  3005. case EHTokReturn:
  3006. return acceptJumpStatement(statement);
  3007. case EHTokCase:
  3008. return acceptCaseLabel(statement);
  3009. case EHTokDefault:
  3010. return acceptDefaultLabel(statement);
  3011. case EHTokRightBrace:
  3012. // Performance: not strictly necessary, but stops a bunch of hunting early,
  3013. // and is how sequences of statements end.
  3014. return false;
  3015. default:
  3016. return acceptSimpleStatement(statement);
  3017. }
  3018. return true;
  3019. }
  3020. // attributes
  3021. // : [zero or more:] bracketed-attribute
  3022. //
  3023. // bracketed-attribute:
  3024. // : LEFT_BRACKET scoped-attribute RIGHT_BRACKET
  3025. // : LEFT_BRACKET LEFT_BRACKET scoped-attribute RIGHT_BRACKET RIGHT_BRACKET
  3026. //
  3027. // scoped-attribute:
  3028. // : attribute
  3029. // | namespace COLON COLON attribute
  3030. //
  3031. // attribute:
  3032. // : UNROLL
  3033. // | UNROLL LEFT_PAREN literal RIGHT_PAREN
  3034. // | FASTOPT
  3035. // | ALLOW_UAV_CONDITION
  3036. // | BRANCH
  3037. // | FLATTEN
  3038. // | FORCECASE
  3039. // | CALL
  3040. // | DOMAIN
  3041. // | EARLYDEPTHSTENCIL
  3042. // | INSTANCE
  3043. // | MAXTESSFACTOR
  3044. // | OUTPUTCONTROLPOINTS
  3045. // | OUTPUTTOPOLOGY
  3046. // | PARTITIONING
  3047. // | PATCHCONSTANTFUNC
  3048. // | NUMTHREADS LEFT_PAREN x_size, y_size,z z_size RIGHT_PAREN
  3049. //
  3050. void HlslGrammar::acceptAttributes(TAttributes& attributes)
  3051. {
  3052. // For now, accept the [ XXX(X) ] syntax, but drop all but
  3053. // numthreads, which is used to set the CS local size.
  3054. // TODO: subset to correct set? Pass on?
  3055. do {
  3056. HlslToken attributeToken;
  3057. // LEFT_BRACKET?
  3058. if (! acceptTokenClass(EHTokLeftBracket))
  3059. return;
  3060. // another LEFT_BRACKET?
  3061. bool doubleBrackets = false;
  3062. if (acceptTokenClass(EHTokLeftBracket))
  3063. doubleBrackets = true;
  3064. // attribute? (could be namespace; will adjust later)
  3065. if (!acceptIdentifier(attributeToken)) {
  3066. if (!peekTokenClass(EHTokRightBracket)) {
  3067. expected("namespace or attribute identifier");
  3068. advanceToken();
  3069. }
  3070. }
  3071. TString nameSpace;
  3072. if (acceptTokenClass(EHTokColonColon)) {
  3073. // namespace COLON COLON
  3074. nameSpace = *attributeToken.string;
  3075. // attribute
  3076. if (!acceptIdentifier(attributeToken)) {
  3077. expected("attribute identifier");
  3078. return;
  3079. }
  3080. }
  3081. TIntermAggregate* expressions = nullptr;
  3082. // (x, ...)
  3083. if (acceptTokenClass(EHTokLeftParen)) {
  3084. expressions = new TIntermAggregate;
  3085. TIntermTyped* node;
  3086. bool expectingExpression = false;
  3087. while (acceptAssignmentExpression(node)) {
  3088. expectingExpression = false;
  3089. expressions->getSequence().push_back(node);
  3090. if (acceptTokenClass(EHTokComma))
  3091. expectingExpression = true;
  3092. }
  3093. // 'expressions' is an aggregate with the expressions in it
  3094. if (! acceptTokenClass(EHTokRightParen))
  3095. expected(")");
  3096. // Error for partial or missing expression
  3097. if (expectingExpression || expressions->getSequence().empty())
  3098. expected("expression");
  3099. }
  3100. // RIGHT_BRACKET
  3101. if (!acceptTokenClass(EHTokRightBracket)) {
  3102. expected("]");
  3103. return;
  3104. }
  3105. // another RIGHT_BRACKET?
  3106. if (doubleBrackets && !acceptTokenClass(EHTokRightBracket)) {
  3107. expected("]]");
  3108. return;
  3109. }
  3110. // Add any values we found into the attribute map.
  3111. if (attributeToken.string != nullptr) {
  3112. TAttributeType attributeType = parseContext.attributeFromName(nameSpace, *attributeToken.string);
  3113. if (attributeType == EatNone)
  3114. parseContext.warn(attributeToken.loc, "unrecognized attribute", attributeToken.string->c_str(), "");
  3115. else {
  3116. TAttributeArgs attributeArgs = { attributeType, expressions };
  3117. attributes.push_back(attributeArgs);
  3118. }
  3119. }
  3120. } while (true);
  3121. }
  3122. // selection_statement
  3123. // : IF LEFT_PAREN expression RIGHT_PAREN statement
  3124. // : IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement
  3125. //
  3126. bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement, const TAttributes& attributes)
  3127. {
  3128. TSourceLoc loc = token.loc;
  3129. // IF
  3130. if (! acceptTokenClass(EHTokIf))
  3131. return false;
  3132. // so that something declared in the condition is scoped to the lifetimes
  3133. // of the then-else statements
  3134. parseContext.pushScope();
  3135. // LEFT_PAREN expression RIGHT_PAREN
  3136. TIntermTyped* condition;
  3137. if (! acceptParenExpression(condition))
  3138. return false;
  3139. condition = parseContext.convertConditionalExpression(loc, condition);
  3140. if (condition == nullptr)
  3141. return false;
  3142. // create the child statements
  3143. TIntermNodePair thenElse = { nullptr, nullptr };
  3144. ++parseContext.controlFlowNestingLevel; // this only needs to work right if no errors
  3145. // then statement
  3146. if (! acceptScopedStatement(thenElse.node1)) {
  3147. expected("then statement");
  3148. return false;
  3149. }
  3150. // ELSE
  3151. if (acceptTokenClass(EHTokElse)) {
  3152. // else statement
  3153. if (! acceptScopedStatement(thenElse.node2)) {
  3154. expected("else statement");
  3155. return false;
  3156. }
  3157. }
  3158. // Put the pieces together
  3159. statement = intermediate.addSelection(condition, thenElse, loc);
  3160. parseContext.handleSelectionAttributes(loc, statement->getAsSelectionNode(), attributes);
  3161. parseContext.popScope();
  3162. --parseContext.controlFlowNestingLevel;
  3163. return true;
  3164. }
  3165. // switch_statement
  3166. // : SWITCH LEFT_PAREN expression RIGHT_PAREN compound_statement
  3167. //
  3168. bool HlslGrammar::acceptSwitchStatement(TIntermNode*& statement, const TAttributes& attributes)
  3169. {
  3170. // SWITCH
  3171. TSourceLoc loc = token.loc;
  3172. if (! acceptTokenClass(EHTokSwitch))
  3173. return false;
  3174. // LEFT_PAREN expression RIGHT_PAREN
  3175. parseContext.pushScope();
  3176. TIntermTyped* switchExpression;
  3177. if (! acceptParenExpression(switchExpression)) {
  3178. parseContext.popScope();
  3179. return false;
  3180. }
  3181. // compound_statement
  3182. parseContext.pushSwitchSequence(new TIntermSequence);
  3183. ++parseContext.controlFlowNestingLevel;
  3184. bool statementOkay = acceptCompoundStatement(statement);
  3185. --parseContext.controlFlowNestingLevel;
  3186. if (statementOkay)
  3187. statement = parseContext.addSwitch(loc, switchExpression, statement ? statement->getAsAggregate() : nullptr,
  3188. attributes);
  3189. parseContext.popSwitchSequence();
  3190. parseContext.popScope();
  3191. return statementOkay;
  3192. }
  3193. // iteration_statement
  3194. // : WHILE LEFT_PAREN condition RIGHT_PAREN statement
  3195. // | DO LEFT_BRACE statement RIGHT_BRACE WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON
  3196. // | FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement
  3197. //
  3198. // Non-speculative, only call if it needs to be found; WHILE or DO or FOR already seen.
  3199. bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement, const TAttributes& attributes)
  3200. {
  3201. TSourceLoc loc = token.loc;
  3202. TIntermTyped* condition = nullptr;
  3203. EHlslTokenClass loop = peek();
  3204. assert(loop == EHTokDo || loop == EHTokFor || loop == EHTokWhile);
  3205. // WHILE or DO or FOR
  3206. advanceToken();
  3207. TIntermLoop* loopNode = nullptr;
  3208. switch (loop) {
  3209. case EHTokWhile:
  3210. // so that something declared in the condition is scoped to the lifetime
  3211. // of the while sub-statement
  3212. parseContext.pushScope(); // this only needs to work right if no errors
  3213. parseContext.nestLooping();
  3214. ++parseContext.controlFlowNestingLevel;
  3215. // LEFT_PAREN condition RIGHT_PAREN
  3216. if (! acceptParenExpression(condition))
  3217. return false;
  3218. condition = parseContext.convertConditionalExpression(loc, condition);
  3219. if (condition == nullptr)
  3220. return false;
  3221. // statement
  3222. if (! acceptScopedStatement(statement)) {
  3223. expected("while sub-statement");
  3224. return false;
  3225. }
  3226. parseContext.unnestLooping();
  3227. parseContext.popScope();
  3228. --parseContext.controlFlowNestingLevel;
  3229. loopNode = intermediate.addLoop(statement, condition, nullptr, true, loc);
  3230. statement = loopNode;
  3231. break;
  3232. case EHTokDo:
  3233. parseContext.nestLooping(); // this only needs to work right if no errors
  3234. ++parseContext.controlFlowNestingLevel;
  3235. // statement
  3236. if (! acceptScopedStatement(statement)) {
  3237. expected("do sub-statement");
  3238. return false;
  3239. }
  3240. // WHILE
  3241. if (! acceptTokenClass(EHTokWhile)) {
  3242. expected("while");
  3243. return false;
  3244. }
  3245. // LEFT_PAREN condition RIGHT_PAREN
  3246. if (! acceptParenExpression(condition))
  3247. return false;
  3248. condition = parseContext.convertConditionalExpression(loc, condition);
  3249. if (condition == nullptr)
  3250. return false;
  3251. if (! acceptTokenClass(EHTokSemicolon))
  3252. expected(";");
  3253. parseContext.unnestLooping();
  3254. --parseContext.controlFlowNestingLevel;
  3255. loopNode = intermediate.addLoop(statement, condition, 0, false, loc);
  3256. statement = loopNode;
  3257. break;
  3258. case EHTokFor:
  3259. {
  3260. // LEFT_PAREN
  3261. if (! acceptTokenClass(EHTokLeftParen))
  3262. expected("(");
  3263. // so that something declared in the condition is scoped to the lifetime
  3264. // of the for sub-statement
  3265. parseContext.pushScope();
  3266. // initializer
  3267. TIntermNode* initNode = nullptr;
  3268. if (! acceptSimpleStatement(initNode))
  3269. expected("for-loop initializer statement");
  3270. parseContext.nestLooping(); // this only needs to work right if no errors
  3271. ++parseContext.controlFlowNestingLevel;
  3272. // condition SEMI_COLON
  3273. acceptExpression(condition);
  3274. if (! acceptTokenClass(EHTokSemicolon))
  3275. expected(";");
  3276. if (condition != nullptr) {
  3277. condition = parseContext.convertConditionalExpression(loc, condition);
  3278. if (condition == nullptr)
  3279. return false;
  3280. }
  3281. // iterator SEMI_COLON
  3282. TIntermTyped* iterator = nullptr;
  3283. acceptExpression(iterator);
  3284. if (! acceptTokenClass(EHTokRightParen))
  3285. expected(")");
  3286. // statement
  3287. if (! acceptScopedStatement(statement)) {
  3288. expected("for sub-statement");
  3289. return false;
  3290. }
  3291. statement = intermediate.addForLoop(statement, initNode, condition, iterator, true, loc, loopNode);
  3292. parseContext.popScope();
  3293. parseContext.unnestLooping();
  3294. --parseContext.controlFlowNestingLevel;
  3295. break;
  3296. }
  3297. default:
  3298. return false;
  3299. }
  3300. parseContext.handleLoopAttributes(loc, loopNode, attributes);
  3301. return true;
  3302. }
  3303. // jump_statement
  3304. // : CONTINUE SEMICOLON
  3305. // | BREAK SEMICOLON
  3306. // | DISCARD SEMICOLON
  3307. // | RETURN SEMICOLON
  3308. // | RETURN expression SEMICOLON
  3309. //
  3310. bool HlslGrammar::acceptJumpStatement(TIntermNode*& statement)
  3311. {
  3312. EHlslTokenClass jump = peek();
  3313. switch (jump) {
  3314. case EHTokContinue:
  3315. case EHTokBreak:
  3316. case EHTokDiscard:
  3317. case EHTokReturn:
  3318. advanceToken();
  3319. break;
  3320. default:
  3321. // not something we handle in this function
  3322. return false;
  3323. }
  3324. switch (jump) {
  3325. case EHTokContinue:
  3326. statement = intermediate.addBranch(EOpContinue, token.loc);
  3327. if (parseContext.loopNestingLevel == 0) {
  3328. expected("loop");
  3329. return false;
  3330. }
  3331. break;
  3332. case EHTokBreak:
  3333. statement = intermediate.addBranch(EOpBreak, token.loc);
  3334. if (parseContext.loopNestingLevel == 0 && parseContext.switchSequenceStack.size() == 0) {
  3335. expected("loop or switch");
  3336. return false;
  3337. }
  3338. break;
  3339. case EHTokDiscard:
  3340. statement = intermediate.addBranch(EOpKill, token.loc);
  3341. break;
  3342. case EHTokReturn:
  3343. {
  3344. // expression
  3345. TIntermTyped* node;
  3346. if (acceptExpression(node)) {
  3347. // hook it up
  3348. statement = parseContext.handleReturnValue(token.loc, node);
  3349. } else
  3350. statement = intermediate.addBranch(EOpReturn, token.loc);
  3351. break;
  3352. }
  3353. default:
  3354. assert(0);
  3355. return false;
  3356. }
  3357. // SEMICOLON
  3358. if (! acceptTokenClass(EHTokSemicolon))
  3359. expected(";");
  3360. return true;
  3361. }
  3362. // case_label
  3363. // : CASE expression COLON
  3364. //
  3365. bool HlslGrammar::acceptCaseLabel(TIntermNode*& statement)
  3366. {
  3367. TSourceLoc loc = token.loc;
  3368. if (! acceptTokenClass(EHTokCase))
  3369. return false;
  3370. TIntermTyped* expression;
  3371. if (! acceptExpression(expression)) {
  3372. expected("case expression");
  3373. return false;
  3374. }
  3375. if (! acceptTokenClass(EHTokColon)) {
  3376. expected(":");
  3377. return false;
  3378. }
  3379. statement = parseContext.intermediate.addBranch(EOpCase, expression, loc);
  3380. return true;
  3381. }
  3382. // default_label
  3383. // : DEFAULT COLON
  3384. //
  3385. bool HlslGrammar::acceptDefaultLabel(TIntermNode*& statement)
  3386. {
  3387. TSourceLoc loc = token.loc;
  3388. if (! acceptTokenClass(EHTokDefault))
  3389. return false;
  3390. if (! acceptTokenClass(EHTokColon)) {
  3391. expected(":");
  3392. return false;
  3393. }
  3394. statement = parseContext.intermediate.addBranch(EOpDefault, loc);
  3395. return true;
  3396. }
  3397. // array_specifier
  3398. // : LEFT_BRACKET integer_expression RGHT_BRACKET ... // optional
  3399. // : LEFT_BRACKET RGHT_BRACKET // optional
  3400. //
  3401. void HlslGrammar::acceptArraySpecifier(TArraySizes*& arraySizes)
  3402. {
  3403. arraySizes = nullptr;
  3404. // Early-out if there aren't any array dimensions
  3405. if (!peekTokenClass(EHTokLeftBracket))
  3406. return;
  3407. // If we get here, we have at least one array dimension. This will track the sizes we find.
  3408. arraySizes = new TArraySizes;
  3409. // Collect each array dimension.
  3410. while (acceptTokenClass(EHTokLeftBracket)) {
  3411. TSourceLoc loc = token.loc;
  3412. TIntermTyped* sizeExpr = nullptr;
  3413. // Array sizing expression is optional. If omitted, array will be later sized by initializer list.
  3414. const bool hasArraySize = acceptAssignmentExpression(sizeExpr);
  3415. if (! acceptTokenClass(EHTokRightBracket)) {
  3416. expected("]");
  3417. return;
  3418. }
  3419. if (hasArraySize) {
  3420. TArraySize arraySize;
  3421. parseContext.arraySizeCheck(loc, sizeExpr, arraySize);
  3422. arraySizes->addInnerSize(arraySize);
  3423. } else {
  3424. arraySizes->addInnerSize(0); // sized by initializers.
  3425. }
  3426. }
  3427. }
  3428. // post_decls
  3429. // : COLON semantic // optional
  3430. // COLON PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN // optional
  3431. // COLON REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN // optional
  3432. // COLON LAYOUT layout_qualifier_list
  3433. // annotations // optional
  3434. //
  3435. // Return true if any tokens were accepted. That is,
  3436. // false can be returned on successfully recognizing nothing,
  3437. // not necessarily meaning bad syntax.
  3438. //
  3439. bool HlslGrammar::acceptPostDecls(TQualifier& qualifier)
  3440. {
  3441. bool found = false;
  3442. do {
  3443. // COLON
  3444. if (acceptTokenClass(EHTokColon)) {
  3445. found = true;
  3446. HlslToken idToken;
  3447. if (peekTokenClass(EHTokLayout))
  3448. acceptLayoutQualifierList(qualifier);
  3449. else if (acceptTokenClass(EHTokPackOffset)) {
  3450. // PACKOFFSET LEFT_PAREN c[Subcomponent][.component] RIGHT_PAREN
  3451. if (! acceptTokenClass(EHTokLeftParen)) {
  3452. expected("(");
  3453. return false;
  3454. }
  3455. HlslToken locationToken;
  3456. if (! acceptIdentifier(locationToken)) {
  3457. expected("c[subcomponent][.component]");
  3458. return false;
  3459. }
  3460. HlslToken componentToken;
  3461. if (acceptTokenClass(EHTokDot)) {
  3462. if (! acceptIdentifier(componentToken)) {
  3463. expected("component");
  3464. return false;
  3465. }
  3466. }
  3467. if (! acceptTokenClass(EHTokRightParen)) {
  3468. expected(")");
  3469. break;
  3470. }
  3471. parseContext.handlePackOffset(locationToken.loc, qualifier, *locationToken.string, componentToken.string);
  3472. } else if (! acceptIdentifier(idToken)) {
  3473. expected("layout, semantic, packoffset, or register");
  3474. return false;
  3475. } else if (*idToken.string == "register") {
  3476. // REGISTER LEFT_PAREN [shader_profile,] Type#[subcomp]opt (COMMA SPACEN)opt RIGHT_PAREN
  3477. // LEFT_PAREN
  3478. if (! acceptTokenClass(EHTokLeftParen)) {
  3479. expected("(");
  3480. return false;
  3481. }
  3482. HlslToken registerDesc; // for Type#
  3483. HlslToken profile;
  3484. if (! acceptIdentifier(registerDesc)) {
  3485. expected("register number description");
  3486. return false;
  3487. }
  3488. if (registerDesc.string->size() > 1 && !isdigit((*registerDesc.string)[1]) &&
  3489. acceptTokenClass(EHTokComma)) {
  3490. // Then we didn't really see the registerDesc yet, it was
  3491. // actually the profile. Adjust...
  3492. profile = registerDesc;
  3493. if (! acceptIdentifier(registerDesc)) {
  3494. expected("register number description");
  3495. return false;
  3496. }
  3497. }
  3498. int subComponent = 0;
  3499. if (acceptTokenClass(EHTokLeftBracket)) {
  3500. // LEFT_BRACKET subcomponent RIGHT_BRACKET
  3501. if (! peekTokenClass(EHTokIntConstant)) {
  3502. expected("literal integer");
  3503. return false;
  3504. }
  3505. subComponent = token.i;
  3506. advanceToken();
  3507. if (! acceptTokenClass(EHTokRightBracket)) {
  3508. expected("]");
  3509. break;
  3510. }
  3511. }
  3512. // (COMMA SPACEN)opt
  3513. HlslToken spaceDesc;
  3514. if (acceptTokenClass(EHTokComma)) {
  3515. if (! acceptIdentifier(spaceDesc)) {
  3516. expected ("space identifier");
  3517. return false;
  3518. }
  3519. }
  3520. // RIGHT_PAREN
  3521. if (! acceptTokenClass(EHTokRightParen)) {
  3522. expected(")");
  3523. break;
  3524. }
  3525. parseContext.handleRegister(registerDesc.loc, qualifier, profile.string, *registerDesc.string, subComponent, spaceDesc.string);
  3526. } else {
  3527. // semantic, in idToken.string
  3528. TString semanticUpperCase = *idToken.string;
  3529. std::transform(semanticUpperCase.begin(), semanticUpperCase.end(), semanticUpperCase.begin(), ::toupper);
  3530. parseContext.handleSemantic(idToken.loc, qualifier, mapSemantic(semanticUpperCase.c_str()), semanticUpperCase);
  3531. }
  3532. } else if (peekTokenClass(EHTokLeftAngle)) {
  3533. found = true;
  3534. acceptAnnotations(qualifier);
  3535. } else
  3536. break;
  3537. } while (true);
  3538. return found;
  3539. }
  3540. //
  3541. // Get the stream of tokens from the scanner, but skip all syntactic/semantic
  3542. // processing.
  3543. //
  3544. bool HlslGrammar::captureBlockTokens(TVector<HlslToken>& tokens)
  3545. {
  3546. if (! peekTokenClass(EHTokLeftBrace))
  3547. return false;
  3548. int braceCount = 0;
  3549. do {
  3550. switch (peek()) {
  3551. case EHTokLeftBrace:
  3552. ++braceCount;
  3553. break;
  3554. case EHTokRightBrace:
  3555. --braceCount;
  3556. break;
  3557. case EHTokNone:
  3558. // End of input before balance { } is bad...
  3559. return false;
  3560. default:
  3561. break;
  3562. }
  3563. tokens.push_back(token);
  3564. advanceToken();
  3565. } while (braceCount > 0);
  3566. return true;
  3567. }
  3568. // Return a string for just the types that can also be declared as an identifier.
  3569. const char* HlslGrammar::getTypeString(EHlslTokenClass tokenClass) const
  3570. {
  3571. switch (tokenClass) {
  3572. case EHTokSample: return "sample";
  3573. case EHTokHalf: return "half";
  3574. case EHTokHalf1x1: return "half1x1";
  3575. case EHTokHalf1x2: return "half1x2";
  3576. case EHTokHalf1x3: return "half1x3";
  3577. case EHTokHalf1x4: return "half1x4";
  3578. case EHTokHalf2x1: return "half2x1";
  3579. case EHTokHalf2x2: return "half2x2";
  3580. case EHTokHalf2x3: return "half2x3";
  3581. case EHTokHalf2x4: return "half2x4";
  3582. case EHTokHalf3x1: return "half3x1";
  3583. case EHTokHalf3x2: return "half3x2";
  3584. case EHTokHalf3x3: return "half3x3";
  3585. case EHTokHalf3x4: return "half3x4";
  3586. case EHTokHalf4x1: return "half4x1";
  3587. case EHTokHalf4x2: return "half4x2";
  3588. case EHTokHalf4x3: return "half4x3";
  3589. case EHTokHalf4x4: return "half4x4";
  3590. case EHTokBool: return "bool";
  3591. case EHTokFloat: return "float";
  3592. case EHTokDouble: return "double";
  3593. case EHTokInt: return "int";
  3594. case EHTokUint: return "uint";
  3595. case EHTokMin16float: return "min16float";
  3596. case EHTokMin10float: return "min10float";
  3597. case EHTokMin16int: return "min16int";
  3598. case EHTokMin12int: return "min12int";
  3599. case EHTokConstantBuffer: return "ConstantBuffer";
  3600. case EHTokLayout: return "layout";
  3601. default:
  3602. return nullptr;
  3603. }
  3604. }
  3605. } // end namespace glslang