xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/AST/OpenMPClause.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
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/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclOpenMP.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/OpenMPKinds.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include <algorithm>
25 #include <cassert>
26 
27 using namespace clang;
28 using namespace llvm;
29 using namespace omp;
30 
children()31 OMPClause::child_range OMPClause::children() {
32   switch (getClauseKind()) {
33   default:
34     break;
35 #define GEN_CLANG_CLAUSE_CLASS
36 #define CLAUSE_CLASS(Enum, Str, Class)                                         \
37   case Enum:                                                                   \
38     return static_cast<Class *>(this)->children();
39 #include "llvm/Frontend/OpenMP/OMP.inc"
40   }
41   llvm_unreachable("unknown OMPClause");
42 }
43 
used_children()44 OMPClause::child_range OMPClause::used_children() {
45   switch (getClauseKind()) {
46 #define GEN_CLANG_CLAUSE_CLASS
47 #define CLAUSE_CLASS(Enum, Str, Class)                                         \
48   case Enum:                                                                   \
49     return static_cast<Class *>(this)->used_children();
50 #define CLAUSE_NO_CLASS(Enum, Str)                                             \
51   case Enum:                                                                   \
52     break;
53 #include "llvm/Frontend/OpenMP/OMP.inc"
54   }
55   llvm_unreachable("unknown OMPClause");
56 }
57 
get(OMPClause * C)58 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
59   auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
60   return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
61 }
62 
get(const OMPClause * C)63 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
64   switch (C->getClauseKind()) {
65   case OMPC_schedule:
66     return static_cast<const OMPScheduleClause *>(C);
67   case OMPC_dist_schedule:
68     return static_cast<const OMPDistScheduleClause *>(C);
69   case OMPC_firstprivate:
70     return static_cast<const OMPFirstprivateClause *>(C);
71   case OMPC_lastprivate:
72     return static_cast<const OMPLastprivateClause *>(C);
73   case OMPC_reduction:
74     return static_cast<const OMPReductionClause *>(C);
75   case OMPC_task_reduction:
76     return static_cast<const OMPTaskReductionClause *>(C);
77   case OMPC_in_reduction:
78     return static_cast<const OMPInReductionClause *>(C);
79   case OMPC_linear:
80     return static_cast<const OMPLinearClause *>(C);
81   case OMPC_if:
82     return static_cast<const OMPIfClause *>(C);
83   case OMPC_num_threads:
84     return static_cast<const OMPNumThreadsClause *>(C);
85   case OMPC_num_teams:
86     return static_cast<const OMPNumTeamsClause *>(C);
87   case OMPC_thread_limit:
88     return static_cast<const OMPThreadLimitClause *>(C);
89   case OMPC_device:
90     return static_cast<const OMPDeviceClause *>(C);
91   case OMPC_grainsize:
92     return static_cast<const OMPGrainsizeClause *>(C);
93   case OMPC_num_tasks:
94     return static_cast<const OMPNumTasksClause *>(C);
95   case OMPC_final:
96     return static_cast<const OMPFinalClause *>(C);
97   case OMPC_priority:
98     return static_cast<const OMPPriorityClause *>(C);
99   case OMPC_novariants:
100     return static_cast<const OMPNovariantsClause *>(C);
101   case OMPC_nocontext:
102     return static_cast<const OMPNocontextClause *>(C);
103   case OMPC_filter:
104     return static_cast<const OMPFilterClause *>(C);
105   case OMPC_default:
106   case OMPC_proc_bind:
107   case OMPC_safelen:
108   case OMPC_simdlen:
109   case OMPC_sizes:
110   case OMPC_allocator:
111   case OMPC_allocate:
112   case OMPC_collapse:
113   case OMPC_private:
114   case OMPC_shared:
115   case OMPC_aligned:
116   case OMPC_copyin:
117   case OMPC_copyprivate:
118   case OMPC_ordered:
119   case OMPC_nowait:
120   case OMPC_untied:
121   case OMPC_mergeable:
122   case OMPC_threadprivate:
123   case OMPC_flush:
124   case OMPC_depobj:
125   case OMPC_read:
126   case OMPC_write:
127   case OMPC_update:
128   case OMPC_capture:
129   case OMPC_seq_cst:
130   case OMPC_acq_rel:
131   case OMPC_acquire:
132   case OMPC_release:
133   case OMPC_relaxed:
134   case OMPC_depend:
135   case OMPC_threads:
136   case OMPC_simd:
137   case OMPC_map:
138   case OMPC_nogroup:
139   case OMPC_hint:
140   case OMPC_defaultmap:
141   case OMPC_unknown:
142   case OMPC_uniform:
143   case OMPC_to:
144   case OMPC_from:
145   case OMPC_use_device_ptr:
146   case OMPC_use_device_addr:
147   case OMPC_is_device_ptr:
148   case OMPC_unified_address:
149   case OMPC_unified_shared_memory:
150   case OMPC_reverse_offload:
151   case OMPC_dynamic_allocators:
152   case OMPC_atomic_default_mem_order:
153   case OMPC_device_type:
154   case OMPC_match:
155   case OMPC_nontemporal:
156   case OMPC_order:
157   case OMPC_destroy:
158   case OMPC_detach:
159   case OMPC_inclusive:
160   case OMPC_exclusive:
161   case OMPC_uses_allocators:
162   case OMPC_affinity:
163     break;
164   default:
165     break;
166   }
167 
168   return nullptr;
169 }
170 
get(OMPClause * C)171 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
172   auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
173   return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
174 }
175 
get(const OMPClause * C)176 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
177   switch (C->getClauseKind()) {
178   case OMPC_lastprivate:
179     return static_cast<const OMPLastprivateClause *>(C);
180   case OMPC_reduction:
181     return static_cast<const OMPReductionClause *>(C);
182   case OMPC_task_reduction:
183     return static_cast<const OMPTaskReductionClause *>(C);
184   case OMPC_in_reduction:
185     return static_cast<const OMPInReductionClause *>(C);
186   case OMPC_linear:
187     return static_cast<const OMPLinearClause *>(C);
188   case OMPC_schedule:
189   case OMPC_dist_schedule:
190   case OMPC_firstprivate:
191   case OMPC_default:
192   case OMPC_proc_bind:
193   case OMPC_if:
194   case OMPC_final:
195   case OMPC_num_threads:
196   case OMPC_safelen:
197   case OMPC_simdlen:
198   case OMPC_sizes:
199   case OMPC_allocator:
200   case OMPC_allocate:
201   case OMPC_collapse:
202   case OMPC_private:
203   case OMPC_shared:
204   case OMPC_aligned:
205   case OMPC_copyin:
206   case OMPC_copyprivate:
207   case OMPC_ordered:
208   case OMPC_nowait:
209   case OMPC_untied:
210   case OMPC_mergeable:
211   case OMPC_threadprivate:
212   case OMPC_flush:
213   case OMPC_depobj:
214   case OMPC_read:
215   case OMPC_write:
216   case OMPC_update:
217   case OMPC_capture:
218   case OMPC_seq_cst:
219   case OMPC_acq_rel:
220   case OMPC_acquire:
221   case OMPC_release:
222   case OMPC_relaxed:
223   case OMPC_depend:
224   case OMPC_device:
225   case OMPC_threads:
226   case OMPC_simd:
227   case OMPC_map:
228   case OMPC_num_teams:
229   case OMPC_thread_limit:
230   case OMPC_priority:
231   case OMPC_grainsize:
232   case OMPC_nogroup:
233   case OMPC_num_tasks:
234   case OMPC_hint:
235   case OMPC_defaultmap:
236   case OMPC_unknown:
237   case OMPC_uniform:
238   case OMPC_to:
239   case OMPC_from:
240   case OMPC_use_device_ptr:
241   case OMPC_use_device_addr:
242   case OMPC_is_device_ptr:
243   case OMPC_unified_address:
244   case OMPC_unified_shared_memory:
245   case OMPC_reverse_offload:
246   case OMPC_dynamic_allocators:
247   case OMPC_atomic_default_mem_order:
248   case OMPC_device_type:
249   case OMPC_match:
250   case OMPC_nontemporal:
251   case OMPC_order:
252   case OMPC_destroy:
253   case OMPC_novariants:
254   case OMPC_nocontext:
255   case OMPC_detach:
256   case OMPC_inclusive:
257   case OMPC_exclusive:
258   case OMPC_uses_allocators:
259   case OMPC_affinity:
260     break;
261   default:
262     break;
263   }
264 
265   return nullptr;
266 }
267 
268 /// Gets the address of the original, non-captured, expression used in the
269 /// clause as the preinitializer.
getAddrOfExprAsWritten(Stmt * S)270 static Stmt **getAddrOfExprAsWritten(Stmt *S) {
271   if (!S)
272     return nullptr;
273   if (auto *DS = dyn_cast<DeclStmt>(S)) {
274     assert(DS->isSingleDecl() && "Only single expression must be captured.");
275     if (auto *OED = dyn_cast<OMPCapturedExprDecl>(DS->getSingleDecl()))
276       return OED->getInitAddress();
277   }
278   return nullptr;
279 }
280 
used_children()281 OMPClause::child_range OMPIfClause::used_children() {
282   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
283     return child_range(C, C + 1);
284   return child_range(&Condition, &Condition + 1);
285 }
286 
used_children()287 OMPClause::child_range OMPGrainsizeClause::used_children() {
288   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
289     return child_range(C, C + 1);
290   return child_range(&Grainsize, &Grainsize + 1);
291 }
292 
used_children()293 OMPClause::child_range OMPNumTasksClause::used_children() {
294   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
295     return child_range(C, C + 1);
296   return child_range(&NumTasks, &NumTasks + 1);
297 }
298 
used_children()299 OMPClause::child_range OMPFinalClause::used_children() {
300   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
301     return child_range(C, C + 1);
302   return child_range(&Condition, &Condition + 1);
303 }
304 
used_children()305 OMPClause::child_range OMPPriorityClause::used_children() {
306   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
307     return child_range(C, C + 1);
308   return child_range(&Priority, &Priority + 1);
309 }
310 
used_children()311 OMPClause::child_range OMPNovariantsClause::used_children() {
312   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
313     return child_range(C, C + 1);
314   return child_range(&Condition, &Condition + 1);
315 }
316 
used_children()317 OMPClause::child_range OMPNocontextClause::used_children() {
318   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
319     return child_range(C, C + 1);
320   return child_range(&Condition, &Condition + 1);
321 }
322 
Create(const ASTContext & C,Expr * Num,unsigned NumLoops,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)323 OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
324                                            unsigned NumLoops,
325                                            SourceLocation StartLoc,
326                                            SourceLocation LParenLoc,
327                                            SourceLocation EndLoc) {
328   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
329   auto *Clause =
330       new (Mem) OMPOrderedClause(Num, NumLoops, StartLoc, LParenLoc, EndLoc);
331   for (unsigned I = 0; I < NumLoops; ++I) {
332     Clause->setLoopNumIterations(I, nullptr);
333     Clause->setLoopCounter(I, nullptr);
334   }
335   return Clause;
336 }
337 
CreateEmpty(const ASTContext & C,unsigned NumLoops)338 OMPOrderedClause *OMPOrderedClause::CreateEmpty(const ASTContext &C,
339                                                 unsigned NumLoops) {
340   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
341   auto *Clause = new (Mem) OMPOrderedClause(NumLoops);
342   for (unsigned I = 0; I < NumLoops; ++I) {
343     Clause->setLoopNumIterations(I, nullptr);
344     Clause->setLoopCounter(I, nullptr);
345   }
346   return Clause;
347 }
348 
setLoopNumIterations(unsigned NumLoop,Expr * NumIterations)349 void OMPOrderedClause::setLoopNumIterations(unsigned NumLoop,
350                                             Expr *NumIterations) {
351   assert(NumLoop < NumberOfLoops && "out of loops number.");
352   getTrailingObjects<Expr *>()[NumLoop] = NumIterations;
353 }
354 
getLoopNumIterations() const355 ArrayRef<Expr *> OMPOrderedClause::getLoopNumIterations() const {
356   return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumberOfLoops);
357 }
358 
setLoopCounter(unsigned NumLoop,Expr * Counter)359 void OMPOrderedClause::setLoopCounter(unsigned NumLoop, Expr *Counter) {
360   assert(NumLoop < NumberOfLoops && "out of loops number.");
361   getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop] = Counter;
362 }
363 
getLoopCounter(unsigned NumLoop)364 Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) {
365   assert(NumLoop < NumberOfLoops && "out of loops number.");
366   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
367 }
368 
getLoopCounter(unsigned NumLoop) const369 const Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) const {
370   assert(NumLoop < NumberOfLoops && "out of loops number.");
371   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
372 }
373 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation EndLoc)374 OMPUpdateClause *OMPUpdateClause::Create(const ASTContext &C,
375                                          SourceLocation StartLoc,
376                                          SourceLocation EndLoc) {
377   return new (C) OMPUpdateClause(StartLoc, EndLoc, /*IsExtended=*/false);
378 }
379 
380 OMPUpdateClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ArgumentLoc,OpenMPDependClauseKind DK,SourceLocation EndLoc)381 OMPUpdateClause::Create(const ASTContext &C, SourceLocation StartLoc,
382                         SourceLocation LParenLoc, SourceLocation ArgumentLoc,
383                         OpenMPDependClauseKind DK, SourceLocation EndLoc) {
384   void *Mem =
385       C.Allocate(totalSizeToAlloc<SourceLocation, OpenMPDependClauseKind>(2, 1),
386                  alignof(OMPUpdateClause));
387   auto *Clause =
388       new (Mem) OMPUpdateClause(StartLoc, EndLoc, /*IsExtended=*/true);
389   Clause->setLParenLoc(LParenLoc);
390   Clause->setArgumentLoc(ArgumentLoc);
391   Clause->setDependencyKind(DK);
392   return Clause;
393 }
394 
CreateEmpty(const ASTContext & C,bool IsExtended)395 OMPUpdateClause *OMPUpdateClause::CreateEmpty(const ASTContext &C,
396                                               bool IsExtended) {
397   if (!IsExtended)
398     return new (C) OMPUpdateClause(/*IsExtended=*/false);
399   void *Mem =
400       C.Allocate(totalSizeToAlloc<SourceLocation, OpenMPDependClauseKind>(2, 1),
401                  alignof(OMPUpdateClause));
402   auto *Clause = new (Mem) OMPUpdateClause(/*IsExtended=*/true);
403   Clause->IsExtended = true;
404   return Clause;
405 }
406 
setPrivateCopies(ArrayRef<Expr * > VL)407 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
408   assert(VL.size() == varlist_size() &&
409          "Number of private copies is not the same as the preallocated buffer");
410   std::copy(VL.begin(), VL.end(), varlist_end());
411 }
412 
413 OMPPrivateClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > PrivateVL)414 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
415                          SourceLocation LParenLoc, SourceLocation EndLoc,
416                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
417   // Allocate space for private variables and initializer expressions.
418   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
419   OMPPrivateClause *Clause =
420       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
421   Clause->setVarRefs(VL);
422   Clause->setPrivateCopies(PrivateVL);
423   return Clause;
424 }
425 
CreateEmpty(const ASTContext & C,unsigned N)426 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
427                                                 unsigned N) {
428   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
429   return new (Mem) OMPPrivateClause(N);
430 }
431 
setPrivateCopies(ArrayRef<Expr * > VL)432 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
433   assert(VL.size() == varlist_size() &&
434          "Number of private copies is not the same as the preallocated buffer");
435   std::copy(VL.begin(), VL.end(), varlist_end());
436 }
437 
setInits(ArrayRef<Expr * > VL)438 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
439   assert(VL.size() == varlist_size() &&
440          "Number of inits is not the same as the preallocated buffer");
441   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
442 }
443 
444 OMPFirstprivateClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > PrivateVL,ArrayRef<Expr * > InitVL,Stmt * PreInit)445 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
446                               SourceLocation LParenLoc, SourceLocation EndLoc,
447                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
448                               ArrayRef<Expr *> InitVL, Stmt *PreInit) {
449   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
450   OMPFirstprivateClause *Clause =
451       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
452   Clause->setVarRefs(VL);
453   Clause->setPrivateCopies(PrivateVL);
454   Clause->setInits(InitVL);
455   Clause->setPreInitStmt(PreInit);
456   return Clause;
457 }
458 
CreateEmpty(const ASTContext & C,unsigned N)459 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
460                                                           unsigned N) {
461   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
462   return new (Mem) OMPFirstprivateClause(N);
463 }
464 
setPrivateCopies(ArrayRef<Expr * > PrivateCopies)465 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
466   assert(PrivateCopies.size() == varlist_size() &&
467          "Number of private copies is not the same as the preallocated buffer");
468   std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
469 }
470 
setSourceExprs(ArrayRef<Expr * > SrcExprs)471 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
472   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
473                                               "not the same as the "
474                                               "preallocated buffer");
475   std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
476 }
477 
setDestinationExprs(ArrayRef<Expr * > DstExprs)478 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
479   assert(DstExprs.size() == varlist_size() && "Number of destination "
480                                               "expressions is not the same as "
481                                               "the preallocated buffer");
482   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
483 }
484 
setAssignmentOps(ArrayRef<Expr * > AssignmentOps)485 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
486   assert(AssignmentOps.size() == varlist_size() &&
487          "Number of assignment expressions is not the same as the preallocated "
488          "buffer");
489   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
490             getDestinationExprs().end());
491 }
492 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > SrcExprs,ArrayRef<Expr * > DstExprs,ArrayRef<Expr * > AssignmentOps,OpenMPLastprivateModifier LPKind,SourceLocation LPKindLoc,SourceLocation ColonLoc,Stmt * PreInit,Expr * PostUpdate)493 OMPLastprivateClause *OMPLastprivateClause::Create(
494     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
495     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
496     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps,
497     OpenMPLastprivateModifier LPKind, SourceLocation LPKindLoc,
498     SourceLocation ColonLoc, Stmt *PreInit, Expr *PostUpdate) {
499   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
500   OMPLastprivateClause *Clause = new (Mem) OMPLastprivateClause(
501       StartLoc, LParenLoc, EndLoc, LPKind, LPKindLoc, ColonLoc, VL.size());
502   Clause->setVarRefs(VL);
503   Clause->setSourceExprs(SrcExprs);
504   Clause->setDestinationExprs(DstExprs);
505   Clause->setAssignmentOps(AssignmentOps);
506   Clause->setPreInitStmt(PreInit);
507   Clause->setPostUpdateExpr(PostUpdate);
508   return Clause;
509 }
510 
CreateEmpty(const ASTContext & C,unsigned N)511 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
512                                                         unsigned N) {
513   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
514   return new (Mem) OMPLastprivateClause(N);
515 }
516 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)517 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
518                                          SourceLocation StartLoc,
519                                          SourceLocation LParenLoc,
520                                          SourceLocation EndLoc,
521                                          ArrayRef<Expr *> VL) {
522   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
523   OMPSharedClause *Clause =
524       new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
525   Clause->setVarRefs(VL);
526   return Clause;
527 }
528 
CreateEmpty(const ASTContext & C,unsigned N)529 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
530   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
531   return new (Mem) OMPSharedClause(N);
532 }
533 
setPrivates(ArrayRef<Expr * > PL)534 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
535   assert(PL.size() == varlist_size() &&
536          "Number of privates is not the same as the preallocated buffer");
537   std::copy(PL.begin(), PL.end(), varlist_end());
538 }
539 
setInits(ArrayRef<Expr * > IL)540 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
541   assert(IL.size() == varlist_size() &&
542          "Number of inits is not the same as the preallocated buffer");
543   std::copy(IL.begin(), IL.end(), getPrivates().end());
544 }
545 
setUpdates(ArrayRef<Expr * > UL)546 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
547   assert(UL.size() == varlist_size() &&
548          "Number of updates is not the same as the preallocated buffer");
549   std::copy(UL.begin(), UL.end(), getInits().end());
550 }
551 
setFinals(ArrayRef<Expr * > FL)552 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
553   assert(FL.size() == varlist_size() &&
554          "Number of final updates is not the same as the preallocated buffer");
555   std::copy(FL.begin(), FL.end(), getUpdates().end());
556 }
557 
setUsedExprs(ArrayRef<Expr * > UE)558 void OMPLinearClause::setUsedExprs(ArrayRef<Expr *> UE) {
559   assert(
560       UE.size() == varlist_size() + 1 &&
561       "Number of used expressions is not the same as the preallocated buffer");
562   std::copy(UE.begin(), UE.end(), getFinals().end() + 2);
563 }
564 
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)565 OMPLinearClause *OMPLinearClause::Create(
566     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
567     OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
568     SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
569     ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
570     Stmt *PreInit, Expr *PostUpdate) {
571   // Allocate space for 5 lists (Vars, Inits, Updates, Finals), 2 expressions
572   // (Step and CalcStep), list of used expression + step.
573   void *Mem =
574       C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2 + VL.size() + 1));
575   OMPLinearClause *Clause = new (Mem) OMPLinearClause(
576       StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
577   Clause->setVarRefs(VL);
578   Clause->setPrivates(PL);
579   Clause->setInits(IL);
580   // Fill update and final expressions with zeroes, they are provided later,
581   // after the directive construction.
582   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
583             nullptr);
584   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
585             nullptr);
586   std::fill(Clause->getUsedExprs().begin(), Clause->getUsedExprs().end(),
587             nullptr);
588   Clause->setStep(Step);
589   Clause->setCalcStep(CalcStep);
590   Clause->setPreInitStmt(PreInit);
591   Clause->setPostUpdateExpr(PostUpdate);
592   return Clause;
593 }
594 
CreateEmpty(const ASTContext & C,unsigned NumVars)595 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
596                                               unsigned NumVars) {
597   // Allocate space for 5 lists (Vars, Inits, Updates, Finals), 2 expressions
598   // (Step and CalcStep), list of used expression + step.
599   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2 + NumVars  +1));
600   return new (Mem) OMPLinearClause(NumVars);
601 }
602 
used_children()603 OMPClause::child_range OMPLinearClause::used_children() {
604   // Range includes only non-nullptr elements.
605   return child_range(
606       reinterpret_cast<Stmt **>(getUsedExprs().begin()),
607       reinterpret_cast<Stmt **>(llvm::find(getUsedExprs(), nullptr)));
608 }
609 
610 OMPAlignedClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,Expr * A)611 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
612                          SourceLocation LParenLoc, SourceLocation ColonLoc,
613                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
614   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
615   OMPAlignedClause *Clause = new (Mem)
616       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
617   Clause->setVarRefs(VL);
618   Clause->setAlignment(A);
619   return Clause;
620 }
621 
CreateEmpty(const ASTContext & C,unsigned NumVars)622 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
623                                                 unsigned NumVars) {
624   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
625   return new (Mem) OMPAlignedClause(NumVars);
626 }
627 
setSourceExprs(ArrayRef<Expr * > SrcExprs)628 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
629   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
630                                               "not the same as the "
631                                               "preallocated buffer");
632   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
633 }
634 
setDestinationExprs(ArrayRef<Expr * > DstExprs)635 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
636   assert(DstExprs.size() == varlist_size() && "Number of destination "
637                                               "expressions is not the same as "
638                                               "the preallocated buffer");
639   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
640 }
641 
setAssignmentOps(ArrayRef<Expr * > AssignmentOps)642 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
643   assert(AssignmentOps.size() == varlist_size() &&
644          "Number of assignment expressions is not the same as the preallocated "
645          "buffer");
646   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
647             getDestinationExprs().end());
648 }
649 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > SrcExprs,ArrayRef<Expr * > DstExprs,ArrayRef<Expr * > AssignmentOps)650 OMPCopyinClause *OMPCopyinClause::Create(
651     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
652     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
653     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
654   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
655   OMPCopyinClause *Clause =
656       new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
657   Clause->setVarRefs(VL);
658   Clause->setSourceExprs(SrcExprs);
659   Clause->setDestinationExprs(DstExprs);
660   Clause->setAssignmentOps(AssignmentOps);
661   return Clause;
662 }
663 
CreateEmpty(const ASTContext & C,unsigned N)664 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
665   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
666   return new (Mem) OMPCopyinClause(N);
667 }
668 
setSourceExprs(ArrayRef<Expr * > SrcExprs)669 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
670   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
671                                               "not the same as the "
672                                               "preallocated buffer");
673   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
674 }
675 
setDestinationExprs(ArrayRef<Expr * > DstExprs)676 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
677   assert(DstExprs.size() == varlist_size() && "Number of destination "
678                                               "expressions is not the same as "
679                                               "the preallocated buffer");
680   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
681 }
682 
setAssignmentOps(ArrayRef<Expr * > AssignmentOps)683 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
684   assert(AssignmentOps.size() == varlist_size() &&
685          "Number of assignment expressions is not the same as the preallocated "
686          "buffer");
687   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
688             getDestinationExprs().end());
689 }
690 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL,ArrayRef<Expr * > SrcExprs,ArrayRef<Expr * > DstExprs,ArrayRef<Expr * > AssignmentOps)691 OMPCopyprivateClause *OMPCopyprivateClause::Create(
692     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
693     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
694     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
695   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
696   OMPCopyprivateClause *Clause =
697       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
698   Clause->setVarRefs(VL);
699   Clause->setSourceExprs(SrcExprs);
700   Clause->setDestinationExprs(DstExprs);
701   Clause->setAssignmentOps(AssignmentOps);
702   return Clause;
703 }
704 
CreateEmpty(const ASTContext & C,unsigned N)705 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
706                                                         unsigned N) {
707   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
708   return new (Mem) OMPCopyprivateClause(N);
709 }
710 
setPrivates(ArrayRef<Expr * > Privates)711 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
712   assert(Privates.size() == varlist_size() &&
713          "Number of private copies is not the same as the preallocated buffer");
714   std::copy(Privates.begin(), Privates.end(), varlist_end());
715 }
716 
setLHSExprs(ArrayRef<Expr * > LHSExprs)717 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
718   assert(
719       LHSExprs.size() == varlist_size() &&
720       "Number of LHS expressions is not the same as the preallocated buffer");
721   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
722 }
723 
setRHSExprs(ArrayRef<Expr * > RHSExprs)724 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
725   assert(
726       RHSExprs.size() == varlist_size() &&
727       "Number of RHS expressions is not the same as the preallocated buffer");
728   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
729 }
730 
setReductionOps(ArrayRef<Expr * > ReductionOps)731 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
732   assert(ReductionOps.size() == varlist_size() && "Number of reduction "
733                                                   "expressions is not the same "
734                                                   "as the preallocated buffer");
735   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
736 }
737 
setInscanCopyOps(ArrayRef<Expr * > Ops)738 void OMPReductionClause::setInscanCopyOps(ArrayRef<Expr *> Ops) {
739   assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
740   assert(Ops.size() == varlist_size() && "Number of copy "
741                                          "expressions is not the same "
742                                          "as the preallocated buffer");
743   llvm::copy(Ops, getReductionOps().end());
744 }
745 
setInscanCopyArrayTemps(ArrayRef<Expr * > CopyArrayTemps)746 void OMPReductionClause::setInscanCopyArrayTemps(
747     ArrayRef<Expr *> CopyArrayTemps) {
748   assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
749   assert(CopyArrayTemps.size() == varlist_size() &&
750          "Number of copy temp expressions is not the same as the preallocated "
751          "buffer");
752   llvm::copy(CopyArrayTemps, getInscanCopyOps().end());
753 }
754 
setInscanCopyArrayElems(ArrayRef<Expr * > CopyArrayElems)755 void OMPReductionClause::setInscanCopyArrayElems(
756     ArrayRef<Expr *> CopyArrayElems) {
757   assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
758   assert(CopyArrayElems.size() == varlist_size() &&
759          "Number of copy temp expressions is not the same as the preallocated "
760          "buffer");
761   llvm::copy(CopyArrayElems, getInscanCopyArrayTemps().end());
762 }
763 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ModifierLoc,SourceLocation EndLoc,SourceLocation ColonLoc,OpenMPReductionClauseModifier Modifier,ArrayRef<Expr * > VL,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,ArrayRef<Expr * > Privates,ArrayRef<Expr * > LHSExprs,ArrayRef<Expr * > RHSExprs,ArrayRef<Expr * > ReductionOps,ArrayRef<Expr * > CopyOps,ArrayRef<Expr * > CopyArrayTemps,ArrayRef<Expr * > CopyArrayElems,Stmt * PreInit,Expr * PostUpdate)764 OMPReductionClause *OMPReductionClause::Create(
765     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
766     SourceLocation ModifierLoc, SourceLocation EndLoc, SourceLocation ColonLoc,
767     OpenMPReductionClauseModifier Modifier, ArrayRef<Expr *> VL,
768     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
769     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
770     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
771     ArrayRef<Expr *> CopyOps, ArrayRef<Expr *> CopyArrayTemps,
772     ArrayRef<Expr *> CopyArrayElems, Stmt *PreInit, Expr *PostUpdate) {
773   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
774       (Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * VL.size()));
775   auto *Clause = new (Mem)
776       OMPReductionClause(StartLoc, LParenLoc, ModifierLoc, EndLoc, ColonLoc,
777                          Modifier, VL.size(), QualifierLoc, NameInfo);
778   Clause->setVarRefs(VL);
779   Clause->setPrivates(Privates);
780   Clause->setLHSExprs(LHSExprs);
781   Clause->setRHSExprs(RHSExprs);
782   Clause->setReductionOps(ReductionOps);
783   Clause->setPreInitStmt(PreInit);
784   Clause->setPostUpdateExpr(PostUpdate);
785   if (Modifier == OMPC_REDUCTION_inscan) {
786     Clause->setInscanCopyOps(CopyOps);
787     Clause->setInscanCopyArrayTemps(CopyArrayTemps);
788     Clause->setInscanCopyArrayElems(CopyArrayElems);
789   } else {
790     assert(CopyOps.empty() &&
791            "copy operations are expected in inscan reductions only.");
792     assert(CopyArrayTemps.empty() &&
793            "copy array temps are expected in inscan reductions only.");
794     assert(CopyArrayElems.empty() &&
795            "copy array temps are expected in inscan reductions only.");
796   }
797   return Clause;
798 }
799 
800 OMPReductionClause *
CreateEmpty(const ASTContext & C,unsigned N,OpenMPReductionClauseModifier Modifier)801 OMPReductionClause::CreateEmpty(const ASTContext &C, unsigned N,
802                                 OpenMPReductionClauseModifier Modifier) {
803   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
804       (Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * N));
805   auto *Clause = new (Mem) OMPReductionClause(N);
806   Clause->setModifier(Modifier);
807   return Clause;
808 }
809 
setPrivates(ArrayRef<Expr * > Privates)810 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
811   assert(Privates.size() == varlist_size() &&
812          "Number of private copies is not the same as the preallocated buffer");
813   std::copy(Privates.begin(), Privates.end(), varlist_end());
814 }
815 
setLHSExprs(ArrayRef<Expr * > LHSExprs)816 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
817   assert(
818       LHSExprs.size() == varlist_size() &&
819       "Number of LHS expressions is not the same as the preallocated buffer");
820   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
821 }
822 
setRHSExprs(ArrayRef<Expr * > RHSExprs)823 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
824   assert(
825       RHSExprs.size() == varlist_size() &&
826       "Number of RHS expressions is not the same as the preallocated buffer");
827   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
828 }
829 
setReductionOps(ArrayRef<Expr * > ReductionOps)830 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
831   assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
832                                                   "expressions is not the same "
833                                                   "as the preallocated buffer");
834   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
835 }
836 
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)837 OMPTaskReductionClause *OMPTaskReductionClause::Create(
838     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
839     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
840     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
841     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
842     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
843     Expr *PostUpdate) {
844   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
845   OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
846       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
847   Clause->setVarRefs(VL);
848   Clause->setPrivates(Privates);
849   Clause->setLHSExprs(LHSExprs);
850   Clause->setRHSExprs(RHSExprs);
851   Clause->setReductionOps(ReductionOps);
852   Clause->setPreInitStmt(PreInit);
853   Clause->setPostUpdateExpr(PostUpdate);
854   return Clause;
855 }
856 
CreateEmpty(const ASTContext & C,unsigned N)857 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
858                                                             unsigned N) {
859   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
860   return new (Mem) OMPTaskReductionClause(N);
861 }
862 
setPrivates(ArrayRef<Expr * > Privates)863 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
864   assert(Privates.size() == varlist_size() &&
865          "Number of private copies is not the same as the preallocated buffer");
866   std::copy(Privates.begin(), Privates.end(), varlist_end());
867 }
868 
setLHSExprs(ArrayRef<Expr * > LHSExprs)869 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
870   assert(
871       LHSExprs.size() == varlist_size() &&
872       "Number of LHS expressions is not the same as the preallocated buffer");
873   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
874 }
875 
setRHSExprs(ArrayRef<Expr * > RHSExprs)876 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
877   assert(
878       RHSExprs.size() == varlist_size() &&
879       "Number of RHS expressions is not the same as the preallocated buffer");
880   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
881 }
882 
setReductionOps(ArrayRef<Expr * > ReductionOps)883 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
884   assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
885                                                   "expressions is not the same "
886                                                   "as the preallocated buffer");
887   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
888 }
889 
setTaskgroupDescriptors(ArrayRef<Expr * > TaskgroupDescriptors)890 void OMPInReductionClause::setTaskgroupDescriptors(
891     ArrayRef<Expr *> TaskgroupDescriptors) {
892   assert(TaskgroupDescriptors.size() == varlist_size() &&
893          "Number of in reduction descriptors is not the same as the "
894          "preallocated buffer");
895   std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
896             getReductionOps().end());
897 }
898 
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)899 OMPInReductionClause *OMPInReductionClause::Create(
900     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
901     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
902     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
903     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
904     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
905     ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
906   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
907   OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
908       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
909   Clause->setVarRefs(VL);
910   Clause->setPrivates(Privates);
911   Clause->setLHSExprs(LHSExprs);
912   Clause->setRHSExprs(RHSExprs);
913   Clause->setReductionOps(ReductionOps);
914   Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
915   Clause->setPreInitStmt(PreInit);
916   Clause->setPostUpdateExpr(PostUpdate);
917   return Clause;
918 }
919 
CreateEmpty(const ASTContext & C,unsigned N)920 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
921                                                         unsigned N) {
922   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
923   return new (Mem) OMPInReductionClause(N);
924 }
925 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > Sizes)926 OMPSizesClause *OMPSizesClause::Create(const ASTContext &C,
927                                        SourceLocation StartLoc,
928                                        SourceLocation LParenLoc,
929                                        SourceLocation EndLoc,
930                                        ArrayRef<Expr *> Sizes) {
931   OMPSizesClause *Clause = CreateEmpty(C, Sizes.size());
932   Clause->setLocStart(StartLoc);
933   Clause->setLParenLoc(LParenLoc);
934   Clause->setLocEnd(EndLoc);
935   Clause->setSizesRefs(Sizes);
936   return Clause;
937 }
938 
CreateEmpty(const ASTContext & C,unsigned NumSizes)939 OMPSizesClause *OMPSizesClause::CreateEmpty(const ASTContext &C,
940                                             unsigned NumSizes) {
941   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumSizes));
942   return new (Mem) OMPSizesClause(NumSizes);
943 }
944 
945 OMPAllocateClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,Expr * Allocator,SourceLocation ColonLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)946 OMPAllocateClause::Create(const ASTContext &C, SourceLocation StartLoc,
947                           SourceLocation LParenLoc, Expr *Allocator,
948                           SourceLocation ColonLoc, SourceLocation EndLoc,
949                           ArrayRef<Expr *> VL) {
950   // Allocate space for private variables and initializer expressions.
951   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
952   auto *Clause = new (Mem) OMPAllocateClause(StartLoc, LParenLoc, Allocator,
953                                              ColonLoc, EndLoc, VL.size());
954   Clause->setVarRefs(VL);
955   return Clause;
956 }
957 
CreateEmpty(const ASTContext & C,unsigned N)958 OMPAllocateClause *OMPAllocateClause::CreateEmpty(const ASTContext &C,
959                                                   unsigned N) {
960   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
961   return new (Mem) OMPAllocateClause(N);
962 }
963 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)964 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
965                                        SourceLocation StartLoc,
966                                        SourceLocation LParenLoc,
967                                        SourceLocation EndLoc,
968                                        ArrayRef<Expr *> VL) {
969   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
970   OMPFlushClause *Clause =
971       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
972   Clause->setVarRefs(VL);
973   return Clause;
974 }
975 
CreateEmpty(const ASTContext & C,unsigned N)976 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
977   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
978   return new (Mem) OMPFlushClause(N);
979 }
980 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation RParenLoc,Expr * Depobj)981 OMPDepobjClause *OMPDepobjClause::Create(const ASTContext &C,
982                                          SourceLocation StartLoc,
983                                          SourceLocation LParenLoc,
984                                          SourceLocation RParenLoc,
985                                          Expr *Depobj) {
986   auto *Clause = new (C) OMPDepobjClause(StartLoc, LParenLoc, RParenLoc);
987   Clause->setDepobj(Depobj);
988   return Clause;
989 }
990 
CreateEmpty(const ASTContext & C)991 OMPDepobjClause *OMPDepobjClause::CreateEmpty(const ASTContext &C) {
992   return new (C) OMPDepobjClause();
993 }
994 
995 OMPDependClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,Expr * DepModifier,OpenMPDependClauseKind DepKind,SourceLocation DepLoc,SourceLocation ColonLoc,ArrayRef<Expr * > VL,unsigned NumLoops)996 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
997                         SourceLocation LParenLoc, SourceLocation EndLoc,
998                         Expr *DepModifier, OpenMPDependClauseKind DepKind,
999                         SourceLocation DepLoc, SourceLocation ColonLoc,
1000                         ArrayRef<Expr *> VL, unsigned NumLoops) {
1001   void *Mem = C.Allocate(
1002       totalSizeToAlloc<Expr *>(VL.size() + /*depend-modifier*/ 1 + NumLoops),
1003       alignof(OMPDependClause));
1004   OMPDependClause *Clause = new (Mem)
1005       OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
1006   Clause->setVarRefs(VL);
1007   Clause->setDependencyKind(DepKind);
1008   Clause->setDependencyLoc(DepLoc);
1009   Clause->setColonLoc(ColonLoc);
1010   Clause->setModifier(DepModifier);
1011   for (unsigned I = 0 ; I < NumLoops; ++I)
1012     Clause->setLoopData(I, nullptr);
1013   return Clause;
1014 }
1015 
CreateEmpty(const ASTContext & C,unsigned N,unsigned NumLoops)1016 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N,
1017                                               unsigned NumLoops) {
1018   void *Mem =
1019       C.Allocate(totalSizeToAlloc<Expr *>(N + /*depend-modifier*/ 1 + NumLoops),
1020                  alignof(OMPDependClause));
1021   return new (Mem) OMPDependClause(N, NumLoops);
1022 }
1023 
setLoopData(unsigned NumLoop,Expr * Cnt)1024 void OMPDependClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
1025   assert((getDependencyKind() == OMPC_DEPEND_sink ||
1026           getDependencyKind() == OMPC_DEPEND_source) &&
1027          NumLoop < NumLoops &&
1028          "Expected sink or source depend + loop index must be less number of "
1029          "loops.");
1030   auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1031   *It = Cnt;
1032 }
1033 
getLoopData(unsigned NumLoop)1034 Expr *OMPDependClause::getLoopData(unsigned NumLoop) {
1035   assert((getDependencyKind() == OMPC_DEPEND_sink ||
1036           getDependencyKind() == OMPC_DEPEND_source) &&
1037          NumLoop < NumLoops &&
1038          "Expected sink or source depend + loop index must be less number of "
1039          "loops.");
1040   auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1041   return *It;
1042 }
1043 
getLoopData(unsigned NumLoop) const1044 const Expr *OMPDependClause::getLoopData(unsigned NumLoop) const {
1045   assert((getDependencyKind() == OMPC_DEPEND_sink ||
1046           getDependencyKind() == OMPC_DEPEND_source) &&
1047          NumLoop < NumLoops &&
1048          "Expected sink or source depend + loop index must be less number of "
1049          "loops.");
1050   const auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1051   return *It;
1052 }
1053 
setModifier(Expr * DepModifier)1054 void OMPDependClause::setModifier(Expr *DepModifier) {
1055   *getVarRefs().end() = DepModifier;
1056 }
getModifier()1057 Expr *OMPDependClause::getModifier() { return *getVarRefs().end(); }
1058 
getComponentsTotalNumber(MappableExprComponentListsRef ComponentLists)1059 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
1060     MappableExprComponentListsRef ComponentLists) {
1061   unsigned TotalNum = 0u;
1062   for (auto &C : ComponentLists)
1063     TotalNum += C.size();
1064   return TotalNum;
1065 }
1066 
getUniqueDeclarationsTotalNumber(ArrayRef<const ValueDecl * > Declarations)1067 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
1068     ArrayRef<const ValueDecl *> Declarations) {
1069   unsigned TotalNum = 0u;
1070   llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
1071   for (const ValueDecl *D : Declarations) {
1072     const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
1073     if (Cache.count(VD))
1074       continue;
1075     ++TotalNum;
1076     Cache.insert(VD);
1077   }
1078   return TotalNum;
1079 }
1080 
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)1081 OMPMapClause *OMPMapClause::Create(
1082     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1083     ArrayRef<ValueDecl *> Declarations,
1084     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1085     ArrayRef<OpenMPMapModifierKind> MapModifiers,
1086     ArrayRef<SourceLocation> MapModifiersLoc,
1087     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId,
1088     OpenMPMapClauseKind Type, bool TypeIsImplicit, SourceLocation TypeLoc) {
1089   OMPMappableExprListSizeTy Sizes;
1090   Sizes.NumVars = Vars.size();
1091   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1092   Sizes.NumComponentLists = ComponentLists.size();
1093   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1094 
1095   // We need to allocate:
1096   // 2 x NumVars x Expr* - we have an original list expression and an associated
1097   // user-defined mapper for each clause list entry.
1098   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1099   // with each component list.
1100   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1101   // number of lists for each unique declaration and the size of each component
1102   // list.
1103   // NumComponents x MappableComponent - the total of all the components in all
1104   // the lists.
1105   void *Mem = C.Allocate(
1106       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1107                        OMPClauseMappableExprCommon::MappableComponent>(
1108           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1109           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1110           Sizes.NumComponents));
1111   OMPMapClause *Clause = new (Mem)
1112       OMPMapClause(MapModifiers, MapModifiersLoc, UDMQualifierLoc, MapperId,
1113                    Type, TypeIsImplicit, TypeLoc, Locs, Sizes);
1114 
1115   Clause->setVarRefs(Vars);
1116   Clause->setUDMapperRefs(UDMapperRefs);
1117   Clause->setClauseInfo(Declarations, ComponentLists);
1118   Clause->setMapType(Type);
1119   Clause->setMapLoc(TypeLoc);
1120   return Clause;
1121 }
1122 
1123 OMPMapClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1124 OMPMapClause::CreateEmpty(const ASTContext &C,
1125                           const OMPMappableExprListSizeTy &Sizes) {
1126   void *Mem = C.Allocate(
1127       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1128                        OMPClauseMappableExprCommon::MappableComponent>(
1129           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1130           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1131           Sizes.NumComponents));
1132   return new (Mem) OMPMapClause(Sizes);
1133 }
1134 
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists,ArrayRef<Expr * > UDMapperRefs,ArrayRef<OpenMPMotionModifierKind> MotionModifiers,ArrayRef<SourceLocation> MotionModifiersLoc,NestedNameSpecifierLoc UDMQualifierLoc,DeclarationNameInfo MapperId)1135 OMPToClause *OMPToClause::Create(
1136     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1137     ArrayRef<ValueDecl *> Declarations,
1138     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1139     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1140     ArrayRef<SourceLocation> MotionModifiersLoc,
1141     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
1142   OMPMappableExprListSizeTy Sizes;
1143   Sizes.NumVars = Vars.size();
1144   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1145   Sizes.NumComponentLists = ComponentLists.size();
1146   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1147 
1148   // We need to allocate:
1149   // 2 x NumVars x Expr* - we have an original list expression and an associated
1150   // user-defined mapper for each clause list entry.
1151   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1152   // with each component list.
1153   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1154   // number of lists for each unique declaration and the size of each component
1155   // list.
1156   // NumComponents x MappableComponent - the total of all the components in all
1157   // the lists.
1158   void *Mem = C.Allocate(
1159       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1160                        OMPClauseMappableExprCommon::MappableComponent>(
1161           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1162           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1163           Sizes.NumComponents));
1164 
1165   auto *Clause = new (Mem) OMPToClause(MotionModifiers, MotionModifiersLoc,
1166                                        UDMQualifierLoc, MapperId, Locs, Sizes);
1167 
1168   Clause->setVarRefs(Vars);
1169   Clause->setUDMapperRefs(UDMapperRefs);
1170   Clause->setClauseInfo(Declarations, ComponentLists);
1171   return Clause;
1172 }
1173 
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1174 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C,
1175                                       const OMPMappableExprListSizeTy &Sizes) {
1176   void *Mem = C.Allocate(
1177       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1178                        OMPClauseMappableExprCommon::MappableComponent>(
1179           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1180           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1181           Sizes.NumComponents));
1182   return new (Mem) OMPToClause(Sizes);
1183 }
1184 
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists,ArrayRef<Expr * > UDMapperRefs,ArrayRef<OpenMPMotionModifierKind> MotionModifiers,ArrayRef<SourceLocation> MotionModifiersLoc,NestedNameSpecifierLoc UDMQualifierLoc,DeclarationNameInfo MapperId)1185 OMPFromClause *OMPFromClause::Create(
1186     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1187     ArrayRef<ValueDecl *> Declarations,
1188     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1189     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1190     ArrayRef<SourceLocation> MotionModifiersLoc,
1191     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
1192   OMPMappableExprListSizeTy Sizes;
1193   Sizes.NumVars = Vars.size();
1194   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1195   Sizes.NumComponentLists = ComponentLists.size();
1196   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1197 
1198   // We need to allocate:
1199   // 2 x NumVars x Expr* - we have an original list expression and an associated
1200   // user-defined mapper for each clause list entry.
1201   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1202   // with each component list.
1203   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1204   // number of lists for each unique declaration and the size of each component
1205   // list.
1206   // NumComponents x MappableComponent - the total of all the components in all
1207   // the lists.
1208   void *Mem = C.Allocate(
1209       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1210                        OMPClauseMappableExprCommon::MappableComponent>(
1211           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1212           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1213           Sizes.NumComponents));
1214 
1215   auto *Clause =
1216       new (Mem) OMPFromClause(MotionModifiers, MotionModifiersLoc,
1217                               UDMQualifierLoc, MapperId, Locs, Sizes);
1218 
1219   Clause->setVarRefs(Vars);
1220   Clause->setUDMapperRefs(UDMapperRefs);
1221   Clause->setClauseInfo(Declarations, ComponentLists);
1222   return Clause;
1223 }
1224 
1225 OMPFromClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1226 OMPFromClause::CreateEmpty(const ASTContext &C,
1227                            const OMPMappableExprListSizeTy &Sizes) {
1228   void *Mem = C.Allocate(
1229       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1230                        OMPClauseMappableExprCommon::MappableComponent>(
1231           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1232           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1233           Sizes.NumComponents));
1234   return new (Mem) OMPFromClause(Sizes);
1235 }
1236 
setPrivateCopies(ArrayRef<Expr * > VL)1237 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1238   assert(VL.size() == varlist_size() &&
1239          "Number of private copies is not the same as the preallocated buffer");
1240   std::copy(VL.begin(), VL.end(), varlist_end());
1241 }
1242 
setInits(ArrayRef<Expr * > VL)1243 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
1244   assert(VL.size() == varlist_size() &&
1245          "Number of inits is not the same as the preallocated buffer");
1246   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
1247 }
1248 
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<Expr * > PrivateVars,ArrayRef<Expr * > Inits,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists)1249 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
1250     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1251     ArrayRef<Expr *> PrivateVars, ArrayRef<Expr *> Inits,
1252     ArrayRef<ValueDecl *> Declarations,
1253     MappableExprComponentListsRef ComponentLists) {
1254   OMPMappableExprListSizeTy Sizes;
1255   Sizes.NumVars = Vars.size();
1256   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1257   Sizes.NumComponentLists = ComponentLists.size();
1258   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1259 
1260   // We need to allocate:
1261   // NumVars x Expr* - we have an original list expression for each clause
1262   // list entry.
1263   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1264   // with each component list.
1265   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1266   // number of lists for each unique declaration and the size of each component
1267   // list.
1268   // NumComponents x MappableComponent - the total of all the components in all
1269   // the lists.
1270   void *Mem = C.Allocate(
1271       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1272                        OMPClauseMappableExprCommon::MappableComponent>(
1273           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1274           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1275           Sizes.NumComponents));
1276 
1277   OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(Locs, Sizes);
1278 
1279   Clause->setVarRefs(Vars);
1280   Clause->setPrivateCopies(PrivateVars);
1281   Clause->setInits(Inits);
1282   Clause->setClauseInfo(Declarations, ComponentLists);
1283   return Clause;
1284 }
1285 
1286 OMPUseDevicePtrClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1287 OMPUseDevicePtrClause::CreateEmpty(const ASTContext &C,
1288                                    const OMPMappableExprListSizeTy &Sizes) {
1289   void *Mem = C.Allocate(
1290       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1291                        OMPClauseMappableExprCommon::MappableComponent>(
1292           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1293           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1294           Sizes.NumComponents));
1295   return new (Mem) OMPUseDevicePtrClause(Sizes);
1296 }
1297 
1298 OMPUseDeviceAddrClause *
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists)1299 OMPUseDeviceAddrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1300                                ArrayRef<Expr *> Vars,
1301                                ArrayRef<ValueDecl *> Declarations,
1302                                MappableExprComponentListsRef ComponentLists) {
1303   OMPMappableExprListSizeTy Sizes;
1304   Sizes.NumVars = Vars.size();
1305   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1306   Sizes.NumComponentLists = ComponentLists.size();
1307   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1308 
1309   // We need to allocate:
1310   // 3 x NumVars x Expr* - we have an original list expression for each clause
1311   // list entry and an equal number of private copies and inits.
1312   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1313   // with each component list.
1314   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1315   // number of lists for each unique declaration and the size of each component
1316   // list.
1317   // NumComponents x MappableComponent - the total of all the components in all
1318   // the lists.
1319   void *Mem = C.Allocate(
1320       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1321                        OMPClauseMappableExprCommon::MappableComponent>(
1322           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1323           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1324           Sizes.NumComponents));
1325 
1326   auto *Clause = new (Mem) OMPUseDeviceAddrClause(Locs, Sizes);
1327 
1328   Clause->setVarRefs(Vars);
1329   Clause->setClauseInfo(Declarations, ComponentLists);
1330   return Clause;
1331 }
1332 
1333 OMPUseDeviceAddrClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1334 OMPUseDeviceAddrClause::CreateEmpty(const ASTContext &C,
1335                                     const OMPMappableExprListSizeTy &Sizes) {
1336   void *Mem = C.Allocate(
1337       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1338                        OMPClauseMappableExprCommon::MappableComponent>(
1339           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1340           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1341           Sizes.NumComponents));
1342   return new (Mem) OMPUseDeviceAddrClause(Sizes);
1343 }
1344 
1345 OMPIsDevicePtrClause *
Create(const ASTContext & C,const OMPVarListLocTy & Locs,ArrayRef<Expr * > Vars,ArrayRef<ValueDecl * > Declarations,MappableExprComponentListsRef ComponentLists)1346 OMPIsDevicePtrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1347                              ArrayRef<Expr *> Vars,
1348                              ArrayRef<ValueDecl *> Declarations,
1349                              MappableExprComponentListsRef ComponentLists) {
1350   OMPMappableExprListSizeTy Sizes;
1351   Sizes.NumVars = Vars.size();
1352   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1353   Sizes.NumComponentLists = ComponentLists.size();
1354   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1355 
1356   // We need to allocate:
1357   // NumVars x Expr* - we have an original list expression for each clause list
1358   // entry.
1359   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1360   // with each component list.
1361   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1362   // number of lists for each unique declaration and the size of each component
1363   // list.
1364   // NumComponents x MappableComponent - the total of all the components in all
1365   // the lists.
1366   void *Mem = C.Allocate(
1367       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1368                        OMPClauseMappableExprCommon::MappableComponent>(
1369           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1370           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1371           Sizes.NumComponents));
1372 
1373   OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(Locs, Sizes);
1374 
1375   Clause->setVarRefs(Vars);
1376   Clause->setClauseInfo(Declarations, ComponentLists);
1377   return Clause;
1378 }
1379 
1380 OMPIsDevicePtrClause *
CreateEmpty(const ASTContext & C,const OMPMappableExprListSizeTy & Sizes)1381 OMPIsDevicePtrClause::CreateEmpty(const ASTContext &C,
1382                                   const OMPMappableExprListSizeTy &Sizes) {
1383   void *Mem = C.Allocate(
1384       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1385                        OMPClauseMappableExprCommon::MappableComponent>(
1386           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1387           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1388           Sizes.NumComponents));
1389   return new (Mem) OMPIsDevicePtrClause(Sizes);
1390 }
1391 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)1392 OMPNontemporalClause *OMPNontemporalClause::Create(const ASTContext &C,
1393                                                    SourceLocation StartLoc,
1394                                                    SourceLocation LParenLoc,
1395                                                    SourceLocation EndLoc,
1396                                                    ArrayRef<Expr *> VL) {
1397   // Allocate space for nontemporal variables + private references.
1398   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
1399   auto *Clause =
1400       new (Mem) OMPNontemporalClause(StartLoc, LParenLoc, EndLoc, VL.size());
1401   Clause->setVarRefs(VL);
1402   return Clause;
1403 }
1404 
CreateEmpty(const ASTContext & C,unsigned N)1405 OMPNontemporalClause *OMPNontemporalClause::CreateEmpty(const ASTContext &C,
1406                                                         unsigned N) {
1407   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
1408   return new (Mem) OMPNontemporalClause(N);
1409 }
1410 
setPrivateRefs(ArrayRef<Expr * > VL)1411 void OMPNontemporalClause::setPrivateRefs(ArrayRef<Expr *> VL) {
1412   assert(VL.size() == varlist_size() && "Number of private references is not "
1413                                         "the same as the preallocated buffer");
1414   std::copy(VL.begin(), VL.end(), varlist_end());
1415 }
1416 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)1417 OMPInclusiveClause *OMPInclusiveClause::Create(const ASTContext &C,
1418                                                SourceLocation StartLoc,
1419                                                SourceLocation LParenLoc,
1420                                                SourceLocation EndLoc,
1421                                                ArrayRef<Expr *> VL) {
1422   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
1423   auto *Clause =
1424       new (Mem) OMPInclusiveClause(StartLoc, LParenLoc, EndLoc, VL.size());
1425   Clause->setVarRefs(VL);
1426   return Clause;
1427 }
1428 
CreateEmpty(const ASTContext & C,unsigned N)1429 OMPInclusiveClause *OMPInclusiveClause::CreateEmpty(const ASTContext &C,
1430                                                     unsigned N) {
1431   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1432   return new (Mem) OMPInclusiveClause(N);
1433 }
1434 
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<Expr * > VL)1435 OMPExclusiveClause *OMPExclusiveClause::Create(const ASTContext &C,
1436                                                SourceLocation StartLoc,
1437                                                SourceLocation LParenLoc,
1438                                                SourceLocation EndLoc,
1439                                                ArrayRef<Expr *> VL) {
1440   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
1441   auto *Clause =
1442       new (Mem) OMPExclusiveClause(StartLoc, LParenLoc, EndLoc, VL.size());
1443   Clause->setVarRefs(VL);
1444   return Clause;
1445 }
1446 
CreateEmpty(const ASTContext & C,unsigned N)1447 OMPExclusiveClause *OMPExclusiveClause::CreateEmpty(const ASTContext &C,
1448                                                     unsigned N) {
1449   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1450   return new (Mem) OMPExclusiveClause(N);
1451 }
1452 
setAllocatorsData(ArrayRef<OMPUsesAllocatorsClause::Data> Data)1453 void OMPUsesAllocatorsClause::setAllocatorsData(
1454     ArrayRef<OMPUsesAllocatorsClause::Data> Data) {
1455   assert(Data.size() == NumOfAllocators &&
1456          "Size of allocators data is not the same as the preallocated buffer.");
1457   for (unsigned I = 0, E = Data.size(); I < E; ++I) {
1458     const OMPUsesAllocatorsClause::Data &D = Data[I];
1459     getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1460                                  static_cast<int>(ExprOffsets::Allocator)] =
1461         D.Allocator;
1462     getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1463                                  static_cast<int>(
1464                                      ExprOffsets::AllocatorTraits)] =
1465         D.AllocatorTraits;
1466     getTrailingObjects<
1467         SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1468                           static_cast<int>(ParenLocsOffsets::LParen)] =
1469         D.LParenLoc;
1470     getTrailingObjects<
1471         SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1472                           static_cast<int>(ParenLocsOffsets::RParen)] =
1473         D.RParenLoc;
1474   }
1475 }
1476 
1477 OMPUsesAllocatorsClause::Data
getAllocatorData(unsigned I) const1478 OMPUsesAllocatorsClause::getAllocatorData(unsigned I) const {
1479   OMPUsesAllocatorsClause::Data Data;
1480   Data.Allocator =
1481       getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1482                                    static_cast<int>(ExprOffsets::Allocator)];
1483   Data.AllocatorTraits =
1484       getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1485                                    static_cast<int>(
1486                                        ExprOffsets::AllocatorTraits)];
1487   Data.LParenLoc = getTrailingObjects<
1488       SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1489                         static_cast<int>(ParenLocsOffsets::LParen)];
1490   Data.RParenLoc = getTrailingObjects<
1491       SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1492                         static_cast<int>(ParenLocsOffsets::RParen)];
1493   return Data;
1494 }
1495 
1496 OMPUsesAllocatorsClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc,ArrayRef<OMPUsesAllocatorsClause::Data> Data)1497 OMPUsesAllocatorsClause::Create(const ASTContext &C, SourceLocation StartLoc,
1498                                 SourceLocation LParenLoc, SourceLocation EndLoc,
1499                                 ArrayRef<OMPUsesAllocatorsClause::Data> Data) {
1500   void *Mem = C.Allocate(totalSizeToAlloc<Expr *, SourceLocation>(
1501       static_cast<int>(ExprOffsets::Total) * Data.size(),
1502       static_cast<int>(ParenLocsOffsets::Total) * Data.size()));
1503   auto *Clause = new (Mem)
1504       OMPUsesAllocatorsClause(StartLoc, LParenLoc, EndLoc, Data.size());
1505   Clause->setAllocatorsData(Data);
1506   return Clause;
1507 }
1508 
1509 OMPUsesAllocatorsClause *
CreateEmpty(const ASTContext & C,unsigned N)1510 OMPUsesAllocatorsClause::CreateEmpty(const ASTContext &C, unsigned N) {
1511   void *Mem = C.Allocate(totalSizeToAlloc<Expr *, SourceLocation>(
1512       static_cast<int>(ExprOffsets::Total) * N,
1513       static_cast<int>(ParenLocsOffsets::Total) * N));
1514   return new (Mem) OMPUsesAllocatorsClause(N);
1515 }
1516 
1517 OMPAffinityClause *
Create(const ASTContext & C,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc,Expr * Modifier,ArrayRef<Expr * > Locators)1518 OMPAffinityClause::Create(const ASTContext &C, SourceLocation StartLoc,
1519                           SourceLocation LParenLoc, SourceLocation ColonLoc,
1520                           SourceLocation EndLoc, Expr *Modifier,
1521                           ArrayRef<Expr *> Locators) {
1522   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Locators.size() + 1));
1523   auto *Clause = new (Mem)
1524       OMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, Locators.size());
1525   Clause->setModifier(Modifier);
1526   Clause->setVarRefs(Locators);
1527   return Clause;
1528 }
1529 
CreateEmpty(const ASTContext & C,unsigned N)1530 OMPAffinityClause *OMPAffinityClause::CreateEmpty(const ASTContext &C,
1531                                                   unsigned N) {
1532   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
1533   return new (Mem) OMPAffinityClause(N);
1534 }
1535 
Create(const ASTContext & C,Expr * InteropVar,ArrayRef<Expr * > PrefExprs,bool IsTarget,bool IsTargetSync,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation VarLoc,SourceLocation EndLoc)1536 OMPInitClause *OMPInitClause::Create(const ASTContext &C, Expr *InteropVar,
1537                                      ArrayRef<Expr *> PrefExprs, bool IsTarget,
1538                                      bool IsTargetSync, SourceLocation StartLoc,
1539                                      SourceLocation LParenLoc,
1540                                      SourceLocation VarLoc,
1541                                      SourceLocation EndLoc) {
1542 
1543   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(PrefExprs.size() + 1));
1544   auto *Clause =
1545       new (Mem) OMPInitClause(IsTarget, IsTargetSync, StartLoc, LParenLoc,
1546                               VarLoc, EndLoc, PrefExprs.size() + 1);
1547   Clause->setInteropVar(InteropVar);
1548   llvm::copy(PrefExprs, Clause->getTrailingObjects<Expr *>() + 1);
1549   return Clause;
1550 }
1551 
CreateEmpty(const ASTContext & C,unsigned N)1552 OMPInitClause *OMPInitClause::CreateEmpty(const ASTContext &C, unsigned N) {
1553   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1554   return new (Mem) OMPInitClause(N);
1555 }
1556 
1557 //===----------------------------------------------------------------------===//
1558 //  OpenMP clauses printing methods
1559 //===----------------------------------------------------------------------===//
1560 
VisitOMPIfClause(OMPIfClause * Node)1561 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
1562   OS << "if(";
1563   if (Node->getNameModifier() != OMPD_unknown)
1564     OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
1565   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1566   OS << ")";
1567 }
1568 
VisitOMPFinalClause(OMPFinalClause * Node)1569 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
1570   OS << "final(";
1571   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1572   OS << ")";
1573 }
1574 
VisitOMPNumThreadsClause(OMPNumThreadsClause * Node)1575 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
1576   OS << "num_threads(";
1577   Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
1578   OS << ")";
1579 }
1580 
VisitOMPSafelenClause(OMPSafelenClause * Node)1581 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
1582   OS << "safelen(";
1583   Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
1584   OS << ")";
1585 }
1586 
VisitOMPSimdlenClause(OMPSimdlenClause * Node)1587 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
1588   OS << "simdlen(";
1589   Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
1590   OS << ")";
1591 }
1592 
VisitOMPSizesClause(OMPSizesClause * Node)1593 void OMPClausePrinter::VisitOMPSizesClause(OMPSizesClause *Node) {
1594   OS << "sizes(";
1595   bool First = true;
1596   for (auto Size : Node->getSizesRefs()) {
1597     if (!First)
1598       OS << ", ";
1599     Size->printPretty(OS, nullptr, Policy, 0);
1600     First = false;
1601   }
1602   OS << ")";
1603 }
1604 
VisitOMPAllocatorClause(OMPAllocatorClause * Node)1605 void OMPClausePrinter::VisitOMPAllocatorClause(OMPAllocatorClause *Node) {
1606   OS << "allocator(";
1607   Node->getAllocator()->printPretty(OS, nullptr, Policy, 0);
1608   OS << ")";
1609 }
1610 
VisitOMPCollapseClause(OMPCollapseClause * Node)1611 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
1612   OS << "collapse(";
1613   Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
1614   OS << ")";
1615 }
1616 
VisitOMPDetachClause(OMPDetachClause * Node)1617 void OMPClausePrinter::VisitOMPDetachClause(OMPDetachClause *Node) {
1618   OS << "detach(";
1619   Node->getEventHandler()->printPretty(OS, nullptr, Policy, 0);
1620   OS << ")";
1621 }
1622 
VisitOMPDefaultClause(OMPDefaultClause * Node)1623 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
1624   OS << "default("
1625      << getOpenMPSimpleClauseTypeName(OMPC_default,
1626                                       unsigned(Node->getDefaultKind()))
1627      << ")";
1628 }
1629 
VisitOMPProcBindClause(OMPProcBindClause * Node)1630 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
1631   OS << "proc_bind("
1632      << getOpenMPSimpleClauseTypeName(OMPC_proc_bind,
1633                                       unsigned(Node->getProcBindKind()))
1634      << ")";
1635 }
1636 
VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *)1637 void OMPClausePrinter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {
1638   OS << "unified_address";
1639 }
1640 
VisitOMPUnifiedSharedMemoryClause(OMPUnifiedSharedMemoryClause *)1641 void OMPClausePrinter::VisitOMPUnifiedSharedMemoryClause(
1642     OMPUnifiedSharedMemoryClause *) {
1643   OS << "unified_shared_memory";
1644 }
1645 
VisitOMPReverseOffloadClause(OMPReverseOffloadClause *)1646 void OMPClausePrinter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {
1647   OS << "reverse_offload";
1648 }
1649 
VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *)1650 void OMPClausePrinter::VisitOMPDynamicAllocatorsClause(
1651     OMPDynamicAllocatorsClause *) {
1652   OS << "dynamic_allocators";
1653 }
1654 
VisitOMPAtomicDefaultMemOrderClause(OMPAtomicDefaultMemOrderClause * Node)1655 void OMPClausePrinter::VisitOMPAtomicDefaultMemOrderClause(
1656     OMPAtomicDefaultMemOrderClause *Node) {
1657   OS << "atomic_default_mem_order("
1658      << getOpenMPSimpleClauseTypeName(OMPC_atomic_default_mem_order,
1659                                       Node->getAtomicDefaultMemOrderKind())
1660      << ")";
1661 }
1662 
VisitOMPScheduleClause(OMPScheduleClause * Node)1663 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
1664   OS << "schedule(";
1665   if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1666     OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1667                                         Node->getFirstScheduleModifier());
1668     if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1669       OS << ", ";
1670       OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1671                                           Node->getSecondScheduleModifier());
1672     }
1673     OS << ": ";
1674   }
1675   OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
1676   if (auto *E = Node->getChunkSize()) {
1677     OS << ", ";
1678     E->printPretty(OS, nullptr, Policy);
1679   }
1680   OS << ")";
1681 }
1682 
VisitOMPOrderedClause(OMPOrderedClause * Node)1683 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
1684   OS << "ordered";
1685   if (auto *Num = Node->getNumForLoops()) {
1686     OS << "(";
1687     Num->printPretty(OS, nullptr, Policy, 0);
1688     OS << ")";
1689   }
1690 }
1691 
VisitOMPNowaitClause(OMPNowaitClause *)1692 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
1693   OS << "nowait";
1694 }
1695 
VisitOMPUntiedClause(OMPUntiedClause *)1696 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
1697   OS << "untied";
1698 }
1699 
VisitOMPNogroupClause(OMPNogroupClause *)1700 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
1701   OS << "nogroup";
1702 }
1703 
VisitOMPMergeableClause(OMPMergeableClause *)1704 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
1705   OS << "mergeable";
1706 }
1707 
VisitOMPReadClause(OMPReadClause *)1708 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
1709 
VisitOMPWriteClause(OMPWriteClause *)1710 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
1711 
VisitOMPUpdateClause(OMPUpdateClause * Node)1712 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *Node) {
1713   OS << "update";
1714   if (Node->isExtended()) {
1715     OS << "(";
1716     OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
1717                                         Node->getDependencyKind());
1718     OS << ")";
1719   }
1720 }
1721 
VisitOMPCaptureClause(OMPCaptureClause *)1722 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
1723   OS << "capture";
1724 }
1725 
VisitOMPSeqCstClause(OMPSeqCstClause *)1726 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
1727   OS << "seq_cst";
1728 }
1729 
VisitOMPAcqRelClause(OMPAcqRelClause *)1730 void OMPClausePrinter::VisitOMPAcqRelClause(OMPAcqRelClause *) {
1731   OS << "acq_rel";
1732 }
1733 
VisitOMPAcquireClause(OMPAcquireClause *)1734 void OMPClausePrinter::VisitOMPAcquireClause(OMPAcquireClause *) {
1735   OS << "acquire";
1736 }
1737 
VisitOMPReleaseClause(OMPReleaseClause *)1738 void OMPClausePrinter::VisitOMPReleaseClause(OMPReleaseClause *) {
1739   OS << "release";
1740 }
1741 
VisitOMPRelaxedClause(OMPRelaxedClause *)1742 void OMPClausePrinter::VisitOMPRelaxedClause(OMPRelaxedClause *) {
1743   OS << "relaxed";
1744 }
1745 
VisitOMPThreadsClause(OMPThreadsClause *)1746 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
1747   OS << "threads";
1748 }
1749 
VisitOMPSIMDClause(OMPSIMDClause *)1750 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
1751 
VisitOMPDeviceClause(OMPDeviceClause * Node)1752 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
1753   OS << "device(";
1754   OpenMPDeviceClauseModifier Modifier = Node->getModifier();
1755   if (Modifier != OMPC_DEVICE_unknown) {
1756     OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
1757        << ": ";
1758   }
1759   Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
1760   OS << ")";
1761 }
1762 
VisitOMPNumTeamsClause(OMPNumTeamsClause * Node)1763 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
1764   OS << "num_teams(";
1765   Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
1766   OS << ")";
1767 }
1768 
VisitOMPThreadLimitClause(OMPThreadLimitClause * Node)1769 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
1770   OS << "thread_limit(";
1771   Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
1772   OS << ")";
1773 }
1774 
VisitOMPPriorityClause(OMPPriorityClause * Node)1775 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
1776   OS << "priority(";
1777   Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
1778   OS << ")";
1779 }
1780 
VisitOMPGrainsizeClause(OMPGrainsizeClause * Node)1781 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
1782   OS << "grainsize(";
1783   Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
1784   OS << ")";
1785 }
1786 
VisitOMPNumTasksClause(OMPNumTasksClause * Node)1787 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
1788   OS << "num_tasks(";
1789   Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
1790   OS << ")";
1791 }
1792 
VisitOMPHintClause(OMPHintClause * Node)1793 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
1794   OS << "hint(";
1795   Node->getHint()->printPretty(OS, nullptr, Policy, 0);
1796   OS << ")";
1797 }
1798 
VisitOMPInitClause(OMPInitClause * Node)1799 void OMPClausePrinter::VisitOMPInitClause(OMPInitClause *Node) {
1800   OS << "init(";
1801   bool First = true;
1802   for (const Expr *E : Node->prefs()) {
1803     if (First)
1804       OS << "prefer_type(";
1805     else
1806       OS << ",";
1807     E->printPretty(OS, nullptr, Policy);
1808     First = false;
1809   }
1810   if (!First)
1811     OS << "), ";
1812   if (Node->getIsTarget())
1813     OS << "target";
1814   if (Node->getIsTargetSync()) {
1815     if (Node->getIsTarget())
1816       OS << ", ";
1817     OS << "targetsync";
1818   }
1819   OS << " : ";
1820   Node->getInteropVar()->printPretty(OS, nullptr, Policy);
1821   OS << ")";
1822 }
1823 
VisitOMPUseClause(OMPUseClause * Node)1824 void OMPClausePrinter::VisitOMPUseClause(OMPUseClause *Node) {
1825   OS << "use(";
1826   Node->getInteropVar()->printPretty(OS, nullptr, Policy);
1827   OS << ")";
1828 }
1829 
VisitOMPDestroyClause(OMPDestroyClause * Node)1830 void OMPClausePrinter::VisitOMPDestroyClause(OMPDestroyClause *Node) {
1831   OS << "destroy";
1832   if (Expr *E = Node->getInteropVar()) {
1833     OS << "(";
1834     E->printPretty(OS, nullptr, Policy);
1835     OS << ")";
1836   }
1837 }
1838 
VisitOMPNovariantsClause(OMPNovariantsClause * Node)1839 void OMPClausePrinter::VisitOMPNovariantsClause(OMPNovariantsClause *Node) {
1840   OS << "novariants";
1841   if (Expr *E = Node->getCondition()) {
1842     OS << "(";
1843     E->printPretty(OS, nullptr, Policy, 0);
1844     OS << ")";
1845   }
1846 }
1847 
VisitOMPNocontextClause(OMPNocontextClause * Node)1848 void OMPClausePrinter::VisitOMPNocontextClause(OMPNocontextClause *Node) {
1849   OS << "nocontext";
1850   if (Expr *E = Node->getCondition()) {
1851     OS << "(";
1852     E->printPretty(OS, nullptr, Policy, 0);
1853     OS << ")";
1854   }
1855 }
1856 
1857 template<typename T>
VisitOMPClauseList(T * Node,char StartSym)1858 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
1859   for (typename T::varlist_iterator I = Node->varlist_begin(),
1860                                     E = Node->varlist_end();
1861        I != E; ++I) {
1862     assert(*I && "Expected non-null Stmt");
1863     OS << (I == Node->varlist_begin() ? StartSym : ',');
1864     if (auto *DRE = dyn_cast<DeclRefExpr>(*I)) {
1865       if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
1866         DRE->printPretty(OS, nullptr, Policy, 0);
1867       else
1868         DRE->getDecl()->printQualifiedName(OS);
1869     } else
1870       (*I)->printPretty(OS, nullptr, Policy, 0);
1871   }
1872 }
1873 
VisitOMPAllocateClause(OMPAllocateClause * Node)1874 void OMPClausePrinter::VisitOMPAllocateClause(OMPAllocateClause *Node) {
1875   if (Node->varlist_empty())
1876     return;
1877   OS << "allocate";
1878   if (Expr *Allocator = Node->getAllocator()) {
1879     OS << "(";
1880     Allocator->printPretty(OS, nullptr, Policy, 0);
1881     OS << ":";
1882     VisitOMPClauseList(Node, ' ');
1883   } else {
1884     VisitOMPClauseList(Node, '(');
1885   }
1886   OS << ")";
1887 }
1888 
VisitOMPPrivateClause(OMPPrivateClause * Node)1889 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
1890   if (!Node->varlist_empty()) {
1891     OS << "private";
1892     VisitOMPClauseList(Node, '(');
1893     OS << ")";
1894   }
1895 }
1896 
VisitOMPFirstprivateClause(OMPFirstprivateClause * Node)1897 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
1898   if (!Node->varlist_empty()) {
1899     OS << "firstprivate";
1900     VisitOMPClauseList(Node, '(');
1901     OS << ")";
1902   }
1903 }
1904 
VisitOMPLastprivateClause(OMPLastprivateClause * Node)1905 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
1906   if (!Node->varlist_empty()) {
1907     OS << "lastprivate";
1908     OpenMPLastprivateModifier LPKind = Node->getKind();
1909     if (LPKind != OMPC_LASTPRIVATE_unknown) {
1910       OS << "("
1911          << getOpenMPSimpleClauseTypeName(OMPC_lastprivate, Node->getKind())
1912          << ":";
1913     }
1914     VisitOMPClauseList(Node, LPKind == OMPC_LASTPRIVATE_unknown ? '(' : ' ');
1915     OS << ")";
1916   }
1917 }
1918 
VisitOMPSharedClause(OMPSharedClause * Node)1919 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
1920   if (!Node->varlist_empty()) {
1921     OS << "shared";
1922     VisitOMPClauseList(Node, '(');
1923     OS << ")";
1924   }
1925 }
1926 
VisitOMPReductionClause(OMPReductionClause * Node)1927 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
1928   if (!Node->varlist_empty()) {
1929     OS << "reduction(";
1930     if (Node->getModifierLoc().isValid())
1931       OS << getOpenMPSimpleClauseTypeName(OMPC_reduction, Node->getModifier())
1932          << ", ";
1933     NestedNameSpecifier *QualifierLoc =
1934         Node->getQualifierLoc().getNestedNameSpecifier();
1935     OverloadedOperatorKind OOK =
1936         Node->getNameInfo().getName().getCXXOverloadedOperator();
1937     if (QualifierLoc == nullptr && OOK != OO_None) {
1938       // Print reduction identifier in C format
1939       OS << getOperatorSpelling(OOK);
1940     } else {
1941       // Use C++ format
1942       if (QualifierLoc != nullptr)
1943         QualifierLoc->print(OS, Policy);
1944       OS << Node->getNameInfo();
1945     }
1946     OS << ":";
1947     VisitOMPClauseList(Node, ' ');
1948     OS << ")";
1949   }
1950 }
1951 
VisitOMPTaskReductionClause(OMPTaskReductionClause * Node)1952 void OMPClausePrinter::VisitOMPTaskReductionClause(
1953     OMPTaskReductionClause *Node) {
1954   if (!Node->varlist_empty()) {
1955     OS << "task_reduction(";
1956     NestedNameSpecifier *QualifierLoc =
1957         Node->getQualifierLoc().getNestedNameSpecifier();
1958     OverloadedOperatorKind OOK =
1959         Node->getNameInfo().getName().getCXXOverloadedOperator();
1960     if (QualifierLoc == nullptr && OOK != OO_None) {
1961       // Print reduction identifier in C format
1962       OS << getOperatorSpelling(OOK);
1963     } else {
1964       // Use C++ format
1965       if (QualifierLoc != nullptr)
1966         QualifierLoc->print(OS, Policy);
1967       OS << Node->getNameInfo();
1968     }
1969     OS << ":";
1970     VisitOMPClauseList(Node, ' ');
1971     OS << ")";
1972   }
1973 }
1974 
VisitOMPInReductionClause(OMPInReductionClause * Node)1975 void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
1976   if (!Node->varlist_empty()) {
1977     OS << "in_reduction(";
1978     NestedNameSpecifier *QualifierLoc =
1979         Node->getQualifierLoc().getNestedNameSpecifier();
1980     OverloadedOperatorKind OOK =
1981         Node->getNameInfo().getName().getCXXOverloadedOperator();
1982     if (QualifierLoc == nullptr && OOK != OO_None) {
1983       // Print reduction identifier in C format
1984       OS << getOperatorSpelling(OOK);
1985     } else {
1986       // Use C++ format
1987       if (QualifierLoc != nullptr)
1988         QualifierLoc->print(OS, Policy);
1989       OS << Node->getNameInfo();
1990     }
1991     OS << ":";
1992     VisitOMPClauseList(Node, ' ');
1993     OS << ")";
1994   }
1995 }
1996 
VisitOMPLinearClause(OMPLinearClause * Node)1997 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
1998   if (!Node->varlist_empty()) {
1999     OS << "linear";
2000     if (Node->getModifierLoc().isValid()) {
2001       OS << '('
2002          << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
2003     }
2004     VisitOMPClauseList(Node, '(');
2005     if (Node->getModifierLoc().isValid())
2006       OS << ')';
2007     if (Node->getStep() != nullptr) {
2008       OS << ": ";
2009       Node->getStep()->printPretty(OS, nullptr, Policy, 0);
2010     }
2011     OS << ")";
2012   }
2013 }
2014 
VisitOMPAlignedClause(OMPAlignedClause * Node)2015 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
2016   if (!Node->varlist_empty()) {
2017     OS << "aligned";
2018     VisitOMPClauseList(Node, '(');
2019     if (Node->getAlignment() != nullptr) {
2020       OS << ": ";
2021       Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
2022     }
2023     OS << ")";
2024   }
2025 }
2026 
VisitOMPCopyinClause(OMPCopyinClause * Node)2027 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
2028   if (!Node->varlist_empty()) {
2029     OS << "copyin";
2030     VisitOMPClauseList(Node, '(');
2031     OS << ")";
2032   }
2033 }
2034 
VisitOMPCopyprivateClause(OMPCopyprivateClause * Node)2035 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
2036   if (!Node->varlist_empty()) {
2037     OS << "copyprivate";
2038     VisitOMPClauseList(Node, '(');
2039     OS << ")";
2040   }
2041 }
2042 
VisitOMPFlushClause(OMPFlushClause * Node)2043 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
2044   if (!Node->varlist_empty()) {
2045     VisitOMPClauseList(Node, '(');
2046     OS << ")";
2047   }
2048 }
2049 
VisitOMPDepobjClause(OMPDepobjClause * Node)2050 void OMPClausePrinter::VisitOMPDepobjClause(OMPDepobjClause *Node) {
2051   OS << "(";
2052   Node->getDepobj()->printPretty(OS, nullptr, Policy, 0);
2053   OS << ")";
2054 }
2055 
VisitOMPDependClause(OMPDependClause * Node)2056 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
2057   OS << "depend(";
2058   if (Expr *DepModifier = Node->getModifier()) {
2059     DepModifier->printPretty(OS, nullptr, Policy);
2060     OS << ", ";
2061   }
2062   OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2063                                       Node->getDependencyKind());
2064   if (!Node->varlist_empty()) {
2065     OS << " :";
2066     VisitOMPClauseList(Node, ' ');
2067   }
2068   OS << ")";
2069 }
2070 
2071 template <typename T>
PrintMapper(raw_ostream & OS,T * Node,const PrintingPolicy & Policy)2072 static void PrintMapper(raw_ostream &OS, T *Node,
2073                         const PrintingPolicy &Policy) {
2074   OS << '(';
2075   NestedNameSpecifier *MapperNNS =
2076       Node->getMapperQualifierLoc().getNestedNameSpecifier();
2077   if (MapperNNS)
2078     MapperNNS->print(OS, Policy);
2079   OS << Node->getMapperIdInfo() << ')';
2080 }
2081 
VisitOMPMapClause(OMPMapClause * Node)2082 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
2083   if (!Node->varlist_empty()) {
2084     OS << "map(";
2085     if (Node->getMapType() != OMPC_MAP_unknown) {
2086       for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
2087         if (Node->getMapTypeModifier(I) != OMPC_MAP_MODIFIER_unknown) {
2088           OS << getOpenMPSimpleClauseTypeName(OMPC_map,
2089                                               Node->getMapTypeModifier(I));
2090           if (Node->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_mapper)
2091             PrintMapper(OS, Node, Policy);
2092           OS << ',';
2093         }
2094       }
2095       OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
2096       OS << ':';
2097     }
2098     VisitOMPClauseList(Node, ' ');
2099     OS << ")";
2100   }
2101 }
2102 
VisitOMPMotionClause(T * Node)2103 template <typename T> void OMPClausePrinter::VisitOMPMotionClause(T *Node) {
2104   if (Node->varlist_empty())
2105     return;
2106   OS << getOpenMPClauseName(Node->getClauseKind());
2107   unsigned ModifierCount = 0;
2108   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2109     if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown)
2110       ++ModifierCount;
2111   }
2112   if (ModifierCount) {
2113     OS << '(';
2114     for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2115       if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown) {
2116         OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2117                                             Node->getMotionModifier(I));
2118         if (Node->getMotionModifier(I) == OMPC_MOTION_MODIFIER_mapper)
2119           PrintMapper(OS, Node, Policy);
2120         if (I < ModifierCount - 1)
2121           OS << ", ";
2122       }
2123     }
2124     OS << ':';
2125     VisitOMPClauseList(Node, ' ');
2126   } else {
2127     VisitOMPClauseList(Node, '(');
2128   }
2129   OS << ")";
2130 }
2131 
VisitOMPToClause(OMPToClause * Node)2132 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
2133   VisitOMPMotionClause(Node);
2134 }
2135 
VisitOMPFromClause(OMPFromClause * Node)2136 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
2137   VisitOMPMotionClause(Node);
2138 }
2139 
VisitOMPDistScheduleClause(OMPDistScheduleClause * Node)2140 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
2141   OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
2142                            OMPC_dist_schedule, Node->getDistScheduleKind());
2143   if (auto *E = Node->getChunkSize()) {
2144     OS << ", ";
2145     E->printPretty(OS, nullptr, Policy);
2146   }
2147   OS << ")";
2148 }
2149 
VisitOMPDefaultmapClause(OMPDefaultmapClause * Node)2150 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
2151   OS << "defaultmap(";
2152   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2153                                       Node->getDefaultmapModifier());
2154   if (Node->getDefaultmapKind() != OMPC_DEFAULTMAP_unknown) {
2155     OS << ": ";
2156     OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2157                                         Node->getDefaultmapKind());
2158   }
2159   OS << ")";
2160 }
2161 
VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause * Node)2162 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
2163   if (!Node->varlist_empty()) {
2164     OS << "use_device_ptr";
2165     VisitOMPClauseList(Node, '(');
2166     OS << ")";
2167   }
2168 }
2169 
VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause * Node)2170 void OMPClausePrinter::VisitOMPUseDeviceAddrClause(
2171     OMPUseDeviceAddrClause *Node) {
2172   if (!Node->varlist_empty()) {
2173     OS << "use_device_addr";
2174     VisitOMPClauseList(Node, '(');
2175     OS << ")";
2176   }
2177 }
2178 
VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause * Node)2179 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
2180   if (!Node->varlist_empty()) {
2181     OS << "is_device_ptr";
2182     VisitOMPClauseList(Node, '(');
2183     OS << ")";
2184   }
2185 }
2186 
VisitOMPNontemporalClause(OMPNontemporalClause * Node)2187 void OMPClausePrinter::VisitOMPNontemporalClause(OMPNontemporalClause *Node) {
2188   if (!Node->varlist_empty()) {
2189     OS << "nontemporal";
2190     VisitOMPClauseList(Node, '(');
2191     OS << ")";
2192   }
2193 }
2194 
VisitOMPOrderClause(OMPOrderClause * Node)2195 void OMPClausePrinter::VisitOMPOrderClause(OMPOrderClause *Node) {
2196   OS << "order(" << getOpenMPSimpleClauseTypeName(OMPC_order, Node->getKind())
2197      << ")";
2198 }
2199 
VisitOMPInclusiveClause(OMPInclusiveClause * Node)2200 void OMPClausePrinter::VisitOMPInclusiveClause(OMPInclusiveClause *Node) {
2201   if (!Node->varlist_empty()) {
2202     OS << "inclusive";
2203     VisitOMPClauseList(Node, '(');
2204     OS << ")";
2205   }
2206 }
2207 
VisitOMPExclusiveClause(OMPExclusiveClause * Node)2208 void OMPClausePrinter::VisitOMPExclusiveClause(OMPExclusiveClause *Node) {
2209   if (!Node->varlist_empty()) {
2210     OS << "exclusive";
2211     VisitOMPClauseList(Node, '(');
2212     OS << ")";
2213   }
2214 }
2215 
VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause * Node)2216 void OMPClausePrinter::VisitOMPUsesAllocatorsClause(
2217     OMPUsesAllocatorsClause *Node) {
2218   if (Node->getNumberOfAllocators() == 0)
2219     return;
2220   OS << "uses_allocators(";
2221   for (unsigned I = 0, E = Node->getNumberOfAllocators(); I < E; ++I) {
2222     OMPUsesAllocatorsClause::Data Data = Node->getAllocatorData(I);
2223     Data.Allocator->printPretty(OS, nullptr, Policy);
2224     if (Data.AllocatorTraits) {
2225       OS << "(";
2226       Data.AllocatorTraits->printPretty(OS, nullptr, Policy);
2227       OS << ")";
2228     }
2229     if (I < E - 1)
2230       OS << ",";
2231   }
2232   OS << ")";
2233 }
2234 
VisitOMPAffinityClause(OMPAffinityClause * Node)2235 void OMPClausePrinter::VisitOMPAffinityClause(OMPAffinityClause *Node) {
2236   if (Node->varlist_empty())
2237     return;
2238   OS << "affinity";
2239   char StartSym = '(';
2240   if (Expr *Modifier = Node->getModifier()) {
2241     OS << "(";
2242     Modifier->printPretty(OS, nullptr, Policy);
2243     OS << " :";
2244     StartSym = ' ';
2245   }
2246   VisitOMPClauseList(Node, StartSym);
2247   OS << ")";
2248 }
2249 
VisitOMPFilterClause(OMPFilterClause * Node)2250 void OMPClausePrinter::VisitOMPFilterClause(OMPFilterClause *Node) {
2251   OS << "filter(";
2252   Node->getThreadID()->printPretty(OS, nullptr, Policy, 0);
2253   OS << ")";
2254 }
2255 
getAsVariantMatchInfo(ASTContext & ASTCtx,VariantMatchInfo & VMI) const2256 void OMPTraitInfo::getAsVariantMatchInfo(ASTContext &ASTCtx,
2257                                          VariantMatchInfo &VMI) const {
2258   for (const OMPTraitSet &Set : Sets) {
2259     for (const OMPTraitSelector &Selector : Set.Selectors) {
2260 
2261       // User conditions are special as we evaluate the condition here.
2262       if (Selector.Kind == TraitSelector::user_condition) {
2263         assert(Selector.ScoreOrCondition &&
2264                "Ill-formed user condition, expected condition expression!");
2265         assert(Selector.Properties.size() == 1 &&
2266                Selector.Properties.front().Kind ==
2267                    TraitProperty::user_condition_unknown &&
2268                "Ill-formed user condition, expected unknown trait property!");
2269 
2270         if (Optional<APSInt> CondVal =
2271                 Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx))
2272           VMI.addTrait(CondVal->isNullValue()
2273                            ? TraitProperty::user_condition_false
2274                            : TraitProperty::user_condition_true,
2275                        "<condition>");
2276         else
2277           VMI.addTrait(TraitProperty::user_condition_false, "<condition>");
2278         continue;
2279       }
2280 
2281       Optional<llvm::APSInt> Score;
2282       llvm::APInt *ScorePtr = nullptr;
2283       if (Selector.ScoreOrCondition) {
2284         if ((Score = Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx)))
2285           ScorePtr = &*Score;
2286         else
2287           VMI.addTrait(TraitProperty::user_condition_false,
2288                        "<non-constant-score>");
2289       }
2290 
2291       for (const OMPTraitProperty &Property : Selector.Properties)
2292         VMI.addTrait(Set.Kind, Property.Kind, Property.RawString, ScorePtr);
2293 
2294       if (Set.Kind != TraitSet::construct)
2295         continue;
2296 
2297       // TODO: This might not hold once we implement SIMD properly.
2298       assert(Selector.Properties.size() == 1 &&
2299              Selector.Properties.front().Kind ==
2300                  getOpenMPContextTraitPropertyForSelector(
2301                      Selector.Kind) &&
2302              "Ill-formed construct selector!");
2303 
2304       VMI.ConstructTraits.push_back(Selector.Properties.front().Kind);
2305     }
2306   }
2307 }
2308 
print(llvm::raw_ostream & OS,const PrintingPolicy & Policy) const2309 void OMPTraitInfo::print(llvm::raw_ostream &OS,
2310                          const PrintingPolicy &Policy) const {
2311   bool FirstSet = true;
2312   for (const OMPTraitSet &Set : Sets) {
2313     if (!FirstSet)
2314       OS << ", ";
2315     FirstSet = false;
2316     OS << getOpenMPContextTraitSetName(Set.Kind) << "={";
2317 
2318     bool FirstSelector = true;
2319     for (const OMPTraitSelector &Selector : Set.Selectors) {
2320       if (!FirstSelector)
2321         OS << ", ";
2322       FirstSelector = false;
2323       OS << getOpenMPContextTraitSelectorName(Selector.Kind);
2324 
2325       bool AllowsTraitScore = false;
2326       bool RequiresProperty = false;
2327       isValidTraitSelectorForTraitSet(
2328           Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2329 
2330       if (!RequiresProperty)
2331         continue;
2332 
2333       OS << "(";
2334       if (Selector.Kind == TraitSelector::user_condition) {
2335         if (Selector.ScoreOrCondition)
2336           Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2337         else
2338           OS << "...";
2339       } else {
2340 
2341         if (Selector.ScoreOrCondition) {
2342           OS << "score(";
2343           Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2344           OS << "): ";
2345         }
2346 
2347         bool FirstProperty = true;
2348         for (const OMPTraitProperty &Property : Selector.Properties) {
2349           if (!FirstProperty)
2350             OS << ", ";
2351           FirstProperty = false;
2352           OS << getOpenMPContextTraitPropertyName(Property.Kind,
2353                                                   Property.RawString);
2354         }
2355       }
2356       OS << ")";
2357     }
2358     OS << "}";
2359   }
2360 }
2361 
getMangledName() const2362 std::string OMPTraitInfo::getMangledName() const {
2363   std::string MangledName;
2364   llvm::raw_string_ostream OS(MangledName);
2365   for (const OMPTraitSet &Set : Sets) {
2366     OS << '$' << 'S' << unsigned(Set.Kind);
2367     for (const OMPTraitSelector &Selector : Set.Selectors) {
2368 
2369       bool AllowsTraitScore = false;
2370       bool RequiresProperty = false;
2371       isValidTraitSelectorForTraitSet(
2372           Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2373       OS << '$' << 's' << unsigned(Selector.Kind);
2374 
2375       if (!RequiresProperty ||
2376           Selector.Kind == TraitSelector::user_condition)
2377         continue;
2378 
2379       for (const OMPTraitProperty &Property : Selector.Properties)
2380         OS << '$' << 'P'
2381            << getOpenMPContextTraitPropertyName(Property.Kind,
2382                                                 Property.RawString);
2383     }
2384   }
2385   return OS.str();
2386 }
2387 
OMPTraitInfo(StringRef MangledName)2388 OMPTraitInfo::OMPTraitInfo(StringRef MangledName) {
2389   unsigned long U;
2390   do {
2391     if (!MangledName.consume_front("$S"))
2392       break;
2393     if (MangledName.consumeInteger(10, U))
2394       break;
2395     Sets.push_back(OMPTraitSet());
2396     OMPTraitSet &Set = Sets.back();
2397     Set.Kind = TraitSet(U);
2398     do {
2399       if (!MangledName.consume_front("$s"))
2400         break;
2401       if (MangledName.consumeInteger(10, U))
2402         break;
2403       Set.Selectors.push_back(OMPTraitSelector());
2404       OMPTraitSelector &Selector = Set.Selectors.back();
2405       Selector.Kind = TraitSelector(U);
2406       do {
2407         if (!MangledName.consume_front("$P"))
2408           break;
2409         Selector.Properties.push_back(OMPTraitProperty());
2410         OMPTraitProperty &Property = Selector.Properties.back();
2411         std::pair<StringRef, StringRef> PropRestPair = MangledName.split('$');
2412         Property.RawString = PropRestPair.first;
2413         Property.Kind = getOpenMPContextTraitPropertyKind(
2414             Set.Kind, Selector.Kind, PropRestPair.first);
2415         MangledName = MangledName.drop_front(PropRestPair.first.size());
2416       } while (true);
2417     } while (true);
2418   } while (true);
2419 }
2420 
operator <<(llvm::raw_ostream & OS,const OMPTraitInfo & TI)2421 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2422                                      const OMPTraitInfo &TI) {
2423   LangOptions LO;
2424   PrintingPolicy Policy(LO);
2425   TI.print(OS, Policy);
2426   return OS;
2427 }
operator <<(llvm::raw_ostream & OS,const OMPTraitInfo * TI)2428 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2429                                      const OMPTraitInfo *TI) {
2430   return TI ? OS << *TI : OS;
2431 }
2432 
TargetOMPContext(ASTContext & ASTCtx,std::function<void (StringRef)> && DiagUnknownTrait,const FunctionDecl * CurrentFunctionDecl)2433 TargetOMPContext::TargetOMPContext(
2434     ASTContext &ASTCtx, std::function<void(StringRef)> &&DiagUnknownTrait,
2435     const FunctionDecl *CurrentFunctionDecl)
2436     : OMPContext(ASTCtx.getLangOpts().OpenMPIsDevice,
2437                  ASTCtx.getTargetInfo().getTriple()),
2438       FeatureValidityCheck([&](StringRef FeatureName) {
2439         return ASTCtx.getTargetInfo().isValidFeatureName(FeatureName);
2440       }),
2441       DiagUnknownTrait(std::move(DiagUnknownTrait)) {
2442   ASTCtx.getFunctionFeatureMap(FeatureMap, CurrentFunctionDecl);
2443 }
2444 
matchesISATrait(StringRef RawString) const2445 bool TargetOMPContext::matchesISATrait(StringRef RawString) const {
2446   auto It = FeatureMap.find(RawString);
2447   if (It != FeatureMap.end())
2448     return It->second;
2449   if (!FeatureValidityCheck(RawString))
2450     DiagUnknownTrait(RawString);
2451   return false;
2452 }
2453