1 //===- OpenMPClause.cpp - Classes for OpenMP clauses ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/OpenMPClause.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclOpenMP.h"
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include <algorithm>
22 #include <cassert>
23 
24 using namespace clang;
25 
children()26 OMPClause::child_range OMPClause::children() {
27   switch (getClauseKind()) {
28   default:
29     break;
30 #define OPENMP_CLAUSE(Name, Class)                                             \
31   case OMPC_##Name:                                                            \
32     return static_cast<Class *>(this)->children();
33 #include "clang/Basic/OpenMPKinds.def"
34   }
35   llvm_unreachable("unknown OMPClause");
36 }
37 
used_children()38 OMPClause::child_range OMPClause::used_children() {
39   switch (getClauseKind()) {
40 #define OPENMP_CLAUSE(Name, Class)                                             \
41   case OMPC_##Name:                                                            \
42     return static_cast<Class *>(this)->used_children();
43 #include "clang/Basic/OpenMPKinds.def"
44   case OMPC_threadprivate:
45   case OMPC_uniform:
46   case OMPC_unknown:
47     break;
48   }
49   llvm_unreachable("unknown OMPClause");
50 }
51 
get(OMPClause * C)52 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
53   auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
54   return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
55 }
56 
get(const OMPClause * C)57 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
58   switch (C->getClauseKind()) {
59   case OMPC_schedule:
60     return static_cast<const OMPScheduleClause *>(C);
61   case OMPC_dist_schedule:
62     return static_cast<const OMPDistScheduleClause *>(C);
63   case OMPC_firstprivate:
64     return static_cast<const OMPFirstprivateClause *>(C);
65   case OMPC_lastprivate:
66     return static_cast<const OMPLastprivateClause *>(C);
67   case OMPC_reduction:
68     return static_cast<const OMPReductionClause *>(C);
69   case OMPC_task_reduction:
70     return static_cast<const OMPTaskReductionClause *>(C);
71   case OMPC_in_reduction:
72     return static_cast<const OMPInReductionClause *>(C);
73   case OMPC_linear:
74     return static_cast<const OMPLinearClause *>(C);
75   case OMPC_if:
76     return static_cast<const OMPIfClause *>(C);
77   case OMPC_num_threads:
78     return static_cast<const OMPNumThreadsClause *>(C);
79   case OMPC_num_teams:
80     return static_cast<const OMPNumTeamsClause *>(C);
81   case OMPC_thread_limit:
82     return static_cast<const OMPThreadLimitClause *>(C);
83   case OMPC_device:
84     return static_cast<const OMPDeviceClause *>(C);
85   case OMPC_default:
86   case OMPC_proc_bind:
87   case OMPC_final:
88   case OMPC_safelen:
89   case OMPC_simdlen:
90   case OMPC_allocator:
91   case OMPC_allocate:
92   case OMPC_collapse:
93   case OMPC_private:
94   case OMPC_shared:
95   case OMPC_aligned:
96   case OMPC_copyin:
97   case OMPC_copyprivate:
98   case OMPC_ordered:
99   case OMPC_nowait:
100   case OMPC_untied:
101   case OMPC_mergeable:
102   case OMPC_threadprivate:
103   case OMPC_flush:
104   case OMPC_read:
105   case OMPC_write:
106   case OMPC_update:
107   case OMPC_capture:
108   case OMPC_seq_cst:
109   case OMPC_depend:
110   case OMPC_threads:
111   case OMPC_simd:
112   case OMPC_map:
113   case OMPC_priority:
114   case OMPC_grainsize:
115   case OMPC_nogroup:
116   case OMPC_num_tasks:
117   case OMPC_hint:
118   case OMPC_defaultmap:
119   case OMPC_unknown:
120   case OMPC_uniform:
121   case OMPC_to:
122   case OMPC_from:
123   case OMPC_use_device_ptr:
124   case OMPC_is_device_ptr:
125   case OMPC_unified_address:
126   case OMPC_unified_shared_memory:
127   case OMPC_reverse_offload:
128   case OMPC_dynamic_allocators:
129   case OMPC_atomic_default_mem_order:
130     break;
131   }
132 
133   return nullptr;
134 }
135 
get(OMPClause * C)136 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
137   auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
138   return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
139 }
140 
get(const OMPClause * C)141 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
142   switch (C->getClauseKind()) {
143   case OMPC_lastprivate:
144     return static_cast<const OMPLastprivateClause *>(C);
145   case OMPC_reduction:
146     return static_cast<const OMPReductionClause *>(C);
147   case OMPC_task_reduction:
148     return static_cast<const OMPTaskReductionClause *>(C);
149   case OMPC_in_reduction:
150     return static_cast<const OMPInReductionClause *>(C);
151   case OMPC_linear:
152     return static_cast<const OMPLinearClause *>(C);
153   case OMPC_schedule:
154   case OMPC_dist_schedule:
155   case OMPC_firstprivate:
156   case OMPC_default:
157   case OMPC_proc_bind:
158   case OMPC_if:
159   case OMPC_final:
160   case OMPC_num_threads:
161   case OMPC_safelen:
162   case OMPC_simdlen:
163   case OMPC_allocator:
164   case OMPC_allocate:
165   case OMPC_collapse:
166   case OMPC_private:
167   case OMPC_shared:
168   case OMPC_aligned:
169   case OMPC_copyin:
170   case OMPC_copyprivate:
171   case OMPC_ordered:
172   case OMPC_nowait:
173   case OMPC_untied:
174   case OMPC_mergeable:
175   case OMPC_threadprivate:
176   case OMPC_flush:
177   case OMPC_read:
178   case OMPC_write:
179   case OMPC_update:
180   case OMPC_capture:
181   case OMPC_seq_cst:
182   case OMPC_depend:
183   case OMPC_device:
184   case OMPC_threads:
185   case OMPC_simd:
186   case OMPC_map:
187   case OMPC_num_teams:
188   case OMPC_thread_limit:
189   case OMPC_priority:
190   case OMPC_grainsize:
191   case OMPC_nogroup:
192   case OMPC_num_tasks:
193   case OMPC_hint:
194   case OMPC_defaultmap:
195   case OMPC_unknown:
196   case OMPC_uniform:
197   case OMPC_to:
198   case OMPC_from:
199   case OMPC_use_device_ptr:
200   case OMPC_is_device_ptr:
201   case OMPC_unified_address:
202   case OMPC_unified_shared_memory:
203   case OMPC_reverse_offload:
204   case OMPC_dynamic_allocators:
205   case OMPC_atomic_default_mem_order:
206     break;
207   }
208 
209   return nullptr;
210 }
211 
212 /// Gets the address of the original, non-captured, expression used in the
213 /// clause as the preinitializer.
getAddrOfExprAsWritten(Stmt * S)214 static Stmt **getAddrOfExprAsWritten(Stmt *S) {
215   if (!S)
216     return nullptr;
217   if (auto *DS = dyn_cast<DeclStmt>(S)) {
218     assert(DS->isSingleDecl() && "Only single expression must be captured.");
219     if (auto *OED = dyn_cast<OMPCapturedExprDecl>(DS->getSingleDecl()))
220       return OED->getInitAddress();
221   }
222   return nullptr;
223 }
224 
used_children()225 OMPClause::child_range OMPIfClause::used_children() {
226   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
227     return child_range(C, C + 1);
228   return child_range(&Condition, &Condition + 1);
229 }
230 
Create(const ASTContext & C,Expr * Num,unsigned NumLoops,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)231 OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
232                                            unsigned NumLoops,
233                                            SourceLocation StartLoc,
234                                            SourceLocation LParenLoc,
235                                            SourceLocation EndLoc) {
236   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
237   auto *Clause =
238       new (Mem) OMPOrderedClause(Num, NumLoops, StartLoc, LParenLoc, EndLoc);
239   for (unsigned I = 0; I < NumLoops; ++I) {
240     Clause->setLoopNumIterations(I, nullptr);
241     Clause->setLoopCounter(I, nullptr);
242   }
243   return Clause;
244 }
245 
CreateEmpty(const ASTContext & C,unsigned NumLoops)246 OMPOrderedClause *OMPOrderedClause::CreateEmpty(const ASTContext &C,
247                                                 unsigned NumLoops) {
248   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
249   auto *Clause = new (Mem) OMPOrderedClause(NumLoops);
250   for (unsigned I = 0; I < NumLoops; ++I) {
251     Clause->setLoopNumIterations(I, nullptr);
252     Clause->setLoopCounter(I, nullptr);
253   }
254   return Clause;
255 }
256 
setLoopNumIterations(unsigned NumLoop,Expr * NumIterations)257 void OMPOrderedClause::setLoopNumIterations(unsigned NumLoop,
258                                             Expr *NumIterations) {
259   assert(NumLoop < NumberOfLoops && "out of loops number.");
260   getTrailingObjects<Expr *>()[NumLoop] = NumIterations;
261 }
262 
getLoopNumIterations() const263 ArrayRef<Expr *> OMPOrderedClause::getLoopNumIterations() const {
264   return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumberOfLoops);
265 }
266 
setLoopCounter(unsigned NumLoop,Expr * Counter)267 void OMPOrderedClause::setLoopCounter(unsigned NumLoop, Expr *Counter) {
268   assert(NumLoop < NumberOfLoops && "out of loops number.");
269   getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop] = Counter;
270 }
271 
getLoopCounter(unsigned NumLoop)272 Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) {
273   assert(NumLoop < NumberOfLoops && "out of loops number.");
274   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
275 }
276 
getLoopCounter(unsigned NumLoop) const277 const Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) const {
278   assert(NumLoop < NumberOfLoops && "out of loops number.");
279   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
280 }
281 
setPrivateCopies(ArrayRef<Expr * > VL)282 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
283   assert(VL.size() == varlist_size() &&
284          "Number of private copies is not the same as the preallocated buffer");
285   std::copy(VL.begin(), VL.end(), varlist_end());
286 }
287 
288 OMPPrivateClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > PrivateVL)289 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
290                          SourceLocation LParenLoc, SourceLocation EndLoc,
291                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
292   // Allocate space for private variables and initializer expressions.
293   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
294   OMPPrivateClause *Clause =
295       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
296   Clause->setVarRefs(VL);
297   Clause->setPrivateCopies(PrivateVL);
298   return Clause;
299 }
300 
CreateEmpty(const ASTContext & C,unsigned N)301 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
302                                                 unsigned N) {
303   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
304   return new (Mem) OMPPrivateClause(N);
305 }
306 
setPrivateCopies(ArrayRef<Expr * > VL)307 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
308   assert(VL.size() == varlist_size() &&
309          "Number of private copies is not the same as the preallocated buffer");
310   std::copy(VL.begin(), VL.end(), varlist_end());
311 }
312 
setInits(ArrayRef<Expr * > VL)313 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
314   assert(VL.size() == varlist_size() &&
315          "Number of inits is not the same as the preallocated buffer");
316   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
317 }
318 
319 OMPFirstprivateClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > PrivateVL,ArrayRef<Expr * > InitVL,Stmt * PreInit)320 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
321                               SourceLocation LParenLoc, SourceLocation EndLoc,
322                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
323                               ArrayRef<Expr *> InitVL, Stmt *PreInit) {
324   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
325   OMPFirstprivateClause *Clause =
326       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
327   Clause->setVarRefs(VL);
328   Clause->setPrivateCopies(PrivateVL);
329   Clause->setInits(InitVL);
330   Clause->setPreInitStmt(PreInit);
331   return Clause;
332 }
333 
CreateEmpty(const ASTContext & C,unsigned N)334 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
335                                                           unsigned N) {
336   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
337   return new (Mem) OMPFirstprivateClause(N);
338 }
339 
setPrivateCopies(ArrayRef<Expr * > PrivateCopies)340 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
341   assert(PrivateCopies.size() == varlist_size() &&
342          "Number of private copies is not the same as the preallocated buffer");
343   std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
344 }
345 
setSourceExprs(ArrayRef<Expr * > SrcExprs)346 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
347   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
348                                               "not the same as the "
349                                               "preallocated buffer");
350   std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
351 }
352 
setDestinationExprs(ArrayRef<Expr * > DstExprs)353 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
354   assert(DstExprs.size() == varlist_size() && "Number of destination "
355                                               "expressions is not the same as "
356                                               "the preallocated buffer");
357   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
358 }
359 
setAssignmentOps(ArrayRef<Expr * > AssignmentOps)360 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
361   assert(AssignmentOps.size() == varlist_size() &&
362          "Number of assignment expressions is not the same as the preallocated "
363          "buffer");
364   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
365             getDestinationExprs().end());
366 }
367 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > SrcExprs,ArrayRef<Expr * > DstExprs,ArrayRef<Expr * > AssignmentOps,Stmt * PreInit,Expr * PostUpdate)368 OMPLastprivateClause *OMPLastprivateClause::Create(
369     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
370     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
371     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
372     Expr *PostUpdate) {
373   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
374   OMPLastprivateClause *Clause =
375       new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
376   Clause->setVarRefs(VL);
377   Clause->setSourceExprs(SrcExprs);
378   Clause->setDestinationExprs(DstExprs);
379   Clause->setAssignmentOps(AssignmentOps);
380   Clause->setPreInitStmt(PreInit);
381   Clause->setPostUpdateExpr(PostUpdate);
382   return Clause;
383 }
384 
CreateEmpty(const ASTContext & C,unsigned N)385 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
386                                                         unsigned N) {
387   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
388   return new (Mem) OMPLastprivateClause(N);
389 }
390 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)391 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
392                                          SourceLocation StartLoc,
393                                          SourceLocation LParenLoc,
394                                          SourceLocation EndLoc,
395                                          ArrayRef<Expr *> VL) {
396   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
397   OMPSharedClause *Clause =
398       new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
399   Clause->setVarRefs(VL);
400   return Clause;
401 }
402 
CreateEmpty(const ASTContext & C,unsigned N)403 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
404   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
405   return new (Mem) OMPSharedClause(N);
406 }
407 
setPrivates(ArrayRef<Expr * > PL)408 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
409   assert(PL.size() == varlist_size() &&
410          "Number of privates is not the same as the preallocated buffer");
411   std::copy(PL.begin(), PL.end(), varlist_end());
412 }
413 
setInits(ArrayRef<Expr * > IL)414 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
415   assert(IL.size() == varlist_size() &&
416          "Number of inits is not the same as the preallocated buffer");
417   std::copy(IL.begin(), IL.end(), getPrivates().end());
418 }
419 
setUpdates(ArrayRef<Expr * > UL)420 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
421   assert(UL.size() == varlist_size() &&
422          "Number of updates is not the same as the preallocated buffer");
423   std::copy(UL.begin(), UL.end(), getInits().end());
424 }
425 
setFinals(ArrayRef<Expr * > FL)426 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
427   assert(FL.size() == varlist_size() &&
428          "Number of final updates is not the same as the preallocated buffer");
429   std::copy(FL.begin(), FL.end(), getUpdates().end());
430 }
431 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,OpenMPLinearClauseKind Modifier,SourceLocation ModifierLoc,SourceLocation ColonLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > PL,ArrayRef<Expr * > IL,Expr * Step,Expr * CalcStep,Stmt * PreInit,Expr * PostUpdate)432 OMPLinearClause *OMPLinearClause::Create(
433     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
434     OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
435     SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
436     ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
437     Stmt *PreInit, Expr *PostUpdate) {
438   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
439   // (Step and CalcStep).
440   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
441   OMPLinearClause *Clause = new (Mem) OMPLinearClause(
442       StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
443   Clause->setVarRefs(VL);
444   Clause->setPrivates(PL);
445   Clause->setInits(IL);
446   // Fill update and final expressions with zeroes, they are provided later,
447   // after the directive construction.
448   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
449             nullptr);
450   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
451             nullptr);
452   Clause->setStep(Step);
453   Clause->setCalcStep(CalcStep);
454   Clause->setPreInitStmt(PreInit);
455   Clause->setPostUpdateExpr(PostUpdate);
456   return Clause;
457 }
458 
CreateEmpty(const ASTContext & C,unsigned NumVars)459 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
460                                               unsigned NumVars) {
461   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
462   // (Step and CalcStep).
463   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
464   return new (Mem) OMPLinearClause(NumVars);
465 }
466 
467 OMPAlignedClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,Expr * A)468 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
469                          SourceLocation LParenLoc, SourceLocation ColonLoc,
470                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
471   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
472   OMPAlignedClause *Clause = new (Mem)
473       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
474   Clause->setVarRefs(VL);
475   Clause->setAlignment(A);
476   return Clause;
477 }
478 
CreateEmpty(const ASTContext & C,unsigned NumVars)479 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
480                                                 unsigned NumVars) {
481   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
482   return new (Mem) OMPAlignedClause(NumVars);
483 }
484 
setSourceExprs(ArrayRef<Expr * > SrcExprs)485 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
486   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
487                                               "not the same as the "
488                                               "preallocated buffer");
489   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
490 }
491 
setDestinationExprs(ArrayRef<Expr * > DstExprs)492 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
493   assert(DstExprs.size() == varlist_size() && "Number of destination "
494                                               "expressions is not the same as "
495                                               "the preallocated buffer");
496   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
497 }
498 
setAssignmentOps(ArrayRef<Expr * > AssignmentOps)499 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
500   assert(AssignmentOps.size() == varlist_size() &&
501          "Number of assignment expressions is not the same as the preallocated "
502          "buffer");
503   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
504             getDestinationExprs().end());
505 }
506 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > SrcExprs,ArrayRef<Expr * > DstExprs,ArrayRef<Expr * > AssignmentOps)507 OMPCopyinClause *OMPCopyinClause::Create(
508     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
509     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
510     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
511   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
512   OMPCopyinClause *Clause =
513       new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
514   Clause->setVarRefs(VL);
515   Clause->setSourceExprs(SrcExprs);
516   Clause->setDestinationExprs(DstExprs);
517   Clause->setAssignmentOps(AssignmentOps);
518   return Clause;
519 }
520 
CreateEmpty(const ASTContext & C,unsigned N)521 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
522   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
523   return new (Mem) OMPCopyinClause(N);
524 }
525 
setSourceExprs(ArrayRef<Expr * > SrcExprs)526 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
527   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
528                                               "not the same as the "
529                                               "preallocated buffer");
530   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
531 }
532 
setDestinationExprs(ArrayRef<Expr * > DstExprs)533 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
534   assert(DstExprs.size() == varlist_size() && "Number of destination "
535                                               "expressions is not the same as "
536                                               "the preallocated buffer");
537   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
538 }
539 
setAssignmentOps(ArrayRef<Expr * > AssignmentOps)540 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
541   assert(AssignmentOps.size() == varlist_size() &&
542          "Number of assignment expressions is not the same as the preallocated "
543          "buffer");
544   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
545             getDestinationExprs().end());
546 }
547 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > SrcExprs,ArrayRef<Expr * > DstExprs,ArrayRef<Expr * > AssignmentOps)548 OMPCopyprivateClause *OMPCopyprivateClause::Create(
549     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
550     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
551     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
552   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
553   OMPCopyprivateClause *Clause =
554       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
555   Clause->setVarRefs(VL);
556   Clause->setSourceExprs(SrcExprs);
557   Clause->setDestinationExprs(DstExprs);
558   Clause->setAssignmentOps(AssignmentOps);
559   return Clause;
560 }
561 
CreateEmpty(const ASTContext & C,unsigned N)562 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
563                                                         unsigned N) {
564   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
565   return new (Mem) OMPCopyprivateClause(N);
566 }
567 
setPrivates(ArrayRef<Expr * > Privates)568 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
569   assert(Privates.size() == varlist_size() &&
570          "Number of private copies is not the same as the preallocated buffer");
571   std::copy(Privates.begin(), Privates.end(), varlist_end());
572 }
573 
setLHSExprs(ArrayRef<Expr * > LHSExprs)574 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
575   assert(
576       LHSExprs.size() == varlist_size() &&
577       "Number of LHS expressions is not the same as the preallocated buffer");
578   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
579 }
580 
setRHSExprs(ArrayRef<Expr * > RHSExprs)581 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
582   assert(
583       RHSExprs.size() == varlist_size() &&
584       "Number of RHS expressions is not the same as the preallocated buffer");
585   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
586 }
587 
setReductionOps(ArrayRef<Expr * > ReductionOps)588 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
589   assert(ReductionOps.size() == varlist_size() && "Number of reduction "
590                                                   "expressions is not the same "
591                                                   "as the preallocated buffer");
592   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
593 }
594 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,SourceLocation ColonLoc,ArrayRef<Expr * > VL,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,ArrayRef<Expr * > Privates,ArrayRef<Expr * > LHSExprs,ArrayRef<Expr * > RHSExprs,ArrayRef<Expr * > ReductionOps,Stmt * PreInit,Expr * PostUpdate)595 OMPReductionClause *OMPReductionClause::Create(
596     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
597     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
598     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
599     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
600     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
601     Expr *PostUpdate) {
602   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
603   OMPReductionClause *Clause = new (Mem) OMPReductionClause(
604       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
605   Clause->setVarRefs(VL);
606   Clause->setPrivates(Privates);
607   Clause->setLHSExprs(LHSExprs);
608   Clause->setRHSExprs(RHSExprs);
609   Clause->setReductionOps(ReductionOps);
610   Clause->setPreInitStmt(PreInit);
611   Clause->setPostUpdateExpr(PostUpdate);
612   return Clause;
613 }
614 
CreateEmpty(const ASTContext & C,unsigned N)615 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
616                                                     unsigned N) {
617   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
618   return new (Mem) OMPReductionClause(N);
619 }
620 
setPrivates(ArrayRef<Expr * > Privates)621 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
622   assert(Privates.size() == varlist_size() &&
623          "Number of private copies is not the same as the preallocated buffer");
624   std::copy(Privates.begin(), Privates.end(), varlist_end());
625 }
626 
setLHSExprs(ArrayRef<Expr * > LHSExprs)627 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
628   assert(
629       LHSExprs.size() == varlist_size() &&
630       "Number of LHS expressions is not the same as the preallocated buffer");
631   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
632 }
633 
setRHSExprs(ArrayRef<Expr * > RHSExprs)634 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
635   assert(
636       RHSExprs.size() == varlist_size() &&
637       "Number of RHS expressions is not the same as the preallocated buffer");
638   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
639 }
640 
setReductionOps(ArrayRef<Expr * > ReductionOps)641 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
642   assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
643                                                   "expressions is not the same "
644                                                   "as the preallocated buffer");
645   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
646 }
647 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,SourceLocation ColonLoc,ArrayRef<Expr * > VL,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,ArrayRef<Expr * > Privates,ArrayRef<Expr * > LHSExprs,ArrayRef<Expr * > RHSExprs,ArrayRef<Expr * > ReductionOps,Stmt * PreInit,Expr * PostUpdate)648 OMPTaskReductionClause *OMPTaskReductionClause::Create(
649     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
650     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
651     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
652     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
653     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
654     Expr *PostUpdate) {
655   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
656   OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
657       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
658   Clause->setVarRefs(VL);
659   Clause->setPrivates(Privates);
660   Clause->setLHSExprs(LHSExprs);
661   Clause->setRHSExprs(RHSExprs);
662   Clause->setReductionOps(ReductionOps);
663   Clause->setPreInitStmt(PreInit);
664   Clause->setPostUpdateExpr(PostUpdate);
665   return Clause;
666 }
667 
CreateEmpty(const ASTContext & C,unsigned N)668 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
669                                                             unsigned N) {
670   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
671   return new (Mem) OMPTaskReductionClause(N);
672 }
673 
setPrivates(ArrayRef<Expr * > Privates)674 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
675   assert(Privates.size() == varlist_size() &&
676          "Number of private copies is not the same as the preallocated buffer");
677   std::copy(Privates.begin(), Privates.end(), varlist_end());
678 }
679 
setLHSExprs(ArrayRef<Expr * > LHSExprs)680 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
681   assert(
682       LHSExprs.size() == varlist_size() &&
683       "Number of LHS expressions is not the same as the preallocated buffer");
684   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
685 }
686 
setRHSExprs(ArrayRef<Expr * > RHSExprs)687 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
688   assert(
689       RHSExprs.size() == varlist_size() &&
690       "Number of RHS expressions is not the same as the preallocated buffer");
691   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
692 }
693 
setReductionOps(ArrayRef<Expr * > ReductionOps)694 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
695   assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
696                                                   "expressions is not the same "
697                                                   "as the preallocated buffer");
698   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
699 }
700 
setTaskgroupDescriptors(ArrayRef<Expr * > TaskgroupDescriptors)701 void OMPInReductionClause::setTaskgroupDescriptors(
702     ArrayRef<Expr *> TaskgroupDescriptors) {
703   assert(TaskgroupDescriptors.size() == varlist_size() &&
704          "Number of in reduction descriptors is not the same as the "
705          "preallocated buffer");
706   std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
707             getReductionOps().end());
708 }
709 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,SourceLocation ColonLoc,ArrayRef<Expr * > VL,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,ArrayRef<Expr * > Privates,ArrayRef<Expr * > LHSExprs,ArrayRef<Expr * > RHSExprs,ArrayRef<Expr * > ReductionOps,ArrayRef<Expr * > TaskgroupDescriptors,Stmt * PreInit,Expr * PostUpdate)710 OMPInReductionClause *OMPInReductionClause::Create(
711     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
712     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
713     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
714     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
715     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
716     ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
717   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
718   OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
719       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
720   Clause->setVarRefs(VL);
721   Clause->setPrivates(Privates);
722   Clause->setLHSExprs(LHSExprs);
723   Clause->setRHSExprs(RHSExprs);
724   Clause->setReductionOps(ReductionOps);
725   Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
726   Clause->setPreInitStmt(PreInit);
727   Clause->setPostUpdateExpr(PostUpdate);
728   return Clause;
729 }
730 
CreateEmpty(const ASTContext & C,unsigned N)731 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
732                                                         unsigned N) {
733   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
734   return new (Mem) OMPInReductionClause(N);
735 }
736 
737 OMPAllocateClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,Expr * Allocator,SourceLocation ColonLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)738 OMPAllocateClause::Create(const ASTContext &C, SourceLocation StartLoc,
739                           SourceLocation LParenLoc, Expr *Allocator,
740                           SourceLocation ColonLoc, SourceLocation EndLoc,
741                           ArrayRef<Expr *> VL) {
742   // Allocate space for private variables and initializer expressions.
743   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
744   auto *Clause = new (Mem) OMPAllocateClause(StartLoc, LParenLoc, Allocator,
745                                              ColonLoc, EndLoc, VL.size());
746   Clause->setVarRefs(VL);
747   return Clause;
748 }
749 
CreateEmpty(const ASTContext & C,unsigned N)750 OMPAllocateClause *OMPAllocateClause::CreateEmpty(const ASTContext &C,
751                                                   unsigned N) {
752   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
753   return new (Mem) OMPAllocateClause(N);
754 }
755 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)756 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
757                                        SourceLocation StartLoc,
758                                        SourceLocation LParenLoc,
759                                        SourceLocation EndLoc,
760                                        ArrayRef<Expr *> VL) {
761   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
762   OMPFlushClause *Clause =
763       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
764   Clause->setVarRefs(VL);
765   return Clause;
766 }
767 
CreateEmpty(const ASTContext & C,unsigned N)768 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
769   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
770   return new (Mem) OMPFlushClause(N);
771 }
772 
773 OMPDependClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,OpenMPDependClauseKind DepKind,SourceLocation DepLoc,SourceLocation ColonLoc,ArrayRef<Expr * > VL,unsigned NumLoops)774 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
775                         SourceLocation LParenLoc, SourceLocation EndLoc,
776                         OpenMPDependClauseKind DepKind, SourceLocation DepLoc,
777                         SourceLocation ColonLoc, ArrayRef<Expr *> VL,
778                         unsigned NumLoops) {
779   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + NumLoops));
780   OMPDependClause *Clause = new (Mem)
781       OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
782   Clause->setVarRefs(VL);
783   Clause->setDependencyKind(DepKind);
784   Clause->setDependencyLoc(DepLoc);
785   Clause->setColonLoc(ColonLoc);
786   for (unsigned I = 0 ; I < NumLoops; ++I)
787     Clause->setLoopData(I, nullptr);
788   return Clause;
789 }
790 
CreateEmpty(const ASTContext & C,unsigned N,unsigned NumLoops)791 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N,
792                                               unsigned NumLoops) {
793   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + NumLoops));
794   return new (Mem) OMPDependClause(N, NumLoops);
795 }
796 
setLoopData(unsigned NumLoop,Expr * Cnt)797 void OMPDependClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
798   assert((getDependencyKind() == OMPC_DEPEND_sink ||
799           getDependencyKind() == OMPC_DEPEND_source) &&
800          NumLoop < NumLoops &&
801          "Expected sink or source depend + loop index must be less number of "
802          "loops.");
803   auto It = std::next(getVarRefs().end(), NumLoop);
804   *It = Cnt;
805 }
806 
getLoopData(unsigned NumLoop)807 Expr *OMPDependClause::getLoopData(unsigned NumLoop) {
808   assert((getDependencyKind() == OMPC_DEPEND_sink ||
809           getDependencyKind() == OMPC_DEPEND_source) &&
810          NumLoop < NumLoops &&
811          "Expected sink or source depend + loop index must be less number of "
812          "loops.");
813   auto It = std::next(getVarRefs().end(), NumLoop);
814   return *It;
815 }
816 
getLoopData(unsigned NumLoop) const817 const Expr *OMPDependClause::getLoopData(unsigned NumLoop) const {
818   assert((getDependencyKind() == OMPC_DEPEND_sink ||
819           getDependencyKind() == OMPC_DEPEND_source) &&
820          NumLoop < NumLoops &&
821          "Expected sink or source depend + loop index must be less number of "
822          "loops.");
823   auto It = std::next(getVarRefs().end(), NumLoop);
824   return *It;
825 }
826 
getComponentsTotalNumber(MappableExprComponentListsRef ComponentLists)827 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
828     MappableExprComponentListsRef ComponentLists) {
829   unsigned TotalNum = 0u;
830   for (auto &C : ComponentLists)
831     TotalNum += C.size();
832   return TotalNum;
833 }
834 
getUniqueDeclarationsTotalNumber(ArrayRef<const ValueDecl * > Declarations)835 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
836     ArrayRef<const ValueDecl *> Declarations) {
837   unsigned TotalNum = 0u;
838   llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
839   for (const ValueDecl *D : Declarations) {
840     const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
841     if (Cache.count(VD))
842       continue;
843     ++TotalNum;
844     Cache.insert(VD);
845   }
846   return TotalNum;
847 }
848 
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists,ArrayRef<Expr * > UDMapperRefs,ArrayRef<OpenMPMapModifierKind> MapModifiers,ArrayRef<SourceLocation> MapModifiersLoc,NestedNameSpecifierLoc UDMQualifierLoc,DeclarationNameInfo MapperId,OpenMPMapClauseKind Type,bool TypeIsImplicit,SourceLocation TypeLoc)849 OMPMapClause *OMPMapClause::Create(
850     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
851     ArrayRef<ValueDecl *> Declarations,
852     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
853     ArrayRef<OpenMPMapModifierKind> MapModifiers,
854     ArrayRef<SourceLocation> MapModifiersLoc,
855     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId,
856     OpenMPMapClauseKind Type, bool TypeIsImplicit, SourceLocation TypeLoc) {
857   OMPMappableExprListSizeTy Sizes;
858   Sizes.NumVars = Vars.size();
859   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
860   Sizes.NumComponentLists = ComponentLists.size();
861   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
862 
863   // We need to allocate:
864   // 2 x NumVars x Expr* - we have an original list expression and an associated
865   // user-defined mapper for each clause list entry.
866   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
867   // with each component list.
868   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
869   // number of lists for each unique declaration and the size of each component
870   // list.
871   // NumComponents x MappableComponent - the total of all the components in all
872   // the lists.
873   void *Mem = C.Allocate(
874       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
875                        OMPClauseMappableExprCommon::MappableComponent>(
876           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
877           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
878           Sizes.NumComponents));
879   OMPMapClause *Clause = new (Mem)
880       OMPMapClause(MapModifiers, MapModifiersLoc, UDMQualifierLoc, MapperId,
881                    Type, TypeIsImplicit, TypeLoc, Locs, Sizes);
882 
883   Clause->setVarRefs(Vars);
884   Clause->setUDMapperRefs(UDMapperRefs);
885   Clause->setClauseInfo(Declarations, ComponentLists);
886   Clause->setMapType(Type);
887   Clause->setMapLoc(TypeLoc);
888   return Clause;
889 }
890 
891 OMPMapClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)892 OMPMapClause::CreateEmpty(const ASTContext &C,
893                           const OMPMappableExprListSizeTy &Sizes) {
894   void *Mem = C.Allocate(
895       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
896                        OMPClauseMappableExprCommon::MappableComponent>(
897           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
898           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
899           Sizes.NumComponents));
900   return new (Mem) OMPMapClause(Sizes);
901 }
902 
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists,ArrayRef<Expr * > UDMapperRefs,NestedNameSpecifierLoc UDMQualifierLoc,DeclarationNameInfo MapperId)903 OMPToClause *OMPToClause::Create(
904     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
905     ArrayRef<ValueDecl *> Declarations,
906     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
907     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
908   OMPMappableExprListSizeTy Sizes;
909   Sizes.NumVars = Vars.size();
910   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
911   Sizes.NumComponentLists = ComponentLists.size();
912   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
913 
914   // We need to allocate:
915   // 2 x NumVars x Expr* - we have an original list expression and an associated
916   // user-defined mapper for each clause list entry.
917   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
918   // with each component list.
919   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
920   // number of lists for each unique declaration and the size of each component
921   // list.
922   // NumComponents x MappableComponent - the total of all the components in all
923   // the lists.
924   void *Mem = C.Allocate(
925       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
926                        OMPClauseMappableExprCommon::MappableComponent>(
927           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
928           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
929           Sizes.NumComponents));
930 
931   auto *Clause = new (Mem) OMPToClause(UDMQualifierLoc, MapperId, Locs, Sizes);
932 
933   Clause->setVarRefs(Vars);
934   Clause->setUDMapperRefs(UDMapperRefs);
935   Clause->setClauseInfo(Declarations, ComponentLists);
936   return Clause;
937 }
938 
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)939 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C,
940                                       const OMPMappableExprListSizeTy &Sizes) {
941   void *Mem = C.Allocate(
942       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
943                        OMPClauseMappableExprCommon::MappableComponent>(
944           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
945           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
946           Sizes.NumComponents));
947   return new (Mem) OMPToClause(Sizes);
948 }
949 
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists,ArrayRef<Expr * > UDMapperRefs,NestedNameSpecifierLoc UDMQualifierLoc,DeclarationNameInfo MapperId)950 OMPFromClause *OMPFromClause::Create(
951     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
952     ArrayRef<ValueDecl *> Declarations,
953     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
954     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
955   OMPMappableExprListSizeTy Sizes;
956   Sizes.NumVars = Vars.size();
957   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
958   Sizes.NumComponentLists = ComponentLists.size();
959   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
960 
961   // We need to allocate:
962   // 2 x NumVars x Expr* - we have an original list expression and an associated
963   // user-defined mapper for each clause list entry.
964   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
965   // with each component list.
966   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
967   // number of lists for each unique declaration and the size of each component
968   // list.
969   // NumComponents x MappableComponent - the total of all the components in all
970   // the lists.
971   void *Mem = C.Allocate(
972       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
973                        OMPClauseMappableExprCommon::MappableComponent>(
974           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
975           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
976           Sizes.NumComponents));
977 
978   auto *Clause =
979       new (Mem) OMPFromClause(UDMQualifierLoc, MapperId, Locs, Sizes);
980 
981   Clause->setVarRefs(Vars);
982   Clause->setUDMapperRefs(UDMapperRefs);
983   Clause->setClauseInfo(Declarations, ComponentLists);
984   return Clause;
985 }
986 
987 OMPFromClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)988 OMPFromClause::CreateEmpty(const ASTContext &C,
989                            const OMPMappableExprListSizeTy &Sizes) {
990   void *Mem = C.Allocate(
991       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
992                        OMPClauseMappableExprCommon::MappableComponent>(
993           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
994           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
995           Sizes.NumComponents));
996   return new (Mem) OMPFromClause(Sizes);
997 }
998 
setPrivateCopies(ArrayRef<Expr * > VL)999 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1000   assert(VL.size() == varlist_size() &&
1001          "Number of private copies is not the same as the preallocated buffer");
1002   std::copy(VL.begin(), VL.end(), varlist_end());
1003 }
1004 
setInits(ArrayRef<Expr * > VL)1005 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
1006   assert(VL.size() == varlist_size() &&
1007          "Number of inits is not the same as the preallocated buffer");
1008   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
1009 }
1010 
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<Expr * > PrivateVars,ArrayRef<Expr * > Inits,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists)1011 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
1012     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1013     ArrayRef<Expr *> PrivateVars, ArrayRef<Expr *> Inits,
1014     ArrayRef<ValueDecl *> Declarations,
1015     MappableExprComponentListsRef ComponentLists) {
1016   OMPMappableExprListSizeTy Sizes;
1017   Sizes.NumVars = Vars.size();
1018   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1019   Sizes.NumComponentLists = ComponentLists.size();
1020   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1021 
1022   // We need to allocate:
1023   // 3 x NumVars x Expr* - we have an original list expression for each clause
1024   // list entry and an equal number of private copies and inits.
1025   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1026   // with each component list.
1027   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1028   // number of lists for each unique declaration and the size of each component
1029   // list.
1030   // NumComponents x MappableComponent - the total of all the components in all
1031   // the lists.
1032   void *Mem = C.Allocate(
1033       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1034                        OMPClauseMappableExprCommon::MappableComponent>(
1035           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1036           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1037           Sizes.NumComponents));
1038 
1039   OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(Locs, Sizes);
1040 
1041   Clause->setVarRefs(Vars);
1042   Clause->setPrivateCopies(PrivateVars);
1043   Clause->setInits(Inits);
1044   Clause->setClauseInfo(Declarations, ComponentLists);
1045   return Clause;
1046 }
1047 
1048 OMPUseDevicePtrClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1049 OMPUseDevicePtrClause::CreateEmpty(const ASTContext &C,
1050                                    const OMPMappableExprListSizeTy &Sizes) {
1051   void *Mem = C.Allocate(
1052       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1053                        OMPClauseMappableExprCommon::MappableComponent>(
1054           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1055           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1056           Sizes.NumComponents));
1057   return new (Mem) OMPUseDevicePtrClause(Sizes);
1058 }
1059 
1060 OMPIsDevicePtrClause *
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists)1061 OMPIsDevicePtrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1062                              ArrayRef<Expr *> Vars,
1063                              ArrayRef<ValueDecl *> Declarations,
1064                              MappableExprComponentListsRef ComponentLists) {
1065   OMPMappableExprListSizeTy Sizes;
1066   Sizes.NumVars = Vars.size();
1067   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1068   Sizes.NumComponentLists = ComponentLists.size();
1069   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1070 
1071   // We need to allocate:
1072   // NumVars x Expr* - we have an original list expression for each clause list
1073   // entry.
1074   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1075   // with each component list.
1076   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1077   // number of lists for each unique declaration and the size of each component
1078   // list.
1079   // NumComponents x MappableComponent - the total of all the components in all
1080   // the lists.
1081   void *Mem = C.Allocate(
1082       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1083                        OMPClauseMappableExprCommon::MappableComponent>(
1084           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1085           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1086           Sizes.NumComponents));
1087 
1088   OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(Locs, Sizes);
1089 
1090   Clause->setVarRefs(Vars);
1091   Clause->setClauseInfo(Declarations, ComponentLists);
1092   return Clause;
1093 }
1094 
1095 OMPIsDevicePtrClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1096 OMPIsDevicePtrClause::CreateEmpty(const ASTContext &C,
1097                                   const OMPMappableExprListSizeTy &Sizes) {
1098   void *Mem = C.Allocate(
1099       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1100                        OMPClauseMappableExprCommon::MappableComponent>(
1101           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1102           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1103           Sizes.NumComponents));
1104   return new (Mem) OMPIsDevicePtrClause(Sizes);
1105 }
1106 
1107 //===----------------------------------------------------------------------===//
1108 //  OpenMP clauses printing methods
1109 //===----------------------------------------------------------------------===//
1110 
VisitOMPIfClause(OMPIfClause * Node)1111 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
1112   OS << "if(";
1113   if (Node->getNameModifier() != OMPD_unknown)
1114     OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
1115   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1116   OS << ")";
1117 }
1118 
VisitOMPFinalClause(OMPFinalClause * Node)1119 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
1120   OS << "final(";
1121   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1122   OS << ")";
1123 }
1124 
VisitOMPNumThreadsClause(OMPNumThreadsClause * Node)1125 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
1126   OS << "num_threads(";
1127   Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
1128   OS << ")";
1129 }
1130 
VisitOMPSafelenClause(OMPSafelenClause * Node)1131 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
1132   OS << "safelen(";
1133   Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
1134   OS << ")";
1135 }
1136 
VisitOMPSimdlenClause(OMPSimdlenClause * Node)1137 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
1138   OS << "simdlen(";
1139   Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
1140   OS << ")";
1141 }
1142 
VisitOMPAllocatorClause(OMPAllocatorClause * Node)1143 void OMPClausePrinter::VisitOMPAllocatorClause(OMPAllocatorClause *Node) {
1144   OS << "allocator(";
1145   Node->getAllocator()->printPretty(OS, nullptr, Policy, 0);
1146   OS << ")";
1147 }
1148 
VisitOMPCollapseClause(OMPCollapseClause * Node)1149 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
1150   OS << "collapse(";
1151   Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
1152   OS << ")";
1153 }
1154 
VisitOMPDefaultClause(OMPDefaultClause * Node)1155 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
1156   OS << "default("
1157      << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
1158      << ")";
1159 }
1160 
VisitOMPProcBindClause(OMPProcBindClause * Node)1161 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
1162   OS << "proc_bind("
1163      << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
1164      << ")";
1165 }
1166 
VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *)1167 void OMPClausePrinter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {
1168   OS << "unified_address";
1169 }
1170 
VisitOMPUnifiedSharedMemoryClause(OMPUnifiedSharedMemoryClause *)1171 void OMPClausePrinter::VisitOMPUnifiedSharedMemoryClause(
1172     OMPUnifiedSharedMemoryClause *) {
1173   OS << "unified_shared_memory";
1174 }
1175 
VisitOMPReverseOffloadClause(OMPReverseOffloadClause *)1176 void OMPClausePrinter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {
1177   OS << "reverse_offload";
1178 }
1179 
VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *)1180 void OMPClausePrinter::VisitOMPDynamicAllocatorsClause(
1181     OMPDynamicAllocatorsClause *) {
1182   OS << "dynamic_allocators";
1183 }
1184 
VisitOMPAtomicDefaultMemOrderClause(OMPAtomicDefaultMemOrderClause * Node)1185 void OMPClausePrinter::VisitOMPAtomicDefaultMemOrderClause(
1186     OMPAtomicDefaultMemOrderClause *Node) {
1187   OS << "atomic_default_mem_order("
1188      << getOpenMPSimpleClauseTypeName(OMPC_atomic_default_mem_order,
1189                                       Node->getAtomicDefaultMemOrderKind())
1190      << ")";
1191 }
1192 
VisitOMPScheduleClause(OMPScheduleClause * Node)1193 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
1194   OS << "schedule(";
1195   if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1196     OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1197                                         Node->getFirstScheduleModifier());
1198     if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1199       OS << ", ";
1200       OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1201                                           Node->getSecondScheduleModifier());
1202     }
1203     OS << ": ";
1204   }
1205   OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
1206   if (auto *E = Node->getChunkSize()) {
1207     OS << ", ";
1208     E->printPretty(OS, nullptr, Policy);
1209   }
1210   OS << ")";
1211 }
1212 
VisitOMPOrderedClause(OMPOrderedClause * Node)1213 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
1214   OS << "ordered";
1215   if (auto *Num = Node->getNumForLoops()) {
1216     OS << "(";
1217     Num->printPretty(OS, nullptr, Policy, 0);
1218     OS << ")";
1219   }
1220 }
1221 
VisitOMPNowaitClause(OMPNowaitClause *)1222 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
1223   OS << "nowait";
1224 }
1225 
VisitOMPUntiedClause(OMPUntiedClause *)1226 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
1227   OS << "untied";
1228 }
1229 
VisitOMPNogroupClause(OMPNogroupClause *)1230 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
1231   OS << "nogroup";
1232 }
1233 
VisitOMPMergeableClause(OMPMergeableClause *)1234 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
1235   OS << "mergeable";
1236 }
1237 
VisitOMPReadClause(OMPReadClause *)1238 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
1239 
VisitOMPWriteClause(OMPWriteClause *)1240 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
1241 
VisitOMPUpdateClause(OMPUpdateClause *)1242 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
1243   OS << "update";
1244 }
1245 
VisitOMPCaptureClause(OMPCaptureClause *)1246 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
1247   OS << "capture";
1248 }
1249 
VisitOMPSeqCstClause(OMPSeqCstClause *)1250 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
1251   OS << "seq_cst";
1252 }
1253 
VisitOMPThreadsClause(OMPThreadsClause *)1254 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
1255   OS << "threads";
1256 }
1257 
VisitOMPSIMDClause(OMPSIMDClause *)1258 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
1259 
VisitOMPDeviceClause(OMPDeviceClause * Node)1260 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
1261   OS << "device(";
1262   Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
1263   OS << ")";
1264 }
1265 
VisitOMPNumTeamsClause(OMPNumTeamsClause * Node)1266 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
1267   OS << "num_teams(";
1268   Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
1269   OS << ")";
1270 }
1271 
VisitOMPThreadLimitClause(OMPThreadLimitClause * Node)1272 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
1273   OS << "thread_limit(";
1274   Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
1275   OS << ")";
1276 }
1277 
VisitOMPPriorityClause(OMPPriorityClause * Node)1278 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
1279   OS << "priority(";
1280   Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
1281   OS << ")";
1282 }
1283 
VisitOMPGrainsizeClause(OMPGrainsizeClause * Node)1284 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
1285   OS << "grainsize(";
1286   Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
1287   OS << ")";
1288 }
1289 
VisitOMPNumTasksClause(OMPNumTasksClause * Node)1290 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
1291   OS << "num_tasks(";
1292   Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
1293   OS << ")";
1294 }
1295 
VisitOMPHintClause(OMPHintClause * Node)1296 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
1297   OS << "hint(";
1298   Node->getHint()->printPretty(OS, nullptr, Policy, 0);
1299   OS << ")";
1300 }
1301 
1302 template<typename T>
VisitOMPClauseList(T * Node,char StartSym)1303 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
1304   for (typename T::varlist_iterator I = Node->varlist_begin(),
1305                                     E = Node->varlist_end();
1306        I != E; ++I) {
1307     assert(*I && "Expected non-null Stmt");
1308     OS << (I == Node->varlist_begin() ? StartSym : ',');
1309     if (auto *DRE = dyn_cast<DeclRefExpr>(*I)) {
1310       if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
1311         DRE->printPretty(OS, nullptr, Policy, 0);
1312       else
1313         DRE->getDecl()->printQualifiedName(OS);
1314     } else
1315       (*I)->printPretty(OS, nullptr, Policy, 0);
1316   }
1317 }
1318 
VisitOMPAllocateClause(OMPAllocateClause * Node)1319 void OMPClausePrinter::VisitOMPAllocateClause(OMPAllocateClause *Node) {
1320   if (Node->varlist_empty())
1321     return;
1322   OS << "allocate";
1323   if (Expr *Allocator = Node->getAllocator()) {
1324     OS << "(";
1325     Allocator->printPretty(OS, nullptr, Policy, 0);
1326     OS << ":";
1327     VisitOMPClauseList(Node, ' ');
1328   } else {
1329     VisitOMPClauseList(Node, '(');
1330   }
1331   OS << ")";
1332 }
1333 
VisitOMPPrivateClause(OMPPrivateClause * Node)1334 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
1335   if (!Node->varlist_empty()) {
1336     OS << "private";
1337     VisitOMPClauseList(Node, '(');
1338     OS << ")";
1339   }
1340 }
1341 
VisitOMPFirstprivateClause(OMPFirstprivateClause * Node)1342 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
1343   if (!Node->varlist_empty()) {
1344     OS << "firstprivate";
1345     VisitOMPClauseList(Node, '(');
1346     OS << ")";
1347   }
1348 }
1349 
VisitOMPLastprivateClause(OMPLastprivateClause * Node)1350 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
1351   if (!Node->varlist_empty()) {
1352     OS << "lastprivate";
1353     VisitOMPClauseList(Node, '(');
1354     OS << ")";
1355   }
1356 }
1357 
VisitOMPSharedClause(OMPSharedClause * Node)1358 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
1359   if (!Node->varlist_empty()) {
1360     OS << "shared";
1361     VisitOMPClauseList(Node, '(');
1362     OS << ")";
1363   }
1364 }
1365 
VisitOMPReductionClause(OMPReductionClause * Node)1366 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
1367   if (!Node->varlist_empty()) {
1368     OS << "reduction(";
1369     NestedNameSpecifier *QualifierLoc =
1370         Node->getQualifierLoc().getNestedNameSpecifier();
1371     OverloadedOperatorKind OOK =
1372         Node->getNameInfo().getName().getCXXOverloadedOperator();
1373     if (QualifierLoc == nullptr && OOK != OO_None) {
1374       // Print reduction identifier in C format
1375       OS << getOperatorSpelling(OOK);
1376     } else {
1377       // Use C++ format
1378       if (QualifierLoc != nullptr)
1379         QualifierLoc->print(OS, Policy);
1380       OS << Node->getNameInfo();
1381     }
1382     OS << ":";
1383     VisitOMPClauseList(Node, ' ');
1384     OS << ")";
1385   }
1386 }
1387 
VisitOMPTaskReductionClause(OMPTaskReductionClause * Node)1388 void OMPClausePrinter::VisitOMPTaskReductionClause(
1389     OMPTaskReductionClause *Node) {
1390   if (!Node->varlist_empty()) {
1391     OS << "task_reduction(";
1392     NestedNameSpecifier *QualifierLoc =
1393         Node->getQualifierLoc().getNestedNameSpecifier();
1394     OverloadedOperatorKind OOK =
1395         Node->getNameInfo().getName().getCXXOverloadedOperator();
1396     if (QualifierLoc == nullptr && OOK != OO_None) {
1397       // Print reduction identifier in C format
1398       OS << getOperatorSpelling(OOK);
1399     } else {
1400       // Use C++ format
1401       if (QualifierLoc != nullptr)
1402         QualifierLoc->print(OS, Policy);
1403       OS << Node->getNameInfo();
1404     }
1405     OS << ":";
1406     VisitOMPClauseList(Node, ' ');
1407     OS << ")";
1408   }
1409 }
1410 
VisitOMPInReductionClause(OMPInReductionClause * Node)1411 void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
1412   if (!Node->varlist_empty()) {
1413     OS << "in_reduction(";
1414     NestedNameSpecifier *QualifierLoc =
1415         Node->getQualifierLoc().getNestedNameSpecifier();
1416     OverloadedOperatorKind OOK =
1417         Node->getNameInfo().getName().getCXXOverloadedOperator();
1418     if (QualifierLoc == nullptr && OOK != OO_None) {
1419       // Print reduction identifier in C format
1420       OS << getOperatorSpelling(OOK);
1421     } else {
1422       // Use C++ format
1423       if (QualifierLoc != nullptr)
1424         QualifierLoc->print(OS, Policy);
1425       OS << Node->getNameInfo();
1426     }
1427     OS << ":";
1428     VisitOMPClauseList(Node, ' ');
1429     OS << ")";
1430   }
1431 }
1432 
VisitOMPLinearClause(OMPLinearClause * Node)1433 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
1434   if (!Node->varlist_empty()) {
1435     OS << "linear";
1436     if (Node->getModifierLoc().isValid()) {
1437       OS << '('
1438          << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
1439     }
1440     VisitOMPClauseList(Node, '(');
1441     if (Node->getModifierLoc().isValid())
1442       OS << ')';
1443     if (Node->getStep() != nullptr) {
1444       OS << ": ";
1445       Node->getStep()->printPretty(OS, nullptr, Policy, 0);
1446     }
1447     OS << ")";
1448   }
1449 }
1450 
VisitOMPAlignedClause(OMPAlignedClause * Node)1451 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
1452   if (!Node->varlist_empty()) {
1453     OS << "aligned";
1454     VisitOMPClauseList(Node, '(');
1455     if (Node->getAlignment() != nullptr) {
1456       OS << ": ";
1457       Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
1458     }
1459     OS << ")";
1460   }
1461 }
1462 
VisitOMPCopyinClause(OMPCopyinClause * Node)1463 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
1464   if (!Node->varlist_empty()) {
1465     OS << "copyin";
1466     VisitOMPClauseList(Node, '(');
1467     OS << ")";
1468   }
1469 }
1470 
VisitOMPCopyprivateClause(OMPCopyprivateClause * Node)1471 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
1472   if (!Node->varlist_empty()) {
1473     OS << "copyprivate";
1474     VisitOMPClauseList(Node, '(');
1475     OS << ")";
1476   }
1477 }
1478 
VisitOMPFlushClause(OMPFlushClause * Node)1479 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
1480   if (!Node->varlist_empty()) {
1481     VisitOMPClauseList(Node, '(');
1482     OS << ")";
1483   }
1484 }
1485 
VisitOMPDependClause(OMPDependClause * Node)1486 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
1487   OS << "depend(";
1488   OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
1489                                       Node->getDependencyKind());
1490   if (!Node->varlist_empty()) {
1491     OS << " :";
1492     VisitOMPClauseList(Node, ' ');
1493   }
1494   OS << ")";
1495 }
1496 
VisitOMPMapClause(OMPMapClause * Node)1497 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
1498   if (!Node->varlist_empty()) {
1499     OS << "map(";
1500     if (Node->getMapType() != OMPC_MAP_unknown) {
1501       for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
1502         if (Node->getMapTypeModifier(I) != OMPC_MAP_MODIFIER_unknown) {
1503           OS << getOpenMPSimpleClauseTypeName(OMPC_map,
1504                                               Node->getMapTypeModifier(I));
1505           if (Node->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_mapper) {
1506             OS << '(';
1507             NestedNameSpecifier *MapperNNS =
1508                 Node->getMapperQualifierLoc().getNestedNameSpecifier();
1509             if (MapperNNS)
1510               MapperNNS->print(OS, Policy);
1511             OS << Node->getMapperIdInfo() << ')';
1512           }
1513           OS << ',';
1514         }
1515       }
1516       OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
1517       OS << ':';
1518     }
1519     VisitOMPClauseList(Node, ' ');
1520     OS << ")";
1521   }
1522 }
1523 
VisitOMPToClause(OMPToClause * Node)1524 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
1525   if (!Node->varlist_empty()) {
1526     OS << "to";
1527     DeclarationNameInfo MapperId = Node->getMapperIdInfo();
1528     if (MapperId.getName() && !MapperId.getName().isEmpty()) {
1529       OS << '(';
1530       OS << "mapper(";
1531       NestedNameSpecifier *MapperNNS =
1532           Node->getMapperQualifierLoc().getNestedNameSpecifier();
1533       if (MapperNNS)
1534         MapperNNS->print(OS, Policy);
1535       OS << MapperId << "):";
1536       VisitOMPClauseList(Node, ' ');
1537     } else {
1538       VisitOMPClauseList(Node, '(');
1539     }
1540     OS << ")";
1541   }
1542 }
1543 
VisitOMPFromClause(OMPFromClause * Node)1544 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
1545   if (!Node->varlist_empty()) {
1546     OS << "from";
1547     DeclarationNameInfo MapperId = Node->getMapperIdInfo();
1548     if (MapperId.getName() && !MapperId.getName().isEmpty()) {
1549       OS << '(';
1550       OS << "mapper(";
1551       NestedNameSpecifier *MapperNNS =
1552           Node->getMapperQualifierLoc().getNestedNameSpecifier();
1553       if (MapperNNS)
1554         MapperNNS->print(OS, Policy);
1555       OS << MapperId << "):";
1556       VisitOMPClauseList(Node, ' ');
1557     } else {
1558       VisitOMPClauseList(Node, '(');
1559     }
1560     OS << ")";
1561   }
1562 }
1563 
VisitOMPDistScheduleClause(OMPDistScheduleClause * Node)1564 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
1565   OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
1566                            OMPC_dist_schedule, Node->getDistScheduleKind());
1567   if (auto *E = Node->getChunkSize()) {
1568     OS << ", ";
1569     E->printPretty(OS, nullptr, Policy);
1570   }
1571   OS << ")";
1572 }
1573 
VisitOMPDefaultmapClause(OMPDefaultmapClause * Node)1574 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
1575   OS << "defaultmap(";
1576   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
1577                                       Node->getDefaultmapModifier());
1578   OS << ": ";
1579   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
1580     Node->getDefaultmapKind());
1581   OS << ")";
1582 }
1583 
VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause * Node)1584 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
1585   if (!Node->varlist_empty()) {
1586     OS << "use_device_ptr";
1587     VisitOMPClauseList(Node, '(');
1588     OS << ")";
1589   }
1590 }
1591 
VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause * Node)1592 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
1593   if (!Node->varlist_empty()) {
1594     OS << "is_device_ptr";
1595     VisitOMPClauseList(Node, '(');
1596     OS << ")";
1597   }
1598 }
1599 
1600