12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795 |
- //===--- SemaHLSL.cpp - HLSL support for AST nodes and operations ---===//
- ///////////////////////////////////////////////////////////////////////////////
- // //
- // SemaHLSL.cpp //
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- // This file is distributed under the University of Illinois Open Source //
- // License. See LICENSE.TXT for details. //
- // //
- // This file implements the semantic support for HLSL. //
- // //
- ///////////////////////////////////////////////////////////////////////////////
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/Attr.h"
- #include "clang/AST/DeclCXX.h"
- #include "clang/AST/DeclTemplate.h"
- #include "clang/AST/Expr.h"
- #include "clang/AST/ExprCXX.h"
- #include "clang/AST/ExternalASTSource.h"
- #include "clang/AST/TypeLoc.h"
- #include "clang/AST/HlslTypes.h"
- #include "clang/Sema/Overload.h"
- #include "clang/Sema/SemaDiagnostic.h"
- #include "clang/Sema/Initialization.h"
- #include "clang/Sema/ExternalSemaSource.h"
- #include "clang/Sema/Lookup.h"
- #include "clang/Sema/Template.h"
- #include "clang/Sema/TemplateDeduction.h"
- #include "clang/Sema/SemaHLSL.h"
- #include <map>
- #include "dxc/Support/Global.h"
- #include "dxc/Support/WinIncludes.h"
- #include "dxc/dxcapi.internal.h"
- #include "dxc/HlslIntrinsicOp.h"
- #include "gen_intrin_main_tables_15.h"
- #include "dxc/HLSL/HLOperations.h"
- #include <array>
- enum ArBasicKind {
- AR_BASIC_BOOL,
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_FLOAT16,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT64,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_INT8,
- AR_BASIC_UINT8,
- AR_BASIC_INT16,
- AR_BASIC_UINT16,
- AR_BASIC_INT32,
- AR_BASIC_UINT32,
- AR_BASIC_INT64,
- AR_BASIC_UINT64,
- AR_BASIC_MIN10FLOAT,
- AR_BASIC_MIN16FLOAT,
- AR_BASIC_MIN12INT,
- AR_BASIC_MIN16INT,
- AR_BASIC_MIN16UINT,
- AR_BASIC_COUNT,
- //
- // Pseudo-entries for intrinsic tables and such.
- //
- AR_BASIC_NONE,
- AR_BASIC_UNKNOWN,
- AR_BASIC_NOCAST,
- //
- // The following pseudo-entries represent higher-level
- // object types that are treated as units.
- //
- AR_BASIC_POINTER,
- AR_OBJECT_NULL,
- AR_OBJECT_STRING,
- // AR_OBJECT_TEXTURE,
- AR_OBJECT_TEXTURE1D,
- AR_OBJECT_TEXTURE1D_ARRAY,
- AR_OBJECT_TEXTURE2D,
- AR_OBJECT_TEXTURE2D_ARRAY,
- AR_OBJECT_TEXTURE3D,
- AR_OBJECT_TEXTURECUBE,
- AR_OBJECT_TEXTURECUBE_ARRAY,
- AR_OBJECT_TEXTURE2DMS,
- AR_OBJECT_TEXTURE2DMS_ARRAY,
- AR_OBJECT_SAMPLER,
- AR_OBJECT_SAMPLER1D,
- AR_OBJECT_SAMPLER2D,
- AR_OBJECT_SAMPLER3D,
- AR_OBJECT_SAMPLERCUBE,
- AR_OBJECT_SAMPLERCOMPARISON,
- AR_OBJECT_BUFFER,
- //
- // View objects are only used as variable/types within the Effects
- // framework, for example in calls to OMSetRenderTargets.
- //
- AR_OBJECT_RENDERTARGETVIEW,
- AR_OBJECT_DEPTHSTENCILVIEW,
- //
- // Shader objects are only used as variable/types within the Effects
- // framework, for example as a result of CompileShader().
- //
- AR_OBJECT_COMPUTESHADER,
- AR_OBJECT_DOMAINSHADER,
- AR_OBJECT_GEOMETRYSHADER,
- AR_OBJECT_HULLSHADER,
- AR_OBJECT_PIXELSHADER,
- AR_OBJECT_VERTEXSHADER,
- AR_OBJECT_PIXELFRAGMENT,
- AR_OBJECT_VERTEXFRAGMENT,
- AR_OBJECT_STATEBLOCK,
- AR_OBJECT_RASTERIZER,
- AR_OBJECT_DEPTHSTENCIL,
- AR_OBJECT_BLEND,
- AR_OBJECT_POINTSTREAM,
- AR_OBJECT_LINESTREAM,
- AR_OBJECT_TRIANGLESTREAM,
- AR_OBJECT_INPUTPATCH,
- AR_OBJECT_OUTPUTPATCH,
- AR_OBJECT_RWTEXTURE1D,
- AR_OBJECT_RWTEXTURE1D_ARRAY,
- AR_OBJECT_RWTEXTURE2D,
- AR_OBJECT_RWTEXTURE2D_ARRAY,
- AR_OBJECT_RWTEXTURE3D,
- AR_OBJECT_RWBUFFER,
- AR_OBJECT_BYTEADDRESS_BUFFER,
- AR_OBJECT_RWBYTEADDRESS_BUFFER,
- AR_OBJECT_STRUCTURED_BUFFER,
- AR_OBJECT_RWSTRUCTURED_BUFFER,
- AR_OBJECT_RWSTRUCTURED_BUFFER_ALLOC,
- AR_OBJECT_RWSTRUCTURED_BUFFER_CONSUME,
- AR_OBJECT_APPEND_STRUCTURED_BUFFER,
- AR_OBJECT_CONSUME_STRUCTURED_BUFFER,
- AR_OBJECT_CONSTANT_BUFFER,
- AR_OBJECT_TEXTURE_BUFFER,
- AR_OBJECT_ROVBUFFER,
- AR_OBJECT_ROVBYTEADDRESS_BUFFER,
- AR_OBJECT_ROVSTRUCTURED_BUFFER,
- AR_OBJECT_ROVTEXTURE1D,
- AR_OBJECT_ROVTEXTURE1D_ARRAY,
- AR_OBJECT_ROVTEXTURE2D,
- AR_OBJECT_ROVTEXTURE2D_ARRAY,
- AR_OBJECT_ROVTEXTURE3D,
- AR_OBJECT_INNER, // Used for internal type object
- AR_OBJECT_LEGACY_EFFECT,
- AR_OBJECT_WAVE,
- AR_BASIC_MAXIMUM_COUNT
- };
- #define AR_BASIC_TEXTURE_MS_CASES \
- case AR_OBJECT_TEXTURE2DMS: \
- case AR_OBJECT_TEXTURE2DMS_ARRAY
- #define AR_BASIC_NON_TEXTURE_MS_CASES \
- case AR_OBJECT_TEXTURE1D: \
- case AR_OBJECT_TEXTURE1D_ARRAY: \
- case AR_OBJECT_TEXTURE2D: \
- case AR_OBJECT_TEXTURE2D_ARRAY: \
- case AR_OBJECT_TEXTURE3D: \
- case AR_OBJECT_TEXTURECUBE: \
- case AR_OBJECT_TEXTURECUBE_ARRAY
- #define AR_BASIC_TEXTURE_CASES \
- AR_BASIC_TEXTURE_MS_CASES: \
- AR_BASIC_NON_TEXTURE_MS_CASES
- #define AR_BASIC_NON_CMP_SAMPLER_CASES \
- case AR_OBJECT_SAMPLER: \
- case AR_OBJECT_SAMPLER1D: \
- case AR_OBJECT_SAMPLER2D: \
- case AR_OBJECT_SAMPLER3D: \
- case AR_OBJECT_SAMPLERCUBE
- #define AR_BASIC_ROBJECT_CASES \
- case AR_OBJECT_BLEND: \
- case AR_OBJECT_RASTERIZER: \
- case AR_OBJECT_DEPTHSTENCIL: \
- case AR_OBJECT_STATEBLOCK
- //
- // Properties of entries in the ArBasicKind enumeration.
- // These properties are intended to allow easy identification
- // of classes of basic kinds. More specific checks on the
- // actual kind values could then be done.
- //
- // The first four bits are used as a subtype indicator,
- // such as bit count for primitive kinds or specific
- // types for non-primitive-data kinds.
- #define BPROP_SUBTYPE_MASK 0x0000000f
- // Bit counts must be ordered from smaller to larger.
- #define BPROP_BITS0 0x00000000
- #define BPROP_BITS8 0x00000001
- #define BPROP_BITS10 0x00000002
- #define BPROP_BITS12 0x00000003
- #define BPROP_BITS16 0x00000004
- #define BPROP_BITS32 0x00000005
- #define BPROP_BITS64 0x00000006
- #define BPROP_BITS_NON_PRIM 0x00000007
- #define GET_BPROP_SUBTYPE(_Props) ((_Props) & BPROP_SUBTYPE_MASK)
- #define GET_BPROP_BITS(_Props) ((_Props) & BPROP_SUBTYPE_MASK)
- #define BPROP_BOOLEAN 0x00000010 // Whether the type is bool
- #define BPROP_INTEGER 0x00000020 // Whether the type is an integer
- #define BPROP_UNSIGNED 0x00000040 // Whether the type is an unsigned numeric (its absence implies signed)
- #define BPROP_NUMERIC 0x00000080 // Whether the type is numeric or boolean
- #define BPROP_LITERAL 0x00000100 // Whether the type is a literal float or integer
- #define BPROP_FLOATING 0x00000200 // Whether the type is a float
- #define BPROP_OBJECT 0x00000400 // Whether the type is an object (including null or stream)
- #define BPROP_OTHER 0x00000800 // Whether the type is a pseudo-entry in another table.
- #define BPROP_PARTIAL_PRECISION 0x00001000 // Whether the type has partial precision for calculations (i.e., is this 'half')
- #define BPROP_POINTER 0x00002000 // Whether the type is a basic pointer.
- #define BPROP_TEXTURE 0x00004000 // Whether the type is any kind of texture.
- #define BPROP_SAMPLER 0x00008000 // Whether the type is any kind of sampler object.
- #define BPROP_STREAM 0x00010000 // Whether the type is a point, line or triangle stream.
- #define BPROP_PATCH 0x00020000 // Whether the type is an input or output patch.
- #define BPROP_RBUFFER 0x00040000 // Whether the type acts as a read-only buffer.
- #define BPROP_RWBUFFER 0x00080000 // Whether the type acts as a read-write buffer.
- #define BPROP_PRIMITIVE 0x00100000 // Whether the type is a primitive scalar type.
- #define BPROP_MIN_PRECISION 0x00200000 // Whether the type is qualified with a minimum precision.
- #define BPROP_ROVBUFFER 0x00400000 // Whether the type is a ROV object.
- #define GET_BPROP_PRIM_KIND(_Props) \
- ((_Props) & (BPROP_BOOLEAN | BPROP_INTEGER | BPROP_FLOATING))
- #define GET_BPROP_PRIM_KIND_SU(_Props) \
- ((_Props) & (BPROP_BOOLEAN | BPROP_INTEGER | BPROP_FLOATING | BPROP_UNSIGNED))
- #define IS_BPROP_PRIMITIVE(_Props) \
- (((_Props) & BPROP_PRIMITIVE) != 0)
- #define IS_BPROP_BOOL(_Props) \
- (((_Props) & BPROP_BOOLEAN) != 0)
- #define IS_BPROP_FLOAT(_Props) \
- (((_Props) & BPROP_FLOATING) != 0)
- #define IS_BPROP_SINT(_Props) \
- (((_Props) & (BPROP_INTEGER | BPROP_UNSIGNED | BPROP_BOOLEAN)) == \
- BPROP_INTEGER)
- #define IS_BPROP_UINT(_Props) \
- (((_Props) & (BPROP_INTEGER | BPROP_UNSIGNED | BPROP_BOOLEAN)) == \
- (BPROP_INTEGER | BPROP_UNSIGNED))
- #define IS_BPROP_AINT(_Props) \
- (((_Props) & (BPROP_INTEGER | BPROP_BOOLEAN)) == BPROP_INTEGER)
- #define IS_BPROP_STREAM(_Props) \
- (((_Props) & BPROP_STREAM) != 0)
- #define IS_BPROP_SAMPLER(_Props) \
- (((_Props) & BPROP_SAMPLER) != 0)
- #define IS_BPROP_TEXTURE(_Props) \
- (((_Props) & BPROP_TEXTURE) != 0)
- #define IS_BPROP_OBJECT(_Props) \
- (((_Props) & BPROP_OBJECT) != 0)
- #define IS_BPROP_MIN_PRECISION(_Props) \
- (((_Props) & BPROP_MIN_PRECISION) != 0)
- #define IS_BPROP_UNSIGNABLE(_Props) \
- (IS_BPROP_AINT(_Props) && GET_BPROP_BITS(_Props) != BPROP_BITS12)
- const UINT g_uBasicKindProps[] =
- {
- BPROP_PRIMITIVE | BPROP_BOOLEAN | BPROP_INTEGER | BPROP_NUMERIC | BPROP_BITS0, // AR_BASIC_BOOL
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_FLOATING | BPROP_LITERAL | BPROP_BITS0, // AR_BASIC_LITERAL_FLOAT
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_FLOATING | BPROP_BITS16, // AR_BASIC_FLOAT16
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_FLOATING | BPROP_BITS32 | BPROP_PARTIAL_PRECISION, // AR_BASIC_FLOAT32_PARTIAL_PRECISION
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_FLOATING | BPROP_BITS32, // AR_BASIC_FLOAT32
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_FLOATING | BPROP_BITS64, // AR_BASIC_FLOAT64
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_LITERAL | BPROP_BITS0, // AR_BASIC_LITERAL_INT
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_BITS8, // AR_BASIC_INT8
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_UNSIGNED | BPROP_BITS8, // AR_BASIC_UINT8
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_BITS16, // AR_BASIC_INT16
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_UNSIGNED | BPROP_BITS16,// AR_BASIC_UINT16
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_BITS32, // AR_BASIC_INT32
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_UNSIGNED | BPROP_BITS32,// AR_BASIC_UINT32
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_BITS64, // AR_BASIC_INT64
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_UNSIGNED | BPROP_BITS64,// AR_BASIC_UINT64
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_FLOATING | BPROP_BITS10 | BPROP_MIN_PRECISION, // AR_BASIC_MIN10FLOAT
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_FLOATING | BPROP_BITS16 | BPROP_MIN_PRECISION, // AR_BASIC_MIN16FLOAT
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_BITS12 | BPROP_MIN_PRECISION, // AR_BASIC_MIN12INT
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_BITS16 | BPROP_MIN_PRECISION, // AR_BASIC_MIN16INT
- BPROP_PRIMITIVE | BPROP_NUMERIC | BPROP_INTEGER | BPROP_UNSIGNED | BPROP_BITS16 | BPROP_MIN_PRECISION, // AR_BASIC_MIN16UINT
- BPROP_OTHER, // AR_BASIC_COUNT
- //
- // Pseudo-entries for intrinsic tables and such.
- //
- 0, // AR_BASIC_NONE
- BPROP_OTHER, // AR_BASIC_UNKNOWN
- BPROP_OTHER, // AR_BASIC_NOCAST
- //
- // The following pseudo-entries represent higher-level
- // object types that are treated as units.
- //
- BPROP_POINTER, // AR_BASIC_POINTER
- BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_NULL
- BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_STRING
- // BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE1D
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE1D_ARRAY
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE2D
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE2D_ARRAY
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE3D
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURECUBE
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURECUBE_ARRAY
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE2DMS
- BPROP_OBJECT | BPROP_TEXTURE, // AR_OBJECT_TEXTURE2DMS_ARRAY
- BPROP_OBJECT | BPROP_SAMPLER, // AR_OBJECT_SAMPLER
- BPROP_OBJECT | BPROP_SAMPLER, // AR_OBJECT_SAMPLER1D
- BPROP_OBJECT | BPROP_SAMPLER, // AR_OBJECT_SAMPLER2D
- BPROP_OBJECT | BPROP_SAMPLER, // AR_OBJECT_SAMPLER3D
- BPROP_OBJECT | BPROP_SAMPLER, // AR_OBJECT_SAMPLERCUBE
- BPROP_OBJECT | BPROP_SAMPLER, // AR_OBJECT_SAMPLERCOMPARISON
- BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_BUFFER
- BPROP_OBJECT, // AR_OBJECT_RENDERTARGETVIEW
- BPROP_OBJECT, // AR_OBJECT_DEPTHSTENCILVIEW
- BPROP_OBJECT, // AR_OBJECT_COMPUTESHADER
- BPROP_OBJECT, // AR_OBJECT_DOMAINSHADER
- BPROP_OBJECT, // AR_OBJECT_GEOMETRYSHADER
- BPROP_OBJECT, // AR_OBJECT_HULLSHADER
- BPROP_OBJECT, // AR_OBJECT_PIXELSHADER
- BPROP_OBJECT, // AR_OBJECT_VERTEXSHADER
- BPROP_OBJECT, // AR_OBJECT_PIXELFRAGMENT
- BPROP_OBJECT, // AR_OBJECT_VERTEXFRAGMENT
- BPROP_OBJECT, // AR_OBJECT_STATEBLOCK
- BPROP_OBJECT, // AR_OBJECT_RASTERIZER
- BPROP_OBJECT, // AR_OBJECT_DEPTHSTENCIL
- BPROP_OBJECT, // AR_OBJECT_BLEND
- BPROP_OBJECT | BPROP_STREAM, // AR_OBJECT_POINTSTREAM
- BPROP_OBJECT | BPROP_STREAM, // AR_OBJECT_LINESTREAM
- BPROP_OBJECT | BPROP_STREAM, // AR_OBJECT_TRIANGLESTREAM
- BPROP_OBJECT | BPROP_PATCH, // AR_OBJECT_INPUTPATCH
- BPROP_OBJECT | BPROP_PATCH, // AR_OBJECT_OUTPUTPATCH
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWTEXTURE1D
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWTEXTURE1D_ARRAY
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWTEXTURE2D
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWTEXTURE2D_ARRAY
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWTEXTURE3D
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWBUFFER
- BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_BYTEADDRESS_BUFFER
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWBYTEADDRESS_BUFFER
- BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_STRUCTURED_BUFFER
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWSTRUCTURED_BUFFER
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWSTRUCTURED_BUFFER_ALLOC
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_RWSTRUCTURED_BUFFER_CONSUME
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_APPEND_STRUCTURED_BUFFER
- BPROP_OBJECT | BPROP_RWBUFFER, // AR_OBJECT_CONSUME_STRUCTURED_BUFFER
- BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_CONSTANT_BUFFER
- BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_TEXTURE_BUFFER
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVBUFFER
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVBYTEADDRESS_BUFFER
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVSTRUCTURED_BUFFER
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVTEXTURE1D
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVTEXTURE1D_ARRAY
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVTEXTURE2D
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVTEXTURE2D_ARRAY
- BPROP_OBJECT | BPROP_RWBUFFER | BPROP_ROVBUFFER, // AR_OBJECT_ROVTEXTURE3D
- BPROP_OBJECT, // AR_OBJECT_INNER
- BPROP_OBJECT, // AR_OBJECT_LEGACY_EFFECT
- BPROP_OBJECT, // AR_OBJECT_WAVE
- // AR_BASIC_MAXIMUM_COUNT
- };
- C_ASSERT(ARRAYSIZE(g_uBasicKindProps) == AR_BASIC_MAXIMUM_COUNT);
- #define GetBasicKindProps(_Kind) g_uBasicKindProps[(_Kind)]
- #define GET_BASIC_BITS(_Kind) \
- GET_BPROP_BITS(GetBasicKindProps(_Kind))
- #define GET_BASIC_PRIM_KIND(_Kind) \
- GET_BPROP_PRIM_KIND(GetBasicKindProps(_Kind))
- #define GET_BASIC_PRIM_KIND_SU(_Kind) \
- GET_BPROP_PRIM_KIND_SU(GetBasicKindProps(_Kind))
- #define IS_BASIC_PRIMITIVE(_Kind) \
- IS_BPROP_PRIMITIVE(GetBasicKindProps(_Kind))
- #define IS_BASIC_BOOL(_Kind) \
- IS_BPROP_BOOL(GetBasicKindProps(_Kind))
- #define IS_BASIC_FLOAT(_Kind) \
- IS_BPROP_FLOAT(GetBasicKindProps(_Kind))
- #define IS_BASIC_SINT(_Kind) \
- IS_BPROP_SINT(GetBasicKindProps(_Kind))
- #define IS_BASIC_UINT(_Kind) \
- IS_BPROP_UINT(GetBasicKindProps(_Kind))
- #define IS_BASIC_AINT(_Kind) \
- IS_BPROP_AINT(GetBasicKindProps(_Kind))
- #define IS_BASIC_STREAM(_Kind) \
- IS_BPROP_STREAM(GetBasicKindProps(_Kind))
- #define IS_BASIC_SAMPLER(_Kind) \
- IS_BPROP_SAMPLER(GetBasicKindProps(_Kind))
- #define IS_BASIC_TEXTURE(_Kind) \
- IS_BPROP_TEXTURE(GetBasicKindProps(_Kind))
- #define IS_BASIC_OBJECT(_Kind) \
- IS_BPROP_OBJECT(GetBasicKindProps(_Kind))
- #define IS_BASIC_MIN_PRECISION(_Kind) \
- IS_BPROP_MIN_PRECISION(GetBasicKindProps(_Kind))
- #define IS_BASIC_UNSIGNABLE(_Kind) \
- IS_BPROP_UNSIGNABLE(GetBasicKindProps(_Kind))
- #define BITWISE_ENUM_OPS(_Type) \
- inline _Type operator|(_Type F1, _Type F2) \
- { \
- return (_Type)((UINT)F1 | (UINT)F2); \
- } \
- inline _Type operator&(_Type F1, _Type F2) \
- { \
- return (_Type)((UINT)F1 & (UINT)F2); \
- } \
- inline _Type& operator|=(_Type& F1, _Type F2) \
- { \
- F1 = F1 | F2; \
- return F1; \
- } \
- inline _Type& operator&=(_Type& F1, _Type F2) \
- { \
- F1 = F1 & F2; \
- return F1; \
- } \
- inline _Type& operator&=(_Type& F1, UINT F2) \
- { \
- F1 = (_Type)((UINT)F1 & F2); \
- return F1; \
- }
- enum ArTypeObjectKind {
- AR_TOBJ_INVALID, // Flag for an unassigned / unavailable object type.
- AR_TOBJ_VOID, // Represents the type for functions with not returned valued.
- AR_TOBJ_BASIC, // Represents a primitive type.
- AR_TOBJ_COMPOUND, // Represents a struct or class.
- AR_TOBJ_INTERFACE, // Represents an interface.
- AR_TOBJ_POINTER, // Represents a pointer to another type.
- AR_TOBJ_OBJECT, // Represents a built-in object.
- AR_TOBJ_ARRAY, // Represents an array of other types.
- AR_TOBJ_MATRIX, // Represents a matrix of basic types.
- AR_TOBJ_VECTOR, // Represents a vector of basic types.
- AR_TOBJ_QUALIFIER, // Represents another type plus an ArTypeQualifier.
- AR_TOBJ_INNER_OBJ, // Represents a built-in inner object, such as an
- // indexer object used to implement .mips[1].
- };
- enum TYPE_CONVERSION_FLAGS
- {
- TYPE_CONVERSION_DEFAULT = 0x00000000, // Indicates an implicit conversion is done.
- TYPE_CONVERSION_EXPLICIT = 0x00000001, // Indicates a conversion is done through an explicit cast.
- TYPE_CONVERSION_BY_REFERENCE = 0x00000002, // Indicates a conversion is done to an output parameter.
- };
- enum TYPE_CONVERSION_REMARKS
- {
- TYPE_CONVERSION_NONE = 0x00000000,
- TYPE_CONVERSION_PRECISION_LOSS = 0x00000001,
- TYPE_CONVERSION_IDENTICAL = 0x00000002,
- TYPE_CONVERSION_TO_VOID = 0x00000004,
- TYPE_CONVERSION_ELT_TRUNCATION = 0x00000008,
- };
- BITWISE_ENUM_OPS(TYPE_CONVERSION_REMARKS)
- #define AR_TOBJ_SCALAR AR_TOBJ_BASIC
- #define AR_TOBJ_UNKNOWN AR_TOBJ_INVALID
- #define AR_TPROP_VOID 0x0000000000000001
- #define AR_TPROP_CONST 0x0000000000000002
- #define AR_TPROP_IMP_CONST 0x0000000000000004
- #define AR_TPROP_OBJECT 0x0000000000000008
- #define AR_TPROP_SCALAR 0x0000000000000010
- #define AR_TPROP_UNSIGNED 0x0000000000000020
- #define AR_TPROP_NUMERIC 0x0000000000000040
- #define AR_TPROP_INTEGRAL 0x0000000000000080
- #define AR_TPROP_FLOATING 0x0000000000000100
- #define AR_TPROP_LITERAL 0x0000000000000200
- #define AR_TPROP_POINTER 0x0000000000000400
- #define AR_TPROP_INPUT_PATCH 0x0000000000000800
- #define AR_TPROP_OUTPUT_PATCH 0x0000000000001000
- #define AR_TPROP_INH_IFACE 0x0000000000002000
- #define AR_TPROP_HAS_COMPOUND 0x0000000000004000
- #define AR_TPROP_HAS_TEXTURES 0x0000000000008000
- #define AR_TPROP_HAS_SAMPLERS 0x0000000000010000
- #define AR_TPROP_HAS_SAMPLER_CMPS 0x0000000000020000
- #define AR_TPROP_HAS_STREAMS 0x0000000000040000
- #define AR_TPROP_HAS_OTHER_OBJECTS 0x0000000000080000
- #define AR_TPROP_HAS_BASIC 0x0000000000100000
- #define AR_TPROP_HAS_BUFFERS 0x0000000000200000
- #define AR_TPROP_HAS_ROBJECTS 0x0000000000400000
- #define AR_TPROP_HAS_POINTERS 0x0000000000800000
- #define AR_TPROP_INDEXABLE 0x0000000001000000
- #define AR_TPROP_HAS_MIPS 0x0000000002000000
- #define AR_TPROP_WRITABLE_GLOBAL 0x0000000004000000
- #define AR_TPROP_HAS_UAVS 0x0000000008000000
- #define AR_TPROP_HAS_BYTEADDRESS 0x0000000010000000
- #define AR_TPROP_HAS_STRUCTURED 0x0000000020000000
- #define AR_TPROP_HAS_SAMPLE 0x0000000040000000
- #define AR_TPROP_MIN_PRECISION 0x0000000080000000
- #define AR_TPROP_HAS_CBUFFERS 0x0000000100008000
- #define AR_TPROP_HAS_TBUFFERS 0x0000000200008000
- #define AR_TPROP_ALL 0xffffffffffffffff
- #define AR_TPROP_HAS_OBJECTS \
- (AR_TPROP_HAS_TEXTURES | AR_TPROP_HAS_SAMPLERS | \
- AR_TPROP_HAS_SAMPLER_CMPS | AR_TPROP_HAS_STREAMS | \
- AR_TPROP_HAS_OTHER_OBJECTS | AR_TPROP_HAS_BUFFERS | \
- AR_TPROP_HAS_ROBJECTS | AR_TPROP_HAS_UAVS | \
- AR_TPROP_HAS_BYTEADDRESS | AR_TPROP_HAS_STRUCTURED)
- #define AR_TPROP_HAS_BASIC_RESOURCES \
- (AR_TPROP_HAS_TEXTURES | AR_TPROP_HAS_SAMPLERS | \
- AR_TPROP_HAS_SAMPLER_CMPS | AR_TPROP_HAS_BUFFERS | \
- AR_TPROP_HAS_UAVS)
- #define AR_TPROP_UNION_BITS \
- (AR_TPROP_INH_IFACE | AR_TPROP_HAS_COMPOUND | AR_TPROP_HAS_TEXTURES | \
- AR_TPROP_HAS_SAMPLERS | AR_TPROP_HAS_SAMPLER_CMPS | \
- AR_TPROP_HAS_STREAMS | AR_TPROP_HAS_OTHER_OBJECTS | AR_TPROP_HAS_BASIC | \
- AR_TPROP_HAS_BUFFERS | AR_TPROP_HAS_ROBJECTS | AR_TPROP_HAS_POINTERS | \
- AR_TPROP_WRITABLE_GLOBAL | AR_TPROP_HAS_UAVS | \
- AR_TPROP_HAS_BYTEADDRESS | AR_TPROP_HAS_STRUCTURED | AR_TPROP_MIN_PRECISION)
- #define AR_TINFO_ALLOW_COMPLEX 0x00000001
- #define AR_TINFO_ALLOW_OBJECTS 0x00000002
- #define AR_TINFO_IGNORE_QUALIFIERS 0x00000004
- #define AR_TINFO_OBJECTS_AS_ELEMENTS 0x00000008
- #define AR_TINFO_PACK_SCALAR 0x00000010
- #define AR_TINFO_PACK_ROW_MAJOR 0x00000020
- #define AR_TINFO_PACK_TEMP_ARRAY 0x00000040
- #define AR_TINFO_ALL_VAR_INFO 0x00000080
- #define AR_TINFO_ALLOW_ALL (AR_TINFO_ALLOW_COMPLEX | AR_TINFO_ALLOW_OBJECTS)
- #define AR_TINFO_PACK_CBUFFER 0
- #define AR_TINFO_LAYOUT_PACK_ALL (AR_TINFO_PACK_SCALAR | AR_TINFO_PACK_TEMP_ARRAY)
- #define AR_TINFO_SIMPLE_OBJECTS \
- (AR_TINFO_ALLOW_OBJECTS | AR_TINFO_OBJECTS_AS_ELEMENTS)
- struct ArTypeInfo {
- ArTypeObjectKind ShapeKind; // The shape of the type (basic, matrix, etc.)
- ArBasicKind EltKind; // The primitive type of elements in this type.
- ArBasicKind ObjKind; // The object type for this type (textures, buffers, etc.)
- UINT uRows;
- UINT uCols;
- UINT uTotalElts;
- };
- using namespace clang;
- using namespace clang::sema;
- using namespace hlsl;
- static const int FirstTemplateDepth = 0;
- static const int FirstParamPosition = 0;
- static const bool ExplicitConversionFalse = false;// a conversion operation is not the result of an explicit cast
- static const bool InheritedFalse = false; // template parameter default value is not inherited.
- static const bool ParameterPackFalse = false; // template parameter is not an ellipsis.
- static const bool TypenameTrue = false; // 'typename' specified rather than 'class' for a template argument.
- static const bool DelayTypeCreationTrue = true; // delay type creation for a declaration
- static const bool DelayTypeCreationFalse = false; // immediately create a type when the declaration is created
- static const unsigned int NoQuals = 0; // no qualifiers in effect
- static const SourceLocation NoLoc; // no source location attribution available
- static const SourceRange NoRange; // no source range attribution available
- static const bool HasWrittenPrototypeTrue = true; // function had the prototype written
- static const bool InlineSpecifiedFalse = false; // function was not specified as inline
- static const bool IsConstexprFalse = false; // function is not constexpr
- static const bool ListInitializationFalse = false;// not performing a list initialization
- static const bool SuppressWarningsFalse = false; // do not suppress warning diagnostics
- static const bool SuppressWarningsTrue = true; // suppress warning diagnostics
- static const bool SuppressErrorsFalse = false; // do not suppress error diagnostics
- static const bool SuppressErrorsTrue = true; // suppress error diagnostics
- static const int OneRow = 1; // a single row for a type
- static const bool MipsFalse = false; // a type does not support the .mips member
- static const bool MipsTrue = true; // a type supports the .mips member
- static const bool SampleFalse = false; // a type does not support the .sample member
- static const bool SampleTrue = true; // a type supports the .sample member
- static const size_t MaxVectorSize = 4; // maximum size for a vector
- static
- QualType GetOrCreateTemplateSpecialization(
- ASTContext& context,
- Sema& sema,
- _In_ ClassTemplateDecl* templateDecl,
- ArrayRef<TemplateArgument> templateArgs
- )
- {
- DXASSERT_NOMSG(templateDecl);
- DeclContext* currentDeclContext = context.getTranslationUnitDecl();
- SmallVector<TemplateArgument, 3> templateArgsForDecl;
- for (const TemplateArgument& Arg : templateArgs) {
- if (Arg.getKind() == TemplateArgument::Type) {
- // the class template need to use CanonicalType
- templateArgsForDecl.emplace_back(TemplateArgument(Arg.getAsType().getCanonicalType()));
- }else
- templateArgsForDecl.emplace_back(Arg);
- }
- // First, try looking up existing specialization
- void* InsertPos = nullptr;
- ClassTemplateSpecializationDecl* specializationDecl =
- templateDecl->findSpecialization(templateArgsForDecl, InsertPos);
- if (specializationDecl) {
- // Instantiate the class template if not yet.
- if (specializationDecl->getInstantiatedFrom().isNull()) {
- // InstantiateClassTemplateSpecialization returns true if it finds an
- // error.
- DXVERIFY_NOMSG(false ==
- sema.InstantiateClassTemplateSpecialization(
- NoLoc, specializationDecl,
- TemplateSpecializationKind::TSK_ImplicitInstantiation,
- true));
- }
- return context.getTemplateSpecializationType(
- TemplateName(templateDecl), templateArgs.data(), templateArgs.size(),
- context.getTypeDeclType(specializationDecl));
- }
- specializationDecl = ClassTemplateSpecializationDecl::Create(
- context, TagDecl::TagKind::TTK_Class, currentDeclContext, NoLoc, NoLoc,
- templateDecl, templateArgsForDecl.data(), templateArgsForDecl.size(), nullptr);
- // InstantiateClassTemplateSpecialization returns true if it finds an error.
- DXVERIFY_NOMSG(false == sema.InstantiateClassTemplateSpecialization(
- NoLoc, specializationDecl, TemplateSpecializationKind::TSK_ImplicitInstantiation, true));
- templateDecl->AddSpecialization(specializationDecl, InsertPos);
- specializationDecl->setImplicit(true);
- QualType canonType = context.getTypeDeclType(specializationDecl);
- DXASSERT(isa<RecordType>(canonType), "type of non-dependent specialization is not a RecordType");
- TemplateArgumentListInfo templateArgumentList(NoLoc, NoLoc);
- TemplateArgumentLocInfo NoTemplateArgumentLocInfo;
- for (unsigned i = 0; i < templateArgs.size(); i++) {
- templateArgumentList.addArgument(TemplateArgumentLoc(templateArgs[i], NoTemplateArgumentLocInfo));
- }
- return context.getTemplateSpecializationType(
- TemplateName(templateDecl), templateArgumentList, canonType);
- }
- /// <summary>Instantiates a new matrix type specialization or gets an existing one from the AST.</summary>
- static
- QualType GetOrCreateMatrixSpecialization(ASTContext& context, Sema* sema,
- _In_ ClassTemplateDecl* matrixTemplateDecl,
- QualType elementType, uint64_t rowCount, uint64_t colCount)
- {
- DXASSERT_NOMSG(sema);
- TemplateArgument templateArgs[3] = {
- TemplateArgument(elementType),
- TemplateArgument(
- context,
- llvm::APSInt(
- llvm::APInt(context.getIntWidth(context.IntTy), rowCount), false),
- context.IntTy),
- TemplateArgument(
- context,
- llvm::APSInt(
- llvm::APInt(context.getIntWidth(context.IntTy), colCount), false),
- context.IntTy)};
- QualType matrixSpecializationType = GetOrCreateTemplateSpecialization(context, *sema, matrixTemplateDecl, ArrayRef<TemplateArgument>(templateArgs));
- #ifdef DBG
- // Verify that we can read the field member from the template record.
- DXASSERT(matrixSpecializationType->getAsCXXRecordDecl(),
- "type of non-dependent specialization is not a RecordType");
- DeclContext::lookup_result lookupResult = matrixSpecializationType->getAsCXXRecordDecl()->
- lookup(DeclarationName(&context.Idents.get(StringRef("h"))));
- DXASSERT(!lookupResult.empty(), "otherwise matrix handle cannot be looked up");
- #endif
- return matrixSpecializationType;
- }
- /// <summary>Instantiates a new vector type specialization or gets an existing one from the AST.</summary>
- static
- QualType GetOrCreateVectorSpecialization(ASTContext& context, Sema* sema,
- _In_ ClassTemplateDecl* vectorTemplateDecl,
- QualType elementType, uint64_t colCount)
- {
- DXASSERT_NOMSG(sema);
- DXASSERT_NOMSG(vectorTemplateDecl);
- TemplateArgument templateArgs[2] = {
- TemplateArgument(elementType),
- TemplateArgument(
- context,
- llvm::APSInt(
- llvm::APInt(context.getIntWidth(context.IntTy), colCount), false),
- context.IntTy)};
- QualType vectorSpecializationType = GetOrCreateTemplateSpecialization(context, *sema, vectorTemplateDecl, ArrayRef<TemplateArgument>(templateArgs));
- #ifdef DBG
- // Verify that we can read the field member from the template record.
- DXASSERT(vectorSpecializationType->getAsCXXRecordDecl(),
- "type of non-dependent specialization is not a RecordType");
- DeclContext::lookup_result lookupResult = vectorSpecializationType->getAsCXXRecordDecl()->
- lookup(DeclarationName(&context.Idents.get(StringRef("h"))));
- DXASSERT(!lookupResult.empty(), "otherwise vector handle cannot be looked up");
- #endif
- return vectorSpecializationType;
- }
- // Decls.cpp constants start here - these should be refactored or, better, replaced with clang::Type-based constructs.
- static const LPCSTR kBuiltinIntrinsicTableName = "op";
- static const unsigned kAtomicDstOperandIdx = 1;
- static const ArTypeObjectKind g_ScalarTT[] =
- {
- AR_TOBJ_SCALAR,
- AR_TOBJ_UNKNOWN
- };
- static const ArTypeObjectKind g_VectorTT[] =
- {
- AR_TOBJ_VECTOR,
- AR_TOBJ_UNKNOWN
- };
- static const ArTypeObjectKind g_MatrixTT[] =
- {
- AR_TOBJ_MATRIX,
- AR_TOBJ_UNKNOWN
- };
- static const ArTypeObjectKind g_AnyTT[] =
- {
- AR_TOBJ_SCALAR,
- AR_TOBJ_VECTOR,
- AR_TOBJ_MATRIX,
- AR_TOBJ_UNKNOWN
- };
- static const ArTypeObjectKind g_ObjectTT[] =
- {
- AR_TOBJ_OBJECT,
- AR_TOBJ_UNKNOWN
- };
- static const ArTypeObjectKind g_NullTT[] =
- {
- AR_TOBJ_VOID,
- AR_TOBJ_UNKNOWN
- };
- const ArTypeObjectKind* g_LegalIntrinsicTemplates[] =
- {
- g_NullTT,
- g_ScalarTT,
- g_VectorTT,
- g_MatrixTT,
- g_AnyTT,
- g_ObjectTT,
- };
- C_ASSERT(ARRAYSIZE(g_LegalIntrinsicTemplates) == LITEMPLATE_COUNT);
- //
- // The first one is used to name the representative group, so make
- // sure its name will make sense in error messages.
- //
- static const ArBasicKind g_BoolCT[] =
- {
- AR_BASIC_BOOL,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_IntCT[] =
- {
- AR_BASIC_INT32,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_UIntCT[] =
- {
- AR_BASIC_UINT32,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_AnyIntCT[] =
- {
- AR_BASIC_INT32,
- AR_BASIC_UINT32,
- AR_BASIC_INT64,
- AR_BASIC_UINT64,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_AnyInt32CT[] =
- {
- AR_BASIC_INT32,
- AR_BASIC_UINT32,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_UIntOnlyCT[] =
- {
- AR_BASIC_UINT32,
- AR_BASIC_UINT64,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_NOCAST,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_FloatCT[] =
- {
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_AnyFloatCT[] =
- {
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- // TODO - AR_BASIC_FLOAT16,
- AR_BASIC_FLOAT64,
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_MIN10FLOAT,
- AR_BASIC_MIN16FLOAT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_FloatLikeCT[] =
- {
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_MIN10FLOAT,
- AR_BASIC_MIN16FLOAT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_FloatDoubleCT[] =
- {
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- AR_BASIC_FLOAT64,
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_DoubleCT[] =
- {
- AR_BASIC_FLOAT64,
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_DoubleOnlyCT[] =
- {
- AR_BASIC_FLOAT64,
- AR_BASIC_NOCAST,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_NumericCT[] =
- {
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- // TODO - AR_BASIC_FLOAT16,
- AR_BASIC_FLOAT64,
- AR_BASIC_MIN10FLOAT,
- AR_BASIC_MIN16FLOAT,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_INT32,
- AR_BASIC_UINT32,
- AR_BASIC_MIN12INT,
- AR_BASIC_MIN16INT,
- AR_BASIC_MIN16UINT,
- AR_BASIC_INT64,
- AR_BASIC_UINT64,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_Numeric32CT[] =
- {
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_INT32,
- AR_BASIC_UINT32,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_Numeric32OnlyCT[] =
- {
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_INT32,
- AR_BASIC_UINT32,
- AR_BASIC_NOCAST,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_AnyCT[] =
- {
- AR_BASIC_LITERAL_FLOAT,
- AR_BASIC_FLOAT32,
- AR_BASIC_FLOAT32_PARTIAL_PRECISION,
- // TODO - AR_BASIC_FLOAT16,
- AR_BASIC_FLOAT64,
- AR_BASIC_MIN10FLOAT,
- AR_BASIC_MIN16FLOAT,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_INT32,
- AR_BASIC_UINT32,
- AR_BASIC_MIN12INT,
- AR_BASIC_MIN16INT,
- AR_BASIC_MIN16UINT,
- AR_BASIC_BOOL,
- AR_BASIC_INT64,
- AR_BASIC_UINT64,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_Sampler1DCT[] =
- {
- AR_OBJECT_SAMPLER1D,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_Sampler2DCT[] =
- {
- AR_OBJECT_SAMPLER2D,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_Sampler3DCT[] =
- {
- AR_OBJECT_SAMPLER3D,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_SamplerCUBECT[] =
- {
- AR_OBJECT_SAMPLERCUBE,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_SamplerCmpCT[] =
- {
- AR_OBJECT_SAMPLERCOMPARISON,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_SamplerCT[] =
- {
- AR_OBJECT_SAMPLER,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_StringCT[] =
- {
- AR_OBJECT_STRING,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_NullCT[] =
- {
- AR_OBJECT_NULL,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_WaveCT[] =
- {
- AR_OBJECT_WAVE,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_UInt64CT[] =
- {
- AR_BASIC_UINT64,
- AR_BASIC_UNKNOWN
- };
- static const ArBasicKind g_UInt3264CT[] =
- {
- AR_BASIC_UINT32,
- AR_BASIC_UINT64,
- AR_BASIC_LITERAL_INT,
- AR_BASIC_UNKNOWN
- };
- // Basic kinds, indexed by a LEGAL_INTRINSIC_COMPTYPES value.
- const ArBasicKind* g_LegalIntrinsicCompTypes[] =
- {
- g_NullCT, // LICOMPTYPE_VOID
- g_BoolCT, // LICOMPTYPE_BOOL
- g_IntCT, // LICOMPTYPE_INT
- g_UIntCT, // LICOMPTYPE_UINT
- g_AnyIntCT, // LICOMPTYPE_ANY_INT
- g_AnyInt32CT, // LICOMPTYPE_ANY_INT32
- g_UIntOnlyCT, // LICOMPTYPE_UINT_ONLY
- g_FloatCT, // LICOMPTYPE_FLOAT
- g_AnyFloatCT, // LICOMPTYPE_ANY_FLOAT
- g_FloatLikeCT, // LICOMPTYPE_FLOAT_LIKE
- g_FloatDoubleCT, // LICOMPTYPE_FLOAT_DOUBLE
- g_DoubleCT, // LICOMPTYPE_DOUBLE
- g_DoubleOnlyCT, // LICOMPTYPE_DOUBLE_ONLY
- g_NumericCT, // LICOMPTYPE_NUMERIC
- g_Numeric32CT, // LICOMPTYPE_NUMERIC32
- g_Numeric32OnlyCT, // LICOMPTYPE_NUMERIC32_ONLY
- g_AnyCT, // LICOMPTYPE_ANY
- g_Sampler1DCT, // LICOMPTYPE_SAMPLER1D
- g_Sampler2DCT, // LICOMPTYPE_SAMPLER2D
- g_Sampler3DCT, // LICOMPTYPE_SAMPLER3D
- g_SamplerCUBECT, // LICOMPTYPE_SAMPLERCUBE
- g_SamplerCmpCT, // LICOMPTYPE_SAMPLERCMP
- g_SamplerCT, // LICOMPTYPE_SAMPLER
- g_StringCT, // LICOMPTYPE_STRING
- g_WaveCT, // LICOMPTYPE_WAVE
- g_UInt64CT, // LICOMPTYPE_UINT64
- g_UInt3264CT // LICOMPTYPE_UINT32_64
- };
- C_ASSERT(ARRAYSIZE(g_LegalIntrinsicCompTypes) == LICOMPTYPE_COUNT);
- // Decls.cpp constants ends here - these should be refactored or, better, replaced with clang::Type-based constructs.
- // Basic kind objects that are represented as HLSL structures or templates.
- static
- const ArBasicKind g_ArBasicKindsAsTypes[] =
- {
- AR_OBJECT_BUFFER, // Buffer
- // AR_OBJECT_TEXTURE,
- AR_OBJECT_TEXTURE1D, // Texture1D
- AR_OBJECT_TEXTURE1D_ARRAY, // Texture1DArray
- AR_OBJECT_TEXTURE2D, // Texture2D
- AR_OBJECT_TEXTURE2D_ARRAY, // Texture2DArray
- AR_OBJECT_TEXTURE3D, // Texture3D
- AR_OBJECT_TEXTURECUBE, // TextureCube
- AR_OBJECT_TEXTURECUBE_ARRAY, // TextureCubeArray
- AR_OBJECT_TEXTURE2DMS, // Texture2DMS
- AR_OBJECT_TEXTURE2DMS_ARRAY, // Texture2DMSArray
- AR_OBJECT_SAMPLER,
- //AR_OBJECT_SAMPLER1D,
- //AR_OBJECT_SAMPLER2D,
- //AR_OBJECT_SAMPLER3D,
- //AR_OBJECT_SAMPLERCUBE,
- AR_OBJECT_SAMPLERCOMPARISON,
- AR_OBJECT_POINTSTREAM,
- AR_OBJECT_LINESTREAM,
- AR_OBJECT_TRIANGLESTREAM,
- AR_OBJECT_INPUTPATCH,
- AR_OBJECT_OUTPUTPATCH,
- AR_OBJECT_RWTEXTURE1D,
- AR_OBJECT_RWTEXTURE1D_ARRAY,
- AR_OBJECT_RWTEXTURE2D,
- AR_OBJECT_RWTEXTURE2D_ARRAY,
- AR_OBJECT_RWTEXTURE3D,
- AR_OBJECT_RWBUFFER,
- AR_OBJECT_BYTEADDRESS_BUFFER,
- AR_OBJECT_RWBYTEADDRESS_BUFFER,
- AR_OBJECT_STRUCTURED_BUFFER,
- AR_OBJECT_RWSTRUCTURED_BUFFER,
- // AR_OBJECT_RWSTRUCTURED_BUFFER_ALLOC,
- // AR_OBJECT_RWSTRUCTURED_BUFFER_CONSUME,
- AR_OBJECT_APPEND_STRUCTURED_BUFFER,
- AR_OBJECT_CONSUME_STRUCTURED_BUFFER,
- AR_OBJECT_ROVBUFFER,
- AR_OBJECT_ROVBYTEADDRESS_BUFFER,
- AR_OBJECT_ROVSTRUCTURED_BUFFER,
- AR_OBJECT_ROVTEXTURE1D,
- AR_OBJECT_ROVTEXTURE1D_ARRAY,
- AR_OBJECT_ROVTEXTURE2D,
- AR_OBJECT_ROVTEXTURE2D_ARRAY,
- AR_OBJECT_ROVTEXTURE3D,
- AR_OBJECT_LEGACY_EFFECT, // Used for all unsupported but ignored legacy effect types
- AR_OBJECT_WAVE
- };
- // Count of template arguments for basic kind of objects that look like templates (one or more type arguments).
- static
- const uint8_t g_ArBasicKindsTemplateCount[] =
- {
- 1, // AR_OBJECT_BUFFER
- // AR_OBJECT_TEXTURE,
- 1, // AR_OBJECT_TEXTURE1D
- 1, // AR_OBJECT_TEXTURE1D_ARRAY
- 1, // AR_OBJECT_TEXTURE2D
- 1, // AR_OBJECT_TEXTURE2D_ARRAY
- 1, // AR_OBJECT_TEXTURE3D
- 1, // AR_OBJECT_TEXTURECUBE
- 1, // AR_OBJECT_TEXTURECUBE_ARRAY
- 2, // AR_OBJECT_TEXTURE2DMS
- 2, // AR_OBJECT_TEXTURE2DMS_ARRAY
- 0, // AR_OBJECT_SAMPLER
- //AR_OBJECT_SAMPLER1D,
- //AR_OBJECT_SAMPLER2D,
- //AR_OBJECT_SAMPLER3D,
- //AR_OBJECT_SAMPLERCUBE,
- 0, // AR_OBJECT_SAMPLERCOMPARISON
- 1, // AR_OBJECT_POINTSTREAM
- 1, // AR_OBJECT_LINESTREAM
- 1, // AR_OBJECT_TRIANGLESTREAM
- 2, // AR_OBJECT_INPUTPATCH
- 2, // AR_OBJECT_OUTPUTPATCH
- 1, // AR_OBJECT_RWTEXTURE1D
- 1, // AR_OBJECT_RWTEXTURE1D_ARRAY
- 1, // AR_OBJECT_RWTEXTURE2D
- 1, // AR_OBJECT_RWTEXTURE2D_ARRAY
- 1, // AR_OBJECT_RWTEXTURE3D
- 1, // AR_OBJECT_RWBUFFER
- 0, // AR_OBJECT_BYTEADDRESS_BUFFER
- 0, // AR_OBJECT_RWBYTEADDRESS_BUFFER
- 1, // AR_OBJECT_STRUCTURED_BUFFER
- 1, // AR_OBJECT_RWSTRUCTURED_BUFFER
- // 1, // AR_OBJECT_RWSTRUCTURED_BUFFER_ALLOC
- // 1, // AR_OBJECT_RWSTRUCTURED_BUFFER_CONSUME
- 1, // AR_OBJECT_APPEND_STRUCTURED_BUFFER
- 1, // AR_OBJECT_CONSUME_STRUCTURED_BUFFER
- 1, // AR_OBJECT_ROVBUFFER
- 0, // AR_OBJECT_ROVBYTEADDRESS_BUFFER
- 1, // AR_OBJECT_ROVSTRUCTURED_BUFFER
- 1, // AR_OBJECT_ROVTEXTURE1D
- 1, // AR_OBJECT_ROVTEXTURE1D_ARRAY
- 1, // AR_OBJECT_ROVTEXTURE2D
- 1, // AR_OBJECT_ROVTEXTURE2D_ARRAY
- 1, // AR_OBJECT_ROVTEXTURE3D
- 0, // AR_OBJECT_LEGACY_EFFECT // Used for all unsupported but ignored legacy effect types
- 0, // AR_OBJECT_WAVE
- };
- C_ASSERT(_countof(g_ArBasicKindsAsTypes) == _countof(g_ArBasicKindsTemplateCount));
- /// <summary>Describes the how the subscript or indexing operators work on a given type.</summary>
- struct SubscriptOperatorRecord
- {
- unsigned int SubscriptCardinality : 4; // Number of elements expected in subscript - zero if operator not supported.
- bool HasMips : 1; // true if the kind has a mips member; false otherwise
- bool HasSample : 1; // true if the kind has a sample member; false otherwise
- };
- // Subscript operators for objects that are represented as HLSL structures or templates.
- static
- const SubscriptOperatorRecord g_ArBasicKindsSubscripts[] =
- {
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_BUFFER (Buffer)
- // AR_OBJECT_TEXTURE,
- { 1, MipsTrue, SampleFalse }, // AR_OBJECT_TEXTURE1D (Texture1D)
- { 2, MipsTrue, SampleFalse }, // AR_OBJECT_TEXTURE1D_ARRAY (Texture1DArray)
- { 2, MipsTrue, SampleFalse }, // AR_OBJECT_TEXTURE2D (Texture2D)
- { 3, MipsTrue, SampleFalse }, // AR_OBJECT_TEXTURE2D_ARRAY (Texture2DArray)
- { 3, MipsTrue, SampleFalse }, // AR_OBJECT_TEXTURE3D (Texture3D)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_TEXTURECUBE (TextureCube)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_TEXTURECUBE_ARRAY (TextureCubeArray)
- { 2, MipsFalse, SampleTrue }, // AR_OBJECT_TEXTURE2DMS (Texture2DMS)
- { 3, MipsFalse, SampleTrue }, // AR_OBJECT_TEXTURE2DMS_ARRAY (Texture2DMSArray)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_SAMPLER (SamplerState)
- //AR_OBJECT_SAMPLER1D,
- //AR_OBJECT_SAMPLER2D,
- //AR_OBJECT_SAMPLER3D,
- //AR_OBJECT_SAMPLERCUBE,
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_SAMPLERCOMPARISON (SamplerComparison)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_POINTSTREAM (PointStream)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_LINESTREAM (LineStream)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_TRIANGLESTREAM (TriangleStream)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_INPUTPATCH (InputPatch)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_OUTPUTPATCH (OutputPatch)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_RWTEXTURE1D (RWTexture1D)
- { 2, MipsFalse, SampleFalse }, // AR_OBJECT_RWTEXTURE1D_ARRAY (RWTexture1DArray)
- { 2, MipsFalse, SampleFalse }, // AR_OBJECT_RWTEXTURE2D (RWTexture2D)
- { 3, MipsFalse, SampleFalse }, // AR_OBJECT_RWTEXTURE2D_ARRAY (RWTexture2DArray)
- { 3, MipsFalse, SampleFalse }, // AR_OBJECT_RWTEXTURE3D (RWTexture3D)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_RWBUFFER (RWBuffer)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_BYTEADDRESS_BUFFER (ByteAddressBuffer)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_RWBYTEADDRESS_BUFFER (RWByteAddressBuffer)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_STRUCTURED_BUFFER (StructuredBuffer)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_RWSTRUCTURED_BUFFER (RWStructuredBuffer)
- // AR_OBJECT_RWSTRUCTURED_BUFFER_ALLOC,
- // AR_OBJECT_RWSTRUCTURED_BUFFER_CONSUME,
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_APPEND_STRUCTURED_BUFFER (AppendStructuredBuffer)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_CONSUME_STRUCTURED_BUFFER (ConsumeStructuredBuffer)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_ROVBUFFER (ROVBuffer)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_ROVBYTEADDRESS_BUFFER (ROVByteAddressBuffer)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_ROVSTRUCTURED_BUFFER (ROVStructuredBuffer)
- { 1, MipsFalse, SampleFalse }, // AR_OBJECT_ROVTEXTURE1D (ROVTexture1D)
- { 2, MipsFalse, SampleFalse }, // AR_OBJECT_ROVTEXTURE1D_ARRAY (ROVTexture1DArray)
- { 2, MipsFalse, SampleFalse }, // AR_OBJECT_ROVTEXTURE2D (ROVTexture2D)
- { 3, MipsFalse, SampleFalse }, // AR_OBJECT_ROVTEXTURE2D_ARRAY (ROVTexture2DArray)
- { 3, MipsFalse, SampleFalse }, // AR_OBJECT_ROVTEXTURE3D (ROVTexture3D)
- { 0, MipsFalse, SampleFalse }, // AR_OBJECT_LEGACY_EFFECT (legacy effect objects)
- { 0, MipsFalse, SampleFalse } // AR_OBJECT_WAVE
- };
- C_ASSERT(_countof(g_ArBasicKindsAsTypes) == _countof(g_ArBasicKindsSubscripts));
- // Type names for ArBasicKind values.
- static
- const char* g_ArBasicTypeNames[] =
- {
- "bool", "float", "<float16>", "half", "float", "double",
- "int", "sbyte", "byte", "short", "ushort",
- "int", "uint", "long", "ulong",
- "min10float", "min16float",
- "min12int", "min16int", "min16uint",
- "<count>",
- "<none>",
- "<unknown>",
- "<nocast>",
- "<pointer>",
- "null",
- "string",
- // "texture",
- "Texture1D",
- "Texture1DArray",
- "Texture2D",
- "Texture2DArray",
- "Texture3D",
- "TextureCube",
- "TextureCubeArray",
- "Texture2DMS",
- "Texture2DMSArray",
- "SamplerState",
- "sampler1D",
- "sampler2D",
- "sampler3D",
- "samplerCUBE",
- "SamplerComparisonState",
- "Buffer",
- "RenderTargetView",
- "DepthStencilView",
- "ComputeShader",
- "DomainShader",
- "GeometryShader",
- "HullShader",
- "PixelShader",
- "VertexShader",
- "pixelfragment",
- "vertexfragment",
- "StateBlock",
- "Rasterizer",
- "DepthStencil",
- "Blend",
- "PointStream",
- "LineStream",
- "TriangleStream",
- "InputPatch",
- "OutputPatch",
- "RWTexture1D",
- "RWTexture1DArray",
- "RWTexture2D",
- "RWTexture2DArray",
- "RWTexture3D",
- "RWBuffer",
- "ByteAddressBuffer",
- "RWByteAddressBuffer",
- "StructuredBuffer",
- "RWStructuredBuffer",
- "RWStructuredBuffer(Incrementable)",
- "RWStructuredBuffer(Decrementable)",
- "AppendStructuredBuffer",
- "ConsumeStructuredBuffer",
- "ConstantBuffer",
- "TextureBuffer",
- "RasterizerOrderedBuffer",
- "RasterizerOrderedByteAddressBuffer",
- "RasterizerOrderedStructuredBuffer",
- "RasterizerOrderedTexture1D",
- "RasterizerOrderedTexture1DArray",
- "RasterizerOrderedTexture2D",
- "RasterizerOrderedTexture2DArray",
- "RasterizerOrderedTexture3D",
- "<internal inner type object>",
- "deprecated effect object",
- "wave_t"
- };
- C_ASSERT(_countof(g_ArBasicTypeNames) == AR_BASIC_MAXIMUM_COUNT);
- // kind should never be a flag value or effects framework type - we simply do not expect to deal with these
- #define DXASSERT_VALIDBASICKIND(kind) \
- DXASSERT(\
- kind != AR_BASIC_COUNT && \
- kind != AR_BASIC_NONE && \
- kind != AR_BASIC_UNKNOWN && \
- kind != AR_BASIC_NOCAST && \
- kind != AR_BASIC_POINTER && \
- kind != AR_OBJECT_RENDERTARGETVIEW && \
- kind != AR_OBJECT_DEPTHSTENCILVIEW && \
- kind != AR_OBJECT_COMPUTESHADER && \
- kind != AR_OBJECT_DOMAINSHADER && \
- kind != AR_OBJECT_GEOMETRYSHADER && \
- kind != AR_OBJECT_HULLSHADER && \
- kind != AR_OBJECT_PIXELSHADER && \
- kind != AR_OBJECT_VERTEXSHADER && \
- kind != AR_OBJECT_PIXELFRAGMENT && \
- kind != AR_OBJECT_VERTEXFRAGMENT, "otherwise caller is using a special flag or an unsupported kind value");
- static
- const char* g_DeprecatedEffectObjectNames[] =
- {
- // These are case insensitive in fxc, but we'll just create two case aliases
- // to capture the majority of cases
- "texture", "Texture",
- "pixelshader", "PixelShader",
- "vertexshader", "VertexShader",
- // These are case sensitive in fxc
- "pixelfragment", // 13
- "vertexfragment", // 14
- "ComputeShader", // 13
- "DomainShader", // 12
- "GeometryShader", // 14
- "HullShader", // 10
- "BlendState", // 10
- "DepthStencilState",// 17
- "DepthStencilView", // 16
- "RasterizerState", // 15
- "RenderTargetView", // 16
- };
- // The CompareStringsWithLen function lexicographically compares LHS and RHS and
- // returns a value indicating the relationship between the strings - < 0 if LHS is
- // less than RHS, 0 if they are equal, > 0 if LHS is greater than RHS.
- static
- int CompareStringsWithLen(
- _In_count_(LHSlen) const char* LHS, size_t LHSlen,
- _In_count_(RHSlen) const char* RHS, size_t RHSlen
- )
- {
- // Check whether the name is greater or smaller (without walking past end).
- size_t maxNameComparable = std::min(LHSlen, RHSlen);
- int comparison = strncmp(LHS, RHS, maxNameComparable);
- if (comparison != 0) return comparison;
- // Check whether the name is greater or smaller based on extra characters.
- return LHSlen - RHSlen;
- }
- static hlsl::ParameterModifier
- ParamModsFromIntrinsicArg(const HLSL_INTRINSIC_ARGUMENT *pArg) {
- if (pArg->qwUsage == AR_QUAL_IN_OUT) {
- return hlsl::ParameterModifier(hlsl::ParameterModifier::Kind::InOut);
- }
- if (pArg->qwUsage == AR_QUAL_OUT) {
- return hlsl::ParameterModifier(hlsl::ParameterModifier::Kind::Out);
- }
- DXASSERT(pArg->qwUsage & AR_QUAL_IN, "else usage is incorrect");
- return hlsl::ParameterModifier(hlsl::ParameterModifier::Kind::In);
- }
- static void InitParamMods(const HLSL_INTRINSIC *pIntrinsic,
- SmallVectorImpl<hlsl::ParameterModifier> ¶mMods) {
- // The first argument is the return value, which isn't included.
- for (UINT i = 1; i < pIntrinsic->uNumArgs; ++i) {
- paramMods.push_back(ParamModsFromIntrinsicArg(&pIntrinsic->pArgs[i]));
- }
- }
- static bool IsAtomicOperation(IntrinsicOp op) {
- switch (op) {
- case IntrinsicOp::IOP_InterlockedAdd:
- case IntrinsicOp::IOP_InterlockedAnd:
- case IntrinsicOp::IOP_InterlockedCompareExchange:
- case IntrinsicOp::IOP_InterlockedCompareStore:
- case IntrinsicOp::IOP_InterlockedExchange:
- case IntrinsicOp::IOP_InterlockedMax:
- case IntrinsicOp::IOP_InterlockedMin:
- case IntrinsicOp::IOP_InterlockedOr:
- case IntrinsicOp::IOP_InterlockedXor:
- case IntrinsicOp::MOP_InterlockedAdd:
- case IntrinsicOp::MOP_InterlockedAnd:
- case IntrinsicOp::MOP_InterlockedCompareExchange:
- case IntrinsicOp::MOP_InterlockedCompareStore:
- case IntrinsicOp::MOP_InterlockedExchange:
- case IntrinsicOp::MOP_InterlockedMax:
- case IntrinsicOp::MOP_InterlockedMin:
- case IntrinsicOp::MOP_InterlockedOr:
- case IntrinsicOp::MOP_InterlockedXor:
- return true;
- default:
- return false;
- }
- }
- static bool IsBuiltinTable(LPCSTR tableName) {
- return tableName == kBuiltinIntrinsicTableName;
- }
- static void AddHLSLIntrinsicAttr(FunctionDecl *FD, ASTContext &context,
- LPCSTR tableName, LPCSTR lowering,
- const HLSL_INTRINSIC *pIntrinsic) {
- unsigned opcode = (unsigned)pIntrinsic->Op;
- if (HasUnsignedOpcode(opcode) && IsBuiltinTable(tableName)) {
- QualType Ty = FD->getReturnType();
- IntrinsicOp intrinOp = static_cast<IntrinsicOp>(pIntrinsic->Op);
- if (pIntrinsic->iOverloadParamIndex != -1) {
- const FunctionProtoType *FT =
- FD->getFunctionType()->getAs<FunctionProtoType>();
- Ty = FT->getParamType(pIntrinsic->iOverloadParamIndex);
- }
- // TODO: refine the code for getting element type
- if (const ExtVectorType *VecTy = hlsl::ConvertHLSLVecMatTypeToExtVectorType(context, Ty)) {
- Ty = VecTy->getElementType();
- }
- if (Ty->isUnsignedIntegerType()) {
- opcode = hlsl::GetUnsignedOpcode(opcode);
- }
- }
- FD->addAttr(HLSLIntrinsicAttr::CreateImplicit(context, tableName, lowering, opcode));
- if (pIntrinsic->bReadNone)
- FD->addAttr(ConstAttr::CreateImplicit(context));
- if (pIntrinsic->bReadOnly)
- FD->addAttr(PureAttr::CreateImplicit(context));
- }
- static
- FunctionDecl *AddHLSLIntrinsicFunction(
- ASTContext &context, _In_ NamespaceDecl *NS,
- LPCSTR tableName, LPCSTR lowering,
- _In_ const HLSL_INTRINSIC *pIntrinsic,
- _In_count_(functionArgTypeCount) QualType *functionArgQualTypes,
- _In_range_(0, g_MaxIntrinsicParamCount - 1) size_t functionArgTypeCount) {
- DXASSERT(functionArgTypeCount - 1 < g_MaxIntrinsicParamCount,
- "otherwise g_MaxIntrinsicParamCount should be larger");
- DeclContext *currentDeclContext = context.getTranslationUnitDecl();
- SmallVector<hlsl::ParameterModifier, g_MaxIntrinsicParamCount> paramMods;
- InitParamMods(pIntrinsic, paramMods);
- // Change dest address into reference type for atomic.
- if (IsBuiltinTable(tableName)) {
- if (IsAtomicOperation(static_cast<IntrinsicOp>(pIntrinsic->Op))) {
- DXASSERT(functionArgTypeCount > kAtomicDstOperandIdx,
- "else operation was misrecognized");
- functionArgQualTypes[kAtomicDstOperandIdx] =
- context.getLValueReferenceType(functionArgQualTypes[kAtomicDstOperandIdx]);
- }
- }
- for (size_t i = 1; i < functionArgTypeCount; i++) {
- // Change out/inout param to reference type.
- if (paramMods[i-1].isAnyOut()) {
- functionArgQualTypes[i] = context.getLValueReferenceType(functionArgQualTypes[i]);
- }
- }
- IdentifierInfo &functionId = context.Idents.get(
- StringRef(pIntrinsic->pArgs[0].pName), tok::TokenKind::identifier);
- DeclarationName functionName(&functionId);
- QualType returnQualType = functionArgQualTypes[0];
- QualType functionType = context.getFunctionType(
- functionArgQualTypes[0],
- ArrayRef<QualType>(functionArgQualTypes + 1,
- functionArgQualTypes + functionArgTypeCount),
- clang::FunctionProtoType::ExtProtoInfo(), paramMods);
- FunctionDecl *functionDecl = FunctionDecl::Create(
- context, currentDeclContext, NoLoc,
- DeclarationNameInfo(functionName, NoLoc), functionType, nullptr,
- StorageClass::SC_Extern, InlineSpecifiedFalse, HasWrittenPrototypeTrue);
- currentDeclContext->addDecl(functionDecl);
- functionDecl->setLexicalDeclContext(currentDeclContext);
- // put under hlsl namespace
- functionDecl->setDeclContext(NS);
- // Add intrinsic attribute
- AddHLSLIntrinsicAttr(functionDecl, context, tableName, lowering, pIntrinsic);
- ParmVarDecl *paramDecls[g_MaxIntrinsicParamCount];
- for (size_t i = 1; i < functionArgTypeCount; i++) {
- IdentifierInfo ¶meterId = context.Idents.get(
- StringRef(pIntrinsic->pArgs[i].pName), tok::TokenKind::identifier);
- ParmVarDecl *paramDecl =
- ParmVarDecl::Create(context, functionDecl, NoLoc, NoLoc, ¶meterId,
- functionArgQualTypes[i], nullptr,
- StorageClass::SC_None, nullptr, paramMods[i - 1]);
- functionDecl->addDecl(paramDecl);
- paramDecls[i - 1] = paramDecl;
- }
- functionDecl->setParams(
- ArrayRef<ParmVarDecl *>(paramDecls, functionArgTypeCount - 1));
- functionDecl->setImplicit(true);
- return functionDecl;
- }
- /// <summary>
- /// Checks whether the specified expression is a (possibly parenthesized) comma operator.
- /// </summary>
- static
- bool IsExpressionBinaryComma(_In_ const Expr* expr)
- {
- DXASSERT_NOMSG(expr != nullptr);
- expr = expr->IgnoreParens();
- return
- expr->getStmtClass() == Expr::StmtClass::BinaryOperatorClass &&
- cast<BinaryOperator>(expr)->getOpcode() == BinaryOperatorKind::BO_Comma;
- }
- /// <summary>
- /// Silences diagnostics for the initialization sequence, typically because they have already
- /// been emitted.
- /// </summary>
- static
- void SilenceSequenceDiagnostics(_Inout_ InitializationSequence* initSequence)
- {
- DXASSERT_NOMSG(initSequence != nullptr);
- initSequence->SetFailed(InitializationSequence::FK_ListInitializationFailed);
- }
- class UsedIntrinsic
- {
- public:
- static int compareArgs(const QualType& LHS, const QualType& RHS)
- {
- // The canonical representations are unique'd in an ASTContext, and so these
- // should be stable.
- return RHS.getTypePtr() - LHS.getTypePtr();
- }
- static int compareIntrinsic(const HLSL_INTRINSIC* LHS, const HLSL_INTRINSIC* RHS)
- {
- // The intrinsics are defined in a single static table, and so should be stable.
- return RHS - LHS;
- }
- int compare(const UsedIntrinsic& other) const
- {
- // Check whether it's the same instance.
- if (this == &other) return 0;
- int result = compareIntrinsic(m_intrinsicSource, other.m_intrinsicSource);
- if (result != 0) return result;
- // At this point, it's the exact same intrinsic name.
- // Compare the arguments for ordering then.
- DXASSERT(m_argLength == other.m_argLength, "intrinsics aren't overloaded on argument count, so we should never create a key with different #s");
- for (size_t i = 0; i < m_argLength; i++) {
- int argComparison = compareArgs(m_args[i], other.m_args[i]);
- if (argComparison != 0) return argComparison;
- }
- // Exactly the same.
- return 0;
- }
- public:
- UsedIntrinsic(const HLSL_INTRINSIC* intrinsicSource, _In_count_(argCount) QualType* args, size_t argCount)
- : m_intrinsicSource(intrinsicSource), m_argLength(argCount), m_functionDecl(nullptr)
- {
- std::copy(args, args + argCount, m_args);
- }
- void setFunctionDecl(FunctionDecl* value) const
- {
- DXASSERT(value != nullptr, "no reason to clear this out");
- DXASSERT(m_functionDecl == nullptr, "otherwise cached value is being invaldiated");
- m_functionDecl = value;
- }
- FunctionDecl* getFunctionDecl() const { return m_functionDecl; }
- bool operator==(const UsedIntrinsic& other) const
- {
- return compare(other) == 0;
- }
- bool operator<(const UsedIntrinsic& other) const
- {
- return compare(other) < 0;
- }
- private:
- QualType m_args[g_MaxIntrinsicParamCount];
- size_t m_argLength;
- const HLSL_INTRINSIC* m_intrinsicSource;
- mutable FunctionDecl* m_functionDecl;
- };
- template <typename T>
- inline void AssignOpt(T value, _Out_opt_ T* ptr)
- {
- if (ptr != nullptr)
- {
- *ptr = value;
- }
- }
- static bool CombineBasicTypes(
- ArBasicKind LeftKind,
- ArBasicKind RightKind,
- _Out_ ArBasicKind* pOutKind,
- _Out_opt_ CastKind* leftCastKind = nullptr,
- _Out_opt_ CastKind* rightCastKind = nullptr)
- {
- AssignOpt(CastKind::CK_NoOp, leftCastKind);
- AssignOpt(CastKind::CK_NoOp, rightCastKind);
- if ((LeftKind < 0 || LeftKind >= AR_BASIC_COUNT) ||
- (RightKind < 0 || RightKind >= AR_BASIC_COUNT)) {
- return false;
- }
- if (LeftKind == RightKind) {
- *pOutKind = LeftKind;
- return true;
- }
- UINT uLeftProps = GetBasicKindProps(LeftKind);
- UINT uRightProps = GetBasicKindProps(RightKind);
- UINT uBits = GET_BPROP_BITS(uLeftProps) > GET_BPROP_BITS(uRightProps) ?
- GET_BPROP_BITS(uLeftProps) : GET_BPROP_BITS(uRightProps);
- UINT uBothFlags = uLeftProps & uRightProps;
- UINT uEitherFlags = uLeftProps | uRightProps;
- if ((BPROP_BOOLEAN & uBothFlags) != 0)
- {
- *pOutKind = AR_BASIC_BOOL;
- return true;
- }
- if ((BPROP_LITERAL & uBothFlags) != 0)
- {
- if ((BPROP_INTEGER & uBothFlags) != 0)
- {
- *pOutKind = AR_BASIC_LITERAL_INT;
- }
- else
- {
- *pOutKind = AR_BASIC_LITERAL_FLOAT;
- }
- return true;
- }
- if ((BPROP_UNSIGNED & uBothFlags) != 0)
- {
- switch (uBits)
- {
- case BPROP_BITS8: *pOutKind = AR_BASIC_UINT8; break;
- case BPROP_BITS16: (uEitherFlags & BPROP_MIN_PRECISION) ?
- *pOutKind = AR_BASIC_MIN16UINT : *pOutKind = AR_BASIC_UINT16; break;
- case BPROP_BITS32: *pOutKind = AR_BASIC_UINT32; break;
- case BPROP_BITS64: *pOutKind = AR_BASIC_UINT64; break;
- default: DXASSERT_NOMSG(false); break;
- }
- AssignOpt(CK_IntegralCast, leftCastKind);
- AssignOpt(CK_IntegralCast, rightCastKind);
- return true;
- }
- if ((BPROP_INTEGER & uBothFlags) != 0)
- {
- if ((BPROP_UNSIGNED & uEitherFlags) != 0)
- {
- switch (uBits)
- {
- case BPROP_BITS8: *pOutKind = AR_BASIC_UINT8; break;
- case BPROP_BITS16: (uEitherFlags & BPROP_MIN_PRECISION) ?
- *pOutKind = AR_BASIC_MIN16UINT : *pOutKind = AR_BASIC_UINT16; break;
- case BPROP_BITS32: *pOutKind = AR_BASIC_UINT32; break;
- case BPROP_BITS64: *pOutKind = AR_BASIC_UINT64; break;
- default: DXASSERT_NOMSG(false); break;
- }
- }
- else
- {
- switch (uBits)
- {
- case BPROP_BITS0: *pOutKind = AR_BASIC_LITERAL_INT; break;
- case BPROP_BITS8: *pOutKind = AR_BASIC_INT8; break;
- case BPROP_BITS12: *pOutKind = AR_BASIC_MIN12INT; break;
- case BPROP_BITS16: (uEitherFlags & BPROP_MIN_PRECISION) ?
- *pOutKind = AR_BASIC_MIN16INT : *pOutKind = AR_BASIC_INT16; break;
- case BPROP_BITS32: *pOutKind = AR_BASIC_INT32; break;
- case BPROP_BITS64: *pOutKind = AR_BASIC_INT64; break;
- default: DXASSERT_NOMSG(false); break;
- }
- }
- AssignOpt(CK_IntegralCast, leftCastKind);
- AssignOpt(CK_IntegralCast, rightCastKind);
- return true;
- }
- // At least one side is floating-point. Assume both are and fix later
- // in this function.
- DXASSERT_NOMSG((BPROP_FLOATING & uEitherFlags) != 0);
- AssignOpt(CK_FloatingCast, leftCastKind);
- AssignOpt(CK_FloatingCast, rightCastKind);
- if ((BPROP_FLOATING & uBothFlags) == 0)
- {
- // One side is floating-point and one isn't,
- // convert to the floating-point type.
- if ((BPROP_FLOATING & uLeftProps) != 0)
- {
- uBits = GET_BPROP_BITS(uLeftProps);
- AssignOpt(CK_IntegralToFloating, rightCastKind);
- }
- else
- {
- DXASSERT_NOMSG((BPROP_FLOATING & uRightProps) != 0);
- uBits = GET_BPROP_BITS(uRightProps);
- AssignOpt(CK_IntegralToFloating, leftCastKind);
- }
- if (uBits == 0)
- {
- // We have a literal plus a non-literal so drop
- // any literalness.
- uBits = BPROP_BITS32;
- }
- }
- switch (uBits)
- {
- case BPROP_BITS10:
- *pOutKind = AR_BASIC_MIN10FLOAT;
- break;
- case BPROP_BITS16:
- if ((uEitherFlags & BPROP_MIN_PRECISION) != 0)
- {
- *pOutKind = AR_BASIC_MIN16FLOAT;
- }
- else
- {
- *pOutKind = AR_BASIC_FLOAT16;
- }
- break;
- case BPROP_BITS32:
- if ((uEitherFlags & BPROP_LITERAL) != 0 &&
- (uEitherFlags & BPROP_PARTIAL_PRECISION) != 0)
- {
- *pOutKind = AR_BASIC_FLOAT32_PARTIAL_PRECISION;
- }
- else if ((uBothFlags & BPROP_PARTIAL_PRECISION) != 0)
- {
- *pOutKind = AR_BASIC_FLOAT32_PARTIAL_PRECISION;
- }
- else
- {
- *pOutKind = AR_BASIC_FLOAT32;
- }
- break;
- case BPROP_BITS64:
- *pOutKind = AR_BASIC_FLOAT64;
- break;
- default:
- DXASSERT(false, "unexpected bit count");
- *pOutKind = AR_BASIC_FLOAT32;
- break;
- }
- return true;
- }
- class UsedIntrinsicStore : public std::set<UsedIntrinsic>
- {
- };
- static
- void GetIntrinsicMethods(ArBasicKind kind, _Outptr_result_buffer_(*intrinsicCount) const HLSL_INTRINSIC** intrinsics, _Out_ size_t* intrinsicCount)
- {
- DXASSERT_NOMSG(intrinsics != nullptr);
- DXASSERT_NOMSG(intrinsicCount != nullptr);
- switch (kind)
- {
- case AR_OBJECT_TRIANGLESTREAM:
- case AR_OBJECT_POINTSTREAM:
- case AR_OBJECT_LINESTREAM:
- *intrinsics = g_StreamMethods;
- *intrinsicCount = _countof(g_StreamMethods);
- break;
- case AR_OBJECT_TEXTURE1D:
- *intrinsics = g_Texture1DMethods;
- *intrinsicCount = _countof(g_Texture1DMethods);
- break;
- case AR_OBJECT_TEXTURE1D_ARRAY:
- *intrinsics = g_Texture1DArrayMethods;
- *intrinsicCount = _countof(g_Texture1DArrayMethods);
- break;
- case AR_OBJECT_TEXTURE2D:
- *intrinsics = g_Texture2DMethods;
- *intrinsicCount = _countof(g_Texture2DMethods);
- break;
- case AR_OBJECT_TEXTURE2DMS:
- *intrinsics = g_Texture2DMSMethods;
- *intrinsicCount = _countof(g_Texture2DMSMethods);
- break;
- case AR_OBJECT_TEXTURE2D_ARRAY:
- *intrinsics = g_Texture2DArrayMethods;
- *intrinsicCount = _countof(g_Texture2DArrayMethods);
- break;
- case AR_OBJECT_TEXTURE2DMS_ARRAY:
- *intrinsics = g_Texture2DArrayMSMethods;
- *intrinsicCount = _countof(g_Texture2DArrayMSMethods);
- break;
- case AR_OBJECT_TEXTURE3D:
- *intrinsics = g_Texture3DMethods;
- *intrinsicCount = _countof(g_Texture3DMethods);
- break;
- case AR_OBJECT_TEXTURECUBE:
- *intrinsics = g_TextureCUBEMethods;
- *intrinsicCount = _countof(g_TextureCUBEMethods);
- break;
- case AR_OBJECT_TEXTURECUBE_ARRAY:
- *intrinsics = g_TextureCUBEArrayMethods;
- *intrinsicCount = _countof(g_TextureCUBEArrayMethods);
- break;
- case AR_OBJECT_BUFFER:
- *intrinsics = g_BufferMethods;
- *intrinsicCount = _countof(g_BufferMethods);
- break;
- case AR_OBJECT_RWTEXTURE1D:
- case AR_OBJECT_ROVTEXTURE1D:
- *intrinsics = g_RWTexture1DMethods;
- *intrinsicCount = _countof(g_RWTexture1DMethods);
- break;
- case AR_OBJECT_RWTEXTURE1D_ARRAY:
- case AR_OBJECT_ROVTEXTURE1D_ARRAY:
- *intrinsics = g_RWTexture1DArrayMethods;
- *intrinsicCount = _countof(g_RWTexture1DArrayMethods);
- break;
- case AR_OBJECT_RWTEXTURE2D:
- case AR_OBJECT_ROVTEXTURE2D:
- *intrinsics = g_RWTexture2DMethods;
- *intrinsicCount = _countof(g_RWTexture2DMethods);
- break;
- case AR_OBJECT_RWTEXTURE2D_ARRAY:
- case AR_OBJECT_ROVTEXTURE2D_ARRAY:
- *intrinsics = g_RWTexture2DArrayMethods;
- *intrinsicCount = _countof(g_RWTexture2DArrayMethods);
- break;
- case AR_OBJECT_RWTEXTURE3D:
- case AR_OBJECT_ROVTEXTURE3D:
- *intrinsics = g_RWTexture3DMethods;
- *intrinsicCount = _countof(g_RWTexture3DMethods);
- break;
- case AR_OBJECT_RWBUFFER:
- case AR_OBJECT_ROVBUFFER:
- *intrinsics = g_RWBufferMethods;
- *intrinsicCount = _countof(g_RWBufferMethods);
- break;
- case AR_OBJECT_BYTEADDRESS_BUFFER:
- *intrinsics = g_ByteAddressBufferMethods;
- *intrinsicCount = _countof(g_ByteAddressBufferMethods);
- break;
- case AR_OBJECT_RWBYTEADDRESS_BUFFER:
- case AR_OBJECT_ROVBYTEADDRESS_BUFFER:
- *intrinsics = g_RWByteAddressBufferMethods;
- *intrinsicCount = _countof(g_RWByteAddressBufferMethods);
- break;
- case AR_OBJECT_STRUCTURED_BUFFER:
- *intrinsics = g_StructuredBufferMethods;
- *intrinsicCount = _countof(g_StructuredBufferMethods);
- break;
- case AR_OBJECT_RWSTRUCTURED_BUFFER:
- case AR_OBJECT_ROVSTRUCTURED_BUFFER:
- *intrinsics = g_RWStructuredBufferMethods;
- *intrinsicCount = _countof(g_RWStructuredBufferMethods);
- break;
- case AR_OBJECT_APPEND_STRUCTURED_BUFFER:
- *intrinsics = g_AppendStructuredBufferMethods;
- *intrinsicCount = _countof(g_AppendStructuredBufferMethods);
- break;
- case AR_OBJECT_CONSUME_STRUCTURED_BUFFER:
- *intrinsics = g_ConsumeStructuredBufferMethods;
- *intrinsicCount = _countof(g_ConsumeStructuredBufferMethods);
- break;
- default:
- *intrinsics = nullptr;
- *intrinsicCount = 0;
- break;
- }
- }
- static
- bool IsRowOrColumnVariable(size_t value)
- {
- return IA_SPECIAL_BASE <= value && value <= (IA_SPECIAL_BASE + IA_SPECIAL_SLOTS - 1);
- }
- static
- bool DoesComponentTypeAcceptMultipleTypes(LEGAL_INTRINSIC_COMPTYPES value)
- {
- return
- value == LICOMPTYPE_ANY_INT || // signed or unsigned ints
- value == LICOMPTYPE_ANY_INT32 || // signed or unsigned ints
- value == LICOMPTYPE_ANY_FLOAT || // float or double
- value == LICOMPTYPE_FLOAT_LIKE || // float or min16
- value == LICOMPTYPE_FLOAT_DOUBLE || // float or double
- value == LICOMPTYPE_NUMERIC || // all sorts of numbers
- value == LICOMPTYPE_NUMERIC32 || // all sorts of numbers
- value == LICOMPTYPE_NUMERIC32_ONLY || // all sorts of numbers
- value == LICOMPTYPE_ANY; // any time
- }
- static
- bool DoesComponentTypeAcceptMultipleTypes(BYTE value)
- {
- return DoesComponentTypeAcceptMultipleTypes(static_cast<LEGAL_INTRINSIC_COMPTYPES>(value));
- }
- static
- bool DoesLegalTemplateAcceptMultipleTypes(LEGAL_INTRINSIC_TEMPLATES value)
- {
- // Note that LITEMPLATE_OBJECT can accept different types, but it
- // specifies a single 'layout'. In practice, this information is used
- // together with a component type that specifies a single object.
- return value == LITEMPLATE_ANY; // Any layout
- }
- static
- bool DoesLegalTemplateAcceptMultipleTypes(BYTE value)
- {
- return DoesLegalTemplateAcceptMultipleTypes(static_cast<LEGAL_INTRINSIC_TEMPLATES>(value));
- }
- static
- bool DoesIntrinsicRequireTemplate(const HLSL_INTRINSIC* intrinsic)
- {
- const HLSL_INTRINSIC_ARGUMENT* argument = intrinsic->pArgs;
- for (size_t i = 0; i < intrinsic->uNumArgs; i++)
- {
- // The intrinsic will require a template for any of these reasons:
- // - A type template (layout) or component needs to match something else.
- // - A parameter can take multiple types.
- // - Row or columns numbers may vary.
- if (
- argument->uTemplateId != i ||
- DoesLegalTemplateAcceptMultipleTypes(argument->uLegalTemplates) ||
- DoesComponentTypeAcceptMultipleTypes(argument->uLegalComponentTypes) ||
- IsRowOrColumnVariable(argument->uCols) ||
- IsRowOrColumnVariable(argument->uRows))
- {
- return true;
- }
- argument++;
- }
- return false;
- }
- static
- bool TemplateHasDefaultType(ArBasicKind kind)
- {
- return
- kind == AR_OBJECT_BUFFER ||
- kind == AR_OBJECT_TEXTURE1D || kind == AR_OBJECT_TEXTURE2D || kind == AR_OBJECT_TEXTURE3D ||
- kind == AR_OBJECT_TEXTURE1D_ARRAY || kind == AR_OBJECT_TEXTURE2D_ARRAY ||
- kind == AR_OBJECT_TEXTURECUBE || kind == AR_OBJECT_TEXTURECUBE_ARRAY;
- }
- /// <summary>
- /// Use this class to iterate over intrinsic definitions that come from an external source.
- /// </summary>
- class IntrinsicTableDefIter
- {
- private:
- StringRef _typeName;
- StringRef _functionName;
- llvm::SmallVector<CComPtr<IDxcIntrinsicTable>, 2>& _tables;
- const HLSL_INTRINSIC* _tableIntrinsic;
- UINT64 _tableLookupCookie;
- unsigned _tableIndex;
- unsigned _argCount;
- bool _firstChecked;
- IntrinsicTableDefIter(
- llvm::SmallVector<CComPtr<IDxcIntrinsicTable>, 2>& tables,
- StringRef typeName,
- StringRef functionName,
- unsigned argCount) :
- _tables(tables), _typeName(typeName), _functionName(functionName), _argCount(argCount),
- _tableIndex(0), _tableLookupCookie(0), _tableIntrinsic(nullptr), _firstChecked(false)
- {
- }
- void CheckForIntrinsic() {
- if (_tableIndex >= _tables.size()) {
- return;
- }
- _firstChecked = true;
- // TODO: review this - this will allocate at least once per string
- CA2WEX<> typeName(_typeName.str().c_str(), CP_UTF8);
- CA2WEX<> functionName(_functionName.str().c_str(), CP_UTF8);
- if (FAILED(_tables[_tableIndex]->LookupIntrinsic(
- typeName, functionName, &_tableIntrinsic, &_tableLookupCookie))) {
- _tableLookupCookie = 0;
- _tableIntrinsic = nullptr;
- }
- }
- void MoveToNext() {
- for (;;) {
- // If we don't have an intrinsic, try the following table.
- if (_firstChecked && _tableIntrinsic == nullptr) {
- _tableIndex++;
- }
- CheckForIntrinsic();
- if (_tableIndex == _tables.size() ||
- (_tableIntrinsic != nullptr &&
- _tableIntrinsic->uNumArgs ==
- (_argCount + 1))) // uNumArgs includes return
- break;
- }
- }
- public:
- static IntrinsicTableDefIter CreateStart(llvm::SmallVector<CComPtr<IDxcIntrinsicTable>, 2>& tables,
- StringRef typeName,
- StringRef functionName,
- unsigned argCount)
- {
- IntrinsicTableDefIter result(tables, typeName, functionName, argCount);
- return result;
- }
- static IntrinsicTableDefIter CreateEnd(llvm::SmallVector<CComPtr<IDxcIntrinsicTable>, 2>& tables)
- {
- IntrinsicTableDefIter result(tables, StringRef(), StringRef(), 0);
- result._tableIndex = tables.size();
- return result;
- }
- bool operator!=(const IntrinsicTableDefIter& other)
- {
- if (!_firstChecked) {
- MoveToNext();
- }
- return _tableIndex != other._tableIndex; // More things could be compared but we only match end.
- }
- const HLSL_INTRINSIC* operator*()
- {
- DXASSERT(_firstChecked, "otherwise deref without comparing to end");
- return _tableIntrinsic;
- }
- LPCSTR GetTableName()
- {
- LPCSTR tableName = nullptr;
- if (FAILED(_tables[_tableIndex]->GetTableName(&tableName))) {
- return nullptr;
- }
- return tableName;
- }
- LPCSTR GetLoweringStrategy()
- {
- LPCSTR lowering = nullptr;
- if (FAILED(_tables[_tableIndex]->GetLoweringStrategy(_tableIntrinsic->Op, &lowering))) {
- return nullptr;
- }
- return lowering;
- }
- IntrinsicTableDefIter& operator++()
- {
- MoveToNext();
- return *this;
- }
- };
- /// <summary>
- /// Use this class to iterate over intrinsic definitions that have the same name and parameter count.
- /// </summary>
- class IntrinsicDefIter
- {
- const HLSL_INTRINSIC* _current;
- const HLSL_INTRINSIC* _end;
- IntrinsicTableDefIter _tableIter;
- IntrinsicDefIter(const HLSL_INTRINSIC* value, const HLSL_INTRINSIC* end, IntrinsicTableDefIter tableIter) :
- _current(value), _end(end), _tableIter(tableIter)
- { }
- public:
- static IntrinsicDefIter CreateStart(const HLSL_INTRINSIC* table, size_t count, const HLSL_INTRINSIC* start, IntrinsicTableDefIter tableIter)
- {
- return IntrinsicDefIter(start, table + count, tableIter);
- }
- static IntrinsicDefIter CreateEnd(const HLSL_INTRINSIC* table, size_t count, IntrinsicTableDefIter tableIter)
- {
- return IntrinsicDefIter(table + count, table + count, tableIter);
- }
- bool operator!=(const IntrinsicDefIter& other)
- {
- return _current != other._current || _tableIter.operator!=(other._tableIter);
- }
- const HLSL_INTRINSIC* operator*()
- {
- return (_current != _end) ? _current : *_tableIter;
- }
- LPCSTR GetTableName()
- {
- return (_current != _end) ? kBuiltinIntrinsicTableName : _tableIter.GetTableName();
- }
- LPCSTR GetLoweringStrategy()
- {
- return (_current != _end) ? "" : _tableIter.GetLoweringStrategy();
- }
- IntrinsicDefIter& operator++()
- {
- if (_current != _end) {
- const HLSL_INTRINSIC* next = _current + 1;
- if (next != _end && _current->uNumArgs == next->uNumArgs && 0 == strcmp(_current->pArgs[0].pName, next->pArgs[0].pName)) {
- _current = next;
- }
- else {
- _current = _end;
- }
- } else {
- ++_tableIter;
- }
- return *this;
- }
- };
- static void AddHLSLSubscriptAttr(Decl *D, ASTContext &context, HLSubscriptOpcode opcode) {
- StringRef group = GetHLOpcodeGroupName(HLOpcodeGroup::HLSubscript);
- D->addAttr(HLSLIntrinsicAttr::CreateImplicit(context, group, "", static_cast<unsigned>(opcode)));
- }
- class HLSLExternalSource : public ExternalSemaSource {
- private:
- // Inner types.
- struct FindStructBasicTypeResult {
- ArBasicKind Kind; // Kind of struct (eg, AR_OBJECT_TEXTURE2D)
- unsigned int BasicKindsAsTypeIndex; // Index into g_ArBasicKinds*
- FindStructBasicTypeResult(ArBasicKind kind,
- unsigned int basicKindAsTypeIndex)
- : Kind(kind), BasicKindsAsTypeIndex(basicKindAsTypeIndex) {}
- bool Found() const { return Kind != AR_BASIC_UNKNOWN; }
- };
- // Declaration for matrix and vector templates.
- ClassTemplateDecl* m_matrixTemplateDecl;
- ClassTemplateDecl* m_vectorTemplateDecl;
- // Namespace decl for hlsl intrin functions
- NamespaceDecl* m_hlslNSDecl;
- // Context being processed.
- _Notnull_ ASTContext* m_context;
- // Semantic analyzer being processed.
- Sema* m_sema;
- // Intrinsic tables available externally.
- llvm::SmallVector<CComPtr<IDxcIntrinsicTable>, 2> m_intrinsicTables;
- // Scalar types indexed by HLSLScalarType.
- QualType m_scalarTypes[HLSLScalarTypeCount];
- // Matrix types already built indexed by type, row-count, col-count. Should probably move to a sparse map. Instrument to figure out best initial size.
- QualType m_matrixTypes[HLSLScalarTypeCount][4][4];
- // Matrix types already built, in shorthand form.
- TypedefDecl* m_matrixShorthandTypes[HLSLScalarTypeCount][4][4];
- // Vector types already built.
- QualType m_vectorTypes[HLSLScalarTypeCount][4];
- TypedefDecl* m_vectorTypedefs[HLSLScalarTypeCount][4];
- // Built-in object types declarations, indexed by basic kind constant.
- CXXRecordDecl* m_objectTypeDecls[_countof(g_ArBasicKindsAsTypes)];
- // Map from object decl to the object index.
- using ObjectTypeDeclMapType = std::array<std::pair<CXXRecordDecl*,unsigned>, _countof(g_ArBasicKindsAsTypes)+_countof(g_DeprecatedEffectObjectNames)>;
- ObjectTypeDeclMapType m_objectTypeDeclsMap;
- // Mask for object which not has methods created.
- uint64_t m_objectTypeLazyInitMask;
- UsedIntrinsicStore m_usedIntrinsics;
- /// <summary>Adds all supporting declarations to reference scalar types.</summary>
- void AddHLSLScalarTypes();
- QualType GetTemplateObjectDataType(_In_ CXXRecordDecl* recordDecl)
- {
- DXASSERT_NOMSG(recordDecl != nullptr);
- TemplateParameterList* parameterList = recordDecl->getTemplateParameterList(0);
- NamedDecl* parameterDecl = parameterList->getParam(0);
- DXASSERT(parameterDecl->getKind() == Decl::Kind::TemplateTypeParm, "otherwise recordDecl isn't one of the built-in objects with templates");
- TemplateTypeParmDecl* parmDecl = dyn_cast<TemplateTypeParmDecl>(parameterDecl);
- return QualType(parmDecl->getTypeForDecl(), 0);
- }
- // Determines whether the given intrinsic parameter type has a single QualType mapping.
- QualType GetSingleQualTypeForMapping(const HLSL_INTRINSIC* intrinsic, int index)
- {
- int templateRef = intrinsic->pArgs[index].uTemplateId;
- int componentRef = intrinsic->pArgs[index].uComponentTypeId;
- const HLSL_INTRINSIC_ARGUMENT* templateArg = &intrinsic->pArgs[templateRef];
- const HLSL_INTRINSIC_ARGUMENT* componentArg = &intrinsic->pArgs[componentRef];
- const HLSL_INTRINSIC_ARGUMENT* matrixArg = &intrinsic->pArgs[index];
- if (
- templateRef >= 0 &&
- templateArg->uTemplateId == templateRef &&
- !DoesLegalTemplateAcceptMultipleTypes(templateArg->uLegalTemplates) &&
- componentRef >= 0 &&
- componentRef != INTRIN_COMPTYPE_FROM_TYPE_ELT0 &&
- componentArg->uComponentTypeId == 0 &&
- !DoesComponentTypeAcceptMultipleTypes(componentArg->uLegalComponentTypes) &&
- !IsRowOrColumnVariable(matrixArg->uCols) &&
- !IsRowOrColumnVariable(matrixArg->uRows))
- {
- ArTypeObjectKind templateKind = g_LegalIntrinsicTemplates[templateArg->uLegalTemplates][0];
- ArBasicKind elementKind = g_LegalIntrinsicCompTypes[componentArg->uLegalComponentTypes][0];
- return NewSimpleAggregateType(templateKind, elementKind, 0, matrixArg->uRows, matrixArg->uCols);
- }
- return QualType();
- }
- // Adds a new template parameter declaration to the specified array and returns the type for the parameter.
- QualType AddTemplateParamToArray(_In_z_ const char* name, _Inout_ CXXRecordDecl* recordDecl, int templateDepth,
- _Inout_count_c_(g_MaxIntrinsicParamCount + 1) NamedDecl* (&templateParamNamedDecls)[g_MaxIntrinsicParamCount + 1],
- _Inout_ size_t* templateParamNamedDeclsCount)
- {
- DXASSERT_NOMSG(name != nullptr);
- DXASSERT_NOMSG(recordDecl != nullptr);
- DXASSERT_NOMSG(templateParamNamedDecls != nullptr);
- DXASSERT_NOMSG(templateParamNamedDeclsCount != nullptr);
- DXASSERT(*templateParamNamedDeclsCount < _countof(templateParamNamedDecls), "otherwise constants should be updated");
- _Analysis_assume_(*templateParamNamedDeclsCount < _countof(templateParamNamedDecls));
- // Create the declaration for the template parameter.
- IdentifierInfo* id = &m_context->Idents.get(StringRef(name));
- TemplateTypeParmDecl* templateTypeParmDecl =
- TemplateTypeParmDecl::Create(*m_context, recordDecl, NoLoc, NoLoc, templateDepth, *templateParamNamedDeclsCount,
- id, TypenameTrue, ParameterPackFalse);
- templateParamNamedDecls[*templateParamNamedDeclsCount] = templateTypeParmDecl;
- // Create the type that the parameter represents.
- QualType result = m_context->getTemplateTypeParmType(
- templateDepth, *templateParamNamedDeclsCount, ParameterPackFalse, templateTypeParmDecl);
- // Increment the declaration count for the array; as long as caller passes in both arguments,
- // it need not concern itself with maintaining this value.
- (*templateParamNamedDeclsCount)++;
- return result;
- }
- // Adds a function specified by the given intrinsic to a record declaration.
- // The template depth will be zero for records that don't have a "template<>" line
- // even if conceptual; or one if it does have one.
- void AddObjectIntrinsicTemplate(_Inout_ CXXRecordDecl* recordDecl, int templateDepth, _In_ const HLSL_INTRINSIC* intrinsic)
- {
- DXASSERT_NOMSG(recordDecl != nullptr);
- DXASSERT_NOMSG(intrinsic != nullptr);
- DXASSERT(intrinsic->uNumArgs > 0, "otherwise there isn't even an intrinsic name");
- DXASSERT(intrinsic->uNumArgs <= (g_MaxIntrinsicParamCount + 1), "otherwise g_MaxIntrinsicParamCount should be updated");
-
- // uNumArgs includes the result type, g_MaxIntrinsicParamCount doesn't, thus the +1.
- _Analysis_assume_(intrinsic->uNumArgs <= (g_MaxIntrinsicParamCount + 1));
- // TODO: implement template parameter constraints for HLSL intrinsic methods in declarations
- //
- // Build template parameters, parameter types, and the return type.
- // Parameter declarations are built after the function is created, to use it as their scope.
- //
- unsigned int numParams = intrinsic->uNumArgs - 1;
- NamedDecl* templateParamNamedDecls[g_MaxIntrinsicParamCount + 1];
- size_t templateParamNamedDeclsCount = 0;
- QualType argsQTs[g_MaxIntrinsicParamCount];
- StringRef argNames[g_MaxIntrinsicParamCount];
- QualType functionResultQT;
- DXASSERT(
- _countof(templateParamNamedDecls) >= numParams + 1,
- "need enough templates for all parameters and the return type, otherwise constants need updating");
- // Handle the return type.
- // functionResultQT = GetSingleQualTypeForMapping(intrinsic, 0);
- // if (functionResultQT.isNull()) {
- // Workaround for template parameter argument count mismatch.
- // Create template parameter for return type always
- // TODO: reenable the check and skip template argument.
- functionResultQT = AddTemplateParamToArray(
- "TResult", recordDecl, templateDepth, templateParamNamedDecls,
- &templateParamNamedDeclsCount);
- // }
- SmallVector<hlsl::ParameterModifier, g_MaxIntrinsicParamCount> paramMods;
- InitParamMods(intrinsic, paramMods);
- // Consider adding more cases where return type can be handled a priori. Ultimately #260431 should do significantly better.
- // Handle parameters.
- for (unsigned int i = 1; i < intrinsic->uNumArgs; i++)
- {
- //
- // GetSingleQualTypeForMapping can be used here to remove unnecessary template arguments.
- //
- // However this may produce template instantiations with equivalent template arguments
- // for overloaded methods. It's possible to resolve some of these by generating specializations,
- // but the current intrinsic table has rules that are hard to process in their current form
- // to find all cases.
- //
- char name[g_MaxIntrinsicParamName + 2];
- name[0] = 'T';
- name[1] = '\0';
- strcat_s(name, intrinsic->pArgs[i].pName);
- argsQTs[i - 1] = AddTemplateParamToArray(name, recordDecl, templateDepth, templateParamNamedDecls, &templateParamNamedDeclsCount);
- // Change out/inout param to reference type.
- if (paramMods[i-1].isAnyOut())
- argsQTs[i - 1] = m_context->getLValueReferenceType(argsQTs[i - 1]);
-
- argNames[i - 1] = StringRef(intrinsic->pArgs[i].pName);
- }
- // Create the declaration.
- IdentifierInfo* ii = &m_context->Idents.get(StringRef(intrinsic->pArgs[0].pName));
- DeclarationName declarationName = DeclarationName(ii);
- CXXMethodDecl* functionDecl = CreateObjectFunctionDeclarationWithParams(*m_context, recordDecl,
- functionResultQT, ArrayRef<QualType>(argsQTs, numParams), ArrayRef<StringRef>(argNames, numParams),
- declarationName, true);
- functionDecl->setImplicit(true);
- // If the function is a template function, create the declaration and cross-reference.
- if (templateParamNamedDeclsCount > 0)
- {
- hlsl::CreateFunctionTemplateDecl(
- *m_context, recordDecl, functionDecl, templateParamNamedDecls, templateParamNamedDeclsCount);
- }
- }
- // Checks whether the two specified intrinsics generate equivalent templates.
- // For example: foo (any_int) and foo (any_float) are only unambiguous in the context
- // of HLSL intrinsic rules, and their difference can't be expressed with C++ templates.
- bool AreIntrinsicTemplatesEquivalent(const HLSL_INTRINSIC* left, const HLSL_INTRINSIC* right)
- {
- if (left == right)
- {
- return true;
- }
- if (left == nullptr || right == nullptr)
- {
- return false;
- }
- return (left->uNumArgs == right->uNumArgs &&
- 0 == strcmp(left->pArgs[0].pName, right->pArgs[0].pName));
- }
- // Adds all the intrinsic methods that correspond to the specified type.
- void AddObjectMethods(ArBasicKind kind, _In_ CXXRecordDecl* recordDecl, int templateDepth)
- {
- DXASSERT_NOMSG(recordDecl != nullptr);
- DXASSERT_NOMSG(templateDepth >= 0);
- const HLSL_INTRINSIC* intrinsics;
- const HLSL_INTRINSIC* prior = nullptr;
- size_t intrinsicCount;
- GetIntrinsicMethods(kind, &intrinsics, &intrinsicCount);
- DXASSERT(
- (intrinsics == nullptr) == (intrinsicCount == 0),
- "intrinsic table pointer must match count (null for zero, something valid otherwise");
- while (intrinsicCount--)
- {
- if (!AreIntrinsicTemplatesEquivalent(intrinsics, prior))
- {
- AddObjectIntrinsicTemplate(recordDecl, templateDepth, intrinsics);
- prior = intrinsics;
- }
- intrinsics++;
- }
- }
- void AddDoubleSubscriptSupport(
- _In_ ClassTemplateDecl* typeDecl,
- _In_ CXXRecordDecl* recordDecl,
- _In_z_ const char* memberName, QualType elementType, TemplateTypeParmDecl* templateTypeParmDecl,
- _In_z_ const char* type0Name,
- _In_z_ const char* type1Name,
- _In_z_ const char* indexer0Name, QualType indexer0Type,
- _In_z_ const char* indexer1Name, QualType indexer1Type)
- {
- DXASSERT_NOMSG(typeDecl != nullptr);
- DXASSERT_NOMSG(recordDecl != nullptr);
- DXASSERT_NOMSG(memberName != nullptr);
- DXASSERT_NOMSG(!elementType.isNull());
- DXASSERT_NOMSG(templateTypeParmDecl != nullptr);
- DXASSERT_NOMSG(type0Name != nullptr);
- DXASSERT_NOMSG(type1Name != nullptr);
- DXASSERT_NOMSG(indexer0Name != nullptr);
- DXASSERT_NOMSG(!indexer0Type.isNull());
- DXASSERT_NOMSG(indexer1Name != nullptr);
- DXASSERT_NOMSG(!indexer1Type.isNull());
- //
- // Add inner types to the templates to represent the following C++ code inside the class.
- // public:
- // class sample_slice_type
- // {
- // public: TElement operator[](uint3 index);
- // };
- // class sample_type
- // {
- // public: sample_slice_type operator[](uint slice);
- // };
- // sample_type sample;
- //
- // Variable names reflect this structure, but this code will also produce the types
- // for .mips access.
- //
- const bool MutableTrue = true;
- DeclarationName subscriptName = m_context->DeclarationNames.getCXXOperatorName(OO_Subscript);
- CXXRecordDecl* sampleSliceTypeDecl = CXXRecordDecl::Create(*m_context, TTK_Class, recordDecl, NoLoc, NoLoc,
- &m_context->Idents.get(StringRef(type1Name)));
- sampleSliceTypeDecl->setAccess(AS_public);
- sampleSliceTypeDecl->setImplicit();
- recordDecl->addDecl(sampleSliceTypeDecl);
- sampleSliceTypeDecl->startDefinition();
- const bool MutableFalse = false;
- FieldDecl* sliceHandleDecl = FieldDecl::Create(*m_context, sampleSliceTypeDecl, NoLoc, NoLoc,
- &m_context->Idents.get(StringRef("handle")), indexer0Type,
- m_context->CreateTypeSourceInfo(indexer0Type), nullptr, MutableFalse, ICIS_NoInit);
- sliceHandleDecl->setAccess(AS_private);
- sampleSliceTypeDecl->addDecl(sliceHandleDecl);
- CXXMethodDecl* sampleSliceSubscriptDecl = CreateObjectFunctionDeclarationWithParams(*m_context,
- sampleSliceTypeDecl, elementType,
- ArrayRef<QualType>(indexer1Type), ArrayRef<StringRef>(StringRef(indexer1Name)), subscriptName, true);
- hlsl::CreateFunctionTemplateDecl(*m_context, sampleSliceTypeDecl, sampleSliceSubscriptDecl,
- reinterpret_cast<NamedDecl**>(&templateTypeParmDecl), 1);
- sampleSliceTypeDecl->completeDefinition();
- CXXRecordDecl* sampleTypeDecl = CXXRecordDecl::Create(*m_context, TTK_Class, recordDecl, NoLoc, NoLoc,
- &m_context->Idents.get(StringRef(type0Name)));
- sampleTypeDecl->setAccess(AS_public);
- recordDecl->addDecl(sampleTypeDecl);
- sampleTypeDecl->startDefinition();
- sampleTypeDecl->setImplicit();
- FieldDecl* sampleHandleDecl = FieldDecl::Create(*m_context, sampleTypeDecl, NoLoc, NoLoc,
- &m_context->Idents.get(StringRef("handle")), indexer0Type,
- m_context->CreateTypeSourceInfo(indexer0Type), nullptr, MutableFalse, ICIS_NoInit);
- sampleHandleDecl->setAccess(AS_private);
- sampleTypeDecl->addDecl(sampleHandleDecl);
- QualType sampleSliceType = m_context->getRecordType(sampleSliceTypeDecl);
- CXXMethodDecl* sampleSubscriptDecl = CreateObjectFunctionDeclarationWithParams(*m_context,
- sampleTypeDecl, m_context->getRValueReferenceType(sampleSliceType), // TODO: choose LValueRef if writable.
- ArrayRef<QualType>(indexer0Type), ArrayRef<StringRef>(StringRef(indexer0Name)), subscriptName, true);
- sampleTypeDecl->completeDefinition();
- // Add subscript attribute
- AddHLSLSubscriptAttr(sampleSubscriptDecl, *m_context, HLSubscriptOpcode::DoubleSubscript);
- QualType sampleTypeQT = m_context->getRecordType(sampleTypeDecl);
- FieldDecl* sampleFieldDecl = FieldDecl::Create(*m_context, recordDecl, NoLoc, NoLoc,
- &m_context->Idents.get(StringRef(memberName)), sampleTypeQT,
- m_context->CreateTypeSourceInfo(sampleTypeQT), nullptr, MutableTrue, ICIS_NoInit);
- sampleFieldDecl->setAccess(AS_public);
- recordDecl->addDecl(sampleFieldDecl);
- }
- void AddObjectSubscripts(ArBasicKind kind, _In_ ClassTemplateDecl *typeDecl,
- _In_ CXXRecordDecl *recordDecl,
- SubscriptOperatorRecord op) {
- DXASSERT_NOMSG(typeDecl != nullptr);
- DXASSERT_NOMSG(recordDecl != nullptr);
- DXASSERT_NOMSG(0 <= op.SubscriptCardinality &&
- op.SubscriptCardinality <= 3);
- DXASSERT(op.SubscriptCardinality > 0 ||
- (op.HasMips == false && op.HasSample == false),
- "objects that have .mips or .sample member also have a plain "
- "subscript defined (otherwise static table is "
- "likely incorrect, and this function won't know the cardinality "
- "of the position parameter");
- bool isReadWrite = GetBasicKindProps(kind) & BPROP_RWBUFFER;
- DXASSERT(!isReadWrite || (op.HasMips == false && op.HasSample == false),
- "read/write objects don't have .mips or .sample members");
- // Return early if there is no work to be done.
- if (op.SubscriptCardinality == 0) {
- return;
- }
- const unsigned int templateDepth = 1;
- // Add an operator[].
- TemplateTypeParmDecl *templateTypeParmDecl = cast<TemplateTypeParmDecl>(
- typeDecl->getTemplateParameters()->getParam(0));
- QualType resultType = m_context->getTemplateTypeParmType(
- templateDepth, 0, ParameterPackFalse, templateTypeParmDecl);
- if (isReadWrite)
- resultType = m_context->getLValueReferenceType(resultType, false);
- else
- resultType = m_context->getRValueReferenceType(resultType);
- QualType indexType =
- op.SubscriptCardinality == 1
- ? m_context->UnsignedIntTy
- : NewSimpleAggregateType(AR_TOBJ_VECTOR, AR_BASIC_UINT32, 0, 1,
- op.SubscriptCardinality);
- CXXMethodDecl *functionDecl = CreateObjectFunctionDeclarationWithParams(
- *m_context, recordDecl, resultType, ArrayRef<QualType>(indexType),
- ArrayRef<StringRef>(StringRef("index")),
- m_context->DeclarationNames.getCXXOperatorName(OO_Subscript), true);
- hlsl::CreateFunctionTemplateDecl(
- *m_context, recordDecl, functionDecl,
- reinterpret_cast<NamedDecl **>(&templateTypeParmDecl), 1);
- // Add a .mips member if necessary.
- QualType uintType = m_context->UnsignedIntTy;
- if (op.HasMips) {
- AddDoubleSubscriptSupport(typeDecl, recordDecl, "mips", resultType,
- templateTypeParmDecl, "mips_type",
- "mips_slice_type", "mipSlice", uintType, "pos",
- indexType);
- }
- // Add a .sample member if necessary.
- if (op.HasSample) {
- AddDoubleSubscriptSupport(typeDecl, recordDecl, "sample", resultType,
- templateTypeParmDecl, "sample_type",
- "sample_slice_type", "sampleSlice", uintType,
- "pos", indexType);
- // TODO: support operator[][](indexType, uint).
- }
- }
- static bool ObjectTypeDeclMapTypeCmp(const std::pair<CXXRecordDecl*,unsigned> &a,
- const std::pair<CXXRecordDecl*,unsigned> &b) {
- return a.first < b.first;
- };
- int FindObjectBasicKindIndex(const CXXRecordDecl* recordDecl) {
- auto begin = m_objectTypeDeclsMap.begin();
- auto end = m_objectTypeDeclsMap.end();
- auto val = std::make_pair(const_cast<CXXRecordDecl*>(recordDecl), 0);
- auto low = std::lower_bound(begin, end, val, ObjectTypeDeclMapTypeCmp);
- if (low == end)
- return -1;
- if (recordDecl == low->first)
- return low->second;
- else
- return -1;
- }
- // Adds all built-in HLSL object types.
- void AddObjectTypes()
- {
- DXASSERT(m_context != nullptr, "otherwise caller hasn't initialized context yet");
- QualType float4Type = LookupVectorType(HLSLScalarType_float, 4);
- TypeSourceInfo *float4TypeSourceInfo = m_context->getTrivialTypeSourceInfo(float4Type, NoLoc);
- m_objectTypeLazyInitMask = 0;
- unsigned effectKindIndex = 0;
- for (int i = 0; i < _countof(g_ArBasicKindsAsTypes); i++)
- {
- ArBasicKind kind = g_ArBasicKindsAsTypes[i];
- if (kind == AR_OBJECT_WAVE) { // wave objects are currently unused
- continue;
- }
- if (kind == AR_OBJECT_LEGACY_EFFECT)
- effectKindIndex = i;
- DXASSERT(kind < _countof(g_ArBasicTypeNames), "g_ArBasicTypeNames has the wrong number of entries");
- _Analysis_assume_(kind < _countof(g_ArBasicTypeNames));
- const char* typeName = g_ArBasicTypeNames[kind];
- uint8_t templateArgCount = g_ArBasicKindsTemplateCount[i];
- CXXRecordDecl* recordDecl = nullptr;
- if (templateArgCount == 0)
- {
- AddRecordTypeWithHandle(*m_context, &recordDecl, typeName);
- DXASSERT(recordDecl != nullptr, "AddRecordTypeWithHandle failed to return the object declaration");
- recordDecl->setImplicit(true);
- }
- else
- {
- DXASSERT(templateArgCount == 1 || templateArgCount == 2, "otherwise a new case has been added");
- ClassTemplateDecl* typeDecl = nullptr;
- TypeSourceInfo* typeDefault = TemplateHasDefaultType(kind) ? float4TypeSourceInfo : nullptr;
- AddTemplateTypeWithHandle(*m_context, &typeDecl, &recordDecl, typeName, templateArgCount, typeDefault);
- DXASSERT(typeDecl != nullptr, "AddTemplateTypeWithHandle failed to return the object declaration");
- typeDecl->setImplicit(true);
- recordDecl->setImplicit(true);
- }
- m_objectTypeDecls[i] = recordDecl;
- m_objectTypeDeclsMap[i] = std::make_pair(recordDecl, i);
- m_objectTypeLazyInitMask |= ((uint64_t)1)<<i;
- }
- // Create an alias for SamplerState. 'sampler' is very commonly used.
- {
- DeclContext* currentDeclContext = m_context->getTranslationUnitDecl();
- IdentifierInfo& samplerId = m_context->Idents.get(StringRef("sampler"), tok::TokenKind::identifier);
- TypeSourceInfo* samplerTypeSource = m_context->getTrivialTypeSourceInfo(GetBasicKindType(AR_OBJECT_SAMPLER));
- TypedefDecl* samplerDecl = TypedefDecl::Create(*m_context, currentDeclContext, NoLoc, NoLoc, &samplerId, samplerTypeSource);
- currentDeclContext->addDecl(samplerDecl);
- samplerDecl->setImplicit(true);
- // Create decls for each deprecated effect object type:
- unsigned effectObjBase = _countof(g_ArBasicKindsAsTypes);
- // TypeSourceInfo* effectObjTypeSource = m_context->getTrivialTypeSourceInfo(GetBasicKindType(AR_OBJECT_LEGACY_EFFECT));
- for (int i = 0; i < _countof(g_DeprecatedEffectObjectNames); i++) {
- IdentifierInfo& idInfo = m_context->Idents.get(StringRef(g_DeprecatedEffectObjectNames[i]), tok::TokenKind::identifier);
- //TypedefDecl* effectObjDecl = TypedefDecl::Create(*m_context, currentDeclContext, NoLoc, NoLoc, &idInfo, effectObjTypeSource);
- CXXRecordDecl *effectObjDecl = CXXRecordDecl::Create(*m_context, TagTypeKind::TTK_Struct, currentDeclContext, NoLoc, NoLoc, &idInfo);
- currentDeclContext->addDecl(effectObjDecl);
- effectObjDecl->setImplicit(true);
- m_objectTypeDeclsMap[i+effectObjBase] = std::make_pair(effectObjDecl, effectKindIndex);
- }
- }
- // Make sure it's in order.
- std::sort(m_objectTypeDeclsMap.begin(), m_objectTypeDeclsMap.end(), ObjectTypeDeclMapTypeCmp);
- }
- FunctionDecl* AddSubscriptSpecialization(
- _In_ FunctionTemplateDecl* functionTemplate,
- QualType objectElement,
- const FindStructBasicTypeResult& findResult);
- ImplicitCastExpr* CreateLValueToRValueCast(Expr* input) {
- return ImplicitCastExpr::Create(*m_context, input->getType(), CK_LValueToRValue, input, nullptr, VK_RValue);
- }
- ImplicitCastExpr* CreateFlatConversionCast(Expr* input) {
- return ImplicitCastExpr::Create(*m_context, input->getType(), CK_LValueToRValue, input, nullptr, VK_RValue);
- }
- HRESULT CombineDimensions(QualType leftType, QualType rightType, QualType *resultType);
- clang::TypedefDecl *LookupMatrixShorthandType(HLSLScalarType scalarType, UINT rowCount, UINT colCount) {
- DXASSERT_NOMSG(scalarType != HLSLScalarType::HLSLScalarType_unknown &&
- rowCount >= 0 && rowCount <= 4 && colCount >= 0 &&
- colCount <= 4);
- TypedefDecl *qts =
- m_matrixShorthandTypes[scalarType][rowCount - 1][colCount - 1];
- if (qts == nullptr) {
- QualType type = LookupMatrixType(scalarType, rowCount, colCount);
- qts = CreateMatrixSpecializationShorthand(*m_context, type, scalarType,
- rowCount, colCount);
- m_matrixShorthandTypes[scalarType][rowCount - 1][colCount - 1] = qts;
- }
- return qts;
- }
- clang::TypedefDecl *LookupVectorShorthandType(HLSLScalarType scalarType, UINT colCount) {
- DXASSERT_NOMSG(scalarType != HLSLScalarType::HLSLScalarType_unknown &&
- colCount >= 0 && colCount <= 4);
- TypedefDecl *qts = m_vectorTypedefs[scalarType][colCount - 1];
- if (qts == nullptr) {
- QualType type = LookupVectorType(scalarType, colCount);
- qts = CreateVectorSpecializationShorthand(*m_context, type, scalarType,
- colCount);
- m_vectorTypedefs[scalarType][colCount - 1] = qts;
- }
- return qts;
- }
- public:
- HLSLExternalSource() :
- m_context(nullptr),
- m_sema(nullptr),
- m_vectorTemplateDecl(nullptr),
- m_matrixTemplateDecl(nullptr)
- {
- memset(m_matrixTypes, 0, sizeof(m_matrixTypes));
- memset(m_matrixShorthandTypes, 0, sizeof(m_matrixShorthandTypes));
- memset(m_vectorTypes, 0, sizeof(m_vectorTypes));
- memset(m_vectorTypedefs, 0, sizeof(m_vectorTypedefs));
- }
- ~HLSLExternalSource() { }
- static HLSLExternalSource* FromSema(_In_ Sema* self)
- {
- DXASSERT_NOMSG(self != nullptr);
- ExternalSemaSource* externalSource = self->getExternalSource();
- DXASSERT(externalSource != nullptr, "otherwise caller shouldn't call HLSL-specific function");
- HLSLExternalSource* hlsl = reinterpret_cast<HLSLExternalSource*>(externalSource);
- return hlsl;
- }
- void InitializeSema(Sema& S) override
- {
- m_sema = &S;
- S.addExternalSource(this);
- AddObjectTypes();
- AddStdIsEqualImplementation(S.getASTContext(), S);
- for (auto && intrinsic : m_intrinsicTables) {
- AddIntrinsicTableMethods(intrinsic);
- }
- }
- void ForgetSema() override
- {
- m_sema = nullptr;
- }
- Sema* getSema() {
- return m_sema;
- }
- QualType LookupMatrixType(HLSLScalarType scalarType, unsigned int rowCount, unsigned int colCount)
- {
- QualType qt = m_matrixTypes[scalarType][rowCount - 1][colCount - 1];
- if (qt.isNull()) {
- qt = GetOrCreateMatrixSpecialization(*m_context, m_sema, m_matrixTemplateDecl, m_scalarTypes[scalarType], rowCount, colCount);
- m_matrixTypes[scalarType][rowCount - 1][colCount - 1] = qt;
- }
- return qt;
- }
- QualType LookupVectorType(HLSLScalarType scalarType, unsigned int colCount)
- {
- QualType qt = m_vectorTypes[scalarType][colCount - 1];
- if (qt.isNull()) {
- qt = GetOrCreateVectorSpecialization(*m_context, m_sema, m_vectorTemplateDecl, m_scalarTypes[scalarType], colCount);
- m_vectorTypes[scalarType][colCount - 1] = qt;
- }
- return qt;
- }
- bool LookupUnqualified(LookupResult &R, Scope *S) override
- {
- const DeclarationNameInfo declName = R.getLookupNameInfo();
- IdentifierInfo* idInfo = declName.getName().getAsIdentifierInfo();
- if (idInfo == nullptr) {
- return false;
- }
- StringRef nameIdentifier = idInfo->getName();
- HLSLScalarType parsedType;
- int rowCount;
- int colCount;
- if (TryParseMatrixShorthand(nameIdentifier.data(), nameIdentifier.size(), &parsedType, &rowCount, &colCount)) {
- assert(parsedType != HLSLScalarType_unknown && "otherwise, TryParseMatrixShorthand should not have succeeded");
- QualType qt = LookupMatrixType(parsedType, rowCount, colCount);
- if (parsedType == HLSLScalarType_int_min12)
- m_sema->Diag(R.getNameLoc(), diag::warn_hlsl_sema_minprecision_promotion) << "min12int" << "min16int";
- else if (parsedType == HLSLScalarType_float_min10)
- m_sema->Diag(R.getNameLoc(), diag::warn_hlsl_sema_minprecision_promotion) << "min10float" << "min16float";
- TypedefDecl* qts = LookupMatrixShorthandType(parsedType, rowCount, colCount);
- R.addDecl(qts);
- return true;
- } else if (TryParseVectorShorthand(nameIdentifier.data(), nameIdentifier.size(), &parsedType, &colCount)) {
- assert(parsedType != HLSLScalarType_unknown && "otherwise, TryParseVectorShorthand should not have succeeded");
- QualType qt = LookupVectorType(parsedType, colCount);
- if (parsedType == HLSLScalarType_int_min12)
- m_sema->Diag(R.getNameLoc(), diag::warn_hlsl_sema_minprecision_promotion) << "min12int" << "min16int";
- else if (parsedType == HLSLScalarType_float_min10)
- m_sema->Diag(R.getNameLoc(), diag::warn_hlsl_sema_minprecision_promotion) << "min10float" << "min16float";
- TypedefDecl *qts = LookupVectorShorthandType(parsedType, colCount);
- R.addDecl(qts);
- return true;
- }
- return false;
- }
- /// <summary>
- /// Determines whether the specify record type is a matrix, another HLSL object, or a user-defined structure.
- /// </sumary>
- ArTypeObjectKind ClassifyRecordType(const RecordType* type)
- {
- DXASSERT_NOMSG(type != nullptr);
- const CXXRecordDecl* typeRecordDecl = type->getAsCXXRecordDecl();
- const ClassTemplateSpecializationDecl* templateSpecializationDecl = dyn_cast<ClassTemplateSpecializationDecl>(typeRecordDecl);
- if (templateSpecializationDecl) {
- ClassTemplateDecl *decl = templateSpecializationDecl->getSpecializedTemplate();
- if (decl == m_matrixTemplateDecl)
- return AR_TOBJ_MATRIX;
- else if (decl == m_vectorTemplateDecl)
- return AR_TOBJ_VECTOR;
- DXASSERT(decl->isImplicit(), "otherwise object template decl is not set to implicit");
- return AR_TOBJ_OBJECT;
- }
- if (typeRecordDecl && typeRecordDecl->isImplicit()) {
- if (typeRecordDecl->getDeclContext()->isFileContext())
- return AR_TOBJ_OBJECT;
- else
- return AR_TOBJ_INNER_OBJ;
- }
- return AR_TOBJ_COMPOUND;
- }
- /// <summary>Given a Clang type, determines whether it is a built-in object type (sampler, texture, etc).</summary>
- bool IsBuiltInObjectType(QualType type)
- {
- type = GetStructuralForm(type);
- if (!type.isNull() && type->isStructureOrClassType()) {
- const RecordType* recordType = type->getAs<RecordType>();
- return ClassifyRecordType(recordType) == AR_TOBJ_OBJECT;
- }
- return false;
- }
- /// <summary>
- /// Given the specified type (typed a DeclContext for convenience), determines its RecordDecl,
- /// possibly refering to original template record if it's a specialization; this makes the result
- /// suitable for looking up in initialization tables.
- /// </summary>
- const CXXRecordDecl* GetRecordDeclForBuiltInOrStruct(const DeclContext* context)
- {
- const CXXRecordDecl* recordDecl;
- if (const ClassTemplateSpecializationDecl* decl = dyn_cast<ClassTemplateSpecializationDecl>(context))
- {
- recordDecl = decl->getSpecializedTemplate()->getTemplatedDecl();
- }
- else
- {
- recordDecl = dyn_cast<CXXRecordDecl>(context);
- }
- return recordDecl;
- }
- /// <summary>Given a Clang type, return the ArTypeObjectKind classification, (eg AR_TOBJ_VECTOR).</summary>
- ArTypeObjectKind GetTypeObjectKind(QualType type)
- {
- DXASSERT_NOMSG(!type.isNull());
- type = GetStructuralForm(type);
- if (type->isVoidType()) return AR_TOBJ_VOID;
- if (type->isArrayType()) return AR_TOBJ_ARRAY;
- if (type->isPointerType()) {
- return AR_TOBJ_POINTER;
- }
- if (type->isStructureOrClassType()) {
- const RecordType* recordType = type->getAs<RecordType>();
- return ClassifyRecordType(recordType);
- } else if (const InjectedClassNameType *ClassNameTy =
- type->getAs<InjectedClassNameType>()) {
- const CXXRecordDecl *typeRecordDecl = ClassNameTy->getDecl();
- const ClassTemplateSpecializationDecl *templateSpecializationDecl =
- dyn_cast<ClassTemplateSpecializationDecl>(typeRecordDecl);
- if (templateSpecializationDecl) {
- ClassTemplateDecl *decl =
- templateSpecializationDecl->getSpecializedTemplate();
- if (decl == m_matrixTemplateDecl)
- return AR_TOBJ_MATRIX;
- else if (decl == m_vectorTemplateDecl)
- return AR_TOBJ_VECTOR;
- DXASSERT(decl->isImplicit(),
- "otherwise object template decl is not set to implicit");
- return AR_TOBJ_OBJECT;
- }
- if (typeRecordDecl && typeRecordDecl->isImplicit()) {
- if (typeRecordDecl->getDeclContext()->isFileContext())
- return AR_TOBJ_OBJECT;
- else
- return AR_TOBJ_INNER_OBJ;
- }
- return AR_TOBJ_COMPOUND;
- }
- if (type->isBuiltinType()) return AR_TOBJ_BASIC;
- return AR_TOBJ_INVALID;
- }
- /// <summary>Gets the element type of a matrix or vector type (eg, the 'float' in 'float4x4' or 'float4').</summary>
- QualType GetMatrixOrVectorElementType(QualType type)
- {
- type = GetStructuralForm(type);
- const CXXRecordDecl* typeRecordDecl = type->getAsCXXRecordDecl();
- DXASSERT_NOMSG(typeRecordDecl);
- const ClassTemplateSpecializationDecl* templateSpecializationDecl = dyn_cast<ClassTemplateSpecializationDecl>(typeRecordDecl);
- DXASSERT_NOMSG(templateSpecializationDecl);
- DXASSERT_NOMSG(templateSpecializationDecl->getSpecializedTemplate() == m_matrixTemplateDecl ||
- templateSpecializationDecl->getSpecializedTemplate() == m_vectorTemplateDecl);
- return templateSpecializationDecl->getTemplateArgs().get(0).getAsType();
- }
- /// <summary>Gets the type with structural information (elements and shape) for the given type.</summary>
- /// <remarks>This function will strip lvalue/rvalue references, attributes and qualifiers.</remarks>
- QualType GetStructuralForm(QualType type)
- {
- if (type.isNull()) {
- return type;
- }
- const ReferenceType *RefType = nullptr;
- const AttributedType *AttrType = nullptr;
- while ( (RefType = dyn_cast<ReferenceType>(type)) ||
- (AttrType = dyn_cast<AttributedType>(type)))
- {
- type = RefType ? RefType->getPointeeType() : AttrType->getEquivalentType();
- }
- return type->getCanonicalTypeUnqualified();
- }
- /// <summary>Given a Clang type, return the ArBasicKind classification for its contents.</summary>
- ArBasicKind GetTypeElementKind(QualType type)
- {
- type = GetStructuralForm(type);
- ArTypeObjectKind kind = GetTypeObjectKind(type);
- if (kind == AR_TOBJ_MATRIX || kind == AR_TOBJ_VECTOR) {
- QualType elementType = GetMatrixOrVectorElementType(type);
- return GetTypeElementKind(elementType);
- }
- if (type->isArrayType()) {
- const ArrayType* arrayType = type->getAsArrayTypeUnsafe();
- return GetTypeElementKind(arrayType->getElementType());
- }
- if (kind == AR_TOBJ_INNER_OBJ) {
- return AR_OBJECT_INNER;
- } else if (kind == AR_TOBJ_OBJECT) {
- // Classify the object as the element type.
- const CXXRecordDecl* typeRecordDecl = GetRecordDeclForBuiltInOrStruct(type->getAsCXXRecordDecl());
- int index = FindObjectBasicKindIndex(typeRecordDecl);
- // NOTE: this will likely need to be updated for specialized records
- DXASSERT(index != -1, "otherwise can't find type we already determined was an object");
- return g_ArBasicKindsAsTypes[index];
- }
- CanQualType canType = type->getCanonicalTypeUnqualified();
- return BasicTypeForScalarType(canType);
- }
- ArBasicKind BasicTypeForScalarType(CanQualType type)
- {
- if (const BuiltinType *BT = dyn_cast<BuiltinType>(type))
- {
- switch (BT->getKind())
- {
- case BuiltinType::Bool: return AR_BASIC_BOOL;
- case BuiltinType::Double: return AR_BASIC_FLOAT64;
- case BuiltinType::Float: return AR_BASIC_FLOAT32;
- case BuiltinType::Half: return AR_BASIC_MIN16FLOAT; // rather than AR_BASIC_FLOAT16
- case BuiltinType::Int: return AR_BASIC_INT32;
- case BuiltinType::UInt: return AR_BASIC_UINT32;
- case BuiltinType::Short: return AR_BASIC_MIN16INT; // rather than AR_BASIC_INT16
- case BuiltinType::UShort: return AR_BASIC_MIN16UINT; // rather than AR_BASIC_UINT16
- case BuiltinType::LongLong: return AR_BASIC_INT64;
- case BuiltinType::ULongLong: return AR_BASIC_UINT64;
- case BuiltinType::Min12Int: return AR_BASIC_MIN12INT;
- case BuiltinType::Min10Float: return AR_BASIC_MIN10FLOAT;
- case BuiltinType::LitFloat: return AR_BASIC_LITERAL_FLOAT;
- case BuiltinType::LitInt: return AR_BASIC_LITERAL_INT;
- }
- }
- return AR_BASIC_UNKNOWN;
- }
- void AddIntrinsicTableMethods(_In_ IDxcIntrinsicTable *table) {
- DXASSERT_NOMSG(table != nullptr);
- // Function intrinsics are added on-demand, objects get template methods.
- for (int i = 0; i < _countof(g_ArBasicKindsAsTypes); i++) {
- // Grab information already processed by AddObjectTypes.
- ArBasicKind kind = g_ArBasicKindsAsTypes[i];
- const char *typeName = g_ArBasicTypeNames[kind];
- uint8_t templateArgCount = g_ArBasicKindsTemplateCount[i];
- DXASSERT(0 <= templateArgCount && templateArgCount <= 2,
- "otherwise a new case has been added");
- int startDepth = (templateArgCount == 0) ? 0 : 1;
- CXXRecordDecl *recordDecl = m_objectTypeDecls[i];
- if (recordDecl == nullptr) {
- DXASSERT(kind == AR_OBJECT_WAVE, "else objects other than reserved not initialized");
- continue;
- }
- // This is a variation of AddObjectMethods using the new table.
- const HLSL_INTRINSIC *pIntrinsic = nullptr;
- const HLSL_INTRINSIC *pPrior = nullptr;
- UINT64 lookupCookie = 0;
- CA2W wideTypeName(typeName);
- HRESULT found = table->LookupIntrinsic(wideTypeName, L"*", &pIntrinsic, &lookupCookie);
- while (pIntrinsic != nullptr && SUCCEEDED(found)) {
- if (!AreIntrinsicTemplatesEquivalent(pIntrinsic, pPrior)) {
- AddObjectIntrinsicTemplate(recordDecl, startDepth, pIntrinsic);
- // NOTE: this only works with the current implementation because
- // intrinsics are alive as long as the table is alive.
- pPrior = pIntrinsic;
- }
- found = table->LookupIntrinsic(wideTypeName, L"*", &pIntrinsic, &lookupCookie);
- }
- }
- }
- void RegisterIntrinsicTable(_In_ IDxcIntrinsicTable *table) {
- DXASSERT_NOMSG(table != nullptr);
- m_intrinsicTables.push_back(table);
- // If already initialized, add methods immediately.
- if (m_sema != nullptr) {
- AddIntrinsicTableMethods(table);
- }
- }
- HLSLScalarType ScalarTypeForBasic(ArBasicKind kind)
- {
- DXASSERT(kind < AR_BASIC_COUNT, "otherwise caller didn't check that the value was in range");
- switch (kind) {
- case AR_BASIC_BOOL: return HLSLScalarType_bool;
- case AR_BASIC_LITERAL_FLOAT: return HLSLScalarType_float_lit;
- case AR_BASIC_FLOAT16: return HLSLScalarType_float_min16;
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- return HLSLScalarType_float;
- case AR_BASIC_FLOAT32: return HLSLScalarType_float;
- case AR_BASIC_FLOAT64: return HLSLScalarType_double;
- case AR_BASIC_LITERAL_INT: return HLSLScalarType_int_lit;
- case AR_BASIC_INT8: return HLSLScalarType_int;
- case AR_BASIC_UINT8: return HLSLScalarType_uint;
- case AR_BASIC_INT16: return HLSLScalarType_uint;
- case AR_BASIC_UINT16: return HLSLScalarType_uint;
- case AR_BASIC_INT32: return HLSLScalarType_int;
- case AR_BASIC_UINT32: return HLSLScalarType_uint;
- case AR_BASIC_MIN10FLOAT: return HLSLScalarType_float_min10;
- case AR_BASIC_MIN16FLOAT: return HLSLScalarType_float_min16;
- case AR_BASIC_MIN12INT: return HLSLScalarType_int_min12;
- case AR_BASIC_MIN16INT: return HLSLScalarType_int_min16;
- case AR_BASIC_MIN16UINT: return HLSLScalarType_uint_min16;
- case AR_BASIC_INT64: return HLSLScalarType_int64;
- case AR_BASIC_UINT64: return HLSLScalarType_uint64;
- default:
- return HLSLScalarType_unknown;
- }
- }
- QualType GetBasicKindType(ArBasicKind kind)
- {
- DXASSERT_VALIDBASICKIND(kind);
- switch (kind) {
- case AR_OBJECT_NULL: return m_context->VoidTy;
- case AR_BASIC_BOOL: return m_context->BoolTy;
- case AR_BASIC_LITERAL_FLOAT: return m_context->LitFloatTy;
- case AR_BASIC_FLOAT16: return m_context->FloatTy;
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION: return m_context->FloatTy;
- case AR_BASIC_FLOAT32: return m_context->FloatTy;
- case AR_BASIC_FLOAT64: return m_context->DoubleTy;
- case AR_BASIC_LITERAL_INT: return m_context->LitIntTy;
- case AR_BASIC_INT8: return m_context->IntTy;
- case AR_BASIC_UINT8: return m_context->UnsignedIntTy;
- case AR_BASIC_INT16: return m_context->IntTy;
- case AR_BASIC_UINT16: return m_context->UnsignedIntTy;
- case AR_BASIC_INT32: return m_context->IntTy;
- case AR_BASIC_UINT32: return m_context->UnsignedIntTy;
- case AR_BASIC_INT64: return m_context->LongLongTy;
- case AR_BASIC_UINT64: return m_context->UnsignedLongLongTy;
- case AR_BASIC_MIN10FLOAT: return m_scalarTypes[HLSLScalarType_float_min10];
- case AR_BASIC_MIN16FLOAT: return m_scalarTypes[HLSLScalarType_float_min16];
- case AR_BASIC_MIN12INT: return m_scalarTypes[HLSLScalarType_int_min12];
- case AR_BASIC_MIN16INT: return m_scalarTypes[HLSLScalarType_int_min16];
- case AR_BASIC_MIN16UINT: return m_scalarTypes[HLSLScalarType_uint_min16];
- case AR_OBJECT_STRING: return QualType();
- case AR_OBJECT_LEGACY_EFFECT: // used for all legacy effect object types
- case AR_OBJECT_TEXTURE1D:
- case AR_OBJECT_TEXTURE1D_ARRAY:
- case AR_OBJECT_TEXTURE2D:
- case AR_OBJECT_TEXTURE2D_ARRAY:
- case AR_OBJECT_TEXTURE3D:
- case AR_OBJECT_TEXTURECUBE:
- case AR_OBJECT_TEXTURECUBE_ARRAY:
- case AR_OBJECT_TEXTURE2DMS:
- case AR_OBJECT_TEXTURE2DMS_ARRAY:
- case AR_OBJECT_SAMPLER:
- case AR_OBJECT_SAMPLERCOMPARISON:
- case AR_OBJECT_BUFFER:
- case AR_OBJECT_POINTSTREAM:
- case AR_OBJECT_LINESTREAM:
- case AR_OBJECT_TRIANGLESTREAM:
- case AR_OBJECT_INPUTPATCH:
- case AR_OBJECT_OUTPUTPATCH:
- case AR_OBJECT_RWTEXTURE1D:
- case AR_OBJECT_RWTEXTURE1D_ARRAY:
- case AR_OBJECT_RWTEXTURE2D:
- case AR_OBJECT_RWTEXTURE2D_ARRAY:
- case AR_OBJECT_RWTEXTURE3D:
- case AR_OBJECT_RWBUFFER:
- case AR_OBJECT_BYTEADDRESS_BUFFER:
- case AR_OBJECT_RWBYTEADDRESS_BUFFER:
- case AR_OBJECT_STRUCTURED_BUFFER:
- case AR_OBJECT_RWSTRUCTURED_BUFFER:
- case AR_OBJECT_APPEND_STRUCTURED_BUFFER:
- case AR_OBJECT_CONSUME_STRUCTURED_BUFFER:
- case AR_OBJECT_WAVE:
- {
- const ArBasicKind* match = std::find(g_ArBasicKindsAsTypes, &g_ArBasicKindsAsTypes[_countof(g_ArBasicKindsAsTypes)], kind);
- DXASSERT(match != &g_ArBasicKindsAsTypes[_countof(g_ArBasicKindsAsTypes)], "otherwise can't find constant in basic kinds");
- size_t index = match - g_ArBasicKindsAsTypes;
- return m_context->getTagDeclType(this->m_objectTypeDecls[index]);
- }
- case AR_OBJECT_SAMPLER1D:
- case AR_OBJECT_SAMPLER2D:
- case AR_OBJECT_SAMPLER3D:
- case AR_OBJECT_SAMPLERCUBE:
- // Turn dimension-typed samplers into sampler states.
- return GetBasicKindType(AR_OBJECT_SAMPLER);
- case AR_OBJECT_STATEBLOCK:
- case AR_OBJECT_RASTERIZER:
- case AR_OBJECT_DEPTHSTENCIL:
- case AR_OBJECT_BLEND:
- case AR_OBJECT_RWSTRUCTURED_BUFFER_ALLOC:
- case AR_OBJECT_RWSTRUCTURED_BUFFER_CONSUME:
- default:
- return QualType();
- }
- }
- /// <summary>Promotes the specified expression to an integer type if it's a boolean type.</summary
- /// <param name="E">Expression to typecast.</param>
- /// <returns>E typecast to a integer type if it's a valid boolean type; E otherwise.</returns>
- ExprResult PromoteToIntIfBool(ExprResult& E);
- QualType NewQualifiedType(UINT64 qwUsages, QualType type)
- {
- // NOTE: NewQualifiedType does quite a bit more in the prior compiler
- (qwUsages);
- return type;
- }
- QualType NewSimpleAggregateType(
- _In_ ArTypeObjectKind ExplicitKind,
- _In_ ArBasicKind componentType,
- _In_ UINT64 qwQual,
- _In_ UINT uRows,
- _In_ UINT uCols)
- {
- DXASSERT_VALIDBASICKIND(componentType);
- QualType pType; // The type to return.
- QualType pEltType = GetBasicKindType(componentType);
- DXASSERT(!pEltType.isNull(), "otherwise caller is specifying an incorrect basic kind type");
- // TODO: handle adding qualifications like const
- pType = NewQualifiedType(
- qwQual & ~(UINT64)(AR_QUAL_COLMAJOR | AR_QUAL_ROWMAJOR),
- pEltType);
- if (uRows > 1 ||
- uCols > 1 ||
- ExplicitKind == AR_TOBJ_VECTOR ||
- ExplicitKind == AR_TOBJ_MATRIX)
- {
- HLSLScalarType scalarType = ScalarTypeForBasic(componentType);
- DXASSERT(scalarType != HLSLScalarType_unknown, "otherwise caller is specifying an incorrect type");
- if ((uRows == 1 &&
- ExplicitKind != AR_TOBJ_MATRIX) ||
- ExplicitKind == AR_TOBJ_VECTOR)
- {
- pType = LookupVectorType(scalarType, uCols);
- }
- else
- {
- pType = LookupMatrixType(scalarType, uRows, uCols);
- }
- // TODO: handle colmajor/rowmajor
- //if ((qwQual & (AR_QUAL_COLMAJOR | AR_QUAL_ROWMAJOR)) != 0)
- //{
- // VN(pType = NewQualifiedType(pSrcLoc,
- // qwQual & (AR_QUAL_COLMAJOR |
- // AR_QUAL_ROWMAJOR),
- // pMatrix));
- //}
- //else
- //{
- // pType = pMatrix;
- //}
- }
- return pType;
- }
- /// <summary>Attempts to match Args to the signature specification in pIntrinsic.</summary>
- /// <param name="pIntrinsic">Intrinsic function to match.</param>
- /// <param name="objectElement">Type element on the class intrinsic belongs to; possibly null (eg, 'float' in 'Texture2D<float>').</param>
- /// <param name="Args">Invocation arguments to match.</param>
- /// <param name="argTypes">After exectuion, type of arguments.</param>
- /// <param name="argCount">After execution, number of arguments in argTypes.</param>
- /// <remarks>On success, argTypes includes the clang Types to use for the signature, with the first being the return type.</remarks>
- bool MatchArguments(
- _In_ const HLSL_INTRINSIC *pIntrinsic,
- _In_ QualType objectElement,
- _In_ ArrayRef<Expr *> Args,
- _Out_writes_(g_MaxIntrinsicParamCount + 1) QualType(&argTypes)[g_MaxIntrinsicParamCount + 1],
- _Out_range_(0, g_MaxIntrinsicParamCount + 1) size_t* argCount);
- /// <summary>Validate object element on intrinsic to catch case like integer on Sample.</summary>
- /// <param name="pIntrinsic">Intrinsic function to validate.</param>
- /// <param name="objectElement">Type element on the class intrinsic belongs to; possibly null (eg, 'float' in 'Texture2D<float>').</param>
- bool IsValidateObjectElement(
- _In_ const HLSL_INTRINSIC *pIntrinsic,
- _In_ QualType objectElement);
- IntrinsicDefIter FindIntrinsicByNameAndArgCount(
- _In_count_(tableSize) const HLSL_INTRINSIC* table,
- size_t tableSize,
- StringRef typeName,
- StringRef nameIdentifier,
- size_t argumentCount)
- {
- // TODO: avoid linear scan
- for (unsigned int i = 0; i < tableSize; i++) {
- const HLSL_INTRINSIC* pIntrinsic = &table[i];
- // Do some quick checks to verify size and name.
- if (pIntrinsic->uNumArgs != 1 + argumentCount) {
- continue;
- }
- if (!nameIdentifier.equals(StringRef(pIntrinsic->pArgs[0].pName))) {
- continue;
- }
- return IntrinsicDefIter::CreateStart(table, tableSize, pIntrinsic,
- IntrinsicTableDefIter::CreateStart(m_intrinsicTables, typeName, nameIdentifier, argumentCount));
- }
- return IntrinsicDefIter::CreateStart(table, tableSize, table + tableSize,
- IntrinsicTableDefIter::CreateStart(m_intrinsicTables, typeName, nameIdentifier, argumentCount));
- }
- bool AddOverloadedCallCandidates(
- UnresolvedLookupExpr *ULE,
- ArrayRef<Expr *> Args,
- OverloadCandidateSet &CandidateSet,
- bool PartialOverloading) override
- {
- DXASSERT_NOMSG(ULE != nullptr);
- const DeclarationNameInfo declName = ULE->getNameInfo();
- IdentifierInfo* idInfo = declName.getName().getAsIdentifierInfo();
- if (idInfo == nullptr)
- {
- return false;
- }
- StringRef nameIdentifier = idInfo->getName();
- IntrinsicDefIter cursor = FindIntrinsicByNameAndArgCount(
- g_Intrinsics, _countof(g_Intrinsics), StringRef(), nameIdentifier, Args.size());
- IntrinsicDefIter end = IntrinsicDefIter::CreateEnd(
- g_Intrinsics, _countof(g_Intrinsics), IntrinsicTableDefIter::CreateEnd(m_intrinsicTables));
- while (cursor != end)
- {
- // If this is the intrinsic we're interested in, build up a representation
- // of the types we need.
- const HLSL_INTRINSIC* pIntrinsic = *cursor;
- LPCSTR tableName = cursor.GetTableName();
- LPCSTR lowering = cursor.GetLoweringStrategy();
- DXASSERT(
- pIntrinsic->uNumArgs <= g_MaxIntrinsicParamCount + 1,
- "otherwise g_MaxIntrinsicParamCount needs to be updated for wider signatures");
- QualType functionArgTypes[g_MaxIntrinsicParamCount + 1];
- size_t functionArgTypeCount = 0;
- if (!MatchArguments(pIntrinsic, QualType(), Args, functionArgTypes, &functionArgTypeCount))
- {
- ++cursor;
- continue;
- }
- // Get or create the overload we're interested in.
- FunctionDecl* intrinsicFuncDecl = nullptr;
- std::pair<UsedIntrinsicStore::iterator, bool> insertResult = m_usedIntrinsics.insert(UsedIntrinsic(
- pIntrinsic, functionArgTypes, functionArgTypeCount));
- bool insertedNewValue = insertResult.second;
- if (insertedNewValue)
- {
- DXASSERT(tableName, "otherwise IDxcIntrinsicTable::GetTableName() failed");
- intrinsicFuncDecl = AddHLSLIntrinsicFunction(*m_context, m_hlslNSDecl, tableName, lowering, pIntrinsic, functionArgTypes, functionArgTypeCount);
- insertResult.first->setFunctionDecl(intrinsicFuncDecl);
- }
- else
- {
- intrinsicFuncDecl = (*insertResult.first).getFunctionDecl();
- }
- OverloadCandidate& candidate = CandidateSet.addCandidate();
- candidate.Function = intrinsicFuncDecl;
- candidate.FoundDecl.setDecl(intrinsicFuncDecl);
- candidate.Viable = true;
- return true;
- }
- return false;
- }
- bool Initialize(ASTContext& context)
- {
- m_context = &context;
- m_hlslNSDecl = NamespaceDecl::Create(context, context.getTranslationUnitDecl(),
- /*Inline*/ false, SourceLocation(),
- SourceLocation(), &context.Idents.get("hlsl"),
- /*PrevDecl*/ nullptr);
- m_hlslNSDecl->setImplicit();
- AddHLSLScalarTypes();
- AddHLSLVectorTemplate(*m_context, &m_vectorTemplateDecl);
- DXASSERT(m_vectorTemplateDecl != nullptr, "AddHLSLVectorTypes failed to return the vector template declaration");
- AddHLSLMatrixTemplate(*m_context, m_vectorTemplateDecl, &m_matrixTemplateDecl);
- DXASSERT(m_matrixTemplateDecl != nullptr, "AddHLSLMatrixTypes failed to return the matrix template declaration");
- return true;
- }
- /// <summary>Checks whether the specified type is numeric or composed of numeric elements exclusively.</summary>
- bool IsTypeNumeric(QualType type, _Out_ UINT* count);
- /// <summary>Checks whether the specified type is a scalar type.</summary>
- bool IsScalarType(const QualType& type) {
- DXASSERT(!type.isNull(), "caller should validate its type is initialized");
- return BasicTypeForScalarType(type->getCanonicalTypeUnqualified()) != AR_BASIC_UNKNOWN;
- }
- /// <summary>Checks whether the specified value is a valid vector size.</summary>
- bool IsValidVectorSize(size_t length) {
- return 1 <= length && length <= 4;
- }
- /// <summary>Checks whether the specified value is a valid matrix row or column size.</summary>
- bool IsValidMatrixColOrRowSize(size_t length) {
- return 1 <= length && length <= 4;
- }
- bool IsValidTemplateArgumentType(SourceLocation argLoc, const QualType& type, bool requireScalar) {
- if (type.isNull()) {
- return false;
- }
- if (type.hasQualifiers()) {
- return false;
- }
- // TemplateTypeParm here will be construction of vector return template in matrix operator[]
- if (type->getTypeClass() == Type::TemplateTypeParm)
- return true;
- QualType qt = GetStructuralForm(type);
- if (requireScalar) {
- if (!IsScalarType(qt)) {
- m_sema->Diag(argLoc, diag::err_hlsl_typeintemplateargument_requires_scalar) << type;
- return false;
- }
- return true;
- }
- else {
- ArTypeObjectKind objectKind = GetTypeObjectKind(qt);
- if (qt->isArrayType()) {
- const ArrayType* arrayType = qt->getAsArrayTypeUnsafe();
- return IsValidTemplateArgumentType(argLoc, arrayType->getElementType(), false);
- }
- else if (objectKind == AR_TOBJ_VECTOR) {
- bool valid = true;
- if (!IsValidVectorSize(GetHLSLVecSize(type))) {
- valid = false;
- m_sema->Diag(argLoc, diag::err_hlsl_unsupportedvectorsize) << type << GetHLSLVecSize(type);
- }
- if (!IsScalarType(GetMatrixOrVectorElementType(type))) {
- valid = false;
- m_sema->Diag(argLoc, diag::err_hlsl_unsupportedvectortype) << type << GetMatrixOrVectorElementType(type);
- }
- return valid;
- }
- else if (objectKind == AR_TOBJ_MATRIX) {
- bool valid = true;
- UINT rowCount, colCount;
- GetRowsAndCols(type, rowCount, colCount);
- if (!IsValidMatrixColOrRowSize(rowCount) || !IsValidMatrixColOrRowSize(colCount)) {
- valid = false;
- m_sema->Diag(argLoc, diag::err_hlsl_unsupportedmatrixsize) << type << rowCount << colCount;
- }
- if (!IsScalarType(GetMatrixOrVectorElementType(type))) {
- valid = false;
- m_sema->Diag(argLoc, diag::err_hlsl_unsupportedvectortype) << type << GetMatrixOrVectorElementType(type);
- }
- return valid;
- }
- else if (qt->isStructureType()) {
- const RecordType* recordType = qt->getAsStructureType();
- objectKind = ClassifyRecordType(recordType);
- switch (objectKind)
- {
- case AR_TOBJ_OBJECT:
- m_sema->Diag(argLoc, diag::err_hlsl_objectintemplateargument) << type;
- return false;
- case AR_TOBJ_COMPOUND:
- {
- const RecordDecl* recordDecl = recordType->getDecl();
- RecordDecl::field_iterator begin = recordDecl->field_begin();
- RecordDecl::field_iterator end = recordDecl->field_end();
- bool result = true;
- while (begin != end) {
- const FieldDecl* fieldDecl = *begin;
- if (!IsValidTemplateArgumentType(argLoc, fieldDecl->getType(), false)) {
- m_sema->Diag(argLoc, diag::note_field_type_usage)
- << fieldDecl->getType() << fieldDecl->getIdentifier() << type;
- result = false;
- }
- begin++;
- }
- return result;
- }
- default:
- m_sema->Diag(argLoc, diag::err_hlsl_typeintemplateargument) << type;
- return false;
- }
- }
- else if(IsScalarType(qt)) {
- return true;
- }
- else {
- m_sema->Diag(argLoc, diag::err_hlsl_typeintemplateargument) << type;
- return false;
- }
- }
- }
- /// <summary>Checks whether the source type can be converted to the target type.</summary>
- bool CanConvert(SourceLocation loc, Expr* sourceExpr, QualType target, bool explicitConversion,
- _Out_opt_ TYPE_CONVERSION_REMARKS* remarks,
- _Inout_opt_ StandardConversionSequence* sequence);
- /// <summary>Produces an expression that turns the given expression into the specified numeric type.</summary>
- Expr* CastExprToTypeNumeric(Expr* expr, QualType targetType);
- void CollectInfo(QualType type, _Out_ ArTypeInfo* pTypeInfo);
- void GetConversionForm(
- QualType type,
- bool explicitConversion,
- ArTypeInfo* pTypeInfo);
- bool ValidateCast(SourceLocation Loc, _In_ Expr* source, QualType target, bool explicitConversion,
- bool suppressWarnings, bool suppressErrors,
- _Inout_opt_ StandardConversionSequence* sequence);
- bool ValidatePrimitiveTypeForOperand(SourceLocation loc, QualType type, ArTypeObjectKind kind);
- bool ValidateTypeRequirements(
- SourceLocation loc,
- ArBasicKind elementKind,
- ArTypeObjectKind objectKind,
- bool requiresIntegrals,
- bool requiresNumerics);
- /// <summary>Validates and adjusts operands for the specified binary operator.</summary>
- /// <param name="OpLoc">Source location for operator.</param>
- /// <param name="Opc">Kind of binary operator.</param>
- /// <param name="LHS">Left-hand-side expression, possibly updated by this function.</param>
- /// <param name="RHS">Right-hand-side expression, possibly updated by this function.</param>
- /// <param name="ResultTy">Result type for operator expression.</param>
- /// <param name="CompLHSTy">Type of LHS after promotions for computation.</param>
- /// <param name="CompResultTy">Type of computation result.</param>
- void CheckBinOpForHLSL(
- SourceLocation OpLoc,
- BinaryOperatorKind Opc,
- ExprResult& LHS,
- ExprResult& RHS,
- QualType& ResultTy,
- QualType& CompLHSTy,
- QualType& CompResultTy);
- /// <summary>Validates and adjusts operands for the specified unary operator.</summary>
- /// <param name="OpLoc">Source location for operator.</param>
- /// <param name="Opc">Kind of operator.</param>
- /// <param name="InputExpr">Input expression to the operator.</param>
- /// <param name="VK">Value kind for resulting expression.</param>
- /// <param name="OK">Object kind for resulting expression.</param>
- /// <returns>The result type for the expression.</returns>
- QualType CheckUnaryOpForHLSL(
- SourceLocation OpLoc,
- UnaryOperatorKind Opc,
- ExprResult& InputExpr,
- ExprValueKind& VK,
- ExprObjectKind& OK);
- /// <summary>Checks vector conditional operator (Cond ? LHS : RHS).</summary>
- /// <param name="Cond">Vector condition expression.</param>
- /// <param name="LHS">Left hand side.</param>
- /// <param name="RHS">Right hand side.</param>
- /// <param name="QuestionLoc">Location of question mark in operator.</param>
- /// <returns>Result type of vector conditional expression.</returns>
- clang::QualType HLSLExternalSource::CheckVectorConditional(
- _In_ ExprResult &Cond,
- _In_ ExprResult &LHS,
- _In_ ExprResult &RHS,
- _In_ SourceLocation QuestionLoc);
- clang::QualType ApplyTypeSpecSignToParsedType(
- _In_ clang::QualType &type,
- _In_ TypeSpecifierSign TSS,
- _In_ SourceLocation Loc
- );
- bool CheckRangedTemplateArgument(SourceLocation diagLoc, llvm::APSInt& sintValue)
- {
- if (!sintValue.isStrictlyPositive() || sintValue.getLimitedValue() > 4)
- {
- m_sema->Diag(diagLoc, diag::err_hlsl_invalid_range_1_4);
- return true;
- }
- return false;
- }
- /// <summary>Performs HLSL-specific processing of template declarations.</summary>
- bool
- CheckTemplateArgumentListForHLSL(_In_ TemplateDecl *Template,
- SourceLocation /* TemplateLoc */,
- TemplateArgumentListInfo &TemplateArgList) {
- DXASSERT_NOMSG(Template != nullptr);
- // Determine which object type the template refers to.
- StringRef templateName = Template->getName();
- // NOTE: this 'escape valve' allows unit tests to perform type checks.
- if (templateName.equals(StringRef("is_same"))) {
- return false;
- }
- bool isMatrix = Template->getCanonicalDecl() ==
- m_matrixTemplateDecl->getCanonicalDecl();
- bool isVector = Template->getCanonicalDecl() ==
- m_vectorTemplateDecl->getCanonicalDecl();
- bool requireScalar = isMatrix || isVector;
-
- // Check constraints on the type. Right now we only check that template
- // types are primitive types.
- for (unsigned int i = 0; i < TemplateArgList.size(); i++) {
- const TemplateArgumentLoc &argLoc = TemplateArgList[i];
- SourceLocation argSrcLoc = argLoc.getLocation();
- const TemplateArgument &arg = argLoc.getArgument();
- if (arg.getKind() == TemplateArgument::ArgKind::Type) {
- QualType argType = arg.getAsType();
- if (!IsValidTemplateArgumentType(argSrcLoc, argType, requireScalar)) {
- // NOTE: IsValidTemplateArgumentType emits its own diagnostics
- return true;
- }
- }
- else if (arg.getKind() == TemplateArgument::ArgKind::Expression) {
- if (isMatrix || isVector) {
- Expr *expr = arg.getAsExpr();
- llvm::APSInt constantResult;
- if (expr != nullptr &&
- expr->isIntegerConstantExpr(constantResult, *m_context)) {
- if (CheckRangedTemplateArgument(argSrcLoc, constantResult)) {
- return true;
- }
- }
- }
- }
- else if (arg.getKind() == TemplateArgument::ArgKind::Integral) {
- if (isMatrix || isVector) {
- llvm::APSInt Val = arg.getAsIntegral();
- if (CheckRangedTemplateArgument(argSrcLoc, Val)) {
- return true;
- }
- }
- }
- }
- return false;
- }
- /// <summary>Diagnoses an assignment operation.</summary>
- /// <param name="ConvTy">Type of conversion assignment.</param>
- /// <param name="Loc">Location for operation.</param>
- /// <param name="DstType">Destination type.</param>
- /// <param name="SrcType">Source type.</param>
- /// <param name="SrcExpr">Source expression.</param>
- /// <param name="Action">Action that triggers the assignment (assignment, passing, return, etc).</param>
- /// <param name="Complained">Whether a diagnostic was emitted.</param>
- void DiagnoseAssignmentResultForHLSL(
- Sema::AssignConvertType ConvTy,
- SourceLocation Loc,
- QualType DstType, QualType SrcType,
- _In_ Expr *SrcExpr, Sema::AssignmentAction Action,
- _Out_opt_ bool *Complained);
- FindStructBasicTypeResult FindStructBasicType(_In_ DeclContext* functionDeclContext);
- /// <summary>Finds the table of intrinsics for the declaration context of a member function.</summary>
- /// <param name="functionDeclContext">Declaration context of function.</param>
- /// <param name="name">After execution, the name of the object to which the table applies.</param>
- /// <param name="intrinsics">After execution, the intrinsic table.</param>
- /// <param name="intrinsicCount">After execution, the count of elements in the intrinsic table.</param>
- void FindIntrinsicTable(
- _In_ DeclContext* functionDeclContext,
- _Outptr_result_z_ const char** name,
- _Outptr_result_buffer_(*intrinsicCount) const HLSL_INTRINSIC** intrinsics,
- _Out_ size_t* intrinsicCount);
- /// <summary>Deduces the template arguments by comparing the argument types and the HLSL intrinsic tables.</summary>
- /// <param name="FunctionTemplate">The declaration for the function template being deduced.</param>
- /// <param name="ExplicitTemplateArgs">Explicitly-provided template arguments. Should be empty for an HLSL program.</param>
- /// <param name="Args">Array of expressions being used as arguments.</param>
- /// <param name="Specialization">The declaration for the resolved specialization.</param>
- /// <param name="Info">Provides information about an attempted template argument deduction.</param>
- /// <returns>The result of the template deduction, TDK_Invalid if no HLSL-specific processing done.</returns>
- Sema::TemplateDeductionResult DeduceTemplateArgumentsForHLSL(
- FunctionTemplateDecl *FunctionTemplate,
- TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
- FunctionDecl *&Specialization, TemplateDeductionInfo &Info);
- clang::OverloadingResult GetBestViableFunction(
- clang::SourceLocation Loc,
- clang::OverloadCandidateSet& set,
- clang::OverloadCandidateSet::iterator& Best);
- /// <summary>
- /// Initializes the specified <paramref name="initSequence" /> describing how
- /// <paramref name="Entity" /> is initialized with <paramref name="Args" />.
- /// </summary>
- /// <param name="Entity">Entity being initialized; a variable, return result, etc.</param>
- /// <param name="Kind">Kind of initialization: copying, list-initializing, constructing, etc.</param>
- /// <param name="Args">Arguments to the initialization.</param>
- /// <param name="TopLevelOfInitList">Whether this is the top-level of an initialization list.</param>
- /// <param name="initSequence">Initialization sequence description to initialize.</param>
- void InitializeInitSequenceForHLSL(
- const InitializedEntity& Entity,
- const InitializationKind& Kind,
- MultiExprArg Args,
- bool TopLevelOfInitList,
- _Inout_ InitializationSequence* initSequence);
- /// <summary>
- /// Checks whether the specified conversion occurs to a type of idential element type but less elements.
- /// </summary>
- /// <remarks>This is an important case because a cast of this type does not turn an lvalue into an rvalue.</remarks>
- bool IsConversionToLessOrEqualElements(
- const ExprResult& sourceExpr,
- const QualType& targetType,
- bool explicitConversion);
- /// <summary>
- /// Checks whether the specified conversion occurs to a type of idential element type but less elements.
- /// </summary>
- /// <remarks>This is an important case because a cast of this type does not turn an lvalue into an rvalue.</remarks>
- bool IsConversionToLessOrEqualElements(
- const QualType& sourceType,
- const QualType& targetType,
- bool explicitConversion);
- /// <summary>Performs a member lookup on the specified BaseExpr if it's a matrix.</summary>
- /// <param name="BaseExpr">Base expression for member access.</param>
- /// <param name="MemberName">Name of member to look up.</param>
- /// <param name="IsArrow">Whether access is through arrow (a->b) rather than period (a.b).</param>
- /// <param name="OpLoc">Location of access operand.</param>
- /// <param name="MemberLoc">Location of member.</param>
- /// <param name="result">Result of lookup operation.</param>
- /// <returns>true if the base type is a matrix and the lookup has been handled.</returns>
- bool LookupMatrixMemberExprForHLSL(
- Expr& BaseExpr,
- DeclarationName MemberName,
- bool IsArrow,
- SourceLocation OpLoc,
- SourceLocation MemberLoc,
- ExprResult* result);
- /// <summary>Performs a member lookup on the specified BaseExpr if it's a vector.</summary>
- /// <param name="BaseExpr">Base expression for member access.</param>
- /// <param name="MemberName">Name of member to look up.</param>
- /// <param name="IsArrow">Whether access is through arrow (a->b) rather than period (a.b).</param>
- /// <param name="OpLoc">Location of access operand.</param>
- /// <param name="MemberLoc">Location of member.</param>
- /// <param name="result">Result of lookup operation.</param>
- /// <returns>true if the base type is a vector and the lookup has been handled.</returns>
- bool LookupVectorMemberExprForHLSL(
- Expr& BaseExpr,
- DeclarationName MemberName,
- bool IsArrow,
- SourceLocation OpLoc,
- SourceLocation MemberLoc,
- ExprResult* result);
- /// <summary>If E is a scalar, converts it to a 1-element vector.</summary>
- /// <param name="E">Expression to convert.</param>
- /// <returns>The result of the conversion; or E if the type is not a scalar.</returns>
- ExprResult MaybeConvertScalarToVector(_In_ clang::Expr* E);
- clang::Expr *HLSLImpCastToScalar(
- _In_ clang::Sema* self,
- _In_ clang::Expr* From,
- ArTypeObjectKind FromShape,
- ArBasicKind EltKind);
- clang::ExprResult PerformHLSLConversion(
- _In_ clang::Expr* From,
- _In_ clang::QualType targetType,
- _In_ const clang::StandardConversionSequence &SCS,
- _In_ clang::Sema::CheckedConversionKind CCK);
- /// <summary>Diagnoses an error when precessing the specified type if nesting is too deep.</summary>
- void ReportUnsupportedTypeNesting(SourceLocation loc, QualType type);
- /// <summary>
- /// Checks if a static cast can be performed, and performs it if possible.
- /// </summary>
- /// <param name="SrcExpr">Expression to cast.</param>
- /// <param name="DestType">Type to cast SrcExpr to.</param>
- /// <param name="CCK">Kind of conversion: implicit, C-style, functional, other.</param>
- /// <param name="OpRange">Source range for the cast operation.</param>
- /// <param name="msg">Error message from the diag::* enumeration to fail with; zero to suppress messages.</param>
- /// <param name="Kind">The kind of operation required for a conversion.</param>
- /// <param name="BasePath">A simple array of base specifiers.</param>
- /// <param name="ListInitialization">Whether the cast is in the context of a list initialization.</param>
- /// <param name="SuppressWarnings">Whether warnings should be omitted.</param>
- /// <param name="SuppressErrors">Whether errors should be omitted.</param>
- bool TryStaticCastForHLSL(ExprResult &SrcExpr,
- QualType DestType,
- Sema::CheckedConversionKind CCK,
- const SourceRange &OpRange, unsigned &msg,
- CastKind &Kind, CXXCastPath &BasePath,
- bool ListInitialization, bool SuppressWarnings, bool SuppressErrors,
- _Inout_opt_ StandardConversionSequence* standard);
- /// <summary>
- /// Checks if a subscript index argument can be initialized from the given expression.
- /// </summary>
- /// <param name="SrcExpr">Source expression used as argument.</param>
- /// <param name="DestType">Parameter type to initialize.</param>
- /// <remarks>
- /// Rules for subscript index initialization follow regular implicit casting rules, with the exception that
- /// no changes in arity are allowed (i.e., int2 can become uint2, but uint or uint3 cannot).
- /// </remarks>
- ImplicitConversionSequence TrySubscriptIndexInitialization(_In_ clang::Expr* SrcExpr, clang::QualType DestType);
- void AddHLSLObjectMethodsIfNotReady(QualType qt) {
- static_assert((sizeof(uint64_t)*8) >= _countof(g_ArBasicKindsAsTypes), "Bitmask size is too small");
- // Everything is ready.
- if (m_objectTypeLazyInitMask == 0)
- return;
- CXXRecordDecl *recordDecl = const_cast<CXXRecordDecl *>(GetRecordDeclForBuiltInOrStruct(qt->getAsCXXRecordDecl()));
- int idx = FindObjectBasicKindIndex(recordDecl);
- // Not object type.
- if (idx == -1)
- return;
- uint64_t bit = ((uint64_t)1)<<idx;
- // Already created.
- if ((m_objectTypeLazyInitMask & bit) == 0)
- return;
-
- ArBasicKind kind = g_ArBasicKindsAsTypes[idx];
- uint8_t templateArgCount = g_ArBasicKindsTemplateCount[idx];
-
- int startDepth = 0;
- if (templateArgCount > 0) {
- DXASSERT(templateArgCount == 1 || templateArgCount == 2,
- "otherwise a new case has been added");
- ClassTemplateDecl *typeDecl = recordDecl->getDescribedClassTemplate();
- AddObjectSubscripts(kind, typeDecl, recordDecl,
- g_ArBasicKindsSubscripts[idx]);
- startDepth = 1;
- }
- AddObjectMethods(kind, recordDecl, startDepth);
- // Clear the object.
- m_objectTypeLazyInitMask &= ~bit;
- }
- FunctionDecl* AddHLSLIntrinsicMethod(
- LPCSTR tableName,
- LPCSTR lowering,
- _In_ const HLSL_INTRINSIC* intrinsic,
- _In_ FunctionTemplateDecl *FunctionTemplate,
- ArrayRef<Expr *> Args,
- _In_count_(parameterTypeCount) QualType* parameterTypes,
- size_t parameterTypeCount)
- {
- DXASSERT_NOMSG(intrinsic != nullptr);
- DXASSERT_NOMSG(FunctionTemplate != nullptr);
- DXASSERT_NOMSG(parameterTypes != nullptr);
- DXASSERT(parameterTypeCount >= 1, "otherwise caller didn't initialize - there should be at least a void return type");
- // Create the template arguments.
- SmallVector<TemplateArgument, g_MaxIntrinsicParamCount + 1> templateArgs;
- for (size_t i = 0; i < parameterTypeCount; i++) {
- templateArgs.push_back(TemplateArgument(parameterTypes[i]));
- }
- // Look for an existing specialization.
- void *InsertPos = nullptr;
- FunctionDecl *SpecFunc =
- FunctionTemplate->findSpecialization(templateArgs, InsertPos);
- if (SpecFunc != nullptr) {
- return SpecFunc;
- }
- // Change return type to rvalue reference type for aggregate types
- QualType retTy = parameterTypes[0];
- if (retTy->isAggregateType() && !IsHLSLVecMatType(retTy))
- parameterTypes[0] = m_context->getRValueReferenceType(retTy);
- // Create a new specialization.
- SmallVector<ParameterModifier, g_MaxIntrinsicParamCount> paramMods;
- InitParamMods(intrinsic, paramMods);
- for (unsigned int i = 1; i < parameterTypeCount; i++) {
- // Change out/inout parameter type to rvalue reference type.
- if (paramMods[i - 1].isAnyOut()) {
- parameterTypes[i] = m_context->getLValueReferenceType(parameterTypes[i]);
- }
- }
- IntrinsicOp intrinOp = static_cast<IntrinsicOp>(intrinsic->Op);
- if (intrinOp == IntrinsicOp::MOP_SampleBias) {
- // Remove this when update intrinsic table not affect other things.
- // Change vector<float,1> into float for bias.
- const unsigned biasOperandID = 3; // return type, sampler, coord, bias.
- DXASSERT(parameterTypeCount > biasOperandID,
- "else operation was misrecognized");
- if (const ExtVectorType *VecTy =
- hlsl::ConvertHLSLVecMatTypeToExtVectorType(
- *m_context, parameterTypes[biasOperandID])) {
- if (VecTy->getNumElements() == 1)
- parameterTypes[biasOperandID] = VecTy->getElementType();
- }
- }
- DeclContext *owner = FunctionTemplate->getDeclContext();
- TemplateArgumentList templateArgumentList(
- TemplateArgumentList::OnStackType::OnStack, templateArgs.data(),
- templateArgs.size());
- MultiLevelTemplateArgumentList mlTemplateArgumentList(templateArgumentList);
- TemplateDeclInstantiator declInstantiator(*this->m_sema, owner,
- mlTemplateArgumentList);
- FunctionProtoType::ExtProtoInfo EmptyEPI;
- QualType functionType = m_context->getFunctionType(
- parameterTypes[0],
- ArrayRef<QualType>(parameterTypes + 1, parameterTypeCount - 1),
- EmptyEPI, paramMods);
- TypeSourceInfo *TInfo = m_context->CreateTypeSourceInfo(functionType, 0);
- FunctionProtoTypeLoc Proto =
- TInfo->getTypeLoc().getAs<FunctionProtoTypeLoc>();
- SmallVector<ParmVarDecl*, g_MaxIntrinsicParamCount> Params;
- for (unsigned int i = 1; i < parameterTypeCount; i++) {
- IdentifierInfo* id = &m_context->Idents.get(StringRef(intrinsic->pArgs[i - 1].pName));
- ParmVarDecl *paramDecl = ParmVarDecl::Create(
- *m_context, nullptr, NoLoc, NoLoc, id, parameterTypes[i], nullptr,
- StorageClass::SC_None, nullptr, paramMods[i - 1]);
- Params.push_back(paramDecl);
- }
- QualType T = TInfo->getType();
- DeclarationNameInfo NameInfo(FunctionTemplate->getDeclName(), NoLoc);
- CXXMethodDecl* method = CXXMethodDecl::Create(
- *m_context, dyn_cast<CXXRecordDecl>(owner), NoLoc, NameInfo, T, TInfo,
- SC_Extern, InlineSpecifiedFalse, IsConstexprFalse, NoLoc);
- // Add intrinsic attr
- AddHLSLIntrinsicAttr(method, *m_context, tableName, lowering, intrinsic);
- // Record this function template specialization.
- TemplateArgumentList *argListCopy = TemplateArgumentList::CreateCopy(
- *m_context, templateArgs.data(), templateArgs.size());
- method->setFunctionTemplateSpecialization(FunctionTemplate, argListCopy, 0);
- // Attach the parameters
- for (unsigned P = 0; P < Params.size(); ++P) {
- Params[P]->setOwningFunction(method);
- Proto.setParam(P, Params[P]);
- }
- method->setParams(Params);
- // Adjust access.
- method->setAccess(AccessSpecifier::AS_public);
- FunctionTemplate->setAccess(method->getAccess());
- return method;
- }
- // Overload support.
- UINT64 ScoreCast(QualType leftType, QualType rightType);
- UINT64 ScoreFunction(OverloadCandidateSet::iterator &Cand);
- UINT64 ScoreImplicitConversionSequence(const ImplicitConversionSequence *s);
- unsigned GetNumElements(QualType anyType);
- unsigned GetNumConvertCheckElts(QualType leftType, unsigned leftSize, QualType rightType, unsigned rightSize);
- QualType GetNthElementType(QualType type, unsigned index);
- bool IsPromotion(ArBasicKind leftKind, ArBasicKind rightKind);
- bool IsCast(ArBasicKind leftKind, ArBasicKind rightKind);
- bool IsIntCast(ArBasicKind leftKind, ArBasicKind rightKind);
- };
- // Use this class to flatten a type into HLSL primitives and iterate through them.
- class FlattenedTypeIterator
- {
- private:
- enum FlattenedIterKind {
- FK_Simple,
- FK_Fields,
- FK_Expressions,
- FK_IncompleteArray
- };
- // Use this struct to represent a specific point in the tracked tree.
- struct FlattenedTypeTracker {
- QualType Type; // Type at this position in the tree.
- unsigned int Count; // Count of consecutive types
- RecordDecl::field_iterator CurrentField; // Current field in for a structure type.
- RecordDecl::field_iterator EndField; // STL-style end of fields.
- MultiExprArg::iterator CurrentExpr; // Current expression (advanceable for a list of expressions).
- MultiExprArg::iterator EndExpr; // STL-style end of expressions.
- FlattenedIterKind IterKind; // Kind of tracker.
- FlattenedTypeTracker(QualType type) : Type(type), Count(0), CurrentExpr(nullptr), IterKind(FK_IncompleteArray) {}
- FlattenedTypeTracker(QualType type, unsigned int count, MultiExprArg::iterator expression) :
- Type(type), Count(count), CurrentExpr(expression), IterKind(FK_Simple) {}
- FlattenedTypeTracker(QualType type, RecordDecl::field_iterator current, RecordDecl::field_iterator end)
- : Type(type), CurrentField(current), EndField(end), Count(0), CurrentExpr(nullptr), IterKind(FK_Fields) {}
- FlattenedTypeTracker(MultiExprArg::iterator current, MultiExprArg::iterator end)
- : CurrentExpr(current), EndExpr(end), Count(0), IterKind(FK_Expressions) {}
- /// <summary>Gets the current expression if one is available.</summary>
- Expr* getExprOrNull() const { return CurrentExpr ? *CurrentExpr : nullptr; }
- /// <summary>Replaces the current expression.</summary>
- void replaceExpr(Expr* e) { *CurrentExpr = e; }
- };
- HLSLExternalSource& m_source; // Source driving the iteration.
- SmallVector<FlattenedTypeTracker, 4> m_typeTrackers; // Active stack of trackers.
- bool m_draining; // Whether the iterator is meant to drain (will not generate new elements in incomplete arrays).
- bool m_springLoaded; // Whether the current element has been set up by an incomplete array but hasn't been used yet.
- unsigned int m_incompleteCount; // The number of elements in an incomplete array.
- size_t m_typeDepth; // Depth of type analysis, to avoid stack overflows.
- QualType m_firstType; // Name of first type found, used for diagnostics.
- SourceLocation m_loc; // Location used for diagnostics.
- static const size_t MaxTypeDepth = 100;
- void advanceLeafTracker();
- /// <summary>Consumes leaves.</summary>
- void consumeLeaf();
- /// <summary>Considers whether the leaf has a usable expression without consuming anything.</summary>
- bool considerLeaf();
- /// <summary>Pushes a tracker for the specified expression; returns true if there is something to evaluate.</summary>
- bool pushTrackerForExpression(MultiExprArg::iterator expression);
- /// <summary>Pushes a tracker for the specified type; returns true if there is something to evaluate.</summary>
- bool pushTrackerForType(QualType type, _In_opt_ MultiExprArg::iterator expression);
- public:
- /// <summary>Constructs a FlattenedTypeIterator for the specified type.</summary>
- FlattenedTypeIterator(SourceLocation loc, QualType type, HLSLExternalSource& source);
- /// <summary>Constructs a FlattenedTypeIterator for the specified arguments.</summary>
- FlattenedTypeIterator(SourceLocation loc, MultiExprArg args, HLSLExternalSource& source);
- /// <summary>Gets the current element in the flattened type hierarchy.</summary>
- QualType getCurrentElement() const;
- /// <summary>Get the number of repeated current elements.</summary>
- unsigned int getCurrentElementSize() const;
- /// <summary>Checks whether the iterator has a current element type to report.</summary>
- bool hasCurrentElement() const;
- /// <summary>Consumes count elements on this iterator.</summary>
- void advanceCurrentElement(unsigned int count);
- /// <summary>Counts the remaining elements in this iterator (consuming all elements).</summary>
- unsigned int countRemaining();
- /// <summary>Gets the current expression if one is available.</summary>
- Expr* getExprOrNull() const { return m_typeTrackers.back().getExprOrNull(); }
- /// <summary>Replaces the current expression.</summary>
- void replaceExpr(Expr* e) { m_typeTrackers.back().replaceExpr(e); }
- struct ComparisonResult
- {
- unsigned int LeftCount;
- unsigned int RightCount;
- /// <summary>Whether elements from right sequence are identical into left sequence elements.</summary>
- bool AreElementsEqual;
- /// <summary>Whether elements from right sequence can be converted into left sequence elements.</summary>
- bool CanConvertElements;
- /// <summary>Whether the elements can be converted and the sequences have the same length.</summary>
- bool IsConvertibleAndEqualLength() const {
- return LeftCount == RightCount;
- }
- /// <summary>Whether the elements can be converted but the left-hand sequence is longer.</summary>
- bool IsConvertibleAndLeftLonger() const {
- return CanConvertElements && LeftCount > RightCount;
- }
- bool IsRightLonger() const {
- return RightCount > LeftCount;
- }
- };
- static ComparisonResult CompareIterators(
- HLSLExternalSource& source, SourceLocation loc,
- FlattenedTypeIterator& leftIter, FlattenedTypeIterator& rightIter);
- static ComparisonResult CompareTypes(
- HLSLExternalSource& source,
- SourceLocation leftLoc, SourceLocation rightLoc,
- QualType left, QualType right);
- // Compares the arguments to initialize the left type, modifying them if necessary.
- static ComparisonResult CompareTypesForInit(
- HLSLExternalSource& source, QualType left, MultiExprArg args,
- SourceLocation leftLoc, SourceLocation rightLoc);
- };
- static
- QualType GetFirstElementTypeFromDecl(const Decl* decl)
- {
- const ClassTemplateSpecializationDecl* specialization = dyn_cast<ClassTemplateSpecializationDecl>(decl);
- if (specialization) {
- const TemplateArgumentList& list = specialization->getTemplateArgs();
- if (list.size()) {
- return list[0].getAsType();
- }
- }
- return QualType();
- }
- static
- QualType GetFirstElementType(QualType type)
- {
- if (!type.isNull()) {
- const RecordType* record = type->getAs<RecordType>();
- if (record) {
- return GetFirstElementTypeFromDecl(record->getDecl());
- }
- }
- return QualType();
- }
- /// <summary>Creates a Typedef in the specified ASTContext.</summary>
- static
- QualType CreateGlobalTypedef(ASTContext* context, const char* ident, QualType baseType)
- {
- DXASSERT_NOMSG(context != nullptr);
- DXASSERT_NOMSG(ident != nullptr);
- DXASSERT_NOMSG(!baseType.isNull());
- DeclContext* declContext = context->getTranslationUnitDecl();
- TypeSourceInfo* typeSource = context->getTrivialTypeSourceInfo(baseType);
- TypedefDecl* decl = TypedefDecl::Create(*context, declContext, NoLoc, NoLoc, &context->Idents.get(ident), typeSource);
- declContext->addDecl(decl);
- decl->setImplicit(true);
- return context->getTypeDeclType(decl);
- }
- void HLSLExternalSource::AddHLSLScalarTypes()
- {
- DXASSERT(m_scalarTypes[HLSLScalarType_unknown].isNull(), "otherwise unknown was initialized to an actual type");
- m_scalarTypes[HLSLScalarType_bool] = m_context->BoolTy;
- m_scalarTypes[HLSLScalarType_int] = m_context->IntTy;
- m_scalarTypes[HLSLScalarType_uint] = CreateGlobalTypedef(m_context, "uint", m_context->UnsignedIntTy);
- m_scalarTypes[HLSLScalarType_dword] = CreateGlobalTypedef(m_context, "dword", m_context->UnsignedIntTy);
- m_scalarTypes[HLSLScalarType_half] = CreateGlobalTypedef(m_context, "half", m_context->FloatTy);
- m_scalarTypes[HLSLScalarType_float] = m_context->FloatTy;
- m_scalarTypes[HLSLScalarType_double] = m_context->DoubleTy;
- m_scalarTypes[HLSLScalarType_float_min10] = m_context->Min10FloatTy;
- m_scalarTypes[HLSLScalarType_float_min16] = CreateGlobalTypedef(m_context, "min16float", m_context->HalfTy);
- m_scalarTypes[HLSLScalarType_int_min12] = m_context->Min12IntTy;
- m_scalarTypes[HLSLScalarType_int_min16] = CreateGlobalTypedef(m_context, "min16int", m_context->ShortTy);
- m_scalarTypes[HLSLScalarType_uint_min16] = CreateGlobalTypedef(m_context, "min16uint", m_context->UnsignedShortTy);
- m_scalarTypes[HLSLScalarType_float_lit] = m_context->LitFloatTy;
- m_scalarTypes[HLSLScalarType_int_lit] = m_context->LitIntTy;
- m_scalarTypes[HLSLScalarType_int64] = CreateGlobalTypedef(m_context, "int64_t", m_context->LongLongTy);
- m_scalarTypes[HLSLScalarType_uint64] = CreateGlobalTypedef(m_context, "uint64_t", m_context->UnsignedLongLongTy);
- }
- FunctionDecl* HLSLExternalSource::AddSubscriptSpecialization(
- _In_ FunctionTemplateDecl* functionTemplate,
- QualType objectElement,
- const FindStructBasicTypeResult& findResult)
- {
- DXASSERT_NOMSG(functionTemplate != nullptr);
- DXASSERT_NOMSG(!objectElement.isNull());
- DXASSERT_NOMSG(findResult.Found());
- DXASSERT(
- g_ArBasicKindsSubscripts[findResult.BasicKindsAsTypeIndex].SubscriptCardinality > 0,
- "otherwise the template shouldn't have an operator[] that the caller is trying to specialize");
- // Subscript is templated only on its return type.
- // Create the template argument.
- bool isReadWrite = GetBasicKindProps(findResult.Kind) & BPROP_RWBUFFER;
- QualType resultType = objectElement;
- if (isReadWrite)
- resultType = m_context->getLValueReferenceType(resultType, false);
- else {
- // Add const to avoid write.
- resultType = m_context->getConstType(resultType);
- resultType = m_context->getLValueReferenceType(resultType);
- }
- TemplateArgument templateArgument(resultType);
- unsigned subscriptCardinality =
- g_ArBasicKindsSubscripts[findResult.BasicKindsAsTypeIndex].SubscriptCardinality;
- QualType subscriptIndexType =
- subscriptCardinality == 1
- ? m_context->UnsignedIntTy
- : NewSimpleAggregateType(AR_TOBJ_VECTOR, AR_BASIC_UINT32, 0, 1,
- subscriptCardinality);
- // Look for an existing specialization.
- void* InsertPos = nullptr;
- FunctionDecl *SpecFunc = functionTemplate->findSpecialization(ArrayRef<TemplateArgument>(&templateArgument, 1), InsertPos);
- if (SpecFunc != nullptr) {
- return SpecFunc;
- }
- // Create a new specialization.
- DeclContext* owner = functionTemplate->getDeclContext();
- TemplateArgumentList templateArgumentList(
- TemplateArgumentList::OnStackType::OnStack, &templateArgument, 1);
- MultiLevelTemplateArgumentList mlTemplateArgumentList(templateArgumentList);
- TemplateDeclInstantiator declInstantiator(*this->m_sema, owner, mlTemplateArgumentList);
- const FunctionType *templateFnType = functionTemplate->getTemplatedDecl()->getType()->getAs<FunctionType>();
- const FunctionProtoType *protoType = dyn_cast<FunctionProtoType>(templateFnType);
- FunctionProtoType::ExtProtoInfo templateEPI = protoType->getExtProtoInfo();
- QualType functionType = m_context->getFunctionType(
- resultType, subscriptIndexType, templateEPI, None);
- TypeSourceInfo *TInfo = m_context->CreateTypeSourceInfo(functionType, 0);
- FunctionProtoTypeLoc Proto = TInfo->getTypeLoc().getAs<FunctionProtoTypeLoc>();
- IdentifierInfo* id = &m_context->Idents.get(StringRef("index"));
- ParmVarDecl* indexerParam = ParmVarDecl::Create(
- *m_context, nullptr, NoLoc, NoLoc, id, subscriptIndexType, nullptr, StorageClass::SC_None, nullptr);
- QualType T = TInfo->getType();
- DeclarationNameInfo NameInfo(functionTemplate->getDeclName(), NoLoc);
- CXXMethodDecl* method = CXXMethodDecl::Create(
- *m_context, dyn_cast<CXXRecordDecl>(owner), NoLoc, NameInfo, T, TInfo,
- SC_Extern, InlineSpecifiedFalse, IsConstexprFalse, NoLoc);
- // Add subscript attribute
- AddHLSLSubscriptAttr(method, *m_context, HLSubscriptOpcode::DefaultSubscript);
- // Record this function template specialization.
- method->setFunctionTemplateSpecialization(functionTemplate,
- TemplateArgumentList::CreateCopy(*m_context, &templateArgument, 1), 0);
- // Attach the parameters
- indexerParam->setOwningFunction(method);
- Proto.setParam(0, indexerParam);
- method->setParams(ArrayRef<ParmVarDecl*>(indexerParam));
- // Adjust access.
- method->setAccess(AccessSpecifier::AS_public);
- functionTemplate->setAccess(method->getAccess());
- return method;
- }
- /// <summary>
- /// This routine combines Source into Target. If you have a symmetric operation
- /// and want to treat either side equally you should call it twice, swapping the
- /// parameter order.
- /// </summary>
- static bool CombineObjectTypes(ArBasicKind Target, __in ArBasicKind Source,
- _Out_opt_ ArBasicKind *pCombined) {
- if (Target == Source) {
- AssignOpt(Target, pCombined);
- return true;
- }
- if (Source == AR_OBJECT_NULL) {
- // NULL is valid for any object type.
- AssignOpt(Target, pCombined);
- return true;
- }
- switch (Target) {
- AR_BASIC_ROBJECT_CASES:
- if (Source == AR_OBJECT_STATEBLOCK) {
- AssignOpt(Target, pCombined);
- return true;
- }
- break;
- AR_BASIC_TEXTURE_CASES:
- AR_BASIC_NON_CMP_SAMPLER_CASES:
- if (Source == AR_OBJECT_SAMPLER || Source == AR_OBJECT_STATEBLOCK) {
- AssignOpt(Target, pCombined);
- return true;
- }
- break;
- case AR_OBJECT_SAMPLERCOMPARISON:
- if (Source == AR_OBJECT_STATEBLOCK) {
- AssignOpt(Target, pCombined);
- return true;
- }
- break;
- }
- AssignOpt(AR_BASIC_UNKNOWN, pCombined);
- return false;
- }
- static ArBasicKind LiteralToConcrete(Expr *litExpr) {
- if (IntegerLiteral *intLit = dyn_cast<IntegerLiteral>(litExpr)) {
- llvm::APInt val = intLit->getValue();
- unsigned width = val.getActiveBits();
- bool isNeg = val.isNegative();
- if (isNeg) {
- // Signed.
- if (width <= 32)
- return ArBasicKind::AR_BASIC_INT32;
- else
- return ArBasicKind::AR_BASIC_INT64;
- } else {
- // Unsigned.
- if (width <= 32)
- return ArBasicKind::AR_BASIC_UINT32;
- else
- return ArBasicKind::AR_BASIC_UINT64;
- }
- } else if (FloatingLiteral *floatLit = dyn_cast<FloatingLiteral>(litExpr)) {
- llvm::APFloat val = floatLit->getValue();
- unsigned width = val.getSizeInBits(val.getSemantics());
- if (width <= 16)
- return ArBasicKind::AR_BASIC_FLOAT16;
- else if (width <= 32)
- return ArBasicKind::AR_BASIC_FLOAT32;
- else
- return AR_BASIC_FLOAT64;
- } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(litExpr)) {
- ArBasicKind kind = LiteralToConcrete(UO->getSubExpr());
- if (UO->getOpcode() == UnaryOperator::Opcode::UO_Minus) {
- if (kind == ArBasicKind::AR_BASIC_UINT32)
- kind = ArBasicKind::AR_BASIC_INT32;
- else if (kind == ArBasicKind::AR_BASIC_UINT64)
- kind = ArBasicKind::AR_BASIC_INT64;
- }
- return kind;
- } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(litExpr)) {
- ArBasicKind kind = LiteralToConcrete(BO->getLHS());
- ArBasicKind kind1 = LiteralToConcrete(BO->getRHS());
- CombineBasicTypes(kind, kind1, &kind);
- return kind;
- } else if (ParenExpr *PE = dyn_cast<ParenExpr>(litExpr)) {
- ArBasicKind kind = LiteralToConcrete(PE->getSubExpr());
- return kind;
- } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(litExpr)) {
- ArBasicKind kind = LiteralToConcrete(CO->getLHS());
- ArBasicKind kind1 = LiteralToConcrete(CO->getRHS());
- CombineBasicTypes(kind, kind1, &kind);
- return kind;
- } else {
- // Could only be function call.
- CallExpr *CE = cast<CallExpr>(litExpr);
- // TODO: calculate the function call result.
- if (CE->getNumArgs() == 1)
- return LiteralToConcrete(CE->getArg(0));
- else {
- ArBasicKind kind = LiteralToConcrete(CE->getArg(0));
- for (unsigned i = 1; i < CE->getNumArgs(); i++) {
- ArBasicKind kindI = LiteralToConcrete(CE->getArg(i));
- CombineBasicTypes(kind, kindI, &kind);
- }
- return kind;
- }
- }
- }
- static bool SearchTypeInTable(ArBasicKind kind, const ArBasicKind *pCT) {
- while (AR_BASIC_UNKNOWN != *pCT && AR_BASIC_NOCAST != *pCT) {
- if (kind == *pCT)
- return true;
- pCT++;
- }
- return false;
- }
- static ArBasicKind ConcreteLiteralType(Expr *litExpr,
- ArBasicKind kind,
- unsigned uLegalComponentTypes) {
- const ArBasicKind *pCT = g_LegalIntrinsicCompTypes[uLegalComponentTypes];
- ArBasicKind defaultKind = *pCT;
- // Use first none literal kind as defaultKind.
- while (AR_BASIC_UNKNOWN != *pCT && AR_BASIC_NOCAST != *pCT) {
- ArBasicKind kind = *pCT;
- pCT++;
- // Skip literal type.
- if (kind == AR_BASIC_LITERAL_INT || kind == AR_BASIC_LITERAL_FLOAT)
- continue;
- defaultKind = kind;
- break;
- }
- ArBasicKind litKind = LiteralToConcrete(litExpr);
- if (kind == AR_BASIC_LITERAL_INT) {
- // Search for match first.
- // For literal arg which don't affect return type, the search should always success.
- // Unless use literal int on a float parameter.
- if (SearchTypeInTable(litKind, g_LegalIntrinsicCompTypes[uLegalComponentTypes]))
- return litKind;
- // Return the default.
- return defaultKind;
- }
- else {
- // Search for float32 first.
- if (SearchTypeInTable(AR_BASIC_FLOAT32, g_LegalIntrinsicCompTypes[uLegalComponentTypes]))
- return AR_BASIC_FLOAT32;
- // Search for float64.
- if (SearchTypeInTable(AR_BASIC_FLOAT64, g_LegalIntrinsicCompTypes[uLegalComponentTypes]))
- return AR_BASIC_FLOAT64;
- // return default.
- return defaultKind;
- }
- }
- _Use_decl_annotations_ bool
- HLSLExternalSource::IsValidateObjectElement(const HLSL_INTRINSIC *pIntrinsic,
- QualType objectElement) {
- IntrinsicOp op = static_cast<IntrinsicOp>(pIntrinsic->Op);
- switch (op) {
- case IntrinsicOp::MOP_Sample:
- case IntrinsicOp::MOP_SampleBias:
- case IntrinsicOp::MOP_SampleCmp:
- case IntrinsicOp::MOP_SampleCmpLevelZero:
- case IntrinsicOp::MOP_SampleGrad:
- case IntrinsicOp::MOP_SampleLevel: {
- ArBasicKind kind = GetTypeElementKind(objectElement);
- UINT uBits = GET_BPROP_BITS(kind);
- return IS_BASIC_FLOAT(kind) && uBits != BPROP_BITS64;
- } break;
- default:
- return true;
- }
- }
- _Use_decl_annotations_
- bool HLSLExternalSource::MatchArguments(
- const HLSL_INTRINSIC* pIntrinsic,
- QualType objectElement,
- ArrayRef<Expr *> Args,
- QualType(&argTypes)[g_MaxIntrinsicParamCount + 1],
- size_t* argCount)
- {
- DXASSERT_NOMSG(pIntrinsic != nullptr);
- DXASSERT_NOMSG(argCount != nullptr);
- static const UINT UnusedSize = 0xFF;
- static const BYTE MaxIntrinsicArgs = g_MaxIntrinsicParamCount + 1;
- #define CAB(_) { if (!(_)) return false; }
- *argCount = 0;
- ArTypeObjectKind Template[MaxIntrinsicArgs]; // Template type for each argument, AR_TOBJ_UNKNOWN if unspecified.
- ArBasicKind ComponentType[MaxIntrinsicArgs]; // Component type for each argument, AR_BASIC_UNKNOWN if unspecified.
- UINT uSpecialSize[IA_SPECIAL_SLOTS]; // row/col matching types, UNUSED_INDEX32 if unspecified.
- // Reset infos
- std::fill(Template, Template + _countof(Template), AR_TOBJ_UNKNOWN);
- std::fill(ComponentType, ComponentType + _countof(ComponentType), AR_BASIC_UNKNOWN);
- std::fill(uSpecialSize, uSpecialSize + _countof(uSpecialSize), UnusedSize);
-
- const unsigned retArgIdx = 0;
- unsigned retTypeIdx = pIntrinsic->pArgs[retArgIdx].uComponentTypeId;
- // Populate the template for each argument.
- ArrayRef<Expr*>::iterator iterArg = Args.begin();
- ArrayRef<Expr*>::iterator end = Args.end();
- unsigned int iArg = 1;
- for (; iterArg != end; ++iterArg) {
- Expr* pCallArg = *iterArg;
- // No vararg support.
- if (iArg >= _countof(Template) || iArg > pIntrinsic->uNumArgs) {
- return false;
- }
- const HLSL_INTRINSIC_ARGUMENT *pIntrinsicArg;
- pIntrinsicArg = &pIntrinsic->pArgs[iArg];
- DXASSERT(pIntrinsicArg->uTemplateId != INTRIN_TEMPLATE_VARARGS, "no vararg support");
- // If we are a type and templateID requires one, this isn't a match.
- if (pIntrinsicArg->uTemplateId == INTRIN_TEMPLATE_FROM_TYPE) {
- ++iArg;
- continue;
- }
- QualType pType = pCallArg->getType();
- ArTypeObjectKind TypeInfoShapeKind = GetTypeObjectKind(pType);
- ArBasicKind TypeInfoEltKind = GetTypeElementKind(pType);
- if (TypeInfoEltKind == AR_BASIC_LITERAL_INT ||
- TypeInfoEltKind == AR_BASIC_LITERAL_FLOAT) {
- bool affectRetType =
- (iArg != retArgIdx && retTypeIdx == pIntrinsicArg->uComponentTypeId);
- // For literal arg which don't affect return type, find concrete type.
- // For literal arg affect return type,
- // TryEvalIntrinsic in CGHLSLMS.cpp will take care of cases
- // where all argumentss are literal.
- // CombineBasicTypes will cover the rest cases.
- if (!affectRetType) {
- TypeInfoEltKind = ConcreteLiteralType(
- pCallArg, TypeInfoEltKind, pIntrinsicArg->uLegalComponentTypes);
- }
- }
- UINT TypeInfoCols = 1;
- UINT TypeInfoRows = 1;
- switch (TypeInfoShapeKind) {
- case AR_TOBJ_MATRIX:
- GetRowsAndCols(pType, TypeInfoRows, TypeInfoCols);
- break;
- case AR_TOBJ_VECTOR:
- TypeInfoCols = GetHLSLVecSize(pType);
- break;
- case AR_TOBJ_BASIC:
- case AR_TOBJ_OBJECT:
- break;
- default:
- return false; // no struct, arrays or void
- }
- DXASSERT(
- pIntrinsicArg->uTemplateId < MaxIntrinsicArgs,
- "otherwise intrinsic table was modified and g_MaxIntrinsicParamCount was not updated (or uTemplateId is out of bounds)");
- // Compare template
- if ((AR_TOBJ_UNKNOWN == Template[pIntrinsicArg->uTemplateId]) ||
- (AR_TOBJ_SCALAR == Template[pIntrinsicArg->uTemplateId]) &&
- (AR_TOBJ_VECTOR == TypeInfoShapeKind || AR_TOBJ_MATRIX == TypeInfoShapeKind)) {
- Template[pIntrinsicArg->uTemplateId] = TypeInfoShapeKind;
- }
- else if (AR_TOBJ_SCALAR == TypeInfoShapeKind) {
- if (AR_TOBJ_SCALAR != Template[pIntrinsicArg->uTemplateId] &&
- AR_TOBJ_VECTOR != Template[pIntrinsicArg->uTemplateId] &&
- AR_TOBJ_MATRIX != Template[pIntrinsicArg->uTemplateId]) {
- return false;
- }
- }
- else {
- if (TypeInfoShapeKind != Template[pIntrinsicArg->uTemplateId]) {
- return false;
- }
- }
- DXASSERT(
- pIntrinsicArg->uComponentTypeId < MaxIntrinsicArgs,
- "otherwise intrinsic table was modified and MaxIntrinsicArgs was not updated (or uComponentTypeId is out of bounds)");
- // Merge ComponentTypes
- if (AR_BASIC_UNKNOWN == ComponentType[pIntrinsicArg->uComponentTypeId]) {
- ComponentType[pIntrinsicArg->uComponentTypeId] = TypeInfoEltKind;
- }
- else {
- if (!CombineBasicTypes(
- ComponentType[pIntrinsicArg->uComponentTypeId],
- TypeInfoEltKind,
- &ComponentType[pIntrinsicArg->uComponentTypeId])) {
- return false;
- }
- }
- // Rows
- if (AR_TOBJ_SCALAR != TypeInfoShapeKind) {
- if (pIntrinsicArg->uRows >= IA_SPECIAL_BASE) {
- UINT uSpecialId = pIntrinsicArg->uRows - IA_SPECIAL_BASE;
- CAB(uSpecialId < IA_SPECIAL_SLOTS);
- if (uSpecialSize[uSpecialId] > TypeInfoRows) {
- uSpecialSize[uSpecialId] = TypeInfoRows;
- }
- }
- else {
- if (TypeInfoRows < pIntrinsicArg->uRows) {
- return false;
- }
- }
- }
- // Columns
- if (AR_TOBJ_SCALAR != TypeInfoShapeKind) {
- if (pIntrinsicArg->uCols >= IA_SPECIAL_BASE) {
- UINT uSpecialId = pIntrinsicArg->uCols - IA_SPECIAL_BASE;
- CAB(uSpecialId < IA_SPECIAL_SLOTS);
- if (uSpecialSize[uSpecialId] > TypeInfoCols) {
- uSpecialSize[uSpecialId] = TypeInfoCols;
- }
- }
- else {
- if (TypeInfoCols < pIntrinsicArg->uCols) {
- return false;
- }
- }
- }
- // Usage
- if (pIntrinsicArg->qwUsage & AR_QUAL_OUT) {
- if (pCallArg->getType().isConstQualified()) {
- // Can't use a const type in an out or inout parameter.
- return false;
- }
- }
- iArg++;
- }
- DXASSERT(iterArg == end, "otherwise the argument list wasn't fully processed");
- // Default template and component type for return value
- if (pIntrinsic->pArgs[0].qwUsage && pIntrinsic->pArgs[0].uTemplateId != INTRIN_TEMPLATE_FROM_TYPE) {
- CAB(pIntrinsic->pArgs[0].uTemplateId < MaxIntrinsicArgs);
- if (AR_TOBJ_UNKNOWN == Template[pIntrinsic->pArgs[0].uTemplateId]) {
- Template[pIntrinsic->pArgs[0].uTemplateId] =
- g_LegalIntrinsicTemplates[pIntrinsic->pArgs[0].uLegalTemplates][0];
- if (pIntrinsic->pArgs[0].uComponentTypeId != INTRIN_COMPTYPE_FROM_TYPE_ELT0) {
- DXASSERT_NOMSG(pIntrinsic->pArgs[0].uComponentTypeId < MaxIntrinsicArgs);
- if (AR_BASIC_UNKNOWN == ComponentType[pIntrinsic->pArgs[0].uComponentTypeId]) {
- ComponentType[pIntrinsic->pArgs[0].uComponentTypeId] =
- g_LegalIntrinsicCompTypes[pIntrinsic->pArgs[0].uLegalComponentTypes][0];
- }
- }
- }
- }
- // Make sure all template, component type, and texture type selections are valid.
- for (size_t i = 0; i < Args.size() + 1; i++) {
- const HLSL_INTRINSIC_ARGUMENT *pArgument = &pIntrinsic->pArgs[i];
- // Check template.
- if (pArgument->uTemplateId == INTRIN_TEMPLATE_FROM_TYPE) {
- continue; // Already verified that this is available.
- }
- const ArTypeObjectKind *pTT = g_LegalIntrinsicTemplates[pArgument->uLegalTemplates];
- if (AR_TOBJ_UNKNOWN != Template[i]) {
- if ((AR_TOBJ_SCALAR == Template[i]) && (AR_TOBJ_VECTOR == *pTT || AR_TOBJ_MATRIX == *pTT)) {
- Template[i] = *pTT;
- }
- else {
- while (AR_TOBJ_UNKNOWN != *pTT) {
- if (Template[i] == *pTT)
- break;
- pTT++;
- }
- }
- if (AR_TOBJ_UNKNOWN == *pTT)
- return false;
- }
- else if (pTT) {
- Template[i] = *pTT;
- }
- // Check component type.
- const ArBasicKind *pCT = g_LegalIntrinsicCompTypes[pArgument->uLegalComponentTypes];
- if (AR_BASIC_UNKNOWN != ComponentType[i]) {
- while (AR_BASIC_UNKNOWN != *pCT && AR_BASIC_NOCAST != *pCT) {
- if (ComponentType[i] == *pCT)
- break;
- pCT++;
- }
- // has to be a strict match
- if (*pCT == AR_BASIC_NOCAST)
- return false;
- // If it is an object, see if it can be cast to the first thing in the
- // list, otherwise move on to next intrinsic.
- if (AR_TOBJ_OBJECT == Template[i] && AR_BASIC_UNKNOWN == *pCT) {
- if (!CombineObjectTypes(g_LegalIntrinsicCompTypes[pArgument->uLegalComponentTypes][0], ComponentType[i], nullptr)) {
- return false;
- }
- }
- if (AR_BASIC_UNKNOWN == *pCT) {
- ComponentType[i] = g_LegalIntrinsicCompTypes[pArgument->uLegalComponentTypes][0];
- }
- }
- else if (pCT) {
- ComponentType[i] = *pCT;
- }
- }
- // Default to a void return type.
- argTypes[0] = m_context->VoidTy;
- // Default specials sizes.
- for (UINT i = 0; i < IA_SPECIAL_SLOTS; i++) {
- if (UnusedSize == uSpecialSize[i]) {
- uSpecialSize[i] = 1;
- }
- }
- // Populate argTypes.
- for (size_t i = 0; i <= Args.size(); i++) {
- const HLSL_INTRINSIC_ARGUMENT *pArgument = &pIntrinsic->pArgs[i];
- if (!pArgument->qwUsage)
- continue;
- QualType pNewType;
- unsigned int quals = 0; // qualifications for this argument
- // If we have no type, set it to our input type (templatized)
- if (pArgument->uTemplateId == INTRIN_TEMPLATE_FROM_TYPE) {
- // Use the templated input type, but resize it if the
- // intrinsic's rows/cols isn't 0
- if (pArgument->uRows && pArgument->uCols) {
- UINT uRows, uCols;
- // if type is overriden, use new type size, for
- // now it only supports scalars
- if (pArgument->uRows >= IA_SPECIAL_BASE) {
- UINT uSpecialId = pArgument->uRows - IA_SPECIAL_BASE;
- CAB(uSpecialId < IA_SPECIAL_SLOTS);
- uRows = uSpecialSize[uSpecialId];
- }
- else if (pArgument->uRows > 0) {
- uRows = pArgument->uRows;
- }
- if (pArgument->uCols >= IA_SPECIAL_BASE) {
- UINT uSpecialId = pArgument->uCols - IA_SPECIAL_BASE;
- CAB(uSpecialId < IA_SPECIAL_SLOTS);
- uCols = uSpecialSize[uSpecialId];
- }
- else if (pArgument->uCols > 0) {
- uCols = pArgument->uCols;
- }
- // 1x1 numeric outputs are always scalar.. since these
- // are most flexible
- if ((1 == uCols) && (1 == uRows)) {
- pNewType = objectElement;
- if (pNewType.isNull()) {
- return false;
- }
- }
- else {
- // non-scalars unsupported right now since nothing
- // uses it, would have to create either a type
- // list for sub-structures or just resize the
- // given type
- // VH(E_NOTIMPL);
- return false;
- }
- }
- else {
- DXASSERT_NOMSG(!pArgument->uRows && !pArgument->uCols);
- if (objectElement.isNull()) {
- return false;
- }
- pNewType = objectElement;
- }
- }
- else {
- ArBasicKind pEltType;
- // ComponentType, if the Id is special then it gets the
- // component type from the first component of the type, if
- // we need more (for the second component, e.g.), then we
- // can use more specials, etc.
- if (pArgument->uComponentTypeId == INTRIN_COMPTYPE_FROM_TYPE_ELT0) {
- if (objectElement.isNull()) {
- return false;
- }
- pEltType = GetTypeElementKind(objectElement);
- DXASSERT_VALIDBASICKIND(pEltType);
- }
- else {
- pEltType = ComponentType[pArgument->uComponentTypeId];
- DXASSERT_VALIDBASICKIND(pEltType);
- }
- UINT uRows, uCols;
- // Rows
- if (pArgument->uRows >= IA_SPECIAL_BASE) {
- UINT uSpecialId = pArgument->uRows - IA_SPECIAL_BASE;
- CAB(uSpecialId < IA_SPECIAL_SLOTS);
- uRows = uSpecialSize[uSpecialId];
- }
- else {
- uRows = pArgument->uRows;
- }
- // Cols
- if (pArgument->uCols >= IA_SPECIAL_BASE) {
- UINT uSpecialId = pArgument->uCols - IA_SPECIAL_BASE;
- CAB(uSpecialId < IA_SPECIAL_SLOTS);
- uCols = uSpecialSize[uSpecialId];
- }
- else {
- uCols = pArgument->uCols;
- }
- // Verify that the final results are in bounds.
- CAB(uCols > 0 && uCols <= MaxVectorSize && uRows > 0 && uRows <= MaxVectorSize);
- // Const
- UINT64 qwQual = pArgument->qwUsage & (AR_QUAL_ROWMAJOR | AR_QUAL_COLMAJOR);
- if ((0 == i) || !(pArgument->qwUsage & AR_QUAL_OUT))
- qwQual |= AR_QUAL_CONST;
- DXASSERT_VALIDBASICKIND(pEltType);
- pNewType = NewSimpleAggregateType(Template[pArgument->uTemplateId], pEltType, qwQual, uRows, uCols);
- }
- DXASSERT(!pNewType.isNull(), "otherwise there's a branch in this function that fails to assign this");
- argTypes[i] = QualType(pNewType.getTypePtr(), quals);
- // TODO: support out modifier
- //if (pArgument->qwUsage & AR_QUAL_OUT) {
- // argTypes[i] = m_context->getLValueReferenceType(argTypes[i].withConst());
- //}
- }
- *argCount = iArg;
- DXASSERT(
- *argCount == pIntrinsic->uNumArgs,
- "In the absence of varargs, a successful match would indicate we have as many arguments and types as the intrinsic template");
- return true;
- #undef CAB
- }
- _Use_decl_annotations_
- HLSLExternalSource::FindStructBasicTypeResult
- HLSLExternalSource::FindStructBasicType(DeclContext* functionDeclContext)
- {
- DXASSERT_NOMSG(functionDeclContext != nullptr);
- // functionDeclContext may be a specialization of a template, such as AppendBuffer<MY_STRUCT>, or it
- // may be a simple class, such as RWByteAddressBuffer.
- const CXXRecordDecl* recordDecl = GetRecordDeclForBuiltInOrStruct(functionDeclContext);
- // We save the caller from filtering out other types of context (like the translation unit itself).
- if (recordDecl != nullptr)
- {
- int index = FindObjectBasicKindIndex(recordDecl);
- if (index != -1) {
- ArBasicKind kind = g_ArBasicKindsAsTypes[index];
- return HLSLExternalSource::FindStructBasicTypeResult(kind, index);
- }
- }
- return HLSLExternalSource::FindStructBasicTypeResult(AR_BASIC_UNKNOWN, 0);
- }
- _Use_decl_annotations_
- void HLSLExternalSource::FindIntrinsicTable(DeclContext* functionDeclContext, const char** name, const HLSL_INTRINSIC** intrinsics, size_t* intrinsicCount)
- {
- DXASSERT_NOMSG(functionDeclContext != nullptr);
- DXASSERT_NOMSG(name != nullptr);
- DXASSERT_NOMSG(intrinsics != nullptr);
- DXASSERT_NOMSG(intrinsicCount != nullptr);
- *intrinsics = nullptr;
- *intrinsicCount = 0;
- *name = nullptr;
- HLSLExternalSource::FindStructBasicTypeResult lookup = FindStructBasicType(functionDeclContext);
- if (lookup.Found()) {
- GetIntrinsicMethods(lookup.Kind, intrinsics, intrinsicCount);
- *name = g_ArBasicTypeNames[lookup.Kind];
- }
- }
- static bool BinaryOperatorKindIsArithmetic(BinaryOperatorKind Opc)
- {
- return
- // Arithmetic operators.
- Opc == BinaryOperatorKind::BO_Add ||
- Opc == BinaryOperatorKind::BO_AddAssign ||
- Opc == BinaryOperatorKind::BO_Sub ||
- Opc == BinaryOperatorKind::BO_SubAssign ||
- Opc == BinaryOperatorKind::BO_Rem ||
- Opc == BinaryOperatorKind::BO_RemAssign ||
- Opc == BinaryOperatorKind::BO_Div ||
- Opc == BinaryOperatorKind::BO_DivAssign ||
- Opc == BinaryOperatorKind::BO_Mul ||
- Opc == BinaryOperatorKind::BO_MulAssign;
- }
- static bool BinaryOperatorKindIsCompoundAssignment(BinaryOperatorKind Opc)
- {
- return
- // Arithmetic-and-assignment operators.
- Opc == BinaryOperatorKind::BO_AddAssign ||
- Opc == BinaryOperatorKind::BO_SubAssign ||
- Opc == BinaryOperatorKind::BO_RemAssign ||
- Opc == BinaryOperatorKind::BO_DivAssign ||
- Opc == BinaryOperatorKind::BO_MulAssign ||
- // Bitwise-and-assignment operators.
- Opc == BinaryOperatorKind::BO_ShlAssign ||
- Opc == BinaryOperatorKind::BO_ShrAssign ||
- Opc == BinaryOperatorKind::BO_AndAssign ||
- Opc == BinaryOperatorKind::BO_OrAssign ||
- Opc == BinaryOperatorKind::BO_XorAssign;
- }
- static bool BinaryOperatorKindIsCompoundAssignmentForBool(BinaryOperatorKind Opc)
- {
- return
- Opc == BinaryOperatorKind::BO_AndAssign ||
- Opc == BinaryOperatorKind::BO_OrAssign ||
- Opc == BinaryOperatorKind::BO_XorAssign;
- }
- static bool BinaryOperatorKindIsBitwise(BinaryOperatorKind Opc)
- {
- return
- Opc == BinaryOperatorKind::BO_Shl ||
- Opc == BinaryOperatorKind::BO_ShlAssign ||
- Opc == BinaryOperatorKind::BO_Shr ||
- Opc == BinaryOperatorKind::BO_ShrAssign ||
- Opc == BinaryOperatorKind::BO_And ||
- Opc == BinaryOperatorKind::BO_AndAssign ||
- Opc == BinaryOperatorKind::BO_Or ||
- Opc == BinaryOperatorKind::BO_OrAssign ||
- Opc == BinaryOperatorKind::BO_Xor ||
- Opc == BinaryOperatorKind::BO_XorAssign;
- }
- static bool BinaryOperatorKindIsBitwiseShift(BinaryOperatorKind Opc)
- {
- return
- Opc == BinaryOperatorKind::BO_Shl ||
- Opc == BinaryOperatorKind::BO_ShlAssign ||
- Opc == BinaryOperatorKind::BO_Shr ||
- Opc == BinaryOperatorKind::BO_ShrAssign;
- }
- static bool BinaryOperatorKindIsEqualComparison(BinaryOperatorKind Opc)
- {
- return
- Opc == BinaryOperatorKind::BO_EQ ||
- Opc == BinaryOperatorKind::BO_NE;
- }
- static bool BinaryOperatorKindIsOrderComparison(BinaryOperatorKind Opc)
- {
- return
- Opc == BinaryOperatorKind::BO_LT ||
- Opc == BinaryOperatorKind::BO_GT ||
- Opc == BinaryOperatorKind::BO_LE ||
- Opc == BinaryOperatorKind::BO_GE;
- }
- static bool BinaryOperatorKindIsComparison(BinaryOperatorKind Opc)
- {
- return BinaryOperatorKindIsEqualComparison(Opc) || BinaryOperatorKindIsOrderComparison(Opc);
- }
- static bool BinaryOperatorKindIsLogical(BinaryOperatorKind Opc)
- {
- return
- Opc == BinaryOperatorKind::BO_LAnd ||
- Opc == BinaryOperatorKind::BO_LOr;
- }
- static bool BinaryOperatorKindRequiresNumeric(BinaryOperatorKind Opc)
- {
- return
- BinaryOperatorKindIsArithmetic(Opc) ||
- BinaryOperatorKindIsOrderComparison(Opc) ||
- BinaryOperatorKindIsLogical(Opc);
- }
- static bool BinaryOperatorKindRequiresIntegrals(BinaryOperatorKind Opc)
- {
- return BinaryOperatorKindIsBitwise(Opc);
- }
- static bool BinaryOperatorKindRequiresBoolAsNumeric(BinaryOperatorKind Opc)
- {
- return
- BinaryOperatorKindIsBitwise(Opc) ||
- BinaryOperatorKindIsArithmetic(Opc);
- }
- static bool UnaryOperatorKindRequiresIntegrals(UnaryOperatorKind Opc)
- {
- return Opc == UnaryOperatorKind::UO_Not;
- }
- static bool UnaryOperatorKindRequiresNumerics(UnaryOperatorKind Opc)
- {
- return
- Opc == UnaryOperatorKind::UO_LNot ||
- Opc == UnaryOperatorKind::UO_Plus ||
- Opc == UnaryOperatorKind::UO_Minus ||
- // The omission in fxc caused objects and structs to accept this.
- Opc == UnaryOperatorKind::UO_PreDec || Opc == UnaryOperatorKind::UO_PreInc ||
- Opc == UnaryOperatorKind::UO_PostDec || Opc == UnaryOperatorKind::UO_PostInc;
- }
- static bool UnaryOperatorKindRequiresModifiableValue(UnaryOperatorKind Opc)
- {
- return
- Opc == UnaryOperatorKind::UO_PreDec || Opc == UnaryOperatorKind::UO_PreInc ||
- Opc == UnaryOperatorKind::UO_PostDec || Opc == UnaryOperatorKind::UO_PostInc;
- }
- static bool UnaryOperatorKindRequiresBoolAsNumeric(UnaryOperatorKind Opc)
- {
- return
- Opc == UnaryOperatorKind::UO_Not ||
- Opc == UnaryOperatorKind::UO_Plus ||
- Opc == UnaryOperatorKind::UO_Minus;
- }
- static bool UnaryOperatorKindDisallowsBool(UnaryOperatorKind Opc)
- {
- return
- Opc == UnaryOperatorKind::UO_PreDec || Opc == UnaryOperatorKind::UO_PreInc ||
- Opc == UnaryOperatorKind::UO_PostDec || Opc == UnaryOperatorKind::UO_PostInc;
- }
- /// <summary>
- /// Checks whether the specified AR_TOBJ* value is a primitive or aggregate of primitive elements
- /// (as opposed to a built-in object like a sampler or texture, or a void type).
- /// </summary>
- static bool IsObjectKindPrimitiveAggregate(ArTypeObjectKind value)
- {
- return
- value == AR_TOBJ_BASIC ||
- value == AR_TOBJ_MATRIX ||
- value == AR_TOBJ_VECTOR;
- }
- static bool IsBasicKindIntegral(ArBasicKind value)
- {
- return IS_BASIC_AINT(value) || IS_BASIC_BOOL(value);
- }
- static bool IsBasicKindIntMinPrecision(ArBasicKind kind)
- {
- return IS_BASIC_SINT(kind) && IS_BASIC_MIN_PRECISION(kind);
- }
- static bool IsBasicKindNumeric(ArBasicKind value)
- {
- return GetBasicKindProps(value) & BPROP_NUMERIC;
- }
- ExprResult HLSLExternalSource::PromoteToIntIfBool(ExprResult& E)
- {
- // An invalid expression is pass-through at this point.
- if (E.isInvalid())
- {
- return E;
- }
- QualType qt = E.get()->getType();
- ArBasicKind elementKind = this->GetTypeElementKind(qt);
- if (elementKind != AR_BASIC_BOOL)
- {
- return E;
- }
- // Construct a scalar/vector/matrix type with the same shape as E.
- ArTypeObjectKind objectKind = this->GetTypeObjectKind(qt);
- QualType targetType;
- UINT colCount, rowCount;
- GetRowsAndColsForAny(qt, rowCount, colCount);
- targetType = NewSimpleAggregateType(objectKind, AR_BASIC_INT32, 0, rowCount, colCount)->getCanonicalTypeInternal();
- if (E.get()->isLValue()) {
- E = m_sema->DefaultLvalueConversion(E.get()).get();
- }
- switch (objectKind)
- {
- case AR_TOBJ_SCALAR:
- return ImplicitCastExpr::Create(*m_context, targetType, CastKind::CK_IntegralCast, E.get(), nullptr, ExprValueKind::VK_RValue);
- case AR_TOBJ_ARRAY:
- case AR_TOBJ_VECTOR:
- case AR_TOBJ_MATRIX:
- return ImplicitCastExpr::Create(*m_context, targetType, CastKind::CK_HLSLCC_IntegralCast, E.get(), nullptr, ExprValueKind::VK_RValue);
- default:
- DXASSERT(false, "unsupported objectKind for PromoteToIntIfBool");
- }
- return E;
- }
- _Use_decl_annotations_
- void HLSLExternalSource::CollectInfo(QualType type, ArTypeInfo* pTypeInfo)
- {
- DXASSERT_NOMSG(pTypeInfo != nullptr);
- DXASSERT_NOMSG(!type.isNull());
- memset(pTypeInfo, 0, sizeof(*pTypeInfo));
- pTypeInfo->ObjKind = GetTypeElementKind(type);
- pTypeInfo->EltKind = pTypeInfo->ObjKind;
- pTypeInfo->ShapeKind = GetTypeObjectKind(type);
- GetRowsAndColsForAny(type, pTypeInfo->uRows, pTypeInfo->uCols);
- pTypeInfo->uTotalElts = pTypeInfo->uRows * pTypeInfo->uCols;
- }
- // Highest possible score (i.e., worst possible score).
- static const UINT64 SCORE_MAX = 0xFFFFFFFFFFFFFFFF;
- // Leave the first two score bits to handle higher-level
- // variations like target type.
- #define SCORE_MIN_SHIFT 2
- // Space out scores to allow up to 128 parameters to
- // vary between score sets spill into each other.
- #define SCORE_PARAM_SHIFT 7
- unsigned HLSLExternalSource::GetNumElements(QualType anyType) {
- if (anyType.isNull()) {
- return 0;
- }
- anyType = GetStructuralForm(anyType);
- ArTypeObjectKind kind = GetTypeObjectKind(anyType);
- switch (kind) {
- case AR_TOBJ_BASIC:
- case AR_TOBJ_OBJECT:
- return 1;
- case AR_TOBJ_COMPOUND: {
- // TODO: consider caching this value for perf
- unsigned total = 0;
- const RecordType *recordType = anyType->getAs<RecordType>();
- RecordDecl::field_iterator fi = recordType->getDecl()->field_begin();
- RecordDecl::field_iterator fend = recordType->getDecl()->field_end();
- while (fi != fend) {
- total += GetNumElements(fi->getType());
- ++fi;
- }
- return total;
- }
- case AR_TOBJ_ARRAY:
- case AR_TOBJ_MATRIX:
- case AR_TOBJ_VECTOR:
- return GetElementCount(anyType);
- default:
- DXASSERT(kind == AR_TOBJ_VOID,
- "otherwise the type cannot be classified or is not supported");
- return 0;
- }
- }
- unsigned HLSLExternalSource::GetNumConvertCheckElts(QualType leftType,
- unsigned leftSize,
- QualType rightType,
- unsigned rightSize) {
- // We can convert from a larger type to a smaller
- // but not a smaller type to a larger so default
- // to just comparing the destination size.
- unsigned uElts = leftSize;
- leftType = GetStructuralForm(leftType);
- rightType = GetStructuralForm(rightType);
- if (leftType->isArrayType() && rightType->isArrayType()) {
- //
- // If we're comparing arrays we don't
- // need to compare every element of
- // the arrays since all elements
- // will have the same type.
- // We only need to compare enough
- // elements that we've tried every
- // possible mix of dst and src elements.
- //
- // TODO: handle multidimensional arrays and arrays of arrays
- QualType pDstElt = leftType->getAsArrayTypeUnsafe()->getElementType();
- unsigned uDstEltSize = GetNumElements(pDstElt);
- QualType pSrcElt = rightType->getAsArrayTypeUnsafe()->getElementType();
- unsigned uSrcEltSize = GetNumElements(pSrcElt);
- if (uDstEltSize == uSrcEltSize) {
- uElts = uDstEltSize;
- } else if (uDstEltSize > uSrcEltSize) {
- // If one size is not an even multiple of the other we need to let the
- // full compare run in order to try all alignments.
- if (uSrcEltSize && (uDstEltSize % uSrcEltSize) == 0) {
- uElts = uDstEltSize;
- }
- } else if (uDstEltSize && (uSrcEltSize % uDstEltSize) == 0) {
- uElts = uSrcEltSize;
- }
- }
- return uElts;
- }
- QualType HLSLExternalSource::GetNthElementType(QualType type, unsigned index) {
- if (type.isNull()) {
- return type;
- }
- ArTypeObjectKind kind = GetTypeObjectKind(type);
- switch (kind) {
- case AR_TOBJ_BASIC:
- case AR_TOBJ_OBJECT:
- return (index == 0) ? type : QualType();
- case AR_TOBJ_COMPOUND: {
- // TODO: consider caching this value for perf
- const RecordType *recordType = type->getAsStructureType();
- RecordDecl::field_iterator fi = recordType->getDecl()->field_begin();
- RecordDecl::field_iterator fend = recordType->getDecl()->field_end();
- while (fi != fend) {
- if (!fi->getType().isNull()) {
- unsigned subElements = GetNumElements(fi->getType());
- if (index < subElements) {
- return GetNthElementType(fi->getType(), index);
- } else {
- index -= subElements;
- }
- }
- ++fi;
- }
- return QualType();
- }
- case AR_TOBJ_ARRAY: {
- unsigned arraySize;
- QualType elementType;
- unsigned elementCount;
- elementType = type.getNonReferenceType()->getAsArrayTypeUnsafe()->getElementType();
- elementCount = GetElementCount(elementType);
- if (index < elementCount) {
- return GetNthElementType(elementType, index);
- }
- arraySize = GetArraySize(type);
- if (index >= arraySize * elementCount) {
- return QualType();
- }
- return GetNthElementType(elementType, index % elementCount);
- }
- case AR_TOBJ_MATRIX:
- case AR_TOBJ_VECTOR:
- return (index < GetElementCount(type)) ? GetMatrixOrVectorElementType(type)
- : QualType();
- default:
- DXASSERT(kind == AR_TOBJ_VOID,
- "otherwise the type cannot be classified or is not supported");
- return QualType();
- }
- }
- bool HLSLExternalSource::IsPromotion(ArBasicKind leftKind, ArBasicKind rightKind) {
- // Eliminate exact matches first, then check for promotions.
- if (leftKind == rightKind) {
- return false;
- }
- switch (rightKind) {
- case AR_BASIC_FLOAT16:
- switch (leftKind) {
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT64:
- return true;
- }
- break;
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- switch (leftKind) {
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT64:
- return true;
- }
- break;
- case AR_BASIC_FLOAT32:
- switch (leftKind) {
- case AR_BASIC_FLOAT64:
- return true;
- }
- break;
- case AR_BASIC_MIN10FLOAT:
- switch (leftKind) {
- case AR_BASIC_MIN16FLOAT:
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT64:
- return true;
- }
- break;
- case AR_BASIC_MIN16FLOAT:
- switch (leftKind) {
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT64:
- return true;
- }
- break;
- case AR_BASIC_INT8:
- case AR_BASIC_UINT8:
- // For backwards compat we consider signed/unsigned the same.
- switch (leftKind) {
- case AR_BASIC_INT16:
- case AR_BASIC_INT32:
- case AR_BASIC_INT64:
- case AR_BASIC_UINT16:
- case AR_BASIC_UINT32:
- case AR_BASIC_UINT64:
- return true;
- }
- break;
- case AR_BASIC_INT16:
- case AR_BASIC_UINT16:
- // For backwards compat we consider signed/unsigned the same.
- switch (leftKind) {
- case AR_BASIC_INT32:
- case AR_BASIC_INT64:
- case AR_BASIC_UINT32:
- case AR_BASIC_UINT64:
- return true;
- }
- break;
- case AR_BASIC_INT32:
- case AR_BASIC_UINT32:
- // For backwards compat we consider signed/unsigned the same.
- switch (leftKind) {
- case AR_BASIC_INT64:
- case AR_BASIC_UINT64:
- return true;
- }
- break;
- case AR_BASIC_MIN12INT:
- switch (leftKind) {
- case AR_BASIC_MIN16INT:
- case AR_BASIC_INT32:
- case AR_BASIC_INT64:
- return true;
- }
- break;
- case AR_BASIC_MIN16INT:
- switch (leftKind) {
- case AR_BASIC_INT32:
- case AR_BASIC_INT64:
- return true;
- }
- break;
- case AR_BASIC_MIN16UINT:
- switch (leftKind) {
- case AR_BASIC_UINT32:
- case AR_BASIC_UINT64:
- return true;
- }
- break;
- }
- return false;
- }
- bool HLSLExternalSource::IsCast(ArBasicKind leftKind, ArBasicKind rightKind) {
- // Eliminate exact matches first, then check for casts.
- if (leftKind == rightKind) {
- return false;
- }
- //
- // All minimum-bits types are only considered matches of themselves
- // and thus are not in this table.
- //
- switch (leftKind) {
- case AR_BASIC_LITERAL_INT:
- switch (rightKind) {
- case AR_BASIC_INT8:
- case AR_BASIC_INT16:
- case AR_BASIC_INT32:
- case AR_BASIC_INT64:
- case AR_BASIC_UINT8:
- case AR_BASIC_UINT16:
- case AR_BASIC_UINT32:
- case AR_BASIC_UINT64:
- return false;
- }
- break;
- case AR_BASIC_INT8:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_UINT8:
- return false;
- }
- break;
- case AR_BASIC_INT16:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_UINT16:
- return false;
- }
- break;
- case AR_BASIC_INT32:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_UINT32:
- return false;
- }
- break;
- case AR_BASIC_INT64:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_UINT64:
- return false;
- }
- break;
- case AR_BASIC_UINT8:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_INT8:
- return false;
- }
- break;
- case AR_BASIC_UINT16:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_INT16:
- return false;
- }
- break;
- case AR_BASIC_UINT32:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_INT32:
- return false;
- }
- break;
- case AR_BASIC_UINT64:
- switch (rightKind) {
- // For backwards compat we consider signed/unsigned the same.
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_INT64:
- return false;
- }
- break;
- case AR_BASIC_LITERAL_FLOAT:
- switch (rightKind) {
- case AR_BASIC_LITERAL_FLOAT:
- case AR_BASIC_FLOAT16:
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT64:
- return false;
- }
- break;
- case AR_BASIC_FLOAT16:
- switch (rightKind) {
- case AR_BASIC_LITERAL_FLOAT:
- return false;
- }
- break;
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- switch (rightKind) {
- case AR_BASIC_LITERAL_FLOAT:
- return false;
- }
- break;
- case AR_BASIC_FLOAT32:
- switch (rightKind) {
- case AR_BASIC_LITERAL_FLOAT:
- return false;
- }
- break;
- case AR_BASIC_FLOAT64:
- switch (rightKind) {
- case AR_BASIC_LITERAL_FLOAT:
- return false;
- }
- break;
- }
- return true;
- }
- bool HLSLExternalSource::IsIntCast(ArBasicKind leftKind, ArBasicKind rightKind) {
- // Eliminate exact matches first, then check for casts.
- if (leftKind == rightKind) {
- return false;
- }
- //
- // All minimum-bits types are only considered matches of themselves
- // and thus are not in this table.
- //
- switch (leftKind) {
- case AR_BASIC_LITERAL_INT:
- switch (rightKind) {
- case AR_BASIC_INT8:
- case AR_BASIC_INT16:
- case AR_BASIC_INT32:
- case AR_BASIC_INT64:
- case AR_BASIC_UINT8:
- case AR_BASIC_UINT16:
- case AR_BASIC_UINT32:
- case AR_BASIC_UINT64:
- return false;
- }
- break;
- case AR_BASIC_INT8:
- case AR_BASIC_INT16:
- case AR_BASIC_INT32:
- case AR_BASIC_INT64:
- case AR_BASIC_UINT8:
- case AR_BASIC_UINT16:
- case AR_BASIC_UINT32:
- case AR_BASIC_UINT64:
- switch (rightKind) {
- case AR_BASIC_LITERAL_INT:
- return false;
- }
- break;
- case AR_BASIC_LITERAL_FLOAT:
- switch (rightKind) {
- case AR_BASIC_LITERAL_FLOAT:
- case AR_BASIC_FLOAT16:
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT64:
- return false;
- }
- break;
- case AR_BASIC_FLOAT16:
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT64:
- switch (rightKind) {
- case AR_BASIC_LITERAL_FLOAT:
- return false;
- }
- break;
- }
- return true;
- }
- UINT64 HLSLExternalSource::ScoreCast(QualType pLType, QualType pRType)
- {
- if (pLType.getCanonicalType() == pRType.getCanonicalType()) {
- return 0;
- }
- UINT64 uScore = 0;
- UINT uLSize = GetNumElements(pLType);
- UINT uRSize = GetNumElements(pRType);
- UINT uCompareSize;
- bool bLCast = false;
- bool bRCast = false;
- bool bLIntCast = false;
- bool bRIntCast = false;
- bool bLPromo = false;
- bool bRPromo = false;
- uCompareSize = GetNumConvertCheckElts(pLType, uLSize, pRType, uRSize);
- if (uCompareSize > uRSize) {
- uCompareSize = uRSize;
- }
- for (UINT i = 0; i < uCompareSize; i++) {
- ArBasicKind LeftElementKind, RightElementKind;
- ArBasicKind CombinedKind = AR_BASIC_BOOL;
- QualType leftSub = GetNthElementType(pLType, i);
- QualType rightSub = GetNthElementType(pRType, i);
- ArTypeObjectKind leftKind = GetTypeObjectKind(leftSub);
- ArTypeObjectKind rightKind = GetTypeObjectKind(rightSub);
- LeftElementKind = GetTypeElementKind(leftSub);
- RightElementKind = GetTypeElementKind(rightSub);
- // CollectInfo is called with AR_TINFO_ALLOW_OBJECTS, and the resulting
- // information needed is the ShapeKind, EltKind and ObjKind.
- if (!leftSub.isNull() && !rightSub.isNull() && leftKind != AR_TOBJ_INVALID && rightKind != AR_TOBJ_INVALID) {
- bool bCombine;
-
- if (leftKind == AR_TOBJ_OBJECT || rightKind == AR_TOBJ_OBJECT) {
- DXASSERT(rightKind == AR_TOBJ_OBJECT, "otherwise prior check is incorrect");
- ArBasicKind LeftObjKind = LeftElementKind; // actually LeftElementKind would have been the element
- ArBasicKind RightObjKind = RightElementKind;
- LeftElementKind = LeftObjKind;
- RightElementKind = RightObjKind;
- if (leftKind != rightKind) {
- bCombine = false;
- }
- else if (!(bCombine = CombineObjectTypes(LeftObjKind, RightObjKind, &CombinedKind))) {
- bCombine = CombineObjectTypes(RightObjKind, LeftObjKind, &CombinedKind);
- }
- }
- else {
- bCombine = CombineBasicTypes(LeftElementKind, RightElementKind, &CombinedKind);
- }
- if (bCombine && IsPromotion(LeftElementKind, CombinedKind)) {
- bLPromo = true;
- }
- else if (!bCombine || IsCast(LeftElementKind, CombinedKind)) {
- bLCast = true;
- }
- else if (IsIntCast(LeftElementKind, CombinedKind)) {
- bLIntCast = true;
- }
- if (bCombine && IsPromotion(CombinedKind, RightElementKind)) {
- bRPromo = true;
- } else if (!bCombine || IsCast(CombinedKind, RightElementKind)) {
- bRCast = true;
- } else if (IsIntCast(CombinedKind, RightElementKind)) {
- bRIntCast = true;
- }
- } else {
- bLCast = true;
- bRCast = true;
- }
- }
- #define SCORE_COND(shift, cond) { \
- if (cond) uScore += 1UI64 << (SCORE_MIN_SHIFT + SCORE_PARAM_SHIFT * shift); }
- SCORE_COND(0, uRSize < uLSize);
- SCORE_COND(1, bLPromo);
- SCORE_COND(2, bRPromo);
- SCORE_COND(3, bLIntCast);
- SCORE_COND(4, bRIntCast);
- SCORE_COND(5, bLCast);
- SCORE_COND(6, bRCast);
- SCORE_COND(7, uLSize < uRSize);
- #undef SCORE_COND
- // Make sure our scores fit in a UINT64.
- C_ASSERT(SCORE_MIN_SHIFT + SCORE_PARAM_SHIFT * 8 <= 64);
- return uScore;
- }
- UINT64 HLSLExternalSource::ScoreImplicitConversionSequence(const ImplicitConversionSequence *ics) {
- DXASSERT(ics, "otherwise conversion has not been initialized");
- if (!ics->isInitialized()) {
- return 0;
- }
- if (!ics->isStandard()) {
- return SCORE_MAX;
- }
- QualType fromType = ics->Standard.getFromType();
- QualType toType = ics->Standard.getToType(2); // final type
- return ScoreCast(toType, fromType);
- }
- UINT64 HLSLExternalSource::ScoreFunction(OverloadCandidateSet::iterator &Cand) {
- // Ignore target version mismatches.
- // in/out considerations have been taken care of by viability.
- // 'this' considerations don't matter without inheritance, other
- // than lookup and viability.
- UINT64 result = 0;
- for (unsigned convIdx = 0; convIdx < Cand->NumConversions; ++convIdx) {
- UINT64 score;
-
- score = ScoreImplicitConversionSequence(Cand->Conversions + convIdx);
- if (score == SCORE_MAX) {
- return SCORE_MAX;
- }
- result += score;
- score = ScoreImplicitConversionSequence(Cand->OutConversions + convIdx);
- if (score == SCORE_MAX) {
- return SCORE_MAX;
- }
- result += score;
- }
- return result;
- }
- OverloadingResult HLSLExternalSource::GetBestViableFunction(
- SourceLocation Loc,
- OverloadCandidateSet& set,
- OverloadCandidateSet::iterator& Best)
- {
- UINT64 bestScore = SCORE_MAX;
- unsigned scoreMatch = 0;
- Best = set.end();
- if (set.size() == 1 && set.begin()->Viable) {
- Best = set.begin();
- return OR_Success;
- }
- for (OverloadCandidateSet::iterator Cand = set.begin(); Cand != set.end(); ++Cand) {
- if (Cand->Viable) {
- UINT64 score = ScoreFunction(Cand);
- if (score != SCORE_MAX) {
- if (score == bestScore) {
- ++scoreMatch;
- } else if (score < bestScore) {
- Best = Cand;
- scoreMatch = 1;
- bestScore = score;
- }
- }
- }
- }
- if (Best == set.end()) {
- return OR_No_Viable_Function;
- }
- if (scoreMatch > 1) {
- Best = set.end();
- return OR_Ambiguous;
- }
- // No need to check for deleted functions to yield OR_Deleted.
- return OR_Success;
- }
- /// <summary>
- /// Initializes the specified <paramref name="initSequence" /> describing how
- /// <paramref name="Entity" /> is initialized with <paramref name="Args" />.
- /// </summary>
- /// <param name="Entity">Entity being initialized; a variable, return result, etc.</param>
- /// <param name="Kind">Kind of initialization: copying, list-initializing, constructing, etc.</param>
- /// <param name="Args">Arguments to the initialization.</param>
- /// <param name="TopLevelOfInitList">Whether this is the top-level of an initialization list.</param>
- /// <param name="initSequence">Initialization sequence description to initialize.</param>
- void HLSLExternalSource::InitializeInitSequenceForHLSL(
- const InitializedEntity& Entity,
- const InitializationKind& Kind,
- MultiExprArg Args,
- bool TopLevelOfInitList,
- _Inout_ InitializationSequence* initSequence)
- {
- DXASSERT_NOMSG(initSequence != nullptr);
- // In HLSL there are no default initializers, eg float4x4 m();
- if (Kind.getKind() == InitializationKind::IK_Default) {
- return;
- }
- // Value initializers occur for temporaries with empty parens or braces.
- if (Kind.getKind() == InitializationKind::IK_Value) {
- m_sema->Diag(Kind.getLocation(), diag::err_hlsl_type_empty_init) << Entity.getType();
- SilenceSequenceDiagnostics(initSequence);
- return;
- }
- // If we have a DirectList, we should have a single InitListExprClass argument.
- DXASSERT(
- Kind.getKind() != InitializationKind::IK_DirectList ||
- (Args.size() == 1 && Args.front()->getStmtClass() == Stmt::InitListExprClass),
- "otherwise caller is passing in incorrect initialization configuration");
- bool isCast = Kind.isCStyleCast();
- QualType destType = Entity.getType();
- ArTypeObjectKind destShape = GetTypeObjectKind(destType);
- // Direct initialization occurs for explicit constructor arguments.
- // E.g.: http://en.cppreference.com/w/cpp/language/direct_initialization
- if (Kind.getKind() == InitializationKind::IK_Direct && destShape == AR_TOBJ_COMPOUND &&
- !Kind.isCStyleOrFunctionalCast()) {
- m_sema->Diag(Kind.getLocation(), diag::err_hlsl_require_numeric_base_for_ctor);
- SilenceSequenceDiagnostics(initSequence);
- return;
- }
- bool flatten =
- (Kind.getKind() == InitializationKind::IK_Direct && !isCast) ||
- Kind.getKind() == InitializationKind::IK_DirectList ||
- (Args.size() == 1 && Args.front()->getStmtClass() == Stmt::InitListExprClass);
- if (flatten) {
- // TODO: InitializationSequence::Perform in SemaInit should take the arity of incomplete
- // array types to adjust the value - we do calculate this as part of type analysis.
- // Until this is done, s_arr_i_f arr_struct_none[] = { }; succeeds when it should instead fail.
- FlattenedTypeIterator::ComparisonResult comparisonResult =
- FlattenedTypeIterator::CompareTypesForInit(
- *this, destType, Args,
- Kind.getLocation(), Kind.getLocation());
- if (comparisonResult.IsConvertibleAndEqualLength() ||
- (isCast && comparisonResult.IsConvertibleAndLeftLonger()))
- {
- initSequence->AddListInitializationStep(destType);
- }
- else
- {
- SourceLocation diagLocation;
- if (Args.size() > 0)
- {
- diagLocation = Args.front()->getLocStart();
- }
- else
- {
- diagLocation = Entity.getDiagLoc();
- }
- m_sema->Diag(diagLocation,
- diag::err_vector_incorrect_num_initializers)
- << (comparisonResult.RightCount < comparisonResult.LeftCount)
- << comparisonResult.LeftCount << comparisonResult.RightCount;
- SilenceSequenceDiagnostics(initSequence);
- }
- }
- else {
- DXASSERT(Args.size() == 1, "otherwise this was mis-parsed or should be a list initialization");
- Expr* firstArg = Args.front();
- if (IsExpressionBinaryComma(firstArg)) {
- m_sema->Diag(firstArg->getExprLoc(), diag::warn_hlsl_comma_in_init);
- }
- ExprResult expr = ExprResult(firstArg);
- Sema::CheckedConversionKind cck = Kind.isExplicitCast() ?
- Sema::CheckedConversionKind::CCK_CStyleCast :
- Sema::CheckedConversionKind::CCK_ImplicitConversion;
- unsigned int msg = 0;
- CastKind castKind;
- CXXCastPath basePath;
- SourceRange range = Kind.getRange();
- ImplicitConversionSequence ics;
- ics.setStandard();
- bool castWorked = TryStaticCastForHLSL(
- expr, destType, cck, range, msg, castKind, basePath, ListInitializationFalse, SuppressWarningsFalse, SuppressErrorsTrue, &ics.Standard);
- if (castWorked) {
- if (destType.getCanonicalType() ==
- firstArg->getType().getCanonicalType() &&
- (ics.Standard).First != ICK_Lvalue_To_Rvalue) {
- initSequence->AddCAssignmentStep(destType);
- } else {
- initSequence->AddConversionSequenceStep(ics, destType.getNonReferenceType(), TopLevelOfInitList);
- }
- }
- else {
- initSequence->SetFailed(InitializationSequence::FK_ConversionFailed);
- }
- }
- }
- bool HLSLExternalSource::IsConversionToLessOrEqualElements(
- const QualType& sourceType,
- const QualType& targetType,
- bool explicitConversion)
- {
- DXASSERT_NOMSG(!sourceType.isNull());
- DXASSERT_NOMSG(!targetType.isNull());
- ArTypeInfo sourceTypeInfo;
- ArTypeInfo targetTypeInfo;
- GetConversionForm(sourceType, explicitConversion, &sourceTypeInfo);
- GetConversionForm(targetType, explicitConversion, &targetTypeInfo);
- if (sourceTypeInfo.EltKind != targetTypeInfo.EltKind)
- {
- return false;
- }
- bool isVecMatTrunc = sourceTypeInfo.ShapeKind == AR_TOBJ_VECTOR &&
- targetTypeInfo.ShapeKind == AR_TOBJ_BASIC;
- if (sourceTypeInfo.ShapeKind != targetTypeInfo.ShapeKind &&
- !isVecMatTrunc)
- {
- return false;
- }
- if (sourceTypeInfo.ShapeKind == AR_TOBJ_OBJECT &&
- sourceTypeInfo.ObjKind == targetTypeInfo.ObjKind) {
- return true;
- }
- // Same struct is eqaul.
- if (sourceTypeInfo.ShapeKind == AR_TOBJ_COMPOUND &&
- sourceType.getCanonicalType().getUnqualifiedType() ==
- targetType.getCanonicalType().getUnqualifiedType()) {
- return true;
- }
- if (sourceTypeInfo.ShapeKind != AR_TOBJ_SCALAR &&
- sourceTypeInfo.ShapeKind != AR_TOBJ_VECTOR &&
- sourceTypeInfo.ShapeKind != AR_TOBJ_MATRIX)
- {
- return false;
- }
- return targetTypeInfo.uTotalElts <= sourceTypeInfo.uTotalElts;
- }
- bool HLSLExternalSource::IsConversionToLessOrEqualElements(
- const ExprResult& sourceExpr,
- const QualType& targetType,
- bool explicitConversion)
- {
- if (sourceExpr.isInvalid() || targetType.isNull())
- {
- return false;
- }
- return IsConversionToLessOrEqualElements(sourceExpr.get()->getType(), targetType, explicitConversion);
- }
- bool HLSLExternalSource::IsTypeNumeric(QualType type, UINT* count)
- {
- DXASSERT_NOMSG(!type.isNull());
- DXASSERT_NOMSG(count != nullptr);
- *count = 0;
- UINT subCount = 0;
- ArTypeObjectKind shapeKind = GetTypeObjectKind(type);
- switch (shapeKind)
- {
- case AR_TOBJ_ARRAY:
- if (IsTypeNumeric(m_context->getAsArrayType(type)->getElementType(), &subCount))
- {
- *count = subCount * GetArraySize(type);
- return true;
- }
- return false;
- case AR_TOBJ_COMPOUND:
- {
- FlattenedTypeIterator it(SourceLocation(), type, *this);
- // Return false for empty struct.
- if (!it.hasCurrentElement())
- return false;
- while (it.hasCurrentElement()) {
- bool isFieldNumeric = IsTypeNumeric(it.getCurrentElement(), &subCount);
- if (!isFieldNumeric) {
- return false;
- }
- *count += (subCount * it.getCurrentElementSize());
- it.advanceCurrentElement(it.getCurrentElementSize());
- }
- return true;
- }
- default:
- DXASSERT(false, "unreachable");
- case AR_TOBJ_BASIC:
- case AR_TOBJ_MATRIX:
- case AR_TOBJ_VECTOR:
- *count = GetElementCount(type);
- return IsBasicKindNumeric(GetTypeElementKind(type));
- case AR_TOBJ_OBJECT:
- return false;
- }
- }
- enum MatrixMemberAccessError {
- MatrixMemberAccessError_None, // No errors found.
- MatrixMemberAccessError_BadFormat, // Formatting error (non-digit).
- MatrixMemberAccessError_MixingRefs, // Mix of zero-based and one-based references.
- MatrixMemberAccessError_Empty, // No members specified.
- MatrixMemberAccessError_ZeroInOneBased, // A zero was used in a one-based reference.
- MatrixMemberAccessError_FourInZeroBased, // A four was used in a zero-based reference.
- MatrixMemberAccessError_TooManyPositions, // Too many positions (more than four) were specified.
- };
- static
- MatrixMemberAccessError TryConsumeMatrixDigit(const char*& memberText, uint32_t* value)
- {
- DXASSERT_NOMSG(memberText != nullptr);
- DXASSERT_NOMSG(value != nullptr);
- if ('0' <= *memberText && *memberText <= '9')
- {
- *value = (*memberText) - '0';
- }
- else
- {
- return MatrixMemberAccessError_BadFormat;
- }
- memberText++;
- return MatrixMemberAccessError_None;
- }
- static
- MatrixMemberAccessError TryParseMatrixMemberAccess(_In_z_ const char* memberText, _Out_ MatrixMemberAccessPositions* value)
- {
- DXASSERT_NOMSG(memberText != nullptr);
- DXASSERT_NOMSG(value != nullptr);
- MatrixMemberAccessPositions result;
- bool zeroBasedDecided = false;
- bool zeroBased = false;
- // Set the output value to invalid to allow early exits when errors are found.
- value->IsValid = 0;
- // Assume this is true until proven otherwise.
- result.IsValid = 1;
- result.Count = 0;
- while (*memberText)
- {
- // Check for a leading underscore.
- if (*memberText != '_')
- {
- return MatrixMemberAccessError_BadFormat;
- }
- ++memberText;
- // Check whether we have an 'm' or a digit.
- if (*memberText == 'm')
- {
- if (zeroBasedDecided && !zeroBased)
- {
- return MatrixMemberAccessError_MixingRefs;
- }
- zeroBased = true;
- zeroBasedDecided = true;
- ++memberText;
- }
- else if (!('0' <= *memberText && *memberText <= '9'))
- {
- return MatrixMemberAccessError_BadFormat;
- }
- else
- {
- if (zeroBasedDecided && zeroBased)
- {
- return MatrixMemberAccessError_MixingRefs;
- }
- zeroBased = false;
- zeroBasedDecided = true;
- }
- // Consume two digits for the position.
- uint32_t rowPosition;
- uint32_t colPosition;
- MatrixMemberAccessError digitError;
- if (MatrixMemberAccessError_None != (digitError = TryConsumeMatrixDigit(memberText, &rowPosition)))
- {
- return digitError;
- }
- if (MatrixMemberAccessError_None != (digitError = TryConsumeMatrixDigit(memberText, &colPosition)))
- {
- return digitError;
- }
- // Look for specific common errors (developer likely mixed up reference style).
- if (zeroBased)
- {
- if (rowPosition == 4 || colPosition == 4)
- {
- return MatrixMemberAccessError_FourInZeroBased;
- }
- }
- else
- {
- if (rowPosition == 0 || colPosition == 0)
- {
- return MatrixMemberAccessError_ZeroInOneBased;
- }
- // SetPosition will use zero-based indices.
- --rowPosition;
- --colPosition;
- }
- if (result.Count == 4)
- {
- return MatrixMemberAccessError_TooManyPositions;
- }
- result.SetPosition(result.Count, rowPosition, colPosition);
- result.Count++;
- }
- if (result.Count == 0)
- {
- return MatrixMemberAccessError_Empty;
- }
- *value = result;
- return MatrixMemberAccessError_None;
- }
- bool HLSLExternalSource::LookupMatrixMemberExprForHLSL(
- Expr& BaseExpr,
- DeclarationName MemberName,
- bool IsArrow,
- SourceLocation OpLoc,
- SourceLocation MemberLoc,
- ExprResult* result)
- {
- DXASSERT_NOMSG(result != nullptr);
- QualType BaseType = BaseExpr.getType();
- DXASSERT(!BaseType.isNull(), "otherwise caller should have stopped analysis much earlier");
- // Assume failure.
- *result = ExprError();
- if (GetTypeObjectKind(BaseType) != AR_TOBJ_MATRIX)
- {
- return false;
- }
- QualType elementType;
- UINT rowCount, colCount;
- GetRowsAndCols(BaseType, rowCount, colCount);
- elementType = GetMatrixOrVectorElementType(BaseType);
- IdentifierInfo *member = MemberName.getAsIdentifierInfo();
- const char *memberText = member->getNameStart();
- MatrixMemberAccessPositions positions;
- MatrixMemberAccessError memberAccessError;
- unsigned msg = 0;
- memberAccessError = TryParseMatrixMemberAccess(memberText, &positions);
- switch (memberAccessError)
- {
- case MatrixMemberAccessError_BadFormat:
- msg = diag::err_hlsl_matrix_member_bad_format;
- break;
- case MatrixMemberAccessError_Empty:
- msg = diag::err_hlsl_matrix_member_empty;
- break;
- case MatrixMemberAccessError_FourInZeroBased:
- msg = diag::err_hlsl_matrix_member_four_in_zero_based;
- break;
- case MatrixMemberAccessError_MixingRefs:
- msg = diag::err_hlsl_matrix_member_mixing_refs;
- break;
- case MatrixMemberAccessError_None:
- msg = 0;
- DXASSERT(positions.IsValid, "otherwise an error should have been returned");
- // Check the position with the type now.
- for (unsigned int i = 0; i < positions.Count; i++)
- {
- uint32_t rowPos, colPos;
- positions.GetPosition(i, &rowPos, &colPos);
- if (rowPos >= rowCount || colPos >= colCount)
- {
- msg = diag::err_hlsl_matrix_member_out_of_bounds;
- break;
- }
- }
- break;
- case MatrixMemberAccessError_TooManyPositions:
- msg = diag::err_hlsl_matrix_member_too_many_positions;
- break;
- case MatrixMemberAccessError_ZeroInOneBased:
- msg = diag::err_hlsl_matrix_member_zero_in_one_based;
- break;
- default:
- llvm_unreachable("Unknown MatrixMemberAccessError value");
- }
- if (msg != 0)
- {
- m_sema->Diag(MemberLoc, msg) << memberText;
- // It's possible that it's a simple out-of-bounds condition. In this case,
- // generate the member access expression with the correct arity and continue
- // processing.
- if (!positions.IsValid)
- {
- return true;
- }
- }
- DXASSERT(positions.IsValid, "otherwise an error should have been returned");
- // Consume elements
- QualType resultType;
- if (positions.Count == 1)
- resultType = elementType;
- else
- resultType = NewSimpleAggregateType(AR_TOBJ_UNKNOWN, GetTypeElementKind(elementType), 0, OneRow, positions.Count);
- // Add qualifiers from BaseType.
- resultType = m_context->getQualifiedType(resultType, BaseType.getQualifiers());
- ExprValueKind VK =
- positions.ContainsDuplicateElements() ? VK_RValue :
- (IsArrow ? VK_LValue : BaseExpr.getValueKind());
- ExtMatrixElementExpr* matrixExpr = new (m_context)ExtMatrixElementExpr(resultType, VK, &BaseExpr, *member, MemberLoc, positions);
- *result = matrixExpr;
- return true;
- }
- enum VectorMemberAccessError {
- VectorMemberAccessError_None, // No errors found.
- VectorMemberAccessError_BadFormat, // Formatting error (not in 'rgba' or 'xyzw').
- VectorMemberAccessError_MixingStyles, // Mix of rgba and xyzw swizzle styles.
- VectorMemberAccessError_Empty, // No members specified.
- VectorMemberAccessError_TooManyPositions, // Too many positions (more than four) were specified.
- };
- static
- VectorMemberAccessError TryConsumeVectorDigit(const char*& memberText, uint32_t* value, bool &rgbaStyle) {
- DXASSERT_NOMSG(memberText != nullptr);
- DXASSERT_NOMSG(value != nullptr);
- rgbaStyle = false;
- switch (*memberText) {
- case 'r':
- rgbaStyle = true;
- case 'x':
- *value = 0;
- break;
- case 'g':
- rgbaStyle = true;
- case 'y':
- *value = 1;
- break;
- case 'b':
- rgbaStyle = true;
- case 'z':
- *value = 2;
- break;
- case 'a':
- rgbaStyle = true;
- case 'w':
- *value = 3;
- break;
- default:
- return VectorMemberAccessError_BadFormat;
- }
- memberText++;
- return VectorMemberAccessError_None;
- }
- static
- VectorMemberAccessError TryParseVectorMemberAccess(_In_z_ const char* memberText, _Out_ VectorMemberAccessPositions* value) {
- DXASSERT_NOMSG(memberText != nullptr);
- DXASSERT_NOMSG(value != nullptr);
- VectorMemberAccessPositions result;
- bool rgbaStyleDecided = false;
- bool rgbaStyle = false;
- // Set the output value to invalid to allow early exits when errors are found.
- value->IsValid = 0;
- // Assume this is true until proven otherwise.
- result.IsValid = 1;
- result.Count = 0;
- while (*memberText) {
- // Consume one character for the swizzle.
- uint32_t colPosition;
- VectorMemberAccessError digitError;
- bool rgbaStyleTmp = false;
- if (VectorMemberAccessError_None != (digitError = TryConsumeVectorDigit(memberText, &colPosition, rgbaStyleTmp))) {
- return digitError;
- }
- if (rgbaStyleDecided && rgbaStyleTmp != rgbaStyle) {
- return VectorMemberAccessError_MixingStyles;
- }
- else {
- rgbaStyleDecided = true;
- rgbaStyle = rgbaStyleTmp;
- }
- if (result.Count == 4) {
- return VectorMemberAccessError_TooManyPositions;
- }
- result.SetPosition(result.Count, colPosition);
- result.Count++;
- }
- if (result.Count == 0) {
- return VectorMemberAccessError_Empty;
- }
- *value = result;
- return VectorMemberAccessError_None;
- }
- bool HLSLExternalSource::LookupVectorMemberExprForHLSL(
- Expr& BaseExpr,
- DeclarationName MemberName,
- bool IsArrow,
- SourceLocation OpLoc,
- SourceLocation MemberLoc,
- ExprResult* result) {
- DXASSERT_NOMSG(result != nullptr);
- QualType BaseType = BaseExpr.getType();
- DXASSERT(!BaseType.isNull(), "otherwise caller should have stopped analysis much earlier");
- // Assume failure.
- *result = ExprError();
- if (GetTypeObjectKind(BaseType) != AR_TOBJ_VECTOR) {
- return false;
- }
- QualType elementType;
- UINT colCount = GetHLSLVecSize(BaseType);
- elementType = GetMatrixOrVectorElementType(BaseType);
- IdentifierInfo *member = MemberName.getAsIdentifierInfo();
- const char *memberText = member->getNameStart();
- VectorMemberAccessPositions positions;
- VectorMemberAccessError memberAccessError;
- unsigned msg = 0;
- memberAccessError = TryParseVectorMemberAccess(memberText, &positions);
- switch (memberAccessError) {
- case VectorMemberAccessError_BadFormat:
- msg = diag::err_hlsl_vector_member_bad_format;
- break;
- case VectorMemberAccessError_Empty:
- msg = diag::err_hlsl_vector_member_empty;
- break;
- case VectorMemberAccessError_MixingStyles:
- msg = diag::err_ext_vector_component_name_mixedsets;
- break;
- case VectorMemberAccessError_None:
- msg = 0;
- DXASSERT(positions.IsValid, "otherwise an error should have been returned");
- // Check the position with the type now.
- for (unsigned int i = 0; i < positions.Count; i++) {
- uint32_t colPos;
- positions.GetPosition(i, &colPos);
- if (colPos >= colCount) {
- msg = diag::err_hlsl_vector_member_out_of_bounds;
- break;
- }
- }
- break;
- case VectorMemberAccessError_TooManyPositions:
- msg = diag::err_hlsl_vector_member_too_many_positions;
- break;
- default:
- llvm_unreachable("Unknown VectorMemberAccessError value");
- }
- if (msg != 0) {
- m_sema->Diag(MemberLoc, msg) << memberText;
- // It's possible that it's a simple out-of-bounds condition. In this case,
- // generate the member access expression with the correct arity and continue
- // processing.
- if (!positions.IsValid) {
- return true;
- }
- }
- DXASSERT(positions.IsValid, "otherwise an error should have been returned");
- // Consume elements
- QualType resultType;
- if (positions.Count == 1)
- resultType = elementType;
- else
- resultType = NewSimpleAggregateType(AR_TOBJ_UNKNOWN, GetTypeElementKind(elementType), 0, OneRow, positions.Count);
-
- // Add qualifiers from BaseType.
- resultType = m_context->getQualifiedType(resultType, BaseType.getQualifiers());
- ExprValueKind VK =
- positions.ContainsDuplicateElements() ? VK_RValue :
- (IsArrow ? VK_LValue : BaseExpr.getValueKind());
- HLSLVectorElementExpr* vectorExpr = new (m_context)HLSLVectorElementExpr(resultType, VK, &BaseExpr, *member, MemberLoc, positions);
- *result = vectorExpr;
- return true;
- }
- ExprResult HLSLExternalSource::MaybeConvertScalarToVector(_In_ clang::Expr* E) {
- DXASSERT_NOMSG(E != nullptr);
- ArBasicKind basic = GetTypeElementKind(E->getType());
- if (!IS_BASIC_PRIMITIVE(basic)) {
- return E;
- }
- ArTypeObjectKind kind = GetTypeObjectKind(E->getType());
- if (kind != AR_TOBJ_SCALAR) {
- return E;
- }
- QualType targetType = NewSimpleAggregateType(AR_TOBJ_VECTOR, basic, 0, 1, 1);
- return ImplicitCastExpr::Create(*m_context, targetType, CastKind::CK_HLSLVectorSplat, E, nullptr, E->getValueKind());
- }
- static clang::CastKind ImplicitConversionKindToCastKind(
- clang::ImplicitConversionKind ICK,
- ArBasicKind FromKind,
- ArBasicKind ToKind) {
- // TODO: Shouldn't we have more specific ICK enums so we don't have to re-evaluate
- // based on from/to kinds in order to determine CastKind?
- // There's a FIXME note in PerformImplicitConversion that calls out exactly this
- // problem.
- switch (ICK) {
- case ICK_Integral_Promotion:
- case ICK_Integral_Conversion:
- return CK_IntegralCast;
- case ICK_Floating_Promotion:
- case ICK_Floating_Conversion:
- return CK_FloatingCast;
- case ICK_Floating_Integral:
- if (IS_BASIC_FLOAT(FromKind) && IS_BASIC_AINT(ToKind))
- return CK_FloatingToIntegral;
- else if ((IS_BASIC_AINT(FromKind) || IS_BASIC_BOOL(FromKind)) && IS_BASIC_FLOAT(ToKind))
- return CK_IntegralToFloating;
- break;
- case ICK_Boolean_Conversion:
- if (IS_BASIC_FLOAT(FromKind) && IS_BASIC_BOOL(ToKind))
- return CK_FloatingToBoolean;
- else if (IS_BASIC_AINT(FromKind) && IS_BASIC_BOOL(ToKind))
- return CK_IntegralToBoolean;
- break;
- }
- return CK_Invalid;
- }
- static clang::CastKind ConvertToComponentCastKind(clang::CastKind CK) {
- switch (CK) {
- case CK_IntegralCast:
- return CK_HLSLCC_IntegralCast;
- case CK_FloatingCast:
- return CK_HLSLCC_FloatingCast;
- case CK_FloatingToIntegral:
- return CK_HLSLCC_FloatingToIntegral;
- case CK_IntegralToFloating:
- return CK_HLSLCC_IntegralToFloating;
- case CK_FloatingToBoolean:
- return CK_HLSLCC_FloatingToBoolean;
- case CK_IntegralToBoolean:
- return CK_HLSLCC_IntegralToBoolean;
- }
- return CK_Invalid;
- }
- clang::Expr *HLSLExternalSource::HLSLImpCastToScalar(
- _In_ clang::Sema* self,
- _In_ clang::Expr* From,
- ArTypeObjectKind FromShape,
- ArBasicKind EltKind)
- {
- clang::CastKind CK = CK_Invalid;
- if (AR_TOBJ_MATRIX == FromShape)
- CK = CK_HLSLMatrixToScalarCast;
- if (AR_TOBJ_VECTOR == FromShape)
- CK = CK_HLSLVectorToScalarCast;
- if (CK_Invalid != CK) {
- return self->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_BASIC, EltKind, 0, 1, 1), CK, From->getValueKind()).get();
- }
- return From;
- }
- clang::ExprResult HLSLExternalSource::PerformHLSLConversion(
- _In_ clang::Expr* From,
- _In_ clang::QualType targetType,
- _In_ const clang::StandardConversionSequence &SCS,
- _In_ clang::Sema::CheckedConversionKind CCK)
- {
- QualType sourceType = From->getType();
- sourceType = GetStructuralForm(sourceType);
- targetType = GetStructuralForm(targetType);
- ArTypeInfo SourceInfo, TargetInfo;
- CollectInfo(sourceType, &SourceInfo);
- CollectInfo(targetType, &TargetInfo);
- clang::CastKind CK = CK_Invalid;
- QualType intermediateTarget;
- // TODO: construct vector/matrix and component cast expressions
- switch (SCS.Second) {
- case ICK_Flat_Conversion: {
- // TODO: determine how to handle individual component conversions:
- // - have an array of conversions for ComponentConversion in SCS?
- // convert that to an array of casts under a special kind of flat
- // flat conversion node? What do component conversion casts cast
- // from? We don't have a From expression for individiual components.
- From = m_sema->ImpCastExprToType(From, targetType.getUnqualifiedType(), CK_FlatConversion, From->getValueKind(), /*BasePath=*/0, CCK).get();
- break;
- }
- case ICK_HLSLVector_Splat: {
- // 1. optionally convert from vec1 or mat1x1 to scalar
- From = HLSLImpCastToScalar(m_sema, From, SourceInfo.ShapeKind, SourceInfo.EltKind);
- // 2. optionally convert component type
- if (ICK_Identity != SCS.ComponentConversion) {
- CK = ImplicitConversionKindToCastKind(SCS.ComponentConversion, SourceInfo.EltKind, TargetInfo.EltKind);
- if (CK_Invalid != CK) {
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_BASIC, TargetInfo.EltKind, 0, 1, 1), CK, From->getValueKind(), /*BasePath=*/0, CCK).get();
- }
- }
- // 3. splat scalar to final vector or matrix
- CK = CK_Invalid;
- if (AR_TOBJ_VECTOR == TargetInfo.ShapeKind)
- CK = CK_HLSLVectorSplat;
- else if (AR_TOBJ_MATRIX == TargetInfo.ShapeKind)
- CK = CK_HLSLMatrixSplat;
- if (CK_Invalid != CK) {
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(TargetInfo.ShapeKind, TargetInfo.EltKind, 0, TargetInfo.uRows, TargetInfo.uCols), CK, From->getValueKind(), /*BasePath=*/0, CCK).get();
- }
- break;
- }
- case ICK_HLSLVector_Scalar: {
- // 1. select vector or matrix component
- From = HLSLImpCastToScalar(m_sema, From, SourceInfo.ShapeKind, SourceInfo.EltKind);
- // 2. optionally convert component type
- if (ICK_Identity != SCS.ComponentConversion) {
- CK = ImplicitConversionKindToCastKind(SCS.ComponentConversion, SourceInfo.EltKind, TargetInfo.EltKind);
- if (CK_Invalid != CK) {
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_BASIC, TargetInfo.EltKind, 0, 1, 1), CK, From->getValueKind(), /*BasePath=*/0, CCK).get();
- }
- }
- break;
- }
- // The following two (three if we re-introduce ICK_HLSLComponent_Conversion) steps
- // can be done with case fall-through, since this is the order in which we want to
- // do the conversion operations.
- case ICK_HLSLVector_Truncation: {
- // 1. dimension truncation
- // vector truncation or matrix truncation?
- if (SourceInfo.ShapeKind == AR_TOBJ_VECTOR) {
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_VECTOR, SourceInfo.EltKind, 0, 1, TargetInfo.uTotalElts),
- CK_HLSLVectorTruncationCast, From->getValueKind(), /*BasePath=*/0, CCK).get();
- } else if (SourceInfo.ShapeKind == AR_TOBJ_MATRIX) {
- if (TargetInfo.ShapeKind == AR_TOBJ_VECTOR && 1 == SourceInfo.uCols) {
- // Handle the column to vector case
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_MATRIX, SourceInfo.EltKind, 0, TargetInfo.uCols, 1),
- CK_HLSLMatrixTruncationCast, From->getValueKind(), /*BasePath=*/0, CCK).get();
- } else {
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_MATRIX, SourceInfo.EltKind, 0, TargetInfo.uRows, TargetInfo.uCols),
- CK_HLSLMatrixTruncationCast, From->getValueKind(), /*BasePath=*/0, CCK).get();
- }
- } else {
- DXASSERT(false, "PerformHLSLConversion: Invalid source type for truncation cast");
- }
- }
- __fallthrough;
- case ICK_HLSLVector_Conversion: {
- // 2. Do ShapeKind conversion if necessary
- if (SourceInfo.ShapeKind != TargetInfo.ShapeKind) {
- switch (TargetInfo.ShapeKind) {
- case AR_TOBJ_VECTOR:
- DXASSERT(AR_TOBJ_MATRIX == SourceInfo.ShapeKind, "otherwise, invalid casting sequence");
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_VECTOR, SourceInfo.EltKind, 0, TargetInfo.uRows, TargetInfo.uCols),
- CK_HLSLMatrixToVectorCast, From->getValueKind(), /*BasePath=*/0, CCK).get();
- break;
- case AR_TOBJ_MATRIX:
- DXASSERT(AR_TOBJ_VECTOR == SourceInfo.ShapeKind, "otherwise, invalid casting sequence");
- From = m_sema->ImpCastExprToType(From,
- NewSimpleAggregateType(AR_TOBJ_MATRIX, SourceInfo.EltKind, 0, TargetInfo.uRows, TargetInfo.uCols),
- CK_HLSLVectorToMatrixCast, From->getValueKind(), /*BasePath=*/0, CCK).get();
- break;
- case AR_TOBJ_BASIC:
- // Truncation may be followed by cast to scalar
- From = HLSLImpCastToScalar(m_sema, From, SourceInfo.ShapeKind, SourceInfo.EltKind);
- break;
- default:
- DXASSERT(false, "otherwise, invalid casting sequence");
- break;
- }
- }
- // 3. Do component type conversion
- if (ICK_Identity != SCS.ComponentConversion) {
- CK = ImplicitConversionKindToCastKind(SCS.ComponentConversion, SourceInfo.EltKind, TargetInfo.EltKind);
- if (TargetInfo.ShapeKind != AR_TOBJ_BASIC)
- CK = ConvertToComponentCastKind(CK);
- if (CK_Invalid != CK) {
- From = m_sema->ImpCastExprToType(From, targetType, CK, From->getValueKind(), /*BasePath=*/0, CCK).get();
- }
- }
- break;
- }
- case ICK_Identity:
- // Nothing to do.
- break;
- default:
- DXASSERT(false, "PerformHLSLConversion: Invalid SCS.Second conversion kind");
- }
- return From;
- }
- void HLSLExternalSource::GetConversionForm(
- QualType type,
- bool explicitConversion,
- ArTypeInfo* pTypeInfo)
- {
- //if (!CollectInfo(AR_TINFO_ALLOW_ALL, pTypeInfo))
- CollectInfo(type, pTypeInfo);
- // The fxc implementation reported pTypeInfo->ShapeKind separately in an output argument,
- // but that value is only used for pointer conversions.
- // When explicitly converting types complex aggregates can be treated
- // as vectors if they are entirely numeric.
- switch (pTypeInfo->ShapeKind)
- {
- case AR_TOBJ_COMPOUND:
- case AR_TOBJ_ARRAY:
- if (explicitConversion && IsTypeNumeric(type, &pTypeInfo->uTotalElts))
- {
- pTypeInfo->ShapeKind = AR_TOBJ_VECTOR;
- }
- else
- {
- pTypeInfo->ShapeKind = AR_TOBJ_COMPOUND;
- }
- DXASSERT_NOMSG(pTypeInfo->uRows == 1);
- pTypeInfo->uCols = pTypeInfo->uTotalElts;
- break;
- case AR_TOBJ_VECTOR:
- case AR_TOBJ_MATRIX:
- // Convert 1x1 types to scalars.
- if (pTypeInfo->uCols == 1 && pTypeInfo->uRows == 1)
- {
- pTypeInfo->ShapeKind = AR_TOBJ_BASIC;
- }
- break;
- }
- }
- static
- bool HandleVoidConversion(QualType source, QualType target, bool explicitConversion, _Out_ bool* allowed)
- {
- DXASSERT_NOMSG(allowed != nullptr);
- bool applicable = true;
- *allowed = true;
- if (explicitConversion) {
- // (void) non-void
- if (target->isVoidType()) {
- DXASSERT_NOMSG(*allowed);
- }
- // (non-void) void
- else if (source->isVoidType()) {
- *allowed = false;
- }
- else {
- applicable = false;
- }
- }
- else {
- // (void) void
- if (source->isVoidType() && target->isVoidType()) {
- DXASSERT_NOMSG(*allowed);
- }
- // (void) non-void, (non-void) void
- else if (source->isVoidType() || target->isVoidType()) {
- *allowed = false;
- }
- else {
- applicable = false;
- }
- }
- return applicable;
- }
- _Use_decl_annotations_
- bool HLSLExternalSource::CanConvert(
- SourceLocation loc,
- Expr* sourceExpr,
- QualType target,
- bool explicitConversion,
- _Out_opt_ TYPE_CONVERSION_REMARKS* remarks,
- _Inout_opt_ StandardConversionSequence* standard)
- {
- DXASSERT_NOMSG(sourceExpr != nullptr);
- DXASSERT_NOMSG(!target.isNull());
- // Implements the semantics of ArType::CanConvertTo.
- TYPE_CONVERSION_FLAGS Flags = explicitConversion ? TYPE_CONVERSION_EXPLICIT : TYPE_CONVERSION_DEFAULT;
- TYPE_CONVERSION_REMARKS Remarks = TYPE_CONVERSION_NONE;
- QualType source = sourceExpr->getType();
-
- // Convert to an r-value to begin with.
- bool needsLValueToRValue = sourceExpr->isLValue() &&
- !target->isLValueReferenceType() &&
- IsConversionToLessOrEqualElements(source, target, explicitConversion);
- bool targetRef = target->isReferenceType();
- // Initialize the output standard sequence if available.
- if (standard != nullptr) {
- // Set up a no-op conversion, other than lvalue to rvalue - HLSL does not support references.
- standard->setAsIdentityConversion();
- if (needsLValueToRValue) {
- standard->First = ICK_Lvalue_To_Rvalue;
- }
- standard->setFromType(source);
- standard->setAllToTypes(target);
- }
- source = GetStructuralForm(source);
- target = GetStructuralForm(target);
- // Temporary conversion kind tracking which will be used/fixed up at the end
- ImplicitConversionKind Second = ICK_Identity;
- ImplicitConversionKind ComponentConversion = ICK_Identity;
- // Identical types require no conversion.
- if (source == target) {
- Remarks = TYPE_CONVERSION_IDENTICAL;
- goto lSuccess;
- }
- // Trivial cases for void.
- bool allowed;
- if (HandleVoidConversion(source, target, explicitConversion, &allowed)) {
- if (allowed) {
- Remarks = target->isVoidType() ? TYPE_CONVERSION_TO_VOID : Remarks;
- goto lSuccess;
- }
- else {
- return false;
- }
- }
- ArTypeInfo TargetInfo, SourceInfo;
- CollectInfo(target, &TargetInfo);
- CollectInfo(source, &SourceInfo);
- UINT uTSize = TargetInfo.uTotalElts;
- UINT uSSize = SourceInfo.uTotalElts;
- // TODO: TYPE_CONVERSION_BY_REFERENCE does not seem possible here
- // are we missing cases?
- if ((Flags & TYPE_CONVERSION_BY_REFERENCE) != 0 && uTSize != uSSize) {
- return false;
- }
- // Structure cast.
- if (TargetInfo.ShapeKind == AR_TOBJ_COMPOUND || TargetInfo.ShapeKind == AR_TOBJ_ARRAY ||
- SourceInfo.ShapeKind == AR_TOBJ_COMPOUND || SourceInfo.ShapeKind == AR_TOBJ_ARRAY) {
- if (!explicitConversion && TargetInfo.ShapeKind != SourceInfo.ShapeKind)
- {
- return false;
- }
- const RecordType *targetRT = target->getAsStructureType();
- if (!targetRT)
- targetRT = dyn_cast<RecordType>(target);
- const RecordType *sourceRT = source->getAsStructureType();
- if (!sourceRT)
- sourceRT = dyn_cast<RecordType>(source);
- if (targetRT && sourceRT) {
- RecordDecl *targetRD = targetRT->getDecl();
- RecordDecl *sourceRD = sourceRT->getDecl();
- const CXXRecordDecl *targetCXXRD = dyn_cast<CXXRecordDecl>(targetRD);
- const CXXRecordDecl *sourceCXXRD = dyn_cast<CXXRecordDecl>(sourceRD);
- if (targetCXXRD && sourceCXXRD) {
- if (targetRD == sourceRD) {
- Second = ICK_Flat_Conversion;
- goto lSuccess;
- }
- if (sourceCXXRD->isDerivedFrom(targetCXXRD)) {
- Second = ICK_Flat_Conversion;
- goto lSuccess;
- }
- } else {
- if (targetRD == sourceRD) {
- Second = ICK_Flat_Conversion;
- goto lSuccess;
- }
- }
- }
- if (const BuiltinType *BT = source->getAs<BuiltinType>()) {
- BuiltinType::Kind kind = BT->getKind();
- switch (kind) {
- case BuiltinType::Kind::UInt:
- case BuiltinType::Kind::Int:
- case BuiltinType::Kind::Float:
- case BuiltinType::Kind::LitFloat:
- case BuiltinType::Kind::LitInt:
- if (explicitConversion) {
- Second = ICK_Flat_Conversion;
- goto lSuccess;
- }
- break;
- }
- }
- if (const BuiltinType *BT = source->getAs<BuiltinType>()) {
- BuiltinType::Kind kind = BT->getKind();
- switch (kind) {
- case BuiltinType::Kind::UInt:
- case BuiltinType::Kind::Int:
- case BuiltinType::Kind::Float:
- case BuiltinType::Kind::LitFloat:
- case BuiltinType::Kind::LitInt:
- if (explicitConversion) {
- Second = ICK_Flat_Conversion;
- goto lSuccess;
- }
- break;
- }
- }
- FlattenedTypeIterator::ComparisonResult result =
- FlattenedTypeIterator::CompareTypes(*this, loc, loc, target, source);
- if (!result.CanConvertElements) {
- return false;
- }
- // Only allow scalar to compound or array with explicit cast
- if (result.IsConvertibleAndLeftLonger()) {
- if (!explicitConversion || SourceInfo.ShapeKind != AR_TOBJ_SCALAR) {
- return false;
- }
- }
- // Assignment is valid if elements are exactly the same in type and size; if
- // an explicit conversion is being done, we accept converted elements and a
- // longer right-hand sequence.
- if (!explicitConversion &&
- (!result.AreElementsEqual || result.IsRightLonger()))
- {
- return false;
- }
- Second = ICK_Flat_Conversion;
- goto lSuccess;
- }
- // Base type cast.
- //
- // The rules for aggregate conversions are:
- // 1. A scalar can be replicated to any layout.
- // 2. The result of two vectors is the smaller vector.
- // 3. The result of two matrices is the smaller matrix.
- // 4. The result of a vector and a matrix is:
- // a. If the matrix has one row it's a vector-sized
- // piece of the row.
- // b. If the matrix has one column it's a vector-sized
- // piece of the column.
- // c. Otherwise the number of elements in the vector
- // and matrix must match and the result is the vector.
- // 5. The result of a matrix and a vector is similar to #4.
- //
- bool bCheckElt = false;
- switch (TargetInfo.ShapeKind) {
- case AR_TOBJ_BASIC:
- switch (SourceInfo.ShapeKind)
- {
- case AR_TOBJ_BASIC:
- Second = ICK_Identity;
- break;
- case AR_TOBJ_VECTOR:
- if(1 < SourceInfo.uCols)
- Second = ICK_HLSLVector_Truncation;
- else
- Second = ICK_HLSLVector_Scalar;
- break;
- case AR_TOBJ_MATRIX:
- if(1 < SourceInfo.uRows * SourceInfo.uCols)
- Second = ICK_HLSLVector_Truncation;
- else
- Second = ICK_HLSLVector_Scalar;
- break;
-
- case AR_TOBJ_OBJECT:
- case AR_TOBJ_INTERFACE:
- case AR_TOBJ_POINTER:
- return false;
- }
- bCheckElt = true;
- break;
- case AR_TOBJ_VECTOR:
- switch (SourceInfo.ShapeKind)
- {
- case AR_TOBJ_BASIC:
- // Conversions between scalars and aggregates are always supported.
- Second = ICK_HLSLVector_Splat;
- break;
- case AR_TOBJ_VECTOR:
- if (TargetInfo.uCols > SourceInfo.uCols) {
- if (SourceInfo.uCols == 1) {
- Second = ICK_HLSLVector_Splat;
- } else {
- return false;
- }
- } else if (TargetInfo.uCols < SourceInfo.uCols) {
- Second = ICK_HLSLVector_Truncation;
- } else {
- Second = ICK_Identity;
- }
- break;
- case AR_TOBJ_MATRIX: {
- UINT SourceComponents = SourceInfo.uRows * SourceInfo.uCols;
- if (1 == SourceComponents && TargetInfo.uCols != 1) {
- // splat: matrix<[..], 1, 1> -> vector<[..], O>
- Second = ICK_HLSLVector_Splat;
- } else if (1 == SourceInfo.uRows || 1 == SourceInfo.uCols) {
- // cases for: matrix<[..], M, N> -> vector<[..], O>, where N == 1 or M == 1
- if (TargetInfo.uCols > SourceComponents) // illegal: O > N*M
- return false;
- else if (TargetInfo.uCols < SourceComponents) // truncation: O < N*M
- Second = ICK_HLSLVector_Truncation;
- else // equalivalent: O == N*M
- Second = ICK_HLSLVector_Conversion;
- } else if (TargetInfo.uCols != SourceComponents) {
- // illegal: matrix<[..], M, N> -> vector<[..], O> where N != 1 and M != 1 and O != N*M
- return false;
- } else {
- // legal: matrix<[..], M, N> -> vector<[..], O> where N != 1 and M != 1 and O == N*M
- Second = ICK_HLSLVector_Conversion;
- }
- break;
- }
- case AR_TOBJ_OBJECT:
- case AR_TOBJ_INTERFACE:
- case AR_TOBJ_POINTER:
- return false;
- }
- bCheckElt = true;
- break;
- case AR_TOBJ_MATRIX: {
- UINT TargetComponents = TargetInfo.uRows * TargetInfo.uCols;
- switch (SourceInfo.ShapeKind)
- {
- case AR_TOBJ_BASIC:
- // Conversions between scalars and aggregates are always supported.
- Second = ICK_HLSLVector_Splat;
- break;
- case AR_TOBJ_VECTOR: {
- if (1 == SourceInfo.uCols && TargetComponents != 1) {
- // splat: vector<[..], 1> -> matrix<[..], M, N>
- Second = ICK_HLSLVector_Splat;
- } else if (1 == TargetInfo.uRows || 1 == TargetInfo.uCols) {
- // cases for: vector<[..], O> -> matrix<[..], N, M>, where N == 1 or M == 1
- if (TargetComponents > SourceInfo.uCols) // illegal: N*M > O
- return false;
- else if (TargetComponents < SourceInfo.uCols) // truncation: N*M < O
- Second = ICK_HLSLVector_Truncation;
- else // equalivalent: N*M == O
- Second = ICK_HLSLVector_Conversion;
- } else if (TargetComponents != SourceInfo.uCols) {
- // illegal: vector<[..], O> -> matrix<[..], M, N> where N != 1 and M != 1 and O != N*M
- return false;
- } else {
- // legal: vector<[..], O> -> matrix<[..], M, N> where N != 1 and M != 1 and O == N*M
- Second = ICK_HLSLVector_Conversion;
- }
- break;
- }
- case AR_TOBJ_MATRIX: {
- UINT SourceComponents = SourceInfo.uRows * SourceInfo.uCols;
- if (1 == SourceComponents && TargetComponents != 1) {
- // splat: matrix<[..], 1, 1> -> matrix<[..], M, N>
- Second = ICK_HLSLVector_Splat;
- } else if (TargetInfo.uRows > SourceInfo.uRows || TargetInfo.uCols > SourceInfo.uCols) {
- return false;
- } else if(TargetInfo.uRows < SourceInfo.uRows || TargetInfo.uCols < SourceInfo.uCols) {
- Second = ICK_HLSLVector_Truncation;
- } else {
- Second = ICK_Identity;
- }
- break;
- }
- case AR_TOBJ_OBJECT:
- case AR_TOBJ_INTERFACE:
- case AR_TOBJ_POINTER:
- return false;
- }
- bCheckElt = true;
- break;
- }
- case AR_TOBJ_OBJECT:
- // There are no compatible object assignments that aren't
- // from one type to itself, which is already covered.
- DXASSERT(source != target, "otherwise trivial case was not checked by this function");
- return false;
- default:
- DXASSERT_NOMSG(false);
- return false;
- }
- if (bCheckElt)
- {
- bool precisionLoss = false;
- if (GET_BASIC_BITS(TargetInfo.EltKind) != 0 &&
- GET_BASIC_BITS(TargetInfo.EltKind) <
- GET_BASIC_BITS(SourceInfo.EltKind))
- {
- precisionLoss = true;
- Remarks |= TYPE_CONVERSION_PRECISION_LOSS;
- }
- if (TargetInfo.uTotalElts < SourceInfo.uTotalElts)
- {
- Remarks |= TYPE_CONVERSION_ELT_TRUNCATION;
- }
- if (SourceInfo.EltKind != TargetInfo.EltKind)
- {
- if (TargetInfo.EltKind == AR_BASIC_UNKNOWN ||
- SourceInfo.EltKind == AR_BASIC_UNKNOWN)
- {
- Second = ICK_Flat_Conversion;
- }
- else if (IS_BASIC_BOOL(TargetInfo.EltKind))
- {
- ComponentConversion = ICK_Boolean_Conversion;
- }
- else
- {
- bool targetIsInt = IS_BASIC_AINT(TargetInfo.EltKind);
- if (IS_BASIC_AINT(SourceInfo.EltKind))
- {
- if (targetIsInt)
- {
- ComponentConversion = precisionLoss ? ICK_Integral_Conversion : ICK_Integral_Promotion;
- }
- else
- {
- ComponentConversion = ICK_Floating_Integral;
- }
- }
- else if (IS_BASIC_FLOAT(SourceInfo.EltKind))
- {
- DXASSERT(IS_BASIC_FLOAT(SourceInfo.EltKind), "otherwise should not be checking element types");
- if (targetIsInt)
- {
- ComponentConversion = ICK_Floating_Integral;
- }
- else
- {
- ComponentConversion = precisionLoss ? ICK_Floating_Conversion : ICK_Floating_Promotion;
- }
- } else if (IS_BASIC_BOOL(SourceInfo.EltKind)) {
- if (targetIsInt)
- ComponentConversion = ICK_Integral_Conversion;
- else
- ComponentConversion = ICK_Floating_Integral;
- }
- }
- }
- }
- lSuccess:
- if (standard)
- {
- if (sourceExpr->isLValue())
- {
- if (needsLValueToRValue) {
- standard->First = ICK_Lvalue_To_Rvalue;
- } else {
- switch (Second)
- {
- case ICK_NoReturn_Adjustment:
- case ICK_Vector_Conversion:
- case ICK_Vector_Splat:
- DXASSERT(false, "We shouldn't be producing these implicit conversion kinds");
- case ICK_Flat_Conversion:
- case ICK_HLSLVector_Splat:
- standard->First = ICK_Lvalue_To_Rvalue;
- break;
- }
- switch (ComponentConversion)
- {
- case ICK_Integral_Promotion:
- case ICK_Integral_Conversion:
- case ICK_Floating_Promotion:
- case ICK_Floating_Conversion:
- case ICK_Floating_Integral:
- case ICK_Boolean_Conversion:
- standard->First = ICK_Lvalue_To_Rvalue;
- break;
- }
- }
- }
- // Finally fix up the cases for scalar->scalar component conversion, and
- // identity vector/matrix component conversion
- if (ICK_Identity != ComponentConversion) {
- if (Second == ICK_Identity) {
- if (TargetInfo.ShapeKind == AR_TOBJ_BASIC) {
- // Scalar to scalar type conversion, use normal mechanism (Second)
- Second = ComponentConversion;
- ComponentConversion = ICK_Identity;
- } else {
- // vector or matrix dimensions are not being changed, but component type
- // is being converted, so change Second to signal the conversion
- Second = ICK_HLSLVector_Conversion;
- }
- }
- }
- standard->Second = Second;
- standard->ComponentConversion = ComponentConversion;
- // For conversion which change to RValue but targeting reference type
- // Hold the conversion to codeGen
- if (targetRef && standard->First == ICK_Lvalue_To_Rvalue) {
- standard->First = ICK_Identity;
- standard->Second = ICK_Identity;
- }
- }
- AssignOpt(Remarks, remarks);
- return true;
- }
- Expr* HLSLExternalSource::CastExprToTypeNumeric(Expr* expr, QualType type)
- {
- DXASSERT_NOMSG(expr != nullptr);
- DXASSERT_NOMSG(!type.isNull());
- if (expr->getType() != type) {
- StandardConversionSequence standard;
- if (CanConvert(SourceLocation(), expr, type, /*explicitConversion*/false, nullptr, &standard) &&
- (standard.First != ICK_Identity || !standard.isIdentityConversion())) {
- ExprResult result = m_sema->PerformImplicitConversion(expr, type, standard, Sema::AA_Casting, Sema::CCK_ImplicitConversion);
- if (result.isUsable()) {
- return result.get();
- }
- }
- }
- return expr;
- }
- bool HLSLExternalSource::ValidateTypeRequirements(
- SourceLocation loc,
- ArBasicKind elementKind,
- ArTypeObjectKind objectKind,
- bool requiresIntegrals,
- bool requiresNumerics)
- {
- if (requiresIntegrals || requiresNumerics)
- {
- if (!IsObjectKindPrimitiveAggregate(objectKind))
- {
- m_sema->Diag(loc, diag::err_hlsl_requires_non_aggregate);
- return false;
- }
- }
- if (requiresIntegrals)
- {
- if (!IsBasicKindIntegral(elementKind))
- {
- m_sema->Diag(loc, diag::err_hlsl_requires_int_or_uint);
- return false;
- }
- }
- else if (requiresNumerics)
- {
- if (!IsBasicKindNumeric(elementKind))
- {
- m_sema->Diag(loc, diag::err_hlsl_requires_numeric);
- return false;
- }
- }
- return true;
- }
- bool HLSLExternalSource::ValidatePrimitiveTypeForOperand(SourceLocation loc, QualType type, ArTypeObjectKind kind)
- {
- bool isValid = true;
- if (IsBuiltInObjectType(type)) {
- m_sema->Diag(loc, diag::err_hlsl_unsupported_builtin_op) << type;
- isValid = false;
- }
- if (kind == AR_TOBJ_COMPOUND) {
- m_sema->Diag(loc, diag::err_hlsl_unsupported_struct_op) << type;
- isValid = false;
- }
- return isValid;
- }
- HRESULT HLSLExternalSource::CombineDimensions(QualType leftType, QualType rightType, QualType *resultType)
- {
- UINT leftRows, leftCols;
- UINT rightRows, rightCols;
- GetRowsAndColsForAny(leftType, leftRows, leftCols);
- GetRowsAndColsForAny(rightType, rightRows, rightCols);
- UINT leftTotal = leftRows * leftCols;
- UINT rightTotal = rightRows * rightCols;
- if (rightTotal == 1) {
- *resultType = leftType;
- return S_OK;
- } else if (leftTotal == 1) {
- *resultType = rightType;
- return S_OK;
- } else if (leftRows <= rightRows && leftCols <= rightCols) {
- *resultType = leftType;
- return S_OK;
- } else if (rightRows <= leftRows && rightCols <= leftCols) {
- *resultType = rightType;
- return S_OK;
- } else if ( (1 == leftRows || 1 == leftCols) &&
- (1 == rightRows || 1 == rightCols)) {
- // Handles cases where 1xN or Nx1 matrices are involved possibly mixed with vectors
- if (leftTotal <= rightTotal) {
- *resultType = leftType;
- return S_OK;
- } else {
- *resultType = rightType;
- return S_OK;
- }
- }
- return E_FAIL;
- }
- /// <summary>Validates and adjusts operands for the specified binary operator.</summary>
- /// <param name="OpLoc">Source location for operator.</param>
- /// <param name="Opc">Kind of binary operator.</param>
- /// <param name="LHS">Left-hand-side expression, possibly updated by this function.</param>
- /// <param name="RHS">Right-hand-side expression, possibly updated by this function.</param>
- /// <param name="ResultTy">Result type for operator expression.</param>
- /// <param name="CompLHSTy">Type of LHS after promotions for computation.</param>
- /// <param name="CompResultTy">Type of computation result.</param>
- void HLSLExternalSource::CheckBinOpForHLSL(
- SourceLocation OpLoc,
- BinaryOperatorKind Opc,
- ExprResult& LHS,
- ExprResult& RHS,
- QualType& ResultTy,
- QualType& CompLHSTy,
- QualType& CompResultTy)
- {
- // At the start, none of the output types should be valid.
- DXASSERT_NOMSG(ResultTy.isNull());
- DXASSERT_NOMSG(CompLHSTy.isNull());
- DXASSERT_NOMSG(CompResultTy.isNull());
- LHS = m_sema->CorrectDelayedTyposInExpr(LHS);
- RHS = m_sema->CorrectDelayedTyposInExpr(RHS);
- // If either expression is invalid to begin with, propagate that.
- if (LHS.isInvalid() || RHS.isInvalid()) {
- return;
- }
- // TODO: re-review the Check** in Clang and add equivalent diagnostics if/as needed, possibly after conversions
- // Handle Assign and Comma operators and return
- switch (Opc)
- {
- case BO_AddAssign:
- case BO_AndAssign:
- case BO_DivAssign:
- case BO_MulAssign:
- case BO_RemAssign:
- case BO_ShlAssign:
- case BO_ShrAssign:
- case BO_SubAssign:
- case BO_XorAssign: {
- extern bool CheckForModifiableLvalue(Expr * E, SourceLocation Loc,
- Sema & S);
- if (CheckForModifiableLvalue(LHS.get(), OpLoc, *m_sema)) {
- return;
- }
- } break;
- case BO_Assign: {
- extern bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S);
- if (CheckForModifiableLvalue(LHS.get(), OpLoc, *m_sema)) {
- return;
- }
- bool complained = false;
- ResultTy = LHS.get()->getType();
- if (m_sema->DiagnoseAssignmentResult(Sema::AssignConvertType::Compatible,
- OpLoc, ResultTy, RHS.get()->getType(), RHS.get(),
- Sema::AssignmentAction::AA_Assigning, &complained)) {
- return;
- }
- StandardConversionSequence standard;
- if (!ValidateCast(OpLoc, RHS.get(), ResultTy,
- ExplicitConversionFalse, complained, complained, &standard)) {
- return;
- }
- if (RHS.get()->isLValue()) {
- standard.First = ICK_Lvalue_To_Rvalue;
- }
- RHS = m_sema->PerformImplicitConversion(RHS.get(), ResultTy,
- standard, Sema::AA_Converting, Sema::CCK_ImplicitConversion);
- return;
- }
- break;
- case BO_Comma:
- // C performs conversions, C++ doesn't but still checks for type completeness.
- // There are also diagnostics for improper comma use.
- // In the HLSL case these cases don't apply or simply aren't surfaced.
- ResultTy = RHS.get()->getType();
- return;
- }
- // Leave this diagnostic for last to emulate fxc behavior.
- bool isCompoundAssignment = BinaryOperatorKindIsCompoundAssignment(Opc);
- bool unsupportedBoolLvalue = isCompoundAssignment &&
- !BinaryOperatorKindIsCompoundAssignmentForBool(Opc) &&
- GetTypeElementKind(LHS.get()->getType()) == AR_BASIC_BOOL;
- // Turn operand inputs into r-values.
- QualType LHSTypeAsPossibleLValue = LHS.get()->getType();
- if (!isCompoundAssignment) {
- LHS = m_sema->DefaultLvalueConversion(LHS.get());
- }
- RHS = m_sema->DefaultLvalueConversion(RHS.get());
- if (LHS.isInvalid() || RHS.isInvalid()) {
- return;
- }
- // Promote bool to int now if necessary
- if (BinaryOperatorKindRequiresBoolAsNumeric(Opc) &&
- !isCompoundAssignment) {
- LHS = PromoteToIntIfBool(LHS);
- }
- // Gather type info
- QualType leftType = GetStructuralForm(LHS.get()->getType());
- QualType rightType = GetStructuralForm(RHS.get()->getType());
- ArBasicKind leftElementKind = GetTypeElementKind(leftType);
- ArBasicKind rightElementKind = GetTypeElementKind(rightType);
- ArTypeObjectKind leftObjectKind = GetTypeObjectKind(leftType);
- ArTypeObjectKind rightObjectKind = GetTypeObjectKind(rightType);
- // Validate type requirements
- {
- bool requiresNumerics = BinaryOperatorKindRequiresNumeric(Opc);
- bool requiresIntegrals = BinaryOperatorKindRequiresIntegrals(Opc);
- if (!ValidateTypeRequirements(OpLoc, leftElementKind, leftObjectKind, requiresIntegrals, requiresNumerics)) {
- return;
- }
- if (!ValidateTypeRequirements(OpLoc, rightElementKind, rightObjectKind, requiresIntegrals, requiresNumerics)) {
- return;
- }
- }
- // Promote rhs bool to int if necessary.
- if (BinaryOperatorKindRequiresBoolAsNumeric(Opc)) {
- RHS = PromoteToIntIfBool(RHS);
- }
- if (unsupportedBoolLvalue) {
- m_sema->Diag(OpLoc, diag::err_hlsl_unsupported_bool_lvalue_op);
- return;
- }
- // We don't support binary operators on built-in object types other than assignment or commas.
- {
- DXASSERT(Opc != BO_Assign, "otherwise this wasn't handled as an early exit");
- DXASSERT(Opc != BO_Comma, "otherwise this wasn't handled as an early exit");
- bool isValid;
- isValid = ValidatePrimitiveTypeForOperand(OpLoc, leftType, leftObjectKind);
- if (leftType != rightType && !ValidatePrimitiveTypeForOperand(OpLoc, rightType, rightObjectKind)) {
- isValid = false;
- }
- if (!isValid) {
- return;
- }
- }
- // We don't support equality comparisons on arrays.
- if ((Opc == BO_EQ || Opc == BO_NE) && (leftObjectKind == AR_TOBJ_ARRAY || rightObjectKind == AR_TOBJ_ARRAY)) {
- m_sema->Diag(OpLoc, diag::err_hlsl_unsupported_array_equality_op);
- return;
- }
- // Combine element types for computation.
- ArBasicKind resultElementKind = leftElementKind;
- {
- if (BinaryOperatorKindIsLogical(Opc)) {
- resultElementKind = AR_BASIC_BOOL;
- } else if (!BinaryOperatorKindIsBitwiseShift(Opc) && leftElementKind != rightElementKind) {
- if (!CombineBasicTypes(leftElementKind, rightElementKind, &resultElementKind, nullptr, nullptr)) {
- m_sema->Diag(OpLoc, diag::err_hlsl_type_mismatch);
- return;
- }
- } else if (BinaryOperatorKindIsBitwiseShift(Opc) &&
- (resultElementKind == AR_BASIC_LITERAL_INT ||
- resultElementKind == AR_BASIC_LITERAL_FLOAT) &&
- rightElementKind != AR_BASIC_LITERAL_INT &&
- rightElementKind != AR_BASIC_LITERAL_FLOAT) {
- // For case like 1<<x.
- resultElementKind = AR_BASIC_UINT32;
- }
- // The following combines the selected/combined element kind above with
- // the dimensions that are legal to implicitly cast. This means that
- // element kind may be taken from one side and the dimensions from the
- // other.
- // Legal dimension combinations are identical, splat, and truncation.
- // ResultTy will be set to whichever type can be converted to, if legal,
- // with preference for leftType if both are possible.
- if (FAILED(CombineDimensions(leftType, rightType, &ResultTy))) {
- m_sema->Diag(OpLoc, diag::err_hlsl_type_mismatch);
- return;
- }
- // Here, element kind is combined with dimensions for computation type.
- UINT rowCount, colCount;
- GetRowsAndColsForAny(ResultTy, rowCount, colCount);
- ResultTy = NewSimpleAggregateType(AR_TOBJ_INVALID, resultElementKind, 0, rowCount, colCount)->getCanonicalTypeInternal();
- }
- // Perform necessary conversion sequences for LHS and RHS
- if (RHS.get()->getType() != ResultTy) {
- RHS = CastExprToTypeNumeric(RHS.get(), ResultTy);
- }
- if (isCompoundAssignment) {
- bool complained = false;
- StandardConversionSequence standard;
- if (!ValidateCast(OpLoc, RHS.get(), LHS.get()->getType(), ExplicitConversionFalse,
- complained, complained, &standard)) {
- ResultTy = QualType();
- return;
- }
- CompResultTy = ResultTy;
- CompLHSTy = CompResultTy;
- // For a compound operation, C/C++ promotes both types, performs the arithmetic,
- // then converts to the result type and then assigns.
- //
- // So int + float promotes the int to float, does a floating-point addition,
- // then the result becomes and int and is assigned.
- ResultTy = LHSTypeAsPossibleLValue;
- } else if (LHS.get()->getType() != ResultTy) {
- LHS = CastExprToTypeNumeric(LHS.get(), ResultTy);
- }
- if (BinaryOperatorKindIsComparison(Opc) || BinaryOperatorKindIsLogical(Opc))
- {
- DXASSERT(!isCompoundAssignment, "otherwise binary lookup tables are inconsistent");
- // Return bool vector for vector types.
- if (IsVectorType(m_sema, ResultTy)) {
- UINT rowCount, colCount;
- GetRowsAndColsForAny(ResultTy, rowCount, colCount);
- ResultTy = LookupVectorType(HLSLScalarType::HLSLScalarType_bool, colCount);
- } else if (IsMatrixType(m_sema, ResultTy)) {
- UINT rowCount, colCount;
- GetRowsAndColsForAny(ResultTy, rowCount, colCount);
- ResultTy = LookupMatrixType(HLSLScalarType::HLSLScalarType_bool, rowCount, colCount);
- } else
- ResultTy = m_context->BoolTy.withConst();
- }
- // Run diagnostics. Some are emulating checks that occur in IR emission in fxc.
- if (Opc == BO_Div || Opc == BO_DivAssign || Opc == BO_Rem || Opc == BO_RemAssign) {
- if (IsBasicKindIntMinPrecision(resultElementKind)) {
- m_sema->Diag(OpLoc, diag::err_hlsl_unsupported_div_minint);
- return;
- }
- }
- if (Opc == BO_Rem || Opc == BO_RemAssign) {
- if (resultElementKind == AR_BASIC_FLOAT64) {
- m_sema->Diag(OpLoc, diag::err_hlsl_unsupported_mod_double);
- return;
- }
- }
- }
- /// <summary>Validates and adjusts operands for the specified unary operator.</summary>
- /// <param name="OpLoc">Source location for operator.</param>
- /// <param name="Opc">Kind of operator.</param>
- /// <param name="InputExpr">Input expression to the operator.</param>
- /// <param name="VK">Value kind for resulting expression.</param>
- /// <param name="OK">Object kind for resulting expression.</param>
- /// <returns>The result type for the expression.</returns>
- QualType HLSLExternalSource::CheckUnaryOpForHLSL(
- SourceLocation OpLoc,
- UnaryOperatorKind Opc,
- ExprResult& InputExpr,
- ExprValueKind& VK,
- ExprObjectKind& OK)
- {
- InputExpr = m_sema->CorrectDelayedTyposInExpr(InputExpr);
- // Reject unsupported operators * and &
- switch (Opc) {
- case UO_AddrOf:
- case UO_Deref:
- m_sema->Diag(OpLoc, diag::err_hlsl_unsupported_operator);
- return QualType();
- }
- Expr* expr = InputExpr.get();
- if (expr->isTypeDependent())
- return m_context->DependentTy;
- ArBasicKind elementKind = GetTypeElementKind(expr->getType());
- if (UnaryOperatorKindRequiresModifiableValue(Opc)) {
- extern bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S);
- if (CheckForModifiableLvalue(expr, OpLoc, *m_sema))
- return QualType();
- } else {
- InputExpr = m_sema->DefaultLvalueConversion(InputExpr.get()).get();
- if (InputExpr.isInvalid()) return QualType();
- }
- if (UnaryOperatorKindDisallowsBool(Opc) && IS_BASIC_BOOL(elementKind)) {
- m_sema->Diag(OpLoc, diag::err_hlsl_unsupported_bool_lvalue_op);
- return QualType();
- }
- if (UnaryOperatorKindRequiresBoolAsNumeric(Opc)) {
- InputExpr = PromoteToIntIfBool(InputExpr);
- expr = InputExpr.get();
- elementKind = GetTypeElementKind(expr->getType());
- }
- ArTypeObjectKind objectKind = GetTypeObjectKind(expr->getType());
- bool requiresIntegrals = UnaryOperatorKindRequiresIntegrals(Opc);
- bool requiresNumerics = UnaryOperatorKindRequiresNumerics(Opc);
- if (!ValidateTypeRequirements(OpLoc, elementKind, objectKind, requiresIntegrals, requiresNumerics)) {
- return QualType();
- }
- if (Opc == UnaryOperatorKind::UO_Minus) {
- if (IS_BASIC_UINT(Opc)) {
- m_sema->Diag(OpLoc, diag::warn_hlsl_unary_negate_unsigned);
- }
- }
- // By default, the result type is the operand type.
- // Logical not however should cast to a bool.
- QualType resultType = expr->getType();
- if (Opc == UnaryOperatorKind::UO_LNot) {
- UINT rowCount, colCount;
- GetRowsAndColsForAny(expr->getType(), rowCount, colCount);
- resultType = NewSimpleAggregateType(objectKind, AR_BASIC_BOOL, AR_QUAL_CONST, rowCount, colCount);
- StandardConversionSequence standard;
- if (!CanConvert(OpLoc, expr, resultType, false, nullptr, &standard)) {
- m_sema->Diag(OpLoc, diag::err_hlsl_requires_bool_for_not);
- return QualType();
- }
- // Cast argument.
- ExprResult result = m_sema->PerformImplicitConversion(InputExpr.get(), resultType, standard, Sema::AA_Casting, Sema::CCK_ImplicitConversion);
- if (result.isUsable()) {
- InputExpr = result.get();
- }
- }
- bool isPrefix = Opc == UO_PreInc || Opc == UO_PreDec;
- if (isPrefix) {
- VK = VK_LValue;
- return resultType;
- }
- else {
- VK = VK_RValue;
- return resultType.getUnqualifiedType();
- }
- }
- clang::QualType HLSLExternalSource::CheckVectorConditional(
- _In_ ExprResult &Cond,
- _In_ ExprResult &LHS,
- _In_ ExprResult &RHS,
- _In_ SourceLocation QuestionLoc)
- {
- Cond = m_sema->CorrectDelayedTyposInExpr(Cond);
- LHS = m_sema->CorrectDelayedTyposInExpr(LHS);
- RHS = m_sema->CorrectDelayedTyposInExpr(RHS);
- // If either expression is invalid to begin with, propagate that.
- if (Cond.isInvalid() || LHS.isInvalid() || RHS.isInvalid()) {
- return QualType();
- }
- // Gather type info
- QualType condType = GetStructuralForm(Cond.get()->getType());
- QualType leftType = GetStructuralForm(LHS.get()->getType());
- QualType rightType = GetStructuralForm(RHS.get()->getType());
- ArBasicKind condElementKind = GetTypeElementKind(condType);
- ArBasicKind leftElementKind = GetTypeElementKind(leftType);
- ArBasicKind rightElementKind = GetTypeElementKind(rightType);
- ArTypeObjectKind condObjectKind = GetTypeObjectKind(condType);
- ArTypeObjectKind leftObjectKind = GetTypeObjectKind(leftType);
- ArTypeObjectKind rightObjectKind = GetTypeObjectKind(rightType);
- QualType ResultTy = leftType;
- bool condIsSimple = condObjectKind == AR_TOBJ_BASIC || condObjectKind == AR_TOBJ_VECTOR || condObjectKind == AR_TOBJ_MATRIX;
- if (!condIsSimple) {
- m_sema->Diag(QuestionLoc, diag::err_hlsl_conditional_cond_typecheck);
- return QualType();
- }
- UINT rowCountCond, colCountCond;
- GetRowsAndColsForAny(condType, rowCountCond, colCountCond);
- bool leftIsSimple =
- leftObjectKind == AR_TOBJ_BASIC || leftObjectKind == AR_TOBJ_VECTOR ||
- leftObjectKind == AR_TOBJ_MATRIX;
- bool rightIsSimple =
- rightObjectKind == AR_TOBJ_BASIC || rightObjectKind == AR_TOBJ_VECTOR ||
- rightObjectKind == AR_TOBJ_MATRIX;
-
- UINT rowCount, colCount;
- GetRowsAndColsForAny(ResultTy, rowCount, colCount);
- if (!leftIsSimple || !rightIsSimple) {
- if (leftObjectKind == AR_TOBJ_OBJECT && leftObjectKind == AR_TOBJ_OBJECT) {
- if (leftType == rightType) {
- return leftType;
- }
- }
- // NOTE: Limiting this operator to working only on basic numeric types.
- // This is due to extremely limited (and even broken) support for any other case.
- // In the future we may decide to support more cases.
- m_sema->Diag(QuestionLoc, diag::err_hlsl_conditional_result_typecheck);
- return QualType();
- }
- // Types should be only scalar, vector, or matrix after this point.
- ArBasicKind resultElementKind = leftElementKind;
- // Combine LHS and RHS element types for computation.
- if (leftElementKind != rightElementKind) {
- if (!CombineBasicTypes(leftElementKind, rightElementKind, &resultElementKind, nullptr, nullptr)) {
- m_sema->Diag(QuestionLoc, diag::err_hlsl_conditional_result_comptype_mismatch);
- return QualType();
- }
- }
- // Combine LHS and RHS dimensions
- if (FAILED(CombineDimensions(leftType, rightType, &ResultTy))) {
- m_sema->Diag(QuestionLoc, diag::err_hlsl_conditional_result_dimensions);
- return QualType();
- }
- // If result is scalar, use condition dimensions.
- // Otherwise, condition must either match or is scalar, then use result dimensions
- if (rowCount * colCount == 1) {
- rowCount = rowCountCond;
- colCount = colCountCond;
- }
- else if (rowCountCond * colCountCond != 1 && (rowCountCond != rowCount || colCountCond != colCount)) {
- m_sema->Diag(QuestionLoc, diag::err_hlsl_conditional_dimensions);
- return QualType();
- }
- // Here, element kind is combined with dimensions for result type.
- ResultTy = NewSimpleAggregateType(AR_TOBJ_INVALID, resultElementKind, 0, rowCount, colCount)->getCanonicalTypeInternal();
- // Cast condition to RValue
- if (Cond.get()->isLValue())
- Cond.set(CreateLValueToRValueCast(Cond.get()));
- // Convert condition component type to bool, using result component dimensions
- if (condElementKind != AR_BASIC_BOOL) {
- Cond = CastExprToTypeNumeric(Cond.get(),
- NewSimpleAggregateType(AR_TOBJ_INVALID, AR_BASIC_BOOL, 0, rowCount, colCount)->getCanonicalTypeInternal());
- }
- // Cast LHS/RHS to RValue
- if (LHS.get()->isLValue())
- LHS.set(CreateLValueToRValueCast(LHS.get()));
- if (RHS.get()->isLValue())
- RHS.set(CreateLValueToRValueCast(RHS.get()));
- // TODO: Why isn't vector truncation being reported?
- if (leftType != ResultTy) {
- LHS = CastExprToTypeNumeric(LHS.get(), ResultTy);
- }
- if (rightType != ResultTy) {
- RHS = CastExprToTypeNumeric(RHS.get(), ResultTy);
- }
- return ResultTy;
- }
- // Apply type specifier sign to the given QualType.
- // Other than privmitive int type, only allow shorthand vectors and matrices to be unsigned.
- clang::QualType HLSLExternalSource::ApplyTypeSpecSignToParsedType(
- _In_ clang::QualType &type, _In_ clang::TypeSpecifierSign TSS,
- _In_ clang::SourceLocation Loc) {
- if (TSS == TypeSpecifierSign::TSS_unspecified) {
- return type;
- }
- DXASSERT(TSS != TypeSpecifierSign::TSS_signed, "else signed keyword is supported in HLSL");
- ArTypeObjectKind objKind = GetTypeObjectKind(type);
- if (objKind != AR_TOBJ_VECTOR && objKind != AR_TOBJ_MATRIX &&
- objKind != AR_TOBJ_BASIC && objKind != AR_TOBJ_ARRAY) {
- return type;
- }
- // check if element type is unsigned and check if such vector exists
- // If not create a new one, Make a QualType of the new kind
- ArBasicKind elementKind = GetTypeElementKind(type);
- // Only ints can have signed/unsigend ty
- if (!IS_BASIC_UNSIGNABLE(elementKind)) {
- return type;
- }
- else {
- // Check given TypeSpecifierSign. If unsigned, change int to uint.
- HLSLScalarType scalarType = ScalarTypeForBasic(elementKind);
- HLSLScalarType newScalarType = MakeUnsigned(scalarType);
- // Get new vector types for a given TypeSpecifierSign.
- if (objKind == AR_TOBJ_VECTOR) {
- UINT colCount = GetHLSLVecSize(type);
- TypedefDecl *qts = LookupVectorShorthandType(newScalarType, colCount);
- return m_context->getTypeDeclType(qts);
- } else if (objKind == AR_TOBJ_MATRIX) {
- UINT rowCount, colCount;
- GetRowsAndCols(type, rowCount, colCount);
- TypedefDecl *qts = LookupMatrixShorthandType(newScalarType, rowCount, colCount);
- return m_context->getTypeDeclType(qts);
- } else {
- DXASSERT_NOMSG(objKind == AR_TOBJ_BASIC || objKind == AR_TOBJ_ARRAY);
- return m_scalarTypes[newScalarType];
- }
- }
- }
- Sema::TemplateDeductionResult HLSLExternalSource::DeduceTemplateArgumentsForHLSL(
- FunctionTemplateDecl *FunctionTemplate,
- TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
- FunctionDecl *&Specialization, TemplateDeductionInfo &Info)
- {
- DXASSERT_NOMSG(FunctionTemplate != nullptr);
- DXASSERT(
- ExplicitTemplateArgs == nullptr ||
- ExplicitTemplateArgs->size() == 0, "otherwise parser failed to reject explicit template argument syntax");
- // Get information about the function we have.
- CXXMethodDecl* functionMethod = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl());
- DXASSERT(functionMethod != nullptr,
- "otherwise this is standalone function rather than a method, which isn't supported in the HLSL object model");
- CXXRecordDecl* functionParentRecord = functionMethod->getParent();
- DXASSERT(functionParentRecord != nullptr, "otherwise function is orphaned");
- QualType objectElement = GetFirstElementTypeFromDecl(functionParentRecord);
- // Handle subscript overloads.
- if (FunctionTemplate->getDeclName() == m_context->DeclarationNames.getCXXOperatorName(OO_Subscript))
- {
- DeclContext* functionTemplateContext = FunctionTemplate->getDeclContext();
- FindStructBasicTypeResult findResult = FindStructBasicType(functionTemplateContext);
- if (!findResult.Found())
- {
- // This might be a nested type. Do a lookup on the parent.
- CXXRecordDecl* parentRecordType = dyn_cast_or_null<CXXRecordDecl>(functionTemplateContext);
- if (parentRecordType == nullptr || parentRecordType->getDeclContext() == nullptr)
- {
- return Sema::TemplateDeductionResult::TDK_Invalid;
- }
- findResult = FindStructBasicType(parentRecordType->getDeclContext());
- if (!findResult.Found())
- {
- return Sema::TemplateDeductionResult::TDK_Invalid;
- }
- DXASSERT(
- parentRecordType->getDeclContext()->getDeclKind() == Decl::Kind::CXXRecord ||
- parentRecordType->getDeclContext()->getDeclKind() == Decl::Kind::ClassTemplateSpecialization,
- "otherwise FindStructBasicType should have failed - no other types are allowed");
- objectElement = GetFirstElementTypeFromDecl(
- cast<CXXRecordDecl>(parentRecordType->getDeclContext()));
- }
- Specialization = AddSubscriptSpecialization(FunctionTemplate, objectElement, findResult);
- DXASSERT_NOMSG(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
- FunctionTemplate->getCanonicalDecl());
- return Sema::TemplateDeductionResult::TDK_Success;
- }
- // Reject overload lookups that aren't identifier-based.
- if (!FunctionTemplate->getDeclName().isIdentifier())
- {
- return Sema::TemplateDeductionResult::TDK_NonDeducedMismatch;
- }
- // Find the table of intrinsics based on the object type.
- const HLSL_INTRINSIC* intrinsics;
- size_t intrinsicCount;
- const char* objectName;
- FindIntrinsicTable(FunctionTemplate->getDeclContext(), &objectName, &intrinsics, &intrinsicCount);
- DXASSERT(intrinsics != nullptr,
- "otherwise FindIntrinsicTable failed to lookup a valid object, "
- "or the parser let a user-defined template object through");
- // Look for an intrinsic for which we can match arguments.
- size_t argCount;
- QualType argTypes[g_MaxIntrinsicParamCount + 1];
- StringRef nameIdentifier = FunctionTemplate->getName();
- IntrinsicDefIter cursor = FindIntrinsicByNameAndArgCount(intrinsics, intrinsicCount, objectName, nameIdentifier, Args.size());
- IntrinsicDefIter end = IntrinsicDefIter::CreateEnd(intrinsics, intrinsicCount, IntrinsicTableDefIter::CreateEnd(m_intrinsicTables));
- while (cursor != end)
- {
- if (!MatchArguments(*cursor, objectElement, Args, argTypes, &argCount))
- {
- ++cursor;
- continue;
- }
- Specialization = AddHLSLIntrinsicMethod(cursor.GetTableName(), cursor.GetLoweringStrategy(), *cursor, FunctionTemplate, Args, argTypes, argCount);
- DXASSERT_NOMSG(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
- FunctionTemplate->getCanonicalDecl());
- if (!IsValidateObjectElement(*cursor, objectElement)) {
- m_sema->Diag(Args[0]->getExprLoc(), diag::err_hlsl_invalid_resource_type_on_intrinsic) <<
- nameIdentifier << g_ArBasicTypeNames[GetTypeElementKind(objectElement)];
- }
- return Sema::TemplateDeductionResult::TDK_Success;
- }
- return Sema::TemplateDeductionResult::TDK_NonDeducedMismatch;
- }
- void HLSLExternalSource::DiagnoseAssignmentResultForHLSL(
- Sema::AssignConvertType ConvTy,
- SourceLocation Loc,
- QualType DstType, QualType SrcType,
- _In_ Expr *SrcExpr, Sema::AssignmentAction Action,
- _Out_opt_ bool *Complained)
- {
- if (Complained) *Complained = false;
- // No work to do if there is not type change.
- if (DstType == SrcType) {
- return;
- }
- // Don't generate a warning if the user is casting explicitly
- // or if initializing - our initialization handling already emits this.
- if (Action == Sema::AssignmentAction::AA_Casting ||
- Action == Sema::AssignmentAction::AA_Initializing) {
- return;
- }
- ArBasicKind src = BasicTypeForScalarType(SrcType->getCanonicalTypeUnqualified());
- if (src == AR_BASIC_UNKNOWN || src == AR_BASIC_BOOL) {
- return;
- }
- ArBasicKind dst = BasicTypeForScalarType(DstType->getCanonicalTypeUnqualified());
- if (dst == AR_BASIC_UNKNOWN) {
- return;
- }
- bool warnAboutNarrowing = false;
- switch (dst) {
- case AR_BASIC_FLOAT32:
- case AR_BASIC_INT32:
- case AR_BASIC_UINT32:
- case AR_BASIC_FLOAT16:
- warnAboutNarrowing = src == AR_BASIC_FLOAT64;
- break;
- case AR_BASIC_MIN16FLOAT:
- warnAboutNarrowing = (src == AR_BASIC_INT32 || src == AR_BASIC_UINT32 || src == AR_BASIC_FLOAT32 || src == AR_BASIC_FLOAT64);
- break;
- case AR_BASIC_MIN16INT:
- case AR_BASIC_MIN16UINT:
- case AR_BASIC_MIN12INT:
- case AR_BASIC_MIN10FLOAT:
- warnAboutNarrowing = (src == AR_BASIC_INT32 || src == AR_BASIC_UINT32 || src == AR_BASIC_FLOAT32 || src == AR_BASIC_FLOAT64);
- break;
- }
- // fxc errors looked like this:
- // warning X3205: conversion from larger type to smaller, possible loss of data
- if (warnAboutNarrowing) {
- m_sema->Diag(Loc, diag::warn_hlsl_narrowing) << SrcType << DstType;
- AssignOpt(true, Complained);
- }
- }
- void HLSLExternalSource::ReportUnsupportedTypeNesting(SourceLocation loc, QualType type)
- {
- m_sema->Diag(loc, diag::err_hlsl_unsupported_type_nesting) << type;
- }
- bool HLSLExternalSource::TryStaticCastForHLSL(ExprResult &SrcExpr,
- QualType DestType,
- Sema::CheckedConversionKind CCK,
- const SourceRange &OpRange, unsigned &msg,
- CastKind &Kind, CXXCastPath &BasePath,
- bool ListInitialization, bool SuppressWarnings, bool SuppressErrors,
- _Inout_opt_ StandardConversionSequence* standard)
- {
- DXASSERT(!SrcExpr.isInvalid(), "caller should check for invalid expressions and placeholder types");
- bool explicitConversion
- = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
- QualType sourceType = SrcExpr.get()->getType();
- bool suppressWarnings = explicitConversion || SuppressWarnings;
- SourceLocation loc = OpRange.getBegin();
- if (ValidateCast(loc, SrcExpr.get(), DestType, explicitConversion, suppressWarnings, SuppressErrors, standard)) {
- // TODO: LValue to RValue cast was all that CanConvert (ValidateCast) did anyway,
- // so do this here until we figure out why this is needed.
- if (standard && standard->First == ICK_Lvalue_To_Rvalue) {
- SrcExpr.set(CreateLValueToRValueCast(SrcExpr.get()));
- }
- return true;
- }
- // ValidateCast includes its own error messages.
- msg = 0;
- return false;
- }
- /// <summary>
- /// Checks if a subscript index argument can be initialized from the given expression.
- /// </summary>
- /// <param name="SrcExpr">Source expression used as argument.</param>
- /// <param name="DestType">Parameter type to initialize.</param>
- /// <remarks>
- /// Rules for subscript index initialization follow regular implicit casting rules, with the exception that
- /// no changes in arity are allowed (i.e., int2 can become uint2, but uint or uint3 cannot).
- /// </remarks>
- ImplicitConversionSequence
- HLSLExternalSource::TrySubscriptIndexInitialization(_In_ clang::Expr *SrcExpr,
- clang::QualType DestType) {
- DXASSERT_NOMSG(SrcExpr != nullptr);
- DXASSERT_NOMSG(!DestType.isNull());
- unsigned int msg = 0;
- CastKind kind;
- CXXCastPath path;
- ImplicitConversionSequence sequence;
- sequence.setStandard();
- ExprResult sourceExpr(SrcExpr);
- if (GetElementCount(SrcExpr->getType()) != GetElementCount(DestType)) {
- sequence.setBad(BadConversionSequence::FailureKind::no_conversion,
- SrcExpr->getType(), DestType);
- } else if (!TryStaticCastForHLSL(
- sourceExpr, DestType, Sema::CCK_ImplicitConversion, NoRange,
- msg, kind, path, ListInitializationFalse,
- SuppressWarningsFalse, SuppressErrorsTrue, &sequence.Standard)) {
- sequence.setBad(BadConversionSequence::FailureKind::no_conversion,
- SrcExpr->getType(), DestType);
- }
- return sequence;
- }
- template <typename T>
- static
- bool IsValueInRange(T value, T minValue, T maxValue) {
- return minValue <= value && value <= maxValue;
- }
- #define D3DX_16F_MAX 6.550400e+004 // max value
- #define D3DX_16F_MIN 6.1035156e-5f // min positive value
- static
- void GetFloatLimits(ArBasicKind basicKind, double* minValue, double* maxValue)
- {
- DXASSERT_NOMSG(minValue != nullptr);
- DXASSERT_NOMSG(maxValue != nullptr);
- switch (basicKind) {
- case AR_BASIC_MIN10FLOAT:
- case AR_BASIC_MIN16FLOAT:
- case AR_BASIC_FLOAT16: *minValue = -(D3DX_16F_MIN); *maxValue = D3DX_16F_MAX; return;
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT32: *minValue = -(FLT_MIN); *maxValue = FLT_MAX; return;
- case AR_BASIC_FLOAT64: *minValue = -(DBL_MIN); *maxValue = DBL_MAX; return;
- }
- DXASSERT(false, "unreachable");
- *minValue = 0; *maxValue = 0;
- return;
- }
- static
- void GetUnsignedLimit(ArBasicKind basicKind, uint64_t* maxValue)
- {
- DXASSERT_NOMSG(maxValue != nullptr);
- switch (basicKind) {
- case AR_BASIC_BOOL: *maxValue = 1; return;
- case AR_BASIC_UINT8: *maxValue = UINT8_MAX; return;
- case AR_BASIC_MIN16UINT:
- case AR_BASIC_UINT16: *maxValue = UINT16_MAX; return;
- case AR_BASIC_UINT32: *maxValue = UINT32_MAX; return;
- case AR_BASIC_UINT64: *maxValue = UINT64_MAX; return;
- }
- DXASSERT(false, "unreachable");
- *maxValue = 0;
- return;
- }
- static
- void GetSignedLimits(ArBasicKind basicKind, int64_t* minValue, int64_t* maxValue)
- {
- DXASSERT_NOMSG(minValue != nullptr);
- DXASSERT_NOMSG(maxValue != nullptr);
- switch (basicKind) {
- case AR_BASIC_INT8: *minValue = INT8_MIN; *maxValue = INT8_MAX; return;
- case AR_BASIC_MIN12INT:
- case AR_BASIC_MIN16INT:
- case AR_BASIC_INT16: *minValue = INT16_MIN; *maxValue = INT16_MAX; return;
- case AR_BASIC_INT32: *minValue = INT32_MIN; *maxValue = INT32_MAX; return;
- case AR_BASIC_INT64: *minValue = INT64_MIN; *maxValue = INT64_MAX; return;
- }
- DXASSERT(false, "unreachable");
- *minValue = 0; *maxValue = 0;
- return;
- }
- static
- bool IsValueInBasicRange(ArBasicKind basicKind, const APValue& value)
- {
- if (IS_BASIC_FLOAT(basicKind)) {
- double val;
- if (value.isInt()) {
- val = value.getInt().getLimitedValue();
- } else if (value.isFloat()) {
- llvm::APFloat floatValue = value.getFloat();
- if (!floatValue.isFinite()) {
- return false;
- }
- val = value.getFloat().convertToDouble();
- } else {
- return false;
- }
- double minValue, maxValue;
- GetFloatLimits(basicKind, &minValue, &maxValue);
- return IsValueInRange(val, minValue, maxValue);
- }
- else if (IS_BASIC_SINT(basicKind)) {
- if (!value.isInt()) {
- return false;
- }
- int64_t val = value.getInt().getSExtValue();
- int64_t minValue, maxValue;
- GetSignedLimits(basicKind, &minValue, &maxValue);
- return IsValueInRange(val, minValue, maxValue);
- }
- else if (IS_BASIC_UINT(basicKind) || IS_BASIC_BOOL(basicKind)) {
- if (!value.isInt()) {
- return false;
- }
- uint64_t val = value.getInt().getLimitedValue();
- uint64_t maxValue;
- GetUnsignedLimit(basicKind, &maxValue);
- return IsValueInRange(val, (uint64_t)0, maxValue);
- }
- else {
- return false;
- }
- }
- static
- bool IsPrecisionLossIrrelevant(ASTContext& Ctx, _In_ const Expr* sourceExpr, QualType targetType, ArBasicKind targetKind)
- {
- DXASSERT_NOMSG(!targetType.isNull());
- DXASSERT_NOMSG(sourceExpr != nullptr);
- Expr::EvalResult evalResult;
- if (sourceExpr->EvaluateAsRValue(evalResult, Ctx)) {
- if (evalResult.Diag == nullptr || evalResult.Diag->empty()) {
- return IsValueInBasicRange(targetKind, evalResult.Val);
- }
- }
- return false;
- }
- bool HLSLExternalSource::ValidateCast(
- SourceLocation OpLoc,
- _In_ Expr* sourceExpr,
- QualType target,
- bool explicitConversion,
- bool suppressWarnings,
- bool suppressErrors,
- _Inout_opt_ StandardConversionSequence* standard)
- {
- DXASSERT_NOMSG(sourceExpr != nullptr);
- QualType source = sourceExpr->getType();
- TYPE_CONVERSION_REMARKS remarks;
- if (!CanConvert(OpLoc, sourceExpr, target, explicitConversion, &remarks, standard))
- {
- const bool IsOutputParameter = false;
- //
- // Check whether the lack of explicit-ness matters.
- //
- // Setting explicitForDiagnostics to true in that case will avoid the message
- // saying anything about the implicit nature of the cast, when adding the
- // explicit cast won't make a difference.
- //
- bool explicitForDiagnostics = explicitConversion;
- if (explicitConversion == false)
- {
- if (!CanConvert(OpLoc, sourceExpr, target, true, &remarks, nullptr))
- {
- // Can't convert either way - implicit/explicit doesn't matter.
- explicitForDiagnostics = true;
- }
- }
- if (!suppressErrors)
- {
- m_sema->Diag(OpLoc, diag::err_hlsl_cannot_convert)
- << explicitForDiagnostics << IsOutputParameter << source << target;
- }
- return false;
- }
- if (!suppressWarnings)
- {
- if (!explicitConversion)
- {
- if ((remarks & TYPE_CONVERSION_PRECISION_LOSS) != 0)
- {
- // This is a much more restricted version of the analysis does
- // StandardConversionSequence::getNarrowingKind
- if (!IsPrecisionLossIrrelevant(*m_context, sourceExpr, target, GetTypeElementKind(target)))
- {
- m_sema->Diag(OpLoc, diag::warn_hlsl_narrowing) << source << target;
- }
- }
- if ((remarks & TYPE_CONVERSION_ELT_TRUNCATION) != 0)
- {
- m_sema->Diag(OpLoc, diag::warn_hlsl_implicit_vector_truncation);
- }
- }
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Functions exported from this translation unit. //
- /// <summary>Performs HLSL-specific processing for unary operators.</summary>
- QualType hlsl::CheckUnaryOpForHLSL(Sema& self,
- SourceLocation OpLoc,
- UnaryOperatorKind Opc,
- ExprResult& InputExpr,
- ExprValueKind& VK,
- ExprObjectKind& OK)
- {
- ExternalSemaSource* externalSource = self.getExternalSource();
- if (externalSource == nullptr) {
- return QualType();
- }
- HLSLExternalSource* hlsl = reinterpret_cast<HLSLExternalSource*>(externalSource);
- return hlsl->CheckUnaryOpForHLSL(OpLoc, Opc, InputExpr, VK, OK);
- }
- /// <summary>Performs HLSL-specific processing for binary operators.</summary>
- void hlsl::CheckBinOpForHLSL(Sema& self,
- SourceLocation OpLoc,
- BinaryOperatorKind Opc,
- ExprResult& LHS,
- ExprResult& RHS,
- QualType& ResultTy,
- QualType& CompLHSTy,
- QualType& CompResultTy)
- {
- ExternalSemaSource* externalSource = self.getExternalSource();
- if (externalSource == nullptr) {
- return;
- }
- HLSLExternalSource* hlsl = reinterpret_cast<HLSLExternalSource*>(externalSource);
- return hlsl->CheckBinOpForHLSL(OpLoc, Opc, LHS, RHS, ResultTy, CompLHSTy, CompResultTy);
- }
- /// <summary>Performs HLSL-specific processing of template declarations.</summary>
- bool hlsl::CheckTemplateArgumentListForHLSL(Sema& self, TemplateDecl* Template, SourceLocation TemplateLoc, TemplateArgumentListInfo& TemplateArgList)
- {
- DXASSERT_NOMSG(Template != nullptr);
- ExternalSemaSource* externalSource = self.getExternalSource();
- if (externalSource == nullptr) {
- return false;
- }
- HLSLExternalSource* hlsl = reinterpret_cast<HLSLExternalSource*>(externalSource);
- return hlsl->CheckTemplateArgumentListForHLSL(Template, TemplateLoc, TemplateArgList);
- }
- /// <summary>Deduces template arguments on a function call in an HLSL program.</summary>
- Sema::TemplateDeductionResult hlsl::DeduceTemplateArgumentsForHLSL(Sema* self,
- FunctionTemplateDecl *FunctionTemplate,
- TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
- FunctionDecl *&Specialization, TemplateDeductionInfo &Info)
- {
- return HLSLExternalSource::FromSema(self)
- ->DeduceTemplateArgumentsForHLSL(FunctionTemplate, ExplicitTemplateArgs, Args, Specialization, Info);
- }
- void hlsl::DiagnoseAssignmentResultForHLSL(Sema* self,
- Sema::AssignConvertType ConvTy,
- SourceLocation Loc,
- QualType DstType, QualType SrcType,
- Expr *SrcExpr, Sema::AssignmentAction Action,
- bool *Complained)
- {
- return HLSLExternalSource::FromSema(self)
- ->DiagnoseAssignmentResultForHLSL(ConvTy, Loc, DstType, SrcType, SrcExpr, Action, Complained);
- }
- void hlsl::DiagnoseControlFlowConditionForHLSL(Sema *self, Expr *condExpr, StringRef StmtName) {
- while (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(condExpr)) {
- if (IC->getCastKind() == CastKind::CK_HLSLMatrixTruncationCast ||
- IC->getCastKind() == CastKind::CK_HLSLVectorTruncationCast) {
- self->Diag(condExpr->getLocStart(),
- diag::err_hlsl_control_flow_cond_not_scalar)
- << StmtName;
- return;
- }
- condExpr = IC->getSubExpr();
- }
- }
- static bool ShaderModelsMatch(const StringRef& left, const StringRef& right)
- {
- // TODO: handle shorthand cases.
- return left.size() == 0 || right.size() == 0 || left.equals(right);
- }
- void hlsl::DiagnosePackingOffset(
- clang::Sema* self,
- SourceLocation loc,
- clang::QualType type,
- int componentOffset)
- {
- DXASSERT_NOMSG(0 <= componentOffset && componentOffset <= 3);
- if (componentOffset > 0) {
- HLSLExternalSource* source = HLSLExternalSource::FromSema(self);
- ArBasicKind element = source->GetTypeElementKind(type);
- ArTypeObjectKind shape = source->GetTypeObjectKind(type);
- // Only perform some simple validation for now.
- if (IsObjectKindPrimitiveAggregate(shape) && IsBasicKindNumeric(element)) {
- int count = GetElementCount(type);
- if (count > (4 - componentOffset)) {
- self->Diag(loc, diag::err_hlsl_register_or_offset_bind_not_valid);
- }
- }
- }
- }
- void hlsl::DiagnoseRegisterType(
- clang::Sema* self,
- clang::SourceLocation loc,
- clang::QualType type,
- char registerType)
- {
- HLSLExternalSource* source = HLSLExternalSource::FromSema(self);
- ArBasicKind element = source->GetTypeElementKind(type);
- StringRef expected("none");
- bool isValid = true;
- bool isWarning = false;
- switch (element)
- {
- case AR_BASIC_BOOL:
- case AR_BASIC_LITERAL_FLOAT:
- case AR_BASIC_FLOAT16:
- case AR_BASIC_FLOAT32_PARTIAL_PRECISION:
- case AR_BASIC_FLOAT32:
- case AR_BASIC_FLOAT64:
- case AR_BASIC_LITERAL_INT:
- case AR_BASIC_INT8:
- case AR_BASIC_UINT8:
- case AR_BASIC_INT16:
- case AR_BASIC_UINT16:
- case AR_BASIC_INT32:
- case AR_BASIC_UINT32:
- case AR_BASIC_INT64:
- case AR_BASIC_UINT64:
- case AR_BASIC_MIN10FLOAT:
- case AR_BASIC_MIN16FLOAT:
- case AR_BASIC_MIN12INT:
- case AR_BASIC_MIN16INT:
- case AR_BASIC_MIN16UINT:
- expected = "'b', 'c', or 'i'";
- isValid = registerType == 'b' || registerType == 'c' || registerType == 'i' ||
- registerType == 'B' || registerType == 'C' || registerType == 'I';
- break;
- case AR_OBJECT_TEXTURE1D:
- case AR_OBJECT_TEXTURE1D_ARRAY:
- case AR_OBJECT_TEXTURE2D:
- case AR_OBJECT_TEXTURE2D_ARRAY:
- case AR_OBJECT_TEXTURE3D:
- case AR_OBJECT_TEXTURECUBE:
- case AR_OBJECT_TEXTURECUBE_ARRAY:
- case AR_OBJECT_TEXTURE2DMS:
- case AR_OBJECT_TEXTURE2DMS_ARRAY:
- expected = "'t' or 's'";
- isValid = registerType == 't' || registerType == 's' ||
- registerType == 'T' || registerType == 'S';
- break;
- case AR_OBJECT_SAMPLER:
- case AR_OBJECT_SAMPLER1D:
- case AR_OBJECT_SAMPLER2D:
- case AR_OBJECT_SAMPLER3D:
- case AR_OBJECT_SAMPLERCUBE:
- case AR_OBJECT_SAMPLERCOMPARISON:
- expected = "'s' or 't'";
- isValid = registerType == 's' || registerType == 't' ||
- registerType == 'S' || registerType == 'T';
- break;
- case AR_OBJECT_BUFFER:
- expected = "'t'";
- isValid = registerType == 't' || registerType == 'T';
- break;
- case AR_OBJECT_POINTSTREAM:
- case AR_OBJECT_LINESTREAM:
- case AR_OBJECT_TRIANGLESTREAM:
- isValid = false;
- isWarning = true;
- break;
- case AR_OBJECT_INPUTPATCH:
- case AR_OBJECT_OUTPUTPATCH:
- isValid = false;
- isWarning = true;
- break;
- case AR_OBJECT_RWTEXTURE1D:
- case AR_OBJECT_RWTEXTURE1D_ARRAY:
- case AR_OBJECT_RWTEXTURE2D:
- case AR_OBJECT_RWTEXTURE2D_ARRAY:
- case AR_OBJECT_RWTEXTURE3D:
- case AR_OBJECT_RWBUFFER:
- expected = "'u'";
- isValid = registerType == 'u' || registerType == 'U';
- break;
- case AR_OBJECT_BYTEADDRESS_BUFFER:
- case AR_OBJECT_STRUCTURED_BUFFER:
- expected = "'t'";
- isValid = registerType == 't' || registerType == 'T';
- break;
- case AR_OBJECT_CONSUME_STRUCTURED_BUFFER:
- case AR_OBJECT_RWBYTEADDRESS_BUFFER:
- case AR_OBJECT_RWSTRUCTURED_BUFFER:
- case AR_OBJECT_RWSTRUCTURED_BUFFER_ALLOC:
- case AR_OBJECT_RWSTRUCTURED_BUFFER_CONSUME:
- case AR_OBJECT_APPEND_STRUCTURED_BUFFER:
- expected = "'u'";
- isValid = registerType == 'u' || registerType == 'U';
- break;
- case AR_OBJECT_CONSTANT_BUFFER:
- expected = "'b'";
- isValid = registerType == 'b' || registerType == 'B';
- break;
- case AR_OBJECT_TEXTURE_BUFFER:
- expected = "'t'";
- isValid = registerType == 't' || registerType == 'T';
- break;
- case AR_OBJECT_ROVBUFFER:
- case AR_OBJECT_ROVBYTEADDRESS_BUFFER:
- case AR_OBJECT_ROVSTRUCTURED_BUFFER:
- case AR_OBJECT_ROVTEXTURE1D:
- case AR_OBJECT_ROVTEXTURE1D_ARRAY:
- case AR_OBJECT_ROVTEXTURE2D:
- case AR_OBJECT_ROVTEXTURE2D_ARRAY:
- case AR_OBJECT_ROVTEXTURE3D:
- expected = "'u'";
- isValid = registerType == 'u' || registerType == 'U';
- break;
- case AR_OBJECT_LEGACY_EFFECT: // Used for all unsupported but ignored legacy effect types
- isWarning = true;
- break; // So we don't care what you tried to bind it to
- };
- // fxc is inconsistent as to when it reports an error and when it ignores invalid bind semantics, so emit
- // a warning instead.
- if (!isValid)
- {
- if (isWarning)
- self->Diag(loc, diag::warn_hlsl_incorrect_bind_semantic) << expected;
- else
- self->Diag(loc, diag::err_hlsl_incorrect_bind_semantic) << expected;
- }
- }
- void hlsl::DiagnoseUnusualAnnotationsForHLSL(
- Sema& S,
- std::vector<hlsl::UnusualAnnotation *>& annotations)
- {
- bool packoffsetOverriddenReported = false;
- auto && iter = annotations.begin();
- auto && end = annotations.end();
- for (; iter != end; ++iter) {
- switch ((*iter)->getKind()) {
- case hlsl::UnusualAnnotation::UA_ConstantPacking: {
- hlsl::ConstantPacking* constantPacking = cast<hlsl::ConstantPacking>(*iter);
- // Check whether this will conflict with other packoffsets. If so, only issue a warning; last one wins.
- if (!packoffsetOverriddenReported) {
- auto newIter = iter;
- ++newIter;
- while (newIter != end) {
- hlsl::ConstantPacking* other = dyn_cast_or_null<hlsl::ConstantPacking>(*newIter);
- if (other != nullptr &&
- (other->Subcomponent != constantPacking->Subcomponent || other->ComponentOffset != constantPacking->ComponentOffset)) {
- S.Diag(constantPacking->Loc, diag::warn_hlsl_packoffset_overridden);
- packoffsetOverriddenReported = true;
- break;
- }
- ++newIter;
- }
- }
- break;
- }
- case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
- hlsl::RegisterAssignment* registerAssignment = cast<hlsl::RegisterAssignment>(*iter);
- // Check whether this will conflict with other register assignments of the same type.
- auto newIter = iter;
- ++newIter;
- while (newIter != end) {
- hlsl::RegisterAssignment* other = dyn_cast_or_null<hlsl::RegisterAssignment>(*newIter);
- // Same register bank and profile, but different number.
- if (other != nullptr &&
- ShaderModelsMatch(other->ShaderProfile, registerAssignment->ShaderProfile) &&
- other->RegisterType == registerAssignment->RegisterType &&
- (other->RegisterNumber != registerAssignment->RegisterNumber ||
- other->RegisterOffset != registerAssignment->RegisterOffset)) {
- // Obvious conflict - report it up front.
- S.Diag(registerAssignment->Loc, diag::err_hlsl_register_semantics_conflicting);
- }
- ++newIter;
- }
- break;
- }
- case hlsl::UnusualAnnotation::UA_SemanticDecl: {
- // hlsl::SemanticDecl* semanticDecl = cast<hlsl::SemanticDecl>(*iter);
- // No common validation to be performed.
- break;
- }
- }
- }
- }
- clang::OverloadingResult
- hlsl::GetBestViableFunction(clang::Sema &S, clang::SourceLocation Loc,
- clang::OverloadCandidateSet &set,
- clang::OverloadCandidateSet::iterator &Best) {
- return HLSLExternalSource::FromSema(&S)
- ->GetBestViableFunction(Loc, set, Best);
- }
- void hlsl::InitializeInitSequenceForHLSL(Sema *self,
- const InitializedEntity &Entity,
- const InitializationKind &Kind,
- MultiExprArg Args,
- bool TopLevelOfInitList,
- InitializationSequence *initSequence) {
- return HLSLExternalSource::FromSema(self)
- ->InitializeInitSequenceForHLSL(Entity, Kind, Args, TopLevelOfInitList, initSequence);
- }
- bool hlsl::IsConversionToLessOrEqualElements(
- _In_ clang::Sema* self,
- const clang::ExprResult& sourceExpr,
- const clang::QualType& targetType,
- bool explicitConversion)
- {
- return HLSLExternalSource::FromSema(self)
- ->IsConversionToLessOrEqualElements(sourceExpr, targetType, explicitConversion);
- }
- bool hlsl::LookupMatrixMemberExprForHLSL(
- Sema* self,
- Expr& BaseExpr,
- DeclarationName MemberName,
- bool IsArrow,
- SourceLocation OpLoc,
- SourceLocation MemberLoc,
- ExprResult* result)
- {
- return HLSLExternalSource::FromSema(self)
- ->LookupMatrixMemberExprForHLSL(BaseExpr, MemberName, IsArrow, OpLoc, MemberLoc, result);
- }
- bool hlsl::LookupVectorMemberExprForHLSL(
- Sema* self,
- Expr& BaseExpr,
- DeclarationName MemberName,
- bool IsArrow,
- SourceLocation OpLoc,
- SourceLocation MemberLoc,
- ExprResult* result)
- {
- return HLSLExternalSource::FromSema(self)
- ->LookupVectorMemberExprForHLSL(BaseExpr, MemberName, IsArrow, OpLoc, MemberLoc, result);
- }
- clang::ExprResult hlsl::MaybeConvertScalarToVector(
- _In_ clang::Sema* self,
- _In_ clang::Expr* E)
- {
- return HLSLExternalSource::FromSema(self)->MaybeConvertScalarToVector(E);
- }
- bool hlsl::TryStaticCastForHLSL(_In_ Sema* self, ExprResult &SrcExpr,
- QualType DestType,
- Sema::CheckedConversionKind CCK,
- const SourceRange &OpRange, unsigned &msg,
- CastKind &Kind, CXXCastPath &BasePath,
- bool ListInitialization,
- bool SuppressDiagnostics,
- _Inout_opt_ StandardConversionSequence* standard)
- {
- return HLSLExternalSource::FromSema(self)->TryStaticCastForHLSL(
- SrcExpr, DestType, CCK, OpRange, msg, Kind, BasePath, ListInitialization,
- SuppressDiagnostics, SuppressDiagnostics, standard);
- }
- clang::ExprResult hlsl::PerformHLSLConversion(
- _In_ clang::Sema* self,
- _In_ clang::Expr* From,
- _In_ clang::QualType targetType,
- _In_ const clang::StandardConversionSequence &SCS,
- _In_ clang::Sema::CheckedConversionKind CCK)
- {
- return HLSLExternalSource::FromSema(self)->PerformHLSLConversion(From, targetType, SCS, CCK);
- }
- clang::ImplicitConversionSequence hlsl::TrySubscriptIndexInitialization(
- _In_ clang::Sema* self,
- _In_ clang::Expr* SrcExpr,
- clang::QualType DestType)
- {
- return HLSLExternalSource::FromSema(self)
- ->TrySubscriptIndexInitialization(SrcExpr, DestType);
- }
- /// <summary>Performs HLSL-specific initialization on the specified context.</summary>
- void hlsl::InitializeASTContextForHLSL(ASTContext& context)
- {
- HLSLExternalSource* hlslSource = new HLSLExternalSource();
- IntrusiveRefCntPtr<ExternalASTSource> externalSource(hlslSource);
- if (hlslSource->Initialize(context)) {
- context.setExternalSource(externalSource);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- // FlattenedTypeIterator implementation //
- /// <summary>Constructs a FlattenedTypeIterator for the specified type.</summary>
- FlattenedTypeIterator::FlattenedTypeIterator(SourceLocation loc, QualType type, HLSLExternalSource& source) :
- m_source(source), m_typeDepth(0), m_loc(loc), m_draining(false), m_springLoaded(false), m_incompleteCount(0)
- {
- if (pushTrackerForType(type, nullptr)) {
- considerLeaf();
- }
- }
- /// <summary>Constructs a FlattenedTypeIterator for the specified expressions.</summary>
- FlattenedTypeIterator::FlattenedTypeIterator(SourceLocation loc, MultiExprArg args, HLSLExternalSource& source) :
- m_source(source), m_typeDepth(0), m_loc(loc), m_draining(false), m_springLoaded(false), m_incompleteCount(0)
- {
- if (!args.empty()) {
- MultiExprArg::iterator ii = args.begin();
- MultiExprArg::iterator ie = args.end();
- DXASSERT(ii != ie, "otherwise empty() returned an incorrect value");
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(ii, ie));
- if (!considerLeaf()) {
- m_typeTrackers.clear();
- }
- }
- }
- /// <summary>Gets the current element in the flattened type hierarchy.</summary>
- QualType FlattenedTypeIterator::getCurrentElement() const
- {
- return m_typeTrackers.back().Type;
- }
- /// <summary>Get the number of repeated current elements.</summary>
- unsigned int FlattenedTypeIterator::getCurrentElementSize() const
- {
- const FlattenedTypeTracker& back = m_typeTrackers.back();
- return (back.IterKind == FK_IncompleteArray) ? 1 : back.Count;
- }
- /// <summary>Checks whether the iterator has a current element type to report.</summary>
- bool FlattenedTypeIterator::hasCurrentElement() const
- {
- return m_typeTrackers.size() > 0;
- }
- /// <summary>Consumes count elements on this iterator.</summary>
- void FlattenedTypeIterator::advanceCurrentElement(unsigned int count)
- {
- DXASSERT(!m_typeTrackers.empty(), "otherwise caller should not be trying to advance to another element");
- DXASSERT(m_typeTrackers.back().IterKind == FK_IncompleteArray || count <= m_typeTrackers.back().Count, "caller should never exceed currently pending element count");
- FlattenedTypeTracker& tracker = m_typeTrackers.back();
- if (tracker.IterKind == FK_IncompleteArray)
- {
- tracker.Count += count;
- m_springLoaded = true;
- }
- else
- {
- tracker.Count -= count;
- m_springLoaded = false;
- if (m_typeTrackers.back().Count == 0)
- {
- advanceLeafTracker();
- }
- }
- }
- unsigned int FlattenedTypeIterator::countRemaining()
- {
- m_draining = true; // when draining the iterator, incomplete arrays stop functioning as an infinite array
- size_t result = 0;
- while (hasCurrentElement() && !m_springLoaded)
- {
- size_t pending = getCurrentElementSize();
- result += pending;
- advanceCurrentElement(pending);
- }
- return result;
- }
- void FlattenedTypeIterator::advanceLeafTracker()
- {
- DXASSERT(!m_typeTrackers.empty(), "otherwise caller should not be trying to advance to another element");
- for (;;)
- {
- consumeLeaf();
- if (m_typeTrackers.empty()) {
- return;
- }
- if (considerLeaf()) {
- return;
- }
- }
- }
- bool FlattenedTypeIterator::considerLeaf()
- {
- if (m_typeTrackers.empty()) {
- return false;
- }
- m_typeDepth++;
- if (m_typeDepth > MaxTypeDepth) {
- m_source.ReportUnsupportedTypeNesting(m_loc, m_firstType);
- m_typeTrackers.clear();
- m_typeDepth--;
- return false;
- }
- bool result = false;
- FlattenedTypeTracker& tracker = m_typeTrackers.back();
- switch (tracker.IterKind) {
- case FlattenedIterKind::FK_Expressions:
- if (pushTrackerForExpression(tracker.CurrentExpr)) {
- result = considerLeaf();
- }
- break;
- case FlattenedIterKind::FK_Fields:
- if (pushTrackerForType(tracker.CurrentField->getType(), nullptr)) {
- result = considerLeaf();
- }
- break;
- case FlattenedIterKind::FK_IncompleteArray:
- m_springLoaded = true; // fall through.
- default:
- case FlattenedIterKind::FK_Simple: {
- ArTypeObjectKind objectKind = m_source.GetTypeObjectKind(tracker.Type);
- if (objectKind != ArTypeObjectKind::AR_TOBJ_BASIC &&
- objectKind != ArTypeObjectKind::AR_TOBJ_OBJECT) {
- if (pushTrackerForType(tracker.Type, tracker.CurrentExpr)) {
- result = considerLeaf();
- }
- } else {
- result = true;
- }
- }
- }
- m_typeDepth--;
- return result;
- }
- void FlattenedTypeIterator::consumeLeaf()
- {
- bool topConsumed = true; // Tracks whether we're processing the topmost item which we should consume.
- for (;;) {
- if (m_typeTrackers.empty()) {
- return;
- }
- FlattenedTypeTracker& tracker = m_typeTrackers.back();
- switch (tracker.IterKind) {
- case FlattenedIterKind::FK_Expressions:
- ++tracker.CurrentExpr;
- if (tracker.CurrentExpr == tracker.EndExpr) {
- m_typeTrackers.pop_back();
- topConsumed = false;
- } else {
- return;
- }
- break;
- case FlattenedIterKind::FK_Fields:
- ++tracker.CurrentField;
- if (tracker.CurrentField == tracker.EndField) {
- m_typeTrackers.pop_back();
- topConsumed = false;
- } else {
- return;
- }
- break;
- case FlattenedIterKind::FK_IncompleteArray:
- if (m_draining) {
- DXASSERT(m_typeTrackers.size() == 1, "m_typeTrackers.size() == 1, otherwise incomplete array isn't topmost");
- m_incompleteCount = tracker.Count;
- m_typeTrackers.pop_back();
- }
- return;
- default:
- case FlattenedIterKind::FK_Simple: {
- m_springLoaded = false;
- if (!topConsumed) {
- DXASSERT(tracker.Count > 0, "tracker.Count > 0 - otherwise we shouldn't be on stack");
- --tracker.Count;
- }
- else {
- topConsumed = false;
- }
- if (tracker.Count == 0) {
- m_typeTrackers.pop_back();
- } else {
- return;
- }
- }
- }
- }
- }
- bool FlattenedTypeIterator::pushTrackerForExpression(MultiExprArg::iterator expression)
- {
- Expr* e = *expression;
- Stmt::StmtClass expressionClass = e->getStmtClass();
- if (expressionClass == Stmt::StmtClass::InitListExprClass) {
- InitListExpr* initExpr = dyn_cast<InitListExpr>(e);
- if (initExpr->getNumInits() == 0) {
- return false;
- }
- MultiExprArg inits(initExpr->getInits(), initExpr->getNumInits());
- MultiExprArg::iterator ii = inits.begin();
- MultiExprArg::iterator ie = inits.end();
- DXASSERT(ii != ie, "otherwise getNumInits() returned an incorrect value");
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(ii, ie));
- return true;
- }
- return pushTrackerForType(e->getType(), expression);
- }
- // TODO: improve this to provide a 'peek' at intermediate types,
- // which should help compare struct foo[1000] to avoid 1000 steps + per-field steps
- bool FlattenedTypeIterator::pushTrackerForType(QualType type, MultiExprArg::iterator expression)
- {
- if (type->isVoidType()) {
- return false;
- }
- if (m_firstType.isNull()) {
- m_firstType = type;
- }
- ArTypeObjectKind objectKind = m_source.GetTypeObjectKind(type);
- QualType elementType;
- unsigned int elementCount;
- const RecordType* recordType;
- RecordDecl::field_iterator fi, fe;
- switch (objectKind)
- {
- case ArTypeObjectKind::AR_TOBJ_ARRAY:
- // TODO: handle multi-dimensional arrays
- elementType = type->getAsArrayTypeUnsafe()->getElementType(); // handle arrays of arrays
- elementCount = GetArraySize(type);
- if (elementCount == 0) {
- if (type->isIncompleteArrayType()) {
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(elementType));
- return true;
- }
- return false;
- }
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(
- elementType, elementCount, nullptr));
- return true;
- case ArTypeObjectKind::AR_TOBJ_BASIC:
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(type, 1, expression));
- return true;
- case ArTypeObjectKind::AR_TOBJ_COMPOUND:
- recordType = type->getAsStructureType();
- if (recordType == nullptr)
- recordType = dyn_cast<RecordType>(type.getTypePtr());
- fi = recordType->getDecl()->field_begin();
- fe = recordType->getDecl()->field_end();
- // Skip empty struct.
- if (fi == fe)
- return false;
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(type, fi, fe));
- type = (*fi)->getType();
- return true;
- case ArTypeObjectKind::AR_TOBJ_MATRIX:
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(
- m_source.GetMatrixOrVectorElementType(type),
- GetElementCount(type), nullptr));
- return true;
- case ArTypeObjectKind::AR_TOBJ_VECTOR:
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(
- m_source.GetMatrixOrVectorElementType(type),
- GetHLSLVecSize(type), nullptr));
- return true;
- case ArTypeObjectKind::AR_TOBJ_OBJECT:
- // Object have no sub-types.
- m_typeTrackers.push_back(FlattenedTypeIterator::FlattenedTypeTracker(type.getCanonicalType(), 1, expression));
- return true;
- default:
- DXASSERT(false, "unreachable");
- return false;
- }
- }
- FlattenedTypeIterator::ComparisonResult
- FlattenedTypeIterator::CompareIterators(
- HLSLExternalSource& source,
- SourceLocation loc,
- FlattenedTypeIterator& leftIter,
- FlattenedTypeIterator& rightIter)
- {
- FlattenedTypeIterator::ComparisonResult result;
- result.LeftCount = 0;
- result.RightCount = 0;
- result.AreElementsEqual = true; // Until proven otherwise.
- result.CanConvertElements = true; // Until proven otherwise.
- while (leftIter.hasCurrentElement() && rightIter.hasCurrentElement())
- {
- Expr* actualExpr = rightIter.getExprOrNull();
- bool hasExpr = actualExpr != nullptr;
- StmtExpr scratchExpr(nullptr, rightIter.getCurrentElement(), NoLoc, NoLoc);
- StandardConversionSequence standard;
- ExprResult convertedExpr;
- if (!source.CanConvert(loc,
- hasExpr ? actualExpr : &scratchExpr,
- leftIter.getCurrentElement(),
- ExplicitConversionFalse,
- nullptr,
- &standard)) {
- result.AreElementsEqual = false;
- result.CanConvertElements = false;
- break;
- }
- else if (hasExpr && (standard.First != ICK_Identity || !standard.isIdentityConversion()))
- {
- convertedExpr = source.getSema()->PerformImplicitConversion(actualExpr,
- leftIter.getCurrentElement(),
- standard,
- Sema::AA_Casting,
- Sema::CCK_ImplicitConversion);
- }
- if (rightIter.getCurrentElement()->getCanonicalTypeUnqualified() !=
- leftIter.getCurrentElement()->getCanonicalTypeUnqualified())
- {
- result.AreElementsEqual = false;
- }
- unsigned int advance = std::min(leftIter.getCurrentElementSize(), rightIter.getCurrentElementSize());
- DXASSERT(advance > 0, "otherwise one iterator should report empty");
- // If we need to apply conversions to the expressions, then advance a single element.
- if (hasExpr && convertedExpr.isUsable()) {
- rightIter.replaceExpr(convertedExpr.get());
- advance = 1;
- }
- leftIter.advanceCurrentElement(advance);
- rightIter.advanceCurrentElement(advance);
- result.LeftCount += advance;
- result.RightCount += advance;
- }
- result.LeftCount += leftIter.countRemaining();
- result.RightCount += rightIter.countRemaining();
- return result;
- }
- FlattenedTypeIterator::ComparisonResult
- FlattenedTypeIterator::CompareTypes(
- HLSLExternalSource& source,
- SourceLocation leftLoc, SourceLocation rightLoc,
- QualType left, QualType right)
- {
- FlattenedTypeIterator leftIter(leftLoc, left, source);
- FlattenedTypeIterator rightIter(rightLoc, right, source);
- return CompareIterators(source, leftLoc, leftIter, rightIter);
- }
- FlattenedTypeIterator::ComparisonResult
- FlattenedTypeIterator::CompareTypesForInit(
- HLSLExternalSource& source, QualType left, MultiExprArg args,
- SourceLocation leftLoc, SourceLocation rightLoc)
- {
- FlattenedTypeIterator leftIter(leftLoc, left, source);
- FlattenedTypeIterator rightIter(rightLoc, args, source);
- return CompareIterators(source, leftLoc, leftIter, rightIter);
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Attribute processing support. //
- static int ValidateAttributeIntArg(Sema& S, const AttributeList &Attr, unsigned index = 0)
- {
- int64_t value = 0;
- if (Attr.getNumArgs() > index)
- {
- Expr *E = nullptr;
- if (!Attr.isArgExpr(index)) {
- // For case arg is constant variable.
- IdentifierLoc *loc = Attr.getArgAsIdent(index);
- VarDecl *decl = dyn_cast<VarDecl>(S.LookupSingleName(S.TUScope, loc->Ident, loc->Loc, Sema::LookupNameKind::LookupOrdinaryName));
- if (!decl) {
- S.Diag(Attr.getLoc(), diag::warn_hlsl_attribute_expects_uint_literal) << Attr.getName();
- return value;
- }
- Expr *init = decl->getInit();
- if (!init) {
- S.Diag(Attr.getLoc(), diag::warn_hlsl_attribute_expects_uint_literal) << Attr.getName();
- return value;
- }
- E = init;
- } else
- E = Attr.getArgAsExpr(index);
- clang::APValue ArgNum;
- bool displayError = false;
- if (E->isTypeDependent() || E->isValueDependent() || !E->isCXX11ConstantExpr(S.Context, &ArgNum))
- {
- displayError = true;
- }
- else
- {
- if (ArgNum.isInt())
- {
- value = ArgNum.getInt().getSExtValue();
- }
- else if (ArgNum.isFloat())
- {
- llvm::APSInt floatInt;
- bool isPrecise;
- if (ArgNum.getFloat().convertToInteger(floatInt, llvm::APFloat::rmTowardZero, &isPrecise) == llvm::APFloat::opStatus::opOK)
- {
- value = floatInt.getSExtValue();
- }
- else
- {
- S.Diag(Attr.getLoc(), diag::warn_hlsl_attribute_expects_uint_literal) << Attr.getName();
- }
- }
- else
- {
- displayError = true;
- }
- if (value < 0)
- {
- S.Diag(Attr.getLoc(), diag::warn_hlsl_attribute_expects_uint_literal) << Attr.getName();
- }
- }
- if (displayError)
- {
- S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
- << Attr.getName() << AANT_ArgumentIntegerConstant
- << E->getSourceRange();
- }
- }
- return (int)value;
- }
- // TODO: support float arg directly.
- static int ValidateAttributeFloatArg(Sema &S, const AttributeList &Attr,
- unsigned index = 0) {
- int value = 0;
- if (Attr.getNumArgs() > index) {
- Expr *E = Attr.getArgAsExpr(index);
- if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) {
- llvm::APFloat flV = FL->getValue();
- if (flV.getSizeInBits(flV.getSemantics()) == 64) {
- llvm::APInt intV = llvm::APInt::floatToBits(flV.convertToDouble());
- value = intV.getLimitedValue();
- } else {
- llvm::APInt intV = llvm::APInt::floatToBits(flV.convertToFloat());
- value = intV.getLimitedValue();
- }
- } else if (IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) {
- llvm::APInt intV =
- llvm::APInt::floatToBits((float)IL->getValue().getLimitedValue());
- value = intV.getLimitedValue();
- } else {
- S.Diag(E->getLocStart(), diag::err_hlsl_attribute_expects_float_literal)
- << Attr.getName();
- }
- }
- return value;
- }
- static Stmt* IgnoreParensAndDecay(Stmt* S)
- {
- for (;;)
- {
- switch (S->getStmtClass())
- {
- case Stmt::ParenExprClass:
- S = cast<ParenExpr>(S)->getSubExpr();
- break;
- case Stmt::ImplicitCastExprClass:
- {
- ImplicitCastExpr* castExpr = cast<ImplicitCastExpr>(S);
- if (castExpr->getCastKind() != CK_ArrayToPointerDecay &&
- castExpr->getCastKind() != CK_NoOp &&
- castExpr->getCastKind() != CK_LValueToRValue)
- {
- return S;
- }
- S = castExpr->getSubExpr();
- }
- break;
- default:
- return S;
- }
- }
- }
- static Expr* ValidateClipPlaneArraySubscriptExpr(Sema& S, ArraySubscriptExpr* E)
- {
- DXASSERT_NOMSG(E != nullptr);
- Expr* subscriptExpr = E->getIdx();
- subscriptExpr = dyn_cast<Expr>(subscriptExpr->IgnoreParens());
- if (subscriptExpr == nullptr ||
- subscriptExpr->isTypeDependent() || subscriptExpr->isValueDependent() ||
- !subscriptExpr->isCXX11ConstantExpr(S.Context))
- {
- S.Diag(
- (subscriptExpr == nullptr) ? E->getLocStart() : subscriptExpr->getLocStart(),
- diag::err_hlsl_unsupported_clipplane_argument_subscript_expression);
- return nullptr;
- }
- return E->getBase();
- }
- static bool IsValidClipPlaneDecl(Decl* D)
- {
- Decl::Kind kind = D->getKind();
- if (kind == Decl::Var)
- {
- VarDecl* varDecl = cast<VarDecl>(D);
- if (varDecl->getStorageClass() == StorageClass::SC_Static &&
- varDecl->getType().isConstQualified())
- {
- return false;
- }
- return true;
- }
- else if (kind == Decl::Field)
- {
- return true;
- }
- return false;
- }
- static Expr* ValidateClipPlaneExpr(Sema& S, Expr* E)
- {
- Stmt* cursor = E;
- // clip plane expressions are a linear path, so no need to traverse the tree here.
- while (cursor != nullptr)
- {
- bool supported = true;
- cursor = IgnoreParensAndDecay(cursor);
- switch (cursor->getStmtClass())
- {
- case Stmt::ArraySubscriptExprClass:
- cursor = ValidateClipPlaneArraySubscriptExpr(S, cast<ArraySubscriptExpr>(cursor));
- if (cursor == nullptr)
- {
- // nullptr indicates failure, and the error message has already been printed out
- return nullptr;
- }
- break;
- case Stmt::DeclRefExprClass:
- {
- DeclRefExpr* declRef = cast<DeclRefExpr>(cursor);
- Decl* decl = declRef->getDecl();
- supported = IsValidClipPlaneDecl(decl);
- cursor = supported ? nullptr : cursor;
- }
- break;
- case Stmt::MemberExprClass:
- {
- MemberExpr* member = cast<MemberExpr>(cursor);
- supported = IsValidClipPlaneDecl(member->getMemberDecl());
- cursor = supported ? member->getBase() : cursor;
- }
- break;
- default:
- supported = false;
- break;
- }
- if (!supported)
- {
- DXASSERT(cursor != nullptr, "otherwise it was cleared when the supported flag was set to false");
- S.Diag(cursor->getLocStart(), diag::err_hlsl_unsupported_clipplane_argument_expression);
- return nullptr;
- }
- }
- // Validate that the type is a float4.
- QualType expressionType = E->getType();
- HLSLExternalSource* hlslSource = HLSLExternalSource::FromSema(&S);
- if (hlslSource->GetTypeElementKind(expressionType) != ArBasicKind::AR_BASIC_FLOAT32 ||
- hlslSource->GetTypeObjectKind(expressionType) != ArTypeObjectKind::AR_TOBJ_VECTOR)
- {
- S.Diag(E->getLocStart(), diag::err_hlsl_unsupported_clipplane_argument_type) << expressionType;
- return nullptr;
- }
- return E;
- }
- static Attr* HandleClipPlanes(Sema& S, const AttributeList &A)
- {
- Expr* clipExprs[6];
- for (unsigned int index = 0; index < _countof(clipExprs); index++)
- {
- if (A.getNumArgs() <= index)
- {
- clipExprs[index] = nullptr;
- continue;
- }
- Expr *E = A.getArgAsExpr(index);
- clipExprs[index] = ValidateClipPlaneExpr(S, E);
- }
- return ::new (S.Context) HLSLClipPlanesAttr(A.getRange(), S.Context,
- clipExprs[0], clipExprs[1], clipExprs[2], clipExprs[3], clipExprs[4], clipExprs[5],
- A.getAttributeSpellingListIndex());
- }
- static Attr* HandleUnrollAttribute(Sema& S, const AttributeList &Attr)
- {
- int argValue = ValidateAttributeIntArg(S, Attr);
- // Default value is 1.
- if (Attr.getNumArgs() == 0) argValue = 1;
- return ::new (S.Context) HLSLUnrollAttr(Attr.getRange(), S.Context,
- argValue, Attr.getAttributeSpellingListIndex());
- }
- static void ValidateAttributeOnLoop(Sema& S, Stmt* St, const AttributeList &Attr)
- {
- Stmt::StmtClass stClass = St->getStmtClass();
- if (stClass != Stmt::ForStmtClass && stClass != Stmt::WhileStmtClass && stClass != Stmt::DoStmtClass)
- {
- S.Diag(Attr.getLoc(), diag::warn_hlsl_unsupported_statement_for_loop_attribute)
- << Attr.getName();
- }
- }
- static void ValidateAttributeOnSwitch(Sema& S, Stmt* St, const AttributeList &Attr)
- {
- Stmt::StmtClass stClass = St->getStmtClass();
- if (stClass != Stmt::SwitchStmtClass)
- {
- S.Diag(Attr.getLoc(), diag::warn_hlsl_unsupported_statement_for_switch_attribute)
- << Attr.getName();
- }
- }
- static void ValidateAttributeOnSwitchOrIf(Sema& S, Stmt* St, const AttributeList &Attr)
- {
- Stmt::StmtClass stClass = St->getStmtClass();
- if (stClass != Stmt::SwitchStmtClass && stClass != Stmt::IfStmtClass)
- {
- S.Diag(Attr.getLoc(), diag::warn_hlsl_unsupported_statement_for_if_switch_attribute)
- << Attr.getName();
- }
- }
- static StringRef ValidateAttributeStringArg(Sema& S, const AttributeList &A, _In_opt_z_ const char* values)
- {
- // values is an optional comma-separated list of potential values.
- Expr* E = A.getArgAsExpr(0);
- if (E->isTypeDependent() || E->isValueDependent() || E->getStmtClass() != Stmt::StringLiteralClass)
- {
- S.Diag(E->getLocStart(), diag::err_hlsl_attribute_expects_string_literal)
- << A.getName();
- return StringRef();
- }
- StringLiteral* sl = cast<StringLiteral>(E);
- StringRef result = sl->getString();
- // Return result with no additional validation.
- if (values == nullptr)
- {
- return result;
- }
- const char* value = values;
- while (*value != '\0')
- {
- DXASSERT_NOMSG(*value != ','); // no leading commas in values
- // Look for a match.
- const char* argData = result.data();
- size_t argDataLen = result.size();
- while (argDataLen != 0 && *argData == *value && *value)
- {
- ++argData;
- ++value;
- --argDataLen;
- }
- // Match found if every input character matched.
- if (argDataLen == 0 && (*value == '\0' || *value == ','))
- {
- return result;
- }
- // Move to next separator.
- while (*value != '\0' && *value != ',')
- {
- ++value;
- }
- // Move to the start of the next item if any.
- if (*value == ',') value++;
- }
- DXASSERT_NOMSG(*value == '\0'); // no other terminating conditions
- // No match found.
- S.Diag(E->getLocStart(), diag::err_hlsl_attribute_expects_string_literal_from_list)
- << A.getName() << values;
- return StringRef();
- }
- static
- bool ValidateAttributeTargetIsFunction(Sema& S, Decl* D, const AttributeList &A)
- {
- if (D->isFunctionOrFunctionTemplate())
- {
- return true;
- }
- S.Diag(A.getLoc(), diag::err_hlsl_attribute_valid_on_function_only);
- return false;
- }
- void hlsl::HandleDeclAttributeForHLSL(Sema &S, Decl *D, const AttributeList &A, bool& Handled)
- {
- DXASSERT_NOMSG(D != nullptr);
- DXASSERT_NOMSG(!A.isInvalid());
- Attr* declAttr = nullptr;
- Handled = true;
- switch (A.getKind())
- {
- case AttributeList::AT_HLSLIn:
- declAttr = ::new (S.Context) HLSLInAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLOut:
- declAttr = ::new (S.Context) HLSLOutAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLInOut:
- declAttr = ::new (S.Context) HLSLInOutAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLNoInterpolation:
- declAttr = ::new (S.Context) HLSLNoInterpolationAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLLinear:
- declAttr = ::new (S.Context) HLSLLinearAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLNoPerspective:
- declAttr = ::new (S.Context) HLSLNoPerspectiveAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLSample:
- declAttr = ::new (S.Context) HLSLSampleAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLCentroid:
- declAttr = ::new (S.Context) HLSLCentroidAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLPrecise:
- declAttr = ::new (S.Context) HLSLPreciseAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLShared:
- declAttr = ::new (S.Context) HLSLSharedAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLGroupShared:
- declAttr = ::new (S.Context) HLSLGroupSharedAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLUniform:
- declAttr = ::new (S.Context) HLSLUniformAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLColumnMajor:
- declAttr = ::new (S.Context) HLSLColumnMajorAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLRowMajor:
- declAttr = ::new (S.Context) HLSLRowMajorAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLUnorm:
- declAttr = ::new (S.Context) HLSLUnormAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLSnorm:
- declAttr = ::new (S.Context) HLSLSnormAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLPoint:
- declAttr = ::new (S.Context) HLSLPointAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLLine:
- declAttr = ::new (S.Context) HLSLLineAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLLineAdj:
- declAttr = ::new (S.Context) HLSLLineAdjAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLTriangle:
- declAttr = ::new (S.Context) HLSLTriangleAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLTriangleAdj:
- declAttr = ::new (S.Context) HLSLTriangleAdjAttr(A.getRange(), S.Context,
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLGloballyCoherent:
- declAttr = ::new (S.Context) HLSLGloballyCoherentAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- default:
- Handled = false;
- break;
- }
- if (declAttr != nullptr)
- {
- DXASSERT_NOMSG(Handled);
- D->addAttr(declAttr);
- return;
- }
- Handled = true;
- switch (A.getKind())
- {
- // These apply to statements, not declarations. The warning messages clarify this properly.
- case AttributeList::AT_HLSLUnroll:
- case AttributeList::AT_HLSLAllowUAVCondition:
- case AttributeList::AT_HLSLLoop:
- case AttributeList::AT_HLSLFastOpt:
- S.Diag(A.getLoc(), diag::warn_hlsl_unsupported_statement_for_loop_attribute)
- << A.getName();
- return;
- case AttributeList::AT_HLSLBranch:
- case AttributeList::AT_HLSLFlatten:
- S.Diag(A.getLoc(), diag::warn_hlsl_unsupported_statement_for_if_switch_attribute)
- << A.getName();
- return;
- case AttributeList::AT_HLSLForceCase:
- case AttributeList::AT_HLSLCall:
- S.Diag(A.getLoc(), diag::warn_hlsl_unsupported_statement_for_switch_attribute)
- << A.getName();
- return;
- // These are the cases that actually apply to declarations.
- case AttributeList::AT_HLSLClipPlanes:
- declAttr = HandleClipPlanes(S, A);
- break;
- case AttributeList::AT_HLSLDomain:
- declAttr = ::new (S.Context) HLSLDomainAttr(A.getRange(), S.Context,
- ValidateAttributeStringArg(S, A, "tri,quad,isoline"), A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLEarlyDepthStencil:
- declAttr = ::new (S.Context) HLSLEarlyDepthStencilAttr(A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLInstance:
- declAttr = ::new (S.Context) HLSLInstanceAttr(A.getRange(), S.Context,
- ValidateAttributeIntArg(S, A), A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLMaxTessFactor:
- declAttr = ::new (S.Context) HLSLMaxTessFactorAttr(A.getRange(), S.Context,
- ValidateAttributeFloatArg(S, A), A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLNumThreads:
- declAttr = ::new (S.Context) HLSLNumThreadsAttr(A.getRange(), S.Context,
- ValidateAttributeIntArg(S, A), ValidateAttributeIntArg(S, A, 1), ValidateAttributeIntArg(S, A, 2),
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLRootSignature:
- declAttr = ::new (S.Context) HLSLRootSignatureAttr(A.getRange(), S.Context,
- ValidateAttributeStringArg(S, A, /*validate strings*/nullptr),
- A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLOutputControlPoints:
- declAttr = ::new (S.Context) HLSLOutputControlPointsAttr(A.getRange(), S.Context,
- ValidateAttributeIntArg(S, A), A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLOutputTopology:
- declAttr = ::new (S.Context) HLSLOutputTopologyAttr(A.getRange(), S.Context,
- ValidateAttributeStringArg(S, A, "point,line,triangle,triangle_cw,triangle_ccw"), A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLPartitioning:
- declAttr = ::new (S.Context) HLSLPartitioningAttr(A.getRange(), S.Context,
- ValidateAttributeStringArg(S, A, "integer,fractional_even,fractional_odd,pow2"), A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLPatchConstantFunc:
- declAttr = ::new (S.Context) HLSLPatchConstantFuncAttr(A.getRange(), S.Context,
- ValidateAttributeStringArg(S, A, nullptr), A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLMaxVertexCount:
- declAttr = ::new (S.Context) HLSLMaxVertexCountAttr(A.getRange(), S.Context,
- ValidateAttributeIntArg(S, A), A.getAttributeSpellingListIndex());
- break;
- default:
- Handled = false;
- return;
- }
- if (declAttr != nullptr)
- {
- DXASSERT_NOMSG(Handled);
- D->addAttr(declAttr);
- // The attribute has been set but will have no effect. Validation will emit a diagnostic
- // and prevent code generation.
- ValidateAttributeTargetIsFunction(S, D, A);
- }
- }
- /// <summary>Processes an attribute for a statement.</summary>
- /// <param name="S">Sema with context.</param>
- /// <param name="St">Statement annotated.</param>
- /// <param name="A">Single parsed attribute to process.</param>
- /// <param name="Range">Range of all attribute lists (useful for FixIts to suggest inclusions).</param>
- /// <param name="Handled">After execution, whether this was recognized and handled.</param>
- /// <returns>An attribute instance if processed, nullptr if not recognized or an error was found.</returns>
- Attr *hlsl::ProcessStmtAttributeForHLSL(Sema &S, Stmt *St, const AttributeList &A, SourceRange Range, bool& Handled)
- {
- // | Construct | Allowed Attributes |
- // +------------------+--------------------------------------------+
- // | for, while, do | loop, fastopt, unroll, allow_uav_condition |
- // | if | branch, flatten |
- // | switch | branch, flatten, forcecase, call |
- Attr * result = nullptr;
- Handled = true;
- switch (A.getKind())
- {
- case AttributeList::AT_HLSLUnroll:
- ValidateAttributeOnLoop(S, St, A);
- result = HandleUnrollAttribute(S, A);
- break;
- case AttributeList::AT_HLSLAllowUAVCondition:
- ValidateAttributeOnLoop(S, St, A);
- result = ::new (S.Context) HLSLAllowUAVConditionAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLLoop:
- ValidateAttributeOnLoop(S, St, A);
- result = ::new (S.Context) HLSLLoopAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLFastOpt:
- ValidateAttributeOnLoop(S, St, A);
- result = ::new (S.Context) HLSLFastOptAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLBranch:
- ValidateAttributeOnSwitchOrIf(S, St, A);
- result = ::new (S.Context) HLSLBranchAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLFlatten:
- ValidateAttributeOnSwitchOrIf(S, St, A);
- result = ::new (S.Context) HLSLFlattenAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLForceCase:
- ValidateAttributeOnSwitch(S, St, A);
- result = ::new (S.Context) HLSLForceCaseAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- case AttributeList::AT_HLSLCall:
- ValidateAttributeOnSwitch(S, St, A);
- result = ::new (S.Context) HLSLCallAttr(
- A.getRange(), S.Context, A.getAttributeSpellingListIndex());
- break;
- default:
- Handled = false;
- break;
- }
- return result;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Implementation of Sema members. //
- Decl* Sema::ActOnStartHLSLBuffer(
- Scope* bufferScope,
- bool cbuffer, SourceLocation KwLoc,
- IdentifierInfo *Ident, SourceLocation IdentLoc,
- std::vector<hlsl::UnusualAnnotation *>& BufferAttributes,
- SourceLocation LBrace)
- {
- // For anonymous namespace, take the location of the left brace.
- SourceLocation Loc = Ident ? IdentLoc : LBrace;
- DeclContext* lexicalParent = getCurLexicalContext();
- clang::HLSLBufferDecl *result = HLSLBufferDecl::Create(
- Context, lexicalParent, cbuffer, /*isConstantBufferView*/ false, KwLoc,
- Ident, IdentLoc, BufferAttributes, LBrace);
- // Keep track of the currently active buffer.
- HLSLBuffers.push_back(result);
- // Validate unusual annotations and emit diagnostics.
- DiagnoseUnusualAnnotationsForHLSL(*this, BufferAttributes);
- auto && unusualIter = BufferAttributes.begin();
- auto && unusualEnd = BufferAttributes.end();
- char expectedRegisterType = cbuffer ? 'b' : 't';
- for (; unusualIter != unusualEnd; ++unusualIter) {
- switch ((*unusualIter)->getKind()) {
- case hlsl::UnusualAnnotation::UA_ConstantPacking: {
- hlsl::ConstantPacking* constantPacking = cast<hlsl::ConstantPacking>(*unusualIter);
- Diag(constantPacking->Loc, diag::err_hlsl_unsupported_buffer_packoffset);
- break;
- }
- case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
- hlsl::RegisterAssignment* registerAssignment = cast<hlsl::RegisterAssignment>(*unusualIter);
- if (registerAssignment->RegisterType != expectedRegisterType && registerAssignment->RegisterType != toupper(expectedRegisterType)) {
- Diag(registerAssignment->Loc, cbuffer ? diag::err_hlsl_unsupported_cbuffer_register :
- diag::err_hlsl_unsupported_tbuffer_register);
- } else if (registerAssignment->ShaderProfile.size() > 0) {
- Diag(registerAssignment->Loc, diag::err_hlsl_unsupported_buffer_slot_target_specific);
- }
- break;
- }
- case hlsl::UnusualAnnotation::UA_SemanticDecl: {
- // Ignore semantic declarations.
- break;
- }
- }
- }
- PushOnScopeChains(result, bufferScope);
- PushDeclContext(bufferScope, result);
- ActOnDocumentableDecl(result);
- return result;
- }
- void Sema::ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace)
- {
- DXASSERT_NOMSG(Dcl != nullptr);
- DXASSERT(Dcl == HLSLBuffers.back(), "otherwise push/pop is incorrect");
- dyn_cast<HLSLBufferDecl>(Dcl)->setRBraceLoc(RBrace);
- HLSLBuffers.pop_back();
- PopDeclContext();
- }
- Decl* Sema::getActiveHLSLBuffer() const
- {
- return HLSLBuffers.empty() ? nullptr : HLSLBuffers.back();
- }
- Decl *Sema::ActOnHLSLBufferView(Scope *bufferScope, SourceLocation KwLoc,
- DeclGroupPtrTy &dcl, bool iscbuf) {
- DXASSERT(nullptr == HLSLBuffers.back(), "otherwise push/pop is incorrect");
- HLSLBuffers.pop_back();
- DXASSERT(HLSLBuffers.empty(), "otherwise push/pop is incorrect");
- Decl *decl = dcl.get().getSingleDecl();
- NamedDecl *namedDecl = cast<NamedDecl>(decl);
- IdentifierInfo *Ident = namedDecl->getIdentifier();
- // No anonymous namespace for ConstantBuffer, take the location of the decl.
- SourceLocation Loc = decl->getLocation();
- // Prevent array type in template. The only way to specify an array in the template type
- // is to use a typedef, so we will strip non-typedef arrays off, since these are the legal
- // array dimensions for the CBV/TBV, and if any array type remains, that is illegal.
- QualType declType = cast<VarDecl>(namedDecl)->getType();
- while (declType->isArrayType() && declType->getTypeClass() != Type::TypeClass::Typedef) {
- const ArrayType *arrayType = declType->getAsArrayTypeUnsafe();
- declType = arrayType->getElementType();
- }
- if (declType->isArrayType()) {
- Diag(Loc, diag::err_hlsl_typeintemplateargument) << "array";
- return nullptr;
- }
- std::vector<hlsl::UnusualAnnotation *> hlslAttrs;
- DeclContext *lexicalParent = getCurLexicalContext();
- clang::HLSLBufferDecl *result = HLSLBufferDecl::Create(
- Context, lexicalParent, iscbuf, /*isConstantBufferView*/ true,
- KwLoc, Ident, Loc, hlslAttrs, Loc);
- // set relation
- namedDecl->setDeclContext(result);
- result->addDecl(namedDecl);
- // move attribute from constant to constant buffer
- result->setUnusualAnnotations(namedDecl->getUnusualAnnotations());
- namedDecl->setUnusualAnnotations(hlslAttrs);
- return result;
- }
- bool Sema::IsOnHLSLBufferView() {
- // nullptr will not pushed for cbuffer.
- return !HLSLBuffers.empty() && getActiveHLSLBuffer() == nullptr;
- }
- void Sema::ActOnStartHLSLBufferView() {
- // Push nullptr to mark HLSLBufferView.
- DXASSERT(HLSLBuffers.empty(), "otherwise push/pop is incorrect");
- HLSLBuffers.emplace_back(nullptr);
- }
- HLSLBufferDecl::HLSLBufferDecl(
- DeclContext *DC, bool cbuffer, bool cbufferView, SourceLocation KwLoc,
- IdentifierInfo *Id, SourceLocation IdLoc,
- std::vector<hlsl::UnusualAnnotation *> &BufferAttributes,
- SourceLocation LBrace)
- : NamedDecl(Decl::HLSLBuffer, DC, IdLoc, DeclarationName(Id)),
- DeclContext(Decl::HLSLBuffer), IsCBuffer(cbuffer),
- IsConstantBufferView(cbufferView), KwLoc(KwLoc), LBraceLoc(LBrace) {
- if (!BufferAttributes.empty()) {
- setUnusualAnnotations(UnusualAnnotation::CopyToASTContextArray(
- getASTContext(), BufferAttributes.data(), BufferAttributes.size()));
- }
- }
- HLSLBufferDecl *
- HLSLBufferDecl::Create(ASTContext &C, DeclContext *lexicalParent, bool cbuffer,
- bool constantbuffer, SourceLocation KwLoc,
- IdentifierInfo *Id, SourceLocation IdLoc,
- std::vector<hlsl::UnusualAnnotation *> &BufferAttributes,
- SourceLocation LBrace) {
- DeclContext *DC = C.getTranslationUnitDecl();
- HLSLBufferDecl *result = ::new (C) HLSLBufferDecl(
- DC, cbuffer, constantbuffer, KwLoc, Id, IdLoc, BufferAttributes, LBrace);
- if (DC != lexicalParent) {
- result->setLexicalDeclContext(lexicalParent);
- }
- return result;
- }
- void Sema::TransferUnusualAttributes(Declarator &D, NamedDecl *NewDecl) {
- assert(NewDecl != nullptr);
- if (!getLangOpts().HLSL) {
- return;
- }
- if (!D.UnusualAnnotations.empty()) {
- NewDecl->setUnusualAnnotations(UnusualAnnotation::CopyToASTContextArray(
- getASTContext(), D.UnusualAnnotations.data(),
- D.UnusualAnnotations.size()));
- D.UnusualAnnotations.clear();
- }
- }
- /// Checks whether a usage attribute is compatible with those seen so far and
- /// maintains history.
- static bool IsUsageAttributeCompatible(AttributeList::Kind kind, bool &usageIn,
- bool &usageOut) {
- switch (kind) {
- case AttributeList::AT_HLSLIn:
- if (usageIn)
- return false;
- usageIn = true;
- break;
- case AttributeList::AT_HLSLOut:
- if (usageOut)
- return false;
- usageOut = true;
- break;
- default:
- assert(kind == AttributeList::AT_HLSLInOut);
- if (usageOut || usageIn)
- return false;
- usageIn = usageOut = true;
- break;
- }
- return true;
- }
- // Diagnose valid/invalid modifiers for HLSL.
- bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC,
- TypeSourceInfo *TInfo, bool isParameter) {
- assert(getLangOpts().HLSL &&
- "otherwise this is called without checking language first");
- // NOTE: some tests may declare templates.
- if (DC->isNamespace() || DC->isDependentContext()) return true;
- DeclSpec::SCS storage = D.getDeclSpec().getStorageClassSpec();
- assert(!DC->isClosure() && "otherwise parser accepted closure syntax instead of failing with a syntax error");
- assert(!DC->isDependentContext() && "otherwise parser accepted a template instead of failing with a syntax error");
- assert(!DC->isNamespace() && "otherwise parser accepted a namespace instead of failing a syntax error");
- bool result = true;
- bool isTypedef = storage == DeclSpec::SCS_typedef;
- bool isFunction = D.isFunctionDeclarator() && !DC->isRecord();
- bool isLocalVar = DC->isFunctionOrMethod() && !isFunction && !isTypedef;
- bool isGlobal = !isParameter && !isTypedef && !isFunction && (DC->isTranslationUnit() || DC->getDeclKind() == Decl::HLSLBuffer);
- bool isMethod = DC->isRecord() && D.isFunctionDeclarator() && !isTypedef;
- bool isField = DC->isRecord() && !D.isFunctionDeclarator() && !isTypedef;
- bool isConst = D.getDeclSpec().getTypeQualifiers() & DeclSpec::TQ::TQ_const;
- bool isVolatile = D.getDeclSpec().getTypeQualifiers() & DeclSpec::TQ::TQ_volatile;
- bool isStatic = storage == DeclSpec::SCS::SCS_static;
- bool isExtern = storage == DeclSpec::SCS::SCS_extern;
- bool hasSignSpec = D.getDeclSpec().getTypeSpecSign() != DeclSpec::TSS::TSS_unspecified;
- assert(
- (1 == (isLocalVar ? 1 : 0) + (isGlobal ? 1 : 0) + (isField ? 1 : 0) +
- (isTypedef ? 1 : 0) + (isFunction ? 1 : 0) + (isMethod ? 1 : 0) +
- (isParameter ? 1 : 0))
- && "exactly one type of declarator is being processed");
- // qt/pType captures either the type being modified, or the return type in the
- // case of a function (or method).
- QualType qt = TInfo->getType();
- const Type* pType = qt.getTypePtrOrNull();
- // Early checks - these are not simple attribution errors, but constructs that
- // are fundamentally unsupported,
- // and so we avoid errors that might indicate they can be repaired.
- if (DC->isRecord()) {
- unsigned int nestedDiagId = 0;
- if (isTypedef) {
- nestedDiagId = diag::err_hlsl_unsupported_nested_typedef;
- }
- if (nestedDiagId) {
- Diag(D.getLocStart(), nestedDiagId);
- D.setInvalidType();
- return false;
- }
- }
- const char* declarationType =
- (isLocalVar) ? "local variable" :
- (isTypedef) ? "typedef" :
- (isFunction) ? "function" :
- (isMethod) ? "method" :
- (isGlobal) ? "global variable" :
- (isParameter) ? "parameter" :
- (isField) ? "field" : "<unknown>";
- if (pType && D.isFunctionDeclarator()) {
- const FunctionProtoType *pFP = pType->getAs<FunctionProtoType>();
- if (pFP) {
- qt = pFP->getReturnType();
- pType = qt.getTypePtrOrNull();
- }
- }
- // Check for deprecated effect object type here, warn, and invalidate decl
- bool bDeprecatedEffectObject = false;
- bool bIsObject = false;
- if (hlsl::IsObjectType(this, qt, &bDeprecatedEffectObject)) {
- bIsObject = true;
- if (bDeprecatedEffectObject) {
- Diag(D.getLocStart(), diag::warn_hlsl_effect_object);
- D.setInvalidType();
- return false;
- }
- // Add methods if not ready.
- HLSLExternalSource *hlslSource = HLSLExternalSource::FromSema(this);
- hlslSource->AddHLSLObjectMethodsIfNotReady(qt);
- } else if (qt->isArrayType()) {
- QualType eltQt(qt->getArrayElementTypeNoTypeQual(), 0);
- if (hlsl::IsObjectType(this, eltQt, &bDeprecatedEffectObject)) {
- // Add methods if not ready.
- HLSLExternalSource *hlslSource = HLSLExternalSource::FromSema(this);
- hlslSource->AddHLSLObjectMethodsIfNotReady(eltQt);
- }
- }
- if (isExtern) {
- if (!(isFunction || isGlobal)) {
- Diag(D.getLocStart(), diag::err_hlsl_varmodifierna) << "'extern'"
- << declarationType;
- result = false;
- }
- }
- if (isStatic) {
- if (!(isLocalVar || isGlobal || isFunction || isMethod)) {
- Diag(D.getLocStart(), diag::err_hlsl_varmodifierna) << "'static'"
- << declarationType;
- result = false;
- }
- }
- if (isVolatile) {
- if (!(isLocalVar || isTypedef)) {
- Diag(D.getLocStart(), diag::err_hlsl_varmodifierna) << "'volatile'"
- << declarationType;
- result = false;
- }
- }
- if (isConst) {
- if (isField) {
- Diag(D.getLocStart(), diag::err_hlsl_varmodifierna) << "'const'"
- << declarationType;
- result = false;
- }
- }
- if (hasSignSpec) {
- HLSLExternalSource *hlslSource = HLSLExternalSource::FromSema(this);
- ArTypeObjectKind objKind = hlslSource->GetTypeObjectKind(qt);
- ArBasicKind basicKind = hlslSource->GetTypeElementKind(qt);
- // vectors or matrices can only have unsigned integer types.
- if (objKind == AR_TOBJ_MATRIX || objKind == AR_TOBJ_VECTOR || objKind == AR_TOBJ_BASIC || objKind == AR_TOBJ_ARRAY) {
- if (!IS_BASIC_UNSIGNABLE(basicKind)) {
- Diag(D.getLocStart(), diag::err_sema_invalid_sign_spec)
- << g_ArBasicTypeNames[basicKind];
- result = false;
- }
- }
- else {
- Diag(D.getLocStart(), diag::err_sema_invalid_sign_spec) << g_ArBasicTypeNames[basicKind];
- result = false;
- }
- }
- // Validate attributes
- clang::AttributeList
- *pPrecise = nullptr,
- *pShared = nullptr,
- *pGroupShared = nullptr,
- *pUniform = nullptr,
- *pUsage = nullptr,
- *pNoInterpolation = nullptr,
- *pLinear = nullptr,
- *pNoPerspective = nullptr,
- *pSample = nullptr,
- *pCentroid = nullptr,
- *pAnyLinear = nullptr, // first linear attribute found
- *pTopology = nullptr;
- bool usageIn = false;
- bool usageOut = false;
- for (clang::AttributeList *pAttr = D.getDeclSpec().getAttributes().getList();
- pAttr != NULL; pAttr = pAttr->getNext()) {
- if (pAttr->isInvalid() || pAttr->isUsedAsTypeAttr())
- continue;
- switch (pAttr->getKind()) {
- case AttributeList::AT_HLSLPrecise: // precise is applicable everywhere.
- pPrecise = pAttr;
- break;
- case AttributeList::AT_HLSLShared:
- if (!isGlobal) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
- << pAttr->getName() << declarationType << pAttr->getRange();
- result = false;
- }
- if (isStatic) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifiersna)
- << "'static'" << pAttr->getName() << declarationType
- << pAttr->getRange();
- result = false;
- }
- pShared = pAttr;
- break;
- case AttributeList::AT_HLSLGroupShared:
- if (!isGlobal) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
- << pAttr->getName() << declarationType << pAttr->getRange();
- result = false;
- }
- if (isExtern) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifiersna)
- << "'extern'" << pAttr->getName() << declarationType
- << pAttr->getRange();
- result = false;
- }
- pGroupShared = pAttr;
- break;
- case AttributeList::AT_HLSLGloballyCoherent:
- if (!bIsObject) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
- << pAttr->getName() << "non-UAV type";
- result = false;
- }
- break;
- case AttributeList::AT_HLSLUniform:
- if (!(isGlobal || isParameter)) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
- << pAttr->getName() << declarationType << pAttr->getRange();
- result = false;
- }
- if (isStatic) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifiersna)
- << "'static'" << pAttr->getName() << declarationType
- << pAttr->getRange();
- result = false;
- }
- pUniform = pAttr;
- break;
- case AttributeList::AT_HLSLIn:
- case AttributeList::AT_HLSLOut:
- case AttributeList::AT_HLSLInOut:
- if (!isParameter) {
- Diag(pAttr->getLoc(), diag::err_hlsl_usage_not_on_parameter)
- << pAttr->getName() << pAttr->getRange();
- result = false;
- }
- if (!IsUsageAttributeCompatible(pAttr->getKind(), usageIn, usageOut)) {
- Diag(pAttr->getLoc(), diag::err_hlsl_duplicate_parameter_usages)
- << pAttr->getName() << pAttr->getRange();
- result = false;
- }
- pUsage = pAttr;
- break;
- case AttributeList::AT_HLSLNoInterpolation:
- if (!(isParameter || isField || isFunction)) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
- << pAttr->getName() << declarationType << pAttr->getRange();
- result = false;
- }
- if (pNoInterpolation) {
- Diag(pAttr->getLoc(), diag::warn_hlsl_duplicate_specifier)
- << pAttr->getName() << pAttr->getRange();
- }
- pNoInterpolation = pAttr;
- break;
- case AttributeList::AT_HLSLLinear:
- case AttributeList::AT_HLSLNoPerspective:
- case AttributeList::AT_HLSLSample:
- case AttributeList::AT_HLSLCentroid:
- if (!(isParameter || isField || isFunction)) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
- << pAttr->getName() << declarationType << pAttr->getRange();
- result = false;
- }
- if (nullptr == pAnyLinear)
- pAnyLinear = pAttr;
- switch (pAttr->getKind()) {
- case AttributeList::AT_HLSLLinear:
- if (pLinear) {
- Diag(pAttr->getLoc(), diag::warn_hlsl_duplicate_specifier)
- << pAttr->getName() << pAttr->getRange();
- }
- pLinear = pAttr;
- break;
- case AttributeList::AT_HLSLNoPerspective:
- if (pNoPerspective) {
- Diag(pAttr->getLoc(), diag::warn_hlsl_duplicate_specifier)
- << pAttr->getName() << pAttr->getRange();
- }
- pNoPerspective = pAttr;
- break;
- case AttributeList::AT_HLSLSample:
- if (pSample) {
- Diag(pAttr->getLoc(), diag::warn_hlsl_duplicate_specifier)
- << pAttr->getName() << pAttr->getRange();
- }
- pSample = pAttr;
- break;
- case AttributeList::AT_HLSLCentroid:
- if (pCentroid) {
- Diag(pAttr->getLoc(), diag::warn_hlsl_duplicate_specifier)
- << pAttr->getName() << pAttr->getRange();
- }
- pCentroid = pAttr;
- break;
- }
- break;
- case AttributeList::AT_HLSLPoint:
- case AttributeList::AT_HLSLLine:
- case AttributeList::AT_HLSLLineAdj:
- case AttributeList::AT_HLSLTriangle:
- case AttributeList::AT_HLSLTriangleAdj:
- if (!(isParameter)) {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
- << pAttr->getName() << declarationType << pAttr->getRange();
- result = false;
- }
- if (pTopology) {
- if (pTopology->getKind() == pAttr->getKind()) {
- Diag(pAttr->getLoc(), diag::warn_hlsl_duplicate_specifier)
- << pAttr->getName() << pAttr->getRange();
- } else {
- Diag(pAttr->getLoc(), diag::err_hlsl_varmodifiersna)
- << pAttr->getName() << pTopology->getName()
- << declarationType << pAttr->getRange();
- result = false;
- }
- }
- pTopology = pAttr;
- break;
- default:
- break;
- }
- }
- if (pNoInterpolation && pAnyLinear) {
- Diag(pNoInterpolation->getLoc(), diag::err_hlsl_varmodifiersna)
- << pNoInterpolation->getName() << pAnyLinear->getName()
- << declarationType << pNoInterpolation->getRange();
- result = false;
- }
- if (pSample && pCentroid) {
- Diag(pCentroid->getLoc(), diag::warn_hlsl_specifier_overridden)
- << pCentroid->getName() << pSample->getName() << pCentroid->getRange();
- }
- clang::AttributeList *pNonUniformAttr = pAnyLinear ? pAnyLinear : (
- pNoInterpolation ? pNoInterpolation : pTopology);
- if (pUniform && pNonUniformAttr) {
- Diag(pUniform->getLoc(), diag::err_hlsl_varmodifiersna)
- << pNonUniformAttr->getName()
- << pUniform->getName() << declarationType << pUniform->getRange();
- result = false;
- }
- if (pAnyLinear && pTopology) {
- Diag(pAnyLinear->getLoc(), diag::err_hlsl_varmodifiersna)
- << pTopology->getName()
- << pAnyLinear->getName() << declarationType << pAnyLinear->getRange();
- result = false;
- }
- if (pNoInterpolation && pTopology) {
- Diag(pNoInterpolation->getLoc(), diag::err_hlsl_varmodifiersna)
- << pTopology->getName()
- << pNoInterpolation->getName() << declarationType << pNoInterpolation->getRange();
- result = false;
- }
- if (pUniform && pUsage) {
- if (pUsage->getKind() != AttributeList::Kind::AT_HLSLIn) {
- Diag(pUniform->getLoc(), diag::err_hlsl_varmodifiersna)
- << pUsage->getName() << pUniform->getName() << declarationType
- << pUniform->getRange();
- result = false;
- }
- }
- // Validate unusual annotations.
- hlsl::DiagnoseUnusualAnnotationsForHLSL(*this, D.UnusualAnnotations);
- auto && unusualIter = D.UnusualAnnotations.begin();
- auto && unusualEnd = D.UnusualAnnotations.end();
- for (; unusualIter != unusualEnd; ++unusualIter) {
- switch ((*unusualIter)->getKind()) {
- case hlsl::UnusualAnnotation::UA_ConstantPacking: {
- hlsl::ConstantPacking *constantPacking =
- cast<hlsl::ConstantPacking>(*unusualIter);
- if (!isGlobal || HLSLBuffers.size() == 0) {
- Diag(constantPacking->Loc, diag::err_hlsl_packoffset_requires_cbuffer);
- continue;
- }
- if (constantPacking->ComponentOffset > 0) {
- // Validate that this will fit.
- if (!qt.isNull()) {
- hlsl::DiagnosePackingOffset(this, constantPacking->Loc, qt,
- constantPacking->ComponentOffset);
- }
- }
- break;
- }
- case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
- hlsl::RegisterAssignment *registerAssignment =
- cast<hlsl::RegisterAssignment>(*unusualIter);
- if (registerAssignment->IsValid) {
- if (!qt.isNull()) {
- hlsl::DiagnoseRegisterType(this, registerAssignment->Loc, qt,
- registerAssignment->RegisterType);
- }
- }
- break;
- }
- case hlsl::UnusualAnnotation::UA_SemanticDecl: {
- hlsl::SemanticDecl *semanticDecl = cast<hlsl::SemanticDecl>(*unusualIter);
- if (isTypedef || isLocalVar) {
- Diag(semanticDecl->Loc, diag::err_hlsl_varmodifierna)
- << "semantic" << declarationType;
- }
- break;
- }
- }
- }
- if (!result) {
- D.setInvalidType();
- }
- return result;
- }
- static QualType getUnderlyingType(QualType Type)
- {
- while (const TypedefType *TD = dyn_cast<TypedefType>(Type))
- {
- if (const TypedefNameDecl* pDecl = TD->getDecl())
- Type = pDecl->getUnderlyingType();
- else
- break;
- }
- return Type;
- }
- /// <summary>Return HLSL AttributedType objects if they exist on type.</summary>
- /// <param name="self">Sema with context.</param>
- /// <param name="type">QualType to inspect.</param>
- /// <param name="ppMatrixOrientation">Set pointer to column_major/row_major AttributedType if supplied.</param>
- /// <param name="ppNorm">Set pointer to snorm/unorm AttributedType if supplied.</param>
- void hlsl::GetHLSLAttributedTypes(
- _In_ clang::Sema* self,
- clang::QualType type,
- _Inout_opt_ const clang::AttributedType** ppMatrixOrientation,
- _Inout_opt_ const clang::AttributedType** ppNorm)
- {
- if (ppMatrixOrientation)
- *ppMatrixOrientation = nullptr;
- if (ppNorm)
- *ppNorm = nullptr;
- // Note: we clear output pointers once set so we can stop searching
- QualType Desugared = getUnderlyingType(type);
- const AttributedType *AT = dyn_cast<AttributedType>(Desugared);
- while (AT && (ppMatrixOrientation || ppNorm)) {
- AttributedType::Kind Kind = AT->getAttrKind();
- if (Kind == AttributedType::attr_hlsl_row_major ||
- Kind == AttributedType::attr_hlsl_column_major)
- {
- if (ppMatrixOrientation)
- {
- *ppMatrixOrientation = AT;
- ppMatrixOrientation = nullptr;
- }
- }
- else if (Kind == AttributedType::attr_hlsl_unorm ||
- Kind == AttributedType::attr_hlsl_snorm)
- {
- if (ppNorm)
- {
- *ppNorm = AT;
- ppNorm = nullptr;
- }
- }
- Desugared = getUnderlyingType(AT->getEquivalentType());
- AT = dyn_cast<AttributedType>(Desugared);
- }
- // Unwrap component type on vector or matrix and check snorm/unorm
- Desugared = getUnderlyingType(hlsl::GetOriginalElementType(self, Desugared));
- AT = dyn_cast<AttributedType>(Desugared);
- while (AT && ppNorm) {
- AttributedType::Kind Kind = AT->getAttrKind();
- if (Kind == AttributedType::attr_hlsl_unorm ||
- Kind == AttributedType::attr_hlsl_snorm)
- {
- *ppNorm = AT;
- ppNorm = nullptr;
- }
- Desugared = getUnderlyingType(AT->getEquivalentType());
- AT = dyn_cast<AttributedType>(Desugared);
- }
- }
- /// <summary>Returns true if QualType is an HLSL Matrix type.</summary>
- /// <param name="self">Sema with context.</param>
- /// <param name="type">QualType to check.</param>
- bool hlsl::IsMatrixType(
- _In_ clang::Sema* self,
- _In_ clang::QualType type)
- {
- return HLSLExternalSource::FromSema(self)->GetTypeObjectKind(type) == AR_TOBJ_MATRIX;
- }
- /// <summary>Returns true if QualType is an HLSL Vector type.</summary>
- /// <param name="self">Sema with context.</param>
- /// <param name="type">QualType to check.</param>
- bool hlsl::IsVectorType(
- _In_ clang::Sema* self,
- _In_ clang::QualType type)
- {
- return HLSLExternalSource::FromSema(self)->GetTypeObjectKind(type) == AR_TOBJ_VECTOR;
- }
- /// <summary>Get element type for an HLSL Matrix or Vector, preserving AttributedType.</summary>
- /// <param name="self">Sema with context.</param>
- /// <param name="type">Matrix or Vector type.</param>
- clang::QualType hlsl::GetOriginalMatrixOrVectorElementType(
- _In_ clang::QualType type)
- {
- // TODO: Determine if this is really the best way to get the matrix/vector specialization
- // without losing the AttributedType on the template parameter
- if (const Type* pType = type.getTypePtrOrNull()) {
- // A non-dependent template specialization type is always "sugar",
- // typically for a RecordType. For example, a class template
- // specialization type of @c vector<int> will refer to a tag type for
- // the instantiation @c std::vector<int, std::allocator<int>>.
- if (const TemplateSpecializationType* pTemplate = pType->getAs<TemplateSpecializationType>()) {
- // If we have enough arguments, pull them from the template directly, rather than doing
- // the extra lookups.
- if (pTemplate->getNumArgs() > 0)
- return pTemplate->getArg(0).getAsType();
- QualType templateRecord = pTemplate->desugar();
- const Type *pTemplateRecordType = templateRecord.getTypePtr();
- if (pTemplateRecordType) {
- const TagType *pTemplateTagType = pTemplateRecordType->getAs<TagType>();
- if (pTemplateTagType) {
- const ClassTemplateSpecializationDecl *specializationDecl =
- dyn_cast_or_null<ClassTemplateSpecializationDecl>(
- pTemplateTagType->getDecl());
- if (specializationDecl) {
- return specializationDecl->getTemplateArgs()[0].getAsType();
- }
- }
- }
- }
- }
- return QualType();
- }
- /// <summary>Get element type, preserving AttributedType, if vector or matrix, otherwise return the type unmodified.</summary>
- /// <param name="self">Sema with context.</param>
- /// <param name="type">Input type.</param>
- clang::QualType hlsl::GetOriginalElementType(
- _In_ clang::Sema* self,
- _In_ clang::QualType type)
- {
- ArTypeObjectKind Kind = HLSLExternalSource::FromSema(self)->GetTypeObjectKind(type);
- if (Kind == AR_TOBJ_MATRIX || Kind == AR_TOBJ_VECTOR) {
- return GetOriginalMatrixOrVectorElementType(type);
- }
- return type;
- }
- void hlsl::CustomPrintHLSLAttr(const clang::Attr *A, llvm::raw_ostream &Out, const clang::PrintingPolicy &Policy, unsigned int Indentation) {
- switch (A->getKind()) {
- // Parameter modifiers
- case clang::attr::HLSLIn:
- Out << "in ";
- break;
- case clang::attr::HLSLInOut:
- Out << "inout ";
- break;
- case clang::attr::HLSLOut:
- Out << "out ";
- break;
-
- // Interpolation modifiers
- case clang::attr::HLSLLinear:
- Out << "linear ";
- break;
- case clang::attr::HLSLCentroid:
- Out << "centroid ";
- break;
- case clang::attr::HLSLNoInterpolation:
- Out << "nointerpolation ";
- break;
- case clang::attr::HLSLNoPerspective:
- Out << "noperspective ";
- break;
- case clang::attr::HLSLSample:
- Out << "sample ";
- break;
-
- // Function attributes
- case clang::attr::HLSLClipPlanes:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLClipPlanesAttr *ACast = static_cast<HLSLClipPlanesAttr*>(noconst);
-
- if (!ACast->getClipPlane1())
- break;
-
- Indent(Indentation, Out);
- Out << "[clipplanes(";
- ACast->getClipPlane1()->printPretty(Out, 0, Policy);
- PrintClipPlaneIfPresent(ACast->getClipPlane2(), Out, Policy);
- PrintClipPlaneIfPresent(ACast->getClipPlane3(), Out, Policy);
- PrintClipPlaneIfPresent(ACast->getClipPlane4(), Out, Policy);
- PrintClipPlaneIfPresent(ACast->getClipPlane5(), Out, Policy);
- PrintClipPlaneIfPresent(ACast->getClipPlane6(), Out, Policy);
- Out << ")]\n";
-
- break;
- }
-
- case clang::attr::HLSLDomain:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLDomainAttr *ACast = static_cast<HLSLDomainAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[domain(\"" << ACast->getDomainType() << "\")]\n";
- break;
- }
-
- case clang::attr::HLSLEarlyDepthStencil:
- Indent(Indentation, Out);
- Out << "[earlydepthstencil]\n";
- break;
-
- case clang::attr::HLSLInstance: //TODO - test
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLInstanceAttr *ACast = static_cast<HLSLInstanceAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[instance(" << ACast->getCount() << ")]\n";
- break;
- }
-
- case clang::attr::HLSLMaxTessFactor: //TODO - test
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLMaxTessFactorAttr *ACast = static_cast<HLSLMaxTessFactorAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[maxtessfactor(" << ACast->getFactor() << ")]\n";
- break;
- }
-
- case clang::attr::HLSLNumThreads: //TODO - test
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLNumThreadsAttr *ACast = static_cast<HLSLNumThreadsAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[numthreads(" << ACast->getX() << ", " << ACast->getY() << ", " << ACast->getZ() << ")]\n";
- break;
- }
-
- case clang::attr::HLSLRootSignature:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLRootSignatureAttr *ACast = static_cast<HLSLRootSignatureAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[RootSignature(" << ACast->getSignatureName() << ")]\n";
- break;
- }
- case clang::attr::HLSLOutputControlPoints:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLOutputControlPointsAttr *ACast = static_cast<HLSLOutputControlPointsAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[outputcontrolpoints(" << ACast->getCount() << ")]\n";
- break;
- }
-
- case clang::attr::HLSLOutputTopology:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLOutputTopologyAttr *ACast = static_cast<HLSLOutputTopologyAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[outputtopology(\"" << ACast->getTopology() << "\")]\n";
- break;
- }
-
- case clang::attr::HLSLPartitioning:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLPartitioningAttr *ACast = static_cast<HLSLPartitioningAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[partitioning(\"" << ACast->getScheme() << "\")]\n";
- break;
- }
-
- case clang::attr::HLSLPatchConstantFunc:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLPatchConstantFuncAttr *ACast = static_cast<HLSLPatchConstantFuncAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[patchconstantfunc(\"" << ACast->getFunctionName() << "\")]\n";
- break;
- }
-
- case clang::attr::HLSLMaxVertexCount:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLMaxVertexCountAttr *ACast = static_cast<HLSLMaxVertexCountAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[maxvertexcount(" << ACast->getCount() << ")]\n";
- break;
- }
-
- // Statement attributes
- case clang::attr::HLSLAllowUAVCondition:
- Indent(Indentation, Out);
- Out << "[allow_uav_condition]\n";
- break;
-
- case clang::attr::HLSLBranch:
- Indent(Indentation, Out);
- Out << "[branch]\n";
- break;
-
- case clang::attr::HLSLCall:
- Indent(Indentation, Out);
- Out << "[call]\n";
- break;
-
- case clang::attr::HLSLFastOpt:
- Indent(Indentation, Out);
- Out << "[fastopt]\n";
- break;
-
- case clang::attr::HLSLFlatten:
- Indent(Indentation, Out);
- Out << "[flatten]\n";
- break;
-
- case clang::attr::HLSLForceCase:
- Indent(Indentation, Out);
- Out << "[forcecase]\n";
- break;
-
- case clang::attr::HLSLLoop:
- Indent(Indentation, Out);
- Out << "[loop]\n";
- break;
- case clang::attr::HLSLUnroll:
- {
- Attr * noconst = const_cast<Attr*>(A);
- HLSLUnrollAttr *ACast = static_cast<HLSLUnrollAttr*>(noconst);
- Indent(Indentation, Out);
- Out << "[unroll(" << ACast->getCount() << ")]\n";
- break;
- }
-
- // Variable modifiers
- case clang::attr::HLSLGroupShared:
- Out << "groupshared ";
- break;
-
- case clang::attr::HLSLPrecise:
- Out << "precise ";
- break;
-
- case clang::attr::HLSLSemantic: // TODO: Consider removing HLSLSemantic attribute
- break;
-
- case clang::attr::HLSLShared:
- Out << "shared ";
- break;
-
- case clang::attr::HLSLUniform:
- Out << "uniform ";
- break;
-
- // These four cases are printed in TypePrinter::printAttributedBefore
- case clang::attr::HLSLColumnMajor:
- case clang::attr::HLSLRowMajor:
- case clang::attr::HLSLSnorm:
- case clang::attr::HLSLUnorm:
- break;
- case clang::attr::HLSLPoint:
- Out << "point ";
- break;
- case clang::attr::HLSLLine:
- Out << "line ";
- break;
- case clang::attr::HLSLLineAdj:
- Out << "lineadj ";
- break;
- case clang::attr::HLSLTriangle:
- Out << "triangle ";
- break;
- case clang::attr::HLSLTriangleAdj:
- Out << "triangleadj ";
- break;
- case clang::attr::HLSLGloballyCoherent:
- Out << "globallycoherent ";
- break;
- default:
- A->printPretty(Out, Policy);
- break;
- }
- }
- bool hlsl::IsHLSLAttr(clang::attr::Kind AttrKind) {
- switch (AttrKind){
- case clang::attr::HLSLAllowUAVCondition:
- case clang::attr::HLSLBranch:
- case clang::attr::HLSLCall:
- case clang::attr::HLSLCentroid:
- case clang::attr::HLSLClipPlanes:
- case clang::attr::HLSLColumnMajor:
- case clang::attr::HLSLDomain:
- case clang::attr::HLSLEarlyDepthStencil:
- case clang::attr::HLSLFastOpt:
- case clang::attr::HLSLFlatten:
- case clang::attr::HLSLForceCase:
- case clang::attr::HLSLGroupShared:
- case clang::attr::HLSLIn:
- case clang::attr::HLSLInOut:
- case clang::attr::HLSLInstance:
- case clang::attr::HLSLLinear:
- case clang::attr::HLSLLoop:
- case clang::attr::HLSLMaxTessFactor:
- case clang::attr::HLSLNoInterpolation:
- case clang::attr::HLSLNoPerspective:
- case clang::attr::HLSLNumThreads:
- case clang::attr::HLSLRootSignature:
- case clang::attr::HLSLOut:
- case clang::attr::HLSLOutputControlPoints:
- case clang::attr::HLSLOutputTopology:
- case clang::attr::HLSLPartitioning:
- case clang::attr::HLSLPatchConstantFunc:
- case clang::attr::HLSLMaxVertexCount:
- case clang::attr::HLSLPrecise:
- case clang::attr::HLSLRowMajor:
- case clang::attr::HLSLSample:
- case clang::attr::HLSLSemantic:
- case clang::attr::HLSLShared:
- case clang::attr::HLSLSnorm:
- case clang::attr::HLSLUniform:
- case clang::attr::HLSLUnorm:
- case clang::attr::HLSLUnroll:
- case clang::attr::HLSLPoint:
- case clang::attr::HLSLLine:
- case clang::attr::HLSLLineAdj:
- case clang::attr::HLSLTriangle:
- case clang::attr::HLSLTriangleAdj:
- case clang::attr::HLSLGloballyCoherent:
- return true;
- }
-
- return false;
- }
- void hlsl::PrintClipPlaneIfPresent(clang::Expr *ClipPlane, llvm::raw_ostream &Out, const clang::PrintingPolicy &Policy) {
- if (ClipPlane) {
- Out << ", ";
- ClipPlane->printPretty(Out, 0, Policy);
- }
- }
- bool hlsl::IsObjectType(
- _In_ clang::Sema* self,
- _In_ clang::QualType type,
- _Inout_opt_ bool *isDeprecatedEffectObject)
- {
- HLSLExternalSource *pExternalSource = HLSLExternalSource::FromSema(self);
- if (pExternalSource && pExternalSource->GetTypeObjectKind(type) == AR_TOBJ_OBJECT) {
- if (isDeprecatedEffectObject)
- *isDeprecatedEffectObject = pExternalSource->GetTypeElementKind(type) == AR_OBJECT_LEGACY_EFFECT;
- return true;
- }
- if (isDeprecatedEffectObject)
- *isDeprecatedEffectObject = false;
- return false;
- }
- bool hlsl::CanConvert(
- _In_ clang::Sema* self,
- clang::SourceLocation loc,
- _In_ clang::Expr* sourceExpr,
- clang::QualType target,
- bool explicitConversion,
- _Inout_opt_ clang::StandardConversionSequence* standard)
- {
- return HLSLExternalSource::FromSema(self)->CanConvert(loc, sourceExpr, target, explicitConversion, nullptr, standard);
- }
- void hlsl::Indent(unsigned int Indentation, llvm::raw_ostream &Out)
- {
- for (unsigned i = 0; i != Indentation; ++i)
- Out << " ";
- }
- void hlsl::RegisterIntrinsicTable(_In_ clang::ExternalSemaSource* self, _In_ IDxcIntrinsicTable* table)
- {
- DXASSERT_NOMSG(self != nullptr);
- DXASSERT_NOMSG(table != nullptr);
- HLSLExternalSource* source = (HLSLExternalSource*)self;
- source->RegisterIntrinsicTable(table);
- }
- clang::QualType hlsl::CheckVectorConditional(
- _In_ clang::Sema* self,
- _In_ clang::ExprResult &Cond,
- _In_ clang::ExprResult &LHS,
- _In_ clang::ExprResult &RHS,
- _In_ clang::SourceLocation QuestionLoc)
- {
- return HLSLExternalSource::FromSema(self)->CheckVectorConditional(Cond, LHS, RHS, QuestionLoc);
- }
- clang::QualType ApplyTypeSpecSignToParsedType(
- _In_ clang::Sema* self,
- _In_ clang::QualType &type,
- _In_ clang::TypeSpecifierSign TSS,
- _In_ clang::SourceLocation Loc
- )
- {
- return HLSLExternalSource::FromSema(self)->ApplyTypeSpecSignToParsedType(type, TSS, Loc);
- }
|