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