hlslGrammar.cpp 126 KB

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