Stmt.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  1. //===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Stmt class and statement subclasses.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/ASTDiagnostic.h"
  15. #include "clang/AST/ExprCXX.h"
  16. #include "clang/AST/ExprObjC.h"
  17. #include "clang/AST/Stmt.h"
  18. #include "clang/AST/StmtCXX.h"
  19. #include "clang/AST/StmtObjC.h"
  20. #include "clang/AST/StmtOpenMP.h"
  21. #include "clang/AST/Type.h"
  22. #include "clang/Basic/CharInfo.h"
  23. #include "clang/Basic/TargetInfo.h"
  24. #include "clang/Lex/Token.h"
  25. #include "llvm/ADT/StringExtras.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace clang;
  28. static struct StmtClassNameTable {
  29. const char *Name;
  30. unsigned Counter;
  31. unsigned Size;
  32. } StmtClassInfo[Stmt::lastStmtConstant+1];
  33. static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
  34. static bool Initialized = false;
  35. if (Initialized)
  36. return StmtClassInfo[E];
  37. // Intialize the table on the first use.
  38. Initialized = true;
  39. #define ABSTRACT_STMT(STMT)
  40. #define STMT(CLASS, PARENT) \
  41. StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
  42. StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
  43. #include "clang/AST/StmtNodes.inc"
  44. return StmtClassInfo[E];
  45. }
  46. void *Stmt::operator new(size_t bytes, const ASTContext& C,
  47. unsigned alignment) {
  48. return ::operator new(bytes, C, alignment);
  49. }
  50. const char *Stmt::getStmtClassName() const {
  51. return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
  52. }
  53. void Stmt::PrintStats() {
  54. // Ensure the table is primed.
  55. getStmtInfoTableEntry(Stmt::NullStmtClass);
  56. unsigned sum = 0;
  57. llvm::errs() << "\n*** Stmt/Expr Stats:\n";
  58. for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
  59. if (StmtClassInfo[i].Name == nullptr) continue;
  60. sum += StmtClassInfo[i].Counter;
  61. }
  62. llvm::errs() << " " << sum << " stmts/exprs total.\n";
  63. sum = 0;
  64. for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
  65. if (StmtClassInfo[i].Name == nullptr) continue;
  66. if (StmtClassInfo[i].Counter == 0) continue;
  67. llvm::errs() << " " << StmtClassInfo[i].Counter << " "
  68. << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
  69. << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
  70. << " bytes)\n";
  71. sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
  72. }
  73. llvm::errs() << "Total bytes = " << sum << "\n";
  74. }
  75. void Stmt::addStmtClass(StmtClass s) {
  76. ++getStmtInfoTableEntry(s).Counter;
  77. }
  78. bool Stmt::StatisticsEnabled = false;
  79. void Stmt::EnableStatistics() {
  80. StatisticsEnabled = true;
  81. }
  82. Stmt *Stmt::IgnoreImplicit() {
  83. Stmt *s = this;
  84. if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
  85. s = ewc->getSubExpr();
  86. if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
  87. s = mte->GetTemporaryExpr();
  88. if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
  89. s = bte->getSubExpr();
  90. while (auto *ice = dyn_cast<ImplicitCastExpr>(s))
  91. s = ice->getSubExpr();
  92. return s;
  93. }
  94. /// \brief Skip no-op (attributed, compound) container stmts and skip captured
  95. /// stmt at the top, if \a IgnoreCaptured is true.
  96. Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
  97. Stmt *S = this;
  98. if (IgnoreCaptured)
  99. if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
  100. S = CapS->getCapturedStmt();
  101. while (true) {
  102. if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
  103. S = AS->getSubStmt();
  104. else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
  105. if (CS->size() != 1)
  106. break;
  107. S = CS->body_back();
  108. } else
  109. break;
  110. }
  111. return S;
  112. }
  113. /// \brief Strip off all label-like statements.
  114. ///
  115. /// This will strip off label statements, case statements, attributed
  116. /// statements and default statements recursively.
  117. const Stmt *Stmt::stripLabelLikeStatements() const {
  118. const Stmt *S = this;
  119. while (true) {
  120. if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
  121. S = LS->getSubStmt();
  122. else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
  123. S = SC->getSubStmt();
  124. else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
  125. S = AS->getSubStmt();
  126. else
  127. return S;
  128. }
  129. }
  130. namespace {
  131. struct good {};
  132. struct bad {};
  133. // These silly little functions have to be static inline to suppress
  134. // unused warnings, and they have to be defined to suppress other
  135. // warnings.
  136. static inline good is_good(good) { return good(); }
  137. typedef Stmt::child_range children_t();
  138. template <class T> good implements_children(children_t T::*) {
  139. return good();
  140. }
  141. LLVM_ATTRIBUTE_UNUSED
  142. static inline bad implements_children(children_t Stmt::*) {
  143. return bad();
  144. }
  145. typedef SourceLocation getLocStart_t() const;
  146. template <class T> good implements_getLocStart(getLocStart_t T::*) {
  147. return good();
  148. }
  149. LLVM_ATTRIBUTE_UNUSED
  150. static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
  151. return bad();
  152. }
  153. typedef SourceLocation getLocEnd_t() const;
  154. template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
  155. return good();
  156. }
  157. LLVM_ATTRIBUTE_UNUSED
  158. static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
  159. return bad();
  160. }
  161. #define ASSERT_IMPLEMENTS_children(type) \
  162. (void) is_good(implements_children(&type::children))
  163. #define ASSERT_IMPLEMENTS_getLocStart(type) \
  164. (void) is_good(implements_getLocStart(&type::getLocStart))
  165. #define ASSERT_IMPLEMENTS_getLocEnd(type) \
  166. (void) is_good(implements_getLocEnd(&type::getLocEnd))
  167. }
  168. /// Check whether the various Stmt classes implement their member
  169. /// functions.
  170. LLVM_ATTRIBUTE_UNUSED
  171. static inline void check_implementations() {
  172. #define ABSTRACT_STMT(type)
  173. #define STMT(type, base) \
  174. ASSERT_IMPLEMENTS_children(type); \
  175. ASSERT_IMPLEMENTS_getLocStart(type); \
  176. ASSERT_IMPLEMENTS_getLocEnd(type);
  177. #include "clang/AST/StmtNodes.inc"
  178. }
  179. Stmt::child_range Stmt::children() {
  180. switch (getStmtClass()) {
  181. case Stmt::NoStmtClass: llvm_unreachable("statement without class");
  182. #define ABSTRACT_STMT(type)
  183. #define STMT(type, base) \
  184. case Stmt::type##Class: \
  185. return static_cast<type*>(this)->children();
  186. #include "clang/AST/StmtNodes.inc"
  187. }
  188. llvm_unreachable("unknown statement kind!");
  189. }
  190. // Amusing macro metaprogramming hack: check whether a class provides
  191. // a more specific implementation of getSourceRange.
  192. //
  193. // See also Expr.cpp:getExprLoc().
  194. namespace {
  195. /// This implementation is used when a class provides a custom
  196. /// implementation of getSourceRange.
  197. template <class S, class T>
  198. SourceRange getSourceRangeImpl(const Stmt *stmt,
  199. SourceRange (T::*v)() const) {
  200. return static_cast<const S*>(stmt)->getSourceRange();
  201. }
  202. /// This implementation is used when a class doesn't provide a custom
  203. /// implementation of getSourceRange. Overload resolution should pick it over
  204. /// the implementation above because it's more specialized according to
  205. /// function template partial ordering.
  206. template <class S>
  207. SourceRange getSourceRangeImpl(const Stmt *stmt,
  208. SourceRange (Stmt::*v)() const) {
  209. return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
  210. static_cast<const S*>(stmt)->getLocEnd());
  211. }
  212. }
  213. SourceRange Stmt::getSourceRange() const {
  214. switch (getStmtClass()) {
  215. case Stmt::NoStmtClass: llvm_unreachable("statement without class");
  216. #define ABSTRACT_STMT(type)
  217. #define STMT(type, base) \
  218. case Stmt::type##Class: \
  219. return getSourceRangeImpl<type>(this, &type::getSourceRange);
  220. #include "clang/AST/StmtNodes.inc"
  221. }
  222. llvm_unreachable("unknown statement kind!");
  223. }
  224. SourceLocation Stmt::getLocStart() const {
  225. // llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
  226. switch (getStmtClass()) {
  227. case Stmt::NoStmtClass: llvm_unreachable("statement without class");
  228. #define ABSTRACT_STMT(type)
  229. #define STMT(type, base) \
  230. case Stmt::type##Class: \
  231. return static_cast<const type*>(this)->getLocStart();
  232. #include "clang/AST/StmtNodes.inc"
  233. }
  234. llvm_unreachable("unknown statement kind");
  235. }
  236. SourceLocation Stmt::getLocEnd() const {
  237. switch (getStmtClass()) {
  238. case Stmt::NoStmtClass: llvm_unreachable("statement without class");
  239. #define ABSTRACT_STMT(type)
  240. #define STMT(type, base) \
  241. case Stmt::type##Class: \
  242. return static_cast<const type*>(this)->getLocEnd();
  243. #include "clang/AST/StmtNodes.inc"
  244. }
  245. llvm_unreachable("unknown statement kind");
  246. }
  247. CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
  248. SourceLocation LB, SourceLocation RB)
  249. : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
  250. CompoundStmtBits.NumStmts = Stmts.size();
  251. assert(CompoundStmtBits.NumStmts == Stmts.size() &&
  252. "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
  253. if (Stmts.size() == 0) {
  254. Body = nullptr;
  255. return;
  256. }
  257. Body = new (C) Stmt*[Stmts.size()];
  258. std::copy(Stmts.begin(), Stmts.end(), Body);
  259. }
  260. void CompoundStmt::setStmts(const ASTContext &C, Stmt **Stmts,
  261. unsigned NumStmts) {
  262. if (this->Body)
  263. C.Deallocate(Body);
  264. this->CompoundStmtBits.NumStmts = NumStmts;
  265. Body = new (C) Stmt*[NumStmts];
  266. memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
  267. }
  268. const char *LabelStmt::getName() const {
  269. return getDecl()->getIdentifier()->getNameStart();
  270. }
  271. AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
  272. ArrayRef<const Attr*> Attrs,
  273. Stmt *SubStmt) {
  274. assert(!Attrs.empty() && "Attrs should not be empty");
  275. void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
  276. llvm::alignOf<AttributedStmt>());
  277. return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
  278. }
  279. AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
  280. unsigned NumAttrs) {
  281. assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
  282. void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
  283. llvm::alignOf<AttributedStmt>());
  284. return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
  285. }
  286. std::string AsmStmt::generateAsmString(const ASTContext &C) const {
  287. if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
  288. return gccAsmStmt->generateAsmString(C);
  289. if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
  290. return msAsmStmt->generateAsmString(C);
  291. llvm_unreachable("unknown asm statement kind!");
  292. }
  293. StringRef AsmStmt::getOutputConstraint(unsigned i) const {
  294. if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
  295. return gccAsmStmt->getOutputConstraint(i);
  296. if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
  297. return msAsmStmt->getOutputConstraint(i);
  298. llvm_unreachable("unknown asm statement kind!");
  299. }
  300. const Expr *AsmStmt::getOutputExpr(unsigned i) const {
  301. if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
  302. return gccAsmStmt->getOutputExpr(i);
  303. if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
  304. return msAsmStmt->getOutputExpr(i);
  305. llvm_unreachable("unknown asm statement kind!");
  306. }
  307. StringRef AsmStmt::getInputConstraint(unsigned i) const {
  308. if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
  309. return gccAsmStmt->getInputConstraint(i);
  310. if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
  311. return msAsmStmt->getInputConstraint(i);
  312. llvm_unreachable("unknown asm statement kind!");
  313. }
  314. const Expr *AsmStmt::getInputExpr(unsigned i) const {
  315. if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
  316. return gccAsmStmt->getInputExpr(i);
  317. if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
  318. return msAsmStmt->getInputExpr(i);
  319. llvm_unreachable("unknown asm statement kind!");
  320. }
  321. StringRef AsmStmt::getClobber(unsigned i) const {
  322. if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
  323. return gccAsmStmt->getClobber(i);
  324. if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
  325. return msAsmStmt->getClobber(i);
  326. llvm_unreachable("unknown asm statement kind!");
  327. }
  328. /// getNumPlusOperands - Return the number of output operands that have a "+"
  329. /// constraint.
  330. unsigned AsmStmt::getNumPlusOperands() const {
  331. unsigned Res = 0;
  332. for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
  333. if (isOutputPlusConstraint(i))
  334. ++Res;
  335. return Res;
  336. }
  337. char GCCAsmStmt::AsmStringPiece::getModifier() const {
  338. assert(isOperand() && "Only Operands can have modifiers.");
  339. return isLetter(Str[0]) ? Str[0] : '\0';
  340. }
  341. StringRef GCCAsmStmt::getClobber(unsigned i) const {
  342. return getClobberStringLiteral(i)->getString();
  343. }
  344. Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
  345. return cast<Expr>(Exprs[i]);
  346. }
  347. /// getOutputConstraint - Return the constraint string for the specified
  348. /// output operand. All output constraints are known to be non-empty (either
  349. /// '=' or '+').
  350. StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
  351. return getOutputConstraintLiteral(i)->getString();
  352. }
  353. Expr *GCCAsmStmt::getInputExpr(unsigned i) {
  354. return cast<Expr>(Exprs[i + NumOutputs]);
  355. }
  356. void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
  357. Exprs[i + NumOutputs] = E;
  358. }
  359. /// getInputConstraint - Return the specified input constraint. Unlike output
  360. /// constraints, these can be empty.
  361. StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
  362. return getInputConstraintLiteral(i)->getString();
  363. }
  364. void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
  365. IdentifierInfo **Names,
  366. StringLiteral **Constraints,
  367. Stmt **Exprs,
  368. unsigned NumOutputs,
  369. unsigned NumInputs,
  370. StringLiteral **Clobbers,
  371. unsigned NumClobbers) {
  372. this->NumOutputs = NumOutputs;
  373. this->NumInputs = NumInputs;
  374. this->NumClobbers = NumClobbers;
  375. unsigned NumExprs = NumOutputs + NumInputs;
  376. C.Deallocate(this->Names);
  377. this->Names = new (C) IdentifierInfo*[NumExprs];
  378. std::copy(Names, Names + NumExprs, this->Names);
  379. C.Deallocate(this->Exprs);
  380. this->Exprs = new (C) Stmt*[NumExprs];
  381. std::copy(Exprs, Exprs + NumExprs, this->Exprs);
  382. C.Deallocate(this->Constraints);
  383. this->Constraints = new (C) StringLiteral*[NumExprs];
  384. std::copy(Constraints, Constraints + NumExprs, this->Constraints);
  385. C.Deallocate(this->Clobbers);
  386. this->Clobbers = new (C) StringLiteral*[NumClobbers];
  387. std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
  388. }
  389. /// getNamedOperand - Given a symbolic operand reference like %[foo],
  390. /// translate this into a numeric value needed to reference the same operand.
  391. /// This returns -1 if the operand name is invalid.
  392. int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
  393. unsigned NumPlusOperands = 0;
  394. // Check if this is an output operand.
  395. for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
  396. if (getOutputName(i) == SymbolicName)
  397. return i;
  398. }
  399. for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
  400. if (getInputName(i) == SymbolicName)
  401. return getNumOutputs() + NumPlusOperands + i;
  402. // Not found.
  403. return -1;
  404. }
  405. /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
  406. /// it into pieces. If the asm string is erroneous, emit errors and return
  407. /// true, otherwise return false.
  408. unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
  409. const ASTContext &C, unsigned &DiagOffs) const {
  410. StringRef Str = getAsmString()->getString();
  411. const char *StrStart = Str.begin();
  412. const char *StrEnd = Str.end();
  413. const char *CurPtr = StrStart;
  414. // "Simple" inline asms have no constraints or operands, just convert the asm
  415. // string to escape $'s.
  416. if (isSimple()) {
  417. std::string Result;
  418. for (; CurPtr != StrEnd; ++CurPtr) {
  419. switch (*CurPtr) {
  420. case '$':
  421. Result += "$$";
  422. break;
  423. default:
  424. Result += *CurPtr;
  425. break;
  426. }
  427. }
  428. Pieces.push_back(AsmStringPiece(Result));
  429. return 0;
  430. }
  431. // CurStringPiece - The current string that we are building up as we scan the
  432. // asm string.
  433. std::string CurStringPiece;
  434. bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
  435. while (1) {
  436. // Done with the string?
  437. if (CurPtr == StrEnd) {
  438. if (!CurStringPiece.empty())
  439. Pieces.push_back(AsmStringPiece(CurStringPiece));
  440. return 0;
  441. }
  442. char CurChar = *CurPtr++;
  443. switch (CurChar) {
  444. case '$': CurStringPiece += "$$"; continue;
  445. case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
  446. case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
  447. case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
  448. case '%':
  449. break;
  450. default:
  451. CurStringPiece += CurChar;
  452. continue;
  453. }
  454. // Escaped "%" character in asm string.
  455. if (CurPtr == StrEnd) {
  456. // % at end of string is invalid (no escape).
  457. DiagOffs = CurPtr-StrStart-1;
  458. return diag::err_asm_invalid_escape;
  459. }
  460. char EscapedChar = *CurPtr++;
  461. if (EscapedChar == '%') { // %% -> %
  462. // Escaped percentage sign.
  463. CurStringPiece += '%';
  464. continue;
  465. }
  466. if (EscapedChar == '=') { // %= -> Generate an unique ID.
  467. CurStringPiece += "${:uid}";
  468. continue;
  469. }
  470. // Otherwise, we have an operand. If we have accumulated a string so far,
  471. // add it to the Pieces list.
  472. if (!CurStringPiece.empty()) {
  473. Pieces.push_back(AsmStringPiece(CurStringPiece));
  474. CurStringPiece.clear();
  475. }
  476. // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
  477. // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
  478. const char *Begin = CurPtr - 1; // Points to the character following '%'.
  479. const char *Percent = Begin - 1; // Points to '%'.
  480. if (isLetter(EscapedChar)) {
  481. if (CurPtr == StrEnd) { // Premature end.
  482. DiagOffs = CurPtr-StrStart-1;
  483. return diag::err_asm_invalid_escape;
  484. }
  485. EscapedChar = *CurPtr++;
  486. }
  487. const TargetInfo &TI = C.getTargetInfo();
  488. const SourceManager &SM = C.getSourceManager();
  489. const LangOptions &LO = C.getLangOpts();
  490. // Handle operands that don't have asmSymbolicName (e.g., %x4).
  491. if (isDigit(EscapedChar)) {
  492. // %n - Assembler operand n
  493. unsigned N = 0;
  494. --CurPtr;
  495. while (CurPtr != StrEnd && isDigit(*CurPtr))
  496. N = N*10 + ((*CurPtr++)-'0');
  497. unsigned NumOperands =
  498. getNumOutputs() + getNumPlusOperands() + getNumInputs();
  499. if (N >= NumOperands) {
  500. DiagOffs = CurPtr-StrStart-1;
  501. return diag::err_asm_invalid_operand_number;
  502. }
  503. // Str contains "x4" (Operand without the leading %).
  504. std::string Str(Begin, CurPtr - Begin);
  505. // (BeginLoc, EndLoc) represents the range of the operand we are currently
  506. // processing. Unlike Str, the range includes the leading '%'.
  507. SourceLocation BeginLoc =
  508. getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
  509. SourceLocation EndLoc =
  510. getAsmString()->getLocationOfByte(CurPtr - StrStart, SM, LO, TI);
  511. Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
  512. continue;
  513. }
  514. // Handle operands that have asmSymbolicName (e.g., %x[foo]).
  515. if (EscapedChar == '[') {
  516. DiagOffs = CurPtr-StrStart-1;
  517. // Find the ']'.
  518. const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
  519. if (NameEnd == nullptr)
  520. return diag::err_asm_unterminated_symbolic_operand_name;
  521. if (NameEnd == CurPtr)
  522. return diag::err_asm_empty_symbolic_operand_name;
  523. StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
  524. int N = getNamedOperand(SymbolicName);
  525. if (N == -1) {
  526. // Verify that an operand with that name exists.
  527. DiagOffs = CurPtr-StrStart;
  528. return diag::err_asm_unknown_symbolic_operand_name;
  529. }
  530. // Str contains "x[foo]" (Operand without the leading %).
  531. std::string Str(Begin, NameEnd + 1 - Begin);
  532. // (BeginLoc, EndLoc) represents the range of the operand we are currently
  533. // processing. Unlike Str, the range includes the leading '%'.
  534. SourceLocation BeginLoc =
  535. getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
  536. SourceLocation EndLoc =
  537. getAsmString()->getLocationOfByte(NameEnd + 1 - StrStart, SM, LO, TI);
  538. Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
  539. CurPtr = NameEnd+1;
  540. continue;
  541. }
  542. DiagOffs = CurPtr-StrStart-1;
  543. return diag::err_asm_invalid_escape;
  544. }
  545. }
  546. /// Assemble final IR asm string (GCC-style).
  547. std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
  548. // Analyze the asm string to decompose it into its pieces. We know that Sema
  549. // has already done this, so it is guaranteed to be successful.
  550. SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
  551. unsigned DiagOffs;
  552. AnalyzeAsmString(Pieces, C, DiagOffs);
  553. std::string AsmString;
  554. for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
  555. if (Pieces[i].isString())
  556. AsmString += Pieces[i].getString();
  557. else if (Pieces[i].getModifier() == '\0')
  558. AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
  559. else
  560. AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
  561. Pieces[i].getModifier() + '}';
  562. }
  563. return AsmString;
  564. }
  565. /// Assemble final IR asm string (MS-style).
  566. std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
  567. // FIXME: This needs to be translated into the IR string representation.
  568. return AsmStr;
  569. }
  570. Expr *MSAsmStmt::getOutputExpr(unsigned i) {
  571. return cast<Expr>(Exprs[i]);
  572. }
  573. Expr *MSAsmStmt::getInputExpr(unsigned i) {
  574. return cast<Expr>(Exprs[i + NumOutputs]);
  575. }
  576. void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
  577. Exprs[i + NumOutputs] = E;
  578. }
  579. QualType CXXCatchStmt::getCaughtType() const {
  580. if (ExceptionDecl)
  581. return ExceptionDecl->getType();
  582. return QualType();
  583. }
  584. //===----------------------------------------------------------------------===//
  585. // Constructors
  586. //===----------------------------------------------------------------------===//
  587. GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
  588. bool issimple, bool isvolatile, unsigned numoutputs,
  589. unsigned numinputs, IdentifierInfo **names,
  590. StringLiteral **constraints, Expr **exprs,
  591. StringLiteral *asmstr, unsigned numclobbers,
  592. StringLiteral **clobbers, SourceLocation rparenloc)
  593. : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
  594. numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
  595. unsigned NumExprs = NumOutputs + NumInputs;
  596. Names = new (C) IdentifierInfo*[NumExprs];
  597. std::copy(names, names + NumExprs, Names);
  598. Exprs = new (C) Stmt*[NumExprs];
  599. std::copy(exprs, exprs + NumExprs, Exprs);
  600. Constraints = new (C) StringLiteral*[NumExprs];
  601. std::copy(constraints, constraints + NumExprs, Constraints);
  602. Clobbers = new (C) StringLiteral*[NumClobbers];
  603. std::copy(clobbers, clobbers + NumClobbers, Clobbers);
  604. }
  605. MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
  606. SourceLocation lbraceloc, bool issimple, bool isvolatile,
  607. ArrayRef<Token> asmtoks, unsigned numoutputs,
  608. unsigned numinputs,
  609. ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
  610. StringRef asmstr, ArrayRef<StringRef> clobbers,
  611. SourceLocation endloc)
  612. : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
  613. numinputs, clobbers.size()), LBraceLoc(lbraceloc),
  614. EndLoc(endloc), NumAsmToks(asmtoks.size()) {
  615. initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
  616. }
  617. static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
  618. if (str.empty())
  619. return StringRef();
  620. size_t size = str.size();
  621. char *buffer = new (C) char[size];
  622. memcpy(buffer, str.data(), size);
  623. return StringRef(buffer, size);
  624. }
  625. void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
  626. ArrayRef<Token> asmtoks,
  627. ArrayRef<StringRef> constraints,
  628. ArrayRef<Expr*> exprs,
  629. ArrayRef<StringRef> clobbers) {
  630. assert(NumAsmToks == asmtoks.size());
  631. assert(NumClobbers == clobbers.size());
  632. unsigned NumExprs = exprs.size();
  633. assert(NumExprs == NumOutputs + NumInputs);
  634. assert(NumExprs == constraints.size());
  635. AsmStr = copyIntoContext(C, asmstr);
  636. Exprs = new (C) Stmt*[NumExprs];
  637. for (unsigned i = 0, e = NumExprs; i != e; ++i)
  638. Exprs[i] = exprs[i];
  639. AsmToks = new (C) Token[NumAsmToks];
  640. for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
  641. AsmToks[i] = asmtoks[i];
  642. Constraints = new (C) StringRef[NumExprs];
  643. for (unsigned i = 0, e = NumExprs; i != e; ++i) {
  644. Constraints[i] = copyIntoContext(C, constraints[i]);
  645. }
  646. Clobbers = new (C) StringRef[NumClobbers];
  647. for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
  648. // FIXME: Avoid the allocation/copy if at all possible.
  649. Clobbers[i] = copyIntoContext(C, clobbers[i]);
  650. }
  651. }
  652. ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
  653. Stmt *Body, SourceLocation FCL,
  654. SourceLocation RPL)
  655. : Stmt(ObjCForCollectionStmtClass) {
  656. SubExprs[ELEM] = Elem;
  657. SubExprs[COLLECTION] = Collect;
  658. SubExprs[BODY] = Body;
  659. ForLoc = FCL;
  660. RParenLoc = RPL;
  661. }
  662. ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
  663. Stmt **CatchStmts, unsigned NumCatchStmts,
  664. Stmt *atFinallyStmt)
  665. : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
  666. NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
  667. Stmt **Stmts = getStmts();
  668. Stmts[0] = atTryStmt;
  669. for (unsigned I = 0; I != NumCatchStmts; ++I)
  670. Stmts[I + 1] = CatchStmts[I];
  671. if (HasFinally)
  672. Stmts[NumCatchStmts + 1] = atFinallyStmt;
  673. }
  674. ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
  675. SourceLocation atTryLoc,
  676. Stmt *atTryStmt,
  677. Stmt **CatchStmts,
  678. unsigned NumCatchStmts,
  679. Stmt *atFinallyStmt) {
  680. unsigned Size = sizeof(ObjCAtTryStmt) +
  681. (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
  682. void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
  683. return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
  684. atFinallyStmt);
  685. }
  686. ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
  687. unsigned NumCatchStmts,
  688. bool HasFinally) {
  689. unsigned Size = sizeof(ObjCAtTryStmt) +
  690. (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
  691. void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
  692. return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
  693. }
  694. SourceLocation ObjCAtTryStmt::getLocEnd() const {
  695. if (HasFinally)
  696. return getFinallyStmt()->getLocEnd();
  697. if (NumCatchStmts)
  698. return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
  699. return getTryBody()->getLocEnd();
  700. }
  701. CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
  702. Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
  703. std::size_t Size = sizeof(CXXTryStmt);
  704. Size += ((handlers.size() + 1) * sizeof(Stmt));
  705. void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
  706. return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
  707. }
  708. CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
  709. unsigned numHandlers) {
  710. std::size_t Size = sizeof(CXXTryStmt);
  711. Size += ((numHandlers + 1) * sizeof(Stmt));
  712. void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
  713. return new (Mem) CXXTryStmt(Empty, numHandlers);
  714. }
  715. CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
  716. ArrayRef<Stmt*> handlers)
  717. : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
  718. Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
  719. Stmts[0] = tryBlock;
  720. std::copy(handlers.begin(), handlers.end(), Stmts + 1);
  721. }
  722. CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
  723. Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
  724. Stmt *Body, SourceLocation FL,
  725. SourceLocation CL, SourceLocation RPL)
  726. : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
  727. SubExprs[RANGE] = Range;
  728. SubExprs[BEGINEND] = BeginEndStmt;
  729. SubExprs[COND] = Cond;
  730. SubExprs[INC] = Inc;
  731. SubExprs[LOOPVAR] = LoopVar;
  732. SubExprs[BODY] = Body;
  733. }
  734. Expr *CXXForRangeStmt::getRangeInit() {
  735. DeclStmt *RangeStmt = getRangeStmt();
  736. VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
  737. assert(RangeDecl && "for-range should have a single var decl");
  738. return RangeDecl->getInit();
  739. }
  740. const Expr *CXXForRangeStmt::getRangeInit() const {
  741. return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
  742. }
  743. VarDecl *CXXForRangeStmt::getLoopVariable() {
  744. Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
  745. assert(LV && "No loop variable in CXXForRangeStmt");
  746. return cast<VarDecl>(LV);
  747. }
  748. const VarDecl *CXXForRangeStmt::getLoopVariable() const {
  749. return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
  750. }
  751. IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
  752. Stmt *then, SourceLocation EL, Stmt *elsev)
  753. : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
  754. {
  755. setConditionVariable(C, var);
  756. SubExprs[COND] = cond;
  757. SubExprs[THEN] = then;
  758. SubExprs[ELSE] = elsev;
  759. }
  760. VarDecl *IfStmt::getConditionVariable() const {
  761. if (!SubExprs[VAR])
  762. return nullptr;
  763. DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
  764. return cast<VarDecl>(DS->getSingleDecl());
  765. }
  766. void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
  767. if (!V) {
  768. SubExprs[VAR] = nullptr;
  769. return;
  770. }
  771. SourceRange VarRange = V->getSourceRange();
  772. SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
  773. VarRange.getEnd());
  774. }
  775. ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
  776. Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
  777. SourceLocation RP)
  778. : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
  779. {
  780. SubExprs[INIT] = Init;
  781. setConditionVariable(C, condVar);
  782. SubExprs[COND] = Cond;
  783. SubExprs[INC] = Inc;
  784. SubExprs[BODY] = Body;
  785. }
  786. VarDecl *ForStmt::getConditionVariable() const {
  787. if (!SubExprs[CONDVAR])
  788. return nullptr;
  789. DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
  790. return cast<VarDecl>(DS->getSingleDecl());
  791. }
  792. void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
  793. if (!V) {
  794. SubExprs[CONDVAR] = nullptr;
  795. return;
  796. }
  797. SourceRange VarRange = V->getSourceRange();
  798. SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
  799. VarRange.getEnd());
  800. }
  801. SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
  802. : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
  803. setConditionVariable(C, Var);
  804. SubExprs[COND] = cond;
  805. SubExprs[BODY] = nullptr;
  806. }
  807. VarDecl *SwitchStmt::getConditionVariable() const {
  808. if (!SubExprs[VAR])
  809. return nullptr;
  810. DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
  811. return cast<VarDecl>(DS->getSingleDecl());
  812. }
  813. void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
  814. if (!V) {
  815. SubExprs[VAR] = nullptr;
  816. return;
  817. }
  818. SourceRange VarRange = V->getSourceRange();
  819. SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
  820. VarRange.getEnd());
  821. }
  822. Stmt *SwitchCase::getSubStmt() {
  823. if (isa<CaseStmt>(this))
  824. return cast<CaseStmt>(this)->getSubStmt();
  825. return cast<DefaultStmt>(this)->getSubStmt();
  826. }
  827. WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
  828. SourceLocation WL)
  829. : Stmt(WhileStmtClass) {
  830. setConditionVariable(C, Var);
  831. SubExprs[COND] = cond;
  832. SubExprs[BODY] = body;
  833. WhileLoc = WL;
  834. }
  835. VarDecl *WhileStmt::getConditionVariable() const {
  836. if (!SubExprs[VAR])
  837. return nullptr;
  838. DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
  839. return cast<VarDecl>(DS->getSingleDecl());
  840. }
  841. void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
  842. if (!V) {
  843. SubExprs[VAR] = nullptr;
  844. return;
  845. }
  846. SourceRange VarRange = V->getSourceRange();
  847. SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
  848. VarRange.getEnd());
  849. }
  850. // IndirectGotoStmt
  851. LabelDecl *IndirectGotoStmt::getConstantTarget() {
  852. if (AddrLabelExpr *E =
  853. dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
  854. return E->getLabel();
  855. return nullptr;
  856. }
  857. // ReturnStmt
  858. const Expr* ReturnStmt::getRetValue() const {
  859. return cast_or_null<Expr>(RetExpr);
  860. }
  861. Expr* ReturnStmt::getRetValue() {
  862. return cast_or_null<Expr>(RetExpr);
  863. }
  864. SEHTryStmt::SEHTryStmt(bool IsCXXTry,
  865. SourceLocation TryLoc,
  866. Stmt *TryBlock,
  867. Stmt *Handler)
  868. : Stmt(SEHTryStmtClass),
  869. IsCXXTry(IsCXXTry),
  870. TryLoc(TryLoc)
  871. {
  872. Children[TRY] = TryBlock;
  873. Children[HANDLER] = Handler;
  874. }
  875. SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
  876. SourceLocation TryLoc, Stmt *TryBlock,
  877. Stmt *Handler) {
  878. return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
  879. }
  880. SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
  881. return dyn_cast<SEHExceptStmt>(getHandler());
  882. }
  883. SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
  884. return dyn_cast<SEHFinallyStmt>(getHandler());
  885. }
  886. SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
  887. Expr *FilterExpr,
  888. Stmt *Block)
  889. : Stmt(SEHExceptStmtClass),
  890. Loc(Loc)
  891. {
  892. Children[FILTER_EXPR] = FilterExpr;
  893. Children[BLOCK] = Block;
  894. }
  895. SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
  896. Expr *FilterExpr, Stmt *Block) {
  897. return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
  898. }
  899. SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
  900. Stmt *Block)
  901. : Stmt(SEHFinallyStmtClass),
  902. Loc(Loc),
  903. Block(Block)
  904. {}
  905. SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
  906. Stmt *Block) {
  907. return new(C)SEHFinallyStmt(Loc,Block);
  908. }
  909. CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
  910. unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
  911. // Offset of the first Capture object.
  912. unsigned FirstCaptureOffset =
  913. llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
  914. return reinterpret_cast<Capture *>(
  915. reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
  916. + FirstCaptureOffset);
  917. }
  918. CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
  919. ArrayRef<Capture> Captures,
  920. ArrayRef<Expr *> CaptureInits,
  921. CapturedDecl *CD,
  922. RecordDecl *RD)
  923. : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
  924. CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
  925. assert( S && "null captured statement");
  926. assert(CD && "null captured declaration for captured statement");
  927. assert(RD && "null record declaration for captured statement");
  928. // Copy initialization expressions.
  929. Stmt **Stored = getStoredStmts();
  930. for (unsigned I = 0, N = NumCaptures; I != N; ++I)
  931. *Stored++ = CaptureInits[I];
  932. // Copy the statement being captured.
  933. *Stored = S;
  934. // Copy all Capture objects.
  935. Capture *Buffer = getStoredCaptures();
  936. std::copy(Captures.begin(), Captures.end(), Buffer);
  937. }
  938. CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
  939. : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
  940. CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
  941. getStoredStmts()[NumCaptures] = nullptr;
  942. }
  943. CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
  944. CapturedRegionKind Kind,
  945. ArrayRef<Capture> Captures,
  946. ArrayRef<Expr *> CaptureInits,
  947. CapturedDecl *CD,
  948. RecordDecl *RD) {
  949. // The layout is
  950. //
  951. // -----------------------------------------------------------
  952. // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
  953. // ----------------^-------------------^----------------------
  954. // getStoredStmts() getStoredCaptures()
  955. //
  956. // where S is the statement being captured.
  957. //
  958. assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
  959. unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
  960. if (!Captures.empty()) {
  961. // Realign for the following Capture array.
  962. Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
  963. Size += sizeof(Capture) * Captures.size();
  964. }
  965. void *Mem = Context.Allocate(Size);
  966. return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
  967. }
  968. CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
  969. unsigned NumCaptures) {
  970. unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
  971. if (NumCaptures > 0) {
  972. // Realign for the following Capture array.
  973. Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
  974. Size += sizeof(Capture) * NumCaptures;
  975. }
  976. void *Mem = Context.Allocate(Size);
  977. return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
  978. }
  979. Stmt::child_range CapturedStmt::children() {
  980. // Children are captured field initilizers.
  981. return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
  982. }
  983. bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
  984. for (const auto &I : captures()) {
  985. if (!I.capturesVariable())
  986. continue;
  987. // This does not handle variable redeclarations. This should be
  988. // extended to capture variables with redeclarations, for example
  989. // a thread-private variable in OpenMP.
  990. if (I.getCapturedVar() == Var)
  991. return true;
  992. }
  993. return false;
  994. }
  995. StmtRange OMPClause::children() {
  996. switch(getClauseKind()) {
  997. default : break;
  998. #define OPENMP_CLAUSE(Name, Class) \
  999. case OMPC_ ## Name : return static_cast<Class *>(this)->children();
  1000. #include "clang/Basic/OpenMPKinds.def"
  1001. }
  1002. llvm_unreachable("unknown OMPClause");
  1003. }
  1004. void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
  1005. assert(VL.size() == varlist_size() &&
  1006. "Number of private copies is not the same as the preallocated buffer");
  1007. std::copy(VL.begin(), VL.end(), varlist_end());
  1008. }
  1009. OMPPrivateClause *
  1010. OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
  1011. SourceLocation LParenLoc, SourceLocation EndLoc,
  1012. ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
  1013. // Allocate space for private variables and initializer expressions.
  1014. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
  1015. llvm::alignOf<Expr *>()) +
  1016. 2 * sizeof(Expr *) * VL.size());
  1017. OMPPrivateClause *Clause =
  1018. new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  1019. Clause->setVarRefs(VL);
  1020. Clause->setPrivateCopies(PrivateVL);
  1021. return Clause;
  1022. }
  1023. OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
  1024. unsigned N) {
  1025. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
  1026. llvm::alignOf<Expr *>()) +
  1027. 2 * sizeof(Expr *) * N);
  1028. return new (Mem) OMPPrivateClause(N);
  1029. }
  1030. void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
  1031. assert(VL.size() == varlist_size() &&
  1032. "Number of private copies is not the same as the preallocated buffer");
  1033. std::copy(VL.begin(), VL.end(), varlist_end());
  1034. }
  1035. void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
  1036. assert(VL.size() == varlist_size() &&
  1037. "Number of inits is not the same as the preallocated buffer");
  1038. std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
  1039. }
  1040. OMPFirstprivateClause *
  1041. OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
  1042. SourceLocation LParenLoc, SourceLocation EndLoc,
  1043. ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
  1044. ArrayRef<Expr *> InitVL) {
  1045. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
  1046. llvm::alignOf<Expr *>()) +
  1047. 3 * sizeof(Expr *) * VL.size());
  1048. OMPFirstprivateClause *Clause =
  1049. new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  1050. Clause->setVarRefs(VL);
  1051. Clause->setPrivateCopies(PrivateVL);
  1052. Clause->setInits(InitVL);
  1053. return Clause;
  1054. }
  1055. OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
  1056. unsigned N) {
  1057. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
  1058. llvm::alignOf<Expr *>()) +
  1059. 3 * sizeof(Expr *) * N);
  1060. return new (Mem) OMPFirstprivateClause(N);
  1061. }
  1062. void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
  1063. assert(PrivateCopies.size() == varlist_size() &&
  1064. "Number of private copies is not the same as the preallocated buffer");
  1065. std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
  1066. }
  1067. void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
  1068. assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
  1069. "not the same as the "
  1070. "preallocated buffer");
  1071. std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
  1072. }
  1073. void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
  1074. assert(DstExprs.size() == varlist_size() && "Number of destination "
  1075. "expressions is not the same as "
  1076. "the preallocated buffer");
  1077. std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
  1078. }
  1079. void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
  1080. assert(AssignmentOps.size() == varlist_size() &&
  1081. "Number of assignment expressions is not the same as the preallocated "
  1082. "buffer");
  1083. std::copy(AssignmentOps.begin(), AssignmentOps.end(),
  1084. getDestinationExprs().end());
  1085. }
  1086. OMPLastprivateClause *OMPLastprivateClause::Create(
  1087. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  1088. SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
  1089. ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
  1090. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
  1091. llvm::alignOf<Expr *>()) +
  1092. 5 * sizeof(Expr *) * VL.size());
  1093. OMPLastprivateClause *Clause =
  1094. new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  1095. Clause->setVarRefs(VL);
  1096. Clause->setSourceExprs(SrcExprs);
  1097. Clause->setDestinationExprs(DstExprs);
  1098. Clause->setAssignmentOps(AssignmentOps);
  1099. return Clause;
  1100. }
  1101. OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
  1102. unsigned N) {
  1103. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
  1104. llvm::alignOf<Expr *>()) +
  1105. 5 * sizeof(Expr *) * N);
  1106. return new (Mem) OMPLastprivateClause(N);
  1107. }
  1108. OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
  1109. SourceLocation StartLoc,
  1110. SourceLocation LParenLoc,
  1111. SourceLocation EndLoc,
  1112. ArrayRef<Expr *> VL) {
  1113. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
  1114. llvm::alignOf<Expr *>()) +
  1115. sizeof(Expr *) * VL.size());
  1116. OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
  1117. EndLoc, VL.size());
  1118. Clause->setVarRefs(VL);
  1119. return Clause;
  1120. }
  1121. OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
  1122. unsigned N) {
  1123. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
  1124. llvm::alignOf<Expr *>()) +
  1125. sizeof(Expr *) * N);
  1126. return new (Mem) OMPSharedClause(N);
  1127. }
  1128. void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
  1129. assert(IL.size() == varlist_size() &&
  1130. "Number of inits is not the same as the preallocated buffer");
  1131. std::copy(IL.begin(), IL.end(), varlist_end());
  1132. }
  1133. void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
  1134. assert(UL.size() == varlist_size() &&
  1135. "Number of updates is not the same as the preallocated buffer");
  1136. std::copy(UL.begin(), UL.end(), getInits().end());
  1137. }
  1138. void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
  1139. assert(FL.size() == varlist_size() &&
  1140. "Number of final updates is not the same as the preallocated buffer");
  1141. std::copy(FL.begin(), FL.end(), getUpdates().end());
  1142. }
  1143. OMPLinearClause *
  1144. OMPLinearClause::Create(const ASTContext &C, SourceLocation StartLoc,
  1145. SourceLocation LParenLoc, SourceLocation ColonLoc,
  1146. SourceLocation EndLoc, ArrayRef<Expr *> VL,
  1147. ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep) {
  1148. // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
  1149. // (Step and CalcStep).
  1150. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
  1151. llvm::alignOf<Expr *>()) +
  1152. (4 * VL.size() + 2) * sizeof(Expr *));
  1153. OMPLinearClause *Clause = new (Mem)
  1154. OMPLinearClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
  1155. Clause->setVarRefs(VL);
  1156. Clause->setInits(IL);
  1157. // Fill update and final expressions with zeroes, they are provided later,
  1158. // after the directive construction.
  1159. std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
  1160. nullptr);
  1161. std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
  1162. nullptr);
  1163. Clause->setStep(Step);
  1164. Clause->setCalcStep(CalcStep);
  1165. return Clause;
  1166. }
  1167. OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
  1168. unsigned NumVars) {
  1169. // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
  1170. // (Step and CalcStep).
  1171. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
  1172. llvm::alignOf<Expr *>()) +
  1173. (4 * NumVars + 2) * sizeof(Expr *));
  1174. return new (Mem) OMPLinearClause(NumVars);
  1175. }
  1176. OMPAlignedClause *
  1177. OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
  1178. SourceLocation LParenLoc, SourceLocation ColonLoc,
  1179. SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
  1180. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
  1181. llvm::alignOf<Expr *>()) +
  1182. sizeof(Expr *) * (VL.size() + 1));
  1183. OMPAlignedClause *Clause = new (Mem)
  1184. OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
  1185. Clause->setVarRefs(VL);
  1186. Clause->setAlignment(A);
  1187. return Clause;
  1188. }
  1189. OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
  1190. unsigned NumVars) {
  1191. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
  1192. llvm::alignOf<Expr *>()) +
  1193. sizeof(Expr *) * (NumVars + 1));
  1194. return new (Mem) OMPAlignedClause(NumVars);
  1195. }
  1196. void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
  1197. assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
  1198. "not the same as the "
  1199. "preallocated buffer");
  1200. std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
  1201. }
  1202. void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
  1203. assert(DstExprs.size() == varlist_size() && "Number of destination "
  1204. "expressions is not the same as "
  1205. "the preallocated buffer");
  1206. std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
  1207. }
  1208. void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
  1209. assert(AssignmentOps.size() == varlist_size() &&
  1210. "Number of assignment expressions is not the same as the preallocated "
  1211. "buffer");
  1212. std::copy(AssignmentOps.begin(), AssignmentOps.end(),
  1213. getDestinationExprs().end());
  1214. }
  1215. OMPCopyinClause *OMPCopyinClause::Create(
  1216. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  1217. SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
  1218. ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
  1219. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
  1220. llvm::alignOf<Expr *>()) +
  1221. 4 * sizeof(Expr *) * VL.size());
  1222. OMPCopyinClause *Clause = new (Mem) OMPCopyinClause(StartLoc, LParenLoc,
  1223. EndLoc, VL.size());
  1224. Clause->setVarRefs(VL);
  1225. Clause->setSourceExprs(SrcExprs);
  1226. Clause->setDestinationExprs(DstExprs);
  1227. Clause->setAssignmentOps(AssignmentOps);
  1228. return Clause;
  1229. }
  1230. OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C,
  1231. unsigned N) {
  1232. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
  1233. llvm::alignOf<Expr *>()) +
  1234. 4 * sizeof(Expr *) * N);
  1235. return new (Mem) OMPCopyinClause(N);
  1236. }
  1237. void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
  1238. assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
  1239. "not the same as the "
  1240. "preallocated buffer");
  1241. std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
  1242. }
  1243. void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
  1244. assert(DstExprs.size() == varlist_size() && "Number of destination "
  1245. "expressions is not the same as "
  1246. "the preallocated buffer");
  1247. std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
  1248. }
  1249. void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
  1250. assert(AssignmentOps.size() == varlist_size() &&
  1251. "Number of assignment expressions is not the same as the preallocated "
  1252. "buffer");
  1253. std::copy(AssignmentOps.begin(), AssignmentOps.end(),
  1254. getDestinationExprs().end());
  1255. }
  1256. OMPCopyprivateClause *OMPCopyprivateClause::Create(
  1257. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  1258. SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
  1259. ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
  1260. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
  1261. llvm::alignOf<Expr *>()) +
  1262. 4 * sizeof(Expr *) * VL.size());
  1263. OMPCopyprivateClause *Clause =
  1264. new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  1265. Clause->setVarRefs(VL);
  1266. Clause->setSourceExprs(SrcExprs);
  1267. Clause->setDestinationExprs(DstExprs);
  1268. Clause->setAssignmentOps(AssignmentOps);
  1269. return Clause;
  1270. }
  1271. OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
  1272. unsigned N) {
  1273. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
  1274. llvm::alignOf<Expr *>()) +
  1275. 4 * sizeof(Expr *) * N);
  1276. return new (Mem) OMPCopyprivateClause(N);
  1277. }
  1278. void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
  1279. assert(Clauses.size() == getNumClauses() &&
  1280. "Number of clauses is not the same as the preallocated buffer");
  1281. std::copy(Clauses.begin(), Clauses.end(), getClauses().begin());
  1282. }
  1283. void OMPLoopDirective::setCounters(ArrayRef<Expr *> A) {
  1284. assert(A.size() == getCollapsedNumber() &&
  1285. "Number of loop counters is not the same as the collapsed number");
  1286. std::copy(A.begin(), A.end(), getCounters().begin());
  1287. }
  1288. void OMPLoopDirective::setInits(ArrayRef<Expr *> A) {
  1289. assert(A.size() == getCollapsedNumber() &&
  1290. "Number of counter inits is not the same as the collapsed number");
  1291. std::copy(A.begin(), A.end(), getInits().begin());
  1292. }
  1293. void OMPLoopDirective::setUpdates(ArrayRef<Expr *> A) {
  1294. assert(A.size() == getCollapsedNumber() &&
  1295. "Number of counter updates is not the same as the collapsed number");
  1296. std::copy(A.begin(), A.end(), getUpdates().begin());
  1297. }
  1298. void OMPLoopDirective::setFinals(ArrayRef<Expr *> A) {
  1299. assert(A.size() == getCollapsedNumber() &&
  1300. "Number of counter finals is not the same as the collapsed number");
  1301. std::copy(A.begin(), A.end(), getFinals().begin());
  1302. }
  1303. void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
  1304. assert(
  1305. LHSExprs.size() == varlist_size() &&
  1306. "Number of LHS expressions is not the same as the preallocated buffer");
  1307. std::copy(LHSExprs.begin(), LHSExprs.end(), varlist_end());
  1308. }
  1309. void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
  1310. assert(
  1311. RHSExprs.size() == varlist_size() &&
  1312. "Number of RHS expressions is not the same as the preallocated buffer");
  1313. std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
  1314. }
  1315. void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
  1316. assert(ReductionOps.size() == varlist_size() && "Number of reduction "
  1317. "expressions is not the same "
  1318. "as the preallocated buffer");
  1319. std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
  1320. }
  1321. OMPReductionClause *OMPReductionClause::Create(
  1322. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  1323. SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
  1324. NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
  1325. ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
  1326. ArrayRef<Expr *> ReductionOps) {
  1327. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
  1328. llvm::alignOf<Expr *>()) +
  1329. 4 * sizeof(Expr *) * VL.size());
  1330. OMPReductionClause *Clause = new (Mem) OMPReductionClause(
  1331. StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
  1332. Clause->setVarRefs(VL);
  1333. Clause->setLHSExprs(LHSExprs);
  1334. Clause->setRHSExprs(RHSExprs);
  1335. Clause->setReductionOps(ReductionOps);
  1336. return Clause;
  1337. }
  1338. OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
  1339. unsigned N) {
  1340. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
  1341. llvm::alignOf<Expr *>()) +
  1342. 4 * sizeof(Expr *) * N);
  1343. return new (Mem) OMPReductionClause(N);
  1344. }
  1345. OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
  1346. SourceLocation StartLoc,
  1347. SourceLocation LParenLoc,
  1348. SourceLocation EndLoc,
  1349. ArrayRef<Expr *> VL) {
  1350. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
  1351. llvm::alignOf<Expr *>()) +
  1352. sizeof(Expr *) * VL.size());
  1353. OMPFlushClause *Clause =
  1354. new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
  1355. Clause->setVarRefs(VL);
  1356. return Clause;
  1357. }
  1358. OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
  1359. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
  1360. llvm::alignOf<Expr *>()) +
  1361. sizeof(Expr *) * N);
  1362. return new (Mem) OMPFlushClause(N);
  1363. }
  1364. OMPDependClause *
  1365. OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
  1366. SourceLocation LParenLoc, SourceLocation EndLoc,
  1367. OpenMPDependClauseKind DepKind, SourceLocation DepLoc,
  1368. SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
  1369. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPDependClause),
  1370. llvm::alignOf<Expr *>()) +
  1371. sizeof(Expr *) * VL.size());
  1372. OMPDependClause *Clause =
  1373. new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
  1374. Clause->setVarRefs(VL);
  1375. Clause->setDependencyKind(DepKind);
  1376. Clause->setDependencyLoc(DepLoc);
  1377. Clause->setColonLoc(ColonLoc);
  1378. return Clause;
  1379. }
  1380. OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
  1381. void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPDependClause),
  1382. llvm::alignOf<Expr *>()) +
  1383. sizeof(Expr *) * N);
  1384. return new (Mem) OMPDependClause(N);
  1385. }
  1386. const OMPClause *
  1387. OMPExecutableDirective::getSingleClause(OpenMPClauseKind K) const {
  1388. auto &&I = getClausesOfKind(K);
  1389. if (I) {
  1390. auto *Clause = *I;
  1391. assert(!++I && "There are at least 2 clauses of the specified kind");
  1392. return Clause;
  1393. }
  1394. return nullptr;
  1395. }
  1396. OMPParallelDirective *OMPParallelDirective::Create(
  1397. const ASTContext &C,
  1398. SourceLocation StartLoc,
  1399. SourceLocation EndLoc,
  1400. ArrayRef<OMPClause *> Clauses,
  1401. Stmt *AssociatedStmt) {
  1402. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
  1403. llvm::alignOf<OMPClause *>());
  1404. void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
  1405. sizeof(Stmt *));
  1406. OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
  1407. Clauses.size());
  1408. Dir->setClauses(Clauses);
  1409. Dir->setAssociatedStmt(AssociatedStmt);
  1410. return Dir;
  1411. }
  1412. OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
  1413. unsigned NumClauses,
  1414. EmptyShell) {
  1415. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
  1416. llvm::alignOf<OMPClause *>());
  1417. void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
  1418. sizeof(Stmt *));
  1419. return new (Mem) OMPParallelDirective(NumClauses);
  1420. }
  1421. OMPSimdDirective *
  1422. OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
  1423. SourceLocation EndLoc, unsigned CollapsedNum,
  1424. ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
  1425. const HelperExprs &Exprs) {
  1426. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
  1427. llvm::alignOf<OMPClause *>());
  1428. void *Mem =
  1429. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
  1430. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
  1431. OMPSimdDirective *Dir = new (Mem)
  1432. OMPSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
  1433. Dir->setClauses(Clauses);
  1434. Dir->setAssociatedStmt(AssociatedStmt);
  1435. Dir->setIterationVariable(Exprs.IterationVarRef);
  1436. Dir->setLastIteration(Exprs.LastIteration);
  1437. Dir->setCalcLastIteration(Exprs.CalcLastIteration);
  1438. Dir->setPreCond(Exprs.PreCond);
  1439. Dir->setCond(Exprs.Cond);
  1440. Dir->setInit(Exprs.Init);
  1441. Dir->setInc(Exprs.Inc);
  1442. Dir->setCounters(Exprs.Counters);
  1443. Dir->setInits(Exprs.Inits);
  1444. Dir->setUpdates(Exprs.Updates);
  1445. Dir->setFinals(Exprs.Finals);
  1446. return Dir;
  1447. }
  1448. OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
  1449. unsigned NumClauses,
  1450. unsigned CollapsedNum,
  1451. EmptyShell) {
  1452. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
  1453. llvm::alignOf<OMPClause *>());
  1454. void *Mem =
  1455. C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
  1456. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
  1457. return new (Mem) OMPSimdDirective(CollapsedNum, NumClauses);
  1458. }
  1459. OMPForDirective *
  1460. OMPForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
  1461. SourceLocation EndLoc, unsigned CollapsedNum,
  1462. ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
  1463. const HelperExprs &Exprs) {
  1464. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
  1465. llvm::alignOf<OMPClause *>());
  1466. void *Mem =
  1467. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
  1468. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
  1469. OMPForDirective *Dir =
  1470. new (Mem) OMPForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
  1471. Dir->setClauses(Clauses);
  1472. Dir->setAssociatedStmt(AssociatedStmt);
  1473. Dir->setIterationVariable(Exprs.IterationVarRef);
  1474. Dir->setLastIteration(Exprs.LastIteration);
  1475. Dir->setCalcLastIteration(Exprs.CalcLastIteration);
  1476. Dir->setPreCond(Exprs.PreCond);
  1477. Dir->setCond(Exprs.Cond);
  1478. Dir->setInit(Exprs.Init);
  1479. Dir->setInc(Exprs.Inc);
  1480. Dir->setIsLastIterVariable(Exprs.IL);
  1481. Dir->setLowerBoundVariable(Exprs.LB);
  1482. Dir->setUpperBoundVariable(Exprs.UB);
  1483. Dir->setStrideVariable(Exprs.ST);
  1484. Dir->setEnsureUpperBound(Exprs.EUB);
  1485. Dir->setNextLowerBound(Exprs.NLB);
  1486. Dir->setNextUpperBound(Exprs.NUB);
  1487. Dir->setCounters(Exprs.Counters);
  1488. Dir->setInits(Exprs.Inits);
  1489. Dir->setUpdates(Exprs.Updates);
  1490. Dir->setFinals(Exprs.Finals);
  1491. return Dir;
  1492. }
  1493. OMPForDirective *OMPForDirective::CreateEmpty(const ASTContext &C,
  1494. unsigned NumClauses,
  1495. unsigned CollapsedNum,
  1496. EmptyShell) {
  1497. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
  1498. llvm::alignOf<OMPClause *>());
  1499. void *Mem =
  1500. C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
  1501. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
  1502. return new (Mem) OMPForDirective(CollapsedNum, NumClauses);
  1503. }
  1504. OMPForSimdDirective *
  1505. OMPForSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
  1506. SourceLocation EndLoc, unsigned CollapsedNum,
  1507. ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
  1508. const HelperExprs &Exprs) {
  1509. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
  1510. llvm::alignOf<OMPClause *>());
  1511. void *Mem =
  1512. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
  1513. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
  1514. OMPForSimdDirective *Dir = new (Mem)
  1515. OMPForSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
  1516. Dir->setClauses(Clauses);
  1517. Dir->setAssociatedStmt(AssociatedStmt);
  1518. Dir->setIterationVariable(Exprs.IterationVarRef);
  1519. Dir->setLastIteration(Exprs.LastIteration);
  1520. Dir->setCalcLastIteration(Exprs.CalcLastIteration);
  1521. Dir->setPreCond(Exprs.PreCond);
  1522. Dir->setCond(Exprs.Cond);
  1523. Dir->setInit(Exprs.Init);
  1524. Dir->setInc(Exprs.Inc);
  1525. Dir->setIsLastIterVariable(Exprs.IL);
  1526. Dir->setLowerBoundVariable(Exprs.LB);
  1527. Dir->setUpperBoundVariable(Exprs.UB);
  1528. Dir->setStrideVariable(Exprs.ST);
  1529. Dir->setEnsureUpperBound(Exprs.EUB);
  1530. Dir->setNextLowerBound(Exprs.NLB);
  1531. Dir->setNextUpperBound(Exprs.NUB);
  1532. Dir->setCounters(Exprs.Counters);
  1533. Dir->setInits(Exprs.Inits);
  1534. Dir->setUpdates(Exprs.Updates);
  1535. Dir->setFinals(Exprs.Finals);
  1536. return Dir;
  1537. }
  1538. OMPForSimdDirective *OMPForSimdDirective::CreateEmpty(const ASTContext &C,
  1539. unsigned NumClauses,
  1540. unsigned CollapsedNum,
  1541. EmptyShell) {
  1542. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
  1543. llvm::alignOf<OMPClause *>());
  1544. void *Mem =
  1545. C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
  1546. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
  1547. return new (Mem) OMPForSimdDirective(CollapsedNum, NumClauses);
  1548. }
  1549. OMPSectionsDirective *OMPSectionsDirective::Create(
  1550. const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
  1551. ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
  1552. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
  1553. llvm::alignOf<OMPClause *>());
  1554. void *Mem =
  1555. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
  1556. OMPSectionsDirective *Dir =
  1557. new (Mem) OMPSectionsDirective(StartLoc, EndLoc, Clauses.size());
  1558. Dir->setClauses(Clauses);
  1559. Dir->setAssociatedStmt(AssociatedStmt);
  1560. return Dir;
  1561. }
  1562. OMPSectionsDirective *OMPSectionsDirective::CreateEmpty(const ASTContext &C,
  1563. unsigned NumClauses,
  1564. EmptyShell) {
  1565. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
  1566. llvm::alignOf<OMPClause *>());
  1567. void *Mem =
  1568. C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
  1569. return new (Mem) OMPSectionsDirective(NumClauses);
  1570. }
  1571. OMPSectionDirective *OMPSectionDirective::Create(const ASTContext &C,
  1572. SourceLocation StartLoc,
  1573. SourceLocation EndLoc,
  1574. Stmt *AssociatedStmt) {
  1575. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
  1576. llvm::alignOf<Stmt *>());
  1577. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1578. OMPSectionDirective *Dir = new (Mem) OMPSectionDirective(StartLoc, EndLoc);
  1579. Dir->setAssociatedStmt(AssociatedStmt);
  1580. return Dir;
  1581. }
  1582. OMPSectionDirective *OMPSectionDirective::CreateEmpty(const ASTContext &C,
  1583. EmptyShell) {
  1584. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
  1585. llvm::alignOf<Stmt *>());
  1586. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1587. return new (Mem) OMPSectionDirective();
  1588. }
  1589. OMPSingleDirective *OMPSingleDirective::Create(const ASTContext &C,
  1590. SourceLocation StartLoc,
  1591. SourceLocation EndLoc,
  1592. ArrayRef<OMPClause *> Clauses,
  1593. Stmt *AssociatedStmt) {
  1594. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
  1595. llvm::alignOf<OMPClause *>());
  1596. void *Mem =
  1597. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
  1598. OMPSingleDirective *Dir =
  1599. new (Mem) OMPSingleDirective(StartLoc, EndLoc, Clauses.size());
  1600. Dir->setClauses(Clauses);
  1601. Dir->setAssociatedStmt(AssociatedStmt);
  1602. return Dir;
  1603. }
  1604. OMPSingleDirective *OMPSingleDirective::CreateEmpty(const ASTContext &C,
  1605. unsigned NumClauses,
  1606. EmptyShell) {
  1607. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
  1608. llvm::alignOf<OMPClause *>());
  1609. void *Mem =
  1610. C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
  1611. return new (Mem) OMPSingleDirective(NumClauses);
  1612. }
  1613. OMPMasterDirective *OMPMasterDirective::Create(const ASTContext &C,
  1614. SourceLocation StartLoc,
  1615. SourceLocation EndLoc,
  1616. Stmt *AssociatedStmt) {
  1617. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
  1618. llvm::alignOf<Stmt *>());
  1619. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1620. OMPMasterDirective *Dir = new (Mem) OMPMasterDirective(StartLoc, EndLoc);
  1621. Dir->setAssociatedStmt(AssociatedStmt);
  1622. return Dir;
  1623. }
  1624. OMPMasterDirective *OMPMasterDirective::CreateEmpty(const ASTContext &C,
  1625. EmptyShell) {
  1626. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
  1627. llvm::alignOf<Stmt *>());
  1628. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1629. return new (Mem) OMPMasterDirective();
  1630. }
  1631. OMPCriticalDirective *OMPCriticalDirective::Create(
  1632. const ASTContext &C, const DeclarationNameInfo &Name,
  1633. SourceLocation StartLoc, SourceLocation EndLoc, Stmt *AssociatedStmt) {
  1634. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
  1635. llvm::alignOf<Stmt *>());
  1636. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1637. OMPCriticalDirective *Dir =
  1638. new (Mem) OMPCriticalDirective(Name, StartLoc, EndLoc);
  1639. Dir->setAssociatedStmt(AssociatedStmt);
  1640. return Dir;
  1641. }
  1642. OMPCriticalDirective *OMPCriticalDirective::CreateEmpty(const ASTContext &C,
  1643. EmptyShell) {
  1644. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
  1645. llvm::alignOf<Stmt *>());
  1646. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1647. return new (Mem) OMPCriticalDirective();
  1648. }
  1649. OMPParallelForDirective *OMPParallelForDirective::Create(
  1650. const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
  1651. unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
  1652. const HelperExprs &Exprs) {
  1653. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
  1654. llvm::alignOf<OMPClause *>());
  1655. void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
  1656. sizeof(Stmt *) *
  1657. numLoopChildren(CollapsedNum, OMPD_parallel_for));
  1658. OMPParallelForDirective *Dir = new (Mem)
  1659. OMPParallelForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
  1660. Dir->setClauses(Clauses);
  1661. Dir->setAssociatedStmt(AssociatedStmt);
  1662. Dir->setIterationVariable(Exprs.IterationVarRef);
  1663. Dir->setLastIteration(Exprs.LastIteration);
  1664. Dir->setCalcLastIteration(Exprs.CalcLastIteration);
  1665. Dir->setPreCond(Exprs.PreCond);
  1666. Dir->setCond(Exprs.Cond);
  1667. Dir->setInit(Exprs.Init);
  1668. Dir->setInc(Exprs.Inc);
  1669. Dir->setIsLastIterVariable(Exprs.IL);
  1670. Dir->setLowerBoundVariable(Exprs.LB);
  1671. Dir->setUpperBoundVariable(Exprs.UB);
  1672. Dir->setStrideVariable(Exprs.ST);
  1673. Dir->setEnsureUpperBound(Exprs.EUB);
  1674. Dir->setNextLowerBound(Exprs.NLB);
  1675. Dir->setNextUpperBound(Exprs.NUB);
  1676. Dir->setCounters(Exprs.Counters);
  1677. Dir->setInits(Exprs.Inits);
  1678. Dir->setUpdates(Exprs.Updates);
  1679. Dir->setFinals(Exprs.Finals);
  1680. return Dir;
  1681. }
  1682. OMPParallelForDirective *
  1683. OMPParallelForDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses,
  1684. unsigned CollapsedNum, EmptyShell) {
  1685. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
  1686. llvm::alignOf<OMPClause *>());
  1687. void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
  1688. sizeof(Stmt *) *
  1689. numLoopChildren(CollapsedNum, OMPD_parallel_for));
  1690. return new (Mem) OMPParallelForDirective(CollapsedNum, NumClauses);
  1691. }
  1692. OMPParallelForSimdDirective *OMPParallelForSimdDirective::Create(
  1693. const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
  1694. unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
  1695. const HelperExprs &Exprs) {
  1696. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
  1697. llvm::alignOf<OMPClause *>());
  1698. void *Mem = C.Allocate(
  1699. Size + sizeof(OMPClause *) * Clauses.size() +
  1700. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
  1701. OMPParallelForSimdDirective *Dir = new (Mem) OMPParallelForSimdDirective(
  1702. StartLoc, EndLoc, CollapsedNum, Clauses.size());
  1703. Dir->setClauses(Clauses);
  1704. Dir->setAssociatedStmt(AssociatedStmt);
  1705. Dir->setIterationVariable(Exprs.IterationVarRef);
  1706. Dir->setLastIteration(Exprs.LastIteration);
  1707. Dir->setCalcLastIteration(Exprs.CalcLastIteration);
  1708. Dir->setPreCond(Exprs.PreCond);
  1709. Dir->setCond(Exprs.Cond);
  1710. Dir->setInit(Exprs.Init);
  1711. Dir->setInc(Exprs.Inc);
  1712. Dir->setIsLastIterVariable(Exprs.IL);
  1713. Dir->setLowerBoundVariable(Exprs.LB);
  1714. Dir->setUpperBoundVariable(Exprs.UB);
  1715. Dir->setStrideVariable(Exprs.ST);
  1716. Dir->setEnsureUpperBound(Exprs.EUB);
  1717. Dir->setNextLowerBound(Exprs.NLB);
  1718. Dir->setNextUpperBound(Exprs.NUB);
  1719. Dir->setCounters(Exprs.Counters);
  1720. Dir->setInits(Exprs.Inits);
  1721. Dir->setUpdates(Exprs.Updates);
  1722. Dir->setFinals(Exprs.Finals);
  1723. return Dir;
  1724. }
  1725. OMPParallelForSimdDirective *
  1726. OMPParallelForSimdDirective::CreateEmpty(const ASTContext &C,
  1727. unsigned NumClauses,
  1728. unsigned CollapsedNum, EmptyShell) {
  1729. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
  1730. llvm::alignOf<OMPClause *>());
  1731. void *Mem = C.Allocate(
  1732. Size + sizeof(OMPClause *) * NumClauses +
  1733. sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
  1734. return new (Mem) OMPParallelForSimdDirective(CollapsedNum, NumClauses);
  1735. }
  1736. OMPParallelSectionsDirective *OMPParallelSectionsDirective::Create(
  1737. const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
  1738. ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
  1739. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
  1740. llvm::alignOf<OMPClause *>());
  1741. void *Mem =
  1742. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
  1743. OMPParallelSectionsDirective *Dir =
  1744. new (Mem) OMPParallelSectionsDirective(StartLoc, EndLoc, Clauses.size());
  1745. Dir->setClauses(Clauses);
  1746. Dir->setAssociatedStmt(AssociatedStmt);
  1747. return Dir;
  1748. }
  1749. OMPParallelSectionsDirective *
  1750. OMPParallelSectionsDirective::CreateEmpty(const ASTContext &C,
  1751. unsigned NumClauses, EmptyShell) {
  1752. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
  1753. llvm::alignOf<OMPClause *>());
  1754. void *Mem =
  1755. C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
  1756. return new (Mem) OMPParallelSectionsDirective(NumClauses);
  1757. }
  1758. OMPTaskDirective *OMPTaskDirective::Create(const ASTContext &C,
  1759. SourceLocation StartLoc,
  1760. SourceLocation EndLoc,
  1761. ArrayRef<OMPClause *> Clauses,
  1762. Stmt *AssociatedStmt) {
  1763. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
  1764. llvm::alignOf<OMPClause *>());
  1765. void *Mem =
  1766. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
  1767. OMPTaskDirective *Dir =
  1768. new (Mem) OMPTaskDirective(StartLoc, EndLoc, Clauses.size());
  1769. Dir->setClauses(Clauses);
  1770. Dir->setAssociatedStmt(AssociatedStmt);
  1771. return Dir;
  1772. }
  1773. OMPTaskDirective *OMPTaskDirective::CreateEmpty(const ASTContext &C,
  1774. unsigned NumClauses,
  1775. EmptyShell) {
  1776. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
  1777. llvm::alignOf<OMPClause *>());
  1778. void *Mem =
  1779. C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
  1780. return new (Mem) OMPTaskDirective(NumClauses);
  1781. }
  1782. OMPTaskyieldDirective *OMPTaskyieldDirective::Create(const ASTContext &C,
  1783. SourceLocation StartLoc,
  1784. SourceLocation EndLoc) {
  1785. void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
  1786. OMPTaskyieldDirective *Dir =
  1787. new (Mem) OMPTaskyieldDirective(StartLoc, EndLoc);
  1788. return Dir;
  1789. }
  1790. OMPTaskyieldDirective *OMPTaskyieldDirective::CreateEmpty(const ASTContext &C,
  1791. EmptyShell) {
  1792. void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
  1793. return new (Mem) OMPTaskyieldDirective();
  1794. }
  1795. OMPBarrierDirective *OMPBarrierDirective::Create(const ASTContext &C,
  1796. SourceLocation StartLoc,
  1797. SourceLocation EndLoc) {
  1798. void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
  1799. OMPBarrierDirective *Dir = new (Mem) OMPBarrierDirective(StartLoc, EndLoc);
  1800. return Dir;
  1801. }
  1802. OMPBarrierDirective *OMPBarrierDirective::CreateEmpty(const ASTContext &C,
  1803. EmptyShell) {
  1804. void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
  1805. return new (Mem) OMPBarrierDirective();
  1806. }
  1807. OMPTaskwaitDirective *OMPTaskwaitDirective::Create(const ASTContext &C,
  1808. SourceLocation StartLoc,
  1809. SourceLocation EndLoc) {
  1810. void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
  1811. OMPTaskwaitDirective *Dir = new (Mem) OMPTaskwaitDirective(StartLoc, EndLoc);
  1812. return Dir;
  1813. }
  1814. OMPTaskwaitDirective *OMPTaskwaitDirective::CreateEmpty(const ASTContext &C,
  1815. EmptyShell) {
  1816. void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
  1817. return new (Mem) OMPTaskwaitDirective();
  1818. }
  1819. OMPTaskgroupDirective *OMPTaskgroupDirective::Create(const ASTContext &C,
  1820. SourceLocation StartLoc,
  1821. SourceLocation EndLoc,
  1822. Stmt *AssociatedStmt) {
  1823. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskgroupDirective),
  1824. llvm::alignOf<Stmt *>());
  1825. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1826. OMPTaskgroupDirective *Dir =
  1827. new (Mem) OMPTaskgroupDirective(StartLoc, EndLoc);
  1828. Dir->setAssociatedStmt(AssociatedStmt);
  1829. return Dir;
  1830. }
  1831. OMPTaskgroupDirective *OMPTaskgroupDirective::CreateEmpty(const ASTContext &C,
  1832. EmptyShell) {
  1833. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskgroupDirective),
  1834. llvm::alignOf<Stmt *>());
  1835. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1836. return new (Mem) OMPTaskgroupDirective();
  1837. }
  1838. OMPCancellationPointDirective *OMPCancellationPointDirective::Create(
  1839. const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
  1840. OpenMPDirectiveKind CancelRegion) {
  1841. unsigned Size = llvm::RoundUpToAlignment(
  1842. sizeof(OMPCancellationPointDirective), llvm::alignOf<Stmt *>());
  1843. void *Mem = C.Allocate(Size);
  1844. OMPCancellationPointDirective *Dir =
  1845. new (Mem) OMPCancellationPointDirective(StartLoc, EndLoc);
  1846. Dir->setCancelRegion(CancelRegion);
  1847. return Dir;
  1848. }
  1849. OMPCancellationPointDirective *
  1850. OMPCancellationPointDirective::CreateEmpty(const ASTContext &C, EmptyShell) {
  1851. unsigned Size = llvm::RoundUpToAlignment(
  1852. sizeof(OMPCancellationPointDirective), llvm::alignOf<Stmt *>());
  1853. void *Mem = C.Allocate(Size);
  1854. return new (Mem) OMPCancellationPointDirective();
  1855. }
  1856. OMPCancelDirective *
  1857. OMPCancelDirective::Create(const ASTContext &C, SourceLocation StartLoc,
  1858. SourceLocation EndLoc,
  1859. OpenMPDirectiveKind CancelRegion) {
  1860. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCancelDirective),
  1861. llvm::alignOf<Stmt *>());
  1862. void *Mem = C.Allocate(Size);
  1863. OMPCancelDirective *Dir = new (Mem) OMPCancelDirective(StartLoc, EndLoc);
  1864. Dir->setCancelRegion(CancelRegion);
  1865. return Dir;
  1866. }
  1867. OMPCancelDirective *OMPCancelDirective::CreateEmpty(const ASTContext &C,
  1868. EmptyShell) {
  1869. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCancelDirective),
  1870. llvm::alignOf<Stmt *>());
  1871. void *Mem = C.Allocate(Size);
  1872. return new (Mem) OMPCancelDirective();
  1873. }
  1874. OMPFlushDirective *OMPFlushDirective::Create(const ASTContext &C,
  1875. SourceLocation StartLoc,
  1876. SourceLocation EndLoc,
  1877. ArrayRef<OMPClause *> Clauses) {
  1878. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
  1879. llvm::alignOf<OMPClause *>());
  1880. void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size());
  1881. OMPFlushDirective *Dir =
  1882. new (Mem) OMPFlushDirective(StartLoc, EndLoc, Clauses.size());
  1883. Dir->setClauses(Clauses);
  1884. return Dir;
  1885. }
  1886. OMPFlushDirective *OMPFlushDirective::CreateEmpty(const ASTContext &C,
  1887. unsigned NumClauses,
  1888. EmptyShell) {
  1889. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
  1890. llvm::alignOf<OMPClause *>());
  1891. void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses);
  1892. return new (Mem) OMPFlushDirective(NumClauses);
  1893. }
  1894. OMPOrderedDirective *OMPOrderedDirective::Create(const ASTContext &C,
  1895. SourceLocation StartLoc,
  1896. SourceLocation EndLoc,
  1897. Stmt *AssociatedStmt) {
  1898. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
  1899. llvm::alignOf<Stmt *>());
  1900. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1901. OMPOrderedDirective *Dir = new (Mem) OMPOrderedDirective(StartLoc, EndLoc);
  1902. Dir->setAssociatedStmt(AssociatedStmt);
  1903. return Dir;
  1904. }
  1905. OMPOrderedDirective *OMPOrderedDirective::CreateEmpty(const ASTContext &C,
  1906. EmptyShell) {
  1907. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
  1908. llvm::alignOf<Stmt *>());
  1909. void *Mem = C.Allocate(Size + sizeof(Stmt *));
  1910. return new (Mem) OMPOrderedDirective();
  1911. }
  1912. OMPAtomicDirective *OMPAtomicDirective::Create(
  1913. const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
  1914. ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt, Expr *X, Expr *V,
  1915. Expr *E, Expr *UE, bool IsXLHSInRHSPart, bool IsPostfixUpdate) {
  1916. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
  1917. llvm::alignOf<OMPClause *>());
  1918. void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
  1919. 5 * sizeof(Stmt *));
  1920. OMPAtomicDirective *Dir =
  1921. new (Mem) OMPAtomicDirective(StartLoc, EndLoc, Clauses.size());
  1922. Dir->setClauses(Clauses);
  1923. Dir->setAssociatedStmt(AssociatedStmt);
  1924. Dir->setX(X);
  1925. Dir->setV(V);
  1926. Dir->setExpr(E);
  1927. Dir->setUpdateExpr(UE);
  1928. Dir->IsXLHSInRHSPart = IsXLHSInRHSPart;
  1929. Dir->IsPostfixUpdate = IsPostfixUpdate;
  1930. return Dir;
  1931. }
  1932. OMPAtomicDirective *OMPAtomicDirective::CreateEmpty(const ASTContext &C,
  1933. unsigned NumClauses,
  1934. EmptyShell) {
  1935. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
  1936. llvm::alignOf<OMPClause *>());
  1937. void *Mem =
  1938. C.Allocate(Size + sizeof(OMPClause *) * NumClauses + 5 * sizeof(Stmt *));
  1939. return new (Mem) OMPAtomicDirective(NumClauses);
  1940. }
  1941. OMPTargetDirective *OMPTargetDirective::Create(const ASTContext &C,
  1942. SourceLocation StartLoc,
  1943. SourceLocation EndLoc,
  1944. ArrayRef<OMPClause *> Clauses,
  1945. Stmt *AssociatedStmt) {
  1946. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
  1947. llvm::alignOf<OMPClause *>());
  1948. void *Mem =
  1949. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
  1950. OMPTargetDirective *Dir =
  1951. new (Mem) OMPTargetDirective(StartLoc, EndLoc, Clauses.size());
  1952. Dir->setClauses(Clauses);
  1953. Dir->setAssociatedStmt(AssociatedStmt);
  1954. return Dir;
  1955. }
  1956. OMPTargetDirective *OMPTargetDirective::CreateEmpty(const ASTContext &C,
  1957. unsigned NumClauses,
  1958. EmptyShell) {
  1959. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
  1960. llvm::alignOf<OMPClause *>());
  1961. void *Mem =
  1962. C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
  1963. return new (Mem) OMPTargetDirective(NumClauses);
  1964. }
  1965. OMPTeamsDirective *OMPTeamsDirective::Create(const ASTContext &C,
  1966. SourceLocation StartLoc,
  1967. SourceLocation EndLoc,
  1968. ArrayRef<OMPClause *> Clauses,
  1969. Stmt *AssociatedStmt) {
  1970. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
  1971. llvm::alignOf<OMPClause *>());
  1972. void *Mem =
  1973. C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
  1974. OMPTeamsDirective *Dir =
  1975. new (Mem) OMPTeamsDirective(StartLoc, EndLoc, Clauses.size());
  1976. Dir->setClauses(Clauses);
  1977. Dir->setAssociatedStmt(AssociatedStmt);
  1978. return Dir;
  1979. }
  1980. OMPTeamsDirective *OMPTeamsDirective::CreateEmpty(const ASTContext &C,
  1981. unsigned NumClauses,
  1982. EmptyShell) {
  1983. unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
  1984. llvm::alignOf<OMPClause *>());
  1985. void *Mem =
  1986. C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
  1987. return new (Mem) OMPTeamsDirective(NumClauses);
  1988. }