ASTImporter.cpp 217 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922
  1. //===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the ASTImporter class which imports AST nodes from one
  11. // context into another context.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ASTImporter.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/ASTDiagnostic.h"
  17. #include "clang/AST/DeclCXX.h"
  18. #include "clang/AST/DeclObjC.h"
  19. #include "clang/AST/DeclVisitor.h"
  20. #include "clang/AST/StmtVisitor.h"
  21. #include "clang/AST/TypeVisitor.h"
  22. #include "clang/Basic/FileManager.h"
  23. #include "clang/Basic/SourceManager.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include <deque>
  26. // //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. namespace clang {
  29. class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
  30. public DeclVisitor<ASTNodeImporter, Decl *>,
  31. public StmtVisitor<ASTNodeImporter, Stmt *> {
  32. ASTImporter &Importer;
  33. public:
  34. explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
  35. using TypeVisitor<ASTNodeImporter, QualType>::Visit;
  36. using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
  37. using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
  38. // Importing types
  39. QualType VisitType(const Type *T);
  40. QualType VisitBuiltinType(const BuiltinType *T);
  41. QualType VisitComplexType(const ComplexType *T);
  42. QualType VisitPointerType(const PointerType *T);
  43. QualType VisitBlockPointerType(const BlockPointerType *T);
  44. QualType VisitLValueReferenceType(const LValueReferenceType *T);
  45. QualType VisitRValueReferenceType(const RValueReferenceType *T);
  46. QualType VisitMemberPointerType(const MemberPointerType *T);
  47. QualType VisitConstantArrayType(const ConstantArrayType *T);
  48. QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
  49. QualType VisitVariableArrayType(const VariableArrayType *T);
  50. // FIXME: DependentSizedArrayType
  51. // FIXME: DependentSizedExtVectorType
  52. QualType VisitVectorType(const VectorType *T);
  53. QualType VisitExtVectorType(const ExtVectorType *T);
  54. QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
  55. QualType VisitFunctionProtoType(const FunctionProtoType *T);
  56. // FIXME: UnresolvedUsingType
  57. QualType VisitParenType(const ParenType *T);
  58. QualType VisitTypedefType(const TypedefType *T);
  59. QualType VisitTypeOfExprType(const TypeOfExprType *T);
  60. // FIXME: DependentTypeOfExprType
  61. QualType VisitTypeOfType(const TypeOfType *T);
  62. QualType VisitDecltypeType(const DecltypeType *T);
  63. QualType VisitUnaryTransformType(const UnaryTransformType *T);
  64. QualType VisitAutoType(const AutoType *T);
  65. // FIXME: DependentDecltypeType
  66. QualType VisitRecordType(const RecordType *T);
  67. QualType VisitEnumType(const EnumType *T);
  68. QualType VisitAttributedType(const AttributedType *T);
  69. // FIXME: TemplateTypeParmType
  70. // FIXME: SubstTemplateTypeParmType
  71. QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
  72. QualType VisitElaboratedType(const ElaboratedType *T);
  73. // FIXME: DependentNameType
  74. // FIXME: DependentTemplateSpecializationType
  75. QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
  76. QualType VisitObjCObjectType(const ObjCObjectType *T);
  77. QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
  78. // Importing declarations
  79. bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
  80. DeclContext *&LexicalDC, DeclarationName &Name,
  81. NamedDecl *&ToD, SourceLocation &Loc);
  82. void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
  83. void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
  84. DeclarationNameInfo& To);
  85. void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
  86. /// \brief What we should import from the definition.
  87. enum ImportDefinitionKind {
  88. /// \brief Import the default subset of the definition, which might be
  89. /// nothing (if minimal import is set) or might be everything (if minimal
  90. /// import is not set).
  91. IDK_Default,
  92. /// \brief Import everything.
  93. IDK_Everything,
  94. /// \brief Import only the bare bones needed to establish a valid
  95. /// DeclContext.
  96. IDK_Basic
  97. };
  98. bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
  99. return IDK == IDK_Everything ||
  100. (IDK == IDK_Default && !Importer.isMinimalImport());
  101. }
  102. bool ImportDefinition(RecordDecl *From, RecordDecl *To,
  103. ImportDefinitionKind Kind = IDK_Default);
  104. bool ImportDefinition(VarDecl *From, VarDecl *To,
  105. ImportDefinitionKind Kind = IDK_Default);
  106. bool ImportDefinition(EnumDecl *From, EnumDecl *To,
  107. ImportDefinitionKind Kind = IDK_Default);
  108. bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
  109. ImportDefinitionKind Kind = IDK_Default);
  110. bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
  111. ImportDefinitionKind Kind = IDK_Default);
  112. TemplateParameterList *ImportTemplateParameterList(
  113. TemplateParameterList *Params);
  114. TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
  115. bool ImportTemplateArguments(const TemplateArgument *FromArgs,
  116. unsigned NumFromArgs,
  117. SmallVectorImpl<TemplateArgument> &ToArgs);
  118. bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
  119. bool Complain = true);
  120. bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
  121. bool Complain = true);
  122. bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
  123. bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
  124. bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
  125. bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
  126. Decl *VisitDecl(Decl *D);
  127. Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
  128. Decl *VisitNamespaceDecl(NamespaceDecl *D);
  129. Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
  130. Decl *VisitTypedefDecl(TypedefDecl *D);
  131. Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
  132. Decl *VisitEnumDecl(EnumDecl *D);
  133. Decl *VisitRecordDecl(RecordDecl *D);
  134. Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
  135. Decl *VisitFunctionDecl(FunctionDecl *D);
  136. Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
  137. Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
  138. Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
  139. Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
  140. Decl *VisitFieldDecl(FieldDecl *D);
  141. Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
  142. Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
  143. Decl *VisitVarDecl(VarDecl *D);
  144. Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
  145. Decl *VisitParmVarDecl(ParmVarDecl *D);
  146. Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
  147. Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
  148. Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
  149. Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
  150. Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
  151. ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
  152. Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
  153. Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
  154. Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
  155. Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
  156. Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
  157. Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
  158. Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
  159. Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
  160. Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
  161. Decl *VisitClassTemplateSpecializationDecl(
  162. ClassTemplateSpecializationDecl *D);
  163. Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
  164. Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
  165. // Importing statements
  166. DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
  167. Stmt *VisitStmt(Stmt *S);
  168. Stmt *VisitDeclStmt(DeclStmt *S);
  169. Stmt *VisitNullStmt(NullStmt *S);
  170. Stmt *VisitCompoundStmt(CompoundStmt *S);
  171. Stmt *VisitCaseStmt(CaseStmt *S);
  172. Stmt *VisitDefaultStmt(DefaultStmt *S);
  173. Stmt *VisitLabelStmt(LabelStmt *S);
  174. Stmt *VisitAttributedStmt(AttributedStmt *S);
  175. Stmt *VisitIfStmt(IfStmt *S);
  176. Stmt *VisitSwitchStmt(SwitchStmt *S);
  177. Stmt *VisitWhileStmt(WhileStmt *S);
  178. Stmt *VisitDoStmt(DoStmt *S);
  179. Stmt *VisitForStmt(ForStmt *S);
  180. Stmt *VisitGotoStmt(GotoStmt *S);
  181. Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
  182. Stmt *VisitContinueStmt(ContinueStmt *S);
  183. Stmt *VisitBreakStmt(BreakStmt *S);
  184. Stmt *VisitReturnStmt(ReturnStmt *S);
  185. // FIXME: GCCAsmStmt
  186. // FIXME: MSAsmStmt
  187. // FIXME: SEHExceptStmt
  188. // FIXME: SEHFinallyStmt
  189. // FIXME: SEHTryStmt
  190. // FIXME: SEHLeaveStmt
  191. // FIXME: CapturedStmt
  192. Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
  193. Stmt *VisitCXXTryStmt(CXXTryStmt *S);
  194. Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
  195. // FIXME: MSDependentExistsStmt
  196. Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
  197. Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
  198. Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
  199. Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
  200. Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
  201. Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
  202. Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
  203. // Importing expressions
  204. Expr *VisitExpr(Expr *E);
  205. Expr *VisitDeclRefExpr(DeclRefExpr *E);
  206. Expr *VisitIntegerLiteral(IntegerLiteral *E);
  207. Expr *VisitCharacterLiteral(CharacterLiteral *E);
  208. Expr *VisitParenExpr(ParenExpr *E);
  209. Expr *VisitUnaryOperator(UnaryOperator *E);
  210. Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
  211. Expr *VisitBinaryOperator(BinaryOperator *E);
  212. Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
  213. Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
  214. Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
  215. Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
  216. Expr *VisitMemberExpr(MemberExpr *E);
  217. Expr *VisitCallExpr(CallExpr *E);
  218. };
  219. }
  220. using namespace clang;
  221. //----------------------------------------------------------------------------
  222. // Structural Equivalence
  223. //----------------------------------------------------------------------------
  224. namespace {
  225. struct StructuralEquivalenceContext {
  226. /// \brief AST contexts for which we are checking structural equivalence.
  227. ASTContext &C1, &C2;
  228. /// \brief The set of "tentative" equivalences between two canonical
  229. /// declarations, mapping from a declaration in the first context to the
  230. /// declaration in the second context that we believe to be equivalent.
  231. llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
  232. /// \brief Queue of declarations in the first context whose equivalence
  233. /// with a declaration in the second context still needs to be verified.
  234. std::deque<Decl *> DeclsToCheck;
  235. /// \brief Declaration (from, to) pairs that are known not to be equivalent
  236. /// (which we have already complained about).
  237. llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
  238. /// \brief Whether we're being strict about the spelling of types when
  239. /// unifying two types.
  240. bool StrictTypeSpelling;
  241. /// \brief Whether to complain about failures.
  242. bool Complain;
  243. /// \brief \c true if the last diagnostic came from C2.
  244. bool LastDiagFromC2;
  245. StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
  246. llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
  247. bool StrictTypeSpelling = false,
  248. bool Complain = true)
  249. : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
  250. StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
  251. LastDiagFromC2(false) {}
  252. /// \brief Determine whether the two declarations are structurally
  253. /// equivalent.
  254. bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
  255. /// \brief Determine whether the two types are structurally equivalent.
  256. bool IsStructurallyEquivalent(QualType T1, QualType T2);
  257. private:
  258. /// \brief Finish checking all of the structural equivalences.
  259. ///
  260. /// \returns true if an error occurred, false otherwise.
  261. bool Finish();
  262. public:
  263. DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
  264. assert(Complain && "Not allowed to complain");
  265. if (LastDiagFromC2)
  266. C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
  267. LastDiagFromC2 = false;
  268. return C1.getDiagnostics().Report(Loc, DiagID);
  269. }
  270. DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
  271. assert(Complain && "Not allowed to complain");
  272. if (!LastDiagFromC2)
  273. C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
  274. LastDiagFromC2 = true;
  275. return C2.getDiagnostics().Report(Loc, DiagID);
  276. }
  277. };
  278. }
  279. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  280. QualType T1, QualType T2);
  281. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  282. Decl *D1, Decl *D2);
  283. /// \brief Determine structural equivalence of two expressions.
  284. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  285. Expr *E1, Expr *E2) {
  286. if (!E1 || !E2)
  287. return E1 == E2;
  288. // FIXME: Actually perform a structural comparison!
  289. return true;
  290. }
  291. /// \brief Determine whether two identifiers are equivalent.
  292. static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
  293. const IdentifierInfo *Name2) {
  294. if (!Name1 || !Name2)
  295. return Name1 == Name2;
  296. return Name1->getName() == Name2->getName();
  297. }
  298. /// \brief Determine whether two nested-name-specifiers are equivalent.
  299. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  300. NestedNameSpecifier *NNS1,
  301. NestedNameSpecifier *NNS2) {
  302. // FIXME: Implement!
  303. return true;
  304. }
  305. /// \brief Determine whether two template arguments are equivalent.
  306. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  307. const TemplateArgument &Arg1,
  308. const TemplateArgument &Arg2) {
  309. if (Arg1.getKind() != Arg2.getKind())
  310. return false;
  311. switch (Arg1.getKind()) {
  312. case TemplateArgument::Null:
  313. return true;
  314. case TemplateArgument::Type:
  315. return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
  316. case TemplateArgument::Integral:
  317. if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
  318. Arg2.getIntegralType()))
  319. return false;
  320. return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
  321. case TemplateArgument::Declaration:
  322. return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
  323. case TemplateArgument::NullPtr:
  324. return true; // FIXME: Is this correct?
  325. case TemplateArgument::Template:
  326. return IsStructurallyEquivalent(Context,
  327. Arg1.getAsTemplate(),
  328. Arg2.getAsTemplate());
  329. case TemplateArgument::TemplateExpansion:
  330. return IsStructurallyEquivalent(Context,
  331. Arg1.getAsTemplateOrTemplatePattern(),
  332. Arg2.getAsTemplateOrTemplatePattern());
  333. case TemplateArgument::Expression:
  334. return IsStructurallyEquivalent(Context,
  335. Arg1.getAsExpr(), Arg2.getAsExpr());
  336. case TemplateArgument::Pack:
  337. if (Arg1.pack_size() != Arg2.pack_size())
  338. return false;
  339. for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
  340. if (!IsStructurallyEquivalent(Context,
  341. Arg1.pack_begin()[I],
  342. Arg2.pack_begin()[I]))
  343. return false;
  344. return true;
  345. }
  346. llvm_unreachable("Invalid template argument kind");
  347. }
  348. /// \brief Determine structural equivalence for the common part of array
  349. /// types.
  350. static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
  351. const ArrayType *Array1,
  352. const ArrayType *Array2) {
  353. if (!IsStructurallyEquivalent(Context,
  354. Array1->getElementType(),
  355. Array2->getElementType()))
  356. return false;
  357. if (Array1->getSizeModifier() != Array2->getSizeModifier())
  358. return false;
  359. if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
  360. return false;
  361. return true;
  362. }
  363. /// \brief Determine structural equivalence of two types.
  364. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  365. QualType T1, QualType T2) {
  366. if (T1.isNull() || T2.isNull())
  367. return T1.isNull() && T2.isNull();
  368. if (!Context.StrictTypeSpelling) {
  369. // We aren't being strict about token-to-token equivalence of types,
  370. // so map down to the canonical type.
  371. T1 = Context.C1.getCanonicalType(T1);
  372. T2 = Context.C2.getCanonicalType(T2);
  373. }
  374. if (T1.getQualifiers() != T2.getQualifiers())
  375. return false;
  376. Type::TypeClass TC = T1->getTypeClass();
  377. if (T1->getTypeClass() != T2->getTypeClass()) {
  378. // Compare function types with prototypes vs. without prototypes as if
  379. // both did not have prototypes.
  380. if (T1->getTypeClass() == Type::FunctionProto &&
  381. T2->getTypeClass() == Type::FunctionNoProto)
  382. TC = Type::FunctionNoProto;
  383. else if (T1->getTypeClass() == Type::FunctionNoProto &&
  384. T2->getTypeClass() == Type::FunctionProto)
  385. TC = Type::FunctionNoProto;
  386. else
  387. return false;
  388. }
  389. switch (TC) {
  390. case Type::Builtin:
  391. // FIXME: Deal with Char_S/Char_U.
  392. if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
  393. return false;
  394. break;
  395. case Type::Complex:
  396. if (!IsStructurallyEquivalent(Context,
  397. cast<ComplexType>(T1)->getElementType(),
  398. cast<ComplexType>(T2)->getElementType()))
  399. return false;
  400. break;
  401. case Type::Adjusted:
  402. case Type::Decayed:
  403. if (!IsStructurallyEquivalent(Context,
  404. cast<AdjustedType>(T1)->getOriginalType(),
  405. cast<AdjustedType>(T2)->getOriginalType()))
  406. return false;
  407. break;
  408. case Type::Pointer:
  409. if (!IsStructurallyEquivalent(Context,
  410. cast<PointerType>(T1)->getPointeeType(),
  411. cast<PointerType>(T2)->getPointeeType()))
  412. return false;
  413. break;
  414. case Type::BlockPointer:
  415. if (!IsStructurallyEquivalent(Context,
  416. cast<BlockPointerType>(T1)->getPointeeType(),
  417. cast<BlockPointerType>(T2)->getPointeeType()))
  418. return false;
  419. break;
  420. case Type::LValueReference:
  421. case Type::RValueReference: {
  422. const ReferenceType *Ref1 = cast<ReferenceType>(T1);
  423. const ReferenceType *Ref2 = cast<ReferenceType>(T2);
  424. if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
  425. return false;
  426. if (Ref1->isInnerRef() != Ref2->isInnerRef())
  427. return false;
  428. if (!IsStructurallyEquivalent(Context,
  429. Ref1->getPointeeTypeAsWritten(),
  430. Ref2->getPointeeTypeAsWritten()))
  431. return false;
  432. break;
  433. }
  434. case Type::MemberPointer: {
  435. const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
  436. const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
  437. if (!IsStructurallyEquivalent(Context,
  438. MemPtr1->getPointeeType(),
  439. MemPtr2->getPointeeType()))
  440. return false;
  441. if (!IsStructurallyEquivalent(Context,
  442. QualType(MemPtr1->getClass(), 0),
  443. QualType(MemPtr2->getClass(), 0)))
  444. return false;
  445. break;
  446. }
  447. case Type::ConstantArray: {
  448. const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
  449. const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
  450. if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
  451. return false;
  452. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  453. return false;
  454. break;
  455. }
  456. case Type::IncompleteArray:
  457. if (!IsArrayStructurallyEquivalent(Context,
  458. cast<ArrayType>(T1),
  459. cast<ArrayType>(T2)))
  460. return false;
  461. break;
  462. case Type::VariableArray: {
  463. const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
  464. const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
  465. if (!IsStructurallyEquivalent(Context,
  466. Array1->getSizeExpr(), Array2->getSizeExpr()))
  467. return false;
  468. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  469. return false;
  470. break;
  471. }
  472. case Type::DependentSizedArray: {
  473. const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
  474. const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
  475. if (!IsStructurallyEquivalent(Context,
  476. Array1->getSizeExpr(), Array2->getSizeExpr()))
  477. return false;
  478. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  479. return false;
  480. break;
  481. }
  482. case Type::DependentSizedExtVector: {
  483. const DependentSizedExtVectorType *Vec1
  484. = cast<DependentSizedExtVectorType>(T1);
  485. const DependentSizedExtVectorType *Vec2
  486. = cast<DependentSizedExtVectorType>(T2);
  487. if (!IsStructurallyEquivalent(Context,
  488. Vec1->getSizeExpr(), Vec2->getSizeExpr()))
  489. return false;
  490. if (!IsStructurallyEquivalent(Context,
  491. Vec1->getElementType(),
  492. Vec2->getElementType()))
  493. return false;
  494. break;
  495. }
  496. case Type::Vector:
  497. case Type::ExtVector: {
  498. const VectorType *Vec1 = cast<VectorType>(T1);
  499. const VectorType *Vec2 = cast<VectorType>(T2);
  500. if (!IsStructurallyEquivalent(Context,
  501. Vec1->getElementType(),
  502. Vec2->getElementType()))
  503. return false;
  504. if (Vec1->getNumElements() != Vec2->getNumElements())
  505. return false;
  506. if (Vec1->getVectorKind() != Vec2->getVectorKind())
  507. return false;
  508. break;
  509. }
  510. case Type::FunctionProto: {
  511. const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
  512. const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
  513. if (Proto1->getNumParams() != Proto2->getNumParams())
  514. return false;
  515. for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
  516. if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
  517. Proto2->getParamType(I)))
  518. return false;
  519. }
  520. if (Proto1->isVariadic() != Proto2->isVariadic())
  521. return false;
  522. if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
  523. return false;
  524. if (Proto1->getExceptionSpecType() == EST_Dynamic) {
  525. if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
  526. return false;
  527. for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
  528. if (!IsStructurallyEquivalent(Context,
  529. Proto1->getExceptionType(I),
  530. Proto2->getExceptionType(I)))
  531. return false;
  532. }
  533. } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
  534. if (!IsStructurallyEquivalent(Context,
  535. Proto1->getNoexceptExpr(),
  536. Proto2->getNoexceptExpr()))
  537. return false;
  538. }
  539. if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
  540. return false;
  541. // Fall through to check the bits common with FunctionNoProtoType.
  542. }
  543. case Type::FunctionNoProto: {
  544. const FunctionType *Function1 = cast<FunctionType>(T1);
  545. const FunctionType *Function2 = cast<FunctionType>(T2);
  546. if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
  547. Function2->getReturnType()))
  548. return false;
  549. if (Function1->getExtInfo() != Function2->getExtInfo())
  550. return false;
  551. break;
  552. }
  553. case Type::UnresolvedUsing:
  554. if (!IsStructurallyEquivalent(Context,
  555. cast<UnresolvedUsingType>(T1)->getDecl(),
  556. cast<UnresolvedUsingType>(T2)->getDecl()))
  557. return false;
  558. break;
  559. case Type::Attributed:
  560. if (!IsStructurallyEquivalent(Context,
  561. cast<AttributedType>(T1)->getModifiedType(),
  562. cast<AttributedType>(T2)->getModifiedType()))
  563. return false;
  564. if (!IsStructurallyEquivalent(Context,
  565. cast<AttributedType>(T1)->getEquivalentType(),
  566. cast<AttributedType>(T2)->getEquivalentType()))
  567. return false;
  568. break;
  569. case Type::Paren:
  570. if (!IsStructurallyEquivalent(Context,
  571. cast<ParenType>(T1)->getInnerType(),
  572. cast<ParenType>(T2)->getInnerType()))
  573. return false;
  574. break;
  575. case Type::Typedef:
  576. if (!IsStructurallyEquivalent(Context,
  577. cast<TypedefType>(T1)->getDecl(),
  578. cast<TypedefType>(T2)->getDecl()))
  579. return false;
  580. break;
  581. case Type::TypeOfExpr:
  582. if (!IsStructurallyEquivalent(Context,
  583. cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
  584. cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
  585. return false;
  586. break;
  587. case Type::TypeOf:
  588. if (!IsStructurallyEquivalent(Context,
  589. cast<TypeOfType>(T1)->getUnderlyingType(),
  590. cast<TypeOfType>(T2)->getUnderlyingType()))
  591. return false;
  592. break;
  593. case Type::UnaryTransform:
  594. if (!IsStructurallyEquivalent(Context,
  595. cast<UnaryTransformType>(T1)->getUnderlyingType(),
  596. cast<UnaryTransformType>(T1)->getUnderlyingType()))
  597. return false;
  598. break;
  599. case Type::Decltype:
  600. if (!IsStructurallyEquivalent(Context,
  601. cast<DecltypeType>(T1)->getUnderlyingExpr(),
  602. cast<DecltypeType>(T2)->getUnderlyingExpr()))
  603. return false;
  604. break;
  605. case Type::Auto:
  606. if (!IsStructurallyEquivalent(Context,
  607. cast<AutoType>(T1)->getDeducedType(),
  608. cast<AutoType>(T2)->getDeducedType()))
  609. return false;
  610. break;
  611. case Type::Record:
  612. case Type::Enum:
  613. if (!IsStructurallyEquivalent(Context,
  614. cast<TagType>(T1)->getDecl(),
  615. cast<TagType>(T2)->getDecl()))
  616. return false;
  617. break;
  618. case Type::TemplateTypeParm: {
  619. const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
  620. const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
  621. if (Parm1->getDepth() != Parm2->getDepth())
  622. return false;
  623. if (Parm1->getIndex() != Parm2->getIndex())
  624. return false;
  625. if (Parm1->isParameterPack() != Parm2->isParameterPack())
  626. return false;
  627. // Names of template type parameters are never significant.
  628. break;
  629. }
  630. case Type::SubstTemplateTypeParm: {
  631. const SubstTemplateTypeParmType *Subst1
  632. = cast<SubstTemplateTypeParmType>(T1);
  633. const SubstTemplateTypeParmType *Subst2
  634. = cast<SubstTemplateTypeParmType>(T2);
  635. if (!IsStructurallyEquivalent(Context,
  636. QualType(Subst1->getReplacedParameter(), 0),
  637. QualType(Subst2->getReplacedParameter(), 0)))
  638. return false;
  639. if (!IsStructurallyEquivalent(Context,
  640. Subst1->getReplacementType(),
  641. Subst2->getReplacementType()))
  642. return false;
  643. break;
  644. }
  645. case Type::SubstTemplateTypeParmPack: {
  646. const SubstTemplateTypeParmPackType *Subst1
  647. = cast<SubstTemplateTypeParmPackType>(T1);
  648. const SubstTemplateTypeParmPackType *Subst2
  649. = cast<SubstTemplateTypeParmPackType>(T2);
  650. if (!IsStructurallyEquivalent(Context,
  651. QualType(Subst1->getReplacedParameter(), 0),
  652. QualType(Subst2->getReplacedParameter(), 0)))
  653. return false;
  654. if (!IsStructurallyEquivalent(Context,
  655. Subst1->getArgumentPack(),
  656. Subst2->getArgumentPack()))
  657. return false;
  658. break;
  659. }
  660. case Type::TemplateSpecialization: {
  661. const TemplateSpecializationType *Spec1
  662. = cast<TemplateSpecializationType>(T1);
  663. const TemplateSpecializationType *Spec2
  664. = cast<TemplateSpecializationType>(T2);
  665. if (!IsStructurallyEquivalent(Context,
  666. Spec1->getTemplateName(),
  667. Spec2->getTemplateName()))
  668. return false;
  669. if (Spec1->getNumArgs() != Spec2->getNumArgs())
  670. return false;
  671. for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
  672. if (!IsStructurallyEquivalent(Context,
  673. Spec1->getArg(I), Spec2->getArg(I)))
  674. return false;
  675. }
  676. break;
  677. }
  678. case Type::Elaborated: {
  679. const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
  680. const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
  681. // CHECKME: what if a keyword is ETK_None or ETK_typename ?
  682. if (Elab1->getKeyword() != Elab2->getKeyword())
  683. return false;
  684. if (!IsStructurallyEquivalent(Context,
  685. Elab1->getQualifier(),
  686. Elab2->getQualifier()))
  687. return false;
  688. if (!IsStructurallyEquivalent(Context,
  689. Elab1->getNamedType(),
  690. Elab2->getNamedType()))
  691. return false;
  692. break;
  693. }
  694. case Type::InjectedClassName: {
  695. const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
  696. const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
  697. if (!IsStructurallyEquivalent(Context,
  698. Inj1->getInjectedSpecializationType(),
  699. Inj2->getInjectedSpecializationType()))
  700. return false;
  701. break;
  702. }
  703. case Type::DependentName: {
  704. const DependentNameType *Typename1 = cast<DependentNameType>(T1);
  705. const DependentNameType *Typename2 = cast<DependentNameType>(T2);
  706. if (!IsStructurallyEquivalent(Context,
  707. Typename1->getQualifier(),
  708. Typename2->getQualifier()))
  709. return false;
  710. if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
  711. Typename2->getIdentifier()))
  712. return false;
  713. break;
  714. }
  715. case Type::DependentTemplateSpecialization: {
  716. const DependentTemplateSpecializationType *Spec1 =
  717. cast<DependentTemplateSpecializationType>(T1);
  718. const DependentTemplateSpecializationType *Spec2 =
  719. cast<DependentTemplateSpecializationType>(T2);
  720. if (!IsStructurallyEquivalent(Context,
  721. Spec1->getQualifier(),
  722. Spec2->getQualifier()))
  723. return false;
  724. if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
  725. Spec2->getIdentifier()))
  726. return false;
  727. if (Spec1->getNumArgs() != Spec2->getNumArgs())
  728. return false;
  729. for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
  730. if (!IsStructurallyEquivalent(Context,
  731. Spec1->getArg(I), Spec2->getArg(I)))
  732. return false;
  733. }
  734. break;
  735. }
  736. case Type::PackExpansion:
  737. if (!IsStructurallyEquivalent(Context,
  738. cast<PackExpansionType>(T1)->getPattern(),
  739. cast<PackExpansionType>(T2)->getPattern()))
  740. return false;
  741. break;
  742. case Type::ObjCInterface: {
  743. const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
  744. const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
  745. if (!IsStructurallyEquivalent(Context,
  746. Iface1->getDecl(), Iface2->getDecl()))
  747. return false;
  748. break;
  749. }
  750. case Type::ObjCObject: {
  751. const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
  752. const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
  753. if (!IsStructurallyEquivalent(Context,
  754. Obj1->getBaseType(),
  755. Obj2->getBaseType()))
  756. return false;
  757. if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
  758. return false;
  759. for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
  760. if (!IsStructurallyEquivalent(Context,
  761. Obj1->getProtocol(I),
  762. Obj2->getProtocol(I)))
  763. return false;
  764. }
  765. break;
  766. }
  767. case Type::ObjCObjectPointer: {
  768. const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
  769. const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
  770. if (!IsStructurallyEquivalent(Context,
  771. Ptr1->getPointeeType(),
  772. Ptr2->getPointeeType()))
  773. return false;
  774. break;
  775. }
  776. case Type::Atomic: {
  777. if (!IsStructurallyEquivalent(Context,
  778. cast<AtomicType>(T1)->getValueType(),
  779. cast<AtomicType>(T2)->getValueType()))
  780. return false;
  781. break;
  782. }
  783. } // end switch
  784. return true;
  785. }
  786. /// \brief Determine structural equivalence of two fields.
  787. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  788. FieldDecl *Field1, FieldDecl *Field2) {
  789. RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
  790. // For anonymous structs/unions, match up the anonymous struct/union type
  791. // declarations directly, so that we don't go off searching for anonymous
  792. // types
  793. if (Field1->isAnonymousStructOrUnion() &&
  794. Field2->isAnonymousStructOrUnion()) {
  795. RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
  796. RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
  797. return IsStructurallyEquivalent(Context, D1, D2);
  798. }
  799. // Check for equivalent field names.
  800. IdentifierInfo *Name1 = Field1->getIdentifier();
  801. IdentifierInfo *Name2 = Field2->getIdentifier();
  802. if (!::IsStructurallyEquivalent(Name1, Name2))
  803. return false;
  804. if (!IsStructurallyEquivalent(Context,
  805. Field1->getType(), Field2->getType())) {
  806. if (Context.Complain) {
  807. Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  808. << Context.C2.getTypeDeclType(Owner2);
  809. Context.Diag2(Field2->getLocation(), diag::note_odr_field)
  810. << Field2->getDeclName() << Field2->getType();
  811. Context.Diag1(Field1->getLocation(), diag::note_odr_field)
  812. << Field1->getDeclName() << Field1->getType();
  813. }
  814. return false;
  815. }
  816. if (Field1->isBitField() != Field2->isBitField()) {
  817. if (Context.Complain) {
  818. Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  819. << Context.C2.getTypeDeclType(Owner2);
  820. if (Field1->isBitField()) {
  821. Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
  822. << Field1->getDeclName() << Field1->getType()
  823. << Field1->getBitWidthValue(Context.C1);
  824. Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
  825. << Field2->getDeclName();
  826. } else {
  827. Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
  828. << Field2->getDeclName() << Field2->getType()
  829. << Field2->getBitWidthValue(Context.C2);
  830. Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
  831. << Field1->getDeclName();
  832. }
  833. }
  834. return false;
  835. }
  836. if (Field1->isBitField()) {
  837. // Make sure that the bit-fields are the same length.
  838. unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
  839. unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
  840. if (Bits1 != Bits2) {
  841. if (Context.Complain) {
  842. Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  843. << Context.C2.getTypeDeclType(Owner2);
  844. Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
  845. << Field2->getDeclName() << Field2->getType() << Bits2;
  846. Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
  847. << Field1->getDeclName() << Field1->getType() << Bits1;
  848. }
  849. return false;
  850. }
  851. }
  852. return true;
  853. }
  854. /// \brief Find the index of the given anonymous struct/union within its
  855. /// context.
  856. ///
  857. /// \returns Returns the index of this anonymous struct/union in its context,
  858. /// including the next assigned index (if none of them match). Returns an
  859. /// empty option if the context is not a record, i.e.. if the anonymous
  860. /// struct/union is at namespace or block scope.
  861. static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
  862. ASTContext &Context = Anon->getASTContext();
  863. QualType AnonTy = Context.getRecordType(Anon);
  864. RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
  865. if (!Owner)
  866. return None;
  867. unsigned Index = 0;
  868. for (const auto *D : Owner->noload_decls()) {
  869. const auto *F = dyn_cast<FieldDecl>(D);
  870. if (!F || !F->isAnonymousStructOrUnion())
  871. continue;
  872. if (Context.hasSameType(F->getType(), AnonTy))
  873. break;
  874. ++Index;
  875. }
  876. return Index;
  877. }
  878. /// \brief Determine structural equivalence of two records.
  879. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  880. RecordDecl *D1, RecordDecl *D2) {
  881. if (D1->isUnion() != D2->isUnion()) {
  882. if (Context.Complain) {
  883. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  884. << Context.C2.getTypeDeclType(D2);
  885. Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
  886. << D1->getDeclName() << (unsigned)D1->getTagKind();
  887. }
  888. return false;
  889. }
  890. if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
  891. // If both anonymous structs/unions are in a record context, make sure
  892. // they occur in the same location in the context records.
  893. if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
  894. if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
  895. if (*Index1 != *Index2)
  896. return false;
  897. }
  898. }
  899. }
  900. // If both declarations are class template specializations, we know
  901. // the ODR applies, so check the template and template arguments.
  902. ClassTemplateSpecializationDecl *Spec1
  903. = dyn_cast<ClassTemplateSpecializationDecl>(D1);
  904. ClassTemplateSpecializationDecl *Spec2
  905. = dyn_cast<ClassTemplateSpecializationDecl>(D2);
  906. if (Spec1 && Spec2) {
  907. // Check that the specialized templates are the same.
  908. if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
  909. Spec2->getSpecializedTemplate()))
  910. return false;
  911. // Check that the template arguments are the same.
  912. if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
  913. return false;
  914. for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
  915. if (!IsStructurallyEquivalent(Context,
  916. Spec1->getTemplateArgs().get(I),
  917. Spec2->getTemplateArgs().get(I)))
  918. return false;
  919. }
  920. // If one is a class template specialization and the other is not, these
  921. // structures are different.
  922. else if (Spec1 || Spec2)
  923. return false;
  924. // Compare the definitions of these two records. If either or both are
  925. // incomplete, we assume that they are equivalent.
  926. D1 = D1->getDefinition();
  927. D2 = D2->getDefinition();
  928. if (!D1 || !D2)
  929. return true;
  930. if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
  931. if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
  932. if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
  933. if (Context.Complain) {
  934. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  935. << Context.C2.getTypeDeclType(D2);
  936. Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
  937. << D2CXX->getNumBases();
  938. Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
  939. << D1CXX->getNumBases();
  940. }
  941. return false;
  942. }
  943. // Check the base classes.
  944. for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
  945. BaseEnd1 = D1CXX->bases_end(),
  946. Base2 = D2CXX->bases_begin();
  947. Base1 != BaseEnd1;
  948. ++Base1, ++Base2) {
  949. if (!IsStructurallyEquivalent(Context,
  950. Base1->getType(), Base2->getType())) {
  951. if (Context.Complain) {
  952. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  953. << Context.C2.getTypeDeclType(D2);
  954. Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
  955. << Base2->getType()
  956. << Base2->getSourceRange();
  957. Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
  958. << Base1->getType()
  959. << Base1->getSourceRange();
  960. }
  961. return false;
  962. }
  963. // Check virtual vs. non-virtual inheritance mismatch.
  964. if (Base1->isVirtual() != Base2->isVirtual()) {
  965. if (Context.Complain) {
  966. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  967. << Context.C2.getTypeDeclType(D2);
  968. Context.Diag2(Base2->getLocStart(),
  969. diag::note_odr_virtual_base)
  970. << Base2->isVirtual() << Base2->getSourceRange();
  971. Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
  972. << Base1->isVirtual()
  973. << Base1->getSourceRange();
  974. }
  975. return false;
  976. }
  977. }
  978. } else if (D1CXX->getNumBases() > 0) {
  979. if (Context.Complain) {
  980. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  981. << Context.C2.getTypeDeclType(D2);
  982. const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
  983. Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
  984. << Base1->getType()
  985. << Base1->getSourceRange();
  986. Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
  987. }
  988. return false;
  989. }
  990. }
  991. // Check the fields for consistency.
  992. RecordDecl::field_iterator Field2 = D2->field_begin(),
  993. Field2End = D2->field_end();
  994. for (RecordDecl::field_iterator Field1 = D1->field_begin(),
  995. Field1End = D1->field_end();
  996. Field1 != Field1End;
  997. ++Field1, ++Field2) {
  998. if (Field2 == Field2End) {
  999. if (Context.Complain) {
  1000. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  1001. << Context.C2.getTypeDeclType(D2);
  1002. Context.Diag1(Field1->getLocation(), diag::note_odr_field)
  1003. << Field1->getDeclName() << Field1->getType();
  1004. Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
  1005. }
  1006. return false;
  1007. }
  1008. if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
  1009. return false;
  1010. }
  1011. if (Field2 != Field2End) {
  1012. if (Context.Complain) {
  1013. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  1014. << Context.C2.getTypeDeclType(D2);
  1015. Context.Diag2(Field2->getLocation(), diag::note_odr_field)
  1016. << Field2->getDeclName() << Field2->getType();
  1017. Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
  1018. }
  1019. return false;
  1020. }
  1021. return true;
  1022. }
  1023. /// \brief Determine structural equivalence of two enums.
  1024. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1025. EnumDecl *D1, EnumDecl *D2) {
  1026. EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
  1027. EC2End = D2->enumerator_end();
  1028. for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
  1029. EC1End = D1->enumerator_end();
  1030. EC1 != EC1End; ++EC1, ++EC2) {
  1031. if (EC2 == EC2End) {
  1032. if (Context.Complain) {
  1033. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  1034. << Context.C2.getTypeDeclType(D2);
  1035. Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
  1036. << EC1->getDeclName()
  1037. << EC1->getInitVal().toString(10);
  1038. Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
  1039. }
  1040. return false;
  1041. }
  1042. llvm::APSInt Val1 = EC1->getInitVal();
  1043. llvm::APSInt Val2 = EC2->getInitVal();
  1044. if (!llvm::APSInt::isSameValue(Val1, Val2) ||
  1045. !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
  1046. if (Context.Complain) {
  1047. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  1048. << Context.C2.getTypeDeclType(D2);
  1049. Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
  1050. << EC2->getDeclName()
  1051. << EC2->getInitVal().toString(10);
  1052. Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
  1053. << EC1->getDeclName()
  1054. << EC1->getInitVal().toString(10);
  1055. }
  1056. return false;
  1057. }
  1058. }
  1059. if (EC2 != EC2End) {
  1060. if (Context.Complain) {
  1061. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  1062. << Context.C2.getTypeDeclType(D2);
  1063. Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
  1064. << EC2->getDeclName()
  1065. << EC2->getInitVal().toString(10);
  1066. Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
  1067. }
  1068. return false;
  1069. }
  1070. return true;
  1071. }
  1072. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1073. TemplateParameterList *Params1,
  1074. TemplateParameterList *Params2) {
  1075. if (Params1->size() != Params2->size()) {
  1076. if (Context.Complain) {
  1077. Context.Diag2(Params2->getTemplateLoc(),
  1078. diag::err_odr_different_num_template_parameters)
  1079. << Params1->size() << Params2->size();
  1080. Context.Diag1(Params1->getTemplateLoc(),
  1081. diag::note_odr_template_parameter_list);
  1082. }
  1083. return false;
  1084. }
  1085. for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
  1086. if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
  1087. if (Context.Complain) {
  1088. Context.Diag2(Params2->getParam(I)->getLocation(),
  1089. diag::err_odr_different_template_parameter_kind);
  1090. Context.Diag1(Params1->getParam(I)->getLocation(),
  1091. diag::note_odr_template_parameter_here);
  1092. }
  1093. return false;
  1094. }
  1095. if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
  1096. Params2->getParam(I))) {
  1097. return false;
  1098. }
  1099. }
  1100. return true;
  1101. }
  1102. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1103. TemplateTypeParmDecl *D1,
  1104. TemplateTypeParmDecl *D2) {
  1105. if (D1->isParameterPack() != D2->isParameterPack()) {
  1106. if (Context.Complain) {
  1107. Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
  1108. << D2->isParameterPack();
  1109. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1110. << D1->isParameterPack();
  1111. }
  1112. return false;
  1113. }
  1114. return true;
  1115. }
  1116. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1117. NonTypeTemplateParmDecl *D1,
  1118. NonTypeTemplateParmDecl *D2) {
  1119. if (D1->isParameterPack() != D2->isParameterPack()) {
  1120. if (Context.Complain) {
  1121. Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
  1122. << D2->isParameterPack();
  1123. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1124. << D1->isParameterPack();
  1125. }
  1126. return false;
  1127. }
  1128. // Check types.
  1129. if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
  1130. if (Context.Complain) {
  1131. Context.Diag2(D2->getLocation(),
  1132. diag::err_odr_non_type_parameter_type_inconsistent)
  1133. << D2->getType() << D1->getType();
  1134. Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
  1135. << D1->getType();
  1136. }
  1137. return false;
  1138. }
  1139. return true;
  1140. }
  1141. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1142. TemplateTemplateParmDecl *D1,
  1143. TemplateTemplateParmDecl *D2) {
  1144. if (D1->isParameterPack() != D2->isParameterPack()) {
  1145. if (Context.Complain) {
  1146. Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
  1147. << D2->isParameterPack();
  1148. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1149. << D1->isParameterPack();
  1150. }
  1151. return false;
  1152. }
  1153. // Check template parameter lists.
  1154. return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
  1155. D2->getTemplateParameters());
  1156. }
  1157. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1158. ClassTemplateDecl *D1,
  1159. ClassTemplateDecl *D2) {
  1160. // Check template parameters.
  1161. if (!IsStructurallyEquivalent(Context,
  1162. D1->getTemplateParameters(),
  1163. D2->getTemplateParameters()))
  1164. return false;
  1165. // Check the templated declaration.
  1166. return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
  1167. D2->getTemplatedDecl());
  1168. }
  1169. /// \brief Determine structural equivalence of two declarations.
  1170. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1171. Decl *D1, Decl *D2) {
  1172. // FIXME: Check for known structural equivalences via a callback of some sort.
  1173. // Check whether we already know that these two declarations are not
  1174. // structurally equivalent.
  1175. if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
  1176. D2->getCanonicalDecl())))
  1177. return false;
  1178. // Determine whether we've already produced a tentative equivalence for D1.
  1179. Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
  1180. if (EquivToD1)
  1181. return EquivToD1 == D2->getCanonicalDecl();
  1182. // Produce a tentative equivalence D1 <-> D2, which will be checked later.
  1183. EquivToD1 = D2->getCanonicalDecl();
  1184. Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
  1185. return true;
  1186. }
  1187. bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
  1188. Decl *D2) {
  1189. if (!::IsStructurallyEquivalent(*this, D1, D2))
  1190. return false;
  1191. return !Finish();
  1192. }
  1193. bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
  1194. QualType T2) {
  1195. if (!::IsStructurallyEquivalent(*this, T1, T2))
  1196. return false;
  1197. return !Finish();
  1198. }
  1199. bool StructuralEquivalenceContext::Finish() {
  1200. while (!DeclsToCheck.empty()) {
  1201. // Check the next declaration.
  1202. Decl *D1 = DeclsToCheck.front();
  1203. DeclsToCheck.pop_front();
  1204. Decl *D2 = TentativeEquivalences[D1];
  1205. assert(D2 && "Unrecorded tentative equivalence?");
  1206. bool Equivalent = true;
  1207. // FIXME: Switch on all declaration kinds. For now, we're just going to
  1208. // check the obvious ones.
  1209. if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
  1210. if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
  1211. // Check for equivalent structure names.
  1212. IdentifierInfo *Name1 = Record1->getIdentifier();
  1213. if (!Name1 && Record1->getTypedefNameForAnonDecl())
  1214. Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
  1215. IdentifierInfo *Name2 = Record2->getIdentifier();
  1216. if (!Name2 && Record2->getTypedefNameForAnonDecl())
  1217. Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
  1218. if (!::IsStructurallyEquivalent(Name1, Name2) ||
  1219. !::IsStructurallyEquivalent(*this, Record1, Record2))
  1220. Equivalent = false;
  1221. } else {
  1222. // Record/non-record mismatch.
  1223. Equivalent = false;
  1224. }
  1225. } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
  1226. if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
  1227. // Check for equivalent enum names.
  1228. IdentifierInfo *Name1 = Enum1->getIdentifier();
  1229. if (!Name1 && Enum1->getTypedefNameForAnonDecl())
  1230. Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
  1231. IdentifierInfo *Name2 = Enum2->getIdentifier();
  1232. if (!Name2 && Enum2->getTypedefNameForAnonDecl())
  1233. Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
  1234. if (!::IsStructurallyEquivalent(Name1, Name2) ||
  1235. !::IsStructurallyEquivalent(*this, Enum1, Enum2))
  1236. Equivalent = false;
  1237. } else {
  1238. // Enum/non-enum mismatch
  1239. Equivalent = false;
  1240. }
  1241. } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
  1242. if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
  1243. if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
  1244. Typedef2->getIdentifier()) ||
  1245. !::IsStructurallyEquivalent(*this,
  1246. Typedef1->getUnderlyingType(),
  1247. Typedef2->getUnderlyingType()))
  1248. Equivalent = false;
  1249. } else {
  1250. // Typedef/non-typedef mismatch.
  1251. Equivalent = false;
  1252. }
  1253. } else if (ClassTemplateDecl *ClassTemplate1
  1254. = dyn_cast<ClassTemplateDecl>(D1)) {
  1255. if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
  1256. if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
  1257. ClassTemplate2->getIdentifier()) ||
  1258. !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
  1259. Equivalent = false;
  1260. } else {
  1261. // Class template/non-class-template mismatch.
  1262. Equivalent = false;
  1263. }
  1264. } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
  1265. if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
  1266. if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
  1267. Equivalent = false;
  1268. } else {
  1269. // Kind mismatch.
  1270. Equivalent = false;
  1271. }
  1272. } else if (NonTypeTemplateParmDecl *NTTP1
  1273. = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
  1274. if (NonTypeTemplateParmDecl *NTTP2
  1275. = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
  1276. if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
  1277. Equivalent = false;
  1278. } else {
  1279. // Kind mismatch.
  1280. Equivalent = false;
  1281. }
  1282. } else if (TemplateTemplateParmDecl *TTP1
  1283. = dyn_cast<TemplateTemplateParmDecl>(D1)) {
  1284. if (TemplateTemplateParmDecl *TTP2
  1285. = dyn_cast<TemplateTemplateParmDecl>(D2)) {
  1286. if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
  1287. Equivalent = false;
  1288. } else {
  1289. // Kind mismatch.
  1290. Equivalent = false;
  1291. }
  1292. }
  1293. if (!Equivalent) {
  1294. // Note that these two declarations are not equivalent (and we already
  1295. // know about it).
  1296. NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
  1297. D2->getCanonicalDecl()));
  1298. return true;
  1299. }
  1300. // FIXME: Check other declaration kinds!
  1301. }
  1302. return false;
  1303. }
  1304. //----------------------------------------------------------------------------
  1305. // Import Types
  1306. //----------------------------------------------------------------------------
  1307. QualType ASTNodeImporter::VisitType(const Type *T) {
  1308. Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
  1309. << T->getTypeClassName();
  1310. return QualType();
  1311. }
  1312. QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
  1313. switch (T->getKind()) {
  1314. #define SHARED_SINGLETON_TYPE(Expansion)
  1315. #define BUILTIN_TYPE(Id, SingletonId) \
  1316. case BuiltinType::Id: return Importer.getToContext().SingletonId;
  1317. #include "clang/AST/BuiltinTypes.def"
  1318. // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
  1319. // context supports C++.
  1320. // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
  1321. // context supports ObjC.
  1322. case BuiltinType::Char_U:
  1323. // The context we're importing from has an unsigned 'char'. If we're
  1324. // importing into a context with a signed 'char', translate to
  1325. // 'unsigned char' instead.
  1326. if (Importer.getToContext().getLangOpts().CharIsSigned)
  1327. return Importer.getToContext().UnsignedCharTy;
  1328. return Importer.getToContext().CharTy;
  1329. case BuiltinType::Char_S:
  1330. // The context we're importing from has an unsigned 'char'. If we're
  1331. // importing into a context with a signed 'char', translate to
  1332. // 'unsigned char' instead.
  1333. if (!Importer.getToContext().getLangOpts().CharIsSigned)
  1334. return Importer.getToContext().SignedCharTy;
  1335. return Importer.getToContext().CharTy;
  1336. case BuiltinType::WChar_S:
  1337. case BuiltinType::WChar_U:
  1338. // FIXME: If not in C++, shall we translate to the C equivalent of
  1339. // wchar_t?
  1340. return Importer.getToContext().WCharTy;
  1341. }
  1342. llvm_unreachable("Invalid BuiltinType Kind!");
  1343. }
  1344. QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
  1345. QualType ToElementType = Importer.Import(T->getElementType());
  1346. if (ToElementType.isNull())
  1347. return QualType();
  1348. return Importer.getToContext().getComplexType(ToElementType);
  1349. }
  1350. QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
  1351. QualType ToPointeeType = Importer.Import(T->getPointeeType());
  1352. if (ToPointeeType.isNull())
  1353. return QualType();
  1354. return Importer.getToContext().getPointerType(ToPointeeType);
  1355. }
  1356. QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
  1357. // FIXME: Check for blocks support in "to" context.
  1358. QualType ToPointeeType = Importer.Import(T->getPointeeType());
  1359. if (ToPointeeType.isNull())
  1360. return QualType();
  1361. return Importer.getToContext().getBlockPointerType(ToPointeeType);
  1362. }
  1363. QualType
  1364. ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
  1365. // FIXME: Check for C++ support in "to" context.
  1366. QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
  1367. if (ToPointeeType.isNull())
  1368. return QualType();
  1369. return Importer.getToContext().getLValueReferenceType(ToPointeeType);
  1370. }
  1371. QualType
  1372. ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
  1373. // FIXME: Check for C++0x support in "to" context.
  1374. QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
  1375. if (ToPointeeType.isNull())
  1376. return QualType();
  1377. return Importer.getToContext().getRValueReferenceType(ToPointeeType);
  1378. }
  1379. QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
  1380. // FIXME: Check for C++ support in "to" context.
  1381. QualType ToPointeeType = Importer.Import(T->getPointeeType());
  1382. if (ToPointeeType.isNull())
  1383. return QualType();
  1384. QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
  1385. return Importer.getToContext().getMemberPointerType(ToPointeeType,
  1386. ClassType.getTypePtr());
  1387. }
  1388. QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
  1389. QualType ToElementType = Importer.Import(T->getElementType());
  1390. if (ToElementType.isNull())
  1391. return QualType();
  1392. return Importer.getToContext().getConstantArrayType(ToElementType,
  1393. T->getSize(),
  1394. T->getSizeModifier(),
  1395. T->getIndexTypeCVRQualifiers());
  1396. }
  1397. QualType
  1398. ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
  1399. QualType ToElementType = Importer.Import(T->getElementType());
  1400. if (ToElementType.isNull())
  1401. return QualType();
  1402. return Importer.getToContext().getIncompleteArrayType(ToElementType,
  1403. T->getSizeModifier(),
  1404. T->getIndexTypeCVRQualifiers());
  1405. }
  1406. QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
  1407. QualType ToElementType = Importer.Import(T->getElementType());
  1408. if (ToElementType.isNull())
  1409. return QualType();
  1410. Expr *Size = Importer.Import(T->getSizeExpr());
  1411. if (!Size)
  1412. return QualType();
  1413. SourceRange Brackets = Importer.Import(T->getBracketsRange());
  1414. return Importer.getToContext().getVariableArrayType(ToElementType, Size,
  1415. T->getSizeModifier(),
  1416. T->getIndexTypeCVRQualifiers(),
  1417. Brackets);
  1418. }
  1419. QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
  1420. QualType ToElementType = Importer.Import(T->getElementType());
  1421. if (ToElementType.isNull())
  1422. return QualType();
  1423. return Importer.getToContext().getVectorType(ToElementType,
  1424. T->getNumElements(),
  1425. T->getVectorKind());
  1426. }
  1427. QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
  1428. QualType ToElementType = Importer.Import(T->getElementType());
  1429. if (ToElementType.isNull())
  1430. return QualType();
  1431. return Importer.getToContext().getExtVectorType(ToElementType,
  1432. T->getNumElements());
  1433. }
  1434. QualType
  1435. ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
  1436. // FIXME: What happens if we're importing a function without a prototype
  1437. // into C++? Should we make it variadic?
  1438. QualType ToResultType = Importer.Import(T->getReturnType());
  1439. if (ToResultType.isNull())
  1440. return QualType();
  1441. return Importer.getToContext().getFunctionNoProtoType(ToResultType,
  1442. T->getExtInfo());
  1443. }
  1444. QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
  1445. QualType ToResultType = Importer.Import(T->getReturnType());
  1446. if (ToResultType.isNull())
  1447. return QualType();
  1448. // Import argument types
  1449. SmallVector<QualType, 4> ArgTypes;
  1450. for (const auto &A : T->param_types()) {
  1451. QualType ArgType = Importer.Import(A);
  1452. if (ArgType.isNull())
  1453. return QualType();
  1454. ArgTypes.push_back(ArgType);
  1455. }
  1456. // Import exception types
  1457. SmallVector<QualType, 4> ExceptionTypes;
  1458. for (const auto &E : T->exceptions()) {
  1459. QualType ExceptionType = Importer.Import(E);
  1460. if (ExceptionType.isNull())
  1461. return QualType();
  1462. ExceptionTypes.push_back(ExceptionType);
  1463. }
  1464. FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
  1465. FunctionProtoType::ExtProtoInfo ToEPI;
  1466. ToEPI.ExtInfo = FromEPI.ExtInfo;
  1467. ToEPI.Variadic = FromEPI.Variadic;
  1468. ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
  1469. ToEPI.TypeQuals = FromEPI.TypeQuals;
  1470. ToEPI.RefQualifier = FromEPI.RefQualifier;
  1471. ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
  1472. ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
  1473. ToEPI.ExceptionSpec.NoexceptExpr =
  1474. Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
  1475. ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
  1476. Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
  1477. ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
  1478. Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
  1479. return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI, None); // HLSL Change - add param modifiers
  1480. }
  1481. QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
  1482. QualType ToInnerType = Importer.Import(T->getInnerType());
  1483. if (ToInnerType.isNull())
  1484. return QualType();
  1485. return Importer.getToContext().getParenType(ToInnerType);
  1486. }
  1487. QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
  1488. TypedefNameDecl *ToDecl
  1489. = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
  1490. if (!ToDecl)
  1491. return QualType();
  1492. return Importer.getToContext().getTypeDeclType(ToDecl);
  1493. }
  1494. QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
  1495. Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
  1496. if (!ToExpr)
  1497. return QualType();
  1498. return Importer.getToContext().getTypeOfExprType(ToExpr);
  1499. }
  1500. QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
  1501. QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
  1502. if (ToUnderlyingType.isNull())
  1503. return QualType();
  1504. return Importer.getToContext().getTypeOfType(ToUnderlyingType);
  1505. }
  1506. QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
  1507. // FIXME: Make sure that the "to" context supports C++0x!
  1508. Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
  1509. if (!ToExpr)
  1510. return QualType();
  1511. QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
  1512. if (UnderlyingType.isNull())
  1513. return QualType();
  1514. return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
  1515. }
  1516. QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
  1517. QualType ToBaseType = Importer.Import(T->getBaseType());
  1518. QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
  1519. if (ToBaseType.isNull() || ToUnderlyingType.isNull())
  1520. return QualType();
  1521. return Importer.getToContext().getUnaryTransformType(ToBaseType,
  1522. ToUnderlyingType,
  1523. T->getUTTKind());
  1524. }
  1525. QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
  1526. // FIXME: Make sure that the "to" context supports C++11!
  1527. QualType FromDeduced = T->getDeducedType();
  1528. QualType ToDeduced;
  1529. if (!FromDeduced.isNull()) {
  1530. ToDeduced = Importer.Import(FromDeduced);
  1531. if (ToDeduced.isNull())
  1532. return QualType();
  1533. }
  1534. return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(),
  1535. /*IsDependent*/false);
  1536. }
  1537. QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
  1538. RecordDecl *ToDecl
  1539. = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
  1540. if (!ToDecl)
  1541. return QualType();
  1542. return Importer.getToContext().getTagDeclType(ToDecl);
  1543. }
  1544. QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
  1545. EnumDecl *ToDecl
  1546. = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
  1547. if (!ToDecl)
  1548. return QualType();
  1549. return Importer.getToContext().getTagDeclType(ToDecl);
  1550. }
  1551. QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
  1552. QualType FromModifiedType = T->getModifiedType();
  1553. QualType FromEquivalentType = T->getEquivalentType();
  1554. QualType ToModifiedType;
  1555. QualType ToEquivalentType;
  1556. if (!FromModifiedType.isNull()) {
  1557. ToModifiedType = Importer.Import(FromModifiedType);
  1558. if (ToModifiedType.isNull())
  1559. return QualType();
  1560. }
  1561. if (!FromEquivalentType.isNull()) {
  1562. ToEquivalentType = Importer.Import(FromEquivalentType);
  1563. if (ToEquivalentType.isNull())
  1564. return QualType();
  1565. }
  1566. return Importer.getToContext().getAttributedType(T->getAttrKind(),
  1567. ToModifiedType, ToEquivalentType);
  1568. }
  1569. QualType ASTNodeImporter::VisitTemplateSpecializationType(
  1570. const TemplateSpecializationType *T) {
  1571. TemplateName ToTemplate = Importer.Import(T->getTemplateName());
  1572. if (ToTemplate.isNull())
  1573. return QualType();
  1574. SmallVector<TemplateArgument, 2> ToTemplateArgs;
  1575. if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
  1576. return QualType();
  1577. QualType ToCanonType;
  1578. if (!QualType(T, 0).isCanonical()) {
  1579. QualType FromCanonType
  1580. = Importer.getFromContext().getCanonicalType(QualType(T, 0));
  1581. ToCanonType =Importer.Import(FromCanonType);
  1582. if (ToCanonType.isNull())
  1583. return QualType();
  1584. }
  1585. return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
  1586. ToTemplateArgs.data(),
  1587. ToTemplateArgs.size(),
  1588. ToCanonType);
  1589. }
  1590. QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
  1591. NestedNameSpecifier *ToQualifier = nullptr;
  1592. // Note: the qualifier in an ElaboratedType is optional.
  1593. if (T->getQualifier()) {
  1594. ToQualifier = Importer.Import(T->getQualifier());
  1595. if (!ToQualifier)
  1596. return QualType();
  1597. }
  1598. QualType ToNamedType = Importer.Import(T->getNamedType());
  1599. if (ToNamedType.isNull())
  1600. return QualType();
  1601. return Importer.getToContext().getElaboratedType(T->getKeyword(),
  1602. ToQualifier, ToNamedType);
  1603. }
  1604. QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
  1605. ObjCInterfaceDecl *Class
  1606. = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
  1607. if (!Class)
  1608. return QualType();
  1609. return Importer.getToContext().getObjCInterfaceType(Class);
  1610. }
  1611. QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
  1612. QualType ToBaseType = Importer.Import(T->getBaseType());
  1613. if (ToBaseType.isNull())
  1614. return QualType();
  1615. SmallVector<QualType, 4> TypeArgs;
  1616. for (auto TypeArg : T->getTypeArgsAsWritten()) {
  1617. QualType ImportedTypeArg = Importer.Import(TypeArg);
  1618. if (ImportedTypeArg.isNull())
  1619. return QualType();
  1620. TypeArgs.push_back(ImportedTypeArg);
  1621. }
  1622. SmallVector<ObjCProtocolDecl *, 4> Protocols;
  1623. for (auto *P : T->quals()) {
  1624. ObjCProtocolDecl *Protocol
  1625. = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
  1626. if (!Protocol)
  1627. return QualType();
  1628. Protocols.push_back(Protocol);
  1629. }
  1630. return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
  1631. Protocols,
  1632. T->isKindOfTypeAsWritten());
  1633. }
  1634. QualType
  1635. ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
  1636. QualType ToPointeeType = Importer.Import(T->getPointeeType());
  1637. if (ToPointeeType.isNull())
  1638. return QualType();
  1639. return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
  1640. }
  1641. //----------------------------------------------------------------------------
  1642. // Import Declarations
  1643. //----------------------------------------------------------------------------
  1644. bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
  1645. DeclContext *&LexicalDC,
  1646. DeclarationName &Name,
  1647. NamedDecl *&ToD,
  1648. SourceLocation &Loc) {
  1649. // Import the context of this declaration.
  1650. DC = Importer.ImportContext(D->getDeclContext());
  1651. if (!DC)
  1652. return true;
  1653. LexicalDC = DC;
  1654. if (D->getDeclContext() != D->getLexicalDeclContext()) {
  1655. LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
  1656. if (!LexicalDC)
  1657. return true;
  1658. }
  1659. // Import the name of this declaration.
  1660. Name = Importer.Import(D->getDeclName());
  1661. if (D->getDeclName() && !Name)
  1662. return true;
  1663. // Import the location of this declaration.
  1664. Loc = Importer.Import(D->getLocation());
  1665. ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
  1666. return false;
  1667. }
  1668. void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
  1669. if (!FromD)
  1670. return;
  1671. if (!ToD) {
  1672. ToD = Importer.Import(FromD);
  1673. if (!ToD)
  1674. return;
  1675. }
  1676. if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
  1677. if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
  1678. if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
  1679. ImportDefinition(FromRecord, ToRecord);
  1680. }
  1681. }
  1682. return;
  1683. }
  1684. if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
  1685. if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
  1686. if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
  1687. ImportDefinition(FromEnum, ToEnum);
  1688. }
  1689. }
  1690. return;
  1691. }
  1692. }
  1693. void
  1694. ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
  1695. DeclarationNameInfo& To) {
  1696. // NOTE: To.Name and To.Loc are already imported.
  1697. // We only have to import To.LocInfo.
  1698. switch (To.getName().getNameKind()) {
  1699. case DeclarationName::Identifier:
  1700. case DeclarationName::ObjCZeroArgSelector:
  1701. case DeclarationName::ObjCOneArgSelector:
  1702. case DeclarationName::ObjCMultiArgSelector:
  1703. case DeclarationName::CXXUsingDirective:
  1704. return;
  1705. case DeclarationName::CXXOperatorName: {
  1706. SourceRange Range = From.getCXXOperatorNameRange();
  1707. To.setCXXOperatorNameRange(Importer.Import(Range));
  1708. return;
  1709. }
  1710. case DeclarationName::CXXLiteralOperatorName: {
  1711. SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
  1712. To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
  1713. return;
  1714. }
  1715. case DeclarationName::CXXConstructorName:
  1716. case DeclarationName::CXXDestructorName:
  1717. case DeclarationName::CXXConversionFunctionName: {
  1718. TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
  1719. To.setNamedTypeInfo(Importer.Import(FromTInfo));
  1720. return;
  1721. }
  1722. }
  1723. llvm_unreachable("Unknown name kind.");
  1724. }
  1725. void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
  1726. if (Importer.isMinimalImport() && !ForceImport) {
  1727. Importer.ImportContext(FromDC);
  1728. return;
  1729. }
  1730. for (auto *From : FromDC->decls())
  1731. Importer.Import(From);
  1732. }
  1733. bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
  1734. ImportDefinitionKind Kind) {
  1735. if (To->getDefinition() || To->isBeingDefined()) {
  1736. if (Kind == IDK_Everything)
  1737. ImportDeclContext(From, /*ForceImport=*/true);
  1738. return false;
  1739. }
  1740. To->startDefinition();
  1741. // Add base classes.
  1742. if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
  1743. CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
  1744. struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
  1745. struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
  1746. ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
  1747. ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
  1748. ToData.Aggregate = FromData.Aggregate;
  1749. ToData.PlainOldData = FromData.PlainOldData;
  1750. ToData.Empty = FromData.Empty;
  1751. ToData.Polymorphic = FromData.Polymorphic;
  1752. ToData.Abstract = FromData.Abstract;
  1753. ToData.IsStandardLayout = FromData.IsStandardLayout;
  1754. ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
  1755. ToData.HasPrivateFields = FromData.HasPrivateFields;
  1756. ToData.HasProtectedFields = FromData.HasProtectedFields;
  1757. ToData.HasPublicFields = FromData.HasPublicFields;
  1758. ToData.HasMutableFields = FromData.HasMutableFields;
  1759. ToData.HasVariantMembers = FromData.HasVariantMembers;
  1760. ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
  1761. ToData.HasInClassInitializer = FromData.HasInClassInitializer;
  1762. ToData.HasUninitializedReferenceMember
  1763. = FromData.HasUninitializedReferenceMember;
  1764. ToData.NeedOverloadResolutionForMoveConstructor
  1765. = FromData.NeedOverloadResolutionForMoveConstructor;
  1766. ToData.NeedOverloadResolutionForMoveAssignment
  1767. = FromData.NeedOverloadResolutionForMoveAssignment;
  1768. ToData.NeedOverloadResolutionForDestructor
  1769. = FromData.NeedOverloadResolutionForDestructor;
  1770. ToData.DefaultedMoveConstructorIsDeleted
  1771. = FromData.DefaultedMoveConstructorIsDeleted;
  1772. ToData.DefaultedMoveAssignmentIsDeleted
  1773. = FromData.DefaultedMoveAssignmentIsDeleted;
  1774. ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
  1775. ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
  1776. ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
  1777. ToData.HasConstexprNonCopyMoveConstructor
  1778. = FromData.HasConstexprNonCopyMoveConstructor;
  1779. ToData.DefaultedDefaultConstructorIsConstexpr
  1780. = FromData.DefaultedDefaultConstructorIsConstexpr;
  1781. ToData.HasConstexprDefaultConstructor
  1782. = FromData.HasConstexprDefaultConstructor;
  1783. ToData.HasNonLiteralTypeFieldsOrBases
  1784. = FromData.HasNonLiteralTypeFieldsOrBases;
  1785. // ComputedVisibleConversions not imported.
  1786. ToData.UserProvidedDefaultConstructor
  1787. = FromData.UserProvidedDefaultConstructor;
  1788. ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
  1789. ToData.ImplicitCopyConstructorHasConstParam
  1790. = FromData.ImplicitCopyConstructorHasConstParam;
  1791. ToData.ImplicitCopyAssignmentHasConstParam
  1792. = FromData.ImplicitCopyAssignmentHasConstParam;
  1793. ToData.HasDeclaredCopyConstructorWithConstParam
  1794. = FromData.HasDeclaredCopyConstructorWithConstParam;
  1795. ToData.HasDeclaredCopyAssignmentWithConstParam
  1796. = FromData.HasDeclaredCopyAssignmentWithConstParam;
  1797. ToData.IsLambda = FromData.IsLambda;
  1798. SmallVector<CXXBaseSpecifier *, 4> Bases;
  1799. for (const auto &Base1 : FromCXX->bases()) {
  1800. QualType T = Importer.Import(Base1.getType());
  1801. if (T.isNull())
  1802. return true;
  1803. SourceLocation EllipsisLoc;
  1804. if (Base1.isPackExpansion())
  1805. EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
  1806. // Ensure that we have a definition for the base.
  1807. ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
  1808. Bases.push_back(
  1809. new (Importer.getToContext())
  1810. CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
  1811. Base1.isVirtual(),
  1812. Base1.isBaseOfClass(),
  1813. Base1.getAccessSpecifierAsWritten(),
  1814. Importer.Import(Base1.getTypeSourceInfo()),
  1815. EllipsisLoc));
  1816. }
  1817. if (!Bases.empty())
  1818. ToCXX->setBases(Bases.data(), Bases.size());
  1819. }
  1820. if (shouldForceImportDeclContext(Kind))
  1821. ImportDeclContext(From, /*ForceImport=*/true);
  1822. To->completeDefinition();
  1823. return false;
  1824. }
  1825. bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
  1826. ImportDefinitionKind Kind) {
  1827. if (To->getAnyInitializer())
  1828. return false;
  1829. // FIXME: Can we really import any initializer? Alternatively, we could force
  1830. // ourselves to import every declaration of a variable and then only use
  1831. // getInit() here.
  1832. To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
  1833. // FIXME: Other bits to merge?
  1834. return false;
  1835. }
  1836. bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
  1837. ImportDefinitionKind Kind) {
  1838. if (To->getDefinition() || To->isBeingDefined()) {
  1839. if (Kind == IDK_Everything)
  1840. ImportDeclContext(From, /*ForceImport=*/true);
  1841. return false;
  1842. }
  1843. To->startDefinition();
  1844. QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
  1845. if (T.isNull())
  1846. return true;
  1847. QualType ToPromotionType = Importer.Import(From->getPromotionType());
  1848. if (ToPromotionType.isNull())
  1849. return true;
  1850. if (shouldForceImportDeclContext(Kind))
  1851. ImportDeclContext(From, /*ForceImport=*/true);
  1852. // FIXME: we might need to merge the number of positive or negative bits
  1853. // if the enumerator lists don't match.
  1854. To->completeDefinition(T, ToPromotionType,
  1855. From->getNumPositiveBits(),
  1856. From->getNumNegativeBits());
  1857. return false;
  1858. }
  1859. TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
  1860. TemplateParameterList *Params) {
  1861. SmallVector<NamedDecl *, 4> ToParams;
  1862. ToParams.reserve(Params->size());
  1863. for (TemplateParameterList::iterator P = Params->begin(),
  1864. PEnd = Params->end();
  1865. P != PEnd; ++P) {
  1866. Decl *To = Importer.Import(*P);
  1867. if (!To)
  1868. return nullptr;
  1869. ToParams.push_back(cast<NamedDecl>(To));
  1870. }
  1871. return TemplateParameterList::Create(Importer.getToContext(),
  1872. Importer.Import(Params->getTemplateLoc()),
  1873. Importer.Import(Params->getLAngleLoc()),
  1874. ToParams.data(), ToParams.size(),
  1875. Importer.Import(Params->getRAngleLoc()));
  1876. }
  1877. TemplateArgument
  1878. ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
  1879. switch (From.getKind()) {
  1880. case TemplateArgument::Null:
  1881. return TemplateArgument();
  1882. case TemplateArgument::Type: {
  1883. QualType ToType = Importer.Import(From.getAsType());
  1884. if (ToType.isNull())
  1885. return TemplateArgument();
  1886. return TemplateArgument(ToType);
  1887. }
  1888. case TemplateArgument::Integral: {
  1889. QualType ToType = Importer.Import(From.getIntegralType());
  1890. if (ToType.isNull())
  1891. return TemplateArgument();
  1892. return TemplateArgument(From, ToType);
  1893. }
  1894. case TemplateArgument::Declaration: {
  1895. ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
  1896. QualType ToType = Importer.Import(From.getParamTypeForDecl());
  1897. if (!To || ToType.isNull())
  1898. return TemplateArgument();
  1899. return TemplateArgument(To, ToType);
  1900. }
  1901. case TemplateArgument::NullPtr: {
  1902. QualType ToType = Importer.Import(From.getNullPtrType());
  1903. if (ToType.isNull())
  1904. return TemplateArgument();
  1905. return TemplateArgument(ToType, /*isNullPtr*/true);
  1906. }
  1907. case TemplateArgument::Template: {
  1908. TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
  1909. if (ToTemplate.isNull())
  1910. return TemplateArgument();
  1911. return TemplateArgument(ToTemplate);
  1912. }
  1913. case TemplateArgument::TemplateExpansion: {
  1914. TemplateName ToTemplate
  1915. = Importer.Import(From.getAsTemplateOrTemplatePattern());
  1916. if (ToTemplate.isNull())
  1917. return TemplateArgument();
  1918. return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
  1919. }
  1920. case TemplateArgument::Expression:
  1921. if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
  1922. return TemplateArgument(ToExpr);
  1923. return TemplateArgument();
  1924. case TemplateArgument::Pack: {
  1925. SmallVector<TemplateArgument, 2> ToPack;
  1926. ToPack.reserve(From.pack_size());
  1927. if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
  1928. return TemplateArgument();
  1929. TemplateArgument *ToArgs
  1930. = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
  1931. std::copy(ToPack.begin(), ToPack.end(), ToArgs);
  1932. return TemplateArgument(ToArgs, ToPack.size());
  1933. }
  1934. }
  1935. llvm_unreachable("Invalid template argument kind");
  1936. }
  1937. bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
  1938. unsigned NumFromArgs,
  1939. SmallVectorImpl<TemplateArgument> &ToArgs) {
  1940. for (unsigned I = 0; I != NumFromArgs; ++I) {
  1941. TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
  1942. if (To.isNull() && !FromArgs[I].isNull())
  1943. return true;
  1944. ToArgs.push_back(To);
  1945. }
  1946. return false;
  1947. }
  1948. bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
  1949. RecordDecl *ToRecord, bool Complain) {
  1950. // Eliminate a potential failure point where we attempt to re-import
  1951. // something we're trying to import while completing ToRecord.
  1952. Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
  1953. if (ToOrigin) {
  1954. RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
  1955. if (ToOriginRecord)
  1956. ToRecord = ToOriginRecord;
  1957. }
  1958. StructuralEquivalenceContext Ctx(Importer.getFromContext(),
  1959. ToRecord->getASTContext(),
  1960. Importer.getNonEquivalentDecls(),
  1961. false, Complain);
  1962. return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
  1963. }
  1964. bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
  1965. bool Complain) {
  1966. StructuralEquivalenceContext Ctx(
  1967. Importer.getFromContext(), Importer.getToContext(),
  1968. Importer.getNonEquivalentDecls(), false, Complain);
  1969. return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
  1970. }
  1971. bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
  1972. StructuralEquivalenceContext Ctx(Importer.getFromContext(),
  1973. Importer.getToContext(),
  1974. Importer.getNonEquivalentDecls());
  1975. return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
  1976. }
  1977. bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
  1978. EnumConstantDecl *ToEC)
  1979. {
  1980. const llvm::APSInt &FromVal = FromEC->getInitVal();
  1981. const llvm::APSInt &ToVal = ToEC->getInitVal();
  1982. return FromVal.isSigned() == ToVal.isSigned() &&
  1983. FromVal.getBitWidth() == ToVal.getBitWidth() &&
  1984. FromVal == ToVal;
  1985. }
  1986. bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
  1987. ClassTemplateDecl *To) {
  1988. StructuralEquivalenceContext Ctx(Importer.getFromContext(),
  1989. Importer.getToContext(),
  1990. Importer.getNonEquivalentDecls());
  1991. return Ctx.IsStructurallyEquivalent(From, To);
  1992. }
  1993. bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
  1994. VarTemplateDecl *To) {
  1995. StructuralEquivalenceContext Ctx(Importer.getFromContext(),
  1996. Importer.getToContext(),
  1997. Importer.getNonEquivalentDecls());
  1998. return Ctx.IsStructurallyEquivalent(From, To);
  1999. }
  2000. Decl *ASTNodeImporter::VisitDecl(Decl *D) {
  2001. Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
  2002. << D->getDeclKindName();
  2003. return nullptr;
  2004. }
  2005. Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
  2006. TranslationUnitDecl *ToD =
  2007. Importer.getToContext().getTranslationUnitDecl();
  2008. Importer.Imported(D, ToD);
  2009. return ToD;
  2010. }
  2011. Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
  2012. // Import the major distinguishing characteristics of this namespace.
  2013. DeclContext *DC, *LexicalDC;
  2014. DeclarationName Name;
  2015. SourceLocation Loc;
  2016. NamedDecl *ToD;
  2017. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2018. return nullptr;
  2019. if (ToD)
  2020. return ToD;
  2021. NamespaceDecl *MergeWithNamespace = nullptr;
  2022. if (!Name) {
  2023. // This is an anonymous namespace. Adopt an existing anonymous
  2024. // namespace if we can.
  2025. // FIXME: Not testable.
  2026. if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
  2027. MergeWithNamespace = TU->getAnonymousNamespace();
  2028. else
  2029. MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
  2030. } else {
  2031. SmallVector<NamedDecl *, 4> ConflictingDecls;
  2032. SmallVector<NamedDecl *, 2> FoundDecls;
  2033. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2034. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2035. if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
  2036. continue;
  2037. if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
  2038. MergeWithNamespace = FoundNS;
  2039. ConflictingDecls.clear();
  2040. break;
  2041. }
  2042. ConflictingDecls.push_back(FoundDecls[I]);
  2043. }
  2044. if (!ConflictingDecls.empty()) {
  2045. Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
  2046. ConflictingDecls.data(),
  2047. ConflictingDecls.size());
  2048. }
  2049. }
  2050. // Create the "to" namespace, if needed.
  2051. NamespaceDecl *ToNamespace = MergeWithNamespace;
  2052. if (!ToNamespace) {
  2053. ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
  2054. D->isInline(),
  2055. Importer.Import(D->getLocStart()),
  2056. Loc, Name.getAsIdentifierInfo(),
  2057. /*PrevDecl=*/nullptr);
  2058. ToNamespace->setLexicalDeclContext(LexicalDC);
  2059. LexicalDC->addDeclInternal(ToNamespace);
  2060. // If this is an anonymous namespace, register it as the anonymous
  2061. // namespace within its context.
  2062. if (!Name) {
  2063. if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
  2064. TU->setAnonymousNamespace(ToNamespace);
  2065. else
  2066. cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
  2067. }
  2068. }
  2069. Importer.Imported(D, ToNamespace);
  2070. ImportDeclContext(D);
  2071. return ToNamespace;
  2072. }
  2073. Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
  2074. // Import the major distinguishing characteristics of this typedef.
  2075. DeclContext *DC, *LexicalDC;
  2076. DeclarationName Name;
  2077. SourceLocation Loc;
  2078. NamedDecl *ToD;
  2079. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2080. return nullptr;
  2081. if (ToD)
  2082. return ToD;
  2083. // If this typedef is not in block scope, determine whether we've
  2084. // seen a typedef with the same name (that we can merge with) or any
  2085. // other entity by that name (which name lookup could conflict with).
  2086. if (!DC->isFunctionOrMethod()) {
  2087. SmallVector<NamedDecl *, 4> ConflictingDecls;
  2088. unsigned IDNS = Decl::IDNS_Ordinary;
  2089. SmallVector<NamedDecl *, 2> FoundDecls;
  2090. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2091. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2092. if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
  2093. continue;
  2094. if (TypedefNameDecl *FoundTypedef =
  2095. dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
  2096. if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
  2097. FoundTypedef->getUnderlyingType()))
  2098. return Importer.Imported(D, FoundTypedef);
  2099. }
  2100. ConflictingDecls.push_back(FoundDecls[I]);
  2101. }
  2102. if (!ConflictingDecls.empty()) {
  2103. Name = Importer.HandleNameConflict(Name, DC, IDNS,
  2104. ConflictingDecls.data(),
  2105. ConflictingDecls.size());
  2106. if (!Name)
  2107. return nullptr;
  2108. }
  2109. }
  2110. // Import the underlying type of this typedef;
  2111. QualType T = Importer.Import(D->getUnderlyingType());
  2112. if (T.isNull())
  2113. return nullptr;
  2114. // Create the new typedef node.
  2115. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  2116. SourceLocation StartL = Importer.Import(D->getLocStart());
  2117. TypedefNameDecl *ToTypedef;
  2118. if (IsAlias)
  2119. ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
  2120. StartL, Loc,
  2121. Name.getAsIdentifierInfo(),
  2122. TInfo);
  2123. else
  2124. ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
  2125. StartL, Loc,
  2126. Name.getAsIdentifierInfo(),
  2127. TInfo);
  2128. ToTypedef->setAccess(D->getAccess());
  2129. ToTypedef->setLexicalDeclContext(LexicalDC);
  2130. Importer.Imported(D, ToTypedef);
  2131. LexicalDC->addDeclInternal(ToTypedef);
  2132. return ToTypedef;
  2133. }
  2134. Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
  2135. return VisitTypedefNameDecl(D, /*IsAlias=*/false);
  2136. }
  2137. Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
  2138. return VisitTypedefNameDecl(D, /*IsAlias=*/true);
  2139. }
  2140. Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
  2141. // Import the major distinguishing characteristics of this enum.
  2142. DeclContext *DC, *LexicalDC;
  2143. DeclarationName Name;
  2144. SourceLocation Loc;
  2145. NamedDecl *ToD;
  2146. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2147. return nullptr;
  2148. if (ToD)
  2149. return ToD;
  2150. // Figure out what enum name we're looking for.
  2151. unsigned IDNS = Decl::IDNS_Tag;
  2152. DeclarationName SearchName = Name;
  2153. if (!SearchName && D->getTypedefNameForAnonDecl()) {
  2154. SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
  2155. IDNS = Decl::IDNS_Ordinary;
  2156. } else if (Importer.getToContext().getLangOpts().CPlusPlus)
  2157. IDNS |= Decl::IDNS_Ordinary;
  2158. // We may already have an enum of the same name; try to find and match it.
  2159. if (!DC->isFunctionOrMethod() && SearchName) {
  2160. SmallVector<NamedDecl *, 4> ConflictingDecls;
  2161. SmallVector<NamedDecl *, 2> FoundDecls;
  2162. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2163. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2164. if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
  2165. continue;
  2166. Decl *Found = FoundDecls[I];
  2167. if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
  2168. if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
  2169. Found = Tag->getDecl();
  2170. }
  2171. if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
  2172. if (IsStructuralMatch(D, FoundEnum))
  2173. return Importer.Imported(D, FoundEnum);
  2174. }
  2175. ConflictingDecls.push_back(FoundDecls[I]);
  2176. }
  2177. if (!ConflictingDecls.empty()) {
  2178. Name = Importer.HandleNameConflict(Name, DC, IDNS,
  2179. ConflictingDecls.data(),
  2180. ConflictingDecls.size());
  2181. }
  2182. }
  2183. // Create the enum declaration.
  2184. EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
  2185. Importer.Import(D->getLocStart()),
  2186. Loc, Name.getAsIdentifierInfo(), nullptr,
  2187. D->isScoped(), D->isScopedUsingClassTag(),
  2188. D->isFixed());
  2189. // Import the qualifier, if any.
  2190. D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
  2191. D2->setAccess(D->getAccess());
  2192. D2->setLexicalDeclContext(LexicalDC);
  2193. Importer.Imported(D, D2);
  2194. LexicalDC->addDeclInternal(D2);
  2195. // Import the integer type.
  2196. QualType ToIntegerType = Importer.Import(D->getIntegerType());
  2197. if (ToIntegerType.isNull())
  2198. return nullptr;
  2199. D2->setIntegerType(ToIntegerType);
  2200. // Import the definition
  2201. if (D->isCompleteDefinition() && ImportDefinition(D, D2))
  2202. return nullptr;
  2203. return D2;
  2204. }
  2205. Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
  2206. // If this record has a definition in the translation unit we're coming from,
  2207. // but this particular declaration is not that definition, import the
  2208. // definition and map to that.
  2209. TagDecl *Definition = D->getDefinition();
  2210. if (Definition && Definition != D) {
  2211. Decl *ImportedDef = Importer.Import(Definition);
  2212. if (!ImportedDef)
  2213. return nullptr;
  2214. return Importer.Imported(D, ImportedDef);
  2215. }
  2216. // Import the major distinguishing characteristics of this record.
  2217. DeclContext *DC, *LexicalDC;
  2218. DeclarationName Name;
  2219. SourceLocation Loc;
  2220. NamedDecl *ToD;
  2221. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2222. return nullptr;
  2223. if (ToD)
  2224. return ToD;
  2225. // Figure out what structure name we're looking for.
  2226. unsigned IDNS = Decl::IDNS_Tag;
  2227. DeclarationName SearchName = Name;
  2228. if (!SearchName && D->getTypedefNameForAnonDecl()) {
  2229. SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
  2230. IDNS = Decl::IDNS_Ordinary;
  2231. } else if (Importer.getToContext().getLangOpts().CPlusPlus)
  2232. IDNS |= Decl::IDNS_Ordinary;
  2233. // We may already have a record of the same name; try to find and match it.
  2234. RecordDecl *AdoptDecl = nullptr;
  2235. if (!DC->isFunctionOrMethod()) {
  2236. SmallVector<NamedDecl *, 4> ConflictingDecls;
  2237. SmallVector<NamedDecl *, 2> FoundDecls;
  2238. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2239. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2240. if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
  2241. continue;
  2242. Decl *Found = FoundDecls[I];
  2243. if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
  2244. if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
  2245. Found = Tag->getDecl();
  2246. }
  2247. if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
  2248. if (D->isAnonymousStructOrUnion() &&
  2249. FoundRecord->isAnonymousStructOrUnion()) {
  2250. // If both anonymous structs/unions are in a record context, make sure
  2251. // they occur in the same location in the context records.
  2252. if (Optional<unsigned> Index1
  2253. = findAnonymousStructOrUnionIndex(D)) {
  2254. if (Optional<unsigned> Index2 =
  2255. findAnonymousStructOrUnionIndex(FoundRecord)) {
  2256. if (*Index1 != *Index2)
  2257. continue;
  2258. }
  2259. }
  2260. }
  2261. if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
  2262. if ((SearchName && !D->isCompleteDefinition())
  2263. || (D->isCompleteDefinition() &&
  2264. D->isAnonymousStructOrUnion()
  2265. == FoundDef->isAnonymousStructOrUnion() &&
  2266. IsStructuralMatch(D, FoundDef))) {
  2267. // The record types structurally match, or the "from" translation
  2268. // unit only had a forward declaration anyway; call it the same
  2269. // function.
  2270. // FIXME: For C++, we should also merge methods here.
  2271. return Importer.Imported(D, FoundDef);
  2272. }
  2273. } else if (!D->isCompleteDefinition()) {
  2274. // We have a forward declaration of this type, so adopt that forward
  2275. // declaration rather than building a new one.
  2276. // If one or both can be completed from external storage then try one
  2277. // last time to complete and compare them before doing this.
  2278. if (FoundRecord->hasExternalLexicalStorage() &&
  2279. !FoundRecord->isCompleteDefinition())
  2280. FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
  2281. if (D->hasExternalLexicalStorage())
  2282. D->getASTContext().getExternalSource()->CompleteType(D);
  2283. if (FoundRecord->isCompleteDefinition() &&
  2284. D->isCompleteDefinition() &&
  2285. !IsStructuralMatch(D, FoundRecord))
  2286. continue;
  2287. AdoptDecl = FoundRecord;
  2288. continue;
  2289. } else if (!SearchName) {
  2290. continue;
  2291. }
  2292. }
  2293. ConflictingDecls.push_back(FoundDecls[I]);
  2294. }
  2295. if (!ConflictingDecls.empty() && SearchName) {
  2296. Name = Importer.HandleNameConflict(Name, DC, IDNS,
  2297. ConflictingDecls.data(),
  2298. ConflictingDecls.size());
  2299. }
  2300. }
  2301. // Create the record declaration.
  2302. RecordDecl *D2 = AdoptDecl;
  2303. SourceLocation StartLoc = Importer.Import(D->getLocStart());
  2304. if (!D2) {
  2305. if (isa<CXXRecordDecl>(D)) {
  2306. CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
  2307. D->getTagKind(),
  2308. DC, StartLoc, Loc,
  2309. Name.getAsIdentifierInfo());
  2310. D2 = D2CXX;
  2311. D2->setAccess(D->getAccess());
  2312. } else {
  2313. D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
  2314. DC, StartLoc, Loc, Name.getAsIdentifierInfo());
  2315. }
  2316. D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
  2317. D2->setLexicalDeclContext(LexicalDC);
  2318. LexicalDC->addDeclInternal(D2);
  2319. if (D->isAnonymousStructOrUnion())
  2320. D2->setAnonymousStructOrUnion(true);
  2321. }
  2322. Importer.Imported(D, D2);
  2323. if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
  2324. return nullptr;
  2325. return D2;
  2326. }
  2327. Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
  2328. // Import the major distinguishing characteristics of this enumerator.
  2329. DeclContext *DC, *LexicalDC;
  2330. DeclarationName Name;
  2331. SourceLocation Loc;
  2332. NamedDecl *ToD;
  2333. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2334. return nullptr;
  2335. if (ToD)
  2336. return ToD;
  2337. QualType T = Importer.Import(D->getType());
  2338. if (T.isNull())
  2339. return nullptr;
  2340. // Determine whether there are any other declarations with the same name and
  2341. // in the same context.
  2342. if (!LexicalDC->isFunctionOrMethod()) {
  2343. SmallVector<NamedDecl *, 4> ConflictingDecls;
  2344. unsigned IDNS = Decl::IDNS_Ordinary;
  2345. SmallVector<NamedDecl *, 2> FoundDecls;
  2346. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2347. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2348. if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
  2349. continue;
  2350. if (EnumConstantDecl *FoundEnumConstant
  2351. = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
  2352. if (IsStructuralMatch(D, FoundEnumConstant))
  2353. return Importer.Imported(D, FoundEnumConstant);
  2354. }
  2355. ConflictingDecls.push_back(FoundDecls[I]);
  2356. }
  2357. if (!ConflictingDecls.empty()) {
  2358. Name = Importer.HandleNameConflict(Name, DC, IDNS,
  2359. ConflictingDecls.data(),
  2360. ConflictingDecls.size());
  2361. if (!Name)
  2362. return nullptr;
  2363. }
  2364. }
  2365. Expr *Init = Importer.Import(D->getInitExpr());
  2366. if (D->getInitExpr() && !Init)
  2367. return nullptr;
  2368. EnumConstantDecl *ToEnumerator
  2369. = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
  2370. Name.getAsIdentifierInfo(), T,
  2371. Init, D->getInitVal());
  2372. ToEnumerator->setAccess(D->getAccess());
  2373. ToEnumerator->setLexicalDeclContext(LexicalDC);
  2374. Importer.Imported(D, ToEnumerator);
  2375. LexicalDC->addDeclInternal(ToEnumerator);
  2376. return ToEnumerator;
  2377. }
  2378. Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
  2379. // Import the major distinguishing characteristics of this function.
  2380. DeclContext *DC, *LexicalDC;
  2381. DeclarationName Name;
  2382. SourceLocation Loc;
  2383. NamedDecl *ToD;
  2384. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2385. return nullptr;
  2386. if (ToD)
  2387. return ToD;
  2388. // Try to find a function in our own ("to") context with the same name, same
  2389. // type, and in the same context as the function we're importing.
  2390. if (!LexicalDC->isFunctionOrMethod()) {
  2391. SmallVector<NamedDecl *, 4> ConflictingDecls;
  2392. unsigned IDNS = Decl::IDNS_Ordinary;
  2393. SmallVector<NamedDecl *, 2> FoundDecls;
  2394. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2395. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2396. if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
  2397. continue;
  2398. if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
  2399. if (FoundFunction->hasExternalFormalLinkage() &&
  2400. D->hasExternalFormalLinkage()) {
  2401. if (Importer.IsStructurallyEquivalent(D->getType(),
  2402. FoundFunction->getType())) {
  2403. // FIXME: Actually try to merge the body and other attributes.
  2404. return Importer.Imported(D, FoundFunction);
  2405. }
  2406. // FIXME: Check for overloading more carefully, e.g., by boosting
  2407. // Sema::IsOverload out to the AST library.
  2408. // Function overloading is okay in C++.
  2409. if (Importer.getToContext().getLangOpts().CPlusPlus)
  2410. continue;
  2411. // Complain about inconsistent function types.
  2412. Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
  2413. << Name << D->getType() << FoundFunction->getType();
  2414. Importer.ToDiag(FoundFunction->getLocation(),
  2415. diag::note_odr_value_here)
  2416. << FoundFunction->getType();
  2417. }
  2418. }
  2419. ConflictingDecls.push_back(FoundDecls[I]);
  2420. }
  2421. if (!ConflictingDecls.empty()) {
  2422. Name = Importer.HandleNameConflict(Name, DC, IDNS,
  2423. ConflictingDecls.data(),
  2424. ConflictingDecls.size());
  2425. if (!Name)
  2426. return nullptr;
  2427. }
  2428. }
  2429. DeclarationNameInfo NameInfo(Name, Loc);
  2430. // Import additional name location/type info.
  2431. ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
  2432. QualType FromTy = D->getType();
  2433. bool usedDifferentExceptionSpec = false;
  2434. if (const FunctionProtoType *
  2435. FromFPT = D->getType()->getAs<FunctionProtoType>()) {
  2436. FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
  2437. // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
  2438. // FunctionDecl that we are importing the FunctionProtoType for.
  2439. // To avoid an infinite recursion when importing, create the FunctionDecl
  2440. // with a simplified function type and update it afterwards.
  2441. if (FromEPI.ExceptionSpec.SourceDecl ||
  2442. FromEPI.ExceptionSpec.SourceTemplate ||
  2443. FromEPI.ExceptionSpec.NoexceptExpr) {
  2444. FunctionProtoType::ExtProtoInfo DefaultEPI;
  2445. FromTy = Importer.getFromContext().getFunctionType(
  2446. FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI, None); // HLSL Change - add param modifiers
  2447. usedDifferentExceptionSpec = true;
  2448. }
  2449. }
  2450. // Import the type.
  2451. QualType T = Importer.Import(FromTy);
  2452. if (T.isNull())
  2453. return nullptr;
  2454. // Import the function parameters.
  2455. SmallVector<ParmVarDecl *, 8> Parameters;
  2456. for (auto P : D->params()) {
  2457. ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
  2458. if (!ToP)
  2459. return nullptr;
  2460. Parameters.push_back(ToP);
  2461. }
  2462. // Create the imported function.
  2463. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  2464. FunctionDecl *ToFunction = nullptr;
  2465. SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
  2466. if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
  2467. ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
  2468. cast<CXXRecordDecl>(DC),
  2469. InnerLocStart,
  2470. NameInfo, T, TInfo,
  2471. FromConstructor->isExplicit(),
  2472. D->isInlineSpecified(),
  2473. D->isImplicit(),
  2474. D->isConstexpr());
  2475. } else if (isa<CXXDestructorDecl>(D)) {
  2476. ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
  2477. cast<CXXRecordDecl>(DC),
  2478. InnerLocStart,
  2479. NameInfo, T, TInfo,
  2480. D->isInlineSpecified(),
  2481. D->isImplicit());
  2482. } else if (CXXConversionDecl *FromConversion
  2483. = dyn_cast<CXXConversionDecl>(D)) {
  2484. ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
  2485. cast<CXXRecordDecl>(DC),
  2486. InnerLocStart,
  2487. NameInfo, T, TInfo,
  2488. D->isInlineSpecified(),
  2489. FromConversion->isExplicit(),
  2490. D->isConstexpr(),
  2491. Importer.Import(D->getLocEnd()));
  2492. } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
  2493. ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
  2494. cast<CXXRecordDecl>(DC),
  2495. InnerLocStart,
  2496. NameInfo, T, TInfo,
  2497. Method->getStorageClass(),
  2498. Method->isInlineSpecified(),
  2499. D->isConstexpr(),
  2500. Importer.Import(D->getLocEnd()));
  2501. } else {
  2502. ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
  2503. InnerLocStart,
  2504. NameInfo, T, TInfo, D->getStorageClass(),
  2505. D->isInlineSpecified(),
  2506. D->hasWrittenPrototype(),
  2507. D->isConstexpr());
  2508. }
  2509. // Import the qualifier, if any.
  2510. ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
  2511. ToFunction->setAccess(D->getAccess());
  2512. ToFunction->setLexicalDeclContext(LexicalDC);
  2513. ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
  2514. ToFunction->setTrivial(D->isTrivial());
  2515. ToFunction->setPure(D->isPure());
  2516. Importer.Imported(D, ToFunction);
  2517. // Set the parameters.
  2518. for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
  2519. Parameters[I]->setOwningFunction(ToFunction);
  2520. ToFunction->addDeclInternal(Parameters[I]);
  2521. }
  2522. ToFunction->setParams(Parameters);
  2523. if (usedDifferentExceptionSpec) {
  2524. // Update FunctionProtoType::ExtProtoInfo.
  2525. QualType T = Importer.Import(D->getType());
  2526. if (T.isNull())
  2527. return nullptr;
  2528. ToFunction->setType(T);
  2529. }
  2530. // Import the body, if any.
  2531. if (Stmt *FromBody = D->getBody()) {
  2532. if (Stmt *ToBody = Importer.Import(FromBody)) {
  2533. ToFunction->setBody(ToBody);
  2534. }
  2535. }
  2536. // FIXME: Other bits to merge?
  2537. // Add this function to the lexical context.
  2538. LexicalDC->addDeclInternal(ToFunction);
  2539. return ToFunction;
  2540. }
  2541. Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
  2542. return VisitFunctionDecl(D);
  2543. }
  2544. Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
  2545. return VisitCXXMethodDecl(D);
  2546. }
  2547. Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
  2548. return VisitCXXMethodDecl(D);
  2549. }
  2550. Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
  2551. return VisitCXXMethodDecl(D);
  2552. }
  2553. static unsigned getFieldIndex(Decl *F) {
  2554. RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
  2555. if (!Owner)
  2556. return 0;
  2557. unsigned Index = 1;
  2558. for (const auto *D : Owner->noload_decls()) {
  2559. if (D == F)
  2560. return Index;
  2561. if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
  2562. ++Index;
  2563. }
  2564. return Index;
  2565. }
  2566. Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
  2567. // Import the major distinguishing characteristics of a variable.
  2568. DeclContext *DC, *LexicalDC;
  2569. DeclarationName Name;
  2570. SourceLocation Loc;
  2571. NamedDecl *ToD;
  2572. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2573. return nullptr;
  2574. if (ToD)
  2575. return ToD;
  2576. // Determine whether we've already imported this field.
  2577. SmallVector<NamedDecl *, 2> FoundDecls;
  2578. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2579. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2580. if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
  2581. // For anonymous fields, match up by index.
  2582. if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
  2583. continue;
  2584. if (Importer.IsStructurallyEquivalent(D->getType(),
  2585. FoundField->getType())) {
  2586. Importer.Imported(D, FoundField);
  2587. return FoundField;
  2588. }
  2589. Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
  2590. << Name << D->getType() << FoundField->getType();
  2591. Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
  2592. << FoundField->getType();
  2593. return nullptr;
  2594. }
  2595. }
  2596. // Import the type.
  2597. QualType T = Importer.Import(D->getType());
  2598. if (T.isNull())
  2599. return nullptr;
  2600. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  2601. Expr *BitWidth = Importer.Import(D->getBitWidth());
  2602. if (!BitWidth && D->getBitWidth())
  2603. return nullptr;
  2604. FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
  2605. Importer.Import(D->getInnerLocStart()),
  2606. Loc, Name.getAsIdentifierInfo(),
  2607. T, TInfo, BitWidth, D->isMutable(),
  2608. D->getInClassInitStyle());
  2609. ToField->setAccess(D->getAccess());
  2610. ToField->setLexicalDeclContext(LexicalDC);
  2611. if (ToField->hasInClassInitializer())
  2612. ToField->setInClassInitializer(D->getInClassInitializer());
  2613. ToField->setImplicit(D->isImplicit());
  2614. Importer.Imported(D, ToField);
  2615. LexicalDC->addDeclInternal(ToField);
  2616. return ToField;
  2617. }
  2618. Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
  2619. // Import the major distinguishing characteristics of a variable.
  2620. DeclContext *DC, *LexicalDC;
  2621. DeclarationName Name;
  2622. SourceLocation Loc;
  2623. NamedDecl *ToD;
  2624. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2625. return nullptr;
  2626. if (ToD)
  2627. return ToD;
  2628. // Determine whether we've already imported this field.
  2629. SmallVector<NamedDecl *, 2> FoundDecls;
  2630. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2631. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2632. if (IndirectFieldDecl *FoundField
  2633. = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
  2634. // For anonymous indirect fields, match up by index.
  2635. if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
  2636. continue;
  2637. if (Importer.IsStructurallyEquivalent(D->getType(),
  2638. FoundField->getType(),
  2639. !Name.isEmpty())) {
  2640. Importer.Imported(D, FoundField);
  2641. return FoundField;
  2642. }
  2643. // If there are more anonymous fields to check, continue.
  2644. if (!Name && I < N-1)
  2645. continue;
  2646. Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
  2647. << Name << D->getType() << FoundField->getType();
  2648. Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
  2649. << FoundField->getType();
  2650. return nullptr;
  2651. }
  2652. }
  2653. // Import the type.
  2654. QualType T = Importer.Import(D->getType());
  2655. if (T.isNull())
  2656. return nullptr;
  2657. NamedDecl **NamedChain =
  2658. new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
  2659. unsigned i = 0;
  2660. for (auto *PI : D->chain()) {
  2661. Decl *D = Importer.Import(PI);
  2662. if (!D)
  2663. return nullptr;
  2664. NamedChain[i++] = cast<NamedDecl>(D);
  2665. }
  2666. IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
  2667. Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
  2668. NamedChain, D->getChainingSize());
  2669. for (const auto *Attr : D->attrs())
  2670. ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
  2671. ToIndirectField->setAccess(D->getAccess());
  2672. ToIndirectField->setLexicalDeclContext(LexicalDC);
  2673. Importer.Imported(D, ToIndirectField);
  2674. LexicalDC->addDeclInternal(ToIndirectField);
  2675. return ToIndirectField;
  2676. }
  2677. Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
  2678. // Import the major distinguishing characteristics of an ivar.
  2679. DeclContext *DC, *LexicalDC;
  2680. DeclarationName Name;
  2681. SourceLocation Loc;
  2682. NamedDecl *ToD;
  2683. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2684. return nullptr;
  2685. if (ToD)
  2686. return ToD;
  2687. // Determine whether we've already imported this ivar
  2688. SmallVector<NamedDecl *, 2> FoundDecls;
  2689. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2690. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2691. if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
  2692. if (Importer.IsStructurallyEquivalent(D->getType(),
  2693. FoundIvar->getType())) {
  2694. Importer.Imported(D, FoundIvar);
  2695. return FoundIvar;
  2696. }
  2697. Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
  2698. << Name << D->getType() << FoundIvar->getType();
  2699. Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
  2700. << FoundIvar->getType();
  2701. return nullptr;
  2702. }
  2703. }
  2704. // Import the type.
  2705. QualType T = Importer.Import(D->getType());
  2706. if (T.isNull())
  2707. return nullptr;
  2708. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  2709. Expr *BitWidth = Importer.Import(D->getBitWidth());
  2710. if (!BitWidth && D->getBitWidth())
  2711. return nullptr;
  2712. ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
  2713. cast<ObjCContainerDecl>(DC),
  2714. Importer.Import(D->getInnerLocStart()),
  2715. Loc, Name.getAsIdentifierInfo(),
  2716. T, TInfo, D->getAccessControl(),
  2717. BitWidth, D->getSynthesize());
  2718. ToIvar->setLexicalDeclContext(LexicalDC);
  2719. Importer.Imported(D, ToIvar);
  2720. LexicalDC->addDeclInternal(ToIvar);
  2721. return ToIvar;
  2722. }
  2723. Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
  2724. // Import the major distinguishing characteristics of a variable.
  2725. DeclContext *DC, *LexicalDC;
  2726. DeclarationName Name;
  2727. SourceLocation Loc;
  2728. NamedDecl *ToD;
  2729. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2730. return nullptr;
  2731. if (ToD)
  2732. return ToD;
  2733. // Try to find a variable in our own ("to") context with the same name and
  2734. // in the same context as the variable we're importing.
  2735. if (D->isFileVarDecl()) {
  2736. VarDecl *MergeWithVar = nullptr;
  2737. SmallVector<NamedDecl *, 4> ConflictingDecls;
  2738. unsigned IDNS = Decl::IDNS_Ordinary;
  2739. SmallVector<NamedDecl *, 2> FoundDecls;
  2740. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2741. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2742. if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
  2743. continue;
  2744. if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
  2745. // We have found a variable that we may need to merge with. Check it.
  2746. if (FoundVar->hasExternalFormalLinkage() &&
  2747. D->hasExternalFormalLinkage()) {
  2748. if (Importer.IsStructurallyEquivalent(D->getType(),
  2749. FoundVar->getType())) {
  2750. MergeWithVar = FoundVar;
  2751. break;
  2752. }
  2753. const ArrayType *FoundArray
  2754. = Importer.getToContext().getAsArrayType(FoundVar->getType());
  2755. const ArrayType *TArray
  2756. = Importer.getToContext().getAsArrayType(D->getType());
  2757. if (FoundArray && TArray) {
  2758. if (isa<IncompleteArrayType>(FoundArray) &&
  2759. isa<ConstantArrayType>(TArray)) {
  2760. // Import the type.
  2761. QualType T = Importer.Import(D->getType());
  2762. if (T.isNull())
  2763. return nullptr;
  2764. FoundVar->setType(T);
  2765. MergeWithVar = FoundVar;
  2766. break;
  2767. } else if (isa<IncompleteArrayType>(TArray) &&
  2768. isa<ConstantArrayType>(FoundArray)) {
  2769. MergeWithVar = FoundVar;
  2770. break;
  2771. }
  2772. }
  2773. Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
  2774. << Name << D->getType() << FoundVar->getType();
  2775. Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
  2776. << FoundVar->getType();
  2777. }
  2778. }
  2779. ConflictingDecls.push_back(FoundDecls[I]);
  2780. }
  2781. if (MergeWithVar) {
  2782. // An equivalent variable with external linkage has been found. Link
  2783. // the two declarations, then merge them.
  2784. Importer.Imported(D, MergeWithVar);
  2785. if (VarDecl *DDef = D->getDefinition()) {
  2786. if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
  2787. Importer.ToDiag(ExistingDef->getLocation(),
  2788. diag::err_odr_variable_multiple_def)
  2789. << Name;
  2790. Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
  2791. } else {
  2792. Expr *Init = Importer.Import(DDef->getInit());
  2793. MergeWithVar->setInit(Init);
  2794. if (DDef->isInitKnownICE()) {
  2795. EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
  2796. Eval->CheckedICE = true;
  2797. Eval->IsICE = DDef->isInitICE();
  2798. }
  2799. }
  2800. }
  2801. return MergeWithVar;
  2802. }
  2803. if (!ConflictingDecls.empty()) {
  2804. Name = Importer.HandleNameConflict(Name, DC, IDNS,
  2805. ConflictingDecls.data(),
  2806. ConflictingDecls.size());
  2807. if (!Name)
  2808. return nullptr;
  2809. }
  2810. }
  2811. // Import the type.
  2812. QualType T = Importer.Import(D->getType());
  2813. if (T.isNull())
  2814. return nullptr;
  2815. // Create the imported variable.
  2816. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  2817. VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
  2818. Importer.Import(D->getInnerLocStart()),
  2819. Loc, Name.getAsIdentifierInfo(),
  2820. T, TInfo,
  2821. D->getStorageClass());
  2822. ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
  2823. ToVar->setAccess(D->getAccess());
  2824. ToVar->setLexicalDeclContext(LexicalDC);
  2825. Importer.Imported(D, ToVar);
  2826. LexicalDC->addDeclInternal(ToVar);
  2827. if (!D->isFileVarDecl() &&
  2828. D->isUsed())
  2829. ToVar->setIsUsed();
  2830. // Merge the initializer.
  2831. if (ImportDefinition(D, ToVar))
  2832. return nullptr;
  2833. return ToVar;
  2834. }
  2835. Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
  2836. // Parameters are created in the translation unit's context, then moved
  2837. // into the function declaration's context afterward.
  2838. DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
  2839. // Import the name of this declaration.
  2840. DeclarationName Name = Importer.Import(D->getDeclName());
  2841. if (D->getDeclName() && !Name)
  2842. return nullptr;
  2843. // Import the location of this declaration.
  2844. SourceLocation Loc = Importer.Import(D->getLocation());
  2845. // Import the parameter's type.
  2846. QualType T = Importer.Import(D->getType());
  2847. if (T.isNull())
  2848. return nullptr;
  2849. // Create the imported parameter.
  2850. ImplicitParamDecl *ToParm
  2851. = ImplicitParamDecl::Create(Importer.getToContext(), DC,
  2852. Loc, Name.getAsIdentifierInfo(),
  2853. T);
  2854. return Importer.Imported(D, ToParm);
  2855. }
  2856. Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
  2857. // Parameters are created in the translation unit's context, then moved
  2858. // into the function declaration's context afterward.
  2859. DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
  2860. // Import the name of this declaration.
  2861. DeclarationName Name = Importer.Import(D->getDeclName());
  2862. if (D->getDeclName() && !Name)
  2863. return nullptr;
  2864. // Import the location of this declaration.
  2865. SourceLocation Loc = Importer.Import(D->getLocation());
  2866. // Import the parameter's type.
  2867. QualType T = Importer.Import(D->getType());
  2868. if (T.isNull())
  2869. return nullptr;
  2870. // Create the imported parameter.
  2871. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  2872. ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
  2873. Importer.Import(D->getInnerLocStart()),
  2874. Loc, Name.getAsIdentifierInfo(),
  2875. T, TInfo, D->getStorageClass(),
  2876. /*FIXME: Default argument*/nullptr);
  2877. ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
  2878. if (D->isUsed())
  2879. ToParm->setIsUsed();
  2880. return Importer.Imported(D, ToParm);
  2881. }
  2882. Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
  2883. // Import the major distinguishing characteristics of a method.
  2884. DeclContext *DC, *LexicalDC;
  2885. DeclarationName Name;
  2886. SourceLocation Loc;
  2887. NamedDecl *ToD;
  2888. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2889. return nullptr;
  2890. if (ToD)
  2891. return ToD;
  2892. SmallVector<NamedDecl *, 2> FoundDecls;
  2893. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  2894. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  2895. if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
  2896. if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
  2897. continue;
  2898. // Check return types.
  2899. if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
  2900. FoundMethod->getReturnType())) {
  2901. Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
  2902. << D->isInstanceMethod() << Name << D->getReturnType()
  2903. << FoundMethod->getReturnType();
  2904. Importer.ToDiag(FoundMethod->getLocation(),
  2905. diag::note_odr_objc_method_here)
  2906. << D->isInstanceMethod() << Name;
  2907. return nullptr;
  2908. }
  2909. // Check the number of parameters.
  2910. if (D->param_size() != FoundMethod->param_size()) {
  2911. Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
  2912. << D->isInstanceMethod() << Name
  2913. << D->param_size() << FoundMethod->param_size();
  2914. Importer.ToDiag(FoundMethod->getLocation(),
  2915. diag::note_odr_objc_method_here)
  2916. << D->isInstanceMethod() << Name;
  2917. return nullptr;
  2918. }
  2919. // Check parameter types.
  2920. for (ObjCMethodDecl::param_iterator P = D->param_begin(),
  2921. PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
  2922. P != PEnd; ++P, ++FoundP) {
  2923. if (!Importer.IsStructurallyEquivalent((*P)->getType(),
  2924. (*FoundP)->getType())) {
  2925. Importer.FromDiag((*P)->getLocation(),
  2926. diag::err_odr_objc_method_param_type_inconsistent)
  2927. << D->isInstanceMethod() << Name
  2928. << (*P)->getType() << (*FoundP)->getType();
  2929. Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
  2930. << (*FoundP)->getType();
  2931. return nullptr;
  2932. }
  2933. }
  2934. // Check variadic/non-variadic.
  2935. // Check the number of parameters.
  2936. if (D->isVariadic() != FoundMethod->isVariadic()) {
  2937. Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
  2938. << D->isInstanceMethod() << Name;
  2939. Importer.ToDiag(FoundMethod->getLocation(),
  2940. diag::note_odr_objc_method_here)
  2941. << D->isInstanceMethod() << Name;
  2942. return nullptr;
  2943. }
  2944. // FIXME: Any other bits we need to merge?
  2945. return Importer.Imported(D, FoundMethod);
  2946. }
  2947. }
  2948. // Import the result type.
  2949. QualType ResultTy = Importer.Import(D->getReturnType());
  2950. if (ResultTy.isNull())
  2951. return nullptr;
  2952. TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
  2953. ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
  2954. Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
  2955. Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
  2956. D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
  2957. D->getImplementationControl(), D->hasRelatedResultType());
  2958. // FIXME: When we decide to merge method definitions, we'll need to
  2959. // deal with implicit parameters.
  2960. // Import the parameters
  2961. SmallVector<ParmVarDecl *, 5> ToParams;
  2962. for (auto *FromP : D->params()) {
  2963. ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
  2964. if (!ToP)
  2965. return nullptr;
  2966. ToParams.push_back(ToP);
  2967. }
  2968. // Set the parameters.
  2969. for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
  2970. ToParams[I]->setOwningFunction(ToMethod);
  2971. ToMethod->addDeclInternal(ToParams[I]);
  2972. }
  2973. SmallVector<SourceLocation, 12> SelLocs;
  2974. D->getSelectorLocs(SelLocs);
  2975. ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
  2976. ToMethod->setLexicalDeclContext(LexicalDC);
  2977. Importer.Imported(D, ToMethod);
  2978. LexicalDC->addDeclInternal(ToMethod);
  2979. return ToMethod;
  2980. }
  2981. Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
  2982. // Import the major distinguishing characteristics of a category.
  2983. DeclContext *DC, *LexicalDC;
  2984. DeclarationName Name;
  2985. SourceLocation Loc;
  2986. NamedDecl *ToD;
  2987. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  2988. return nullptr;
  2989. if (ToD)
  2990. return ToD;
  2991. TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
  2992. if (!BoundInfo)
  2993. return nullptr;
  2994. ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
  2995. Importer.getToContext(), DC,
  2996. D->getVariance(),
  2997. Importer.Import(D->getVarianceLoc()),
  2998. D->getIndex(),
  2999. Importer.Import(D->getLocation()),
  3000. Name.getAsIdentifierInfo(),
  3001. Importer.Import(D->getColonLoc()),
  3002. BoundInfo);
  3003. Importer.Imported(D, Result);
  3004. Result->setLexicalDeclContext(LexicalDC);
  3005. return Result;
  3006. }
  3007. Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
  3008. // Import the major distinguishing characteristics of a category.
  3009. DeclContext *DC, *LexicalDC;
  3010. DeclarationName Name;
  3011. SourceLocation Loc;
  3012. NamedDecl *ToD;
  3013. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  3014. return nullptr;
  3015. if (ToD)
  3016. return ToD;
  3017. ObjCInterfaceDecl *ToInterface
  3018. = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
  3019. if (!ToInterface)
  3020. return nullptr;
  3021. // Determine if we've already encountered this category.
  3022. ObjCCategoryDecl *MergeWithCategory
  3023. = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
  3024. ObjCCategoryDecl *ToCategory = MergeWithCategory;
  3025. if (!ToCategory) {
  3026. ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
  3027. Importer.Import(D->getAtStartLoc()),
  3028. Loc,
  3029. Importer.Import(D->getCategoryNameLoc()),
  3030. Name.getAsIdentifierInfo(),
  3031. ToInterface,
  3032. /*TypeParamList=*/nullptr,
  3033. Importer.Import(D->getIvarLBraceLoc()),
  3034. Importer.Import(D->getIvarRBraceLoc()));
  3035. ToCategory->setLexicalDeclContext(LexicalDC);
  3036. LexicalDC->addDeclInternal(ToCategory);
  3037. Importer.Imported(D, ToCategory);
  3038. // Import the type parameter list after calling Imported, to avoid
  3039. // loops when bringing in their DeclContext.
  3040. ToCategory->setTypeParamList(ImportObjCTypeParamList(
  3041. D->getTypeParamList()));
  3042. // Import protocols
  3043. SmallVector<ObjCProtocolDecl *, 4> Protocols;
  3044. SmallVector<SourceLocation, 4> ProtocolLocs;
  3045. ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
  3046. = D->protocol_loc_begin();
  3047. for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
  3048. FromProtoEnd = D->protocol_end();
  3049. FromProto != FromProtoEnd;
  3050. ++FromProto, ++FromProtoLoc) {
  3051. ObjCProtocolDecl *ToProto
  3052. = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
  3053. if (!ToProto)
  3054. return nullptr;
  3055. Protocols.push_back(ToProto);
  3056. ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
  3057. }
  3058. // FIXME: If we're merging, make sure that the protocol list is the same.
  3059. ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
  3060. ProtocolLocs.data(), Importer.getToContext());
  3061. } else {
  3062. Importer.Imported(D, ToCategory);
  3063. }
  3064. // Import all of the members of this category.
  3065. ImportDeclContext(D);
  3066. // If we have an implementation, import it as well.
  3067. if (D->getImplementation()) {
  3068. ObjCCategoryImplDecl *Impl
  3069. = cast_or_null<ObjCCategoryImplDecl>(
  3070. Importer.Import(D->getImplementation()));
  3071. if (!Impl)
  3072. return nullptr;
  3073. ToCategory->setImplementation(Impl);
  3074. }
  3075. return ToCategory;
  3076. }
  3077. bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
  3078. ObjCProtocolDecl *To,
  3079. ImportDefinitionKind Kind) {
  3080. if (To->getDefinition()) {
  3081. if (shouldForceImportDeclContext(Kind))
  3082. ImportDeclContext(From);
  3083. return false;
  3084. }
  3085. // Start the protocol definition
  3086. To->startDefinition();
  3087. // Import protocols
  3088. SmallVector<ObjCProtocolDecl *, 4> Protocols;
  3089. SmallVector<SourceLocation, 4> ProtocolLocs;
  3090. ObjCProtocolDecl::protocol_loc_iterator
  3091. FromProtoLoc = From->protocol_loc_begin();
  3092. for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
  3093. FromProtoEnd = From->protocol_end();
  3094. FromProto != FromProtoEnd;
  3095. ++FromProto, ++FromProtoLoc) {
  3096. ObjCProtocolDecl *ToProto
  3097. = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
  3098. if (!ToProto)
  3099. return true;
  3100. Protocols.push_back(ToProto);
  3101. ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
  3102. }
  3103. // FIXME: If we're merging, make sure that the protocol list is the same.
  3104. To->setProtocolList(Protocols.data(), Protocols.size(),
  3105. ProtocolLocs.data(), Importer.getToContext());
  3106. if (shouldForceImportDeclContext(Kind)) {
  3107. // Import all of the members of this protocol.
  3108. ImportDeclContext(From, /*ForceImport=*/true);
  3109. }
  3110. return false;
  3111. }
  3112. Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
  3113. // If this protocol has a definition in the translation unit we're coming
  3114. // from, but this particular declaration is not that definition, import the
  3115. // definition and map to that.
  3116. ObjCProtocolDecl *Definition = D->getDefinition();
  3117. if (Definition && Definition != D) {
  3118. Decl *ImportedDef = Importer.Import(Definition);
  3119. if (!ImportedDef)
  3120. return nullptr;
  3121. return Importer.Imported(D, ImportedDef);
  3122. }
  3123. // Import the major distinguishing characteristics of a protocol.
  3124. DeclContext *DC, *LexicalDC;
  3125. DeclarationName Name;
  3126. SourceLocation Loc;
  3127. NamedDecl *ToD;
  3128. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  3129. return nullptr;
  3130. if (ToD)
  3131. return ToD;
  3132. ObjCProtocolDecl *MergeWithProtocol = nullptr;
  3133. SmallVector<NamedDecl *, 2> FoundDecls;
  3134. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  3135. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  3136. if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
  3137. continue;
  3138. if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
  3139. break;
  3140. }
  3141. ObjCProtocolDecl *ToProto = MergeWithProtocol;
  3142. if (!ToProto) {
  3143. ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
  3144. Name.getAsIdentifierInfo(), Loc,
  3145. Importer.Import(D->getAtStartLoc()),
  3146. /*PrevDecl=*/nullptr);
  3147. ToProto->setLexicalDeclContext(LexicalDC);
  3148. LexicalDC->addDeclInternal(ToProto);
  3149. }
  3150. Importer.Imported(D, ToProto);
  3151. if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
  3152. return nullptr;
  3153. return ToProto;
  3154. }
  3155. Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
  3156. DeclContext *DC = Importer.ImportContext(D->getDeclContext());
  3157. DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
  3158. SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
  3159. SourceLocation LangLoc = Importer.Import(D->getLocation());
  3160. bool HasBraces = D->hasBraces();
  3161. LinkageSpecDecl *ToLinkageSpec =
  3162. LinkageSpecDecl::Create(Importer.getToContext(),
  3163. DC,
  3164. ExternLoc,
  3165. LangLoc,
  3166. D->getLanguage(),
  3167. HasBraces);
  3168. if (HasBraces) {
  3169. SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
  3170. ToLinkageSpec->setRBraceLoc(RBraceLoc);
  3171. }
  3172. ToLinkageSpec->setLexicalDeclContext(LexicalDC);
  3173. LexicalDC->addDeclInternal(ToLinkageSpec);
  3174. Importer.Imported(D, ToLinkageSpec);
  3175. return ToLinkageSpec;
  3176. }
  3177. bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
  3178. ObjCInterfaceDecl *To,
  3179. ImportDefinitionKind Kind) {
  3180. if (To->getDefinition()) {
  3181. // Check consistency of superclass.
  3182. ObjCInterfaceDecl *FromSuper = From->getSuperClass();
  3183. if (FromSuper) {
  3184. FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
  3185. if (!FromSuper)
  3186. return true;
  3187. }
  3188. ObjCInterfaceDecl *ToSuper = To->getSuperClass();
  3189. if ((bool)FromSuper != (bool)ToSuper ||
  3190. (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
  3191. Importer.ToDiag(To->getLocation(),
  3192. diag::err_odr_objc_superclass_inconsistent)
  3193. << To->getDeclName();
  3194. if (ToSuper)
  3195. Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
  3196. << To->getSuperClass()->getDeclName();
  3197. else
  3198. Importer.ToDiag(To->getLocation(),
  3199. diag::note_odr_objc_missing_superclass);
  3200. if (From->getSuperClass())
  3201. Importer.FromDiag(From->getSuperClassLoc(),
  3202. diag::note_odr_objc_superclass)
  3203. << From->getSuperClass()->getDeclName();
  3204. else
  3205. Importer.FromDiag(From->getLocation(),
  3206. diag::note_odr_objc_missing_superclass);
  3207. }
  3208. if (shouldForceImportDeclContext(Kind))
  3209. ImportDeclContext(From);
  3210. return false;
  3211. }
  3212. // Start the definition.
  3213. To->startDefinition();
  3214. // If this class has a superclass, import it.
  3215. if (From->getSuperClass()) {
  3216. TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
  3217. if (!SuperTInfo)
  3218. return true;
  3219. To->setSuperClass(SuperTInfo);
  3220. }
  3221. // Import protocols
  3222. SmallVector<ObjCProtocolDecl *, 4> Protocols;
  3223. SmallVector<SourceLocation, 4> ProtocolLocs;
  3224. ObjCInterfaceDecl::protocol_loc_iterator
  3225. FromProtoLoc = From->protocol_loc_begin();
  3226. for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
  3227. FromProtoEnd = From->protocol_end();
  3228. FromProto != FromProtoEnd;
  3229. ++FromProto, ++FromProtoLoc) {
  3230. ObjCProtocolDecl *ToProto
  3231. = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
  3232. if (!ToProto)
  3233. return true;
  3234. Protocols.push_back(ToProto);
  3235. ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
  3236. }
  3237. // FIXME: If we're merging, make sure that the protocol list is the same.
  3238. To->setProtocolList(Protocols.data(), Protocols.size(),
  3239. ProtocolLocs.data(), Importer.getToContext());
  3240. // Import categories. When the categories themselves are imported, they'll
  3241. // hook themselves into this interface.
  3242. for (auto *Cat : From->known_categories())
  3243. Importer.Import(Cat);
  3244. // If we have an @implementation, import it as well.
  3245. if (From->getImplementation()) {
  3246. ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
  3247. Importer.Import(From->getImplementation()));
  3248. if (!Impl)
  3249. return true;
  3250. To->setImplementation(Impl);
  3251. }
  3252. if (shouldForceImportDeclContext(Kind)) {
  3253. // Import all of the members of this class.
  3254. ImportDeclContext(From, /*ForceImport=*/true);
  3255. }
  3256. return false;
  3257. }
  3258. ObjCTypeParamList *
  3259. ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
  3260. if (!list)
  3261. return nullptr;
  3262. SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
  3263. for (auto fromTypeParam : *list) {
  3264. auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
  3265. Importer.Import(fromTypeParam));
  3266. if (!toTypeParam)
  3267. return nullptr;
  3268. toTypeParams.push_back(toTypeParam);
  3269. }
  3270. return ObjCTypeParamList::create(Importer.getToContext(),
  3271. Importer.Import(list->getLAngleLoc()),
  3272. toTypeParams,
  3273. Importer.Import(list->getRAngleLoc()));
  3274. }
  3275. Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
  3276. // If this class has a definition in the translation unit we're coming from,
  3277. // but this particular declaration is not that definition, import the
  3278. // definition and map to that.
  3279. ObjCInterfaceDecl *Definition = D->getDefinition();
  3280. if (Definition && Definition != D) {
  3281. Decl *ImportedDef = Importer.Import(Definition);
  3282. if (!ImportedDef)
  3283. return nullptr;
  3284. return Importer.Imported(D, ImportedDef);
  3285. }
  3286. // Import the major distinguishing characteristics of an @interface.
  3287. DeclContext *DC, *LexicalDC;
  3288. DeclarationName Name;
  3289. SourceLocation Loc;
  3290. NamedDecl *ToD;
  3291. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  3292. return nullptr;
  3293. if (ToD)
  3294. return ToD;
  3295. // Look for an existing interface with the same name.
  3296. ObjCInterfaceDecl *MergeWithIface = nullptr;
  3297. SmallVector<NamedDecl *, 2> FoundDecls;
  3298. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  3299. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  3300. if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
  3301. continue;
  3302. if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
  3303. break;
  3304. }
  3305. // Create an interface declaration, if one does not already exist.
  3306. ObjCInterfaceDecl *ToIface = MergeWithIface;
  3307. if (!ToIface) {
  3308. ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
  3309. Importer.Import(D->getAtStartLoc()),
  3310. Name.getAsIdentifierInfo(),
  3311. /*TypeParamList=*/nullptr,
  3312. /*PrevDecl=*/nullptr, Loc,
  3313. D->isImplicitInterfaceDecl());
  3314. ToIface->setLexicalDeclContext(LexicalDC);
  3315. LexicalDC->addDeclInternal(ToIface);
  3316. }
  3317. Importer.Imported(D, ToIface);
  3318. // Import the type parameter list after calling Imported, to avoid
  3319. // loops when bringing in their DeclContext.
  3320. ToIface->setTypeParamList(ImportObjCTypeParamList(
  3321. D->getTypeParamListAsWritten()));
  3322. if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
  3323. return nullptr;
  3324. return ToIface;
  3325. }
  3326. Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
  3327. ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
  3328. Importer.Import(D->getCategoryDecl()));
  3329. if (!Category)
  3330. return nullptr;
  3331. ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
  3332. if (!ToImpl) {
  3333. DeclContext *DC = Importer.ImportContext(D->getDeclContext());
  3334. if (!DC)
  3335. return nullptr;
  3336. SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
  3337. ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
  3338. Importer.Import(D->getIdentifier()),
  3339. Category->getClassInterface(),
  3340. Importer.Import(D->getLocation()),
  3341. Importer.Import(D->getAtStartLoc()),
  3342. CategoryNameLoc);
  3343. DeclContext *LexicalDC = DC;
  3344. if (D->getDeclContext() != D->getLexicalDeclContext()) {
  3345. LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
  3346. if (!LexicalDC)
  3347. return nullptr;
  3348. ToImpl->setLexicalDeclContext(LexicalDC);
  3349. }
  3350. LexicalDC->addDeclInternal(ToImpl);
  3351. Category->setImplementation(ToImpl);
  3352. }
  3353. Importer.Imported(D, ToImpl);
  3354. ImportDeclContext(D);
  3355. return ToImpl;
  3356. }
  3357. Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
  3358. // Find the corresponding interface.
  3359. ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
  3360. Importer.Import(D->getClassInterface()));
  3361. if (!Iface)
  3362. return nullptr;
  3363. // Import the superclass, if any.
  3364. ObjCInterfaceDecl *Super = nullptr;
  3365. if (D->getSuperClass()) {
  3366. Super = cast_or_null<ObjCInterfaceDecl>(
  3367. Importer.Import(D->getSuperClass()));
  3368. if (!Super)
  3369. return nullptr;
  3370. }
  3371. ObjCImplementationDecl *Impl = Iface->getImplementation();
  3372. if (!Impl) {
  3373. // We haven't imported an implementation yet. Create a new @implementation
  3374. // now.
  3375. Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
  3376. Importer.ImportContext(D->getDeclContext()),
  3377. Iface, Super,
  3378. Importer.Import(D->getLocation()),
  3379. Importer.Import(D->getAtStartLoc()),
  3380. Importer.Import(D->getSuperClassLoc()),
  3381. Importer.Import(D->getIvarLBraceLoc()),
  3382. Importer.Import(D->getIvarRBraceLoc()));
  3383. if (D->getDeclContext() != D->getLexicalDeclContext()) {
  3384. DeclContext *LexicalDC
  3385. = Importer.ImportContext(D->getLexicalDeclContext());
  3386. if (!LexicalDC)
  3387. return nullptr;
  3388. Impl->setLexicalDeclContext(LexicalDC);
  3389. }
  3390. // Associate the implementation with the class it implements.
  3391. Iface->setImplementation(Impl);
  3392. Importer.Imported(D, Iface->getImplementation());
  3393. } else {
  3394. Importer.Imported(D, Iface->getImplementation());
  3395. // Verify that the existing @implementation has the same superclass.
  3396. if ((Super && !Impl->getSuperClass()) ||
  3397. (!Super && Impl->getSuperClass()) ||
  3398. (Super && Impl->getSuperClass() &&
  3399. !declaresSameEntity(Super->getCanonicalDecl(),
  3400. Impl->getSuperClass()))) {
  3401. Importer.ToDiag(Impl->getLocation(),
  3402. diag::err_odr_objc_superclass_inconsistent)
  3403. << Iface->getDeclName();
  3404. // FIXME: It would be nice to have the location of the superclass
  3405. // below.
  3406. if (Impl->getSuperClass())
  3407. Importer.ToDiag(Impl->getLocation(),
  3408. diag::note_odr_objc_superclass)
  3409. << Impl->getSuperClass()->getDeclName();
  3410. else
  3411. Importer.ToDiag(Impl->getLocation(),
  3412. diag::note_odr_objc_missing_superclass);
  3413. if (D->getSuperClass())
  3414. Importer.FromDiag(D->getLocation(),
  3415. diag::note_odr_objc_superclass)
  3416. << D->getSuperClass()->getDeclName();
  3417. else
  3418. Importer.FromDiag(D->getLocation(),
  3419. diag::note_odr_objc_missing_superclass);
  3420. return nullptr;
  3421. }
  3422. }
  3423. // Import all of the members of this @implementation.
  3424. ImportDeclContext(D);
  3425. return Impl;
  3426. }
  3427. Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
  3428. // Import the major distinguishing characteristics of an @property.
  3429. DeclContext *DC, *LexicalDC;
  3430. DeclarationName Name;
  3431. SourceLocation Loc;
  3432. NamedDecl *ToD;
  3433. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  3434. return nullptr;
  3435. if (ToD)
  3436. return ToD;
  3437. // Check whether we have already imported this property.
  3438. SmallVector<NamedDecl *, 2> FoundDecls;
  3439. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  3440. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  3441. if (ObjCPropertyDecl *FoundProp
  3442. = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
  3443. // Check property types.
  3444. if (!Importer.IsStructurallyEquivalent(D->getType(),
  3445. FoundProp->getType())) {
  3446. Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
  3447. << Name << D->getType() << FoundProp->getType();
  3448. Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
  3449. << FoundProp->getType();
  3450. return nullptr;
  3451. }
  3452. // FIXME: Check property attributes, getters, setters, etc.?
  3453. // Consider these properties to be equivalent.
  3454. Importer.Imported(D, FoundProp);
  3455. return FoundProp;
  3456. }
  3457. }
  3458. // Import the type.
  3459. TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
  3460. if (!TSI)
  3461. return nullptr;
  3462. // Create the new property.
  3463. ObjCPropertyDecl *ToProperty
  3464. = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
  3465. Name.getAsIdentifierInfo(),
  3466. Importer.Import(D->getAtLoc()),
  3467. Importer.Import(D->getLParenLoc()),
  3468. Importer.Import(D->getType()),
  3469. TSI,
  3470. D->getPropertyImplementation());
  3471. Importer.Imported(D, ToProperty);
  3472. ToProperty->setLexicalDeclContext(LexicalDC);
  3473. LexicalDC->addDeclInternal(ToProperty);
  3474. ToProperty->setPropertyAttributes(D->getPropertyAttributes());
  3475. ToProperty->setPropertyAttributesAsWritten(
  3476. D->getPropertyAttributesAsWritten());
  3477. ToProperty->setGetterName(Importer.Import(D->getGetterName()));
  3478. ToProperty->setSetterName(Importer.Import(D->getSetterName()));
  3479. ToProperty->setGetterMethodDecl(
  3480. cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
  3481. ToProperty->setSetterMethodDecl(
  3482. cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
  3483. ToProperty->setPropertyIvarDecl(
  3484. cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
  3485. return ToProperty;
  3486. }
  3487. Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
  3488. ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
  3489. Importer.Import(D->getPropertyDecl()));
  3490. if (!Property)
  3491. return nullptr;
  3492. DeclContext *DC = Importer.ImportContext(D->getDeclContext());
  3493. if (!DC)
  3494. return nullptr;
  3495. // Import the lexical declaration context.
  3496. DeclContext *LexicalDC = DC;
  3497. if (D->getDeclContext() != D->getLexicalDeclContext()) {
  3498. LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
  3499. if (!LexicalDC)
  3500. return nullptr;
  3501. }
  3502. ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
  3503. if (!InImpl)
  3504. return nullptr;
  3505. // Import the ivar (for an @synthesize).
  3506. ObjCIvarDecl *Ivar = nullptr;
  3507. if (D->getPropertyIvarDecl()) {
  3508. Ivar = cast_or_null<ObjCIvarDecl>(
  3509. Importer.Import(D->getPropertyIvarDecl()));
  3510. if (!Ivar)
  3511. return nullptr;
  3512. }
  3513. ObjCPropertyImplDecl *ToImpl
  3514. = InImpl->FindPropertyImplDecl(Property->getIdentifier());
  3515. if (!ToImpl) {
  3516. ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
  3517. Importer.Import(D->getLocStart()),
  3518. Importer.Import(D->getLocation()),
  3519. Property,
  3520. D->getPropertyImplementation(),
  3521. Ivar,
  3522. Importer.Import(D->getPropertyIvarDeclLoc()));
  3523. ToImpl->setLexicalDeclContext(LexicalDC);
  3524. Importer.Imported(D, ToImpl);
  3525. LexicalDC->addDeclInternal(ToImpl);
  3526. } else {
  3527. // Check that we have the same kind of property implementation (@synthesize
  3528. // vs. @dynamic).
  3529. if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
  3530. Importer.ToDiag(ToImpl->getLocation(),
  3531. diag::err_odr_objc_property_impl_kind_inconsistent)
  3532. << Property->getDeclName()
  3533. << (ToImpl->getPropertyImplementation()
  3534. == ObjCPropertyImplDecl::Dynamic);
  3535. Importer.FromDiag(D->getLocation(),
  3536. diag::note_odr_objc_property_impl_kind)
  3537. << D->getPropertyDecl()->getDeclName()
  3538. << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
  3539. return nullptr;
  3540. }
  3541. // For @synthesize, check that we have the same
  3542. if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
  3543. Ivar != ToImpl->getPropertyIvarDecl()) {
  3544. Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
  3545. diag::err_odr_objc_synthesize_ivar_inconsistent)
  3546. << Property->getDeclName()
  3547. << ToImpl->getPropertyIvarDecl()->getDeclName()
  3548. << Ivar->getDeclName();
  3549. Importer.FromDiag(D->getPropertyIvarDeclLoc(),
  3550. diag::note_odr_objc_synthesize_ivar_here)
  3551. << D->getPropertyIvarDecl()->getDeclName();
  3552. return nullptr;
  3553. }
  3554. // Merge the existing implementation with the new implementation.
  3555. Importer.Imported(D, ToImpl);
  3556. }
  3557. return ToImpl;
  3558. }
  3559. Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
  3560. // For template arguments, we adopt the translation unit as our declaration
  3561. // context. This context will be fixed when the actual template declaration
  3562. // is created.
  3563. // FIXME: Import default argument.
  3564. return TemplateTypeParmDecl::Create(Importer.getToContext(),
  3565. Importer.getToContext().getTranslationUnitDecl(),
  3566. Importer.Import(D->getLocStart()),
  3567. Importer.Import(D->getLocation()),
  3568. D->getDepth(),
  3569. D->getIndex(),
  3570. Importer.Import(D->getIdentifier()),
  3571. D->wasDeclaredWithTypename(),
  3572. D->isParameterPack());
  3573. }
  3574. Decl *
  3575. ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
  3576. // Import the name of this declaration.
  3577. DeclarationName Name = Importer.Import(D->getDeclName());
  3578. if (D->getDeclName() && !Name)
  3579. return nullptr;
  3580. // Import the location of this declaration.
  3581. SourceLocation Loc = Importer.Import(D->getLocation());
  3582. // Import the type of this declaration.
  3583. QualType T = Importer.Import(D->getType());
  3584. if (T.isNull())
  3585. return nullptr;
  3586. // Import type-source information.
  3587. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  3588. if (D->getTypeSourceInfo() && !TInfo)
  3589. return nullptr;
  3590. // FIXME: Import default argument.
  3591. return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
  3592. Importer.getToContext().getTranslationUnitDecl(),
  3593. Importer.Import(D->getInnerLocStart()),
  3594. Loc, D->getDepth(), D->getPosition(),
  3595. Name.getAsIdentifierInfo(),
  3596. T, D->isParameterPack(), TInfo);
  3597. }
  3598. Decl *
  3599. ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
  3600. // Import the name of this declaration.
  3601. DeclarationName Name = Importer.Import(D->getDeclName());
  3602. if (D->getDeclName() && !Name)
  3603. return nullptr;
  3604. // Import the location of this declaration.
  3605. SourceLocation Loc = Importer.Import(D->getLocation());
  3606. // Import template parameters.
  3607. TemplateParameterList *TemplateParams
  3608. = ImportTemplateParameterList(D->getTemplateParameters());
  3609. if (!TemplateParams)
  3610. return nullptr;
  3611. // FIXME: Import default argument.
  3612. return TemplateTemplateParmDecl::Create(Importer.getToContext(),
  3613. Importer.getToContext().getTranslationUnitDecl(),
  3614. Loc, D->getDepth(), D->getPosition(),
  3615. D->isParameterPack(),
  3616. Name.getAsIdentifierInfo(),
  3617. TemplateParams);
  3618. }
  3619. Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
  3620. // If this record has a definition in the translation unit we're coming from,
  3621. // but this particular declaration is not that definition, import the
  3622. // definition and map to that.
  3623. CXXRecordDecl *Definition
  3624. = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
  3625. if (Definition && Definition != D->getTemplatedDecl()) {
  3626. Decl *ImportedDef
  3627. = Importer.Import(Definition->getDescribedClassTemplate());
  3628. if (!ImportedDef)
  3629. return nullptr;
  3630. return Importer.Imported(D, ImportedDef);
  3631. }
  3632. // Import the major distinguishing characteristics of this class template.
  3633. DeclContext *DC, *LexicalDC;
  3634. DeclarationName Name;
  3635. SourceLocation Loc;
  3636. NamedDecl *ToD;
  3637. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  3638. return nullptr;
  3639. if (ToD)
  3640. return ToD;
  3641. // We may already have a template of the same name; try to find and match it.
  3642. if (!DC->isFunctionOrMethod()) {
  3643. SmallVector<NamedDecl *, 4> ConflictingDecls;
  3644. SmallVector<NamedDecl *, 2> FoundDecls;
  3645. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  3646. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  3647. if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
  3648. continue;
  3649. Decl *Found = FoundDecls[I];
  3650. if (ClassTemplateDecl *FoundTemplate
  3651. = dyn_cast<ClassTemplateDecl>(Found)) {
  3652. if (IsStructuralMatch(D, FoundTemplate)) {
  3653. // The class templates structurally match; call it the same template.
  3654. // FIXME: We may be filling in a forward declaration here. Handle
  3655. // this case!
  3656. Importer.Imported(D->getTemplatedDecl(),
  3657. FoundTemplate->getTemplatedDecl());
  3658. return Importer.Imported(D, FoundTemplate);
  3659. }
  3660. }
  3661. ConflictingDecls.push_back(FoundDecls[I]);
  3662. }
  3663. if (!ConflictingDecls.empty()) {
  3664. Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
  3665. ConflictingDecls.data(),
  3666. ConflictingDecls.size());
  3667. }
  3668. if (!Name)
  3669. return nullptr;
  3670. }
  3671. CXXRecordDecl *DTemplated = D->getTemplatedDecl();
  3672. // Create the declaration that is being templated.
  3673. SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
  3674. SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
  3675. CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
  3676. DTemplated->getTagKind(),
  3677. DC, StartLoc, IdLoc,
  3678. Name.getAsIdentifierInfo());
  3679. D2Templated->setAccess(DTemplated->getAccess());
  3680. D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
  3681. D2Templated->setLexicalDeclContext(LexicalDC);
  3682. // Create the class template declaration itself.
  3683. TemplateParameterList *TemplateParams
  3684. = ImportTemplateParameterList(D->getTemplateParameters());
  3685. if (!TemplateParams)
  3686. return nullptr;
  3687. ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
  3688. Loc, Name, TemplateParams,
  3689. D2Templated,
  3690. /*PrevDecl=*/nullptr);
  3691. D2Templated->setDescribedClassTemplate(D2);
  3692. D2->setAccess(D->getAccess());
  3693. D2->setLexicalDeclContext(LexicalDC);
  3694. LexicalDC->addDeclInternal(D2);
  3695. // Note the relationship between the class templates.
  3696. Importer.Imported(D, D2);
  3697. Importer.Imported(DTemplated, D2Templated);
  3698. if (DTemplated->isCompleteDefinition() &&
  3699. !D2Templated->isCompleteDefinition()) {
  3700. // FIXME: Import definition!
  3701. }
  3702. return D2;
  3703. }
  3704. Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
  3705. ClassTemplateSpecializationDecl *D) {
  3706. // If this record has a definition in the translation unit we're coming from,
  3707. // but this particular declaration is not that definition, import the
  3708. // definition and map to that.
  3709. TagDecl *Definition = D->getDefinition();
  3710. if (Definition && Definition != D) {
  3711. Decl *ImportedDef = Importer.Import(Definition);
  3712. if (!ImportedDef)
  3713. return nullptr;
  3714. return Importer.Imported(D, ImportedDef);
  3715. }
  3716. ClassTemplateDecl *ClassTemplate
  3717. = cast_or_null<ClassTemplateDecl>(Importer.Import(
  3718. D->getSpecializedTemplate()));
  3719. if (!ClassTemplate)
  3720. return nullptr;
  3721. // Import the context of this declaration.
  3722. DeclContext *DC = ClassTemplate->getDeclContext();
  3723. if (!DC)
  3724. return nullptr;
  3725. DeclContext *LexicalDC = DC;
  3726. if (D->getDeclContext() != D->getLexicalDeclContext()) {
  3727. LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
  3728. if (!LexicalDC)
  3729. return nullptr;
  3730. }
  3731. // Import the location of this declaration.
  3732. SourceLocation StartLoc = Importer.Import(D->getLocStart());
  3733. SourceLocation IdLoc = Importer.Import(D->getLocation());
  3734. // Import template arguments.
  3735. SmallVector<TemplateArgument, 2> TemplateArgs;
  3736. if (ImportTemplateArguments(D->getTemplateArgs().data(),
  3737. D->getTemplateArgs().size(),
  3738. TemplateArgs))
  3739. return nullptr;
  3740. // Try to find an existing specialization with these template arguments.
  3741. void *InsertPos = nullptr;
  3742. ClassTemplateSpecializationDecl *D2
  3743. = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
  3744. if (D2) {
  3745. // We already have a class template specialization with these template
  3746. // arguments.
  3747. // FIXME: Check for specialization vs. instantiation errors.
  3748. if (RecordDecl *FoundDef = D2->getDefinition()) {
  3749. if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
  3750. // The record types structurally match, or the "from" translation
  3751. // unit only had a forward declaration anyway; call it the same
  3752. // function.
  3753. return Importer.Imported(D, FoundDef);
  3754. }
  3755. }
  3756. } else {
  3757. // Create a new specialization.
  3758. D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
  3759. D->getTagKind(), DC,
  3760. StartLoc, IdLoc,
  3761. ClassTemplate,
  3762. TemplateArgs.data(),
  3763. TemplateArgs.size(),
  3764. /*PrevDecl=*/nullptr);
  3765. D2->setSpecializationKind(D->getSpecializationKind());
  3766. // Add this specialization to the class template.
  3767. ClassTemplate->AddSpecialization(D2, InsertPos);
  3768. // Import the qualifier, if any.
  3769. D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
  3770. // Add the specialization to this context.
  3771. D2->setLexicalDeclContext(LexicalDC);
  3772. LexicalDC->addDeclInternal(D2);
  3773. }
  3774. Importer.Imported(D, D2);
  3775. if (D->isCompleteDefinition() && ImportDefinition(D, D2))
  3776. return nullptr;
  3777. return D2;
  3778. }
  3779. Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
  3780. // If this variable has a definition in the translation unit we're coming
  3781. // from,
  3782. // but this particular declaration is not that definition, import the
  3783. // definition and map to that.
  3784. VarDecl *Definition =
  3785. cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
  3786. if (Definition && Definition != D->getTemplatedDecl()) {
  3787. Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
  3788. if (!ImportedDef)
  3789. return nullptr;
  3790. return Importer.Imported(D, ImportedDef);
  3791. }
  3792. // Import the major distinguishing characteristics of this variable template.
  3793. DeclContext *DC, *LexicalDC;
  3794. DeclarationName Name;
  3795. SourceLocation Loc;
  3796. NamedDecl *ToD;
  3797. if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
  3798. return nullptr;
  3799. if (ToD)
  3800. return ToD;
  3801. // We may already have a template of the same name; try to find and match it.
  3802. assert(!DC->isFunctionOrMethod() &&
  3803. "Variable templates cannot be declared at function scope");
  3804. SmallVector<NamedDecl *, 4> ConflictingDecls;
  3805. SmallVector<NamedDecl *, 2> FoundDecls;
  3806. DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  3807. for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
  3808. if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
  3809. continue;
  3810. Decl *Found = FoundDecls[I];
  3811. if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
  3812. if (IsStructuralMatch(D, FoundTemplate)) {
  3813. // The variable templates structurally match; call it the same template.
  3814. Importer.Imported(D->getTemplatedDecl(),
  3815. FoundTemplate->getTemplatedDecl());
  3816. return Importer.Imported(D, FoundTemplate);
  3817. }
  3818. }
  3819. ConflictingDecls.push_back(FoundDecls[I]);
  3820. }
  3821. if (!ConflictingDecls.empty()) {
  3822. Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
  3823. ConflictingDecls.data(),
  3824. ConflictingDecls.size());
  3825. }
  3826. if (!Name)
  3827. return nullptr;
  3828. VarDecl *DTemplated = D->getTemplatedDecl();
  3829. // Import the type.
  3830. QualType T = Importer.Import(DTemplated->getType());
  3831. if (T.isNull())
  3832. return nullptr;
  3833. // Create the declaration that is being templated.
  3834. SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
  3835. SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
  3836. TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
  3837. VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
  3838. IdLoc, Name.getAsIdentifierInfo(), T,
  3839. TInfo, DTemplated->getStorageClass());
  3840. D2Templated->setAccess(DTemplated->getAccess());
  3841. D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
  3842. D2Templated->setLexicalDeclContext(LexicalDC);
  3843. // Importer.Imported(DTemplated, D2Templated);
  3844. // LexicalDC->addDeclInternal(D2Templated);
  3845. // Merge the initializer.
  3846. if (ImportDefinition(DTemplated, D2Templated))
  3847. return nullptr;
  3848. // Create the variable template declaration itself.
  3849. TemplateParameterList *TemplateParams =
  3850. ImportTemplateParameterList(D->getTemplateParameters());
  3851. if (!TemplateParams)
  3852. return nullptr;
  3853. VarTemplateDecl *D2 = VarTemplateDecl::Create(
  3854. Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
  3855. D2Templated->setDescribedVarTemplate(D2);
  3856. D2->setAccess(D->getAccess());
  3857. D2->setLexicalDeclContext(LexicalDC);
  3858. LexicalDC->addDeclInternal(D2);
  3859. // Note the relationship between the variable templates.
  3860. Importer.Imported(D, D2);
  3861. Importer.Imported(DTemplated, D2Templated);
  3862. if (DTemplated->isThisDeclarationADefinition() &&
  3863. !D2Templated->isThisDeclarationADefinition()) {
  3864. // FIXME: Import definition!
  3865. }
  3866. return D2;
  3867. }
  3868. Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
  3869. VarTemplateSpecializationDecl *D) {
  3870. // If this record has a definition in the translation unit we're coming from,
  3871. // but this particular declaration is not that definition, import the
  3872. // definition and map to that.
  3873. VarDecl *Definition = D->getDefinition();
  3874. if (Definition && Definition != D) {
  3875. Decl *ImportedDef = Importer.Import(Definition);
  3876. if (!ImportedDef)
  3877. return nullptr;
  3878. return Importer.Imported(D, ImportedDef);
  3879. }
  3880. VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
  3881. Importer.Import(D->getSpecializedTemplate()));
  3882. if (!VarTemplate)
  3883. return nullptr;
  3884. // Import the context of this declaration.
  3885. DeclContext *DC = VarTemplate->getDeclContext();
  3886. if (!DC)
  3887. return nullptr;
  3888. DeclContext *LexicalDC = DC;
  3889. if (D->getDeclContext() != D->getLexicalDeclContext()) {
  3890. LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
  3891. if (!LexicalDC)
  3892. return nullptr;
  3893. }
  3894. // Import the location of this declaration.
  3895. SourceLocation StartLoc = Importer.Import(D->getLocStart());
  3896. SourceLocation IdLoc = Importer.Import(D->getLocation());
  3897. // Import template arguments.
  3898. SmallVector<TemplateArgument, 2> TemplateArgs;
  3899. if (ImportTemplateArguments(D->getTemplateArgs().data(),
  3900. D->getTemplateArgs().size(), TemplateArgs))
  3901. return nullptr;
  3902. // Try to find an existing specialization with these template arguments.
  3903. void *InsertPos = nullptr;
  3904. VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
  3905. TemplateArgs, InsertPos);
  3906. if (D2) {
  3907. // We already have a variable template specialization with these template
  3908. // arguments.
  3909. // FIXME: Check for specialization vs. instantiation errors.
  3910. if (VarDecl *FoundDef = D2->getDefinition()) {
  3911. if (!D->isThisDeclarationADefinition() ||
  3912. IsStructuralMatch(D, FoundDef)) {
  3913. // The record types structurally match, or the "from" translation
  3914. // unit only had a forward declaration anyway; call it the same
  3915. // variable.
  3916. return Importer.Imported(D, FoundDef);
  3917. }
  3918. }
  3919. } else {
  3920. // Import the type.
  3921. QualType T = Importer.Import(D->getType());
  3922. if (T.isNull())
  3923. return nullptr;
  3924. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
  3925. // Create a new specialization.
  3926. D2 = VarTemplateSpecializationDecl::Create(
  3927. Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
  3928. D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
  3929. D2->setSpecializationKind(D->getSpecializationKind());
  3930. D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
  3931. // Add this specialization to the class template.
  3932. VarTemplate->AddSpecialization(D2, InsertPos);
  3933. // Import the qualifier, if any.
  3934. D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
  3935. // Add the specialization to this context.
  3936. D2->setLexicalDeclContext(LexicalDC);
  3937. LexicalDC->addDeclInternal(D2);
  3938. }
  3939. Importer.Imported(D, D2);
  3940. if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
  3941. return nullptr;
  3942. return D2;
  3943. }
  3944. //----------------------------------------------------------------------------
  3945. // Import Statements
  3946. //----------------------------------------------------------------------------
  3947. DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
  3948. if (DG.isNull())
  3949. return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
  3950. size_t NumDecls = DG.end() - DG.begin();
  3951. SmallVector<Decl *, 1> ToDecls(NumDecls);
  3952. auto &_Importer = this->Importer;
  3953. std::transform(DG.begin(), DG.end(), ToDecls.begin(),
  3954. [&_Importer](Decl *D) -> Decl * {
  3955. return _Importer.Import(D);
  3956. });
  3957. return DeclGroupRef::Create(Importer.getToContext(),
  3958. ToDecls.begin(),
  3959. NumDecls);
  3960. }
  3961. Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
  3962. Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
  3963. << S->getStmtClassName();
  3964. return nullptr;
  3965. }
  3966. Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
  3967. DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
  3968. for (Decl *ToD : ToDG) {
  3969. if (!ToD)
  3970. return nullptr;
  3971. }
  3972. SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
  3973. SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
  3974. return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
  3975. }
  3976. Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
  3977. SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
  3978. return new (Importer.getToContext()) NullStmt(ToSemiLoc,
  3979. S->hasLeadingEmptyMacro());
  3980. }
  3981. Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
  3982. SmallVector<Stmt *, 4> ToStmts(S->size());
  3983. auto &_Importer = this->Importer;
  3984. std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
  3985. [&_Importer](Stmt *CS) -> Stmt * {
  3986. return _Importer.Import(CS);
  3987. });
  3988. for (Stmt *ToS : ToStmts) {
  3989. if (!ToS)
  3990. return nullptr;
  3991. }
  3992. SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
  3993. SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
  3994. return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
  3995. ToStmts,
  3996. ToLBraceLoc, ToRBraceLoc);
  3997. }
  3998. Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
  3999. Expr *ToLHS = Importer.Import(S->getLHS());
  4000. if (!ToLHS)
  4001. return nullptr;
  4002. Expr *ToRHS = Importer.Import(S->getRHS());
  4003. if (!ToRHS && S->getRHS())
  4004. return nullptr;
  4005. SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
  4006. SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
  4007. SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
  4008. return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
  4009. ToCaseLoc, ToEllipsisLoc,
  4010. ToColonLoc);
  4011. }
  4012. Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
  4013. SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
  4014. SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
  4015. Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
  4016. if (!ToSubStmt && S->getSubStmt())
  4017. return nullptr;
  4018. return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
  4019. ToSubStmt);
  4020. }
  4021. Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
  4022. SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
  4023. LabelDecl *ToLabelDecl =
  4024. cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
  4025. if (!ToLabelDecl && S->getDecl())
  4026. return nullptr;
  4027. Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
  4028. if (!ToSubStmt && S->getSubStmt())
  4029. return nullptr;
  4030. return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
  4031. ToSubStmt);
  4032. }
  4033. Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
  4034. SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
  4035. ArrayRef<const Attr*> FromAttrs(S->getAttrs());
  4036. SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
  4037. ASTContext &_ToContext = Importer.getToContext();
  4038. std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
  4039. [&_ToContext](const Attr *A) -> const Attr * {
  4040. return A->clone(_ToContext);
  4041. });
  4042. for (const Attr *ToA : ToAttrs) {
  4043. if (!ToA)
  4044. return nullptr;
  4045. }
  4046. Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
  4047. if (!ToSubStmt && S->getSubStmt())
  4048. return nullptr;
  4049. return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
  4050. ToAttrs, ToSubStmt);
  4051. }
  4052. Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
  4053. SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
  4054. VarDecl *ToConditionVariable = nullptr;
  4055. if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
  4056. ToConditionVariable =
  4057. dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
  4058. if (!ToConditionVariable)
  4059. return nullptr;
  4060. }
  4061. Expr *ToCondition = Importer.Import(S->getCond());
  4062. if (!ToCondition && S->getCond())
  4063. return nullptr;
  4064. Stmt *ToThenStmt = Importer.Import(S->getThen());
  4065. if (!ToThenStmt && S->getThen())
  4066. return nullptr;
  4067. SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
  4068. Stmt *ToElseStmt = Importer.Import(S->getElse());
  4069. if (!ToElseStmt && S->getElse())
  4070. return nullptr;
  4071. return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
  4072. ToIfLoc, ToConditionVariable,
  4073. ToCondition, ToThenStmt,
  4074. ToElseLoc, ToElseStmt);
  4075. }
  4076. Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
  4077. VarDecl *ToConditionVariable = nullptr;
  4078. if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
  4079. ToConditionVariable =
  4080. dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
  4081. if (!ToConditionVariable)
  4082. return nullptr;
  4083. }
  4084. Expr *ToCondition = Importer.Import(S->getCond());
  4085. if (!ToCondition && S->getCond())
  4086. return nullptr;
  4087. SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
  4088. Importer.getToContext(), ToConditionVariable,
  4089. ToCondition);
  4090. Stmt *ToBody = Importer.Import(S->getBody());
  4091. if (!ToBody && S->getBody())
  4092. return nullptr;
  4093. ToStmt->setBody(ToBody);
  4094. ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
  4095. // Now we have to re-chain the cases.
  4096. SwitchCase *LastChainedSwitchCase = nullptr;
  4097. for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
  4098. SC = SC->getNextSwitchCase()) {
  4099. SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
  4100. if (!ToSC)
  4101. return nullptr;
  4102. if (LastChainedSwitchCase)
  4103. LastChainedSwitchCase->setNextSwitchCase(ToSC);
  4104. else
  4105. ToStmt->setSwitchCaseList(ToSC);
  4106. LastChainedSwitchCase = ToSC;
  4107. }
  4108. return ToStmt;
  4109. }
  4110. Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
  4111. VarDecl *ToConditionVariable = nullptr;
  4112. if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
  4113. ToConditionVariable =
  4114. dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
  4115. if (!ToConditionVariable)
  4116. return nullptr;
  4117. }
  4118. Expr *ToCondition = Importer.Import(S->getCond());
  4119. if (!ToCondition && S->getCond())
  4120. return nullptr;
  4121. Stmt *ToBody = Importer.Import(S->getBody());
  4122. if (!ToBody && S->getBody())
  4123. return nullptr;
  4124. SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
  4125. return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
  4126. ToConditionVariable,
  4127. ToCondition, ToBody,
  4128. ToWhileLoc);
  4129. }
  4130. Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
  4131. Stmt *ToBody = Importer.Import(S->getBody());
  4132. if (!ToBody && S->getBody())
  4133. return nullptr;
  4134. Expr *ToCondition = Importer.Import(S->getCond());
  4135. if (!ToCondition && S->getCond())
  4136. return nullptr;
  4137. SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
  4138. SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
  4139. SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
  4140. return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
  4141. ToDoLoc, ToWhileLoc,
  4142. ToRParenLoc);
  4143. }
  4144. Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
  4145. Stmt *ToInit = Importer.Import(S->getInit());
  4146. if (!ToInit && S->getInit())
  4147. return nullptr;
  4148. Expr *ToCondition = Importer.Import(S->getCond());
  4149. if (!ToCondition && S->getCond())
  4150. return nullptr;
  4151. VarDecl *ToConditionVariable = nullptr;
  4152. if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
  4153. ToConditionVariable =
  4154. dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
  4155. if (!ToConditionVariable)
  4156. return nullptr;
  4157. }
  4158. Expr *ToInc = Importer.Import(S->getInc());
  4159. if (!ToInc && S->getInc())
  4160. return nullptr;
  4161. Stmt *ToBody = Importer.Import(S->getBody());
  4162. if (!ToBody && S->getBody())
  4163. return nullptr;
  4164. SourceLocation ToForLoc = Importer.Import(S->getForLoc());
  4165. SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
  4166. SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
  4167. return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
  4168. ToInit, ToCondition,
  4169. ToConditionVariable,
  4170. ToInc, ToBody,
  4171. ToForLoc, ToLParenLoc,
  4172. ToRParenLoc);
  4173. }
  4174. Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
  4175. LabelDecl *ToLabel = nullptr;
  4176. if (LabelDecl *FromLabel = S->getLabel()) {
  4177. ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
  4178. if (!ToLabel)
  4179. return nullptr;
  4180. }
  4181. SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
  4182. SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
  4183. return new (Importer.getToContext()) GotoStmt(ToLabel,
  4184. ToGotoLoc, ToLabelLoc);
  4185. }
  4186. Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
  4187. SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
  4188. SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
  4189. Expr *ToTarget = Importer.Import(S->getTarget());
  4190. if (!ToTarget && S->getTarget())
  4191. return nullptr;
  4192. return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
  4193. ToTarget);
  4194. }
  4195. Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
  4196. SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
  4197. return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
  4198. }
  4199. Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
  4200. SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
  4201. return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
  4202. }
  4203. Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
  4204. SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
  4205. Expr *ToRetExpr = Importer.Import(S->getRetValue());
  4206. if (!ToRetExpr && S->getRetValue())
  4207. return nullptr;
  4208. VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
  4209. VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
  4210. if (!ToNRVOCandidate && NRVOCandidate)
  4211. return nullptr;
  4212. return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
  4213. ToNRVOCandidate);
  4214. }
  4215. Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
  4216. SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
  4217. VarDecl *ToExceptionDecl = nullptr;
  4218. if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
  4219. ToExceptionDecl =
  4220. dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
  4221. if (!ToExceptionDecl)
  4222. return nullptr;
  4223. }
  4224. Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
  4225. if (!ToHandlerBlock && S->getHandlerBlock())
  4226. return nullptr;
  4227. return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
  4228. ToExceptionDecl,
  4229. ToHandlerBlock);
  4230. }
  4231. Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
  4232. SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
  4233. Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
  4234. if (!ToTryBlock && S->getTryBlock())
  4235. return nullptr;
  4236. SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
  4237. for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
  4238. CXXCatchStmt *FromHandler = S->getHandler(HI);
  4239. if (Stmt *ToHandler = Importer.Import(FromHandler))
  4240. ToHandlers[HI] = ToHandler;
  4241. else
  4242. return nullptr;
  4243. }
  4244. return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
  4245. ToHandlers);
  4246. }
  4247. Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
  4248. DeclStmt *ToRange =
  4249. dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
  4250. if (!ToRange && S->getRangeStmt())
  4251. return nullptr;
  4252. DeclStmt *ToBeginEnd =
  4253. dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
  4254. if (!ToBeginEnd && S->getBeginEndStmt())
  4255. return nullptr;
  4256. Expr *ToCond = Importer.Import(S->getCond());
  4257. if (!ToCond && S->getCond())
  4258. return nullptr;
  4259. Expr *ToInc = Importer.Import(S->getInc());
  4260. if (!ToInc && S->getInc())
  4261. return nullptr;
  4262. DeclStmt *ToLoopVar =
  4263. dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
  4264. if (!ToLoopVar && S->getLoopVarStmt())
  4265. return nullptr;
  4266. Stmt *ToBody = Importer.Import(S->getBody());
  4267. if (!ToBody && S->getBody())
  4268. return nullptr;
  4269. SourceLocation ToForLoc = Importer.Import(S->getForLoc());
  4270. SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
  4271. SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
  4272. return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
  4273. ToCond, ToInc,
  4274. ToLoopVar, ToBody,
  4275. ToForLoc, ToColonLoc,
  4276. ToRParenLoc);
  4277. }
  4278. Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
  4279. Stmt *ToElem = Importer.Import(S->getElement());
  4280. if (!ToElem && S->getElement())
  4281. return nullptr;
  4282. Expr *ToCollect = Importer.Import(S->getCollection());
  4283. if (!ToCollect && S->getCollection())
  4284. return nullptr;
  4285. Stmt *ToBody = Importer.Import(S->getBody());
  4286. if (!ToBody && S->getBody())
  4287. return nullptr;
  4288. SourceLocation ToForLoc = Importer.Import(S->getForLoc());
  4289. SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
  4290. return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
  4291. ToCollect,
  4292. ToBody, ToForLoc,
  4293. ToRParenLoc);
  4294. }
  4295. Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
  4296. SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
  4297. SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
  4298. VarDecl *ToExceptionDecl = nullptr;
  4299. if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
  4300. ToExceptionDecl =
  4301. dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
  4302. if (!ToExceptionDecl)
  4303. return nullptr;
  4304. }
  4305. Stmt *ToBody = Importer.Import(S->getCatchBody());
  4306. if (!ToBody && S->getCatchBody())
  4307. return nullptr;
  4308. return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
  4309. ToRParenLoc,
  4310. ToExceptionDecl,
  4311. ToBody);
  4312. }
  4313. Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
  4314. SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
  4315. Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
  4316. if (!ToAtFinallyStmt && S->getFinallyBody())
  4317. return nullptr;
  4318. return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
  4319. ToAtFinallyStmt);
  4320. }
  4321. Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
  4322. SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
  4323. Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
  4324. if (!ToAtTryStmt && S->getTryBody())
  4325. return nullptr;
  4326. SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
  4327. for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
  4328. ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
  4329. if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
  4330. ToCatchStmts[CI] = ToCatchStmt;
  4331. else
  4332. return nullptr;
  4333. }
  4334. Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
  4335. if (!ToAtFinallyStmt && S->getFinallyStmt())
  4336. return nullptr;
  4337. return ObjCAtTryStmt::Create(Importer.getToContext(),
  4338. ToAtTryLoc, ToAtTryStmt,
  4339. ToCatchStmts.begin(), ToCatchStmts.size(),
  4340. ToAtFinallyStmt);
  4341. }
  4342. Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
  4343. (ObjCAtSynchronizedStmt *S) {
  4344. SourceLocation ToAtSynchronizedLoc =
  4345. Importer.Import(S->getAtSynchronizedLoc());
  4346. Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
  4347. if (!ToSynchExpr && S->getSynchExpr())
  4348. return nullptr;
  4349. Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
  4350. if (!ToSynchBody && S->getSynchBody())
  4351. return nullptr;
  4352. return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
  4353. ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
  4354. }
  4355. Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
  4356. SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
  4357. Expr *ToThrow = Importer.Import(S->getThrowExpr());
  4358. if (!ToThrow && S->getThrowExpr())
  4359. return nullptr;
  4360. return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
  4361. }
  4362. Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
  4363. (ObjCAutoreleasePoolStmt *S) {
  4364. SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
  4365. Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
  4366. if (!ToSubStmt && S->getSubStmt())
  4367. return nullptr;
  4368. return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
  4369. ToSubStmt);
  4370. }
  4371. //----------------------------------------------------------------------------
  4372. // Import Expressions
  4373. //----------------------------------------------------------------------------
  4374. Expr *ASTNodeImporter::VisitExpr(Expr *E) {
  4375. Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
  4376. << E->getStmtClassName();
  4377. return nullptr;
  4378. }
  4379. Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
  4380. ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
  4381. if (!ToD)
  4382. return nullptr;
  4383. NamedDecl *FoundD = nullptr;
  4384. if (E->getDecl() != E->getFoundDecl()) {
  4385. FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
  4386. if (!FoundD)
  4387. return nullptr;
  4388. }
  4389. QualType T = Importer.Import(E->getType());
  4390. if (T.isNull())
  4391. return nullptr;
  4392. DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
  4393. Importer.Import(E->getQualifierLoc()),
  4394. Importer.Import(E->getTemplateKeywordLoc()),
  4395. ToD,
  4396. E->refersToEnclosingVariableOrCapture(),
  4397. Importer.Import(E->getLocation()),
  4398. T, E->getValueKind(),
  4399. FoundD,
  4400. /*FIXME:TemplateArgs=*/nullptr);
  4401. if (E->hadMultipleCandidates())
  4402. DRE->setHadMultipleCandidates(true);
  4403. return DRE;
  4404. }
  4405. Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
  4406. QualType T = Importer.Import(E->getType());
  4407. if (T.isNull())
  4408. return nullptr;
  4409. return IntegerLiteral::Create(Importer.getToContext(),
  4410. E->getValue(), T,
  4411. Importer.Import(E->getLocation()));
  4412. }
  4413. Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
  4414. QualType T = Importer.Import(E->getType());
  4415. if (T.isNull())
  4416. return nullptr;
  4417. return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
  4418. E->getKind(), T,
  4419. Importer.Import(E->getLocation()));
  4420. }
  4421. Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
  4422. Expr *SubExpr = Importer.Import(E->getSubExpr());
  4423. if (!SubExpr)
  4424. return nullptr;
  4425. return new (Importer.getToContext())
  4426. ParenExpr(Importer.Import(E->getLParen()),
  4427. Importer.Import(E->getRParen()),
  4428. SubExpr);
  4429. }
  4430. Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
  4431. QualType T = Importer.Import(E->getType());
  4432. if (T.isNull())
  4433. return nullptr;
  4434. Expr *SubExpr = Importer.Import(E->getSubExpr());
  4435. if (!SubExpr)
  4436. return nullptr;
  4437. return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
  4438. T, E->getValueKind(),
  4439. E->getObjectKind(),
  4440. Importer.Import(E->getOperatorLoc()));
  4441. }
  4442. Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
  4443. UnaryExprOrTypeTraitExpr *E) {
  4444. QualType ResultType = Importer.Import(E->getType());
  4445. if (E->isArgumentType()) {
  4446. TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
  4447. if (!TInfo)
  4448. return nullptr;
  4449. return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
  4450. TInfo, ResultType,
  4451. Importer.Import(E->getOperatorLoc()),
  4452. Importer.Import(E->getRParenLoc()));
  4453. }
  4454. Expr *SubExpr = Importer.Import(E->getArgumentExpr());
  4455. if (!SubExpr)
  4456. return nullptr;
  4457. return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
  4458. SubExpr, ResultType,
  4459. Importer.Import(E->getOperatorLoc()),
  4460. Importer.Import(E->getRParenLoc()));
  4461. }
  4462. Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
  4463. QualType T = Importer.Import(E->getType());
  4464. if (T.isNull())
  4465. return nullptr;
  4466. Expr *LHS = Importer.Import(E->getLHS());
  4467. if (!LHS)
  4468. return nullptr;
  4469. Expr *RHS = Importer.Import(E->getRHS());
  4470. if (!RHS)
  4471. return nullptr;
  4472. return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
  4473. T, E->getValueKind(),
  4474. E->getObjectKind(),
  4475. Importer.Import(E->getOperatorLoc()),
  4476. E->isFPContractable());
  4477. }
  4478. Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
  4479. QualType T = Importer.Import(E->getType());
  4480. if (T.isNull())
  4481. return nullptr;
  4482. QualType CompLHSType = Importer.Import(E->getComputationLHSType());
  4483. if (CompLHSType.isNull())
  4484. return nullptr;
  4485. QualType CompResultType = Importer.Import(E->getComputationResultType());
  4486. if (CompResultType.isNull())
  4487. return nullptr;
  4488. Expr *LHS = Importer.Import(E->getLHS());
  4489. if (!LHS)
  4490. return nullptr;
  4491. Expr *RHS = Importer.Import(E->getRHS());
  4492. if (!RHS)
  4493. return nullptr;
  4494. return new (Importer.getToContext())
  4495. CompoundAssignOperator(LHS, RHS, E->getOpcode(),
  4496. T, E->getValueKind(),
  4497. E->getObjectKind(),
  4498. CompLHSType, CompResultType,
  4499. Importer.Import(E->getOperatorLoc()),
  4500. E->isFPContractable());
  4501. }
  4502. static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
  4503. if (E->path_empty()) return false;
  4504. // TODO: import cast paths
  4505. return true;
  4506. }
  4507. Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
  4508. QualType T = Importer.Import(E->getType());
  4509. if (T.isNull())
  4510. return nullptr;
  4511. Expr *SubExpr = Importer.Import(E->getSubExpr());
  4512. if (!SubExpr)
  4513. return nullptr;
  4514. CXXCastPath BasePath;
  4515. if (ImportCastPath(E, BasePath))
  4516. return nullptr;
  4517. return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
  4518. SubExpr, &BasePath, E->getValueKind());
  4519. }
  4520. Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
  4521. QualType T = Importer.Import(E->getType());
  4522. if (T.isNull())
  4523. return nullptr;
  4524. Expr *SubExpr = Importer.Import(E->getSubExpr());
  4525. if (!SubExpr)
  4526. return nullptr;
  4527. TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
  4528. if (!TInfo && E->getTypeInfoAsWritten())
  4529. return nullptr;
  4530. CXXCastPath BasePath;
  4531. if (ImportCastPath(E, BasePath))
  4532. return nullptr;
  4533. return CStyleCastExpr::Create(Importer.getToContext(), T,
  4534. E->getValueKind(), E->getCastKind(),
  4535. SubExpr, &BasePath, TInfo,
  4536. Importer.Import(E->getLParenLoc()),
  4537. Importer.Import(E->getRParenLoc()));
  4538. }
  4539. Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
  4540. QualType T = Importer.Import(E->getType());
  4541. if (T.isNull())
  4542. return nullptr;
  4543. CXXConstructorDecl *ToCCD =
  4544. dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
  4545. if (!ToCCD && E->getConstructor())
  4546. return nullptr;
  4547. size_t NumArgs = E->getNumArgs();
  4548. SmallVector<Expr *, 1> ToArgs(NumArgs);
  4549. ASTImporter &_Importer = Importer;
  4550. std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
  4551. [&_Importer](Expr *AE) -> Expr * {
  4552. return _Importer.Import(AE);
  4553. });
  4554. for (Expr *ToA : ToArgs) {
  4555. if (!ToA)
  4556. return nullptr;
  4557. }
  4558. return CXXConstructExpr::Create(Importer.getToContext(), T,
  4559. Importer.Import(E->getLocation()),
  4560. ToCCD, E->isElidable(),
  4561. ToArgs, E->hadMultipleCandidates(),
  4562. E->isListInitialization(),
  4563. E->isStdInitListInitialization(),
  4564. E->requiresZeroInitialization(),
  4565. E->getConstructionKind(),
  4566. Importer.Import(E->getParenOrBraceRange()));
  4567. }
  4568. Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
  4569. QualType T = Importer.Import(E->getType());
  4570. if (T.isNull())
  4571. return nullptr;
  4572. Expr *ToBase = Importer.Import(E->getBase());
  4573. if (!ToBase && E->getBase())
  4574. return nullptr;
  4575. ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
  4576. if (!ToMember && E->getMemberDecl())
  4577. return nullptr;
  4578. DeclAccessPair ToFoundDecl = DeclAccessPair::make(
  4579. dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
  4580. E->getFoundDecl().getAccess());
  4581. DeclarationNameInfo ToMemberNameInfo(
  4582. Importer.Import(E->getMemberNameInfo().getName()),
  4583. Importer.Import(E->getMemberNameInfo().getLoc()));
  4584. if (E->hasExplicitTemplateArgs()) {
  4585. return nullptr; // FIXME: handle template arguments
  4586. }
  4587. return MemberExpr::Create(Importer.getToContext(), ToBase,
  4588. E->isArrow(),
  4589. Importer.Import(E->getOperatorLoc()),
  4590. Importer.Import(E->getQualifierLoc()),
  4591. Importer.Import(E->getTemplateKeywordLoc()),
  4592. ToMember, ToFoundDecl, ToMemberNameInfo,
  4593. nullptr, T, E->getValueKind(),
  4594. E->getObjectKind());
  4595. }
  4596. Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
  4597. QualType T = Importer.Import(E->getType());
  4598. if (T.isNull())
  4599. return nullptr;
  4600. Expr *ToCallee = Importer.Import(E->getCallee());
  4601. if (!ToCallee && E->getCallee())
  4602. return nullptr;
  4603. unsigned NumArgs = E->getNumArgs();
  4604. llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
  4605. for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
  4606. Expr *FromArg = E->getArg(ai);
  4607. Expr *ToArg = Importer.Import(FromArg);
  4608. if (!ToArg)
  4609. return nullptr;
  4610. ToArgs[ai] = ToArg;
  4611. }
  4612. Expr **ToArgs_Copied = new (Importer.getToContext())
  4613. Expr*[NumArgs];
  4614. for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
  4615. ToArgs_Copied[ai] = ToArgs[ai];
  4616. return new (Importer.getToContext())
  4617. CallExpr(Importer.getToContext(), ToCallee,
  4618. ArrayRef<Expr*>(ToArgs_Copied, NumArgs), T, E->getValueKind(),
  4619. Importer.Import(E->getRParenLoc()));
  4620. }
  4621. ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
  4622. ASTContext &FromContext, FileManager &FromFileManager,
  4623. bool MinimalImport)
  4624. : ToContext(ToContext), FromContext(FromContext),
  4625. ToFileManager(ToFileManager), FromFileManager(FromFileManager),
  4626. Minimal(MinimalImport), LastDiagFromFrom(false)
  4627. {
  4628. ImportedDecls[FromContext.getTranslationUnitDecl()]
  4629. = ToContext.getTranslationUnitDecl();
  4630. }
  4631. ASTImporter::~ASTImporter() { }
  4632. QualType ASTImporter::Import(QualType FromT) {
  4633. if (FromT.isNull())
  4634. return QualType();
  4635. const Type *fromTy = FromT.getTypePtr();
  4636. // Check whether we've already imported this type.
  4637. llvm::DenseMap<const Type *, const Type *>::iterator Pos
  4638. = ImportedTypes.find(fromTy);
  4639. if (Pos != ImportedTypes.end())
  4640. return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
  4641. // Import the type
  4642. ASTNodeImporter Importer(*this);
  4643. QualType ToT = Importer.Visit(fromTy);
  4644. if (ToT.isNull())
  4645. return ToT;
  4646. // Record the imported type.
  4647. ImportedTypes[fromTy] = ToT.getTypePtr();
  4648. return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
  4649. }
  4650. TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
  4651. if (!FromTSI)
  4652. return FromTSI;
  4653. // FIXME: For now we just create a "trivial" type source info based
  4654. // on the type and a single location. Implement a real version of this.
  4655. QualType T = Import(FromTSI->getType());
  4656. if (T.isNull())
  4657. return nullptr;
  4658. return ToContext.getTrivialTypeSourceInfo(T,
  4659. Import(FromTSI->getTypeLoc().getLocStart()));
  4660. }
  4661. Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
  4662. llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
  4663. if (Pos != ImportedDecls.end()) {
  4664. Decl *ToD = Pos->second;
  4665. ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
  4666. return ToD;
  4667. } else {
  4668. return nullptr;
  4669. }
  4670. }
  4671. Decl *ASTImporter::Import(Decl *FromD) {
  4672. if (!FromD)
  4673. return nullptr;
  4674. ASTNodeImporter Importer(*this);
  4675. // Check whether we've already imported this declaration.
  4676. llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
  4677. if (Pos != ImportedDecls.end()) {
  4678. Decl *ToD = Pos->second;
  4679. Importer.ImportDefinitionIfNeeded(FromD, ToD);
  4680. return ToD;
  4681. }
  4682. // Import the type
  4683. Decl *ToD = Importer.Visit(FromD);
  4684. if (!ToD)
  4685. return nullptr;
  4686. // Record the imported declaration.
  4687. ImportedDecls[FromD] = ToD;
  4688. if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
  4689. // Keep track of anonymous tags that have an associated typedef.
  4690. if (FromTag->getTypedefNameForAnonDecl())
  4691. AnonTagsWithPendingTypedefs.push_back(FromTag);
  4692. } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
  4693. // When we've finished transforming a typedef, see whether it was the
  4694. // typedef for an anonymous tag.
  4695. for (SmallVectorImpl<TagDecl *>::iterator
  4696. FromTag = AnonTagsWithPendingTypedefs.begin(),
  4697. FromTagEnd = AnonTagsWithPendingTypedefs.end();
  4698. FromTag != FromTagEnd; ++FromTag) {
  4699. if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
  4700. if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
  4701. // We found the typedef for an anonymous tag; link them.
  4702. ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
  4703. AnonTagsWithPendingTypedefs.erase(FromTag);
  4704. break;
  4705. }
  4706. }
  4707. }
  4708. }
  4709. return ToD;
  4710. }
  4711. DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
  4712. if (!FromDC)
  4713. return FromDC;
  4714. DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
  4715. if (!ToDC)
  4716. return nullptr;
  4717. // When we're using a record/enum/Objective-C class/protocol as a context, we
  4718. // need it to have a definition.
  4719. if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
  4720. RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
  4721. if (ToRecord->isCompleteDefinition()) {
  4722. // Do nothing.
  4723. } else if (FromRecord->isCompleteDefinition()) {
  4724. ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
  4725. ASTNodeImporter::IDK_Basic);
  4726. } else {
  4727. CompleteDecl(ToRecord);
  4728. }
  4729. } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
  4730. EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
  4731. if (ToEnum->isCompleteDefinition()) {
  4732. // Do nothing.
  4733. } else if (FromEnum->isCompleteDefinition()) {
  4734. ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
  4735. ASTNodeImporter::IDK_Basic);
  4736. } else {
  4737. CompleteDecl(ToEnum);
  4738. }
  4739. } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
  4740. ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
  4741. if (ToClass->getDefinition()) {
  4742. // Do nothing.
  4743. } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
  4744. ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
  4745. ASTNodeImporter::IDK_Basic);
  4746. } else {
  4747. CompleteDecl(ToClass);
  4748. }
  4749. } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
  4750. ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
  4751. if (ToProto->getDefinition()) {
  4752. // Do nothing.
  4753. } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
  4754. ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
  4755. ASTNodeImporter::IDK_Basic);
  4756. } else {
  4757. CompleteDecl(ToProto);
  4758. }
  4759. }
  4760. return ToDC;
  4761. }
  4762. Expr *ASTImporter::Import(Expr *FromE) {
  4763. if (!FromE)
  4764. return nullptr;
  4765. return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
  4766. }
  4767. Stmt *ASTImporter::Import(Stmt *FromS) {
  4768. if (!FromS)
  4769. return nullptr;
  4770. // Check whether we've already imported this declaration.
  4771. llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
  4772. if (Pos != ImportedStmts.end())
  4773. return Pos->second;
  4774. // Import the type
  4775. ASTNodeImporter Importer(*this);
  4776. Stmt *ToS = Importer.Visit(FromS);
  4777. if (!ToS)
  4778. return nullptr;
  4779. // Record the imported declaration.
  4780. ImportedStmts[FromS] = ToS;
  4781. return ToS;
  4782. }
  4783. NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
  4784. if (!FromNNS)
  4785. return nullptr;
  4786. NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
  4787. switch (FromNNS->getKind()) {
  4788. case NestedNameSpecifier::Identifier:
  4789. if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
  4790. return NestedNameSpecifier::Create(ToContext, prefix, II);
  4791. }
  4792. return nullptr;
  4793. case NestedNameSpecifier::Namespace:
  4794. if (NamespaceDecl *NS =
  4795. cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
  4796. return NestedNameSpecifier::Create(ToContext, prefix, NS);
  4797. }
  4798. return nullptr;
  4799. case NestedNameSpecifier::NamespaceAlias:
  4800. if (NamespaceAliasDecl *NSAD =
  4801. cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
  4802. return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
  4803. }
  4804. return nullptr;
  4805. case NestedNameSpecifier::Global:
  4806. return NestedNameSpecifier::GlobalSpecifier(ToContext);
  4807. case NestedNameSpecifier::Super:
  4808. if (CXXRecordDecl *RD =
  4809. cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
  4810. return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
  4811. }
  4812. return nullptr;
  4813. case NestedNameSpecifier::TypeSpec:
  4814. case NestedNameSpecifier::TypeSpecWithTemplate: {
  4815. QualType T = Import(QualType(FromNNS->getAsType(), 0u));
  4816. if (!T.isNull()) {
  4817. bool bTemplate = FromNNS->getKind() ==
  4818. NestedNameSpecifier::TypeSpecWithTemplate;
  4819. return NestedNameSpecifier::Create(ToContext, prefix,
  4820. bTemplate, T.getTypePtr());
  4821. }
  4822. }
  4823. return nullptr;
  4824. }
  4825. llvm_unreachable("Invalid nested name specifier kind");
  4826. }
  4827. NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
  4828. // FIXME: Implement!
  4829. return NestedNameSpecifierLoc();
  4830. }
  4831. TemplateName ASTImporter::Import(TemplateName From) {
  4832. switch (From.getKind()) {
  4833. case TemplateName::Template:
  4834. if (TemplateDecl *ToTemplate
  4835. = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
  4836. return TemplateName(ToTemplate);
  4837. return TemplateName();
  4838. case TemplateName::OverloadedTemplate: {
  4839. OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
  4840. UnresolvedSet<2> ToTemplates;
  4841. for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
  4842. E = FromStorage->end();
  4843. I != E; ++I) {
  4844. if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
  4845. ToTemplates.addDecl(To);
  4846. else
  4847. return TemplateName();
  4848. }
  4849. return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
  4850. ToTemplates.end());
  4851. }
  4852. case TemplateName::QualifiedTemplate: {
  4853. QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
  4854. NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
  4855. if (!Qualifier)
  4856. return TemplateName();
  4857. if (TemplateDecl *ToTemplate
  4858. = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
  4859. return ToContext.getQualifiedTemplateName(Qualifier,
  4860. QTN->hasTemplateKeyword(),
  4861. ToTemplate);
  4862. return TemplateName();
  4863. }
  4864. case TemplateName::DependentTemplate: {
  4865. DependentTemplateName *DTN = From.getAsDependentTemplateName();
  4866. NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
  4867. if (!Qualifier)
  4868. return TemplateName();
  4869. if (DTN->isIdentifier()) {
  4870. return ToContext.getDependentTemplateName(Qualifier,
  4871. Import(DTN->getIdentifier()));
  4872. }
  4873. return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
  4874. }
  4875. case TemplateName::SubstTemplateTemplateParm: {
  4876. SubstTemplateTemplateParmStorage *subst
  4877. = From.getAsSubstTemplateTemplateParm();
  4878. TemplateTemplateParmDecl *param
  4879. = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
  4880. if (!param)
  4881. return TemplateName();
  4882. TemplateName replacement = Import(subst->getReplacement());
  4883. if (replacement.isNull()) return TemplateName();
  4884. return ToContext.getSubstTemplateTemplateParm(param, replacement);
  4885. }
  4886. case TemplateName::SubstTemplateTemplateParmPack: {
  4887. SubstTemplateTemplateParmPackStorage *SubstPack
  4888. = From.getAsSubstTemplateTemplateParmPack();
  4889. TemplateTemplateParmDecl *Param
  4890. = cast_or_null<TemplateTemplateParmDecl>(
  4891. Import(SubstPack->getParameterPack()));
  4892. if (!Param)
  4893. return TemplateName();
  4894. ASTNodeImporter Importer(*this);
  4895. TemplateArgument ArgPack
  4896. = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
  4897. if (ArgPack.isNull())
  4898. return TemplateName();
  4899. return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
  4900. }
  4901. }
  4902. llvm_unreachable("Invalid template name kind");
  4903. }
  4904. SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
  4905. if (FromLoc.isInvalid())
  4906. return SourceLocation();
  4907. SourceManager &FromSM = FromContext.getSourceManager();
  4908. // For now, map everything down to its spelling location, so that we
  4909. // don't have to import macro expansions.
  4910. // FIXME: Import macro expansions!
  4911. FromLoc = FromSM.getSpellingLoc(FromLoc);
  4912. std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
  4913. SourceManager &ToSM = ToContext.getSourceManager();
  4914. FileID ToFileID = Import(Decomposed.first);
  4915. if (ToFileID.isInvalid())
  4916. return SourceLocation();
  4917. SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
  4918. .getLocWithOffset(Decomposed.second);
  4919. return ret;
  4920. }
  4921. SourceRange ASTImporter::Import(SourceRange FromRange) {
  4922. return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
  4923. }
  4924. FileID ASTImporter::Import(FileID FromID) {
  4925. llvm::DenseMap<FileID, FileID>::iterator Pos
  4926. = ImportedFileIDs.find(FromID);
  4927. if (Pos != ImportedFileIDs.end())
  4928. return Pos->second;
  4929. SourceManager &FromSM = FromContext.getSourceManager();
  4930. SourceManager &ToSM = ToContext.getSourceManager();
  4931. const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
  4932. assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
  4933. // Include location of this file.
  4934. SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
  4935. // Map the FileID for to the "to" source manager.
  4936. FileID ToID;
  4937. const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
  4938. if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
  4939. // FIXME: We probably want to use getVirtualFile(), so we don't hit the
  4940. // disk again
  4941. // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
  4942. // than mmap the files several times.
  4943. const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
  4944. if (!Entry)
  4945. return FileID();
  4946. ToID = ToSM.createFileID(Entry, ToIncludeLoc,
  4947. FromSLoc.getFile().getFileCharacteristic());
  4948. } else {
  4949. // FIXME: We want to re-use the existing MemoryBuffer!
  4950. const llvm::MemoryBuffer *
  4951. FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
  4952. std::unique_ptr<llvm::MemoryBuffer> ToBuf
  4953. = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
  4954. FromBuf->getBufferIdentifier());
  4955. ToID = ToSM.createFileID(std::move(ToBuf),
  4956. FromSLoc.getFile().getFileCharacteristic());
  4957. }
  4958. ImportedFileIDs[FromID] = ToID;
  4959. return ToID;
  4960. }
  4961. void ASTImporter::ImportDefinition(Decl *From) {
  4962. Decl *To = Import(From);
  4963. if (!To)
  4964. return;
  4965. if (DeclContext *FromDC = cast<DeclContext>(From)) {
  4966. ASTNodeImporter Importer(*this);
  4967. if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
  4968. if (!ToRecord->getDefinition()) {
  4969. Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
  4970. ASTNodeImporter::IDK_Everything);
  4971. return;
  4972. }
  4973. }
  4974. if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
  4975. if (!ToEnum->getDefinition()) {
  4976. Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
  4977. ASTNodeImporter::IDK_Everything);
  4978. return;
  4979. }
  4980. }
  4981. if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
  4982. if (!ToIFace->getDefinition()) {
  4983. Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
  4984. ASTNodeImporter::IDK_Everything);
  4985. return;
  4986. }
  4987. }
  4988. if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
  4989. if (!ToProto->getDefinition()) {
  4990. Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
  4991. ASTNodeImporter::IDK_Everything);
  4992. return;
  4993. }
  4994. }
  4995. Importer.ImportDeclContext(FromDC, true);
  4996. }
  4997. }
  4998. DeclarationName ASTImporter::Import(DeclarationName FromName) {
  4999. if (!FromName)
  5000. return DeclarationName();
  5001. switch (FromName.getNameKind()) {
  5002. case DeclarationName::Identifier:
  5003. return Import(FromName.getAsIdentifierInfo());
  5004. case DeclarationName::ObjCZeroArgSelector:
  5005. case DeclarationName::ObjCOneArgSelector:
  5006. case DeclarationName::ObjCMultiArgSelector:
  5007. return Import(FromName.getObjCSelector());
  5008. case DeclarationName::CXXConstructorName: {
  5009. QualType T = Import(FromName.getCXXNameType());
  5010. if (T.isNull())
  5011. return DeclarationName();
  5012. return ToContext.DeclarationNames.getCXXConstructorName(
  5013. ToContext.getCanonicalType(T));
  5014. }
  5015. case DeclarationName::CXXDestructorName: {
  5016. QualType T = Import(FromName.getCXXNameType());
  5017. if (T.isNull())
  5018. return DeclarationName();
  5019. return ToContext.DeclarationNames.getCXXDestructorName(
  5020. ToContext.getCanonicalType(T));
  5021. }
  5022. case DeclarationName::CXXConversionFunctionName: {
  5023. QualType T = Import(FromName.getCXXNameType());
  5024. if (T.isNull())
  5025. return DeclarationName();
  5026. return ToContext.DeclarationNames.getCXXConversionFunctionName(
  5027. ToContext.getCanonicalType(T));
  5028. }
  5029. case DeclarationName::CXXOperatorName:
  5030. return ToContext.DeclarationNames.getCXXOperatorName(
  5031. FromName.getCXXOverloadedOperator());
  5032. case DeclarationName::CXXLiteralOperatorName:
  5033. return ToContext.DeclarationNames.getCXXLiteralOperatorName(
  5034. Import(FromName.getCXXLiteralIdentifier()));
  5035. case DeclarationName::CXXUsingDirective:
  5036. // FIXME: STATICS!
  5037. return DeclarationName::getUsingDirectiveName();
  5038. }
  5039. llvm_unreachable("Invalid DeclarationName Kind!");
  5040. }
  5041. IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
  5042. if (!FromId)
  5043. return nullptr;
  5044. return &ToContext.Idents.get(FromId->getName());
  5045. }
  5046. Selector ASTImporter::Import(Selector FromSel) {
  5047. if (FromSel.isNull())
  5048. return Selector();
  5049. SmallVector<IdentifierInfo *, 4> Idents;
  5050. Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
  5051. for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
  5052. Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
  5053. return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
  5054. }
  5055. DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
  5056. DeclContext *DC,
  5057. unsigned IDNS,
  5058. NamedDecl **Decls,
  5059. unsigned NumDecls) {
  5060. return Name;
  5061. }
  5062. DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
  5063. if (LastDiagFromFrom)
  5064. ToContext.getDiagnostics().notePriorDiagnosticFrom(
  5065. FromContext.getDiagnostics());
  5066. LastDiagFromFrom = false;
  5067. return ToContext.getDiagnostics().Report(Loc, DiagID);
  5068. }
  5069. DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
  5070. if (!LastDiagFromFrom)
  5071. FromContext.getDiagnostics().notePriorDiagnosticFrom(
  5072. ToContext.getDiagnostics());
  5073. LastDiagFromFrom = true;
  5074. return FromContext.getDiagnostics().Report(Loc, DiagID);
  5075. }
  5076. void ASTImporter::CompleteDecl (Decl *D) {
  5077. if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
  5078. if (!ID->getDefinition())
  5079. ID->startDefinition();
  5080. }
  5081. else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
  5082. if (!PD->getDefinition())
  5083. PD->startDefinition();
  5084. }
  5085. else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
  5086. if (!TD->getDefinition() && !TD->isBeingDefined()) {
  5087. TD->startDefinition();
  5088. TD->setCompleteDefinition(true);
  5089. }
  5090. }
  5091. else {
  5092. assert (0 && "CompleteDecl called on a Decl that can't be completed");
  5093. }
  5094. }
  5095. Decl *ASTImporter::Imported(Decl *From, Decl *To) {
  5096. ImportedDecls[From] = To;
  5097. return To;
  5098. }
  5099. bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
  5100. bool Complain) {
  5101. llvm::DenseMap<const Type *, const Type *>::iterator Pos
  5102. = ImportedTypes.find(From.getTypePtr());
  5103. if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
  5104. return true;
  5105. StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
  5106. false, Complain);
  5107. return Ctx.IsStructurallyEquivalent(From, To);
  5108. }