hlslGrammar.cpp 128 KB

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