ExprCXX.h 181 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ExprCXX.h - Classes for representing expressions ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// Defines the clang::Expr interface and subclasses for C++ expressions.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_EXPRCXX_H
  19. #define LLVM_CLANG_AST_EXPRCXX_H
  20. #include "clang/AST/ASTConcept.h"
  21. #include "clang/AST/ComputeDependence.h"
  22. #include "clang/AST/Decl.h"
  23. #include "clang/AST/DeclBase.h"
  24. #include "clang/AST/DeclCXX.h"
  25. #include "clang/AST/DeclTemplate.h"
  26. #include "clang/AST/DeclarationName.h"
  27. #include "clang/AST/DependenceFlags.h"
  28. #include "clang/AST/Expr.h"
  29. #include "clang/AST/NestedNameSpecifier.h"
  30. #include "clang/AST/OperationKinds.h"
  31. #include "clang/AST/Stmt.h"
  32. #include "clang/AST/StmtCXX.h"
  33. #include "clang/AST/TemplateBase.h"
  34. #include "clang/AST/Type.h"
  35. #include "clang/AST/UnresolvedSet.h"
  36. #include "clang/Basic/ExceptionSpecificationType.h"
  37. #include "clang/Basic/ExpressionTraits.h"
  38. #include "clang/Basic/LLVM.h"
  39. #include "clang/Basic/Lambda.h"
  40. #include "clang/Basic/LangOptions.h"
  41. #include "clang/Basic/OperatorKinds.h"
  42. #include "clang/Basic/SourceLocation.h"
  43. #include "clang/Basic/Specifiers.h"
  44. #include "clang/Basic/TypeTraits.h"
  45. #include "llvm/ADT/ArrayRef.h"
  46. #include "llvm/ADT/PointerUnion.h"
  47. #include "llvm/ADT/StringRef.h"
  48. #include "llvm/ADT/iterator_range.h"
  49. #include "llvm/Support/Casting.h"
  50. #include "llvm/Support/Compiler.h"
  51. #include "llvm/Support/TrailingObjects.h"
  52. #include <cassert>
  53. #include <cstddef>
  54. #include <cstdint>
  55. #include <memory>
  56. #include <optional>
  57. namespace clang {
  58. class ASTContext;
  59. class DeclAccessPair;
  60. class IdentifierInfo;
  61. class LambdaCapture;
  62. class NonTypeTemplateParmDecl;
  63. class TemplateParameterList;
  64. //===--------------------------------------------------------------------===//
  65. // C++ Expressions.
  66. //===--------------------------------------------------------------------===//
  67. /// A call to an overloaded operator written using operator
  68. /// syntax.
  69. ///
  70. /// Represents a call to an overloaded operator written using operator
  71. /// syntax, e.g., "x + y" or "*p". While semantically equivalent to a
  72. /// normal call, this AST node provides better information about the
  73. /// syntactic representation of the call.
  74. ///
  75. /// In a C++ template, this expression node kind will be used whenever
  76. /// any of the arguments are type-dependent. In this case, the
  77. /// function itself will be a (possibly empty) set of functions and
  78. /// function templates that were found by name lookup at template
  79. /// definition time.
  80. class CXXOperatorCallExpr final : public CallExpr {
  81. friend class ASTStmtReader;
  82. friend class ASTStmtWriter;
  83. SourceRange Range;
  84. // CXXOperatorCallExpr has some trailing objects belonging
  85. // to CallExpr. See CallExpr for the details.
  86. SourceRange getSourceRangeImpl() const LLVM_READONLY;
  87. CXXOperatorCallExpr(OverloadedOperatorKind OpKind, Expr *Fn,
  88. ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
  89. SourceLocation OperatorLoc, FPOptionsOverride FPFeatures,
  90. ADLCallKind UsesADL);
  91. CXXOperatorCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
  92. public:
  93. static CXXOperatorCallExpr *
  94. Create(const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn,
  95. ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
  96. SourceLocation OperatorLoc, FPOptionsOverride FPFeatures,
  97. ADLCallKind UsesADL = NotADL);
  98. static CXXOperatorCallExpr *CreateEmpty(const ASTContext &Ctx,
  99. unsigned NumArgs, bool HasFPFeatures,
  100. EmptyShell Empty);
  101. /// Returns the kind of overloaded operator that this expression refers to.
  102. OverloadedOperatorKind getOperator() const {
  103. return static_cast<OverloadedOperatorKind>(
  104. CXXOperatorCallExprBits.OperatorKind);
  105. }
  106. static bool isAssignmentOp(OverloadedOperatorKind Opc) {
  107. return Opc == OO_Equal || Opc == OO_StarEqual || Opc == OO_SlashEqual ||
  108. Opc == OO_PercentEqual || Opc == OO_PlusEqual ||
  109. Opc == OO_MinusEqual || Opc == OO_LessLessEqual ||
  110. Opc == OO_GreaterGreaterEqual || Opc == OO_AmpEqual ||
  111. Opc == OO_CaretEqual || Opc == OO_PipeEqual;
  112. }
  113. bool isAssignmentOp() const { return isAssignmentOp(getOperator()); }
  114. static bool isComparisonOp(OverloadedOperatorKind Opc) {
  115. switch (Opc) {
  116. case OO_EqualEqual:
  117. case OO_ExclaimEqual:
  118. case OO_Greater:
  119. case OO_GreaterEqual:
  120. case OO_Less:
  121. case OO_LessEqual:
  122. case OO_Spaceship:
  123. return true;
  124. default:
  125. return false;
  126. }
  127. }
  128. bool isComparisonOp() const { return isComparisonOp(getOperator()); }
  129. /// Is this written as an infix binary operator?
  130. bool isInfixBinaryOp() const;
  131. /// Returns the location of the operator symbol in the expression.
  132. ///
  133. /// When \c getOperator()==OO_Call, this is the location of the right
  134. /// parentheses; when \c getOperator()==OO_Subscript, this is the location
  135. /// of the right bracket.
  136. SourceLocation getOperatorLoc() const { return getRParenLoc(); }
  137. SourceLocation getExprLoc() const LLVM_READONLY {
  138. OverloadedOperatorKind Operator = getOperator();
  139. return (Operator < OO_Plus || Operator >= OO_Arrow ||
  140. Operator == OO_PlusPlus || Operator == OO_MinusMinus)
  141. ? getBeginLoc()
  142. : getOperatorLoc();
  143. }
  144. SourceLocation getBeginLoc() const { return Range.getBegin(); }
  145. SourceLocation getEndLoc() const { return Range.getEnd(); }
  146. SourceRange getSourceRange() const { return Range; }
  147. static bool classof(const Stmt *T) {
  148. return T->getStmtClass() == CXXOperatorCallExprClass;
  149. }
  150. };
  151. /// Represents a call to a member function that
  152. /// may be written either with member call syntax (e.g., "obj.func()"
  153. /// or "objptr->func()") or with normal function-call syntax
  154. /// ("func()") within a member function that ends up calling a member
  155. /// function. The callee in either case is a MemberExpr that contains
  156. /// both the object argument and the member function, while the
  157. /// arguments are the arguments within the parentheses (not including
  158. /// the object argument).
  159. class CXXMemberCallExpr final : public CallExpr {
  160. // CXXMemberCallExpr has some trailing objects belonging
  161. // to CallExpr. See CallExpr for the details.
  162. CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty,
  163. ExprValueKind VK, SourceLocation RP,
  164. FPOptionsOverride FPOptions, unsigned MinNumArgs);
  165. CXXMemberCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
  166. public:
  167. static CXXMemberCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
  168. ArrayRef<Expr *> Args, QualType Ty,
  169. ExprValueKind VK, SourceLocation RP,
  170. FPOptionsOverride FPFeatures,
  171. unsigned MinNumArgs = 0);
  172. static CXXMemberCallExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs,
  173. bool HasFPFeatures, EmptyShell Empty);
  174. /// Retrieve the implicit object argument for the member call.
  175. ///
  176. /// For example, in "x.f(5)", this returns the sub-expression "x".
  177. Expr *getImplicitObjectArgument() const;
  178. /// Retrieve the type of the object argument.
  179. ///
  180. /// Note that this always returns a non-pointer type.
  181. QualType getObjectType() const;
  182. /// Retrieve the declaration of the called method.
  183. CXXMethodDecl *getMethodDecl() const;
  184. /// Retrieve the CXXRecordDecl for the underlying type of
  185. /// the implicit object argument.
  186. ///
  187. /// Note that this is may not be the same declaration as that of the class
  188. /// context of the CXXMethodDecl which this function is calling.
  189. /// FIXME: Returns 0 for member pointer call exprs.
  190. CXXRecordDecl *getRecordDecl() const;
  191. SourceLocation getExprLoc() const LLVM_READONLY {
  192. SourceLocation CLoc = getCallee()->getExprLoc();
  193. if (CLoc.isValid())
  194. return CLoc;
  195. return getBeginLoc();
  196. }
  197. static bool classof(const Stmt *T) {
  198. return T->getStmtClass() == CXXMemberCallExprClass;
  199. }
  200. };
  201. /// Represents a call to a CUDA kernel function.
  202. class CUDAKernelCallExpr final : public CallExpr {
  203. friend class ASTStmtReader;
  204. enum { CONFIG, END_PREARG };
  205. // CUDAKernelCallExpr has some trailing objects belonging
  206. // to CallExpr. See CallExpr for the details.
  207. CUDAKernelCallExpr(Expr *Fn, CallExpr *Config, ArrayRef<Expr *> Args,
  208. QualType Ty, ExprValueKind VK, SourceLocation RP,
  209. FPOptionsOverride FPFeatures, unsigned MinNumArgs);
  210. CUDAKernelCallExpr(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
  211. public:
  212. static CUDAKernelCallExpr *Create(const ASTContext &Ctx, Expr *Fn,
  213. CallExpr *Config, ArrayRef<Expr *> Args,
  214. QualType Ty, ExprValueKind VK,
  215. SourceLocation RP,
  216. FPOptionsOverride FPFeatures,
  217. unsigned MinNumArgs = 0);
  218. static CUDAKernelCallExpr *CreateEmpty(const ASTContext &Ctx,
  219. unsigned NumArgs, bool HasFPFeatures,
  220. EmptyShell Empty);
  221. const CallExpr *getConfig() const {
  222. return cast_or_null<CallExpr>(getPreArg(CONFIG));
  223. }
  224. CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); }
  225. static bool classof(const Stmt *T) {
  226. return T->getStmtClass() == CUDAKernelCallExprClass;
  227. }
  228. };
  229. /// A rewritten comparison expression that was originally written using
  230. /// operator syntax.
  231. ///
  232. /// In C++20, the following rewrites are performed:
  233. /// - <tt>a == b</tt> -> <tt>b == a</tt>
  234. /// - <tt>a != b</tt> -> <tt>!(a == b)</tt>
  235. /// - <tt>a != b</tt> -> <tt>!(b == a)</tt>
  236. /// - For \c \@ in \c <, \c <=, \c >, \c >=, \c <=>:
  237. /// - <tt>a @ b</tt> -> <tt>(a <=> b) @ 0</tt>
  238. /// - <tt>a @ b</tt> -> <tt>0 @ (b <=> a)</tt>
  239. ///
  240. /// This expression provides access to both the original syntax and the
  241. /// rewritten expression.
  242. ///
  243. /// Note that the rewritten calls to \c ==, \c <=>, and \c \@ are typically
  244. /// \c CXXOperatorCallExprs, but could theoretically be \c BinaryOperators.
  245. class CXXRewrittenBinaryOperator : public Expr {
  246. friend class ASTStmtReader;
  247. /// The rewritten semantic form.
  248. Stmt *SemanticForm;
  249. public:
  250. CXXRewrittenBinaryOperator(Expr *SemanticForm, bool IsReversed)
  251. : Expr(CXXRewrittenBinaryOperatorClass, SemanticForm->getType(),
  252. SemanticForm->getValueKind(), SemanticForm->getObjectKind()),
  253. SemanticForm(SemanticForm) {
  254. CXXRewrittenBinaryOperatorBits.IsReversed = IsReversed;
  255. setDependence(computeDependence(this));
  256. }
  257. CXXRewrittenBinaryOperator(EmptyShell Empty)
  258. : Expr(CXXRewrittenBinaryOperatorClass, Empty), SemanticForm() {}
  259. /// Get an equivalent semantic form for this expression.
  260. Expr *getSemanticForm() { return cast<Expr>(SemanticForm); }
  261. const Expr *getSemanticForm() const { return cast<Expr>(SemanticForm); }
  262. struct DecomposedForm {
  263. /// The original opcode, prior to rewriting.
  264. BinaryOperatorKind Opcode;
  265. /// The original left-hand side.
  266. const Expr *LHS;
  267. /// The original right-hand side.
  268. const Expr *RHS;
  269. /// The inner \c == or \c <=> operator expression.
  270. const Expr *InnerBinOp;
  271. };
  272. /// Decompose this operator into its syntactic form.
  273. DecomposedForm getDecomposedForm() const LLVM_READONLY;
  274. /// Determine whether this expression was rewritten in reverse form.
  275. bool isReversed() const { return CXXRewrittenBinaryOperatorBits.IsReversed; }
  276. BinaryOperatorKind getOperator() const { return getDecomposedForm().Opcode; }
  277. BinaryOperatorKind getOpcode() const { return getOperator(); }
  278. static StringRef getOpcodeStr(BinaryOperatorKind Op) {
  279. return BinaryOperator::getOpcodeStr(Op);
  280. }
  281. StringRef getOpcodeStr() const {
  282. return BinaryOperator::getOpcodeStr(getOpcode());
  283. }
  284. bool isComparisonOp() const { return true; }
  285. bool isAssignmentOp() const { return false; }
  286. const Expr *getLHS() const { return getDecomposedForm().LHS; }
  287. const Expr *getRHS() const { return getDecomposedForm().RHS; }
  288. SourceLocation getOperatorLoc() const LLVM_READONLY {
  289. return getDecomposedForm().InnerBinOp->getExprLoc();
  290. }
  291. SourceLocation getExprLoc() const LLVM_READONLY { return getOperatorLoc(); }
  292. /// Compute the begin and end locations from the decomposed form.
  293. /// The locations of the semantic form are not reliable if this is
  294. /// a reversed expression.
  295. //@{
  296. SourceLocation getBeginLoc() const LLVM_READONLY {
  297. return getDecomposedForm().LHS->getBeginLoc();
  298. }
  299. SourceLocation getEndLoc() const LLVM_READONLY {
  300. return getDecomposedForm().RHS->getEndLoc();
  301. }
  302. SourceRange getSourceRange() const LLVM_READONLY {
  303. DecomposedForm DF = getDecomposedForm();
  304. return SourceRange(DF.LHS->getBeginLoc(), DF.RHS->getEndLoc());
  305. }
  306. //@}
  307. child_range children() {
  308. return child_range(&SemanticForm, &SemanticForm + 1);
  309. }
  310. static bool classof(const Stmt *T) {
  311. return T->getStmtClass() == CXXRewrittenBinaryOperatorClass;
  312. }
  313. };
  314. /// Abstract class common to all of the C++ "named"/"keyword" casts.
  315. ///
  316. /// This abstract class is inherited by all of the classes
  317. /// representing "named" casts: CXXStaticCastExpr for \c static_cast,
  318. /// CXXDynamicCastExpr for \c dynamic_cast, CXXReinterpretCastExpr for
  319. /// reinterpret_cast, CXXConstCastExpr for \c const_cast and
  320. /// CXXAddrspaceCastExpr for addrspace_cast (in OpenCL).
  321. class CXXNamedCastExpr : public ExplicitCastExpr {
  322. private:
  323. // the location of the casting op
  324. SourceLocation Loc;
  325. // the location of the right parenthesis
  326. SourceLocation RParenLoc;
  327. // range for '<' '>'
  328. SourceRange AngleBrackets;
  329. protected:
  330. friend class ASTStmtReader;
  331. CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK, CastKind kind,
  332. Expr *op, unsigned PathSize, bool HasFPFeatures,
  333. TypeSourceInfo *writtenTy, SourceLocation l,
  334. SourceLocation RParenLoc, SourceRange AngleBrackets)
  335. : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, HasFPFeatures,
  336. writtenTy),
  337. Loc(l), RParenLoc(RParenLoc), AngleBrackets(AngleBrackets) {}
  338. explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize,
  339. bool HasFPFeatures)
  340. : ExplicitCastExpr(SC, Shell, PathSize, HasFPFeatures) {}
  341. public:
  342. const char *getCastName() const;
  343. /// Retrieve the location of the cast operator keyword, e.g.,
  344. /// \c static_cast.
  345. SourceLocation getOperatorLoc() const { return Loc; }
  346. /// Retrieve the location of the closing parenthesis.
  347. SourceLocation getRParenLoc() const { return RParenLoc; }
  348. SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
  349. SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
  350. SourceRange getAngleBrackets() const LLVM_READONLY { return AngleBrackets; }
  351. static bool classof(const Stmt *T) {
  352. switch (T->getStmtClass()) {
  353. case CXXStaticCastExprClass:
  354. case CXXDynamicCastExprClass:
  355. case CXXReinterpretCastExprClass:
  356. case CXXConstCastExprClass:
  357. case CXXAddrspaceCastExprClass:
  358. return true;
  359. default:
  360. return false;
  361. }
  362. }
  363. };
  364. /// A C++ \c static_cast expression (C++ [expr.static.cast]).
  365. ///
  366. /// This expression node represents a C++ static cast, e.g.,
  367. /// \c static_cast<int>(1.0).
  368. class CXXStaticCastExpr final
  369. : public CXXNamedCastExpr,
  370. private llvm::TrailingObjects<CXXStaticCastExpr, CXXBaseSpecifier *,
  371. FPOptionsOverride> {
  372. CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op,
  373. unsigned pathSize, TypeSourceInfo *writtenTy,
  374. FPOptionsOverride FPO, SourceLocation l,
  375. SourceLocation RParenLoc, SourceRange AngleBrackets)
  376. : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize,
  377. FPO.requiresTrailingStorage(), writtenTy, l, RParenLoc,
  378. AngleBrackets) {
  379. if (hasStoredFPFeatures())
  380. *getTrailingFPFeatures() = FPO;
  381. }
  382. explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize,
  383. bool HasFPFeatures)
  384. : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize,
  385. HasFPFeatures) {}
  386. unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const {
  387. return path_size();
  388. }
  389. public:
  390. friend class CastExpr;
  391. friend TrailingObjects;
  392. static CXXStaticCastExpr *
  393. Create(const ASTContext &Context, QualType T, ExprValueKind VK, CastKind K,
  394. Expr *Op, const CXXCastPath *Path, TypeSourceInfo *Written,
  395. FPOptionsOverride FPO, SourceLocation L, SourceLocation RParenLoc,
  396. SourceRange AngleBrackets);
  397. static CXXStaticCastExpr *CreateEmpty(const ASTContext &Context,
  398. unsigned PathSize, bool hasFPFeatures);
  399. static bool classof(const Stmt *T) {
  400. return T->getStmtClass() == CXXStaticCastExprClass;
  401. }
  402. };
  403. /// A C++ @c dynamic_cast expression (C++ [expr.dynamic.cast]).
  404. ///
  405. /// This expression node represents a dynamic cast, e.g.,
  406. /// \c dynamic_cast<Derived*>(BasePtr). Such a cast may perform a run-time
  407. /// check to determine how to perform the type conversion.
  408. class CXXDynamicCastExpr final
  409. : public CXXNamedCastExpr,
  410. private llvm::TrailingObjects<CXXDynamicCastExpr, CXXBaseSpecifier *> {
  411. CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind, Expr *op,
  412. unsigned pathSize, TypeSourceInfo *writtenTy,
  413. SourceLocation l, SourceLocation RParenLoc,
  414. SourceRange AngleBrackets)
  415. : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize,
  416. /*HasFPFeatures*/ false, writtenTy, l, RParenLoc,
  417. AngleBrackets) {}
  418. explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize)
  419. : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize,
  420. /*HasFPFeatures*/ false) {}
  421. public:
  422. friend class CastExpr;
  423. friend TrailingObjects;
  424. static CXXDynamicCastExpr *Create(const ASTContext &Context, QualType T,
  425. ExprValueKind VK, CastKind Kind, Expr *Op,
  426. const CXXCastPath *Path,
  427. TypeSourceInfo *Written, SourceLocation L,
  428. SourceLocation RParenLoc,
  429. SourceRange AngleBrackets);
  430. static CXXDynamicCastExpr *CreateEmpty(const ASTContext &Context,
  431. unsigned pathSize);
  432. bool isAlwaysNull() const;
  433. static bool classof(const Stmt *T) {
  434. return T->getStmtClass() == CXXDynamicCastExprClass;
  435. }
  436. };
  437. /// A C++ @c reinterpret_cast expression (C++ [expr.reinterpret.cast]).
  438. ///
  439. /// This expression node represents a reinterpret cast, e.g.,
  440. /// @c reinterpret_cast<int>(VoidPtr).
  441. ///
  442. /// A reinterpret_cast provides a differently-typed view of a value but
  443. /// (in Clang, as in most C++ implementations) performs no actual work at
  444. /// run time.
  445. class CXXReinterpretCastExpr final
  446. : public CXXNamedCastExpr,
  447. private llvm::TrailingObjects<CXXReinterpretCastExpr,
  448. CXXBaseSpecifier *> {
  449. CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op,
  450. unsigned pathSize, TypeSourceInfo *writtenTy,
  451. SourceLocation l, SourceLocation RParenLoc,
  452. SourceRange AngleBrackets)
  453. : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op,
  454. pathSize, /*HasFPFeatures*/ false, writtenTy, l,
  455. RParenLoc, AngleBrackets) {}
  456. CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize)
  457. : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize,
  458. /*HasFPFeatures*/ false) {}
  459. public:
  460. friend class CastExpr;
  461. friend TrailingObjects;
  462. static CXXReinterpretCastExpr *Create(const ASTContext &Context, QualType T,
  463. ExprValueKind VK, CastKind Kind,
  464. Expr *Op, const CXXCastPath *Path,
  465. TypeSourceInfo *WrittenTy, SourceLocation L,
  466. SourceLocation RParenLoc,
  467. SourceRange AngleBrackets);
  468. static CXXReinterpretCastExpr *CreateEmpty(const ASTContext &Context,
  469. unsigned pathSize);
  470. static bool classof(const Stmt *T) {
  471. return T->getStmtClass() == CXXReinterpretCastExprClass;
  472. }
  473. };
  474. /// A C++ \c const_cast expression (C++ [expr.const.cast]).
  475. ///
  476. /// This expression node represents a const cast, e.g.,
  477. /// \c const_cast<char*>(PtrToConstChar).
  478. ///
  479. /// A const_cast can remove type qualifiers but does not change the underlying
  480. /// value.
  481. class CXXConstCastExpr final
  482. : public CXXNamedCastExpr,
  483. private llvm::TrailingObjects<CXXConstCastExpr, CXXBaseSpecifier *> {
  484. CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op,
  485. TypeSourceInfo *writtenTy, SourceLocation l,
  486. SourceLocation RParenLoc, SourceRange AngleBrackets)
  487. : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op, 0,
  488. /*HasFPFeatures*/ false, writtenTy, l, RParenLoc,
  489. AngleBrackets) {}
  490. explicit CXXConstCastExpr(EmptyShell Empty)
  491. : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0,
  492. /*HasFPFeatures*/ false) {}
  493. public:
  494. friend class CastExpr;
  495. friend TrailingObjects;
  496. static CXXConstCastExpr *Create(const ASTContext &Context, QualType T,
  497. ExprValueKind VK, Expr *Op,
  498. TypeSourceInfo *WrittenTy, SourceLocation L,
  499. SourceLocation RParenLoc,
  500. SourceRange AngleBrackets);
  501. static CXXConstCastExpr *CreateEmpty(const ASTContext &Context);
  502. static bool classof(const Stmt *T) {
  503. return T->getStmtClass() == CXXConstCastExprClass;
  504. }
  505. };
  506. /// A C++ addrspace_cast expression (currently only enabled for OpenCL).
  507. ///
  508. /// This expression node represents a cast between pointers to objects in
  509. /// different address spaces e.g.,
  510. /// \c addrspace_cast<global int*>(PtrToGenericInt).
  511. ///
  512. /// A addrspace_cast can cast address space type qualifiers but does not change
  513. /// the underlying value.
  514. class CXXAddrspaceCastExpr final
  515. : public CXXNamedCastExpr,
  516. private llvm::TrailingObjects<CXXAddrspaceCastExpr, CXXBaseSpecifier *> {
  517. CXXAddrspaceCastExpr(QualType ty, ExprValueKind VK, CastKind Kind, Expr *op,
  518. TypeSourceInfo *writtenTy, SourceLocation l,
  519. SourceLocation RParenLoc, SourceRange AngleBrackets)
  520. : CXXNamedCastExpr(CXXAddrspaceCastExprClass, ty, VK, Kind, op, 0,
  521. /*HasFPFeatures*/ false, writtenTy, l, RParenLoc,
  522. AngleBrackets) {}
  523. explicit CXXAddrspaceCastExpr(EmptyShell Empty)
  524. : CXXNamedCastExpr(CXXAddrspaceCastExprClass, Empty, 0,
  525. /*HasFPFeatures*/ false) {}
  526. public:
  527. friend class CastExpr;
  528. friend TrailingObjects;
  529. static CXXAddrspaceCastExpr *
  530. Create(const ASTContext &Context, QualType T, ExprValueKind VK, CastKind Kind,
  531. Expr *Op, TypeSourceInfo *WrittenTy, SourceLocation L,
  532. SourceLocation RParenLoc, SourceRange AngleBrackets);
  533. static CXXAddrspaceCastExpr *CreateEmpty(const ASTContext &Context);
  534. static bool classof(const Stmt *T) {
  535. return T->getStmtClass() == CXXAddrspaceCastExprClass;
  536. }
  537. };
  538. /// A call to a literal operator (C++11 [over.literal])
  539. /// written as a user-defined literal (C++11 [lit.ext]).
  540. ///
  541. /// Represents a user-defined literal, e.g. "foo"_bar or 1.23_xyz. While this
  542. /// is semantically equivalent to a normal call, this AST node provides better
  543. /// information about the syntactic representation of the literal.
  544. ///
  545. /// Since literal operators are never found by ADL and can only be declared at
  546. /// namespace scope, a user-defined literal is never dependent.
  547. class UserDefinedLiteral final : public CallExpr {
  548. friend class ASTStmtReader;
  549. friend class ASTStmtWriter;
  550. /// The location of a ud-suffix within the literal.
  551. SourceLocation UDSuffixLoc;
  552. // UserDefinedLiteral has some trailing objects belonging
  553. // to CallExpr. See CallExpr for the details.
  554. UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty,
  555. ExprValueKind VK, SourceLocation LitEndLoc,
  556. SourceLocation SuffixLoc, FPOptionsOverride FPFeatures);
  557. UserDefinedLiteral(unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty);
  558. public:
  559. static UserDefinedLiteral *Create(const ASTContext &Ctx, Expr *Fn,
  560. ArrayRef<Expr *> Args, QualType Ty,
  561. ExprValueKind VK, SourceLocation LitEndLoc,
  562. SourceLocation SuffixLoc,
  563. FPOptionsOverride FPFeatures);
  564. static UserDefinedLiteral *CreateEmpty(const ASTContext &Ctx,
  565. unsigned NumArgs, bool HasFPOptions,
  566. EmptyShell Empty);
  567. /// The kind of literal operator which is invoked.
  568. enum LiteralOperatorKind {
  569. /// Raw form: operator "" X (const char *)
  570. LOK_Raw,
  571. /// Raw form: operator "" X<cs...> ()
  572. LOK_Template,
  573. /// operator "" X (unsigned long long)
  574. LOK_Integer,
  575. /// operator "" X (long double)
  576. LOK_Floating,
  577. /// operator "" X (const CharT *, size_t)
  578. LOK_String,
  579. /// operator "" X (CharT)
  580. LOK_Character
  581. };
  582. /// Returns the kind of literal operator invocation
  583. /// which this expression represents.
  584. LiteralOperatorKind getLiteralOperatorKind() const;
  585. /// If this is not a raw user-defined literal, get the
  586. /// underlying cooked literal (representing the literal with the suffix
  587. /// removed).
  588. Expr *getCookedLiteral();
  589. const Expr *getCookedLiteral() const {
  590. return const_cast<UserDefinedLiteral*>(this)->getCookedLiteral();
  591. }
  592. SourceLocation getBeginLoc() const {
  593. if (getLiteralOperatorKind() == LOK_Template)
  594. return getRParenLoc();
  595. return getArg(0)->getBeginLoc();
  596. }
  597. SourceLocation getEndLoc() const { return getRParenLoc(); }
  598. /// Returns the location of a ud-suffix in the expression.
  599. ///
  600. /// For a string literal, there may be multiple identical suffixes. This
  601. /// returns the first.
  602. SourceLocation getUDSuffixLoc() const { return UDSuffixLoc; }
  603. /// Returns the ud-suffix specified for this literal.
  604. const IdentifierInfo *getUDSuffix() const;
  605. static bool classof(const Stmt *S) {
  606. return S->getStmtClass() == UserDefinedLiteralClass;
  607. }
  608. };
  609. /// A boolean literal, per ([C++ lex.bool] Boolean literals).
  610. class CXXBoolLiteralExpr : public Expr {
  611. public:
  612. CXXBoolLiteralExpr(bool Val, QualType Ty, SourceLocation Loc)
  613. : Expr(CXXBoolLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) {
  614. CXXBoolLiteralExprBits.Value = Val;
  615. CXXBoolLiteralExprBits.Loc = Loc;
  616. setDependence(ExprDependence::None);
  617. }
  618. explicit CXXBoolLiteralExpr(EmptyShell Empty)
  619. : Expr(CXXBoolLiteralExprClass, Empty) {}
  620. static CXXBoolLiteralExpr *Create(const ASTContext &C, bool Val, QualType Ty,
  621. SourceLocation Loc) {
  622. return new (C) CXXBoolLiteralExpr(Val, Ty, Loc);
  623. }
  624. bool getValue() const { return CXXBoolLiteralExprBits.Value; }
  625. void setValue(bool V) { CXXBoolLiteralExprBits.Value = V; }
  626. SourceLocation getBeginLoc() const { return getLocation(); }
  627. SourceLocation getEndLoc() const { return getLocation(); }
  628. SourceLocation getLocation() const { return CXXBoolLiteralExprBits.Loc; }
  629. void setLocation(SourceLocation L) { CXXBoolLiteralExprBits.Loc = L; }
  630. static bool classof(const Stmt *T) {
  631. return T->getStmtClass() == CXXBoolLiteralExprClass;
  632. }
  633. // Iterators
  634. child_range children() {
  635. return child_range(child_iterator(), child_iterator());
  636. }
  637. const_child_range children() const {
  638. return const_child_range(const_child_iterator(), const_child_iterator());
  639. }
  640. };
  641. /// The null pointer literal (C++11 [lex.nullptr])
  642. ///
  643. /// Introduced in C++11, the only literal of type \c nullptr_t is \c nullptr.
  644. /// This also implements the null pointer literal in C2x (C2x 6.4.1) which is
  645. /// intended to have the same semantics as the feature in C++.
  646. class CXXNullPtrLiteralExpr : public Expr {
  647. public:
  648. CXXNullPtrLiteralExpr(QualType Ty, SourceLocation Loc)
  649. : Expr(CXXNullPtrLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) {
  650. CXXNullPtrLiteralExprBits.Loc = Loc;
  651. setDependence(ExprDependence::None);
  652. }
  653. explicit CXXNullPtrLiteralExpr(EmptyShell Empty)
  654. : Expr(CXXNullPtrLiteralExprClass, Empty) {}
  655. SourceLocation getBeginLoc() const { return getLocation(); }
  656. SourceLocation getEndLoc() const { return getLocation(); }
  657. SourceLocation getLocation() const { return CXXNullPtrLiteralExprBits.Loc; }
  658. void setLocation(SourceLocation L) { CXXNullPtrLiteralExprBits.Loc = L; }
  659. static bool classof(const Stmt *T) {
  660. return T->getStmtClass() == CXXNullPtrLiteralExprClass;
  661. }
  662. child_range children() {
  663. return child_range(child_iterator(), child_iterator());
  664. }
  665. const_child_range children() const {
  666. return const_child_range(const_child_iterator(), const_child_iterator());
  667. }
  668. };
  669. /// Implicit construction of a std::initializer_list<T> object from an
  670. /// array temporary within list-initialization (C++11 [dcl.init.list]p5).
  671. class CXXStdInitializerListExpr : public Expr {
  672. Stmt *SubExpr = nullptr;
  673. CXXStdInitializerListExpr(EmptyShell Empty)
  674. : Expr(CXXStdInitializerListExprClass, Empty) {}
  675. public:
  676. friend class ASTReader;
  677. friend class ASTStmtReader;
  678. CXXStdInitializerListExpr(QualType Ty, Expr *SubExpr)
  679. : Expr(CXXStdInitializerListExprClass, Ty, VK_PRValue, OK_Ordinary),
  680. SubExpr(SubExpr) {
  681. setDependence(computeDependence(this));
  682. }
  683. Expr *getSubExpr() { return static_cast<Expr*>(SubExpr); }
  684. const Expr *getSubExpr() const { return static_cast<const Expr*>(SubExpr); }
  685. SourceLocation getBeginLoc() const LLVM_READONLY {
  686. return SubExpr->getBeginLoc();
  687. }
  688. SourceLocation getEndLoc() const LLVM_READONLY {
  689. return SubExpr->getEndLoc();
  690. }
  691. /// Retrieve the source range of the expression.
  692. SourceRange getSourceRange() const LLVM_READONLY {
  693. return SubExpr->getSourceRange();
  694. }
  695. static bool classof(const Stmt *S) {
  696. return S->getStmtClass() == CXXStdInitializerListExprClass;
  697. }
  698. child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
  699. const_child_range children() const {
  700. return const_child_range(&SubExpr, &SubExpr + 1);
  701. }
  702. };
  703. /// A C++ \c typeid expression (C++ [expr.typeid]), which gets
  704. /// the \c type_info that corresponds to the supplied type, or the (possibly
  705. /// dynamic) type of the supplied expression.
  706. ///
  707. /// This represents code like \c typeid(int) or \c typeid(*objPtr)
  708. class CXXTypeidExpr : public Expr {
  709. friend class ASTStmtReader;
  710. private:
  711. llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
  712. SourceRange Range;
  713. public:
  714. CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R)
  715. : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
  716. Range(R) {
  717. setDependence(computeDependence(this));
  718. }
  719. CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R)
  720. : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
  721. Range(R) {
  722. setDependence(computeDependence(this));
  723. }
  724. CXXTypeidExpr(EmptyShell Empty, bool isExpr)
  725. : Expr(CXXTypeidExprClass, Empty) {
  726. if (isExpr)
  727. Operand = (Expr*)nullptr;
  728. else
  729. Operand = (TypeSourceInfo*)nullptr;
  730. }
  731. /// Determine whether this typeid has a type operand which is potentially
  732. /// evaluated, per C++11 [expr.typeid]p3.
  733. bool isPotentiallyEvaluated() const;
  734. /// Best-effort check if the expression operand refers to a most derived
  735. /// object. This is not a strong guarantee.
  736. bool isMostDerived(ASTContext &Context) const;
  737. bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
  738. /// Retrieves the type operand of this typeid() expression after
  739. /// various required adjustments (removing reference types, cv-qualifiers).
  740. QualType getTypeOperand(ASTContext &Context) const;
  741. /// Retrieve source information for the type operand.
  742. TypeSourceInfo *getTypeOperandSourceInfo() const {
  743. assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
  744. return Operand.get<TypeSourceInfo *>();
  745. }
  746. Expr *getExprOperand() const {
  747. assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
  748. return static_cast<Expr*>(Operand.get<Stmt *>());
  749. }
  750. SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
  751. SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
  752. SourceRange getSourceRange() const LLVM_READONLY { return Range; }
  753. void setSourceRange(SourceRange R) { Range = R; }
  754. static bool classof(const Stmt *T) {
  755. return T->getStmtClass() == CXXTypeidExprClass;
  756. }
  757. // Iterators
  758. child_range children() {
  759. if (isTypeOperand())
  760. return child_range(child_iterator(), child_iterator());
  761. auto **begin = reinterpret_cast<Stmt **>(&Operand);
  762. return child_range(begin, begin + 1);
  763. }
  764. const_child_range children() const {
  765. if (isTypeOperand())
  766. return const_child_range(const_child_iterator(), const_child_iterator());
  767. auto **begin =
  768. reinterpret_cast<Stmt **>(&const_cast<CXXTypeidExpr *>(this)->Operand);
  769. return const_child_range(begin, begin + 1);
  770. }
  771. };
  772. /// A member reference to an MSPropertyDecl.
  773. ///
  774. /// This expression always has pseudo-object type, and therefore it is
  775. /// typically not encountered in a fully-typechecked expression except
  776. /// within the syntactic form of a PseudoObjectExpr.
  777. class MSPropertyRefExpr : public Expr {
  778. Expr *BaseExpr;
  779. MSPropertyDecl *TheDecl;
  780. SourceLocation MemberLoc;
  781. bool IsArrow;
  782. NestedNameSpecifierLoc QualifierLoc;
  783. public:
  784. friend class ASTStmtReader;
  785. MSPropertyRefExpr(Expr *baseExpr, MSPropertyDecl *decl, bool isArrow,
  786. QualType ty, ExprValueKind VK,
  787. NestedNameSpecifierLoc qualifierLoc, SourceLocation nameLoc)
  788. : Expr(MSPropertyRefExprClass, ty, VK, OK_Ordinary), BaseExpr(baseExpr),
  789. TheDecl(decl), MemberLoc(nameLoc), IsArrow(isArrow),
  790. QualifierLoc(qualifierLoc) {
  791. setDependence(computeDependence(this));
  792. }
  793. MSPropertyRefExpr(EmptyShell Empty) : Expr(MSPropertyRefExprClass, Empty) {}
  794. SourceRange getSourceRange() const LLVM_READONLY {
  795. return SourceRange(getBeginLoc(), getEndLoc());
  796. }
  797. bool isImplicitAccess() const {
  798. return getBaseExpr() && getBaseExpr()->isImplicitCXXThis();
  799. }
  800. SourceLocation getBeginLoc() const {
  801. if (!isImplicitAccess())
  802. return BaseExpr->getBeginLoc();
  803. else if (QualifierLoc)
  804. return QualifierLoc.getBeginLoc();
  805. else
  806. return MemberLoc;
  807. }
  808. SourceLocation getEndLoc() const { return getMemberLoc(); }
  809. child_range children() {
  810. return child_range((Stmt**)&BaseExpr, (Stmt**)&BaseExpr + 1);
  811. }
  812. const_child_range children() const {
  813. auto Children = const_cast<MSPropertyRefExpr *>(this)->children();
  814. return const_child_range(Children.begin(), Children.end());
  815. }
  816. static bool classof(const Stmt *T) {
  817. return T->getStmtClass() == MSPropertyRefExprClass;
  818. }
  819. Expr *getBaseExpr() const { return BaseExpr; }
  820. MSPropertyDecl *getPropertyDecl() const { return TheDecl; }
  821. bool isArrow() const { return IsArrow; }
  822. SourceLocation getMemberLoc() const { return MemberLoc; }
  823. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  824. };
  825. /// MS property subscript expression.
  826. /// MSVC supports 'property' attribute and allows to apply it to the
  827. /// declaration of an empty array in a class or structure definition.
  828. /// For example:
  829. /// \code
  830. /// __declspec(property(get=GetX, put=PutX)) int x[];
  831. /// \endcode
  832. /// The above statement indicates that x[] can be used with one or more array
  833. /// indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), and
  834. /// p->x[a][b] = i will be turned into p->PutX(a, b, i).
  835. /// This is a syntactic pseudo-object expression.
  836. class MSPropertySubscriptExpr : public Expr {
  837. friend class ASTStmtReader;
  838. enum { BASE_EXPR, IDX_EXPR, NUM_SUBEXPRS = 2 };
  839. Stmt *SubExprs[NUM_SUBEXPRS];
  840. SourceLocation RBracketLoc;
  841. void setBase(Expr *Base) { SubExprs[BASE_EXPR] = Base; }
  842. void setIdx(Expr *Idx) { SubExprs[IDX_EXPR] = Idx; }
  843. public:
  844. MSPropertySubscriptExpr(Expr *Base, Expr *Idx, QualType Ty, ExprValueKind VK,
  845. ExprObjectKind OK, SourceLocation RBracketLoc)
  846. : Expr(MSPropertySubscriptExprClass, Ty, VK, OK),
  847. RBracketLoc(RBracketLoc) {
  848. SubExprs[BASE_EXPR] = Base;
  849. SubExprs[IDX_EXPR] = Idx;
  850. setDependence(computeDependence(this));
  851. }
  852. /// Create an empty array subscript expression.
  853. explicit MSPropertySubscriptExpr(EmptyShell Shell)
  854. : Expr(MSPropertySubscriptExprClass, Shell) {}
  855. Expr *getBase() { return cast<Expr>(SubExprs[BASE_EXPR]); }
  856. const Expr *getBase() const { return cast<Expr>(SubExprs[BASE_EXPR]); }
  857. Expr *getIdx() { return cast<Expr>(SubExprs[IDX_EXPR]); }
  858. const Expr *getIdx() const { return cast<Expr>(SubExprs[IDX_EXPR]); }
  859. SourceLocation getBeginLoc() const LLVM_READONLY {
  860. return getBase()->getBeginLoc();
  861. }
  862. SourceLocation getEndLoc() const LLVM_READONLY { return RBracketLoc; }
  863. SourceLocation getRBracketLoc() const { return RBracketLoc; }
  864. void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
  865. SourceLocation getExprLoc() const LLVM_READONLY {
  866. return getBase()->getExprLoc();
  867. }
  868. static bool classof(const Stmt *T) {
  869. return T->getStmtClass() == MSPropertySubscriptExprClass;
  870. }
  871. // Iterators
  872. child_range children() {
  873. return child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
  874. }
  875. const_child_range children() const {
  876. return const_child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS);
  877. }
  878. };
  879. /// A Microsoft C++ @c __uuidof expression, which gets
  880. /// the _GUID that corresponds to the supplied type or expression.
  881. ///
  882. /// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr)
  883. class CXXUuidofExpr : public Expr {
  884. friend class ASTStmtReader;
  885. private:
  886. llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand;
  887. MSGuidDecl *Guid;
  888. SourceRange Range;
  889. public:
  890. CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, MSGuidDecl *Guid,
  891. SourceRange R)
  892. : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
  893. Guid(Guid), Range(R) {
  894. setDependence(computeDependence(this));
  895. }
  896. CXXUuidofExpr(QualType Ty, Expr *Operand, MSGuidDecl *Guid, SourceRange R)
  897. : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand),
  898. Guid(Guid), Range(R) {
  899. setDependence(computeDependence(this));
  900. }
  901. CXXUuidofExpr(EmptyShell Empty, bool isExpr)
  902. : Expr(CXXUuidofExprClass, Empty) {
  903. if (isExpr)
  904. Operand = (Expr*)nullptr;
  905. else
  906. Operand = (TypeSourceInfo*)nullptr;
  907. }
  908. bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); }
  909. /// Retrieves the type operand of this __uuidof() expression after
  910. /// various required adjustments (removing reference types, cv-qualifiers).
  911. QualType getTypeOperand(ASTContext &Context) const;
  912. /// Retrieve source information for the type operand.
  913. TypeSourceInfo *getTypeOperandSourceInfo() const {
  914. assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
  915. return Operand.get<TypeSourceInfo *>();
  916. }
  917. Expr *getExprOperand() const {
  918. assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)");
  919. return static_cast<Expr*>(Operand.get<Stmt *>());
  920. }
  921. MSGuidDecl *getGuidDecl() const { return Guid; }
  922. SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
  923. SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
  924. SourceRange getSourceRange() const LLVM_READONLY { return Range; }
  925. void setSourceRange(SourceRange R) { Range = R; }
  926. static bool classof(const Stmt *T) {
  927. return T->getStmtClass() == CXXUuidofExprClass;
  928. }
  929. // Iterators
  930. child_range children() {
  931. if (isTypeOperand())
  932. return child_range(child_iterator(), child_iterator());
  933. auto **begin = reinterpret_cast<Stmt **>(&Operand);
  934. return child_range(begin, begin + 1);
  935. }
  936. const_child_range children() const {
  937. if (isTypeOperand())
  938. return const_child_range(const_child_iterator(), const_child_iterator());
  939. auto **begin =
  940. reinterpret_cast<Stmt **>(&const_cast<CXXUuidofExpr *>(this)->Operand);
  941. return const_child_range(begin, begin + 1);
  942. }
  943. };
  944. /// Represents the \c this expression in C++.
  945. ///
  946. /// This is a pointer to the object on which the current member function is
  947. /// executing (C++ [expr.prim]p3). Example:
  948. ///
  949. /// \code
  950. /// class Foo {
  951. /// public:
  952. /// void bar();
  953. /// void test() { this->bar(); }
  954. /// };
  955. /// \endcode
  956. class CXXThisExpr : public Expr {
  957. public:
  958. CXXThisExpr(SourceLocation L, QualType Ty, bool IsImplicit)
  959. : Expr(CXXThisExprClass, Ty, VK_PRValue, OK_Ordinary) {
  960. CXXThisExprBits.IsImplicit = IsImplicit;
  961. CXXThisExprBits.Loc = L;
  962. setDependence(computeDependence(this));
  963. }
  964. CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {}
  965. SourceLocation getLocation() const { return CXXThisExprBits.Loc; }
  966. void setLocation(SourceLocation L) { CXXThisExprBits.Loc = L; }
  967. SourceLocation getBeginLoc() const { return getLocation(); }
  968. SourceLocation getEndLoc() const { return getLocation(); }
  969. bool isImplicit() const { return CXXThisExprBits.IsImplicit; }
  970. void setImplicit(bool I) { CXXThisExprBits.IsImplicit = I; }
  971. static bool classof(const Stmt *T) {
  972. return T->getStmtClass() == CXXThisExprClass;
  973. }
  974. // Iterators
  975. child_range children() {
  976. return child_range(child_iterator(), child_iterator());
  977. }
  978. const_child_range children() const {
  979. return const_child_range(const_child_iterator(), const_child_iterator());
  980. }
  981. };
  982. /// A C++ throw-expression (C++ [except.throw]).
  983. ///
  984. /// This handles 'throw' (for re-throwing the current exception) and
  985. /// 'throw' assignment-expression. When assignment-expression isn't
  986. /// present, Op will be null.
  987. class CXXThrowExpr : public Expr {
  988. friend class ASTStmtReader;
  989. /// The optional expression in the throw statement.
  990. Stmt *Operand;
  991. public:
  992. // \p Ty is the void type which is used as the result type of the
  993. // expression. The \p Loc is the location of the throw keyword.
  994. // \p Operand is the expression in the throw statement, and can be
  995. // null if not present.
  996. CXXThrowExpr(Expr *Operand, QualType Ty, SourceLocation Loc,
  997. bool IsThrownVariableInScope)
  998. : Expr(CXXThrowExprClass, Ty, VK_PRValue, OK_Ordinary), Operand(Operand) {
  999. CXXThrowExprBits.ThrowLoc = Loc;
  1000. CXXThrowExprBits.IsThrownVariableInScope = IsThrownVariableInScope;
  1001. setDependence(computeDependence(this));
  1002. }
  1003. CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {}
  1004. const Expr *getSubExpr() const { return cast_or_null<Expr>(Operand); }
  1005. Expr *getSubExpr() { return cast_or_null<Expr>(Operand); }
  1006. SourceLocation getThrowLoc() const { return CXXThrowExprBits.ThrowLoc; }
  1007. /// Determines whether the variable thrown by this expression (if any!)
  1008. /// is within the innermost try block.
  1009. ///
  1010. /// This information is required to determine whether the NRVO can apply to
  1011. /// this variable.
  1012. bool isThrownVariableInScope() const {
  1013. return CXXThrowExprBits.IsThrownVariableInScope;
  1014. }
  1015. SourceLocation getBeginLoc() const { return getThrowLoc(); }
  1016. SourceLocation getEndLoc() const LLVM_READONLY {
  1017. if (!getSubExpr())
  1018. return getThrowLoc();
  1019. return getSubExpr()->getEndLoc();
  1020. }
  1021. static bool classof(const Stmt *T) {
  1022. return T->getStmtClass() == CXXThrowExprClass;
  1023. }
  1024. // Iterators
  1025. child_range children() {
  1026. return child_range(&Operand, Operand ? &Operand + 1 : &Operand);
  1027. }
  1028. const_child_range children() const {
  1029. return const_child_range(&Operand, Operand ? &Operand + 1 : &Operand);
  1030. }
  1031. };
  1032. /// A default argument (C++ [dcl.fct.default]).
  1033. ///
  1034. /// This wraps up a function call argument that was created from the
  1035. /// corresponding parameter's default argument, when the call did not
  1036. /// explicitly supply arguments for all of the parameters.
  1037. class CXXDefaultArgExpr final
  1038. : public Expr,
  1039. private llvm::TrailingObjects<CXXDefaultArgExpr, Expr *> {
  1040. friend class ASTStmtReader;
  1041. friend class ASTReader;
  1042. friend TrailingObjects;
  1043. /// The parameter whose default is being used.
  1044. ParmVarDecl *Param;
  1045. /// The context where the default argument expression was used.
  1046. DeclContext *UsedContext;
  1047. CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *Param,
  1048. Expr *RewrittenExpr, DeclContext *UsedContext)
  1049. : Expr(SC,
  1050. Param->hasUnparsedDefaultArg()
  1051. ? Param->getType().getNonReferenceType()
  1052. : Param->getDefaultArg()->getType(),
  1053. Param->getDefaultArg()->getValueKind(),
  1054. Param->getDefaultArg()->getObjectKind()),
  1055. Param(Param), UsedContext(UsedContext) {
  1056. CXXDefaultArgExprBits.Loc = Loc;
  1057. CXXDefaultArgExprBits.HasRewrittenInit = RewrittenExpr != nullptr;
  1058. if (RewrittenExpr)
  1059. *getTrailingObjects<Expr *>() = RewrittenExpr;
  1060. setDependence(computeDependence(this));
  1061. }
  1062. CXXDefaultArgExpr(EmptyShell Empty, bool HasRewrittenInit)
  1063. : Expr(CXXDefaultArgExprClass, Empty) {
  1064. CXXDefaultArgExprBits.HasRewrittenInit = HasRewrittenInit;
  1065. }
  1066. public:
  1067. static CXXDefaultArgExpr *CreateEmpty(const ASTContext &C,
  1068. bool HasRewrittenInit);
  1069. // \p Param is the parameter whose default argument is used by this
  1070. // expression.
  1071. static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
  1072. ParmVarDecl *Param, Expr *RewrittenExpr,
  1073. DeclContext *UsedContext);
  1074. // Retrieve the parameter that the argument was created from.
  1075. const ParmVarDecl *getParam() const { return Param; }
  1076. ParmVarDecl *getParam() { return Param; }
  1077. bool hasRewrittenInit() const {
  1078. return CXXDefaultArgExprBits.HasRewrittenInit;
  1079. }
  1080. // Retrieve the argument to the function call.
  1081. Expr *getExpr();
  1082. const Expr *getExpr() const {
  1083. return const_cast<CXXDefaultArgExpr *>(this)->getExpr();
  1084. }
  1085. Expr *getRewrittenExpr() {
  1086. return hasRewrittenInit() ? *getTrailingObjects<Expr *>() : nullptr;
  1087. }
  1088. const Expr *getRewrittenExpr() const {
  1089. return const_cast<CXXDefaultArgExpr *>(this)->getRewrittenExpr();
  1090. }
  1091. // Retrieve the rewritten init expression (for an init expression containing
  1092. // immediate calls) with the top level FullExpr and ConstantExpr stripped off.
  1093. Expr *getAdjustedRewrittenExpr();
  1094. const Expr *getAdjustedRewrittenExpr() const {
  1095. return const_cast<CXXDefaultArgExpr *>(this)->getAdjustedRewrittenExpr();
  1096. }
  1097. const DeclContext *getUsedContext() const { return UsedContext; }
  1098. DeclContext *getUsedContext() { return UsedContext; }
  1099. /// Retrieve the location where this default argument was actually used.
  1100. SourceLocation getUsedLocation() const { return CXXDefaultArgExprBits.Loc; }
  1101. /// Default argument expressions have no representation in the
  1102. /// source, so they have an empty source range.
  1103. SourceLocation getBeginLoc() const { return SourceLocation(); }
  1104. SourceLocation getEndLoc() const { return SourceLocation(); }
  1105. SourceLocation getExprLoc() const { return getUsedLocation(); }
  1106. static bool classof(const Stmt *T) {
  1107. return T->getStmtClass() == CXXDefaultArgExprClass;
  1108. }
  1109. // Iterators
  1110. child_range children() {
  1111. return child_range(child_iterator(), child_iterator());
  1112. }
  1113. const_child_range children() const {
  1114. return const_child_range(const_child_iterator(), const_child_iterator());
  1115. }
  1116. };
  1117. /// A use of a default initializer in a constructor or in aggregate
  1118. /// initialization.
  1119. ///
  1120. /// This wraps a use of a C++ default initializer (technically,
  1121. /// a brace-or-equal-initializer for a non-static data member) when it
  1122. /// is implicitly used in a mem-initializer-list in a constructor
  1123. /// (C++11 [class.base.init]p8) or in aggregate initialization
  1124. /// (C++1y [dcl.init.aggr]p7).
  1125. class CXXDefaultInitExpr final
  1126. : public Expr,
  1127. private llvm::TrailingObjects<CXXDefaultInitExpr, Expr *> {
  1128. friend class ASTStmtReader;
  1129. friend class ASTReader;
  1130. friend TrailingObjects;
  1131. /// The field whose default is being used.
  1132. FieldDecl *Field;
  1133. /// The context where the default initializer expression was used.
  1134. DeclContext *UsedContext;
  1135. CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
  1136. FieldDecl *Field, QualType Ty, DeclContext *UsedContext,
  1137. Expr *RewrittenInitExpr);
  1138. CXXDefaultInitExpr(EmptyShell Empty, bool HasRewrittenInit)
  1139. : Expr(CXXDefaultInitExprClass, Empty) {
  1140. CXXDefaultInitExprBits.HasRewrittenInit = HasRewrittenInit;
  1141. }
  1142. public:
  1143. static CXXDefaultInitExpr *CreateEmpty(const ASTContext &C,
  1144. bool HasRewrittenInit);
  1145. /// \p Field is the non-static data member whose default initializer is used
  1146. /// by this expression.
  1147. static CXXDefaultInitExpr *Create(const ASTContext &Ctx, SourceLocation Loc,
  1148. FieldDecl *Field, DeclContext *UsedContext,
  1149. Expr *RewrittenInitExpr);
  1150. bool hasRewrittenInit() const {
  1151. return CXXDefaultInitExprBits.HasRewrittenInit;
  1152. }
  1153. /// Get the field whose initializer will be used.
  1154. FieldDecl *getField() { return Field; }
  1155. const FieldDecl *getField() const { return Field; }
  1156. /// Get the initialization expression that will be used.
  1157. Expr *getExpr();
  1158. const Expr *getExpr() const {
  1159. return const_cast<CXXDefaultInitExpr *>(this)->getExpr();
  1160. }
  1161. /// Retrieve the initializing expression with evaluated immediate calls, if
  1162. /// any.
  1163. const Expr *getRewrittenExpr() const {
  1164. assert(hasRewrittenInit() && "expected a rewritten init expression");
  1165. return *getTrailingObjects<Expr *>();
  1166. }
  1167. /// Retrieve the initializing expression with evaluated immediate calls, if
  1168. /// any.
  1169. Expr *getRewrittenExpr() {
  1170. assert(hasRewrittenInit() && "expected a rewritten init expression");
  1171. return *getTrailingObjects<Expr *>();
  1172. }
  1173. const DeclContext *getUsedContext() const { return UsedContext; }
  1174. DeclContext *getUsedContext() { return UsedContext; }
  1175. /// Retrieve the location where this default initializer expression was
  1176. /// actually used.
  1177. SourceLocation getUsedLocation() const { return getBeginLoc(); }
  1178. SourceLocation getBeginLoc() const { return CXXDefaultInitExprBits.Loc; }
  1179. SourceLocation getEndLoc() const { return CXXDefaultInitExprBits.Loc; }
  1180. static bool classof(const Stmt *T) {
  1181. return T->getStmtClass() == CXXDefaultInitExprClass;
  1182. }
  1183. // Iterators
  1184. child_range children() {
  1185. return child_range(child_iterator(), child_iterator());
  1186. }
  1187. const_child_range children() const {
  1188. return const_child_range(const_child_iterator(), const_child_iterator());
  1189. }
  1190. };
  1191. /// Represents a C++ temporary.
  1192. class CXXTemporary {
  1193. /// The destructor that needs to be called.
  1194. const CXXDestructorDecl *Destructor;
  1195. explicit CXXTemporary(const CXXDestructorDecl *destructor)
  1196. : Destructor(destructor) {}
  1197. public:
  1198. static CXXTemporary *Create(const ASTContext &C,
  1199. const CXXDestructorDecl *Destructor);
  1200. const CXXDestructorDecl *getDestructor() const { return Destructor; }
  1201. void setDestructor(const CXXDestructorDecl *Dtor) {
  1202. Destructor = Dtor;
  1203. }
  1204. };
  1205. /// Represents binding an expression to a temporary.
  1206. ///
  1207. /// This ensures the destructor is called for the temporary. It should only be
  1208. /// needed for non-POD, non-trivially destructable class types. For example:
  1209. ///
  1210. /// \code
  1211. /// struct S {
  1212. /// S() { } // User defined constructor makes S non-POD.
  1213. /// ~S() { } // User defined destructor makes it non-trivial.
  1214. /// };
  1215. /// void test() {
  1216. /// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr.
  1217. /// }
  1218. /// \endcode
  1219. class CXXBindTemporaryExpr : public Expr {
  1220. CXXTemporary *Temp = nullptr;
  1221. Stmt *SubExpr = nullptr;
  1222. CXXBindTemporaryExpr(CXXTemporary *temp, Expr *SubExpr)
  1223. : Expr(CXXBindTemporaryExprClass, SubExpr->getType(), VK_PRValue,
  1224. OK_Ordinary),
  1225. Temp(temp), SubExpr(SubExpr) {
  1226. setDependence(computeDependence(this));
  1227. }
  1228. public:
  1229. CXXBindTemporaryExpr(EmptyShell Empty)
  1230. : Expr(CXXBindTemporaryExprClass, Empty) {}
  1231. static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp,
  1232. Expr* SubExpr);
  1233. CXXTemporary *getTemporary() { return Temp; }
  1234. const CXXTemporary *getTemporary() const { return Temp; }
  1235. void setTemporary(CXXTemporary *T) { Temp = T; }
  1236. const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
  1237. Expr *getSubExpr() { return cast<Expr>(SubExpr); }
  1238. void setSubExpr(Expr *E) { SubExpr = E; }
  1239. SourceLocation getBeginLoc() const LLVM_READONLY {
  1240. return SubExpr->getBeginLoc();
  1241. }
  1242. SourceLocation getEndLoc() const LLVM_READONLY {
  1243. return SubExpr->getEndLoc();
  1244. }
  1245. // Implement isa/cast/dyncast/etc.
  1246. static bool classof(const Stmt *T) {
  1247. return T->getStmtClass() == CXXBindTemporaryExprClass;
  1248. }
  1249. // Iterators
  1250. child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
  1251. const_child_range children() const {
  1252. return const_child_range(&SubExpr, &SubExpr + 1);
  1253. }
  1254. };
  1255. /// Represents a call to a C++ constructor.
  1256. class CXXConstructExpr : public Expr {
  1257. friend class ASTStmtReader;
  1258. public:
  1259. enum ConstructionKind {
  1260. CK_Complete,
  1261. CK_NonVirtualBase,
  1262. CK_VirtualBase,
  1263. CK_Delegating
  1264. };
  1265. private:
  1266. /// A pointer to the constructor which will be ultimately called.
  1267. CXXConstructorDecl *Constructor;
  1268. SourceRange ParenOrBraceRange;
  1269. /// The number of arguments.
  1270. unsigned NumArgs;
  1271. // We would like to stash the arguments of the constructor call after
  1272. // CXXConstructExpr. However CXXConstructExpr is used as a base class of
  1273. // CXXTemporaryObjectExpr which makes the use of llvm::TrailingObjects
  1274. // impossible.
  1275. //
  1276. // Instead we manually stash the trailing object after the full object
  1277. // containing CXXConstructExpr (that is either CXXConstructExpr or
  1278. // CXXTemporaryObjectExpr).
  1279. //
  1280. // The trailing objects are:
  1281. //
  1282. // * An array of getNumArgs() "Stmt *" for the arguments of the
  1283. // constructor call.
  1284. /// Return a pointer to the start of the trailing arguments.
  1285. /// Defined just after CXXTemporaryObjectExpr.
  1286. inline Stmt **getTrailingArgs();
  1287. const Stmt *const *getTrailingArgs() const {
  1288. return const_cast<CXXConstructExpr *>(this)->getTrailingArgs();
  1289. }
  1290. protected:
  1291. /// Build a C++ construction expression.
  1292. CXXConstructExpr(StmtClass SC, QualType Ty, SourceLocation Loc,
  1293. CXXConstructorDecl *Ctor, bool Elidable,
  1294. ArrayRef<Expr *> Args, bool HadMultipleCandidates,
  1295. bool ListInitialization, bool StdInitListInitialization,
  1296. bool ZeroInitialization, ConstructionKind ConstructKind,
  1297. SourceRange ParenOrBraceRange);
  1298. /// Build an empty C++ construction expression.
  1299. CXXConstructExpr(StmtClass SC, EmptyShell Empty, unsigned NumArgs);
  1300. /// Return the size in bytes of the trailing objects. Used by
  1301. /// CXXTemporaryObjectExpr to allocate the right amount of storage.
  1302. static unsigned sizeOfTrailingObjects(unsigned NumArgs) {
  1303. return NumArgs * sizeof(Stmt *);
  1304. }
  1305. public:
  1306. /// Create a C++ construction expression.
  1307. static CXXConstructExpr *
  1308. Create(const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
  1309. CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
  1310. bool HadMultipleCandidates, bool ListInitialization,
  1311. bool StdInitListInitialization, bool ZeroInitialization,
  1312. ConstructionKind ConstructKind, SourceRange ParenOrBraceRange);
  1313. /// Create an empty C++ construction expression.
  1314. static CXXConstructExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs);
  1315. /// Get the constructor that this expression will (ultimately) call.
  1316. CXXConstructorDecl *getConstructor() const { return Constructor; }
  1317. SourceLocation getLocation() const { return CXXConstructExprBits.Loc; }
  1318. void setLocation(SourceLocation Loc) { CXXConstructExprBits.Loc = Loc; }
  1319. /// Whether this construction is elidable.
  1320. bool isElidable() const { return CXXConstructExprBits.Elidable; }
  1321. void setElidable(bool E) { CXXConstructExprBits.Elidable = E; }
  1322. /// Whether the referred constructor was resolved from
  1323. /// an overloaded set having size greater than 1.
  1324. bool hadMultipleCandidates() const {
  1325. return CXXConstructExprBits.HadMultipleCandidates;
  1326. }
  1327. void setHadMultipleCandidates(bool V) {
  1328. CXXConstructExprBits.HadMultipleCandidates = V;
  1329. }
  1330. /// Whether this constructor call was written as list-initialization.
  1331. bool isListInitialization() const {
  1332. return CXXConstructExprBits.ListInitialization;
  1333. }
  1334. void setListInitialization(bool V) {
  1335. CXXConstructExprBits.ListInitialization = V;
  1336. }
  1337. /// Whether this constructor call was written as list-initialization,
  1338. /// but was interpreted as forming a std::initializer_list<T> from the list
  1339. /// and passing that as a single constructor argument.
  1340. /// See C++11 [over.match.list]p1 bullet 1.
  1341. bool isStdInitListInitialization() const {
  1342. return CXXConstructExprBits.StdInitListInitialization;
  1343. }
  1344. void setStdInitListInitialization(bool V) {
  1345. CXXConstructExprBits.StdInitListInitialization = V;
  1346. }
  1347. /// Whether this construction first requires
  1348. /// zero-initialization before the initializer is called.
  1349. bool requiresZeroInitialization() const {
  1350. return CXXConstructExprBits.ZeroInitialization;
  1351. }
  1352. void setRequiresZeroInitialization(bool ZeroInit) {
  1353. CXXConstructExprBits.ZeroInitialization = ZeroInit;
  1354. }
  1355. /// Determine whether this constructor is actually constructing
  1356. /// a base class (rather than a complete object).
  1357. ConstructionKind getConstructionKind() const {
  1358. return static_cast<ConstructionKind>(CXXConstructExprBits.ConstructionKind);
  1359. }
  1360. void setConstructionKind(ConstructionKind CK) {
  1361. CXXConstructExprBits.ConstructionKind = CK;
  1362. }
  1363. using arg_iterator = ExprIterator;
  1364. using const_arg_iterator = ConstExprIterator;
  1365. using arg_range = llvm::iterator_range<arg_iterator>;
  1366. using const_arg_range = llvm::iterator_range<const_arg_iterator>;
  1367. arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
  1368. const_arg_range arguments() const {
  1369. return const_arg_range(arg_begin(), arg_end());
  1370. }
  1371. arg_iterator arg_begin() { return getTrailingArgs(); }
  1372. arg_iterator arg_end() { return arg_begin() + getNumArgs(); }
  1373. const_arg_iterator arg_begin() const { return getTrailingArgs(); }
  1374. const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); }
  1375. Expr **getArgs() { return reinterpret_cast<Expr **>(getTrailingArgs()); }
  1376. const Expr *const *getArgs() const {
  1377. return reinterpret_cast<const Expr *const *>(getTrailingArgs());
  1378. }
  1379. /// Return the number of arguments to the constructor call.
  1380. unsigned getNumArgs() const { return NumArgs; }
  1381. /// Return the specified argument.
  1382. Expr *getArg(unsigned Arg) {
  1383. assert(Arg < getNumArgs() && "Arg access out of range!");
  1384. return getArgs()[Arg];
  1385. }
  1386. const Expr *getArg(unsigned Arg) const {
  1387. assert(Arg < getNumArgs() && "Arg access out of range!");
  1388. return getArgs()[Arg];
  1389. }
  1390. /// Set the specified argument.
  1391. void setArg(unsigned Arg, Expr *ArgExpr) {
  1392. assert(Arg < getNumArgs() && "Arg access out of range!");
  1393. getArgs()[Arg] = ArgExpr;
  1394. }
  1395. SourceLocation getBeginLoc() const LLVM_READONLY;
  1396. SourceLocation getEndLoc() const LLVM_READONLY;
  1397. SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
  1398. void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; }
  1399. static bool classof(const Stmt *T) {
  1400. return T->getStmtClass() == CXXConstructExprClass ||
  1401. T->getStmtClass() == CXXTemporaryObjectExprClass;
  1402. }
  1403. // Iterators
  1404. child_range children() {
  1405. return child_range(getTrailingArgs(), getTrailingArgs() + getNumArgs());
  1406. }
  1407. const_child_range children() const {
  1408. auto Children = const_cast<CXXConstructExpr *>(this)->children();
  1409. return const_child_range(Children.begin(), Children.end());
  1410. }
  1411. };
  1412. /// Represents a call to an inherited base class constructor from an
  1413. /// inheriting constructor. This call implicitly forwards the arguments from
  1414. /// the enclosing context (an inheriting constructor) to the specified inherited
  1415. /// base class constructor.
  1416. class CXXInheritedCtorInitExpr : public Expr {
  1417. private:
  1418. CXXConstructorDecl *Constructor = nullptr;
  1419. /// The location of the using declaration.
  1420. SourceLocation Loc;
  1421. /// Whether this is the construction of a virtual base.
  1422. unsigned ConstructsVirtualBase : 1;
  1423. /// Whether the constructor is inherited from a virtual base class of the
  1424. /// class that we construct.
  1425. unsigned InheritedFromVirtualBase : 1;
  1426. public:
  1427. friend class ASTStmtReader;
  1428. /// Construct a C++ inheriting construction expression.
  1429. CXXInheritedCtorInitExpr(SourceLocation Loc, QualType T,
  1430. CXXConstructorDecl *Ctor, bool ConstructsVirtualBase,
  1431. bool InheritedFromVirtualBase)
  1432. : Expr(CXXInheritedCtorInitExprClass, T, VK_PRValue, OK_Ordinary),
  1433. Constructor(Ctor), Loc(Loc),
  1434. ConstructsVirtualBase(ConstructsVirtualBase),
  1435. InheritedFromVirtualBase(InheritedFromVirtualBase) {
  1436. assert(!T->isDependentType());
  1437. setDependence(ExprDependence::None);
  1438. }
  1439. /// Construct an empty C++ inheriting construction expression.
  1440. explicit CXXInheritedCtorInitExpr(EmptyShell Empty)
  1441. : Expr(CXXInheritedCtorInitExprClass, Empty),
  1442. ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {}
  1443. /// Get the constructor that this expression will call.
  1444. CXXConstructorDecl *getConstructor() const { return Constructor; }
  1445. /// Determine whether this constructor is actually constructing
  1446. /// a base class (rather than a complete object).
  1447. bool constructsVBase() const { return ConstructsVirtualBase; }
  1448. CXXConstructExpr::ConstructionKind getConstructionKind() const {
  1449. return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase
  1450. : CXXConstructExpr::CK_NonVirtualBase;
  1451. }
  1452. /// Determine whether the inherited constructor is inherited from a
  1453. /// virtual base of the object we construct. If so, we are not responsible
  1454. /// for calling the inherited constructor (the complete object constructor
  1455. /// does that), and so we don't need to pass any arguments.
  1456. bool inheritedFromVBase() const { return InheritedFromVirtualBase; }
  1457. SourceLocation getLocation() const LLVM_READONLY { return Loc; }
  1458. SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
  1459. SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
  1460. static bool classof(const Stmt *T) {
  1461. return T->getStmtClass() == CXXInheritedCtorInitExprClass;
  1462. }
  1463. child_range children() {
  1464. return child_range(child_iterator(), child_iterator());
  1465. }
  1466. const_child_range children() const {
  1467. return const_child_range(const_child_iterator(), const_child_iterator());
  1468. }
  1469. };
  1470. /// Represents an explicit C++ type conversion that uses "functional"
  1471. /// notation (C++ [expr.type.conv]).
  1472. ///
  1473. /// Example:
  1474. /// \code
  1475. /// x = int(0.5);
  1476. /// \endcode
  1477. class CXXFunctionalCastExpr final
  1478. : public ExplicitCastExpr,
  1479. private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *,
  1480. FPOptionsOverride> {
  1481. SourceLocation LParenLoc;
  1482. SourceLocation RParenLoc;
  1483. CXXFunctionalCastExpr(QualType ty, ExprValueKind VK,
  1484. TypeSourceInfo *writtenTy, CastKind kind,
  1485. Expr *castExpr, unsigned pathSize,
  1486. FPOptionsOverride FPO, SourceLocation lParenLoc,
  1487. SourceLocation rParenLoc)
  1488. : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, castExpr,
  1489. pathSize, FPO.requiresTrailingStorage(), writtenTy),
  1490. LParenLoc(lParenLoc), RParenLoc(rParenLoc) {
  1491. if (hasStoredFPFeatures())
  1492. *getTrailingFPFeatures() = FPO;
  1493. }
  1494. explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize,
  1495. bool HasFPFeatures)
  1496. : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize,
  1497. HasFPFeatures) {}
  1498. unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const {
  1499. return path_size();
  1500. }
  1501. public:
  1502. friend class CastExpr;
  1503. friend TrailingObjects;
  1504. static CXXFunctionalCastExpr *
  1505. Create(const ASTContext &Context, QualType T, ExprValueKind VK,
  1506. TypeSourceInfo *Written, CastKind Kind, Expr *Op,
  1507. const CXXCastPath *Path, FPOptionsOverride FPO, SourceLocation LPLoc,
  1508. SourceLocation RPLoc);
  1509. static CXXFunctionalCastExpr *
  1510. CreateEmpty(const ASTContext &Context, unsigned PathSize, bool HasFPFeatures);
  1511. SourceLocation getLParenLoc() const { return LParenLoc; }
  1512. void setLParenLoc(SourceLocation L) { LParenLoc = L; }
  1513. SourceLocation getRParenLoc() const { return RParenLoc; }
  1514. void setRParenLoc(SourceLocation L) { RParenLoc = L; }
  1515. /// Determine whether this expression models list-initialization.
  1516. bool isListInitialization() const { return LParenLoc.isInvalid(); }
  1517. SourceLocation getBeginLoc() const LLVM_READONLY;
  1518. SourceLocation getEndLoc() const LLVM_READONLY;
  1519. static bool classof(const Stmt *T) {
  1520. return T->getStmtClass() == CXXFunctionalCastExprClass;
  1521. }
  1522. };
  1523. /// Represents a C++ functional cast expression that builds a
  1524. /// temporary object.
  1525. ///
  1526. /// This expression type represents a C++ "functional" cast
  1527. /// (C++[expr.type.conv]) with N != 1 arguments that invokes a
  1528. /// constructor to build a temporary object. With N == 1 arguments the
  1529. /// functional cast expression will be represented by CXXFunctionalCastExpr.
  1530. /// Example:
  1531. /// \code
  1532. /// struct X { X(int, float); }
  1533. ///
  1534. /// X create_X() {
  1535. /// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr
  1536. /// };
  1537. /// \endcode
  1538. class CXXTemporaryObjectExpr final : public CXXConstructExpr {
  1539. friend class ASTStmtReader;
  1540. // CXXTemporaryObjectExpr has some trailing objects belonging
  1541. // to CXXConstructExpr. See the comment inside CXXConstructExpr
  1542. // for more details.
  1543. TypeSourceInfo *TSI;
  1544. CXXTemporaryObjectExpr(CXXConstructorDecl *Cons, QualType Ty,
  1545. TypeSourceInfo *TSI, ArrayRef<Expr *> Args,
  1546. SourceRange ParenOrBraceRange,
  1547. bool HadMultipleCandidates, bool ListInitialization,
  1548. bool StdInitListInitialization,
  1549. bool ZeroInitialization);
  1550. CXXTemporaryObjectExpr(EmptyShell Empty, unsigned NumArgs);
  1551. public:
  1552. static CXXTemporaryObjectExpr *
  1553. Create(const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
  1554. TypeSourceInfo *TSI, ArrayRef<Expr *> Args,
  1555. SourceRange ParenOrBraceRange, bool HadMultipleCandidates,
  1556. bool ListInitialization, bool StdInitListInitialization,
  1557. bool ZeroInitialization);
  1558. static CXXTemporaryObjectExpr *CreateEmpty(const ASTContext &Ctx,
  1559. unsigned NumArgs);
  1560. TypeSourceInfo *getTypeSourceInfo() const { return TSI; }
  1561. SourceLocation getBeginLoc() const LLVM_READONLY;
  1562. SourceLocation getEndLoc() const LLVM_READONLY;
  1563. static bool classof(const Stmt *T) {
  1564. return T->getStmtClass() == CXXTemporaryObjectExprClass;
  1565. }
  1566. };
  1567. Stmt **CXXConstructExpr::getTrailingArgs() {
  1568. if (auto *E = dyn_cast<CXXTemporaryObjectExpr>(this))
  1569. return reinterpret_cast<Stmt **>(E + 1);
  1570. assert((getStmtClass() == CXXConstructExprClass) &&
  1571. "Unexpected class deriving from CXXConstructExpr!");
  1572. return reinterpret_cast<Stmt **>(this + 1);
  1573. }
  1574. /// A C++ lambda expression, which produces a function object
  1575. /// (of unspecified type) that can be invoked later.
  1576. ///
  1577. /// Example:
  1578. /// \code
  1579. /// void low_pass_filter(std::vector<double> &values, double cutoff) {
  1580. /// values.erase(std::remove_if(values.begin(), values.end(),
  1581. /// [=](double value) { return value > cutoff; });
  1582. /// }
  1583. /// \endcode
  1584. ///
  1585. /// C++11 lambda expressions can capture local variables, either by copying
  1586. /// the values of those local variables at the time the function
  1587. /// object is constructed (not when it is called!) or by holding a
  1588. /// reference to the local variable. These captures can occur either
  1589. /// implicitly or can be written explicitly between the square
  1590. /// brackets ([...]) that start the lambda expression.
  1591. ///
  1592. /// C++1y introduces a new form of "capture" called an init-capture that
  1593. /// includes an initializing expression (rather than capturing a variable),
  1594. /// and which can never occur implicitly.
  1595. class LambdaExpr final : public Expr,
  1596. private llvm::TrailingObjects<LambdaExpr, Stmt *> {
  1597. // LambdaExpr has some data stored in LambdaExprBits.
  1598. /// The source range that covers the lambda introducer ([...]).
  1599. SourceRange IntroducerRange;
  1600. /// The source location of this lambda's capture-default ('=' or '&').
  1601. SourceLocation CaptureDefaultLoc;
  1602. /// The location of the closing brace ('}') that completes
  1603. /// the lambda.
  1604. ///
  1605. /// The location of the brace is also available by looking up the
  1606. /// function call operator in the lambda class. However, it is
  1607. /// stored here to improve the performance of getSourceRange(), and
  1608. /// to avoid having to deserialize the function call operator from a
  1609. /// module file just to determine the source range.
  1610. SourceLocation ClosingBrace;
  1611. /// Construct a lambda expression.
  1612. LambdaExpr(QualType T, SourceRange IntroducerRange,
  1613. LambdaCaptureDefault CaptureDefault,
  1614. SourceLocation CaptureDefaultLoc, bool ExplicitParams,
  1615. bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
  1616. SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack);
  1617. /// Construct an empty lambda expression.
  1618. LambdaExpr(EmptyShell Empty, unsigned NumCaptures);
  1619. Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
  1620. Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
  1621. void initBodyIfNeeded() const;
  1622. public:
  1623. friend class ASTStmtReader;
  1624. friend class ASTStmtWriter;
  1625. friend TrailingObjects;
  1626. /// Construct a new lambda expression.
  1627. static LambdaExpr *
  1628. Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange,
  1629. LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc,
  1630. bool ExplicitParams, bool ExplicitResultType,
  1631. ArrayRef<Expr *> CaptureInits, SourceLocation ClosingBrace,
  1632. bool ContainsUnexpandedParameterPack);
  1633. /// Construct a new lambda expression that will be deserialized from
  1634. /// an external source.
  1635. static LambdaExpr *CreateDeserialized(const ASTContext &C,
  1636. unsigned NumCaptures);
  1637. /// Determine the default capture kind for this lambda.
  1638. LambdaCaptureDefault getCaptureDefault() const {
  1639. return static_cast<LambdaCaptureDefault>(LambdaExprBits.CaptureDefault);
  1640. }
  1641. /// Retrieve the location of this lambda's capture-default, if any.
  1642. SourceLocation getCaptureDefaultLoc() const { return CaptureDefaultLoc; }
  1643. /// Determine whether one of this lambda's captures is an init-capture.
  1644. bool isInitCapture(const LambdaCapture *Capture) const;
  1645. /// An iterator that walks over the captures of the lambda,
  1646. /// both implicit and explicit.
  1647. using capture_iterator = const LambdaCapture *;
  1648. /// An iterator over a range of lambda captures.
  1649. using capture_range = llvm::iterator_range<capture_iterator>;
  1650. /// Retrieve this lambda's captures.
  1651. capture_range captures() const;
  1652. /// Retrieve an iterator pointing to the first lambda capture.
  1653. capture_iterator capture_begin() const;
  1654. /// Retrieve an iterator pointing past the end of the
  1655. /// sequence of lambda captures.
  1656. capture_iterator capture_end() const;
  1657. /// Determine the number of captures in this lambda.
  1658. unsigned capture_size() const { return LambdaExprBits.NumCaptures; }
  1659. /// Retrieve this lambda's explicit captures.
  1660. capture_range explicit_captures() const;
  1661. /// Retrieve an iterator pointing to the first explicit
  1662. /// lambda capture.
  1663. capture_iterator explicit_capture_begin() const;
  1664. /// Retrieve an iterator pointing past the end of the sequence of
  1665. /// explicit lambda captures.
  1666. capture_iterator explicit_capture_end() const;
  1667. /// Retrieve this lambda's implicit captures.
  1668. capture_range implicit_captures() const;
  1669. /// Retrieve an iterator pointing to the first implicit
  1670. /// lambda capture.
  1671. capture_iterator implicit_capture_begin() const;
  1672. /// Retrieve an iterator pointing past the end of the sequence of
  1673. /// implicit lambda captures.
  1674. capture_iterator implicit_capture_end() const;
  1675. /// Iterator that walks over the capture initialization
  1676. /// arguments.
  1677. using capture_init_iterator = Expr **;
  1678. /// Const iterator that walks over the capture initialization
  1679. /// arguments.
  1680. /// FIXME: This interface is prone to being used incorrectly.
  1681. using const_capture_init_iterator = Expr *const *;
  1682. /// Retrieve the initialization expressions for this lambda's captures.
  1683. llvm::iterator_range<capture_init_iterator> capture_inits() {
  1684. return llvm::make_range(capture_init_begin(), capture_init_end());
  1685. }
  1686. /// Retrieve the initialization expressions for this lambda's captures.
  1687. llvm::iterator_range<const_capture_init_iterator> capture_inits() const {
  1688. return llvm::make_range(capture_init_begin(), capture_init_end());
  1689. }
  1690. /// Retrieve the first initialization argument for this
  1691. /// lambda expression (which initializes the first capture field).
  1692. capture_init_iterator capture_init_begin() {
  1693. return reinterpret_cast<Expr **>(getStoredStmts());
  1694. }
  1695. /// Retrieve the first initialization argument for this
  1696. /// lambda expression (which initializes the first capture field).
  1697. const_capture_init_iterator capture_init_begin() const {
  1698. return reinterpret_cast<Expr *const *>(getStoredStmts());
  1699. }
  1700. /// Retrieve the iterator pointing one past the last
  1701. /// initialization argument for this lambda expression.
  1702. capture_init_iterator capture_init_end() {
  1703. return capture_init_begin() + capture_size();
  1704. }
  1705. /// Retrieve the iterator pointing one past the last
  1706. /// initialization argument for this lambda expression.
  1707. const_capture_init_iterator capture_init_end() const {
  1708. return capture_init_begin() + capture_size();
  1709. }
  1710. /// Retrieve the source range covering the lambda introducer,
  1711. /// which contains the explicit capture list surrounded by square
  1712. /// brackets ([...]).
  1713. SourceRange getIntroducerRange() const { return IntroducerRange; }
  1714. /// Retrieve the class that corresponds to the lambda.
  1715. ///
  1716. /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the
  1717. /// captures in its fields and provides the various operations permitted
  1718. /// on a lambda (copying, calling).
  1719. CXXRecordDecl *getLambdaClass() const;
  1720. /// Retrieve the function call operator associated with this
  1721. /// lambda expression.
  1722. CXXMethodDecl *getCallOperator() const;
  1723. /// Retrieve the function template call operator associated with this
  1724. /// lambda expression.
  1725. FunctionTemplateDecl *getDependentCallOperator() const;
  1726. /// If this is a generic lambda expression, retrieve the template
  1727. /// parameter list associated with it, or else return null.
  1728. TemplateParameterList *getTemplateParameterList() const;
  1729. /// Get the template parameters were explicitly specified (as opposed to being
  1730. /// invented by use of an auto parameter).
  1731. ArrayRef<NamedDecl *> getExplicitTemplateParameters() const;
  1732. /// Get the trailing requires clause, if any.
  1733. Expr *getTrailingRequiresClause() const;
  1734. /// Whether this is a generic lambda.
  1735. bool isGenericLambda() const { return getTemplateParameterList(); }
  1736. /// Retrieve the body of the lambda. This will be most of the time
  1737. /// a \p CompoundStmt, but can also be \p CoroutineBodyStmt wrapping
  1738. /// a \p CompoundStmt. Note that unlike functions, lambda-expressions
  1739. /// cannot have a function-try-block.
  1740. Stmt *getBody() const;
  1741. /// Retrieve the \p CompoundStmt representing the body of the lambda.
  1742. /// This is a convenience function for callers who do not need
  1743. /// to handle node(s) which may wrap a \p CompoundStmt.
  1744. const CompoundStmt *getCompoundStmtBody() const;
  1745. CompoundStmt *getCompoundStmtBody() {
  1746. const auto *ConstThis = this;
  1747. return const_cast<CompoundStmt *>(ConstThis->getCompoundStmtBody());
  1748. }
  1749. /// Determine whether the lambda is mutable, meaning that any
  1750. /// captures values can be modified.
  1751. bool isMutable() const;
  1752. /// Determine whether this lambda has an explicit parameter
  1753. /// list vs. an implicit (empty) parameter list.
  1754. bool hasExplicitParameters() const { return LambdaExprBits.ExplicitParams; }
  1755. /// Whether this lambda had its result type explicitly specified.
  1756. bool hasExplicitResultType() const {
  1757. return LambdaExprBits.ExplicitResultType;
  1758. }
  1759. static bool classof(const Stmt *T) {
  1760. return T->getStmtClass() == LambdaExprClass;
  1761. }
  1762. SourceLocation getBeginLoc() const LLVM_READONLY {
  1763. return IntroducerRange.getBegin();
  1764. }
  1765. SourceLocation getEndLoc() const LLVM_READONLY { return ClosingBrace; }
  1766. /// Includes the captures and the body of the lambda.
  1767. child_range children();
  1768. const_child_range children() const;
  1769. };
  1770. /// An expression "T()" which creates a value-initialized rvalue of type
  1771. /// T, which is a non-class type. See (C++98 [5.2.3p2]).
  1772. class CXXScalarValueInitExpr : public Expr {
  1773. friend class ASTStmtReader;
  1774. TypeSourceInfo *TypeInfo;
  1775. public:
  1776. /// Create an explicitly-written scalar-value initialization
  1777. /// expression.
  1778. CXXScalarValueInitExpr(QualType Type, TypeSourceInfo *TypeInfo,
  1779. SourceLocation RParenLoc)
  1780. : Expr(CXXScalarValueInitExprClass, Type, VK_PRValue, OK_Ordinary),
  1781. TypeInfo(TypeInfo) {
  1782. CXXScalarValueInitExprBits.RParenLoc = RParenLoc;
  1783. setDependence(computeDependence(this));
  1784. }
  1785. explicit CXXScalarValueInitExpr(EmptyShell Shell)
  1786. : Expr(CXXScalarValueInitExprClass, Shell) {}
  1787. TypeSourceInfo *getTypeSourceInfo() const {
  1788. return TypeInfo;
  1789. }
  1790. SourceLocation getRParenLoc() const {
  1791. return CXXScalarValueInitExprBits.RParenLoc;
  1792. }
  1793. SourceLocation getBeginLoc() const LLVM_READONLY;
  1794. SourceLocation getEndLoc() const { return getRParenLoc(); }
  1795. static bool classof(const Stmt *T) {
  1796. return T->getStmtClass() == CXXScalarValueInitExprClass;
  1797. }
  1798. // Iterators
  1799. child_range children() {
  1800. return child_range(child_iterator(), child_iterator());
  1801. }
  1802. const_child_range children() const {
  1803. return const_child_range(const_child_iterator(), const_child_iterator());
  1804. }
  1805. };
  1806. /// Represents a new-expression for memory allocation and constructor
  1807. /// calls, e.g: "new CXXNewExpr(foo)".
  1808. class CXXNewExpr final
  1809. : public Expr,
  1810. private llvm::TrailingObjects<CXXNewExpr, Stmt *, SourceRange> {
  1811. friend class ASTStmtReader;
  1812. friend class ASTStmtWriter;
  1813. friend TrailingObjects;
  1814. /// Points to the allocation function used.
  1815. FunctionDecl *OperatorNew;
  1816. /// Points to the deallocation function used in case of error. May be null.
  1817. FunctionDecl *OperatorDelete;
  1818. /// The allocated type-source information, as written in the source.
  1819. TypeSourceInfo *AllocatedTypeInfo;
  1820. /// Range of the entire new expression.
  1821. SourceRange Range;
  1822. /// Source-range of a paren-delimited initializer.
  1823. SourceRange DirectInitRange;
  1824. // CXXNewExpr is followed by several optional trailing objects.
  1825. // They are in order:
  1826. //
  1827. // * An optional "Stmt *" for the array size expression.
  1828. // Present if and ony if isArray().
  1829. //
  1830. // * An optional "Stmt *" for the init expression.
  1831. // Present if and only if hasInitializer().
  1832. //
  1833. // * An array of getNumPlacementArgs() "Stmt *" for the placement new
  1834. // arguments, if any.
  1835. //
  1836. // * An optional SourceRange for the range covering the parenthesized type-id
  1837. // if the allocated type was expressed as a parenthesized type-id.
  1838. // Present if and only if isParenTypeId().
  1839. unsigned arraySizeOffset() const { return 0; }
  1840. unsigned initExprOffset() const { return arraySizeOffset() + isArray(); }
  1841. unsigned placementNewArgsOffset() const {
  1842. return initExprOffset() + hasInitializer();
  1843. }
  1844. unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
  1845. return isArray() + hasInitializer() + getNumPlacementArgs();
  1846. }
  1847. unsigned numTrailingObjects(OverloadToken<SourceRange>) const {
  1848. return isParenTypeId();
  1849. }
  1850. public:
  1851. enum InitializationStyle {
  1852. /// New-expression has no initializer as written.
  1853. NoInit,
  1854. /// New-expression has a C++98 paren-delimited initializer.
  1855. CallInit,
  1856. /// New-expression has a C++11 list-initializer.
  1857. ListInit
  1858. };
  1859. private:
  1860. /// Build a c++ new expression.
  1861. CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
  1862. FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
  1863. bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
  1864. SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
  1865. InitializationStyle InitializationStyle, Expr *Initializer,
  1866. QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
  1867. SourceRange DirectInitRange);
  1868. /// Build an empty c++ new expression.
  1869. CXXNewExpr(EmptyShell Empty, bool IsArray, unsigned NumPlacementArgs,
  1870. bool IsParenTypeId);
  1871. public:
  1872. /// Create a c++ new expression.
  1873. static CXXNewExpr *
  1874. Create(const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
  1875. FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
  1876. bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
  1877. SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
  1878. InitializationStyle InitializationStyle, Expr *Initializer,
  1879. QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
  1880. SourceRange DirectInitRange);
  1881. /// Create an empty c++ new expression.
  1882. static CXXNewExpr *CreateEmpty(const ASTContext &Ctx, bool IsArray,
  1883. bool HasInit, unsigned NumPlacementArgs,
  1884. bool IsParenTypeId);
  1885. QualType getAllocatedType() const {
  1886. return getType()->castAs<PointerType>()->getPointeeType();
  1887. }
  1888. TypeSourceInfo *getAllocatedTypeSourceInfo() const {
  1889. return AllocatedTypeInfo;
  1890. }
  1891. /// True if the allocation result needs to be null-checked.
  1892. ///
  1893. /// C++11 [expr.new]p13:
  1894. /// If the allocation function returns null, initialization shall
  1895. /// not be done, the deallocation function shall not be called,
  1896. /// and the value of the new-expression shall be null.
  1897. ///
  1898. /// C++ DR1748:
  1899. /// If the allocation function is a reserved placement allocation
  1900. /// function that returns null, the behavior is undefined.
  1901. ///
  1902. /// An allocation function is not allowed to return null unless it
  1903. /// has a non-throwing exception-specification. The '03 rule is
  1904. /// identical except that the definition of a non-throwing
  1905. /// exception specification is just "is it throw()?".
  1906. bool shouldNullCheckAllocation() const;
  1907. FunctionDecl *getOperatorNew() const { return OperatorNew; }
  1908. void setOperatorNew(FunctionDecl *D) { OperatorNew = D; }
  1909. FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
  1910. void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; }
  1911. bool isArray() const { return CXXNewExprBits.IsArray; }
  1912. /// This might return std::nullopt even if isArray() returns true,
  1913. /// since there might not be an array size expression.
  1914. /// If the result is not-None, it will never wrap a nullptr.
  1915. std::optional<Expr *> getArraySize() {
  1916. if (!isArray())
  1917. return std::nullopt;
  1918. if (auto *Result =
  1919. cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
  1920. return Result;
  1921. return std::nullopt;
  1922. }
  1923. /// This might return std::nullopt even if isArray() returns true,
  1924. /// since there might not be an array size expression.
  1925. /// If the result is not-None, it will never wrap a nullptr.
  1926. std::optional<const Expr *> getArraySize() const {
  1927. if (!isArray())
  1928. return std::nullopt;
  1929. if (auto *Result =
  1930. cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
  1931. return Result;
  1932. return std::nullopt;
  1933. }
  1934. unsigned getNumPlacementArgs() const {
  1935. return CXXNewExprBits.NumPlacementArgs;
  1936. }
  1937. Expr **getPlacementArgs() {
  1938. return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>() +
  1939. placementNewArgsOffset());
  1940. }
  1941. Expr *getPlacementArg(unsigned I) {
  1942. assert((I < getNumPlacementArgs()) && "Index out of range!");
  1943. return getPlacementArgs()[I];
  1944. }
  1945. const Expr *getPlacementArg(unsigned I) const {
  1946. return const_cast<CXXNewExpr *>(this)->getPlacementArg(I);
  1947. }
  1948. bool isParenTypeId() const { return CXXNewExprBits.IsParenTypeId; }
  1949. SourceRange getTypeIdParens() const {
  1950. return isParenTypeId() ? getTrailingObjects<SourceRange>()[0]
  1951. : SourceRange();
  1952. }
  1953. bool isGlobalNew() const { return CXXNewExprBits.IsGlobalNew; }
  1954. /// Whether this new-expression has any initializer at all.
  1955. bool hasInitializer() const {
  1956. return CXXNewExprBits.StoredInitializationStyle > 0;
  1957. }
  1958. /// The kind of initializer this new-expression has.
  1959. InitializationStyle getInitializationStyle() const {
  1960. if (CXXNewExprBits.StoredInitializationStyle == 0)
  1961. return NoInit;
  1962. return static_cast<InitializationStyle>(
  1963. CXXNewExprBits.StoredInitializationStyle - 1);
  1964. }
  1965. /// The initializer of this new-expression.
  1966. Expr *getInitializer() {
  1967. return hasInitializer()
  1968. ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
  1969. : nullptr;
  1970. }
  1971. const Expr *getInitializer() const {
  1972. return hasInitializer()
  1973. ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()])
  1974. : nullptr;
  1975. }
  1976. /// Returns the CXXConstructExpr from this new-expression, or null.
  1977. const CXXConstructExpr *getConstructExpr() const {
  1978. return dyn_cast_or_null<CXXConstructExpr>(getInitializer());
  1979. }
  1980. /// Indicates whether the required alignment should be implicitly passed to
  1981. /// the allocation function.
  1982. bool passAlignment() const { return CXXNewExprBits.ShouldPassAlignment; }
  1983. /// Answers whether the usual array deallocation function for the
  1984. /// allocated type expects the size of the allocation as a
  1985. /// parameter.
  1986. bool doesUsualArrayDeleteWantSize() const {
  1987. return CXXNewExprBits.UsualArrayDeleteWantsSize;
  1988. }
  1989. using arg_iterator = ExprIterator;
  1990. using const_arg_iterator = ConstExprIterator;
  1991. llvm::iterator_range<arg_iterator> placement_arguments() {
  1992. return llvm::make_range(placement_arg_begin(), placement_arg_end());
  1993. }
  1994. llvm::iterator_range<const_arg_iterator> placement_arguments() const {
  1995. return llvm::make_range(placement_arg_begin(), placement_arg_end());
  1996. }
  1997. arg_iterator placement_arg_begin() {
  1998. return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
  1999. }
  2000. arg_iterator placement_arg_end() {
  2001. return placement_arg_begin() + getNumPlacementArgs();
  2002. }
  2003. const_arg_iterator placement_arg_begin() const {
  2004. return getTrailingObjects<Stmt *>() + placementNewArgsOffset();
  2005. }
  2006. const_arg_iterator placement_arg_end() const {
  2007. return placement_arg_begin() + getNumPlacementArgs();
  2008. }
  2009. using raw_arg_iterator = Stmt **;
  2010. raw_arg_iterator raw_arg_begin() { return getTrailingObjects<Stmt *>(); }
  2011. raw_arg_iterator raw_arg_end() {
  2012. return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
  2013. }
  2014. const_arg_iterator raw_arg_begin() const {
  2015. return getTrailingObjects<Stmt *>();
  2016. }
  2017. const_arg_iterator raw_arg_end() const {
  2018. return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>());
  2019. }
  2020. SourceLocation getBeginLoc() const { return Range.getBegin(); }
  2021. SourceLocation getEndLoc() const { return Range.getEnd(); }
  2022. SourceRange getDirectInitRange() const { return DirectInitRange; }
  2023. SourceRange getSourceRange() const { return Range; }
  2024. static bool classof(const Stmt *T) {
  2025. return T->getStmtClass() == CXXNewExprClass;
  2026. }
  2027. // Iterators
  2028. child_range children() { return child_range(raw_arg_begin(), raw_arg_end()); }
  2029. const_child_range children() const {
  2030. return const_child_range(const_cast<CXXNewExpr *>(this)->children());
  2031. }
  2032. };
  2033. /// Represents a \c delete expression for memory deallocation and
  2034. /// destructor calls, e.g. "delete[] pArray".
  2035. class CXXDeleteExpr : public Expr {
  2036. friend class ASTStmtReader;
  2037. /// Points to the operator delete overload that is used. Could be a member.
  2038. FunctionDecl *OperatorDelete = nullptr;
  2039. /// The pointer expression to be deleted.
  2040. Stmt *Argument = nullptr;
  2041. public:
  2042. CXXDeleteExpr(QualType Ty, bool GlobalDelete, bool ArrayForm,
  2043. bool ArrayFormAsWritten, bool UsualArrayDeleteWantsSize,
  2044. FunctionDecl *OperatorDelete, Expr *Arg, SourceLocation Loc)
  2045. : Expr(CXXDeleteExprClass, Ty, VK_PRValue, OK_Ordinary),
  2046. OperatorDelete(OperatorDelete), Argument(Arg) {
  2047. CXXDeleteExprBits.GlobalDelete = GlobalDelete;
  2048. CXXDeleteExprBits.ArrayForm = ArrayForm;
  2049. CXXDeleteExprBits.ArrayFormAsWritten = ArrayFormAsWritten;
  2050. CXXDeleteExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
  2051. CXXDeleteExprBits.Loc = Loc;
  2052. setDependence(computeDependence(this));
  2053. }
  2054. explicit CXXDeleteExpr(EmptyShell Shell) : Expr(CXXDeleteExprClass, Shell) {}
  2055. bool isGlobalDelete() const { return CXXDeleteExprBits.GlobalDelete; }
  2056. bool isArrayForm() const { return CXXDeleteExprBits.ArrayForm; }
  2057. bool isArrayFormAsWritten() const {
  2058. return CXXDeleteExprBits.ArrayFormAsWritten;
  2059. }
  2060. /// Answers whether the usual array deallocation function for the
  2061. /// allocated type expects the size of the allocation as a
  2062. /// parameter. This can be true even if the actual deallocation
  2063. /// function that we're using doesn't want a size.
  2064. bool doesUsualArrayDeleteWantSize() const {
  2065. return CXXDeleteExprBits.UsualArrayDeleteWantsSize;
  2066. }
  2067. FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
  2068. Expr *getArgument() { return cast<Expr>(Argument); }
  2069. const Expr *getArgument() const { return cast<Expr>(Argument); }
  2070. /// Retrieve the type being destroyed.
  2071. ///
  2072. /// If the type being destroyed is a dependent type which may or may not
  2073. /// be a pointer, return an invalid type.
  2074. QualType getDestroyedType() const;
  2075. SourceLocation getBeginLoc() const { return CXXDeleteExprBits.Loc; }
  2076. SourceLocation getEndLoc() const LLVM_READONLY {
  2077. return Argument->getEndLoc();
  2078. }
  2079. static bool classof(const Stmt *T) {
  2080. return T->getStmtClass() == CXXDeleteExprClass;
  2081. }
  2082. // Iterators
  2083. child_range children() { return child_range(&Argument, &Argument + 1); }
  2084. const_child_range children() const {
  2085. return const_child_range(&Argument, &Argument + 1);
  2086. }
  2087. };
  2088. /// Stores the type being destroyed by a pseudo-destructor expression.
  2089. class PseudoDestructorTypeStorage {
  2090. /// Either the type source information or the name of the type, if
  2091. /// it couldn't be resolved due to type-dependence.
  2092. llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type;
  2093. /// The starting source location of the pseudo-destructor type.
  2094. SourceLocation Location;
  2095. public:
  2096. PseudoDestructorTypeStorage() = default;
  2097. PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc)
  2098. : Type(II), Location(Loc) {}
  2099. PseudoDestructorTypeStorage(TypeSourceInfo *Info);
  2100. TypeSourceInfo *getTypeSourceInfo() const {
  2101. return Type.dyn_cast<TypeSourceInfo *>();
  2102. }
  2103. IdentifierInfo *getIdentifier() const {
  2104. return Type.dyn_cast<IdentifierInfo *>();
  2105. }
  2106. SourceLocation getLocation() const { return Location; }
  2107. };
  2108. /// Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
  2109. ///
  2110. /// A pseudo-destructor is an expression that looks like a member access to a
  2111. /// destructor of a scalar type, except that scalar types don't have
  2112. /// destructors. For example:
  2113. ///
  2114. /// \code
  2115. /// typedef int T;
  2116. /// void f(int *p) {
  2117. /// p->T::~T();
  2118. /// }
  2119. /// \endcode
  2120. ///
  2121. /// Pseudo-destructors typically occur when instantiating templates such as:
  2122. ///
  2123. /// \code
  2124. /// template<typename T>
  2125. /// void destroy(T* ptr) {
  2126. /// ptr->T::~T();
  2127. /// }
  2128. /// \endcode
  2129. ///
  2130. /// for scalar types. A pseudo-destructor expression has no run-time semantics
  2131. /// beyond evaluating the base expression.
  2132. class CXXPseudoDestructorExpr : public Expr {
  2133. friend class ASTStmtReader;
  2134. /// The base expression (that is being destroyed).
  2135. Stmt *Base = nullptr;
  2136. /// Whether the operator was an arrow ('->'); otherwise, it was a
  2137. /// period ('.').
  2138. bool IsArrow : 1;
  2139. /// The location of the '.' or '->' operator.
  2140. SourceLocation OperatorLoc;
  2141. /// The nested-name-specifier that follows the operator, if present.
  2142. NestedNameSpecifierLoc QualifierLoc;
  2143. /// The type that precedes the '::' in a qualified pseudo-destructor
  2144. /// expression.
  2145. TypeSourceInfo *ScopeType = nullptr;
  2146. /// The location of the '::' in a qualified pseudo-destructor
  2147. /// expression.
  2148. SourceLocation ColonColonLoc;
  2149. /// The location of the '~'.
  2150. SourceLocation TildeLoc;
  2151. /// The type being destroyed, or its name if we were unable to
  2152. /// resolve the name.
  2153. PseudoDestructorTypeStorage DestroyedType;
  2154. public:
  2155. CXXPseudoDestructorExpr(const ASTContext &Context,
  2156. Expr *Base, bool isArrow, SourceLocation OperatorLoc,
  2157. NestedNameSpecifierLoc QualifierLoc,
  2158. TypeSourceInfo *ScopeType,
  2159. SourceLocation ColonColonLoc,
  2160. SourceLocation TildeLoc,
  2161. PseudoDestructorTypeStorage DestroyedType);
  2162. explicit CXXPseudoDestructorExpr(EmptyShell Shell)
  2163. : Expr(CXXPseudoDestructorExprClass, Shell), IsArrow(false) {}
  2164. Expr *getBase() const { return cast<Expr>(Base); }
  2165. /// Determines whether this member expression actually had
  2166. /// a C++ nested-name-specifier prior to the name of the member, e.g.,
  2167. /// x->Base::foo.
  2168. bool hasQualifier() const { return QualifierLoc.hasQualifier(); }
  2169. /// Retrieves the nested-name-specifier that qualifies the type name,
  2170. /// with source-location information.
  2171. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  2172. /// If the member name was qualified, retrieves the
  2173. /// nested-name-specifier that precedes the member name. Otherwise, returns
  2174. /// null.
  2175. NestedNameSpecifier *getQualifier() const {
  2176. return QualifierLoc.getNestedNameSpecifier();
  2177. }
  2178. /// Determine whether this pseudo-destructor expression was written
  2179. /// using an '->' (otherwise, it used a '.').
  2180. bool isArrow() const { return IsArrow; }
  2181. /// Retrieve the location of the '.' or '->' operator.
  2182. SourceLocation getOperatorLoc() const { return OperatorLoc; }
  2183. /// Retrieve the scope type in a qualified pseudo-destructor
  2184. /// expression.
  2185. ///
  2186. /// Pseudo-destructor expressions can have extra qualification within them
  2187. /// that is not part of the nested-name-specifier, e.g., \c p->T::~T().
  2188. /// Here, if the object type of the expression is (or may be) a scalar type,
  2189. /// \p T may also be a scalar type and, therefore, cannot be part of a
  2190. /// nested-name-specifier. It is stored as the "scope type" of the pseudo-
  2191. /// destructor expression.
  2192. TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; }
  2193. /// Retrieve the location of the '::' in a qualified pseudo-destructor
  2194. /// expression.
  2195. SourceLocation getColonColonLoc() const { return ColonColonLoc; }
  2196. /// Retrieve the location of the '~'.
  2197. SourceLocation getTildeLoc() const { return TildeLoc; }
  2198. /// Retrieve the source location information for the type
  2199. /// being destroyed.
  2200. ///
  2201. /// This type-source information is available for non-dependent
  2202. /// pseudo-destructor expressions and some dependent pseudo-destructor
  2203. /// expressions. Returns null if we only have the identifier for a
  2204. /// dependent pseudo-destructor expression.
  2205. TypeSourceInfo *getDestroyedTypeInfo() const {
  2206. return DestroyedType.getTypeSourceInfo();
  2207. }
  2208. /// In a dependent pseudo-destructor expression for which we do not
  2209. /// have full type information on the destroyed type, provides the name
  2210. /// of the destroyed type.
  2211. IdentifierInfo *getDestroyedTypeIdentifier() const {
  2212. return DestroyedType.getIdentifier();
  2213. }
  2214. /// Retrieve the type being destroyed.
  2215. QualType getDestroyedType() const;
  2216. /// Retrieve the starting location of the type being destroyed.
  2217. SourceLocation getDestroyedTypeLoc() const {
  2218. return DestroyedType.getLocation();
  2219. }
  2220. /// Set the name of destroyed type for a dependent pseudo-destructor
  2221. /// expression.
  2222. void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) {
  2223. DestroyedType = PseudoDestructorTypeStorage(II, Loc);
  2224. }
  2225. /// Set the destroyed type.
  2226. void setDestroyedType(TypeSourceInfo *Info) {
  2227. DestroyedType = PseudoDestructorTypeStorage(Info);
  2228. }
  2229. SourceLocation getBeginLoc() const LLVM_READONLY {
  2230. return Base->getBeginLoc();
  2231. }
  2232. SourceLocation getEndLoc() const LLVM_READONLY;
  2233. static bool classof(const Stmt *T) {
  2234. return T->getStmtClass() == CXXPseudoDestructorExprClass;
  2235. }
  2236. // Iterators
  2237. child_range children() { return child_range(&Base, &Base + 1); }
  2238. const_child_range children() const {
  2239. return const_child_range(&Base, &Base + 1);
  2240. }
  2241. };
  2242. /// A type trait used in the implementation of various C++11 and
  2243. /// Library TR1 trait templates.
  2244. ///
  2245. /// \code
  2246. /// __is_pod(int) == true
  2247. /// __is_enum(std::string) == false
  2248. /// __is_trivially_constructible(vector<int>, int*, int*)
  2249. /// \endcode
  2250. class TypeTraitExpr final
  2251. : public Expr,
  2252. private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> {
  2253. /// The location of the type trait keyword.
  2254. SourceLocation Loc;
  2255. /// The location of the closing parenthesis.
  2256. SourceLocation RParenLoc;
  2257. // Note: The TypeSourceInfos for the arguments are allocated after the
  2258. // TypeTraitExpr.
  2259. TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
  2260. ArrayRef<TypeSourceInfo *> Args,
  2261. SourceLocation RParenLoc,
  2262. bool Value);
  2263. TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) {}
  2264. size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const {
  2265. return getNumArgs();
  2266. }
  2267. public:
  2268. friend class ASTStmtReader;
  2269. friend class ASTStmtWriter;
  2270. friend TrailingObjects;
  2271. /// Create a new type trait expression.
  2272. static TypeTraitExpr *Create(const ASTContext &C, QualType T,
  2273. SourceLocation Loc, TypeTrait Kind,
  2274. ArrayRef<TypeSourceInfo *> Args,
  2275. SourceLocation RParenLoc,
  2276. bool Value);
  2277. static TypeTraitExpr *CreateDeserialized(const ASTContext &C,
  2278. unsigned NumArgs);
  2279. /// Determine which type trait this expression uses.
  2280. TypeTrait getTrait() const {
  2281. return static_cast<TypeTrait>(TypeTraitExprBits.Kind);
  2282. }
  2283. bool getValue() const {
  2284. assert(!isValueDependent());
  2285. return TypeTraitExprBits.Value;
  2286. }
  2287. /// Determine the number of arguments to this type trait.
  2288. unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; }
  2289. /// Retrieve the Ith argument.
  2290. TypeSourceInfo *getArg(unsigned I) const {
  2291. assert(I < getNumArgs() && "Argument out-of-range");
  2292. return getArgs()[I];
  2293. }
  2294. /// Retrieve the argument types.
  2295. ArrayRef<TypeSourceInfo *> getArgs() const {
  2296. return llvm::ArrayRef(getTrailingObjects<TypeSourceInfo *>(), getNumArgs());
  2297. }
  2298. SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
  2299. SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
  2300. static bool classof(const Stmt *T) {
  2301. return T->getStmtClass() == TypeTraitExprClass;
  2302. }
  2303. // Iterators
  2304. child_range children() {
  2305. return child_range(child_iterator(), child_iterator());
  2306. }
  2307. const_child_range children() const {
  2308. return const_child_range(const_child_iterator(), const_child_iterator());
  2309. }
  2310. };
  2311. /// An Embarcadero array type trait, as used in the implementation of
  2312. /// __array_rank and __array_extent.
  2313. ///
  2314. /// Example:
  2315. /// \code
  2316. /// __array_rank(int[10][20]) == 2
  2317. /// __array_extent(int, 1) == 20
  2318. /// \endcode
  2319. class ArrayTypeTraitExpr : public Expr {
  2320. /// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
  2321. unsigned ATT : 2;
  2322. /// The value of the type trait. Unspecified if dependent.
  2323. uint64_t Value = 0;
  2324. /// The array dimension being queried, or -1 if not used.
  2325. Expr *Dimension;
  2326. /// The location of the type trait keyword.
  2327. SourceLocation Loc;
  2328. /// The location of the closing paren.
  2329. SourceLocation RParen;
  2330. /// The type being queried.
  2331. TypeSourceInfo *QueriedType = nullptr;
  2332. public:
  2333. friend class ASTStmtReader;
  2334. ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att,
  2335. TypeSourceInfo *queried, uint64_t value, Expr *dimension,
  2336. SourceLocation rparen, QualType ty)
  2337. : Expr(ArrayTypeTraitExprClass, ty, VK_PRValue, OK_Ordinary), ATT(att),
  2338. Value(value), Dimension(dimension), Loc(loc), RParen(rparen),
  2339. QueriedType(queried) {
  2340. assert(att <= ATT_Last && "invalid enum value!");
  2341. assert(static_cast<unsigned>(att) == ATT && "ATT overflow!");
  2342. setDependence(computeDependence(this));
  2343. }
  2344. explicit ArrayTypeTraitExpr(EmptyShell Empty)
  2345. : Expr(ArrayTypeTraitExprClass, Empty), ATT(0) {}
  2346. SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
  2347. SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
  2348. ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); }
  2349. QualType getQueriedType() const { return QueriedType->getType(); }
  2350. TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; }
  2351. uint64_t getValue() const { assert(!isTypeDependent()); return Value; }
  2352. Expr *getDimensionExpression() const { return Dimension; }
  2353. static bool classof(const Stmt *T) {
  2354. return T->getStmtClass() == ArrayTypeTraitExprClass;
  2355. }
  2356. // Iterators
  2357. child_range children() {
  2358. return child_range(child_iterator(), child_iterator());
  2359. }
  2360. const_child_range children() const {
  2361. return const_child_range(const_child_iterator(), const_child_iterator());
  2362. }
  2363. };
  2364. /// An expression trait intrinsic.
  2365. ///
  2366. /// Example:
  2367. /// \code
  2368. /// __is_lvalue_expr(std::cout) == true
  2369. /// __is_lvalue_expr(1) == false
  2370. /// \endcode
  2371. class ExpressionTraitExpr : public Expr {
  2372. /// The trait. A ExpressionTrait enum in MSVC compatible unsigned.
  2373. unsigned ET : 31;
  2374. /// The value of the type trait. Unspecified if dependent.
  2375. unsigned Value : 1;
  2376. /// The location of the type trait keyword.
  2377. SourceLocation Loc;
  2378. /// The location of the closing paren.
  2379. SourceLocation RParen;
  2380. /// The expression being queried.
  2381. Expr* QueriedExpression = nullptr;
  2382. public:
  2383. friend class ASTStmtReader;
  2384. ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et, Expr *queried,
  2385. bool value, SourceLocation rparen, QualType resultType)
  2386. : Expr(ExpressionTraitExprClass, resultType, VK_PRValue, OK_Ordinary),
  2387. ET(et), Value(value), Loc(loc), RParen(rparen),
  2388. QueriedExpression(queried) {
  2389. assert(et <= ET_Last && "invalid enum value!");
  2390. assert(static_cast<unsigned>(et) == ET && "ET overflow!");
  2391. setDependence(computeDependence(this));
  2392. }
  2393. explicit ExpressionTraitExpr(EmptyShell Empty)
  2394. : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false) {}
  2395. SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
  2396. SourceLocation getEndLoc() const LLVM_READONLY { return RParen; }
  2397. ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); }
  2398. Expr *getQueriedExpression() const { return QueriedExpression; }
  2399. bool getValue() const { return Value; }
  2400. static bool classof(const Stmt *T) {
  2401. return T->getStmtClass() == ExpressionTraitExprClass;
  2402. }
  2403. // Iterators
  2404. child_range children() {
  2405. return child_range(child_iterator(), child_iterator());
  2406. }
  2407. const_child_range children() const {
  2408. return const_child_range(const_child_iterator(), const_child_iterator());
  2409. }
  2410. };
  2411. /// A reference to an overloaded function set, either an
  2412. /// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr.
  2413. class OverloadExpr : public Expr {
  2414. friend class ASTStmtReader;
  2415. friend class ASTStmtWriter;
  2416. /// The common name of these declarations.
  2417. DeclarationNameInfo NameInfo;
  2418. /// The nested-name-specifier that qualifies the name, if any.
  2419. NestedNameSpecifierLoc QualifierLoc;
  2420. protected:
  2421. OverloadExpr(StmtClass SC, const ASTContext &Context,
  2422. NestedNameSpecifierLoc QualifierLoc,
  2423. SourceLocation TemplateKWLoc,
  2424. const DeclarationNameInfo &NameInfo,
  2425. const TemplateArgumentListInfo *TemplateArgs,
  2426. UnresolvedSetIterator Begin, UnresolvedSetIterator End,
  2427. bool KnownDependent, bool KnownInstantiationDependent,
  2428. bool KnownContainsUnexpandedParameterPack);
  2429. OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
  2430. bool HasTemplateKWAndArgsInfo);
  2431. /// Return the results. Defined after UnresolvedMemberExpr.
  2432. inline DeclAccessPair *getTrailingResults();
  2433. const DeclAccessPair *getTrailingResults() const {
  2434. return const_cast<OverloadExpr *>(this)->getTrailingResults();
  2435. }
  2436. /// Return the optional template keyword and arguments info.
  2437. /// Defined after UnresolvedMemberExpr.
  2438. inline ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo();
  2439. const ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo() const {
  2440. return const_cast<OverloadExpr *>(this)
  2441. ->getTrailingASTTemplateKWAndArgsInfo();
  2442. }
  2443. /// Return the optional template arguments. Defined after
  2444. /// UnresolvedMemberExpr.
  2445. inline TemplateArgumentLoc *getTrailingTemplateArgumentLoc();
  2446. const TemplateArgumentLoc *getTrailingTemplateArgumentLoc() const {
  2447. return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
  2448. }
  2449. bool hasTemplateKWAndArgsInfo() const {
  2450. return OverloadExprBits.HasTemplateKWAndArgsInfo;
  2451. }
  2452. public:
  2453. struct FindResult {
  2454. OverloadExpr *Expression;
  2455. bool IsAddressOfOperand;
  2456. bool HasFormOfMemberPointer;
  2457. };
  2458. /// Finds the overloaded expression in the given expression \p E of
  2459. /// OverloadTy.
  2460. ///
  2461. /// \return the expression (which must be there) and true if it has
  2462. /// the particular form of a member pointer expression
  2463. static FindResult find(Expr *E) {
  2464. assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload));
  2465. FindResult Result;
  2466. E = E->IgnoreParens();
  2467. if (isa<UnaryOperator>(E)) {
  2468. assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf);
  2469. E = cast<UnaryOperator>(E)->getSubExpr();
  2470. auto *Ovl = cast<OverloadExpr>(E->IgnoreParens());
  2471. Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier());
  2472. Result.IsAddressOfOperand = true;
  2473. Result.Expression = Ovl;
  2474. } else {
  2475. Result.HasFormOfMemberPointer = false;
  2476. Result.IsAddressOfOperand = false;
  2477. Result.Expression = cast<OverloadExpr>(E);
  2478. }
  2479. return Result;
  2480. }
  2481. /// Gets the naming class of this lookup, if any.
  2482. /// Defined after UnresolvedMemberExpr.
  2483. inline CXXRecordDecl *getNamingClass();
  2484. const CXXRecordDecl *getNamingClass() const {
  2485. return const_cast<OverloadExpr *>(this)->getNamingClass();
  2486. }
  2487. using decls_iterator = UnresolvedSetImpl::iterator;
  2488. decls_iterator decls_begin() const {
  2489. return UnresolvedSetIterator(getTrailingResults());
  2490. }
  2491. decls_iterator decls_end() const {
  2492. return UnresolvedSetIterator(getTrailingResults() + getNumDecls());
  2493. }
  2494. llvm::iterator_range<decls_iterator> decls() const {
  2495. return llvm::make_range(decls_begin(), decls_end());
  2496. }
  2497. /// Gets the number of declarations in the unresolved set.
  2498. unsigned getNumDecls() const { return OverloadExprBits.NumResults; }
  2499. /// Gets the full name info.
  2500. const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
  2501. /// Gets the name looked up.
  2502. DeclarationName getName() const { return NameInfo.getName(); }
  2503. /// Gets the location of the name.
  2504. SourceLocation getNameLoc() const { return NameInfo.getLoc(); }
  2505. /// Fetches the nested-name qualifier, if one was given.
  2506. NestedNameSpecifier *getQualifier() const {
  2507. return QualifierLoc.getNestedNameSpecifier();
  2508. }
  2509. /// Fetches the nested-name qualifier with source-location
  2510. /// information, if one was given.
  2511. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  2512. /// Retrieve the location of the template keyword preceding
  2513. /// this name, if any.
  2514. SourceLocation getTemplateKeywordLoc() const {
  2515. if (!hasTemplateKWAndArgsInfo())
  2516. return SourceLocation();
  2517. return getTrailingASTTemplateKWAndArgsInfo()->TemplateKWLoc;
  2518. }
  2519. /// Retrieve the location of the left angle bracket starting the
  2520. /// explicit template argument list following the name, if any.
  2521. SourceLocation getLAngleLoc() const {
  2522. if (!hasTemplateKWAndArgsInfo())
  2523. return SourceLocation();
  2524. return getTrailingASTTemplateKWAndArgsInfo()->LAngleLoc;
  2525. }
  2526. /// Retrieve the location of the right angle bracket ending the
  2527. /// explicit template argument list following the name, if any.
  2528. SourceLocation getRAngleLoc() const {
  2529. if (!hasTemplateKWAndArgsInfo())
  2530. return SourceLocation();
  2531. return getTrailingASTTemplateKWAndArgsInfo()->RAngleLoc;
  2532. }
  2533. /// Determines whether the name was preceded by the template keyword.
  2534. bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
  2535. /// Determines whether this expression had explicit template arguments.
  2536. bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
  2537. TemplateArgumentLoc const *getTemplateArgs() const {
  2538. if (!hasExplicitTemplateArgs())
  2539. return nullptr;
  2540. return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc();
  2541. }
  2542. unsigned getNumTemplateArgs() const {
  2543. if (!hasExplicitTemplateArgs())
  2544. return 0;
  2545. return getTrailingASTTemplateKWAndArgsInfo()->NumTemplateArgs;
  2546. }
  2547. ArrayRef<TemplateArgumentLoc> template_arguments() const {
  2548. return {getTemplateArgs(), getNumTemplateArgs()};
  2549. }
  2550. /// Copies the template arguments into the given structure.
  2551. void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
  2552. if (hasExplicitTemplateArgs())
  2553. getTrailingASTTemplateKWAndArgsInfo()->copyInto(getTemplateArgs(), List);
  2554. }
  2555. static bool classof(const Stmt *T) {
  2556. return T->getStmtClass() == UnresolvedLookupExprClass ||
  2557. T->getStmtClass() == UnresolvedMemberExprClass;
  2558. }
  2559. };
  2560. /// A reference to a name which we were able to look up during
  2561. /// parsing but could not resolve to a specific declaration.
  2562. ///
  2563. /// This arises in several ways:
  2564. /// * we might be waiting for argument-dependent lookup;
  2565. /// * the name might resolve to an overloaded function;
  2566. /// and eventually:
  2567. /// * the lookup might have included a function template.
  2568. ///
  2569. /// These never include UnresolvedUsingValueDecls, which are always class
  2570. /// members and therefore appear only in UnresolvedMemberLookupExprs.
  2571. class UnresolvedLookupExpr final
  2572. : public OverloadExpr,
  2573. private llvm::TrailingObjects<UnresolvedLookupExpr, DeclAccessPair,
  2574. ASTTemplateKWAndArgsInfo,
  2575. TemplateArgumentLoc> {
  2576. friend class ASTStmtReader;
  2577. friend class OverloadExpr;
  2578. friend TrailingObjects;
  2579. /// The naming class (C++ [class.access.base]p5) of the lookup, if
  2580. /// any. This can generally be recalculated from the context chain,
  2581. /// but that can be fairly expensive for unqualified lookups.
  2582. CXXRecordDecl *NamingClass;
  2583. // UnresolvedLookupExpr is followed by several trailing objects.
  2584. // They are in order:
  2585. //
  2586. // * An array of getNumResults() DeclAccessPair for the results. These are
  2587. // undesugared, which is to say, they may include UsingShadowDecls.
  2588. // Access is relative to the naming class.
  2589. //
  2590. // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
  2591. // template keyword and arguments. Present if and only if
  2592. // hasTemplateKWAndArgsInfo().
  2593. //
  2594. // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
  2595. // location information for the explicitly specified template arguments.
  2596. UnresolvedLookupExpr(const ASTContext &Context, CXXRecordDecl *NamingClass,
  2597. NestedNameSpecifierLoc QualifierLoc,
  2598. SourceLocation TemplateKWLoc,
  2599. const DeclarationNameInfo &NameInfo, bool RequiresADL,
  2600. bool Overloaded,
  2601. const TemplateArgumentListInfo *TemplateArgs,
  2602. UnresolvedSetIterator Begin, UnresolvedSetIterator End);
  2603. UnresolvedLookupExpr(EmptyShell Empty, unsigned NumResults,
  2604. bool HasTemplateKWAndArgsInfo);
  2605. unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
  2606. return getNumDecls();
  2607. }
  2608. unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
  2609. return hasTemplateKWAndArgsInfo();
  2610. }
  2611. public:
  2612. static UnresolvedLookupExpr *
  2613. Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
  2614. NestedNameSpecifierLoc QualifierLoc,
  2615. const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
  2616. UnresolvedSetIterator Begin, UnresolvedSetIterator End);
  2617. static UnresolvedLookupExpr *
  2618. Create(const ASTContext &Context, CXXRecordDecl *NamingClass,
  2619. NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
  2620. const DeclarationNameInfo &NameInfo, bool RequiresADL,
  2621. const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
  2622. UnresolvedSetIterator End);
  2623. static UnresolvedLookupExpr *CreateEmpty(const ASTContext &Context,
  2624. unsigned NumResults,
  2625. bool HasTemplateKWAndArgsInfo,
  2626. unsigned NumTemplateArgs);
  2627. /// True if this declaration should be extended by
  2628. /// argument-dependent lookup.
  2629. bool requiresADL() const { return UnresolvedLookupExprBits.RequiresADL; }
  2630. /// True if this lookup is overloaded.
  2631. bool isOverloaded() const { return UnresolvedLookupExprBits.Overloaded; }
  2632. /// Gets the 'naming class' (in the sense of C++0x
  2633. /// [class.access.base]p5) of the lookup. This is the scope
  2634. /// that was looked in to find these results.
  2635. CXXRecordDecl *getNamingClass() { return NamingClass; }
  2636. const CXXRecordDecl *getNamingClass() const { return NamingClass; }
  2637. SourceLocation getBeginLoc() const LLVM_READONLY {
  2638. if (NestedNameSpecifierLoc l = getQualifierLoc())
  2639. return l.getBeginLoc();
  2640. return getNameInfo().getBeginLoc();
  2641. }
  2642. SourceLocation getEndLoc() const LLVM_READONLY {
  2643. if (hasExplicitTemplateArgs())
  2644. return getRAngleLoc();
  2645. return getNameInfo().getEndLoc();
  2646. }
  2647. child_range children() {
  2648. return child_range(child_iterator(), child_iterator());
  2649. }
  2650. const_child_range children() const {
  2651. return const_child_range(const_child_iterator(), const_child_iterator());
  2652. }
  2653. static bool classof(const Stmt *T) {
  2654. return T->getStmtClass() == UnresolvedLookupExprClass;
  2655. }
  2656. };
  2657. /// A qualified reference to a name whose declaration cannot
  2658. /// yet be resolved.
  2659. ///
  2660. /// DependentScopeDeclRefExpr is similar to DeclRefExpr in that
  2661. /// it expresses a reference to a declaration such as
  2662. /// X<T>::value. The difference, however, is that an
  2663. /// DependentScopeDeclRefExpr node is used only within C++ templates when
  2664. /// the qualification (e.g., X<T>::) refers to a dependent type. In
  2665. /// this case, X<T>::value cannot resolve to a declaration because the
  2666. /// declaration will differ from one instantiation of X<T> to the
  2667. /// next. Therefore, DependentScopeDeclRefExpr keeps track of the
  2668. /// qualifier (X<T>::) and the name of the entity being referenced
  2669. /// ("value"). Such expressions will instantiate to a DeclRefExpr once the
  2670. /// declaration can be found.
  2671. class DependentScopeDeclRefExpr final
  2672. : public Expr,
  2673. private llvm::TrailingObjects<DependentScopeDeclRefExpr,
  2674. ASTTemplateKWAndArgsInfo,
  2675. TemplateArgumentLoc> {
  2676. friend class ASTStmtReader;
  2677. friend class ASTStmtWriter;
  2678. friend TrailingObjects;
  2679. /// The nested-name-specifier that qualifies this unresolved
  2680. /// declaration name.
  2681. NestedNameSpecifierLoc QualifierLoc;
  2682. /// The name of the entity we will be referencing.
  2683. DeclarationNameInfo NameInfo;
  2684. DependentScopeDeclRefExpr(QualType Ty, NestedNameSpecifierLoc QualifierLoc,
  2685. SourceLocation TemplateKWLoc,
  2686. const DeclarationNameInfo &NameInfo,
  2687. const TemplateArgumentListInfo *Args);
  2688. size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
  2689. return hasTemplateKWAndArgsInfo();
  2690. }
  2691. bool hasTemplateKWAndArgsInfo() const {
  2692. return DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo;
  2693. }
  2694. public:
  2695. static DependentScopeDeclRefExpr *
  2696. Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
  2697. SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
  2698. const TemplateArgumentListInfo *TemplateArgs);
  2699. static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &Context,
  2700. bool HasTemplateKWAndArgsInfo,
  2701. unsigned NumTemplateArgs);
  2702. /// Retrieve the name that this expression refers to.
  2703. const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
  2704. /// Retrieve the name that this expression refers to.
  2705. DeclarationName getDeclName() const { return NameInfo.getName(); }
  2706. /// Retrieve the location of the name within the expression.
  2707. ///
  2708. /// For example, in "X<T>::value" this is the location of "value".
  2709. SourceLocation getLocation() const { return NameInfo.getLoc(); }
  2710. /// Retrieve the nested-name-specifier that qualifies the
  2711. /// name, with source location information.
  2712. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  2713. /// Retrieve the nested-name-specifier that qualifies this
  2714. /// declaration.
  2715. NestedNameSpecifier *getQualifier() const {
  2716. return QualifierLoc.getNestedNameSpecifier();
  2717. }
  2718. /// Retrieve the location of the template keyword preceding
  2719. /// this name, if any.
  2720. SourceLocation getTemplateKeywordLoc() const {
  2721. if (!hasTemplateKWAndArgsInfo())
  2722. return SourceLocation();
  2723. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
  2724. }
  2725. /// Retrieve the location of the left angle bracket starting the
  2726. /// explicit template argument list following the name, if any.
  2727. SourceLocation getLAngleLoc() const {
  2728. if (!hasTemplateKWAndArgsInfo())
  2729. return SourceLocation();
  2730. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
  2731. }
  2732. /// Retrieve the location of the right angle bracket ending the
  2733. /// explicit template argument list following the name, if any.
  2734. SourceLocation getRAngleLoc() const {
  2735. if (!hasTemplateKWAndArgsInfo())
  2736. return SourceLocation();
  2737. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
  2738. }
  2739. /// Determines whether the name was preceded by the template keyword.
  2740. bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
  2741. /// Determines whether this lookup had explicit template arguments.
  2742. bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
  2743. /// Copies the template arguments (if present) into the given
  2744. /// structure.
  2745. void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
  2746. if (hasExplicitTemplateArgs())
  2747. getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
  2748. getTrailingObjects<TemplateArgumentLoc>(), List);
  2749. }
  2750. TemplateArgumentLoc const *getTemplateArgs() const {
  2751. if (!hasExplicitTemplateArgs())
  2752. return nullptr;
  2753. return getTrailingObjects<TemplateArgumentLoc>();
  2754. }
  2755. unsigned getNumTemplateArgs() const {
  2756. if (!hasExplicitTemplateArgs())
  2757. return 0;
  2758. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
  2759. }
  2760. ArrayRef<TemplateArgumentLoc> template_arguments() const {
  2761. return {getTemplateArgs(), getNumTemplateArgs()};
  2762. }
  2763. /// Note: getBeginLoc() is the start of the whole DependentScopeDeclRefExpr,
  2764. /// and differs from getLocation().getStart().
  2765. SourceLocation getBeginLoc() const LLVM_READONLY {
  2766. return QualifierLoc.getBeginLoc();
  2767. }
  2768. SourceLocation getEndLoc() const LLVM_READONLY {
  2769. if (hasExplicitTemplateArgs())
  2770. return getRAngleLoc();
  2771. return getLocation();
  2772. }
  2773. static bool classof(const Stmt *T) {
  2774. return T->getStmtClass() == DependentScopeDeclRefExprClass;
  2775. }
  2776. child_range children() {
  2777. return child_range(child_iterator(), child_iterator());
  2778. }
  2779. const_child_range children() const {
  2780. return const_child_range(const_child_iterator(), const_child_iterator());
  2781. }
  2782. };
  2783. /// Represents an expression -- generally a full-expression -- that
  2784. /// introduces cleanups to be run at the end of the sub-expression's
  2785. /// evaluation. The most common source of expression-introduced
  2786. /// cleanups is temporary objects in C++, but several other kinds of
  2787. /// expressions can create cleanups, including basically every
  2788. /// call in ARC that returns an Objective-C pointer.
  2789. ///
  2790. /// This expression also tracks whether the sub-expression contains a
  2791. /// potentially-evaluated block literal. The lifetime of a block
  2792. /// literal is the extent of the enclosing scope.
  2793. class ExprWithCleanups final
  2794. : public FullExpr,
  2795. private llvm::TrailingObjects<
  2796. ExprWithCleanups,
  2797. llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>> {
  2798. public:
  2799. /// The type of objects that are kept in the cleanup.
  2800. /// It's useful to remember the set of blocks and block-scoped compound
  2801. /// literals; we could also remember the set of temporaries, but there's
  2802. /// currently no need.
  2803. using CleanupObject = llvm::PointerUnion<BlockDecl *, CompoundLiteralExpr *>;
  2804. private:
  2805. friend class ASTStmtReader;
  2806. friend TrailingObjects;
  2807. ExprWithCleanups(EmptyShell, unsigned NumObjects);
  2808. ExprWithCleanups(Expr *SubExpr, bool CleanupsHaveSideEffects,
  2809. ArrayRef<CleanupObject> Objects);
  2810. public:
  2811. static ExprWithCleanups *Create(const ASTContext &C, EmptyShell empty,
  2812. unsigned numObjects);
  2813. static ExprWithCleanups *Create(const ASTContext &C, Expr *subexpr,
  2814. bool CleanupsHaveSideEffects,
  2815. ArrayRef<CleanupObject> objects);
  2816. ArrayRef<CleanupObject> getObjects() const {
  2817. return llvm::ArrayRef(getTrailingObjects<CleanupObject>(), getNumObjects());
  2818. }
  2819. unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; }
  2820. CleanupObject getObject(unsigned i) const {
  2821. assert(i < getNumObjects() && "Index out of range");
  2822. return getObjects()[i];
  2823. }
  2824. bool cleanupsHaveSideEffects() const {
  2825. return ExprWithCleanupsBits.CleanupsHaveSideEffects;
  2826. }
  2827. SourceLocation getBeginLoc() const LLVM_READONLY {
  2828. return SubExpr->getBeginLoc();
  2829. }
  2830. SourceLocation getEndLoc() const LLVM_READONLY {
  2831. return SubExpr->getEndLoc();
  2832. }
  2833. // Implement isa/cast/dyncast/etc.
  2834. static bool classof(const Stmt *T) {
  2835. return T->getStmtClass() == ExprWithCleanupsClass;
  2836. }
  2837. // Iterators
  2838. child_range children() { return child_range(&SubExpr, &SubExpr + 1); }
  2839. const_child_range children() const {
  2840. return const_child_range(&SubExpr, &SubExpr + 1);
  2841. }
  2842. };
  2843. /// Describes an explicit type conversion that uses functional
  2844. /// notion but could not be resolved because one or more arguments are
  2845. /// type-dependent.
  2846. ///
  2847. /// The explicit type conversions expressed by
  2848. /// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>,
  2849. /// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and
  2850. /// either \c T is a dependent type or one or more of the <tt>a</tt>'s is
  2851. /// type-dependent. For example, this would occur in a template such
  2852. /// as:
  2853. ///
  2854. /// \code
  2855. /// template<typename T, typename A1>
  2856. /// inline T make_a(const A1& a1) {
  2857. /// return T(a1);
  2858. /// }
  2859. /// \endcode
  2860. ///
  2861. /// When the returned expression is instantiated, it may resolve to a
  2862. /// constructor call, conversion function call, or some kind of type
  2863. /// conversion.
  2864. class CXXUnresolvedConstructExpr final
  2865. : public Expr,
  2866. private llvm::TrailingObjects<CXXUnresolvedConstructExpr, Expr *> {
  2867. friend class ASTStmtReader;
  2868. friend TrailingObjects;
  2869. /// The type being constructed.
  2870. TypeSourceInfo *TSI;
  2871. /// The location of the left parentheses ('(').
  2872. SourceLocation LParenLoc;
  2873. /// The location of the right parentheses (')').
  2874. SourceLocation RParenLoc;
  2875. CXXUnresolvedConstructExpr(QualType T, TypeSourceInfo *TSI,
  2876. SourceLocation LParenLoc, ArrayRef<Expr *> Args,
  2877. SourceLocation RParenLoc);
  2878. CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs)
  2879. : Expr(CXXUnresolvedConstructExprClass, Empty), TSI(nullptr) {
  2880. CXXUnresolvedConstructExprBits.NumArgs = NumArgs;
  2881. }
  2882. public:
  2883. static CXXUnresolvedConstructExpr *Create(const ASTContext &Context,
  2884. QualType T, TypeSourceInfo *TSI,
  2885. SourceLocation LParenLoc,
  2886. ArrayRef<Expr *> Args,
  2887. SourceLocation RParenLoc);
  2888. static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &Context,
  2889. unsigned NumArgs);
  2890. /// Retrieve the type that is being constructed, as specified
  2891. /// in the source code.
  2892. QualType getTypeAsWritten() const { return TSI->getType(); }
  2893. /// Retrieve the type source information for the type being
  2894. /// constructed.
  2895. TypeSourceInfo *getTypeSourceInfo() const { return TSI; }
  2896. /// Retrieve the location of the left parentheses ('(') that
  2897. /// precedes the argument list.
  2898. SourceLocation getLParenLoc() const { return LParenLoc; }
  2899. void setLParenLoc(SourceLocation L) { LParenLoc = L; }
  2900. /// Retrieve the location of the right parentheses (')') that
  2901. /// follows the argument list.
  2902. SourceLocation getRParenLoc() const { return RParenLoc; }
  2903. void setRParenLoc(SourceLocation L) { RParenLoc = L; }
  2904. /// Determine whether this expression models list-initialization.
  2905. /// If so, there will be exactly one subexpression, which will be
  2906. /// an InitListExpr.
  2907. bool isListInitialization() const { return LParenLoc.isInvalid(); }
  2908. /// Retrieve the number of arguments.
  2909. unsigned getNumArgs() const { return CXXUnresolvedConstructExprBits.NumArgs; }
  2910. using arg_iterator = Expr **;
  2911. using arg_range = llvm::iterator_range<arg_iterator>;
  2912. arg_iterator arg_begin() { return getTrailingObjects<Expr *>(); }
  2913. arg_iterator arg_end() { return arg_begin() + getNumArgs(); }
  2914. arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
  2915. using const_arg_iterator = const Expr* const *;
  2916. using const_arg_range = llvm::iterator_range<const_arg_iterator>;
  2917. const_arg_iterator arg_begin() const { return getTrailingObjects<Expr *>(); }
  2918. const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); }
  2919. const_arg_range arguments() const {
  2920. return const_arg_range(arg_begin(), arg_end());
  2921. }
  2922. Expr *getArg(unsigned I) {
  2923. assert(I < getNumArgs() && "Argument index out-of-range");
  2924. return arg_begin()[I];
  2925. }
  2926. const Expr *getArg(unsigned I) const {
  2927. assert(I < getNumArgs() && "Argument index out-of-range");
  2928. return arg_begin()[I];
  2929. }
  2930. void setArg(unsigned I, Expr *E) {
  2931. assert(I < getNumArgs() && "Argument index out-of-range");
  2932. arg_begin()[I] = E;
  2933. }
  2934. SourceLocation getBeginLoc() const LLVM_READONLY;
  2935. SourceLocation getEndLoc() const LLVM_READONLY {
  2936. if (!RParenLoc.isValid() && getNumArgs() > 0)
  2937. return getArg(getNumArgs() - 1)->getEndLoc();
  2938. return RParenLoc;
  2939. }
  2940. static bool classof(const Stmt *T) {
  2941. return T->getStmtClass() == CXXUnresolvedConstructExprClass;
  2942. }
  2943. // Iterators
  2944. child_range children() {
  2945. auto **begin = reinterpret_cast<Stmt **>(arg_begin());
  2946. return child_range(begin, begin + getNumArgs());
  2947. }
  2948. const_child_range children() const {
  2949. auto **begin = reinterpret_cast<Stmt **>(
  2950. const_cast<CXXUnresolvedConstructExpr *>(this)->arg_begin());
  2951. return const_child_range(begin, begin + getNumArgs());
  2952. }
  2953. };
  2954. /// Represents a C++ member access expression where the actual
  2955. /// member referenced could not be resolved because the base
  2956. /// expression or the member name was dependent.
  2957. ///
  2958. /// Like UnresolvedMemberExprs, these can be either implicit or
  2959. /// explicit accesses. It is only possible to get one of these with
  2960. /// an implicit access if a qualifier is provided.
  2961. class CXXDependentScopeMemberExpr final
  2962. : public Expr,
  2963. private llvm::TrailingObjects<CXXDependentScopeMemberExpr,
  2964. ASTTemplateKWAndArgsInfo,
  2965. TemplateArgumentLoc, NamedDecl *> {
  2966. friend class ASTStmtReader;
  2967. friend class ASTStmtWriter;
  2968. friend TrailingObjects;
  2969. /// The expression for the base pointer or class reference,
  2970. /// e.g., the \c x in x.f. Can be null in implicit accesses.
  2971. Stmt *Base;
  2972. /// The type of the base expression. Never null, even for
  2973. /// implicit accesses.
  2974. QualType BaseType;
  2975. /// The nested-name-specifier that precedes the member name, if any.
  2976. /// FIXME: This could be in principle store as a trailing object.
  2977. /// However the performance impact of doing so should be investigated first.
  2978. NestedNameSpecifierLoc QualifierLoc;
  2979. /// The member to which this member expression refers, which
  2980. /// can be name, overloaded operator, or destructor.
  2981. ///
  2982. /// FIXME: could also be a template-id
  2983. DeclarationNameInfo MemberNameInfo;
  2984. // CXXDependentScopeMemberExpr is followed by several trailing objects,
  2985. // some of which optional. They are in order:
  2986. //
  2987. // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
  2988. // template keyword and arguments. Present if and only if
  2989. // hasTemplateKWAndArgsInfo().
  2990. //
  2991. // * An array of getNumTemplateArgs() TemplateArgumentLoc containing location
  2992. // information for the explicitly specified template arguments.
  2993. //
  2994. // * An optional NamedDecl *. In a qualified member access expression such
  2995. // as t->Base::f, this member stores the resolves of name lookup in the
  2996. // context of the member access expression, to be used at instantiation
  2997. // time. Present if and only if hasFirstQualifierFoundInScope().
  2998. bool hasTemplateKWAndArgsInfo() const {
  2999. return CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo;
  3000. }
  3001. bool hasFirstQualifierFoundInScope() const {
  3002. return CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope;
  3003. }
  3004. unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
  3005. return hasTemplateKWAndArgsInfo();
  3006. }
  3007. unsigned numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
  3008. return getNumTemplateArgs();
  3009. }
  3010. unsigned numTrailingObjects(OverloadToken<NamedDecl *>) const {
  3011. return hasFirstQualifierFoundInScope();
  3012. }
  3013. CXXDependentScopeMemberExpr(const ASTContext &Ctx, Expr *Base,
  3014. QualType BaseType, bool IsArrow,
  3015. SourceLocation OperatorLoc,
  3016. NestedNameSpecifierLoc QualifierLoc,
  3017. SourceLocation TemplateKWLoc,
  3018. NamedDecl *FirstQualifierFoundInScope,
  3019. DeclarationNameInfo MemberNameInfo,
  3020. const TemplateArgumentListInfo *TemplateArgs);
  3021. CXXDependentScopeMemberExpr(EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
  3022. bool HasFirstQualifierFoundInScope);
  3023. public:
  3024. static CXXDependentScopeMemberExpr *
  3025. Create(const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
  3026. SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
  3027. SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
  3028. DeclarationNameInfo MemberNameInfo,
  3029. const TemplateArgumentListInfo *TemplateArgs);
  3030. static CXXDependentScopeMemberExpr *
  3031. CreateEmpty(const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
  3032. unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope);
  3033. /// True if this is an implicit access, i.e. one in which the
  3034. /// member being accessed was not written in the source. The source
  3035. /// location of the operator is invalid in this case.
  3036. bool isImplicitAccess() const {
  3037. if (!Base)
  3038. return true;
  3039. return cast<Expr>(Base)->isImplicitCXXThis();
  3040. }
  3041. /// Retrieve the base object of this member expressions,
  3042. /// e.g., the \c x in \c x.m.
  3043. Expr *getBase() const {
  3044. assert(!isImplicitAccess());
  3045. return cast<Expr>(Base);
  3046. }
  3047. QualType getBaseType() const { return BaseType; }
  3048. /// Determine whether this member expression used the '->'
  3049. /// operator; otherwise, it used the '.' operator.
  3050. bool isArrow() const { return CXXDependentScopeMemberExprBits.IsArrow; }
  3051. /// Retrieve the location of the '->' or '.' operator.
  3052. SourceLocation getOperatorLoc() const {
  3053. return CXXDependentScopeMemberExprBits.OperatorLoc;
  3054. }
  3055. /// Retrieve the nested-name-specifier that qualifies the member name.
  3056. NestedNameSpecifier *getQualifier() const {
  3057. return QualifierLoc.getNestedNameSpecifier();
  3058. }
  3059. /// Retrieve the nested-name-specifier that qualifies the member
  3060. /// name, with source location information.
  3061. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  3062. /// Retrieve the first part of the nested-name-specifier that was
  3063. /// found in the scope of the member access expression when the member access
  3064. /// was initially parsed.
  3065. ///
  3066. /// This function only returns a useful result when member access expression
  3067. /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration
  3068. /// returned by this function describes what was found by unqualified name
  3069. /// lookup for the identifier "Base" within the scope of the member access
  3070. /// expression itself. At template instantiation time, this information is
  3071. /// combined with the results of name lookup into the type of the object
  3072. /// expression itself (the class type of x).
  3073. NamedDecl *getFirstQualifierFoundInScope() const {
  3074. if (!hasFirstQualifierFoundInScope())
  3075. return nullptr;
  3076. return *getTrailingObjects<NamedDecl *>();
  3077. }
  3078. /// Retrieve the name of the member that this expression refers to.
  3079. const DeclarationNameInfo &getMemberNameInfo() const {
  3080. return MemberNameInfo;
  3081. }
  3082. /// Retrieve the name of the member that this expression refers to.
  3083. DeclarationName getMember() const { return MemberNameInfo.getName(); }
  3084. // Retrieve the location of the name of the member that this
  3085. // expression refers to.
  3086. SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); }
  3087. /// Retrieve the location of the template keyword preceding the
  3088. /// member name, if any.
  3089. SourceLocation getTemplateKeywordLoc() const {
  3090. if (!hasTemplateKWAndArgsInfo())
  3091. return SourceLocation();
  3092. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc;
  3093. }
  3094. /// Retrieve the location of the left angle bracket starting the
  3095. /// explicit template argument list following the member name, if any.
  3096. SourceLocation getLAngleLoc() const {
  3097. if (!hasTemplateKWAndArgsInfo())
  3098. return SourceLocation();
  3099. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc;
  3100. }
  3101. /// Retrieve the location of the right angle bracket ending the
  3102. /// explicit template argument list following the member name, if any.
  3103. SourceLocation getRAngleLoc() const {
  3104. if (!hasTemplateKWAndArgsInfo())
  3105. return SourceLocation();
  3106. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc;
  3107. }
  3108. /// Determines whether the member name was preceded by the template keyword.
  3109. bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); }
  3110. /// Determines whether this member expression actually had a C++
  3111. /// template argument list explicitly specified, e.g., x.f<int>.
  3112. bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); }
  3113. /// Copies the template arguments (if present) into the given
  3114. /// structure.
  3115. void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
  3116. if (hasExplicitTemplateArgs())
  3117. getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto(
  3118. getTrailingObjects<TemplateArgumentLoc>(), List);
  3119. }
  3120. /// Retrieve the template arguments provided as part of this
  3121. /// template-id.
  3122. const TemplateArgumentLoc *getTemplateArgs() const {
  3123. if (!hasExplicitTemplateArgs())
  3124. return nullptr;
  3125. return getTrailingObjects<TemplateArgumentLoc>();
  3126. }
  3127. /// Retrieve the number of template arguments provided as part of this
  3128. /// template-id.
  3129. unsigned getNumTemplateArgs() const {
  3130. if (!hasExplicitTemplateArgs())
  3131. return 0;
  3132. return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs;
  3133. }
  3134. ArrayRef<TemplateArgumentLoc> template_arguments() const {
  3135. return {getTemplateArgs(), getNumTemplateArgs()};
  3136. }
  3137. SourceLocation getBeginLoc() const LLVM_READONLY {
  3138. if (!isImplicitAccess())
  3139. return Base->getBeginLoc();
  3140. if (getQualifier())
  3141. return getQualifierLoc().getBeginLoc();
  3142. return MemberNameInfo.getBeginLoc();
  3143. }
  3144. SourceLocation getEndLoc() const LLVM_READONLY {
  3145. if (hasExplicitTemplateArgs())
  3146. return getRAngleLoc();
  3147. return MemberNameInfo.getEndLoc();
  3148. }
  3149. static bool classof(const Stmt *T) {
  3150. return T->getStmtClass() == CXXDependentScopeMemberExprClass;
  3151. }
  3152. // Iterators
  3153. child_range children() {
  3154. if (isImplicitAccess())
  3155. return child_range(child_iterator(), child_iterator());
  3156. return child_range(&Base, &Base + 1);
  3157. }
  3158. const_child_range children() const {
  3159. if (isImplicitAccess())
  3160. return const_child_range(const_child_iterator(), const_child_iterator());
  3161. return const_child_range(&Base, &Base + 1);
  3162. }
  3163. };
  3164. /// Represents a C++ member access expression for which lookup
  3165. /// produced a set of overloaded functions.
  3166. ///
  3167. /// The member access may be explicit or implicit:
  3168. /// \code
  3169. /// struct A {
  3170. /// int a, b;
  3171. /// int explicitAccess() { return this->a + this->A::b; }
  3172. /// int implicitAccess() { return a + A::b; }
  3173. /// };
  3174. /// \endcode
  3175. ///
  3176. /// In the final AST, an explicit access always becomes a MemberExpr.
  3177. /// An implicit access may become either a MemberExpr or a
  3178. /// DeclRefExpr, depending on whether the member is static.
  3179. class UnresolvedMemberExpr final
  3180. : public OverloadExpr,
  3181. private llvm::TrailingObjects<UnresolvedMemberExpr, DeclAccessPair,
  3182. ASTTemplateKWAndArgsInfo,
  3183. TemplateArgumentLoc> {
  3184. friend class ASTStmtReader;
  3185. friend class OverloadExpr;
  3186. friend TrailingObjects;
  3187. /// The expression for the base pointer or class reference,
  3188. /// e.g., the \c x in x.f.
  3189. ///
  3190. /// This can be null if this is an 'unbased' member expression.
  3191. Stmt *Base;
  3192. /// The type of the base expression; never null.
  3193. QualType BaseType;
  3194. /// The location of the '->' or '.' operator.
  3195. SourceLocation OperatorLoc;
  3196. // UnresolvedMemberExpr is followed by several trailing objects.
  3197. // They are in order:
  3198. //
  3199. // * An array of getNumResults() DeclAccessPair for the results. These are
  3200. // undesugared, which is to say, they may include UsingShadowDecls.
  3201. // Access is relative to the naming class.
  3202. //
  3203. // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified
  3204. // template keyword and arguments. Present if and only if
  3205. // hasTemplateKWAndArgsInfo().
  3206. //
  3207. // * An array of getNumTemplateArgs() TemplateArgumentLoc containing
  3208. // location information for the explicitly specified template arguments.
  3209. UnresolvedMemberExpr(const ASTContext &Context, bool HasUnresolvedUsing,
  3210. Expr *Base, QualType BaseType, bool IsArrow,
  3211. SourceLocation OperatorLoc,
  3212. NestedNameSpecifierLoc QualifierLoc,
  3213. SourceLocation TemplateKWLoc,
  3214. const DeclarationNameInfo &MemberNameInfo,
  3215. const TemplateArgumentListInfo *TemplateArgs,
  3216. UnresolvedSetIterator Begin, UnresolvedSetIterator End);
  3217. UnresolvedMemberExpr(EmptyShell Empty, unsigned NumResults,
  3218. bool HasTemplateKWAndArgsInfo);
  3219. unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const {
  3220. return getNumDecls();
  3221. }
  3222. unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const {
  3223. return hasTemplateKWAndArgsInfo();
  3224. }
  3225. public:
  3226. static UnresolvedMemberExpr *
  3227. Create(const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
  3228. QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
  3229. NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
  3230. const DeclarationNameInfo &MemberNameInfo,
  3231. const TemplateArgumentListInfo *TemplateArgs,
  3232. UnresolvedSetIterator Begin, UnresolvedSetIterator End);
  3233. static UnresolvedMemberExpr *CreateEmpty(const ASTContext &Context,
  3234. unsigned NumResults,
  3235. bool HasTemplateKWAndArgsInfo,
  3236. unsigned NumTemplateArgs);
  3237. /// True if this is an implicit access, i.e., one in which the
  3238. /// member being accessed was not written in the source.
  3239. ///
  3240. /// The source location of the operator is invalid in this case.
  3241. bool isImplicitAccess() const;
  3242. /// Retrieve the base object of this member expressions,
  3243. /// e.g., the \c x in \c x.m.
  3244. Expr *getBase() {
  3245. assert(!isImplicitAccess());
  3246. return cast<Expr>(Base);
  3247. }
  3248. const Expr *getBase() const {
  3249. assert(!isImplicitAccess());
  3250. return cast<Expr>(Base);
  3251. }
  3252. QualType getBaseType() const { return BaseType; }
  3253. /// Determine whether the lookup results contain an unresolved using
  3254. /// declaration.
  3255. bool hasUnresolvedUsing() const {
  3256. return UnresolvedMemberExprBits.HasUnresolvedUsing;
  3257. }
  3258. /// Determine whether this member expression used the '->'
  3259. /// operator; otherwise, it used the '.' operator.
  3260. bool isArrow() const { return UnresolvedMemberExprBits.IsArrow; }
  3261. /// Retrieve the location of the '->' or '.' operator.
  3262. SourceLocation getOperatorLoc() const { return OperatorLoc; }
  3263. /// Retrieve the naming class of this lookup.
  3264. CXXRecordDecl *getNamingClass();
  3265. const CXXRecordDecl *getNamingClass() const {
  3266. return const_cast<UnresolvedMemberExpr *>(this)->getNamingClass();
  3267. }
  3268. /// Retrieve the full name info for the member that this expression
  3269. /// refers to.
  3270. const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); }
  3271. /// Retrieve the name of the member that this expression refers to.
  3272. DeclarationName getMemberName() const { return getName(); }
  3273. /// Retrieve the location of the name of the member that this
  3274. /// expression refers to.
  3275. SourceLocation getMemberLoc() const { return getNameLoc(); }
  3276. /// Return the preferred location (the member name) for the arrow when
  3277. /// diagnosing a problem with this expression.
  3278. SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); }
  3279. SourceLocation getBeginLoc() const LLVM_READONLY {
  3280. if (!isImplicitAccess())
  3281. return Base->getBeginLoc();
  3282. if (NestedNameSpecifierLoc l = getQualifierLoc())
  3283. return l.getBeginLoc();
  3284. return getMemberNameInfo().getBeginLoc();
  3285. }
  3286. SourceLocation getEndLoc() const LLVM_READONLY {
  3287. if (hasExplicitTemplateArgs())
  3288. return getRAngleLoc();
  3289. return getMemberNameInfo().getEndLoc();
  3290. }
  3291. static bool classof(const Stmt *T) {
  3292. return T->getStmtClass() == UnresolvedMemberExprClass;
  3293. }
  3294. // Iterators
  3295. child_range children() {
  3296. if (isImplicitAccess())
  3297. return child_range(child_iterator(), child_iterator());
  3298. return child_range(&Base, &Base + 1);
  3299. }
  3300. const_child_range children() const {
  3301. if (isImplicitAccess())
  3302. return const_child_range(const_child_iterator(), const_child_iterator());
  3303. return const_child_range(&Base, &Base + 1);
  3304. }
  3305. };
  3306. DeclAccessPair *OverloadExpr::getTrailingResults() {
  3307. if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
  3308. return ULE->getTrailingObjects<DeclAccessPair>();
  3309. return cast<UnresolvedMemberExpr>(this)->getTrailingObjects<DeclAccessPair>();
  3310. }
  3311. ASTTemplateKWAndArgsInfo *OverloadExpr::getTrailingASTTemplateKWAndArgsInfo() {
  3312. if (!hasTemplateKWAndArgsInfo())
  3313. return nullptr;
  3314. if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
  3315. return ULE->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
  3316. return cast<UnresolvedMemberExpr>(this)
  3317. ->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
  3318. }
  3319. TemplateArgumentLoc *OverloadExpr::getTrailingTemplateArgumentLoc() {
  3320. if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
  3321. return ULE->getTrailingObjects<TemplateArgumentLoc>();
  3322. return cast<UnresolvedMemberExpr>(this)
  3323. ->getTrailingObjects<TemplateArgumentLoc>();
  3324. }
  3325. CXXRecordDecl *OverloadExpr::getNamingClass() {
  3326. if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this))
  3327. return ULE->getNamingClass();
  3328. return cast<UnresolvedMemberExpr>(this)->getNamingClass();
  3329. }
  3330. /// Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
  3331. ///
  3332. /// The noexcept expression tests whether a given expression might throw. Its
  3333. /// result is a boolean constant.
  3334. class CXXNoexceptExpr : public Expr {
  3335. friend class ASTStmtReader;
  3336. Stmt *Operand;
  3337. SourceRange Range;
  3338. public:
  3339. CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val,
  3340. SourceLocation Keyword, SourceLocation RParen)
  3341. : Expr(CXXNoexceptExprClass, Ty, VK_PRValue, OK_Ordinary),
  3342. Operand(Operand), Range(Keyword, RParen) {
  3343. CXXNoexceptExprBits.Value = Val == CT_Cannot;
  3344. setDependence(computeDependence(this, Val));
  3345. }
  3346. CXXNoexceptExpr(EmptyShell Empty) : Expr(CXXNoexceptExprClass, Empty) {}
  3347. Expr *getOperand() const { return static_cast<Expr *>(Operand); }
  3348. SourceLocation getBeginLoc() const { return Range.getBegin(); }
  3349. SourceLocation getEndLoc() const { return Range.getEnd(); }
  3350. SourceRange getSourceRange() const { return Range; }
  3351. bool getValue() const { return CXXNoexceptExprBits.Value; }
  3352. static bool classof(const Stmt *T) {
  3353. return T->getStmtClass() == CXXNoexceptExprClass;
  3354. }
  3355. // Iterators
  3356. child_range children() { return child_range(&Operand, &Operand + 1); }
  3357. const_child_range children() const {
  3358. return const_child_range(&Operand, &Operand + 1);
  3359. }
  3360. };
  3361. /// Represents a C++11 pack expansion that produces a sequence of
  3362. /// expressions.
  3363. ///
  3364. /// A pack expansion expression contains a pattern (which itself is an
  3365. /// expression) followed by an ellipsis. For example:
  3366. ///
  3367. /// \code
  3368. /// template<typename F, typename ...Types>
  3369. /// void forward(F f, Types &&...args) {
  3370. /// f(static_cast<Types&&>(args)...);
  3371. /// }
  3372. /// \endcode
  3373. ///
  3374. /// Here, the argument to the function object \c f is a pack expansion whose
  3375. /// pattern is \c static_cast<Types&&>(args). When the \c forward function
  3376. /// template is instantiated, the pack expansion will instantiate to zero or
  3377. /// or more function arguments to the function object \c f.
  3378. class PackExpansionExpr : public Expr {
  3379. friend class ASTStmtReader;
  3380. friend class ASTStmtWriter;
  3381. SourceLocation EllipsisLoc;
  3382. /// The number of expansions that will be produced by this pack
  3383. /// expansion expression, if known.
  3384. ///
  3385. /// When zero, the number of expansions is not known. Otherwise, this value
  3386. /// is the number of expansions + 1.
  3387. unsigned NumExpansions;
  3388. Stmt *Pattern;
  3389. public:
  3390. PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
  3391. std::optional<unsigned> NumExpansions)
  3392. : Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
  3393. Pattern->getObjectKind()),
  3394. EllipsisLoc(EllipsisLoc),
  3395. NumExpansions(NumExpansions ? *NumExpansions + 1 : 0),
  3396. Pattern(Pattern) {
  3397. setDependence(computeDependence(this));
  3398. }
  3399. PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) {}
  3400. /// Retrieve the pattern of the pack expansion.
  3401. Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); }
  3402. /// Retrieve the pattern of the pack expansion.
  3403. const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); }
  3404. /// Retrieve the location of the ellipsis that describes this pack
  3405. /// expansion.
  3406. SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
  3407. /// Determine the number of expansions that will be produced when
  3408. /// this pack expansion is instantiated, if already known.
  3409. std::optional<unsigned> getNumExpansions() const {
  3410. if (NumExpansions)
  3411. return NumExpansions - 1;
  3412. return std::nullopt;
  3413. }
  3414. SourceLocation getBeginLoc() const LLVM_READONLY {
  3415. return Pattern->getBeginLoc();
  3416. }
  3417. SourceLocation getEndLoc() const LLVM_READONLY { return EllipsisLoc; }
  3418. static bool classof(const Stmt *T) {
  3419. return T->getStmtClass() == PackExpansionExprClass;
  3420. }
  3421. // Iterators
  3422. child_range children() {
  3423. return child_range(&Pattern, &Pattern + 1);
  3424. }
  3425. const_child_range children() const {
  3426. return const_child_range(&Pattern, &Pattern + 1);
  3427. }
  3428. };
  3429. /// Represents an expression that computes the length of a parameter
  3430. /// pack.
  3431. ///
  3432. /// \code
  3433. /// template<typename ...Types>
  3434. /// struct count {
  3435. /// static const unsigned value = sizeof...(Types);
  3436. /// };
  3437. /// \endcode
  3438. class SizeOfPackExpr final
  3439. : public Expr,
  3440. private llvm::TrailingObjects<SizeOfPackExpr, TemplateArgument> {
  3441. friend class ASTStmtReader;
  3442. friend class ASTStmtWriter;
  3443. friend TrailingObjects;
  3444. /// The location of the \c sizeof keyword.
  3445. SourceLocation OperatorLoc;
  3446. /// The location of the name of the parameter pack.
  3447. SourceLocation PackLoc;
  3448. /// The location of the closing parenthesis.
  3449. SourceLocation RParenLoc;
  3450. /// The length of the parameter pack, if known.
  3451. ///
  3452. /// When this expression is not value-dependent, this is the length of
  3453. /// the pack. When the expression was parsed rather than instantiated
  3454. /// (and thus is value-dependent), this is zero.
  3455. ///
  3456. /// After partial substitution into a sizeof...(X) expression (for instance,
  3457. /// within an alias template or during function template argument deduction),
  3458. /// we store a trailing array of partially-substituted TemplateArguments,
  3459. /// and this is the length of that array.
  3460. unsigned Length;
  3461. /// The parameter pack.
  3462. NamedDecl *Pack = nullptr;
  3463. /// Create an expression that computes the length of
  3464. /// the given parameter pack.
  3465. SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
  3466. SourceLocation PackLoc, SourceLocation RParenLoc,
  3467. std::optional<unsigned> Length,
  3468. ArrayRef<TemplateArgument> PartialArgs)
  3469. : Expr(SizeOfPackExprClass, SizeType, VK_PRValue, OK_Ordinary),
  3470. OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
  3471. Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
  3472. assert((!Length || PartialArgs.empty()) &&
  3473. "have partial args for non-dependent sizeof... expression");
  3474. auto *Args = getTrailingObjects<TemplateArgument>();
  3475. std::uninitialized_copy(PartialArgs.begin(), PartialArgs.end(), Args);
  3476. setDependence(Length ? ExprDependence::None
  3477. : ExprDependence::ValueInstantiation);
  3478. }
  3479. /// Create an empty expression.
  3480. SizeOfPackExpr(EmptyShell Empty, unsigned NumPartialArgs)
  3481. : Expr(SizeOfPackExprClass, Empty), Length(NumPartialArgs) {}
  3482. public:
  3483. static SizeOfPackExpr *
  3484. Create(ASTContext &Context, SourceLocation OperatorLoc, NamedDecl *Pack,
  3485. SourceLocation PackLoc, SourceLocation RParenLoc,
  3486. std::optional<unsigned> Length = std::nullopt,
  3487. ArrayRef<TemplateArgument> PartialArgs = std::nullopt);
  3488. static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
  3489. unsigned NumPartialArgs);
  3490. /// Determine the location of the 'sizeof' keyword.
  3491. SourceLocation getOperatorLoc() const { return OperatorLoc; }
  3492. /// Determine the location of the parameter pack.
  3493. SourceLocation getPackLoc() const { return PackLoc; }
  3494. /// Determine the location of the right parenthesis.
  3495. SourceLocation getRParenLoc() const { return RParenLoc; }
  3496. /// Retrieve the parameter pack.
  3497. NamedDecl *getPack() const { return Pack; }
  3498. /// Retrieve the length of the parameter pack.
  3499. ///
  3500. /// This routine may only be invoked when the expression is not
  3501. /// value-dependent.
  3502. unsigned getPackLength() const {
  3503. assert(!isValueDependent() &&
  3504. "Cannot get the length of a value-dependent pack size expression");
  3505. return Length;
  3506. }
  3507. /// Determine whether this represents a partially-substituted sizeof...
  3508. /// expression, such as is produced for:
  3509. ///
  3510. /// template<typename ...Ts> using X = int[sizeof...(Ts)];
  3511. /// template<typename ...Us> void f(X<Us..., 1, 2, 3, Us...>);
  3512. bool isPartiallySubstituted() const {
  3513. return isValueDependent() && Length;
  3514. }
  3515. /// Get
  3516. ArrayRef<TemplateArgument> getPartialArguments() const {
  3517. assert(isPartiallySubstituted());
  3518. const auto *Args = getTrailingObjects<TemplateArgument>();
  3519. return llvm::ArrayRef(Args, Args + Length);
  3520. }
  3521. SourceLocation getBeginLoc() const LLVM_READONLY { return OperatorLoc; }
  3522. SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
  3523. static bool classof(const Stmt *T) {
  3524. return T->getStmtClass() == SizeOfPackExprClass;
  3525. }
  3526. // Iterators
  3527. child_range children() {
  3528. return child_range(child_iterator(), child_iterator());
  3529. }
  3530. const_child_range children() const {
  3531. return const_child_range(const_child_iterator(), const_child_iterator());
  3532. }
  3533. };
  3534. /// Represents a reference to a non-type template parameter
  3535. /// that has been substituted with a template argument.
  3536. class SubstNonTypeTemplateParmExpr : public Expr {
  3537. friend class ASTReader;
  3538. friend class ASTStmtReader;
  3539. /// The replacement expression.
  3540. Stmt *Replacement;
  3541. /// The associated declaration and a flag indicating if it was a reference
  3542. /// parameter. For class NTTPs, we can't determine that based on the value
  3543. /// category alone.
  3544. llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndRef;
  3545. unsigned Index : 15;
  3546. unsigned PackIndex : 16;
  3547. explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty)
  3548. : Expr(SubstNonTypeTemplateParmExprClass, Empty) {}
  3549. public:
  3550. SubstNonTypeTemplateParmExpr(QualType Ty, ExprValueKind ValueKind,
  3551. SourceLocation Loc, Expr *Replacement,
  3552. Decl *AssociatedDecl, unsigned Index,
  3553. std::optional<unsigned> PackIndex, bool RefParam)
  3554. : Expr(SubstNonTypeTemplateParmExprClass, Ty, ValueKind, OK_Ordinary),
  3555. Replacement(Replacement),
  3556. AssociatedDeclAndRef(AssociatedDecl, RefParam), Index(Index),
  3557. PackIndex(PackIndex ? *PackIndex + 1 : 0) {
  3558. assert(AssociatedDecl != nullptr);
  3559. SubstNonTypeTemplateParmExprBits.NameLoc = Loc;
  3560. setDependence(computeDependence(this));
  3561. }
  3562. SourceLocation getNameLoc() const {
  3563. return SubstNonTypeTemplateParmExprBits.NameLoc;
  3564. }
  3565. SourceLocation getBeginLoc() const { return getNameLoc(); }
  3566. SourceLocation getEndLoc() const { return getNameLoc(); }
  3567. Expr *getReplacement() const { return cast<Expr>(Replacement); }
  3568. /// A template-like entity which owns the whole pattern being substituted.
  3569. /// This will own a set of template parameters.
  3570. Decl *getAssociatedDecl() const { return AssociatedDeclAndRef.getPointer(); }
  3571. /// Returns the index of the replaced parameter in the associated declaration.
  3572. /// This should match the result of `getParameter()->getIndex()`.
  3573. unsigned getIndex() const { return Index; }
  3574. std::optional<unsigned> getPackIndex() const {
  3575. if (PackIndex == 0)
  3576. return std::nullopt;
  3577. return PackIndex - 1;
  3578. }
  3579. NonTypeTemplateParmDecl *getParameter() const;
  3580. bool isReferenceParameter() const { return AssociatedDeclAndRef.getInt(); }
  3581. /// Determine the substituted type of the template parameter.
  3582. QualType getParameterType(const ASTContext &Ctx) const;
  3583. static bool classof(const Stmt *s) {
  3584. return s->getStmtClass() == SubstNonTypeTemplateParmExprClass;
  3585. }
  3586. // Iterators
  3587. child_range children() { return child_range(&Replacement, &Replacement + 1); }
  3588. const_child_range children() const {
  3589. return const_child_range(&Replacement, &Replacement + 1);
  3590. }
  3591. };
  3592. /// Represents a reference to a non-type template parameter pack that
  3593. /// has been substituted with a non-template argument pack.
  3594. ///
  3595. /// When a pack expansion in the source code contains multiple parameter packs
  3596. /// and those parameter packs correspond to different levels of template
  3597. /// parameter lists, this node is used to represent a non-type template
  3598. /// parameter pack from an outer level, which has already had its argument pack
  3599. /// substituted but that still lives within a pack expansion that itself
  3600. /// could not be instantiated. When actually performing a substitution into
  3601. /// that pack expansion (e.g., when all template parameters have corresponding
  3602. /// arguments), this type will be replaced with the appropriate underlying
  3603. /// expression at the current pack substitution index.
  3604. class SubstNonTypeTemplateParmPackExpr : public Expr {
  3605. friend class ASTReader;
  3606. friend class ASTStmtReader;
  3607. /// The non-type template parameter pack itself.
  3608. Decl *AssociatedDecl;
  3609. /// A pointer to the set of template arguments that this
  3610. /// parameter pack is instantiated with.
  3611. const TemplateArgument *Arguments;
  3612. /// The number of template arguments in \c Arguments.
  3613. unsigned NumArguments : 16;
  3614. unsigned Index : 16;
  3615. /// The location of the non-type template parameter pack reference.
  3616. SourceLocation NameLoc;
  3617. explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty)
  3618. : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) {}
  3619. public:
  3620. SubstNonTypeTemplateParmPackExpr(QualType T, ExprValueKind ValueKind,
  3621. SourceLocation NameLoc,
  3622. const TemplateArgument &ArgPack,
  3623. Decl *AssociatedDecl, unsigned Index);
  3624. /// A template-like entity which owns the whole pattern being substituted.
  3625. /// This will own a set of template parameters.
  3626. Decl *getAssociatedDecl() const { return AssociatedDecl; }
  3627. /// Returns the index of the replaced parameter in the associated declaration.
  3628. /// This should match the result of `getParameterPack()->getIndex()`.
  3629. unsigned getIndex() const { return Index; }
  3630. /// Retrieve the non-type template parameter pack being substituted.
  3631. NonTypeTemplateParmDecl *getParameterPack() const;
  3632. /// Retrieve the location of the parameter pack name.
  3633. SourceLocation getParameterPackLocation() const { return NameLoc; }
  3634. /// Retrieve the template argument pack containing the substituted
  3635. /// template arguments.
  3636. TemplateArgument getArgumentPack() const;
  3637. SourceLocation getBeginLoc() const LLVM_READONLY { return NameLoc; }
  3638. SourceLocation getEndLoc() const LLVM_READONLY { return NameLoc; }
  3639. static bool classof(const Stmt *T) {
  3640. return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass;
  3641. }
  3642. // Iterators
  3643. child_range children() {
  3644. return child_range(child_iterator(), child_iterator());
  3645. }
  3646. const_child_range children() const {
  3647. return const_child_range(const_child_iterator(), const_child_iterator());
  3648. }
  3649. };
  3650. /// Represents a reference to a function parameter pack or init-capture pack
  3651. /// that has been substituted but not yet expanded.
  3652. ///
  3653. /// When a pack expansion contains multiple parameter packs at different levels,
  3654. /// this node is used to represent a function parameter pack at an outer level
  3655. /// which we have already substituted to refer to expanded parameters, but where
  3656. /// the containing pack expansion cannot yet be expanded.
  3657. ///
  3658. /// \code
  3659. /// template<typename...Ts> struct S {
  3660. /// template<typename...Us> auto f(Ts ...ts) -> decltype(g(Us(ts)...));
  3661. /// };
  3662. /// template struct S<int, int>;
  3663. /// \endcode
  3664. class FunctionParmPackExpr final
  3665. : public Expr,
  3666. private llvm::TrailingObjects<FunctionParmPackExpr, VarDecl *> {
  3667. friend class ASTReader;
  3668. friend class ASTStmtReader;
  3669. friend TrailingObjects;
  3670. /// The function parameter pack which was referenced.
  3671. VarDecl *ParamPack;
  3672. /// The location of the function parameter pack reference.
  3673. SourceLocation NameLoc;
  3674. /// The number of expansions of this pack.
  3675. unsigned NumParameters;
  3676. FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
  3677. SourceLocation NameLoc, unsigned NumParams,
  3678. VarDecl *const *Params);
  3679. public:
  3680. static FunctionParmPackExpr *Create(const ASTContext &Context, QualType T,
  3681. VarDecl *ParamPack,
  3682. SourceLocation NameLoc,
  3683. ArrayRef<VarDecl *> Params);
  3684. static FunctionParmPackExpr *CreateEmpty(const ASTContext &Context,
  3685. unsigned NumParams);
  3686. /// Get the parameter pack which this expression refers to.
  3687. VarDecl *getParameterPack() const { return ParamPack; }
  3688. /// Get the location of the parameter pack.
  3689. SourceLocation getParameterPackLocation() const { return NameLoc; }
  3690. /// Iterators over the parameters which the parameter pack expanded
  3691. /// into.
  3692. using iterator = VarDecl * const *;
  3693. iterator begin() const { return getTrailingObjects<VarDecl *>(); }
  3694. iterator end() const { return begin() + NumParameters; }
  3695. /// Get the number of parameters in this parameter pack.
  3696. unsigned getNumExpansions() const { return NumParameters; }
  3697. /// Get an expansion of the parameter pack by index.
  3698. VarDecl *getExpansion(unsigned I) const { return begin()[I]; }
  3699. SourceLocation getBeginLoc() const LLVM_READONLY { return NameLoc; }
  3700. SourceLocation getEndLoc() const LLVM_READONLY { return NameLoc; }
  3701. static bool classof(const Stmt *T) {
  3702. return T->getStmtClass() == FunctionParmPackExprClass;
  3703. }
  3704. child_range children() {
  3705. return child_range(child_iterator(), child_iterator());
  3706. }
  3707. const_child_range children() const {
  3708. return const_child_range(const_child_iterator(), const_child_iterator());
  3709. }
  3710. };
  3711. /// Represents a prvalue temporary that is written into memory so that
  3712. /// a reference can bind to it.
  3713. ///
  3714. /// Prvalue expressions are materialized when they need to have an address
  3715. /// in memory for a reference to bind to. This happens when binding a
  3716. /// reference to the result of a conversion, e.g.,
  3717. ///
  3718. /// \code
  3719. /// const int &r = 1.0;
  3720. /// \endcode
  3721. ///
  3722. /// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is
  3723. /// then materialized via a \c MaterializeTemporaryExpr, and the reference
  3724. /// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues
  3725. /// (either an lvalue or an xvalue, depending on the kind of reference binding
  3726. /// to it), maintaining the invariant that references always bind to glvalues.
  3727. ///
  3728. /// Reference binding and copy-elision can both extend the lifetime of a
  3729. /// temporary. When either happens, the expression will also track the
  3730. /// declaration which is responsible for the lifetime extension.
  3731. class MaterializeTemporaryExpr : public Expr {
  3732. private:
  3733. friend class ASTStmtReader;
  3734. friend class ASTStmtWriter;
  3735. llvm::PointerUnion<Stmt *, LifetimeExtendedTemporaryDecl *> State;
  3736. public:
  3737. MaterializeTemporaryExpr(QualType T, Expr *Temporary,
  3738. bool BoundToLvalueReference,
  3739. LifetimeExtendedTemporaryDecl *MTD = nullptr);
  3740. MaterializeTemporaryExpr(EmptyShell Empty)
  3741. : Expr(MaterializeTemporaryExprClass, Empty) {}
  3742. /// Retrieve the temporary-generating subexpression whose value will
  3743. /// be materialized into a glvalue.
  3744. Expr *getSubExpr() const {
  3745. return cast<Expr>(
  3746. State.is<Stmt *>()
  3747. ? State.get<Stmt *>()
  3748. : State.get<LifetimeExtendedTemporaryDecl *>()->getTemporaryExpr());
  3749. }
  3750. /// Retrieve the storage duration for the materialized temporary.
  3751. StorageDuration getStorageDuration() const {
  3752. return State.is<Stmt *>() ? SD_FullExpression
  3753. : State.get<LifetimeExtendedTemporaryDecl *>()
  3754. ->getStorageDuration();
  3755. }
  3756. /// Get the storage for the constant value of a materialized temporary
  3757. /// of static storage duration.
  3758. APValue *getOrCreateValue(bool MayCreate) const {
  3759. assert(State.is<LifetimeExtendedTemporaryDecl *>() &&
  3760. "the temporary has not been lifetime extended");
  3761. return State.get<LifetimeExtendedTemporaryDecl *>()->getOrCreateValue(
  3762. MayCreate);
  3763. }
  3764. LifetimeExtendedTemporaryDecl *getLifetimeExtendedTemporaryDecl() {
  3765. return State.dyn_cast<LifetimeExtendedTemporaryDecl *>();
  3766. }
  3767. const LifetimeExtendedTemporaryDecl *
  3768. getLifetimeExtendedTemporaryDecl() const {
  3769. return State.dyn_cast<LifetimeExtendedTemporaryDecl *>();
  3770. }
  3771. /// Get the declaration which triggered the lifetime-extension of this
  3772. /// temporary, if any.
  3773. ValueDecl *getExtendingDecl() {
  3774. return State.is<Stmt *>() ? nullptr
  3775. : State.get<LifetimeExtendedTemporaryDecl *>()
  3776. ->getExtendingDecl();
  3777. }
  3778. const ValueDecl *getExtendingDecl() const {
  3779. return const_cast<MaterializeTemporaryExpr *>(this)->getExtendingDecl();
  3780. }
  3781. void setExtendingDecl(ValueDecl *ExtendedBy, unsigned ManglingNumber);
  3782. unsigned getManglingNumber() const {
  3783. return State.is<Stmt *>() ? 0
  3784. : State.get<LifetimeExtendedTemporaryDecl *>()
  3785. ->getManglingNumber();
  3786. }
  3787. /// Determine whether this materialized temporary is bound to an
  3788. /// lvalue reference; otherwise, it's bound to an rvalue reference.
  3789. bool isBoundToLvalueReference() const { return isLValue(); }
  3790. /// Determine whether this temporary object is usable in constant
  3791. /// expressions, as specified in C++20 [expr.const]p4.
  3792. bool isUsableInConstantExpressions(const ASTContext &Context) const;
  3793. SourceLocation getBeginLoc() const LLVM_READONLY {
  3794. return getSubExpr()->getBeginLoc();
  3795. }
  3796. SourceLocation getEndLoc() const LLVM_READONLY {
  3797. return getSubExpr()->getEndLoc();
  3798. }
  3799. static bool classof(const Stmt *T) {
  3800. return T->getStmtClass() == MaterializeTemporaryExprClass;
  3801. }
  3802. // Iterators
  3803. child_range children() {
  3804. return State.is<Stmt *>()
  3805. ? child_range(State.getAddrOfPtr1(), State.getAddrOfPtr1() + 1)
  3806. : State.get<LifetimeExtendedTemporaryDecl *>()->childrenExpr();
  3807. }
  3808. const_child_range children() const {
  3809. return State.is<Stmt *>()
  3810. ? const_child_range(State.getAddrOfPtr1(),
  3811. State.getAddrOfPtr1() + 1)
  3812. : const_cast<const LifetimeExtendedTemporaryDecl *>(
  3813. State.get<LifetimeExtendedTemporaryDecl *>())
  3814. ->childrenExpr();
  3815. }
  3816. };
  3817. /// Represents a folding of a pack over an operator.
  3818. ///
  3819. /// This expression is always dependent and represents a pack expansion of the
  3820. /// forms:
  3821. ///
  3822. /// ( expr op ... )
  3823. /// ( ... op expr )
  3824. /// ( expr op ... op expr )
  3825. class CXXFoldExpr : public Expr {
  3826. friend class ASTStmtReader;
  3827. friend class ASTStmtWriter;
  3828. enum SubExpr { Callee, LHS, RHS, Count };
  3829. SourceLocation LParenLoc;
  3830. SourceLocation EllipsisLoc;
  3831. SourceLocation RParenLoc;
  3832. // When 0, the number of expansions is not known. Otherwise, this is one more
  3833. // than the number of expansions.
  3834. unsigned NumExpansions;
  3835. Stmt *SubExprs[SubExpr::Count];
  3836. BinaryOperatorKind Opcode;
  3837. public:
  3838. CXXFoldExpr(QualType T, UnresolvedLookupExpr *Callee,
  3839. SourceLocation LParenLoc, Expr *LHS, BinaryOperatorKind Opcode,
  3840. SourceLocation EllipsisLoc, Expr *RHS, SourceLocation RParenLoc,
  3841. std::optional<unsigned> NumExpansions)
  3842. : Expr(CXXFoldExprClass, T, VK_PRValue, OK_Ordinary),
  3843. LParenLoc(LParenLoc), EllipsisLoc(EllipsisLoc), RParenLoc(RParenLoc),
  3844. NumExpansions(NumExpansions ? *NumExpansions + 1 : 0), Opcode(Opcode) {
  3845. SubExprs[SubExpr::Callee] = Callee;
  3846. SubExprs[SubExpr::LHS] = LHS;
  3847. SubExprs[SubExpr::RHS] = RHS;
  3848. setDependence(computeDependence(this));
  3849. }
  3850. CXXFoldExpr(EmptyShell Empty) : Expr(CXXFoldExprClass, Empty) {}
  3851. UnresolvedLookupExpr *getCallee() const {
  3852. return static_cast<UnresolvedLookupExpr *>(SubExprs[SubExpr::Callee]);
  3853. }
  3854. Expr *getLHS() const { return static_cast<Expr*>(SubExprs[SubExpr::LHS]); }
  3855. Expr *getRHS() const { return static_cast<Expr*>(SubExprs[SubExpr::RHS]); }
  3856. /// Does this produce a right-associated sequence of operators?
  3857. bool isRightFold() const {
  3858. return getLHS() && getLHS()->containsUnexpandedParameterPack();
  3859. }
  3860. /// Does this produce a left-associated sequence of operators?
  3861. bool isLeftFold() const { return !isRightFold(); }
  3862. /// Get the pattern, that is, the operand that contains an unexpanded pack.
  3863. Expr *getPattern() const { return isLeftFold() ? getRHS() : getLHS(); }
  3864. /// Get the operand that doesn't contain a pack, for a binary fold.
  3865. Expr *getInit() const { return isLeftFold() ? getLHS() : getRHS(); }
  3866. SourceLocation getLParenLoc() const { return LParenLoc; }
  3867. SourceLocation getRParenLoc() const { return RParenLoc; }
  3868. SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
  3869. BinaryOperatorKind getOperator() const { return Opcode; }
  3870. std::optional<unsigned> getNumExpansions() const {
  3871. if (NumExpansions)
  3872. return NumExpansions - 1;
  3873. return std::nullopt;
  3874. }
  3875. SourceLocation getBeginLoc() const LLVM_READONLY {
  3876. if (LParenLoc.isValid())
  3877. return LParenLoc;
  3878. if (isLeftFold())
  3879. return getEllipsisLoc();
  3880. return getLHS()->getBeginLoc();
  3881. }
  3882. SourceLocation getEndLoc() const LLVM_READONLY {
  3883. if (RParenLoc.isValid())
  3884. return RParenLoc;
  3885. if (isRightFold())
  3886. return getEllipsisLoc();
  3887. return getRHS()->getEndLoc();
  3888. }
  3889. static bool classof(const Stmt *T) {
  3890. return T->getStmtClass() == CXXFoldExprClass;
  3891. }
  3892. // Iterators
  3893. child_range children() {
  3894. return child_range(SubExprs, SubExprs + SubExpr::Count);
  3895. }
  3896. const_child_range children() const {
  3897. return const_child_range(SubExprs, SubExprs + SubExpr::Count);
  3898. }
  3899. };
  3900. /// Represents a list-initialization with parenthesis.
  3901. ///
  3902. /// As per P0960R3, this is a C++20 feature that allows aggregate to
  3903. /// be initialized with a parenthesized list of values:
  3904. /// ```
  3905. /// struct A {
  3906. /// int a;
  3907. /// double b;
  3908. /// };
  3909. ///
  3910. /// void foo() {
  3911. /// A a1(0); // Well-formed in C++20
  3912. /// A a2(1.5, 1.0); // Well-formed in C++20
  3913. /// }
  3914. /// ```
  3915. /// It has some sort of similiarity to braced
  3916. /// list-initialization, with some differences such as
  3917. /// it allows narrowing conversion whilst braced
  3918. /// list-initialization doesn't.
  3919. /// ```
  3920. /// struct A {
  3921. /// char a;
  3922. /// };
  3923. /// void foo() {
  3924. /// A a(1.5); // Well-formed in C++20
  3925. /// A b{1.5}; // Ill-formed !
  3926. /// }
  3927. /// ```
  3928. class CXXParenListInitExpr final
  3929. : public Expr,
  3930. private llvm::TrailingObjects<CXXParenListInitExpr, Expr *> {
  3931. friend class TrailingObjects;
  3932. friend class ASTStmtReader;
  3933. friend class ASTStmtWriter;
  3934. unsigned NumExprs;
  3935. unsigned NumUserSpecifiedExprs;
  3936. SourceLocation InitLoc, LParenLoc, RParenLoc;
  3937. llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit;
  3938. CXXParenListInitExpr(ArrayRef<Expr *> Args, QualType T,
  3939. unsigned NumUserSpecifiedExprs, SourceLocation InitLoc,
  3940. SourceLocation LParenLoc, SourceLocation RParenLoc)
  3941. : Expr(CXXParenListInitExprClass, T, getValueKindForType(T), OK_Ordinary),
  3942. NumExprs(Args.size()), NumUserSpecifiedExprs(NumUserSpecifiedExprs),
  3943. InitLoc(InitLoc), LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
  3944. std::copy(Args.begin(), Args.end(), getTrailingObjects<Expr *>());
  3945. assert(NumExprs >= NumUserSpecifiedExprs &&
  3946. "number of user specified inits is greater than the number of "
  3947. "passed inits");
  3948. setDependence(computeDependence(this));
  3949. }
  3950. size_t numTrailingObjects(OverloadToken<Expr *>) const { return NumExprs; }
  3951. public:
  3952. static CXXParenListInitExpr *
  3953. Create(ASTContext &C, ArrayRef<Expr *> Args, QualType T,
  3954. unsigned NumUserSpecifiedExprs, SourceLocation InitLoc,
  3955. SourceLocation LParenLoc, SourceLocation RParenLoc);
  3956. static CXXParenListInitExpr *CreateEmpty(ASTContext &C, unsigned numExprs,
  3957. EmptyShell Empty);
  3958. explicit CXXParenListInitExpr(EmptyShell Empty, unsigned NumExprs)
  3959. : Expr(CXXParenListInitExprClass, Empty), NumExprs(NumExprs),
  3960. NumUserSpecifiedExprs(0) {}
  3961. void updateDependence() { setDependence(computeDependence(this)); }
  3962. ArrayRef<Expr *> getInitExprs() {
  3963. return ArrayRef(getTrailingObjects<Expr *>(), NumExprs);
  3964. }
  3965. const ArrayRef<Expr *> getInitExprs() const {
  3966. return ArrayRef(getTrailingObjects<Expr *>(), NumExprs);
  3967. }
  3968. ArrayRef<Expr *> getUserSpecifiedInitExprs() {
  3969. return ArrayRef(getTrailingObjects<Expr *>(), NumUserSpecifiedExprs);
  3970. }
  3971. const ArrayRef<Expr *> getUserSpecifiedInitExprs() const {
  3972. return ArrayRef(getTrailingObjects<Expr *>(), NumUserSpecifiedExprs);
  3973. }
  3974. SourceLocation getBeginLoc() const LLVM_READONLY { return LParenLoc; }
  3975. SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
  3976. SourceLocation getInitLoc() const LLVM_READONLY { return InitLoc; }
  3977. SourceRange getSourceRange() const LLVM_READONLY {
  3978. return SourceRange(getBeginLoc(), getEndLoc());
  3979. }
  3980. void setArrayFiller(Expr *E) { ArrayFillerOrUnionFieldInit = E; }
  3981. Expr *getArrayFiller() {
  3982. return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
  3983. }
  3984. const Expr *getArrayFiller() const {
  3985. return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
  3986. }
  3987. void setInitializedFieldInUnion(FieldDecl *FD) {
  3988. ArrayFillerOrUnionFieldInit = FD;
  3989. }
  3990. FieldDecl *getInitializedFieldInUnion() {
  3991. return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
  3992. }
  3993. const FieldDecl *getInitializedFieldInUnion() const {
  3994. return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
  3995. }
  3996. child_range children() {
  3997. Stmt **Begin = reinterpret_cast<Stmt **>(getTrailingObjects<Expr *>());
  3998. return child_range(Begin, Begin + NumExprs);
  3999. }
  4000. const_child_range children() const {
  4001. Stmt *const *Begin =
  4002. reinterpret_cast<Stmt *const *>(getTrailingObjects<Expr *>());
  4003. return const_child_range(Begin, Begin + NumExprs);
  4004. }
  4005. static bool classof(const Stmt *T) {
  4006. return T->getStmtClass() == CXXParenListInitExprClass;
  4007. }
  4008. };
  4009. /// Represents an expression that might suspend coroutine execution;
  4010. /// either a co_await or co_yield expression.
  4011. ///
  4012. /// Evaluation of this expression first evaluates its 'ready' expression. If
  4013. /// that returns 'false':
  4014. /// -- execution of the coroutine is suspended
  4015. /// -- the 'suspend' expression is evaluated
  4016. /// -- if the 'suspend' expression returns 'false', the coroutine is
  4017. /// resumed
  4018. /// -- otherwise, control passes back to the resumer.
  4019. /// If the coroutine is not suspended, or when it is resumed, the 'resume'
  4020. /// expression is evaluated, and its result is the result of the overall
  4021. /// expression.
  4022. class CoroutineSuspendExpr : public Expr {
  4023. friend class ASTStmtReader;
  4024. SourceLocation KeywordLoc;
  4025. enum SubExpr { Operand, Common, Ready, Suspend, Resume, Count };
  4026. Stmt *SubExprs[SubExpr::Count];
  4027. OpaqueValueExpr *OpaqueValue = nullptr;
  4028. public:
  4029. CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, Expr *Operand,
  4030. Expr *Common, Expr *Ready, Expr *Suspend, Expr *Resume,
  4031. OpaqueValueExpr *OpaqueValue)
  4032. : Expr(SC, Resume->getType(), Resume->getValueKind(),
  4033. Resume->getObjectKind()),
  4034. KeywordLoc(KeywordLoc), OpaqueValue(OpaqueValue) {
  4035. SubExprs[SubExpr::Operand] = Operand;
  4036. SubExprs[SubExpr::Common] = Common;
  4037. SubExprs[SubExpr::Ready] = Ready;
  4038. SubExprs[SubExpr::Suspend] = Suspend;
  4039. SubExprs[SubExpr::Resume] = Resume;
  4040. setDependence(computeDependence(this));
  4041. }
  4042. CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, QualType Ty,
  4043. Expr *Operand, Expr *Common)
  4044. : Expr(SC, Ty, VK_PRValue, OK_Ordinary), KeywordLoc(KeywordLoc) {
  4045. assert(Common->isTypeDependent() && Ty->isDependentType() &&
  4046. "wrong constructor for non-dependent co_await/co_yield expression");
  4047. SubExprs[SubExpr::Operand] = Operand;
  4048. SubExprs[SubExpr::Common] = Common;
  4049. SubExprs[SubExpr::Ready] = nullptr;
  4050. SubExprs[SubExpr::Suspend] = nullptr;
  4051. SubExprs[SubExpr::Resume] = nullptr;
  4052. setDependence(computeDependence(this));
  4053. }
  4054. CoroutineSuspendExpr(StmtClass SC, EmptyShell Empty) : Expr(SC, Empty) {
  4055. SubExprs[SubExpr::Operand] = nullptr;
  4056. SubExprs[SubExpr::Common] = nullptr;
  4057. SubExprs[SubExpr::Ready] = nullptr;
  4058. SubExprs[SubExpr::Suspend] = nullptr;
  4059. SubExprs[SubExpr::Resume] = nullptr;
  4060. }
  4061. Expr *getCommonExpr() const {
  4062. return static_cast<Expr*>(SubExprs[SubExpr::Common]);
  4063. }
  4064. /// getOpaqueValue - Return the opaque value placeholder.
  4065. OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; }
  4066. Expr *getReadyExpr() const {
  4067. return static_cast<Expr*>(SubExprs[SubExpr::Ready]);
  4068. }
  4069. Expr *getSuspendExpr() const {
  4070. return static_cast<Expr*>(SubExprs[SubExpr::Suspend]);
  4071. }
  4072. Expr *getResumeExpr() const {
  4073. return static_cast<Expr*>(SubExprs[SubExpr::Resume]);
  4074. }
  4075. // The syntactic operand written in the code
  4076. Expr *getOperand() const {
  4077. return static_cast<Expr *>(SubExprs[SubExpr::Operand]);
  4078. }
  4079. SourceLocation getKeywordLoc() const { return KeywordLoc; }
  4080. SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
  4081. SourceLocation getEndLoc() const LLVM_READONLY {
  4082. return getOperand()->getEndLoc();
  4083. }
  4084. child_range children() {
  4085. return child_range(SubExprs, SubExprs + SubExpr::Count);
  4086. }
  4087. const_child_range children() const {
  4088. return const_child_range(SubExprs, SubExprs + SubExpr::Count);
  4089. }
  4090. static bool classof(const Stmt *T) {
  4091. return T->getStmtClass() == CoawaitExprClass ||
  4092. T->getStmtClass() == CoyieldExprClass;
  4093. }
  4094. };
  4095. /// Represents a 'co_await' expression.
  4096. class CoawaitExpr : public CoroutineSuspendExpr {
  4097. friend class ASTStmtReader;
  4098. public:
  4099. CoawaitExpr(SourceLocation CoawaitLoc, Expr *Operand, Expr *Common,
  4100. Expr *Ready, Expr *Suspend, Expr *Resume,
  4101. OpaqueValueExpr *OpaqueValue, bool IsImplicit = false)
  4102. : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Operand, Common,
  4103. Ready, Suspend, Resume, OpaqueValue) {
  4104. CoawaitBits.IsImplicit = IsImplicit;
  4105. }
  4106. CoawaitExpr(SourceLocation CoawaitLoc, QualType Ty, Expr *Operand,
  4107. Expr *Common, bool IsImplicit = false)
  4108. : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Ty, Operand,
  4109. Common) {
  4110. CoawaitBits.IsImplicit = IsImplicit;
  4111. }
  4112. CoawaitExpr(EmptyShell Empty)
  4113. : CoroutineSuspendExpr(CoawaitExprClass, Empty) {}
  4114. bool isImplicit() const { return CoawaitBits.IsImplicit; }
  4115. void setIsImplicit(bool value = true) { CoawaitBits.IsImplicit = value; }
  4116. static bool classof(const Stmt *T) {
  4117. return T->getStmtClass() == CoawaitExprClass;
  4118. }
  4119. };
  4120. /// Represents a 'co_await' expression while the type of the promise
  4121. /// is dependent.
  4122. class DependentCoawaitExpr : public Expr {
  4123. friend class ASTStmtReader;
  4124. SourceLocation KeywordLoc;
  4125. Stmt *SubExprs[2];
  4126. public:
  4127. DependentCoawaitExpr(SourceLocation KeywordLoc, QualType Ty, Expr *Op,
  4128. UnresolvedLookupExpr *OpCoawait)
  4129. : Expr(DependentCoawaitExprClass, Ty, VK_PRValue, OK_Ordinary),
  4130. KeywordLoc(KeywordLoc) {
  4131. // NOTE: A co_await expression is dependent on the coroutines promise
  4132. // type and may be dependent even when the `Op` expression is not.
  4133. assert(Ty->isDependentType() &&
  4134. "wrong constructor for non-dependent co_await/co_yield expression");
  4135. SubExprs[0] = Op;
  4136. SubExprs[1] = OpCoawait;
  4137. setDependence(computeDependence(this));
  4138. }
  4139. DependentCoawaitExpr(EmptyShell Empty)
  4140. : Expr(DependentCoawaitExprClass, Empty) {}
  4141. Expr *getOperand() const { return cast<Expr>(SubExprs[0]); }
  4142. UnresolvedLookupExpr *getOperatorCoawaitLookup() const {
  4143. return cast<UnresolvedLookupExpr>(SubExprs[1]);
  4144. }
  4145. SourceLocation getKeywordLoc() const { return KeywordLoc; }
  4146. SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
  4147. SourceLocation getEndLoc() const LLVM_READONLY {
  4148. return getOperand()->getEndLoc();
  4149. }
  4150. child_range children() { return child_range(SubExprs, SubExprs + 2); }
  4151. const_child_range children() const {
  4152. return const_child_range(SubExprs, SubExprs + 2);
  4153. }
  4154. static bool classof(const Stmt *T) {
  4155. return T->getStmtClass() == DependentCoawaitExprClass;
  4156. }
  4157. };
  4158. /// Represents a 'co_yield' expression.
  4159. class CoyieldExpr : public CoroutineSuspendExpr {
  4160. friend class ASTStmtReader;
  4161. public:
  4162. CoyieldExpr(SourceLocation CoyieldLoc, Expr *Operand, Expr *Common,
  4163. Expr *Ready, Expr *Suspend, Expr *Resume,
  4164. OpaqueValueExpr *OpaqueValue)
  4165. : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Operand, Common,
  4166. Ready, Suspend, Resume, OpaqueValue) {}
  4167. CoyieldExpr(SourceLocation CoyieldLoc, QualType Ty, Expr *Operand,
  4168. Expr *Common)
  4169. : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Ty, Operand,
  4170. Common) {}
  4171. CoyieldExpr(EmptyShell Empty)
  4172. : CoroutineSuspendExpr(CoyieldExprClass, Empty) {}
  4173. static bool classof(const Stmt *T) {
  4174. return T->getStmtClass() == CoyieldExprClass;
  4175. }
  4176. };
  4177. /// Represents a C++2a __builtin_bit_cast(T, v) expression. Used to implement
  4178. /// std::bit_cast. These can sometimes be evaluated as part of a constant
  4179. /// expression, but otherwise CodeGen to a simple memcpy in general.
  4180. class BuiltinBitCastExpr final
  4181. : public ExplicitCastExpr,
  4182. private llvm::TrailingObjects<BuiltinBitCastExpr, CXXBaseSpecifier *> {
  4183. friend class ASTStmtReader;
  4184. friend class CastExpr;
  4185. friend TrailingObjects;
  4186. SourceLocation KWLoc;
  4187. SourceLocation RParenLoc;
  4188. public:
  4189. BuiltinBitCastExpr(QualType T, ExprValueKind VK, CastKind CK, Expr *SrcExpr,
  4190. TypeSourceInfo *DstType, SourceLocation KWLoc,
  4191. SourceLocation RParenLoc)
  4192. : ExplicitCastExpr(BuiltinBitCastExprClass, T, VK, CK, SrcExpr, 0, false,
  4193. DstType),
  4194. KWLoc(KWLoc), RParenLoc(RParenLoc) {}
  4195. BuiltinBitCastExpr(EmptyShell Empty)
  4196. : ExplicitCastExpr(BuiltinBitCastExprClass, Empty, 0, false) {}
  4197. SourceLocation getBeginLoc() const LLVM_READONLY { return KWLoc; }
  4198. SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
  4199. static bool classof(const Stmt *T) {
  4200. return T->getStmtClass() == BuiltinBitCastExprClass;
  4201. }
  4202. };
  4203. } // namespace clang
  4204. #endif // LLVM_CLANG_AST_EXPRCXX_H
  4205. #ifdef __GNUC__
  4206. #pragma GCC diagnostic pop
  4207. #endif