]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/AST/OpenMPClause.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / AST / OpenMPClause.cpp
1 //===--- OpenMPClause.cpp - Classes for OpenMP clauses --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/OpenMPClause.h"
15
16 #include "clang/AST/ASTContext.h"
17
18 using namespace clang;
19
20 OMPClause::child_range OMPClause::children() {
21   switch (getClauseKind()) {
22   default:
23     break;
24 #define OPENMP_CLAUSE(Name, Class)                                             \
25   case OMPC_##Name:                                                            \
26     return static_cast<Class *>(this)->children();
27 #include "clang/Basic/OpenMPKinds.def"
28   }
29   llvm_unreachable("unknown OMPClause");
30 }
31
32 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
33   auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
34   return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
35 }
36
37 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
38   switch (C->getClauseKind()) {
39   case OMPC_schedule:
40     return static_cast<const OMPScheduleClause *>(C);
41   case OMPC_dist_schedule:
42     return static_cast<const OMPDistScheduleClause *>(C);
43   case OMPC_firstprivate:
44     return static_cast<const OMPFirstprivateClause *>(C);
45   case OMPC_lastprivate:
46     return static_cast<const OMPLastprivateClause *>(C);
47   case OMPC_reduction:
48     return static_cast<const OMPReductionClause *>(C);
49   case OMPC_linear:
50     return static_cast<const OMPLinearClause *>(C);
51   case OMPC_if:
52     return static_cast<const OMPIfClause *>(C);
53   case OMPC_num_threads:
54     return static_cast<const OMPNumThreadsClause *>(C);
55   case OMPC_num_teams:
56     return static_cast<const OMPNumTeamsClause *>(C);
57   case OMPC_thread_limit:
58     return static_cast<const OMPThreadLimitClause *>(C);
59   case OMPC_default:
60   case OMPC_proc_bind:
61   case OMPC_final:
62   case OMPC_safelen:
63   case OMPC_simdlen:
64   case OMPC_collapse:
65   case OMPC_private:
66   case OMPC_shared:
67   case OMPC_aligned:
68   case OMPC_copyin:
69   case OMPC_copyprivate:
70   case OMPC_ordered:
71   case OMPC_nowait:
72   case OMPC_untied:
73   case OMPC_mergeable:
74   case OMPC_threadprivate:
75   case OMPC_flush:
76   case OMPC_read:
77   case OMPC_write:
78   case OMPC_update:
79   case OMPC_capture:
80   case OMPC_seq_cst:
81   case OMPC_depend:
82   case OMPC_device:
83   case OMPC_threads:
84   case OMPC_simd:
85   case OMPC_map:
86   case OMPC_priority:
87   case OMPC_grainsize:
88   case OMPC_nogroup:
89   case OMPC_num_tasks:
90   case OMPC_hint:
91   case OMPC_defaultmap:
92   case OMPC_unknown:
93   case OMPC_uniform:
94   case OMPC_to:
95   case OMPC_from:
96   case OMPC_use_device_ptr:
97   case OMPC_is_device_ptr:
98     break;
99   }
100
101   return nullptr;
102 }
103
104 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
105   auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
106   return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
107 }
108
109 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
110   switch (C->getClauseKind()) {
111   case OMPC_lastprivate:
112     return static_cast<const OMPLastprivateClause *>(C);
113   case OMPC_reduction:
114     return static_cast<const OMPReductionClause *>(C);
115   case OMPC_linear:
116     return static_cast<const OMPLinearClause *>(C);
117   case OMPC_schedule:
118   case OMPC_dist_schedule:
119   case OMPC_firstprivate:
120   case OMPC_default:
121   case OMPC_proc_bind:
122   case OMPC_if:
123   case OMPC_final:
124   case OMPC_num_threads:
125   case OMPC_safelen:
126   case OMPC_simdlen:
127   case OMPC_collapse:
128   case OMPC_private:
129   case OMPC_shared:
130   case OMPC_aligned:
131   case OMPC_copyin:
132   case OMPC_copyprivate:
133   case OMPC_ordered:
134   case OMPC_nowait:
135   case OMPC_untied:
136   case OMPC_mergeable:
137   case OMPC_threadprivate:
138   case OMPC_flush:
139   case OMPC_read:
140   case OMPC_write:
141   case OMPC_update:
142   case OMPC_capture:
143   case OMPC_seq_cst:
144   case OMPC_depend:
145   case OMPC_device:
146   case OMPC_threads:
147   case OMPC_simd:
148   case OMPC_map:
149   case OMPC_num_teams:
150   case OMPC_thread_limit:
151   case OMPC_priority:
152   case OMPC_grainsize:
153   case OMPC_nogroup:
154   case OMPC_num_tasks:
155   case OMPC_hint:
156   case OMPC_defaultmap:
157   case OMPC_unknown:
158   case OMPC_uniform:
159   case OMPC_to:
160   case OMPC_from:
161   case OMPC_use_device_ptr:
162   case OMPC_is_device_ptr:
163     break;
164   }
165
166   return nullptr;
167 }
168
169 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
170   assert(VL.size() == varlist_size() &&
171          "Number of private copies is not the same as the preallocated buffer");
172   std::copy(VL.begin(), VL.end(), varlist_end());
173 }
174
175 OMPPrivateClause *
176 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
177                          SourceLocation LParenLoc, SourceLocation EndLoc,
178                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
179   // Allocate space for private variables and initializer expressions.
180   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
181   OMPPrivateClause *Clause =
182       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
183   Clause->setVarRefs(VL);
184   Clause->setPrivateCopies(PrivateVL);
185   return Clause;
186 }
187
188 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
189                                                 unsigned N) {
190   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
191   return new (Mem) OMPPrivateClause(N);
192 }
193
194 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
195   assert(VL.size() == varlist_size() &&
196          "Number of private copies is not the same as the preallocated buffer");
197   std::copy(VL.begin(), VL.end(), varlist_end());
198 }
199
200 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
201   assert(VL.size() == varlist_size() &&
202          "Number of inits is not the same as the preallocated buffer");
203   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
204 }
205
206 OMPFirstprivateClause *
207 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
208                               SourceLocation LParenLoc, SourceLocation EndLoc,
209                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
210                               ArrayRef<Expr *> InitVL, Stmt *PreInit) {
211   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
212   OMPFirstprivateClause *Clause =
213       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
214   Clause->setVarRefs(VL);
215   Clause->setPrivateCopies(PrivateVL);
216   Clause->setInits(InitVL);
217   Clause->setPreInitStmt(PreInit);
218   return Clause;
219 }
220
221 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
222                                                           unsigned N) {
223   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
224   return new (Mem) OMPFirstprivateClause(N);
225 }
226
227 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
228   assert(PrivateCopies.size() == varlist_size() &&
229          "Number of private copies is not the same as the preallocated buffer");
230   std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
231 }
232
233 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
234   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
235                                               "not the same as the "
236                                               "preallocated buffer");
237   std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
238 }
239
240 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
241   assert(DstExprs.size() == varlist_size() && "Number of destination "
242                                               "expressions is not the same as "
243                                               "the preallocated buffer");
244   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
245 }
246
247 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
248   assert(AssignmentOps.size() == varlist_size() &&
249          "Number of assignment expressions is not the same as the preallocated "
250          "buffer");
251   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
252             getDestinationExprs().end());
253 }
254
255 OMPLastprivateClause *OMPLastprivateClause::Create(
256     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
257     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
258     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
259     Expr *PostUpdate) {
260   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
261   OMPLastprivateClause *Clause =
262       new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
263   Clause->setVarRefs(VL);
264   Clause->setSourceExprs(SrcExprs);
265   Clause->setDestinationExprs(DstExprs);
266   Clause->setAssignmentOps(AssignmentOps);
267   Clause->setPreInitStmt(PreInit);
268   Clause->setPostUpdateExpr(PostUpdate);
269   return Clause;
270 }
271
272 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
273                                                         unsigned N) {
274   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
275   return new (Mem) OMPLastprivateClause(N);
276 }
277
278 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
279                                          SourceLocation StartLoc,
280                                          SourceLocation LParenLoc,
281                                          SourceLocation EndLoc,
282                                          ArrayRef<Expr *> VL) {
283   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
284   OMPSharedClause *Clause =
285       new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
286   Clause->setVarRefs(VL);
287   return Clause;
288 }
289
290 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
291   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
292   return new (Mem) OMPSharedClause(N);
293 }
294
295 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
296   assert(PL.size() == varlist_size() &&
297          "Number of privates is not the same as the preallocated buffer");
298   std::copy(PL.begin(), PL.end(), varlist_end());
299 }
300
301 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
302   assert(IL.size() == varlist_size() &&
303          "Number of inits is not the same as the preallocated buffer");
304   std::copy(IL.begin(), IL.end(), getPrivates().end());
305 }
306
307 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
308   assert(UL.size() == varlist_size() &&
309          "Number of updates is not the same as the preallocated buffer");
310   std::copy(UL.begin(), UL.end(), getInits().end());
311 }
312
313 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
314   assert(FL.size() == varlist_size() &&
315          "Number of final updates is not the same as the preallocated buffer");
316   std::copy(FL.begin(), FL.end(), getUpdates().end());
317 }
318
319 OMPLinearClause *OMPLinearClause::Create(
320     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
321     OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
322     SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
323     ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
324     Stmt *PreInit, Expr *PostUpdate) {
325   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
326   // (Step and CalcStep).
327   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
328   OMPLinearClause *Clause = new (Mem) OMPLinearClause(
329       StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
330   Clause->setVarRefs(VL);
331   Clause->setPrivates(PL);
332   Clause->setInits(IL);
333   // Fill update and final expressions with zeroes, they are provided later,
334   // after the directive construction.
335   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
336             nullptr);
337   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
338             nullptr);
339   Clause->setStep(Step);
340   Clause->setCalcStep(CalcStep);
341   Clause->setPreInitStmt(PreInit);
342   Clause->setPostUpdateExpr(PostUpdate);
343   return Clause;
344 }
345
346 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
347                                               unsigned NumVars) {
348   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
349   // (Step and CalcStep).
350   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
351   return new (Mem) OMPLinearClause(NumVars);
352 }
353
354 OMPAlignedClause *
355 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
356                          SourceLocation LParenLoc, SourceLocation ColonLoc,
357                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
358   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
359   OMPAlignedClause *Clause = new (Mem)
360       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
361   Clause->setVarRefs(VL);
362   Clause->setAlignment(A);
363   return Clause;
364 }
365
366 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
367                                                 unsigned NumVars) {
368   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
369   return new (Mem) OMPAlignedClause(NumVars);
370 }
371
372 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
373   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
374                                               "not the same as the "
375                                               "preallocated buffer");
376   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
377 }
378
379 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
380   assert(DstExprs.size() == varlist_size() && "Number of destination "
381                                               "expressions is not the same as "
382                                               "the preallocated buffer");
383   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
384 }
385
386 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
387   assert(AssignmentOps.size() == varlist_size() &&
388          "Number of assignment expressions is not the same as the preallocated "
389          "buffer");
390   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
391             getDestinationExprs().end());
392 }
393
394 OMPCopyinClause *OMPCopyinClause::Create(
395     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
396     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
397     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
398   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
399   OMPCopyinClause *Clause =
400       new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
401   Clause->setVarRefs(VL);
402   Clause->setSourceExprs(SrcExprs);
403   Clause->setDestinationExprs(DstExprs);
404   Clause->setAssignmentOps(AssignmentOps);
405   return Clause;
406 }
407
408 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
409   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
410   return new (Mem) OMPCopyinClause(N);
411 }
412
413 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
414   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
415                                               "not the same as the "
416                                               "preallocated buffer");
417   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
418 }
419
420 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
421   assert(DstExprs.size() == varlist_size() && "Number of destination "
422                                               "expressions is not the same as "
423                                               "the preallocated buffer");
424   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
425 }
426
427 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
428   assert(AssignmentOps.size() == varlist_size() &&
429          "Number of assignment expressions is not the same as the preallocated "
430          "buffer");
431   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
432             getDestinationExprs().end());
433 }
434
435 OMPCopyprivateClause *OMPCopyprivateClause::Create(
436     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
437     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
438     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
439   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
440   OMPCopyprivateClause *Clause =
441       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
442   Clause->setVarRefs(VL);
443   Clause->setSourceExprs(SrcExprs);
444   Clause->setDestinationExprs(DstExprs);
445   Clause->setAssignmentOps(AssignmentOps);
446   return Clause;
447 }
448
449 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
450                                                         unsigned N) {
451   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
452   return new (Mem) OMPCopyprivateClause(N);
453 }
454
455 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
456   assert(Privates.size() == varlist_size() &&
457          "Number of private copies is not the same as the preallocated buffer");
458   std::copy(Privates.begin(), Privates.end(), varlist_end());
459 }
460
461 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
462   assert(
463       LHSExprs.size() == varlist_size() &&
464       "Number of LHS expressions is not the same as the preallocated buffer");
465   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
466 }
467
468 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
469   assert(
470       RHSExprs.size() == varlist_size() &&
471       "Number of RHS expressions is not the same as the preallocated buffer");
472   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
473 }
474
475 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
476   assert(ReductionOps.size() == varlist_size() && "Number of reduction "
477                                                   "expressions is not the same "
478                                                   "as the preallocated buffer");
479   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
480 }
481
482 OMPReductionClause *OMPReductionClause::Create(
483     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
484     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
485     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
486     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
487     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
488     Expr *PostUpdate) {
489   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
490   OMPReductionClause *Clause = new (Mem) OMPReductionClause(
491       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
492   Clause->setVarRefs(VL);
493   Clause->setPrivates(Privates);
494   Clause->setLHSExprs(LHSExprs);
495   Clause->setRHSExprs(RHSExprs);
496   Clause->setReductionOps(ReductionOps);
497   Clause->setPreInitStmt(PreInit);
498   Clause->setPostUpdateExpr(PostUpdate);
499   return Clause;
500 }
501
502 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
503                                                     unsigned N) {
504   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
505   return new (Mem) OMPReductionClause(N);
506 }
507
508 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
509                                        SourceLocation StartLoc,
510                                        SourceLocation LParenLoc,
511                                        SourceLocation EndLoc,
512                                        ArrayRef<Expr *> VL) {
513   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
514   OMPFlushClause *Clause =
515       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
516   Clause->setVarRefs(VL);
517   return Clause;
518 }
519
520 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
521   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
522   return new (Mem) OMPFlushClause(N);
523 }
524
525 OMPDependClause *OMPDependClause::Create(
526     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
527     SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
528     SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
529   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
530   OMPDependClause *Clause =
531       new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
532   Clause->setVarRefs(VL);
533   Clause->setDependencyKind(DepKind);
534   Clause->setDependencyLoc(DepLoc);
535   Clause->setColonLoc(ColonLoc);
536   Clause->setCounterValue(nullptr);
537   return Clause;
538 }
539
540 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
541   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
542   return new (Mem) OMPDependClause(N);
543 }
544
545 void OMPDependClause::setCounterValue(Expr *V) {
546   assert(getDependencyKind() == OMPC_DEPEND_sink ||
547          getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
548   *getVarRefs().end() = V;
549 }
550
551 const Expr *OMPDependClause::getCounterValue() const {
552   auto *V = *getVarRefs().end();
553   assert(getDependencyKind() == OMPC_DEPEND_sink ||
554          getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
555   return V;
556 }
557
558 Expr *OMPDependClause::getCounterValue() {
559   auto *V = *getVarRefs().end();
560   assert(getDependencyKind() == OMPC_DEPEND_sink ||
561          getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
562   return V;
563 }
564
565 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
566     MappableExprComponentListsRef ComponentLists) {
567   unsigned TotalNum = 0u;
568   for (auto &C : ComponentLists)
569     TotalNum += C.size();
570   return TotalNum;
571 }
572
573 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
574     ArrayRef<ValueDecl *> Declarations) {
575   unsigned TotalNum = 0u;
576   llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
577   for (auto *D : Declarations) {
578     const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
579     if (Cache.count(VD))
580       continue;
581     ++TotalNum;
582     Cache.insert(VD);
583   }
584   return TotalNum;
585 }
586
587 OMPMapClause *
588 OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
589                      SourceLocation LParenLoc, SourceLocation EndLoc,
590                      ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
591                      MappableExprComponentListsRef ComponentLists,
592                      OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
593                      bool TypeIsImplicit, SourceLocation TypeLoc) {
594
595   unsigned NumVars = Vars.size();
596   unsigned NumUniqueDeclarations =
597       getUniqueDeclarationsTotalNumber(Declarations);
598   unsigned NumComponentLists = ComponentLists.size();
599   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
600
601   // We need to allocate:
602   // NumVars x Expr* - we have an original list expression for each clause list
603   // entry.
604   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
605   // with each component list.
606   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
607   // number of lists for each unique declaration and the size of each component
608   // list.
609   // NumComponents x MappableComponent - the total of all the components in all
610   // the lists.
611   void *Mem = C.Allocate(
612       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
613                        OMPClauseMappableExprCommon::MappableComponent>(
614           NumVars, NumUniqueDeclarations,
615           NumUniqueDeclarations + NumComponentLists, NumComponents));
616   OMPMapClause *Clause = new (Mem) OMPMapClause(
617       TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
618       NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
619
620   Clause->setVarRefs(Vars);
621   Clause->setClauseInfo(Declarations, ComponentLists);
622   Clause->setMapTypeModifier(TypeModifier);
623   Clause->setMapType(Type);
624   Clause->setMapLoc(TypeLoc);
625   return Clause;
626 }
627
628 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
629                                         unsigned NumUniqueDeclarations,
630                                         unsigned NumComponentLists,
631                                         unsigned NumComponents) {
632   void *Mem = C.Allocate(
633       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
634                        OMPClauseMappableExprCommon::MappableComponent>(
635           NumVars, NumUniqueDeclarations,
636           NumUniqueDeclarations + NumComponentLists, NumComponents));
637   return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
638                                 NumComponentLists, NumComponents);
639 }
640
641 OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
642                                  SourceLocation LParenLoc,
643                                  SourceLocation EndLoc, ArrayRef<Expr *> Vars,
644                                  ArrayRef<ValueDecl *> Declarations,
645                                  MappableExprComponentListsRef ComponentLists) {
646   unsigned NumVars = Vars.size();
647   unsigned NumUniqueDeclarations =
648       getUniqueDeclarationsTotalNumber(Declarations);
649   unsigned NumComponentLists = ComponentLists.size();
650   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
651
652   // We need to allocate:
653   // NumVars x Expr* - we have an original list expression for each clause list
654   // entry.
655   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
656   // with each component list.
657   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
658   // number of lists for each unique declaration and the size of each component
659   // list.
660   // NumComponents x MappableComponent - the total of all the components in all
661   // the lists.
662   void *Mem = C.Allocate(
663       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
664                        OMPClauseMappableExprCommon::MappableComponent>(
665           NumVars, NumUniqueDeclarations,
666           NumUniqueDeclarations + NumComponentLists, NumComponents));
667
668   OMPToClause *Clause = new (Mem)
669       OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
670                   NumComponentLists, NumComponents);
671
672   Clause->setVarRefs(Vars);
673   Clause->setClauseInfo(Declarations, ComponentLists);
674   return Clause;
675 }
676
677 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
678                                       unsigned NumUniqueDeclarations,
679                                       unsigned NumComponentLists,
680                                       unsigned NumComponents) {
681   void *Mem = C.Allocate(
682       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
683                        OMPClauseMappableExprCommon::MappableComponent>(
684           NumVars, NumUniqueDeclarations,
685           NumUniqueDeclarations + NumComponentLists, NumComponents));
686   return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
687                                NumComponentLists, NumComponents);
688 }
689
690 OMPFromClause *
691 OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
692                       SourceLocation LParenLoc, SourceLocation EndLoc,
693                       ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
694                       MappableExprComponentListsRef ComponentLists) {
695   unsigned NumVars = Vars.size();
696   unsigned NumUniqueDeclarations =
697       getUniqueDeclarationsTotalNumber(Declarations);
698   unsigned NumComponentLists = ComponentLists.size();
699   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
700
701   // We need to allocate:
702   // NumVars x Expr* - we have an original list expression for each clause list
703   // entry.
704   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
705   // with each component list.
706   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
707   // number of lists for each unique declaration and the size of each component
708   // list.
709   // NumComponents x MappableComponent - the total of all the components in all
710   // the lists.
711   void *Mem = C.Allocate(
712       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
713                        OMPClauseMappableExprCommon::MappableComponent>(
714           NumVars, NumUniqueDeclarations,
715           NumUniqueDeclarations + NumComponentLists, NumComponents));
716
717   OMPFromClause *Clause = new (Mem)
718       OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
719                     NumComponentLists, NumComponents);
720
721   Clause->setVarRefs(Vars);
722   Clause->setClauseInfo(Declarations, ComponentLists);
723   return Clause;
724 }
725
726 OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
727                                           unsigned NumUniqueDeclarations,
728                                           unsigned NumComponentLists,
729                                           unsigned NumComponents) {
730   void *Mem = C.Allocate(
731       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
732                        OMPClauseMappableExprCommon::MappableComponent>(
733           NumVars, NumUniqueDeclarations,
734           NumUniqueDeclarations + NumComponentLists, NumComponents));
735   return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
736                                  NumComponentLists, NumComponents);
737 }
738
739 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
740   assert(VL.size() == varlist_size() &&
741          "Number of private copies is not the same as the preallocated buffer");
742   std::copy(VL.begin(), VL.end(), varlist_end());
743 }
744
745 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
746   assert(VL.size() == varlist_size() &&
747          "Number of inits is not the same as the preallocated buffer");
748   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
749 }
750
751 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
752     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
753     SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
754     ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
755     MappableExprComponentListsRef ComponentLists) {
756   unsigned NumVars = Vars.size();
757   unsigned NumUniqueDeclarations =
758       getUniqueDeclarationsTotalNumber(Declarations);
759   unsigned NumComponentLists = ComponentLists.size();
760   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
761
762   // We need to allocate:
763   // 3 x NumVars x Expr* - we have an original list expression for each clause
764   // list entry and an equal number of private copies and inits.
765   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
766   // with each component list.
767   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
768   // number of lists for each unique declaration and the size of each component
769   // list.
770   // NumComponents x MappableComponent - the total of all the components in all
771   // the lists.
772   void *Mem = C.Allocate(
773       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
774                        OMPClauseMappableExprCommon::MappableComponent>(
775           3 * NumVars, NumUniqueDeclarations,
776           NumUniqueDeclarations + NumComponentLists, NumComponents));
777
778   OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
779       StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
780       NumComponentLists, NumComponents);
781
782   Clause->setVarRefs(Vars);
783   Clause->setPrivateCopies(PrivateVars);
784   Clause->setInits(Inits);
785   Clause->setClauseInfo(Declarations, ComponentLists);
786   return Clause;
787 }
788
789 OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
790     const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
791     unsigned NumComponentLists, unsigned NumComponents) {
792   void *Mem = C.Allocate(
793       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
794                        OMPClauseMappableExprCommon::MappableComponent>(
795           3 * NumVars, NumUniqueDeclarations,
796           NumUniqueDeclarations + NumComponentLists, NumComponents));
797   return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
798                                          NumComponentLists, NumComponents);
799 }
800
801 OMPIsDevicePtrClause *
802 OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
803                              SourceLocation LParenLoc, SourceLocation EndLoc,
804                              ArrayRef<Expr *> Vars,
805                              ArrayRef<ValueDecl *> Declarations,
806                              MappableExprComponentListsRef ComponentLists) {
807   unsigned NumVars = Vars.size();
808   unsigned NumUniqueDeclarations =
809       getUniqueDeclarationsTotalNumber(Declarations);
810   unsigned NumComponentLists = ComponentLists.size();
811   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
812
813   // We need to allocate:
814   // NumVars x Expr* - we have an original list expression for each clause list
815   // entry.
816   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
817   // with each component list.
818   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
819   // number of lists for each unique declaration and the size of each component
820   // list.
821   // NumComponents x MappableComponent - the total of all the components in all
822   // the lists.
823   void *Mem = C.Allocate(
824       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
825                        OMPClauseMappableExprCommon::MappableComponent>(
826           NumVars, NumUniqueDeclarations,
827           NumUniqueDeclarations + NumComponentLists, NumComponents));
828
829   OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
830       StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
831       NumComponentLists, NumComponents);
832
833   Clause->setVarRefs(Vars);
834   Clause->setClauseInfo(Declarations, ComponentLists);
835   return Clause;
836 }
837
838 OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
839     const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
840     unsigned NumComponentLists, unsigned NumComponents) {
841   void *Mem = C.Allocate(
842       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
843                        OMPClauseMappableExprCommon::MappableComponent>(
844           NumVars, NumUniqueDeclarations,
845           NumUniqueDeclarations + NumComponentLists, NumComponents));
846   return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
847                                         NumComponentLists, NumComponents);
848 }