Stmt.h 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Stmt.h - Classes for representing statements -------------*- 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. // This file defines the Stmt interface and subclasses.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_STMT_H
  18. #define LLVM_CLANG_AST_STMT_H
  19. #include "clang/AST/DeclGroup.h"
  20. #include "clang/AST/DependenceFlags.h"
  21. #include "clang/AST/StmtIterator.h"
  22. #include "clang/Basic/CapturedStmt.h"
  23. #include "clang/Basic/IdentifierTable.h"
  24. #include "clang/Basic/LLVM.h"
  25. #include "clang/Basic/SourceLocation.h"
  26. #include "clang/Basic/Specifiers.h"
  27. #include "llvm/ADT/ArrayRef.h"
  28. #include "llvm/ADT/BitmaskEnum.h"
  29. #include "llvm/ADT/PointerIntPair.h"
  30. #include "llvm/ADT/StringRef.h"
  31. #include "llvm/ADT/iterator.h"
  32. #include "llvm/ADT/iterator_range.h"
  33. #include "llvm/Support/Casting.h"
  34. #include "llvm/Support/Compiler.h"
  35. #include "llvm/Support/ErrorHandling.h"
  36. #include <algorithm>
  37. #include <cassert>
  38. #include <cstddef>
  39. #include <iterator>
  40. #include <string>
  41. namespace llvm {
  42. class FoldingSetNodeID;
  43. } // namespace llvm
  44. namespace clang {
  45. class ASTContext;
  46. class Attr;
  47. class CapturedDecl;
  48. class Decl;
  49. class Expr;
  50. class AddrLabelExpr;
  51. class LabelDecl;
  52. class ODRHash;
  53. class PrinterHelper;
  54. struct PrintingPolicy;
  55. class RecordDecl;
  56. class SourceManager;
  57. class StringLiteral;
  58. class Token;
  59. class VarDecl;
  60. //===----------------------------------------------------------------------===//
  61. // AST classes for statements.
  62. //===----------------------------------------------------------------------===//
  63. /// Stmt - This represents one statement.
  64. ///
  65. class alignas(void *) Stmt {
  66. public:
  67. enum StmtClass {
  68. NoStmtClass = 0,
  69. #define STMT(CLASS, PARENT) CLASS##Class,
  70. #define STMT_RANGE(BASE, FIRST, LAST) \
  71. first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
  72. #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
  73. first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
  74. #define ABSTRACT_STMT(STMT)
  75. #include "clang/AST/StmtNodes.inc"
  76. };
  77. // Make vanilla 'new' and 'delete' illegal for Stmts.
  78. protected:
  79. friend class ASTStmtReader;
  80. friend class ASTStmtWriter;
  81. void *operator new(size_t bytes) noexcept {
  82. llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
  83. }
  84. void operator delete(void *data) noexcept {
  85. llvm_unreachable("Stmts cannot be released with regular 'delete'.");
  86. }
  87. //===--- Statement bitfields classes ---===//
  88. class StmtBitfields {
  89. friend class ASTStmtReader;
  90. friend class ASTStmtWriter;
  91. friend class Stmt;
  92. /// The statement class.
  93. unsigned sClass : 8;
  94. };
  95. enum { NumStmtBits = 8 };
  96. class NullStmtBitfields {
  97. friend class ASTStmtReader;
  98. friend class ASTStmtWriter;
  99. friend class NullStmt;
  100. unsigned : NumStmtBits;
  101. /// True if the null statement was preceded by an empty macro, e.g:
  102. /// @code
  103. /// #define CALL(x)
  104. /// CALL(0);
  105. /// @endcode
  106. unsigned HasLeadingEmptyMacro : 1;
  107. /// The location of the semi-colon.
  108. SourceLocation SemiLoc;
  109. };
  110. class CompoundStmtBitfields {
  111. friend class ASTStmtReader;
  112. friend class CompoundStmt;
  113. unsigned : NumStmtBits;
  114. unsigned NumStmts : 32 - NumStmtBits;
  115. /// The location of the opening "{".
  116. SourceLocation LBraceLoc;
  117. };
  118. class LabelStmtBitfields {
  119. friend class LabelStmt;
  120. unsigned : NumStmtBits;
  121. SourceLocation IdentLoc;
  122. };
  123. class AttributedStmtBitfields {
  124. friend class ASTStmtReader;
  125. friend class AttributedStmt;
  126. unsigned : NumStmtBits;
  127. /// Number of attributes.
  128. unsigned NumAttrs : 32 - NumStmtBits;
  129. /// The location of the attribute.
  130. SourceLocation AttrLoc;
  131. };
  132. class IfStmtBitfields {
  133. friend class ASTStmtReader;
  134. friend class IfStmt;
  135. unsigned : NumStmtBits;
  136. /// Whether this is a constexpr if, or a consteval if, or neither.
  137. unsigned Kind : 3;
  138. /// True if this if statement has storage for an else statement.
  139. unsigned HasElse : 1;
  140. /// True if this if statement has storage for a variable declaration.
  141. unsigned HasVar : 1;
  142. /// True if this if statement has storage for an init statement.
  143. unsigned HasInit : 1;
  144. /// The location of the "if".
  145. SourceLocation IfLoc;
  146. };
  147. class SwitchStmtBitfields {
  148. friend class SwitchStmt;
  149. unsigned : NumStmtBits;
  150. /// True if the SwitchStmt has storage for an init statement.
  151. unsigned HasInit : 1;
  152. /// True if the SwitchStmt has storage for a condition variable.
  153. unsigned HasVar : 1;
  154. /// If the SwitchStmt is a switch on an enum value, records whether all
  155. /// the enum values were covered by CaseStmts. The coverage information
  156. /// value is meant to be a hint for possible clients.
  157. unsigned AllEnumCasesCovered : 1;
  158. /// The location of the "switch".
  159. SourceLocation SwitchLoc;
  160. };
  161. class WhileStmtBitfields {
  162. friend class ASTStmtReader;
  163. friend class WhileStmt;
  164. unsigned : NumStmtBits;
  165. /// True if the WhileStmt has storage for a condition variable.
  166. unsigned HasVar : 1;
  167. /// The location of the "while".
  168. SourceLocation WhileLoc;
  169. };
  170. class DoStmtBitfields {
  171. friend class DoStmt;
  172. unsigned : NumStmtBits;
  173. /// The location of the "do".
  174. SourceLocation DoLoc;
  175. };
  176. class ForStmtBitfields {
  177. friend class ForStmt;
  178. unsigned : NumStmtBits;
  179. /// The location of the "for".
  180. SourceLocation ForLoc;
  181. };
  182. class GotoStmtBitfields {
  183. friend class GotoStmt;
  184. friend class IndirectGotoStmt;
  185. unsigned : NumStmtBits;
  186. /// The location of the "goto".
  187. SourceLocation GotoLoc;
  188. };
  189. class ContinueStmtBitfields {
  190. friend class ContinueStmt;
  191. unsigned : NumStmtBits;
  192. /// The location of the "continue".
  193. SourceLocation ContinueLoc;
  194. };
  195. class BreakStmtBitfields {
  196. friend class BreakStmt;
  197. unsigned : NumStmtBits;
  198. /// The location of the "break".
  199. SourceLocation BreakLoc;
  200. };
  201. class ReturnStmtBitfields {
  202. friend class ReturnStmt;
  203. unsigned : NumStmtBits;
  204. /// True if this ReturnStmt has storage for an NRVO candidate.
  205. unsigned HasNRVOCandidate : 1;
  206. /// The location of the "return".
  207. SourceLocation RetLoc;
  208. };
  209. class SwitchCaseBitfields {
  210. friend class SwitchCase;
  211. friend class CaseStmt;
  212. unsigned : NumStmtBits;
  213. /// Used by CaseStmt to store whether it is a case statement
  214. /// of the form case LHS ... RHS (a GNU extension).
  215. unsigned CaseStmtIsGNURange : 1;
  216. /// The location of the "case" or "default" keyword.
  217. SourceLocation KeywordLoc;
  218. };
  219. //===--- Expression bitfields classes ---===//
  220. class ExprBitfields {
  221. friend class ASTStmtReader; // deserialization
  222. friend class AtomicExpr; // ctor
  223. friend class BlockDeclRefExpr; // ctor
  224. friend class CallExpr; // ctor
  225. friend class CXXConstructExpr; // ctor
  226. friend class CXXDependentScopeMemberExpr; // ctor
  227. friend class CXXNewExpr; // ctor
  228. friend class CXXUnresolvedConstructExpr; // ctor
  229. friend class DeclRefExpr; // computeDependence
  230. friend class DependentScopeDeclRefExpr; // ctor
  231. friend class DesignatedInitExpr; // ctor
  232. friend class Expr;
  233. friend class InitListExpr; // ctor
  234. friend class ObjCArrayLiteral; // ctor
  235. friend class ObjCDictionaryLiteral; // ctor
  236. friend class ObjCMessageExpr; // ctor
  237. friend class OffsetOfExpr; // ctor
  238. friend class OpaqueValueExpr; // ctor
  239. friend class OverloadExpr; // ctor
  240. friend class ParenListExpr; // ctor
  241. friend class PseudoObjectExpr; // ctor
  242. friend class ShuffleVectorExpr; // ctor
  243. unsigned : NumStmtBits;
  244. unsigned ValueKind : 2;
  245. unsigned ObjectKind : 3;
  246. unsigned /*ExprDependence*/ Dependent : llvm::BitWidth<ExprDependence>;
  247. };
  248. enum { NumExprBits = NumStmtBits + 5 + llvm::BitWidth<ExprDependence> };
  249. class ConstantExprBitfields {
  250. friend class ASTStmtReader;
  251. friend class ASTStmtWriter;
  252. friend class ConstantExpr;
  253. unsigned : NumExprBits;
  254. /// The kind of result that is tail-allocated.
  255. unsigned ResultKind : 2;
  256. /// The kind of Result as defined by APValue::Kind.
  257. unsigned APValueKind : 4;
  258. /// When ResultKind == RSK_Int64, true if the tail-allocated integer is
  259. /// unsigned.
  260. unsigned IsUnsigned : 1;
  261. /// When ResultKind == RSK_Int64. the BitWidth of the tail-allocated
  262. /// integer. 7 bits because it is the minimal number of bits to represent a
  263. /// value from 0 to 64 (the size of the tail-allocated integer).
  264. unsigned BitWidth : 7;
  265. /// When ResultKind == RSK_APValue, true if the ASTContext will cleanup the
  266. /// tail-allocated APValue.
  267. unsigned HasCleanup : 1;
  268. /// True if this ConstantExpr was created for immediate invocation.
  269. unsigned IsImmediateInvocation : 1;
  270. };
  271. class PredefinedExprBitfields {
  272. friend class ASTStmtReader;
  273. friend class PredefinedExpr;
  274. unsigned : NumExprBits;
  275. /// The kind of this PredefinedExpr. One of the enumeration values
  276. /// in PredefinedExpr::IdentKind.
  277. unsigned Kind : 4;
  278. /// True if this PredefinedExpr has a trailing "StringLiteral *"
  279. /// for the predefined identifier.
  280. unsigned HasFunctionName : 1;
  281. /// The location of this PredefinedExpr.
  282. SourceLocation Loc;
  283. };
  284. class DeclRefExprBitfields {
  285. friend class ASTStmtReader; // deserialization
  286. friend class DeclRefExpr;
  287. unsigned : NumExprBits;
  288. unsigned HasQualifier : 1;
  289. unsigned HasTemplateKWAndArgsInfo : 1;
  290. unsigned HasFoundDecl : 1;
  291. unsigned HadMultipleCandidates : 1;
  292. unsigned RefersToEnclosingVariableOrCapture : 1;
  293. unsigned NonOdrUseReason : 2;
  294. /// The location of the declaration name itself.
  295. SourceLocation Loc;
  296. };
  297. class FloatingLiteralBitfields {
  298. friend class FloatingLiteral;
  299. unsigned : NumExprBits;
  300. unsigned Semantics : 3; // Provides semantics for APFloat construction
  301. unsigned IsExact : 1;
  302. };
  303. class StringLiteralBitfields {
  304. friend class ASTStmtReader;
  305. friend class StringLiteral;
  306. unsigned : NumExprBits;
  307. /// The kind of this string literal.
  308. /// One of the enumeration values of StringLiteral::StringKind.
  309. unsigned Kind : 3;
  310. /// The width of a single character in bytes. Only values of 1, 2,
  311. /// and 4 bytes are supported. StringLiteral::mapCharByteWidth maps
  312. /// the target + string kind to the appropriate CharByteWidth.
  313. unsigned CharByteWidth : 3;
  314. unsigned IsPascal : 1;
  315. /// The number of concatenated token this string is made of.
  316. /// This is the number of trailing SourceLocation.
  317. unsigned NumConcatenated;
  318. };
  319. class CharacterLiteralBitfields {
  320. friend class CharacterLiteral;
  321. unsigned : NumExprBits;
  322. unsigned Kind : 3;
  323. };
  324. class UnaryOperatorBitfields {
  325. friend class UnaryOperator;
  326. unsigned : NumExprBits;
  327. unsigned Opc : 5;
  328. unsigned CanOverflow : 1;
  329. //
  330. /// This is only meaningful for operations on floating point
  331. /// types when additional values need to be in trailing storage.
  332. /// It is 0 otherwise.
  333. unsigned HasFPFeatures : 1;
  334. SourceLocation Loc;
  335. };
  336. class UnaryExprOrTypeTraitExprBitfields {
  337. friend class UnaryExprOrTypeTraitExpr;
  338. unsigned : NumExprBits;
  339. unsigned Kind : 3;
  340. unsigned IsType : 1; // true if operand is a type, false if an expression.
  341. };
  342. class ArrayOrMatrixSubscriptExprBitfields {
  343. friend class ArraySubscriptExpr;
  344. friend class MatrixSubscriptExpr;
  345. unsigned : NumExprBits;
  346. SourceLocation RBracketLoc;
  347. };
  348. class CallExprBitfields {
  349. friend class CallExpr;
  350. unsigned : NumExprBits;
  351. unsigned NumPreArgs : 1;
  352. /// True if the callee of the call expression was found using ADL.
  353. unsigned UsesADL : 1;
  354. /// True if the call expression has some floating-point features.
  355. unsigned HasFPFeatures : 1;
  356. /// Padding used to align OffsetToTrailingObjects to a byte multiple.
  357. unsigned : 24 - 3 - NumExprBits;
  358. /// The offset in bytes from the this pointer to the start of the
  359. /// trailing objects belonging to CallExpr. Intentionally byte sized
  360. /// for faster access.
  361. unsigned OffsetToTrailingObjects : 8;
  362. };
  363. enum { NumCallExprBits = 32 };
  364. class MemberExprBitfields {
  365. friend class ASTStmtReader;
  366. friend class MemberExpr;
  367. unsigned : NumExprBits;
  368. /// IsArrow - True if this is "X->F", false if this is "X.F".
  369. unsigned IsArrow : 1;
  370. /// True if this member expression used a nested-name-specifier to
  371. /// refer to the member, e.g., "x->Base::f", or found its member via
  372. /// a using declaration. When true, a MemberExprNameQualifier
  373. /// structure is allocated immediately after the MemberExpr.
  374. unsigned HasQualifierOrFoundDecl : 1;
  375. /// True if this member expression specified a template keyword
  376. /// and/or a template argument list explicitly, e.g., x->f<int>,
  377. /// x->template f, x->template f<int>.
  378. /// When true, an ASTTemplateKWAndArgsInfo structure and its
  379. /// TemplateArguments (if any) are present.
  380. unsigned HasTemplateKWAndArgsInfo : 1;
  381. /// True if this member expression refers to a method that
  382. /// was resolved from an overloaded set having size greater than 1.
  383. unsigned HadMultipleCandidates : 1;
  384. /// Value of type NonOdrUseReason indicating why this MemberExpr does
  385. /// not constitute an odr-use of the named declaration. Meaningful only
  386. /// when naming a static member.
  387. unsigned NonOdrUseReason : 2;
  388. /// This is the location of the -> or . in the expression.
  389. SourceLocation OperatorLoc;
  390. };
  391. class CastExprBitfields {
  392. friend class CastExpr;
  393. friend class ImplicitCastExpr;
  394. unsigned : NumExprBits;
  395. unsigned Kind : 7;
  396. unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr.
  397. /// True if the call expression has some floating-point features.
  398. unsigned HasFPFeatures : 1;
  399. /// The number of CXXBaseSpecifiers in the cast. 14 bits would be enough
  400. /// here. ([implimits] Direct and indirect base classes [16384]).
  401. unsigned BasePathSize;
  402. };
  403. class BinaryOperatorBitfields {
  404. friend class BinaryOperator;
  405. unsigned : NumExprBits;
  406. unsigned Opc : 6;
  407. /// This is only meaningful for operations on floating point
  408. /// types when additional values need to be in trailing storage.
  409. /// It is 0 otherwise.
  410. unsigned HasFPFeatures : 1;
  411. SourceLocation OpLoc;
  412. };
  413. class InitListExprBitfields {
  414. friend class InitListExpr;
  415. unsigned : NumExprBits;
  416. /// Whether this initializer list originally had a GNU array-range
  417. /// designator in it. This is a temporary marker used by CodeGen.
  418. unsigned HadArrayRangeDesignator : 1;
  419. };
  420. class ParenListExprBitfields {
  421. friend class ASTStmtReader;
  422. friend class ParenListExpr;
  423. unsigned : NumExprBits;
  424. /// The number of expressions in the paren list.
  425. unsigned NumExprs;
  426. };
  427. class GenericSelectionExprBitfields {
  428. friend class ASTStmtReader;
  429. friend class GenericSelectionExpr;
  430. unsigned : NumExprBits;
  431. /// The location of the "_Generic".
  432. SourceLocation GenericLoc;
  433. };
  434. class PseudoObjectExprBitfields {
  435. friend class ASTStmtReader; // deserialization
  436. friend class PseudoObjectExpr;
  437. unsigned : NumExprBits;
  438. // These don't need to be particularly wide, because they're
  439. // strictly limited by the forms of expressions we permit.
  440. unsigned NumSubExprs : 8;
  441. unsigned ResultIndex : 32 - 8 - NumExprBits;
  442. };
  443. class SourceLocExprBitfields {
  444. friend class ASTStmtReader;
  445. friend class SourceLocExpr;
  446. unsigned : NumExprBits;
  447. /// The kind of source location builtin represented by the SourceLocExpr.
  448. /// Ex. __builtin_LINE, __builtin_FUNCTION, ect.
  449. unsigned Kind : 2;
  450. };
  451. class StmtExprBitfields {
  452. friend class ASTStmtReader;
  453. friend class StmtExpr;
  454. unsigned : NumExprBits;
  455. /// The number of levels of template parameters enclosing this statement
  456. /// expression. Used to determine if a statement expression remains
  457. /// dependent after instantiation.
  458. unsigned TemplateDepth;
  459. };
  460. //===--- C++ Expression bitfields classes ---===//
  461. class CXXOperatorCallExprBitfields {
  462. friend class ASTStmtReader;
  463. friend class CXXOperatorCallExpr;
  464. unsigned : NumCallExprBits;
  465. /// The kind of this overloaded operator. One of the enumerator
  466. /// value of OverloadedOperatorKind.
  467. unsigned OperatorKind : 6;
  468. };
  469. class CXXRewrittenBinaryOperatorBitfields {
  470. friend class ASTStmtReader;
  471. friend class CXXRewrittenBinaryOperator;
  472. unsigned : NumCallExprBits;
  473. unsigned IsReversed : 1;
  474. };
  475. class CXXBoolLiteralExprBitfields {
  476. friend class CXXBoolLiteralExpr;
  477. unsigned : NumExprBits;
  478. /// The value of the boolean literal.
  479. unsigned Value : 1;
  480. /// The location of the boolean literal.
  481. SourceLocation Loc;
  482. };
  483. class CXXNullPtrLiteralExprBitfields {
  484. friend class CXXNullPtrLiteralExpr;
  485. unsigned : NumExprBits;
  486. /// The location of the null pointer literal.
  487. SourceLocation Loc;
  488. };
  489. class CXXThisExprBitfields {
  490. friend class CXXThisExpr;
  491. unsigned : NumExprBits;
  492. /// Whether this is an implicit "this".
  493. unsigned IsImplicit : 1;
  494. /// The location of the "this".
  495. SourceLocation Loc;
  496. };
  497. class CXXThrowExprBitfields {
  498. friend class ASTStmtReader;
  499. friend class CXXThrowExpr;
  500. unsigned : NumExprBits;
  501. /// Whether the thrown variable (if any) is in scope.
  502. unsigned IsThrownVariableInScope : 1;
  503. /// The location of the "throw".
  504. SourceLocation ThrowLoc;
  505. };
  506. class CXXDefaultArgExprBitfields {
  507. friend class ASTStmtReader;
  508. friend class CXXDefaultArgExpr;
  509. unsigned : NumExprBits;
  510. /// The location where the default argument expression was used.
  511. SourceLocation Loc;
  512. };
  513. class CXXDefaultInitExprBitfields {
  514. friend class ASTStmtReader;
  515. friend class CXXDefaultInitExpr;
  516. unsigned : NumExprBits;
  517. /// The location where the default initializer expression was used.
  518. SourceLocation Loc;
  519. };
  520. class CXXScalarValueInitExprBitfields {
  521. friend class ASTStmtReader;
  522. friend class CXXScalarValueInitExpr;
  523. unsigned : NumExprBits;
  524. SourceLocation RParenLoc;
  525. };
  526. class CXXNewExprBitfields {
  527. friend class ASTStmtReader;
  528. friend class ASTStmtWriter;
  529. friend class CXXNewExpr;
  530. unsigned : NumExprBits;
  531. /// Was the usage ::new, i.e. is the global new to be used?
  532. unsigned IsGlobalNew : 1;
  533. /// Do we allocate an array? If so, the first trailing "Stmt *" is the
  534. /// size expression.
  535. unsigned IsArray : 1;
  536. /// Should the alignment be passed to the allocation function?
  537. unsigned ShouldPassAlignment : 1;
  538. /// If this is an array allocation, does the usual deallocation
  539. /// function for the allocated type want to know the allocated size?
  540. unsigned UsualArrayDeleteWantsSize : 1;
  541. /// What kind of initializer do we have? Could be none, parens, or braces.
  542. /// In storage, we distinguish between "none, and no initializer expr", and
  543. /// "none, but an implicit initializer expr".
  544. unsigned StoredInitializationStyle : 2;
  545. /// True if the allocated type was expressed as a parenthesized type-id.
  546. unsigned IsParenTypeId : 1;
  547. /// The number of placement new arguments.
  548. unsigned NumPlacementArgs;
  549. };
  550. class CXXDeleteExprBitfields {
  551. friend class ASTStmtReader;
  552. friend class CXXDeleteExpr;
  553. unsigned : NumExprBits;
  554. /// Is this a forced global delete, i.e. "::delete"?
  555. unsigned GlobalDelete : 1;
  556. /// Is this the array form of delete, i.e. "delete[]"?
  557. unsigned ArrayForm : 1;
  558. /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is
  559. /// applied to pointer-to-array type (ArrayFormAsWritten will be false
  560. /// while ArrayForm will be true).
  561. unsigned ArrayFormAsWritten : 1;
  562. /// Does the usual deallocation function for the element type require
  563. /// a size_t argument?
  564. unsigned UsualArrayDeleteWantsSize : 1;
  565. /// Location of the expression.
  566. SourceLocation Loc;
  567. };
  568. class TypeTraitExprBitfields {
  569. friend class ASTStmtReader;
  570. friend class ASTStmtWriter;
  571. friend class TypeTraitExpr;
  572. unsigned : NumExprBits;
  573. /// The kind of type trait, which is a value of a TypeTrait enumerator.
  574. unsigned Kind : 8;
  575. /// If this expression is not value-dependent, this indicates whether
  576. /// the trait evaluated true or false.
  577. unsigned Value : 1;
  578. /// The number of arguments to this type trait. According to [implimits]
  579. /// 8 bits would be enough, but we require (and test for) at least 16 bits
  580. /// to mirror FunctionType.
  581. unsigned NumArgs;
  582. };
  583. class DependentScopeDeclRefExprBitfields {
  584. friend class ASTStmtReader;
  585. friend class ASTStmtWriter;
  586. friend class DependentScopeDeclRefExpr;
  587. unsigned : NumExprBits;
  588. /// Whether the name includes info for explicit template
  589. /// keyword and arguments.
  590. unsigned HasTemplateKWAndArgsInfo : 1;
  591. };
  592. class CXXConstructExprBitfields {
  593. friend class ASTStmtReader;
  594. friend class CXXConstructExpr;
  595. unsigned : NumExprBits;
  596. unsigned Elidable : 1;
  597. unsigned HadMultipleCandidates : 1;
  598. unsigned ListInitialization : 1;
  599. unsigned StdInitListInitialization : 1;
  600. unsigned ZeroInitialization : 1;
  601. unsigned ConstructionKind : 3;
  602. SourceLocation Loc;
  603. };
  604. class ExprWithCleanupsBitfields {
  605. friend class ASTStmtReader; // deserialization
  606. friend class ExprWithCleanups;
  607. unsigned : NumExprBits;
  608. // When false, it must not have side effects.
  609. unsigned CleanupsHaveSideEffects : 1;
  610. unsigned NumObjects : 32 - 1 - NumExprBits;
  611. };
  612. class CXXUnresolvedConstructExprBitfields {
  613. friend class ASTStmtReader;
  614. friend class CXXUnresolvedConstructExpr;
  615. unsigned : NumExprBits;
  616. /// The number of arguments used to construct the type.
  617. unsigned NumArgs;
  618. };
  619. class CXXDependentScopeMemberExprBitfields {
  620. friend class ASTStmtReader;
  621. friend class CXXDependentScopeMemberExpr;
  622. unsigned : NumExprBits;
  623. /// Whether this member expression used the '->' operator or
  624. /// the '.' operator.
  625. unsigned IsArrow : 1;
  626. /// Whether this member expression has info for explicit template
  627. /// keyword and arguments.
  628. unsigned HasTemplateKWAndArgsInfo : 1;
  629. /// See getFirstQualifierFoundInScope() and the comment listing
  630. /// the trailing objects.
  631. unsigned HasFirstQualifierFoundInScope : 1;
  632. /// The location of the '->' or '.' operator.
  633. SourceLocation OperatorLoc;
  634. };
  635. class OverloadExprBitfields {
  636. friend class ASTStmtReader;
  637. friend class OverloadExpr;
  638. unsigned : NumExprBits;
  639. /// Whether the name includes info for explicit template
  640. /// keyword and arguments.
  641. unsigned HasTemplateKWAndArgsInfo : 1;
  642. /// Padding used by the derived classes to store various bits. If you
  643. /// need to add some data here, shrink this padding and add your data
  644. /// above. NumOverloadExprBits also needs to be updated.
  645. unsigned : 32 - NumExprBits - 1;
  646. /// The number of results.
  647. unsigned NumResults;
  648. };
  649. enum { NumOverloadExprBits = NumExprBits + 1 };
  650. class UnresolvedLookupExprBitfields {
  651. friend class ASTStmtReader;
  652. friend class UnresolvedLookupExpr;
  653. unsigned : NumOverloadExprBits;
  654. /// True if these lookup results should be extended by
  655. /// argument-dependent lookup if this is the operand of a function call.
  656. unsigned RequiresADL : 1;
  657. /// True if these lookup results are overloaded. This is pretty trivially
  658. /// rederivable if we urgently need to kill this field.
  659. unsigned Overloaded : 1;
  660. };
  661. static_assert(sizeof(UnresolvedLookupExprBitfields) <= 4,
  662. "UnresolvedLookupExprBitfields must be <= than 4 bytes to"
  663. "avoid trashing OverloadExprBitfields::NumResults!");
  664. class UnresolvedMemberExprBitfields {
  665. friend class ASTStmtReader;
  666. friend class UnresolvedMemberExpr;
  667. unsigned : NumOverloadExprBits;
  668. /// Whether this member expression used the '->' operator or
  669. /// the '.' operator.
  670. unsigned IsArrow : 1;
  671. /// Whether the lookup results contain an unresolved using declaration.
  672. unsigned HasUnresolvedUsing : 1;
  673. };
  674. static_assert(sizeof(UnresolvedMemberExprBitfields) <= 4,
  675. "UnresolvedMemberExprBitfields must be <= than 4 bytes to"
  676. "avoid trashing OverloadExprBitfields::NumResults!");
  677. class CXXNoexceptExprBitfields {
  678. friend class ASTStmtReader;
  679. friend class CXXNoexceptExpr;
  680. unsigned : NumExprBits;
  681. unsigned Value : 1;
  682. };
  683. class SubstNonTypeTemplateParmExprBitfields {
  684. friend class ASTStmtReader;
  685. friend class SubstNonTypeTemplateParmExpr;
  686. unsigned : NumExprBits;
  687. /// The location of the non-type template parameter reference.
  688. SourceLocation NameLoc;
  689. };
  690. class LambdaExprBitfields {
  691. friend class ASTStmtReader;
  692. friend class ASTStmtWriter;
  693. friend class LambdaExpr;
  694. unsigned : NumExprBits;
  695. /// The default capture kind, which is a value of type
  696. /// LambdaCaptureDefault.
  697. unsigned CaptureDefault : 2;
  698. /// Whether this lambda had an explicit parameter list vs. an
  699. /// implicit (and empty) parameter list.
  700. unsigned ExplicitParams : 1;
  701. /// Whether this lambda had the result type explicitly specified.
  702. unsigned ExplicitResultType : 1;
  703. /// The number of captures.
  704. unsigned NumCaptures : 16;
  705. };
  706. class RequiresExprBitfields {
  707. friend class ASTStmtReader;
  708. friend class ASTStmtWriter;
  709. friend class RequiresExpr;
  710. unsigned : NumExprBits;
  711. unsigned IsSatisfied : 1;
  712. SourceLocation RequiresKWLoc;
  713. };
  714. //===--- C++ Coroutines TS bitfields classes ---===//
  715. class CoawaitExprBitfields {
  716. friend class CoawaitExpr;
  717. unsigned : NumExprBits;
  718. unsigned IsImplicit : 1;
  719. };
  720. //===--- Obj-C Expression bitfields classes ---===//
  721. class ObjCIndirectCopyRestoreExprBitfields {
  722. friend class ObjCIndirectCopyRestoreExpr;
  723. unsigned : NumExprBits;
  724. unsigned ShouldCopy : 1;
  725. };
  726. //===--- Clang Extensions bitfields classes ---===//
  727. class OpaqueValueExprBitfields {
  728. friend class ASTStmtReader;
  729. friend class OpaqueValueExpr;
  730. unsigned : NumExprBits;
  731. /// The OVE is a unique semantic reference to its source expression if this
  732. /// bit is set to true.
  733. unsigned IsUnique : 1;
  734. SourceLocation Loc;
  735. };
  736. union {
  737. // Same order as in StmtNodes.td.
  738. // Statements
  739. StmtBitfields StmtBits;
  740. NullStmtBitfields NullStmtBits;
  741. CompoundStmtBitfields CompoundStmtBits;
  742. LabelStmtBitfields LabelStmtBits;
  743. AttributedStmtBitfields AttributedStmtBits;
  744. IfStmtBitfields IfStmtBits;
  745. SwitchStmtBitfields SwitchStmtBits;
  746. WhileStmtBitfields WhileStmtBits;
  747. DoStmtBitfields DoStmtBits;
  748. ForStmtBitfields ForStmtBits;
  749. GotoStmtBitfields GotoStmtBits;
  750. ContinueStmtBitfields ContinueStmtBits;
  751. BreakStmtBitfields BreakStmtBits;
  752. ReturnStmtBitfields ReturnStmtBits;
  753. SwitchCaseBitfields SwitchCaseBits;
  754. // Expressions
  755. ExprBitfields ExprBits;
  756. ConstantExprBitfields ConstantExprBits;
  757. PredefinedExprBitfields PredefinedExprBits;
  758. DeclRefExprBitfields DeclRefExprBits;
  759. FloatingLiteralBitfields FloatingLiteralBits;
  760. StringLiteralBitfields StringLiteralBits;
  761. CharacterLiteralBitfields CharacterLiteralBits;
  762. UnaryOperatorBitfields UnaryOperatorBits;
  763. UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
  764. ArrayOrMatrixSubscriptExprBitfields ArrayOrMatrixSubscriptExprBits;
  765. CallExprBitfields CallExprBits;
  766. MemberExprBitfields MemberExprBits;
  767. CastExprBitfields CastExprBits;
  768. BinaryOperatorBitfields BinaryOperatorBits;
  769. InitListExprBitfields InitListExprBits;
  770. ParenListExprBitfields ParenListExprBits;
  771. GenericSelectionExprBitfields GenericSelectionExprBits;
  772. PseudoObjectExprBitfields PseudoObjectExprBits;
  773. SourceLocExprBitfields SourceLocExprBits;
  774. // GNU Extensions.
  775. StmtExprBitfields StmtExprBits;
  776. // C++ Expressions
  777. CXXOperatorCallExprBitfields CXXOperatorCallExprBits;
  778. CXXRewrittenBinaryOperatorBitfields CXXRewrittenBinaryOperatorBits;
  779. CXXBoolLiteralExprBitfields CXXBoolLiteralExprBits;
  780. CXXNullPtrLiteralExprBitfields CXXNullPtrLiteralExprBits;
  781. CXXThisExprBitfields CXXThisExprBits;
  782. CXXThrowExprBitfields CXXThrowExprBits;
  783. CXXDefaultArgExprBitfields CXXDefaultArgExprBits;
  784. CXXDefaultInitExprBitfields CXXDefaultInitExprBits;
  785. CXXScalarValueInitExprBitfields CXXScalarValueInitExprBits;
  786. CXXNewExprBitfields CXXNewExprBits;
  787. CXXDeleteExprBitfields CXXDeleteExprBits;
  788. TypeTraitExprBitfields TypeTraitExprBits;
  789. DependentScopeDeclRefExprBitfields DependentScopeDeclRefExprBits;
  790. CXXConstructExprBitfields CXXConstructExprBits;
  791. ExprWithCleanupsBitfields ExprWithCleanupsBits;
  792. CXXUnresolvedConstructExprBitfields CXXUnresolvedConstructExprBits;
  793. CXXDependentScopeMemberExprBitfields CXXDependentScopeMemberExprBits;
  794. OverloadExprBitfields OverloadExprBits;
  795. UnresolvedLookupExprBitfields UnresolvedLookupExprBits;
  796. UnresolvedMemberExprBitfields UnresolvedMemberExprBits;
  797. CXXNoexceptExprBitfields CXXNoexceptExprBits;
  798. SubstNonTypeTemplateParmExprBitfields SubstNonTypeTemplateParmExprBits;
  799. LambdaExprBitfields LambdaExprBits;
  800. RequiresExprBitfields RequiresExprBits;
  801. // C++ Coroutines TS expressions
  802. CoawaitExprBitfields CoawaitBits;
  803. // Obj-C Expressions
  804. ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
  805. // Clang Extensions
  806. OpaqueValueExprBitfields OpaqueValueExprBits;
  807. };
  808. public:
  809. // Only allow allocation of Stmts using the allocator in ASTContext
  810. // or by doing a placement new.
  811. void* operator new(size_t bytes, const ASTContext& C,
  812. unsigned alignment = 8);
  813. void* operator new(size_t bytes, const ASTContext* C,
  814. unsigned alignment = 8) {
  815. return operator new(bytes, *C, alignment);
  816. }
  817. void *operator new(size_t bytes, void *mem) noexcept { return mem; }
  818. void operator delete(void *, const ASTContext &, unsigned) noexcept {}
  819. void operator delete(void *, const ASTContext *, unsigned) noexcept {}
  820. void operator delete(void *, size_t) noexcept {}
  821. void operator delete(void *, void *) noexcept {}
  822. public:
  823. /// A placeholder type used to construct an empty shell of a
  824. /// type, that will be filled in later (e.g., by some
  825. /// de-serialization).
  826. struct EmptyShell {};
  827. /// The likelihood of a branch being taken.
  828. enum Likelihood {
  829. LH_Unlikely = -1, ///< Branch has the [[unlikely]] attribute.
  830. LH_None, ///< No attribute set or branches of the IfStmt have
  831. ///< the same attribute.
  832. LH_Likely ///< Branch has the [[likely]] attribute.
  833. };
  834. protected:
  835. /// Iterator for iterating over Stmt * arrays that contain only T *.
  836. ///
  837. /// This is needed because AST nodes use Stmt* arrays to store
  838. /// references to children (to be compatible with StmtIterator).
  839. template<typename T, typename TPtr = T *, typename StmtPtr = Stmt *>
  840. struct CastIterator
  841. : llvm::iterator_adaptor_base<CastIterator<T, TPtr, StmtPtr>, StmtPtr *,
  842. std::random_access_iterator_tag, TPtr> {
  843. using Base = typename CastIterator::iterator_adaptor_base;
  844. CastIterator() : Base(nullptr) {}
  845. CastIterator(StmtPtr *I) : Base(I) {}
  846. typename Base::value_type operator*() const {
  847. return cast_or_null<T>(*this->I);
  848. }
  849. };
  850. /// Const iterator for iterating over Stmt * arrays that contain only T *.
  851. template <typename T>
  852. using ConstCastIterator = CastIterator<T, const T *const, const Stmt *const>;
  853. using ExprIterator = CastIterator<Expr>;
  854. using ConstExprIterator = ConstCastIterator<Expr>;
  855. private:
  856. /// Whether statistic collection is enabled.
  857. static bool StatisticsEnabled;
  858. protected:
  859. /// Construct an empty statement.
  860. explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
  861. public:
  862. Stmt() = delete;
  863. Stmt(const Stmt &) = delete;
  864. Stmt(Stmt &&) = delete;
  865. Stmt &operator=(const Stmt &) = delete;
  866. Stmt &operator=(Stmt &&) = delete;
  867. Stmt(StmtClass SC) {
  868. static_assert(sizeof(*this) <= 8,
  869. "changing bitfields changed sizeof(Stmt)");
  870. static_assert(sizeof(*this) % alignof(void *) == 0,
  871. "Insufficient alignment!");
  872. StmtBits.sClass = SC;
  873. if (StatisticsEnabled) Stmt::addStmtClass(SC);
  874. }
  875. StmtClass getStmtClass() const {
  876. return static_cast<StmtClass>(StmtBits.sClass);
  877. }
  878. const char *getStmtClassName() const;
  879. /// SourceLocation tokens are not useful in isolation - they are low level
  880. /// value objects created/interpreted by SourceManager. We assume AST
  881. /// clients will have a pointer to the respective SourceManager.
  882. SourceRange getSourceRange() const LLVM_READONLY;
  883. SourceLocation getBeginLoc() const LLVM_READONLY;
  884. SourceLocation getEndLoc() const LLVM_READONLY;
  885. // global temp stats (until we have a per-module visitor)
  886. static void addStmtClass(const StmtClass s);
  887. static void EnableStatistics();
  888. static void PrintStats();
  889. /// \returns the likelihood of a set of attributes.
  890. static Likelihood getLikelihood(ArrayRef<const Attr *> Attrs);
  891. /// \returns the likelihood of a statement.
  892. static Likelihood getLikelihood(const Stmt *S);
  893. /// \returns the likelihood attribute of a statement.
  894. static const Attr *getLikelihoodAttr(const Stmt *S);
  895. /// \returns the likelihood of the 'then' branch of an 'if' statement. The
  896. /// 'else' branch is required to determine whether both branches specify the
  897. /// same likelihood, which affects the result.
  898. static Likelihood getLikelihood(const Stmt *Then, const Stmt *Else);
  899. /// \returns whether the likelihood of the branches of an if statement are
  900. /// conflicting. When the first element is \c true there's a conflict and
  901. /// the Attr's are the conflicting attributes of the Then and Else Stmt.
  902. static std::tuple<bool, const Attr *, const Attr *>
  903. determineLikelihoodConflict(const Stmt *Then, const Stmt *Else);
  904. /// Dumps the specified AST fragment and all subtrees to
  905. /// \c llvm::errs().
  906. void dump() const;
  907. void dump(raw_ostream &OS, const ASTContext &Context) const;
  908. /// \return Unique reproducible object identifier
  909. int64_t getID(const ASTContext &Context) const;
  910. /// dumpColor - same as dump(), but forces color highlighting.
  911. void dumpColor() const;
  912. /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
  913. /// back to its original source language syntax.
  914. void dumpPretty(const ASTContext &Context) const;
  915. void printPretty(raw_ostream &OS, PrinterHelper *Helper,
  916. const PrintingPolicy &Policy, unsigned Indentation = 0,
  917. StringRef NewlineSymbol = "\n",
  918. const ASTContext *Context = nullptr) const;
  919. void printPrettyControlled(raw_ostream &OS, PrinterHelper *Helper,
  920. const PrintingPolicy &Policy,
  921. unsigned Indentation = 0,
  922. StringRef NewlineSymbol = "\n",
  923. const ASTContext *Context = nullptr) const;
  924. /// Pretty-prints in JSON format.
  925. void printJson(raw_ostream &Out, PrinterHelper *Helper,
  926. const PrintingPolicy &Policy, bool AddQuotes) const;
  927. /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
  928. /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
  929. void viewAST() const;
  930. /// Skip no-op (attributed, compound) container stmts and skip captured
  931. /// stmt at the top, if \a IgnoreCaptured is true.
  932. Stmt *IgnoreContainers(bool IgnoreCaptured = false);
  933. const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
  934. return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
  935. }
  936. const Stmt *stripLabelLikeStatements() const;
  937. Stmt *stripLabelLikeStatements() {
  938. return const_cast<Stmt*>(
  939. const_cast<const Stmt*>(this)->stripLabelLikeStatements());
  940. }
  941. /// Child Iterators: All subclasses must implement 'children'
  942. /// to permit easy iteration over the substatements/subexpessions of an
  943. /// AST node. This permits easy iteration over all nodes in the AST.
  944. using child_iterator = StmtIterator;
  945. using const_child_iterator = ConstStmtIterator;
  946. using child_range = llvm::iterator_range<child_iterator>;
  947. using const_child_range = llvm::iterator_range<const_child_iterator>;
  948. child_range children();
  949. const_child_range children() const {
  950. auto Children = const_cast<Stmt *>(this)->children();
  951. return const_child_range(Children.begin(), Children.end());
  952. }
  953. child_iterator child_begin() { return children().begin(); }
  954. child_iterator child_end() { return children().end(); }
  955. const_child_iterator child_begin() const { return children().begin(); }
  956. const_child_iterator child_end() const { return children().end(); }
  957. /// Produce a unique representation of the given statement.
  958. ///
  959. /// \param ID once the profiling operation is complete, will contain
  960. /// the unique representation of the given statement.
  961. ///
  962. /// \param Context the AST context in which the statement resides
  963. ///
  964. /// \param Canonical whether the profile should be based on the canonical
  965. /// representation of this statement (e.g., where non-type template
  966. /// parameters are identified by index/level rather than their
  967. /// declaration pointers) or the exact representation of the statement as
  968. /// written in the source.
  969. void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
  970. bool Canonical) const;
  971. /// Calculate a unique representation for a statement that is
  972. /// stable across compiler invocations.
  973. ///
  974. /// \param ID profile information will be stored in ID.
  975. ///
  976. /// \param Hash an ODRHash object which will be called where pointers would
  977. /// have been used in the Profile function.
  978. void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
  979. };
  980. /// DeclStmt - Adaptor class for mixing declarations with statements and
  981. /// expressions. For example, CompoundStmt mixes statements, expressions
  982. /// and declarations (variables, types). Another example is ForStmt, where
  983. /// the first statement can be an expression or a declaration.
  984. class DeclStmt : public Stmt {
  985. DeclGroupRef DG;
  986. SourceLocation StartLoc, EndLoc;
  987. public:
  988. DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
  989. : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
  990. /// Build an empty declaration statement.
  991. explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
  992. /// isSingleDecl - This method returns true if this DeclStmt refers
  993. /// to a single Decl.
  994. bool isSingleDecl() const { return DG.isSingleDecl(); }
  995. const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
  996. Decl *getSingleDecl() { return DG.getSingleDecl(); }
  997. const DeclGroupRef getDeclGroup() const { return DG; }
  998. DeclGroupRef getDeclGroup() { return DG; }
  999. void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
  1000. void setStartLoc(SourceLocation L) { StartLoc = L; }
  1001. SourceLocation getEndLoc() const { return EndLoc; }
  1002. void setEndLoc(SourceLocation L) { EndLoc = L; }
  1003. SourceLocation getBeginLoc() const LLVM_READONLY { return StartLoc; }
  1004. static bool classof(const Stmt *T) {
  1005. return T->getStmtClass() == DeclStmtClass;
  1006. }
  1007. // Iterators over subexpressions.
  1008. child_range children() {
  1009. return child_range(child_iterator(DG.begin(), DG.end()),
  1010. child_iterator(DG.end(), DG.end()));
  1011. }
  1012. const_child_range children() const {
  1013. auto Children = const_cast<DeclStmt *>(this)->children();
  1014. return const_child_range(Children);
  1015. }
  1016. using decl_iterator = DeclGroupRef::iterator;
  1017. using const_decl_iterator = DeclGroupRef::const_iterator;
  1018. using decl_range = llvm::iterator_range<decl_iterator>;
  1019. using decl_const_range = llvm::iterator_range<const_decl_iterator>;
  1020. decl_range decls() { return decl_range(decl_begin(), decl_end()); }
  1021. decl_const_range decls() const {
  1022. return decl_const_range(decl_begin(), decl_end());
  1023. }
  1024. decl_iterator decl_begin() { return DG.begin(); }
  1025. decl_iterator decl_end() { return DG.end(); }
  1026. const_decl_iterator decl_begin() const { return DG.begin(); }
  1027. const_decl_iterator decl_end() const { return DG.end(); }
  1028. using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
  1029. reverse_decl_iterator decl_rbegin() {
  1030. return reverse_decl_iterator(decl_end());
  1031. }
  1032. reverse_decl_iterator decl_rend() {
  1033. return reverse_decl_iterator(decl_begin());
  1034. }
  1035. };
  1036. /// NullStmt - This is the null statement ";": C99 6.8.3p3.
  1037. ///
  1038. class NullStmt : public Stmt {
  1039. public:
  1040. NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
  1041. : Stmt(NullStmtClass) {
  1042. NullStmtBits.HasLeadingEmptyMacro = hasLeadingEmptyMacro;
  1043. setSemiLoc(L);
  1044. }
  1045. /// Build an empty null statement.
  1046. explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
  1047. SourceLocation getSemiLoc() const { return NullStmtBits.SemiLoc; }
  1048. void setSemiLoc(SourceLocation L) { NullStmtBits.SemiLoc = L; }
  1049. bool hasLeadingEmptyMacro() const {
  1050. return NullStmtBits.HasLeadingEmptyMacro;
  1051. }
  1052. SourceLocation getBeginLoc() const { return getSemiLoc(); }
  1053. SourceLocation getEndLoc() const { return getSemiLoc(); }
  1054. static bool classof(const Stmt *T) {
  1055. return T->getStmtClass() == NullStmtClass;
  1056. }
  1057. child_range children() {
  1058. return child_range(child_iterator(), child_iterator());
  1059. }
  1060. const_child_range children() const {
  1061. return const_child_range(const_child_iterator(), const_child_iterator());
  1062. }
  1063. };
  1064. /// CompoundStmt - This represents a group of statements like { stmt stmt }.
  1065. class CompoundStmt final : public Stmt,
  1066. private llvm::TrailingObjects<CompoundStmt, Stmt *> {
  1067. friend class ASTStmtReader;
  1068. friend TrailingObjects;
  1069. /// The location of the closing "}". LBraceLoc is stored in CompoundStmtBits.
  1070. SourceLocation RBraceLoc;
  1071. CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
  1072. explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
  1073. void setStmts(ArrayRef<Stmt *> Stmts);
  1074. public:
  1075. static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
  1076. SourceLocation LB, SourceLocation RB);
  1077. // Build an empty compound statement with a location.
  1078. explicit CompoundStmt(SourceLocation Loc)
  1079. : Stmt(CompoundStmtClass), RBraceLoc(Loc) {
  1080. CompoundStmtBits.NumStmts = 0;
  1081. CompoundStmtBits.LBraceLoc = Loc;
  1082. }
  1083. // Build an empty compound statement.
  1084. static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
  1085. bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
  1086. unsigned size() const { return CompoundStmtBits.NumStmts; }
  1087. using body_iterator = Stmt **;
  1088. using body_range = llvm::iterator_range<body_iterator>;
  1089. body_range body() { return body_range(body_begin(), body_end()); }
  1090. body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
  1091. body_iterator body_end() { return body_begin() + size(); }
  1092. Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
  1093. Stmt *body_back() {
  1094. return !body_empty() ? body_begin()[size() - 1] : nullptr;
  1095. }
  1096. using const_body_iterator = Stmt *const *;
  1097. using body_const_range = llvm::iterator_range<const_body_iterator>;
  1098. body_const_range body() const {
  1099. return body_const_range(body_begin(), body_end());
  1100. }
  1101. const_body_iterator body_begin() const {
  1102. return getTrailingObjects<Stmt *>();
  1103. }
  1104. const_body_iterator body_end() const { return body_begin() + size(); }
  1105. const Stmt *body_front() const {
  1106. return !body_empty() ? body_begin()[0] : nullptr;
  1107. }
  1108. const Stmt *body_back() const {
  1109. return !body_empty() ? body_begin()[size() - 1] : nullptr;
  1110. }
  1111. using reverse_body_iterator = std::reverse_iterator<body_iterator>;
  1112. reverse_body_iterator body_rbegin() {
  1113. return reverse_body_iterator(body_end());
  1114. }
  1115. reverse_body_iterator body_rend() {
  1116. return reverse_body_iterator(body_begin());
  1117. }
  1118. using const_reverse_body_iterator =
  1119. std::reverse_iterator<const_body_iterator>;
  1120. const_reverse_body_iterator body_rbegin() const {
  1121. return const_reverse_body_iterator(body_end());
  1122. }
  1123. const_reverse_body_iterator body_rend() const {
  1124. return const_reverse_body_iterator(body_begin());
  1125. }
  1126. // Get the Stmt that StmtExpr would consider to be the result of this
  1127. // compound statement. This is used by StmtExpr to properly emulate the GCC
  1128. // compound expression extension, which ignores trailing NullStmts when
  1129. // getting the result of the expression.
  1130. // i.e. ({ 5;;; })
  1131. // ^^ ignored
  1132. // If we don't find something that isn't a NullStmt, just return the last
  1133. // Stmt.
  1134. Stmt *getStmtExprResult() {
  1135. for (auto *B : llvm::reverse(body())) {
  1136. if (!isa<NullStmt>(B))
  1137. return B;
  1138. }
  1139. return body_back();
  1140. }
  1141. const Stmt *getStmtExprResult() const {
  1142. return const_cast<CompoundStmt *>(this)->getStmtExprResult();
  1143. }
  1144. SourceLocation getBeginLoc() const { return CompoundStmtBits.LBraceLoc; }
  1145. SourceLocation getEndLoc() const { return RBraceLoc; }
  1146. SourceLocation getLBracLoc() const { return CompoundStmtBits.LBraceLoc; }
  1147. SourceLocation getRBracLoc() const { return RBraceLoc; }
  1148. static bool classof(const Stmt *T) {
  1149. return T->getStmtClass() == CompoundStmtClass;
  1150. }
  1151. // Iterators
  1152. child_range children() { return child_range(body_begin(), body_end()); }
  1153. const_child_range children() const {
  1154. return const_child_range(body_begin(), body_end());
  1155. }
  1156. };
  1157. // SwitchCase is the base class for CaseStmt and DefaultStmt,
  1158. class SwitchCase : public Stmt {
  1159. protected:
  1160. /// The location of the ":".
  1161. SourceLocation ColonLoc;
  1162. // The location of the "case" or "default" keyword. Stored in SwitchCaseBits.
  1163. // SourceLocation KeywordLoc;
  1164. /// A pointer to the following CaseStmt or DefaultStmt class,
  1165. /// used by SwitchStmt.
  1166. SwitchCase *NextSwitchCase = nullptr;
  1167. SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
  1168. : Stmt(SC), ColonLoc(ColonLoc) {
  1169. setKeywordLoc(KWLoc);
  1170. }
  1171. SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
  1172. public:
  1173. const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
  1174. SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
  1175. void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
  1176. SourceLocation getKeywordLoc() const { return SwitchCaseBits.KeywordLoc; }
  1177. void setKeywordLoc(SourceLocation L) { SwitchCaseBits.KeywordLoc = L; }
  1178. SourceLocation getColonLoc() const { return ColonLoc; }
  1179. void setColonLoc(SourceLocation L) { ColonLoc = L; }
  1180. inline Stmt *getSubStmt();
  1181. const Stmt *getSubStmt() const {
  1182. return const_cast<SwitchCase *>(this)->getSubStmt();
  1183. }
  1184. SourceLocation getBeginLoc() const { return getKeywordLoc(); }
  1185. inline SourceLocation getEndLoc() const LLVM_READONLY;
  1186. static bool classof(const Stmt *T) {
  1187. return T->getStmtClass() == CaseStmtClass ||
  1188. T->getStmtClass() == DefaultStmtClass;
  1189. }
  1190. };
  1191. /// CaseStmt - Represent a case statement. It can optionally be a GNU case
  1192. /// statement of the form LHS ... RHS representing a range of cases.
  1193. class CaseStmt final
  1194. : public SwitchCase,
  1195. private llvm::TrailingObjects<CaseStmt, Stmt *, SourceLocation> {
  1196. friend TrailingObjects;
  1197. // CaseStmt is followed by several trailing objects, some of which optional.
  1198. // Note that it would be more convenient to put the optional trailing objects
  1199. // at the end but this would impact children().
  1200. // The trailing objects are in order:
  1201. //
  1202. // * A "Stmt *" for the LHS of the case statement. Always present.
  1203. //
  1204. // * A "Stmt *" for the RHS of the case statement. This is a GNU extension
  1205. // which allow ranges in cases statement of the form LHS ... RHS.
  1206. // Present if and only if caseStmtIsGNURange() is true.
  1207. //
  1208. // * A "Stmt *" for the substatement of the case statement. Always present.
  1209. //
  1210. // * A SourceLocation for the location of the ... if this is a case statement
  1211. // with a range. Present if and only if caseStmtIsGNURange() is true.
  1212. enum { LhsOffset = 0, SubStmtOffsetFromRhs = 1 };
  1213. enum { NumMandatoryStmtPtr = 2 };
  1214. unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
  1215. return NumMandatoryStmtPtr + caseStmtIsGNURange();
  1216. }
  1217. unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
  1218. return caseStmtIsGNURange();
  1219. }
  1220. unsigned lhsOffset() const { return LhsOffset; }
  1221. unsigned rhsOffset() const { return LhsOffset + caseStmtIsGNURange(); }
  1222. unsigned subStmtOffset() const { return rhsOffset() + SubStmtOffsetFromRhs; }
  1223. /// Build a case statement assuming that the storage for the
  1224. /// trailing objects has been properly allocated.
  1225. CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
  1226. SourceLocation ellipsisLoc, SourceLocation colonLoc)
  1227. : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
  1228. // Handle GNU case statements of the form LHS ... RHS.
  1229. bool IsGNURange = rhs != nullptr;
  1230. SwitchCaseBits.CaseStmtIsGNURange = IsGNURange;
  1231. setLHS(lhs);
  1232. setSubStmt(nullptr);
  1233. if (IsGNURange) {
  1234. setRHS(rhs);
  1235. setEllipsisLoc(ellipsisLoc);
  1236. }
  1237. }
  1238. /// Build an empty switch case statement.
  1239. explicit CaseStmt(EmptyShell Empty, bool CaseStmtIsGNURange)
  1240. : SwitchCase(CaseStmtClass, Empty) {
  1241. SwitchCaseBits.CaseStmtIsGNURange = CaseStmtIsGNURange;
  1242. }
  1243. public:
  1244. /// Build a case statement.
  1245. static CaseStmt *Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
  1246. SourceLocation caseLoc, SourceLocation ellipsisLoc,
  1247. SourceLocation colonLoc);
  1248. /// Build an empty case statement.
  1249. static CaseStmt *CreateEmpty(const ASTContext &Ctx, bool CaseStmtIsGNURange);
  1250. /// True if this case statement is of the form case LHS ... RHS, which
  1251. /// is a GNU extension. In this case the RHS can be obtained with getRHS()
  1252. /// and the location of the ellipsis can be obtained with getEllipsisLoc().
  1253. bool caseStmtIsGNURange() const { return SwitchCaseBits.CaseStmtIsGNURange; }
  1254. SourceLocation getCaseLoc() const { return getKeywordLoc(); }
  1255. void setCaseLoc(SourceLocation L) { setKeywordLoc(L); }
  1256. /// Get the location of the ... in a case statement of the form LHS ... RHS.
  1257. SourceLocation getEllipsisLoc() const {
  1258. return caseStmtIsGNURange() ? *getTrailingObjects<SourceLocation>()
  1259. : SourceLocation();
  1260. }
  1261. /// Set the location of the ... in a case statement of the form LHS ... RHS.
  1262. /// Assert that this case statement is of this form.
  1263. void setEllipsisLoc(SourceLocation L) {
  1264. assert(
  1265. caseStmtIsGNURange() &&
  1266. "setEllipsisLoc but this is not a case stmt of the form LHS ... RHS!");
  1267. *getTrailingObjects<SourceLocation>() = L;
  1268. }
  1269. Expr *getLHS() {
  1270. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
  1271. }
  1272. const Expr *getLHS() const {
  1273. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
  1274. }
  1275. void setLHS(Expr *Val) {
  1276. getTrailingObjects<Stmt *>()[lhsOffset()] = reinterpret_cast<Stmt *>(Val);
  1277. }
  1278. Expr *getRHS() {
  1279. return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
  1280. getTrailingObjects<Stmt *>()[rhsOffset()])
  1281. : nullptr;
  1282. }
  1283. const Expr *getRHS() const {
  1284. return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
  1285. getTrailingObjects<Stmt *>()[rhsOffset()])
  1286. : nullptr;
  1287. }
  1288. void setRHS(Expr *Val) {
  1289. assert(caseStmtIsGNURange() &&
  1290. "setRHS but this is not a case stmt of the form LHS ... RHS!");
  1291. getTrailingObjects<Stmt *>()[rhsOffset()] = reinterpret_cast<Stmt *>(Val);
  1292. }
  1293. Stmt *getSubStmt() { return getTrailingObjects<Stmt *>()[subStmtOffset()]; }
  1294. const Stmt *getSubStmt() const {
  1295. return getTrailingObjects<Stmt *>()[subStmtOffset()];
  1296. }
  1297. void setSubStmt(Stmt *S) {
  1298. getTrailingObjects<Stmt *>()[subStmtOffset()] = S;
  1299. }
  1300. SourceLocation getBeginLoc() const { return getKeywordLoc(); }
  1301. SourceLocation getEndLoc() const LLVM_READONLY {
  1302. // Handle deeply nested case statements with iteration instead of recursion.
  1303. const CaseStmt *CS = this;
  1304. while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
  1305. CS = CS2;
  1306. return CS->getSubStmt()->getEndLoc();
  1307. }
  1308. static bool classof(const Stmt *T) {
  1309. return T->getStmtClass() == CaseStmtClass;
  1310. }
  1311. // Iterators
  1312. child_range children() {
  1313. return child_range(getTrailingObjects<Stmt *>(),
  1314. getTrailingObjects<Stmt *>() +
  1315. numTrailingObjects(OverloadToken<Stmt *>()));
  1316. }
  1317. const_child_range children() const {
  1318. return const_child_range(getTrailingObjects<Stmt *>(),
  1319. getTrailingObjects<Stmt *>() +
  1320. numTrailingObjects(OverloadToken<Stmt *>()));
  1321. }
  1322. };
  1323. class DefaultStmt : public SwitchCase {
  1324. Stmt *SubStmt;
  1325. public:
  1326. DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt)
  1327. : SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
  1328. /// Build an empty default statement.
  1329. explicit DefaultStmt(EmptyShell Empty)
  1330. : SwitchCase(DefaultStmtClass, Empty) {}
  1331. Stmt *getSubStmt() { return SubStmt; }
  1332. const Stmt *getSubStmt() const { return SubStmt; }
  1333. void setSubStmt(Stmt *S) { SubStmt = S; }
  1334. SourceLocation getDefaultLoc() const { return getKeywordLoc(); }
  1335. void setDefaultLoc(SourceLocation L) { setKeywordLoc(L); }
  1336. SourceLocation getBeginLoc() const { return getKeywordLoc(); }
  1337. SourceLocation getEndLoc() const LLVM_READONLY {
  1338. return SubStmt->getEndLoc();
  1339. }
  1340. static bool classof(const Stmt *T) {
  1341. return T->getStmtClass() == DefaultStmtClass;
  1342. }
  1343. // Iterators
  1344. child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
  1345. const_child_range children() const {
  1346. return const_child_range(&SubStmt, &SubStmt + 1);
  1347. }
  1348. };
  1349. SourceLocation SwitchCase::getEndLoc() const {
  1350. if (const auto *CS = dyn_cast<CaseStmt>(this))
  1351. return CS->getEndLoc();
  1352. else if (const auto *DS = dyn_cast<DefaultStmt>(this))
  1353. return DS->getEndLoc();
  1354. llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
  1355. }
  1356. Stmt *SwitchCase::getSubStmt() {
  1357. if (auto *CS = dyn_cast<CaseStmt>(this))
  1358. return CS->getSubStmt();
  1359. else if (auto *DS = dyn_cast<DefaultStmt>(this))
  1360. return DS->getSubStmt();
  1361. llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
  1362. }
  1363. /// Represents a statement that could possibly have a value and type. This
  1364. /// covers expression-statements, as well as labels and attributed statements.
  1365. ///
  1366. /// Value statements have a special meaning when they are the last non-null
  1367. /// statement in a GNU statement expression, where they determine the value
  1368. /// of the statement expression.
  1369. class ValueStmt : public Stmt {
  1370. protected:
  1371. using Stmt::Stmt;
  1372. public:
  1373. const Expr *getExprStmt() const;
  1374. Expr *getExprStmt() {
  1375. const ValueStmt *ConstThis = this;
  1376. return const_cast<Expr*>(ConstThis->getExprStmt());
  1377. }
  1378. static bool classof(const Stmt *T) {
  1379. return T->getStmtClass() >= firstValueStmtConstant &&
  1380. T->getStmtClass() <= lastValueStmtConstant;
  1381. }
  1382. };
  1383. /// LabelStmt - Represents a label, which has a substatement. For example:
  1384. /// foo: return;
  1385. class LabelStmt : public ValueStmt {
  1386. LabelDecl *TheDecl;
  1387. Stmt *SubStmt;
  1388. bool SideEntry = false;
  1389. public:
  1390. /// Build a label statement.
  1391. LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
  1392. : ValueStmt(LabelStmtClass), TheDecl(D), SubStmt(substmt) {
  1393. setIdentLoc(IL);
  1394. }
  1395. /// Build an empty label statement.
  1396. explicit LabelStmt(EmptyShell Empty) : ValueStmt(LabelStmtClass, Empty) {}
  1397. SourceLocation getIdentLoc() const { return LabelStmtBits.IdentLoc; }
  1398. void setIdentLoc(SourceLocation L) { LabelStmtBits.IdentLoc = L; }
  1399. LabelDecl *getDecl() const { return TheDecl; }
  1400. void setDecl(LabelDecl *D) { TheDecl = D; }
  1401. const char *getName() const;
  1402. Stmt *getSubStmt() { return SubStmt; }
  1403. const Stmt *getSubStmt() const { return SubStmt; }
  1404. void setSubStmt(Stmt *SS) { SubStmt = SS; }
  1405. SourceLocation getBeginLoc() const { return getIdentLoc(); }
  1406. SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
  1407. child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
  1408. const_child_range children() const {
  1409. return const_child_range(&SubStmt, &SubStmt + 1);
  1410. }
  1411. static bool classof(const Stmt *T) {
  1412. return T->getStmtClass() == LabelStmtClass;
  1413. }
  1414. bool isSideEntry() const { return SideEntry; }
  1415. void setSideEntry(bool SE) { SideEntry = SE; }
  1416. };
  1417. /// Represents an attribute applied to a statement.
  1418. ///
  1419. /// Represents an attribute applied to a statement. For example:
  1420. /// [[omp::for(...)]] for (...) { ... }
  1421. class AttributedStmt final
  1422. : public ValueStmt,
  1423. private llvm::TrailingObjects<AttributedStmt, const Attr *> {
  1424. friend class ASTStmtReader;
  1425. friend TrailingObjects;
  1426. Stmt *SubStmt;
  1427. AttributedStmt(SourceLocation Loc, ArrayRef<const Attr *> Attrs,
  1428. Stmt *SubStmt)
  1429. : ValueStmt(AttributedStmtClass), SubStmt(SubStmt) {
  1430. AttributedStmtBits.NumAttrs = Attrs.size();
  1431. AttributedStmtBits.AttrLoc = Loc;
  1432. std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
  1433. }
  1434. explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
  1435. : ValueStmt(AttributedStmtClass, Empty) {
  1436. AttributedStmtBits.NumAttrs = NumAttrs;
  1437. AttributedStmtBits.AttrLoc = SourceLocation{};
  1438. std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
  1439. }
  1440. const Attr *const *getAttrArrayPtr() const {
  1441. return getTrailingObjects<const Attr *>();
  1442. }
  1443. const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
  1444. public:
  1445. static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
  1446. ArrayRef<const Attr *> Attrs, Stmt *SubStmt);
  1447. // Build an empty attributed statement.
  1448. static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
  1449. SourceLocation getAttrLoc() const { return AttributedStmtBits.AttrLoc; }
  1450. ArrayRef<const Attr *> getAttrs() const {
  1451. return llvm::makeArrayRef(getAttrArrayPtr(), AttributedStmtBits.NumAttrs);
  1452. }
  1453. Stmt *getSubStmt() { return SubStmt; }
  1454. const Stmt *getSubStmt() const { return SubStmt; }
  1455. SourceLocation getBeginLoc() const { return getAttrLoc(); }
  1456. SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
  1457. child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
  1458. const_child_range children() const {
  1459. return const_child_range(&SubStmt, &SubStmt + 1);
  1460. }
  1461. static bool classof(const Stmt *T) {
  1462. return T->getStmtClass() == AttributedStmtClass;
  1463. }
  1464. };
  1465. /// IfStmt - This represents an if/then/else.
  1466. class IfStmt final
  1467. : public Stmt,
  1468. private llvm::TrailingObjects<IfStmt, Stmt *, SourceLocation> {
  1469. friend TrailingObjects;
  1470. // IfStmt is followed by several trailing objects, some of which optional.
  1471. // Note that it would be more convenient to put the optional trailing
  1472. // objects at then end but this would change the order of the children.
  1473. // The trailing objects are in order:
  1474. //
  1475. // * A "Stmt *" for the init statement.
  1476. // Present if and only if hasInitStorage().
  1477. //
  1478. // * A "Stmt *" for the condition variable.
  1479. // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
  1480. //
  1481. // * A "Stmt *" for the condition.
  1482. // Always present. This is in fact a "Expr *".
  1483. //
  1484. // * A "Stmt *" for the then statement.
  1485. // Always present.
  1486. //
  1487. // * A "Stmt *" for the else statement.
  1488. // Present if and only if hasElseStorage().
  1489. //
  1490. // * A "SourceLocation" for the location of the "else".
  1491. // Present if and only if hasElseStorage().
  1492. enum { InitOffset = 0, ThenOffsetFromCond = 1, ElseOffsetFromCond = 2 };
  1493. enum { NumMandatoryStmtPtr = 2 };
  1494. SourceLocation LParenLoc;
  1495. SourceLocation RParenLoc;
  1496. unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
  1497. return NumMandatoryStmtPtr + hasElseStorage() + hasVarStorage() +
  1498. hasInitStorage();
  1499. }
  1500. unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
  1501. return hasElseStorage();
  1502. }
  1503. unsigned initOffset() const { return InitOffset; }
  1504. unsigned varOffset() const { return InitOffset + hasInitStorage(); }
  1505. unsigned condOffset() const {
  1506. return InitOffset + hasInitStorage() + hasVarStorage();
  1507. }
  1508. unsigned thenOffset() const { return condOffset() + ThenOffsetFromCond; }
  1509. unsigned elseOffset() const { return condOffset() + ElseOffsetFromCond; }
  1510. /// Build an if/then/else statement.
  1511. IfStmt(const ASTContext &Ctx, SourceLocation IL, IfStatementKind Kind,
  1512. Stmt *Init, VarDecl *Var, Expr *Cond, SourceLocation LParenLoc,
  1513. SourceLocation RParenLoc, Stmt *Then, SourceLocation EL, Stmt *Else);
  1514. /// Build an empty if/then/else statement.
  1515. explicit IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit);
  1516. public:
  1517. /// Create an IfStmt.
  1518. static IfStmt *Create(const ASTContext &Ctx, SourceLocation IL,
  1519. IfStatementKind Kind, Stmt *Init, VarDecl *Var,
  1520. Expr *Cond, SourceLocation LPL, SourceLocation RPL,
  1521. Stmt *Then, SourceLocation EL = SourceLocation(),
  1522. Stmt *Else = nullptr);
  1523. /// Create an empty IfStmt optionally with storage for an else statement,
  1524. /// condition variable and init expression.
  1525. static IfStmt *CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
  1526. bool HasInit);
  1527. /// True if this IfStmt has the storage for an init statement.
  1528. bool hasInitStorage() const { return IfStmtBits.HasInit; }
  1529. /// True if this IfStmt has storage for a variable declaration.
  1530. bool hasVarStorage() const { return IfStmtBits.HasVar; }
  1531. /// True if this IfStmt has storage for an else statement.
  1532. bool hasElseStorage() const { return IfStmtBits.HasElse; }
  1533. Expr *getCond() {
  1534. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
  1535. }
  1536. const Expr *getCond() const {
  1537. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
  1538. }
  1539. void setCond(Expr *Cond) {
  1540. getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
  1541. }
  1542. Stmt *getThen() { return getTrailingObjects<Stmt *>()[thenOffset()]; }
  1543. const Stmt *getThen() const {
  1544. return getTrailingObjects<Stmt *>()[thenOffset()];
  1545. }
  1546. void setThen(Stmt *Then) {
  1547. getTrailingObjects<Stmt *>()[thenOffset()] = Then;
  1548. }
  1549. Stmt *getElse() {
  1550. return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
  1551. : nullptr;
  1552. }
  1553. const Stmt *getElse() const {
  1554. return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
  1555. : nullptr;
  1556. }
  1557. void setElse(Stmt *Else) {
  1558. assert(hasElseStorage() &&
  1559. "This if statement has no storage for an else statement!");
  1560. getTrailingObjects<Stmt *>()[elseOffset()] = Else;
  1561. }
  1562. /// Retrieve the variable declared in this "if" statement, if any.
  1563. ///
  1564. /// In the following example, "x" is the condition variable.
  1565. /// \code
  1566. /// if (int x = foo()) {
  1567. /// printf("x is %d", x);
  1568. /// }
  1569. /// \endcode
  1570. VarDecl *getConditionVariable();
  1571. const VarDecl *getConditionVariable() const {
  1572. return const_cast<IfStmt *>(this)->getConditionVariable();
  1573. }
  1574. /// Set the condition variable for this if statement.
  1575. /// The if statement must have storage for the condition variable.
  1576. void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
  1577. /// If this IfStmt has a condition variable, return the faux DeclStmt
  1578. /// associated with the creation of that condition variable.
  1579. DeclStmt *getConditionVariableDeclStmt() {
  1580. return hasVarStorage() ? static_cast<DeclStmt *>(
  1581. getTrailingObjects<Stmt *>()[varOffset()])
  1582. : nullptr;
  1583. }
  1584. const DeclStmt *getConditionVariableDeclStmt() const {
  1585. return hasVarStorage() ? static_cast<DeclStmt *>(
  1586. getTrailingObjects<Stmt *>()[varOffset()])
  1587. : nullptr;
  1588. }
  1589. Stmt *getInit() {
  1590. return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
  1591. : nullptr;
  1592. }
  1593. const Stmt *getInit() const {
  1594. return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
  1595. : nullptr;
  1596. }
  1597. void setInit(Stmt *Init) {
  1598. assert(hasInitStorage() &&
  1599. "This if statement has no storage for an init statement!");
  1600. getTrailingObjects<Stmt *>()[initOffset()] = Init;
  1601. }
  1602. SourceLocation getIfLoc() const { return IfStmtBits.IfLoc; }
  1603. void setIfLoc(SourceLocation IfLoc) { IfStmtBits.IfLoc = IfLoc; }
  1604. SourceLocation getElseLoc() const {
  1605. return hasElseStorage() ? *getTrailingObjects<SourceLocation>()
  1606. : SourceLocation();
  1607. }
  1608. void setElseLoc(SourceLocation ElseLoc) {
  1609. assert(hasElseStorage() &&
  1610. "This if statement has no storage for an else statement!");
  1611. *getTrailingObjects<SourceLocation>() = ElseLoc;
  1612. }
  1613. bool isConsteval() const {
  1614. return getStatementKind() == IfStatementKind::ConstevalNonNegated ||
  1615. getStatementKind() == IfStatementKind::ConstevalNegated;
  1616. }
  1617. bool isNonNegatedConsteval() const {
  1618. return getStatementKind() == IfStatementKind::ConstevalNonNegated;
  1619. }
  1620. bool isNegatedConsteval() const {
  1621. return getStatementKind() == IfStatementKind::ConstevalNegated;
  1622. }
  1623. bool isConstexpr() const {
  1624. return getStatementKind() == IfStatementKind::Constexpr;
  1625. }
  1626. void setStatementKind(IfStatementKind Kind) {
  1627. IfStmtBits.Kind = static_cast<unsigned>(Kind);
  1628. }
  1629. IfStatementKind getStatementKind() const {
  1630. return static_cast<IfStatementKind>(IfStmtBits.Kind);
  1631. }
  1632. /// If this is an 'if constexpr', determine which substatement will be taken.
  1633. /// Otherwise, or if the condition is value-dependent, returns None.
  1634. Optional<const Stmt*> getNondiscardedCase(const ASTContext &Ctx) const;
  1635. Optional<Stmt *> getNondiscardedCase(const ASTContext &Ctx);
  1636. bool isObjCAvailabilityCheck() const;
  1637. SourceLocation getBeginLoc() const { return getIfLoc(); }
  1638. SourceLocation getEndLoc() const LLVM_READONLY {
  1639. if (getElse())
  1640. return getElse()->getEndLoc();
  1641. return getThen()->getEndLoc();
  1642. }
  1643. SourceLocation getLParenLoc() const { return LParenLoc; }
  1644. void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
  1645. SourceLocation getRParenLoc() const { return RParenLoc; }
  1646. void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
  1647. // Iterators over subexpressions. The iterators will include iterating
  1648. // over the initialization expression referenced by the condition variable.
  1649. child_range children() {
  1650. // We always store a condition, but there is none for consteval if
  1651. // statements, so skip it.
  1652. return child_range(getTrailingObjects<Stmt *>() +
  1653. (isConsteval() ? thenOffset() : 0),
  1654. getTrailingObjects<Stmt *>() +
  1655. numTrailingObjects(OverloadToken<Stmt *>()));
  1656. }
  1657. const_child_range children() const {
  1658. // We always store a condition, but there is none for consteval if
  1659. // statements, so skip it.
  1660. return const_child_range(getTrailingObjects<Stmt *>() +
  1661. (isConsteval() ? thenOffset() : 0),
  1662. getTrailingObjects<Stmt *>() +
  1663. numTrailingObjects(OverloadToken<Stmt *>()));
  1664. }
  1665. static bool classof(const Stmt *T) {
  1666. return T->getStmtClass() == IfStmtClass;
  1667. }
  1668. };
  1669. /// SwitchStmt - This represents a 'switch' stmt.
  1670. class SwitchStmt final : public Stmt,
  1671. private llvm::TrailingObjects<SwitchStmt, Stmt *> {
  1672. friend TrailingObjects;
  1673. /// Points to a linked list of case and default statements.
  1674. SwitchCase *FirstCase = nullptr;
  1675. // SwitchStmt is followed by several trailing objects,
  1676. // some of which optional. Note that it would be more convenient to
  1677. // put the optional trailing objects at the end but this would change
  1678. // the order in children().
  1679. // The trailing objects are in order:
  1680. //
  1681. // * A "Stmt *" for the init statement.
  1682. // Present if and only if hasInitStorage().
  1683. //
  1684. // * A "Stmt *" for the condition variable.
  1685. // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
  1686. //
  1687. // * A "Stmt *" for the condition.
  1688. // Always present. This is in fact an "Expr *".
  1689. //
  1690. // * A "Stmt *" for the body.
  1691. // Always present.
  1692. enum { InitOffset = 0, BodyOffsetFromCond = 1 };
  1693. enum { NumMandatoryStmtPtr = 2 };
  1694. SourceLocation LParenLoc;
  1695. SourceLocation RParenLoc;
  1696. unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
  1697. return NumMandatoryStmtPtr + hasInitStorage() + hasVarStorage();
  1698. }
  1699. unsigned initOffset() const { return InitOffset; }
  1700. unsigned varOffset() const { return InitOffset + hasInitStorage(); }
  1701. unsigned condOffset() const {
  1702. return InitOffset + hasInitStorage() + hasVarStorage();
  1703. }
  1704. unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
  1705. /// Build a switch statement.
  1706. SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, Expr *Cond,
  1707. SourceLocation LParenLoc, SourceLocation RParenLoc);
  1708. /// Build a empty switch statement.
  1709. explicit SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar);
  1710. public:
  1711. /// Create a switch statement.
  1712. static SwitchStmt *Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
  1713. Expr *Cond, SourceLocation LParenLoc,
  1714. SourceLocation RParenLoc);
  1715. /// Create an empty switch statement optionally with storage for
  1716. /// an init expression and a condition variable.
  1717. static SwitchStmt *CreateEmpty(const ASTContext &Ctx, bool HasInit,
  1718. bool HasVar);
  1719. /// True if this SwitchStmt has storage for an init statement.
  1720. bool hasInitStorage() const { return SwitchStmtBits.HasInit; }
  1721. /// True if this SwitchStmt has storage for a condition variable.
  1722. bool hasVarStorage() const { return SwitchStmtBits.HasVar; }
  1723. Expr *getCond() {
  1724. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
  1725. }
  1726. const Expr *getCond() const {
  1727. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
  1728. }
  1729. void setCond(Expr *Cond) {
  1730. getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
  1731. }
  1732. Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
  1733. const Stmt *getBody() const {
  1734. return getTrailingObjects<Stmt *>()[bodyOffset()];
  1735. }
  1736. void setBody(Stmt *Body) {
  1737. getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
  1738. }
  1739. Stmt *getInit() {
  1740. return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
  1741. : nullptr;
  1742. }
  1743. const Stmt *getInit() const {
  1744. return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
  1745. : nullptr;
  1746. }
  1747. void setInit(Stmt *Init) {
  1748. assert(hasInitStorage() &&
  1749. "This switch statement has no storage for an init statement!");
  1750. getTrailingObjects<Stmt *>()[initOffset()] = Init;
  1751. }
  1752. /// Retrieve the variable declared in this "switch" statement, if any.
  1753. ///
  1754. /// In the following example, "x" is the condition variable.
  1755. /// \code
  1756. /// switch (int x = foo()) {
  1757. /// case 0: break;
  1758. /// // ...
  1759. /// }
  1760. /// \endcode
  1761. VarDecl *getConditionVariable();
  1762. const VarDecl *getConditionVariable() const {
  1763. return const_cast<SwitchStmt *>(this)->getConditionVariable();
  1764. }
  1765. /// Set the condition variable in this switch statement.
  1766. /// The switch statement must have storage for it.
  1767. void setConditionVariable(const ASTContext &Ctx, VarDecl *VD);
  1768. /// If this SwitchStmt has a condition variable, return the faux DeclStmt
  1769. /// associated with the creation of that condition variable.
  1770. DeclStmt *getConditionVariableDeclStmt() {
  1771. return hasVarStorage() ? static_cast<DeclStmt *>(
  1772. getTrailingObjects<Stmt *>()[varOffset()])
  1773. : nullptr;
  1774. }
  1775. const DeclStmt *getConditionVariableDeclStmt() const {
  1776. return hasVarStorage() ? static_cast<DeclStmt *>(
  1777. getTrailingObjects<Stmt *>()[varOffset()])
  1778. : nullptr;
  1779. }
  1780. SwitchCase *getSwitchCaseList() { return FirstCase; }
  1781. const SwitchCase *getSwitchCaseList() const { return FirstCase; }
  1782. void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
  1783. SourceLocation getSwitchLoc() const { return SwitchStmtBits.SwitchLoc; }
  1784. void setSwitchLoc(SourceLocation L) { SwitchStmtBits.SwitchLoc = L; }
  1785. SourceLocation getLParenLoc() const { return LParenLoc; }
  1786. void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
  1787. SourceLocation getRParenLoc() const { return RParenLoc; }
  1788. void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
  1789. void setBody(Stmt *S, SourceLocation SL) {
  1790. setBody(S);
  1791. setSwitchLoc(SL);
  1792. }
  1793. void addSwitchCase(SwitchCase *SC) {
  1794. assert(!SC->getNextSwitchCase() &&
  1795. "case/default already added to a switch");
  1796. SC->setNextSwitchCase(FirstCase);
  1797. FirstCase = SC;
  1798. }
  1799. /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
  1800. /// switch over an enum value then all cases have been explicitly covered.
  1801. void setAllEnumCasesCovered() { SwitchStmtBits.AllEnumCasesCovered = true; }
  1802. /// Returns true if the SwitchStmt is a switch of an enum value and all cases
  1803. /// have been explicitly covered.
  1804. bool isAllEnumCasesCovered() const {
  1805. return SwitchStmtBits.AllEnumCasesCovered;
  1806. }
  1807. SourceLocation getBeginLoc() const { return getSwitchLoc(); }
  1808. SourceLocation getEndLoc() const LLVM_READONLY {
  1809. return getBody() ? getBody()->getEndLoc()
  1810. : reinterpret_cast<const Stmt *>(getCond())->getEndLoc();
  1811. }
  1812. // Iterators
  1813. child_range children() {
  1814. return child_range(getTrailingObjects<Stmt *>(),
  1815. getTrailingObjects<Stmt *>() +
  1816. numTrailingObjects(OverloadToken<Stmt *>()));
  1817. }
  1818. const_child_range children() const {
  1819. return const_child_range(getTrailingObjects<Stmt *>(),
  1820. getTrailingObjects<Stmt *>() +
  1821. numTrailingObjects(OverloadToken<Stmt *>()));
  1822. }
  1823. static bool classof(const Stmt *T) {
  1824. return T->getStmtClass() == SwitchStmtClass;
  1825. }
  1826. };
  1827. /// WhileStmt - This represents a 'while' stmt.
  1828. class WhileStmt final : public Stmt,
  1829. private llvm::TrailingObjects<WhileStmt, Stmt *> {
  1830. friend TrailingObjects;
  1831. // WhileStmt is followed by several trailing objects,
  1832. // some of which optional. Note that it would be more
  1833. // convenient to put the optional trailing object at the end
  1834. // but this would affect children().
  1835. // The trailing objects are in order:
  1836. //
  1837. // * A "Stmt *" for the condition variable.
  1838. // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
  1839. //
  1840. // * A "Stmt *" for the condition.
  1841. // Always present. This is in fact an "Expr *".
  1842. //
  1843. // * A "Stmt *" for the body.
  1844. // Always present.
  1845. //
  1846. enum { VarOffset = 0, BodyOffsetFromCond = 1 };
  1847. enum { NumMandatoryStmtPtr = 2 };
  1848. SourceLocation LParenLoc, RParenLoc;
  1849. unsigned varOffset() const { return VarOffset; }
  1850. unsigned condOffset() const { return VarOffset + hasVarStorage(); }
  1851. unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
  1852. unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
  1853. return NumMandatoryStmtPtr + hasVarStorage();
  1854. }
  1855. /// Build a while statement.
  1856. WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, Stmt *Body,
  1857. SourceLocation WL, SourceLocation LParenLoc,
  1858. SourceLocation RParenLoc);
  1859. /// Build an empty while statement.
  1860. explicit WhileStmt(EmptyShell Empty, bool HasVar);
  1861. public:
  1862. /// Create a while statement.
  1863. static WhileStmt *Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
  1864. Stmt *Body, SourceLocation WL,
  1865. SourceLocation LParenLoc, SourceLocation RParenLoc);
  1866. /// Create an empty while statement optionally with storage for
  1867. /// a condition variable.
  1868. static WhileStmt *CreateEmpty(const ASTContext &Ctx, bool HasVar);
  1869. /// True if this WhileStmt has storage for a condition variable.
  1870. bool hasVarStorage() const { return WhileStmtBits.HasVar; }
  1871. Expr *getCond() {
  1872. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
  1873. }
  1874. const Expr *getCond() const {
  1875. return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
  1876. }
  1877. void setCond(Expr *Cond) {
  1878. getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
  1879. }
  1880. Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
  1881. const Stmt *getBody() const {
  1882. return getTrailingObjects<Stmt *>()[bodyOffset()];
  1883. }
  1884. void setBody(Stmt *Body) {
  1885. getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
  1886. }
  1887. /// Retrieve the variable declared in this "while" statement, if any.
  1888. ///
  1889. /// In the following example, "x" is the condition variable.
  1890. /// \code
  1891. /// while (int x = random()) {
  1892. /// // ...
  1893. /// }
  1894. /// \endcode
  1895. VarDecl *getConditionVariable();
  1896. const VarDecl *getConditionVariable() const {
  1897. return const_cast<WhileStmt *>(this)->getConditionVariable();
  1898. }
  1899. /// Set the condition variable of this while statement.
  1900. /// The while statement must have storage for it.
  1901. void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
  1902. /// If this WhileStmt has a condition variable, return the faux DeclStmt
  1903. /// associated with the creation of that condition variable.
  1904. DeclStmt *getConditionVariableDeclStmt() {
  1905. return hasVarStorage() ? static_cast<DeclStmt *>(
  1906. getTrailingObjects<Stmt *>()[varOffset()])
  1907. : nullptr;
  1908. }
  1909. const DeclStmt *getConditionVariableDeclStmt() const {
  1910. return hasVarStorage() ? static_cast<DeclStmt *>(
  1911. getTrailingObjects<Stmt *>()[varOffset()])
  1912. : nullptr;
  1913. }
  1914. SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; }
  1915. void setWhileLoc(SourceLocation L) { WhileStmtBits.WhileLoc = L; }
  1916. SourceLocation getLParenLoc() const { return LParenLoc; }
  1917. void setLParenLoc(SourceLocation L) { LParenLoc = L; }
  1918. SourceLocation getRParenLoc() const { return RParenLoc; }
  1919. void setRParenLoc(SourceLocation L) { RParenLoc = L; }
  1920. SourceLocation getBeginLoc() const { return getWhileLoc(); }
  1921. SourceLocation getEndLoc() const LLVM_READONLY {
  1922. return getBody()->getEndLoc();
  1923. }
  1924. static bool classof(const Stmt *T) {
  1925. return T->getStmtClass() == WhileStmtClass;
  1926. }
  1927. // Iterators
  1928. child_range children() {
  1929. return child_range(getTrailingObjects<Stmt *>(),
  1930. getTrailingObjects<Stmt *>() +
  1931. numTrailingObjects(OverloadToken<Stmt *>()));
  1932. }
  1933. const_child_range children() const {
  1934. return const_child_range(getTrailingObjects<Stmt *>(),
  1935. getTrailingObjects<Stmt *>() +
  1936. numTrailingObjects(OverloadToken<Stmt *>()));
  1937. }
  1938. };
  1939. /// DoStmt - This represents a 'do/while' stmt.
  1940. class DoStmt : public Stmt {
  1941. enum { BODY, COND, END_EXPR };
  1942. Stmt *SubExprs[END_EXPR];
  1943. SourceLocation WhileLoc;
  1944. SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
  1945. public:
  1946. DoStmt(Stmt *Body, Expr *Cond, SourceLocation DL, SourceLocation WL,
  1947. SourceLocation RP)
  1948. : Stmt(DoStmtClass), WhileLoc(WL), RParenLoc(RP) {
  1949. setCond(Cond);
  1950. setBody(Body);
  1951. setDoLoc(DL);
  1952. }
  1953. /// Build an empty do-while statement.
  1954. explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
  1955. Expr *getCond() { return reinterpret_cast<Expr *>(SubExprs[COND]); }
  1956. const Expr *getCond() const {
  1957. return reinterpret_cast<Expr *>(SubExprs[COND]);
  1958. }
  1959. void setCond(Expr *Cond) { SubExprs[COND] = reinterpret_cast<Stmt *>(Cond); }
  1960. Stmt *getBody() { return SubExprs[BODY]; }
  1961. const Stmt *getBody() const { return SubExprs[BODY]; }
  1962. void setBody(Stmt *Body) { SubExprs[BODY] = Body; }
  1963. SourceLocation getDoLoc() const { return DoStmtBits.DoLoc; }
  1964. void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L; }
  1965. SourceLocation getWhileLoc() const { return WhileLoc; }
  1966. void setWhileLoc(SourceLocation L) { WhileLoc = L; }
  1967. SourceLocation getRParenLoc() const { return RParenLoc; }
  1968. void setRParenLoc(SourceLocation L) { RParenLoc = L; }
  1969. SourceLocation getBeginLoc() const { return getDoLoc(); }
  1970. SourceLocation getEndLoc() const { return getRParenLoc(); }
  1971. static bool classof(const Stmt *T) {
  1972. return T->getStmtClass() == DoStmtClass;
  1973. }
  1974. // Iterators
  1975. child_range children() {
  1976. return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
  1977. }
  1978. const_child_range children() const {
  1979. return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
  1980. }
  1981. };
  1982. /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
  1983. /// the init/cond/inc parts of the ForStmt will be null if they were not
  1984. /// specified in the source.
  1985. class ForStmt : public Stmt {
  1986. enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
  1987. Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
  1988. SourceLocation LParenLoc, RParenLoc;
  1989. public:
  1990. ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
  1991. Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
  1992. SourceLocation RP);
  1993. /// Build an empty for statement.
  1994. explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
  1995. Stmt *getInit() { return SubExprs[INIT]; }
  1996. /// Retrieve the variable declared in this "for" statement, if any.
  1997. ///
  1998. /// In the following example, "y" is the condition variable.
  1999. /// \code
  2000. /// for (int x = random(); int y = mangle(x); ++x) {
  2001. /// // ...
  2002. /// }
  2003. /// \endcode
  2004. VarDecl *getConditionVariable() const;
  2005. void setConditionVariable(const ASTContext &C, VarDecl *V);
  2006. /// If this ForStmt has a condition variable, return the faux DeclStmt
  2007. /// associated with the creation of that condition variable.
  2008. const DeclStmt *getConditionVariableDeclStmt() const {
  2009. return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
  2010. }
  2011. Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
  2012. Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
  2013. Stmt *getBody() { return SubExprs[BODY]; }
  2014. const Stmt *getInit() const { return SubExprs[INIT]; }
  2015. const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
  2016. const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
  2017. const Stmt *getBody() const { return SubExprs[BODY]; }
  2018. void setInit(Stmt *S) { SubExprs[INIT] = S; }
  2019. void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
  2020. void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
  2021. void setBody(Stmt *S) { SubExprs[BODY] = S; }
  2022. SourceLocation getForLoc() const { return ForStmtBits.ForLoc; }
  2023. void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L; }
  2024. SourceLocation getLParenLoc() const { return LParenLoc; }
  2025. void setLParenLoc(SourceLocation L) { LParenLoc = L; }
  2026. SourceLocation getRParenLoc() const { return RParenLoc; }
  2027. void setRParenLoc(SourceLocation L) { RParenLoc = L; }
  2028. SourceLocation getBeginLoc() const { return getForLoc(); }
  2029. SourceLocation getEndLoc() const { return getBody()->getEndLoc(); }
  2030. static bool classof(const Stmt *T) {
  2031. return T->getStmtClass() == ForStmtClass;
  2032. }
  2033. // Iterators
  2034. child_range children() {
  2035. return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
  2036. }
  2037. const_child_range children() const {
  2038. return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
  2039. }
  2040. };
  2041. /// GotoStmt - This represents a direct goto.
  2042. class GotoStmt : public Stmt {
  2043. LabelDecl *Label;
  2044. SourceLocation LabelLoc;
  2045. public:
  2046. GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
  2047. : Stmt(GotoStmtClass), Label(label), LabelLoc(LL) {
  2048. setGotoLoc(GL);
  2049. }
  2050. /// Build an empty goto statement.
  2051. explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
  2052. LabelDecl *getLabel() const { return Label; }
  2053. void setLabel(LabelDecl *D) { Label = D; }
  2054. SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
  2055. void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
  2056. SourceLocation getLabelLoc() const { return LabelLoc; }
  2057. void setLabelLoc(SourceLocation L) { LabelLoc = L; }
  2058. SourceLocation getBeginLoc() const { return getGotoLoc(); }
  2059. SourceLocation getEndLoc() const { return getLabelLoc(); }
  2060. static bool classof(const Stmt *T) {
  2061. return T->getStmtClass() == GotoStmtClass;
  2062. }
  2063. // Iterators
  2064. child_range children() {
  2065. return child_range(child_iterator(), child_iterator());
  2066. }
  2067. const_child_range children() const {
  2068. return const_child_range(const_child_iterator(), const_child_iterator());
  2069. }
  2070. };
  2071. /// IndirectGotoStmt - This represents an indirect goto.
  2072. class IndirectGotoStmt : public Stmt {
  2073. SourceLocation StarLoc;
  2074. Stmt *Target;
  2075. public:
  2076. IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, Expr *target)
  2077. : Stmt(IndirectGotoStmtClass), StarLoc(starLoc) {
  2078. setTarget(target);
  2079. setGotoLoc(gotoLoc);
  2080. }
  2081. /// Build an empty indirect goto statement.
  2082. explicit IndirectGotoStmt(EmptyShell Empty)
  2083. : Stmt(IndirectGotoStmtClass, Empty) {}
  2084. void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
  2085. SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
  2086. void setStarLoc(SourceLocation L) { StarLoc = L; }
  2087. SourceLocation getStarLoc() const { return StarLoc; }
  2088. Expr *getTarget() { return reinterpret_cast<Expr *>(Target); }
  2089. const Expr *getTarget() const {
  2090. return reinterpret_cast<const Expr *>(Target);
  2091. }
  2092. void setTarget(Expr *E) { Target = reinterpret_cast<Stmt *>(E); }
  2093. /// getConstantTarget - Returns the fixed target of this indirect
  2094. /// goto, if one exists.
  2095. LabelDecl *getConstantTarget();
  2096. const LabelDecl *getConstantTarget() const {
  2097. return const_cast<IndirectGotoStmt *>(this)->getConstantTarget();
  2098. }
  2099. SourceLocation getBeginLoc() const { return getGotoLoc(); }
  2100. SourceLocation getEndLoc() const LLVM_READONLY { return Target->getEndLoc(); }
  2101. static bool classof(const Stmt *T) {
  2102. return T->getStmtClass() == IndirectGotoStmtClass;
  2103. }
  2104. // Iterators
  2105. child_range children() { return child_range(&Target, &Target + 1); }
  2106. const_child_range children() const {
  2107. return const_child_range(&Target, &Target + 1);
  2108. }
  2109. };
  2110. /// ContinueStmt - This represents a continue.
  2111. class ContinueStmt : public Stmt {
  2112. public:
  2113. ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass) {
  2114. setContinueLoc(CL);
  2115. }
  2116. /// Build an empty continue statement.
  2117. explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
  2118. SourceLocation getContinueLoc() const { return ContinueStmtBits.ContinueLoc; }
  2119. void setContinueLoc(SourceLocation L) { ContinueStmtBits.ContinueLoc = L; }
  2120. SourceLocation getBeginLoc() const { return getContinueLoc(); }
  2121. SourceLocation getEndLoc() const { return getContinueLoc(); }
  2122. static bool classof(const Stmt *T) {
  2123. return T->getStmtClass() == ContinueStmtClass;
  2124. }
  2125. // Iterators
  2126. child_range children() {
  2127. return child_range(child_iterator(), child_iterator());
  2128. }
  2129. const_child_range children() const {
  2130. return const_child_range(const_child_iterator(), const_child_iterator());
  2131. }
  2132. };
  2133. /// BreakStmt - This represents a break.
  2134. class BreakStmt : public Stmt {
  2135. public:
  2136. BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass) {
  2137. setBreakLoc(BL);
  2138. }
  2139. /// Build an empty break statement.
  2140. explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
  2141. SourceLocation getBreakLoc() const { return BreakStmtBits.BreakLoc; }
  2142. void setBreakLoc(SourceLocation L) { BreakStmtBits.BreakLoc = L; }
  2143. SourceLocation getBeginLoc() const { return getBreakLoc(); }
  2144. SourceLocation getEndLoc() const { return getBreakLoc(); }
  2145. static bool classof(const Stmt *T) {
  2146. return T->getStmtClass() == BreakStmtClass;
  2147. }
  2148. // Iterators
  2149. child_range children() {
  2150. return child_range(child_iterator(), child_iterator());
  2151. }
  2152. const_child_range children() const {
  2153. return const_child_range(const_child_iterator(), const_child_iterator());
  2154. }
  2155. };
  2156. /// ReturnStmt - This represents a return, optionally of an expression:
  2157. /// return;
  2158. /// return 4;
  2159. ///
  2160. /// Note that GCC allows return with no argument in a function declared to
  2161. /// return a value, and it allows returning a value in functions declared to
  2162. /// return void. We explicitly model this in the AST, which means you can't
  2163. /// depend on the return type of the function and the presence of an argument.
  2164. class ReturnStmt final
  2165. : public Stmt,
  2166. private llvm::TrailingObjects<ReturnStmt, const VarDecl *> {
  2167. friend TrailingObjects;
  2168. /// The return expression.
  2169. Stmt *RetExpr;
  2170. // ReturnStmt is followed optionally by a trailing "const VarDecl *"
  2171. // for the NRVO candidate. Present if and only if hasNRVOCandidate().
  2172. /// True if this ReturnStmt has storage for an NRVO candidate.
  2173. bool hasNRVOCandidate() const { return ReturnStmtBits.HasNRVOCandidate; }
  2174. unsigned numTrailingObjects(OverloadToken<const VarDecl *>) const {
  2175. return hasNRVOCandidate();
  2176. }
  2177. /// Build a return statement.
  2178. ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate);
  2179. /// Build an empty return statement.
  2180. explicit ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate);
  2181. public:
  2182. /// Create a return statement.
  2183. static ReturnStmt *Create(const ASTContext &Ctx, SourceLocation RL, Expr *E,
  2184. const VarDecl *NRVOCandidate);
  2185. /// Create an empty return statement, optionally with
  2186. /// storage for an NRVO candidate.
  2187. static ReturnStmt *CreateEmpty(const ASTContext &Ctx, bool HasNRVOCandidate);
  2188. Expr *getRetValue() { return reinterpret_cast<Expr *>(RetExpr); }
  2189. const Expr *getRetValue() const { return reinterpret_cast<Expr *>(RetExpr); }
  2190. void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt *>(E); }
  2191. /// Retrieve the variable that might be used for the named return
  2192. /// value optimization.
  2193. ///
  2194. /// The optimization itself can only be performed if the variable is
  2195. /// also marked as an NRVO object.
  2196. const VarDecl *getNRVOCandidate() const {
  2197. return hasNRVOCandidate() ? *getTrailingObjects<const VarDecl *>()
  2198. : nullptr;
  2199. }
  2200. /// Set the variable that might be used for the named return value
  2201. /// optimization. The return statement must have storage for it,
  2202. /// which is the case if and only if hasNRVOCandidate() is true.
  2203. void setNRVOCandidate(const VarDecl *Var) {
  2204. assert(hasNRVOCandidate() &&
  2205. "This return statement has no storage for an NRVO candidate!");
  2206. *getTrailingObjects<const VarDecl *>() = Var;
  2207. }
  2208. SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; }
  2209. void setReturnLoc(SourceLocation L) { ReturnStmtBits.RetLoc = L; }
  2210. SourceLocation getBeginLoc() const { return getReturnLoc(); }
  2211. SourceLocation getEndLoc() const LLVM_READONLY {
  2212. return RetExpr ? RetExpr->getEndLoc() : getReturnLoc();
  2213. }
  2214. static bool classof(const Stmt *T) {
  2215. return T->getStmtClass() == ReturnStmtClass;
  2216. }
  2217. // Iterators
  2218. child_range children() {
  2219. if (RetExpr)
  2220. return child_range(&RetExpr, &RetExpr + 1);
  2221. return child_range(child_iterator(), child_iterator());
  2222. }
  2223. const_child_range children() const {
  2224. if (RetExpr)
  2225. return const_child_range(&RetExpr, &RetExpr + 1);
  2226. return const_child_range(const_child_iterator(), const_child_iterator());
  2227. }
  2228. };
  2229. /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
  2230. class AsmStmt : public Stmt {
  2231. protected:
  2232. friend class ASTStmtReader;
  2233. SourceLocation AsmLoc;
  2234. /// True if the assembly statement does not have any input or output
  2235. /// operands.
  2236. bool IsSimple;
  2237. /// If true, treat this inline assembly as having side effects.
  2238. /// This assembly statement should not be optimized, deleted or moved.
  2239. bool IsVolatile;
  2240. unsigned NumOutputs;
  2241. unsigned NumInputs;
  2242. unsigned NumClobbers;
  2243. Stmt **Exprs = nullptr;
  2244. AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
  2245. unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
  2246. : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
  2247. NumOutputs(numoutputs), NumInputs(numinputs),
  2248. NumClobbers(numclobbers) {}
  2249. public:
  2250. /// Build an empty inline-assembly statement.
  2251. explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
  2252. SourceLocation getAsmLoc() const { return AsmLoc; }
  2253. void setAsmLoc(SourceLocation L) { AsmLoc = L; }
  2254. bool isSimple() const { return IsSimple; }
  2255. void setSimple(bool V) { IsSimple = V; }
  2256. bool isVolatile() const { return IsVolatile; }
  2257. void setVolatile(bool V) { IsVolatile = V; }
  2258. SourceLocation getBeginLoc() const LLVM_READONLY { return {}; }
  2259. SourceLocation getEndLoc() const LLVM_READONLY { return {}; }
  2260. //===--- Asm String Analysis ---===//
  2261. /// Assemble final IR asm string.
  2262. std::string generateAsmString(const ASTContext &C) const;
  2263. //===--- Output operands ---===//
  2264. unsigned getNumOutputs() const { return NumOutputs; }
  2265. /// getOutputConstraint - Return the constraint string for the specified
  2266. /// output operand. All output constraints are known to be non-empty (either
  2267. /// '=' or '+').
  2268. StringRef getOutputConstraint(unsigned i) const;
  2269. /// isOutputPlusConstraint - Return true if the specified output constraint
  2270. /// is a "+" constraint (which is both an input and an output) or false if it
  2271. /// is an "=" constraint (just an output).
  2272. bool isOutputPlusConstraint(unsigned i) const {
  2273. return getOutputConstraint(i)[0] == '+';
  2274. }
  2275. const Expr *getOutputExpr(unsigned i) const;
  2276. /// getNumPlusOperands - Return the number of output operands that have a "+"
  2277. /// constraint.
  2278. unsigned getNumPlusOperands() const;
  2279. //===--- Input operands ---===//
  2280. unsigned getNumInputs() const { return NumInputs; }
  2281. /// getInputConstraint - Return the specified input constraint. Unlike output
  2282. /// constraints, these can be empty.
  2283. StringRef getInputConstraint(unsigned i) const;
  2284. const Expr *getInputExpr(unsigned i) const;
  2285. //===--- Other ---===//
  2286. unsigned getNumClobbers() const { return NumClobbers; }
  2287. StringRef getClobber(unsigned i) const;
  2288. static bool classof(const Stmt *T) {
  2289. return T->getStmtClass() == GCCAsmStmtClass ||
  2290. T->getStmtClass() == MSAsmStmtClass;
  2291. }
  2292. // Input expr iterators.
  2293. using inputs_iterator = ExprIterator;
  2294. using const_inputs_iterator = ConstExprIterator;
  2295. using inputs_range = llvm::iterator_range<inputs_iterator>;
  2296. using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
  2297. inputs_iterator begin_inputs() {
  2298. return &Exprs[0] + NumOutputs;
  2299. }
  2300. inputs_iterator end_inputs() {
  2301. return &Exprs[0] + NumOutputs + NumInputs;
  2302. }
  2303. inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
  2304. const_inputs_iterator begin_inputs() const {
  2305. return &Exprs[0] + NumOutputs;
  2306. }
  2307. const_inputs_iterator end_inputs() const {
  2308. return &Exprs[0] + NumOutputs + NumInputs;
  2309. }
  2310. inputs_const_range inputs() const {
  2311. return inputs_const_range(begin_inputs(), end_inputs());
  2312. }
  2313. // Output expr iterators.
  2314. using outputs_iterator = ExprIterator;
  2315. using const_outputs_iterator = ConstExprIterator;
  2316. using outputs_range = llvm::iterator_range<outputs_iterator>;
  2317. using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
  2318. outputs_iterator begin_outputs() {
  2319. return &Exprs[0];
  2320. }
  2321. outputs_iterator end_outputs() {
  2322. return &Exprs[0] + NumOutputs;
  2323. }
  2324. outputs_range outputs() {
  2325. return outputs_range(begin_outputs(), end_outputs());
  2326. }
  2327. const_outputs_iterator begin_outputs() const {
  2328. return &Exprs[0];
  2329. }
  2330. const_outputs_iterator end_outputs() const {
  2331. return &Exprs[0] + NumOutputs;
  2332. }
  2333. outputs_const_range outputs() const {
  2334. return outputs_const_range(begin_outputs(), end_outputs());
  2335. }
  2336. child_range children() {
  2337. return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
  2338. }
  2339. const_child_range children() const {
  2340. return const_child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
  2341. }
  2342. };
  2343. /// This represents a GCC inline-assembly statement extension.
  2344. class GCCAsmStmt : public AsmStmt {
  2345. friend class ASTStmtReader;
  2346. SourceLocation RParenLoc;
  2347. StringLiteral *AsmStr;
  2348. // FIXME: If we wanted to, we could allocate all of these in one big array.
  2349. StringLiteral **Constraints = nullptr;
  2350. StringLiteral **Clobbers = nullptr;
  2351. IdentifierInfo **Names = nullptr;
  2352. unsigned NumLabels = 0;
  2353. public:
  2354. GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
  2355. bool isvolatile, unsigned numoutputs, unsigned numinputs,
  2356. IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
  2357. StringLiteral *asmstr, unsigned numclobbers,
  2358. StringLiteral **clobbers, unsigned numlabels,
  2359. SourceLocation rparenloc);
  2360. /// Build an empty inline-assembly statement.
  2361. explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
  2362. SourceLocation getRParenLoc() const { return RParenLoc; }
  2363. void setRParenLoc(SourceLocation L) { RParenLoc = L; }
  2364. //===--- Asm String Analysis ---===//
  2365. const StringLiteral *getAsmString() const { return AsmStr; }
  2366. StringLiteral *getAsmString() { return AsmStr; }
  2367. void setAsmString(StringLiteral *E) { AsmStr = E; }
  2368. /// AsmStringPiece - this is part of a decomposed asm string specification
  2369. /// (for use with the AnalyzeAsmString function below). An asm string is
  2370. /// considered to be a concatenation of these parts.
  2371. class AsmStringPiece {
  2372. public:
  2373. enum Kind {
  2374. String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
  2375. Operand // Operand reference, with optional modifier %c4.
  2376. };
  2377. private:
  2378. Kind MyKind;
  2379. std::string Str;
  2380. unsigned OperandNo;
  2381. // Source range for operand references.
  2382. CharSourceRange Range;
  2383. public:
  2384. AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
  2385. AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
  2386. SourceLocation End)
  2387. : MyKind(Operand), Str(S), OperandNo(OpNo),
  2388. Range(CharSourceRange::getCharRange(Begin, End)) {}
  2389. bool isString() const { return MyKind == String; }
  2390. bool isOperand() const { return MyKind == Operand; }
  2391. const std::string &getString() const { return Str; }
  2392. unsigned getOperandNo() const {
  2393. assert(isOperand());
  2394. return OperandNo;
  2395. }
  2396. CharSourceRange getRange() const {
  2397. assert(isOperand() && "Range is currently used only for Operands.");
  2398. return Range;
  2399. }
  2400. /// getModifier - Get the modifier for this operand, if present. This
  2401. /// returns '\0' if there was no modifier.
  2402. char getModifier() const;
  2403. };
  2404. /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
  2405. /// it into pieces. If the asm string is erroneous, emit errors and return
  2406. /// true, otherwise return false. This handles canonicalization and
  2407. /// translation of strings from GCC syntax to LLVM IR syntax, and handles
  2408. //// flattening of named references like %[foo] to Operand AsmStringPiece's.
  2409. unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
  2410. const ASTContext &C, unsigned &DiagOffs) const;
  2411. /// Assemble final IR asm string.
  2412. std::string generateAsmString(const ASTContext &C) const;
  2413. //===--- Output operands ---===//
  2414. IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
  2415. StringRef getOutputName(unsigned i) const {
  2416. if (IdentifierInfo *II = getOutputIdentifier(i))
  2417. return II->getName();
  2418. return {};
  2419. }
  2420. StringRef getOutputConstraint(unsigned i) const;
  2421. const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
  2422. return Constraints[i];
  2423. }
  2424. StringLiteral *getOutputConstraintLiteral(unsigned i) {
  2425. return Constraints[i];
  2426. }
  2427. Expr *getOutputExpr(unsigned i);
  2428. const Expr *getOutputExpr(unsigned i) const {
  2429. return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
  2430. }
  2431. //===--- Input operands ---===//
  2432. IdentifierInfo *getInputIdentifier(unsigned i) const {
  2433. return Names[i + NumOutputs];
  2434. }
  2435. StringRef getInputName(unsigned i) const {
  2436. if (IdentifierInfo *II = getInputIdentifier(i))
  2437. return II->getName();
  2438. return {};
  2439. }
  2440. StringRef getInputConstraint(unsigned i) const;
  2441. const StringLiteral *getInputConstraintLiteral(unsigned i) const {
  2442. return Constraints[i + NumOutputs];
  2443. }
  2444. StringLiteral *getInputConstraintLiteral(unsigned i) {
  2445. return Constraints[i + NumOutputs];
  2446. }
  2447. Expr *getInputExpr(unsigned i);
  2448. void setInputExpr(unsigned i, Expr *E);
  2449. const Expr *getInputExpr(unsigned i) const {
  2450. return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
  2451. }
  2452. //===--- Labels ---===//
  2453. bool isAsmGoto() const {
  2454. return NumLabels > 0;
  2455. }
  2456. unsigned getNumLabels() const {
  2457. return NumLabels;
  2458. }
  2459. IdentifierInfo *getLabelIdentifier(unsigned i) const {
  2460. return Names[i + NumOutputs + NumInputs];
  2461. }
  2462. AddrLabelExpr *getLabelExpr(unsigned i) const;
  2463. StringRef getLabelName(unsigned i) const;
  2464. using labels_iterator = CastIterator<AddrLabelExpr>;
  2465. using const_labels_iterator = ConstCastIterator<AddrLabelExpr>;
  2466. using labels_range = llvm::iterator_range<labels_iterator>;
  2467. using labels_const_range = llvm::iterator_range<const_labels_iterator>;
  2468. labels_iterator begin_labels() {
  2469. return &Exprs[0] + NumOutputs + NumInputs;
  2470. }
  2471. labels_iterator end_labels() {
  2472. return &Exprs[0] + NumOutputs + NumInputs + NumLabels;
  2473. }
  2474. labels_range labels() {
  2475. return labels_range(begin_labels(), end_labels());
  2476. }
  2477. const_labels_iterator begin_labels() const {
  2478. return &Exprs[0] + NumOutputs + NumInputs;
  2479. }
  2480. const_labels_iterator end_labels() const {
  2481. return &Exprs[0] + NumOutputs + NumInputs + NumLabels;
  2482. }
  2483. labels_const_range labels() const {
  2484. return labels_const_range(begin_labels(), end_labels());
  2485. }
  2486. private:
  2487. void setOutputsAndInputsAndClobbers(const ASTContext &C,
  2488. IdentifierInfo **Names,
  2489. StringLiteral **Constraints,
  2490. Stmt **Exprs,
  2491. unsigned NumOutputs,
  2492. unsigned NumInputs,
  2493. unsigned NumLabels,
  2494. StringLiteral **Clobbers,
  2495. unsigned NumClobbers);
  2496. public:
  2497. //===--- Other ---===//
  2498. /// getNamedOperand - Given a symbolic operand reference like %[foo],
  2499. /// translate this into a numeric value needed to reference the same operand.
  2500. /// This returns -1 if the operand name is invalid.
  2501. int getNamedOperand(StringRef SymbolicName) const;
  2502. StringRef getClobber(unsigned i) const;
  2503. StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
  2504. const StringLiteral *getClobberStringLiteral(unsigned i) const {
  2505. return Clobbers[i];
  2506. }
  2507. SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
  2508. SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
  2509. static bool classof(const Stmt *T) {
  2510. return T->getStmtClass() == GCCAsmStmtClass;
  2511. }
  2512. };
  2513. /// This represents a Microsoft inline-assembly statement extension.
  2514. class MSAsmStmt : public AsmStmt {
  2515. friend class ASTStmtReader;
  2516. SourceLocation LBraceLoc, EndLoc;
  2517. StringRef AsmStr;
  2518. unsigned NumAsmToks = 0;
  2519. Token *AsmToks = nullptr;
  2520. StringRef *Constraints = nullptr;
  2521. StringRef *Clobbers = nullptr;
  2522. public:
  2523. MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
  2524. SourceLocation lbraceloc, bool issimple, bool isvolatile,
  2525. ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
  2526. ArrayRef<StringRef> constraints,
  2527. ArrayRef<Expr*> exprs, StringRef asmstr,
  2528. ArrayRef<StringRef> clobbers, SourceLocation endloc);
  2529. /// Build an empty MS-style inline-assembly statement.
  2530. explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
  2531. SourceLocation getLBraceLoc() const { return LBraceLoc; }
  2532. void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
  2533. SourceLocation getEndLoc() const { return EndLoc; }
  2534. void setEndLoc(SourceLocation L) { EndLoc = L; }
  2535. bool hasBraces() const { return LBraceLoc.isValid(); }
  2536. unsigned getNumAsmToks() { return NumAsmToks; }
  2537. Token *getAsmToks() { return AsmToks; }
  2538. //===--- Asm String Analysis ---===//
  2539. StringRef getAsmString() const { return AsmStr; }
  2540. /// Assemble final IR asm string.
  2541. std::string generateAsmString(const ASTContext &C) const;
  2542. //===--- Output operands ---===//
  2543. StringRef getOutputConstraint(unsigned i) const {
  2544. assert(i < NumOutputs);
  2545. return Constraints[i];
  2546. }
  2547. Expr *getOutputExpr(unsigned i);
  2548. const Expr *getOutputExpr(unsigned i) const {
  2549. return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
  2550. }
  2551. //===--- Input operands ---===//
  2552. StringRef getInputConstraint(unsigned i) const {
  2553. assert(i < NumInputs);
  2554. return Constraints[i + NumOutputs];
  2555. }
  2556. Expr *getInputExpr(unsigned i);
  2557. void setInputExpr(unsigned i, Expr *E);
  2558. const Expr *getInputExpr(unsigned i) const {
  2559. return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
  2560. }
  2561. //===--- Other ---===//
  2562. ArrayRef<StringRef> getAllConstraints() const {
  2563. return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
  2564. }
  2565. ArrayRef<StringRef> getClobbers() const {
  2566. return llvm::makeArrayRef(Clobbers, NumClobbers);
  2567. }
  2568. ArrayRef<Expr*> getAllExprs() const {
  2569. return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
  2570. NumInputs + NumOutputs);
  2571. }
  2572. StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
  2573. private:
  2574. void initialize(const ASTContext &C, StringRef AsmString,
  2575. ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
  2576. ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
  2577. public:
  2578. SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
  2579. static bool classof(const Stmt *T) {
  2580. return T->getStmtClass() == MSAsmStmtClass;
  2581. }
  2582. child_range children() {
  2583. return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
  2584. }
  2585. const_child_range children() const {
  2586. return const_child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
  2587. }
  2588. };
  2589. class SEHExceptStmt : public Stmt {
  2590. friend class ASTReader;
  2591. friend class ASTStmtReader;
  2592. SourceLocation Loc;
  2593. Stmt *Children[2];
  2594. enum { FILTER_EXPR, BLOCK };
  2595. SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
  2596. explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
  2597. public:
  2598. static SEHExceptStmt* Create(const ASTContext &C,
  2599. SourceLocation ExceptLoc,
  2600. Expr *FilterExpr,
  2601. Stmt *Block);
  2602. SourceLocation getBeginLoc() const LLVM_READONLY { return getExceptLoc(); }
  2603. SourceLocation getExceptLoc() const { return Loc; }
  2604. SourceLocation getEndLoc() const { return getBlock()->getEndLoc(); }
  2605. Expr *getFilterExpr() const {
  2606. return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
  2607. }
  2608. CompoundStmt *getBlock() const {
  2609. return cast<CompoundStmt>(Children[BLOCK]);
  2610. }
  2611. child_range children() {
  2612. return child_range(Children, Children+2);
  2613. }
  2614. const_child_range children() const {
  2615. return const_child_range(Children, Children + 2);
  2616. }
  2617. static bool classof(const Stmt *T) {
  2618. return T->getStmtClass() == SEHExceptStmtClass;
  2619. }
  2620. };
  2621. class SEHFinallyStmt : public Stmt {
  2622. friend class ASTReader;
  2623. friend class ASTStmtReader;
  2624. SourceLocation Loc;
  2625. Stmt *Block;
  2626. SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
  2627. explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
  2628. public:
  2629. static SEHFinallyStmt* Create(const ASTContext &C,
  2630. SourceLocation FinallyLoc,
  2631. Stmt *Block);
  2632. SourceLocation getBeginLoc() const LLVM_READONLY { return getFinallyLoc(); }
  2633. SourceLocation getFinallyLoc() const { return Loc; }
  2634. SourceLocation getEndLoc() const { return Block->getEndLoc(); }
  2635. CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
  2636. child_range children() {
  2637. return child_range(&Block,&Block+1);
  2638. }
  2639. const_child_range children() const {
  2640. return const_child_range(&Block, &Block + 1);
  2641. }
  2642. static bool classof(const Stmt *T) {
  2643. return T->getStmtClass() == SEHFinallyStmtClass;
  2644. }
  2645. };
  2646. class SEHTryStmt : public Stmt {
  2647. friend class ASTReader;
  2648. friend class ASTStmtReader;
  2649. bool IsCXXTry;
  2650. SourceLocation TryLoc;
  2651. Stmt *Children[2];
  2652. enum { TRY = 0, HANDLER = 1 };
  2653. SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
  2654. SourceLocation TryLoc,
  2655. Stmt *TryBlock,
  2656. Stmt *Handler);
  2657. explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
  2658. public:
  2659. static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
  2660. SourceLocation TryLoc, Stmt *TryBlock,
  2661. Stmt *Handler);
  2662. SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
  2663. SourceLocation getTryLoc() const { return TryLoc; }
  2664. SourceLocation getEndLoc() const { return Children[HANDLER]->getEndLoc(); }
  2665. bool getIsCXXTry() const { return IsCXXTry; }
  2666. CompoundStmt* getTryBlock() const {
  2667. return cast<CompoundStmt>(Children[TRY]);
  2668. }
  2669. Stmt *getHandler() const { return Children[HANDLER]; }
  2670. /// Returns 0 if not defined
  2671. SEHExceptStmt *getExceptHandler() const;
  2672. SEHFinallyStmt *getFinallyHandler() const;
  2673. child_range children() {
  2674. return child_range(Children, Children+2);
  2675. }
  2676. const_child_range children() const {
  2677. return const_child_range(Children, Children + 2);
  2678. }
  2679. static bool classof(const Stmt *T) {
  2680. return T->getStmtClass() == SEHTryStmtClass;
  2681. }
  2682. };
  2683. /// Represents a __leave statement.
  2684. class SEHLeaveStmt : public Stmt {
  2685. SourceLocation LeaveLoc;
  2686. public:
  2687. explicit SEHLeaveStmt(SourceLocation LL)
  2688. : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
  2689. /// Build an empty __leave statement.
  2690. explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
  2691. SourceLocation getLeaveLoc() const { return LeaveLoc; }
  2692. void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
  2693. SourceLocation getBeginLoc() const LLVM_READONLY { return LeaveLoc; }
  2694. SourceLocation getEndLoc() const LLVM_READONLY { return LeaveLoc; }
  2695. static bool classof(const Stmt *T) {
  2696. return T->getStmtClass() == SEHLeaveStmtClass;
  2697. }
  2698. // Iterators
  2699. child_range children() {
  2700. return child_range(child_iterator(), child_iterator());
  2701. }
  2702. const_child_range children() const {
  2703. return const_child_range(const_child_iterator(), const_child_iterator());
  2704. }
  2705. };
  2706. /// This captures a statement into a function. For example, the following
  2707. /// pragma annotated compound statement can be represented as a CapturedStmt,
  2708. /// and this compound statement is the body of an anonymous outlined function.
  2709. /// @code
  2710. /// #pragma omp parallel
  2711. /// {
  2712. /// compute();
  2713. /// }
  2714. /// @endcode
  2715. class CapturedStmt : public Stmt {
  2716. public:
  2717. /// The different capture forms: by 'this', by reference, capture for
  2718. /// variable-length array type etc.
  2719. enum VariableCaptureKind {
  2720. VCK_This,
  2721. VCK_ByRef,
  2722. VCK_ByCopy,
  2723. VCK_VLAType,
  2724. };
  2725. /// Describes the capture of either a variable, or 'this', or
  2726. /// variable-length array type.
  2727. class Capture {
  2728. llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
  2729. SourceLocation Loc;
  2730. public:
  2731. friend class ASTStmtReader;
  2732. /// Create a new capture.
  2733. ///
  2734. /// \param Loc The source location associated with this capture.
  2735. ///
  2736. /// \param Kind The kind of capture (this, ByRef, ...).
  2737. ///
  2738. /// \param Var The variable being captured, or null if capturing this.
  2739. Capture(SourceLocation Loc, VariableCaptureKind Kind,
  2740. VarDecl *Var = nullptr);
  2741. /// Determine the kind of capture.
  2742. VariableCaptureKind getCaptureKind() const;
  2743. /// Retrieve the source location at which the variable or 'this' was
  2744. /// first used.
  2745. SourceLocation getLocation() const { return Loc; }
  2746. /// Determine whether this capture handles the C++ 'this' pointer.
  2747. bool capturesThis() const { return getCaptureKind() == VCK_This; }
  2748. /// Determine whether this capture handles a variable (by reference).
  2749. bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
  2750. /// Determine whether this capture handles a variable by copy.
  2751. bool capturesVariableByCopy() const {
  2752. return getCaptureKind() == VCK_ByCopy;
  2753. }
  2754. /// Determine whether this capture handles a variable-length array
  2755. /// type.
  2756. bool capturesVariableArrayType() const {
  2757. return getCaptureKind() == VCK_VLAType;
  2758. }
  2759. /// Retrieve the declaration of the variable being captured.
  2760. ///
  2761. /// This operation is only valid if this capture captures a variable.
  2762. VarDecl *getCapturedVar() const;
  2763. };
  2764. private:
  2765. /// The number of variable captured, including 'this'.
  2766. unsigned NumCaptures;
  2767. /// The pointer part is the implicit the outlined function and the
  2768. /// int part is the captured region kind, 'CR_Default' etc.
  2769. llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
  2770. /// The record for captured variables, a RecordDecl or CXXRecordDecl.
  2771. RecordDecl *TheRecordDecl = nullptr;
  2772. /// Construct a captured statement.
  2773. CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
  2774. ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
  2775. /// Construct an empty captured statement.
  2776. CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
  2777. Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
  2778. Stmt *const *getStoredStmts() const {
  2779. return reinterpret_cast<Stmt *const *>(this + 1);
  2780. }
  2781. Capture *getStoredCaptures() const;
  2782. void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
  2783. public:
  2784. friend class ASTStmtReader;
  2785. static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
  2786. CapturedRegionKind Kind,
  2787. ArrayRef<Capture> Captures,
  2788. ArrayRef<Expr *> CaptureInits,
  2789. CapturedDecl *CD, RecordDecl *RD);
  2790. static CapturedStmt *CreateDeserialized(const ASTContext &Context,
  2791. unsigned NumCaptures);
  2792. /// Retrieve the statement being captured.
  2793. Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
  2794. const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
  2795. /// Retrieve the outlined function declaration.
  2796. CapturedDecl *getCapturedDecl();
  2797. const CapturedDecl *getCapturedDecl() const;
  2798. /// Set the outlined function declaration.
  2799. void setCapturedDecl(CapturedDecl *D);
  2800. /// Retrieve the captured region kind.
  2801. CapturedRegionKind getCapturedRegionKind() const;
  2802. /// Set the captured region kind.
  2803. void setCapturedRegionKind(CapturedRegionKind Kind);
  2804. /// Retrieve the record declaration for captured variables.
  2805. const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
  2806. /// Set the record declaration for captured variables.
  2807. void setCapturedRecordDecl(RecordDecl *D) {
  2808. assert(D && "null RecordDecl");
  2809. TheRecordDecl = D;
  2810. }
  2811. /// True if this variable has been captured.
  2812. bool capturesVariable(const VarDecl *Var) const;
  2813. /// An iterator that walks over the captures.
  2814. using capture_iterator = Capture *;
  2815. using const_capture_iterator = const Capture *;
  2816. using capture_range = llvm::iterator_range<capture_iterator>;
  2817. using capture_const_range = llvm::iterator_range<const_capture_iterator>;
  2818. capture_range captures() {
  2819. return capture_range(capture_begin(), capture_end());
  2820. }
  2821. capture_const_range captures() const {
  2822. return capture_const_range(capture_begin(), capture_end());
  2823. }
  2824. /// Retrieve an iterator pointing to the first capture.
  2825. capture_iterator capture_begin() { return getStoredCaptures(); }
  2826. const_capture_iterator capture_begin() const { return getStoredCaptures(); }
  2827. /// Retrieve an iterator pointing past the end of the sequence of
  2828. /// captures.
  2829. capture_iterator capture_end() const {
  2830. return getStoredCaptures() + NumCaptures;
  2831. }
  2832. /// Retrieve the number of captures, including 'this'.
  2833. unsigned capture_size() const { return NumCaptures; }
  2834. /// Iterator that walks over the capture initialization arguments.
  2835. using capture_init_iterator = Expr **;
  2836. using capture_init_range = llvm::iterator_range<capture_init_iterator>;
  2837. /// Const iterator that walks over the capture initialization
  2838. /// arguments.
  2839. using const_capture_init_iterator = Expr *const *;
  2840. using const_capture_init_range =
  2841. llvm::iterator_range<const_capture_init_iterator>;
  2842. capture_init_range capture_inits() {
  2843. return capture_init_range(capture_init_begin(), capture_init_end());
  2844. }
  2845. const_capture_init_range capture_inits() const {
  2846. return const_capture_init_range(capture_init_begin(), capture_init_end());
  2847. }
  2848. /// Retrieve the first initialization argument.
  2849. capture_init_iterator capture_init_begin() {
  2850. return reinterpret_cast<Expr **>(getStoredStmts());
  2851. }
  2852. const_capture_init_iterator capture_init_begin() const {
  2853. return reinterpret_cast<Expr *const *>(getStoredStmts());
  2854. }
  2855. /// Retrieve the iterator pointing one past the last initialization
  2856. /// argument.
  2857. capture_init_iterator capture_init_end() {
  2858. return capture_init_begin() + NumCaptures;
  2859. }
  2860. const_capture_init_iterator capture_init_end() const {
  2861. return capture_init_begin() + NumCaptures;
  2862. }
  2863. SourceLocation getBeginLoc() const LLVM_READONLY {
  2864. return getCapturedStmt()->getBeginLoc();
  2865. }
  2866. SourceLocation getEndLoc() const LLVM_READONLY {
  2867. return getCapturedStmt()->getEndLoc();
  2868. }
  2869. SourceRange getSourceRange() const LLVM_READONLY {
  2870. return getCapturedStmt()->getSourceRange();
  2871. }
  2872. static bool classof(const Stmt *T) {
  2873. return T->getStmtClass() == CapturedStmtClass;
  2874. }
  2875. child_range children();
  2876. const_child_range children() const;
  2877. };
  2878. } // namespace clang
  2879. #endif // LLVM_CLANG_AST_STMT_H
  2880. #ifdef __GNUC__
  2881. #pragma GCC diagnostic pop
  2882. #endif