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