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