]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/AST/OpenMPClause.h
Vendor import of stripped clang trunk r375505, the last commit before
[FreeBSD/FreeBSD.git] / include / clang / AST / OpenMPClause.h
1 //===- OpenMPClause.h - Classes for OpenMP clauses --------------*- C++ -*-===//
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 /// \file
10 /// This file defines OpenMP AST classes for clauses.
11 /// There are clauses for executable directives, clauses for declarative
12 /// directives and clauses which can be used in both kinds of directives.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CLANG_AST_OPENMPCLAUSE_H
17 #define LLVM_CLANG_AST_OPENMPCLAUSE_H
18
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclarationName.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/NestedNameSpecifier.h"
23 #include "clang/AST/Stmt.h"
24 #include "clang/AST/StmtIterator.h"
25 #include "clang/Basic/LLVM.h"
26 #include "clang/Basic/OpenMPKinds.h"
27 #include "clang/Basic/SourceLocation.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/iterator.h"
32 #include "llvm/ADT/iterator_range.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/TrailingObjects.h"
36 #include <cassert>
37 #include <cstddef>
38 #include <iterator>
39 #include <utility>
40
41 namespace clang {
42
43 class ASTContext;
44
45 //===----------------------------------------------------------------------===//
46 // AST classes for clauses.
47 //===----------------------------------------------------------------------===//
48
49 /// This is a basic class for representing single OpenMP clause.
50 class OMPClause {
51   /// Starting location of the clause (the clause keyword).
52   SourceLocation StartLoc;
53
54   /// Ending location of the clause.
55   SourceLocation EndLoc;
56
57   /// Kind of the clause.
58   OpenMPClauseKind Kind;
59
60 protected:
61   OMPClause(OpenMPClauseKind K, SourceLocation StartLoc, SourceLocation EndLoc)
62       : StartLoc(StartLoc), EndLoc(EndLoc), Kind(K) {}
63
64 public:
65   /// Returns the starting location of the clause.
66   SourceLocation getBeginLoc() const { return StartLoc; }
67
68   /// Returns the ending location of the clause.
69   SourceLocation getEndLoc() const { return EndLoc; }
70
71   /// Sets the starting location of the clause.
72   void setLocStart(SourceLocation Loc) { StartLoc = Loc; }
73
74   /// Sets the ending location of the clause.
75   void setLocEnd(SourceLocation Loc) { EndLoc = Loc; }
76
77   /// Returns kind of OpenMP clause (private, shared, reduction, etc.).
78   OpenMPClauseKind getClauseKind() const { return Kind; }
79
80   bool isImplicit() const { return StartLoc.isInvalid(); }
81
82   using child_iterator = StmtIterator;
83   using const_child_iterator = ConstStmtIterator;
84   using child_range = llvm::iterator_range<child_iterator>;
85   using const_child_range = llvm::iterator_range<const_child_iterator>;
86
87   child_range children();
88   const_child_range children() const {
89     auto Children = const_cast<OMPClause *>(this)->children();
90     return const_child_range(Children.begin(), Children.end());
91   }
92
93   /// Get the iterator range for the expressions used in the clauses. Used
94   /// expressions include only the children that must be evaluated at the
95   /// runtime before entering the construct.
96   child_range used_children();
97   const_child_range used_children() const {
98     auto Children = const_cast<OMPClause *>(this)->children();
99     return const_child_range(Children.begin(), Children.end());
100   }
101
102   static bool classof(const OMPClause *) { return true; }
103 };
104
105 /// Class that handles pre-initialization statement for some clauses, like
106 /// 'shedule', 'firstprivate' etc.
107 class OMPClauseWithPreInit {
108   friend class OMPClauseReader;
109
110   /// Pre-initialization statement for the clause.
111   Stmt *PreInit = nullptr;
112
113   /// Region that captures the associated stmt.
114   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
115
116 protected:
117   OMPClauseWithPreInit(const OMPClause *This) {
118     assert(get(This) && "get is not tuned for pre-init.");
119   }
120
121   /// Set pre-initialization statement for the clause.
122   void setPreInitStmt(Stmt *S, OpenMPDirectiveKind ThisRegion = OMPD_unknown) {
123     PreInit = S;
124     CaptureRegion = ThisRegion;
125   }
126
127 public:
128   /// Get pre-initialization statement for the clause.
129   const Stmt *getPreInitStmt() const { return PreInit; }
130
131   /// Get pre-initialization statement for the clause.
132   Stmt *getPreInitStmt() { return PreInit; }
133
134   /// Get capture region for the stmt in the clause.
135   OpenMPDirectiveKind getCaptureRegion() const { return CaptureRegion; }
136
137   static OMPClauseWithPreInit *get(OMPClause *C);
138   static const OMPClauseWithPreInit *get(const OMPClause *C);
139 };
140
141 /// Class that handles post-update expression for some clauses, like
142 /// 'lastprivate', 'reduction' etc.
143 class OMPClauseWithPostUpdate : public OMPClauseWithPreInit {
144   friend class OMPClauseReader;
145
146   /// Post-update expression for the clause.
147   Expr *PostUpdate = nullptr;
148
149 protected:
150   OMPClauseWithPostUpdate(const OMPClause *This) : OMPClauseWithPreInit(This) {
151     assert(get(This) && "get is not tuned for post-update.");
152   }
153
154   /// Set pre-initialization statement for the clause.
155   void setPostUpdateExpr(Expr *S) { PostUpdate = S; }
156
157 public:
158   /// Get post-update expression for the clause.
159   const Expr *getPostUpdateExpr() const { return PostUpdate; }
160
161   /// Get post-update expression for the clause.
162   Expr *getPostUpdateExpr() { return PostUpdate; }
163
164   static OMPClauseWithPostUpdate *get(OMPClause *C);
165   static const OMPClauseWithPostUpdate *get(const OMPClause *C);
166 };
167
168 /// This structure contains most locations needed for by an OMPVarListClause.
169 struct OMPVarListLocTy {
170   /// Starting location of the clause (the clause keyword).
171   SourceLocation StartLoc;
172   /// Location of '('.
173   SourceLocation LParenLoc;
174   /// Ending location of the clause.
175   SourceLocation EndLoc;
176   OMPVarListLocTy() = default;
177   OMPVarListLocTy(SourceLocation StartLoc, SourceLocation LParenLoc,
178                   SourceLocation EndLoc)
179       : StartLoc(StartLoc), LParenLoc(LParenLoc), EndLoc(EndLoc) {}
180 };
181
182 /// This represents clauses with the list of variables like 'private',
183 /// 'firstprivate', 'copyin', 'shared', or 'reduction' clauses in the
184 /// '#pragma omp ...' directives.
185 template <class T> class OMPVarListClause : public OMPClause {
186   friend class OMPClauseReader;
187
188   /// Location of '('.
189   SourceLocation LParenLoc;
190
191   /// Number of variables in the list.
192   unsigned NumVars;
193
194 protected:
195   /// Build a clause with \a N variables
196   ///
197   /// \param K Kind of the clause.
198   /// \param StartLoc Starting location of the clause (the clause keyword).
199   /// \param LParenLoc Location of '('.
200   /// \param EndLoc Ending location of the clause.
201   /// \param N Number of the variables in the clause.
202   OMPVarListClause(OpenMPClauseKind K, SourceLocation StartLoc,
203                    SourceLocation LParenLoc, SourceLocation EndLoc, unsigned N)
204       : OMPClause(K, StartLoc, EndLoc), LParenLoc(LParenLoc), NumVars(N) {}
205
206   /// Fetches list of variables associated with this clause.
207   MutableArrayRef<Expr *> getVarRefs() {
208     return MutableArrayRef<Expr *>(
209         static_cast<T *>(this)->template getTrailingObjects<Expr *>(), NumVars);
210   }
211
212   /// Sets the list of variables for this clause.
213   void setVarRefs(ArrayRef<Expr *> VL) {
214     assert(VL.size() == NumVars &&
215            "Number of variables is not the same as the preallocated buffer");
216     std::copy(VL.begin(), VL.end(),
217               static_cast<T *>(this)->template getTrailingObjects<Expr *>());
218   }
219
220 public:
221   using varlist_iterator = MutableArrayRef<Expr *>::iterator;
222   using varlist_const_iterator = ArrayRef<const Expr *>::iterator;
223   using varlist_range = llvm::iterator_range<varlist_iterator>;
224   using varlist_const_range = llvm::iterator_range<varlist_const_iterator>;
225
226   unsigned varlist_size() const { return NumVars; }
227   bool varlist_empty() const { return NumVars == 0; }
228
229   varlist_range varlists() {
230     return varlist_range(varlist_begin(), varlist_end());
231   }
232   varlist_const_range varlists() const {
233     return varlist_const_range(varlist_begin(), varlist_end());
234   }
235
236   varlist_iterator varlist_begin() { return getVarRefs().begin(); }
237   varlist_iterator varlist_end() { return getVarRefs().end(); }
238   varlist_const_iterator varlist_begin() const { return getVarRefs().begin(); }
239   varlist_const_iterator varlist_end() const { return getVarRefs().end(); }
240
241   /// Sets the location of '('.
242   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
243
244   /// Returns the location of '('.
245   SourceLocation getLParenLoc() const { return LParenLoc; }
246
247   /// Fetches list of all variables in the clause.
248   ArrayRef<const Expr *> getVarRefs() const {
249     return llvm::makeArrayRef(
250         static_cast<const T *>(this)->template getTrailingObjects<Expr *>(),
251         NumVars);
252   }
253 };
254
255 /// This represents 'allocator' clause in the '#pragma omp ...'
256 /// directive.
257 ///
258 /// \code
259 /// #pragma omp allocate(a) allocator(omp_default_mem_alloc)
260 /// \endcode
261 /// In this example directive '#pragma omp allocate' has simple 'allocator'
262 /// clause with the allocator 'omp_default_mem_alloc'.
263 class OMPAllocatorClause : public OMPClause {
264   friend class OMPClauseReader;
265
266   /// Location of '('.
267   SourceLocation LParenLoc;
268
269   /// Expression with the allocator.
270   Stmt *Allocator = nullptr;
271
272   /// Set allocator.
273   void setAllocator(Expr *A) { Allocator = A; }
274
275 public:
276   /// Build 'allocator' clause with the given allocator.
277   ///
278   /// \param A Allocator.
279   /// \param StartLoc Starting location of the clause.
280   /// \param LParenLoc Location of '('.
281   /// \param EndLoc Ending location of the clause.
282   OMPAllocatorClause(Expr *A, SourceLocation StartLoc, SourceLocation LParenLoc,
283                      SourceLocation EndLoc)
284       : OMPClause(OMPC_allocator, StartLoc, EndLoc), LParenLoc(LParenLoc),
285         Allocator(A) {}
286
287   /// Build an empty clause.
288   OMPAllocatorClause()
289       : OMPClause(OMPC_allocator, SourceLocation(), SourceLocation()) {}
290
291   /// Sets the location of '('.
292   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
293
294   /// Returns the location of '('.
295   SourceLocation getLParenLoc() const { return LParenLoc; }
296
297   /// Returns allocator.
298   Expr *getAllocator() const { return cast_or_null<Expr>(Allocator); }
299
300   child_range children() { return child_range(&Allocator, &Allocator + 1); }
301
302   const_child_range children() const {
303     return const_child_range(&Allocator, &Allocator + 1);
304   }
305
306   child_range used_children() {
307     return child_range(child_iterator(), child_iterator());
308   }
309   const_child_range used_children() const {
310     return const_child_range(const_child_iterator(), const_child_iterator());
311   }
312
313   static bool classof(const OMPClause *T) {
314     return T->getClauseKind() == OMPC_allocator;
315   }
316 };
317
318 /// This represents clause 'allocate' in the '#pragma omp ...' directives.
319 ///
320 /// \code
321 /// #pragma omp parallel private(a) allocate(omp_default_mem_alloc :a)
322 /// \endcode
323 /// In this example directive '#pragma omp parallel' has clause 'private'
324 /// and clause 'allocate' for the variable 'a'.
325 class OMPAllocateClause final
326     : public OMPVarListClause<OMPAllocateClause>,
327       private llvm::TrailingObjects<OMPAllocateClause, Expr *> {
328   friend class OMPClauseReader;
329   friend OMPVarListClause;
330   friend TrailingObjects;
331
332   /// Allocator specified in the clause, or 'nullptr' if the default one is
333   /// used.
334   Expr *Allocator = nullptr;
335   /// Position of the ':' delimiter in the clause;
336   SourceLocation ColonLoc;
337
338   /// Build clause with number of variables \a N.
339   ///
340   /// \param StartLoc Starting location of the clause.
341   /// \param LParenLoc Location of '('.
342   /// \param Allocator Allocator expression.
343   /// \param ColonLoc Location of ':' delimiter.
344   /// \param EndLoc Ending location of the clause.
345   /// \param N Number of the variables in the clause.
346   OMPAllocateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
347                     Expr *Allocator, SourceLocation ColonLoc,
348                     SourceLocation EndLoc, unsigned N)
349       : OMPVarListClause<OMPAllocateClause>(OMPC_allocate, StartLoc, LParenLoc,
350                                             EndLoc, N),
351         Allocator(Allocator), ColonLoc(ColonLoc) {}
352
353   /// Build an empty clause.
354   ///
355   /// \param N Number of variables.
356   explicit OMPAllocateClause(unsigned N)
357       : OMPVarListClause<OMPAllocateClause>(OMPC_allocate, SourceLocation(),
358                                             SourceLocation(), SourceLocation(),
359                                             N) {}
360
361   /// Sets location of ':' symbol in clause.
362   void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
363
364   void setAllocator(Expr *A) { Allocator = A; }
365
366 public:
367   /// Creates clause with a list of variables \a VL.
368   ///
369   /// \param C AST context.
370   /// \param StartLoc Starting location of the clause.
371   /// \param LParenLoc Location of '('.
372   /// \param Allocator Allocator expression.
373   /// \param ColonLoc Location of ':' delimiter.
374   /// \param EndLoc Ending location of the clause.
375   /// \param VL List of references to the variables.
376   static OMPAllocateClause *Create(const ASTContext &C, SourceLocation StartLoc,
377                                    SourceLocation LParenLoc, Expr *Allocator,
378                                    SourceLocation ColonLoc,
379                                    SourceLocation EndLoc, ArrayRef<Expr *> VL);
380
381   /// Returns the allocator expression or nullptr, if no allocator is specified.
382   Expr *getAllocator() const { return Allocator; }
383
384   /// Returns the location of the ':' delimiter.
385   SourceLocation getColonLoc() const { return ColonLoc; }
386
387   /// Creates an empty clause with the place for \a N variables.
388   ///
389   /// \param C AST context.
390   /// \param N The number of variables.
391   static OMPAllocateClause *CreateEmpty(const ASTContext &C, unsigned N);
392
393   child_range children() {
394     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
395                        reinterpret_cast<Stmt **>(varlist_end()));
396   }
397
398   const_child_range children() const {
399     auto Children = const_cast<OMPAllocateClause *>(this)->children();
400     return const_child_range(Children.begin(), Children.end());
401   }
402
403   child_range used_children() {
404     return child_range(child_iterator(), child_iterator());
405   }
406   const_child_range used_children() const {
407     return const_child_range(const_child_iterator(), const_child_iterator());
408   }
409
410   static bool classof(const OMPClause *T) {
411     return T->getClauseKind() == OMPC_allocate;
412   }
413 };
414
415 /// This represents 'if' clause in the '#pragma omp ...' directive.
416 ///
417 /// \code
418 /// #pragma omp parallel if(parallel:a > 5)
419 /// \endcode
420 /// In this example directive '#pragma omp parallel' has simple 'if' clause with
421 /// condition 'a > 5' and directive name modifier 'parallel'.
422 class OMPIfClause : public OMPClause, public OMPClauseWithPreInit {
423   friend class OMPClauseReader;
424
425   /// Location of '('.
426   SourceLocation LParenLoc;
427
428   /// Condition of the 'if' clause.
429   Stmt *Condition = nullptr;
430
431   /// Location of ':' (if any).
432   SourceLocation ColonLoc;
433
434   /// Directive name modifier for the clause.
435   OpenMPDirectiveKind NameModifier = OMPD_unknown;
436
437   /// Name modifier location.
438   SourceLocation NameModifierLoc;
439
440   /// Set condition.
441   void setCondition(Expr *Cond) { Condition = Cond; }
442
443   /// Set directive name modifier for the clause.
444   void setNameModifier(OpenMPDirectiveKind NM) { NameModifier = NM; }
445
446   /// Set location of directive name modifier for the clause.
447   void setNameModifierLoc(SourceLocation Loc) { NameModifierLoc = Loc; }
448
449   /// Set location of ':'.
450   void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
451
452 public:
453   /// Build 'if' clause with condition \a Cond.
454   ///
455   /// \param NameModifier [OpenMP 4.1] Directive name modifier of clause.
456   /// \param Cond Condition of the clause.
457   /// \param HelperCond Helper condition for the clause.
458   /// \param CaptureRegion Innermost OpenMP region where expressions in this
459   /// clause must be captured.
460   /// \param StartLoc Starting location of the clause.
461   /// \param LParenLoc Location of '('.
462   /// \param NameModifierLoc Location of directive name modifier.
463   /// \param ColonLoc [OpenMP 4.1] Location of ':'.
464   /// \param EndLoc Ending location of the clause.
465   OMPIfClause(OpenMPDirectiveKind NameModifier, Expr *Cond, Stmt *HelperCond,
466               OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
467               SourceLocation LParenLoc, SourceLocation NameModifierLoc,
468               SourceLocation ColonLoc, SourceLocation EndLoc)
469       : OMPClause(OMPC_if, StartLoc, EndLoc), OMPClauseWithPreInit(this),
470         LParenLoc(LParenLoc), Condition(Cond), ColonLoc(ColonLoc),
471         NameModifier(NameModifier), NameModifierLoc(NameModifierLoc) {
472     setPreInitStmt(HelperCond, CaptureRegion);
473   }
474
475   /// Build an empty clause.
476   OMPIfClause()
477       : OMPClause(OMPC_if, SourceLocation(), SourceLocation()),
478         OMPClauseWithPreInit(this) {}
479
480   /// Sets the location of '('.
481   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
482
483   /// Returns the location of '('.
484   SourceLocation getLParenLoc() const { return LParenLoc; }
485
486   /// Return the location of ':'.
487   SourceLocation getColonLoc() const { return ColonLoc; }
488
489   /// Returns condition.
490   Expr *getCondition() const { return cast_or_null<Expr>(Condition); }
491
492   /// Return directive name modifier associated with the clause.
493   OpenMPDirectiveKind getNameModifier() const { return NameModifier; }
494
495   /// Return the location of directive name modifier.
496   SourceLocation getNameModifierLoc() const { return NameModifierLoc; }
497
498   child_range children() { return child_range(&Condition, &Condition + 1); }
499
500   const_child_range children() const {
501     return const_child_range(&Condition, &Condition + 1);
502   }
503
504   child_range used_children();
505   const_child_range used_children() const {
506     auto Children = const_cast<OMPIfClause *>(this)->used_children();
507     return const_child_range(Children.begin(), Children.end());
508   }
509
510   static bool classof(const OMPClause *T) {
511     return T->getClauseKind() == OMPC_if;
512   }
513 };
514
515 /// This represents 'final' clause in the '#pragma omp ...' directive.
516 ///
517 /// \code
518 /// #pragma omp task final(a > 5)
519 /// \endcode
520 /// In this example directive '#pragma omp task' has simple 'final'
521 /// clause with condition 'a > 5'.
522 class OMPFinalClause : public OMPClause, public OMPClauseWithPreInit {
523   friend class OMPClauseReader;
524
525   /// Location of '('.
526   SourceLocation LParenLoc;
527
528   /// Condition of the 'if' clause.
529   Stmt *Condition = nullptr;
530
531   /// Set condition.
532   void setCondition(Expr *Cond) { Condition = Cond; }
533
534 public:
535   /// Build 'final' clause with condition \a Cond.
536   ///
537   /// \param Cond Condition of the clause.
538   /// \param HelperCond Helper condition for the construct.
539   /// \param CaptureRegion Innermost OpenMP region where expressions in this
540   /// clause must be captured.
541   /// \param StartLoc Starting location of the clause.
542   /// \param LParenLoc Location of '('.
543   /// \param EndLoc Ending location of the clause.
544   OMPFinalClause(Expr *Cond, Stmt *HelperCond,
545                  OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
546                  SourceLocation LParenLoc, SourceLocation EndLoc)
547       : OMPClause(OMPC_final, StartLoc, EndLoc), OMPClauseWithPreInit(this),
548         LParenLoc(LParenLoc), Condition(Cond) {
549     setPreInitStmt(HelperCond, CaptureRegion);
550   }
551
552   /// Build an empty clause.
553   OMPFinalClause()
554       : OMPClause(OMPC_final, SourceLocation(), SourceLocation()),
555         OMPClauseWithPreInit(this) {}
556
557   /// Sets the location of '('.
558   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
559
560   /// Returns the location of '('.
561   SourceLocation getLParenLoc() const { return LParenLoc; }
562
563   /// Returns condition.
564   Expr *getCondition() const { return cast_or_null<Expr>(Condition); }
565
566   child_range children() { return child_range(&Condition, &Condition + 1); }
567
568   const_child_range children() const {
569     return const_child_range(&Condition, &Condition + 1);
570   }
571
572   child_range used_children();
573   const_child_range used_children() const {
574     auto Children = const_cast<OMPFinalClause *>(this)->used_children();
575     return const_child_range(Children.begin(), Children.end());
576   }
577
578   static bool classof(const OMPClause *T) {
579     return T->getClauseKind() == OMPC_final;
580   }
581 };
582
583 /// This represents 'num_threads' clause in the '#pragma omp ...'
584 /// directive.
585 ///
586 /// \code
587 /// #pragma omp parallel num_threads(6)
588 /// \endcode
589 /// In this example directive '#pragma omp parallel' has simple 'num_threads'
590 /// clause with number of threads '6'.
591 class OMPNumThreadsClause : public OMPClause, public OMPClauseWithPreInit {
592   friend class OMPClauseReader;
593
594   /// Location of '('.
595   SourceLocation LParenLoc;
596
597   /// Condition of the 'num_threads' clause.
598   Stmt *NumThreads = nullptr;
599
600   /// Set condition.
601   void setNumThreads(Expr *NThreads) { NumThreads = NThreads; }
602
603 public:
604   /// Build 'num_threads' clause with condition \a NumThreads.
605   ///
606   /// \param NumThreads Number of threads for the construct.
607   /// \param HelperNumThreads Helper Number of threads for the construct.
608   /// \param CaptureRegion Innermost OpenMP region where expressions in this
609   /// clause must be captured.
610   /// \param StartLoc Starting location of the clause.
611   /// \param LParenLoc Location of '('.
612   /// \param EndLoc Ending location of the clause.
613   OMPNumThreadsClause(Expr *NumThreads, Stmt *HelperNumThreads,
614                       OpenMPDirectiveKind CaptureRegion,
615                       SourceLocation StartLoc, SourceLocation LParenLoc,
616                       SourceLocation EndLoc)
617       : OMPClause(OMPC_num_threads, StartLoc, EndLoc),
618         OMPClauseWithPreInit(this), LParenLoc(LParenLoc),
619         NumThreads(NumThreads) {
620     setPreInitStmt(HelperNumThreads, CaptureRegion);
621   }
622
623   /// Build an empty clause.
624   OMPNumThreadsClause()
625       : OMPClause(OMPC_num_threads, SourceLocation(), SourceLocation()),
626         OMPClauseWithPreInit(this) {}
627
628   /// Sets the location of '('.
629   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
630
631   /// Returns the location of '('.
632   SourceLocation getLParenLoc() const { return LParenLoc; }
633
634   /// Returns number of threads.
635   Expr *getNumThreads() const { return cast_or_null<Expr>(NumThreads); }
636
637   child_range children() { return child_range(&NumThreads, &NumThreads + 1); }
638
639   const_child_range children() const {
640     return const_child_range(&NumThreads, &NumThreads + 1);
641   }
642
643   child_range used_children() {
644     return child_range(child_iterator(), child_iterator());
645   }
646   const_child_range used_children() const {
647     return const_child_range(const_child_iterator(), const_child_iterator());
648   }
649
650   static bool classof(const OMPClause *T) {
651     return T->getClauseKind() == OMPC_num_threads;
652   }
653 };
654
655 /// This represents 'safelen' clause in the '#pragma omp ...'
656 /// directive.
657 ///
658 /// \code
659 /// #pragma omp simd safelen(4)
660 /// \endcode
661 /// In this example directive '#pragma omp simd' has clause 'safelen'
662 /// with single expression '4'.
663 /// If the safelen clause is used then no two iterations executed
664 /// concurrently with SIMD instructions can have a greater distance
665 /// in the logical iteration space than its value. The parameter of
666 /// the safelen clause must be a constant positive integer expression.
667 class OMPSafelenClause : public OMPClause {
668   friend class OMPClauseReader;
669
670   /// Location of '('.
671   SourceLocation LParenLoc;
672
673   /// Safe iteration space distance.
674   Stmt *Safelen = nullptr;
675
676   /// Set safelen.
677   void setSafelen(Expr *Len) { Safelen = Len; }
678
679 public:
680   /// Build 'safelen' clause.
681   ///
682   /// \param Len Expression associated with this clause.
683   /// \param StartLoc Starting location of the clause.
684   /// \param EndLoc Ending location of the clause.
685   OMPSafelenClause(Expr *Len, SourceLocation StartLoc, SourceLocation LParenLoc,
686                    SourceLocation EndLoc)
687       : OMPClause(OMPC_safelen, StartLoc, EndLoc), LParenLoc(LParenLoc),
688         Safelen(Len) {}
689
690   /// Build an empty clause.
691   explicit OMPSafelenClause()
692       : OMPClause(OMPC_safelen, SourceLocation(), SourceLocation()) {}
693
694   /// Sets the location of '('.
695   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
696
697   /// Returns the location of '('.
698   SourceLocation getLParenLoc() const { return LParenLoc; }
699
700   /// Return safe iteration space distance.
701   Expr *getSafelen() const { return cast_or_null<Expr>(Safelen); }
702
703   child_range children() { return child_range(&Safelen, &Safelen + 1); }
704
705   const_child_range children() const {
706     return const_child_range(&Safelen, &Safelen + 1);
707   }
708
709   child_range used_children() {
710     return child_range(child_iterator(), child_iterator());
711   }
712   const_child_range used_children() const {
713     return const_child_range(const_child_iterator(), const_child_iterator());
714   }
715
716   static bool classof(const OMPClause *T) {
717     return T->getClauseKind() == OMPC_safelen;
718   }
719 };
720
721 /// This represents 'simdlen' clause in the '#pragma omp ...'
722 /// directive.
723 ///
724 /// \code
725 /// #pragma omp simd simdlen(4)
726 /// \endcode
727 /// In this example directive '#pragma omp simd' has clause 'simdlen'
728 /// with single expression '4'.
729 /// If the 'simdlen' clause is used then it specifies the preferred number of
730 /// iterations to be executed concurrently. The parameter of the 'simdlen'
731 /// clause must be a constant positive integer expression.
732 class OMPSimdlenClause : public OMPClause {
733   friend class OMPClauseReader;
734
735   /// Location of '('.
736   SourceLocation LParenLoc;
737
738   /// Safe iteration space distance.
739   Stmt *Simdlen = nullptr;
740
741   /// Set simdlen.
742   void setSimdlen(Expr *Len) { Simdlen = Len; }
743
744 public:
745   /// Build 'simdlen' clause.
746   ///
747   /// \param Len Expression associated with this clause.
748   /// \param StartLoc Starting location of the clause.
749   /// \param EndLoc Ending location of the clause.
750   OMPSimdlenClause(Expr *Len, SourceLocation StartLoc, SourceLocation LParenLoc,
751                    SourceLocation EndLoc)
752       : OMPClause(OMPC_simdlen, StartLoc, EndLoc), LParenLoc(LParenLoc),
753         Simdlen(Len) {}
754
755   /// Build an empty clause.
756   explicit OMPSimdlenClause()
757       : OMPClause(OMPC_simdlen, SourceLocation(), SourceLocation()) {}
758
759   /// Sets the location of '('.
760   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
761
762   /// Returns the location of '('.
763   SourceLocation getLParenLoc() const { return LParenLoc; }
764
765   /// Return safe iteration space distance.
766   Expr *getSimdlen() const { return cast_or_null<Expr>(Simdlen); }
767
768   child_range children() { return child_range(&Simdlen, &Simdlen + 1); }
769
770   const_child_range children() const {
771     return const_child_range(&Simdlen, &Simdlen + 1);
772   }
773
774   child_range used_children() {
775     return child_range(child_iterator(), child_iterator());
776   }
777   const_child_range used_children() const {
778     return const_child_range(const_child_iterator(), const_child_iterator());
779   }
780
781   static bool classof(const OMPClause *T) {
782     return T->getClauseKind() == OMPC_simdlen;
783   }
784 };
785
786 /// This represents 'collapse' clause in the '#pragma omp ...'
787 /// directive.
788 ///
789 /// \code
790 /// #pragma omp simd collapse(3)
791 /// \endcode
792 /// In this example directive '#pragma omp simd' has clause 'collapse'
793 /// with single expression '3'.
794 /// The parameter must be a constant positive integer expression, it specifies
795 /// the number of nested loops that should be collapsed into a single iteration
796 /// space.
797 class OMPCollapseClause : public OMPClause {
798   friend class OMPClauseReader;
799
800   /// Location of '('.
801   SourceLocation LParenLoc;
802
803   /// Number of for-loops.
804   Stmt *NumForLoops = nullptr;
805
806   /// Set the number of associated for-loops.
807   void setNumForLoops(Expr *Num) { NumForLoops = Num; }
808
809 public:
810   /// Build 'collapse' clause.
811   ///
812   /// \param Num Expression associated with this clause.
813   /// \param StartLoc Starting location of the clause.
814   /// \param LParenLoc Location of '('.
815   /// \param EndLoc Ending location of the clause.
816   OMPCollapseClause(Expr *Num, SourceLocation StartLoc,
817                     SourceLocation LParenLoc, SourceLocation EndLoc)
818       : OMPClause(OMPC_collapse, StartLoc, EndLoc), LParenLoc(LParenLoc),
819         NumForLoops(Num) {}
820
821   /// Build an empty clause.
822   explicit OMPCollapseClause()
823       : OMPClause(OMPC_collapse, SourceLocation(), SourceLocation()) {}
824
825   /// Sets the location of '('.
826   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
827
828   /// Returns the location of '('.
829   SourceLocation getLParenLoc() const { return LParenLoc; }
830
831   /// Return the number of associated for-loops.
832   Expr *getNumForLoops() const { return cast_or_null<Expr>(NumForLoops); }
833
834   child_range children() { return child_range(&NumForLoops, &NumForLoops + 1); }
835
836   const_child_range children() const {
837     return const_child_range(&NumForLoops, &NumForLoops + 1);
838   }
839
840   child_range used_children() {
841     return child_range(child_iterator(), child_iterator());
842   }
843   const_child_range used_children() const {
844     return const_child_range(const_child_iterator(), const_child_iterator());
845   }
846
847   static bool classof(const OMPClause *T) {
848     return T->getClauseKind() == OMPC_collapse;
849   }
850 };
851
852 /// This represents 'default' clause in the '#pragma omp ...' directive.
853 ///
854 /// \code
855 /// #pragma omp parallel default(shared)
856 /// \endcode
857 /// In this example directive '#pragma omp parallel' has simple 'default'
858 /// clause with kind 'shared'.
859 class OMPDefaultClause : public OMPClause {
860   friend class OMPClauseReader;
861
862   /// Location of '('.
863   SourceLocation LParenLoc;
864
865   /// A kind of the 'default' clause.
866   OpenMPDefaultClauseKind Kind = OMPC_DEFAULT_unknown;
867
868   /// Start location of the kind in source code.
869   SourceLocation KindKwLoc;
870
871   /// Set kind of the clauses.
872   ///
873   /// \param K Argument of clause.
874   void setDefaultKind(OpenMPDefaultClauseKind K) { Kind = K; }
875
876   /// Set argument location.
877   ///
878   /// \param KLoc Argument location.
879   void setDefaultKindKwLoc(SourceLocation KLoc) { KindKwLoc = KLoc; }
880
881 public:
882   /// Build 'default' clause with argument \a A ('none' or 'shared').
883   ///
884   /// \param A Argument of the clause ('none' or 'shared').
885   /// \param ALoc Starting location of the argument.
886   /// \param StartLoc Starting location of the clause.
887   /// \param LParenLoc Location of '('.
888   /// \param EndLoc Ending location of the clause.
889   OMPDefaultClause(OpenMPDefaultClauseKind A, SourceLocation ALoc,
890                    SourceLocation StartLoc, SourceLocation LParenLoc,
891                    SourceLocation EndLoc)
892       : OMPClause(OMPC_default, StartLoc, EndLoc), LParenLoc(LParenLoc),
893         Kind(A), KindKwLoc(ALoc) {}
894
895   /// Build an empty clause.
896   OMPDefaultClause()
897       : OMPClause(OMPC_default, SourceLocation(), SourceLocation()) {}
898
899   /// Sets the location of '('.
900   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
901
902   /// Returns the location of '('.
903   SourceLocation getLParenLoc() const { return LParenLoc; }
904
905   /// Returns kind of the clause.
906   OpenMPDefaultClauseKind getDefaultKind() const { return Kind; }
907
908   /// Returns location of clause kind.
909   SourceLocation getDefaultKindKwLoc() const { return KindKwLoc; }
910
911   child_range children() {
912     return child_range(child_iterator(), child_iterator());
913   }
914
915   const_child_range children() const {
916     return const_child_range(const_child_iterator(), const_child_iterator());
917   }
918
919   child_range used_children() {
920     return child_range(child_iterator(), child_iterator());
921   }
922   const_child_range used_children() const {
923     return const_child_range(const_child_iterator(), const_child_iterator());
924   }
925
926   static bool classof(const OMPClause *T) {
927     return T->getClauseKind() == OMPC_default;
928   }
929 };
930
931 /// This represents 'proc_bind' clause in the '#pragma omp ...'
932 /// directive.
933 ///
934 /// \code
935 /// #pragma omp parallel proc_bind(master)
936 /// \endcode
937 /// In this example directive '#pragma omp parallel' has simple 'proc_bind'
938 /// clause with kind 'master'.
939 class OMPProcBindClause : public OMPClause {
940   friend class OMPClauseReader;
941
942   /// Location of '('.
943   SourceLocation LParenLoc;
944
945   /// A kind of the 'proc_bind' clause.
946   OpenMPProcBindClauseKind Kind = OMPC_PROC_BIND_unknown;
947
948   /// Start location of the kind in source code.
949   SourceLocation KindKwLoc;
950
951   /// Set kind of the clause.
952   ///
953   /// \param K Kind of clause.
954   void setProcBindKind(OpenMPProcBindClauseKind K) { Kind = K; }
955
956   /// Set clause kind location.
957   ///
958   /// \param KLoc Kind location.
959   void setProcBindKindKwLoc(SourceLocation KLoc) { KindKwLoc = KLoc; }
960
961 public:
962   /// Build 'proc_bind' clause with argument \a A ('master', 'close' or
963   ///        'spread').
964   ///
965   /// \param A Argument of the clause ('master', 'close' or 'spread').
966   /// \param ALoc Starting location of the argument.
967   /// \param StartLoc Starting location of the clause.
968   /// \param LParenLoc Location of '('.
969   /// \param EndLoc Ending location of the clause.
970   OMPProcBindClause(OpenMPProcBindClauseKind A, SourceLocation ALoc,
971                     SourceLocation StartLoc, SourceLocation LParenLoc,
972                     SourceLocation EndLoc)
973       : OMPClause(OMPC_proc_bind, StartLoc, EndLoc), LParenLoc(LParenLoc),
974         Kind(A), KindKwLoc(ALoc) {}
975
976   /// Build an empty clause.
977   OMPProcBindClause()
978       : OMPClause(OMPC_proc_bind, SourceLocation(), SourceLocation()) {}
979
980   /// Sets the location of '('.
981   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
982
983   /// Returns the location of '('.
984   SourceLocation getLParenLoc() const { return LParenLoc; }
985
986   /// Returns kind of the clause.
987   OpenMPProcBindClauseKind getProcBindKind() const { return Kind; }
988
989   /// Returns location of clause kind.
990   SourceLocation getProcBindKindKwLoc() const { return KindKwLoc; }
991
992   child_range children() {
993     return child_range(child_iterator(), child_iterator());
994   }
995
996   const_child_range children() const {
997     return const_child_range(const_child_iterator(), const_child_iterator());
998   }
999
1000   child_range used_children() {
1001     return child_range(child_iterator(), child_iterator());
1002   }
1003   const_child_range used_children() const {
1004     return const_child_range(const_child_iterator(), const_child_iterator());
1005   }
1006
1007   static bool classof(const OMPClause *T) {
1008     return T->getClauseKind() == OMPC_proc_bind;
1009   }
1010 };
1011
1012 /// This represents 'unified_address' clause in the '#pragma omp requires'
1013 /// directive.
1014 ///
1015 /// \code
1016 /// #pragma omp requires unified_address
1017 /// \endcode
1018 /// In this example directive '#pragma omp requires' has 'unified_address'
1019 /// clause.
1020 class OMPUnifiedAddressClause final : public OMPClause {
1021 public:
1022   friend class OMPClauseReader;
1023   /// Build 'unified_address' clause.
1024   ///
1025   /// \param StartLoc Starting location of the clause.
1026   /// \param EndLoc Ending location of the clause.
1027   OMPUnifiedAddressClause(SourceLocation StartLoc, SourceLocation EndLoc)
1028       : OMPClause(OMPC_unified_address, StartLoc, EndLoc) {}
1029
1030   /// Build an empty clause.
1031   OMPUnifiedAddressClause()
1032       : OMPClause(OMPC_unified_address, SourceLocation(), SourceLocation()) {}
1033
1034   child_range children() {
1035     return child_range(child_iterator(), child_iterator());
1036   }
1037
1038   const_child_range children() const {
1039     return const_child_range(const_child_iterator(), const_child_iterator());
1040   }
1041
1042   child_range used_children() {
1043     return child_range(child_iterator(), child_iterator());
1044   }
1045   const_child_range used_children() const {
1046     return const_child_range(const_child_iterator(), const_child_iterator());
1047   }
1048
1049   static bool classof(const OMPClause *T) {
1050     return T->getClauseKind() == OMPC_unified_address;
1051   }
1052 };
1053
1054 /// This represents 'unified_shared_memory' clause in the '#pragma omp requires'
1055 /// directive.
1056 ///
1057 /// \code
1058 /// #pragma omp requires unified_shared_memory
1059 /// \endcode
1060 /// In this example directive '#pragma omp requires' has 'unified_shared_memory'
1061 /// clause.
1062 class OMPUnifiedSharedMemoryClause final : public OMPClause {
1063 public:
1064   friend class OMPClauseReader;
1065   /// Build 'unified_shared_memory' clause.
1066   ///
1067   /// \param StartLoc Starting location of the clause.
1068   /// \param EndLoc Ending location of the clause.
1069   OMPUnifiedSharedMemoryClause(SourceLocation StartLoc, SourceLocation EndLoc)
1070       : OMPClause(OMPC_unified_shared_memory, StartLoc, EndLoc) {}
1071
1072   /// Build an empty clause.
1073   OMPUnifiedSharedMemoryClause()
1074       : OMPClause(OMPC_unified_shared_memory, SourceLocation(), SourceLocation()) {}
1075
1076   child_range children() {
1077     return child_range(child_iterator(), child_iterator());
1078   }
1079
1080   const_child_range children() const {
1081     return const_child_range(const_child_iterator(), const_child_iterator());
1082   }
1083
1084   child_range used_children() {
1085     return child_range(child_iterator(), child_iterator());
1086   }
1087   const_child_range used_children() const {
1088     return const_child_range(const_child_iterator(), const_child_iterator());
1089   }
1090
1091   static bool classof(const OMPClause *T) {
1092     return T->getClauseKind() == OMPC_unified_shared_memory;
1093   }
1094 };
1095
1096 /// This represents 'reverse_offload' clause in the '#pragma omp requires'
1097 /// directive.
1098 ///
1099 /// \code
1100 /// #pragma omp requires reverse_offload
1101 /// \endcode
1102 /// In this example directive '#pragma omp requires' has 'reverse_offload'
1103 /// clause.
1104 class OMPReverseOffloadClause final : public OMPClause {
1105 public:
1106   friend class OMPClauseReader;
1107   /// Build 'reverse_offload' clause.
1108   ///
1109   /// \param StartLoc Starting location of the clause.
1110   /// \param EndLoc Ending location of the clause.
1111   OMPReverseOffloadClause(SourceLocation StartLoc, SourceLocation EndLoc)
1112       : OMPClause(OMPC_reverse_offload, StartLoc, EndLoc) {}
1113
1114   /// Build an empty clause.
1115   OMPReverseOffloadClause()
1116       : OMPClause(OMPC_reverse_offload, SourceLocation(), SourceLocation()) {}
1117
1118   child_range children() {
1119     return child_range(child_iterator(), child_iterator());
1120   }
1121
1122   const_child_range children() const {
1123     return const_child_range(const_child_iterator(), const_child_iterator());
1124   }
1125
1126   child_range used_children() {
1127     return child_range(child_iterator(), child_iterator());
1128   }
1129   const_child_range used_children() const {
1130     return const_child_range(const_child_iterator(), const_child_iterator());
1131   }
1132
1133   static bool classof(const OMPClause *T) {
1134     return T->getClauseKind() == OMPC_reverse_offload;
1135   }
1136 };
1137
1138 /// This represents 'dynamic_allocators' clause in the '#pragma omp requires'
1139 /// directive.
1140 ///
1141 /// \code
1142 /// #pragma omp requires dynamic_allocators
1143 /// \endcode
1144 /// In this example directive '#pragma omp requires' has 'dynamic_allocators'
1145 /// clause.
1146 class OMPDynamicAllocatorsClause final : public OMPClause {
1147 public:
1148   friend class OMPClauseReader;
1149   /// Build 'dynamic_allocators' clause.
1150   ///
1151   /// \param StartLoc Starting location of the clause.
1152   /// \param EndLoc Ending location of the clause.
1153   OMPDynamicAllocatorsClause(SourceLocation StartLoc, SourceLocation EndLoc)
1154       : OMPClause(OMPC_dynamic_allocators, StartLoc, EndLoc) {}
1155
1156   /// Build an empty clause.
1157   OMPDynamicAllocatorsClause()
1158       : OMPClause(OMPC_dynamic_allocators, SourceLocation(), SourceLocation()) {
1159   }
1160
1161   child_range children() {
1162     return child_range(child_iterator(), child_iterator());
1163   }
1164
1165   const_child_range children() const {
1166     return const_child_range(const_child_iterator(), const_child_iterator());
1167   }
1168
1169   child_range used_children() {
1170     return child_range(child_iterator(), child_iterator());
1171   }
1172   const_child_range used_children() const {
1173     return const_child_range(const_child_iterator(), const_child_iterator());
1174   }
1175
1176   static bool classof(const OMPClause *T) {
1177     return T->getClauseKind() == OMPC_dynamic_allocators;
1178   }
1179 };
1180
1181 /// This represents 'atomic_default_mem_order' clause in the '#pragma omp
1182 /// requires'  directive.
1183 ///
1184 /// \code
1185 /// #pragma omp requires atomic_default_mem_order(seq_cst)
1186 /// \endcode
1187 /// In this example directive '#pragma omp requires' has simple
1188 /// atomic_default_mem_order' clause with kind 'seq_cst'.
1189 class OMPAtomicDefaultMemOrderClause final : public OMPClause {
1190   friend class OMPClauseReader;
1191
1192   /// Location of '('
1193   SourceLocation LParenLoc;
1194
1195   /// A kind of the 'atomic_default_mem_order' clause.
1196   OpenMPAtomicDefaultMemOrderClauseKind Kind =
1197       OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown;
1198
1199   /// Start location of the kind in source code.
1200   SourceLocation KindKwLoc;
1201
1202   /// Set kind of the clause.
1203   ///
1204   /// \param K Kind of clause.
1205   void setAtomicDefaultMemOrderKind(OpenMPAtomicDefaultMemOrderClauseKind K) {
1206     Kind = K;
1207   }
1208
1209   /// Set clause kind location.
1210   ///
1211   /// \param KLoc Kind location.
1212   void setAtomicDefaultMemOrderKindKwLoc(SourceLocation KLoc) {
1213     KindKwLoc = KLoc;
1214   }
1215
1216 public:
1217   /// Build 'atomic_default_mem_order' clause with argument \a A ('seq_cst',
1218   /// 'acq_rel' or 'relaxed').
1219   ///
1220   /// \param A Argument of the clause ('seq_cst', 'acq_rel' or 'relaxed').
1221   /// \param ALoc Starting location of the argument.
1222   /// \param StartLoc Starting location of the clause.
1223   /// \param LParenLoc Location of '('.
1224   /// \param EndLoc Ending location of the clause.
1225   OMPAtomicDefaultMemOrderClause(OpenMPAtomicDefaultMemOrderClauseKind A,
1226                                  SourceLocation ALoc, SourceLocation StartLoc,
1227                                  SourceLocation LParenLoc,
1228                                  SourceLocation EndLoc)
1229       : OMPClause(OMPC_atomic_default_mem_order, StartLoc, EndLoc),
1230         LParenLoc(LParenLoc), Kind(A), KindKwLoc(ALoc) {}
1231
1232   /// Build an empty clause.
1233   OMPAtomicDefaultMemOrderClause()
1234       : OMPClause(OMPC_atomic_default_mem_order, SourceLocation(),
1235                   SourceLocation()) {}
1236
1237   /// Sets the location of '('.
1238   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
1239
1240   /// Returns the locaiton of '('.
1241   SourceLocation getLParenLoc() const { return LParenLoc; }
1242
1243   /// Returns kind of the clause.
1244   OpenMPAtomicDefaultMemOrderClauseKind getAtomicDefaultMemOrderKind() const {
1245     return Kind;
1246   }
1247
1248   /// Returns location of clause kind.
1249   SourceLocation getAtomicDefaultMemOrderKindKwLoc() const { return KindKwLoc; }
1250
1251   child_range children() {
1252     return child_range(child_iterator(), child_iterator());
1253   }
1254
1255   const_child_range children() const {
1256     return const_child_range(const_child_iterator(), const_child_iterator());
1257   }
1258
1259   child_range used_children() {
1260     return child_range(child_iterator(), child_iterator());
1261   }
1262   const_child_range used_children() const {
1263     return const_child_range(const_child_iterator(), const_child_iterator());
1264   }
1265
1266   static bool classof(const OMPClause *T) {
1267     return T->getClauseKind() == OMPC_atomic_default_mem_order;
1268   }
1269 };
1270
1271 /// This represents 'schedule' clause in the '#pragma omp ...' directive.
1272 ///
1273 /// \code
1274 /// #pragma omp for schedule(static, 3)
1275 /// \endcode
1276 /// In this example directive '#pragma omp for' has 'schedule' clause with
1277 /// arguments 'static' and '3'.
1278 class OMPScheduleClause : public OMPClause, public OMPClauseWithPreInit {
1279   friend class OMPClauseReader;
1280
1281   /// Location of '('.
1282   SourceLocation LParenLoc;
1283
1284   /// A kind of the 'schedule' clause.
1285   OpenMPScheduleClauseKind Kind = OMPC_SCHEDULE_unknown;
1286
1287   /// Modifiers for 'schedule' clause.
1288   enum {FIRST, SECOND, NUM_MODIFIERS};
1289   OpenMPScheduleClauseModifier Modifiers[NUM_MODIFIERS];
1290
1291   /// Locations of modifiers.
1292   SourceLocation ModifiersLoc[NUM_MODIFIERS];
1293
1294   /// Start location of the schedule ind in source code.
1295   SourceLocation KindLoc;
1296
1297   /// Location of ',' (if any).
1298   SourceLocation CommaLoc;
1299
1300   /// Chunk size.
1301   Expr *ChunkSize = nullptr;
1302
1303   /// Set schedule kind.
1304   ///
1305   /// \param K Schedule kind.
1306   void setScheduleKind(OpenMPScheduleClauseKind K) { Kind = K; }
1307
1308   /// Set the first schedule modifier.
1309   ///
1310   /// \param M Schedule modifier.
1311   void setFirstScheduleModifier(OpenMPScheduleClauseModifier M) {
1312     Modifiers[FIRST] = M;
1313   }
1314
1315   /// Set the second schedule modifier.
1316   ///
1317   /// \param M Schedule modifier.
1318   void setSecondScheduleModifier(OpenMPScheduleClauseModifier M) {
1319     Modifiers[SECOND] = M;
1320   }
1321
1322   /// Set location of the first schedule modifier.
1323   void setFirstScheduleModifierLoc(SourceLocation Loc) {
1324     ModifiersLoc[FIRST] = Loc;
1325   }
1326
1327   /// Set location of the second schedule modifier.
1328   void setSecondScheduleModifierLoc(SourceLocation Loc) {
1329     ModifiersLoc[SECOND] = Loc;
1330   }
1331
1332   /// Set schedule modifier location.
1333   ///
1334   /// \param M Schedule modifier location.
1335   void setScheduleModifer(OpenMPScheduleClauseModifier M) {
1336     if (Modifiers[FIRST] == OMPC_SCHEDULE_MODIFIER_unknown)
1337       Modifiers[FIRST] = M;
1338     else {
1339       assert(Modifiers[SECOND] == OMPC_SCHEDULE_MODIFIER_unknown);
1340       Modifiers[SECOND] = M;
1341     }
1342   }
1343
1344   /// Sets the location of '('.
1345   ///
1346   /// \param Loc Location of '('.
1347   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
1348
1349   /// Set schedule kind start location.
1350   ///
1351   /// \param KLoc Schedule kind location.
1352   void setScheduleKindLoc(SourceLocation KLoc) { KindLoc = KLoc; }
1353
1354   /// Set location of ','.
1355   ///
1356   /// \param Loc Location of ','.
1357   void setCommaLoc(SourceLocation Loc) { CommaLoc = Loc; }
1358
1359   /// Set chunk size.
1360   ///
1361   /// \param E Chunk size.
1362   void setChunkSize(Expr *E) { ChunkSize = E; }
1363
1364 public:
1365   /// Build 'schedule' clause with schedule kind \a Kind and chunk size
1366   /// expression \a ChunkSize.
1367   ///
1368   /// \param StartLoc Starting location of the clause.
1369   /// \param LParenLoc Location of '('.
1370   /// \param KLoc Starting location of the argument.
1371   /// \param CommaLoc Location of ','.
1372   /// \param EndLoc Ending location of the clause.
1373   /// \param Kind Schedule kind.
1374   /// \param ChunkSize Chunk size.
1375   /// \param HelperChunkSize Helper chunk size for combined directives.
1376   /// \param M1 The first modifier applied to 'schedule' clause.
1377   /// \param M1Loc Location of the first modifier
1378   /// \param M2 The second modifier applied to 'schedule' clause.
1379   /// \param M2Loc Location of the second modifier
1380   OMPScheduleClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1381                     SourceLocation KLoc, SourceLocation CommaLoc,
1382                     SourceLocation EndLoc, OpenMPScheduleClauseKind Kind,
1383                     Expr *ChunkSize, Stmt *HelperChunkSize,
1384                     OpenMPScheduleClauseModifier M1, SourceLocation M1Loc,
1385                     OpenMPScheduleClauseModifier M2, SourceLocation M2Loc)
1386       : OMPClause(OMPC_schedule, StartLoc, EndLoc), OMPClauseWithPreInit(this),
1387         LParenLoc(LParenLoc), Kind(Kind), KindLoc(KLoc), CommaLoc(CommaLoc),
1388         ChunkSize(ChunkSize) {
1389     setPreInitStmt(HelperChunkSize);
1390     Modifiers[FIRST] = M1;
1391     Modifiers[SECOND] = M2;
1392     ModifiersLoc[FIRST] = M1Loc;
1393     ModifiersLoc[SECOND] = M2Loc;
1394   }
1395
1396   /// Build an empty clause.
1397   explicit OMPScheduleClause()
1398       : OMPClause(OMPC_schedule, SourceLocation(), SourceLocation()),
1399         OMPClauseWithPreInit(this) {
1400     Modifiers[FIRST] = OMPC_SCHEDULE_MODIFIER_unknown;
1401     Modifiers[SECOND] = OMPC_SCHEDULE_MODIFIER_unknown;
1402   }
1403
1404   /// Get kind of the clause.
1405   OpenMPScheduleClauseKind getScheduleKind() const { return Kind; }
1406
1407   /// Get the first modifier of the clause.
1408   OpenMPScheduleClauseModifier getFirstScheduleModifier() const {
1409     return Modifiers[FIRST];
1410   }
1411
1412   /// Get the second modifier of the clause.
1413   OpenMPScheduleClauseModifier getSecondScheduleModifier() const {
1414     return Modifiers[SECOND];
1415   }
1416
1417   /// Get location of '('.
1418   SourceLocation getLParenLoc() { return LParenLoc; }
1419
1420   /// Get kind location.
1421   SourceLocation getScheduleKindLoc() { return KindLoc; }
1422
1423   /// Get the first modifier location.
1424   SourceLocation getFirstScheduleModifierLoc() const {
1425     return ModifiersLoc[FIRST];
1426   }
1427
1428   /// Get the second modifier location.
1429   SourceLocation getSecondScheduleModifierLoc() const {
1430     return ModifiersLoc[SECOND];
1431   }
1432
1433   /// Get location of ','.
1434   SourceLocation getCommaLoc() { return CommaLoc; }
1435
1436   /// Get chunk size.
1437   Expr *getChunkSize() { return ChunkSize; }
1438
1439   /// Get chunk size.
1440   const Expr *getChunkSize() const { return ChunkSize; }
1441
1442   child_range children() {
1443     return child_range(reinterpret_cast<Stmt **>(&ChunkSize),
1444                        reinterpret_cast<Stmt **>(&ChunkSize) + 1);
1445   }
1446
1447   const_child_range children() const {
1448     auto Children = const_cast<OMPScheduleClause *>(this)->children();
1449     return const_child_range(Children.begin(), Children.end());
1450   }
1451
1452   child_range used_children() {
1453     return child_range(child_iterator(), child_iterator());
1454   }
1455   const_child_range used_children() const {
1456     return const_child_range(const_child_iterator(), const_child_iterator());
1457   }
1458
1459   static bool classof(const OMPClause *T) {
1460     return T->getClauseKind() == OMPC_schedule;
1461   }
1462 };
1463
1464 /// This represents 'ordered' clause in the '#pragma omp ...' directive.
1465 ///
1466 /// \code
1467 /// #pragma omp for ordered (2)
1468 /// \endcode
1469 /// In this example directive '#pragma omp for' has 'ordered' clause with
1470 /// parameter 2.
1471 class OMPOrderedClause final
1472     : public OMPClause,
1473       private llvm::TrailingObjects<OMPOrderedClause, Expr *> {
1474   friend class OMPClauseReader;
1475   friend TrailingObjects;
1476
1477   /// Location of '('.
1478   SourceLocation LParenLoc;
1479
1480   /// Number of for-loops.
1481   Stmt *NumForLoops = nullptr;
1482
1483   /// Real number of loops.
1484   unsigned NumberOfLoops = 0;
1485
1486   /// Build 'ordered' clause.
1487   ///
1488   /// \param Num Expression, possibly associated with this clause.
1489   /// \param NumLoops Number of loops, associated with this clause.
1490   /// \param StartLoc Starting location of the clause.
1491   /// \param LParenLoc Location of '('.
1492   /// \param EndLoc Ending location of the clause.
1493   OMPOrderedClause(Expr *Num, unsigned NumLoops, SourceLocation StartLoc,
1494                    SourceLocation LParenLoc, SourceLocation EndLoc)
1495       : OMPClause(OMPC_ordered, StartLoc, EndLoc), LParenLoc(LParenLoc),
1496         NumForLoops(Num), NumberOfLoops(NumLoops) {}
1497
1498   /// Build an empty clause.
1499   explicit OMPOrderedClause(unsigned NumLoops)
1500       : OMPClause(OMPC_ordered, SourceLocation(), SourceLocation()),
1501         NumberOfLoops(NumLoops) {}
1502
1503   /// Set the number of associated for-loops.
1504   void setNumForLoops(Expr *Num) { NumForLoops = Num; }
1505
1506 public:
1507   /// Build 'ordered' clause.
1508   ///
1509   /// \param Num Expression, possibly associated with this clause.
1510   /// \param NumLoops Number of loops, associated with this clause.
1511   /// \param StartLoc Starting location of the clause.
1512   /// \param LParenLoc Location of '('.
1513   /// \param EndLoc Ending location of the clause.
1514   static OMPOrderedClause *Create(const ASTContext &C, Expr *Num,
1515                                   unsigned NumLoops, SourceLocation StartLoc,
1516                                   SourceLocation LParenLoc,
1517                                   SourceLocation EndLoc);
1518
1519   /// Build an empty clause.
1520   static OMPOrderedClause* CreateEmpty(const ASTContext &C, unsigned NumLoops);
1521
1522   /// Sets the location of '('.
1523   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
1524
1525   /// Returns the location of '('.
1526   SourceLocation getLParenLoc() const { return LParenLoc; }
1527
1528   /// Return the number of associated for-loops.
1529   Expr *getNumForLoops() const { return cast_or_null<Expr>(NumForLoops); }
1530
1531   /// Set number of iterations for the specified loop.
1532   void setLoopNumIterations(unsigned NumLoop, Expr *NumIterations);
1533   /// Get number of iterations for all the loops.
1534   ArrayRef<Expr *> getLoopNumIterations() const;
1535
1536   /// Set loop counter for the specified loop.
1537   void setLoopCounter(unsigned NumLoop, Expr *Counter);
1538   /// Get loops counter for the specified loop.
1539   Expr *getLoopCounter(unsigned NumLoop);
1540   const Expr *getLoopCounter(unsigned NumLoop) const;
1541
1542   child_range children() { return child_range(&NumForLoops, &NumForLoops + 1); }
1543
1544   const_child_range children() const {
1545     return const_child_range(&NumForLoops, &NumForLoops + 1);
1546   }
1547
1548   child_range used_children() {
1549     return child_range(child_iterator(), child_iterator());
1550   }
1551   const_child_range used_children() const {
1552     return const_child_range(const_child_iterator(), const_child_iterator());
1553   }
1554
1555   static bool classof(const OMPClause *T) {
1556     return T->getClauseKind() == OMPC_ordered;
1557   }
1558 };
1559
1560 /// This represents 'nowait' clause in the '#pragma omp ...' directive.
1561 ///
1562 /// \code
1563 /// #pragma omp for nowait
1564 /// \endcode
1565 /// In this example directive '#pragma omp for' has 'nowait' clause.
1566 class OMPNowaitClause : public OMPClause {
1567 public:
1568   /// Build 'nowait' clause.
1569   ///
1570   /// \param StartLoc Starting location of the clause.
1571   /// \param EndLoc Ending location of the clause.
1572   OMPNowaitClause(SourceLocation StartLoc, SourceLocation EndLoc)
1573       : OMPClause(OMPC_nowait, StartLoc, EndLoc) {}
1574
1575   /// Build an empty clause.
1576   OMPNowaitClause()
1577       : OMPClause(OMPC_nowait, SourceLocation(), SourceLocation()) {}
1578
1579   child_range children() {
1580     return child_range(child_iterator(), child_iterator());
1581   }
1582
1583   const_child_range children() const {
1584     return const_child_range(const_child_iterator(), const_child_iterator());
1585   }
1586
1587   child_range used_children() {
1588     return child_range(child_iterator(), child_iterator());
1589   }
1590   const_child_range used_children() const {
1591     return const_child_range(const_child_iterator(), const_child_iterator());
1592   }
1593
1594   static bool classof(const OMPClause *T) {
1595     return T->getClauseKind() == OMPC_nowait;
1596   }
1597 };
1598
1599 /// This represents 'untied' clause in the '#pragma omp ...' directive.
1600 ///
1601 /// \code
1602 /// #pragma omp task untied
1603 /// \endcode
1604 /// In this example directive '#pragma omp task' has 'untied' clause.
1605 class OMPUntiedClause : public OMPClause {
1606 public:
1607   /// Build 'untied' clause.
1608   ///
1609   /// \param StartLoc Starting location of the clause.
1610   /// \param EndLoc Ending location of the clause.
1611   OMPUntiedClause(SourceLocation StartLoc, SourceLocation EndLoc)
1612       : OMPClause(OMPC_untied, StartLoc, EndLoc) {}
1613
1614   /// Build an empty clause.
1615   OMPUntiedClause()
1616       : OMPClause(OMPC_untied, SourceLocation(), SourceLocation()) {}
1617
1618   child_range children() {
1619     return child_range(child_iterator(), child_iterator());
1620   }
1621
1622   const_child_range children() const {
1623     return const_child_range(const_child_iterator(), const_child_iterator());
1624   }
1625
1626   child_range used_children() {
1627     return child_range(child_iterator(), child_iterator());
1628   }
1629   const_child_range used_children() const {
1630     return const_child_range(const_child_iterator(), const_child_iterator());
1631   }
1632
1633   static bool classof(const OMPClause *T) {
1634     return T->getClauseKind() == OMPC_untied;
1635   }
1636 };
1637
1638 /// This represents 'mergeable' clause in the '#pragma omp ...'
1639 /// directive.
1640 ///
1641 /// \code
1642 /// #pragma omp task mergeable
1643 /// \endcode
1644 /// In this example directive '#pragma omp task' has 'mergeable' clause.
1645 class OMPMergeableClause : public OMPClause {
1646 public:
1647   /// Build 'mergeable' clause.
1648   ///
1649   /// \param StartLoc Starting location of the clause.
1650   /// \param EndLoc Ending location of the clause.
1651   OMPMergeableClause(SourceLocation StartLoc, SourceLocation EndLoc)
1652       : OMPClause(OMPC_mergeable, StartLoc, EndLoc) {}
1653
1654   /// Build an empty clause.
1655   OMPMergeableClause()
1656       : OMPClause(OMPC_mergeable, SourceLocation(), SourceLocation()) {}
1657
1658   child_range children() {
1659     return child_range(child_iterator(), child_iterator());
1660   }
1661
1662   const_child_range children() const {
1663     return const_child_range(const_child_iterator(), const_child_iterator());
1664   }
1665
1666   child_range used_children() {
1667     return child_range(child_iterator(), child_iterator());
1668   }
1669   const_child_range used_children() const {
1670     return const_child_range(const_child_iterator(), const_child_iterator());
1671   }
1672
1673   static bool classof(const OMPClause *T) {
1674     return T->getClauseKind() == OMPC_mergeable;
1675   }
1676 };
1677
1678 /// This represents 'read' clause in the '#pragma omp atomic' directive.
1679 ///
1680 /// \code
1681 /// #pragma omp atomic read
1682 /// \endcode
1683 /// In this example directive '#pragma omp atomic' has 'read' clause.
1684 class OMPReadClause : public OMPClause {
1685 public:
1686   /// Build 'read' clause.
1687   ///
1688   /// \param StartLoc Starting location of the clause.
1689   /// \param EndLoc Ending location of the clause.
1690   OMPReadClause(SourceLocation StartLoc, SourceLocation EndLoc)
1691       : OMPClause(OMPC_read, StartLoc, EndLoc) {}
1692
1693   /// Build an empty clause.
1694   OMPReadClause() : OMPClause(OMPC_read, SourceLocation(), SourceLocation()) {}
1695
1696   child_range children() {
1697     return child_range(child_iterator(), child_iterator());
1698   }
1699
1700   const_child_range children() const {
1701     return const_child_range(const_child_iterator(), const_child_iterator());
1702   }
1703
1704   child_range used_children() {
1705     return child_range(child_iterator(), child_iterator());
1706   }
1707   const_child_range used_children() const {
1708     return const_child_range(const_child_iterator(), const_child_iterator());
1709   }
1710
1711   static bool classof(const OMPClause *T) {
1712     return T->getClauseKind() == OMPC_read;
1713   }
1714 };
1715
1716 /// This represents 'write' clause in the '#pragma omp atomic' directive.
1717 ///
1718 /// \code
1719 /// #pragma omp atomic write
1720 /// \endcode
1721 /// In this example directive '#pragma omp atomic' has 'write' clause.
1722 class OMPWriteClause : public OMPClause {
1723 public:
1724   /// Build 'write' clause.
1725   ///
1726   /// \param StartLoc Starting location of the clause.
1727   /// \param EndLoc Ending location of the clause.
1728   OMPWriteClause(SourceLocation StartLoc, SourceLocation EndLoc)
1729       : OMPClause(OMPC_write, StartLoc, EndLoc) {}
1730
1731   /// Build an empty clause.
1732   OMPWriteClause()
1733       : OMPClause(OMPC_write, SourceLocation(), SourceLocation()) {}
1734
1735   child_range children() {
1736     return child_range(child_iterator(), child_iterator());
1737   }
1738
1739   const_child_range children() const {
1740     return const_child_range(const_child_iterator(), const_child_iterator());
1741   }
1742
1743   child_range used_children() {
1744     return child_range(child_iterator(), child_iterator());
1745   }
1746   const_child_range used_children() const {
1747     return const_child_range(const_child_iterator(), const_child_iterator());
1748   }
1749
1750   static bool classof(const OMPClause *T) {
1751     return T->getClauseKind() == OMPC_write;
1752   }
1753 };
1754
1755 /// This represents 'update' clause in the '#pragma omp atomic'
1756 /// directive.
1757 ///
1758 /// \code
1759 /// #pragma omp atomic update
1760 /// \endcode
1761 /// In this example directive '#pragma omp atomic' has 'update' clause.
1762 class OMPUpdateClause : public OMPClause {
1763 public:
1764   /// Build 'update' clause.
1765   ///
1766   /// \param StartLoc Starting location of the clause.
1767   /// \param EndLoc Ending location of the clause.
1768   OMPUpdateClause(SourceLocation StartLoc, SourceLocation EndLoc)
1769       : OMPClause(OMPC_update, StartLoc, EndLoc) {}
1770
1771   /// Build an empty clause.
1772   OMPUpdateClause()
1773       : OMPClause(OMPC_update, SourceLocation(), SourceLocation()) {}
1774
1775   child_range children() {
1776     return child_range(child_iterator(), child_iterator());
1777   }
1778
1779   const_child_range children() const {
1780     return const_child_range(const_child_iterator(), const_child_iterator());
1781   }
1782
1783   child_range used_children() {
1784     return child_range(child_iterator(), child_iterator());
1785   }
1786   const_child_range used_children() const {
1787     return const_child_range(const_child_iterator(), const_child_iterator());
1788   }
1789
1790   static bool classof(const OMPClause *T) {
1791     return T->getClauseKind() == OMPC_update;
1792   }
1793 };
1794
1795 /// This represents 'capture' clause in the '#pragma omp atomic'
1796 /// directive.
1797 ///
1798 /// \code
1799 /// #pragma omp atomic capture
1800 /// \endcode
1801 /// In this example directive '#pragma omp atomic' has 'capture' clause.
1802 class OMPCaptureClause : public OMPClause {
1803 public:
1804   /// Build 'capture' clause.
1805   ///
1806   /// \param StartLoc Starting location of the clause.
1807   /// \param EndLoc Ending location of the clause.
1808   OMPCaptureClause(SourceLocation StartLoc, SourceLocation EndLoc)
1809       : OMPClause(OMPC_capture, StartLoc, EndLoc) {}
1810
1811   /// Build an empty clause.
1812   OMPCaptureClause()
1813       : OMPClause(OMPC_capture, SourceLocation(), SourceLocation()) {}
1814
1815   child_range children() {
1816     return child_range(child_iterator(), child_iterator());
1817   }
1818
1819   const_child_range children() const {
1820     return const_child_range(const_child_iterator(), const_child_iterator());
1821   }
1822
1823   child_range used_children() {
1824     return child_range(child_iterator(), child_iterator());
1825   }
1826   const_child_range used_children() const {
1827     return const_child_range(const_child_iterator(), const_child_iterator());
1828   }
1829
1830   static bool classof(const OMPClause *T) {
1831     return T->getClauseKind() == OMPC_capture;
1832   }
1833 };
1834
1835 /// This represents 'seq_cst' clause in the '#pragma omp atomic'
1836 /// directive.
1837 ///
1838 /// \code
1839 /// #pragma omp atomic seq_cst
1840 /// \endcode
1841 /// In this example directive '#pragma omp atomic' has 'seq_cst' clause.
1842 class OMPSeqCstClause : public OMPClause {
1843 public:
1844   /// Build 'seq_cst' clause.
1845   ///
1846   /// \param StartLoc Starting location of the clause.
1847   /// \param EndLoc Ending location of the clause.
1848   OMPSeqCstClause(SourceLocation StartLoc, SourceLocation EndLoc)
1849       : OMPClause(OMPC_seq_cst, StartLoc, EndLoc) {}
1850
1851   /// Build an empty clause.
1852   OMPSeqCstClause()
1853       : OMPClause(OMPC_seq_cst, SourceLocation(), SourceLocation()) {}
1854
1855   child_range children() {
1856     return child_range(child_iterator(), child_iterator());
1857   }
1858
1859   const_child_range children() const {
1860     return const_child_range(const_child_iterator(), const_child_iterator());
1861   }
1862
1863   child_range used_children() {
1864     return child_range(child_iterator(), child_iterator());
1865   }
1866   const_child_range used_children() const {
1867     return const_child_range(const_child_iterator(), const_child_iterator());
1868   }
1869
1870   static bool classof(const OMPClause *T) {
1871     return T->getClauseKind() == OMPC_seq_cst;
1872   }
1873 };
1874
1875 /// This represents clause 'private' in the '#pragma omp ...' directives.
1876 ///
1877 /// \code
1878 /// #pragma omp parallel private(a,b)
1879 /// \endcode
1880 /// In this example directive '#pragma omp parallel' has clause 'private'
1881 /// with the variables 'a' and 'b'.
1882 class OMPPrivateClause final
1883     : public OMPVarListClause<OMPPrivateClause>,
1884       private llvm::TrailingObjects<OMPPrivateClause, Expr *> {
1885   friend class OMPClauseReader;
1886   friend OMPVarListClause;
1887   friend TrailingObjects;
1888
1889   /// Build clause with number of variables \a N.
1890   ///
1891   /// \param StartLoc Starting location of the clause.
1892   /// \param LParenLoc Location of '('.
1893   /// \param EndLoc Ending location of the clause.
1894   /// \param N Number of the variables in the clause.
1895   OMPPrivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1896                    SourceLocation EndLoc, unsigned N)
1897       : OMPVarListClause<OMPPrivateClause>(OMPC_private, StartLoc, LParenLoc,
1898                                            EndLoc, N) {}
1899
1900   /// Build an empty clause.
1901   ///
1902   /// \param N Number of variables.
1903   explicit OMPPrivateClause(unsigned N)
1904       : OMPVarListClause<OMPPrivateClause>(OMPC_private, SourceLocation(),
1905                                            SourceLocation(), SourceLocation(),
1906                                            N) {}
1907
1908   /// Sets the list of references to private copies with initializers for
1909   /// new private variables.
1910   /// \param VL List of references.
1911   void setPrivateCopies(ArrayRef<Expr *> VL);
1912
1913   /// Gets the list of references to private copies with initializers for
1914   /// new private variables.
1915   MutableArrayRef<Expr *> getPrivateCopies() {
1916     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
1917   }
1918   ArrayRef<const Expr *> getPrivateCopies() const {
1919     return llvm::makeArrayRef(varlist_end(), varlist_size());
1920   }
1921
1922 public:
1923   /// Creates clause with a list of variables \a VL.
1924   ///
1925   /// \param C AST context.
1926   /// \param StartLoc Starting location of the clause.
1927   /// \param LParenLoc Location of '('.
1928   /// \param EndLoc Ending location of the clause.
1929   /// \param VL List of references to the variables.
1930   /// \param PrivateVL List of references to private copies with initializers.
1931   static OMPPrivateClause *Create(const ASTContext &C, SourceLocation StartLoc,
1932                                   SourceLocation LParenLoc,
1933                                   SourceLocation EndLoc, ArrayRef<Expr *> VL,
1934                                   ArrayRef<Expr *> PrivateVL);
1935
1936   /// Creates an empty clause with the place for \a N variables.
1937   ///
1938   /// \param C AST context.
1939   /// \param N The number of variables.
1940   static OMPPrivateClause *CreateEmpty(const ASTContext &C, unsigned N);
1941
1942   using private_copies_iterator = MutableArrayRef<Expr *>::iterator;
1943   using private_copies_const_iterator = ArrayRef<const Expr *>::iterator;
1944   using private_copies_range = llvm::iterator_range<private_copies_iterator>;
1945   using private_copies_const_range =
1946       llvm::iterator_range<private_copies_const_iterator>;
1947
1948   private_copies_range private_copies() {
1949     return private_copies_range(getPrivateCopies().begin(),
1950                                 getPrivateCopies().end());
1951   }
1952
1953   private_copies_const_range private_copies() const {
1954     return private_copies_const_range(getPrivateCopies().begin(),
1955                                       getPrivateCopies().end());
1956   }
1957
1958   child_range children() {
1959     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
1960                        reinterpret_cast<Stmt **>(varlist_end()));
1961   }
1962
1963   const_child_range children() const {
1964     auto Children = const_cast<OMPPrivateClause *>(this)->children();
1965     return const_child_range(Children.begin(), Children.end());
1966   }
1967
1968   child_range used_children() {
1969     return child_range(child_iterator(), child_iterator());
1970   }
1971   const_child_range used_children() const {
1972     return const_child_range(const_child_iterator(), const_child_iterator());
1973   }
1974
1975   static bool classof(const OMPClause *T) {
1976     return T->getClauseKind() == OMPC_private;
1977   }
1978 };
1979
1980 /// This represents clause 'firstprivate' in the '#pragma omp ...'
1981 /// directives.
1982 ///
1983 /// \code
1984 /// #pragma omp parallel firstprivate(a,b)
1985 /// \endcode
1986 /// In this example directive '#pragma omp parallel' has clause 'firstprivate'
1987 /// with the variables 'a' and 'b'.
1988 class OMPFirstprivateClause final
1989     : public OMPVarListClause<OMPFirstprivateClause>,
1990       public OMPClauseWithPreInit,
1991       private llvm::TrailingObjects<OMPFirstprivateClause, Expr *> {
1992   friend class OMPClauseReader;
1993   friend OMPVarListClause;
1994   friend TrailingObjects;
1995
1996   /// Build clause with number of variables \a N.
1997   ///
1998   /// \param StartLoc Starting location of the clause.
1999   /// \param LParenLoc Location of '('.
2000   /// \param EndLoc Ending location of the clause.
2001   /// \param N Number of the variables in the clause.
2002   OMPFirstprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2003                         SourceLocation EndLoc, unsigned N)
2004       : OMPVarListClause<OMPFirstprivateClause>(OMPC_firstprivate, StartLoc,
2005                                                 LParenLoc, EndLoc, N),
2006         OMPClauseWithPreInit(this) {}
2007
2008   /// Build an empty clause.
2009   ///
2010   /// \param N Number of variables.
2011   explicit OMPFirstprivateClause(unsigned N)
2012       : OMPVarListClause<OMPFirstprivateClause>(
2013             OMPC_firstprivate, SourceLocation(), SourceLocation(),
2014             SourceLocation(), N),
2015         OMPClauseWithPreInit(this) {}
2016
2017   /// Sets the list of references to private copies with initializers for
2018   /// new private variables.
2019   /// \param VL List of references.
2020   void setPrivateCopies(ArrayRef<Expr *> VL);
2021
2022   /// Gets the list of references to private copies with initializers for
2023   /// new private variables.
2024   MutableArrayRef<Expr *> getPrivateCopies() {
2025     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2026   }
2027   ArrayRef<const Expr *> getPrivateCopies() const {
2028     return llvm::makeArrayRef(varlist_end(), varlist_size());
2029   }
2030
2031   /// Sets the list of references to initializer variables for new
2032   /// private variables.
2033   /// \param VL List of references.
2034   void setInits(ArrayRef<Expr *> VL);
2035
2036   /// Gets the list of references to initializer variables for new
2037   /// private variables.
2038   MutableArrayRef<Expr *> getInits() {
2039     return MutableArrayRef<Expr *>(getPrivateCopies().end(), varlist_size());
2040   }
2041   ArrayRef<const Expr *> getInits() const {
2042     return llvm::makeArrayRef(getPrivateCopies().end(), varlist_size());
2043   }
2044
2045 public:
2046   /// Creates clause with a list of variables \a VL.
2047   ///
2048   /// \param C AST context.
2049   /// \param StartLoc Starting location of the clause.
2050   /// \param LParenLoc Location of '('.
2051   /// \param EndLoc Ending location of the clause.
2052   /// \param VL List of references to the original variables.
2053   /// \param PrivateVL List of references to private copies with initializers.
2054   /// \param InitVL List of references to auto generated variables used for
2055   /// initialization of a single array element. Used if firstprivate variable is
2056   /// of array type.
2057   /// \param PreInit Statement that must be executed before entering the OpenMP
2058   /// region with this clause.
2059   static OMPFirstprivateClause *
2060   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2061          SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
2062          ArrayRef<Expr *> InitVL, Stmt *PreInit);
2063
2064   /// Creates an empty clause with the place for \a N variables.
2065   ///
2066   /// \param C AST context.
2067   /// \param N The number of variables.
2068   static OMPFirstprivateClause *CreateEmpty(const ASTContext &C, unsigned N);
2069
2070   using private_copies_iterator = MutableArrayRef<Expr *>::iterator;
2071   using private_copies_const_iterator = ArrayRef<const Expr *>::iterator;
2072   using private_copies_range = llvm::iterator_range<private_copies_iterator>;
2073   using private_copies_const_range =
2074       llvm::iterator_range<private_copies_const_iterator>;
2075
2076   private_copies_range private_copies() {
2077     return private_copies_range(getPrivateCopies().begin(),
2078                                 getPrivateCopies().end());
2079   }
2080   private_copies_const_range private_copies() const {
2081     return private_copies_const_range(getPrivateCopies().begin(),
2082                                       getPrivateCopies().end());
2083   }
2084
2085   using inits_iterator = MutableArrayRef<Expr *>::iterator;
2086   using inits_const_iterator = ArrayRef<const Expr *>::iterator;
2087   using inits_range = llvm::iterator_range<inits_iterator>;
2088   using inits_const_range = llvm::iterator_range<inits_const_iterator>;
2089
2090   inits_range inits() {
2091     return inits_range(getInits().begin(), getInits().end());
2092   }
2093   inits_const_range inits() const {
2094     return inits_const_range(getInits().begin(), getInits().end());
2095   }
2096
2097   child_range children() {
2098     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2099                        reinterpret_cast<Stmt **>(varlist_end()));
2100   }
2101
2102   const_child_range children() const {
2103     auto Children = const_cast<OMPFirstprivateClause *>(this)->children();
2104     return const_child_range(Children.begin(), Children.end());
2105   }
2106
2107   child_range used_children() {
2108     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2109                        reinterpret_cast<Stmt **>(varlist_end()));
2110   }
2111   const_child_range used_children() const {
2112     auto Children = const_cast<OMPFirstprivateClause *>(this)->used_children();
2113     return const_child_range(Children.begin(), Children.end());
2114   }
2115
2116   static bool classof(const OMPClause *T) {
2117     return T->getClauseKind() == OMPC_firstprivate;
2118   }
2119 };
2120
2121 /// This represents clause 'lastprivate' in the '#pragma omp ...'
2122 /// directives.
2123 ///
2124 /// \code
2125 /// #pragma omp simd lastprivate(a,b)
2126 /// \endcode
2127 /// In this example directive '#pragma omp simd' has clause 'lastprivate'
2128 /// with the variables 'a' and 'b'.
2129 class OMPLastprivateClause final
2130     : public OMPVarListClause<OMPLastprivateClause>,
2131       public OMPClauseWithPostUpdate,
2132       private llvm::TrailingObjects<OMPLastprivateClause, Expr *> {
2133   // There are 4 additional tail-allocated arrays at the end of the class:
2134   // 1. Contains list of pseudo variables with the default initialization for
2135   // each non-firstprivate variables. Used in codegen for initialization of
2136   // lastprivate copies.
2137   // 2. List of helper expressions for proper generation of assignment operation
2138   // required for lastprivate clause. This list represents private variables
2139   // (for arrays, single array element).
2140   // 3. List of helper expressions for proper generation of assignment operation
2141   // required for lastprivate clause. This list represents original variables
2142   // (for arrays, single array element).
2143   // 4. List of helper expressions that represents assignment operation:
2144   // \code
2145   // DstExprs = SrcExprs;
2146   // \endcode
2147   // Required for proper codegen of final assignment performed by the
2148   // lastprivate clause.
2149   friend class OMPClauseReader;
2150   friend OMPVarListClause;
2151   friend TrailingObjects;
2152
2153   /// Build clause with number of variables \a N.
2154   ///
2155   /// \param StartLoc Starting location of the clause.
2156   /// \param LParenLoc Location of '('.
2157   /// \param EndLoc Ending location of the clause.
2158   /// \param N Number of the variables in the clause.
2159   OMPLastprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2160                        SourceLocation EndLoc, unsigned N)
2161       : OMPVarListClause<OMPLastprivateClause>(OMPC_lastprivate, StartLoc,
2162                                                LParenLoc, EndLoc, N),
2163         OMPClauseWithPostUpdate(this) {}
2164
2165   /// Build an empty clause.
2166   ///
2167   /// \param N Number of variables.
2168   explicit OMPLastprivateClause(unsigned N)
2169       : OMPVarListClause<OMPLastprivateClause>(
2170             OMPC_lastprivate, SourceLocation(), SourceLocation(),
2171             SourceLocation(), N),
2172         OMPClauseWithPostUpdate(this) {}
2173
2174   /// Get the list of helper expressions for initialization of private
2175   /// copies for lastprivate variables.
2176   MutableArrayRef<Expr *> getPrivateCopies() {
2177     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2178   }
2179   ArrayRef<const Expr *> getPrivateCopies() const {
2180     return llvm::makeArrayRef(varlist_end(), varlist_size());
2181   }
2182
2183   /// Set list of helper expressions, required for proper codegen of the
2184   /// clause. These expressions represent private variables (for arrays, single
2185   /// array element) in the final assignment statement performed by the
2186   /// lastprivate clause.
2187   void setSourceExprs(ArrayRef<Expr *> SrcExprs);
2188
2189   /// Get the list of helper source expressions.
2190   MutableArrayRef<Expr *> getSourceExprs() {
2191     return MutableArrayRef<Expr *>(getPrivateCopies().end(), varlist_size());
2192   }
2193   ArrayRef<const Expr *> getSourceExprs() const {
2194     return llvm::makeArrayRef(getPrivateCopies().end(), varlist_size());
2195   }
2196
2197   /// Set list of helper expressions, required for proper codegen of the
2198   /// clause. These expressions represent original variables (for arrays, single
2199   /// array element) in the final assignment statement performed by the
2200   /// lastprivate clause.
2201   void setDestinationExprs(ArrayRef<Expr *> DstExprs);
2202
2203   /// Get the list of helper destination expressions.
2204   MutableArrayRef<Expr *> getDestinationExprs() {
2205     return MutableArrayRef<Expr *>(getSourceExprs().end(), varlist_size());
2206   }
2207   ArrayRef<const Expr *> getDestinationExprs() const {
2208     return llvm::makeArrayRef(getSourceExprs().end(), varlist_size());
2209   }
2210
2211   /// Set list of helper assignment expressions, required for proper
2212   /// codegen of the clause. These expressions are assignment expressions that
2213   /// assign private copy of the variable to original variable.
2214   void setAssignmentOps(ArrayRef<Expr *> AssignmentOps);
2215
2216   /// Get the list of helper assignment expressions.
2217   MutableArrayRef<Expr *> getAssignmentOps() {
2218     return MutableArrayRef<Expr *>(getDestinationExprs().end(), varlist_size());
2219   }
2220   ArrayRef<const Expr *> getAssignmentOps() const {
2221     return llvm::makeArrayRef(getDestinationExprs().end(), varlist_size());
2222   }
2223
2224 public:
2225   /// Creates clause with a list of variables \a VL.
2226   ///
2227   /// \param C AST context.
2228   /// \param StartLoc Starting location of the clause.
2229   /// \param LParenLoc Location of '('.
2230   /// \param EndLoc Ending location of the clause.
2231   /// \param VL List of references to the variables.
2232   /// \param SrcExprs List of helper expressions for proper generation of
2233   /// assignment operation required for lastprivate clause. This list represents
2234   /// private variables (for arrays, single array element).
2235   /// \param DstExprs List of helper expressions for proper generation of
2236   /// assignment operation required for lastprivate clause. This list represents
2237   /// original variables (for arrays, single array element).
2238   /// \param AssignmentOps List of helper expressions that represents assignment
2239   /// operation:
2240   /// \code
2241   /// DstExprs = SrcExprs;
2242   /// \endcode
2243   /// Required for proper codegen of final assignment performed by the
2244   /// lastprivate clause.
2245   /// \param PreInit Statement that must be executed before entering the OpenMP
2246   /// region with this clause.
2247   /// \param PostUpdate Expression that must be executed after exit from the
2248   /// OpenMP region with this clause.
2249   static OMPLastprivateClause *
2250   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2251          SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
2252          ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps,
2253          Stmt *PreInit, Expr *PostUpdate);
2254
2255   /// Creates an empty clause with the place for \a N variables.
2256   ///
2257   /// \param C AST context.
2258   /// \param N The number of variables.
2259   static OMPLastprivateClause *CreateEmpty(const ASTContext &C, unsigned N);
2260
2261   using helper_expr_iterator = MutableArrayRef<Expr *>::iterator;
2262   using helper_expr_const_iterator = ArrayRef<const Expr *>::iterator;
2263   using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;
2264   using helper_expr_const_range =
2265       llvm::iterator_range<helper_expr_const_iterator>;
2266
2267   /// Set list of helper expressions, required for generation of private
2268   /// copies of original lastprivate variables.
2269   void setPrivateCopies(ArrayRef<Expr *> PrivateCopies);
2270
2271   helper_expr_const_range private_copies() const {
2272     return helper_expr_const_range(getPrivateCopies().begin(),
2273                                    getPrivateCopies().end());
2274   }
2275
2276   helper_expr_range private_copies() {
2277     return helper_expr_range(getPrivateCopies().begin(),
2278                              getPrivateCopies().end());
2279   }
2280
2281   helper_expr_const_range source_exprs() const {
2282     return helper_expr_const_range(getSourceExprs().begin(),
2283                                    getSourceExprs().end());
2284   }
2285
2286   helper_expr_range source_exprs() {
2287     return helper_expr_range(getSourceExprs().begin(), getSourceExprs().end());
2288   }
2289
2290   helper_expr_const_range destination_exprs() const {
2291     return helper_expr_const_range(getDestinationExprs().begin(),
2292                                    getDestinationExprs().end());
2293   }
2294
2295   helper_expr_range destination_exprs() {
2296     return helper_expr_range(getDestinationExprs().begin(),
2297                              getDestinationExprs().end());
2298   }
2299
2300   helper_expr_const_range assignment_ops() const {
2301     return helper_expr_const_range(getAssignmentOps().begin(),
2302                                    getAssignmentOps().end());
2303   }
2304
2305   helper_expr_range assignment_ops() {
2306     return helper_expr_range(getAssignmentOps().begin(),
2307                              getAssignmentOps().end());
2308   }
2309
2310   child_range children() {
2311     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2312                        reinterpret_cast<Stmt **>(varlist_end()));
2313   }
2314
2315   const_child_range children() const {
2316     auto Children = const_cast<OMPLastprivateClause *>(this)->children();
2317     return const_child_range(Children.begin(), Children.end());
2318   }
2319
2320   child_range used_children() {
2321     return child_range(child_iterator(), child_iterator());
2322   }
2323   const_child_range used_children() const {
2324     return const_child_range(const_child_iterator(), const_child_iterator());
2325   }
2326
2327   static bool classof(const OMPClause *T) {
2328     return T->getClauseKind() == OMPC_lastprivate;
2329   }
2330 };
2331
2332 /// This represents clause 'shared' in the '#pragma omp ...' directives.
2333 ///
2334 /// \code
2335 /// #pragma omp parallel shared(a,b)
2336 /// \endcode
2337 /// In this example directive '#pragma omp parallel' has clause 'shared'
2338 /// with the variables 'a' and 'b'.
2339 class OMPSharedClause final
2340     : public OMPVarListClause<OMPSharedClause>,
2341       private llvm::TrailingObjects<OMPSharedClause, Expr *> {
2342   friend OMPVarListClause;
2343   friend TrailingObjects;
2344
2345   /// Build clause with number of variables \a N.
2346   ///
2347   /// \param StartLoc Starting location of the clause.
2348   /// \param LParenLoc Location of '('.
2349   /// \param EndLoc Ending location of the clause.
2350   /// \param N Number of the variables in the clause.
2351   OMPSharedClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2352                   SourceLocation EndLoc, unsigned N)
2353       : OMPVarListClause<OMPSharedClause>(OMPC_shared, StartLoc, LParenLoc,
2354                                           EndLoc, N) {}
2355
2356   /// Build an empty clause.
2357   ///
2358   /// \param N Number of variables.
2359   explicit OMPSharedClause(unsigned N)
2360       : OMPVarListClause<OMPSharedClause>(OMPC_shared, SourceLocation(),
2361                                           SourceLocation(), SourceLocation(),
2362                                           N) {}
2363
2364 public:
2365   /// Creates clause with a list of variables \a VL.
2366   ///
2367   /// \param C AST context.
2368   /// \param StartLoc Starting location of the clause.
2369   /// \param LParenLoc Location of '('.
2370   /// \param EndLoc Ending location of the clause.
2371   /// \param VL List of references to the variables.
2372   static OMPSharedClause *Create(const ASTContext &C, SourceLocation StartLoc,
2373                                  SourceLocation LParenLoc,
2374                                  SourceLocation EndLoc, ArrayRef<Expr *> VL);
2375
2376   /// Creates an empty clause with \a N variables.
2377   ///
2378   /// \param C AST context.
2379   /// \param N The number of variables.
2380   static OMPSharedClause *CreateEmpty(const ASTContext &C, unsigned N);
2381
2382   child_range children() {
2383     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2384                        reinterpret_cast<Stmt **>(varlist_end()));
2385   }
2386
2387   const_child_range children() const {
2388     auto Children = const_cast<OMPSharedClause *>(this)->children();
2389     return const_child_range(Children.begin(), Children.end());
2390   }
2391
2392   child_range used_children() {
2393     return child_range(child_iterator(), child_iterator());
2394   }
2395   const_child_range used_children() const {
2396     return const_child_range(const_child_iterator(), const_child_iterator());
2397   }
2398
2399   static bool classof(const OMPClause *T) {
2400     return T->getClauseKind() == OMPC_shared;
2401   }
2402 };
2403
2404 /// This represents clause 'reduction' in the '#pragma omp ...'
2405 /// directives.
2406 ///
2407 /// \code
2408 /// #pragma omp parallel reduction(+:a,b)
2409 /// \endcode
2410 /// In this example directive '#pragma omp parallel' has clause 'reduction'
2411 /// with operator '+' and the variables 'a' and 'b'.
2412 class OMPReductionClause final
2413     : public OMPVarListClause<OMPReductionClause>,
2414       public OMPClauseWithPostUpdate,
2415       private llvm::TrailingObjects<OMPReductionClause, Expr *> {
2416   friend class OMPClauseReader;
2417   friend OMPVarListClause;
2418   friend TrailingObjects;
2419
2420   /// Location of ':'.
2421   SourceLocation ColonLoc;
2422
2423   /// Nested name specifier for C++.
2424   NestedNameSpecifierLoc QualifierLoc;
2425
2426   /// Name of custom operator.
2427   DeclarationNameInfo NameInfo;
2428
2429   /// Build clause with number of variables \a N.
2430   ///
2431   /// \param StartLoc Starting location of the clause.
2432   /// \param LParenLoc Location of '('.
2433   /// \param EndLoc Ending location of the clause.
2434   /// \param ColonLoc Location of ':'.
2435   /// \param N Number of the variables in the clause.
2436   /// \param QualifierLoc The nested-name qualifier with location information
2437   /// \param NameInfo The full name info for reduction identifier.
2438   OMPReductionClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2439                      SourceLocation ColonLoc, SourceLocation EndLoc, unsigned N,
2440                      NestedNameSpecifierLoc QualifierLoc,
2441                      const DeclarationNameInfo &NameInfo)
2442       : OMPVarListClause<OMPReductionClause>(OMPC_reduction, StartLoc,
2443                                              LParenLoc, EndLoc, N),
2444         OMPClauseWithPostUpdate(this), ColonLoc(ColonLoc),
2445         QualifierLoc(QualifierLoc), NameInfo(NameInfo) {}
2446
2447   /// Build an empty clause.
2448   ///
2449   /// \param N Number of variables.
2450   explicit OMPReductionClause(unsigned N)
2451       : OMPVarListClause<OMPReductionClause>(OMPC_reduction, SourceLocation(),
2452                                              SourceLocation(), SourceLocation(),
2453                                              N),
2454         OMPClauseWithPostUpdate(this) {}
2455
2456   /// Sets location of ':' symbol in clause.
2457   void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
2458
2459   /// Sets the name info for specified reduction identifier.
2460   void setNameInfo(DeclarationNameInfo DNI) { NameInfo = DNI; }
2461
2462   /// Sets the nested name specifier.
2463   void setQualifierLoc(NestedNameSpecifierLoc NSL) { QualifierLoc = NSL; }
2464
2465   /// Set list of helper expressions, required for proper codegen of the
2466   /// clause. These expressions represent private copy of the reduction
2467   /// variable.
2468   void setPrivates(ArrayRef<Expr *> Privates);
2469
2470   /// Get the list of helper privates.
2471   MutableArrayRef<Expr *> getPrivates() {
2472     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2473   }
2474   ArrayRef<const Expr *> getPrivates() const {
2475     return llvm::makeArrayRef(varlist_end(), varlist_size());
2476   }
2477
2478   /// Set list of helper expressions, required for proper codegen of the
2479   /// clause. These expressions represent LHS expression in the final
2480   /// reduction expression performed by the reduction clause.
2481   void setLHSExprs(ArrayRef<Expr *> LHSExprs);
2482
2483   /// Get the list of helper LHS expressions.
2484   MutableArrayRef<Expr *> getLHSExprs() {
2485     return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
2486   }
2487   ArrayRef<const Expr *> getLHSExprs() const {
2488     return llvm::makeArrayRef(getPrivates().end(), varlist_size());
2489   }
2490
2491   /// Set list of helper expressions, required for proper codegen of the
2492   /// clause. These expressions represent RHS expression in the final
2493   /// reduction expression performed by the reduction clause.
2494   /// Also, variables in these expressions are used for proper initialization of
2495   /// reduction copies.
2496   void setRHSExprs(ArrayRef<Expr *> RHSExprs);
2497
2498   /// Get the list of helper destination expressions.
2499   MutableArrayRef<Expr *> getRHSExprs() {
2500     return MutableArrayRef<Expr *>(getLHSExprs().end(), varlist_size());
2501   }
2502   ArrayRef<const Expr *> getRHSExprs() const {
2503     return llvm::makeArrayRef(getLHSExprs().end(), varlist_size());
2504   }
2505
2506   /// Set list of helper reduction expressions, required for proper
2507   /// codegen of the clause. These expressions are binary expressions or
2508   /// operator/custom reduction call that calculates new value from source
2509   /// helper expressions to destination helper expressions.
2510   void setReductionOps(ArrayRef<Expr *> ReductionOps);
2511
2512   /// Get the list of helper reduction expressions.
2513   MutableArrayRef<Expr *> getReductionOps() {
2514     return MutableArrayRef<Expr *>(getRHSExprs().end(), varlist_size());
2515   }
2516   ArrayRef<const Expr *> getReductionOps() const {
2517     return llvm::makeArrayRef(getRHSExprs().end(), varlist_size());
2518   }
2519
2520 public:
2521   /// Creates clause with a list of variables \a VL.
2522   ///
2523   /// \param StartLoc Starting location of the clause.
2524   /// \param LParenLoc Location of '('.
2525   /// \param ColonLoc Location of ':'.
2526   /// \param EndLoc Ending location of the clause.
2527   /// \param VL The variables in the clause.
2528   /// \param QualifierLoc The nested-name qualifier with location information
2529   /// \param NameInfo The full name info for reduction identifier.
2530   /// \param Privates List of helper expressions for proper generation of
2531   /// private copies.
2532   /// \param LHSExprs List of helper expressions for proper generation of
2533   /// assignment operation required for copyprivate clause. This list represents
2534   /// LHSs of the reduction expressions.
2535   /// \param RHSExprs List of helper expressions for proper generation of
2536   /// assignment operation required for copyprivate clause. This list represents
2537   /// RHSs of the reduction expressions.
2538   /// Also, variables in these expressions are used for proper initialization of
2539   /// reduction copies.
2540   /// \param ReductionOps List of helper expressions that represents reduction
2541   /// expressions:
2542   /// \code
2543   /// LHSExprs binop RHSExprs;
2544   /// operator binop(LHSExpr, RHSExpr);
2545   /// <CutomReduction>(LHSExpr, RHSExpr);
2546   /// \endcode
2547   /// Required for proper codegen of final reduction operation performed by the
2548   /// reduction clause.
2549   /// \param PreInit Statement that must be executed before entering the OpenMP
2550   /// region with this clause.
2551   /// \param PostUpdate Expression that must be executed after exit from the
2552   /// OpenMP region with this clause.
2553   static OMPReductionClause *
2554   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2555          SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
2556          NestedNameSpecifierLoc QualifierLoc,
2557          const DeclarationNameInfo &NameInfo, ArrayRef<Expr *> Privates,
2558          ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
2559          ArrayRef<Expr *> ReductionOps, Stmt *PreInit, Expr *PostUpdate);
2560
2561   /// Creates an empty clause with the place for \a N variables.
2562   ///
2563   /// \param C AST context.
2564   /// \param N The number of variables.
2565   static OMPReductionClause *CreateEmpty(const ASTContext &C, unsigned N);
2566
2567   /// Gets location of ':' symbol in clause.
2568   SourceLocation getColonLoc() const { return ColonLoc; }
2569
2570   /// Gets the name info for specified reduction identifier.
2571   const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2572
2573   /// Gets the nested name specifier.
2574   NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2575
2576   using helper_expr_iterator = MutableArrayRef<Expr *>::iterator;
2577   using helper_expr_const_iterator = ArrayRef<const Expr *>::iterator;
2578   using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;
2579   using helper_expr_const_range =
2580       llvm::iterator_range<helper_expr_const_iterator>;
2581
2582   helper_expr_const_range privates() const {
2583     return helper_expr_const_range(getPrivates().begin(), getPrivates().end());
2584   }
2585
2586   helper_expr_range privates() {
2587     return helper_expr_range(getPrivates().begin(), getPrivates().end());
2588   }
2589
2590   helper_expr_const_range lhs_exprs() const {
2591     return helper_expr_const_range(getLHSExprs().begin(), getLHSExprs().end());
2592   }
2593
2594   helper_expr_range lhs_exprs() {
2595     return helper_expr_range(getLHSExprs().begin(), getLHSExprs().end());
2596   }
2597
2598   helper_expr_const_range rhs_exprs() const {
2599     return helper_expr_const_range(getRHSExprs().begin(), getRHSExprs().end());
2600   }
2601
2602   helper_expr_range rhs_exprs() {
2603     return helper_expr_range(getRHSExprs().begin(), getRHSExprs().end());
2604   }
2605
2606   helper_expr_const_range reduction_ops() const {
2607     return helper_expr_const_range(getReductionOps().begin(),
2608                                    getReductionOps().end());
2609   }
2610
2611   helper_expr_range reduction_ops() {
2612     return helper_expr_range(getReductionOps().begin(),
2613                              getReductionOps().end());
2614   }
2615
2616   child_range children() {
2617     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2618                        reinterpret_cast<Stmt **>(varlist_end()));
2619   }
2620
2621   const_child_range children() const {
2622     auto Children = const_cast<OMPReductionClause *>(this)->children();
2623     return const_child_range(Children.begin(), Children.end());
2624   }
2625
2626   child_range used_children() {
2627     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2628                        reinterpret_cast<Stmt **>(varlist_end()));
2629   }
2630   const_child_range used_children() const {
2631     auto Children = const_cast<OMPReductionClause *>(this)->used_children();
2632     return const_child_range(Children.begin(), Children.end());
2633   }
2634
2635   static bool classof(const OMPClause *T) {
2636     return T->getClauseKind() == OMPC_reduction;
2637   }
2638 };
2639
2640 /// This represents clause 'task_reduction' in the '#pragma omp taskgroup'
2641 /// directives.
2642 ///
2643 /// \code
2644 /// #pragma omp taskgroup task_reduction(+:a,b)
2645 /// \endcode
2646 /// In this example directive '#pragma omp taskgroup' has clause
2647 /// 'task_reduction' with operator '+' and the variables 'a' and 'b'.
2648 class OMPTaskReductionClause final
2649     : public OMPVarListClause<OMPTaskReductionClause>,
2650       public OMPClauseWithPostUpdate,
2651       private llvm::TrailingObjects<OMPTaskReductionClause, Expr *> {
2652   friend class OMPClauseReader;
2653   friend OMPVarListClause;
2654   friend TrailingObjects;
2655
2656   /// Location of ':'.
2657   SourceLocation ColonLoc;
2658
2659   /// Nested name specifier for C++.
2660   NestedNameSpecifierLoc QualifierLoc;
2661
2662   /// Name of custom operator.
2663   DeclarationNameInfo NameInfo;
2664
2665   /// Build clause with number of variables \a N.
2666   ///
2667   /// \param StartLoc Starting location of the clause.
2668   /// \param LParenLoc Location of '('.
2669   /// \param EndLoc Ending location of the clause.
2670   /// \param ColonLoc Location of ':'.
2671   /// \param N Number of the variables in the clause.
2672   /// \param QualifierLoc The nested-name qualifier with location information
2673   /// \param NameInfo The full name info for reduction identifier.
2674   OMPTaskReductionClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2675                          SourceLocation ColonLoc, SourceLocation EndLoc,
2676                          unsigned N, NestedNameSpecifierLoc QualifierLoc,
2677                          const DeclarationNameInfo &NameInfo)
2678       : OMPVarListClause<OMPTaskReductionClause>(OMPC_task_reduction, StartLoc,
2679                                                  LParenLoc, EndLoc, N),
2680         OMPClauseWithPostUpdate(this), ColonLoc(ColonLoc),
2681         QualifierLoc(QualifierLoc), NameInfo(NameInfo) {}
2682
2683   /// Build an empty clause.
2684   ///
2685   /// \param N Number of variables.
2686   explicit OMPTaskReductionClause(unsigned N)
2687       : OMPVarListClause<OMPTaskReductionClause>(
2688             OMPC_task_reduction, SourceLocation(), SourceLocation(),
2689             SourceLocation(), N),
2690         OMPClauseWithPostUpdate(this) {}
2691
2692   /// Sets location of ':' symbol in clause.
2693   void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
2694
2695   /// Sets the name info for specified reduction identifier.
2696   void setNameInfo(DeclarationNameInfo DNI) { NameInfo = DNI; }
2697
2698   /// Sets the nested name specifier.
2699   void setQualifierLoc(NestedNameSpecifierLoc NSL) { QualifierLoc = NSL; }
2700
2701   /// Set list of helper expressions, required for proper codegen of the clause.
2702   /// These expressions represent private copy of the reduction variable.
2703   void setPrivates(ArrayRef<Expr *> Privates);
2704
2705   /// Get the list of helper privates.
2706   MutableArrayRef<Expr *> getPrivates() {
2707     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2708   }
2709   ArrayRef<const Expr *> getPrivates() const {
2710     return llvm::makeArrayRef(varlist_end(), varlist_size());
2711   }
2712
2713   /// Set list of helper expressions, required for proper codegen of the clause.
2714   /// These expressions represent LHS expression in the final reduction
2715   /// expression performed by the reduction clause.
2716   void setLHSExprs(ArrayRef<Expr *> LHSExprs);
2717
2718   /// Get the list of helper LHS expressions.
2719   MutableArrayRef<Expr *> getLHSExprs() {
2720     return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
2721   }
2722   ArrayRef<const Expr *> getLHSExprs() const {
2723     return llvm::makeArrayRef(getPrivates().end(), varlist_size());
2724   }
2725
2726   /// Set list of helper expressions, required for proper codegen of the clause.
2727   /// These expressions represent RHS expression in the final reduction
2728   /// expression performed by the reduction clause. Also, variables in these
2729   /// expressions are used for proper initialization of reduction copies.
2730   void setRHSExprs(ArrayRef<Expr *> RHSExprs);
2731
2732   ///  Get the list of helper destination expressions.
2733   MutableArrayRef<Expr *> getRHSExprs() {
2734     return MutableArrayRef<Expr *>(getLHSExprs().end(), varlist_size());
2735   }
2736   ArrayRef<const Expr *> getRHSExprs() const {
2737     return llvm::makeArrayRef(getLHSExprs().end(), varlist_size());
2738   }
2739
2740   /// Set list of helper reduction expressions, required for proper
2741   /// codegen of the clause. These expressions are binary expressions or
2742   /// operator/custom reduction call that calculates new value from source
2743   /// helper expressions to destination helper expressions.
2744   void setReductionOps(ArrayRef<Expr *> ReductionOps);
2745
2746   ///  Get the list of helper reduction expressions.
2747   MutableArrayRef<Expr *> getReductionOps() {
2748     return MutableArrayRef<Expr *>(getRHSExprs().end(), varlist_size());
2749   }
2750   ArrayRef<const Expr *> getReductionOps() const {
2751     return llvm::makeArrayRef(getRHSExprs().end(), varlist_size());
2752   }
2753
2754 public:
2755   /// Creates clause with a list of variables \a VL.
2756   ///
2757   /// \param StartLoc Starting location of the clause.
2758   /// \param LParenLoc Location of '('.
2759   /// \param ColonLoc Location of ':'.
2760   /// \param EndLoc Ending location of the clause.
2761   /// \param VL The variables in the clause.
2762   /// \param QualifierLoc The nested-name qualifier with location information
2763   /// \param NameInfo The full name info for reduction identifier.
2764   /// \param Privates List of helper expressions for proper generation of
2765   /// private copies.
2766   /// \param LHSExprs List of helper expressions for proper generation of
2767   /// assignment operation required for copyprivate clause. This list represents
2768   /// LHSs of the reduction expressions.
2769   /// \param RHSExprs List of helper expressions for proper generation of
2770   /// assignment operation required for copyprivate clause. This list represents
2771   /// RHSs of the reduction expressions.
2772   /// Also, variables in these expressions are used for proper initialization of
2773   /// reduction copies.
2774   /// \param ReductionOps List of helper expressions that represents reduction
2775   /// expressions:
2776   /// \code
2777   /// LHSExprs binop RHSExprs;
2778   /// operator binop(LHSExpr, RHSExpr);
2779   /// <CutomReduction>(LHSExpr, RHSExpr);
2780   /// \endcode
2781   /// Required for proper codegen of final reduction operation performed by the
2782   /// reduction clause.
2783   /// \param PreInit Statement that must be executed before entering the OpenMP
2784   /// region with this clause.
2785   /// \param PostUpdate Expression that must be executed after exit from the
2786   /// OpenMP region with this clause.
2787   static OMPTaskReductionClause *
2788   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2789          SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
2790          NestedNameSpecifierLoc QualifierLoc,
2791          const DeclarationNameInfo &NameInfo, ArrayRef<Expr *> Privates,
2792          ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
2793          ArrayRef<Expr *> ReductionOps, Stmt *PreInit, Expr *PostUpdate);
2794
2795   /// Creates an empty clause with the place for \a N variables.
2796   ///
2797   /// \param C AST context.
2798   /// \param N The number of variables.
2799   static OMPTaskReductionClause *CreateEmpty(const ASTContext &C, unsigned N);
2800
2801   /// Gets location of ':' symbol in clause.
2802   SourceLocation getColonLoc() const { return ColonLoc; }
2803
2804   /// Gets the name info for specified reduction identifier.
2805   const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2806
2807   /// Gets the nested name specifier.
2808   NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2809
2810   using helper_expr_iterator = MutableArrayRef<Expr *>::iterator;
2811   using helper_expr_const_iterator = ArrayRef<const Expr *>::iterator;
2812   using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;
2813   using helper_expr_const_range =
2814       llvm::iterator_range<helper_expr_const_iterator>;
2815
2816   helper_expr_const_range privates() const {
2817     return helper_expr_const_range(getPrivates().begin(), getPrivates().end());
2818   }
2819
2820   helper_expr_range privates() {
2821     return helper_expr_range(getPrivates().begin(), getPrivates().end());
2822   }
2823
2824   helper_expr_const_range lhs_exprs() const {
2825     return helper_expr_const_range(getLHSExprs().begin(), getLHSExprs().end());
2826   }
2827
2828   helper_expr_range lhs_exprs() {
2829     return helper_expr_range(getLHSExprs().begin(), getLHSExprs().end());
2830   }
2831
2832   helper_expr_const_range rhs_exprs() const {
2833     return helper_expr_const_range(getRHSExprs().begin(), getRHSExprs().end());
2834   }
2835
2836   helper_expr_range rhs_exprs() {
2837     return helper_expr_range(getRHSExprs().begin(), getRHSExprs().end());
2838   }
2839
2840   helper_expr_const_range reduction_ops() const {
2841     return helper_expr_const_range(getReductionOps().begin(),
2842                                    getReductionOps().end());
2843   }
2844
2845   helper_expr_range reduction_ops() {
2846     return helper_expr_range(getReductionOps().begin(),
2847                              getReductionOps().end());
2848   }
2849
2850   child_range children() {
2851     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2852                        reinterpret_cast<Stmt **>(varlist_end()));
2853   }
2854
2855   const_child_range children() const {
2856     auto Children = const_cast<OMPTaskReductionClause *>(this)->children();
2857     return const_child_range(Children.begin(), Children.end());
2858   }
2859
2860   child_range used_children() {
2861     return child_range(child_iterator(), child_iterator());
2862   }
2863   const_child_range used_children() const {
2864     return const_child_range(const_child_iterator(), const_child_iterator());
2865   }
2866
2867   static bool classof(const OMPClause *T) {
2868     return T->getClauseKind() == OMPC_task_reduction;
2869   }
2870 };
2871
2872 /// This represents clause 'in_reduction' in the '#pragma omp task' directives.
2873 ///
2874 /// \code
2875 /// #pragma omp task in_reduction(+:a,b)
2876 /// \endcode
2877 /// In this example directive '#pragma omp task' has clause 'in_reduction' with
2878 /// operator '+' and the variables 'a' and 'b'.
2879 class OMPInReductionClause final
2880     : public OMPVarListClause<OMPInReductionClause>,
2881       public OMPClauseWithPostUpdate,
2882       private llvm::TrailingObjects<OMPInReductionClause, Expr *> {
2883   friend class OMPClauseReader;
2884   friend OMPVarListClause;
2885   friend TrailingObjects;
2886
2887   /// Location of ':'.
2888   SourceLocation ColonLoc;
2889
2890   /// Nested name specifier for C++.
2891   NestedNameSpecifierLoc QualifierLoc;
2892
2893   /// Name of custom operator.
2894   DeclarationNameInfo NameInfo;
2895
2896   /// Build clause with number of variables \a N.
2897   ///
2898   /// \param StartLoc Starting location of the clause.
2899   /// \param LParenLoc Location of '('.
2900   /// \param EndLoc Ending location of the clause.
2901   /// \param ColonLoc Location of ':'.
2902   /// \param N Number of the variables in the clause.
2903   /// \param QualifierLoc The nested-name qualifier with location information
2904   /// \param NameInfo The full name info for reduction identifier.
2905   OMPInReductionClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2906                        SourceLocation ColonLoc, SourceLocation EndLoc,
2907                        unsigned N, NestedNameSpecifierLoc QualifierLoc,
2908                        const DeclarationNameInfo &NameInfo)
2909       : OMPVarListClause<OMPInReductionClause>(OMPC_in_reduction, StartLoc,
2910                                                LParenLoc, EndLoc, N),
2911         OMPClauseWithPostUpdate(this), ColonLoc(ColonLoc),
2912         QualifierLoc(QualifierLoc), NameInfo(NameInfo) {}
2913
2914   /// Build an empty clause.
2915   ///
2916   /// \param N Number of variables.
2917   explicit OMPInReductionClause(unsigned N)
2918       : OMPVarListClause<OMPInReductionClause>(
2919             OMPC_in_reduction, SourceLocation(), SourceLocation(),
2920             SourceLocation(), N),
2921         OMPClauseWithPostUpdate(this) {}
2922
2923   /// Sets location of ':' symbol in clause.
2924   void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
2925
2926   /// Sets the name info for specified reduction identifier.
2927   void setNameInfo(DeclarationNameInfo DNI) { NameInfo = DNI; }
2928
2929   /// Sets the nested name specifier.
2930   void setQualifierLoc(NestedNameSpecifierLoc NSL) { QualifierLoc = NSL; }
2931
2932   /// Set list of helper expressions, required for proper codegen of the clause.
2933   /// These expressions represent private copy of the reduction variable.
2934   void setPrivates(ArrayRef<Expr *> Privates);
2935
2936   /// Get the list of helper privates.
2937   MutableArrayRef<Expr *> getPrivates() {
2938     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2939   }
2940   ArrayRef<const Expr *> getPrivates() const {
2941     return llvm::makeArrayRef(varlist_end(), varlist_size());
2942   }
2943
2944   /// Set list of helper expressions, required for proper codegen of the clause.
2945   /// These expressions represent LHS expression in the final reduction
2946   /// expression performed by the reduction clause.
2947   void setLHSExprs(ArrayRef<Expr *> LHSExprs);
2948
2949   /// Get the list of helper LHS expressions.
2950   MutableArrayRef<Expr *> getLHSExprs() {
2951     return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
2952   }
2953   ArrayRef<const Expr *> getLHSExprs() const {
2954     return llvm::makeArrayRef(getPrivates().end(), varlist_size());
2955   }
2956
2957   /// Set list of helper expressions, required for proper codegen of the clause.
2958   /// These expressions represent RHS expression in the final reduction
2959   /// expression performed by the reduction clause. Also, variables in these
2960   /// expressions are used for proper initialization of reduction copies.
2961   void setRHSExprs(ArrayRef<Expr *> RHSExprs);
2962
2963   ///  Get the list of helper destination expressions.
2964   MutableArrayRef<Expr *> getRHSExprs() {
2965     return MutableArrayRef<Expr *>(getLHSExprs().end(), varlist_size());
2966   }
2967   ArrayRef<const Expr *> getRHSExprs() const {
2968     return llvm::makeArrayRef(getLHSExprs().end(), varlist_size());
2969   }
2970
2971   /// Set list of helper reduction expressions, required for proper
2972   /// codegen of the clause. These expressions are binary expressions or
2973   /// operator/custom reduction call that calculates new value from source
2974   /// helper expressions to destination helper expressions.
2975   void setReductionOps(ArrayRef<Expr *> ReductionOps);
2976
2977   ///  Get the list of helper reduction expressions.
2978   MutableArrayRef<Expr *> getReductionOps() {
2979     return MutableArrayRef<Expr *>(getRHSExprs().end(), varlist_size());
2980   }
2981   ArrayRef<const Expr *> getReductionOps() const {
2982     return llvm::makeArrayRef(getRHSExprs().end(), varlist_size());
2983   }
2984
2985   /// Set list of helper reduction taskgroup descriptors.
2986   void setTaskgroupDescriptors(ArrayRef<Expr *> ReductionOps);
2987
2988   ///  Get the list of helper reduction taskgroup descriptors.
2989   MutableArrayRef<Expr *> getTaskgroupDescriptors() {
2990     return MutableArrayRef<Expr *>(getReductionOps().end(), varlist_size());
2991   }
2992   ArrayRef<const Expr *> getTaskgroupDescriptors() const {
2993     return llvm::makeArrayRef(getReductionOps().end(), varlist_size());
2994   }
2995
2996 public:
2997   /// Creates clause with a list of variables \a VL.
2998   ///
2999   /// \param StartLoc Starting location of the clause.
3000   /// \param LParenLoc Location of '('.
3001   /// \param ColonLoc Location of ':'.
3002   /// \param EndLoc Ending location of the clause.
3003   /// \param VL The variables in the clause.
3004   /// \param QualifierLoc The nested-name qualifier with location information
3005   /// \param NameInfo The full name info for reduction identifier.
3006   /// \param Privates List of helper expressions for proper generation of
3007   /// private copies.
3008   /// \param LHSExprs List of helper expressions for proper generation of
3009   /// assignment operation required for copyprivate clause. This list represents
3010   /// LHSs of the reduction expressions.
3011   /// \param RHSExprs List of helper expressions for proper generation of
3012   /// assignment operation required for copyprivate clause. This list represents
3013   /// RHSs of the reduction expressions.
3014   /// Also, variables in these expressions are used for proper initialization of
3015   /// reduction copies.
3016   /// \param ReductionOps List of helper expressions that represents reduction
3017   /// expressions:
3018   /// \code
3019   /// LHSExprs binop RHSExprs;
3020   /// operator binop(LHSExpr, RHSExpr);
3021   /// <CutomReduction>(LHSExpr, RHSExpr);
3022   /// \endcode
3023   /// Required for proper codegen of final reduction operation performed by the
3024   /// reduction clause.
3025   /// \param TaskgroupDescriptors List of helper taskgroup descriptors for
3026   /// corresponding items in parent taskgroup task_reduction clause.
3027   /// \param PreInit Statement that must be executed before entering the OpenMP
3028   /// region with this clause.
3029   /// \param PostUpdate Expression that must be executed after exit from the
3030   /// OpenMP region with this clause.
3031   static OMPInReductionClause *
3032   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
3033          SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
3034          NestedNameSpecifierLoc QualifierLoc,
3035          const DeclarationNameInfo &NameInfo, ArrayRef<Expr *> Privates,
3036          ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
3037          ArrayRef<Expr *> ReductionOps, ArrayRef<Expr *> TaskgroupDescriptors,
3038          Stmt *PreInit, Expr *PostUpdate);
3039
3040   /// Creates an empty clause with the place for \a N variables.
3041   ///
3042   /// \param C AST context.
3043   /// \param N The number of variables.
3044   static OMPInReductionClause *CreateEmpty(const ASTContext &C, unsigned N);
3045
3046   /// Gets location of ':' symbol in clause.
3047   SourceLocation getColonLoc() const { return ColonLoc; }
3048
3049   /// Gets the name info for specified reduction identifier.
3050   const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
3051
3052   /// Gets the nested name specifier.
3053   NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3054
3055   using helper_expr_iterator = MutableArrayRef<Expr *>::iterator;
3056   using helper_expr_const_iterator = ArrayRef<const Expr *>::iterator;
3057   using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;
3058   using helper_expr_const_range =
3059       llvm::iterator_range<helper_expr_const_iterator>;
3060
3061   helper_expr_const_range privates() const {
3062     return helper_expr_const_range(getPrivates().begin(), getPrivates().end());
3063   }
3064
3065   helper_expr_range privates() {
3066     return helper_expr_range(getPrivates().begin(), getPrivates().end());
3067   }
3068
3069   helper_expr_const_range lhs_exprs() const {
3070     return helper_expr_const_range(getLHSExprs().begin(), getLHSExprs().end());
3071   }
3072
3073   helper_expr_range lhs_exprs() {
3074     return helper_expr_range(getLHSExprs().begin(), getLHSExprs().end());
3075   }
3076
3077   helper_expr_const_range rhs_exprs() const {
3078     return helper_expr_const_range(getRHSExprs().begin(), getRHSExprs().end());
3079   }
3080
3081   helper_expr_range rhs_exprs() {
3082     return helper_expr_range(getRHSExprs().begin(), getRHSExprs().end());
3083   }
3084
3085   helper_expr_const_range reduction_ops() const {
3086     return helper_expr_const_range(getReductionOps().begin(),
3087                                    getReductionOps().end());
3088   }
3089
3090   helper_expr_range reduction_ops() {
3091     return helper_expr_range(getReductionOps().begin(),
3092                              getReductionOps().end());
3093   }
3094
3095   helper_expr_const_range taskgroup_descriptors() const {
3096     return helper_expr_const_range(getTaskgroupDescriptors().begin(),
3097                                    getTaskgroupDescriptors().end());
3098   }
3099
3100   helper_expr_range taskgroup_descriptors() {
3101     return helper_expr_range(getTaskgroupDescriptors().begin(),
3102                              getTaskgroupDescriptors().end());
3103   }
3104
3105   child_range children() {
3106     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3107                        reinterpret_cast<Stmt **>(varlist_end()));
3108   }
3109
3110   const_child_range children() const {
3111     auto Children = const_cast<OMPInReductionClause *>(this)->children();
3112     return const_child_range(Children.begin(), Children.end());
3113   }
3114
3115   child_range used_children() {
3116     return child_range(child_iterator(), child_iterator());
3117   }
3118   const_child_range used_children() const {
3119     return const_child_range(const_child_iterator(), const_child_iterator());
3120   }
3121
3122   static bool classof(const OMPClause *T) {
3123     return T->getClauseKind() == OMPC_in_reduction;
3124   }
3125 };
3126
3127 /// This represents clause 'linear' in the '#pragma omp ...'
3128 /// directives.
3129 ///
3130 /// \code
3131 /// #pragma omp simd linear(a,b : 2)
3132 /// \endcode
3133 /// In this example directive '#pragma omp simd' has clause 'linear'
3134 /// with variables 'a', 'b' and linear step '2'.
3135 class OMPLinearClause final
3136     : public OMPVarListClause<OMPLinearClause>,
3137       public OMPClauseWithPostUpdate,
3138       private llvm::TrailingObjects<OMPLinearClause, Expr *> {
3139   friend class OMPClauseReader;
3140   friend OMPVarListClause;
3141   friend TrailingObjects;
3142
3143   /// Modifier of 'linear' clause.
3144   OpenMPLinearClauseKind Modifier = OMPC_LINEAR_val;
3145
3146   /// Location of linear modifier if any.
3147   SourceLocation ModifierLoc;
3148
3149   /// Location of ':'.
3150   SourceLocation ColonLoc;
3151
3152   /// Sets the linear step for clause.
3153   void setStep(Expr *Step) { *(getFinals().end()) = Step; }
3154
3155   /// Sets the expression to calculate linear step for clause.
3156   void setCalcStep(Expr *CalcStep) { *(getFinals().end() + 1) = CalcStep; }
3157
3158   /// Build 'linear' clause with given number of variables \a NumVars.
3159   ///
3160   /// \param StartLoc Starting location of the clause.
3161   /// \param LParenLoc Location of '('.
3162   /// \param ColonLoc Location of ':'.
3163   /// \param EndLoc Ending location of the clause.
3164   /// \param NumVars Number of variables.
3165   OMPLinearClause(SourceLocation StartLoc, SourceLocation LParenLoc,
3166                   OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
3167                   SourceLocation ColonLoc, SourceLocation EndLoc,
3168                   unsigned NumVars)
3169       : OMPVarListClause<OMPLinearClause>(OMPC_linear, StartLoc, LParenLoc,
3170                                           EndLoc, NumVars),
3171         OMPClauseWithPostUpdate(this), Modifier(Modifier),
3172         ModifierLoc(ModifierLoc), ColonLoc(ColonLoc) {}
3173
3174   /// Build an empty clause.
3175   ///
3176   /// \param NumVars Number of variables.
3177   explicit OMPLinearClause(unsigned NumVars)
3178       : OMPVarListClause<OMPLinearClause>(OMPC_linear, SourceLocation(),
3179                                           SourceLocation(), SourceLocation(),
3180                                           NumVars),
3181         OMPClauseWithPostUpdate(this) {}
3182
3183   /// Gets the list of initial values for linear variables.
3184   ///
3185   /// There are NumVars expressions with initial values allocated after the
3186   /// varlist, they are followed by NumVars update expressions (used to update
3187   /// the linear variable's value on current iteration) and they are followed by
3188   /// NumVars final expressions (used to calculate the linear variable's
3189   /// value after the loop body). After these lists, there are 2 helper
3190   /// expressions - linear step and a helper to calculate it before the
3191   /// loop body (used when the linear step is not constant):
3192   ///
3193   /// { Vars[] /* in OMPVarListClause */; Privates[]; Inits[]; Updates[];
3194   /// Finals[]; Step; CalcStep; }
3195   MutableArrayRef<Expr *> getPrivates() {
3196     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
3197   }
3198   ArrayRef<const Expr *> getPrivates() const {
3199     return llvm::makeArrayRef(varlist_end(), varlist_size());
3200   }
3201
3202   MutableArrayRef<Expr *> getInits() {
3203     return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
3204   }
3205   ArrayRef<const Expr *> getInits() const {
3206     return llvm::makeArrayRef(getPrivates().end(), varlist_size());
3207   }
3208
3209   /// Sets the list of update expressions for linear variables.
3210   MutableArrayRef<Expr *> getUpdates() {
3211     return MutableArrayRef<Expr *>(getInits().end(), varlist_size());
3212   }
3213   ArrayRef<const Expr *> getUpdates() const {
3214     return llvm::makeArrayRef(getInits().end(), varlist_size());
3215   }
3216
3217   /// Sets the list of final update expressions for linear variables.
3218   MutableArrayRef<Expr *> getFinals() {
3219     return MutableArrayRef<Expr *>(getUpdates().end(), varlist_size());
3220   }
3221   ArrayRef<const Expr *> getFinals() const {
3222     return llvm::makeArrayRef(getUpdates().end(), varlist_size());
3223   }
3224
3225   /// Gets the list of used expressions for linear variables.
3226   MutableArrayRef<Expr *> getUsedExprs() {
3227     return MutableArrayRef<Expr *>(getFinals().end() + 2, varlist_size() + 1);
3228   }
3229   ArrayRef<const Expr *> getUsedExprs() const {
3230     return llvm::makeArrayRef(getFinals().end() + 2, varlist_size() + 1);
3231   }
3232
3233   /// Sets the list of the copies of original linear variables.
3234   /// \param PL List of expressions.
3235   void setPrivates(ArrayRef<Expr *> PL);
3236
3237   /// Sets the list of the initial values for linear variables.
3238   /// \param IL List of expressions.
3239   void setInits(ArrayRef<Expr *> IL);
3240
3241 public:
3242   /// Creates clause with a list of variables \a VL and a linear step
3243   /// \a Step.
3244   ///
3245   /// \param C AST Context.
3246   /// \param StartLoc Starting location of the clause.
3247   /// \param LParenLoc Location of '('.
3248   /// \param Modifier Modifier of 'linear' clause.
3249   /// \param ModifierLoc Modifier location.
3250   /// \param ColonLoc Location of ':'.
3251   /// \param EndLoc Ending location of the clause.
3252   /// \param VL List of references to the variables.
3253   /// \param PL List of private copies of original variables.
3254   /// \param IL List of initial values for the variables.
3255   /// \param Step Linear step.
3256   /// \param CalcStep Calculation of the linear step.
3257   /// \param PreInit Statement that must be executed before entering the OpenMP
3258   /// region with this clause.
3259   /// \param PostUpdate Expression that must be executed after exit from the
3260   /// OpenMP region with this clause.
3261   static OMPLinearClause *
3262   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
3263          OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
3264          SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
3265          ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
3266          Stmt *PreInit, Expr *PostUpdate);
3267
3268   /// Creates an empty clause with the place for \a NumVars variables.
3269   ///
3270   /// \param C AST context.
3271   /// \param NumVars Number of variables.
3272   static OMPLinearClause *CreateEmpty(const ASTContext &C, unsigned NumVars);
3273
3274   /// Set modifier.
3275   void setModifier(OpenMPLinearClauseKind Kind) { Modifier = Kind; }
3276
3277   /// Return modifier.
3278   OpenMPLinearClauseKind getModifier() const { return Modifier; }
3279
3280   /// Set modifier location.
3281   void setModifierLoc(SourceLocation Loc) { ModifierLoc = Loc; }
3282
3283   /// Return modifier location.
3284   SourceLocation getModifierLoc() const { return ModifierLoc; }
3285
3286   /// Sets the location of ':'.
3287   void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
3288
3289   /// Returns the location of ':'.
3290   SourceLocation getColonLoc() const { return ColonLoc; }
3291
3292   /// Returns linear step.
3293   Expr *getStep() { return *(getFinals().end()); }
3294
3295   /// Returns linear step.
3296   const Expr *getStep() const { return *(getFinals().end()); }
3297
3298   /// Returns expression to calculate linear step.
3299   Expr *getCalcStep() { return *(getFinals().end() + 1); }
3300
3301   /// Returns expression to calculate linear step.
3302   const Expr *getCalcStep() const { return *(getFinals().end() + 1); }
3303
3304   /// Sets the list of update expressions for linear variables.
3305   /// \param UL List of expressions.
3306   void setUpdates(ArrayRef<Expr *> UL);
3307
3308   /// Sets the list of final update expressions for linear variables.
3309   /// \param FL List of expressions.
3310   void setFinals(ArrayRef<Expr *> FL);
3311
3312   /// Sets the list of used expressions for the linear clause.
3313   void setUsedExprs(ArrayRef<Expr *> UE);
3314
3315   using privates_iterator = MutableArrayRef<Expr *>::iterator;
3316   using privates_const_iterator = ArrayRef<const Expr *>::iterator;
3317   using privates_range = llvm::iterator_range<privates_iterator>;
3318   using privates_const_range = llvm::iterator_range<privates_const_iterator>;
3319
3320   privates_range privates() {
3321     return privates_range(getPrivates().begin(), getPrivates().end());
3322   }
3323
3324   privates_const_range privates() const {
3325     return privates_const_range(getPrivates().begin(), getPrivates().end());
3326   }
3327
3328   using inits_iterator = MutableArrayRef<Expr *>::iterator;
3329   using inits_const_iterator = ArrayRef<const Expr *>::iterator;
3330   using inits_range = llvm::iterator_range<inits_iterator>;
3331   using inits_const_range = llvm::iterator_range<inits_const_iterator>;
3332
3333   inits_range inits() {
3334     return inits_range(getInits().begin(), getInits().end());
3335   }
3336
3337   inits_const_range inits() const {
3338     return inits_const_range(getInits().begin(), getInits().end());
3339   }
3340
3341   using updates_iterator = MutableArrayRef<Expr *>::iterator;
3342   using updates_const_iterator = ArrayRef<const Expr *>::iterator;
3343   using updates_range = llvm::iterator_range<updates_iterator>;
3344   using updates_const_range = llvm::iterator_range<updates_const_iterator>;
3345
3346   updates_range updates() {
3347     return updates_range(getUpdates().begin(), getUpdates().end());
3348   }
3349
3350   updates_const_range updates() const {
3351     return updates_const_range(getUpdates().begin(), getUpdates().end());
3352   }
3353
3354   using finals_iterator = MutableArrayRef<Expr *>::iterator;
3355   using finals_const_iterator = ArrayRef<const Expr *>::iterator;
3356   using finals_range = llvm::iterator_range<finals_iterator>;
3357   using finals_const_range = llvm::iterator_range<finals_const_iterator>;
3358
3359   finals_range finals() {
3360     return finals_range(getFinals().begin(), getFinals().end());
3361   }
3362
3363   finals_const_range finals() const {
3364     return finals_const_range(getFinals().begin(), getFinals().end());
3365   }
3366
3367   using used_expressions_iterator = MutableArrayRef<Expr *>::iterator;
3368   using used_expressions_const_iterator = ArrayRef<const Expr *>::iterator;
3369   using used_expressions_range =
3370       llvm::iterator_range<used_expressions_iterator>;
3371   using used_expressions_const_range =
3372       llvm::iterator_range<used_expressions_const_iterator>;
3373
3374   used_expressions_range used_expressions() {
3375     return finals_range(getUsedExprs().begin(), getUsedExprs().end());
3376   }
3377
3378   used_expressions_const_range used_expressions() const {
3379     return finals_const_range(getUsedExprs().begin(), getUsedExprs().end());
3380   }
3381
3382   child_range children() {
3383     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3384                        reinterpret_cast<Stmt **>(varlist_end()));
3385   }
3386
3387   const_child_range children() const {
3388     auto Children = const_cast<OMPLinearClause *>(this)->children();
3389     return const_child_range(Children.begin(), Children.end());
3390   }
3391
3392   child_range used_children();
3393
3394   const_child_range used_children() const {
3395     auto Children = const_cast<OMPLinearClause *>(this)->used_children();
3396     return const_child_range(Children.begin(), Children.end());
3397   }
3398
3399   static bool classof(const OMPClause *T) {
3400     return T->getClauseKind() == OMPC_linear;
3401   }
3402 };
3403
3404 /// This represents clause 'aligned' in the '#pragma omp ...'
3405 /// directives.
3406 ///
3407 /// \code
3408 /// #pragma omp simd aligned(a,b : 8)
3409 /// \endcode
3410 /// In this example directive '#pragma omp simd' has clause 'aligned'
3411 /// with variables 'a', 'b' and alignment '8'.
3412 class OMPAlignedClause final
3413     : public OMPVarListClause<OMPAlignedClause>,
3414       private llvm::TrailingObjects<OMPAlignedClause, Expr *> {
3415   friend class OMPClauseReader;
3416   friend OMPVarListClause;
3417   friend TrailingObjects;
3418
3419   /// Location of ':'.
3420   SourceLocation ColonLoc;
3421
3422   /// Sets the alignment for clause.
3423   void setAlignment(Expr *A) { *varlist_end() = A; }
3424
3425   /// Build 'aligned' clause with given number of variables \a NumVars.
3426   ///
3427   /// \param StartLoc Starting location of the clause.
3428   /// \param LParenLoc Location of '('.
3429   /// \param ColonLoc Location of ':'.
3430   /// \param EndLoc Ending location of the clause.
3431   /// \param NumVars Number of variables.
3432   OMPAlignedClause(SourceLocation StartLoc, SourceLocation LParenLoc,
3433                    SourceLocation ColonLoc, SourceLocation EndLoc,
3434                    unsigned NumVars)
3435       : OMPVarListClause<OMPAlignedClause>(OMPC_aligned, StartLoc, LParenLoc,
3436                                            EndLoc, NumVars),
3437         ColonLoc(ColonLoc) {}
3438
3439   /// Build an empty clause.
3440   ///
3441   /// \param NumVars Number of variables.
3442   explicit OMPAlignedClause(unsigned NumVars)
3443       : OMPVarListClause<OMPAlignedClause>(OMPC_aligned, SourceLocation(),
3444                                            SourceLocation(), SourceLocation(),
3445                                            NumVars) {}
3446
3447 public:
3448   /// Creates clause with a list of variables \a VL and alignment \a A.
3449   ///
3450   /// \param C AST Context.
3451   /// \param StartLoc Starting location of the clause.
3452   /// \param LParenLoc Location of '('.
3453   /// \param ColonLoc Location of ':'.
3454   /// \param EndLoc Ending location of the clause.
3455   /// \param VL List of references to the variables.
3456   /// \param A Alignment.
3457   static OMPAlignedClause *Create(const ASTContext &C, SourceLocation StartLoc,
3458                                   SourceLocation LParenLoc,
3459                                   SourceLocation ColonLoc,
3460                                   SourceLocation EndLoc, ArrayRef<Expr *> VL,
3461                                   Expr *A);
3462
3463   /// Creates an empty clause with the place for \a NumVars variables.
3464   ///
3465   /// \param C AST context.
3466   /// \param NumVars Number of variables.
3467   static OMPAlignedClause *CreateEmpty(const ASTContext &C, unsigned NumVars);
3468
3469   /// Sets the location of ':'.
3470   void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
3471
3472   /// Returns the location of ':'.
3473   SourceLocation getColonLoc() const { return ColonLoc; }
3474
3475   /// Returns alignment.
3476   Expr *getAlignment() { return *varlist_end(); }
3477
3478   /// Returns alignment.
3479   const Expr *getAlignment() const { return *varlist_end(); }
3480
3481   child_range children() {
3482     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3483                        reinterpret_cast<Stmt **>(varlist_end()));
3484   }
3485
3486   const_child_range children() const {
3487     auto Children = const_cast<OMPAlignedClause *>(this)->children();
3488     return const_child_range(Children.begin(), Children.end());
3489   }
3490
3491   child_range used_children() {
3492     return child_range(child_iterator(), child_iterator());
3493   }
3494   const_child_range used_children() const {
3495     return const_child_range(const_child_iterator(), const_child_iterator());
3496   }
3497
3498   static bool classof(const OMPClause *T) {
3499     return T->getClauseKind() == OMPC_aligned;
3500   }
3501 };
3502
3503 /// This represents clause 'copyin' in the '#pragma omp ...' directives.
3504 ///
3505 /// \code
3506 /// #pragma omp parallel copyin(a,b)
3507 /// \endcode
3508 /// In this example directive '#pragma omp parallel' has clause 'copyin'
3509 /// with the variables 'a' and 'b'.
3510 class OMPCopyinClause final
3511     : public OMPVarListClause<OMPCopyinClause>,
3512       private llvm::TrailingObjects<OMPCopyinClause, Expr *> {
3513   // Class has 3 additional tail allocated arrays:
3514   // 1. List of helper expressions for proper generation of assignment operation
3515   // required for copyin clause. This list represents sources.
3516   // 2. List of helper expressions for proper generation of assignment operation
3517   // required for copyin clause. This list represents destinations.
3518   // 3. List of helper expressions that represents assignment operation:
3519   // \code
3520   // DstExprs = SrcExprs;
3521   // \endcode
3522   // Required for proper codegen of propagation of master's thread values of
3523   // threadprivate variables to local instances of that variables in other
3524   // implicit threads.
3525
3526   friend class OMPClauseReader;
3527   friend OMPVarListClause;
3528   friend TrailingObjects;
3529
3530   /// Build clause with number of variables \a N.
3531   ///
3532   /// \param StartLoc Starting location of the clause.
3533   /// \param LParenLoc Location of '('.
3534   /// \param EndLoc Ending location of the clause.
3535   /// \param N Number of the variables in the clause.
3536   OMPCopyinClause(SourceLocation StartLoc, SourceLocation LParenLoc,
3537                   SourceLocation EndLoc, unsigned N)
3538       : OMPVarListClause<OMPCopyinClause>(OMPC_copyin, StartLoc, LParenLoc,
3539                                           EndLoc, N) {}
3540
3541   /// Build an empty clause.
3542   ///
3543   /// \param N Number of variables.
3544   explicit OMPCopyinClause(unsigned N)
3545       : OMPVarListClause<OMPCopyinClause>(OMPC_copyin, SourceLocation(),
3546                                           SourceLocation(), SourceLocation(),
3547                                           N) {}
3548
3549   /// Set list of helper expressions, required for proper codegen of the
3550   /// clause. These expressions represent source expression in the final
3551   /// assignment statement performed by the copyin clause.
3552   void setSourceExprs(ArrayRef<Expr *> SrcExprs);
3553
3554   /// Get the list of helper source expressions.
3555   MutableArrayRef<Expr *> getSourceExprs() {
3556     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
3557   }
3558   ArrayRef<const Expr *> getSourceExprs() const {
3559     return llvm::makeArrayRef(varlist_end(), varlist_size());
3560   }
3561
3562   /// Set list of helper expressions, required for proper codegen of the
3563   /// clause. These expressions represent destination expression in the final
3564   /// assignment statement performed by the copyin clause.
3565   void setDestinationExprs(ArrayRef<Expr *> DstExprs);
3566
3567   /// Get the list of helper destination expressions.
3568   MutableArrayRef<Expr *> getDestinationExprs() {
3569     return MutableArrayRef<Expr *>(getSourceExprs().end(), varlist_size());
3570   }
3571   ArrayRef<const Expr *> getDestinationExprs() const {
3572     return llvm::makeArrayRef(getSourceExprs().end(), varlist_size());
3573   }
3574
3575   /// Set list of helper assignment expressions, required for proper
3576   /// codegen of the clause. These expressions are assignment expressions that
3577   /// assign source helper expressions to destination helper expressions
3578   /// correspondingly.
3579   void setAssignmentOps(ArrayRef<Expr *> AssignmentOps);
3580
3581   /// Get the list of helper assignment expressions.
3582   MutableArrayRef<Expr *> getAssignmentOps() {
3583     return MutableArrayRef<Expr *>(getDestinationExprs().end(), varlist_size());
3584   }
3585   ArrayRef<const Expr *> getAssignmentOps() const {
3586     return llvm::makeArrayRef(getDestinationExprs().end(), varlist_size());
3587   }
3588
3589 public:
3590   /// Creates clause with a list of variables \a VL.
3591   ///
3592   /// \param C AST context.
3593   /// \param StartLoc Starting location of the clause.
3594   /// \param LParenLoc Location of '('.
3595   /// \param EndLoc Ending location of the clause.
3596   /// \param VL List of references to the variables.
3597   /// \param SrcExprs List of helper expressions for proper generation of
3598   /// assignment operation required for copyin clause. This list represents
3599   /// sources.
3600   /// \param DstExprs List of helper expressions for proper generation of
3601   /// assignment operation required for copyin clause. This list represents
3602   /// destinations.
3603   /// \param AssignmentOps List of helper expressions that represents assignment
3604   /// operation:
3605   /// \code
3606   /// DstExprs = SrcExprs;
3607   /// \endcode
3608   /// Required for proper codegen of propagation of master's thread values of
3609   /// threadprivate variables to local instances of that variables in other
3610   /// implicit threads.
3611   static OMPCopyinClause *
3612   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
3613          SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
3614          ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps);
3615
3616   /// Creates an empty clause with \a N variables.
3617   ///
3618   /// \param C AST context.
3619   /// \param N The number of variables.
3620   static OMPCopyinClause *CreateEmpty(const ASTContext &C, unsigned N);
3621
3622   using helper_expr_iterator = MutableArrayRef<Expr *>::iterator;
3623   using helper_expr_const_iterator = ArrayRef<const Expr *>::iterator;
3624   using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;
3625   using helper_expr_const_range =
3626       llvm::iterator_range<helper_expr_const_iterator>;
3627
3628   helper_expr_const_range source_exprs() const {
3629     return helper_expr_const_range(getSourceExprs().begin(),
3630                                    getSourceExprs().end());
3631   }
3632
3633   helper_expr_range source_exprs() {
3634     return helper_expr_range(getSourceExprs().begin(), getSourceExprs().end());
3635   }
3636
3637   helper_expr_const_range destination_exprs() const {
3638     return helper_expr_const_range(getDestinationExprs().begin(),
3639                                    getDestinationExprs().end());
3640   }
3641
3642   helper_expr_range destination_exprs() {
3643     return helper_expr_range(getDestinationExprs().begin(),
3644                              getDestinationExprs().end());
3645   }
3646
3647   helper_expr_const_range assignment_ops() const {
3648     return helper_expr_const_range(getAssignmentOps().begin(),
3649                                    getAssignmentOps().end());
3650   }
3651
3652   helper_expr_range assignment_ops() {
3653     return helper_expr_range(getAssignmentOps().begin(),
3654                              getAssignmentOps().end());
3655   }
3656
3657   child_range children() {
3658     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3659                        reinterpret_cast<Stmt **>(varlist_end()));
3660   }
3661
3662   const_child_range children() const {
3663     auto Children = const_cast<OMPCopyinClause *>(this)->children();
3664     return const_child_range(Children.begin(), Children.end());
3665   }
3666
3667   child_range used_children() {
3668     return child_range(child_iterator(), child_iterator());
3669   }
3670   const_child_range used_children() const {
3671     return const_child_range(const_child_iterator(), const_child_iterator());
3672   }
3673
3674   static bool classof(const OMPClause *T) {
3675     return T->getClauseKind() == OMPC_copyin;
3676   }
3677 };
3678
3679 /// This represents clause 'copyprivate' in the '#pragma omp ...'
3680 /// directives.
3681 ///
3682 /// \code
3683 /// #pragma omp single copyprivate(a,b)
3684 /// \endcode
3685 /// In this example directive '#pragma omp single' has clause 'copyprivate'
3686 /// with the variables 'a' and 'b'.
3687 class OMPCopyprivateClause final
3688     : public OMPVarListClause<OMPCopyprivateClause>,
3689       private llvm::TrailingObjects<OMPCopyprivateClause, Expr *> {
3690   friend class OMPClauseReader;
3691   friend OMPVarListClause;
3692   friend TrailingObjects;
3693
3694   /// Build clause with number of variables \a N.
3695   ///
3696   /// \param StartLoc Starting location of the clause.
3697   /// \param LParenLoc Location of '('.
3698   /// \param EndLoc Ending location of the clause.
3699   /// \param N Number of the variables in the clause.
3700   OMPCopyprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
3701                        SourceLocation EndLoc, unsigned N)
3702       : OMPVarListClause<OMPCopyprivateClause>(OMPC_copyprivate, StartLoc,
3703                                                LParenLoc, EndLoc, N) {}
3704
3705   /// Build an empty clause.
3706   ///
3707   /// \param N Number of variables.
3708   explicit OMPCopyprivateClause(unsigned N)
3709       : OMPVarListClause<OMPCopyprivateClause>(
3710             OMPC_copyprivate, SourceLocation(), SourceLocation(),
3711             SourceLocation(), N) {}
3712
3713   /// Set list of helper expressions, required for proper codegen of the
3714   /// clause. These expressions represent source expression in the final
3715   /// assignment statement performed by the copyprivate clause.
3716   void setSourceExprs(ArrayRef<Expr *> SrcExprs);
3717
3718   /// Get the list of helper source expressions.
3719   MutableArrayRef<Expr *> getSourceExprs() {
3720     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
3721   }
3722   ArrayRef<const Expr *> getSourceExprs() const {
3723     return llvm::makeArrayRef(varlist_end(), varlist_size());
3724   }
3725
3726   /// Set list of helper expressions, required for proper codegen of the
3727   /// clause. These expressions represent destination expression in the final
3728   /// assignment statement performed by the copyprivate clause.
3729   void setDestinationExprs(ArrayRef<Expr *> DstExprs);
3730
3731   /// Get the list of helper destination expressions.
3732   MutableArrayRef<Expr *> getDestinationExprs() {
3733     return MutableArrayRef<Expr *>(getSourceExprs().end(), varlist_size());
3734   }
3735   ArrayRef<const Expr *> getDestinationExprs() const {
3736     return llvm::makeArrayRef(getSourceExprs().end(), varlist_size());
3737   }
3738
3739   /// Set list of helper assignment expressions, required for proper
3740   /// codegen of the clause. These expressions are assignment expressions that
3741   /// assign source helper expressions to destination helper expressions
3742   /// correspondingly.
3743   void setAssignmentOps(ArrayRef<Expr *> AssignmentOps);
3744
3745   /// Get the list of helper assignment expressions.
3746   MutableArrayRef<Expr *> getAssignmentOps() {
3747     return MutableArrayRef<Expr *>(getDestinationExprs().end(), varlist_size());
3748   }
3749   ArrayRef<const Expr *> getAssignmentOps() const {
3750     return llvm::makeArrayRef(getDestinationExprs().end(), varlist_size());
3751   }
3752
3753 public:
3754   /// Creates clause with a list of variables \a VL.
3755   ///
3756   /// \param C AST context.
3757   /// \param StartLoc Starting location of the clause.
3758   /// \param LParenLoc Location of '('.
3759   /// \param EndLoc Ending location of the clause.
3760   /// \param VL List of references to the variables.
3761   /// \param SrcExprs List of helper expressions for proper generation of
3762   /// assignment operation required for copyprivate clause. This list represents
3763   /// sources.
3764   /// \param DstExprs List of helper expressions for proper generation of
3765   /// assignment operation required for copyprivate clause. This list represents
3766   /// destinations.
3767   /// \param AssignmentOps List of helper expressions that represents assignment
3768   /// operation:
3769   /// \code
3770   /// DstExprs = SrcExprs;
3771   /// \endcode
3772   /// Required for proper codegen of final assignment performed by the
3773   /// copyprivate clause.
3774   static OMPCopyprivateClause *
3775   Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
3776          SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
3777          ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps);
3778
3779   /// Creates an empty clause with \a N variables.
3780   ///
3781   /// \param C AST context.
3782   /// \param N The number of variables.
3783   static OMPCopyprivateClause *CreateEmpty(const ASTContext &C, unsigned N);
3784
3785   using helper_expr_iterator = MutableArrayRef<Expr *>::iterator;
3786   using helper_expr_const_iterator = ArrayRef<const Expr *>::iterator;
3787   using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;
3788   using helper_expr_const_range =
3789       llvm::iterator_range<helper_expr_const_iterator>;
3790
3791   helper_expr_const_range source_exprs() const {
3792     return helper_expr_const_range(getSourceExprs().begin(),
3793                                    getSourceExprs().end());
3794   }
3795
3796   helper_expr_range source_exprs() {
3797     return helper_expr_range(getSourceExprs().begin(), getSourceExprs().end());
3798   }
3799
3800   helper_expr_const_range destination_exprs() const {
3801     return helper_expr_const_range(getDestinationExprs().begin(),
3802                                    getDestinationExprs().end());
3803   }
3804
3805   helper_expr_range destination_exprs() {
3806     return helper_expr_range(getDestinationExprs().begin(),
3807                              getDestinationExprs().end());
3808   }
3809
3810   helper_expr_const_range assignment_ops() const {
3811     return helper_expr_const_range(getAssignmentOps().begin(),
3812                                    getAssignmentOps().end());
3813   }
3814
3815   helper_expr_range assignment_ops() {
3816     return helper_expr_range(getAssignmentOps().begin(),
3817                              getAssignmentOps().end());
3818   }
3819
3820   child_range children() {
3821     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3822                        reinterpret_cast<Stmt **>(varlist_end()));
3823   }
3824
3825   const_child_range children() const {
3826     auto Children = const_cast<OMPCopyprivateClause *>(this)->children();
3827     return const_child_range(Children.begin(), Children.end());
3828   }
3829
3830   child_range used_children() {
3831     return child_range(child_iterator(), child_iterator());
3832   }
3833   const_child_range used_children() const {
3834     return const_child_range(const_child_iterator(), const_child_iterator());
3835   }
3836
3837   static bool classof(const OMPClause *T) {
3838     return T->getClauseKind() == OMPC_copyprivate;
3839   }
3840 };
3841
3842 /// This represents implicit clause 'flush' for the '#pragma omp flush'
3843 /// directive.
3844 /// This clause does not exist by itself, it can be only as a part of 'omp
3845 /// flush' directive. This clause is introduced to keep the original structure
3846 /// of \a OMPExecutableDirective class and its derivatives and to use the
3847 /// existing infrastructure of clauses with the list of variables.
3848 ///
3849 /// \code
3850 /// #pragma omp flush(a,b)
3851 /// \endcode
3852 /// In this example directive '#pragma omp flush' has implicit clause 'flush'
3853 /// with the variables 'a' and 'b'.
3854 class OMPFlushClause final
3855     : public OMPVarListClause<OMPFlushClause>,
3856       private llvm::TrailingObjects<OMPFlushClause, Expr *> {
3857   friend OMPVarListClause;
3858   friend TrailingObjects;
3859
3860   /// Build clause with number of variables \a N.
3861   ///
3862   /// \param StartLoc Starting location of the clause.
3863   /// \param LParenLoc Location of '('.
3864   /// \param EndLoc Ending location of the clause.
3865   /// \param N Number of the variables in the clause.
3866   OMPFlushClause(SourceLocation StartLoc, SourceLocation LParenLoc,
3867                  SourceLocation EndLoc, unsigned N)
3868       : OMPVarListClause<OMPFlushClause>(OMPC_flush, StartLoc, LParenLoc,
3869                                          EndLoc, N) {}
3870
3871   /// Build an empty clause.
3872   ///
3873   /// \param N Number of variables.
3874   explicit OMPFlushClause(unsigned N)
3875       : OMPVarListClause<OMPFlushClause>(OMPC_flush, SourceLocation(),
3876                                          SourceLocation(), SourceLocation(),
3877                                          N) {}
3878
3879 public:
3880   /// Creates clause with a list of variables \a VL.
3881   ///
3882   /// \param C AST context.
3883   /// \param StartLoc Starting location of the clause.
3884   /// \param LParenLoc Location of '('.
3885   /// \param EndLoc Ending location of the clause.
3886   /// \param VL List of references to the variables.
3887   static OMPFlushClause *Create(const ASTContext &C, SourceLocation StartLoc,
3888                                 SourceLocation LParenLoc, SourceLocation EndLoc,
3889                                 ArrayRef<Expr *> VL);
3890
3891   /// Creates an empty clause with \a N variables.
3892   ///
3893   /// \param C AST context.
3894   /// \param N The number of variables.
3895   static OMPFlushClause *CreateEmpty(const ASTContext &C, unsigned N);
3896
3897   child_range children() {
3898     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3899                        reinterpret_cast<Stmt **>(varlist_end()));
3900   }
3901
3902   const_child_range children() const {
3903     auto Children = const_cast<OMPFlushClause *>(this)->children();
3904     return const_child_range(Children.begin(), Children.end());
3905   }
3906
3907   child_range used_children() {
3908     return child_range(child_iterator(), child_iterator());
3909   }
3910   const_child_range used_children() const {
3911     return const_child_range(const_child_iterator(), const_child_iterator());
3912   }
3913
3914   static bool classof(const OMPClause *T) {
3915     return T->getClauseKind() == OMPC_flush;
3916   }
3917 };
3918
3919 /// This represents implicit clause 'depend' for the '#pragma omp task'
3920 /// directive.
3921 ///
3922 /// \code
3923 /// #pragma omp task depend(in:a,b)
3924 /// \endcode
3925 /// In this example directive '#pragma omp task' with clause 'depend' with the
3926 /// variables 'a' and 'b' with dependency 'in'.
3927 class OMPDependClause final
3928     : public OMPVarListClause<OMPDependClause>,
3929       private llvm::TrailingObjects<OMPDependClause, Expr *> {
3930   friend class OMPClauseReader;
3931   friend OMPVarListClause;
3932   friend TrailingObjects;
3933
3934   /// Dependency type (one of in, out, inout).
3935   OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
3936
3937   /// Dependency type location.
3938   SourceLocation DepLoc;
3939
3940   /// Colon location.
3941   SourceLocation ColonLoc;
3942
3943   /// Number of loops, associated with the depend clause.
3944   unsigned NumLoops = 0;
3945
3946   /// Build clause with number of variables \a N.
3947   ///
3948   /// \param StartLoc Starting location of the clause.
3949   /// \param LParenLoc Location of '('.
3950   /// \param EndLoc Ending location of the clause.
3951   /// \param N Number of the variables in the clause.
3952   /// \param NumLoops Number of loops that is associated with this depend
3953   /// clause.
3954   OMPDependClause(SourceLocation StartLoc, SourceLocation LParenLoc,
3955                   SourceLocation EndLoc, unsigned N, unsigned NumLoops)
3956       : OMPVarListClause<OMPDependClause>(OMPC_depend, StartLoc, LParenLoc,
3957                                           EndLoc, N), NumLoops(NumLoops) {}
3958
3959   /// Build an empty clause.
3960   ///
3961   /// \param N Number of variables.
3962   /// \param NumLoops Number of loops that is associated with this depend
3963   /// clause.
3964   explicit OMPDependClause(unsigned N, unsigned NumLoops)
3965       : OMPVarListClause<OMPDependClause>(OMPC_depend, SourceLocation(),
3966                                           SourceLocation(), SourceLocation(),
3967                                           N),
3968         NumLoops(NumLoops) {}
3969
3970   /// Set dependency kind.
3971   void setDependencyKind(OpenMPDependClauseKind K) { DepKind = K; }
3972
3973   /// Set dependency kind and its location.
3974   void setDependencyLoc(SourceLocation Loc) { DepLoc = Loc; }
3975
3976   /// Set colon location.
3977   void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
3978
3979 public:
3980   /// Creates clause with a list of variables \a VL.
3981   ///
3982   /// \param C AST context.
3983   /// \param StartLoc Starting location of the clause.
3984   /// \param LParenLoc Location of '('.
3985   /// \param EndLoc Ending location of the clause.
3986   /// \param DepKind Dependency type.
3987   /// \param DepLoc Location of the dependency type.
3988   /// \param ColonLoc Colon location.
3989   /// \param VL List of references to the variables.
3990   /// \param NumLoops Number of loops that is associated with this depend
3991   /// clause.
3992   static OMPDependClause *Create(const ASTContext &C, SourceLocation StartLoc,
3993                                  SourceLocation LParenLoc,
3994                                  SourceLocation EndLoc,
3995                                  OpenMPDependClauseKind DepKind,
3996                                  SourceLocation DepLoc, SourceLocation ColonLoc,
3997                                  ArrayRef<Expr *> VL, unsigned NumLoops);
3998
3999   /// Creates an empty clause with \a N variables.
4000   ///
4001   /// \param C AST context.
4002   /// \param N The number of variables.
4003   /// \param NumLoops Number of loops that is associated with this depend
4004   /// clause.
4005   static OMPDependClause *CreateEmpty(const ASTContext &C, unsigned N,
4006                                       unsigned NumLoops);
4007
4008   /// Get dependency type.
4009   OpenMPDependClauseKind getDependencyKind() const { return DepKind; }
4010
4011   /// Get dependency type location.
4012   SourceLocation getDependencyLoc() const { return DepLoc; }
4013
4014   /// Get colon location.
4015   SourceLocation getColonLoc() const { return ColonLoc; }
4016
4017   /// Get number of loops associated with the clause.
4018   unsigned getNumLoops() const { return NumLoops; }
4019
4020   /// Set the loop data for the depend clauses with 'sink|source' kind of
4021   /// dependency.
4022   void setLoopData(unsigned NumLoop, Expr *Cnt);
4023
4024   /// Get the loop data.
4025   Expr *getLoopData(unsigned NumLoop);
4026   const Expr *getLoopData(unsigned NumLoop) const;
4027
4028   child_range children() {
4029     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
4030                        reinterpret_cast<Stmt **>(varlist_end()));
4031   }
4032
4033   const_child_range children() const {
4034     auto Children = const_cast<OMPDependClause *>(this)->children();
4035     return const_child_range(Children.begin(), Children.end());
4036   }
4037
4038   child_range used_children() {
4039     return child_range(child_iterator(), child_iterator());
4040   }
4041   const_child_range used_children() const {
4042     return const_child_range(const_child_iterator(), const_child_iterator());
4043   }
4044
4045   static bool classof(const OMPClause *T) {
4046     return T->getClauseKind() == OMPC_depend;
4047   }
4048 };
4049
4050 /// This represents 'device' clause in the '#pragma omp ...'
4051 /// directive.
4052 ///
4053 /// \code
4054 /// #pragma omp target device(a)
4055 /// \endcode
4056 /// In this example directive '#pragma omp target' has clause 'device'
4057 /// with single expression 'a'.
4058 class OMPDeviceClause : public OMPClause, public OMPClauseWithPreInit {
4059   friend class OMPClauseReader;
4060
4061   /// Location of '('.
4062   SourceLocation LParenLoc;
4063
4064   /// Device number.
4065   Stmt *Device = nullptr;
4066
4067   /// Set the device number.
4068   ///
4069   /// \param E Device number.
4070   void setDevice(Expr *E) { Device = E; }
4071
4072 public:
4073   /// Build 'device' clause.
4074   ///
4075   /// \param E Expression associated with this clause.
4076   /// \param CaptureRegion Innermost OpenMP region where expressions in this
4077   /// clause must be captured.
4078   /// \param StartLoc Starting location of the clause.
4079   /// \param LParenLoc Location of '('.
4080   /// \param EndLoc Ending location of the clause.
4081   OMPDeviceClause(Expr *E, Stmt *HelperE, OpenMPDirectiveKind CaptureRegion,
4082                   SourceLocation StartLoc, SourceLocation LParenLoc,
4083                   SourceLocation EndLoc)
4084       : OMPClause(OMPC_device, StartLoc, EndLoc), OMPClauseWithPreInit(this),
4085         LParenLoc(LParenLoc), Device(E) {
4086     setPreInitStmt(HelperE, CaptureRegion);
4087   }
4088
4089   /// Build an empty clause.
4090   OMPDeviceClause()
4091       : OMPClause(OMPC_device, SourceLocation(), SourceLocation()),
4092         OMPClauseWithPreInit(this) {}
4093
4094   /// Sets the location of '('.
4095   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4096
4097   /// Returns the location of '('.
4098   SourceLocation getLParenLoc() const { return LParenLoc; }
4099
4100   /// Return device number.
4101   Expr *getDevice() { return cast<Expr>(Device); }
4102
4103   /// Return device number.
4104   Expr *getDevice() const { return cast<Expr>(Device); }
4105
4106   child_range children() { return child_range(&Device, &Device + 1); }
4107
4108   const_child_range children() const {
4109     return const_child_range(&Device, &Device + 1);
4110   }
4111
4112   child_range used_children() {
4113     return child_range(child_iterator(), child_iterator());
4114   }
4115   const_child_range used_children() const {
4116     return const_child_range(const_child_iterator(), const_child_iterator());
4117   }
4118
4119   static bool classof(const OMPClause *T) {
4120     return T->getClauseKind() == OMPC_device;
4121   }
4122 };
4123
4124 /// This represents 'threads' clause in the '#pragma omp ...' directive.
4125 ///
4126 /// \code
4127 /// #pragma omp ordered threads
4128 /// \endcode
4129 /// In this example directive '#pragma omp ordered' has simple 'threads' clause.
4130 class OMPThreadsClause : public OMPClause {
4131 public:
4132   /// Build 'threads' clause.
4133   ///
4134   /// \param StartLoc Starting location of the clause.
4135   /// \param EndLoc Ending location of the clause.
4136   OMPThreadsClause(SourceLocation StartLoc, SourceLocation EndLoc)
4137       : OMPClause(OMPC_threads, StartLoc, EndLoc) {}
4138
4139   /// Build an empty clause.
4140   OMPThreadsClause()
4141       : OMPClause(OMPC_threads, SourceLocation(), SourceLocation()) {}
4142
4143   child_range children() {
4144     return child_range(child_iterator(), child_iterator());
4145   }
4146
4147   const_child_range children() const {
4148     return const_child_range(const_child_iterator(), const_child_iterator());
4149   }
4150
4151   child_range used_children() {
4152     return child_range(child_iterator(), child_iterator());
4153   }
4154   const_child_range used_children() const {
4155     return const_child_range(const_child_iterator(), const_child_iterator());
4156   }
4157
4158   static bool classof(const OMPClause *T) {
4159     return T->getClauseKind() == OMPC_threads;
4160   }
4161 };
4162
4163 /// This represents 'simd' clause in the '#pragma omp ...' directive.
4164 ///
4165 /// \code
4166 /// #pragma omp ordered simd
4167 /// \endcode
4168 /// In this example directive '#pragma omp ordered' has simple 'simd' clause.
4169 class OMPSIMDClause : public OMPClause {
4170 public:
4171   /// Build 'simd' clause.
4172   ///
4173   /// \param StartLoc Starting location of the clause.
4174   /// \param EndLoc Ending location of the clause.
4175   OMPSIMDClause(SourceLocation StartLoc, SourceLocation EndLoc)
4176       : OMPClause(OMPC_simd, StartLoc, EndLoc) {}
4177
4178   /// Build an empty clause.
4179   OMPSIMDClause() : OMPClause(OMPC_simd, SourceLocation(), SourceLocation()) {}
4180
4181   child_range children() {
4182     return child_range(child_iterator(), child_iterator());
4183   }
4184
4185   const_child_range children() const {
4186     return const_child_range(const_child_iterator(), const_child_iterator());
4187   }
4188
4189   child_range used_children() {
4190     return child_range(child_iterator(), child_iterator());
4191   }
4192   const_child_range used_children() const {
4193     return const_child_range(const_child_iterator(), const_child_iterator());
4194   }
4195
4196   static bool classof(const OMPClause *T) {
4197     return T->getClauseKind() == OMPC_simd;
4198   }
4199 };
4200
4201 /// Struct that defines common infrastructure to handle mappable
4202 /// expressions used in OpenMP clauses.
4203 class OMPClauseMappableExprCommon {
4204 public:
4205   /// Class that represents a component of a mappable expression. E.g.
4206   /// for an expression S.a, the first component is a declaration reference
4207   /// expression associated with 'S' and the second is a member expression
4208   /// associated with the field declaration 'a'. If the expression is an array
4209   /// subscript it may not have any associated declaration. In that case the
4210   /// associated declaration is set to nullptr.
4211   class MappableComponent {
4212     /// Expression associated with the component.
4213     Expr *AssociatedExpression = nullptr;
4214
4215     /// Declaration associated with the declaration. If the component does
4216     /// not have a declaration (e.g. array subscripts or section), this is set
4217     /// to nullptr.
4218     ValueDecl *AssociatedDeclaration = nullptr;
4219
4220   public:
4221     explicit MappableComponent() = default;
4222     explicit MappableComponent(Expr *AssociatedExpression,
4223                                ValueDecl *AssociatedDeclaration)
4224         : AssociatedExpression(AssociatedExpression),
4225           AssociatedDeclaration(
4226               AssociatedDeclaration
4227                   ? cast<ValueDecl>(AssociatedDeclaration->getCanonicalDecl())
4228                   : nullptr) {}
4229
4230     Expr *getAssociatedExpression() const { return AssociatedExpression; }
4231
4232     ValueDecl *getAssociatedDeclaration() const {
4233       return AssociatedDeclaration;
4234     }
4235   };
4236
4237   // List of components of an expression. This first one is the whole
4238   // expression and the last one is the base expression.
4239   using MappableExprComponentList = SmallVector<MappableComponent, 8>;
4240   using MappableExprComponentListRef = ArrayRef<MappableComponent>;
4241
4242   // List of all component lists associated to the same base declaration.
4243   // E.g. if both 'S.a' and 'S.b' are a mappable expressions, each will have
4244   // their component list but the same base declaration 'S'.
4245   using MappableExprComponentLists = SmallVector<MappableExprComponentList, 8>;
4246   using MappableExprComponentListsRef = ArrayRef<MappableExprComponentList>;
4247
4248 protected:
4249   // Return the total number of elements in a list of component lists.
4250   static unsigned
4251   getComponentsTotalNumber(MappableExprComponentListsRef ComponentLists);
4252
4253   // Return the total number of elements in a list of declarations. All
4254   // declarations are expected to be canonical.
4255   static unsigned
4256   getUniqueDeclarationsTotalNumber(ArrayRef<const ValueDecl *> Declarations);
4257 };
4258
4259 /// This structure contains all sizes needed for by an
4260 /// OMPMappableExprListClause.
4261 struct OMPMappableExprListSizeTy {
4262   /// Number of expressions listed.
4263   unsigned NumVars;
4264   /// Number of unique base declarations.
4265   unsigned NumUniqueDeclarations;
4266   /// Number of component lists.
4267   unsigned NumComponentLists;
4268   /// Total number of expression components.
4269   unsigned NumComponents;
4270   OMPMappableExprListSizeTy() = default;
4271   OMPMappableExprListSizeTy(unsigned NumVars, unsigned NumUniqueDeclarations,
4272                             unsigned NumComponentLists, unsigned NumComponents)
4273       : NumVars(NumVars), NumUniqueDeclarations(NumUniqueDeclarations),
4274         NumComponentLists(NumComponentLists), NumComponents(NumComponents) {}
4275 };
4276
4277 /// This represents clauses with a list of expressions that are mappable.
4278 /// Examples of these clauses are 'map' in
4279 /// '#pragma omp target [enter|exit] [data]...' directives, and  'to' and 'from
4280 /// in '#pragma omp target update...' directives.
4281 template <class T>
4282 class OMPMappableExprListClause : public OMPVarListClause<T>,
4283                                   public OMPClauseMappableExprCommon {
4284   friend class OMPClauseReader;
4285
4286   /// Number of unique declarations in this clause.
4287   unsigned NumUniqueDeclarations;
4288
4289   /// Number of component lists in this clause.
4290   unsigned NumComponentLists;
4291
4292   /// Total number of components in this clause.
4293   unsigned NumComponents;
4294
4295   /// C++ nested name specifier for the associated user-defined mapper.
4296   NestedNameSpecifierLoc MapperQualifierLoc;
4297
4298   /// The associated user-defined mapper identifier information.
4299   DeclarationNameInfo MapperIdInfo;
4300
4301 protected:
4302   /// Build a clause for \a NumUniqueDeclarations declarations, \a
4303   /// NumComponentLists total component lists, and \a NumComponents total
4304   /// components.
4305   ///
4306   /// \param K Kind of the clause.
4307   /// \param Locs Locations needed to build a mappable clause. It includes 1)
4308   /// StartLoc: starting location of the clause (the clause keyword); 2)
4309   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
4310   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
4311   /// NumVars: number of expressions listed in this clause; 2)
4312   /// NumUniqueDeclarations: number of unique base declarations in this clause;
4313   /// 3) NumComponentLists: number of component lists in this clause; and 4)
4314   /// NumComponents: total number of expression components in the clause.
4315   /// \param MapperQualifierLocPtr C++ nested name specifier for the associated
4316   /// user-defined mapper.
4317   /// \param MapperIdInfoPtr The identifier of associated user-defined mapper.
4318   OMPMappableExprListClause(
4319       OpenMPClauseKind K, const OMPVarListLocTy &Locs,
4320       const OMPMappableExprListSizeTy &Sizes,
4321       NestedNameSpecifierLoc *MapperQualifierLocPtr = nullptr,
4322       DeclarationNameInfo *MapperIdInfoPtr = nullptr)
4323       : OMPVarListClause<T>(K, Locs.StartLoc, Locs.LParenLoc, Locs.EndLoc,
4324                             Sizes.NumVars),
4325         NumUniqueDeclarations(Sizes.NumUniqueDeclarations),
4326         NumComponentLists(Sizes.NumComponentLists),
4327         NumComponents(Sizes.NumComponents) {
4328     if (MapperQualifierLocPtr)
4329       MapperQualifierLoc = *MapperQualifierLocPtr;
4330     if (MapperIdInfoPtr)
4331       MapperIdInfo = *MapperIdInfoPtr;
4332   }
4333
4334   /// Get the unique declarations that are in the trailing objects of the
4335   /// class.
4336   MutableArrayRef<ValueDecl *> getUniqueDeclsRef() {
4337     return MutableArrayRef<ValueDecl *>(
4338         static_cast<T *>(this)->template getTrailingObjects<ValueDecl *>(),
4339         NumUniqueDeclarations);
4340   }
4341
4342   /// Get the unique declarations that are in the trailing objects of the
4343   /// class.
4344   ArrayRef<ValueDecl *> getUniqueDeclsRef() const {
4345     return ArrayRef<ValueDecl *>(
4346         static_cast<const T *>(this)
4347             ->template getTrailingObjects<ValueDecl *>(),
4348         NumUniqueDeclarations);
4349   }
4350
4351   /// Set the unique declarations that are in the trailing objects of the
4352   /// class.
4353   void setUniqueDecls(ArrayRef<ValueDecl *> UDs) {
4354     assert(UDs.size() == NumUniqueDeclarations &&
4355            "Unexpected amount of unique declarations.");
4356     std::copy(UDs.begin(), UDs.end(), getUniqueDeclsRef().begin());
4357   }
4358
4359   /// Get the number of lists per declaration that are in the trailing
4360   /// objects of the class.
4361   MutableArrayRef<unsigned> getDeclNumListsRef() {
4362     return MutableArrayRef<unsigned>(
4363         static_cast<T *>(this)->template getTrailingObjects<unsigned>(),
4364         NumUniqueDeclarations);
4365   }
4366
4367   /// Get the number of lists per declaration that are in the trailing
4368   /// objects of the class.
4369   ArrayRef<unsigned> getDeclNumListsRef() const {
4370     return ArrayRef<unsigned>(
4371         static_cast<const T *>(this)->template getTrailingObjects<unsigned>(),
4372         NumUniqueDeclarations);
4373   }
4374
4375   /// Set the number of lists per declaration that are in the trailing
4376   /// objects of the class.
4377   void setDeclNumLists(ArrayRef<unsigned> DNLs) {
4378     assert(DNLs.size() == NumUniqueDeclarations &&
4379            "Unexpected amount of list numbers.");
4380     std::copy(DNLs.begin(), DNLs.end(), getDeclNumListsRef().begin());
4381   }
4382
4383   /// Get the cumulative component lists sizes that are in the trailing
4384   /// objects of the class. They are appended after the number of lists.
4385   MutableArrayRef<unsigned> getComponentListSizesRef() {
4386     return MutableArrayRef<unsigned>(
4387         static_cast<T *>(this)->template getTrailingObjects<unsigned>() +
4388             NumUniqueDeclarations,
4389         NumComponentLists);
4390   }
4391
4392   /// Get the cumulative component lists sizes that are in the trailing
4393   /// objects of the class. They are appended after the number of lists.
4394   ArrayRef<unsigned> getComponentListSizesRef() const {
4395     return ArrayRef<unsigned>(
4396         static_cast<const T *>(this)->template getTrailingObjects<unsigned>() +
4397             NumUniqueDeclarations,
4398         NumComponentLists);
4399   }
4400
4401   /// Set the cumulative component lists sizes that are in the trailing
4402   /// objects of the class.
4403   void setComponentListSizes(ArrayRef<unsigned> CLSs) {
4404     assert(CLSs.size() == NumComponentLists &&
4405            "Unexpected amount of component lists.");
4406     std::copy(CLSs.begin(), CLSs.end(), getComponentListSizesRef().begin());
4407   }
4408
4409   /// Get the components that are in the trailing objects of the class.
4410   MutableArrayRef<MappableComponent> getComponentsRef() {
4411     return MutableArrayRef<MappableComponent>(
4412         static_cast<T *>(this)
4413             ->template getTrailingObjects<MappableComponent>(),
4414         NumComponents);
4415   }
4416
4417   /// Get the components that are in the trailing objects of the class.
4418   ArrayRef<MappableComponent> getComponentsRef() const {
4419     return ArrayRef<MappableComponent>(
4420         static_cast<const T *>(this)
4421             ->template getTrailingObjects<MappableComponent>(),
4422         NumComponents);
4423   }
4424
4425   /// Set the components that are in the trailing objects of the class.
4426   /// This requires the list sizes so that it can also fill the original
4427   /// expressions, which are the first component of each list.
4428   void setComponents(ArrayRef<MappableComponent> Components,
4429                      ArrayRef<unsigned> CLSs) {
4430     assert(Components.size() == NumComponents &&
4431            "Unexpected amount of component lists.");
4432     assert(CLSs.size() == NumComponentLists &&
4433            "Unexpected amount of list sizes.");
4434     std::copy(Components.begin(), Components.end(), getComponentsRef().begin());
4435   }
4436
4437   /// Fill the clause information from the list of declarations and
4438   /// associated component lists.
4439   void setClauseInfo(ArrayRef<ValueDecl *> Declarations,
4440                      MappableExprComponentListsRef ComponentLists) {
4441     // Perform some checks to make sure the data sizes are consistent with the
4442     // information available when the clause was created.
4443     assert(getUniqueDeclarationsTotalNumber(Declarations) ==
4444                NumUniqueDeclarations &&
4445            "Unexpected number of mappable expression info entries!");
4446     assert(getComponentsTotalNumber(ComponentLists) == NumComponents &&
4447            "Unexpected total number of components!");
4448     assert(Declarations.size() == ComponentLists.size() &&
4449            "Declaration and component lists size is not consistent!");
4450     assert(Declarations.size() == NumComponentLists &&
4451            "Unexpected declaration and component lists size!");
4452
4453     // Organize the components by declaration and retrieve the original
4454     // expression. Original expressions are always the first component of the
4455     // mappable component list.
4456     llvm::MapVector<ValueDecl *, SmallVector<MappableExprComponentListRef, 8>>
4457         ComponentListMap;
4458     {
4459       auto CI = ComponentLists.begin();
4460       for (auto DI = Declarations.begin(), DE = Declarations.end(); DI != DE;
4461            ++DI, ++CI) {
4462         assert(!CI->empty() && "Invalid component list!");
4463         ComponentListMap[*DI].push_back(*CI);
4464       }
4465     }
4466
4467     // Iterators of the target storage.
4468     auto UniqueDeclarations = getUniqueDeclsRef();
4469     auto UDI = UniqueDeclarations.begin();
4470
4471     auto DeclNumLists = getDeclNumListsRef();
4472     auto DNLI = DeclNumLists.begin();
4473
4474     auto ComponentListSizes = getComponentListSizesRef();
4475     auto CLSI = ComponentListSizes.begin();
4476
4477     auto Components = getComponentsRef();
4478     auto CI = Components.begin();
4479
4480     // Variable to compute the accumulation of the number of components.
4481     unsigned PrevSize = 0u;
4482
4483     // Scan all the declarations and associated component lists.
4484     for (auto &M : ComponentListMap) {
4485       // The declaration.
4486       auto *D = M.first;
4487       // The component lists.
4488       auto CL = M.second;
4489
4490       // Initialize the entry.
4491       *UDI = D;
4492       ++UDI;
4493
4494       *DNLI = CL.size();
4495       ++DNLI;
4496
4497       // Obtain the cumulative sizes and concatenate all the components in the
4498       // reserved storage.
4499       for (auto C : CL) {
4500         // Accumulate with the previous size.
4501         PrevSize += C.size();
4502
4503         // Save the size.
4504         *CLSI = PrevSize;
4505         ++CLSI;
4506
4507         // Append components after the current components iterator.
4508         CI = std::copy(C.begin(), C.end(), CI);
4509       }
4510     }
4511   }
4512
4513   /// Set the nested name specifier of associated user-defined mapper.
4514   void setMapperQualifierLoc(NestedNameSpecifierLoc NNSL) {
4515     MapperQualifierLoc = NNSL;
4516   }
4517
4518   /// Set the name of associated user-defined mapper.
4519   void setMapperIdInfo(DeclarationNameInfo MapperId) {
4520     MapperIdInfo = MapperId;
4521   }
4522
4523   /// Get the user-defined mapper references that are in the trailing objects of
4524   /// the class.
4525   MutableArrayRef<Expr *> getUDMapperRefs() {
4526     return llvm::makeMutableArrayRef<Expr *>(
4527         static_cast<T *>(this)->template getTrailingObjects<Expr *>() +
4528             OMPVarListClause<T>::varlist_size(),
4529         OMPVarListClause<T>::varlist_size());
4530   }
4531
4532   /// Get the user-defined mappers references that are in the trailing objects
4533   /// of the class.
4534   ArrayRef<Expr *> getUDMapperRefs() const {
4535     return llvm::makeArrayRef<Expr *>(
4536         static_cast<T *>(this)->template getTrailingObjects<Expr *>() +
4537             OMPVarListClause<T>::varlist_size(),
4538         OMPVarListClause<T>::varlist_size());
4539   }
4540
4541   /// Set the user-defined mappers that are in the trailing objects of the
4542   /// class.
4543   void setUDMapperRefs(ArrayRef<Expr *> DMDs) {
4544     assert(DMDs.size() == OMPVarListClause<T>::varlist_size() &&
4545            "Unexpected number of user-defined mappers.");
4546     std::copy(DMDs.begin(), DMDs.end(), getUDMapperRefs().begin());
4547   }
4548
4549 public:
4550   /// Return the number of unique base declarations in this clause.
4551   unsigned getUniqueDeclarationsNum() const { return NumUniqueDeclarations; }
4552
4553   /// Return the number of lists derived from the clause expressions.
4554   unsigned getTotalComponentListNum() const { return NumComponentLists; }
4555
4556   /// Return the total number of components in all lists derived from the
4557   /// clause.
4558   unsigned getTotalComponentsNum() const { return NumComponents; }
4559
4560   /// Gets the nested name specifier for associated user-defined mapper.
4561   NestedNameSpecifierLoc getMapperQualifierLoc() const {
4562     return MapperQualifierLoc;
4563   }
4564
4565   /// Gets the name info for associated user-defined mapper.
4566   const DeclarationNameInfo &getMapperIdInfo() const { return MapperIdInfo; }
4567
4568   /// Iterator that browse the components by lists. It also allows
4569   /// browsing components of a single declaration.
4570   class const_component_lists_iterator
4571       : public llvm::iterator_adaptor_base<
4572             const_component_lists_iterator,
4573             MappableExprComponentListRef::const_iterator,
4574             std::forward_iterator_tag, MappableComponent, ptrdiff_t,
4575             MappableComponent, MappableComponent> {
4576     // The declaration the iterator currently refers to.
4577     ArrayRef<ValueDecl *>::iterator DeclCur;
4578
4579     // The list number associated with the current declaration.
4580     ArrayRef<unsigned>::iterator NumListsCur;
4581
4582     // Remaining lists for the current declaration.
4583     unsigned RemainingLists = 0;
4584
4585     // The cumulative size of the previous list, or zero if there is no previous
4586     // list.
4587     unsigned PrevListSize = 0;
4588
4589     // The cumulative sizes of the current list - it will delimit the remaining
4590     // range of interest.
4591     ArrayRef<unsigned>::const_iterator ListSizeCur;
4592     ArrayRef<unsigned>::const_iterator ListSizeEnd;
4593
4594     // Iterator to the end of the components storage.
4595     MappableExprComponentListRef::const_iterator End;
4596
4597   public:
4598     /// Construct an iterator that scans all lists.
4599     explicit const_component_lists_iterator(
4600         ArrayRef<ValueDecl *> UniqueDecls, ArrayRef<unsigned> DeclsListNum,
4601         ArrayRef<unsigned> CumulativeListSizes,
4602         MappableExprComponentListRef Components)
4603         : const_component_lists_iterator::iterator_adaptor_base(
4604               Components.begin()),
4605           DeclCur(UniqueDecls.begin()), NumListsCur(DeclsListNum.begin()),
4606           ListSizeCur(CumulativeListSizes.begin()),
4607           ListSizeEnd(CumulativeListSizes.end()), End(Components.end()) {
4608       assert(UniqueDecls.size() == DeclsListNum.size() &&
4609              "Inconsistent number of declarations and list sizes!");
4610       if (!DeclsListNum.empty())
4611         RemainingLists = *NumListsCur;
4612     }
4613
4614     /// Construct an iterator that scan lists for a given declaration \a
4615     /// Declaration.
4616     explicit const_component_lists_iterator(
4617         const ValueDecl *Declaration, ArrayRef<ValueDecl *> UniqueDecls,
4618         ArrayRef<unsigned> DeclsListNum, ArrayRef<unsigned> CumulativeListSizes,
4619         MappableExprComponentListRef Components)
4620         : const_component_lists_iterator(UniqueDecls, DeclsListNum,
4621                                          CumulativeListSizes, Components) {
4622       // Look for the desired declaration. While we are looking for it, we
4623       // update the state so that we know the component where a given list
4624       // starts.
4625       for (; DeclCur != UniqueDecls.end(); ++DeclCur, ++NumListsCur) {
4626         if (*DeclCur == Declaration)
4627           break;
4628
4629         assert(*NumListsCur > 0 && "No lists associated with declaration??");
4630
4631         // Skip the lists associated with the current declaration, but save the
4632         // last list size that was skipped.
4633         std::advance(ListSizeCur, *NumListsCur - 1);
4634         PrevListSize = *ListSizeCur;
4635         ++ListSizeCur;
4636       }
4637
4638       // If we didn't find any declaration, advance the iterator to after the
4639       // last component and set remaining lists to zero.
4640       if (ListSizeCur == CumulativeListSizes.end()) {
4641         this->I = End;
4642         RemainingLists = 0u;
4643         return;
4644       }
4645
4646       // Set the remaining lists with the total number of lists of the current
4647       // declaration.
4648       RemainingLists = *NumListsCur;
4649
4650       // Adjust the list size end iterator to the end of the relevant range.
4651       ListSizeEnd = ListSizeCur;
4652       std::advance(ListSizeEnd, RemainingLists);
4653
4654       // Given that the list sizes are cumulative, the index of the component
4655       // that start the list is the size of the previous list.
4656       std::advance(this->I, PrevListSize);
4657     }
4658
4659     // Return the array with the current list. The sizes are cumulative, so the
4660     // array size is the difference between the current size and previous one.
4661     std::pair<const ValueDecl *, MappableExprComponentListRef>
4662     operator*() const {
4663       assert(ListSizeCur != ListSizeEnd && "Invalid iterator!");
4664       return std::make_pair(
4665           *DeclCur,
4666           MappableExprComponentListRef(&*this->I, *ListSizeCur - PrevListSize));
4667     }
4668     std::pair<const ValueDecl *, MappableExprComponentListRef>
4669     operator->() const {
4670       return **this;
4671     }
4672
4673     // Skip the components of the current list.
4674     const_component_lists_iterator &operator++() {
4675       assert(ListSizeCur != ListSizeEnd && RemainingLists &&
4676              "Invalid iterator!");
4677
4678       // If we don't have more lists just skip all the components. Otherwise,
4679       // advance the iterator by the number of components in the current list.
4680       if (std::next(ListSizeCur) == ListSizeEnd) {
4681         this->I = End;
4682         RemainingLists = 0;
4683       } else {
4684         std::advance(this->I, *ListSizeCur - PrevListSize);
4685         PrevListSize = *ListSizeCur;
4686
4687         // We are done with a declaration, move to the next one.
4688         if (!(--RemainingLists)) {
4689           ++DeclCur;
4690           ++NumListsCur;
4691           RemainingLists = *NumListsCur;
4692           assert(RemainingLists && "No lists in the following declaration??");
4693         }
4694       }
4695
4696       ++ListSizeCur;
4697       return *this;
4698     }
4699   };
4700
4701   using const_component_lists_range =
4702       llvm::iterator_range<const_component_lists_iterator>;
4703
4704   /// Iterators for all component lists.
4705   const_component_lists_iterator component_lists_begin() const {
4706     return const_component_lists_iterator(
4707         getUniqueDeclsRef(), getDeclNumListsRef(), getComponentListSizesRef(),
4708         getComponentsRef());
4709   }
4710   const_component_lists_iterator component_lists_end() const {
4711     return const_component_lists_iterator(
4712         ArrayRef<ValueDecl *>(), ArrayRef<unsigned>(), ArrayRef<unsigned>(),
4713         MappableExprComponentListRef(getComponentsRef().end(),
4714                                      getComponentsRef().end()));
4715   }
4716   const_component_lists_range component_lists() const {
4717     return {component_lists_begin(), component_lists_end()};
4718   }
4719
4720   /// Iterators for component lists associated with the provided
4721   /// declaration.
4722   const_component_lists_iterator
4723   decl_component_lists_begin(const ValueDecl *VD) const {
4724     return const_component_lists_iterator(
4725         VD, getUniqueDeclsRef(), getDeclNumListsRef(),
4726         getComponentListSizesRef(), getComponentsRef());
4727   }
4728   const_component_lists_iterator decl_component_lists_end() const {
4729     return component_lists_end();
4730   }
4731   const_component_lists_range decl_component_lists(const ValueDecl *VD) const {
4732     return {decl_component_lists_begin(VD), decl_component_lists_end()};
4733   }
4734
4735   /// Iterators to access all the declarations, number of lists, list sizes, and
4736   /// components.
4737   using const_all_decls_iterator = ArrayRef<ValueDecl *>::iterator;
4738   using const_all_decls_range = llvm::iterator_range<const_all_decls_iterator>;
4739
4740   const_all_decls_range all_decls() const {
4741     auto A = getUniqueDeclsRef();
4742     return const_all_decls_range(A.begin(), A.end());
4743   }
4744
4745   using const_all_num_lists_iterator = ArrayRef<unsigned>::iterator;
4746   using const_all_num_lists_range =
4747       llvm::iterator_range<const_all_num_lists_iterator>;
4748
4749   const_all_num_lists_range all_num_lists() const {
4750     auto A = getDeclNumListsRef();
4751     return const_all_num_lists_range(A.begin(), A.end());
4752   }
4753
4754   using const_all_lists_sizes_iterator = ArrayRef<unsigned>::iterator;
4755   using const_all_lists_sizes_range =
4756       llvm::iterator_range<const_all_lists_sizes_iterator>;
4757
4758   const_all_lists_sizes_range all_lists_sizes() const {
4759     auto A = getComponentListSizesRef();
4760     return const_all_lists_sizes_range(A.begin(), A.end());
4761   }
4762
4763   using const_all_components_iterator = ArrayRef<MappableComponent>::iterator;
4764   using const_all_components_range =
4765       llvm::iterator_range<const_all_components_iterator>;
4766
4767   const_all_components_range all_components() const {
4768     auto A = getComponentsRef();
4769     return const_all_components_range(A.begin(), A.end());
4770   }
4771
4772   using mapperlist_iterator = MutableArrayRef<Expr *>::iterator;
4773   using mapperlist_const_iterator = ArrayRef<const Expr *>::iterator;
4774   using mapperlist_range = llvm::iterator_range<mapperlist_iterator>;
4775   using mapperlist_const_range =
4776       llvm::iterator_range<mapperlist_const_iterator>;
4777
4778   mapperlist_iterator mapperlist_begin() { return getUDMapperRefs().begin(); }
4779   mapperlist_iterator mapperlist_end() { return getUDMapperRefs().end(); }
4780   mapperlist_const_iterator mapperlist_begin() const {
4781     return getUDMapperRefs().begin();
4782   }
4783   mapperlist_const_iterator mapperlist_end() const {
4784     return getUDMapperRefs().end();
4785   }
4786   mapperlist_range mapperlists() {
4787     return mapperlist_range(mapperlist_begin(), mapperlist_end());
4788   }
4789   mapperlist_const_range mapperlists() const {
4790     return mapperlist_const_range(mapperlist_begin(), mapperlist_end());
4791   }
4792 };
4793
4794 /// This represents clause 'map' in the '#pragma omp ...'
4795 /// directives.
4796 ///
4797 /// \code
4798 /// #pragma omp target map(a,b)
4799 /// \endcode
4800 /// In this example directive '#pragma omp target' has clause 'map'
4801 /// with the variables 'a' and 'b'.
4802 class OMPMapClause final : public OMPMappableExprListClause<OMPMapClause>,
4803                            private llvm::TrailingObjects<
4804                                OMPMapClause, Expr *, ValueDecl *, unsigned,
4805                                OMPClauseMappableExprCommon::MappableComponent> {
4806   friend class OMPClauseReader;
4807   friend OMPMappableExprListClause;
4808   friend OMPVarListClause;
4809   friend TrailingObjects;
4810
4811   /// Define the sizes of each trailing object array except the last one. This
4812   /// is required for TrailingObjects to work properly.
4813   size_t numTrailingObjects(OverloadToken<Expr *>) const {
4814     // There are varlist_size() of expressions, and varlist_size() of
4815     // user-defined mappers.
4816     return 2 * varlist_size();
4817   }
4818   size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
4819     return getUniqueDeclarationsNum();
4820   }
4821   size_t numTrailingObjects(OverloadToken<unsigned>) const {
4822     return getUniqueDeclarationsNum() + getTotalComponentListNum();
4823   }
4824
4825 public:
4826   /// Number of allowed map-type-modifiers.
4827   static constexpr unsigned NumberOfModifiers =
4828       OMPC_MAP_MODIFIER_last - OMPC_MAP_MODIFIER_unknown - 1;
4829
4830 private:
4831   /// Map-type-modifiers for the 'map' clause.
4832   OpenMPMapModifierKind MapTypeModifiers[NumberOfModifiers] = {
4833       OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
4834       OMPC_MAP_MODIFIER_unknown};
4835
4836   /// Location of map-type-modifiers for the 'map' clause.
4837   SourceLocation MapTypeModifiersLoc[NumberOfModifiers];
4838
4839   /// Map type for the 'map' clause.
4840   OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
4841
4842   /// Is this an implicit map type or not.
4843   bool MapTypeIsImplicit = false;
4844
4845   /// Location of the map type.
4846   SourceLocation MapLoc;
4847
4848   /// Colon location.
4849   SourceLocation ColonLoc;
4850
4851   /// Build a clause for \a NumVars listed expressions, \a
4852   /// NumUniqueDeclarations declarations, \a NumComponentLists total component
4853   /// lists, and \a NumComponents total expression components.
4854   ///
4855   /// \param MapModifiers Map-type-modifiers.
4856   /// \param MapModifiersLoc Locations of map-type-modifiers.
4857   /// \param MapperQualifierLoc C++ nested name specifier for the associated
4858   /// user-defined mapper.
4859   /// \param MapperIdInfo The identifier of associated user-defined mapper.
4860   /// \param MapType Map type.
4861   /// \param MapTypeIsImplicit Map type is inferred implicitly.
4862   /// \param MapLoc Location of the map type.
4863   /// \param Locs Locations needed to build a mappable clause. It includes 1)
4864   /// StartLoc: starting location of the clause (the clause keyword); 2)
4865   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
4866   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
4867   /// NumVars: number of expressions listed in this clause; 2)
4868   /// NumUniqueDeclarations: number of unique base declarations in this clause;
4869   /// 3) NumComponentLists: number of component lists in this clause; and 4)
4870   /// NumComponents: total number of expression components in the clause.
4871   explicit OMPMapClause(ArrayRef<OpenMPMapModifierKind> MapModifiers,
4872                         ArrayRef<SourceLocation> MapModifiersLoc,
4873                         NestedNameSpecifierLoc MapperQualifierLoc,
4874                         DeclarationNameInfo MapperIdInfo,
4875                         OpenMPMapClauseKind MapType, bool MapTypeIsImplicit,
4876                         SourceLocation MapLoc, const OMPVarListLocTy &Locs,
4877                         const OMPMappableExprListSizeTy &Sizes)
4878       : OMPMappableExprListClause(OMPC_map, Locs, Sizes, &MapperQualifierLoc,
4879                                   &MapperIdInfo),
4880         MapType(MapType), MapTypeIsImplicit(MapTypeIsImplicit), MapLoc(MapLoc) {
4881     assert(llvm::array_lengthof(MapTypeModifiers) == MapModifiers.size() &&
4882            "Unexpected number of map type modifiers.");
4883     llvm::copy(MapModifiers, std::begin(MapTypeModifiers));
4884
4885     assert(llvm::array_lengthof(MapTypeModifiersLoc) ==
4886                MapModifiersLoc.size() &&
4887            "Unexpected number of map type modifier locations.");
4888     llvm::copy(MapModifiersLoc, std::begin(MapTypeModifiersLoc));
4889   }
4890
4891   /// Build an empty clause.
4892   ///
4893   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
4894   /// NumVars: number of expressions listed in this clause; 2)
4895   /// NumUniqueDeclarations: number of unique base declarations in this clause;
4896   /// 3) NumComponentLists: number of component lists in this clause; and 4)
4897   /// NumComponents: total number of expression components in the clause.
4898   explicit OMPMapClause(const OMPMappableExprListSizeTy &Sizes)
4899       : OMPMappableExprListClause(OMPC_map, OMPVarListLocTy(), Sizes) {}
4900
4901   /// Set map-type-modifier for the clause.
4902   ///
4903   /// \param I index for map-type-modifier.
4904   /// \param T map-type-modifier for the clause.
4905   void setMapTypeModifier(unsigned I, OpenMPMapModifierKind T) {
4906     assert(I < NumberOfModifiers &&
4907            "Unexpected index to store map type modifier, exceeds array size.");
4908     MapTypeModifiers[I] = T;
4909   }
4910
4911   /// Set location for the map-type-modifier.
4912   ///
4913   /// \param I index for map-type-modifier location.
4914   /// \param TLoc map-type-modifier location.
4915   void setMapTypeModifierLoc(unsigned I, SourceLocation TLoc) {
4916     assert(I < NumberOfModifiers &&
4917            "Index to store map type modifier location exceeds array size.");
4918     MapTypeModifiersLoc[I] = TLoc;
4919   }
4920
4921   /// Set type for the clause.
4922   ///
4923   /// \param T Type for the clause.
4924   void setMapType(OpenMPMapClauseKind T) { MapType = T; }
4925
4926   /// Set type location.
4927   ///
4928   /// \param TLoc Type location.
4929   void setMapLoc(SourceLocation TLoc) { MapLoc = TLoc; }
4930
4931   /// Set colon location.
4932   void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
4933
4934 public:
4935   /// Creates clause with a list of variables \a VL.
4936   ///
4937   /// \param C AST context.
4938   /// \param Locs Locations needed to build a mappable clause. It includes 1)
4939   /// StartLoc: starting location of the clause (the clause keyword); 2)
4940   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
4941   /// \param Vars The original expression used in the clause.
4942   /// \param Declarations Declarations used in the clause.
4943   /// \param ComponentLists Component lists used in the clause.
4944   /// \param UDMapperRefs References to user-defined mappers associated with
4945   /// expressions used in the clause.
4946   /// \param MapModifiers Map-type-modifiers.
4947   /// \param MapModifiersLoc Location of map-type-modifiers.
4948   /// \param UDMQualifierLoc C++ nested name specifier for the associated
4949   /// user-defined mapper.
4950   /// \param MapperId The identifier of associated user-defined mapper.
4951   /// \param Type Map type.
4952   /// \param TypeIsImplicit Map type is inferred implicitly.
4953   /// \param TypeLoc Location of the map type.
4954   static OMPMapClause *
4955   Create(const ASTContext &C, const OMPVarListLocTy &Locs,
4956          ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
4957          MappableExprComponentListsRef ComponentLists,
4958          ArrayRef<Expr *> UDMapperRefs,
4959          ArrayRef<OpenMPMapModifierKind> MapModifiers,
4960          ArrayRef<SourceLocation> MapModifiersLoc,
4961          NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId,
4962          OpenMPMapClauseKind Type, bool TypeIsImplicit, SourceLocation TypeLoc);
4963
4964   /// Creates an empty clause with the place for \a NumVars original
4965   /// expressions, \a NumUniqueDeclarations declarations, \NumComponentLists
4966   /// lists, and \a NumComponents expression components.
4967   ///
4968   /// \param C AST context.
4969   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
4970   /// NumVars: number of expressions listed in this clause; 2)
4971   /// NumUniqueDeclarations: number of unique base declarations in this clause;
4972   /// 3) NumComponentLists: number of component lists in this clause; and 4)
4973   /// NumComponents: total number of expression components in the clause.
4974   static OMPMapClause *CreateEmpty(const ASTContext &C,
4975                                    const OMPMappableExprListSizeTy &Sizes);
4976
4977   /// Fetches mapping kind for the clause.
4978   OpenMPMapClauseKind getMapType() const LLVM_READONLY { return MapType; }
4979
4980   /// Is this an implicit map type?
4981   /// We have to capture 'IsMapTypeImplicit' from the parser for more
4982   /// informative error messages.  It helps distinguish map(r) from
4983   /// map(tofrom: r), which is important to print more helpful error
4984   /// messages for some target directives.
4985   bool isImplicitMapType() const LLVM_READONLY { return MapTypeIsImplicit; }
4986
4987   /// Fetches the map-type-modifier at 'Cnt' index of array of modifiers.
4988   ///
4989   /// \param Cnt index for map-type-modifier.
4990   OpenMPMapModifierKind getMapTypeModifier(unsigned Cnt) const LLVM_READONLY {
4991     assert(Cnt < NumberOfModifiers &&
4992            "Requested modifier exceeds the total number of modifiers.");
4993     return MapTypeModifiers[Cnt];
4994   }
4995
4996   /// Fetches the map-type-modifier location at 'Cnt' index of array of
4997   /// modifiers' locations.
4998   ///
4999   /// \param Cnt index for map-type-modifier location.
5000   SourceLocation getMapTypeModifierLoc(unsigned Cnt) const LLVM_READONLY {
5001     assert(Cnt < NumberOfModifiers &&
5002            "Requested modifier location exceeds total number of modifiers.");
5003     return MapTypeModifiersLoc[Cnt];
5004   }
5005
5006   /// Fetches ArrayRef of map-type-modifiers.
5007   ArrayRef<OpenMPMapModifierKind> getMapTypeModifiers() const LLVM_READONLY {
5008     return llvm::makeArrayRef(MapTypeModifiers);
5009   }
5010
5011   /// Fetches ArrayRef of location of map-type-modifiers.
5012   ArrayRef<SourceLocation> getMapTypeModifiersLoc() const LLVM_READONLY {
5013     return llvm::makeArrayRef(MapTypeModifiersLoc);
5014   }
5015
5016   /// Fetches location of clause mapping kind.
5017   SourceLocation getMapLoc() const LLVM_READONLY { return MapLoc; }
5018
5019   /// Get colon location.
5020   SourceLocation getColonLoc() const { return ColonLoc; }
5021
5022   child_range children() {
5023     return child_range(
5024         reinterpret_cast<Stmt **>(varlist_begin()),
5025         reinterpret_cast<Stmt **>(varlist_end()));
5026   }
5027
5028   const_child_range children() const {
5029     auto Children = const_cast<OMPMapClause *>(this)->children();
5030     return const_child_range(Children.begin(), Children.end());
5031   }
5032
5033   child_range used_children() {
5034     if (MapType == OMPC_MAP_to || MapType == OMPC_MAP_tofrom)
5035       return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
5036                          reinterpret_cast<Stmt **>(varlist_end()));
5037     return child_range(child_iterator(), child_iterator());
5038   }
5039   const_child_range used_children() const {
5040     auto Children = const_cast<OMPMapClause *>(this)->used_children();
5041     return const_child_range(Children.begin(), Children.end());
5042   }
5043
5044
5045   static bool classof(const OMPClause *T) {
5046     return T->getClauseKind() == OMPC_map;
5047   }
5048 };
5049
5050 /// This represents 'num_teams' clause in the '#pragma omp ...'
5051 /// directive.
5052 ///
5053 /// \code
5054 /// #pragma omp teams num_teams(n)
5055 /// \endcode
5056 /// In this example directive '#pragma omp teams' has clause 'num_teams'
5057 /// with single expression 'n'.
5058 class OMPNumTeamsClause : public OMPClause, public OMPClauseWithPreInit {
5059   friend class OMPClauseReader;
5060
5061   /// Location of '('.
5062   SourceLocation LParenLoc;
5063
5064   /// NumTeams number.
5065   Stmt *NumTeams = nullptr;
5066
5067   /// Set the NumTeams number.
5068   ///
5069   /// \param E NumTeams number.
5070   void setNumTeams(Expr *E) { NumTeams = E; }
5071
5072 public:
5073   /// Build 'num_teams' clause.
5074   ///
5075   /// \param E Expression associated with this clause.
5076   /// \param HelperE Helper Expression associated with this clause.
5077   /// \param CaptureRegion Innermost OpenMP region where expressions in this
5078   /// clause must be captured.
5079   /// \param StartLoc Starting location of the clause.
5080   /// \param LParenLoc Location of '('.
5081   /// \param EndLoc Ending location of the clause.
5082   OMPNumTeamsClause(Expr *E, Stmt *HelperE, OpenMPDirectiveKind CaptureRegion,
5083                     SourceLocation StartLoc, SourceLocation LParenLoc,
5084                     SourceLocation EndLoc)
5085       : OMPClause(OMPC_num_teams, StartLoc, EndLoc), OMPClauseWithPreInit(this),
5086         LParenLoc(LParenLoc), NumTeams(E) {
5087     setPreInitStmt(HelperE, CaptureRegion);
5088   }
5089
5090   /// Build an empty clause.
5091   OMPNumTeamsClause()
5092       : OMPClause(OMPC_num_teams, SourceLocation(), SourceLocation()),
5093         OMPClauseWithPreInit(this) {}
5094
5095   /// Sets the location of '('.
5096   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5097
5098   /// Returns the location of '('.
5099   SourceLocation getLParenLoc() const { return LParenLoc; }
5100
5101   /// Return NumTeams number.
5102   Expr *getNumTeams() { return cast<Expr>(NumTeams); }
5103
5104   /// Return NumTeams number.
5105   Expr *getNumTeams() const { return cast<Expr>(NumTeams); }
5106
5107   child_range children() { return child_range(&NumTeams, &NumTeams + 1); }
5108
5109   const_child_range children() const {
5110     return const_child_range(&NumTeams, &NumTeams + 1);
5111   }
5112
5113   child_range used_children() {
5114     return child_range(child_iterator(), child_iterator());
5115   }
5116   const_child_range used_children() const {
5117     return const_child_range(const_child_iterator(), const_child_iterator());
5118   }
5119
5120   static bool classof(const OMPClause *T) {
5121     return T->getClauseKind() == OMPC_num_teams;
5122   }
5123 };
5124
5125 /// This represents 'thread_limit' clause in the '#pragma omp ...'
5126 /// directive.
5127 ///
5128 /// \code
5129 /// #pragma omp teams thread_limit(n)
5130 /// \endcode
5131 /// In this example directive '#pragma omp teams' has clause 'thread_limit'
5132 /// with single expression 'n'.
5133 class OMPThreadLimitClause : public OMPClause, public OMPClauseWithPreInit {
5134   friend class OMPClauseReader;
5135
5136   /// Location of '('.
5137   SourceLocation LParenLoc;
5138
5139   /// ThreadLimit number.
5140   Stmt *ThreadLimit = nullptr;
5141
5142   /// Set the ThreadLimit number.
5143   ///
5144   /// \param E ThreadLimit number.
5145   void setThreadLimit(Expr *E) { ThreadLimit = E; }
5146
5147 public:
5148   /// Build 'thread_limit' clause.
5149   ///
5150   /// \param E Expression associated with this clause.
5151   /// \param HelperE Helper Expression associated with this clause.
5152   /// \param CaptureRegion Innermost OpenMP region where expressions in this
5153   /// clause must be captured.
5154   /// \param StartLoc Starting location of the clause.
5155   /// \param LParenLoc Location of '('.
5156   /// \param EndLoc Ending location of the clause.
5157   OMPThreadLimitClause(Expr *E, Stmt *HelperE,
5158                        OpenMPDirectiveKind CaptureRegion,
5159                        SourceLocation StartLoc, SourceLocation LParenLoc,
5160                        SourceLocation EndLoc)
5161       : OMPClause(OMPC_thread_limit, StartLoc, EndLoc),
5162         OMPClauseWithPreInit(this), LParenLoc(LParenLoc), ThreadLimit(E) {
5163     setPreInitStmt(HelperE, CaptureRegion);
5164   }
5165
5166   /// Build an empty clause.
5167   OMPThreadLimitClause()
5168       : OMPClause(OMPC_thread_limit, SourceLocation(), SourceLocation()),
5169         OMPClauseWithPreInit(this) {}
5170
5171   /// Sets the location of '('.
5172   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5173
5174   /// Returns the location of '('.
5175   SourceLocation getLParenLoc() const { return LParenLoc; }
5176
5177   /// Return ThreadLimit number.
5178   Expr *getThreadLimit() { return cast<Expr>(ThreadLimit); }
5179
5180   /// Return ThreadLimit number.
5181   Expr *getThreadLimit() const { return cast<Expr>(ThreadLimit); }
5182
5183   child_range children() { return child_range(&ThreadLimit, &ThreadLimit + 1); }
5184
5185   const_child_range children() const {
5186     return const_child_range(&ThreadLimit, &ThreadLimit + 1);
5187   }
5188
5189   child_range used_children() {
5190     return child_range(child_iterator(), child_iterator());
5191   }
5192   const_child_range used_children() const {
5193     return const_child_range(const_child_iterator(), const_child_iterator());
5194   }
5195
5196   static bool classof(const OMPClause *T) {
5197     return T->getClauseKind() == OMPC_thread_limit;
5198   }
5199 };
5200
5201 /// This represents 'priority' clause in the '#pragma omp ...'
5202 /// directive.
5203 ///
5204 /// \code
5205 /// #pragma omp task priority(n)
5206 /// \endcode
5207 /// In this example directive '#pragma omp teams' has clause 'priority' with
5208 /// single expression 'n'.
5209 class OMPPriorityClause : public OMPClause, public OMPClauseWithPreInit {
5210   friend class OMPClauseReader;
5211
5212   /// Location of '('.
5213   SourceLocation LParenLoc;
5214
5215   /// Priority number.
5216   Stmt *Priority = nullptr;
5217
5218   /// Set the Priority number.
5219   ///
5220   /// \param E Priority number.
5221   void setPriority(Expr *E) { Priority = E; }
5222
5223 public:
5224   /// Build 'priority' clause.
5225   ///
5226   /// \param Priority Expression associated with this clause.
5227   /// \param HelperPriority Helper priority for the construct.
5228   /// \param CaptureRegion Innermost OpenMP region where expressions in this
5229   /// clause must be captured.
5230   /// \param StartLoc Starting location of the clause.
5231   /// \param LParenLoc Location of '('.
5232   /// \param EndLoc Ending location of the clause.
5233   OMPPriorityClause(Expr *Priority, Stmt *HelperPriority,
5234                     OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
5235                     SourceLocation LParenLoc, SourceLocation EndLoc)
5236       : OMPClause(OMPC_priority, StartLoc, EndLoc), OMPClauseWithPreInit(this),
5237         LParenLoc(LParenLoc), Priority(Priority) {
5238     setPreInitStmt(HelperPriority, CaptureRegion);
5239   }
5240
5241   /// Build an empty clause.
5242   OMPPriorityClause()
5243       : OMPClause(OMPC_priority, SourceLocation(), SourceLocation()),
5244         OMPClauseWithPreInit(this) {}
5245
5246   /// Sets the location of '('.
5247   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5248
5249   /// Returns the location of '('.
5250   SourceLocation getLParenLoc() const { return LParenLoc; }
5251
5252   /// Return Priority number.
5253   Expr *getPriority() { return cast<Expr>(Priority); }
5254
5255   /// Return Priority number.
5256   Expr *getPriority() const { return cast<Expr>(Priority); }
5257
5258   child_range children() { return child_range(&Priority, &Priority + 1); }
5259
5260   const_child_range children() const {
5261     return const_child_range(&Priority, &Priority + 1);
5262   }
5263
5264   child_range used_children();
5265   const_child_range used_children() const {
5266     auto Children = const_cast<OMPPriorityClause *>(this)->used_children();
5267     return const_child_range(Children.begin(), Children.end());
5268   }
5269
5270   static bool classof(const OMPClause *T) {
5271     return T->getClauseKind() == OMPC_priority;
5272   }
5273 };
5274
5275 /// This represents 'grainsize' clause in the '#pragma omp ...'
5276 /// directive.
5277 ///
5278 /// \code
5279 /// #pragma omp taskloop grainsize(4)
5280 /// \endcode
5281 /// In this example directive '#pragma omp taskloop' has clause 'grainsize'
5282 /// with single expression '4'.
5283 class OMPGrainsizeClause : public OMPClause, public OMPClauseWithPreInit {
5284   friend class OMPClauseReader;
5285
5286   /// Location of '('.
5287   SourceLocation LParenLoc;
5288
5289   /// Safe iteration space distance.
5290   Stmt *Grainsize = nullptr;
5291
5292   /// Set safelen.
5293   void setGrainsize(Expr *Size) { Grainsize = Size; }
5294
5295 public:
5296   /// Build 'grainsize' clause.
5297   ///
5298   /// \param Size Expression associated with this clause.
5299   /// \param HelperSize Helper grainsize for the construct.
5300   /// \param CaptureRegion Innermost OpenMP region where expressions in this
5301   /// clause must be captured.
5302   /// \param StartLoc Starting location of the clause.
5303   /// \param EndLoc Ending location of the clause.
5304   OMPGrainsizeClause(Expr *Size, Stmt *HelperSize,
5305                      OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
5306                      SourceLocation LParenLoc, SourceLocation EndLoc)
5307       : OMPClause(OMPC_grainsize, StartLoc, EndLoc), OMPClauseWithPreInit(this),
5308         LParenLoc(LParenLoc), Grainsize(Size) {
5309     setPreInitStmt(HelperSize, CaptureRegion);
5310   }
5311
5312   /// Build an empty clause.
5313   explicit OMPGrainsizeClause()
5314       : OMPClause(OMPC_grainsize, SourceLocation(), SourceLocation()),
5315         OMPClauseWithPreInit(this) {}
5316
5317   /// Sets the location of '('.
5318   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5319
5320   /// Returns the location of '('.
5321   SourceLocation getLParenLoc() const { return LParenLoc; }
5322
5323   /// Return safe iteration space distance.
5324   Expr *getGrainsize() const { return cast_or_null<Expr>(Grainsize); }
5325
5326   child_range children() { return child_range(&Grainsize, &Grainsize + 1); }
5327
5328   const_child_range children() const {
5329     return const_child_range(&Grainsize, &Grainsize + 1);
5330   }
5331
5332   child_range used_children();
5333   const_child_range used_children() const {
5334     auto Children = const_cast<OMPGrainsizeClause *>(this)->used_children();
5335     return const_child_range(Children.begin(), Children.end());
5336   }
5337
5338   static bool classof(const OMPClause *T) {
5339     return T->getClauseKind() == OMPC_grainsize;
5340   }
5341 };
5342
5343 /// This represents 'nogroup' clause in the '#pragma omp ...' directive.
5344 ///
5345 /// \code
5346 /// #pragma omp taskloop nogroup
5347 /// \endcode
5348 /// In this example directive '#pragma omp taskloop' has 'nogroup' clause.
5349 class OMPNogroupClause : public OMPClause {
5350 public:
5351   /// Build 'nogroup' clause.
5352   ///
5353   /// \param StartLoc Starting location of the clause.
5354   /// \param EndLoc Ending location of the clause.
5355   OMPNogroupClause(SourceLocation StartLoc, SourceLocation EndLoc)
5356       : OMPClause(OMPC_nogroup, StartLoc, EndLoc) {}
5357
5358   /// Build an empty clause.
5359   OMPNogroupClause()
5360       : OMPClause(OMPC_nogroup, SourceLocation(), SourceLocation()) {}
5361
5362   child_range children() {
5363     return child_range(child_iterator(), child_iterator());
5364   }
5365
5366   const_child_range children() const {
5367     return const_child_range(const_child_iterator(), const_child_iterator());
5368   }
5369
5370   child_range used_children() {
5371     return child_range(child_iterator(), child_iterator());
5372   }
5373   const_child_range used_children() const {
5374     return const_child_range(const_child_iterator(), const_child_iterator());
5375   }
5376
5377   static bool classof(const OMPClause *T) {
5378     return T->getClauseKind() == OMPC_nogroup;
5379   }
5380 };
5381
5382 /// This represents 'num_tasks' clause in the '#pragma omp ...'
5383 /// directive.
5384 ///
5385 /// \code
5386 /// #pragma omp taskloop num_tasks(4)
5387 /// \endcode
5388 /// In this example directive '#pragma omp taskloop' has clause 'num_tasks'
5389 /// with single expression '4'.
5390 class OMPNumTasksClause : public OMPClause, public OMPClauseWithPreInit {
5391   friend class OMPClauseReader;
5392
5393   /// Location of '('.
5394   SourceLocation LParenLoc;
5395
5396   /// Safe iteration space distance.
5397   Stmt *NumTasks = nullptr;
5398
5399   /// Set safelen.
5400   void setNumTasks(Expr *Size) { NumTasks = Size; }
5401
5402 public:
5403   /// Build 'num_tasks' clause.
5404   ///
5405   /// \param Size Expression associated with this clause.
5406   /// \param HelperSize Helper grainsize for the construct.
5407   /// \param CaptureRegion Innermost OpenMP region where expressions in this
5408   /// clause must be captured.
5409   /// \param StartLoc Starting location of the clause.
5410   /// \param EndLoc Ending location of the clause.
5411   OMPNumTasksClause(Expr *Size, Stmt *HelperSize,
5412                     OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
5413                     SourceLocation LParenLoc, SourceLocation EndLoc)
5414       : OMPClause(OMPC_num_tasks, StartLoc, EndLoc), OMPClauseWithPreInit(this),
5415         LParenLoc(LParenLoc), NumTasks(Size) {
5416     setPreInitStmt(HelperSize, CaptureRegion);
5417   }
5418
5419   /// Build an empty clause.
5420   explicit OMPNumTasksClause()
5421       : OMPClause(OMPC_num_tasks, SourceLocation(), SourceLocation()),
5422         OMPClauseWithPreInit(this) {}
5423
5424   /// Sets the location of '('.
5425   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5426
5427   /// Returns the location of '('.
5428   SourceLocation getLParenLoc() const { return LParenLoc; }
5429
5430   /// Return safe iteration space distance.
5431   Expr *getNumTasks() const { return cast_or_null<Expr>(NumTasks); }
5432
5433   child_range children() { return child_range(&NumTasks, &NumTasks + 1); }
5434
5435   const_child_range children() const {
5436     return const_child_range(&NumTasks, &NumTasks + 1);
5437   }
5438
5439   child_range used_children();
5440   const_child_range used_children() const {
5441     auto Children = const_cast<OMPNumTasksClause *>(this)->used_children();
5442     return const_child_range(Children.begin(), Children.end());
5443   }
5444
5445   static bool classof(const OMPClause *T) {
5446     return T->getClauseKind() == OMPC_num_tasks;
5447   }
5448 };
5449
5450 /// This represents 'hint' clause in the '#pragma omp ...' directive.
5451 ///
5452 /// \code
5453 /// #pragma omp critical (name) hint(6)
5454 /// \endcode
5455 /// In this example directive '#pragma omp critical' has name 'name' and clause
5456 /// 'hint' with argument '6'.
5457 class OMPHintClause : public OMPClause {
5458   friend class OMPClauseReader;
5459
5460   /// Location of '('.
5461   SourceLocation LParenLoc;
5462
5463   /// Hint expression of the 'hint' clause.
5464   Stmt *Hint = nullptr;
5465
5466   /// Set hint expression.
5467   void setHint(Expr *H) { Hint = H; }
5468
5469 public:
5470   /// Build 'hint' clause with expression \a Hint.
5471   ///
5472   /// \param Hint Hint expression.
5473   /// \param StartLoc Starting location of the clause.
5474   /// \param LParenLoc Location of '('.
5475   /// \param EndLoc Ending location of the clause.
5476   OMPHintClause(Expr *Hint, SourceLocation StartLoc, SourceLocation LParenLoc,
5477                 SourceLocation EndLoc)
5478       : OMPClause(OMPC_hint, StartLoc, EndLoc), LParenLoc(LParenLoc),
5479         Hint(Hint) {}
5480
5481   /// Build an empty clause.
5482   OMPHintClause() : OMPClause(OMPC_hint, SourceLocation(), SourceLocation()) {}
5483
5484   /// Sets the location of '('.
5485   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5486
5487   /// Returns the location of '('.
5488   SourceLocation getLParenLoc() const { return LParenLoc; }
5489
5490   /// Returns number of threads.
5491   Expr *getHint() const { return cast_or_null<Expr>(Hint); }
5492
5493   child_range children() { return child_range(&Hint, &Hint + 1); }
5494
5495   const_child_range children() const {
5496     return const_child_range(&Hint, &Hint + 1);
5497   }
5498
5499   child_range used_children() {
5500     return child_range(child_iterator(), child_iterator());
5501   }
5502   const_child_range used_children() const {
5503     return const_child_range(const_child_iterator(), const_child_iterator());
5504   }
5505
5506   static bool classof(const OMPClause *T) {
5507     return T->getClauseKind() == OMPC_hint;
5508   }
5509 };
5510
5511 /// This represents 'dist_schedule' clause in the '#pragma omp ...'
5512 /// directive.
5513 ///
5514 /// \code
5515 /// #pragma omp distribute dist_schedule(static, 3)
5516 /// \endcode
5517 /// In this example directive '#pragma omp distribute' has 'dist_schedule'
5518 /// clause with arguments 'static' and '3'.
5519 class OMPDistScheduleClause : public OMPClause, public OMPClauseWithPreInit {
5520   friend class OMPClauseReader;
5521
5522   /// Location of '('.
5523   SourceLocation LParenLoc;
5524
5525   /// A kind of the 'schedule' clause.
5526   OpenMPDistScheduleClauseKind Kind = OMPC_DIST_SCHEDULE_unknown;
5527
5528   /// Start location of the schedule kind in source code.
5529   SourceLocation KindLoc;
5530
5531   /// Location of ',' (if any).
5532   SourceLocation CommaLoc;
5533
5534   /// Chunk size.
5535   Expr *ChunkSize = nullptr;
5536
5537   /// Set schedule kind.
5538   ///
5539   /// \param K Schedule kind.
5540   void setDistScheduleKind(OpenMPDistScheduleClauseKind K) { Kind = K; }
5541
5542   /// Sets the location of '('.
5543   ///
5544   /// \param Loc Location of '('.
5545   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5546
5547   /// Set schedule kind start location.
5548   ///
5549   /// \param KLoc Schedule kind location.
5550   void setDistScheduleKindLoc(SourceLocation KLoc) { KindLoc = KLoc; }
5551
5552   /// Set location of ','.
5553   ///
5554   /// \param Loc Location of ','.
5555   void setCommaLoc(SourceLocation Loc) { CommaLoc = Loc; }
5556
5557   /// Set chunk size.
5558   ///
5559   /// \param E Chunk size.
5560   void setChunkSize(Expr *E) { ChunkSize = E; }
5561
5562 public:
5563   /// Build 'dist_schedule' clause with schedule kind \a Kind and chunk
5564   /// size expression \a ChunkSize.
5565   ///
5566   /// \param StartLoc Starting location of the clause.
5567   /// \param LParenLoc Location of '('.
5568   /// \param KLoc Starting location of the argument.
5569   /// \param CommaLoc Location of ','.
5570   /// \param EndLoc Ending location of the clause.
5571   /// \param Kind DistSchedule kind.
5572   /// \param ChunkSize Chunk size.
5573   /// \param HelperChunkSize Helper chunk size for combined directives.
5574   OMPDistScheduleClause(SourceLocation StartLoc, SourceLocation LParenLoc,
5575                         SourceLocation KLoc, SourceLocation CommaLoc,
5576                         SourceLocation EndLoc,
5577                         OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize,
5578                         Stmt *HelperChunkSize)
5579       : OMPClause(OMPC_dist_schedule, StartLoc, EndLoc),
5580         OMPClauseWithPreInit(this), LParenLoc(LParenLoc), Kind(Kind),
5581         KindLoc(KLoc), CommaLoc(CommaLoc), ChunkSize(ChunkSize) {
5582     setPreInitStmt(HelperChunkSize);
5583   }
5584
5585   /// Build an empty clause.
5586   explicit OMPDistScheduleClause()
5587       : OMPClause(OMPC_dist_schedule, SourceLocation(), SourceLocation()),
5588         OMPClauseWithPreInit(this) {}
5589
5590   /// Get kind of the clause.
5591   OpenMPDistScheduleClauseKind getDistScheduleKind() const { return Kind; }
5592
5593   /// Get location of '('.
5594   SourceLocation getLParenLoc() { return LParenLoc; }
5595
5596   /// Get kind location.
5597   SourceLocation getDistScheduleKindLoc() { return KindLoc; }
5598
5599   /// Get location of ','.
5600   SourceLocation getCommaLoc() { return CommaLoc; }
5601
5602   /// Get chunk size.
5603   Expr *getChunkSize() { return ChunkSize; }
5604
5605   /// Get chunk size.
5606   const Expr *getChunkSize() const { return ChunkSize; }
5607
5608   child_range children() {
5609     return child_range(reinterpret_cast<Stmt **>(&ChunkSize),
5610                        reinterpret_cast<Stmt **>(&ChunkSize) + 1);
5611   }
5612
5613   const_child_range children() const {
5614     auto Children = const_cast<OMPDistScheduleClause *>(this)->children();
5615     return const_child_range(Children.begin(), Children.end());
5616   }
5617
5618   child_range used_children() {
5619     return child_range(child_iterator(), child_iterator());
5620   }
5621   const_child_range used_children() const {
5622     return const_child_range(const_child_iterator(), const_child_iterator());
5623   }
5624
5625   static bool classof(const OMPClause *T) {
5626     return T->getClauseKind() == OMPC_dist_schedule;
5627   }
5628 };
5629
5630 /// This represents 'defaultmap' clause in the '#pragma omp ...' directive.
5631 ///
5632 /// \code
5633 /// #pragma omp target defaultmap(tofrom: scalar)
5634 /// \endcode
5635 /// In this example directive '#pragma omp target' has 'defaultmap' clause of kind
5636 /// 'scalar' with modifier 'tofrom'.
5637 class OMPDefaultmapClause : public OMPClause {
5638   friend class OMPClauseReader;
5639
5640   /// Location of '('.
5641   SourceLocation LParenLoc;
5642
5643   /// Modifiers for 'defaultmap' clause.
5644   OpenMPDefaultmapClauseModifier Modifier = OMPC_DEFAULTMAP_MODIFIER_unknown;
5645
5646   /// Locations of modifiers.
5647   SourceLocation ModifierLoc;
5648
5649   /// A kind of the 'defaultmap' clause.
5650   OpenMPDefaultmapClauseKind Kind = OMPC_DEFAULTMAP_unknown;
5651
5652   /// Start location of the defaultmap kind in source code.
5653   SourceLocation KindLoc;
5654
5655   /// Set defaultmap kind.
5656   ///
5657   /// \param K Defaultmap kind.
5658   void setDefaultmapKind(OpenMPDefaultmapClauseKind K) { Kind = K; }
5659
5660   /// Set the defaultmap modifier.
5661   ///
5662   /// \param M Defaultmap modifier.
5663   void setDefaultmapModifier(OpenMPDefaultmapClauseModifier M) {
5664     Modifier = M;
5665   }
5666
5667   /// Set location of the defaultmap modifier.
5668   void setDefaultmapModifierLoc(SourceLocation Loc) {
5669     ModifierLoc = Loc;
5670   }
5671
5672   /// Sets the location of '('.
5673   ///
5674   /// \param Loc Location of '('.
5675   void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
5676
5677   /// Set defaultmap kind start location.
5678   ///
5679   /// \param KLoc Defaultmap kind location.
5680   void setDefaultmapKindLoc(SourceLocation KLoc) { KindLoc = KLoc; }
5681
5682 public:
5683   /// Build 'defaultmap' clause with defaultmap kind \a Kind
5684   ///
5685   /// \param StartLoc Starting location of the clause.
5686   /// \param LParenLoc Location of '('.
5687   /// \param KLoc Starting location of the argument.
5688   /// \param EndLoc Ending location of the clause.
5689   /// \param Kind Defaultmap kind.
5690   /// \param M The modifier applied to 'defaultmap' clause.
5691   /// \param MLoc Location of the modifier
5692   OMPDefaultmapClause(SourceLocation StartLoc, SourceLocation LParenLoc,
5693                       SourceLocation MLoc, SourceLocation KLoc,
5694                       SourceLocation EndLoc, OpenMPDefaultmapClauseKind Kind,
5695                       OpenMPDefaultmapClauseModifier M)
5696       : OMPClause(OMPC_defaultmap, StartLoc, EndLoc), LParenLoc(LParenLoc),
5697         Modifier(M), ModifierLoc(MLoc), Kind(Kind), KindLoc(KLoc) {}
5698
5699   /// Build an empty clause.
5700   explicit OMPDefaultmapClause()
5701       : OMPClause(OMPC_defaultmap, SourceLocation(), SourceLocation()) {}
5702
5703   /// Get kind of the clause.
5704   OpenMPDefaultmapClauseKind getDefaultmapKind() const { return Kind; }
5705
5706   /// Get the modifier of the clause.
5707   OpenMPDefaultmapClauseModifier getDefaultmapModifier() const {
5708     return Modifier;
5709   }
5710
5711   /// Get location of '('.
5712   SourceLocation getLParenLoc() { return LParenLoc; }
5713
5714   /// Get kind location.
5715   SourceLocation getDefaultmapKindLoc() { return KindLoc; }
5716
5717   /// Get the modifier location.
5718   SourceLocation getDefaultmapModifierLoc() const {
5719     return ModifierLoc;
5720   }
5721
5722   child_range children() {
5723     return child_range(child_iterator(), child_iterator());
5724   }
5725
5726   const_child_range children() const {
5727     return const_child_range(const_child_iterator(), const_child_iterator());
5728   }
5729
5730   child_range used_children() {
5731     return child_range(child_iterator(), child_iterator());
5732   }
5733   const_child_range used_children() const {
5734     return const_child_range(const_child_iterator(), const_child_iterator());
5735   }
5736
5737   static bool classof(const OMPClause *T) {
5738     return T->getClauseKind() == OMPC_defaultmap;
5739   }
5740 };
5741
5742 /// This represents clause 'to' in the '#pragma omp ...'
5743 /// directives.
5744 ///
5745 /// \code
5746 /// #pragma omp target update to(a,b)
5747 /// \endcode
5748 /// In this example directive '#pragma omp target update' has clause 'to'
5749 /// with the variables 'a' and 'b'.
5750 class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
5751                           private llvm::TrailingObjects<
5752                               OMPToClause, Expr *, ValueDecl *, unsigned,
5753                               OMPClauseMappableExprCommon::MappableComponent> {
5754   friend class OMPClauseReader;
5755   friend OMPMappableExprListClause;
5756   friend OMPVarListClause;
5757   friend TrailingObjects;
5758
5759   /// Build clause with number of variables \a NumVars.
5760   ///
5761   /// \param MapperQualifierLoc C++ nested name specifier for the associated
5762   /// user-defined mapper.
5763   /// \param MapperIdInfo The identifier of associated user-defined mapper.
5764   /// \param Locs Locations needed to build a mappable clause. It includes 1)
5765   /// StartLoc: starting location of the clause (the clause keyword); 2)
5766   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
5767   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
5768   /// NumVars: number of expressions listed in this clause; 2)
5769   /// NumUniqueDeclarations: number of unique base declarations in this clause;
5770   /// 3) NumComponentLists: number of component lists in this clause; and 4)
5771   /// NumComponents: total number of expression components in the clause.
5772   explicit OMPToClause(NestedNameSpecifierLoc MapperQualifierLoc,
5773                        DeclarationNameInfo MapperIdInfo,
5774                        const OMPVarListLocTy &Locs,
5775                        const OMPMappableExprListSizeTy &Sizes)
5776       : OMPMappableExprListClause(OMPC_to, Locs, Sizes, &MapperQualifierLoc,
5777                                   &MapperIdInfo) {}
5778
5779   /// Build an empty clause.
5780   ///
5781   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
5782   /// NumVars: number of expressions listed in this clause; 2)
5783   /// NumUniqueDeclarations: number of unique base declarations in this clause;
5784   /// 3) NumComponentLists: number of component lists in this clause; and 4)
5785   /// NumComponents: total number of expression components in the clause.
5786   explicit OMPToClause(const OMPMappableExprListSizeTy &Sizes)
5787       : OMPMappableExprListClause(OMPC_to, OMPVarListLocTy(), Sizes) {}
5788
5789   /// Define the sizes of each trailing object array except the last one. This
5790   /// is required for TrailingObjects to work properly.
5791   size_t numTrailingObjects(OverloadToken<Expr *>) const {
5792     // There are varlist_size() of expressions, and varlist_size() of
5793     // user-defined mappers.
5794     return 2 * varlist_size();
5795   }
5796   size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
5797     return getUniqueDeclarationsNum();
5798   }
5799   size_t numTrailingObjects(OverloadToken<unsigned>) const {
5800     return getUniqueDeclarationsNum() + getTotalComponentListNum();
5801   }
5802
5803 public:
5804   /// Creates clause with a list of variables \a Vars.
5805   ///
5806   /// \param C AST context.
5807   /// \param Locs Locations needed to build a mappable clause. It includes 1)
5808   /// StartLoc: starting location of the clause (the clause keyword); 2)
5809   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
5810   /// \param Vars The original expression used in the clause.
5811   /// \param Declarations Declarations used in the clause.
5812   /// \param ComponentLists Component lists used in the clause.
5813   /// \param UDMapperRefs References to user-defined mappers associated with
5814   /// expressions used in the clause.
5815   /// \param UDMQualifierLoc C++ nested name specifier for the associated
5816   /// user-defined mapper.
5817   /// \param MapperId The identifier of associated user-defined mapper.
5818   static OMPToClause *Create(const ASTContext &C, const OMPVarListLocTy &Locs,
5819                              ArrayRef<Expr *> Vars,
5820                              ArrayRef<ValueDecl *> Declarations,
5821                              MappableExprComponentListsRef ComponentLists,
5822                              ArrayRef<Expr *> UDMapperRefs,
5823                              NestedNameSpecifierLoc UDMQualifierLoc,
5824                              DeclarationNameInfo MapperId);
5825
5826   /// Creates an empty clause with the place for \a NumVars variables.
5827   ///
5828   /// \param C AST context.
5829   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
5830   /// NumVars: number of expressions listed in this clause; 2)
5831   /// NumUniqueDeclarations: number of unique base declarations in this clause;
5832   /// 3) NumComponentLists: number of component lists in this clause; and 4)
5833   /// NumComponents: total number of expression components in the clause.
5834   static OMPToClause *CreateEmpty(const ASTContext &C,
5835                                   const OMPMappableExprListSizeTy &Sizes);
5836
5837   child_range children() {
5838     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
5839                        reinterpret_cast<Stmt **>(varlist_end()));
5840   }
5841
5842   const_child_range children() const {
5843     auto Children = const_cast<OMPToClause *>(this)->children();
5844     return const_child_range(Children.begin(), Children.end());
5845   }
5846
5847   child_range used_children() {
5848     return child_range(child_iterator(), child_iterator());
5849   }
5850   const_child_range used_children() const {
5851     return const_child_range(const_child_iterator(), const_child_iterator());
5852   }
5853
5854   static bool classof(const OMPClause *T) {
5855     return T->getClauseKind() == OMPC_to;
5856   }
5857 };
5858
5859 /// This represents clause 'from' in the '#pragma omp ...'
5860 /// directives.
5861 ///
5862 /// \code
5863 /// #pragma omp target update from(a,b)
5864 /// \endcode
5865 /// In this example directive '#pragma omp target update' has clause 'from'
5866 /// with the variables 'a' and 'b'.
5867 class OMPFromClause final
5868     : public OMPMappableExprListClause<OMPFromClause>,
5869       private llvm::TrailingObjects<
5870           OMPFromClause, Expr *, ValueDecl *, unsigned,
5871           OMPClauseMappableExprCommon::MappableComponent> {
5872   friend class OMPClauseReader;
5873   friend OMPMappableExprListClause;
5874   friend OMPVarListClause;
5875   friend TrailingObjects;
5876
5877   /// Build clause with number of variables \a NumVars.
5878   ///
5879   /// \param MapperQualifierLoc C++ nested name specifier for the associated
5880   /// user-defined mapper.
5881   /// \param MapperIdInfo The identifier of associated user-defined mapper.
5882   /// \param Locs Locations needed to build a mappable clause. It includes 1)
5883   /// StartLoc: starting location of the clause (the clause keyword); 2)
5884   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
5885   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
5886   /// NumVars: number of expressions listed in this clause; 2)
5887   /// NumUniqueDeclarations: number of unique base declarations in this clause;
5888   /// 3) NumComponentLists: number of component lists in this clause; and 4)
5889   /// NumComponents: total number of expression components in the clause.
5890   explicit OMPFromClause(NestedNameSpecifierLoc MapperQualifierLoc,
5891                          DeclarationNameInfo MapperIdInfo,
5892                          const OMPVarListLocTy &Locs,
5893                          const OMPMappableExprListSizeTy &Sizes)
5894       : OMPMappableExprListClause(OMPC_from, Locs, Sizes, &MapperQualifierLoc,
5895                                   &MapperIdInfo) {}
5896
5897   /// Build an empty clause.
5898   ///
5899   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
5900   /// NumVars: number of expressions listed in this clause; 2)
5901   /// NumUniqueDeclarations: number of unique base declarations in this clause;
5902   /// 3) NumComponentLists: number of component lists in this clause; and 4)
5903   /// NumComponents: total number of expression components in the clause.
5904   explicit OMPFromClause(const OMPMappableExprListSizeTy &Sizes)
5905       : OMPMappableExprListClause(OMPC_from, OMPVarListLocTy(), Sizes) {}
5906
5907   /// Define the sizes of each trailing object array except the last one. This
5908   /// is required for TrailingObjects to work properly.
5909   size_t numTrailingObjects(OverloadToken<Expr *>) const {
5910     // There are varlist_size() of expressions, and varlist_size() of
5911     // user-defined mappers.
5912     return 2 * varlist_size();
5913   }
5914   size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
5915     return getUniqueDeclarationsNum();
5916   }
5917   size_t numTrailingObjects(OverloadToken<unsigned>) const {
5918     return getUniqueDeclarationsNum() + getTotalComponentListNum();
5919   }
5920
5921 public:
5922   /// Creates clause with a list of variables \a Vars.
5923   ///
5924   /// \param C AST context.
5925   /// \param Locs Locations needed to build a mappable clause. It includes 1)
5926   /// StartLoc: starting location of the clause (the clause keyword); 2)
5927   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
5928   /// \param Vars The original expression used in the clause.
5929   /// \param Declarations Declarations used in the clause.
5930   /// \param ComponentLists Component lists used in the clause.
5931   /// \param UDMapperRefs References to user-defined mappers associated with
5932   /// expressions used in the clause.
5933   /// \param UDMQualifierLoc C++ nested name specifier for the associated
5934   /// user-defined mapper.
5935   /// \param MapperId The identifier of associated user-defined mapper.
5936   static OMPFromClause *Create(const ASTContext &C, const OMPVarListLocTy &Locs,
5937                                ArrayRef<Expr *> Vars,
5938                                ArrayRef<ValueDecl *> Declarations,
5939                                MappableExprComponentListsRef ComponentLists,
5940                                ArrayRef<Expr *> UDMapperRefs,
5941                                NestedNameSpecifierLoc UDMQualifierLoc,
5942                                DeclarationNameInfo MapperId);
5943
5944   /// Creates an empty clause with the place for \a NumVars variables.
5945   ///
5946   /// \param C AST context.
5947   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
5948   /// NumVars: number of expressions listed in this clause; 2)
5949   /// NumUniqueDeclarations: number of unique base declarations in this clause;
5950   /// 3) NumComponentLists: number of component lists in this clause; and 4)
5951   /// NumComponents: total number of expression components in the clause.
5952   static OMPFromClause *CreateEmpty(const ASTContext &C,
5953                                     const OMPMappableExprListSizeTy &Sizes);
5954
5955   child_range children() {
5956     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
5957                        reinterpret_cast<Stmt **>(varlist_end()));
5958   }
5959
5960   const_child_range children() const {
5961     auto Children = const_cast<OMPFromClause *>(this)->children();
5962     return const_child_range(Children.begin(), Children.end());
5963   }
5964
5965   child_range used_children() {
5966     return child_range(child_iterator(), child_iterator());
5967   }
5968   const_child_range used_children() const {
5969     return const_child_range(const_child_iterator(), const_child_iterator());
5970   }
5971
5972   static bool classof(const OMPClause *T) {
5973     return T->getClauseKind() == OMPC_from;
5974   }
5975 };
5976
5977 /// This represents clause 'use_device_ptr' in the '#pragma omp ...'
5978 /// directives.
5979 ///
5980 /// \code
5981 /// #pragma omp target data use_device_ptr(a,b)
5982 /// \endcode
5983 /// In this example directive '#pragma omp target data' has clause
5984 /// 'use_device_ptr' with the variables 'a' and 'b'.
5985 class OMPUseDevicePtrClause final
5986     : public OMPMappableExprListClause<OMPUseDevicePtrClause>,
5987       private llvm::TrailingObjects<
5988           OMPUseDevicePtrClause, Expr *, ValueDecl *, unsigned,
5989           OMPClauseMappableExprCommon::MappableComponent> {
5990   friend class OMPClauseReader;
5991   friend OMPMappableExprListClause;
5992   friend OMPVarListClause;
5993   friend TrailingObjects;
5994
5995   /// Build clause with number of variables \a NumVars.
5996   ///
5997   /// \param Locs Locations needed to build a mappable clause. It includes 1)
5998   /// StartLoc: starting location of the clause (the clause keyword); 2)
5999   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
6000   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
6001   /// NumVars: number of expressions listed in this clause; 2)
6002   /// NumUniqueDeclarations: number of unique base declarations in this clause;
6003   /// 3) NumComponentLists: number of component lists in this clause; and 4)
6004   /// NumComponents: total number of expression components in the clause.
6005   explicit OMPUseDevicePtrClause(const OMPVarListLocTy &Locs,
6006                                  const OMPMappableExprListSizeTy &Sizes)
6007       : OMPMappableExprListClause(OMPC_use_device_ptr, Locs, Sizes) {}
6008
6009   /// Build an empty clause.
6010   ///
6011   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
6012   /// NumVars: number of expressions listed in this clause; 2)
6013   /// NumUniqueDeclarations: number of unique base declarations in this clause;
6014   /// 3) NumComponentLists: number of component lists in this clause; and 4)
6015   /// NumComponents: total number of expression components in the clause.
6016   explicit OMPUseDevicePtrClause(const OMPMappableExprListSizeTy &Sizes)
6017       : OMPMappableExprListClause(OMPC_use_device_ptr, OMPVarListLocTy(),
6018                                   Sizes) {}
6019
6020   /// Define the sizes of each trailing object array except the last one. This
6021   /// is required for TrailingObjects to work properly.
6022   size_t numTrailingObjects(OverloadToken<Expr *>) const {
6023     return 3 * varlist_size();
6024   }
6025   size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
6026     return getUniqueDeclarationsNum();
6027   }
6028   size_t numTrailingObjects(OverloadToken<unsigned>) const {
6029     return getUniqueDeclarationsNum() + getTotalComponentListNum();
6030   }
6031
6032   /// Sets the list of references to private copies with initializers for new
6033   /// private variables.
6034   /// \param VL List of references.
6035   void setPrivateCopies(ArrayRef<Expr *> VL);
6036
6037   /// Gets the list of references to private copies with initializers for new
6038   /// private variables.
6039   MutableArrayRef<Expr *> getPrivateCopies() {
6040     return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
6041   }
6042   ArrayRef<const Expr *> getPrivateCopies() const {
6043     return llvm::makeArrayRef(varlist_end(), varlist_size());
6044   }
6045
6046   /// Sets the list of references to initializer variables for new private
6047   /// variables.
6048   /// \param VL List of references.
6049   void setInits(ArrayRef<Expr *> VL);
6050
6051   /// Gets the list of references to initializer variables for new private
6052   /// variables.
6053   MutableArrayRef<Expr *> getInits() {
6054     return MutableArrayRef<Expr *>(getPrivateCopies().end(), varlist_size());
6055   }
6056   ArrayRef<const Expr *> getInits() const {
6057     return llvm::makeArrayRef(getPrivateCopies().end(), varlist_size());
6058   }
6059
6060 public:
6061   /// Creates clause with a list of variables \a Vars.
6062   ///
6063   /// \param C AST context.
6064   /// \param Locs Locations needed to build a mappable clause. It includes 1)
6065   /// StartLoc: starting location of the clause (the clause keyword); 2)
6066   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
6067   /// \param Vars The original expression used in the clause.
6068   /// \param PrivateVars Expressions referring to private copies.
6069   /// \param Inits Expressions referring to private copy initializers.
6070   /// \param Declarations Declarations used in the clause.
6071   /// \param ComponentLists Component lists used in the clause.
6072   static OMPUseDevicePtrClause *
6073   Create(const ASTContext &C, const OMPVarListLocTy &Locs,
6074          ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
6075          ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
6076          MappableExprComponentListsRef ComponentLists);
6077
6078   /// Creates an empty clause with the place for \a NumVars variables.
6079   ///
6080   /// \param C AST context.
6081   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
6082   /// NumVars: number of expressions listed in this clause; 2)
6083   /// NumUniqueDeclarations: number of unique base declarations in this clause;
6084   /// 3) NumComponentLists: number of component lists in this clause; and 4)
6085   /// NumComponents: total number of expression components in the clause.
6086   static OMPUseDevicePtrClause *
6087   CreateEmpty(const ASTContext &C, const OMPMappableExprListSizeTy &Sizes);
6088
6089   using private_copies_iterator = MutableArrayRef<Expr *>::iterator;
6090   using private_copies_const_iterator = ArrayRef<const Expr *>::iterator;
6091   using private_copies_range = llvm::iterator_range<private_copies_iterator>;
6092   using private_copies_const_range =
6093       llvm::iterator_range<private_copies_const_iterator>;
6094
6095   private_copies_range private_copies() {
6096     return private_copies_range(getPrivateCopies().begin(),
6097                                 getPrivateCopies().end());
6098   }
6099
6100   private_copies_const_range private_copies() const {
6101     return private_copies_const_range(getPrivateCopies().begin(),
6102                                       getPrivateCopies().end());
6103   }
6104
6105   using inits_iterator = MutableArrayRef<Expr *>::iterator;
6106   using inits_const_iterator = ArrayRef<const Expr *>::iterator;
6107   using inits_range = llvm::iterator_range<inits_iterator>;
6108   using inits_const_range = llvm::iterator_range<inits_const_iterator>;
6109
6110   inits_range inits() {
6111     return inits_range(getInits().begin(), getInits().end());
6112   }
6113
6114   inits_const_range inits() const {
6115     return inits_const_range(getInits().begin(), getInits().end());
6116   }
6117
6118   child_range children() {
6119     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
6120                        reinterpret_cast<Stmt **>(varlist_end()));
6121   }
6122
6123   const_child_range children() const {
6124     auto Children = const_cast<OMPUseDevicePtrClause *>(this)->children();
6125     return const_child_range(Children.begin(), Children.end());
6126   }
6127
6128   child_range used_children() {
6129     return child_range(child_iterator(), child_iterator());
6130   }
6131   const_child_range used_children() const {
6132     return const_child_range(const_child_iterator(), const_child_iterator());
6133   }
6134
6135   static bool classof(const OMPClause *T) {
6136     return T->getClauseKind() == OMPC_use_device_ptr;
6137   }
6138 };
6139
6140 /// This represents clause 'is_device_ptr' in the '#pragma omp ...'
6141 /// directives.
6142 ///
6143 /// \code
6144 /// #pragma omp target is_device_ptr(a,b)
6145 /// \endcode
6146 /// In this example directive '#pragma omp target' has clause
6147 /// 'is_device_ptr' with the variables 'a' and 'b'.
6148 class OMPIsDevicePtrClause final
6149     : public OMPMappableExprListClause<OMPIsDevicePtrClause>,
6150       private llvm::TrailingObjects<
6151           OMPIsDevicePtrClause, Expr *, ValueDecl *, unsigned,
6152           OMPClauseMappableExprCommon::MappableComponent> {
6153   friend class OMPClauseReader;
6154   friend OMPMappableExprListClause;
6155   friend OMPVarListClause;
6156   friend TrailingObjects;
6157
6158   /// Build clause with number of variables \a NumVars.
6159   ///
6160   /// \param Locs Locations needed to build a mappable clause. It includes 1)
6161   /// StartLoc: starting location of the clause (the clause keyword); 2)
6162   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
6163   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
6164   /// NumVars: number of expressions listed in this clause; 2)
6165   /// NumUniqueDeclarations: number of unique base declarations in this clause;
6166   /// 3) NumComponentLists: number of component lists in this clause; and 4)
6167   /// NumComponents: total number of expression components in the clause.
6168   explicit OMPIsDevicePtrClause(const OMPVarListLocTy &Locs,
6169                                 const OMPMappableExprListSizeTy &Sizes)
6170       : OMPMappableExprListClause(OMPC_is_device_ptr, Locs, Sizes) {}
6171
6172   /// Build an empty clause.
6173   ///
6174   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
6175   /// NumVars: number of expressions listed in this clause; 2)
6176   /// NumUniqueDeclarations: number of unique base declarations in this clause;
6177   /// 3) NumComponentLists: number of component lists in this clause; and 4)
6178   /// NumComponents: total number of expression components in the clause.
6179   explicit OMPIsDevicePtrClause(const OMPMappableExprListSizeTy &Sizes)
6180       : OMPMappableExprListClause(OMPC_is_device_ptr, OMPVarListLocTy(),
6181                                   Sizes) {}
6182
6183   /// Define the sizes of each trailing object array except the last one. This
6184   /// is required for TrailingObjects to work properly.
6185   size_t numTrailingObjects(OverloadToken<Expr *>) const {
6186     return varlist_size();
6187   }
6188   size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
6189     return getUniqueDeclarationsNum();
6190   }
6191   size_t numTrailingObjects(OverloadToken<unsigned>) const {
6192     return getUniqueDeclarationsNum() + getTotalComponentListNum();
6193   }
6194
6195 public:
6196   /// Creates clause with a list of variables \a Vars.
6197   ///
6198   /// \param C AST context.
6199   /// \param Locs Locations needed to build a mappable clause. It includes 1)
6200   /// StartLoc: starting location of the clause (the clause keyword); 2)
6201   /// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
6202   /// \param Vars The original expression used in the clause.
6203   /// \param Declarations Declarations used in the clause.
6204   /// \param ComponentLists Component lists used in the clause.
6205   static OMPIsDevicePtrClause *
6206   Create(const ASTContext &C, const OMPVarListLocTy &Locs,
6207          ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
6208          MappableExprComponentListsRef ComponentLists);
6209
6210   /// Creates an empty clause with the place for \a NumVars variables.
6211   ///
6212   /// \param C AST context.
6213   /// \param Sizes All required sizes to build a mappable clause. It includes 1)
6214   /// NumVars: number of expressions listed in this clause; 2)
6215   /// NumUniqueDeclarations: number of unique base declarations in this clause;
6216   /// 3) NumComponentLists: number of component lists in this clause; and 4)
6217   /// NumComponents: total number of expression components in the clause.
6218   static OMPIsDevicePtrClause *
6219   CreateEmpty(const ASTContext &C, const OMPMappableExprListSizeTy &Sizes);
6220
6221   child_range children() {
6222     return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
6223                        reinterpret_cast<Stmt **>(varlist_end()));
6224   }
6225
6226   const_child_range children() const {
6227     auto Children = const_cast<OMPIsDevicePtrClause *>(this)->children();
6228     return const_child_range(Children.begin(), Children.end());
6229   }
6230
6231   child_range used_children() {
6232     return child_range(child_iterator(), child_iterator());
6233   }
6234   const_child_range used_children() const {
6235     return const_child_range(const_child_iterator(), const_child_iterator());
6236   }
6237
6238   static bool classof(const OMPClause *T) {
6239     return T->getClauseKind() == OMPC_is_device_ptr;
6240   }
6241 };
6242
6243 /// This class implements a simple visitor for OMPClause
6244 /// subclasses.
6245 template<class ImplClass, template <typename> class Ptr, typename RetTy>
6246 class OMPClauseVisitorBase {
6247 public:
6248 #define PTR(CLASS) typename Ptr<CLASS>::type
6249 #define DISPATCH(CLASS) \
6250   return static_cast<ImplClass*>(this)->Visit##CLASS(static_cast<PTR(CLASS)>(S))
6251
6252 #define OPENMP_CLAUSE(Name, Class)                              \
6253   RetTy Visit ## Class (PTR(Class) S) { DISPATCH(Class); }
6254 #include "clang/Basic/OpenMPKinds.def"
6255
6256   RetTy Visit(PTR(OMPClause) S) {
6257     // Top switch clause: visit each OMPClause.
6258     switch (S->getClauseKind()) {
6259     default: llvm_unreachable("Unknown clause kind!");
6260 #define OPENMP_CLAUSE(Name, Class)                              \
6261     case OMPC_ ## Name : return Visit ## Class(static_cast<PTR(Class)>(S));
6262 #include "clang/Basic/OpenMPKinds.def"
6263     }
6264   }
6265   // Base case, ignore it. :)
6266   RetTy VisitOMPClause(PTR(OMPClause) Node) { return RetTy(); }
6267 #undef PTR
6268 #undef DISPATCH
6269 };
6270
6271 template <typename T>
6272 using const_ptr = typename std::add_pointer<typename std::add_const<T>::type>;
6273
6274 template<class ImplClass, typename RetTy = void>
6275 class OMPClauseVisitor :
6276       public OMPClauseVisitorBase <ImplClass, std::add_pointer, RetTy> {};
6277 template<class ImplClass, typename RetTy = void>
6278 class ConstOMPClauseVisitor :
6279       public OMPClauseVisitorBase <ImplClass, const_ptr, RetTy> {};
6280
6281 class OMPClausePrinter final : public OMPClauseVisitor<OMPClausePrinter> {
6282   raw_ostream &OS;
6283   const PrintingPolicy &Policy;
6284
6285   /// Process clauses with list of variables.
6286   template <typename T> void VisitOMPClauseList(T *Node, char StartSym);
6287
6288 public:
6289   OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
6290       : OS(OS), Policy(Policy) {}
6291
6292 #define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *S);
6293 #include "clang/Basic/OpenMPKinds.def"
6294 };
6295
6296 } // namespace clang
6297
6298 #endif // LLVM_CLANG_AST_OPENMPCLAUSE_H