12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995 |
- //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //===----------------------------------------------------------------------===/
- //
- // This file implements semantic analysis for C++ templates.
- //===----------------------------------------------------------------------===/
- #include "clang/Sema/SemaInternal.h"
- #include "clang/Sema/Lookup.h"
- #include "clang/Sema/Scope.h"
- #include "clang/Sema/Template.h"
- #include "clang/Sema/TemplateDeduction.h"
- #include "TreeTransform.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/Expr.h"
- #include "clang/AST/ExprCXX.h"
- #include "clang/AST/DeclFriend.h"
- #include "clang/AST/DeclTemplate.h"
- #include "clang/AST/RecursiveASTVisitor.h"
- #include "clang/AST/TypeVisitor.h"
- #include "clang/Sema/DeclSpec.h"
- #include "clang/Sema/ParsedTemplate.h"
- #include "clang/Basic/LangOptions.h"
- #include "clang/Basic/PartialDiagnostic.h"
- #include "llvm/ADT/SmallBitVector.h"
- #include "llvm/ADT/SmallString.h"
- #include "llvm/ADT/StringExtras.h"
- using namespace clang;
- using namespace sema;
- // Exported for use by Parser.
- SourceRange
- clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
- unsigned N) {
- if (!N) return SourceRange();
- return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
- }
- /// \brief Determine whether the declaration found is acceptable as the name
- /// of a template and, if so, return that template declaration. Otherwise,
- /// returns NULL.
- static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
- NamedDecl *Orig) {
- NamedDecl *D = Orig->getUnderlyingDecl();
- if (isa<TemplateDecl>(D))
- return Orig;
- if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
- // C++ [temp.local]p1:
- // Like normal (non-template) classes, class templates have an
- // injected-class-name (Clause 9). The injected-class-name
- // can be used with or without a template-argument-list. When
- // it is used without a template-argument-list, it is
- // equivalent to the injected-class-name followed by the
- // template-parameters of the class template enclosed in
- // <>. When it is used with a template-argument-list, it
- // refers to the specified class template specialization,
- // which could be the current specialization or another
- // specialization.
- if (Record->isInjectedClassName()) {
- Record = cast<CXXRecordDecl>(Record->getDeclContext());
- if (Record->getDescribedClassTemplate())
- return Record->getDescribedClassTemplate();
- if (ClassTemplateSpecializationDecl *Spec
- = dyn_cast<ClassTemplateSpecializationDecl>(Record))
- return Spec->getSpecializedTemplate();
- }
- return 0;
- }
- return 0;
- }
- void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
- // The set of class templates we've already seen.
- llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
- LookupResult::Filter filter = R.makeFilter();
- while (filter.hasNext()) {
- NamedDecl *Orig = filter.next();
- NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
- if (!Repl)
- filter.erase();
- else if (Repl != Orig) {
- // C++ [temp.local]p3:
- // A lookup that finds an injected-class-name (10.2) can result in an
- // ambiguity in certain cases (for example, if it is found in more than
- // one base class). If all of the injected-class-names that are found
- // refer to specializations of the same class template, and if the name
- // is used as a template-name, the reference refers to the class
- // template itself and not a specialization thereof, and is not
- // ambiguous.
- if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
- if (!ClassTemplates.insert(ClassTmpl)) {
- filter.erase();
- continue;
- }
- // FIXME: we promote access to public here as a workaround to
- // the fact that LookupResult doesn't let us remember that we
- // found this template through a particular injected class name,
- // which means we end up doing nasty things to the invariants.
- // Pretending that access is public is *much* safer.
- filter.replace(Repl, AS_public);
- }
- }
- filter.done();
- }
- bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
- for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
- if (isAcceptableTemplateName(Context, *I))
- return true;
-
- return false;
- }
- TemplateNameKind Sema::isTemplateName(Scope *S,
- CXXScopeSpec &SS,
- bool hasTemplateKeyword,
- UnqualifiedId &Name,
- ParsedType ObjectTypePtr,
- bool EnteringContext,
- TemplateTy &TemplateResult,
- bool &MemberOfUnknownSpecialization) {
- assert(getLangOptions().CPlusPlus && "No template names in C!");
- DeclarationName TName;
- MemberOfUnknownSpecialization = false;
- switch (Name.getKind()) {
- case UnqualifiedId::IK_Identifier:
- TName = DeclarationName(Name.Identifier);
- break;
- case UnqualifiedId::IK_OperatorFunctionId:
- TName = Context.DeclarationNames.getCXXOperatorName(
- Name.OperatorFunctionId.Operator);
- break;
- case UnqualifiedId::IK_LiteralOperatorId:
- TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
- break;
- default:
- return TNK_Non_template;
- }
- QualType ObjectType = ObjectTypePtr.get();
- LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
- LookupOrdinaryName);
- LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
- MemberOfUnknownSpecialization);
- if (R.empty()) return TNK_Non_template;
- if (R.isAmbiguous()) {
- // Suppress diagnostics; we'll redo this lookup later.
- R.suppressDiagnostics();
- // FIXME: we might have ambiguous templates, in which case we
- // should at least parse them properly!
- return TNK_Non_template;
- }
- TemplateName Template;
- TemplateNameKind TemplateKind;
- unsigned ResultCount = R.end() - R.begin();
- if (ResultCount > 1) {
- // We assume that we'll preserve the qualifier from a function
- // template name in other ways.
- Template = Context.getOverloadedTemplateName(R.begin(), R.end());
- TemplateKind = TNK_Function_template;
- // We'll do this lookup again later.
- R.suppressDiagnostics();
- } else {
- TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
- if (SS.isSet() && !SS.isInvalid()) {
- NestedNameSpecifier *Qualifier
- = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
- Template = Context.getQualifiedTemplateName(Qualifier,
- hasTemplateKeyword, TD);
- } else {
- Template = TemplateName(TD);
- }
- if (isa<FunctionTemplateDecl>(TD)) {
- TemplateKind = TNK_Function_template;
- // We'll do this lookup again later.
- R.suppressDiagnostics();
- } else {
- assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
- isa<TypeAliasTemplateDecl>(TD));
- TemplateKind = TNK_Type_template;
- }
- }
- TemplateResult = TemplateTy::make(Template);
- return TemplateKind;
- }
- bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
- SourceLocation IILoc,
- Scope *S,
- const CXXScopeSpec *SS,
- TemplateTy &SuggestedTemplate,
- TemplateNameKind &SuggestedKind) {
- // We can't recover unless there's a dependent scope specifier preceding the
- // template name.
- // FIXME: Typo correction?
- if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
- computeDeclContext(*SS))
- return false;
- // The code is missing a 'template' keyword prior to the dependent template
- // name.
- NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
- Diag(IILoc, diag::err_template_kw_missing)
- << Qualifier << II.getName()
- << FixItHint::CreateInsertion(IILoc, "template ");
- SuggestedTemplate
- = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
- SuggestedKind = TNK_Dependent_template_name;
- return true;
- }
- void Sema::LookupTemplateName(LookupResult &Found,
- Scope *S, CXXScopeSpec &SS,
- QualType ObjectType,
- bool EnteringContext,
- bool &MemberOfUnknownSpecialization) {
- // Determine where to perform name lookup
- MemberOfUnknownSpecialization = false;
- DeclContext *LookupCtx = 0;
- bool isDependent = false;
- if (!ObjectType.isNull()) {
- // This nested-name-specifier occurs in a member access expression, e.g.,
- // x->B::f, and we are looking into the type of the object.
- assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
- LookupCtx = computeDeclContext(ObjectType);
- isDependent = ObjectType->isDependentType();
- assert((isDependent || !ObjectType->isIncompleteType()) &&
- "Caller should have completed object type");
-
- // Template names cannot appear inside an Objective-C class or object type.
- if (ObjectType->isObjCObjectOrInterfaceType()) {
- Found.clear();
- return;
- }
- } else if (SS.isSet()) {
- // This nested-name-specifier occurs after another nested-name-specifier,
- // so long into the context associated with the prior nested-name-specifier.
- LookupCtx = computeDeclContext(SS, EnteringContext);
- isDependent = isDependentScopeSpecifier(SS);
- // The declaration context must be complete.
- if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
- return;
- }
- bool ObjectTypeSearchedInScope = false;
- if (LookupCtx) {
- // Perform "qualified" name lookup into the declaration context we
- // computed, which is either the type of the base of a member access
- // expression or the declaration context associated with a prior
- // nested-name-specifier.
- LookupQualifiedName(Found, LookupCtx);
- if (!ObjectType.isNull() && Found.empty()) {
- // C++ [basic.lookup.classref]p1:
- // In a class member access expression (5.2.5), if the . or -> token is
- // immediately followed by an identifier followed by a <, the
- // identifier must be looked up to determine whether the < is the
- // beginning of a template argument list (14.2) or a less-than operator.
- // The identifier is first looked up in the class of the object
- // expression. If the identifier is not found, it is then looked up in
- // the context of the entire postfix-expression and shall name a class
- // or function template.
- if (S) LookupName(Found, S);
- ObjectTypeSearchedInScope = true;
- }
- } else if (isDependent && (!S || ObjectType.isNull())) {
- // We cannot look into a dependent object type or nested nme
- // specifier.
- MemberOfUnknownSpecialization = true;
- return;
- } else {
- // Perform unqualified name lookup in the current scope.
- LookupName(Found, S);
- }
- if (Found.empty() && !isDependent) {
- // If we did not find any names, attempt to correct any typos.
- DeclarationName Name = Found.getLookupName();
- Found.clear();
- // Simple filter callback that, for keywords, only accepts the C++ *_cast
- CorrectionCandidateCallback FilterCCC;
- FilterCCC.WantTypeSpecifiers = false;
- FilterCCC.WantExpressionKeywords = false;
- FilterCCC.WantRemainingKeywords = false;
- FilterCCC.WantCXXNamedCasts = true;
- if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
- Found.getLookupKind(), S, &SS,
- FilterCCC, LookupCtx)) {
- Found.setLookupName(Corrected.getCorrection());
- if (Corrected.getCorrectionDecl())
- Found.addDecl(Corrected.getCorrectionDecl());
- FilterAcceptableTemplateNames(Found);
- if (!Found.empty()) {
- std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
- std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
- if (LookupCtx)
- Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
- << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
- << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
- else
- Diag(Found.getNameLoc(), diag::err_no_template_suggest)
- << Name << CorrectedQuotedStr
- << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
- if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
- Diag(Template->getLocation(), diag::note_previous_decl)
- << CorrectedQuotedStr;
- }
- } else {
- Found.setLookupName(Name);
- }
- }
- FilterAcceptableTemplateNames(Found);
- if (Found.empty()) {
- if (isDependent)
- MemberOfUnknownSpecialization = true;
- return;
- }
- if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
- // C++ [basic.lookup.classref]p1:
- // [...] If the lookup in the class of the object expression finds a
- // template, the name is also looked up in the context of the entire
- // postfix-expression and [...]
- //
- LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
- LookupOrdinaryName);
- LookupName(FoundOuter, S);
- FilterAcceptableTemplateNames(FoundOuter);
- if (FoundOuter.empty()) {
- // - if the name is not found, the name found in the class of the
- // object expression is used, otherwise
- } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
- FoundOuter.isAmbiguous()) {
- // - if the name is found in the context of the entire
- // postfix-expression and does not name a class template, the name
- // found in the class of the object expression is used, otherwise
- FoundOuter.clear();
- } else if (!Found.isSuppressingDiagnostics()) {
- // - if the name found is a class template, it must refer to the same
- // entity as the one found in the class of the object expression,
- // otherwise the program is ill-formed.
- if (!Found.isSingleResult() ||
- Found.getFoundDecl()->getCanonicalDecl()
- != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
- Diag(Found.getNameLoc(),
- diag::ext_nested_name_member_ref_lookup_ambiguous)
- << Found.getLookupName()
- << ObjectType;
- Diag(Found.getRepresentativeDecl()->getLocation(),
- diag::note_ambig_member_ref_object_type)
- << ObjectType;
- Diag(FoundOuter.getFoundDecl()->getLocation(),
- diag::note_ambig_member_ref_scope);
- // Recover by taking the template that we found in the object
- // expression's type.
- }
- }
- }
- }
- /// ActOnDependentIdExpression - Handle a dependent id-expression that
- /// was just parsed. This is only possible with an explicit scope
- /// specifier naming a dependent type.
- ExprResult
- Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
- SourceLocation TemplateKWLoc,
- const DeclarationNameInfo &NameInfo,
- bool isAddressOfOperand,
- const TemplateArgumentListInfo *TemplateArgs) {
- DeclContext *DC = getFunctionLevelDeclContext();
- if (!isAddressOfOperand &&
- isa<CXXMethodDecl>(DC) &&
- cast<CXXMethodDecl>(DC)->isInstance()) {
- QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
- // Since the 'this' expression is synthesized, we don't need to
- // perform the double-lookup check.
- NamedDecl *FirstQualifierInScope = 0;
- return Owned(CXXDependentScopeMemberExpr::Create(Context,
- /*This*/ 0, ThisType,
- /*IsArrow*/ true,
- /*Op*/ SourceLocation(),
- SS.getWithLocInContext(Context),
- TemplateKWLoc,
- FirstQualifierInScope,
- NameInfo,
- TemplateArgs));
- }
- return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
- }
- ExprResult
- Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
- SourceLocation TemplateKWLoc,
- const DeclarationNameInfo &NameInfo,
- const TemplateArgumentListInfo *TemplateArgs) {
- return Owned(DependentScopeDeclRefExpr::Create(Context,
- SS.getWithLocInContext(Context),
- TemplateKWLoc,
- NameInfo,
- TemplateArgs));
- }
- /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
- /// that the template parameter 'PrevDecl' is being shadowed by a new
- /// declaration at location Loc. Returns true to indicate that this is
- /// an error, and false otherwise.
- void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
- assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
- // Microsoft Visual C++ permits template parameters to be shadowed.
- if (getLangOptions().MicrosoftExt)
- return;
- // C++ [temp.local]p4:
- // A template-parameter shall not be redeclared within its
- // scope (including nested scopes).
- Diag(Loc, diag::err_template_param_shadow)
- << cast<NamedDecl>(PrevDecl)->getDeclName();
- Diag(PrevDecl->getLocation(), diag::note_template_param_here);
- return;
- }
- /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
- /// the parameter D to reference the templated declaration and return a pointer
- /// to the template declaration. Otherwise, do nothing to D and return null.
- TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
- if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
- D = Temp->getTemplatedDecl();
- return Temp;
- }
- return 0;
- }
- ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
- SourceLocation EllipsisLoc) const {
- assert(Kind == Template &&
- "Only template template arguments can be pack expansions here");
- assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
- "Template template argument pack expansion without packs");
- ParsedTemplateArgument Result(*this);
- Result.EllipsisLoc = EllipsisLoc;
- return Result;
- }
- static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
- const ParsedTemplateArgument &Arg) {
- switch (Arg.getKind()) {
- case ParsedTemplateArgument::Type: {
- TypeSourceInfo *DI;
- QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
- if (!DI)
- DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
- return TemplateArgumentLoc(TemplateArgument(T), DI);
- }
- case ParsedTemplateArgument::NonType: {
- Expr *E = static_cast<Expr *>(Arg.getAsExpr());
- return TemplateArgumentLoc(TemplateArgument(E), E);
- }
- case ParsedTemplateArgument::Template: {
- TemplateName Template = Arg.getAsTemplate().get();
- TemplateArgument TArg;
- if (Arg.getEllipsisLoc().isValid())
- TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
- else
- TArg = Template;
- return TemplateArgumentLoc(TArg,
- Arg.getScopeSpec().getWithLocInContext(
- SemaRef.Context),
- Arg.getLocation(),
- Arg.getEllipsisLoc());
- }
- }
- llvm_unreachable("Unhandled parsed template argument");
- }
- /// \brief Translates template arguments as provided by the parser
- /// into template arguments used by semantic analysis.
- void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
- TemplateArgumentListInfo &TemplateArgs) {
- for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
- TemplateArgs.addArgument(translateTemplateArgument(*this,
- TemplateArgsIn[I]));
- }
- /// ActOnTypeParameter - Called when a C++ template type parameter
- /// (e.g., "typename T") has been parsed. Typename specifies whether
- /// the keyword "typename" was used to declare the type parameter
- /// (otherwise, "class" was used), and KeyLoc is the location of the
- /// "class" or "typename" keyword. ParamName is the name of the
- /// parameter (NULL indicates an unnamed template parameter) and
- /// ParamNameLoc is the location of the parameter name (if any).
- /// If the type parameter has a default argument, it will be added
- /// later via ActOnTypeParameterDefault.
- Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
- SourceLocation EllipsisLoc,
- SourceLocation KeyLoc,
- IdentifierInfo *ParamName,
- SourceLocation ParamNameLoc,
- unsigned Depth, unsigned Position,
- SourceLocation EqualLoc,
- ParsedType DefaultArg) {
- assert(S->isTemplateParamScope() &&
- "Template type parameter not in template parameter scope!");
- bool Invalid = false;
- if (ParamName) {
- NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
- LookupOrdinaryName,
- ForRedeclaration);
- if (PrevDecl && PrevDecl->isTemplateParameter()) {
- DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
- PrevDecl = 0;
- }
- }
- SourceLocation Loc = ParamNameLoc;
- if (!ParamName)
- Loc = KeyLoc;
- TemplateTypeParmDecl *Param
- = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
- KeyLoc, Loc, Depth, Position, ParamName,
- Typename, Ellipsis);
- Param->setAccess(AS_public);
- if (Invalid)
- Param->setInvalidDecl();
- if (ParamName) {
- // Add the template parameter into the current scope.
- S->AddDecl(Param);
- IdResolver.AddDecl(Param);
- }
- // C++0x [temp.param]p9:
- // A default template-argument may be specified for any kind of
- // template-parameter that is not a template parameter pack.
- if (DefaultArg && Ellipsis) {
- Diag(EqualLoc, diag::err_template_param_pack_default_arg);
- DefaultArg = ParsedType();
- }
- // Handle the default argument, if provided.
- if (DefaultArg) {
- TypeSourceInfo *DefaultTInfo;
- GetTypeFromParser(DefaultArg, &DefaultTInfo);
- assert(DefaultTInfo && "expected source information for type");
- // Check for unexpanded parameter packs.
- if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
- UPPC_DefaultArgument))
- return Param;
- // Check the template argument itself.
- if (CheckTemplateArgument(Param, DefaultTInfo)) {
- Param->setInvalidDecl();
- return Param;
- }
- Param->setDefaultArgument(DefaultTInfo, false);
- }
- return Param;
- }
- /// \brief Check that the type of a non-type template parameter is
- /// well-formed.
- ///
- /// \returns the (possibly-promoted) parameter type if valid;
- /// otherwise, produces a diagnostic and returns a NULL type.
- QualType
- Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
- // We don't allow variably-modified types as the type of non-type template
- // parameters.
- if (T->isVariablyModifiedType()) {
- Diag(Loc, diag::err_variably_modified_nontype_template_param)
- << T;
- return QualType();
- }
- // C++ [temp.param]p4:
- //
- // A non-type template-parameter shall have one of the following
- // (optionally cv-qualified) types:
- //
- // -- integral or enumeration type,
- if (T->isIntegralOrEnumerationType() ||
- // -- pointer to object or pointer to function,
- T->isPointerType() ||
- // -- reference to object or reference to function,
- T->isReferenceType() ||
- // -- pointer to member,
- T->isMemberPointerType() ||
- // -- std::nullptr_t.
- T->isNullPtrType() ||
- // If T is a dependent type, we can't do the check now, so we
- // assume that it is well-formed.
- T->isDependentType())
- return T;
- // C++ [temp.param]p8:
- //
- // A non-type template-parameter of type "array of T" or
- // "function returning T" is adjusted to be of type "pointer to
- // T" or "pointer to function returning T", respectively.
- else if (T->isArrayType())
- // FIXME: Keep the type prior to promotion?
- return Context.getArrayDecayedType(T);
- else if (T->isFunctionType())
- // FIXME: Keep the type prior to promotion?
- return Context.getPointerType(T);
- Diag(Loc, diag::err_template_nontype_parm_bad_type)
- << T;
- return QualType();
- }
- Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
- unsigned Depth,
- unsigned Position,
- SourceLocation EqualLoc,
- Expr *Default) {
- TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
- QualType T = TInfo->getType();
- assert(S->isTemplateParamScope() &&
- "Non-type template parameter not in template parameter scope!");
- bool Invalid = false;
- IdentifierInfo *ParamName = D.getIdentifier();
- if (ParamName) {
- NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
- LookupOrdinaryName,
- ForRedeclaration);
- if (PrevDecl && PrevDecl->isTemplateParameter()) {
- DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
- PrevDecl = 0;
- }
- }
- T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
- if (T.isNull()) {
- T = Context.IntTy; // Recover with an 'int' type.
- Invalid = true;
- }
- bool IsParameterPack = D.hasEllipsis();
- NonTypeTemplateParmDecl *Param
- = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
- D.getSourceRange().getBegin(),
- D.getIdentifierLoc(),
- Depth, Position, ParamName, T,
- IsParameterPack, TInfo);
- Param->setAccess(AS_public);
-
- if (Invalid)
- Param->setInvalidDecl();
- if (D.getIdentifier()) {
- // Add the template parameter into the current scope.
- S->AddDecl(Param);
- IdResolver.AddDecl(Param);
- }
- // C++0x [temp.param]p9:
- // A default template-argument may be specified for any kind of
- // template-parameter that is not a template parameter pack.
- if (Default && IsParameterPack) {
- Diag(EqualLoc, diag::err_template_param_pack_default_arg);
- Default = 0;
- }
- // Check the well-formedness of the default template argument, if provided.
- if (Default) {
- // Check for unexpanded parameter packs.
- if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
- return Param;
- TemplateArgument Converted;
- ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
- if (DefaultRes.isInvalid()) {
- Param->setInvalidDecl();
- return Param;
- }
- Default = DefaultRes.take();
- Param->setDefaultArgument(Default, false);
- }
- return Param;
- }
- /// ActOnTemplateTemplateParameter - Called when a C++ template template
- /// parameter (e.g. T in template <template <typename> class T> class array)
- /// has been parsed. S is the current scope.
- Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
- SourceLocation TmpLoc,
- TemplateParameterList *Params,
- SourceLocation EllipsisLoc,
- IdentifierInfo *Name,
- SourceLocation NameLoc,
- unsigned Depth,
- unsigned Position,
- SourceLocation EqualLoc,
- ParsedTemplateArgument Default) {
- assert(S->isTemplateParamScope() &&
- "Template template parameter not in template parameter scope!");
- // Construct the parameter object.
- bool IsParameterPack = EllipsisLoc.isValid();
- TemplateTemplateParmDecl *Param =
- TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
- NameLoc.isInvalid()? TmpLoc : NameLoc,
- Depth, Position, IsParameterPack,
- Name, Params);
- Param->setAccess(AS_public);
-
- // If the template template parameter has a name, then link the identifier
- // into the scope and lookup mechanisms.
- if (Name) {
- S->AddDecl(Param);
- IdResolver.AddDecl(Param);
- }
- if (Params->size() == 0) {
- Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
- << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
- Param->setInvalidDecl();
- }
- // C++0x [temp.param]p9:
- // A default template-argument may be specified for any kind of
- // template-parameter that is not a template parameter pack.
- if (IsParameterPack && !Default.isInvalid()) {
- Diag(EqualLoc, diag::err_template_param_pack_default_arg);
- Default = ParsedTemplateArgument();
- }
- if (!Default.isInvalid()) {
- // Check only that we have a template template argument. We don't want to
- // try to check well-formedness now, because our template template parameter
- // might have dependent types in its template parameters, which we wouldn't
- // be able to match now.
- //
- // If none of the template template parameter's template arguments mention
- // other template parameters, we could actually perform more checking here.
- // However, it isn't worth doing.
- TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
- if (DefaultArg.getArgument().getAsTemplate().isNull()) {
- Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
- << DefaultArg.getSourceRange();
- return Param;
- }
- // Check for unexpanded parameter packs.
- if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
- DefaultArg.getArgument().getAsTemplate(),
- UPPC_DefaultArgument))
- return Param;
- Param->setDefaultArgument(DefaultArg, false);
- }
- return Param;
- }
- /// ActOnTemplateParameterList - Builds a TemplateParameterList that
- /// contains the template parameters in Params/NumParams.
- TemplateParameterList *
- Sema::ActOnTemplateParameterList(unsigned Depth,
- SourceLocation ExportLoc,
- SourceLocation TemplateLoc,
- SourceLocation LAngleLoc,
- Decl **Params, unsigned NumParams,
- SourceLocation RAngleLoc) {
- if (ExportLoc.isValid())
- Diag(ExportLoc, diag::warn_template_export_unsupported);
- return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
- (NamedDecl**)Params, NumParams,
- RAngleLoc);
- }
- static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
- if (SS.isSet())
- T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
- }
- DeclResult
- Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
- SourceLocation KWLoc, CXXScopeSpec &SS,
- IdentifierInfo *Name, SourceLocation NameLoc,
- AttributeList *Attr,
- TemplateParameterList *TemplateParams,
- AccessSpecifier AS, SourceLocation ModulePrivateLoc,
- unsigned NumOuterTemplateParamLists,
- TemplateParameterList** OuterTemplateParamLists) {
- assert(TemplateParams && TemplateParams->size() > 0 &&
- "No template parameters");
- assert(TUK != TUK_Reference && "Can only declare or define class templates");
- bool Invalid = false;
- // Check that we can declare a template here.
- if (CheckTemplateDeclScope(S, TemplateParams))
- return true;
- TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
- assert(Kind != TTK_Enum && "can't build template of enumerated type");
- // There is no such thing as an unnamed class template.
- if (!Name) {
- Diag(KWLoc, diag::err_template_unnamed_class);
- return true;
- }
- // Find any previous declaration with this name.
- DeclContext *SemanticContext;
- LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
- ForRedeclaration);
- if (SS.isNotEmpty() && !SS.isInvalid()) {
- SemanticContext = computeDeclContext(SS, true);
- if (!SemanticContext) {
- // FIXME: Produce a reasonable diagnostic here
- return true;
- }
- if (RequireCompleteDeclContext(SS, SemanticContext))
- return true;
- // If we're adding a template to a dependent context, we may need to
- // rebuilding some of the types used within the template parameter list,
- // now that we know what the current instantiation is.
- if (SemanticContext->isDependentContext()) {
- ContextRAII SavedContext(*this, SemanticContext);
- if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
- Invalid = true;
- }
-
- LookupQualifiedName(Previous, SemanticContext);
- } else {
- SemanticContext = CurContext;
- LookupName(Previous, S);
- }
- if (Previous.isAmbiguous())
- return true;
- NamedDecl *PrevDecl = 0;
- if (Previous.begin() != Previous.end())
- PrevDecl = (*Previous.begin())->getUnderlyingDecl();
- // If there is a previous declaration with the same name, check
- // whether this is a valid redeclaration.
- ClassTemplateDecl *PrevClassTemplate
- = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
- // We may have found the injected-class-name of a class template,
- // class template partial specialization, or class template specialization.
- // In these cases, grab the template that is being defined or specialized.
- if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
- cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
- PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
- PrevClassTemplate
- = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
- if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
- PrevClassTemplate
- = cast<ClassTemplateSpecializationDecl>(PrevDecl)
- ->getSpecializedTemplate();
- }
- }
- if (TUK == TUK_Friend) {
- // C++ [namespace.memdef]p3:
- // [...] When looking for a prior declaration of a class or a function
- // declared as a friend, and when the name of the friend class or
- // function is neither a qualified name nor a template-id, scopes outside
- // the innermost enclosing namespace scope are not considered.
- if (!SS.isSet()) {
- DeclContext *OutermostContext = CurContext;
- while (!OutermostContext->isFileContext())
- OutermostContext = OutermostContext->getLookupParent();
- if (PrevDecl &&
- (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
- OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
- SemanticContext = PrevDecl->getDeclContext();
- } else {
- // Declarations in outer scopes don't matter. However, the outermost
- // context we computed is the semantic context for our new
- // declaration.
- PrevDecl = PrevClassTemplate = 0;
- SemanticContext = OutermostContext;
- }
- }
- if (CurContext->isDependentContext()) {
- // If this is a dependent context, we don't want to link the friend
- // class template to the template in scope, because that would perform
- // checking of the template parameter lists that can't be performed
- // until the outer context is instantiated.
- PrevDecl = PrevClassTemplate = 0;
- }
- } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
- PrevDecl = PrevClassTemplate = 0;
- if (PrevClassTemplate) {
- // Ensure that the template parameter lists are compatible.
- if (!TemplateParameterListsAreEqual(TemplateParams,
- PrevClassTemplate->getTemplateParameters(),
- /*Complain=*/true,
- TPL_TemplateMatch))
- return true;
- // C++ [temp.class]p4:
- // In a redeclaration, partial specialization, explicit
- // specialization or explicit instantiation of a class template,
- // the class-key shall agree in kind with the original class
- // template declaration (7.1.5.3).
- RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
- if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
- TUK == TUK_Definition, KWLoc, *Name)) {
- Diag(KWLoc, diag::err_use_with_wrong_tag)
- << Name
- << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
- Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
- Kind = PrevRecordDecl->getTagKind();
- }
- // Check for redefinition of this class template.
- if (TUK == TUK_Definition) {
- if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
- Diag(NameLoc, diag::err_redefinition) << Name;
- Diag(Def->getLocation(), diag::note_previous_definition);
- // FIXME: Would it make sense to try to "forget" the previous
- // definition, as part of error recovery?
- return true;
- }
- }
- } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
- // Maybe we will complain about the shadowed template parameter.
- DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
- // Just pretend that we didn't see the previous declaration.
- PrevDecl = 0;
- } else if (PrevDecl) {
- // C++ [temp]p5:
- // A class template shall not have the same name as any other
- // template, class, function, object, enumeration, enumerator,
- // namespace, or type in the same scope (3.3), except as specified
- // in (14.5.4).
- Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
- Diag(PrevDecl->getLocation(), diag::note_previous_definition);
- return true;
- }
- // Check the template parameter list of this declaration, possibly
- // merging in the template parameter list from the previous class
- // template declaration.
- if (CheckTemplateParameterList(TemplateParams,
- PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
- (SS.isSet() && SemanticContext &&
- SemanticContext->isRecord() &&
- SemanticContext->isDependentContext())
- ? TPC_ClassTemplateMember
- : TPC_ClassTemplate))
- Invalid = true;
- if (SS.isSet()) {
- // If the name of the template was qualified, we must be defining the
- // template out-of-line.
- if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
- !(TUK == TUK_Friend && CurContext->isDependentContext())) {
- Diag(NameLoc, diag::err_member_def_does_not_match)
- << Name << SemanticContext << SS.getRange();
- Invalid = true;
- }
- }
- CXXRecordDecl *NewClass =
- CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
- PrevClassTemplate?
- PrevClassTemplate->getTemplatedDecl() : 0,
- /*DelayTypeCreation=*/true);
- SetNestedNameSpecifier(NewClass, SS);
- if (NumOuterTemplateParamLists > 0)
- NewClass->setTemplateParameterListsInfo(Context,
- NumOuterTemplateParamLists,
- OuterTemplateParamLists);
- ClassTemplateDecl *NewTemplate
- = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
- DeclarationName(Name), TemplateParams,
- NewClass, PrevClassTemplate);
- NewClass->setDescribedClassTemplate(NewTemplate);
-
- if (ModulePrivateLoc.isValid())
- NewTemplate->setModulePrivate();
-
- // Build the type for the class template declaration now.
- QualType T = NewTemplate->getInjectedClassNameSpecialization();
- T = Context.getInjectedClassNameType(NewClass, T);
- assert(T->isDependentType() && "Class template type is not dependent?");
- (void)T;
- // If we are providing an explicit specialization of a member that is a
- // class template, make a note of that.
- if (PrevClassTemplate &&
- PrevClassTemplate->getInstantiatedFromMemberTemplate())
- PrevClassTemplate->setMemberSpecialization();
- // Set the access specifier.
- if (!Invalid && TUK != TUK_Friend)
- SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
- // Set the lexical context of these templates
- NewClass->setLexicalDeclContext(CurContext);
- NewTemplate->setLexicalDeclContext(CurContext);
- if (TUK == TUK_Definition)
- NewClass->startDefinition();
- if (Attr)
- ProcessDeclAttributeList(S, NewClass, Attr);
- if (TUK != TUK_Friend)
- PushOnScopeChains(NewTemplate, S);
- else {
- if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
- NewTemplate->setAccess(PrevClassTemplate->getAccess());
- NewClass->setAccess(PrevClassTemplate->getAccess());
- }
- NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
- PrevClassTemplate != NULL);
- // Friend templates are visible in fairly strange ways.
- if (!CurContext->isDependentContext()) {
- DeclContext *DC = SemanticContext->getRedeclContext();
- DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
- if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
- PushOnScopeChains(NewTemplate, EnclosingScope,
- /* AddToContext = */ false);
- }
- FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
- NewClass->getLocation(),
- NewTemplate,
- /*FIXME:*/NewClass->getLocation());
- Friend->setAccess(AS_public);
- CurContext->addDecl(Friend);
- }
- if (Invalid) {
- NewTemplate->setInvalidDecl();
- NewClass->setInvalidDecl();
- }
- return NewTemplate;
- }
- /// \brief Diagnose the presence of a default template argument on a
- /// template parameter, which is ill-formed in certain contexts.
- ///
- /// \returns true if the default template argument should be dropped.
- static bool DiagnoseDefaultTemplateArgument(Sema &S,
- Sema::TemplateParamListContext TPC,
- SourceLocation ParamLoc,
- SourceRange DefArgRange) {
- switch (TPC) {
- case Sema::TPC_ClassTemplate:
- case Sema::TPC_TypeAliasTemplate:
- return false;
- case Sema::TPC_FunctionTemplate:
- case Sema::TPC_FriendFunctionTemplateDefinition:
- // C++ [temp.param]p9:
- // A default template-argument shall not be specified in a
- // function template declaration or a function template
- // definition [...]
- // If a friend function template declaration specifies a default
- // template-argument, that declaration shall be a definition and shall be
- // the only declaration of the function template in the translation unit.
- // (C++98/03 doesn't have this wording; see DR226).
- S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_template_parameter_default_in_function_template
- : diag::ext_template_parameter_default_in_function_template)
- << DefArgRange;
- return false;
- case Sema::TPC_ClassTemplateMember:
- // C++0x [temp.param]p9:
- // A default template-argument shall not be specified in the
- // template-parameter-lists of the definition of a member of a
- // class template that appears outside of the member's class.
- S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
- << DefArgRange;
- return true;
- case Sema::TPC_FriendFunctionTemplate:
- // C++ [temp.param]p9:
- // A default template-argument shall not be specified in a
- // friend template declaration.
- S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
- << DefArgRange;
- return true;
- // FIXME: C++0x [temp.param]p9 allows default template-arguments
- // for friend function templates if there is only a single
- // declaration (and it is a definition). Strange!
- }
- llvm_unreachable("Invalid TemplateParamListContext!");
- }
- /// \brief Check for unexpanded parameter packs within the template parameters
- /// of a template template parameter, recursively.
- static bool DiagnoseUnexpandedParameterPacks(Sema &S,
- TemplateTemplateParmDecl *TTP) {
- TemplateParameterList *Params = TTP->getTemplateParameters();
- for (unsigned I = 0, N = Params->size(); I != N; ++I) {
- NamedDecl *P = Params->getParam(I);
- if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
- if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
- NTTP->getTypeSourceInfo(),
- Sema::UPPC_NonTypeTemplateParameterType))
- return true;
- continue;
- }
- if (TemplateTemplateParmDecl *InnerTTP
- = dyn_cast<TemplateTemplateParmDecl>(P))
- if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
- return true;
- }
- return false;
- }
- /// \brief Checks the validity of a template parameter list, possibly
- /// considering the template parameter list from a previous
- /// declaration.
- ///
- /// If an "old" template parameter list is provided, it must be
- /// equivalent (per TemplateParameterListsAreEqual) to the "new"
- /// template parameter list.
- ///
- /// \param NewParams Template parameter list for a new template
- /// declaration. This template parameter list will be updated with any
- /// default arguments that are carried through from the previous
- /// template parameter list.
- ///
- /// \param OldParams If provided, template parameter list from a
- /// previous declaration of the same template. Default template
- /// arguments will be merged from the old template parameter list to
- /// the new template parameter list.
- ///
- /// \param TPC Describes the context in which we are checking the given
- /// template parameter list.
- ///
- /// \returns true if an error occurred, false otherwise.
- bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
- TemplateParameterList *OldParams,
- TemplateParamListContext TPC) {
- bool Invalid = false;
- // C++ [temp.param]p10:
- // The set of default template-arguments available for use with a
- // template declaration or definition is obtained by merging the
- // default arguments from the definition (if in scope) and all
- // declarations in scope in the same way default function
- // arguments are (8.3.6).
- bool SawDefaultArgument = false;
- SourceLocation PreviousDefaultArgLoc;
- // Dummy initialization to avoid warnings.
- TemplateParameterList::iterator OldParam = NewParams->end();
- if (OldParams)
- OldParam = OldParams->begin();
- bool RemoveDefaultArguments = false;
- for (TemplateParameterList::iterator NewParam = NewParams->begin(),
- NewParamEnd = NewParams->end();
- NewParam != NewParamEnd; ++NewParam) {
- // Variables used to diagnose redundant default arguments
- bool RedundantDefaultArg = false;
- SourceLocation OldDefaultLoc;
- SourceLocation NewDefaultLoc;
- // Variable used to diagnose missing default arguments
- bool MissingDefaultArg = false;
- // Variable used to diagnose non-final parameter packs
- bool SawParameterPack = false;
- if (TemplateTypeParmDecl *NewTypeParm
- = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
- // Check the presence of a default argument here.
- if (NewTypeParm->hasDefaultArgument() &&
- DiagnoseDefaultTemplateArgument(*this, TPC,
- NewTypeParm->getLocation(),
- NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
- .getSourceRange()))
- NewTypeParm->removeDefaultArgument();
- // Merge default arguments for template type parameters.
- TemplateTypeParmDecl *OldTypeParm
- = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
- if (NewTypeParm->isParameterPack()) {
- assert(!NewTypeParm->hasDefaultArgument() &&
- "Parameter packs can't have a default argument!");
- SawParameterPack = true;
- } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
- NewTypeParm->hasDefaultArgument()) {
- OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
- NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
- SawDefaultArgument = true;
- RedundantDefaultArg = true;
- PreviousDefaultArgLoc = NewDefaultLoc;
- } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
- // Merge the default argument from the old declaration to the
- // new declaration.
- SawDefaultArgument = true;
- NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
- true);
- PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
- } else if (NewTypeParm->hasDefaultArgument()) {
- SawDefaultArgument = true;
- PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
- } else if (SawDefaultArgument)
- MissingDefaultArg = true;
- } else if (NonTypeTemplateParmDecl *NewNonTypeParm
- = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
- // Check for unexpanded parameter packs.
- if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
- NewNonTypeParm->getTypeSourceInfo(),
- UPPC_NonTypeTemplateParameterType)) {
- Invalid = true;
- continue;
- }
- // Check the presence of a default argument here.
- if (NewNonTypeParm->hasDefaultArgument() &&
- DiagnoseDefaultTemplateArgument(*this, TPC,
- NewNonTypeParm->getLocation(),
- NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
- NewNonTypeParm->removeDefaultArgument();
- }
- // Merge default arguments for non-type template parameters
- NonTypeTemplateParmDecl *OldNonTypeParm
- = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
- if (NewNonTypeParm->isParameterPack()) {
- assert(!NewNonTypeParm->hasDefaultArgument() &&
- "Parameter packs can't have a default argument!");
- SawParameterPack = true;
- } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
- NewNonTypeParm->hasDefaultArgument()) {
- OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
- NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
- SawDefaultArgument = true;
- RedundantDefaultArg = true;
- PreviousDefaultArgLoc = NewDefaultLoc;
- } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
- // Merge the default argument from the old declaration to the
- // new declaration.
- SawDefaultArgument = true;
- // FIXME: We need to create a new kind of "default argument"
- // expression that points to a previous non-type template
- // parameter.
- NewNonTypeParm->setDefaultArgument(
- OldNonTypeParm->getDefaultArgument(),
- /*Inherited=*/ true);
- PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
- } else if (NewNonTypeParm->hasDefaultArgument()) {
- SawDefaultArgument = true;
- PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
- } else if (SawDefaultArgument)
- MissingDefaultArg = true;
- } else {
- TemplateTemplateParmDecl *NewTemplateParm
- = cast<TemplateTemplateParmDecl>(*NewParam);
- // Check for unexpanded parameter packs, recursively.
- if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
- Invalid = true;
- continue;
- }
- // Check the presence of a default argument here.
- if (NewTemplateParm->hasDefaultArgument() &&
- DiagnoseDefaultTemplateArgument(*this, TPC,
- NewTemplateParm->getLocation(),
- NewTemplateParm->getDefaultArgument().getSourceRange()))
- NewTemplateParm->removeDefaultArgument();
- // Merge default arguments for template template parameters
- TemplateTemplateParmDecl *OldTemplateParm
- = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
- if (NewTemplateParm->isParameterPack()) {
- assert(!NewTemplateParm->hasDefaultArgument() &&
- "Parameter packs can't have a default argument!");
- SawParameterPack = true;
- } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
- NewTemplateParm->hasDefaultArgument()) {
- OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
- NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
- SawDefaultArgument = true;
- RedundantDefaultArg = true;
- PreviousDefaultArgLoc = NewDefaultLoc;
- } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
- // Merge the default argument from the old declaration to the
- // new declaration.
- SawDefaultArgument = true;
- // FIXME: We need to create a new kind of "default argument" expression
- // that points to a previous template template parameter.
- NewTemplateParm->setDefaultArgument(
- OldTemplateParm->getDefaultArgument(),
- /*Inherited=*/ true);
- PreviousDefaultArgLoc
- = OldTemplateParm->getDefaultArgument().getLocation();
- } else if (NewTemplateParm->hasDefaultArgument()) {
- SawDefaultArgument = true;
- PreviousDefaultArgLoc
- = NewTemplateParm->getDefaultArgument().getLocation();
- } else if (SawDefaultArgument)
- MissingDefaultArg = true;
- }
- // C++0x [temp.param]p11:
- // If a template parameter of a primary class template or alias template
- // is a template parameter pack, it shall be the last template parameter.
- if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
- (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
- Diag((*NewParam)->getLocation(),
- diag::err_template_param_pack_must_be_last_template_parameter);
- Invalid = true;
- }
- if (RedundantDefaultArg) {
- // C++ [temp.param]p12:
- // A template-parameter shall not be given default arguments
- // by two different declarations in the same scope.
- Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
- Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
- Invalid = true;
- } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
- // C++ [temp.param]p11:
- // If a template-parameter of a class template has a default
- // template-argument, each subsequent template-parameter shall either
- // have a default template-argument supplied or be a template parameter
- // pack.
- Diag((*NewParam)->getLocation(),
- diag::err_template_param_default_arg_missing);
- Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
- Invalid = true;
- RemoveDefaultArguments = true;
- }
- // If we have an old template parameter list that we're merging
- // in, move on to the next parameter.
- if (OldParams)
- ++OldParam;
- }
- // We were missing some default arguments at the end of the list, so remove
- // all of the default arguments.
- if (RemoveDefaultArguments) {
- for (TemplateParameterList::iterator NewParam = NewParams->begin(),
- NewParamEnd = NewParams->end();
- NewParam != NewParamEnd; ++NewParam) {
- if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
- TTP->removeDefaultArgument();
- else if (NonTypeTemplateParmDecl *NTTP
- = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
- NTTP->removeDefaultArgument();
- else
- cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
- }
- }
- return Invalid;
- }
- namespace {
- /// A class which looks for a use of a certain level of template
- /// parameter.
- struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
- typedef RecursiveASTVisitor<DependencyChecker> super;
- unsigned Depth;
- bool Match;
- DependencyChecker(TemplateParameterList *Params) : Match(false) {
- NamedDecl *ND = Params->getParam(0);
- if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
- Depth = PD->getDepth();
- } else if (NonTypeTemplateParmDecl *PD =
- dyn_cast<NonTypeTemplateParmDecl>(ND)) {
- Depth = PD->getDepth();
- } else {
- Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
- }
- }
- bool Matches(unsigned ParmDepth) {
- if (ParmDepth >= Depth) {
- Match = true;
- return true;
- }
- return false;
- }
- bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
- return !Matches(T->getDepth());
- }
- bool TraverseTemplateName(TemplateName N) {
- if (TemplateTemplateParmDecl *PD =
- dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
- if (Matches(PD->getDepth())) return false;
- return super::TraverseTemplateName(N);
- }
- bool VisitDeclRefExpr(DeclRefExpr *E) {
- if (NonTypeTemplateParmDecl *PD =
- dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
- if (PD->getDepth() == Depth) {
- Match = true;
- return false;
- }
- }
- return super::VisitDeclRefExpr(E);
- }
-
- bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
- return TraverseType(T->getInjectedSpecializationType());
- }
- };
- }
- /// Determines whether a given type depends on the given parameter
- /// list.
- static bool
- DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
- DependencyChecker Checker(Params);
- Checker.TraverseType(T);
- return Checker.Match;
- }
- // Find the source range corresponding to the named type in the given
- // nested-name-specifier, if any.
- static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
- QualType T,
- const CXXScopeSpec &SS) {
- NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
- while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
- if (const Type *CurType = NNS->getAsType()) {
- if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
- return NNSLoc.getTypeLoc().getSourceRange();
- } else
- break;
-
- NNSLoc = NNSLoc.getPrefix();
- }
-
- return SourceRange();
- }
- /// \brief Match the given template parameter lists to the given scope
- /// specifier, returning the template parameter list that applies to the
- /// name.
- ///
- /// \param DeclStartLoc the start of the declaration that has a scope
- /// specifier or a template parameter list.
- ///
- /// \param DeclLoc The location of the declaration itself.
- ///
- /// \param SS the scope specifier that will be matched to the given template
- /// parameter lists. This scope specifier precedes a qualified name that is
- /// being declared.
- ///
- /// \param ParamLists the template parameter lists, from the outermost to the
- /// innermost template parameter lists.
- ///
- /// \param NumParamLists the number of template parameter lists in ParamLists.
- ///
- /// \param IsFriend Whether to apply the slightly different rules for
- /// matching template parameters to scope specifiers in friend
- /// declarations.
- ///
- /// \param IsExplicitSpecialization will be set true if the entity being
- /// declared is an explicit specialization, false otherwise.
- ///
- /// \returns the template parameter list, if any, that corresponds to the
- /// name that is preceded by the scope specifier @p SS. This template
- /// parameter list may have template parameters (if we're declaring a
- /// template) or may have no template parameters (if we're declaring a
- /// template specialization), or may be NULL (if what we're declaring isn't
- /// itself a template).
- TemplateParameterList *
- Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
- SourceLocation DeclLoc,
- const CXXScopeSpec &SS,
- TemplateParameterList **ParamLists,
- unsigned NumParamLists,
- bool IsFriend,
- bool &IsExplicitSpecialization,
- bool &Invalid) {
- IsExplicitSpecialization = false;
- Invalid = false;
-
- // The sequence of nested types to which we will match up the template
- // parameter lists. We first build this list by starting with the type named
- // by the nested-name-specifier and walking out until we run out of types.
- SmallVector<QualType, 4> NestedTypes;
- QualType T;
- if (SS.getScopeRep()) {
- if (CXXRecordDecl *Record
- = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
- T = Context.getTypeDeclType(Record);
- else
- T = QualType(SS.getScopeRep()->getAsType(), 0);
- }
-
- // If we found an explicit specialization that prevents us from needing
- // 'template<>' headers, this will be set to the location of that
- // explicit specialization.
- SourceLocation ExplicitSpecLoc;
-
- while (!T.isNull()) {
- NestedTypes.push_back(T);
-
- // Retrieve the parent of a record type.
- if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
- // If this type is an explicit specialization, we're done.
- if (ClassTemplateSpecializationDecl *Spec
- = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
- if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
- Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
- ExplicitSpecLoc = Spec->getLocation();
- break;
- }
- } else if (Record->getTemplateSpecializationKind()
- == TSK_ExplicitSpecialization) {
- ExplicitSpecLoc = Record->getLocation();
- break;
- }
-
- if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
- T = Context.getTypeDeclType(Parent);
- else
- T = QualType();
- continue;
- }
-
- if (const TemplateSpecializationType *TST
- = T->getAs<TemplateSpecializationType>()) {
- if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
- if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
- T = Context.getTypeDeclType(Parent);
- else
- T = QualType();
- continue;
- }
- }
-
- // Look one step prior in a dependent template specialization type.
- if (const DependentTemplateSpecializationType *DependentTST
- = T->getAs<DependentTemplateSpecializationType>()) {
- if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
- T = QualType(NNS->getAsType(), 0);
- else
- T = QualType();
- continue;
- }
-
- // Look one step prior in a dependent name type.
- if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
- if (NestedNameSpecifier *NNS = DependentName->getQualifier())
- T = QualType(NNS->getAsType(), 0);
- else
- T = QualType();
- continue;
- }
-
- // Retrieve the parent of an enumeration type.
- if (const EnumType *EnumT = T->getAs<EnumType>()) {
- // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
- // check here.
- EnumDecl *Enum = EnumT->getDecl();
-
- // Get to the parent type.
- if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
- T = Context.getTypeDeclType(Parent);
- else
- T = QualType();
- continue;
- }
- T = QualType();
- }
- // Reverse the nested types list, since we want to traverse from the outermost
- // to the innermost while checking template-parameter-lists.
- std::reverse(NestedTypes.begin(), NestedTypes.end());
- // C++0x [temp.expl.spec]p17:
- // A member or a member template may be nested within many
- // enclosing class templates. In an explicit specialization for
- // such a member, the member declaration shall be preceded by a
- // template<> for each enclosing class template that is
- // explicitly specialized.
- bool SawNonEmptyTemplateParameterList = false;
- unsigned ParamIdx = 0;
- for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
- ++TypeIdx) {
- T = NestedTypes[TypeIdx];
-
- // Whether we expect a 'template<>' header.
- bool NeedEmptyTemplateHeader = false;
- // Whether we expect a template header with parameters.
- bool NeedNonemptyTemplateHeader = false;
-
- // For a dependent type, the set of template parameters that we
- // expect to see.
- TemplateParameterList *ExpectedTemplateParams = 0;
- // C++0x [temp.expl.spec]p15:
- // A member or a member template may be nested within many enclosing
- // class templates. In an explicit specialization for such a member, the
- // member declaration shall be preceded by a template<> for each
- // enclosing class template that is explicitly specialized.
- if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
- if (ClassTemplatePartialSpecializationDecl *Partial
- = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
- ExpectedTemplateParams = Partial->getTemplateParameters();
- NeedNonemptyTemplateHeader = true;
- } else if (Record->isDependentType()) {
- if (Record->getDescribedClassTemplate()) {
- ExpectedTemplateParams = Record->getDescribedClassTemplate()
- ->getTemplateParameters();
- NeedNonemptyTemplateHeader = true;
- }
- } else if (ClassTemplateSpecializationDecl *Spec
- = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
- // C++0x [temp.expl.spec]p4:
- // Members of an explicitly specialized class template are defined
- // in the same manner as members of normal classes, and not using
- // the template<> syntax.
- if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
- NeedEmptyTemplateHeader = true;
- else
- continue;
- } else if (Record->getTemplateSpecializationKind()) {
- if (Record->getTemplateSpecializationKind()
- != TSK_ExplicitSpecialization &&
- TypeIdx == NumTypes - 1)
- IsExplicitSpecialization = true;
-
- continue;
- }
- } else if (const TemplateSpecializationType *TST
- = T->getAs<TemplateSpecializationType>()) {
- if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
- ExpectedTemplateParams = Template->getTemplateParameters();
- NeedNonemptyTemplateHeader = true;
- }
- } else if (T->getAs<DependentTemplateSpecializationType>()) {
- // FIXME: We actually could/should check the template arguments here
- // against the corresponding template parameter list.
- NeedNonemptyTemplateHeader = false;
- }
-
- // C++ [temp.expl.spec]p16:
- // In an explicit specialization declaration for a member of a class
- // template or a member template that ap- pears in namespace scope, the
- // member template and some of its enclosing class templates may remain
- // unspecialized, except that the declaration shall not explicitly
- // specialize a class member template if its en- closing class templates
- // are not explicitly specialized as well.
- if (ParamIdx < NumParamLists) {
- if (ParamLists[ParamIdx]->size() == 0) {
- if (SawNonEmptyTemplateParameterList) {
- Diag(DeclLoc, diag::err_specialize_member_of_template)
- << ParamLists[ParamIdx]->getSourceRange();
- Invalid = true;
- IsExplicitSpecialization = false;
- return 0;
- }
- } else
- SawNonEmptyTemplateParameterList = true;
- }
-
- if (NeedEmptyTemplateHeader) {
- // If we're on the last of the types, and we need a 'template<>' header
- // here, then it's an explicit specialization.
- if (TypeIdx == NumTypes - 1)
- IsExplicitSpecialization = true;
-
- if (ParamIdx < NumParamLists) {
- if (ParamLists[ParamIdx]->size() > 0) {
- // The header has template parameters when it shouldn't. Complain.
- Diag(ParamLists[ParamIdx]->getTemplateLoc(),
- diag::err_template_param_list_matches_nontemplate)
- << T
- << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
- ParamLists[ParamIdx]->getRAngleLoc())
- << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
- Invalid = true;
- return 0;
- }
-
- // Consume this template header.
- ++ParamIdx;
- continue;
- }
-
- if (!IsFriend) {
- // We don't have a template header, but we should.
- SourceLocation ExpectedTemplateLoc;
- if (NumParamLists > 0)
- ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
- else
- ExpectedTemplateLoc = DeclStartLoc;
- Diag(DeclLoc, diag::err_template_spec_needs_header)
- << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
- << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
- }
-
- continue;
- }
-
- if (NeedNonemptyTemplateHeader) {
- // In friend declarations we can have template-ids which don't
- // depend on the corresponding template parameter lists. But
- // assume that empty parameter lists are supposed to match this
- // template-id.
- if (IsFriend && T->isDependentType()) {
- if (ParamIdx < NumParamLists &&
- DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
- ExpectedTemplateParams = 0;
- else
- continue;
- }
- if (ParamIdx < NumParamLists) {
- // Check the template parameter list, if we can.
- if (ExpectedTemplateParams &&
- !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
- ExpectedTemplateParams,
- true, TPL_TemplateMatch))
- Invalid = true;
-
- if (!Invalid &&
- CheckTemplateParameterList(ParamLists[ParamIdx], 0,
- TPC_ClassTemplateMember))
- Invalid = true;
-
- ++ParamIdx;
- continue;
- }
-
- Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
- << T
- << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
- Invalid = true;
- continue;
- }
- }
-
- // If there were at least as many template-ids as there were template
- // parameter lists, then there are no template parameter lists remaining for
- // the declaration itself.
- if (ParamIdx >= NumParamLists)
- return 0;
- // If there were too many template parameter lists, complain about that now.
- if (ParamIdx < NumParamLists - 1) {
- bool HasAnyExplicitSpecHeader = false;
- bool AllExplicitSpecHeaders = true;
- for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
- if (ParamLists[I]->size() == 0)
- HasAnyExplicitSpecHeader = true;
- else
- AllExplicitSpecHeaders = false;
- }
-
- Diag(ParamLists[ParamIdx]->getTemplateLoc(),
- AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
- : diag::err_template_spec_extra_headers)
- << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
- ParamLists[NumParamLists - 2]->getRAngleLoc());
- // If there was a specialization somewhere, such that 'template<>' is
- // not required, and there were any 'template<>' headers, note where the
- // specialization occurred.
- if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
- Diag(ExplicitSpecLoc,
- diag::note_explicit_template_spec_does_not_need_header)
- << NestedTypes.back();
-
- // We have a template parameter list with no corresponding scope, which
- // means that the resulting template declaration can't be instantiated
- // properly (we'll end up with dependent nodes when we shouldn't).
- if (!AllExplicitSpecHeaders)
- Invalid = true;
- }
- // C++ [temp.expl.spec]p16:
- // In an explicit specialization declaration for a member of a class
- // template or a member template that ap- pears in namespace scope, the
- // member template and some of its enclosing class templates may remain
- // unspecialized, except that the declaration shall not explicitly
- // specialize a class member template if its en- closing class templates
- // are not explicitly specialized as well.
- if (ParamLists[NumParamLists - 1]->size() == 0 &&
- SawNonEmptyTemplateParameterList) {
- Diag(DeclLoc, diag::err_specialize_member_of_template)
- << ParamLists[ParamIdx]->getSourceRange();
- Invalid = true;
- IsExplicitSpecialization = false;
- return 0;
- }
-
- // Return the last template parameter list, which corresponds to the
- // entity being declared.
- return ParamLists[NumParamLists - 1];
- }
- void Sema::NoteAllFoundTemplates(TemplateName Name) {
- if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
- Diag(Template->getLocation(), diag::note_template_declared_here)
- << (isa<FunctionTemplateDecl>(Template)? 0
- : isa<ClassTemplateDecl>(Template)? 1
- : isa<TypeAliasTemplateDecl>(Template)? 2
- : 3)
- << Template->getDeclName();
- return;
- }
-
- if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
- for (OverloadedTemplateStorage::iterator I = OST->begin(),
- IEnd = OST->end();
- I != IEnd; ++I)
- Diag((*I)->getLocation(), diag::note_template_declared_here)
- << 0 << (*I)->getDeclName();
-
- return;
- }
- }
- QualType Sema::CheckTemplateIdType(TemplateName Name,
- SourceLocation TemplateLoc,
- TemplateArgumentListInfo &TemplateArgs) {
- DependentTemplateName *DTN
- = Name.getUnderlying().getAsDependentTemplateName();
- if (DTN && DTN->isIdentifier())
- // When building a template-id where the template-name is dependent,
- // assume the template is a type template. Either our assumption is
- // correct, or the code is ill-formed and will be diagnosed when the
- // dependent name is substituted.
- return Context.getDependentTemplateSpecializationType(ETK_None,
- DTN->getQualifier(),
- DTN->getIdentifier(),
- TemplateArgs);
- TemplateDecl *Template = Name.getAsTemplateDecl();
- if (!Template || isa<FunctionTemplateDecl>(Template)) {
- // We might have a substituted template template parameter pack. If so,
- // build a template specialization type for it.
- if (Name.getAsSubstTemplateTemplateParmPack())
- return Context.getTemplateSpecializationType(Name, TemplateArgs);
- Diag(TemplateLoc, diag::err_template_id_not_a_type)
- << Name;
- NoteAllFoundTemplates(Name);
- return QualType();
- }
- // Check that the template argument list is well-formed for this
- // template.
- SmallVector<TemplateArgument, 4> Converted;
- bool ExpansionIntoFixedList = false;
- if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
- false, Converted, &ExpansionIntoFixedList))
- return QualType();
- QualType CanonType;
- bool InstantiationDependent = false;
- TypeAliasTemplateDecl *AliasTemplate = 0;
- if (!ExpansionIntoFixedList &&
- (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) {
- // Find the canonical type for this type alias template specialization.
- TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
- if (Pattern->isInvalidDecl())
- return QualType();
- TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
- Converted.data(), Converted.size());
- // Only substitute for the innermost template argument list.
- MultiLevelTemplateArgumentList TemplateArgLists;
- TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
- unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
- for (unsigned I = 0; I < Depth; ++I)
- TemplateArgLists.addOuterTemplateArguments(0, 0);
- InstantiatingTemplate Inst(*this, TemplateLoc, Template);
- CanonType = SubstType(Pattern->getUnderlyingType(),
- TemplateArgLists, AliasTemplate->getLocation(),
- AliasTemplate->getDeclName());
- if (CanonType.isNull())
- return QualType();
- } else if (Name.isDependent() ||
- TemplateSpecializationType::anyDependentTemplateArguments(
- TemplateArgs, InstantiationDependent)) {
- // This class template specialization is a dependent
- // type. Therefore, its canonical type is another class template
- // specialization type that contains all of the converted
- // arguments in canonical form. This ensures that, e.g., A<T> and
- // A<T, T> have identical types when A is declared as:
- //
- // template<typename T, typename U = T> struct A;
- TemplateName CanonName = Context.getCanonicalTemplateName(Name);
- CanonType = Context.getTemplateSpecializationType(CanonName,
- Converted.data(),
- Converted.size());
- // FIXME: CanonType is not actually the canonical type, and unfortunately
- // it is a TemplateSpecializationType that we will never use again.
- // In the future, we need to teach getTemplateSpecializationType to only
- // build the canonical type and return that to us.
- CanonType = Context.getCanonicalType(CanonType);
- // This might work out to be a current instantiation, in which
- // case the canonical type needs to be the InjectedClassNameType.
- //
- // TODO: in theory this could be a simple hashtable lookup; most
- // changes to CurContext don't change the set of current
- // instantiations.
- if (isa<ClassTemplateDecl>(Template)) {
- for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
- // If we get out to a namespace, we're done.
- if (Ctx->isFileContext()) break;
- // If this isn't a record, keep looking.
- CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
- if (!Record) continue;
- // Look for one of the two cases with InjectedClassNameTypes
- // and check whether it's the same template.
- if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
- !Record->getDescribedClassTemplate())
- continue;
- // Fetch the injected class name type and check whether its
- // injected type is equal to the type we just built.
- QualType ICNT = Context.getTypeDeclType(Record);
- QualType Injected = cast<InjectedClassNameType>(ICNT)
- ->getInjectedSpecializationType();
- if (CanonType != Injected->getCanonicalTypeInternal())
- continue;
- // If so, the canonical type of this TST is the injected
- // class name type of the record we just found.
- assert(ICNT.isCanonical());
- CanonType = ICNT;
- break;
- }
- }
- } else if (ClassTemplateDecl *ClassTemplate
- = dyn_cast<ClassTemplateDecl>(Template)) {
- // Find the class template specialization declaration that
- // corresponds to these arguments.
- void *InsertPos = 0;
- ClassTemplateSpecializationDecl *Decl
- = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
- InsertPos);
- if (!Decl) {
- // This is the first time we have referenced this class template
- // specialization. Create the canonical declaration and add it to
- // the set of specializations.
- Decl = ClassTemplateSpecializationDecl::Create(Context,
- ClassTemplate->getTemplatedDecl()->getTagKind(),
- ClassTemplate->getDeclContext(),
- ClassTemplate->getTemplatedDecl()->getLocStart(),
- ClassTemplate->getLocation(),
- ClassTemplate,
- Converted.data(),
- Converted.size(), 0);
- ClassTemplate->AddSpecialization(Decl, InsertPos);
- Decl->setLexicalDeclContext(CurContext);
- }
- CanonType = Context.getTypeDeclType(Decl);
- assert(isa<RecordType>(CanonType) &&
- "type of non-dependent specialization is not a RecordType");
- }
- // Build the fully-sugared type for this class template
- // specialization, which refers back to the class template
- // specialization we created or found.
- return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
- }
- TypeResult
- Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
- TemplateTy TemplateD, SourceLocation TemplateLoc,
- SourceLocation LAngleLoc,
- ASTTemplateArgsPtr TemplateArgsIn,
- SourceLocation RAngleLoc,
- bool IsCtorOrDtorName) {
- if (SS.isInvalid())
- return true;
- TemplateName Template = TemplateD.getAsVal<TemplateName>();
- // Translate the parser's template argument list in our AST format.
- TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
- translateTemplateArguments(TemplateArgsIn, TemplateArgs);
- if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
- QualType T
- = Context.getDependentTemplateSpecializationType(ETK_None,
- DTN->getQualifier(),
- DTN->getIdentifier(),
- TemplateArgs);
- // Build type-source information.
- TypeLocBuilder TLB;
- DependentTemplateSpecializationTypeLoc SpecTL
- = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
- SpecTL.setKeywordLoc(SourceLocation());
- SpecTL.setNameLoc(TemplateLoc);
- SpecTL.setLAngleLoc(LAngleLoc);
- SpecTL.setRAngleLoc(RAngleLoc);
- SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
- for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
- SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
- return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
- }
-
- QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
- TemplateArgsIn.release();
- if (Result.isNull())
- return true;
- // Build type-source information.
- TypeLocBuilder TLB;
- TemplateSpecializationTypeLoc SpecTL
- = TLB.push<TemplateSpecializationTypeLoc>(Result);
- SpecTL.setTemplateNameLoc(TemplateLoc);
- SpecTL.setLAngleLoc(LAngleLoc);
- SpecTL.setRAngleLoc(RAngleLoc);
- for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
- SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
- // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
- // constructor or destructor name (in such a case, the scope specifier
- // will be attached to the enclosing Decl or Expr node).
- if (SS.isNotEmpty() && !IsCtorOrDtorName) {
- // Create an elaborated-type-specifier containing the nested-name-specifier.
- Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
- ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
- ElabTL.setKeywordLoc(SourceLocation());
- ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
- }
-
- return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
- }
- TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
- TypeSpecifierType TagSpec,
- SourceLocation TagLoc,
- CXXScopeSpec &SS,
- TemplateTy TemplateD,
- SourceLocation TemplateLoc,
- SourceLocation LAngleLoc,
- ASTTemplateArgsPtr TemplateArgsIn,
- SourceLocation RAngleLoc) {
- TemplateName Template = TemplateD.getAsVal<TemplateName>();
-
- // Translate the parser's template argument list in our AST format.
- TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
- translateTemplateArguments(TemplateArgsIn, TemplateArgs);
-
- // Determine the tag kind
- TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
- ElaboratedTypeKeyword Keyword
- = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
- if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
- QualType T = Context.getDependentTemplateSpecializationType(Keyword,
- DTN->getQualifier(),
- DTN->getIdentifier(),
- TemplateArgs);
-
- // Build type-source information.
- TypeLocBuilder TLB;
- DependentTemplateSpecializationTypeLoc SpecTL
- = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
- SpecTL.setKeywordLoc(TagLoc);
- SpecTL.setNameLoc(TemplateLoc);
- SpecTL.setLAngleLoc(LAngleLoc);
- SpecTL.setRAngleLoc(RAngleLoc);
- SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
- for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
- SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
- return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
- }
- if (TypeAliasTemplateDecl *TAT =
- dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
- // C++0x [dcl.type.elab]p2:
- // If the identifier resolves to a typedef-name or the simple-template-id
- // resolves to an alias template specialization, the
- // elaborated-type-specifier is ill-formed.
- Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
- Diag(TAT->getLocation(), diag::note_declared_at);
- }
-
- QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
- if (Result.isNull())
- return TypeResult(true);
-
- // Check the tag kind
- if (const RecordType *RT = Result->getAs<RecordType>()) {
- RecordDecl *D = RT->getDecl();
-
- IdentifierInfo *Id = D->getIdentifier();
- assert(Id && "templated class must have an identifier");
-
- if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
- TagLoc, *Id)) {
- Diag(TagLoc, diag::err_use_with_wrong_tag)
- << Result
- << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
- Diag(D->getLocation(), diag::note_previous_use);
- }
- }
-
- // Provide source-location information for the template specialization.
- TypeLocBuilder TLB;
- TemplateSpecializationTypeLoc SpecTL
- = TLB.push<TemplateSpecializationTypeLoc>(Result);
- SpecTL.setTemplateNameLoc(TemplateLoc);
- SpecTL.setLAngleLoc(LAngleLoc);
- SpecTL.setRAngleLoc(RAngleLoc);
- for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
- SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
- // Construct an elaborated type containing the nested-name-specifier (if any)
- // and keyword.
- Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
- ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
- ElabTL.setKeywordLoc(TagLoc);
- ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
- return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
- }
- ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
- SourceLocation TemplateKWLoc,
- LookupResult &R,
- bool RequiresADL,
- const TemplateArgumentListInfo &TemplateArgs) {
- // FIXME: Can we do any checking at this point? I guess we could check the
- // template arguments that we have against the template name, if the template
- // name refers to a single template. That's not a terribly common case,
- // though.
- // foo<int> could identify a single function unambiguously
- // This approach does NOT work, since f<int>(1);
- // gets resolved prior to resorting to overload resolution
- // i.e., template<class T> void f(double);
- // vs template<class T, class U> void f(U);
- // These should be filtered out by our callers.
- assert(!R.empty() && "empty lookup results when building templateid");
- assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
- // We don't want lookup warnings at this point.
- R.suppressDiagnostics();
- UnresolvedLookupExpr *ULE
- = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
- SS.getWithLocInContext(Context),
- TemplateKWLoc,
- R.getLookupNameInfo(),
- RequiresADL, TemplateArgs,
- R.begin(), R.end());
- return Owned(ULE);
- }
- // We actually only call this from template instantiation.
- ExprResult
- Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
- SourceLocation TemplateKWLoc,
- const DeclarationNameInfo &NameInfo,
- const TemplateArgumentListInfo &TemplateArgs) {
- DeclContext *DC;
- if (!(DC = computeDeclContext(SS, false)) ||
- DC->isDependentContext() ||
- RequireCompleteDeclContext(SS, DC))
- return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo,
- &TemplateArgs);
- bool MemberOfUnknownSpecialization;
- LookupResult R(*this, NameInfo, LookupOrdinaryName);
- LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
- MemberOfUnknownSpecialization);
- if (R.isAmbiguous())
- return ExprError();
- if (R.empty()) {
- Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
- << NameInfo.getName() << SS.getRange();
- return ExprError();
- }
- if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
- Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
- << (NestedNameSpecifier*) SS.getScopeRep()
- << NameInfo.getName() << SS.getRange();
- Diag(Temp->getLocation(), diag::note_referenced_class_template);
- return ExprError();
- }
- return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
- }
- /// \brief Form a dependent template name.
- ///
- /// This action forms a dependent template name given the template
- /// name and its (presumably dependent) scope specifier. For
- /// example, given "MetaFun::template apply", the scope specifier \p
- /// SS will be "MetaFun::", \p TemplateKWLoc contains the location
- /// of the "template" keyword, and "apply" is the \p Name.
- TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
- CXXScopeSpec &SS,
- SourceLocation TemplateKWLoc,
- UnqualifiedId &Name,
- ParsedType ObjectType,
- bool EnteringContext,
- TemplateTy &Result) {
- if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
- Diag(TemplateKWLoc,
- getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_template_outside_of_template :
- diag::ext_template_outside_of_template)
- << FixItHint::CreateRemoval(TemplateKWLoc);
- DeclContext *LookupCtx = 0;
- if (SS.isSet())
- LookupCtx = computeDeclContext(SS, EnteringContext);
- if (!LookupCtx && ObjectType)
- LookupCtx = computeDeclContext(ObjectType.get());
- if (LookupCtx) {
- // C++0x [temp.names]p5:
- // If a name prefixed by the keyword template is not the name of
- // a template, the program is ill-formed. [Note: the keyword
- // template may not be applied to non-template members of class
- // templates. -end note ] [ Note: as is the case with the
- // typename prefix, the template prefix is allowed in cases
- // where it is not strictly necessary; i.e., when the
- // nested-name-specifier or the expression on the left of the ->
- // or . is not dependent on a template-parameter, or the use
- // does not appear in the scope of a template. -end note]
- //
- // Note: C++03 was more strict here, because it banned the use of
- // the "template" keyword prior to a template-name that was not a
- // dependent name. C++ DR468 relaxed this requirement (the
- // "template" keyword is now permitted). We follow the C++0x
- // rules, even in C++03 mode with a warning, retroactively applying the DR.
- bool MemberOfUnknownSpecialization;
- TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
- ObjectType, EnteringContext, Result,
- MemberOfUnknownSpecialization);
- if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
- isa<CXXRecordDecl>(LookupCtx) &&
- (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
- cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
- // This is a dependent template. Handle it below.
- } else if (TNK == TNK_Non_template) {
- Diag(Name.getSourceRange().getBegin(),
- diag::err_template_kw_refers_to_non_template)
- << GetNameFromUnqualifiedId(Name).getName()
- << Name.getSourceRange()
- << TemplateKWLoc;
- return TNK_Non_template;
- } else {
- // We found something; return it.
- return TNK;
- }
- }
- NestedNameSpecifier *Qualifier
- = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
- switch (Name.getKind()) {
- case UnqualifiedId::IK_Identifier:
- Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
- Name.Identifier));
- return TNK_Dependent_template_name;
- case UnqualifiedId::IK_OperatorFunctionId:
- Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
- Name.OperatorFunctionId.Operator));
- return TNK_Dependent_template_name;
- case UnqualifiedId::IK_LiteralOperatorId:
- llvm_unreachable(
- "We don't support these; Parse shouldn't have allowed propagation");
- default:
- break;
- }
- Diag(Name.getSourceRange().getBegin(),
- diag::err_template_kw_refers_to_non_template)
- << GetNameFromUnqualifiedId(Name).getName()
- << Name.getSourceRange()
- << TemplateKWLoc;
- return TNK_Non_template;
- }
- bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
- const TemplateArgumentLoc &AL,
- SmallVectorImpl<TemplateArgument> &Converted) {
- const TemplateArgument &Arg = AL.getArgument();
- // Check template type parameter.
- switch(Arg.getKind()) {
- case TemplateArgument::Type:
- // C++ [temp.arg.type]p1:
- // A template-argument for a template-parameter which is a
- // type shall be a type-id.
- break;
- case TemplateArgument::Template: {
- // We have a template type parameter but the template argument
- // is a template without any arguments.
- SourceRange SR = AL.getSourceRange();
- TemplateName Name = Arg.getAsTemplate();
- Diag(SR.getBegin(), diag::err_template_missing_args)
- << Name << SR;
- if (TemplateDecl *Decl = Name.getAsTemplateDecl())
- Diag(Decl->getLocation(), diag::note_template_decl_here);
- return true;
- }
- default: {
- // We have a template type parameter but the template argument
- // is not a type.
- SourceRange SR = AL.getSourceRange();
- Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
- Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- }
- if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
- return true;
- // Add the converted template type argument.
- QualType ArgType = Context.getCanonicalType(Arg.getAsType());
-
- // Objective-C ARC:
- // If an explicitly-specified template argument type is a lifetime type
- // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
- if (getLangOptions().ObjCAutoRefCount &&
- ArgType->isObjCLifetimeType() &&
- !ArgType.getObjCLifetime()) {
- Qualifiers Qs;
- Qs.setObjCLifetime(Qualifiers::OCL_Strong);
- ArgType = Context.getQualifiedType(ArgType, Qs);
- }
-
- Converted.push_back(TemplateArgument(ArgType));
- return false;
- }
- /// \brief Substitute template arguments into the default template argument for
- /// the given template type parameter.
- ///
- /// \param SemaRef the semantic analysis object for which we are performing
- /// the substitution.
- ///
- /// \param Template the template that we are synthesizing template arguments
- /// for.
- ///
- /// \param TemplateLoc the location of the template name that started the
- /// template-id we are checking.
- ///
- /// \param RAngleLoc the location of the right angle bracket ('>') that
- /// terminates the template-id.
- ///
- /// \param Param the template template parameter whose default we are
- /// substituting into.
- ///
- /// \param Converted the list of template arguments provided for template
- /// parameters that precede \p Param in the template parameter list.
- /// \returns the substituted template argument, or NULL if an error occurred.
- static TypeSourceInfo *
- SubstDefaultTemplateArgument(Sema &SemaRef,
- TemplateDecl *Template,
- SourceLocation TemplateLoc,
- SourceLocation RAngleLoc,
- TemplateTypeParmDecl *Param,
- SmallVectorImpl<TemplateArgument> &Converted) {
- TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
- // If the argument type is dependent, instantiate it now based
- // on the previously-computed template arguments.
- if (ArgType->getType()->isDependentType()) {
- TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
- Converted.data(), Converted.size());
- MultiLevelTemplateArgumentList AllTemplateArgs
- = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
- Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
- Template, Converted.data(),
- Converted.size(),
- SourceRange(TemplateLoc, RAngleLoc));
- ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
- Param->getDefaultArgumentLoc(),
- Param->getDeclName());
- }
- return ArgType;
- }
- /// \brief Substitute template arguments into the default template argument for
- /// the given non-type template parameter.
- ///
- /// \param SemaRef the semantic analysis object for which we are performing
- /// the substitution.
- ///
- /// \param Template the template that we are synthesizing template arguments
- /// for.
- ///
- /// \param TemplateLoc the location of the template name that started the
- /// template-id we are checking.
- ///
- /// \param RAngleLoc the location of the right angle bracket ('>') that
- /// terminates the template-id.
- ///
- /// \param Param the non-type template parameter whose default we are
- /// substituting into.
- ///
- /// \param Converted the list of template arguments provided for template
- /// parameters that precede \p Param in the template parameter list.
- ///
- /// \returns the substituted template argument, or NULL if an error occurred.
- static ExprResult
- SubstDefaultTemplateArgument(Sema &SemaRef,
- TemplateDecl *Template,
- SourceLocation TemplateLoc,
- SourceLocation RAngleLoc,
- NonTypeTemplateParmDecl *Param,
- SmallVectorImpl<TemplateArgument> &Converted) {
- TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
- Converted.data(), Converted.size());
- MultiLevelTemplateArgumentList AllTemplateArgs
- = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
- Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
- Template, Converted.data(),
- Converted.size(),
- SourceRange(TemplateLoc, RAngleLoc));
- return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
- }
- /// \brief Substitute template arguments into the default template argument for
- /// the given template template parameter.
- ///
- /// \param SemaRef the semantic analysis object for which we are performing
- /// the substitution.
- ///
- /// \param Template the template that we are synthesizing template arguments
- /// for.
- ///
- /// \param TemplateLoc the location of the template name that started the
- /// template-id we are checking.
- ///
- /// \param RAngleLoc the location of the right angle bracket ('>') that
- /// terminates the template-id.
- ///
- /// \param Param the template template parameter whose default we are
- /// substituting into.
- ///
- /// \param Converted the list of template arguments provided for template
- /// parameters that precede \p Param in the template parameter list.
- ///
- /// \param QualifierLoc Will be set to the nested-name-specifier (with
- /// source-location information) that precedes the template name.
- ///
- /// \returns the substituted template argument, or NULL if an error occurred.
- static TemplateName
- SubstDefaultTemplateArgument(Sema &SemaRef,
- TemplateDecl *Template,
- SourceLocation TemplateLoc,
- SourceLocation RAngleLoc,
- TemplateTemplateParmDecl *Param,
- SmallVectorImpl<TemplateArgument> &Converted,
- NestedNameSpecifierLoc &QualifierLoc) {
- TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
- Converted.data(), Converted.size());
- MultiLevelTemplateArgumentList AllTemplateArgs
- = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
- Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
- Template, Converted.data(),
- Converted.size(),
- SourceRange(TemplateLoc, RAngleLoc));
- // Substitute into the nested-name-specifier first,
- QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
- if (QualifierLoc) {
- QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
- AllTemplateArgs);
- if (!QualifierLoc)
- return TemplateName();
- }
-
- return SemaRef.SubstTemplateName(QualifierLoc,
- Param->getDefaultArgument().getArgument().getAsTemplate(),
- Param->getDefaultArgument().getTemplateNameLoc(),
- AllTemplateArgs);
- }
- /// \brief If the given template parameter has a default template
- /// argument, substitute into that default template argument and
- /// return the corresponding template argument.
- TemplateArgumentLoc
- Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
- SourceLocation TemplateLoc,
- SourceLocation RAngleLoc,
- Decl *Param,
- SmallVectorImpl<TemplateArgument> &Converted) {
- if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
- if (!TypeParm->hasDefaultArgument())
- return TemplateArgumentLoc();
- TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
- TemplateLoc,
- RAngleLoc,
- TypeParm,
- Converted);
- if (DI)
- return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
- return TemplateArgumentLoc();
- }
- if (NonTypeTemplateParmDecl *NonTypeParm
- = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
- if (!NonTypeParm->hasDefaultArgument())
- return TemplateArgumentLoc();
- ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
- TemplateLoc,
- RAngleLoc,
- NonTypeParm,
- Converted);
- if (Arg.isInvalid())
- return TemplateArgumentLoc();
- Expr *ArgE = Arg.takeAs<Expr>();
- return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
- }
- TemplateTemplateParmDecl *TempTempParm
- = cast<TemplateTemplateParmDecl>(Param);
- if (!TempTempParm->hasDefaultArgument())
- return TemplateArgumentLoc();
- NestedNameSpecifierLoc QualifierLoc;
- TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
- TemplateLoc,
- RAngleLoc,
- TempTempParm,
- Converted,
- QualifierLoc);
- if (TName.isNull())
- return TemplateArgumentLoc();
- return TemplateArgumentLoc(TemplateArgument(TName),
- TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
- TempTempParm->getDefaultArgument().getTemplateNameLoc());
- }
- /// \brief Check that the given template argument corresponds to the given
- /// template parameter.
- ///
- /// \param Param The template parameter against which the argument will be
- /// checked.
- ///
- /// \param Arg The template argument.
- ///
- /// \param Template The template in which the template argument resides.
- ///
- /// \param TemplateLoc The location of the template name for the template
- /// whose argument list we're matching.
- ///
- /// \param RAngleLoc The location of the right angle bracket ('>') that closes
- /// the template argument list.
- ///
- /// \param ArgumentPackIndex The index into the argument pack where this
- /// argument will be placed. Only valid if the parameter is a parameter pack.
- ///
- /// \param Converted The checked, converted argument will be added to the
- /// end of this small vector.
- ///
- /// \param CTAK Describes how we arrived at this particular template argument:
- /// explicitly written, deduced, etc.
- ///
- /// \returns true on error, false otherwise.
- bool Sema::CheckTemplateArgument(NamedDecl *Param,
- const TemplateArgumentLoc &Arg,
- NamedDecl *Template,
- SourceLocation TemplateLoc,
- SourceLocation RAngleLoc,
- unsigned ArgumentPackIndex,
- SmallVectorImpl<TemplateArgument> &Converted,
- CheckTemplateArgumentKind CTAK) {
- // Check template type parameters.
- if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
- return CheckTemplateTypeArgument(TTP, Arg, Converted);
- // Check non-type template parameters.
- if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
- // Do substitution on the type of the non-type template parameter
- // with the template arguments we've seen thus far. But if the
- // template has a dependent context then we cannot substitute yet.
- QualType NTTPType = NTTP->getType();
- if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
- NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
- if (NTTPType->isDependentType() &&
- !isa<TemplateTemplateParmDecl>(Template) &&
- !Template->getDeclContext()->isDependentContext()) {
- // Do substitution on the type of the non-type template parameter.
- InstantiatingTemplate Inst(*this, TemplateLoc, Template,
- NTTP, Converted.data(), Converted.size(),
- SourceRange(TemplateLoc, RAngleLoc));
- TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
- Converted.data(), Converted.size());
- NTTPType = SubstType(NTTPType,
- MultiLevelTemplateArgumentList(TemplateArgs),
- NTTP->getLocation(),
- NTTP->getDeclName());
- // If that worked, check the non-type template parameter type
- // for validity.
- if (!NTTPType.isNull())
- NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
- NTTP->getLocation());
- if (NTTPType.isNull())
- return true;
- }
- switch (Arg.getArgument().getKind()) {
- case TemplateArgument::Null:
- llvm_unreachable("Should never see a NULL template argument here");
- case TemplateArgument::Expression: {
- TemplateArgument Result;
- ExprResult Res =
- CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
- Result, CTAK);
- if (Res.isInvalid())
- return true;
- Converted.push_back(Result);
- break;
- }
- case TemplateArgument::Declaration:
- case TemplateArgument::Integral:
- // We've already checked this template argument, so just copy
- // it to the list of converted arguments.
- Converted.push_back(Arg.getArgument());
- break;
- case TemplateArgument::Template:
- case TemplateArgument::TemplateExpansion:
- // We were given a template template argument. It may not be ill-formed;
- // see below.
- if (DependentTemplateName *DTN
- = Arg.getArgument().getAsTemplateOrTemplatePattern()
- .getAsDependentTemplateName()) {
- // We have a template argument such as \c T::template X, which we
- // parsed as a template template argument. However, since we now
- // know that we need a non-type template argument, convert this
- // template name into an expression.
- DeclarationNameInfo NameInfo(DTN->getIdentifier(),
- Arg.getTemplateNameLoc());
- CXXScopeSpec SS;
- SS.Adopt(Arg.getTemplateQualifierLoc());
- // FIXME: the template-template arg was a DependentTemplateName,
- // so it was provided with a template keyword. However, its source
- // location is not stored in the template argument structure.
- SourceLocation TemplateKWLoc;
- ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
- SS.getWithLocInContext(Context),
- TemplateKWLoc,
- NameInfo, 0));
- // If we parsed the template argument as a pack expansion, create a
- // pack expansion expression.
- if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
- E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
- if (E.isInvalid())
- return true;
- }
- TemplateArgument Result;
- E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
- if (E.isInvalid())
- return true;
- Converted.push_back(Result);
- break;
- }
- // We have a template argument that actually does refer to a class
- // template, alias template, or template template parameter, and
- // therefore cannot be a non-type template argument.
- Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
- << Arg.getSourceRange();
- Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- case TemplateArgument::Type: {
- // We have a non-type template parameter but the template
- // argument is a type.
- // C++ [temp.arg]p2:
- // In a template-argument, an ambiguity between a type-id and
- // an expression is resolved to a type-id, regardless of the
- // form of the corresponding template-parameter.
- //
- // We warn specifically about this case, since it can be rather
- // confusing for users.
- QualType T = Arg.getArgument().getAsType();
- SourceRange SR = Arg.getSourceRange();
- if (T->isFunctionType())
- Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
- else
- Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
- Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- case TemplateArgument::Pack:
- llvm_unreachable("Caller must expand template argument packs");
- }
- return false;
- }
- // Check template template parameters.
- TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
- // Substitute into the template parameter list of the template
- // template parameter, since previously-supplied template arguments
- // may appear within the template template parameter.
- {
- // Set up a template instantiation context.
- LocalInstantiationScope Scope(*this);
- InstantiatingTemplate Inst(*this, TemplateLoc, Template,
- TempParm, Converted.data(), Converted.size(),
- SourceRange(TemplateLoc, RAngleLoc));
- TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
- Converted.data(), Converted.size());
- TempParm = cast_or_null<TemplateTemplateParmDecl>(
- SubstDecl(TempParm, CurContext,
- MultiLevelTemplateArgumentList(TemplateArgs)));
- if (!TempParm)
- return true;
- }
- switch (Arg.getArgument().getKind()) {
- case TemplateArgument::Null:
- llvm_unreachable("Should never see a NULL template argument here");
- case TemplateArgument::Template:
- case TemplateArgument::TemplateExpansion:
- if (CheckTemplateArgument(TempParm, Arg))
- return true;
- Converted.push_back(Arg.getArgument());
- break;
- case TemplateArgument::Expression:
- case TemplateArgument::Type:
- // We have a template template parameter but the template
- // argument does not refer to a template.
- Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
- << getLangOptions().CPlusPlus0x;
- return true;
- case TemplateArgument::Declaration:
- llvm_unreachable("Declaration argument with template template parameter");
- case TemplateArgument::Integral:
- llvm_unreachable("Integral argument with template template parameter");
- case TemplateArgument::Pack:
- llvm_unreachable("Caller must expand template argument packs");
- }
- return false;
- }
- /// \brief Diagnose an arity mismatch in the
- static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
- SourceLocation TemplateLoc,
- TemplateArgumentListInfo &TemplateArgs) {
- TemplateParameterList *Params = Template->getTemplateParameters();
- unsigned NumParams = Params->size();
- unsigned NumArgs = TemplateArgs.size();
- SourceRange Range;
- if (NumArgs > NumParams)
- Range = SourceRange(TemplateArgs[NumParams].getLocation(),
- TemplateArgs.getRAngleLoc());
- S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
- << (NumArgs > NumParams)
- << (isa<ClassTemplateDecl>(Template)? 0 :
- isa<FunctionTemplateDecl>(Template)? 1 :
- isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
- << Template << Range;
- S.Diag(Template->getLocation(), diag::note_template_decl_here)
- << Params->getSourceRange();
- return true;
- }
- /// \brief Check that the given template argument list is well-formed
- /// for specializing the given template.
- bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
- SourceLocation TemplateLoc,
- TemplateArgumentListInfo &TemplateArgs,
- bool PartialTemplateArgs,
- SmallVectorImpl<TemplateArgument> &Converted,
- bool *ExpansionIntoFixedList) {
- if (ExpansionIntoFixedList)
- *ExpansionIntoFixedList = false;
- TemplateParameterList *Params = Template->getTemplateParameters();
- unsigned NumParams = Params->size();
- unsigned NumArgs = TemplateArgs.size();
- bool Invalid = false;
- SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
- bool HasParameterPack =
- NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
-
- // C++ [temp.arg]p1:
- // [...] The type and form of each template-argument specified in
- // a template-id shall match the type and form specified for the
- // corresponding parameter declared by the template in its
- // template-parameter-list.
- bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
- SmallVector<TemplateArgument, 2> ArgumentPack;
- TemplateParameterList::iterator Param = Params->begin(),
- ParamEnd = Params->end();
- unsigned ArgIdx = 0;
- LocalInstantiationScope InstScope(*this, true);
- bool SawPackExpansion = false;
- while (Param != ParamEnd) {
- if (ArgIdx < NumArgs) {
- // If we have an expanded parameter pack, make sure we don't have too
- // many arguments.
- // FIXME: This really should fall out from the normal arity checking.
- if (NonTypeTemplateParmDecl *NTTP
- = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
- if (NTTP->isExpandedParameterPack() &&
- ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
- Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
- << true
- << (isa<ClassTemplateDecl>(Template)? 0 :
- isa<FunctionTemplateDecl>(Template)? 1 :
- isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
- << Template;
- Diag(Template->getLocation(), diag::note_template_decl_here)
- << Params->getSourceRange();
- return true;
- }
- }
- // Check the template argument we were given.
- if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
- TemplateLoc, RAngleLoc,
- ArgumentPack.size(), Converted))
- return true;
- if ((*Param)->isTemplateParameterPack()) {
- // The template parameter was a template parameter pack, so take the
- // deduced argument and place it on the argument pack. Note that we
- // stay on the same template parameter so that we can deduce more
- // arguments.
- ArgumentPack.push_back(Converted.back());
- Converted.pop_back();
- } else {
- // Move to the next template parameter.
- ++Param;
- }
-
- // If this template argument is a pack expansion, record that fact
- // and break out; we can't actually check any more.
- if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) {
- SawPackExpansion = true;
- ++ArgIdx;
- break;
- }
-
- ++ArgIdx;
- continue;
- }
- // If we're checking a partial template argument list, we're done.
- if (PartialTemplateArgs) {
- if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
- Converted.push_back(TemplateArgument::CreatePackCopy(Context,
- ArgumentPack.data(),
- ArgumentPack.size()));
-
- return Invalid;
- }
- // If we have a template parameter pack with no more corresponding
- // arguments, just break out now and we'll fill in the argument pack below.
- if ((*Param)->isTemplateParameterPack())
- break;
-
- // Check whether we have a default argument.
- TemplateArgumentLoc Arg;
- // Retrieve the default template argument from the template
- // parameter. For each kind of template parameter, we substitute the
- // template arguments provided thus far and any "outer" template arguments
- // (when the template parameter was part of a nested template) into
- // the default argument.
- if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
- if (!TTP->hasDefaultArgument())
- return diagnoseArityMismatch(*this, Template, TemplateLoc,
- TemplateArgs);
- TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
- Template,
- TemplateLoc,
- RAngleLoc,
- TTP,
- Converted);
- if (!ArgType)
- return true;
- Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
- ArgType);
- } else if (NonTypeTemplateParmDecl *NTTP
- = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
- if (!NTTP->hasDefaultArgument())
- return diagnoseArityMismatch(*this, Template, TemplateLoc,
- TemplateArgs);
- ExprResult E = SubstDefaultTemplateArgument(*this, Template,
- TemplateLoc,
- RAngleLoc,
- NTTP,
- Converted);
- if (E.isInvalid())
- return true;
- Expr *Ex = E.takeAs<Expr>();
- Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
- } else {
- TemplateTemplateParmDecl *TempParm
- = cast<TemplateTemplateParmDecl>(*Param);
- if (!TempParm->hasDefaultArgument())
- return diagnoseArityMismatch(*this, Template, TemplateLoc,
- TemplateArgs);
- NestedNameSpecifierLoc QualifierLoc;
- TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
- TemplateLoc,
- RAngleLoc,
- TempParm,
- Converted,
- QualifierLoc);
- if (Name.isNull())
- return true;
- Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
- TempParm->getDefaultArgument().getTemplateNameLoc());
- }
- // Introduce an instantiation record that describes where we are using
- // the default template argument.
- InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
- Converted.data(), Converted.size(),
- SourceRange(TemplateLoc, RAngleLoc));
- // Check the default template argument.
- if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
- RAngleLoc, 0, Converted))
- return true;
- // Core issue 150 (assumed resolution): if this is a template template
- // parameter, keep track of the default template arguments from the
- // template definition.
- if (isTemplateTemplateParameter)
- TemplateArgs.addArgument(Arg);
-
- // Move to the next template parameter and argument.
- ++Param;
- ++ArgIdx;
- }
- // If we saw a pack expansion, then directly convert the remaining arguments,
- // because we don't know what parameters they'll match up with.
- if (SawPackExpansion) {
- bool AddToArgumentPack
- = Param != ParamEnd && (*Param)->isTemplateParameterPack();
- while (ArgIdx < NumArgs) {
- if (AddToArgumentPack)
- ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument());
- else
- Converted.push_back(TemplateArgs[ArgIdx].getArgument());
- ++ArgIdx;
- }
- // Push the argument pack onto the list of converted arguments.
- if (AddToArgumentPack) {
- if (ArgumentPack.empty())
- Converted.push_back(TemplateArgument(0, 0));
- else {
- Converted.push_back(
- TemplateArgument::CreatePackCopy(Context,
- ArgumentPack.data(),
- ArgumentPack.size()));
- ArgumentPack.clear();
- }
- } else if (ExpansionIntoFixedList) {
- // We have expanded a pack into a fixed list.
- *ExpansionIntoFixedList = true;
- }
- return Invalid;
- }
- // If we have any leftover arguments, then there were too many arguments.
- // Complain and fail.
- if (ArgIdx < NumArgs)
- return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
-
- // If we have an expanded parameter pack, make sure we don't have too
- // many arguments.
- // FIXME: This really should fall out from the normal arity checking.
- if (Param != ParamEnd) {
- if (NonTypeTemplateParmDecl *NTTP
- = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
- if (NTTP->isExpandedParameterPack() &&
- ArgumentPack.size() < NTTP->getNumExpansionTypes()) {
- Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
- << false
- << (isa<ClassTemplateDecl>(Template)? 0 :
- isa<FunctionTemplateDecl>(Template)? 1 :
- isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
- << Template;
- Diag(Template->getLocation(), diag::note_template_decl_here)
- << Params->getSourceRange();
- return true;
- }
- }
- }
-
- // Form argument packs for each of the parameter packs remaining.
- while (Param != ParamEnd) {
- // If we're checking a partial list of template arguments, don't fill
- // in arguments for non-template parameter packs.
- if ((*Param)->isTemplateParameterPack()) {
- if (!HasParameterPack)
- return true;
- if (ArgumentPack.empty())
- Converted.push_back(TemplateArgument(0, 0));
- else {
- Converted.push_back(TemplateArgument::CreatePackCopy(Context,
- ArgumentPack.data(),
- ArgumentPack.size()));
- ArgumentPack.clear();
- }
- } else if (!PartialTemplateArgs)
- return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
- ++Param;
- }
- return Invalid;
- }
- namespace {
- class UnnamedLocalNoLinkageFinder
- : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
- {
- Sema &S;
- SourceRange SR;
- typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
- public:
- UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
- bool Visit(QualType T) {
- return inherited::Visit(T.getTypePtr());
- }
- #define TYPE(Class, Parent) \
- bool Visit##Class##Type(const Class##Type *);
- #define ABSTRACT_TYPE(Class, Parent) \
- bool Visit##Class##Type(const Class##Type *) { return false; }
- #define NON_CANONICAL_TYPE(Class, Parent) \
- bool Visit##Class##Type(const Class##Type *) { return false; }
- #include "clang/AST/TypeNodes.def"
- bool VisitTagDecl(const TagDecl *Tag);
- bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
- };
- }
- bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
- return Visit(T->getPointeeType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
- const BlockPointerType* T) {
- return Visit(T->getPointeeType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
- const LValueReferenceType* T) {
- return Visit(T->getPointeeType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
- const RValueReferenceType* T) {
- return Visit(T->getPointeeType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
- const MemberPointerType* T) {
- return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
- }
- bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
- const ConstantArrayType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
- const IncompleteArrayType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
- const VariableArrayType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
- const DependentSizedArrayType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
- const DependentSizedExtVectorType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
- return Visit(T->getElementType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
- const FunctionProtoType* T) {
- for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
- AEnd = T->arg_type_end();
- A != AEnd; ++A) {
- if (Visit(*A))
- return true;
- }
- return Visit(T->getResultType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
- const FunctionNoProtoType* T) {
- return Visit(T->getResultType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
- const UnresolvedUsingType*) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
- return Visit(T->getUnderlyingType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
- const UnaryTransformType*) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
- return Visit(T->getDeducedType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
- return VisitTagDecl(T->getDecl());
- }
- bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
- return VisitTagDecl(T->getDecl());
- }
- bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
- const TemplateTypeParmType*) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
- const SubstTemplateTypeParmPackType *) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
- const TemplateSpecializationType*) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
- const InjectedClassNameType* T) {
- return VisitTagDecl(T->getDecl());
- }
- bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
- const DependentNameType* T) {
- return VisitNestedNameSpecifier(T->getQualifier());
- }
- bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
- const DependentTemplateSpecializationType* T) {
- return VisitNestedNameSpecifier(T->getQualifier());
- }
- bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
- const PackExpansionType* T) {
- return Visit(T->getPattern());
- }
- bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
- const ObjCInterfaceType *) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
- const ObjCObjectPointerType *) {
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
- return Visit(T->getValueType());
- }
- bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
- if (Tag->getDeclContext()->isFunctionOrMethod()) {
- S.Diag(SR.getBegin(),
- S.getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_template_arg_local_type :
- diag::ext_template_arg_local_type)
- << S.Context.getTypeDeclType(Tag) << SR;
- return true;
- }
- if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
- S.Diag(SR.getBegin(),
- S.getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_template_arg_unnamed_type :
- diag::ext_template_arg_unnamed_type) << SR;
- S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
- return true;
- }
- return false;
- }
- bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
- NestedNameSpecifier *NNS) {
- if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
- return true;
- switch (NNS->getKind()) {
- case NestedNameSpecifier::Identifier:
- case NestedNameSpecifier::Namespace:
- case NestedNameSpecifier::NamespaceAlias:
- case NestedNameSpecifier::Global:
- return false;
- case NestedNameSpecifier::TypeSpec:
- case NestedNameSpecifier::TypeSpecWithTemplate:
- return Visit(QualType(NNS->getAsType(), 0));
- }
- llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
- }
- /// \brief Check a template argument against its corresponding
- /// template type parameter.
- ///
- /// This routine implements the semantics of C++ [temp.arg.type]. It
- /// returns true if an error occurred, and false otherwise.
- bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
- TypeSourceInfo *ArgInfo) {
- assert(ArgInfo && "invalid TypeSourceInfo");
- QualType Arg = ArgInfo->getType();
- SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
- if (Arg->isVariablyModifiedType()) {
- return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
- } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
- return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
- }
- // C++03 [temp.arg.type]p2:
- // A local type, a type with no linkage, an unnamed type or a type
- // compounded from any of these types shall not be used as a
- // template-argument for a template type-parameter.
- //
- // C++11 allows these, and even in C++03 we allow them as an extension with
- // a warning.
- if (LangOpts.CPlusPlus0x ?
- Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
- SR.getBegin()) != DiagnosticsEngine::Ignored ||
- Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
- SR.getBegin()) != DiagnosticsEngine::Ignored :
- Arg->hasUnnamedOrLocalType()) {
- UnnamedLocalNoLinkageFinder Finder(*this, SR);
- (void)Finder.Visit(Context.getCanonicalType(Arg));
- }
- return false;
- }
- /// \brief Checks whether the given template argument is the address
- /// of an object or function according to C++ [temp.arg.nontype]p1.
- static bool
- CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
- NonTypeTemplateParmDecl *Param,
- QualType ParamType,
- Expr *ArgIn,
- TemplateArgument &Converted) {
- bool Invalid = false;
- Expr *Arg = ArgIn;
- QualType ArgType = Arg->getType();
- // See through any implicit casts we added to fix the type.
- Arg = Arg->IgnoreImpCasts();
- // C++ [temp.arg.nontype]p1:
- //
- // A template-argument for a non-type, non-template
- // template-parameter shall be one of: [...]
- //
- // -- the address of an object or function with external
- // linkage, including function templates and function
- // template-ids but excluding non-static class members,
- // expressed as & id-expression where the & is optional if
- // the name refers to a function or array, or if the
- // corresponding template-parameter is a reference; or
- // In C++98/03 mode, give an extension warning on any extra parentheses.
- // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
- bool ExtraParens = false;
- while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
- if (!Invalid && !ExtraParens) {
- S.Diag(Arg->getSourceRange().getBegin(),
- S.getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_template_arg_extra_parens :
- diag::ext_template_arg_extra_parens)
- << Arg->getSourceRange();
- ExtraParens = true;
- }
- Arg = Parens->getSubExpr();
- }
- while (SubstNonTypeTemplateParmExpr *subst =
- dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
- Arg = subst->getReplacement()->IgnoreImpCasts();
- bool AddressTaken = false;
- SourceLocation AddrOpLoc;
- if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
- if (UnOp->getOpcode() == UO_AddrOf) {
- Arg = UnOp->getSubExpr();
- AddressTaken = true;
- AddrOpLoc = UnOp->getOperatorLoc();
- }
- }
- if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
- Converted = TemplateArgument(ArgIn);
- return false;
- }
- while (SubstNonTypeTemplateParmExpr *subst =
- dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
- Arg = subst->getReplacement()->IgnoreImpCasts();
- DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
- if (!DRE) {
- S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
- << Arg->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- // Stop checking the precise nature of the argument if it is value dependent,
- // it should be checked when instantiated.
- if (Arg->isValueDependent()) {
- Converted = TemplateArgument(ArgIn);
- return false;
- }
- if (!isa<ValueDecl>(DRE->getDecl())) {
- S.Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_object_or_func_form)
- << Arg->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- NamedDecl *Entity = 0;
- // Cannot refer to non-static data members
- if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
- S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
- << Field << Arg->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- // Cannot refer to non-static member functions
- if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
- if (!Method->isStatic()) {
- S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
- << Method << Arg->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- // Functions must have external linkage.
- if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
- if (!isExternalLinkage(Func->getLinkage())) {
- S.Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_function_not_extern)
- << Func << Arg->getSourceRange();
- S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
- << true;
- return true;
- }
- // Okay: we've named a function with external linkage.
- Entity = Func;
- // If the template parameter has pointer type, the function decays.
- if (ParamType->isPointerType() && !AddressTaken)
- ArgType = S.Context.getPointerType(Func->getType());
- else if (AddressTaken && ParamType->isReferenceType()) {
- // If we originally had an address-of operator, but the
- // parameter has reference type, complain and (if things look
- // like they will work) drop the address-of operator.
- if (!S.Context.hasSameUnqualifiedType(Func->getType(),
- ParamType.getNonReferenceType())) {
- S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
- << ParamType;
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
- << ParamType
- << FixItHint::CreateRemoval(AddrOpLoc);
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- ArgType = Func->getType();
- }
- } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
- if (!isExternalLinkage(Var->getLinkage())) {
- S.Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_object_not_extern)
- << Var << Arg->getSourceRange();
- S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
- << true;
- return true;
- }
- // A value of reference type is not an object.
- if (Var->getType()->isReferenceType()) {
- S.Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_reference_var)
- << Var->getType() << Arg->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- // Okay: we've named an object with external linkage
- Entity = Var;
- // If the template parameter has pointer type, we must have taken
- // the address of this object.
- if (ParamType->isReferenceType()) {
- if (AddressTaken) {
- // If we originally had an address-of operator, but the
- // parameter has reference type, complain and (if things look
- // like they will work) drop the address-of operator.
- if (!S.Context.hasSameUnqualifiedType(Var->getType(),
- ParamType.getNonReferenceType())) {
- S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
- << ParamType;
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
- << ParamType
- << FixItHint::CreateRemoval(AddrOpLoc);
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- ArgType = Var->getType();
- }
- } else if (!AddressTaken && ParamType->isPointerType()) {
- if (Var->getType()->isArrayType()) {
- // Array-to-pointer decay.
- ArgType = S.Context.getArrayDecayedType(Var->getType());
- } else {
- // If the template parameter has pointer type but the address of
- // this object was not taken, complain and (possibly) recover by
- // taking the address of the entity.
- ArgType = S.Context.getPointerType(Var->getType());
- if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
- S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
- << ParamType;
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
- << ParamType
- << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- }
- }
- } else {
- // We found something else, but we don't know specifically what it is.
- S.Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_object_or_func)
- << Arg->getSourceRange();
- S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
- return true;
- }
- bool ObjCLifetimeConversion;
- if (ParamType->isPointerType() &&
- !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
- S.IsQualificationConversion(ArgType, ParamType, false,
- ObjCLifetimeConversion)) {
- // For pointer-to-object types, qualification conversions are
- // permitted.
- } else {
- if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
- if (!ParamRef->getPointeeType()->isFunctionType()) {
- // C++ [temp.arg.nontype]p5b3:
- // For a non-type template-parameter of type reference to
- // object, no conversions apply. The type referred to by the
- // reference may be more cv-qualified than the (otherwise
- // identical) type of the template- argument. The
- // template-parameter is bound directly to the
- // template-argument, which shall be an lvalue.
- // FIXME: Other qualifiers?
- unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
- unsigned ArgQuals = ArgType.getCVRQualifiers();
- if ((ParamQuals | ArgQuals) != ParamQuals) {
- S.Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_ref_bind_ignores_quals)
- << ParamType << Arg->getType()
- << Arg->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- }
- }
- // At this point, the template argument refers to an object or
- // function with external linkage. We now need to check whether the
- // argument and parameter types are compatible.
- if (!S.Context.hasSameUnqualifiedType(ArgType,
- ParamType.getNonReferenceType())) {
- // We can't perform this conversion or binding.
- if (ParamType->isReferenceType())
- S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
- << ParamType << ArgIn->getType() << Arg->getSourceRange();
- else
- S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
- << ArgIn->getType() << ParamType << Arg->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- }
- // Create the template argument.
- Converted = TemplateArgument(Entity->getCanonicalDecl());
- S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity);
- return false;
- }
- /// \brief Checks whether the given template argument is a pointer to
- /// member constant according to C++ [temp.arg.nontype]p1.
- bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
- TemplateArgument &Converted) {
- bool Invalid = false;
- // See through any implicit casts we added to fix the type.
- while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
- Arg = Cast->getSubExpr();
- // C++ [temp.arg.nontype]p1:
- //
- // A template-argument for a non-type, non-template
- // template-parameter shall be one of: [...]
- //
- // -- a pointer to member expressed as described in 5.3.1.
- DeclRefExpr *DRE = 0;
- // In C++98/03 mode, give an extension warning on any extra parentheses.
- // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
- bool ExtraParens = false;
- while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
- if (!Invalid && !ExtraParens) {
- Diag(Arg->getSourceRange().getBegin(),
- getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_template_arg_extra_parens :
- diag::ext_template_arg_extra_parens)
- << Arg->getSourceRange();
- ExtraParens = true;
- }
- Arg = Parens->getSubExpr();
- }
- while (SubstNonTypeTemplateParmExpr *subst =
- dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
- Arg = subst->getReplacement()->IgnoreImpCasts();
- // A pointer-to-member constant written &Class::member.
- if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
- if (UnOp->getOpcode() == UO_AddrOf) {
- DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
- if (DRE && !DRE->getQualifier())
- DRE = 0;
- }
- }
- // A constant of pointer-to-member type.
- else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
- if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
- if (VD->getType()->isMemberPointerType()) {
- if (isa<NonTypeTemplateParmDecl>(VD) ||
- (isa<VarDecl>(VD) &&
- Context.getCanonicalType(VD->getType()).isConstQualified())) {
- if (Arg->isTypeDependent() || Arg->isValueDependent())
- Converted = TemplateArgument(Arg);
- else
- Converted = TemplateArgument(VD->getCanonicalDecl());
- return Invalid;
- }
- }
- }
- DRE = 0;
- }
- if (!DRE)
- return Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_pointer_to_member_form)
- << Arg->getSourceRange();
- if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
- assert((isa<FieldDecl>(DRE->getDecl()) ||
- !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
- "Only non-static member pointers can make it here");
- // Okay: this is the address of a non-static member, and therefore
- // a member pointer constant.
- if (Arg->isTypeDependent() || Arg->isValueDependent())
- Converted = TemplateArgument(Arg);
- else
- Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
- return Invalid;
- }
- // We found something else, but we don't know specifically what it is.
- Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_pointer_to_member_form)
- << Arg->getSourceRange();
- Diag(DRE->getDecl()->getLocation(),
- diag::note_template_arg_refers_here);
- return true;
- }
- /// \brief Check a template argument against its corresponding
- /// non-type template parameter.
- ///
- /// This routine implements the semantics of C++ [temp.arg.nontype].
- /// If an error occurred, it returns ExprError(); otherwise, it
- /// returns the converted template argument. \p
- /// InstantiatedParamType is the type of the non-type template
- /// parameter after it has been instantiated.
- ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
- QualType InstantiatedParamType, Expr *Arg,
- TemplateArgument &Converted,
- CheckTemplateArgumentKind CTAK) {
- SourceLocation StartLoc = Arg->getSourceRange().getBegin();
- // If either the parameter has a dependent type or the argument is
- // type-dependent, there's nothing we can check now.
- if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
- // FIXME: Produce a cloned, canonical expression?
- Converted = TemplateArgument(Arg);
- return Owned(Arg);
- }
- // C++ [temp.arg.nontype]p5:
- // The following conversions are performed on each expression used
- // as a non-type template-argument. If a non-type
- // template-argument cannot be converted to the type of the
- // corresponding template-parameter then the program is
- // ill-formed.
- QualType ParamType = InstantiatedParamType;
- if (ParamType->isIntegralOrEnumerationType()) {
- // C++11:
- // -- for a non-type template-parameter of integral or
- // enumeration type, conversions permitted in a converted
- // constant expression are applied.
- //
- // C++98:
- // -- for a non-type template-parameter of integral or
- // enumeration type, integral promotions (4.5) and integral
- // conversions (4.7) are applied.
- if (CTAK == CTAK_Deduced &&
- !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
- // C++ [temp.deduct.type]p17:
- // If, in the declaration of a function template with a non-type
- // template-parameter, the non-type template-parameter is used
- // in an expression in the function parameter-list and, if the
- // corresponding template-argument is deduced, the
- // template-argument type shall match the type of the
- // template-parameter exactly, except that a template-argument
- // deduced from an array bound may be of any integral type.
- Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
- << Arg->getType().getUnqualifiedType()
- << ParamType.getUnqualifiedType();
- Diag(Param->getLocation(), diag::note_template_param_here);
- return ExprError();
- }
- if (getLangOptions().CPlusPlus0x) {
- // We can't check arbitrary value-dependent arguments.
- // FIXME: If there's no viable conversion to the template parameter type,
- // we should be able to diagnose that prior to instantiation.
- if (Arg->isValueDependent()) {
- Converted = TemplateArgument(Arg);
- return Owned(Arg);
- }
- // C++ [temp.arg.nontype]p1:
- // A template-argument for a non-type, non-template template-parameter
- // shall be one of:
- //
- // -- for a non-type template-parameter of integral or enumeration
- // type, a converted constant expression of the type of the
- // template-parameter; or
- llvm::APSInt Value;
- ExprResult ArgResult =
- CheckConvertedConstantExpression(Arg, ParamType, Value,
- CCEK_TemplateArg);
- if (ArgResult.isInvalid())
- return ExprError();
- // Widen the argument value to sizeof(parameter type). This is almost
- // always a no-op, except when the parameter type is bool. In
- // that case, this may extend the argument from 1 bit to 8 bits.
- QualType IntegerType = ParamType;
- if (const EnumType *Enum = IntegerType->getAs<EnumType>())
- IntegerType = Enum->getDecl()->getIntegerType();
- Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
- Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
- return ArgResult;
- }
- ExprResult ArgResult = DefaultLvalueConversion(Arg);
- if (ArgResult.isInvalid())
- return ExprError();
- Arg = ArgResult.take();
- QualType ArgType = Arg->getType();
- // C++ [temp.arg.nontype]p1:
- // A template-argument for a non-type, non-template
- // template-parameter shall be one of:
- //
- // -- an integral constant-expression of integral or enumeration
- // type; or
- // -- the name of a non-type template-parameter; or
- SourceLocation NonConstantLoc;
- llvm::APSInt Value;
- if (!ArgType->isIntegralOrEnumerationType()) {
- Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_integral_or_enumeral)
- << ArgType << Arg->getSourceRange();
- Diag(Param->getLocation(), diag::note_template_param_here);
- return ExprError();
- } else if (!Arg->isValueDependent()) {
- Arg = VerifyIntegerConstantExpression(Arg, &Value,
- PDiag(diag::err_template_arg_not_ice) << ArgType, false).take();
- if (!Arg)
- return ExprError();
- }
- // From here on out, all we care about are the unqualified forms
- // of the parameter and argument types.
- ParamType = ParamType.getUnqualifiedType();
- ArgType = ArgType.getUnqualifiedType();
- // Try to convert the argument to the parameter's type.
- if (Context.hasSameType(ParamType, ArgType)) {
- // Okay: no conversion necessary
- } else if (ParamType->isBooleanType()) {
- // This is an integral-to-boolean conversion.
- Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
- } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
- !ParamType->isEnumeralType()) {
- // This is an integral promotion or conversion.
- Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
- } else {
- // We can't perform this conversion.
- Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_convertible)
- << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
- Diag(Param->getLocation(), diag::note_template_param_here);
- return ExprError();
- }
- // Add the value of this argument to the list of converted
- // arguments. We use the bitwidth and signedness of the template
- // parameter.
- if (Arg->isValueDependent()) {
- // The argument is value-dependent. Create a new
- // TemplateArgument with the converted expression.
- Converted = TemplateArgument(Arg);
- return Owned(Arg);
- }
- QualType IntegerType = Context.getCanonicalType(ParamType);
- if (const EnumType *Enum = IntegerType->getAs<EnumType>())
- IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
- if (ParamType->isBooleanType()) {
- // Value must be zero or one.
- Value = Value != 0;
- unsigned AllowedBits = Context.getTypeSize(IntegerType);
- if (Value.getBitWidth() != AllowedBits)
- Value = Value.extOrTrunc(AllowedBits);
- Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
- } else {
- llvm::APSInt OldValue = Value;
-
- // Coerce the template argument's value to the value it will have
- // based on the template parameter's type.
- unsigned AllowedBits = Context.getTypeSize(IntegerType);
- if (Value.getBitWidth() != AllowedBits)
- Value = Value.extOrTrunc(AllowedBits);
- Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
-
- // Complain if an unsigned parameter received a negative value.
- if (IntegerType->isUnsignedIntegerOrEnumerationType()
- && (OldValue.isSigned() && OldValue.isNegative())) {
- Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
- << OldValue.toString(10) << Value.toString(10) << Param->getType()
- << Arg->getSourceRange();
- Diag(Param->getLocation(), diag::note_template_param_here);
- }
-
- // Complain if we overflowed the template parameter's type.
- unsigned RequiredBits;
- if (IntegerType->isUnsignedIntegerOrEnumerationType())
- RequiredBits = OldValue.getActiveBits();
- else if (OldValue.isUnsigned())
- RequiredBits = OldValue.getActiveBits() + 1;
- else
- RequiredBits = OldValue.getMinSignedBits();
- if (RequiredBits > AllowedBits) {
- Diag(Arg->getSourceRange().getBegin(),
- diag::warn_template_arg_too_large)
- << OldValue.toString(10) << Value.toString(10) << Param->getType()
- << Arg->getSourceRange();
- Diag(Param->getLocation(), diag::note_template_param_here);
- }
- }
- Converted = TemplateArgument(Value,
- ParamType->isEnumeralType()
- ? Context.getCanonicalType(ParamType)
- : IntegerType);
- return Owned(Arg);
- }
- QualType ArgType = Arg->getType();
- DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
- // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
- // from a template argument of type std::nullptr_t to a non-type
- // template parameter of type pointer to object, pointer to
- // function, or pointer-to-member, respectively.
- if (ArgType->isNullPtrType()) {
- if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
- Converted = TemplateArgument((NamedDecl *)0);
- return Owned(Arg);
- }
-
- if (ParamType->isNullPtrType()) {
- llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
- Converted = TemplateArgument(Zero, Context.NullPtrTy);
- return Owned(Arg);
- }
- }
- // Handle pointer-to-function, reference-to-function, and
- // pointer-to-member-function all in (roughly) the same way.
- if (// -- For a non-type template-parameter of type pointer to
- // function, only the function-to-pointer conversion (4.3) is
- // applied. If the template-argument represents a set of
- // overloaded functions (or a pointer to such), the matching
- // function is selected from the set (13.4).
- (ParamType->isPointerType() &&
- ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
- // -- For a non-type template-parameter of type reference to
- // function, no conversions apply. If the template-argument
- // represents a set of overloaded functions, the matching
- // function is selected from the set (13.4).
- (ParamType->isReferenceType() &&
- ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
- // -- For a non-type template-parameter of type pointer to
- // member function, no conversions apply. If the
- // template-argument represents a set of overloaded member
- // functions, the matching member function is selected from
- // the set (13.4).
- (ParamType->isMemberPointerType() &&
- ParamType->getAs<MemberPointerType>()->getPointeeType()
- ->isFunctionType())) {
- if (Arg->getType() == Context.OverloadTy) {
- if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
- true,
- FoundResult)) {
- if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
- return ExprError();
- Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
- ArgType = Arg->getType();
- } else
- return ExprError();
- }
- if (!ParamType->isMemberPointerType()) {
- if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
- ParamType,
- Arg, Converted))
- return ExprError();
- return Owned(Arg);
- }
- bool ObjCLifetimeConversion;
- if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
- false, ObjCLifetimeConversion)) {
- Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
- Arg->getValueKind()).take();
- } else if (!Context.hasSameUnqualifiedType(ArgType,
- ParamType.getNonReferenceType())) {
- // We can't perform this conversion.
- Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_convertible)
- << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
- Diag(Param->getLocation(), diag::note_template_param_here);
- return ExprError();
- }
- if (CheckTemplateArgumentPointerToMember(Arg, Converted))
- return ExprError();
- return Owned(Arg);
- }
- if (ParamType->isPointerType()) {
- // -- for a non-type template-parameter of type pointer to
- // object, qualification conversions (4.4) and the
- // array-to-pointer conversion (4.2) are applied.
- // C++0x also allows a value of std::nullptr_t.
- assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
- "Only object pointers allowed here");
- if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
- ParamType,
- Arg, Converted))
- return ExprError();
- return Owned(Arg);
- }
- if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
- // -- For a non-type template-parameter of type reference to
- // object, no conversions apply. The type referred to by the
- // reference may be more cv-qualified than the (otherwise
- // identical) type of the template-argument. The
- // template-parameter is bound directly to the
- // template-argument, which must be an lvalue.
- assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
- "Only object references allowed here");
- if (Arg->getType() == Context.OverloadTy) {
- if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
- ParamRefType->getPointeeType(),
- true,
- FoundResult)) {
- if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
- return ExprError();
- Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
- ArgType = Arg->getType();
- } else
- return ExprError();
- }
- if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
- ParamType,
- Arg, Converted))
- return ExprError();
- return Owned(Arg);
- }
- // -- For a non-type template-parameter of type pointer to data
- // member, qualification conversions (4.4) are applied.
- assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
- bool ObjCLifetimeConversion;
- if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
- // Types match exactly: nothing more to do here.
- } else if (IsQualificationConversion(ArgType, ParamType, false,
- ObjCLifetimeConversion)) {
- Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
- Arg->getValueKind()).take();
- } else {
- // We can't perform this conversion.
- Diag(Arg->getSourceRange().getBegin(),
- diag::err_template_arg_not_convertible)
- << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
- Diag(Param->getLocation(), diag::note_template_param_here);
- return ExprError();
- }
- if (CheckTemplateArgumentPointerToMember(Arg, Converted))
- return ExprError();
- return Owned(Arg);
- }
- /// \brief Check a template argument against its corresponding
- /// template template parameter.
- ///
- /// This routine implements the semantics of C++ [temp.arg.template].
- /// It returns true if an error occurred, and false otherwise.
- bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
- const TemplateArgumentLoc &Arg) {
- TemplateName Name = Arg.getArgument().getAsTemplate();
- TemplateDecl *Template = Name.getAsTemplateDecl();
- if (!Template) {
- // Any dependent template name is fine.
- assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
- return false;
- }
- // C++0x [temp.arg.template]p1:
- // A template-argument for a template template-parameter shall be
- // the name of a class template or an alias template, expressed as an
- // id-expression. When the template-argument names a class template, only
- // primary class templates are considered when matching the
- // template template argument with the corresponding parameter;
- // partial specializations are not considered even if their
- // parameter lists match that of the template template parameter.
- //
- // Note that we also allow template template parameters here, which
- // will happen when we are dealing with, e.g., class template
- // partial specializations.
- if (!isa<ClassTemplateDecl>(Template) &&
- !isa<TemplateTemplateParmDecl>(Template) &&
- !isa<TypeAliasTemplateDecl>(Template)) {
- assert(isa<FunctionTemplateDecl>(Template) &&
- "Only function templates are possible here");
- Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
- Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
- << Template;
- }
- return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
- Param->getTemplateParameters(),
- true,
- TPL_TemplateTemplateArgumentMatch,
- Arg.getLocation());
- }
- /// \brief Given a non-type template argument that refers to a
- /// declaration and the type of its corresponding non-type template
- /// parameter, produce an expression that properly refers to that
- /// declaration.
- ExprResult
- Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
- QualType ParamType,
- SourceLocation Loc) {
- assert(Arg.getKind() == TemplateArgument::Declaration &&
- "Only declaration template arguments permitted here");
- ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
- if (VD->getDeclContext()->isRecord() &&
- (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
- // If the value is a class member, we might have a pointer-to-member.
- // Determine whether the non-type template template parameter is of
- // pointer-to-member type. If so, we need to build an appropriate
- // expression for a pointer-to-member, since a "normal" DeclRefExpr
- // would refer to the member itself.
- if (ParamType->isMemberPointerType()) {
- QualType ClassType
- = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
- NestedNameSpecifier *Qualifier
- = NestedNameSpecifier::Create(Context, 0, false,
- ClassType.getTypePtr());
- CXXScopeSpec SS;
- SS.MakeTrivial(Context, Qualifier, Loc);
- // The actual value-ness of this is unimportant, but for
- // internal consistency's sake, references to instance methods
- // are r-values.
- ExprValueKind VK = VK_LValue;
- if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
- VK = VK_RValue;
- ExprResult RefExpr = BuildDeclRefExpr(VD,
- VD->getType().getNonReferenceType(),
- VK,
- Loc,
- &SS);
- if (RefExpr.isInvalid())
- return ExprError();
- RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
- // We might need to perform a trailing qualification conversion, since
- // the element type on the parameter could be more qualified than the
- // element type in the expression we constructed.
- bool ObjCLifetimeConversion;
- if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
- ParamType.getUnqualifiedType(), false,
- ObjCLifetimeConversion))
- RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
- assert(!RefExpr.isInvalid() &&
- Context.hasSameType(((Expr*) RefExpr.get())->getType(),
- ParamType.getUnqualifiedType()));
- return move(RefExpr);
- }
- }
- QualType T = VD->getType().getNonReferenceType();
- if (ParamType->isPointerType()) {
- // When the non-type template parameter is a pointer, take the
- // address of the declaration.
- ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
- if (RefExpr.isInvalid())
- return ExprError();
- if (T->isFunctionType() || T->isArrayType()) {
- // Decay functions and arrays.
- RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
- if (RefExpr.isInvalid())
- return ExprError();
- return move(RefExpr);
- }
- // Take the address of everything else
- return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
- }
- ExprValueKind VK = VK_RValue;
- // If the non-type template parameter has reference type, qualify the
- // resulting declaration reference with the extra qualifiers on the
- // type that the reference refers to.
- if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
- VK = VK_LValue;
- T = Context.getQualifiedType(T,
- TargetRef->getPointeeType().getQualifiers());
- }
- return BuildDeclRefExpr(VD, T, VK, Loc);
- }
- /// \brief Construct a new expression that refers to the given
- /// integral template argument with the given source-location
- /// information.
- ///
- /// This routine takes care of the mapping from an integral template
- /// argument (which may have any integral type) to the appropriate
- /// literal value.
- ExprResult
- Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
- SourceLocation Loc) {
- assert(Arg.getKind() == TemplateArgument::Integral &&
- "Operation is only valid for integral template arguments");
- QualType T = Arg.getIntegralType();
- if (T->isAnyCharacterType()) {
- CharacterLiteral::CharacterKind Kind;
- if (T->isWideCharType())
- Kind = CharacterLiteral::Wide;
- else if (T->isChar16Type())
- Kind = CharacterLiteral::UTF16;
- else if (T->isChar32Type())
- Kind = CharacterLiteral::UTF32;
- else
- Kind = CharacterLiteral::Ascii;
- return Owned(new (Context) CharacterLiteral(
- Arg.getAsIntegral()->getZExtValue(),
- Kind, T, Loc));
- }
- if (T->isBooleanType())
- return Owned(new (Context) CXXBoolLiteralExpr(
- Arg.getAsIntegral()->getBoolValue(),
- T, Loc));
- if (T->isNullPtrType())
- return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
-
- // If this is an enum type that we're instantiating, we need to use an integer
- // type the same size as the enumerator. We don't want to build an
- // IntegerLiteral with enum type.
- QualType BT;
- if (const EnumType *ET = T->getAs<EnumType>())
- BT = ET->getDecl()->getIntegerType();
- else
- BT = T;
- Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
- if (T->isEnumeralType()) {
- // FIXME: This is a hack. We need a better way to handle substituted
- // non-type template parameters.
- E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
- Context.getTrivialTypeSourceInfo(T, Loc),
- Loc, Loc);
- }
-
- return Owned(E);
- }
- /// \brief Match two template parameters within template parameter lists.
- static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
- bool Complain,
- Sema::TemplateParameterListEqualKind Kind,
- SourceLocation TemplateArgLoc) {
- // Check the actual kind (type, non-type, template).
- if (Old->getKind() != New->getKind()) {
- if (Complain) {
- unsigned NextDiag = diag::err_template_param_different_kind;
- if (TemplateArgLoc.isValid()) {
- S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
- NextDiag = diag::note_template_param_different_kind;
- }
- S.Diag(New->getLocation(), NextDiag)
- << (Kind != Sema::TPL_TemplateMatch);
- S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
- << (Kind != Sema::TPL_TemplateMatch);
- }
- return false;
- }
- // Check that both are parameter packs are neither are parameter packs.
- // However, if we are matching a template template argument to a
- // template template parameter, the template template parameter can have
- // a parameter pack where the template template argument does not.
- if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
- !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
- Old->isTemplateParameterPack())) {
- if (Complain) {
- unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
- if (TemplateArgLoc.isValid()) {
- S.Diag(TemplateArgLoc,
- diag::err_template_arg_template_params_mismatch);
- NextDiag = diag::note_template_parameter_pack_non_pack;
- }
- unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
- : isa<NonTypeTemplateParmDecl>(New)? 1
- : 2;
- S.Diag(New->getLocation(), NextDiag)
- << ParamKind << New->isParameterPack();
- S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
- << ParamKind << Old->isParameterPack();
- }
- return false;
- }
- // For non-type template parameters, check the type of the parameter.
- if (NonTypeTemplateParmDecl *OldNTTP
- = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
- NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
- // If we are matching a template template argument to a template
- // template parameter and one of the non-type template parameter types
- // is dependent, then we must wait until template instantiation time
- // to actually compare the arguments.
- if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
- (OldNTTP->getType()->isDependentType() ||
- NewNTTP->getType()->isDependentType()))
- return true;
- if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
- if (Complain) {
- unsigned NextDiag = diag::err_template_nontype_parm_different_type;
- if (TemplateArgLoc.isValid()) {
- S.Diag(TemplateArgLoc,
- diag::err_template_arg_template_params_mismatch);
- NextDiag = diag::note_template_nontype_parm_different_type;
- }
- S.Diag(NewNTTP->getLocation(), NextDiag)
- << NewNTTP->getType()
- << (Kind != Sema::TPL_TemplateMatch);
- S.Diag(OldNTTP->getLocation(),
- diag::note_template_nontype_parm_prev_declaration)
- << OldNTTP->getType();
- }
- return false;
- }
- return true;
- }
- // For template template parameters, check the template parameter types.
- // The template parameter lists of template template
- // parameters must agree.
- if (TemplateTemplateParmDecl *OldTTP
- = dyn_cast<TemplateTemplateParmDecl>(Old)) {
- TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
- return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
- OldTTP->getTemplateParameters(),
- Complain,
- (Kind == Sema::TPL_TemplateMatch
- ? Sema::TPL_TemplateTemplateParmMatch
- : Kind),
- TemplateArgLoc);
- }
- return true;
- }
- /// \brief Diagnose a known arity mismatch when comparing template argument
- /// lists.
- static
- void DiagnoseTemplateParameterListArityMismatch(Sema &S,
- TemplateParameterList *New,
- TemplateParameterList *Old,
- Sema::TemplateParameterListEqualKind Kind,
- SourceLocation TemplateArgLoc) {
- unsigned NextDiag = diag::err_template_param_list_different_arity;
- if (TemplateArgLoc.isValid()) {
- S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
- NextDiag = diag::note_template_param_list_different_arity;
- }
- S.Diag(New->getTemplateLoc(), NextDiag)
- << (New->size() > Old->size())
- << (Kind != Sema::TPL_TemplateMatch)
- << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
- S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
- << (Kind != Sema::TPL_TemplateMatch)
- << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
- }
- /// \brief Determine whether the given template parameter lists are
- /// equivalent.
- ///
- /// \param New The new template parameter list, typically written in the
- /// source code as part of a new template declaration.
- ///
- /// \param Old The old template parameter list, typically found via
- /// name lookup of the template declared with this template parameter
- /// list.
- ///
- /// \param Complain If true, this routine will produce a diagnostic if
- /// the template parameter lists are not equivalent.
- ///
- /// \param Kind describes how we are to match the template parameter lists.
- ///
- /// \param TemplateArgLoc If this source location is valid, then we
- /// are actually checking the template parameter list of a template
- /// argument (New) against the template parameter list of its
- /// corresponding template template parameter (Old). We produce
- /// slightly different diagnostics in this scenario.
- ///
- /// \returns True if the template parameter lists are equal, false
- /// otherwise.
- bool
- Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
- TemplateParameterList *Old,
- bool Complain,
- TemplateParameterListEqualKind Kind,
- SourceLocation TemplateArgLoc) {
- if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
- if (Complain)
- DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
- TemplateArgLoc);
- return false;
- }
- // C++0x [temp.arg.template]p3:
- // A template-argument matches a template template-parameter (call it P)
- // when each of the template parameters in the template-parameter-list of
- // the template-argument's corresponding class template or alias template
- // (call it A) matches the corresponding template parameter in the
- // template-parameter-list of P. [...]
- TemplateParameterList::iterator NewParm = New->begin();
- TemplateParameterList::iterator NewParmEnd = New->end();
- for (TemplateParameterList::iterator OldParm = Old->begin(),
- OldParmEnd = Old->end();
- OldParm != OldParmEnd; ++OldParm) {
- if (Kind != TPL_TemplateTemplateArgumentMatch ||
- !(*OldParm)->isTemplateParameterPack()) {
- if (NewParm == NewParmEnd) {
- if (Complain)
- DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
- TemplateArgLoc);
- return false;
- }
- if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
- Kind, TemplateArgLoc))
- return false;
- ++NewParm;
- continue;
- }
- // C++0x [temp.arg.template]p3:
- // [...] When P's template- parameter-list contains a template parameter
- // pack (14.5.3), the template parameter pack will match zero or more
- // template parameters or template parameter packs in the
- // template-parameter-list of A with the same type and form as the
- // template parameter pack in P (ignoring whether those template
- // parameters are template parameter packs).
- for (; NewParm != NewParmEnd; ++NewParm) {
- if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
- Kind, TemplateArgLoc))
- return false;
- }
- }
- // Make sure we exhausted all of the arguments.
- if (NewParm != NewParmEnd) {
- if (Complain)
- DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
- TemplateArgLoc);
- return false;
- }
- return true;
- }
- /// \brief Check whether a template can be declared within this scope.
- ///
- /// If the template declaration is valid in this scope, returns
- /// false. Otherwise, issues a diagnostic and returns true.
- bool
- Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
- if (!S)
- return false;
- // Find the nearest enclosing declaration scope.
- while ((S->getFlags() & Scope::DeclScope) == 0 ||
- (S->getFlags() & Scope::TemplateParamScope) != 0)
- S = S->getParent();
- // C++ [temp]p2:
- // A template-declaration can appear only as a namespace scope or
- // class scope declaration.
- DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
- if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
- cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
- return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
- << TemplateParams->getSourceRange();
- while (Ctx && isa<LinkageSpecDecl>(Ctx))
- Ctx = Ctx->getParent();
- if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
- return false;
- return Diag(TemplateParams->getTemplateLoc(),
- diag::err_template_outside_namespace_or_class_scope)
- << TemplateParams->getSourceRange();
- }
- /// \brief Determine what kind of template specialization the given declaration
- /// is.
- static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
- if (!D)
- return TSK_Undeclared;
- if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
- return Record->getTemplateSpecializationKind();
- if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
- return Function->getTemplateSpecializationKind();
- if (VarDecl *Var = dyn_cast<VarDecl>(D))
- return Var->getTemplateSpecializationKind();
- return TSK_Undeclared;
- }
- /// \brief Check whether a specialization is well-formed in the current
- /// context.
- ///
- /// This routine determines whether a template specialization can be declared
- /// in the current context (C++ [temp.expl.spec]p2).
- ///
- /// \param S the semantic analysis object for which this check is being
- /// performed.
- ///
- /// \param Specialized the entity being specialized or instantiated, which
- /// may be a kind of template (class template, function template, etc.) or
- /// a member of a class template (member function, static data member,
- /// member class).
- ///
- /// \param PrevDecl the previous declaration of this entity, if any.
- ///
- /// \param Loc the location of the explicit specialization or instantiation of
- /// this entity.
- ///
- /// \param IsPartialSpecialization whether this is a partial specialization of
- /// a class template.
- ///
- /// \returns true if there was an error that we cannot recover from, false
- /// otherwise.
- static bool CheckTemplateSpecializationScope(Sema &S,
- NamedDecl *Specialized,
- NamedDecl *PrevDecl,
- SourceLocation Loc,
- bool IsPartialSpecialization) {
- // Keep these "kind" numbers in sync with the %select statements in the
- // various diagnostics emitted by this routine.
- int EntityKind = 0;
- if (isa<ClassTemplateDecl>(Specialized))
- EntityKind = IsPartialSpecialization? 1 : 0;
- else if (isa<FunctionTemplateDecl>(Specialized))
- EntityKind = 2;
- else if (isa<CXXMethodDecl>(Specialized))
- EntityKind = 3;
- else if (isa<VarDecl>(Specialized))
- EntityKind = 4;
- else if (isa<RecordDecl>(Specialized))
- EntityKind = 5;
- else {
- S.Diag(Loc, diag::err_template_spec_unknown_kind);
- S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
- return true;
- }
- // C++ [temp.expl.spec]p2:
- // An explicit specialization shall be declared in the namespace
- // of which the template is a member, or, for member templates, in
- // the namespace of which the enclosing class or enclosing class
- // template is a member. An explicit specialization of a member
- // function, member class or static data member of a class
- // template shall be declared in the namespace of which the class
- // template is a member. Such a declaration may also be a
- // definition. If the declaration is not a definition, the
- // specialization may be defined later in the name- space in which
- // the explicit specialization was declared, or in a namespace
- // that encloses the one in which the explicit specialization was
- // declared.
- if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
- S.Diag(Loc, diag::err_template_spec_decl_function_scope)
- << Specialized;
- return true;
- }
- if (S.CurContext->isRecord() && !IsPartialSpecialization) {
- if (S.getLangOptions().MicrosoftExt) {
- // Do not warn for class scope explicit specialization during
- // instantiation, warning was already emitted during pattern
- // semantic analysis.
- if (!S.ActiveTemplateInstantiations.size())
- S.Diag(Loc, diag::ext_function_specialization_in_class)
- << Specialized;
- } else {
- S.Diag(Loc, diag::err_template_spec_decl_class_scope)
- << Specialized;
- return true;
- }
- }
- if (S.CurContext->isRecord() &&
- !S.CurContext->Equals(Specialized->getDeclContext())) {
- // Make sure that we're specializing in the right record context.
- // Otherwise, things can go horribly wrong.
- S.Diag(Loc, diag::err_template_spec_decl_class_scope)
- << Specialized;
- return true;
- }
-
- // C++ [temp.class.spec]p6:
- // A class template partial specialization may be declared or redeclared
- // in any namespace scope in which its definition may be defined (14.5.1
- // and 14.5.2).
- bool ComplainedAboutScope = false;
- DeclContext *SpecializedContext
- = Specialized->getDeclContext()->getEnclosingNamespaceContext();
- DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
- if ((!PrevDecl ||
- getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
- getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
- // C++ [temp.exp.spec]p2:
- // An explicit specialization shall be declared in the namespace of which
- // the template is a member, or, for member templates, in the namespace
- // of which the enclosing class or enclosing class template is a member.
- // An explicit specialization of a member function, member class or
- // static data member of a class template shall be declared in the
- // namespace of which the class template is a member.
- //
- // C++0x [temp.expl.spec]p2:
- // An explicit specialization shall be declared in a namespace enclosing
- // the specialized template.
- if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
- bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
- if (isa<TranslationUnitDecl>(SpecializedContext)) {
- assert(!IsCPlusPlus0xExtension &&
- "DC encloses TU but isn't in enclosing namespace set");
- S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
- << EntityKind << Specialized;
- } else if (isa<NamespaceDecl>(SpecializedContext)) {
- int Diag;
- if (!IsCPlusPlus0xExtension)
- Diag = diag::err_template_spec_decl_out_of_scope;
- else if (!S.getLangOptions().CPlusPlus0x)
- Diag = diag::ext_template_spec_decl_out_of_scope;
- else
- Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
- S.Diag(Loc, Diag)
- << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
- }
- S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
- ComplainedAboutScope =
- !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
- }
- }
- // Make sure that this redeclaration (or definition) occurs in an enclosing
- // namespace.
- // Note that HandleDeclarator() performs this check for explicit
- // specializations of function templates, static data members, and member
- // functions, so we skip the check here for those kinds of entities.
- // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
- // Should we refactor that check, so that it occurs later?
- if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
- !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
- isa<FunctionDecl>(Specialized))) {
- if (isa<TranslationUnitDecl>(SpecializedContext))
- S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
- << EntityKind << Specialized;
- else if (isa<NamespaceDecl>(SpecializedContext))
- S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
- << EntityKind << Specialized
- << cast<NamedDecl>(SpecializedContext);
- S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
- }
- // FIXME: check for specialization-after-instantiation errors and such.
- return false;
- }
- /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
- /// that checks non-type template partial specialization arguments.
- static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
- NonTypeTemplateParmDecl *Param,
- const TemplateArgument *Args,
- unsigned NumArgs) {
- for (unsigned I = 0; I != NumArgs; ++I) {
- if (Args[I].getKind() == TemplateArgument::Pack) {
- if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
- Args[I].pack_begin(),
- Args[I].pack_size()))
- return true;
- continue;
- }
- Expr *ArgExpr = Args[I].getAsExpr();
- if (!ArgExpr) {
- continue;
- }
- // We can have a pack expansion of any of the bullets below.
- if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
- ArgExpr = Expansion->getPattern();
- // Strip off any implicit casts we added as part of type checking.
- while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
- ArgExpr = ICE->getSubExpr();
- // C++ [temp.class.spec]p8:
- // A non-type argument is non-specialized if it is the name of a
- // non-type parameter. All other non-type arguments are
- // specialized.
- //
- // Below, we check the two conditions that only apply to
- // specialized non-type arguments, so skip any non-specialized
- // arguments.
- if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
- if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
- continue;
- // C++ [temp.class.spec]p9:
- // Within the argument list of a class template partial
- // specialization, the following restrictions apply:
- // -- A partially specialized non-type argument expression
- // shall not involve a template parameter of the partial
- // specialization except when the argument expression is a
- // simple identifier.
- if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
- S.Diag(ArgExpr->getLocStart(),
- diag::err_dependent_non_type_arg_in_partial_spec)
- << ArgExpr->getSourceRange();
- return true;
- }
- // -- The type of a template parameter corresponding to a
- // specialized non-type argument shall not be dependent on a
- // parameter of the specialization.
- if (Param->getType()->isDependentType()) {
- S.Diag(ArgExpr->getLocStart(),
- diag::err_dependent_typed_non_type_arg_in_partial_spec)
- << Param->getType()
- << ArgExpr->getSourceRange();
- S.Diag(Param->getLocation(), diag::note_template_param_here);
- return true;
- }
- }
- return false;
- }
- /// \brief Check the non-type template arguments of a class template
- /// partial specialization according to C++ [temp.class.spec]p9.
- ///
- /// \param TemplateParams the template parameters of the primary class
- /// template.
- ///
- /// \param TemplateArg the template arguments of the class template
- /// partial specialization.
- ///
- /// \returns true if there was an error, false otherwise.
- static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
- TemplateParameterList *TemplateParams,
- SmallVectorImpl<TemplateArgument> &TemplateArgs) {
- const TemplateArgument *ArgList = TemplateArgs.data();
- for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
- NonTypeTemplateParmDecl *Param
- = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
- if (!Param)
- continue;
- if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
- &ArgList[I], 1))
- return true;
- }
- return false;
- }
- DeclResult
- Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
- TagUseKind TUK,
- SourceLocation KWLoc,
- SourceLocation ModulePrivateLoc,
- CXXScopeSpec &SS,
- TemplateTy TemplateD,
- SourceLocation TemplateNameLoc,
- SourceLocation LAngleLoc,
- ASTTemplateArgsPtr TemplateArgsIn,
- SourceLocation RAngleLoc,
- AttributeList *Attr,
- MultiTemplateParamsArg TemplateParameterLists) {
- assert(TUK != TUK_Reference && "References are not specializations");
- // NOTE: KWLoc is the location of the tag keyword. This will instead
- // store the location of the outermost template keyword in the declaration.
- SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
- ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
- // Find the class template we're specializing
- TemplateName Name = TemplateD.getAsVal<TemplateName>();
- ClassTemplateDecl *ClassTemplate
- = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
- if (!ClassTemplate) {
- Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
- << (Name.getAsTemplateDecl() &&
- isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
- return true;
- }
- bool isExplicitSpecialization = false;
- bool isPartialSpecialization = false;
- // Check the validity of the template headers that introduce this
- // template.
- // FIXME: We probably shouldn't complain about these headers for
- // friend declarations.
- bool Invalid = false;
- TemplateParameterList *TemplateParams
- = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
- TemplateNameLoc,
- SS,
- (TemplateParameterList**)TemplateParameterLists.get(),
- TemplateParameterLists.size(),
- TUK == TUK_Friend,
- isExplicitSpecialization,
- Invalid);
- if (Invalid)
- return true;
- if (TemplateParams && TemplateParams->size() > 0) {
- isPartialSpecialization = true;
- if (TUK == TUK_Friend) {
- Diag(KWLoc, diag::err_partial_specialization_friend)
- << SourceRange(LAngleLoc, RAngleLoc);
- return true;
- }
- // C++ [temp.class.spec]p10:
- // The template parameter list of a specialization shall not
- // contain default template argument values.
- for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
- Decl *Param = TemplateParams->getParam(I);
- if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
- if (TTP->hasDefaultArgument()) {
- Diag(TTP->getDefaultArgumentLoc(),
- diag::err_default_arg_in_partial_spec);
- TTP->removeDefaultArgument();
- }
- } else if (NonTypeTemplateParmDecl *NTTP
- = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
- if (Expr *DefArg = NTTP->getDefaultArgument()) {
- Diag(NTTP->getDefaultArgumentLoc(),
- diag::err_default_arg_in_partial_spec)
- << DefArg->getSourceRange();
- NTTP->removeDefaultArgument();
- }
- } else {
- TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
- if (TTP->hasDefaultArgument()) {
- Diag(TTP->getDefaultArgument().getLocation(),
- diag::err_default_arg_in_partial_spec)
- << TTP->getDefaultArgument().getSourceRange();
- TTP->removeDefaultArgument();
- }
- }
- }
- } else if (TemplateParams) {
- if (TUK == TUK_Friend)
- Diag(KWLoc, diag::err_template_spec_friend)
- << FixItHint::CreateRemoval(
- SourceRange(TemplateParams->getTemplateLoc(),
- TemplateParams->getRAngleLoc()))
- << SourceRange(LAngleLoc, RAngleLoc);
- else
- isExplicitSpecialization = true;
- } else if (TUK != TUK_Friend) {
- Diag(KWLoc, diag::err_template_spec_needs_header)
- << FixItHint::CreateInsertion(KWLoc, "template<> ");
- isExplicitSpecialization = true;
- }
- // Check that the specialization uses the same tag kind as the
- // original template.
- TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
- assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
- if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
- Kind, TUK == TUK_Definition, KWLoc,
- *ClassTemplate->getIdentifier())) {
- Diag(KWLoc, diag::err_use_with_wrong_tag)
- << ClassTemplate
- << FixItHint::CreateReplacement(KWLoc,
- ClassTemplate->getTemplatedDecl()->getKindName());
- Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
- diag::note_previous_use);
- Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
- }
- // Translate the parser's template argument list in our AST format.
- TemplateArgumentListInfo TemplateArgs;
- TemplateArgs.setLAngleLoc(LAngleLoc);
- TemplateArgs.setRAngleLoc(RAngleLoc);
- translateTemplateArguments(TemplateArgsIn, TemplateArgs);
- // Check for unexpanded parameter packs in any of the template arguments.
- for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
- if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
- UPPC_PartialSpecialization))
- return true;
- // Check that the template argument list is well-formed for this
- // template.
- SmallVector<TemplateArgument, 4> Converted;
- if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
- TemplateArgs, false, Converted))
- return true;
- // Find the class template (partial) specialization declaration that
- // corresponds to these arguments.
- if (isPartialSpecialization) {
- if (CheckClassTemplatePartialSpecializationArgs(*this,
- ClassTemplate->getTemplateParameters(),
- Converted))
- return true;
- bool InstantiationDependent;
- if (!Name.isDependent() &&
- !TemplateSpecializationType::anyDependentTemplateArguments(
- TemplateArgs.getArgumentArray(),
- TemplateArgs.size(),
- InstantiationDependent)) {
- Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
- << ClassTemplate->getDeclName();
- isPartialSpecialization = false;
- }
- }
- void *InsertPos = 0;
- ClassTemplateSpecializationDecl *PrevDecl = 0;
- if (isPartialSpecialization)
- // FIXME: Template parameter list matters, too
- PrevDecl
- = ClassTemplate->findPartialSpecialization(Converted.data(),
- Converted.size(),
- InsertPos);
- else
- PrevDecl
- = ClassTemplate->findSpecialization(Converted.data(),
- Converted.size(), InsertPos);
- ClassTemplateSpecializationDecl *Specialization = 0;
- // Check whether we can declare a class template specialization in
- // the current scope.
- if (TUK != TUK_Friend &&
- CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
- TemplateNameLoc,
- isPartialSpecialization))
- return true;
- // The canonical type
- QualType CanonType;
- if (PrevDecl &&
- (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
- TUK == TUK_Friend)) {
- // Since the only prior class template specialization with these
- // arguments was referenced but not declared, or we're only
- // referencing this specialization as a friend, reuse that
- // declaration node as our own, updating its source location and
- // the list of outer template parameters to reflect our new declaration.
- Specialization = PrevDecl;
- Specialization->setLocation(TemplateNameLoc);
- if (TemplateParameterLists.size() > 0) {
- Specialization->setTemplateParameterListsInfo(Context,
- TemplateParameterLists.size(),
- (TemplateParameterList**) TemplateParameterLists.release());
- }
- PrevDecl = 0;
- CanonType = Context.getTypeDeclType(Specialization);
- } else if (isPartialSpecialization) {
- // Build the canonical type that describes the converted template
- // arguments of the class template partial specialization.
- TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
- CanonType = Context.getTemplateSpecializationType(CanonTemplate,
- Converted.data(),
- Converted.size());
- if (Context.hasSameType(CanonType,
- ClassTemplate->getInjectedClassNameSpecialization())) {
- // C++ [temp.class.spec]p9b3:
- //
- // -- The argument list of the specialization shall not be identical
- // to the implicit argument list of the primary template.
- Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
- << (TUK == TUK_Definition)
- << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
- return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
- ClassTemplate->getIdentifier(),
- TemplateNameLoc,
- Attr,
- TemplateParams,
- AS_none, /*ModulePrivateLoc=*/SourceLocation(),
- TemplateParameterLists.size() - 1,
- (TemplateParameterList**) TemplateParameterLists.release());
- }
- // Create a new class template partial specialization declaration node.
- ClassTemplatePartialSpecializationDecl *PrevPartial
- = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
- unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
- : ClassTemplate->getNextPartialSpecSequenceNumber();
- ClassTemplatePartialSpecializationDecl *Partial
- = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
- ClassTemplate->getDeclContext(),
- KWLoc, TemplateNameLoc,
- TemplateParams,
- ClassTemplate,
- Converted.data(),
- Converted.size(),
- TemplateArgs,
- CanonType,
- PrevPartial,
- SequenceNumber);
- SetNestedNameSpecifier(Partial, SS);
- if (TemplateParameterLists.size() > 1 && SS.isSet()) {
- Partial->setTemplateParameterListsInfo(Context,
- TemplateParameterLists.size() - 1,
- (TemplateParameterList**) TemplateParameterLists.release());
- }
- if (!PrevPartial)
- ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
- Specialization = Partial;
- // If we are providing an explicit specialization of a member class
- // template specialization, make a note of that.
- if (PrevPartial && PrevPartial->getInstantiatedFromMember())
- PrevPartial->setMemberSpecialization();
- // Check that all of the template parameters of the class template
- // partial specialization are deducible from the template
- // arguments. If not, this class template partial specialization
- // will never be used.
- llvm::SmallBitVector DeducibleParams(TemplateParams->size());
- MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
- TemplateParams->getDepth(),
- DeducibleParams);
- if (!DeducibleParams.all()) {
- unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
- Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
- << (NumNonDeducible > 1)
- << SourceRange(TemplateNameLoc, RAngleLoc);
- for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
- if (!DeducibleParams[I]) {
- NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
- if (Param->getDeclName())
- Diag(Param->getLocation(),
- diag::note_partial_spec_unused_parameter)
- << Param->getDeclName();
- else
- Diag(Param->getLocation(),
- diag::note_partial_spec_unused_parameter)
- << "<anonymous>";
- }
- }
- }
- } else {
- // Create a new class template specialization declaration node for
- // this explicit specialization or friend declaration.
- Specialization
- = ClassTemplateSpecializationDecl::Create(Context, Kind,
- ClassTemplate->getDeclContext(),
- KWLoc, TemplateNameLoc,
- ClassTemplate,
- Converted.data(),
- Converted.size(),
- PrevDecl);
- SetNestedNameSpecifier(Specialization, SS);
- if (TemplateParameterLists.size() > 0) {
- Specialization->setTemplateParameterListsInfo(Context,
- TemplateParameterLists.size(),
- (TemplateParameterList**) TemplateParameterLists.release());
- }
- if (!PrevDecl)
- ClassTemplate->AddSpecialization(Specialization, InsertPos);
- CanonType = Context.getTypeDeclType(Specialization);
- }
- // C++ [temp.expl.spec]p6:
- // If a template, a member template or the member of a class template is
- // explicitly specialized then that specialization shall be declared
- // before the first use of that specialization that would cause an implicit
- // instantiation to take place, in every translation unit in which such a
- // use occurs; no diagnostic is required.
- if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
- bool Okay = false;
- for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
- // Is there any previous explicit specialization declaration?
- if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
- Okay = true;
- break;
- }
- }
- if (!Okay) {
- SourceRange Range(TemplateNameLoc, RAngleLoc);
- Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
- << Context.getTypeDeclType(Specialization) << Range;
- Diag(PrevDecl->getPointOfInstantiation(),
- diag::note_instantiation_required_here)
- << (PrevDecl->getTemplateSpecializationKind()
- != TSK_ImplicitInstantiation);
- return true;
- }
- }
- // If this is not a friend, note that this is an explicit specialization.
- if (TUK != TUK_Friend)
- Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
- // Check that this isn't a redefinition of this specialization.
- if (TUK == TUK_Definition) {
- if (RecordDecl *Def = Specialization->getDefinition()) {
- SourceRange Range(TemplateNameLoc, RAngleLoc);
- Diag(TemplateNameLoc, diag::err_redefinition)
- << Context.getTypeDeclType(Specialization) << Range;
- Diag(Def->getLocation(), diag::note_previous_definition);
- Specialization->setInvalidDecl();
- return true;
- }
- }
- if (Attr)
- ProcessDeclAttributeList(S, Specialization, Attr);
- if (ModulePrivateLoc.isValid())
- Diag(Specialization->getLocation(), diag::err_module_private_specialization)
- << (isPartialSpecialization? 1 : 0)
- << FixItHint::CreateRemoval(ModulePrivateLoc);
-
- // Build the fully-sugared type for this class template
- // specialization as the user wrote in the specialization
- // itself. This means that we'll pretty-print the type retrieved
- // from the specialization's declaration the way that the user
- // actually wrote the specialization, rather than formatting the
- // name based on the "canonical" representation used to store the
- // template arguments in the specialization.
- TypeSourceInfo *WrittenTy
- = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
- TemplateArgs, CanonType);
- if (TUK != TUK_Friend) {
- Specialization->setTypeAsWritten(WrittenTy);
- Specialization->setTemplateKeywordLoc(TemplateKWLoc);
- }
- TemplateArgsIn.release();
- // C++ [temp.expl.spec]p9:
- // A template explicit specialization is in the scope of the
- // namespace in which the template was defined.
- //
- // We actually implement this paragraph where we set the semantic
- // context (in the creation of the ClassTemplateSpecializationDecl),
- // but we also maintain the lexical context where the actual
- // definition occurs.
- Specialization->setLexicalDeclContext(CurContext);
- // We may be starting the definition of this specialization.
- if (TUK == TUK_Definition)
- Specialization->startDefinition();
- if (TUK == TUK_Friend) {
- FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
- TemplateNameLoc,
- WrittenTy,
- /*FIXME:*/KWLoc);
- Friend->setAccess(AS_public);
- CurContext->addDecl(Friend);
- } else {
- // Add the specialization into its lexical context, so that it can
- // be seen when iterating through the list of declarations in that
- // context. However, specializations are not found by name lookup.
- CurContext->addDecl(Specialization);
- }
- return Specialization;
- }
- Decl *Sema::ActOnTemplateDeclarator(Scope *S,
- MultiTemplateParamsArg TemplateParameterLists,
- Declarator &D) {
- return HandleDeclarator(S, D, move(TemplateParameterLists));
- }
- Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
- MultiTemplateParamsArg TemplateParameterLists,
- Declarator &D) {
- assert(getCurFunctionDecl() == 0 && "Function parsing confused");
- DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
- if (FTI.hasPrototype) {
- // FIXME: Diagnose arguments without names in C.
- }
- Scope *ParentScope = FnBodyScope->getParent();
- D.setFunctionDefinitionKind(FDK_Definition);
- Decl *DP = HandleDeclarator(ParentScope, D,
- move(TemplateParameterLists));
- if (FunctionTemplateDecl *FunctionTemplate
- = dyn_cast_or_null<FunctionTemplateDecl>(DP))
- return ActOnStartOfFunctionDef(FnBodyScope,
- FunctionTemplate->getTemplatedDecl());
- if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
- return ActOnStartOfFunctionDef(FnBodyScope, Function);
- return 0;
- }
- /// \brief Strips various properties off an implicit instantiation
- /// that has just been explicitly specialized.
- static void StripImplicitInstantiation(NamedDecl *D) {
- D->dropAttrs();
- if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
- FD->setInlineSpecified(false);
- }
- }
- /// \brief Compute the diagnostic location for an explicit instantiation
- // declaration or definition.
- static SourceLocation DiagLocForExplicitInstantiation(
- NamedDecl* D, SourceLocation PointOfInstantiation) {
- // Explicit instantiations following a specialization have no effect and
- // hence no PointOfInstantiation. In that case, walk decl backwards
- // until a valid name loc is found.
- SourceLocation PrevDiagLoc = PointOfInstantiation;
- for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
- Prev = Prev->getPreviousDecl()) {
- PrevDiagLoc = Prev->getLocation();
- }
- assert(PrevDiagLoc.isValid() &&
- "Explicit instantiation without point of instantiation?");
- return PrevDiagLoc;
- }
- /// \brief Diagnose cases where we have an explicit template specialization
- /// before/after an explicit template instantiation, producing diagnostics
- /// for those cases where they are required and determining whether the
- /// new specialization/instantiation will have any effect.
- ///
- /// \param NewLoc the location of the new explicit specialization or
- /// instantiation.
- ///
- /// \param NewTSK the kind of the new explicit specialization or instantiation.
- ///
- /// \param PrevDecl the previous declaration of the entity.
- ///
- /// \param PrevTSK the kind of the old explicit specialization or instantiatin.
- ///
- /// \param PrevPointOfInstantiation if valid, indicates where the previus
- /// declaration was instantiated (either implicitly or explicitly).
- ///
- /// \param HasNoEffect will be set to true to indicate that the new
- /// specialization or instantiation has no effect and should be ignored.
- ///
- /// \returns true if there was an error that should prevent the introduction of
- /// the new declaration into the AST, false otherwise.
- bool
- Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
- TemplateSpecializationKind NewTSK,
- NamedDecl *PrevDecl,
- TemplateSpecializationKind PrevTSK,
- SourceLocation PrevPointOfInstantiation,
- bool &HasNoEffect) {
- HasNoEffect = false;
- switch (NewTSK) {
- case TSK_Undeclared:
- case TSK_ImplicitInstantiation:
- llvm_unreachable("Don't check implicit instantiations here");
- case TSK_ExplicitSpecialization:
- switch (PrevTSK) {
- case TSK_Undeclared:
- case TSK_ExplicitSpecialization:
- // Okay, we're just specializing something that is either already
- // explicitly specialized or has merely been mentioned without any
- // instantiation.
- return false;
- case TSK_ImplicitInstantiation:
- if (PrevPointOfInstantiation.isInvalid()) {
- // The declaration itself has not actually been instantiated, so it is
- // still okay to specialize it.
- StripImplicitInstantiation(PrevDecl);
- return false;
- }
- // Fall through
- case TSK_ExplicitInstantiationDeclaration:
- case TSK_ExplicitInstantiationDefinition:
- assert((PrevTSK == TSK_ImplicitInstantiation ||
- PrevPointOfInstantiation.isValid()) &&
- "Explicit instantiation without point of instantiation?");
- // C++ [temp.expl.spec]p6:
- // If a template, a member template or the member of a class template
- // is explicitly specialized then that specialization shall be declared
- // before the first use of that specialization that would cause an
- // implicit instantiation to take place, in every translation unit in
- // which such a use occurs; no diagnostic is required.
- for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
- // Is there any previous explicit specialization declaration?
- if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
- return false;
- }
- Diag(NewLoc, diag::err_specialization_after_instantiation)
- << PrevDecl;
- Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
- << (PrevTSK != TSK_ImplicitInstantiation);
- return true;
- }
- case TSK_ExplicitInstantiationDeclaration:
- switch (PrevTSK) {
- case TSK_ExplicitInstantiationDeclaration:
- // This explicit instantiation declaration is redundant (that's okay).
- HasNoEffect = true;
- return false;
- case TSK_Undeclared:
- case TSK_ImplicitInstantiation:
- // We're explicitly instantiating something that may have already been
- // implicitly instantiated; that's fine.
- return false;
- case TSK_ExplicitSpecialization:
- // C++0x [temp.explicit]p4:
- // For a given set of template parameters, if an explicit instantiation
- // of a template appears after a declaration of an explicit
- // specialization for that template, the explicit instantiation has no
- // effect.
- HasNoEffect = true;
- return false;
- case TSK_ExplicitInstantiationDefinition:
- // C++0x [temp.explicit]p10:
- // If an entity is the subject of both an explicit instantiation
- // declaration and an explicit instantiation definition in the same
- // translation unit, the definition shall follow the declaration.
- Diag(NewLoc,
- diag::err_explicit_instantiation_declaration_after_definition);
- // Explicit instantiations following a specialization have no effect and
- // hence no PrevPointOfInstantiation. In that case, walk decl backwards
- // until a valid name loc is found.
- Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
- diag::note_explicit_instantiation_definition_here);
- HasNoEffect = true;
- return false;
- }
- case TSK_ExplicitInstantiationDefinition:
- switch (PrevTSK) {
- case TSK_Undeclared:
- case TSK_ImplicitInstantiation:
- // We're explicitly instantiating something that may have already been
- // implicitly instantiated; that's fine.
- return false;
- case TSK_ExplicitSpecialization:
- // C++ DR 259, C++0x [temp.explicit]p4:
- // For a given set of template parameters, if an explicit
- // instantiation of a template appears after a declaration of
- // an explicit specialization for that template, the explicit
- // instantiation has no effect.
- //
- // In C++98/03 mode, we only give an extension warning here, because it
- // is not harmful to try to explicitly instantiate something that
- // has been explicitly specialized.
- Diag(NewLoc, getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
- diag::ext_explicit_instantiation_after_specialization)
- << PrevDecl;
- Diag(PrevDecl->getLocation(),
- diag::note_previous_template_specialization);
- HasNoEffect = true;
- return false;
- case TSK_ExplicitInstantiationDeclaration:
- // We're explicity instantiating a definition for something for which we
- // were previously asked to suppress instantiations. That's fine.
- // C++0x [temp.explicit]p4:
- // For a given set of template parameters, if an explicit instantiation
- // of a template appears after a declaration of an explicit
- // specialization for that template, the explicit instantiation has no
- // effect.
- for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
- // Is there any previous explicit specialization declaration?
- if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
- HasNoEffect = true;
- break;
- }
- }
- return false;
- case TSK_ExplicitInstantiationDefinition:
- // C++0x [temp.spec]p5:
- // For a given template and a given set of template-arguments,
- // - an explicit instantiation definition shall appear at most once
- // in a program,
- Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
- << PrevDecl;
- Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
- diag::note_previous_explicit_instantiation);
- HasNoEffect = true;
- return false;
- }
- }
- llvm_unreachable("Missing specialization/instantiation case?");
- }
- /// \brief Perform semantic analysis for the given dependent function
- /// template specialization. The only possible way to get a dependent
- /// function template specialization is with a friend declaration,
- /// like so:
- ///
- /// template <class T> void foo(T);
- /// template <class T> class A {
- /// friend void foo<>(T);
- /// };
- ///
- /// There really isn't any useful analysis we can do here, so we
- /// just store the information.
- bool
- Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
- const TemplateArgumentListInfo &ExplicitTemplateArgs,
- LookupResult &Previous) {
- // Remove anything from Previous that isn't a function template in
- // the correct context.
- DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
- LookupResult::Filter F = Previous.makeFilter();
- while (F.hasNext()) {
- NamedDecl *D = F.next()->getUnderlyingDecl();
- if (!isa<FunctionTemplateDecl>(D) ||
- !FDLookupContext->InEnclosingNamespaceSetOf(
- D->getDeclContext()->getRedeclContext()))
- F.erase();
- }
- F.done();
- // Should this be diagnosed here?
- if (Previous.empty()) return true;
- FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
- ExplicitTemplateArgs);
- return false;
- }
- /// \brief Perform semantic analysis for the given function template
- /// specialization.
- ///
- /// This routine performs all of the semantic analysis required for an
- /// explicit function template specialization. On successful completion,
- /// the function declaration \p FD will become a function template
- /// specialization.
- ///
- /// \param FD the function declaration, which will be updated to become a
- /// function template specialization.
- ///
- /// \param ExplicitTemplateArgs the explicitly-provided template arguments,
- /// if any. Note that this may be valid info even when 0 arguments are
- /// explicitly provided as in, e.g., \c void sort<>(char*, char*);
- /// as it anyway contains info on the angle brackets locations.
- ///
- /// \param Previous the set of declarations that may be specialized by
- /// this function specialization.
- bool
- Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
- TemplateArgumentListInfo *ExplicitTemplateArgs,
- LookupResult &Previous) {
- // The set of function template specializations that could match this
- // explicit function template specialization.
- UnresolvedSet<8> Candidates;
- DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
- for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
- I != E; ++I) {
- NamedDecl *Ovl = (*I)->getUnderlyingDecl();
- if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
- // Only consider templates found within the same semantic lookup scope as
- // FD.
- if (!FDLookupContext->InEnclosingNamespaceSetOf(
- Ovl->getDeclContext()->getRedeclContext()))
- continue;
- // C++ [temp.expl.spec]p11:
- // A trailing template-argument can be left unspecified in the
- // template-id naming an explicit function template specialization
- // provided it can be deduced from the function argument type.
- // Perform template argument deduction to determine whether we may be
- // specializing this template.
- // FIXME: It is somewhat wasteful to build
- TemplateDeductionInfo Info(Context, FD->getLocation());
- FunctionDecl *Specialization = 0;
- if (TemplateDeductionResult TDK
- = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
- FD->getType(),
- Specialization,
- Info)) {
- // FIXME: Template argument deduction failed; record why it failed, so
- // that we can provide nifty diagnostics.
- (void)TDK;
- continue;
- }
- // Record this candidate.
- Candidates.addDecl(Specialization, I.getAccess());
- }
- }
- // Find the most specialized function template.
- UnresolvedSetIterator Result
- = getMostSpecialized(Candidates.begin(), Candidates.end(),
- TPOC_Other, 0, FD->getLocation(),
- PDiag(diag::err_function_template_spec_no_match)
- << FD->getDeclName(),
- PDiag(diag::err_function_template_spec_ambiguous)
- << FD->getDeclName() << (ExplicitTemplateArgs != 0),
- PDiag(diag::note_function_template_spec_matched));
- if (Result == Candidates.end())
- return true;
- // Ignore access information; it doesn't figure into redeclaration checking.
- FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
- FunctionTemplateSpecializationInfo *SpecInfo
- = Specialization->getTemplateSpecializationInfo();
- assert(SpecInfo && "Function template specialization info missing?");
- // Note: do not overwrite location info if previous template
- // specialization kind was explicit.
- TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
- if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
- Specialization->setLocation(FD->getLocation());
- // FIXME: Check if the prior specialization has a point of instantiation.
- // If so, we have run afoul of .
- // If this is a friend declaration, then we're not really declaring
- // an explicit specialization.
- bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
- // Check the scope of this explicit specialization.
- if (!isFriend &&
- CheckTemplateSpecializationScope(*this,
- Specialization->getPrimaryTemplate(),
- Specialization, FD->getLocation(),
- false))
- return true;
- // C++ [temp.expl.spec]p6:
- // If a template, a member template or the member of a class template is
- // explicitly specialized then that specialization shall be declared
- // before the first use of that specialization that would cause an implicit
- // instantiation to take place, in every translation unit in which such a
- // use occurs; no diagnostic is required.
- bool HasNoEffect = false;
- if (!isFriend &&
- CheckSpecializationInstantiationRedecl(FD->getLocation(),
- TSK_ExplicitSpecialization,
- Specialization,
- SpecInfo->getTemplateSpecializationKind(),
- SpecInfo->getPointOfInstantiation(),
- HasNoEffect))
- return true;
-
- // Mark the prior declaration as an explicit specialization, so that later
- // clients know that this is an explicit specialization.
- if (!isFriend) {
- SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
- MarkUnusedFileScopedDecl(Specialization);
- }
- // Turn the given function declaration into a function template
- // specialization, with the template arguments from the previous
- // specialization.
- // Take copies of (semantic and syntactic) template argument lists.
- const TemplateArgumentList* TemplArgs = new (Context)
- TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
- FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
- TemplArgs, /*InsertPos=*/0,
- SpecInfo->getTemplateSpecializationKind(),
- ExplicitTemplateArgs);
- FD->setStorageClass(Specialization->getStorageClass());
-
- // The "previous declaration" for this function template specialization is
- // the prior function template specialization.
- Previous.clear();
- Previous.addDecl(Specialization);
- return false;
- }
- /// \brief Perform semantic analysis for the given non-template member
- /// specialization.
- ///
- /// This routine performs all of the semantic analysis required for an
- /// explicit member function specialization. On successful completion,
- /// the function declaration \p FD will become a member function
- /// specialization.
- ///
- /// \param Member the member declaration, which will be updated to become a
- /// specialization.
- ///
- /// \param Previous the set of declarations, one of which may be specialized
- /// by this function specialization; the set will be modified to contain the
- /// redeclared member.
- bool
- Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
- assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
- // Try to find the member we are instantiating.
- NamedDecl *Instantiation = 0;
- NamedDecl *InstantiatedFrom = 0;
- MemberSpecializationInfo *MSInfo = 0;
- if (Previous.empty()) {
- // Nowhere to look anyway.
- } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
- for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
- I != E; ++I) {
- NamedDecl *D = (*I)->getUnderlyingDecl();
- if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
- if (Context.hasSameType(Function->getType(), Method->getType())) {
- Instantiation = Method;
- InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
- MSInfo = Method->getMemberSpecializationInfo();
- break;
- }
- }
- }
- } else if (isa<VarDecl>(Member)) {
- VarDecl *PrevVar;
- if (Previous.isSingleResult() &&
- (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
- if (PrevVar->isStaticDataMember()) {
- Instantiation = PrevVar;
- InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
- MSInfo = PrevVar->getMemberSpecializationInfo();
- }
- } else if (isa<RecordDecl>(Member)) {
- CXXRecordDecl *PrevRecord;
- if (Previous.isSingleResult() &&
- (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
- Instantiation = PrevRecord;
- InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
- MSInfo = PrevRecord->getMemberSpecializationInfo();
- }
- }
- if (!Instantiation) {
- // There is no previous declaration that matches. Since member
- // specializations are always out-of-line, the caller will complain about
- // this mismatch later.
- return false;
- }
- // If this is a friend, just bail out here before we start turning
- // things into explicit specializations.
- if (Member->getFriendObjectKind() != Decl::FOK_None) {
- // Preserve instantiation information.
- if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
- cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
- cast<CXXMethodDecl>(InstantiatedFrom),
- cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
- } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
- cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
- cast<CXXRecordDecl>(InstantiatedFrom),
- cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
- }
- Previous.clear();
- Previous.addDecl(Instantiation);
- return false;
- }
- // Make sure that this is a specialization of a member.
- if (!InstantiatedFrom) {
- Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
- << Member;
- Diag(Instantiation->getLocation(), diag::note_specialized_decl);
- return true;
- }
- // C++ [temp.expl.spec]p6:
- // If a template, a member template or the member of a class template is
- // explicitly specialized then that specialization shall be declared
- // before the first use of that specialization that would cause an implicit
- // instantiation to take place, in every translation unit in which such a
- // use occurs; no diagnostic is required.
- assert(MSInfo && "Member specialization info missing?");
- bool HasNoEffect = false;
- if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
- TSK_ExplicitSpecialization,
- Instantiation,
- MSInfo->getTemplateSpecializationKind(),
- MSInfo->getPointOfInstantiation(),
- HasNoEffect))
- return true;
- // Check the scope of this explicit specialization.
- if (CheckTemplateSpecializationScope(*this,
- InstantiatedFrom,
- Instantiation, Member->getLocation(),
- false))
- return true;
- // Note that this is an explicit instantiation of a member.
- // the original declaration to note that it is an explicit specialization
- // (if it was previously an implicit instantiation). This latter step
- // makes bookkeeping easier.
- if (isa<FunctionDecl>(Member)) {
- FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
- if (InstantiationFunction->getTemplateSpecializationKind() ==
- TSK_ImplicitInstantiation) {
- InstantiationFunction->setTemplateSpecializationKind(
- TSK_ExplicitSpecialization);
- InstantiationFunction->setLocation(Member->getLocation());
- }
- cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
- cast<CXXMethodDecl>(InstantiatedFrom),
- TSK_ExplicitSpecialization);
- MarkUnusedFileScopedDecl(InstantiationFunction);
- } else if (isa<VarDecl>(Member)) {
- VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
- if (InstantiationVar->getTemplateSpecializationKind() ==
- TSK_ImplicitInstantiation) {
- InstantiationVar->setTemplateSpecializationKind(
- TSK_ExplicitSpecialization);
- InstantiationVar->setLocation(Member->getLocation());
- }
- Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
- cast<VarDecl>(InstantiatedFrom),
- TSK_ExplicitSpecialization);
- MarkUnusedFileScopedDecl(InstantiationVar);
- } else {
- assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
- CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
- if (InstantiationClass->getTemplateSpecializationKind() ==
- TSK_ImplicitInstantiation) {
- InstantiationClass->setTemplateSpecializationKind(
- TSK_ExplicitSpecialization);
- InstantiationClass->setLocation(Member->getLocation());
- }
- cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
- cast<CXXRecordDecl>(InstantiatedFrom),
- TSK_ExplicitSpecialization);
- }
- // Save the caller the trouble of having to figure out which declaration
- // this specialization matches.
- Previous.clear();
- Previous.addDecl(Instantiation);
- return false;
- }
- /// \brief Check the scope of an explicit instantiation.
- ///
- /// \returns true if a serious error occurs, false otherwise.
- static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
- SourceLocation InstLoc,
- bool WasQualifiedName) {
- DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
- DeclContext *CurContext = S.CurContext->getRedeclContext();
- if (CurContext->isRecord()) {
- S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
- << D;
- return true;
- }
- // C++11 [temp.explicit]p3:
- // An explicit instantiation shall appear in an enclosing namespace of its
- // template. If the name declared in the explicit instantiation is an
- // unqualified name, the explicit instantiation shall appear in the
- // namespace where its template is declared or, if that namespace is inline
- // (7.3.1), any namespace from its enclosing namespace set.
- //
- // This is DR275, which we do not retroactively apply to C++98/03.
- if (WasQualifiedName) {
- if (CurContext->Encloses(OrigContext))
- return false;
- } else {
- if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
- return false;
- }
- if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
- if (WasQualifiedName)
- S.Diag(InstLoc,
- S.getLangOptions().CPlusPlus0x?
- diag::err_explicit_instantiation_out_of_scope :
- diag::warn_explicit_instantiation_out_of_scope_0x)
- << D << NS;
- else
- S.Diag(InstLoc,
- S.getLangOptions().CPlusPlus0x?
- diag::err_explicit_instantiation_unqualified_wrong_namespace :
- diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
- << D << NS;
- } else
- S.Diag(InstLoc,
- S.getLangOptions().CPlusPlus0x?
- diag::err_explicit_instantiation_must_be_global :
- diag::warn_explicit_instantiation_must_be_global_0x)
- << D;
- S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
- return false;
- }
- /// \brief Determine whether the given scope specifier has a template-id in it.
- static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
- if (!SS.isSet())
- return false;
- // C++11 [temp.explicit]p3:
- // If the explicit instantiation is for a member function, a member class
- // or a static data member of a class template specialization, the name of
- // the class template specialization in the qualified-id for the member
- // name shall be a simple-template-id.
- //
- // C++98 has the same restriction, just worded differently.
- for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
- NNS; NNS = NNS->getPrefix())
- if (const Type *T = NNS->getAsType())
- if (isa<TemplateSpecializationType>(T))
- return true;
- return false;
- }
- // Explicit instantiation of a class template specialization
- DeclResult
- Sema::ActOnExplicitInstantiation(Scope *S,
- SourceLocation ExternLoc,
- SourceLocation TemplateLoc,
- unsigned TagSpec,
- SourceLocation KWLoc,
- const CXXScopeSpec &SS,
- TemplateTy TemplateD,
- SourceLocation TemplateNameLoc,
- SourceLocation LAngleLoc,
- ASTTemplateArgsPtr TemplateArgsIn,
- SourceLocation RAngleLoc,
- AttributeList *Attr) {
- // Find the class template we're specializing
- TemplateName Name = TemplateD.getAsVal<TemplateName>();
- ClassTemplateDecl *ClassTemplate
- = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
- // Check that the specialization uses the same tag kind as the
- // original template.
- TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
- assert(Kind != TTK_Enum &&
- "Invalid enum tag in class template explicit instantiation!");
- if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
- Kind, /*isDefinition*/false, KWLoc,
- *ClassTemplate->getIdentifier())) {
- Diag(KWLoc, diag::err_use_with_wrong_tag)
- << ClassTemplate
- << FixItHint::CreateReplacement(KWLoc,
- ClassTemplate->getTemplatedDecl()->getKindName());
- Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
- diag::note_previous_use);
- Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
- }
- // C++0x [temp.explicit]p2:
- // There are two forms of explicit instantiation: an explicit instantiation
- // definition and an explicit instantiation declaration. An explicit
- // instantiation declaration begins with the extern keyword. [...]
- TemplateSpecializationKind TSK
- = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
- : TSK_ExplicitInstantiationDeclaration;
- // Translate the parser's template argument list in our AST format.
- TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
- translateTemplateArguments(TemplateArgsIn, TemplateArgs);
- // Check that the template argument list is well-formed for this
- // template.
- SmallVector<TemplateArgument, 4> Converted;
- if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
- TemplateArgs, false, Converted))
- return true;
- // Find the class template specialization declaration that
- // corresponds to these arguments.
- void *InsertPos = 0;
- ClassTemplateSpecializationDecl *PrevDecl
- = ClassTemplate->findSpecialization(Converted.data(),
- Converted.size(), InsertPos);
- TemplateSpecializationKind PrevDecl_TSK
- = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
- // C++0x [temp.explicit]p2:
- // [...] An explicit instantiation shall appear in an enclosing
- // namespace of its template. [...]
- //
- // This is C++ DR 275.
- if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
- SS.isSet()))
- return true;
- ClassTemplateSpecializationDecl *Specialization = 0;
- bool HasNoEffect = false;
- if (PrevDecl) {
- if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
- PrevDecl, PrevDecl_TSK,
- PrevDecl->getPointOfInstantiation(),
- HasNoEffect))
- return PrevDecl;
- // Even though HasNoEffect == true means that this explicit instantiation
- // has no effect on semantics, we go on to put its syntax in the AST.
- if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
- PrevDecl_TSK == TSK_Undeclared) {
- // Since the only prior class template specialization with these
- // arguments was referenced but not declared, reuse that
- // declaration node as our own, updating the source location
- // for the template name to reflect our new declaration.
- // (Other source locations will be updated later.)
- Specialization = PrevDecl;
- Specialization->setLocation(TemplateNameLoc);
- PrevDecl = 0;
- }
- }
- if (!Specialization) {
- // Create a new class template specialization declaration node for
- // this explicit specialization.
- Specialization
- = ClassTemplateSpecializationDecl::Create(Context, Kind,
- ClassTemplate->getDeclContext(),
- KWLoc, TemplateNameLoc,
- ClassTemplate,
- Converted.data(),
- Converted.size(),
- PrevDecl);
- SetNestedNameSpecifier(Specialization, SS);
- if (!HasNoEffect && !PrevDecl) {
- // Insert the new specialization.
- ClassTemplate->AddSpecialization(Specialization, InsertPos);
- }
- }
- // Build the fully-sugared type for this explicit instantiation as
- // the user wrote in the explicit instantiation itself. This means
- // that we'll pretty-print the type retrieved from the
- // specialization's declaration the way that the user actually wrote
- // the explicit instantiation, rather than formatting the name based
- // on the "canonical" representation used to store the template
- // arguments in the specialization.
- TypeSourceInfo *WrittenTy
- = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
- TemplateArgs,
- Context.getTypeDeclType(Specialization));
- Specialization->setTypeAsWritten(WrittenTy);
- TemplateArgsIn.release();
- // Set source locations for keywords.
- Specialization->setExternLoc(ExternLoc);
- Specialization->setTemplateKeywordLoc(TemplateLoc);
- if (Attr)
- ProcessDeclAttributeList(S, Specialization, Attr);
- // Add the explicit instantiation into its lexical context. However,
- // since explicit instantiations are never found by name lookup, we
- // just put it into the declaration context directly.
- Specialization->setLexicalDeclContext(CurContext);
- CurContext->addDecl(Specialization);
- // Syntax is now OK, so return if it has no other effect on semantics.
- if (HasNoEffect) {
- // Set the template specialization kind.
- Specialization->setTemplateSpecializationKind(TSK);
- return Specialization;
- }
- // C++ [temp.explicit]p3:
- // A definition of a class template or class member template
- // shall be in scope at the point of the explicit instantiation of
- // the class template or class member template.
- //
- // This check comes when we actually try to perform the
- // instantiation.
- ClassTemplateSpecializationDecl *Def
- = cast_or_null<ClassTemplateSpecializationDecl>(
- Specialization->getDefinition());
- if (!Def)
- InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
- else if (TSK == TSK_ExplicitInstantiationDefinition) {
- MarkVTableUsed(TemplateNameLoc, Specialization, true);
- Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
- }
- // Instantiate the members of this class template specialization.
- Def = cast_or_null<ClassTemplateSpecializationDecl>(
- Specialization->getDefinition());
- if (Def) {
- TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
- // Fix a TSK_ExplicitInstantiationDeclaration followed by a
- // TSK_ExplicitInstantiationDefinition
- if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
- TSK == TSK_ExplicitInstantiationDefinition)
- Def->setTemplateSpecializationKind(TSK);
- InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
- }
- // Set the template specialization kind.
- Specialization->setTemplateSpecializationKind(TSK);
- return Specialization;
- }
- // Explicit instantiation of a member class of a class template.
- DeclResult
- Sema::ActOnExplicitInstantiation(Scope *S,
- SourceLocation ExternLoc,
- SourceLocation TemplateLoc,
- unsigned TagSpec,
- SourceLocation KWLoc,
- CXXScopeSpec &SS,
- IdentifierInfo *Name,
- SourceLocation NameLoc,
- AttributeList *Attr) {
- bool Owned = false;
- bool IsDependent = false;
- Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
- KWLoc, SS, Name, NameLoc, Attr, AS_none,
- /*ModulePrivateLoc=*/SourceLocation(),
- MultiTemplateParamsArg(*this, 0, 0),
- Owned, IsDependent, SourceLocation(), false,
- TypeResult());
- assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
- if (!TagD)
- return true;
- TagDecl *Tag = cast<TagDecl>(TagD);
- if (Tag->isEnum()) {
- Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
- << Context.getTypeDeclType(Tag);
- return true;
- }
- if (Tag->isInvalidDecl())
- return true;
- CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
- CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
- if (!Pattern) {
- Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
- << Context.getTypeDeclType(Record);
- Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
- return true;
- }
- // C++0x [temp.explicit]p2:
- // If the explicit instantiation is for a class or member class, the
- // elaborated-type-specifier in the declaration shall include a
- // simple-template-id.
- //
- // C++98 has the same restriction, just worded differently.
- if (!ScopeSpecifierHasTemplateId(SS))
- Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
- << Record << SS.getRange();
- // C++0x [temp.explicit]p2:
- // There are two forms of explicit instantiation: an explicit instantiation
- // definition and an explicit instantiation declaration. An explicit
- // instantiation declaration begins with the extern keyword. [...]
- TemplateSpecializationKind TSK
- = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
- : TSK_ExplicitInstantiationDeclaration;
- // C++0x [temp.explicit]p2:
- // [...] An explicit instantiation shall appear in an enclosing
- // namespace of its template. [...]
- //
- // This is C++ DR 275.
- CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
- // Verify that it is okay to explicitly instantiate here.
- CXXRecordDecl *PrevDecl
- = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
- if (!PrevDecl && Record->getDefinition())
- PrevDecl = Record;
- if (PrevDecl) {
- MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
- bool HasNoEffect = false;
- assert(MSInfo && "No member specialization information?");
- if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
- PrevDecl,
- MSInfo->getTemplateSpecializationKind(),
- MSInfo->getPointOfInstantiation(),
- HasNoEffect))
- return true;
- if (HasNoEffect)
- return TagD;
- }
- CXXRecordDecl *RecordDef
- = cast_or_null<CXXRecordDecl>(Record->getDefinition());
- if (!RecordDef) {
- // C++ [temp.explicit]p3:
- // A definition of a member class of a class template shall be in scope
- // at the point of an explicit instantiation of the member class.
- CXXRecordDecl *Def
- = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
- if (!Def) {
- Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
- << 0 << Record->getDeclName() << Record->getDeclContext();
- Diag(Pattern->getLocation(), diag::note_forward_declaration)
- << Pattern;
- return true;
- } else {
- if (InstantiateClass(NameLoc, Record, Def,
- getTemplateInstantiationArgs(Record),
- TSK))
- return true;
- RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
- if (!RecordDef)
- return true;
- }
- }
- // Instantiate all of the members of the class.
- InstantiateClassMembers(NameLoc, RecordDef,
- getTemplateInstantiationArgs(Record), TSK);
- if (TSK == TSK_ExplicitInstantiationDefinition)
- MarkVTableUsed(NameLoc, RecordDef, true);
- // FIXME: We don't have any representation for explicit instantiations of
- // member classes. Such a representation is not needed for compilation, but it
- // should be available for clients that want to see all of the declarations in
- // the source code.
- return TagD;
- }
- DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
- SourceLocation ExternLoc,
- SourceLocation TemplateLoc,
- Declarator &D) {
- // Explicit instantiations always require a name.
- // TODO: check if/when DNInfo should replace Name.
- DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
- DeclarationName Name = NameInfo.getName();
- if (!Name) {
- if (!D.isInvalidType())
- Diag(D.getDeclSpec().getSourceRange().getBegin(),
- diag::err_explicit_instantiation_requires_name)
- << D.getDeclSpec().getSourceRange()
- << D.getSourceRange();
- return true;
- }
- // The scope passed in may not be a decl scope. Zip up the scope tree until
- // we find one that is.
- while ((S->getFlags() & Scope::DeclScope) == 0 ||
- (S->getFlags() & Scope::TemplateParamScope) != 0)
- S = S->getParent();
- // Determine the type of the declaration.
- TypeSourceInfo *T = GetTypeForDeclarator(D, S);
- QualType R = T->getType();
- if (R.isNull())
- return true;
- // C++ [dcl.stc]p1:
- // A storage-class-specifier shall not be specified in [...] an explicit
- // instantiation (14.7.2) directive.
- if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
- Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
- << Name;
- return true;
- } else if (D.getDeclSpec().getStorageClassSpec()
- != DeclSpec::SCS_unspecified) {
- // Complain about then remove the storage class specifier.
- Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
- << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
-
- D.getMutableDeclSpec().ClearStorageClassSpecs();
- }
- // C++0x [temp.explicit]p1:
- // [...] An explicit instantiation of a function template shall not use the
- // inline or constexpr specifiers.
- // Presumably, this also applies to member functions of class templates as
- // well.
- if (D.getDeclSpec().isInlineSpecified())
- Diag(D.getDeclSpec().getInlineSpecLoc(),
- getLangOptions().CPlusPlus0x ?
- diag::err_explicit_instantiation_inline :
- diag::warn_explicit_instantiation_inline_0x)
- << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
- if (D.getDeclSpec().isConstexprSpecified())
- // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
- // not already specified.
- Diag(D.getDeclSpec().getConstexprSpecLoc(),
- diag::err_explicit_instantiation_constexpr);
- // C++0x [temp.explicit]p2:
- // There are two forms of explicit instantiation: an explicit instantiation
- // definition and an explicit instantiation declaration. An explicit
- // instantiation declaration begins with the extern keyword. [...]
- TemplateSpecializationKind TSK
- = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
- : TSK_ExplicitInstantiationDeclaration;
- LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
- LookupParsedName(Previous, S, &D.getCXXScopeSpec());
- if (!R->isFunctionType()) {
- // C++ [temp.explicit]p1:
- // A [...] static data member of a class template can be explicitly
- // instantiated from the member definition associated with its class
- // template.
- if (Previous.isAmbiguous())
- return true;
- VarDecl *Prev = Previous.getAsSingle<VarDecl>();
- if (!Prev || !Prev->isStaticDataMember()) {
- // We expect to see a data data member here.
- Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
- << Name;
- for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
- P != PEnd; ++P)
- Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
- return true;
- }
- if (!Prev->getInstantiatedFromStaticDataMember()) {
- // FIXME: Check for explicit specialization?
- Diag(D.getIdentifierLoc(),
- diag::err_explicit_instantiation_data_member_not_instantiated)
- << Prev;
- Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
- // FIXME: Can we provide a note showing where this was declared?
- return true;
- }
- // C++0x [temp.explicit]p2:
- // If the explicit instantiation is for a member function, a member class
- // or a static data member of a class template specialization, the name of
- // the class template specialization in the qualified-id for the member
- // name shall be a simple-template-id.
- //
- // C++98 has the same restriction, just worded differently.
- if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
- Diag(D.getIdentifierLoc(),
- diag::ext_explicit_instantiation_without_qualified_id)
- << Prev << D.getCXXScopeSpec().getRange();
- // Check the scope of this explicit instantiation.
- CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
- // Verify that it is okay to explicitly instantiate here.
- MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
- assert(MSInfo && "Missing static data member specialization info?");
- bool HasNoEffect = false;
- if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
- MSInfo->getTemplateSpecializationKind(),
- MSInfo->getPointOfInstantiation(),
- HasNoEffect))
- return true;
- if (HasNoEffect)
- return (Decl*) 0;
- // Instantiate static data member.
- Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
- if (TSK == TSK_ExplicitInstantiationDefinition)
- InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
- // FIXME: Create an ExplicitInstantiation node?
- return (Decl*) 0;
- }
- // If the declarator is a template-id, translate the parser's template
- // argument list into our AST format.
- bool HasExplicitTemplateArgs = false;
- TemplateArgumentListInfo TemplateArgs;
- if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
- TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
- TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
- TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
- ASTTemplateArgsPtr TemplateArgsPtr(*this,
- TemplateId->getTemplateArgs(),
- TemplateId->NumArgs);
- translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
- HasExplicitTemplateArgs = true;
- TemplateArgsPtr.release();
- }
- // C++ [temp.explicit]p1:
- // A [...] function [...] can be explicitly instantiated from its template.
- // A member function [...] of a class template can be explicitly
- // instantiated from the member definition associated with its class
- // template.
- UnresolvedSet<8> Matches;
- for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
- P != PEnd; ++P) {
- NamedDecl *Prev = *P;
- if (!HasExplicitTemplateArgs) {
- if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
- if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
- Matches.clear();
- Matches.addDecl(Method, P.getAccess());
- if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
- break;
- }
- }
- }
- FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
- if (!FunTmpl)
- continue;
- TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
- FunctionDecl *Specialization = 0;
- if (TemplateDeductionResult TDK
- = DeduceTemplateArguments(FunTmpl,
- (HasExplicitTemplateArgs ? &TemplateArgs : 0),
- R, Specialization, Info)) {
- // FIXME: Keep track of almost-matches?
- (void)TDK;
- continue;
- }
- Matches.addDecl(Specialization, P.getAccess());
- }
- // Find the most specialized function template specialization.
- UnresolvedSetIterator Result
- = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
- D.getIdentifierLoc(),
- PDiag(diag::err_explicit_instantiation_not_known) << Name,
- PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
- PDiag(diag::note_explicit_instantiation_candidate));
- if (Result == Matches.end())
- return true;
- // Ignore access control bits, we don't need them for redeclaration checking.
- FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
- if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
- Diag(D.getIdentifierLoc(),
- diag::err_explicit_instantiation_member_function_not_instantiated)
- << Specialization
- << (Specialization->getTemplateSpecializationKind() ==
- TSK_ExplicitSpecialization);
- Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
- return true;
- }
- FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
- if (!PrevDecl && Specialization->isThisDeclarationADefinition())
- PrevDecl = Specialization;
- if (PrevDecl) {
- bool HasNoEffect = false;
- if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
- PrevDecl,
- PrevDecl->getTemplateSpecializationKind(),
- PrevDecl->getPointOfInstantiation(),
- HasNoEffect))
- return true;
- // FIXME: We may still want to build some representation of this
- // explicit specialization.
- if (HasNoEffect)
- return (Decl*) 0;
- }
- Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
- AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
- if (Attr)
- ProcessDeclAttributeList(S, Specialization, Attr);
- if (TSK == TSK_ExplicitInstantiationDefinition)
- InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
- // C++0x [temp.explicit]p2:
- // If the explicit instantiation is for a member function, a member class
- // or a static data member of a class template specialization, the name of
- // the class template specialization in the qualified-id for the member
- // name shall be a simple-template-id.
- //
- // C++98 has the same restriction, just worded differently.
- FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
- if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
- D.getCXXScopeSpec().isSet() &&
- !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
- Diag(D.getIdentifierLoc(),
- diag::ext_explicit_instantiation_without_qualified_id)
- << Specialization << D.getCXXScopeSpec().getRange();
- CheckExplicitInstantiationScope(*this,
- FunTmpl? (NamedDecl *)FunTmpl
- : Specialization->getInstantiatedFromMemberFunction(),
- D.getIdentifierLoc(),
- D.getCXXScopeSpec().isSet());
- // FIXME: Create some kind of ExplicitInstantiationDecl here.
- return (Decl*) 0;
- }
- TypeResult
- Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
- const CXXScopeSpec &SS, IdentifierInfo *Name,
- SourceLocation TagLoc, SourceLocation NameLoc) {
- // This has to hold, because SS is expected to be defined.
- assert(Name && "Expected a name in a dependent tag");
- NestedNameSpecifier *NNS
- = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
- if (!NNS)
- return true;
- TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
- if (TUK == TUK_Declaration || TUK == TUK_Definition) {
- Diag(NameLoc, diag::err_dependent_tag_decl)
- << (TUK == TUK_Definition) << Kind << SS.getRange();
- return true;
- }
- // Create the resulting type.
- ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
- QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
-
- // Create type-source location information for this type.
- TypeLocBuilder TLB;
- DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
- TL.setKeywordLoc(TagLoc);
- TL.setQualifierLoc(SS.getWithLocInContext(Context));
- TL.setNameLoc(NameLoc);
- return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
- }
- TypeResult
- Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
- const CXXScopeSpec &SS, const IdentifierInfo &II,
- SourceLocation IdLoc) {
- if (SS.isInvalid())
- return true;
-
- if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
- Diag(TypenameLoc,
- getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_typename_outside_of_template :
- diag::ext_typename_outside_of_template)
- << FixItHint::CreateRemoval(TypenameLoc);
- NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
- QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
- TypenameLoc, QualifierLoc, II, IdLoc);
- if (T.isNull())
- return true;
- TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
- if (isa<DependentNameType>(T)) {
- DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
- TL.setKeywordLoc(TypenameLoc);
- TL.setQualifierLoc(QualifierLoc);
- TL.setNameLoc(IdLoc);
- } else {
- ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
- TL.setKeywordLoc(TypenameLoc);
- TL.setQualifierLoc(QualifierLoc);
- cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
- }
- return CreateParsedType(T, TSI);
- }
- TypeResult
- Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
- const CXXScopeSpec &SS,
- SourceLocation TemplateLoc,
- TemplateTy TemplateIn,
- SourceLocation TemplateNameLoc,
- SourceLocation LAngleLoc,
- ASTTemplateArgsPtr TemplateArgsIn,
- SourceLocation RAngleLoc) {
- if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
- Diag(TypenameLoc,
- getLangOptions().CPlusPlus0x ?
- diag::warn_cxx98_compat_typename_outside_of_template :
- diag::ext_typename_outside_of_template)
- << FixItHint::CreateRemoval(TypenameLoc);
-
- // Translate the parser's template argument list in our AST format.
- TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
- translateTemplateArguments(TemplateArgsIn, TemplateArgs);
-
- TemplateName Template = TemplateIn.get();
- if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
- // Construct a dependent template specialization type.
- assert(DTN && "dependent template has non-dependent name?");
- assert(DTN->getQualifier()
- == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
- QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
- DTN->getQualifier(),
- DTN->getIdentifier(),
- TemplateArgs);
-
- // Create source-location information for this type.
- TypeLocBuilder Builder;
- DependentTemplateSpecializationTypeLoc SpecTL
- = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
- SpecTL.setLAngleLoc(LAngleLoc);
- SpecTL.setRAngleLoc(RAngleLoc);
- SpecTL.setKeywordLoc(TypenameLoc);
- SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
- SpecTL.setNameLoc(TemplateNameLoc);
- for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
- SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
- return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
- }
-
- QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
- if (T.isNull())
- return true;
-
- // Provide source-location information for the template specialization
- // type.
- TypeLocBuilder Builder;
- TemplateSpecializationTypeLoc SpecTL
- = Builder.push<TemplateSpecializationTypeLoc>(T);
-
- // FIXME: No place to set the location of the 'template' keyword!
- SpecTL.setLAngleLoc(LAngleLoc);
- SpecTL.setRAngleLoc(RAngleLoc);
- SpecTL.setTemplateNameLoc(TemplateNameLoc);
- for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
- SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
-
- T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
- ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
- TL.setKeywordLoc(TypenameLoc);
- TL.setQualifierLoc(SS.getWithLocInContext(Context));
-
- TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
- return CreateParsedType(T, TSI);
- }
- /// \brief Build the type that describes a C++ typename specifier,
- /// e.g., "typename T::type".
- QualType
- Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
- SourceLocation KeywordLoc,
- NestedNameSpecifierLoc QualifierLoc,
- const IdentifierInfo &II,
- SourceLocation IILoc) {
- CXXScopeSpec SS;
- SS.Adopt(QualifierLoc);
- DeclContext *Ctx = computeDeclContext(SS);
- if (!Ctx) {
- // If the nested-name-specifier is dependent and couldn't be
- // resolved to a type, build a typename type.
- assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
- return Context.getDependentNameType(Keyword,
- QualifierLoc.getNestedNameSpecifier(),
- &II);
- }
- // If the nested-name-specifier refers to the current instantiation,
- // the "typename" keyword itself is superfluous. In C++03, the
- // program is actually ill-formed. However, DR 382 (in C++0x CD1)
- // allows such extraneous "typename" keywords, and we retroactively
- // apply this DR to C++03 code with only a warning. In any case we continue.
- if (RequireCompleteDeclContext(SS, Ctx))
- return QualType();
- DeclarationName Name(&II);
- LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
- LookupQualifiedName(Result, Ctx);
- unsigned DiagID = 0;
- Decl *Referenced = 0;
- switch (Result.getResultKind()) {
- case LookupResult::NotFound:
- DiagID = diag::err_typename_nested_not_found;
- break;
- case LookupResult::FoundUnresolvedValue: {
- // We found a using declaration that is a value. Most likely, the using
- // declaration itself is meant to have the 'typename' keyword.
- SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
- IILoc);
- Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
- << Name << Ctx << FullRange;
- if (UnresolvedUsingValueDecl *Using
- = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
- SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
- Diag(Loc, diag::note_using_value_decl_missing_typename)
- << FixItHint::CreateInsertion(Loc, "typename ");
- }
- }
- // Fall through to create a dependent typename type, from which we can recover
- // better.
- case LookupResult::NotFoundInCurrentInstantiation:
- // Okay, it's a member of an unknown instantiation.
- return Context.getDependentNameType(Keyword,
- QualifierLoc.getNestedNameSpecifier(),
- &II);
- case LookupResult::Found:
- if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
- // We found a type. Build an ElaboratedType, since the
- // typename-specifier was just sugar.
- return Context.getElaboratedType(ETK_Typename,
- QualifierLoc.getNestedNameSpecifier(),
- Context.getTypeDeclType(Type));
- }
- DiagID = diag::err_typename_nested_not_type;
- Referenced = Result.getFoundDecl();
- break;
- case LookupResult::FoundOverloaded:
- DiagID = diag::err_typename_nested_not_type;
- Referenced = *Result.begin();
- break;
- case LookupResult::Ambiguous:
- return QualType();
- }
- // If we get here, it's because name lookup did not find a
- // type. Emit an appropriate diagnostic and return an error.
- SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
- IILoc);
- Diag(IILoc, DiagID) << FullRange << Name << Ctx;
- if (Referenced)
- Diag(Referenced->getLocation(), diag::note_typename_refers_here)
- << Name;
- return QualType();
- }
- namespace {
- // See Sema::RebuildTypeInCurrentInstantiation
- class CurrentInstantiationRebuilder
- : public TreeTransform<CurrentInstantiationRebuilder> {
- SourceLocation Loc;
- DeclarationName Entity;
- public:
- typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
- CurrentInstantiationRebuilder(Sema &SemaRef,
- SourceLocation Loc,
- DeclarationName Entity)
- : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
- Loc(Loc), Entity(Entity) { }
- /// \brief Determine whether the given type \p T has already been
- /// transformed.
- ///
- /// For the purposes of type reconstruction, a type has already been
- /// transformed if it is NULL or if it is not dependent.
- bool AlreadyTransformed(QualType T) {
- return T.isNull() || !T->isDependentType();
- }
- /// \brief Returns the location of the entity whose type is being
- /// rebuilt.
- SourceLocation getBaseLocation() { return Loc; }
- /// \brief Returns the name of the entity whose type is being rebuilt.
- DeclarationName getBaseEntity() { return Entity; }
- /// \brief Sets the "base" location and entity when that
- /// information is known based on another transformation.
- void setBase(SourceLocation Loc, DeclarationName Entity) {
- this->Loc = Loc;
- this->Entity = Entity;
- }
- };
- }
- /// \brief Rebuilds a type within the context of the current instantiation.
- ///
- /// The type \p T is part of the type of an out-of-line member definition of
- /// a class template (or class template partial specialization) that was parsed
- /// and constructed before we entered the scope of the class template (or
- /// partial specialization thereof). This routine will rebuild that type now
- /// that we have entered the declarator's scope, which may produce different
- /// canonical types, e.g.,
- ///
- /// \code
- /// template<typename T>
- /// struct X {
- /// typedef T* pointer;
- /// pointer data();
- /// };
- ///
- /// template<typename T>
- /// typename X<T>::pointer X<T>::data() { ... }
- /// \endcode
- ///
- /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
- /// since we do not know that we can look into X<T> when we parsed the type.
- /// This function will rebuild the type, performing the lookup of "pointer"
- /// in X<T> and returning an ElaboratedType whose canonical type is the same
- /// as the canonical type of T*, allowing the return types of the out-of-line
- /// definition and the declaration to match.
- TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
- SourceLocation Loc,
- DeclarationName Name) {
- if (!T || !T->getType()->isDependentType())
- return T;
- CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
- return Rebuilder.TransformType(T);
- }
- ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
- CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
- DeclarationName());
- return Rebuilder.TransformExpr(E);
- }
- bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
- if (SS.isInvalid())
- return true;
- NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
- CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
- DeclarationName());
- NestedNameSpecifierLoc Rebuilt
- = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
- if (!Rebuilt)
- return true;
- SS.Adopt(Rebuilt);
- return false;
- }
- /// \brief Rebuild the template parameters now that we know we're in a current
- /// instantiation.
- bool Sema::RebuildTemplateParamsInCurrentInstantiation(
- TemplateParameterList *Params) {
- for (unsigned I = 0, N = Params->size(); I != N; ++I) {
- Decl *Param = Params->getParam(I);
-
- // There is nothing to rebuild in a type parameter.
- if (isa<TemplateTypeParmDecl>(Param))
- continue;
-
- // Rebuild the template parameter list of a template template parameter.
- if (TemplateTemplateParmDecl *TTP
- = dyn_cast<TemplateTemplateParmDecl>(Param)) {
- if (RebuildTemplateParamsInCurrentInstantiation(
- TTP->getTemplateParameters()))
- return true;
-
- continue;
- }
-
- // Rebuild the type of a non-type template parameter.
- NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
- TypeSourceInfo *NewTSI
- = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
- NTTP->getLocation(),
- NTTP->getDeclName());
- if (!NewTSI)
- return true;
-
- if (NewTSI != NTTP->getTypeSourceInfo()) {
- NTTP->setTypeSourceInfo(NewTSI);
- NTTP->setType(NewTSI->getType());
- }
- }
-
- return false;
- }
- /// \brief Produces a formatted string that describes the binding of
- /// template parameters to template arguments.
- std::string
- Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
- const TemplateArgumentList &Args) {
- return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
- }
- std::string
- Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
- const TemplateArgument *Args,
- unsigned NumArgs) {
- SmallString<128> Str;
- llvm::raw_svector_ostream Out(Str);
- if (!Params || Params->size() == 0 || NumArgs == 0)
- return std::string();
- for (unsigned I = 0, N = Params->size(); I != N; ++I) {
- if (I >= NumArgs)
- break;
- if (I == 0)
- Out << "[with ";
- else
- Out << ", ";
- if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
- Out << Id->getName();
- } else {
- Out << '$' << I;
- }
- Out << " = ";
- Args[I].print(getPrintingPolicy(), Out);
- }
- Out << ']';
- return Out.str();
- }
- void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
- if (!FD)
- return;
- FD->setLateTemplateParsed(Flag);
- }
- bool Sema::IsInsideALocalClassWithinATemplateFunction() {
- DeclContext *DC = CurContext;
- while (DC) {
- if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
- const FunctionDecl *FD = RD->isLocalClass();
- return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
- } else if (DC->isTranslationUnit() || DC->isNamespace())
- return false;
- DC = DC->getParent();
- }
- return false;
- }
|