1//===- OpenMPClause.h - Classes for OpenMP clauses --------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief 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/Expr.h"
20#include "clang/AST/Stmt.h"
21#include "clang/Basic/OpenMPKinds.h"
22#include "clang/Basic/SourceLocation.h"
23#include "llvm/ADT/MapVector.h"
24
25namespace clang {
26
27//===----------------------------------------------------------------------===//
28// AST classes for clauses.
29//===----------------------------------------------------------------------===//
30
31/// \brief This is a basic class for representing single OpenMP clause.
32///
33class OMPClause {
34  /// \brief Starting location of the clause (the clause keyword).
35  SourceLocation StartLoc;
36  /// \brief Ending location of the clause.
37  SourceLocation EndLoc;
38  /// \brief Kind of the clause.
39  OpenMPClauseKind Kind;
40
41protected:
42  OMPClause(OpenMPClauseKind K, SourceLocation StartLoc, SourceLocation EndLoc)
43      : StartLoc(StartLoc), EndLoc(EndLoc), Kind(K) {}
44
45public:
46  /// \brief Returns the starting location of the clause.
47  SourceLocation getLocStart() const { return StartLoc; }
48  /// \brief Returns the ending location of the clause.
49  SourceLocation getLocEnd() const { return EndLoc; }
50
51  /// \brief Sets the starting location of the clause.
52  void setLocStart(SourceLocation Loc) { StartLoc = Loc; }
53  /// \brief Sets the ending location of the clause.
54  void setLocEnd(SourceLocation Loc) { EndLoc = Loc; }
55
56  /// \brief Returns kind of OpenMP clause (private, shared, reduction, etc.).
57  OpenMPClauseKind getClauseKind() const { return Kind; }
58
59  bool isImplicit() const { return StartLoc.isInvalid(); }
60
61  typedef StmtIterator child_iterator;
62  typedef ConstStmtIterator const_child_iterator;
63  typedef llvm::iterator_range<child_iterator> child_range;
64  typedef llvm::iterator_range<const_child_iterator> const_child_range;
65
66  child_range children();
67  const_child_range children() const {
68    auto Children = const_cast<OMPClause *>(this)->children();
69    return const_child_range(Children.begin(), Children.end());
70  }
71  static bool classof(const OMPClause *) { return true; }
72};
73
74/// Class that handles pre-initialization statement for some clauses, like
75/// 'shedule', 'firstprivate' etc.
76class OMPClauseWithPreInit {
77  friend class OMPClauseReader;
78  /// Pre-initialization statement for the clause.
79  Stmt *PreInit;
80  /// Region that captures the associated stmt.
81  OpenMPDirectiveKind CaptureRegion;
82
83protected:
84  /// Set pre-initialization statement for the clause.
85  void setPreInitStmt(Stmt *S, OpenMPDirectiveKind ThisRegion = OMPD_unknown) {
86    PreInit = S;
87    CaptureRegion = ThisRegion;
88  }
89  OMPClauseWithPreInit(const OMPClause *This)
90      : PreInit(nullptr), CaptureRegion(OMPD_unknown) {
91    assert(get(This) && "get is not tuned for pre-init.");
92  }
93
94public:
95  /// Get pre-initialization statement for the clause.
96  const Stmt *getPreInitStmt() const { return PreInit; }
97  /// Get pre-initialization statement for the clause.
98  Stmt *getPreInitStmt() { return PreInit; }
99  /// Get capture region for the stmt in the clause.
100  OpenMPDirectiveKind getCaptureRegion() { return CaptureRegion; }
101  static OMPClauseWithPreInit *get(OMPClause *C);
102  static const OMPClauseWithPreInit *get(const OMPClause *C);
103};
104
105/// Class that handles post-update expression for some clauses, like
106/// 'lastprivate', 'reduction' etc.
107class OMPClauseWithPostUpdate : public OMPClauseWithPreInit {
108  friend class OMPClauseReader;
109  /// Post-update expression for the clause.
110  Expr *PostUpdate;
111protected:
112  /// Set pre-initialization statement for the clause.
113  void setPostUpdateExpr(Expr *S) { PostUpdate = S; }
114  OMPClauseWithPostUpdate(const OMPClause *This)
115      : OMPClauseWithPreInit(This), PostUpdate(nullptr) {
116    assert(get(This) && "get is not tuned for post-update.");
117  }
118
119public:
120  /// Get post-update expression for the clause.
121  const Expr *getPostUpdateExpr() const { return PostUpdate; }
122  /// Get post-update expression for the clause.
123  Expr *getPostUpdateExpr() { return PostUpdate; }
124  static OMPClauseWithPostUpdate *get(OMPClause *C);
125  static const OMPClauseWithPostUpdate *get(const OMPClause *C);
126};
127
128/// \brief This represents clauses with the list of variables like 'private',
129/// 'firstprivate', 'copyin', 'shared', or 'reduction' clauses in the
130/// '#pragma omp ...' directives.
131template <class T> class OMPVarListClause : public OMPClause {
132  friend class OMPClauseReader;
133  /// \brief Location of '('.
134  SourceLocation LParenLoc;
135  /// \brief Number of variables in the list.
136  unsigned NumVars;
137
138protected:
139  /// \brief Fetches list of variables associated with this clause.
140  MutableArrayRef<Expr *> getVarRefs() {
141    return MutableArrayRef<Expr *>(
142        static_cast<T *>(this)->template getTrailingObjects<Expr *>(), NumVars);
143  }
144
145  /// \brief Sets the list of variables for this clause.
146  void setVarRefs(ArrayRef<Expr *> VL) {
147    assert(VL.size() == NumVars &&
148           "Number of variables is not the same as the preallocated buffer");
149    std::copy(VL.begin(), VL.end(),
150              static_cast<T *>(this)->template getTrailingObjects<Expr *>());
151  }
152
153  /// \brief Build a clause with \a N variables
154  ///
155  /// \param K Kind of the clause.
156  /// \param StartLoc Starting location of the clause (the clause keyword).
157  /// \param LParenLoc Location of '('.
158  /// \param EndLoc Ending location of the clause.
159  /// \param N Number of the variables in the clause.
160  ///
161  OMPVarListClause(OpenMPClauseKind K, SourceLocation StartLoc,
162                   SourceLocation LParenLoc, SourceLocation EndLoc, unsigned N)
163      : OMPClause(K, StartLoc, EndLoc), LParenLoc(LParenLoc), NumVars(N) {}
164
165public:
166  typedef MutableArrayRef<Expr *>::iterator varlist_iterator;
167  typedef ArrayRef<const Expr *>::iterator varlist_const_iterator;
168  typedef llvm::iterator_range<varlist_iterator> varlist_range;
169  typedef llvm::iterator_range<varlist_const_iterator> varlist_const_range;
170
171  unsigned varlist_size() const { return NumVars; }
172  bool varlist_empty() const { return NumVars == 0; }
173
174  varlist_range varlists() {
175    return varlist_range(varlist_begin(), varlist_end());
176  }
177  varlist_const_range varlists() const {
178    return varlist_const_range(varlist_begin(), varlist_end());
179  }
180
181  varlist_iterator varlist_begin() { return getVarRefs().begin(); }
182  varlist_iterator varlist_end() { return getVarRefs().end(); }
183  varlist_const_iterator varlist_begin() const { return getVarRefs().begin(); }
184  varlist_const_iterator varlist_end() const { return getVarRefs().end(); }
185
186  /// \brief Sets the location of '('.
187  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
188  /// \brief Returns the location of '('.
189  SourceLocation getLParenLoc() const { return LParenLoc; }
190
191  /// \brief Fetches list of all variables in the clause.
192  ArrayRef<const Expr *> getVarRefs() const {
193    return llvm::makeArrayRef(
194        static_cast<const T *>(this)->template getTrailingObjects<Expr *>(),
195        NumVars);
196  }
197};
198
199/// \brief This represents 'if' clause in the '#pragma omp ...' directive.
200///
201/// \code
202/// #pragma omp parallel if(parallel:a > 5)
203/// \endcode
204/// In this example directive '#pragma omp parallel' has simple 'if' clause with
205/// condition 'a > 5' and directive name modifier 'parallel'.
206///
207class OMPIfClause : public OMPClause, public OMPClauseWithPreInit {
208  friend class OMPClauseReader;
209  /// \brief Location of '('.
210  SourceLocation LParenLoc;
211  /// \brief Condition of the 'if' clause.
212  Stmt *Condition;
213  /// \brief Location of ':' (if any).
214  SourceLocation ColonLoc;
215  /// \brief Directive name modifier for the clause.
216  OpenMPDirectiveKind NameModifier;
217  /// \brief Name modifier location.
218  SourceLocation NameModifierLoc;
219
220  /// \brief Set condition.
221  ///
222  void setCondition(Expr *Cond) { Condition = Cond; }
223  /// \brief Set directive name modifier for the clause.
224  ///
225  void setNameModifier(OpenMPDirectiveKind NM) { NameModifier = NM; }
226  /// \brief Set location of directive name modifier for the clause.
227  ///
228  void setNameModifierLoc(SourceLocation Loc) { NameModifierLoc = Loc; }
229  /// \brief Set location of ':'.
230  ///
231  void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
232
233public:
234  /// \brief Build 'if' clause with condition \a Cond.
235  ///
236  /// \param NameModifier [OpenMP 4.1] Directive name modifier of clause.
237  /// \param Cond Condition of the clause.
238  /// \param HelperCond Helper condition for the clause.
239  /// \param CaptureRegion Innermost OpenMP region where expressions in this
240  /// clause must be captured.
241  /// \param StartLoc Starting location of the clause.
242  /// \param LParenLoc Location of '('.
243  /// \param NameModifierLoc Location of directive name modifier.
244  /// \param ColonLoc [OpenMP 4.1] Location of ':'.
245  /// \param EndLoc Ending location of the clause.
246  ///
247  OMPIfClause(OpenMPDirectiveKind NameModifier, Expr *Cond, Stmt *HelperCond,
248              OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
249              SourceLocation LParenLoc, SourceLocation NameModifierLoc,
250              SourceLocation ColonLoc, SourceLocation EndLoc)
251      : OMPClause(OMPC_if, StartLoc, EndLoc), OMPClauseWithPreInit(this),
252        LParenLoc(LParenLoc), Condition(Cond), ColonLoc(ColonLoc),
253        NameModifier(NameModifier), NameModifierLoc(NameModifierLoc) {
254    setPreInitStmt(HelperCond, CaptureRegion);
255  }
256
257  /// \brief Build an empty clause.
258  ///
259  OMPIfClause()
260      : OMPClause(OMPC_if, SourceLocation(), SourceLocation()),
261        OMPClauseWithPreInit(this), LParenLoc(), Condition(nullptr), ColonLoc(),
262        NameModifier(OMPD_unknown), NameModifierLoc() {}
263
264  /// \brief Sets the location of '('.
265  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
266  /// \brief Returns the location of '('.
267  SourceLocation getLParenLoc() const { return LParenLoc; }
268
269  /// \brief Return the location of ':'.
270  SourceLocation getColonLoc() const { return ColonLoc; }
271
272  /// \brief Returns condition.
273  Expr *getCondition() const { return cast_or_null<Expr>(Condition); }
274  /// \brief Return directive name modifier associated with the clause.
275  OpenMPDirectiveKind getNameModifier() const { return NameModifier; }
276
277  /// \brief Return the location of directive name modifier.
278  SourceLocation getNameModifierLoc() const { return NameModifierLoc; }
279
280  static bool classof(const OMPClause *T) {
281    return T->getClauseKind() == OMPC_if;
282  }
283
284  child_range children() { return child_range(&Condition, &Condition + 1); }
285};
286
287/// \brief This represents 'final' clause in the '#pragma omp ...' directive.
288///
289/// \code
290/// #pragma omp task final(a > 5)
291/// \endcode
292/// In this example directive '#pragma omp task' has simple 'final'
293/// clause with condition 'a > 5'.
294///
295class OMPFinalClause : public OMPClause {
296  friend class OMPClauseReader;
297  /// \brief Location of '('.
298  SourceLocation LParenLoc;
299  /// \brief Condition of the 'if' clause.
300  Stmt *Condition;
301
302  /// \brief Set condition.
303  ///
304  void setCondition(Expr *Cond) { Condition = Cond; }
305
306public:
307  /// \brief Build 'final' clause with condition \a Cond.
308  ///
309  /// \param StartLoc Starting location of the clause.
310  /// \param LParenLoc Location of '('.
311  /// \param Cond Condition of the clause.
312  /// \param EndLoc Ending location of the clause.
313  ///
314  OMPFinalClause(Expr *Cond, SourceLocation StartLoc, SourceLocation LParenLoc,
315                 SourceLocation EndLoc)
316      : OMPClause(OMPC_final, StartLoc, EndLoc), LParenLoc(LParenLoc),
317        Condition(Cond) {}
318
319  /// \brief Build an empty clause.
320  ///
321  OMPFinalClause()
322      : OMPClause(OMPC_final, SourceLocation(), SourceLocation()),
323        LParenLoc(SourceLocation()), Condition(nullptr) {}
324
325  /// \brief Sets the location of '('.
326  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
327  /// \brief Returns the location of '('.
328  SourceLocation getLParenLoc() const { return LParenLoc; }
329
330  /// \brief Returns condition.
331  Expr *getCondition() const { return cast_or_null<Expr>(Condition); }
332
333  static bool classof(const OMPClause *T) {
334    return T->getClauseKind() == OMPC_final;
335  }
336
337  child_range children() { return child_range(&Condition, &Condition + 1); }
338};
339
340/// \brief This represents 'num_threads' clause in the '#pragma omp ...'
341/// directive.
342///
343/// \code
344/// #pragma omp parallel num_threads(6)
345/// \endcode
346/// In this example directive '#pragma omp parallel' has simple 'num_threads'
347/// clause with number of threads '6'.
348///
349class OMPNumThreadsClause : public OMPClause, public OMPClauseWithPreInit {
350  friend class OMPClauseReader;
351  /// \brief Location of '('.
352  SourceLocation LParenLoc;
353  /// \brief Condition of the 'num_threads' clause.
354  Stmt *NumThreads;
355
356  /// \brief Set condition.
357  ///
358  void setNumThreads(Expr *NThreads) { NumThreads = NThreads; }
359
360public:
361  /// \brief Build 'num_threads' clause with condition \a NumThreads.
362  ///
363  /// \param NumThreads Number of threads for the construct.
364  /// \param HelperNumThreads Helper Number of threads for the construct.
365  /// \param CaptureRegion Innermost OpenMP region where expressions in this
366  /// clause must be captured.
367  /// \param StartLoc Starting location of the clause.
368  /// \param LParenLoc Location of '('.
369  /// \param EndLoc Ending location of the clause.
370  ///
371  OMPNumThreadsClause(Expr *NumThreads, Stmt *HelperNumThreads,
372                      OpenMPDirectiveKind CaptureRegion,
373                      SourceLocation StartLoc, SourceLocation LParenLoc,
374                      SourceLocation EndLoc)
375      : OMPClause(OMPC_num_threads, StartLoc, EndLoc),
376        OMPClauseWithPreInit(this), LParenLoc(LParenLoc),
377        NumThreads(NumThreads) {
378    setPreInitStmt(HelperNumThreads, CaptureRegion);
379  }
380
381  /// \brief Build an empty clause.
382  ///
383  OMPNumThreadsClause()
384      : OMPClause(OMPC_num_threads, SourceLocation(), SourceLocation()),
385        OMPClauseWithPreInit(this), LParenLoc(SourceLocation()),
386        NumThreads(nullptr) {}
387
388  /// \brief Sets the location of '('.
389  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
390  /// \brief Returns the location of '('.
391  SourceLocation getLParenLoc() const { return LParenLoc; }
392
393  /// \brief Returns number of threads.
394  Expr *getNumThreads() const { return cast_or_null<Expr>(NumThreads); }
395
396  static bool classof(const OMPClause *T) {
397    return T->getClauseKind() == OMPC_num_threads;
398  }
399
400  child_range children() { return child_range(&NumThreads, &NumThreads + 1); }
401};
402
403/// \brief This represents 'safelen' clause in the '#pragma omp ...'
404/// directive.
405///
406/// \code
407/// #pragma omp simd safelen(4)
408/// \endcode
409/// In this example directive '#pragma omp simd' has clause 'safelen'
410/// with single expression '4'.
411/// If the safelen clause is used then no two iterations executed
412/// concurrently with SIMD instructions can have a greater distance
413/// in the logical iteration space than its value. The parameter of
414/// the safelen clause must be a constant positive integer expression.
415///
416class OMPSafelenClause : public OMPClause {
417  friend class OMPClauseReader;
418  /// \brief Location of '('.
419  SourceLocation LParenLoc;
420  /// \brief Safe iteration space distance.
421  Stmt *Safelen;
422
423  /// \brief Set safelen.
424  void setSafelen(Expr *Len) { Safelen = Len; }
425
426public:
427  /// \brief Build 'safelen' clause.
428  ///
429  /// \param Len Expression associated with this clause.
430  /// \param StartLoc Starting location of the clause.
431  /// \param EndLoc Ending location of the clause.
432  ///
433  OMPSafelenClause(Expr *Len, SourceLocation StartLoc, SourceLocation LParenLoc,
434                   SourceLocation EndLoc)
435      : OMPClause(OMPC_safelen, StartLoc, EndLoc), LParenLoc(LParenLoc),
436        Safelen(Len) {}
437
438  /// \brief Build an empty clause.
439  ///
440  explicit OMPSafelenClause()
441      : OMPClause(OMPC_safelen, SourceLocation(), SourceLocation()),
442        LParenLoc(SourceLocation()), Safelen(nullptr) {}
443
444  /// \brief Sets the location of '('.
445  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
446  /// \brief Returns the location of '('.
447  SourceLocation getLParenLoc() const { return LParenLoc; }
448
449  /// \brief Return safe iteration space distance.
450  Expr *getSafelen() const { return cast_or_null<Expr>(Safelen); }
451
452  static bool classof(const OMPClause *T) {
453    return T->getClauseKind() == OMPC_safelen;
454  }
455
456  child_range children() { return child_range(&Safelen, &Safelen + 1); }
457};
458
459/// \brief This represents 'simdlen' clause in the '#pragma omp ...'
460/// directive.
461///
462/// \code
463/// #pragma omp simd simdlen(4)
464/// \endcode
465/// In this example directive '#pragma omp simd' has clause 'simdlen'
466/// with single expression '4'.
467/// If the 'simdlen' clause is used then it specifies the preferred number of
468/// iterations to be executed concurrently. The parameter of the 'simdlen'
469/// clause must be a constant positive integer expression.
470///
471class OMPSimdlenClause : public OMPClause {
472  friend class OMPClauseReader;
473  /// \brief Location of '('.
474  SourceLocation LParenLoc;
475  /// \brief Safe iteration space distance.
476  Stmt *Simdlen;
477
478  /// \brief Set simdlen.
479  void setSimdlen(Expr *Len) { Simdlen = Len; }
480
481public:
482  /// \brief Build 'simdlen' clause.
483  ///
484  /// \param Len Expression associated with this clause.
485  /// \param StartLoc Starting location of the clause.
486  /// \param EndLoc Ending location of the clause.
487  ///
488  OMPSimdlenClause(Expr *Len, SourceLocation StartLoc, SourceLocation LParenLoc,
489                   SourceLocation EndLoc)
490      : OMPClause(OMPC_simdlen, StartLoc, EndLoc), LParenLoc(LParenLoc),
491        Simdlen(Len) {}
492
493  /// \brief Build an empty clause.
494  ///
495  explicit OMPSimdlenClause()
496      : OMPClause(OMPC_simdlen, SourceLocation(), SourceLocation()),
497        LParenLoc(SourceLocation()), Simdlen(nullptr) {}
498
499  /// \brief Sets the location of '('.
500  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
501  /// \brief Returns the location of '('.
502  SourceLocation getLParenLoc() const { return LParenLoc; }
503
504  /// \brief Return safe iteration space distance.
505  Expr *getSimdlen() const { return cast_or_null<Expr>(Simdlen); }
506
507  static bool classof(const OMPClause *T) {
508    return T->getClauseKind() == OMPC_simdlen;
509  }
510
511  child_range children() { return child_range(&Simdlen, &Simdlen + 1); }
512};
513
514/// \brief This represents 'collapse' clause in the '#pragma omp ...'
515/// directive.
516///
517/// \code
518/// #pragma omp simd collapse(3)
519/// \endcode
520/// In this example directive '#pragma omp simd' has clause 'collapse'
521/// with single expression '3'.
522/// The parameter must be a constant positive integer expression, it specifies
523/// the number of nested loops that should be collapsed into a single iteration
524/// space.
525///
526class OMPCollapseClause : public OMPClause {
527  friend class OMPClauseReader;
528  /// \brief Location of '('.
529  SourceLocation LParenLoc;
530  /// \brief Number of for-loops.
531  Stmt *NumForLoops;
532
533  /// \brief Set the number of associated for-loops.
534  void setNumForLoops(Expr *Num) { NumForLoops = Num; }
535
536public:
537  /// \brief Build 'collapse' clause.
538  ///
539  /// \param Num Expression associated with this clause.
540  /// \param StartLoc Starting location of the clause.
541  /// \param LParenLoc Location of '('.
542  /// \param EndLoc Ending location of the clause.
543  ///
544  OMPCollapseClause(Expr *Num, SourceLocation StartLoc,
545                    SourceLocation LParenLoc, SourceLocation EndLoc)
546      : OMPClause(OMPC_collapse, StartLoc, EndLoc), LParenLoc(LParenLoc),
547        NumForLoops(Num) {}
548
549  /// \brief Build an empty clause.
550  ///
551  explicit OMPCollapseClause()
552      : OMPClause(OMPC_collapse, SourceLocation(), SourceLocation()),
553        LParenLoc(SourceLocation()), NumForLoops(nullptr) {}
554
555  /// \brief Sets the location of '('.
556  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
557  /// \brief Returns the location of '('.
558  SourceLocation getLParenLoc() const { return LParenLoc; }
559
560  /// \brief Return the number of associated for-loops.
561  Expr *getNumForLoops() const { return cast_or_null<Expr>(NumForLoops); }
562
563  static bool classof(const OMPClause *T) {
564    return T->getClauseKind() == OMPC_collapse;
565  }
566
567  child_range children() { return child_range(&NumForLoops, &NumForLoops + 1); }
568};
569
570/// \brief This represents 'default' clause in the '#pragma omp ...' directive.
571///
572/// \code
573/// #pragma omp parallel default(shared)
574/// \endcode
575/// In this example directive '#pragma omp parallel' has simple 'default'
576/// clause with kind 'shared'.
577///
578class OMPDefaultClause : public OMPClause {
579  friend class OMPClauseReader;
580  /// \brief Location of '('.
581  SourceLocation LParenLoc;
582  /// \brief A kind of the 'default' clause.
583  OpenMPDefaultClauseKind Kind;
584  /// \brief Start location of the kind in source code.
585  SourceLocation KindKwLoc;
586
587  /// \brief Set kind of the clauses.
588  ///
589  /// \param K Argument of clause.
590  ///
591  void setDefaultKind(OpenMPDefaultClauseKind K) { Kind = K; }
592
593  /// \brief Set argument location.
594  ///
595  /// \param KLoc Argument location.
596  ///
597  void setDefaultKindKwLoc(SourceLocation KLoc) { KindKwLoc = KLoc; }
598
599public:
600  /// \brief Build 'default' clause with argument \a A ('none' or 'shared').
601  ///
602  /// \param A Argument of the clause ('none' or 'shared').
603  /// \param ALoc Starting location of the argument.
604  /// \param StartLoc Starting location of the clause.
605  /// \param LParenLoc Location of '('.
606  /// \param EndLoc Ending location of the clause.
607  ///
608  OMPDefaultClause(OpenMPDefaultClauseKind A, SourceLocation ALoc,
609                   SourceLocation StartLoc, SourceLocation LParenLoc,
610                   SourceLocation EndLoc)
611      : OMPClause(OMPC_default, StartLoc, EndLoc), LParenLoc(LParenLoc),
612        Kind(A), KindKwLoc(ALoc) {}
613
614  /// \brief Build an empty clause.
615  ///
616  OMPDefaultClause()
617      : OMPClause(OMPC_default, SourceLocation(), SourceLocation()),
618        LParenLoc(SourceLocation()), Kind(OMPC_DEFAULT_unknown),
619        KindKwLoc(SourceLocation()) {}
620
621  /// \brief Sets the location of '('.
622  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
623  /// \brief Returns the location of '('.
624  SourceLocation getLParenLoc() const { return LParenLoc; }
625
626  /// \brief Returns kind of the clause.
627  OpenMPDefaultClauseKind getDefaultKind() const { return Kind; }
628
629  /// \brief Returns location of clause kind.
630  SourceLocation getDefaultKindKwLoc() const { return KindKwLoc; }
631
632  static bool classof(const OMPClause *T) {
633    return T->getClauseKind() == OMPC_default;
634  }
635
636  child_range children() {
637    return child_range(child_iterator(), child_iterator());
638  }
639};
640
641/// \brief This represents 'proc_bind' clause in the '#pragma omp ...'
642/// directive.
643///
644/// \code
645/// #pragma omp parallel proc_bind(master)
646/// \endcode
647/// In this example directive '#pragma omp parallel' has simple 'proc_bind'
648/// clause with kind 'master'.
649///
650class OMPProcBindClause : public OMPClause {
651  friend class OMPClauseReader;
652  /// \brief Location of '('.
653  SourceLocation LParenLoc;
654  /// \brief A kind of the 'proc_bind' clause.
655  OpenMPProcBindClauseKind Kind;
656  /// \brief Start location of the kind in source code.
657  SourceLocation KindKwLoc;
658
659  /// \brief Set kind of the clause.
660  ///
661  /// \param K Kind of clause.
662  ///
663  void setProcBindKind(OpenMPProcBindClauseKind K) { Kind = K; }
664
665  /// \brief Set clause kind location.
666  ///
667  /// \param KLoc Kind location.
668  ///
669  void setProcBindKindKwLoc(SourceLocation KLoc) { KindKwLoc = KLoc; }
670
671public:
672  /// \brief Build 'proc_bind' clause with argument \a A ('master', 'close' or
673  ///        'spread').
674  ///
675  /// \param A Argument of the clause ('master', 'close' or 'spread').
676  /// \param ALoc Starting location of the argument.
677  /// \param StartLoc Starting location of the clause.
678  /// \param LParenLoc Location of '('.
679  /// \param EndLoc Ending location of the clause.
680  ///
681  OMPProcBindClause(OpenMPProcBindClauseKind A, SourceLocation ALoc,
682                    SourceLocation StartLoc, SourceLocation LParenLoc,
683                    SourceLocation EndLoc)
684      : OMPClause(OMPC_proc_bind, StartLoc, EndLoc), LParenLoc(LParenLoc),
685        Kind(A), KindKwLoc(ALoc) {}
686
687  /// \brief Build an empty clause.
688  ///
689  OMPProcBindClause()
690      : OMPClause(OMPC_proc_bind, SourceLocation(), SourceLocation()),
691        LParenLoc(SourceLocation()), Kind(OMPC_PROC_BIND_unknown),
692        KindKwLoc(SourceLocation()) {}
693
694  /// \brief Sets the location of '('.
695  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
696  /// \brief Returns the location of '('.
697  SourceLocation getLParenLoc() const { return LParenLoc; }
698
699  /// \brief Returns kind of the clause.
700  OpenMPProcBindClauseKind getProcBindKind() const { return Kind; }
701
702  /// \brief Returns location of clause kind.
703  SourceLocation getProcBindKindKwLoc() const { return KindKwLoc; }
704
705  static bool classof(const OMPClause *T) {
706    return T->getClauseKind() == OMPC_proc_bind;
707  }
708
709  child_range children() {
710    return child_range(child_iterator(), child_iterator());
711  }
712};
713
714/// \brief This represents 'schedule' clause in the '#pragma omp ...' directive.
715///
716/// \code
717/// #pragma omp for schedule(static, 3)
718/// \endcode
719/// In this example directive '#pragma omp for' has 'schedule' clause with
720/// arguments 'static' and '3'.
721///
722class OMPScheduleClause : public OMPClause, public OMPClauseWithPreInit {
723  friend class OMPClauseReader;
724  /// \brief Location of '('.
725  SourceLocation LParenLoc;
726  /// \brief A kind of the 'schedule' clause.
727  OpenMPScheduleClauseKind Kind;
728  /// \brief Modifiers for 'schedule' clause.
729  enum {FIRST, SECOND, NUM_MODIFIERS};
730  OpenMPScheduleClauseModifier Modifiers[NUM_MODIFIERS];
731  /// \brief Locations of modifiers.
732  SourceLocation ModifiersLoc[NUM_MODIFIERS];
733  /// \brief Start location of the schedule ind in source code.
734  SourceLocation KindLoc;
735  /// \brief Location of ',' (if any).
736  SourceLocation CommaLoc;
737  /// \brief Chunk size.
738  Expr *ChunkSize;
739
740  /// \brief Set schedule kind.
741  ///
742  /// \param K Schedule kind.
743  ///
744  void setScheduleKind(OpenMPScheduleClauseKind K) { Kind = K; }
745  /// \brief Set the first schedule modifier.
746  ///
747  /// \param M Schedule modifier.
748  ///
749  void setFirstScheduleModifier(OpenMPScheduleClauseModifier M) {
750    Modifiers[FIRST] = M;
751  }
752  /// \brief Set the second schedule modifier.
753  ///
754  /// \param M Schedule modifier.
755  ///
756  void setSecondScheduleModifier(OpenMPScheduleClauseModifier M) {
757    Modifiers[SECOND] = M;
758  }
759  /// \brief Set location of the first schedule modifier.
760  ///
761  void setFirstScheduleModifierLoc(SourceLocation Loc) {
762    ModifiersLoc[FIRST] = Loc;
763  }
764  /// \brief Set location of the second schedule modifier.
765  ///
766  void setSecondScheduleModifierLoc(SourceLocation Loc) {
767    ModifiersLoc[SECOND] = Loc;
768  }
769  /// \brief Set schedule modifier location.
770  ///
771  /// \param M Schedule modifier location.
772  ///
773  void setScheduleModifer(OpenMPScheduleClauseModifier M) {
774    if (Modifiers[FIRST] == OMPC_SCHEDULE_MODIFIER_unknown)
775      Modifiers[FIRST] = M;
776    else {
777      assert(Modifiers[SECOND] == OMPC_SCHEDULE_MODIFIER_unknown);
778      Modifiers[SECOND] = M;
779    }
780  }
781  /// \brief Sets the location of '('.
782  ///
783  /// \param Loc Location of '('.
784  ///
785  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
786  /// \brief Set schedule kind start location.
787  ///
788  /// \param KLoc Schedule kind location.
789  ///
790  void setScheduleKindLoc(SourceLocation KLoc) { KindLoc = KLoc; }
791  /// \brief Set location of ','.
792  ///
793  /// \param Loc Location of ','.
794  ///
795  void setCommaLoc(SourceLocation Loc) { CommaLoc = Loc; }
796  /// \brief Set chunk size.
797  ///
798  /// \param E Chunk size.
799  ///
800  void setChunkSize(Expr *E) { ChunkSize = E; }
801
802public:
803  /// \brief Build 'schedule' clause with schedule kind \a Kind and chunk size
804  /// expression \a ChunkSize.
805  ///
806  /// \param StartLoc Starting location of the clause.
807  /// \param LParenLoc Location of '('.
808  /// \param KLoc Starting location of the argument.
809  /// \param CommaLoc Location of ','.
810  /// \param EndLoc Ending location of the clause.
811  /// \param Kind Schedule kind.
812  /// \param ChunkSize Chunk size.
813  /// \param HelperChunkSize Helper chunk size for combined directives.
814  /// \param M1 The first modifier applied to 'schedule' clause.
815  /// \param M1Loc Location of the first modifier
816  /// \param M2 The second modifier applied to 'schedule' clause.
817  /// \param M2Loc Location of the second modifier
818  ///
819  OMPScheduleClause(SourceLocation StartLoc, SourceLocation LParenLoc,
820                    SourceLocation KLoc, SourceLocation CommaLoc,
821                    SourceLocation EndLoc, OpenMPScheduleClauseKind Kind,
822                    Expr *ChunkSize, Stmt *HelperChunkSize,
823                    OpenMPScheduleClauseModifier M1, SourceLocation M1Loc,
824                    OpenMPScheduleClauseModifier M2, SourceLocation M2Loc)
825      : OMPClause(OMPC_schedule, StartLoc, EndLoc), OMPClauseWithPreInit(this),
826        LParenLoc(LParenLoc), Kind(Kind), KindLoc(KLoc), CommaLoc(CommaLoc),
827        ChunkSize(ChunkSize) {
828    setPreInitStmt(HelperChunkSize);
829    Modifiers[FIRST] = M1;
830    Modifiers[SECOND] = M2;
831    ModifiersLoc[FIRST] = M1Loc;
832    ModifiersLoc[SECOND] = M2Loc;
833  }
834
835  /// \brief Build an empty clause.
836  ///
837  explicit OMPScheduleClause()
838      : OMPClause(OMPC_schedule, SourceLocation(), SourceLocation()),
839        OMPClauseWithPreInit(this), Kind(OMPC_SCHEDULE_unknown),
840        ChunkSize(nullptr) {
841    Modifiers[FIRST] = OMPC_SCHEDULE_MODIFIER_unknown;
842    Modifiers[SECOND] = OMPC_SCHEDULE_MODIFIER_unknown;
843  }
844
845  /// \brief Get kind of the clause.
846  ///
847  OpenMPScheduleClauseKind getScheduleKind() const { return Kind; }
848  /// \brief Get the first modifier of the clause.
849  ///
850  OpenMPScheduleClauseModifier getFirstScheduleModifier() const {
851    return Modifiers[FIRST];
852  }
853  /// \brief Get the second modifier of the clause.
854  ///
855  OpenMPScheduleClauseModifier getSecondScheduleModifier() const {
856    return Modifiers[SECOND];
857  }
858  /// \brief Get location of '('.
859  ///
860  SourceLocation getLParenLoc() { return LParenLoc; }
861  /// \brief Get kind location.
862  ///
863  SourceLocation getScheduleKindLoc() { return KindLoc; }
864  /// \brief Get the first modifier location.
865  ///
866  SourceLocation getFirstScheduleModifierLoc() const {
867    return ModifiersLoc[FIRST];
868  }
869  /// \brief Get the second modifier location.
870  ///
871  SourceLocation getSecondScheduleModifierLoc() const {
872    return ModifiersLoc[SECOND];
873  }
874  /// \brief Get location of ','.
875  ///
876  SourceLocation getCommaLoc() { return CommaLoc; }
877  /// \brief Get chunk size.
878  ///
879  Expr *getChunkSize() { return ChunkSize; }
880  /// \brief Get chunk size.
881  ///
882  const Expr *getChunkSize() const { return ChunkSize; }
883
884  static bool classof(const OMPClause *T) {
885    return T->getClauseKind() == OMPC_schedule;
886  }
887
888  child_range children() {
889    return child_range(reinterpret_cast<Stmt **>(&ChunkSize),
890                       reinterpret_cast<Stmt **>(&ChunkSize) + 1);
891  }
892};
893
894/// \brief This represents 'ordered' clause in the '#pragma omp ...' directive.
895///
896/// \code
897/// #pragma omp for ordered (2)
898/// \endcode
899/// In this example directive '#pragma omp for' has 'ordered' clause with
900/// parameter 2.
901///
902class OMPOrderedClause : public OMPClause {
903  friend class OMPClauseReader;
904  /// \brief Location of '('.
905  SourceLocation LParenLoc;
906  /// \brief Number of for-loops.
907  Stmt *NumForLoops;
908
909  /// \brief Set the number of associated for-loops.
910  void setNumForLoops(Expr *Num) { NumForLoops = Num; }
911
912public:
913  /// \brief Build 'ordered' clause.
914  ///
915  /// \param Num Expression, possibly associated with this clause.
916  /// \param StartLoc Starting location of the clause.
917  /// \param LParenLoc Location of '('.
918  /// \param EndLoc Ending location of the clause.
919  ///
920  OMPOrderedClause(Expr *Num, SourceLocation StartLoc,
921                    SourceLocation LParenLoc, SourceLocation EndLoc)
922      : OMPClause(OMPC_ordered, StartLoc, EndLoc), LParenLoc(LParenLoc),
923        NumForLoops(Num) {}
924
925  /// \brief Build an empty clause.
926  ///
927  explicit OMPOrderedClause()
928      : OMPClause(OMPC_ordered, SourceLocation(), SourceLocation()),
929        LParenLoc(SourceLocation()), NumForLoops(nullptr) {}
930
931  /// \brief Sets the location of '('.
932  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
933  /// \brief Returns the location of '('.
934  SourceLocation getLParenLoc() const { return LParenLoc; }
935
936  /// \brief Return the number of associated for-loops.
937  Expr *getNumForLoops() const { return cast_or_null<Expr>(NumForLoops); }
938
939  static bool classof(const OMPClause *T) {
940    return T->getClauseKind() == OMPC_ordered;
941  }
942
943  child_range children() { return child_range(&NumForLoops, &NumForLoops + 1); }
944};
945
946/// \brief This represents 'nowait' clause in the '#pragma omp ...' directive.
947///
948/// \code
949/// #pragma omp for nowait
950/// \endcode
951/// In this example directive '#pragma omp for' has 'nowait' clause.
952///
953class OMPNowaitClause : public OMPClause {
954public:
955  /// \brief Build 'nowait' clause.
956  ///
957  /// \param StartLoc Starting location of the clause.
958  /// \param EndLoc Ending location of the clause.
959  ///
960  OMPNowaitClause(SourceLocation StartLoc, SourceLocation EndLoc)
961      : OMPClause(OMPC_nowait, StartLoc, EndLoc) {}
962
963  /// \brief Build an empty clause.
964  ///
965  OMPNowaitClause()
966      : OMPClause(OMPC_nowait, SourceLocation(), SourceLocation()) {}
967
968  static bool classof(const OMPClause *T) {
969    return T->getClauseKind() == OMPC_nowait;
970  }
971
972  child_range children() {
973    return child_range(child_iterator(), child_iterator());
974  }
975};
976
977/// \brief This represents 'untied' clause in the '#pragma omp ...' directive.
978///
979/// \code
980/// #pragma omp task untied
981/// \endcode
982/// In this example directive '#pragma omp task' has 'untied' clause.
983///
984class OMPUntiedClause : public OMPClause {
985public:
986  /// \brief Build 'untied' clause.
987  ///
988  /// \param StartLoc Starting location of the clause.
989  /// \param EndLoc Ending location of the clause.
990  ///
991  OMPUntiedClause(SourceLocation StartLoc, SourceLocation EndLoc)
992      : OMPClause(OMPC_untied, StartLoc, EndLoc) {}
993
994  /// \brief Build an empty clause.
995  ///
996  OMPUntiedClause()
997      : OMPClause(OMPC_untied, SourceLocation(), SourceLocation()) {}
998
999  static bool classof(const OMPClause *T) {
1000    return T->getClauseKind() == OMPC_untied;
1001  }
1002
1003  child_range children() {
1004    return child_range(child_iterator(), child_iterator());
1005  }
1006};
1007
1008/// \brief This represents 'mergeable' clause in the '#pragma omp ...'
1009/// directive.
1010///
1011/// \code
1012/// #pragma omp task mergeable
1013/// \endcode
1014/// In this example directive '#pragma omp task' has 'mergeable' clause.
1015///
1016class OMPMergeableClause : public OMPClause {
1017public:
1018  /// \brief Build 'mergeable' clause.
1019  ///
1020  /// \param StartLoc Starting location of the clause.
1021  /// \param EndLoc Ending location of the clause.
1022  ///
1023  OMPMergeableClause(SourceLocation StartLoc, SourceLocation EndLoc)
1024      : OMPClause(OMPC_mergeable, StartLoc, EndLoc) {}
1025
1026  /// \brief Build an empty clause.
1027  ///
1028  OMPMergeableClause()
1029      : OMPClause(OMPC_mergeable, SourceLocation(), SourceLocation()) {}
1030
1031  static bool classof(const OMPClause *T) {
1032    return T->getClauseKind() == OMPC_mergeable;
1033  }
1034
1035  child_range children() {
1036    return child_range(child_iterator(), child_iterator());
1037  }
1038};
1039
1040/// \brief This represents 'read' clause in the '#pragma omp atomic' directive.
1041///
1042/// \code
1043/// #pragma omp atomic read
1044/// \endcode
1045/// In this example directive '#pragma omp atomic' has 'read' clause.
1046///
1047class OMPReadClause : public OMPClause {
1048public:
1049  /// \brief Build 'read' clause.
1050  ///
1051  /// \param StartLoc Starting location of the clause.
1052  /// \param EndLoc Ending location of the clause.
1053  ///
1054  OMPReadClause(SourceLocation StartLoc, SourceLocation EndLoc)
1055      : OMPClause(OMPC_read, StartLoc, EndLoc) {}
1056
1057  /// \brief Build an empty clause.
1058  ///
1059  OMPReadClause() : OMPClause(OMPC_read, SourceLocation(), SourceLocation()) {}
1060
1061  static bool classof(const OMPClause *T) {
1062    return T->getClauseKind() == OMPC_read;
1063  }
1064
1065  child_range children() {
1066    return child_range(child_iterator(), child_iterator());
1067  }
1068};
1069
1070/// \brief This represents 'write' clause in the '#pragma omp atomic' directive.
1071///
1072/// \code
1073/// #pragma omp atomic write
1074/// \endcode
1075/// In this example directive '#pragma omp atomic' has 'write' clause.
1076///
1077class OMPWriteClause : public OMPClause {
1078public:
1079  /// \brief Build 'write' clause.
1080  ///
1081  /// \param StartLoc Starting location of the clause.
1082  /// \param EndLoc Ending location of the clause.
1083  ///
1084  OMPWriteClause(SourceLocation StartLoc, SourceLocation EndLoc)
1085      : OMPClause(OMPC_write, StartLoc, EndLoc) {}
1086
1087  /// \brief Build an empty clause.
1088  ///
1089  OMPWriteClause()
1090      : OMPClause(OMPC_write, SourceLocation(), SourceLocation()) {}
1091
1092  static bool classof(const OMPClause *T) {
1093    return T->getClauseKind() == OMPC_write;
1094  }
1095
1096  child_range children() {
1097    return child_range(child_iterator(), child_iterator());
1098  }
1099};
1100
1101/// \brief This represents 'update' clause in the '#pragma omp atomic'
1102/// directive.
1103///
1104/// \code
1105/// #pragma omp atomic update
1106/// \endcode
1107/// In this example directive '#pragma omp atomic' has 'update' clause.
1108///
1109class OMPUpdateClause : public OMPClause {
1110public:
1111  /// \brief Build 'update' clause.
1112  ///
1113  /// \param StartLoc Starting location of the clause.
1114  /// \param EndLoc Ending location of the clause.
1115  ///
1116  OMPUpdateClause(SourceLocation StartLoc, SourceLocation EndLoc)
1117      : OMPClause(OMPC_update, StartLoc, EndLoc) {}
1118
1119  /// \brief Build an empty clause.
1120  ///
1121  OMPUpdateClause()
1122      : OMPClause(OMPC_update, SourceLocation(), SourceLocation()) {}
1123
1124  static bool classof(const OMPClause *T) {
1125    return T->getClauseKind() == OMPC_update;
1126  }
1127
1128  child_range children() {
1129    return child_range(child_iterator(), child_iterator());
1130  }
1131};
1132
1133/// \brief This represents 'capture' clause in the '#pragma omp atomic'
1134/// directive.
1135///
1136/// \code
1137/// #pragma omp atomic capture
1138/// \endcode
1139/// In this example directive '#pragma omp atomic' has 'capture' clause.
1140///
1141class OMPCaptureClause : public OMPClause {
1142public:
1143  /// \brief Build 'capture' clause.
1144  ///
1145  /// \param StartLoc Starting location of the clause.
1146  /// \param EndLoc Ending location of the clause.
1147  ///
1148  OMPCaptureClause(SourceLocation StartLoc, SourceLocation EndLoc)
1149      : OMPClause(OMPC_capture, StartLoc, EndLoc) {}
1150
1151  /// \brief Build an empty clause.
1152  ///
1153  OMPCaptureClause()
1154      : OMPClause(OMPC_capture, SourceLocation(), SourceLocation()) {}
1155
1156  static bool classof(const OMPClause *T) {
1157    return T->getClauseKind() == OMPC_capture;
1158  }
1159
1160  child_range children() {
1161    return child_range(child_iterator(), child_iterator());
1162  }
1163};
1164
1165/// \brief This represents 'seq_cst' clause in the '#pragma omp atomic'
1166/// directive.
1167///
1168/// \code
1169/// #pragma omp atomic seq_cst
1170/// \endcode
1171/// In this example directive '#pragma omp atomic' has 'seq_cst' clause.
1172///
1173class OMPSeqCstClause : public OMPClause {
1174public:
1175  /// \brief Build 'seq_cst' clause.
1176  ///
1177  /// \param StartLoc Starting location of the clause.
1178  /// \param EndLoc Ending location of the clause.
1179  ///
1180  OMPSeqCstClause(SourceLocation StartLoc, SourceLocation EndLoc)
1181      : OMPClause(OMPC_seq_cst, StartLoc, EndLoc) {}
1182
1183  /// \brief Build an empty clause.
1184  ///
1185  OMPSeqCstClause()
1186      : OMPClause(OMPC_seq_cst, SourceLocation(), SourceLocation()) {}
1187
1188  static bool classof(const OMPClause *T) {
1189    return T->getClauseKind() == OMPC_seq_cst;
1190  }
1191
1192  child_range children() {
1193    return child_range(child_iterator(), child_iterator());
1194  }
1195};
1196
1197/// \brief This represents clause 'private' in the '#pragma omp ...' directives.
1198///
1199/// \code
1200/// #pragma omp parallel private(a,b)
1201/// \endcode
1202/// In this example directive '#pragma omp parallel' has clause 'private'
1203/// with the variables 'a' and 'b'.
1204///
1205class OMPPrivateClause final
1206    : public OMPVarListClause<OMPPrivateClause>,
1207      private llvm::TrailingObjects<OMPPrivateClause, Expr *> {
1208  friend TrailingObjects;
1209  friend OMPVarListClause;
1210  friend class OMPClauseReader;
1211  /// \brief Build clause with number of variables \a N.
1212  ///
1213  /// \param StartLoc Starting location of the clause.
1214  /// \param LParenLoc Location of '('.
1215  /// \param EndLoc Ending location of the clause.
1216  /// \param N Number of the variables in the clause.
1217  ///
1218  OMPPrivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1219                   SourceLocation EndLoc, unsigned N)
1220      : OMPVarListClause<OMPPrivateClause>(OMPC_private, StartLoc, LParenLoc,
1221                                           EndLoc, N) {}
1222
1223  /// \brief Build an empty clause.
1224  ///
1225  /// \param N Number of variables.
1226  ///
1227  explicit OMPPrivateClause(unsigned N)
1228      : OMPVarListClause<OMPPrivateClause>(OMPC_private, SourceLocation(),
1229                                           SourceLocation(), SourceLocation(),
1230                                           N) {}
1231
1232  /// \brief Sets the list of references to private copies with initializers for
1233  /// new private variables.
1234  /// \param VL List of references.
1235  void setPrivateCopies(ArrayRef<Expr *> VL);
1236
1237  /// \brief Gets the list of references to private copies with initializers for
1238  /// new private variables.
1239  MutableArrayRef<Expr *> getPrivateCopies() {
1240    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
1241  }
1242  ArrayRef<const Expr *> getPrivateCopies() const {
1243    return llvm::makeArrayRef(varlist_end(), varlist_size());
1244  }
1245
1246public:
1247  /// \brief Creates clause with a list of variables \a VL.
1248  ///
1249  /// \param C AST context.
1250  /// \param StartLoc Starting location of the clause.
1251  /// \param LParenLoc Location of '('.
1252  /// \param EndLoc Ending location of the clause.
1253  /// \param VL List of references to the variables.
1254  /// \param PrivateVL List of references to private copies with initializers.
1255  ///
1256  static OMPPrivateClause *Create(const ASTContext &C, SourceLocation StartLoc,
1257                                  SourceLocation LParenLoc,
1258                                  SourceLocation EndLoc, ArrayRef<Expr *> VL,
1259                                  ArrayRef<Expr *> PrivateVL);
1260  /// \brief Creates an empty clause with the place for \a N variables.
1261  ///
1262  /// \param C AST context.
1263  /// \param N The number of variables.
1264  ///
1265  static OMPPrivateClause *CreateEmpty(const ASTContext &C, unsigned N);
1266
1267  typedef MutableArrayRef<Expr *>::iterator private_copies_iterator;
1268  typedef ArrayRef<const Expr *>::iterator private_copies_const_iterator;
1269  typedef llvm::iterator_range<private_copies_iterator> private_copies_range;
1270  typedef llvm::iterator_range<private_copies_const_iterator>
1271      private_copies_const_range;
1272
1273  private_copies_range private_copies() {
1274    return private_copies_range(getPrivateCopies().begin(),
1275                                getPrivateCopies().end());
1276  }
1277  private_copies_const_range private_copies() const {
1278    return private_copies_const_range(getPrivateCopies().begin(),
1279                                      getPrivateCopies().end());
1280  }
1281
1282  child_range children() {
1283    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
1284                       reinterpret_cast<Stmt **>(varlist_end()));
1285  }
1286
1287  static bool classof(const OMPClause *T) {
1288    return T->getClauseKind() == OMPC_private;
1289  }
1290};
1291
1292/// \brief This represents clause 'firstprivate' in the '#pragma omp ...'
1293/// directives.
1294///
1295/// \code
1296/// #pragma omp parallel firstprivate(a,b)
1297/// \endcode
1298/// In this example directive '#pragma omp parallel' has clause 'firstprivate'
1299/// with the variables 'a' and 'b'.
1300///
1301class OMPFirstprivateClause final
1302    : public OMPVarListClause<OMPFirstprivateClause>,
1303      public OMPClauseWithPreInit,
1304      private llvm::TrailingObjects<OMPFirstprivateClause, Expr *> {
1305  friend TrailingObjects;
1306  friend OMPVarListClause;
1307  friend class OMPClauseReader;
1308
1309  /// \brief Build clause with number of variables \a N.
1310  ///
1311  /// \param StartLoc Starting location of the clause.
1312  /// \param LParenLoc Location of '('.
1313  /// \param EndLoc Ending location of the clause.
1314  /// \param N Number of the variables in the clause.
1315  ///
1316  OMPFirstprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1317                        SourceLocation EndLoc, unsigned N)
1318      : OMPVarListClause<OMPFirstprivateClause>(OMPC_firstprivate, StartLoc,
1319                                                LParenLoc, EndLoc, N),
1320        OMPClauseWithPreInit(this) {}
1321
1322  /// \brief Build an empty clause.
1323  ///
1324  /// \param N Number of variables.
1325  ///
1326  explicit OMPFirstprivateClause(unsigned N)
1327      : OMPVarListClause<OMPFirstprivateClause>(
1328            OMPC_firstprivate, SourceLocation(), SourceLocation(),
1329            SourceLocation(), N),
1330        OMPClauseWithPreInit(this) {}
1331  /// \brief Sets the list of references to private copies with initializers for
1332  /// new private variables.
1333  /// \param VL List of references.
1334  void setPrivateCopies(ArrayRef<Expr *> VL);
1335
1336  /// \brief Gets the list of references to private copies with initializers for
1337  /// new private variables.
1338  MutableArrayRef<Expr *> getPrivateCopies() {
1339    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
1340  }
1341  ArrayRef<const Expr *> getPrivateCopies() const {
1342    return llvm::makeArrayRef(varlist_end(), varlist_size());
1343  }
1344
1345  /// \brief Sets the list of references to initializer variables for new
1346  /// private variables.
1347  /// \param VL List of references.
1348  void setInits(ArrayRef<Expr *> VL);
1349
1350  /// \brief Gets the list of references to initializer variables for new
1351  /// private variables.
1352  MutableArrayRef<Expr *> getInits() {
1353    return MutableArrayRef<Expr *>(getPrivateCopies().end(), varlist_size());
1354  }
1355  ArrayRef<const Expr *> getInits() const {
1356    return llvm::makeArrayRef(getPrivateCopies().end(), varlist_size());
1357  }
1358
1359public:
1360  /// \brief Creates clause with a list of variables \a VL.
1361  ///
1362  /// \param C AST context.
1363  /// \param StartLoc Starting location of the clause.
1364  /// \param LParenLoc Location of '('.
1365  /// \param EndLoc Ending location of the clause.
1366  /// \param VL List of references to the original variables.
1367  /// \param PrivateVL List of references to private copies with initializers.
1368  /// \param InitVL List of references to auto generated variables used for
1369  /// initialization of a single array element. Used if firstprivate variable is
1370  /// of array type.
1371  /// \param PreInit Statement that must be executed before entering the OpenMP
1372  /// region with this clause.
1373  ///
1374  static OMPFirstprivateClause *
1375  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1376         SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
1377         ArrayRef<Expr *> InitVL, Stmt *PreInit);
1378  /// \brief Creates an empty clause with the place for \a N variables.
1379  ///
1380  /// \param C AST context.
1381  /// \param N The number of variables.
1382  ///
1383  static OMPFirstprivateClause *CreateEmpty(const ASTContext &C, unsigned N);
1384
1385  typedef MutableArrayRef<Expr *>::iterator private_copies_iterator;
1386  typedef ArrayRef<const Expr *>::iterator private_copies_const_iterator;
1387  typedef llvm::iterator_range<private_copies_iterator> private_copies_range;
1388  typedef llvm::iterator_range<private_copies_const_iterator>
1389      private_copies_const_range;
1390
1391  private_copies_range private_copies() {
1392    return private_copies_range(getPrivateCopies().begin(),
1393                                getPrivateCopies().end());
1394  }
1395  private_copies_const_range private_copies() const {
1396    return private_copies_const_range(getPrivateCopies().begin(),
1397                                      getPrivateCopies().end());
1398  }
1399
1400  typedef MutableArrayRef<Expr *>::iterator inits_iterator;
1401  typedef ArrayRef<const Expr *>::iterator inits_const_iterator;
1402  typedef llvm::iterator_range<inits_iterator> inits_range;
1403  typedef llvm::iterator_range<inits_const_iterator> inits_const_range;
1404
1405  inits_range inits() {
1406    return inits_range(getInits().begin(), getInits().end());
1407  }
1408  inits_const_range inits() const {
1409    return inits_const_range(getInits().begin(), getInits().end());
1410  }
1411
1412  child_range children() {
1413    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
1414                       reinterpret_cast<Stmt **>(varlist_end()));
1415  }
1416
1417  static bool classof(const OMPClause *T) {
1418    return T->getClauseKind() == OMPC_firstprivate;
1419  }
1420};
1421
1422/// \brief This represents clause 'lastprivate' in the '#pragma omp ...'
1423/// directives.
1424///
1425/// \code
1426/// #pragma omp simd lastprivate(a,b)
1427/// \endcode
1428/// In this example directive '#pragma omp simd' has clause 'lastprivate'
1429/// with the variables 'a' and 'b'.
1430class OMPLastprivateClause final
1431    : public OMPVarListClause<OMPLastprivateClause>,
1432      public OMPClauseWithPostUpdate,
1433      private llvm::TrailingObjects<OMPLastprivateClause, Expr *> {
1434  // There are 4 additional tail-allocated arrays at the end of the class:
1435  // 1. Contains list of pseudo variables with the default initialization for
1436  // each non-firstprivate variables. Used in codegen for initialization of
1437  // lastprivate copies.
1438  // 2. List of helper expressions for proper generation of assignment operation
1439  // required for lastprivate clause. This list represents private variables
1440  // (for arrays, single array element).
1441  // 3. List of helper expressions for proper generation of assignment operation
1442  // required for lastprivate clause. This list represents original variables
1443  // (for arrays, single array element).
1444  // 4. List of helper expressions that represents assignment operation:
1445  // \code
1446  // DstExprs = SrcExprs;
1447  // \endcode
1448  // Required for proper codegen of final assignment performed by the
1449  // lastprivate clause.
1450  //
1451  friend TrailingObjects;
1452  friend OMPVarListClause;
1453  friend class OMPClauseReader;
1454
1455  /// \brief Build clause with number of variables \a N.
1456  ///
1457  /// \param StartLoc Starting location of the clause.
1458  /// \param LParenLoc Location of '('.
1459  /// \param EndLoc Ending location of the clause.
1460  /// \param N Number of the variables in the clause.
1461  ///
1462  OMPLastprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1463                       SourceLocation EndLoc, unsigned N)
1464      : OMPVarListClause<OMPLastprivateClause>(OMPC_lastprivate, StartLoc,
1465                                               LParenLoc, EndLoc, N),
1466        OMPClauseWithPostUpdate(this) {}
1467
1468  /// \brief Build an empty clause.
1469  ///
1470  /// \param N Number of variables.
1471  ///
1472  explicit OMPLastprivateClause(unsigned N)
1473      : OMPVarListClause<OMPLastprivateClause>(
1474            OMPC_lastprivate, SourceLocation(), SourceLocation(),
1475            SourceLocation(), N),
1476        OMPClauseWithPostUpdate(this) {}
1477
1478  /// \brief Get the list of helper expressions for initialization of private
1479  /// copies for lastprivate variables.
1480  MutableArrayRef<Expr *> getPrivateCopies() {
1481    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
1482  }
1483  ArrayRef<const Expr *> getPrivateCopies() const {
1484    return llvm::makeArrayRef(varlist_end(), varlist_size());
1485  }
1486
1487  /// \brief Set list of helper expressions, required for proper codegen of the
1488  /// clause. These expressions represent private variables (for arrays, single
1489  /// array element) in the final assignment statement performed by the
1490  /// lastprivate clause.
1491  void setSourceExprs(ArrayRef<Expr *> SrcExprs);
1492
1493  /// \brief Get the list of helper source expressions.
1494  MutableArrayRef<Expr *> getSourceExprs() {
1495    return MutableArrayRef<Expr *>(getPrivateCopies().end(), varlist_size());
1496  }
1497  ArrayRef<const Expr *> getSourceExprs() const {
1498    return llvm::makeArrayRef(getPrivateCopies().end(), varlist_size());
1499  }
1500
1501  /// \brief Set list of helper expressions, required for proper codegen of the
1502  /// clause. These expressions represent original variables (for arrays, single
1503  /// array element) in the final assignment statement performed by the
1504  /// lastprivate clause.
1505  void setDestinationExprs(ArrayRef<Expr *> DstExprs);
1506
1507  /// \brief Get the list of helper destination expressions.
1508  MutableArrayRef<Expr *> getDestinationExprs() {
1509    return MutableArrayRef<Expr *>(getSourceExprs().end(), varlist_size());
1510  }
1511  ArrayRef<const Expr *> getDestinationExprs() const {
1512    return llvm::makeArrayRef(getSourceExprs().end(), varlist_size());
1513  }
1514
1515  /// \brief Set list of helper assignment expressions, required for proper
1516  /// codegen of the clause. These expressions are assignment expressions that
1517  /// assign private copy of the variable to original variable.
1518  void setAssignmentOps(ArrayRef<Expr *> AssignmentOps);
1519
1520  /// \brief Get the list of helper assignment expressions.
1521  MutableArrayRef<Expr *> getAssignmentOps() {
1522    return MutableArrayRef<Expr *>(getDestinationExprs().end(), varlist_size());
1523  }
1524  ArrayRef<const Expr *> getAssignmentOps() const {
1525    return llvm::makeArrayRef(getDestinationExprs().end(), varlist_size());
1526  }
1527
1528public:
1529  /// \brief Creates clause with a list of variables \a VL.
1530  ///
1531  /// \param C AST context.
1532  /// \param StartLoc Starting location of the clause.
1533  /// \param LParenLoc Location of '('.
1534  /// \param EndLoc Ending location of the clause.
1535  /// \param VL List of references to the variables.
1536  /// \param SrcExprs List of helper expressions for proper generation of
1537  /// assignment operation required for lastprivate clause. This list represents
1538  /// private variables (for arrays, single array element).
1539  /// \param DstExprs List of helper expressions for proper generation of
1540  /// assignment operation required for lastprivate clause. This list represents
1541  /// original variables (for arrays, single array element).
1542  /// \param AssignmentOps List of helper expressions that represents assignment
1543  /// operation:
1544  /// \code
1545  /// DstExprs = SrcExprs;
1546  /// \endcode
1547  /// Required for proper codegen of final assignment performed by the
1548  /// lastprivate clause.
1549  /// \param PreInit Statement that must be executed before entering the OpenMP
1550  /// region with this clause.
1551  /// \param PostUpdate Expression that must be executed after exit from the
1552  /// OpenMP region with this clause.
1553  ///
1554  static OMPLastprivateClause *
1555  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1556         SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
1557         ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps,
1558         Stmt *PreInit, Expr *PostUpdate);
1559  /// \brief Creates an empty clause with the place for \a N variables.
1560  ///
1561  /// \param C AST context.
1562  /// \param N The number of variables.
1563  ///
1564  static OMPLastprivateClause *CreateEmpty(const ASTContext &C, unsigned N);
1565
1566  typedef MutableArrayRef<Expr *>::iterator helper_expr_iterator;
1567  typedef ArrayRef<const Expr *>::iterator helper_expr_const_iterator;
1568  typedef llvm::iterator_range<helper_expr_iterator> helper_expr_range;
1569  typedef llvm::iterator_range<helper_expr_const_iterator>
1570      helper_expr_const_range;
1571
1572  /// \brief Set list of helper expressions, required for generation of private
1573  /// copies of original lastprivate variables.
1574  void setPrivateCopies(ArrayRef<Expr *> PrivateCopies);
1575
1576  helper_expr_const_range private_copies() const {
1577    return helper_expr_const_range(getPrivateCopies().begin(),
1578                                   getPrivateCopies().end());
1579  }
1580  helper_expr_range private_copies() {
1581    return helper_expr_range(getPrivateCopies().begin(),
1582                             getPrivateCopies().end());
1583  }
1584  helper_expr_const_range source_exprs() const {
1585    return helper_expr_const_range(getSourceExprs().begin(),
1586                                   getSourceExprs().end());
1587  }
1588  helper_expr_range source_exprs() {
1589    return helper_expr_range(getSourceExprs().begin(), getSourceExprs().end());
1590  }
1591  helper_expr_const_range destination_exprs() const {
1592    return helper_expr_const_range(getDestinationExprs().begin(),
1593                                   getDestinationExprs().end());
1594  }
1595  helper_expr_range destination_exprs() {
1596    return helper_expr_range(getDestinationExprs().begin(),
1597                             getDestinationExprs().end());
1598  }
1599  helper_expr_const_range assignment_ops() const {
1600    return helper_expr_const_range(getAssignmentOps().begin(),
1601                                   getAssignmentOps().end());
1602  }
1603  helper_expr_range assignment_ops() {
1604    return helper_expr_range(getAssignmentOps().begin(),
1605                             getAssignmentOps().end());
1606  }
1607
1608  child_range children() {
1609    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
1610                       reinterpret_cast<Stmt **>(varlist_end()));
1611  }
1612
1613  static bool classof(const OMPClause *T) {
1614    return T->getClauseKind() == OMPC_lastprivate;
1615  }
1616};
1617
1618/// \brief This represents clause 'shared' in the '#pragma omp ...' directives.
1619///
1620/// \code
1621/// #pragma omp parallel shared(a,b)
1622/// \endcode
1623/// In this example directive '#pragma omp parallel' has clause 'shared'
1624/// with the variables 'a' and 'b'.
1625///
1626class OMPSharedClause final
1627    : public OMPVarListClause<OMPSharedClause>,
1628      private llvm::TrailingObjects<OMPSharedClause, Expr *> {
1629  friend TrailingObjects;
1630  friend OMPVarListClause;
1631  /// \brief Build clause with number of variables \a N.
1632  ///
1633  /// \param StartLoc Starting location of the clause.
1634  /// \param LParenLoc Location of '('.
1635  /// \param EndLoc Ending location of the clause.
1636  /// \param N Number of the variables in the clause.
1637  ///
1638  OMPSharedClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1639                  SourceLocation EndLoc, unsigned N)
1640      : OMPVarListClause<OMPSharedClause>(OMPC_shared, StartLoc, LParenLoc,
1641                                          EndLoc, N) {}
1642
1643  /// \brief Build an empty clause.
1644  ///
1645  /// \param N Number of variables.
1646  ///
1647  explicit OMPSharedClause(unsigned N)
1648      : OMPVarListClause<OMPSharedClause>(OMPC_shared, SourceLocation(),
1649                                          SourceLocation(), SourceLocation(),
1650                                          N) {}
1651
1652public:
1653  /// \brief Creates clause with a list of variables \a VL.
1654  ///
1655  /// \param C AST context.
1656  /// \param StartLoc Starting location of the clause.
1657  /// \param LParenLoc Location of '('.
1658  /// \param EndLoc Ending location of the clause.
1659  /// \param VL List of references to the variables.
1660  ///
1661  static OMPSharedClause *Create(const ASTContext &C, SourceLocation StartLoc,
1662                                 SourceLocation LParenLoc,
1663                                 SourceLocation EndLoc, ArrayRef<Expr *> VL);
1664  /// \brief Creates an empty clause with \a N variables.
1665  ///
1666  /// \param C AST context.
1667  /// \param N The number of variables.
1668  ///
1669  static OMPSharedClause *CreateEmpty(const ASTContext &C, unsigned N);
1670
1671  child_range children() {
1672    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
1673                       reinterpret_cast<Stmt **>(varlist_end()));
1674  }
1675
1676  static bool classof(const OMPClause *T) {
1677    return T->getClauseKind() == OMPC_shared;
1678  }
1679};
1680
1681/// \brief This represents clause 'reduction' in the '#pragma omp ...'
1682/// directives.
1683///
1684/// \code
1685/// #pragma omp parallel reduction(+:a,b)
1686/// \endcode
1687/// In this example directive '#pragma omp parallel' has clause 'reduction'
1688/// with operator '+' and the variables 'a' and 'b'.
1689///
1690class OMPReductionClause final
1691    : public OMPVarListClause<OMPReductionClause>,
1692      public OMPClauseWithPostUpdate,
1693      private llvm::TrailingObjects<OMPReductionClause, Expr *> {
1694  friend TrailingObjects;
1695  friend OMPVarListClause;
1696  friend class OMPClauseReader;
1697  /// \brief Location of ':'.
1698  SourceLocation ColonLoc;
1699  /// \brief Nested name specifier for C++.
1700  NestedNameSpecifierLoc QualifierLoc;
1701  /// \brief Name of custom operator.
1702  DeclarationNameInfo NameInfo;
1703
1704  /// \brief Build clause with number of variables \a N.
1705  ///
1706  /// \param StartLoc Starting location of the clause.
1707  /// \param LParenLoc Location of '('.
1708  /// \param EndLoc Ending location of the clause.
1709  /// \param ColonLoc Location of ':'.
1710  /// \param N Number of the variables in the clause.
1711  /// \param QualifierLoc The nested-name qualifier with location information
1712  /// \param NameInfo The full name info for reduction identifier.
1713  ///
1714  OMPReductionClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1715                     SourceLocation ColonLoc, SourceLocation EndLoc, unsigned N,
1716                     NestedNameSpecifierLoc QualifierLoc,
1717                     const DeclarationNameInfo &NameInfo)
1718      : OMPVarListClause<OMPReductionClause>(OMPC_reduction, StartLoc,
1719                                             LParenLoc, EndLoc, N),
1720        OMPClauseWithPostUpdate(this), ColonLoc(ColonLoc),
1721        QualifierLoc(QualifierLoc), NameInfo(NameInfo) {}
1722
1723  /// \brief Build an empty clause.
1724  ///
1725  /// \param N Number of variables.
1726  ///
1727  explicit OMPReductionClause(unsigned N)
1728      : OMPVarListClause<OMPReductionClause>(OMPC_reduction, SourceLocation(),
1729                                             SourceLocation(), SourceLocation(),
1730                                             N),
1731        OMPClauseWithPostUpdate(this), ColonLoc(), QualifierLoc(), NameInfo() {}
1732
1733  /// \brief Sets location of ':' symbol in clause.
1734  void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
1735  /// \brief Sets the name info for specified reduction identifier.
1736  void setNameInfo(DeclarationNameInfo DNI) { NameInfo = DNI; }
1737  /// \brief Sets the nested name specifier.
1738  void setQualifierLoc(NestedNameSpecifierLoc NSL) { QualifierLoc = NSL; }
1739
1740  /// \brief Set list of helper expressions, required for proper codegen of the
1741  /// clause. These expressions represent private copy of the reduction
1742  /// variable.
1743  void setPrivates(ArrayRef<Expr *> Privates);
1744
1745  /// \brief Get the list of helper privates.
1746  MutableArrayRef<Expr *> getPrivates() {
1747    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
1748  }
1749  ArrayRef<const Expr *> getPrivates() const {
1750    return llvm::makeArrayRef(varlist_end(), varlist_size());
1751  }
1752
1753  /// \brief Set list of helper expressions, required for proper codegen of the
1754  /// clause. These expressions represent LHS expression in the final
1755  /// reduction expression performed by the reduction clause.
1756  void setLHSExprs(ArrayRef<Expr *> LHSExprs);
1757
1758  /// \brief Get the list of helper LHS expressions.
1759  MutableArrayRef<Expr *> getLHSExprs() {
1760    return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
1761  }
1762  ArrayRef<const Expr *> getLHSExprs() const {
1763    return llvm::makeArrayRef(getPrivates().end(), varlist_size());
1764  }
1765
1766  /// \brief Set list of helper expressions, required for proper codegen of the
1767  /// clause. These expressions represent RHS expression in the final
1768  /// reduction expression performed by the reduction clause.
1769  /// Also, variables in these expressions are used for proper initialization of
1770  /// reduction copies.
1771  void setRHSExprs(ArrayRef<Expr *> RHSExprs);
1772
1773  /// \brief Get the list of helper destination expressions.
1774  MutableArrayRef<Expr *> getRHSExprs() {
1775    return MutableArrayRef<Expr *>(getLHSExprs().end(), varlist_size());
1776  }
1777  ArrayRef<const Expr *> getRHSExprs() const {
1778    return llvm::makeArrayRef(getLHSExprs().end(), varlist_size());
1779  }
1780
1781  /// \brief Set list of helper reduction expressions, required for proper
1782  /// codegen of the clause. These expressions are binary expressions or
1783  /// operator/custom reduction call that calculates new value from source
1784  /// helper expressions to destination helper expressions.
1785  void setReductionOps(ArrayRef<Expr *> ReductionOps);
1786
1787  /// \brief Get the list of helper reduction expressions.
1788  MutableArrayRef<Expr *> getReductionOps() {
1789    return MutableArrayRef<Expr *>(getRHSExprs().end(), varlist_size());
1790  }
1791  ArrayRef<const Expr *> getReductionOps() const {
1792    return llvm::makeArrayRef(getRHSExprs().end(), varlist_size());
1793  }
1794
1795public:
1796  /// \brief Creates clause with a list of variables \a VL.
1797  ///
1798  /// \param StartLoc Starting location of the clause.
1799  /// \param LParenLoc Location of '('.
1800  /// \param ColonLoc Location of ':'.
1801  /// \param EndLoc Ending location of the clause.
1802  /// \param VL The variables in the clause.
1803  /// \param QualifierLoc The nested-name qualifier with location information
1804  /// \param NameInfo The full name info for reduction identifier.
1805  /// \param Privates List of helper expressions for proper generation of
1806  /// private copies.
1807  /// \param LHSExprs List of helper expressions for proper generation of
1808  /// assignment operation required for copyprivate clause. This list represents
1809  /// LHSs of the reduction expressions.
1810  /// \param RHSExprs List of helper expressions for proper generation of
1811  /// assignment operation required for copyprivate clause. This list represents
1812  /// RHSs of the reduction expressions.
1813  /// Also, variables in these expressions are used for proper initialization of
1814  /// reduction copies.
1815  /// \param ReductionOps List of helper expressions that represents reduction
1816  /// expressions:
1817  /// \code
1818  /// LHSExprs binop RHSExprs;
1819  /// operator binop(LHSExpr, RHSExpr);
1820  /// <CutomReduction>(LHSExpr, RHSExpr);
1821  /// \endcode
1822  /// Required for proper codegen of final reduction operation performed by the
1823  /// reduction clause.
1824  /// \param PreInit Statement that must be executed before entering the OpenMP
1825  /// region with this clause.
1826  /// \param PostUpdate Expression that must be executed after exit from the
1827  /// OpenMP region with this clause.
1828  ///
1829  static OMPReductionClause *
1830  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1831         SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
1832         NestedNameSpecifierLoc QualifierLoc,
1833         const DeclarationNameInfo &NameInfo, ArrayRef<Expr *> Privates,
1834         ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
1835         ArrayRef<Expr *> ReductionOps, Stmt *PreInit, Expr *PostUpdate);
1836  /// \brief Creates an empty clause with the place for \a N variables.
1837  ///
1838  /// \param C AST context.
1839  /// \param N The number of variables.
1840  ///
1841  static OMPReductionClause *CreateEmpty(const ASTContext &C, unsigned N);
1842
1843  /// \brief Gets location of ':' symbol in clause.
1844  SourceLocation getColonLoc() const { return ColonLoc; }
1845  /// \brief Gets the name info for specified reduction identifier.
1846  const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
1847  /// \brief Gets the nested name specifier.
1848  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
1849
1850  typedef MutableArrayRef<Expr *>::iterator helper_expr_iterator;
1851  typedef ArrayRef<const Expr *>::iterator helper_expr_const_iterator;
1852  typedef llvm::iterator_range<helper_expr_iterator> helper_expr_range;
1853  typedef llvm::iterator_range<helper_expr_const_iterator>
1854      helper_expr_const_range;
1855
1856  helper_expr_const_range privates() const {
1857    return helper_expr_const_range(getPrivates().begin(), getPrivates().end());
1858  }
1859  helper_expr_range privates() {
1860    return helper_expr_range(getPrivates().begin(), getPrivates().end());
1861  }
1862  helper_expr_const_range lhs_exprs() const {
1863    return helper_expr_const_range(getLHSExprs().begin(), getLHSExprs().end());
1864  }
1865  helper_expr_range lhs_exprs() {
1866    return helper_expr_range(getLHSExprs().begin(), getLHSExprs().end());
1867  }
1868  helper_expr_const_range rhs_exprs() const {
1869    return helper_expr_const_range(getRHSExprs().begin(), getRHSExprs().end());
1870  }
1871  helper_expr_range rhs_exprs() {
1872    return helper_expr_range(getRHSExprs().begin(), getRHSExprs().end());
1873  }
1874  helper_expr_const_range reduction_ops() const {
1875    return helper_expr_const_range(getReductionOps().begin(),
1876                                   getReductionOps().end());
1877  }
1878  helper_expr_range reduction_ops() {
1879    return helper_expr_range(getReductionOps().begin(),
1880                             getReductionOps().end());
1881  }
1882
1883  child_range children() {
1884    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
1885                       reinterpret_cast<Stmt **>(varlist_end()));
1886  }
1887
1888  static bool classof(const OMPClause *T) {
1889    return T->getClauseKind() == OMPC_reduction;
1890  }
1891};
1892
1893/// This represents clause 'task_reduction' in the '#pragma omp taskgroup'
1894/// directives.
1895///
1896/// \code
1897/// #pragma omp taskgroup task_reduction(+:a,b)
1898/// \endcode
1899/// In this example directive '#pragma omp taskgroup' has clause
1900/// 'task_reduction' with operator '+' and the variables 'a' and 'b'.
1901///
1902class OMPTaskReductionClause final
1903    : public OMPVarListClause<OMPTaskReductionClause>,
1904      public OMPClauseWithPostUpdate,
1905      private llvm::TrailingObjects<OMPTaskReductionClause, Expr *> {
1906  friend TrailingObjects;
1907  friend OMPVarListClause;
1908  friend class OMPClauseReader;
1909  /// Location of ':'.
1910  SourceLocation ColonLoc;
1911  /// Nested name specifier for C++.
1912  NestedNameSpecifierLoc QualifierLoc;
1913  /// Name of custom operator.
1914  DeclarationNameInfo NameInfo;
1915
1916  /// Build clause with number of variables \a N.
1917  ///
1918  /// \param StartLoc Starting location of the clause.
1919  /// \param LParenLoc Location of '('.
1920  /// \param EndLoc Ending location of the clause.
1921  /// \param ColonLoc Location of ':'.
1922  /// \param N Number of the variables in the clause.
1923  /// \param QualifierLoc The nested-name qualifier with location information
1924  /// \param NameInfo The full name info for reduction identifier.
1925  ///
1926  OMPTaskReductionClause(SourceLocation StartLoc, SourceLocation LParenLoc,
1927                         SourceLocation ColonLoc, SourceLocation EndLoc,
1928                         unsigned N, NestedNameSpecifierLoc QualifierLoc,
1929                         const DeclarationNameInfo &NameInfo)
1930      : OMPVarListClause<OMPTaskReductionClause>(OMPC_task_reduction, StartLoc,
1931                                                 LParenLoc, EndLoc, N),
1932        OMPClauseWithPostUpdate(this), ColonLoc(ColonLoc),
1933        QualifierLoc(QualifierLoc), NameInfo(NameInfo) {}
1934
1935  /// Build an empty clause.
1936  ///
1937  /// \param N Number of variables.
1938  ///
1939  explicit OMPTaskReductionClause(unsigned N)
1940      : OMPVarListClause<OMPTaskReductionClause>(
1941            OMPC_task_reduction, SourceLocation(), SourceLocation(),
1942            SourceLocation(), N),
1943        OMPClauseWithPostUpdate(this), ColonLoc(), QualifierLoc(), NameInfo() {}
1944
1945  /// Sets location of ':' symbol in clause.
1946  void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
1947  /// Sets the name info for specified reduction identifier.
1948  void setNameInfo(DeclarationNameInfo DNI) { NameInfo = DNI; }
1949  /// Sets the nested name specifier.
1950  void setQualifierLoc(NestedNameSpecifierLoc NSL) { QualifierLoc = NSL; }
1951
1952  /// Set list of helper expressions, required for proper codegen of the clause.
1953  /// These expressions represent private copy of the reduction variable.
1954  void setPrivates(ArrayRef<Expr *> Privates);
1955
1956  /// Get the list of helper privates.
1957  MutableArrayRef<Expr *> getPrivates() {
1958    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
1959  }
1960  ArrayRef<const Expr *> getPrivates() const {
1961    return llvm::makeArrayRef(varlist_end(), varlist_size());
1962  }
1963
1964  /// Set list of helper expressions, required for proper codegen of the clause.
1965  /// These expressions represent LHS expression in the final reduction
1966  /// expression performed by the reduction clause.
1967  void setLHSExprs(ArrayRef<Expr *> LHSExprs);
1968
1969  /// Get the list of helper LHS expressions.
1970  MutableArrayRef<Expr *> getLHSExprs() {
1971    return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
1972  }
1973  ArrayRef<const Expr *> getLHSExprs() const {
1974    return llvm::makeArrayRef(getPrivates().end(), varlist_size());
1975  }
1976
1977  /// Set list of helper expressions, required for proper codegen of the clause.
1978  /// These expressions represent RHS expression in the final reduction
1979  /// expression performed by the reduction clause. Also, variables in these
1980  /// expressions are used for proper initialization of reduction copies.
1981  void setRHSExprs(ArrayRef<Expr *> RHSExprs);
1982
1983  ///  Get the list of helper destination expressions.
1984  MutableArrayRef<Expr *> getRHSExprs() {
1985    return MutableArrayRef<Expr *>(getLHSExprs().end(), varlist_size());
1986  }
1987  ArrayRef<const Expr *> getRHSExprs() const {
1988    return llvm::makeArrayRef(getLHSExprs().end(), varlist_size());
1989  }
1990
1991  /// Set list of helper reduction expressions, required for proper
1992  /// codegen of the clause. These expressions are binary expressions or
1993  /// operator/custom reduction call that calculates new value from source
1994  /// helper expressions to destination helper expressions.
1995  void setReductionOps(ArrayRef<Expr *> ReductionOps);
1996
1997  ///  Get the list of helper reduction expressions.
1998  MutableArrayRef<Expr *> getReductionOps() {
1999    return MutableArrayRef<Expr *>(getRHSExprs().end(), varlist_size());
2000  }
2001  ArrayRef<const Expr *> getReductionOps() const {
2002    return llvm::makeArrayRef(getRHSExprs().end(), varlist_size());
2003  }
2004
2005public:
2006  /// Creates clause with a list of variables \a VL.
2007  ///
2008  /// \param StartLoc Starting location of the clause.
2009  /// \param LParenLoc Location of '('.
2010  /// \param ColonLoc Location of ':'.
2011  /// \param EndLoc Ending location of the clause.
2012  /// \param VL The variables in the clause.
2013  /// \param QualifierLoc The nested-name qualifier with location information
2014  /// \param NameInfo The full name info for reduction identifier.
2015  /// \param Privates List of helper expressions for proper generation of
2016  /// private copies.
2017  /// \param LHSExprs List of helper expressions for proper generation of
2018  /// assignment operation required for copyprivate clause. This list represents
2019  /// LHSs of the reduction expressions.
2020  /// \param RHSExprs List of helper expressions for proper generation of
2021  /// assignment operation required for copyprivate clause. This list represents
2022  /// RHSs of the reduction expressions.
2023  /// Also, variables in these expressions are used for proper initialization of
2024  /// reduction copies.
2025  /// \param ReductionOps List of helper expressions that represents reduction
2026  /// expressions:
2027  /// \code
2028  /// LHSExprs binop RHSExprs;
2029  /// operator binop(LHSExpr, RHSExpr);
2030  /// <CutomReduction>(LHSExpr, RHSExpr);
2031  /// \endcode
2032  /// Required for proper codegen of final reduction operation performed by the
2033  /// reduction clause.
2034  /// \param PreInit Statement that must be executed before entering the OpenMP
2035  /// region with this clause.
2036  /// \param PostUpdate Expression that must be executed after exit from the
2037  /// OpenMP region with this clause.
2038  ///
2039  static OMPTaskReductionClause *
2040  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2041         SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
2042         NestedNameSpecifierLoc QualifierLoc,
2043         const DeclarationNameInfo &NameInfo, ArrayRef<Expr *> Privates,
2044         ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
2045         ArrayRef<Expr *> ReductionOps, Stmt *PreInit, Expr *PostUpdate);
2046
2047  /// Creates an empty clause with the place for \a N variables.
2048  ///
2049  /// \param C AST context.
2050  /// \param N The number of variables.
2051  ///
2052  static OMPTaskReductionClause *CreateEmpty(const ASTContext &C, unsigned N);
2053
2054  /// Gets location of ':' symbol in clause.
2055  SourceLocation getColonLoc() const { return ColonLoc; }
2056  /// Gets the name info for specified reduction identifier.
2057  const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2058  /// Gets the nested name specifier.
2059  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2060
2061  typedef MutableArrayRef<Expr *>::iterator helper_expr_iterator;
2062  typedef ArrayRef<const Expr *>::iterator helper_expr_const_iterator;
2063  typedef llvm::iterator_range<helper_expr_iterator> helper_expr_range;
2064  typedef llvm::iterator_range<helper_expr_const_iterator>
2065      helper_expr_const_range;
2066
2067  helper_expr_const_range privates() const {
2068    return helper_expr_const_range(getPrivates().begin(), getPrivates().end());
2069  }
2070  helper_expr_range privates() {
2071    return helper_expr_range(getPrivates().begin(), getPrivates().end());
2072  }
2073  helper_expr_const_range lhs_exprs() const {
2074    return helper_expr_const_range(getLHSExprs().begin(), getLHSExprs().end());
2075  }
2076  helper_expr_range lhs_exprs() {
2077    return helper_expr_range(getLHSExprs().begin(), getLHSExprs().end());
2078  }
2079  helper_expr_const_range rhs_exprs() const {
2080    return helper_expr_const_range(getRHSExprs().begin(), getRHSExprs().end());
2081  }
2082  helper_expr_range rhs_exprs() {
2083    return helper_expr_range(getRHSExprs().begin(), getRHSExprs().end());
2084  }
2085  helper_expr_const_range reduction_ops() const {
2086    return helper_expr_const_range(getReductionOps().begin(),
2087                                   getReductionOps().end());
2088  }
2089  helper_expr_range reduction_ops() {
2090    return helper_expr_range(getReductionOps().begin(),
2091                             getReductionOps().end());
2092  }
2093
2094  child_range children() {
2095    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2096                       reinterpret_cast<Stmt **>(varlist_end()));
2097  }
2098
2099  static bool classof(const OMPClause *T) {
2100    return T->getClauseKind() == OMPC_task_reduction;
2101  }
2102};
2103
2104/// This represents clause 'in_reduction' in the '#pragma omp task' directives.
2105///
2106/// \code
2107/// #pragma omp task in_reduction(+:a,b)
2108/// \endcode
2109/// In this example directive '#pragma omp task' has clause 'in_reduction' with
2110/// operator '+' and the variables 'a' and 'b'.
2111///
2112class OMPInReductionClause final
2113    : public OMPVarListClause<OMPInReductionClause>,
2114      public OMPClauseWithPostUpdate,
2115      private llvm::TrailingObjects<OMPInReductionClause, Expr *> {
2116  friend TrailingObjects;
2117  friend OMPVarListClause;
2118  friend class OMPClauseReader;
2119  /// Location of ':'.
2120  SourceLocation ColonLoc;
2121  /// Nested name specifier for C++.
2122  NestedNameSpecifierLoc QualifierLoc;
2123  /// Name of custom operator.
2124  DeclarationNameInfo NameInfo;
2125
2126  /// Build clause with number of variables \a N.
2127  ///
2128  /// \param StartLoc Starting location of the clause.
2129  /// \param LParenLoc Location of '('.
2130  /// \param EndLoc Ending location of the clause.
2131  /// \param ColonLoc Location of ':'.
2132  /// \param N Number of the variables in the clause.
2133  /// \param QualifierLoc The nested-name qualifier with location information
2134  /// \param NameInfo The full name info for reduction identifier.
2135  ///
2136  OMPInReductionClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2137                       SourceLocation ColonLoc, SourceLocation EndLoc,
2138                       unsigned N, NestedNameSpecifierLoc QualifierLoc,
2139                       const DeclarationNameInfo &NameInfo)
2140      : OMPVarListClause<OMPInReductionClause>(OMPC_in_reduction, StartLoc,
2141                                               LParenLoc, EndLoc, N),
2142        OMPClauseWithPostUpdate(this), ColonLoc(ColonLoc),
2143        QualifierLoc(QualifierLoc), NameInfo(NameInfo) {}
2144
2145  /// Build an empty clause.
2146  ///
2147  /// \param N Number of variables.
2148  ///
2149  explicit OMPInReductionClause(unsigned N)
2150      : OMPVarListClause<OMPInReductionClause>(
2151            OMPC_in_reduction, SourceLocation(), SourceLocation(),
2152            SourceLocation(), N),
2153        OMPClauseWithPostUpdate(this), ColonLoc(), QualifierLoc(), NameInfo() {}
2154
2155  /// Sets location of ':' symbol in clause.
2156  void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
2157  /// Sets the name info for specified reduction identifier.
2158  void setNameInfo(DeclarationNameInfo DNI) { NameInfo = DNI; }
2159  /// Sets the nested name specifier.
2160  void setQualifierLoc(NestedNameSpecifierLoc NSL) { QualifierLoc = NSL; }
2161
2162  /// Set list of helper expressions, required for proper codegen of the clause.
2163  /// These expressions represent private copy of the reduction variable.
2164  void setPrivates(ArrayRef<Expr *> Privates);
2165
2166  /// Get the list of helper privates.
2167  MutableArrayRef<Expr *> getPrivates() {
2168    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2169  }
2170  ArrayRef<const Expr *> getPrivates() const {
2171    return llvm::makeArrayRef(varlist_end(), varlist_size());
2172  }
2173
2174  /// Set list of helper expressions, required for proper codegen of the clause.
2175  /// These expressions represent LHS expression in the final reduction
2176  /// expression performed by the reduction clause.
2177  void setLHSExprs(ArrayRef<Expr *> LHSExprs);
2178
2179  /// Get the list of helper LHS expressions.
2180  MutableArrayRef<Expr *> getLHSExprs() {
2181    return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
2182  }
2183  ArrayRef<const Expr *> getLHSExprs() const {
2184    return llvm::makeArrayRef(getPrivates().end(), varlist_size());
2185  }
2186
2187  /// Set list of helper expressions, required for proper codegen of the clause.
2188  /// These expressions represent RHS expression in the final reduction
2189  /// expression performed by the reduction clause. Also, variables in these
2190  /// expressions are used for proper initialization of reduction copies.
2191  void setRHSExprs(ArrayRef<Expr *> RHSExprs);
2192
2193  ///  Get the list of helper destination expressions.
2194  MutableArrayRef<Expr *> getRHSExprs() {
2195    return MutableArrayRef<Expr *>(getLHSExprs().end(), varlist_size());
2196  }
2197  ArrayRef<const Expr *> getRHSExprs() const {
2198    return llvm::makeArrayRef(getLHSExprs().end(), varlist_size());
2199  }
2200
2201  /// Set list of helper reduction expressions, required for proper
2202  /// codegen of the clause. These expressions are binary expressions or
2203  /// operator/custom reduction call that calculates new value from source
2204  /// helper expressions to destination helper expressions.
2205  void setReductionOps(ArrayRef<Expr *> ReductionOps);
2206
2207  ///  Get the list of helper reduction expressions.
2208  MutableArrayRef<Expr *> getReductionOps() {
2209    return MutableArrayRef<Expr *>(getRHSExprs().end(), varlist_size());
2210  }
2211  ArrayRef<const Expr *> getReductionOps() const {
2212    return llvm::makeArrayRef(getRHSExprs().end(), varlist_size());
2213  }
2214
2215  /// Set list of helper reduction taskgroup descriptors.
2216  void setTaskgroupDescriptors(ArrayRef<Expr *> ReductionOps);
2217
2218  ///  Get the list of helper reduction taskgroup descriptors.
2219  MutableArrayRef<Expr *> getTaskgroupDescriptors() {
2220    return MutableArrayRef<Expr *>(getReductionOps().end(), varlist_size());
2221  }
2222  ArrayRef<const Expr *> getTaskgroupDescriptors() const {
2223    return llvm::makeArrayRef(getReductionOps().end(), varlist_size());
2224  }
2225
2226public:
2227  /// Creates clause with a list of variables \a VL.
2228  ///
2229  /// \param StartLoc Starting location of the clause.
2230  /// \param LParenLoc Location of '('.
2231  /// \param ColonLoc Location of ':'.
2232  /// \param EndLoc Ending location of the clause.
2233  /// \param VL The variables in the clause.
2234  /// \param QualifierLoc The nested-name qualifier with location information
2235  /// \param NameInfo The full name info for reduction identifier.
2236  /// \param Privates List of helper expressions for proper generation of
2237  /// private copies.
2238  /// \param LHSExprs List of helper expressions for proper generation of
2239  /// assignment operation required for copyprivate clause. This list represents
2240  /// LHSs of the reduction expressions.
2241  /// \param RHSExprs List of helper expressions for proper generation of
2242  /// assignment operation required for copyprivate clause. This list represents
2243  /// RHSs of the reduction expressions.
2244  /// Also, variables in these expressions are used for proper initialization of
2245  /// reduction copies.
2246  /// \param ReductionOps List of helper expressions that represents reduction
2247  /// expressions:
2248  /// \code
2249  /// LHSExprs binop RHSExprs;
2250  /// operator binop(LHSExpr, RHSExpr);
2251  /// <CutomReduction>(LHSExpr, RHSExpr);
2252  /// \endcode
2253  /// Required for proper codegen of final reduction operation performed by the
2254  /// reduction clause.
2255  /// \param TaskgroupDescriptors List of helper taskgroup descriptors for
2256  /// corresponding items in parent taskgroup task_reduction clause.
2257  /// \param PreInit Statement that must be executed before entering the OpenMP
2258  /// region with this clause.
2259  /// \param PostUpdate Expression that must be executed after exit from the
2260  /// OpenMP region with this clause.
2261  ///
2262  static OMPInReductionClause *
2263  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2264         SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
2265         NestedNameSpecifierLoc QualifierLoc,
2266         const DeclarationNameInfo &NameInfo, ArrayRef<Expr *> Privates,
2267         ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
2268         ArrayRef<Expr *> ReductionOps, ArrayRef<Expr *> TaskgroupDescriptors,
2269         Stmt *PreInit, Expr *PostUpdate);
2270
2271  /// Creates an empty clause with the place for \a N variables.
2272  ///
2273  /// \param C AST context.
2274  /// \param N The number of variables.
2275  ///
2276  static OMPInReductionClause *CreateEmpty(const ASTContext &C, unsigned N);
2277
2278  /// Gets location of ':' symbol in clause.
2279  SourceLocation getColonLoc() const { return ColonLoc; }
2280  /// Gets the name info for specified reduction identifier.
2281  const DeclarationNameInfo &getNameInfo() const { return NameInfo; }
2282  /// Gets the nested name specifier.
2283  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2284
2285  typedef MutableArrayRef<Expr *>::iterator helper_expr_iterator;
2286  typedef ArrayRef<const Expr *>::iterator helper_expr_const_iterator;
2287  typedef llvm::iterator_range<helper_expr_iterator> helper_expr_range;
2288  typedef llvm::iterator_range<helper_expr_const_iterator>
2289      helper_expr_const_range;
2290
2291  helper_expr_const_range privates() const {
2292    return helper_expr_const_range(getPrivates().begin(), getPrivates().end());
2293  }
2294  helper_expr_range privates() {
2295    return helper_expr_range(getPrivates().begin(), getPrivates().end());
2296  }
2297  helper_expr_const_range lhs_exprs() const {
2298    return helper_expr_const_range(getLHSExprs().begin(), getLHSExprs().end());
2299  }
2300  helper_expr_range lhs_exprs() {
2301    return helper_expr_range(getLHSExprs().begin(), getLHSExprs().end());
2302  }
2303  helper_expr_const_range rhs_exprs() const {
2304    return helper_expr_const_range(getRHSExprs().begin(), getRHSExprs().end());
2305  }
2306  helper_expr_range rhs_exprs() {
2307    return helper_expr_range(getRHSExprs().begin(), getRHSExprs().end());
2308  }
2309  helper_expr_const_range reduction_ops() const {
2310    return helper_expr_const_range(getReductionOps().begin(),
2311                                   getReductionOps().end());
2312  }
2313  helper_expr_range reduction_ops() {
2314    return helper_expr_range(getReductionOps().begin(),
2315                             getReductionOps().end());
2316  }
2317  helper_expr_const_range taskgroup_descriptors() const {
2318    return helper_expr_const_range(getTaskgroupDescriptors().begin(),
2319                                   getTaskgroupDescriptors().end());
2320  }
2321  helper_expr_range taskgroup_descriptors() {
2322    return helper_expr_range(getTaskgroupDescriptors().begin(),
2323                             getTaskgroupDescriptors().end());
2324  }
2325
2326  child_range children() {
2327    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2328                       reinterpret_cast<Stmt **>(varlist_end()));
2329  }
2330
2331  static bool classof(const OMPClause *T) {
2332    return T->getClauseKind() == OMPC_in_reduction;
2333  }
2334};
2335
2336/// \brief This represents clause 'linear' in the '#pragma omp ...'
2337/// directives.
2338///
2339/// \code
2340/// #pragma omp simd linear(a,b : 2)
2341/// \endcode
2342/// In this example directive '#pragma omp simd' has clause 'linear'
2343/// with variables 'a', 'b' and linear step '2'.
2344///
2345class OMPLinearClause final
2346    : public OMPVarListClause<OMPLinearClause>,
2347      public OMPClauseWithPostUpdate,
2348      private llvm::TrailingObjects<OMPLinearClause, Expr *> {
2349  friend TrailingObjects;
2350  friend OMPVarListClause;
2351  friend class OMPClauseReader;
2352  /// \brief Modifier of 'linear' clause.
2353  OpenMPLinearClauseKind Modifier;
2354  /// \brief Location of linear modifier if any.
2355  SourceLocation ModifierLoc;
2356  /// \brief Location of ':'.
2357  SourceLocation ColonLoc;
2358
2359  /// \brief Sets the linear step for clause.
2360  void setStep(Expr *Step) { *(getFinals().end()) = Step; }
2361
2362  /// \brief Sets the expression to calculate linear step for clause.
2363  void setCalcStep(Expr *CalcStep) { *(getFinals().end() + 1) = CalcStep; }
2364
2365  /// \brief Build 'linear' clause with given number of variables \a NumVars.
2366  ///
2367  /// \param StartLoc Starting location of the clause.
2368  /// \param LParenLoc Location of '('.
2369  /// \param ColonLoc Location of ':'.
2370  /// \param EndLoc Ending location of the clause.
2371  /// \param NumVars Number of variables.
2372  ///
2373  OMPLinearClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2374                  OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
2375                  SourceLocation ColonLoc, SourceLocation EndLoc,
2376                  unsigned NumVars)
2377      : OMPVarListClause<OMPLinearClause>(OMPC_linear, StartLoc, LParenLoc,
2378                                          EndLoc, NumVars),
2379        OMPClauseWithPostUpdate(this), Modifier(Modifier),
2380        ModifierLoc(ModifierLoc), ColonLoc(ColonLoc) {}
2381
2382  /// \brief Build an empty clause.
2383  ///
2384  /// \param NumVars Number of variables.
2385  ///
2386  explicit OMPLinearClause(unsigned NumVars)
2387      : OMPVarListClause<OMPLinearClause>(OMPC_linear, SourceLocation(),
2388                                          SourceLocation(), SourceLocation(),
2389                                          NumVars),
2390        OMPClauseWithPostUpdate(this), Modifier(OMPC_LINEAR_val), ModifierLoc(),
2391        ColonLoc() {}
2392
2393  /// \brief Gets the list of initial values for linear variables.
2394  ///
2395  /// There are NumVars expressions with initial values allocated after the
2396  /// varlist, they are followed by NumVars update expressions (used to update
2397  /// the linear variable's value on current iteration) and they are followed by
2398  /// NumVars final expressions (used to calculate the linear variable's
2399  /// value after the loop body). After these lists, there are 2 helper
2400  /// expressions - linear step and a helper to calculate it before the
2401  /// loop body (used when the linear step is not constant):
2402  ///
2403  /// { Vars[] /* in OMPVarListClause */; Privates[]; Inits[]; Updates[];
2404  /// Finals[]; Step; CalcStep; }
2405  ///
2406  MutableArrayRef<Expr *> getPrivates() {
2407    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2408  }
2409  ArrayRef<const Expr *> getPrivates() const {
2410    return llvm::makeArrayRef(varlist_end(), varlist_size());
2411  }
2412
2413  MutableArrayRef<Expr *> getInits() {
2414    return MutableArrayRef<Expr *>(getPrivates().end(), varlist_size());
2415  }
2416  ArrayRef<const Expr *> getInits() const {
2417    return llvm::makeArrayRef(getPrivates().end(), varlist_size());
2418  }
2419
2420  /// \brief Sets the list of update expressions for linear variables.
2421  MutableArrayRef<Expr *> getUpdates() {
2422    return MutableArrayRef<Expr *>(getInits().end(), varlist_size());
2423  }
2424  ArrayRef<const Expr *> getUpdates() const {
2425    return llvm::makeArrayRef(getInits().end(), varlist_size());
2426  }
2427
2428  /// \brief Sets the list of final update expressions for linear variables.
2429  MutableArrayRef<Expr *> getFinals() {
2430    return MutableArrayRef<Expr *>(getUpdates().end(), varlist_size());
2431  }
2432  ArrayRef<const Expr *> getFinals() const {
2433    return llvm::makeArrayRef(getUpdates().end(), varlist_size());
2434  }
2435
2436  /// \brief Sets the list of the copies of original linear variables.
2437  /// \param PL List of expressions.
2438  void setPrivates(ArrayRef<Expr *> PL);
2439
2440  /// \brief Sets the list of the initial values for linear variables.
2441  /// \param IL List of expressions.
2442  void setInits(ArrayRef<Expr *> IL);
2443
2444public:
2445  /// \brief Creates clause with a list of variables \a VL and a linear step
2446  /// \a Step.
2447  ///
2448  /// \param C AST Context.
2449  /// \param StartLoc Starting location of the clause.
2450  /// \param LParenLoc Location of '('.
2451  /// \param Modifier Modifier of 'linear' clause.
2452  /// \param ModifierLoc Modifier location.
2453  /// \param ColonLoc Location of ':'.
2454  /// \param EndLoc Ending location of the clause.
2455  /// \param VL List of references to the variables.
2456  /// \param PL List of private copies of original variables.
2457  /// \param IL List of initial values for the variables.
2458  /// \param Step Linear step.
2459  /// \param CalcStep Calculation of the linear step.
2460  /// \param PreInit Statement that must be executed before entering the OpenMP
2461  /// region with this clause.
2462  /// \param PostUpdate Expression that must be executed after exit from the
2463  /// OpenMP region with this clause.
2464  static OMPLinearClause *
2465  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2466         OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
2467         SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
2468         ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
2469         Stmt *PreInit, Expr *PostUpdate);
2470
2471  /// \brief Creates an empty clause with the place for \a NumVars variables.
2472  ///
2473  /// \param C AST context.
2474  /// \param NumVars Number of variables.
2475  ///
2476  static OMPLinearClause *CreateEmpty(const ASTContext &C, unsigned NumVars);
2477
2478  /// \brief Set modifier.
2479  void setModifier(OpenMPLinearClauseKind Kind) { Modifier = Kind; }
2480  /// \brief Return modifier.
2481  OpenMPLinearClauseKind getModifier() const { return Modifier; }
2482
2483  /// \brief Set modifier location.
2484  void setModifierLoc(SourceLocation Loc) { ModifierLoc = Loc; }
2485  /// \brief Return modifier location.
2486  SourceLocation getModifierLoc() const { return ModifierLoc; }
2487
2488  /// \brief Sets the location of ':'.
2489  void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
2490  /// \brief Returns the location of ':'.
2491  SourceLocation getColonLoc() const { return ColonLoc; }
2492
2493  /// \brief Returns linear step.
2494  Expr *getStep() { return *(getFinals().end()); }
2495  /// \brief Returns linear step.
2496  const Expr *getStep() const { return *(getFinals().end()); }
2497  /// \brief Returns expression to calculate linear step.
2498  Expr *getCalcStep() { return *(getFinals().end() + 1); }
2499  /// \brief Returns expression to calculate linear step.
2500  const Expr *getCalcStep() const { return *(getFinals().end() + 1); }
2501
2502  /// \brief Sets the list of update expressions for linear variables.
2503  /// \param UL List of expressions.
2504  void setUpdates(ArrayRef<Expr *> UL);
2505
2506  /// \brief Sets the list of final update expressions for linear variables.
2507  /// \param FL List of expressions.
2508  void setFinals(ArrayRef<Expr *> FL);
2509
2510  typedef MutableArrayRef<Expr *>::iterator privates_iterator;
2511  typedef ArrayRef<const Expr *>::iterator privates_const_iterator;
2512  typedef llvm::iterator_range<privates_iterator> privates_range;
2513  typedef llvm::iterator_range<privates_const_iterator> privates_const_range;
2514
2515  privates_range privates() {
2516    return privates_range(getPrivates().begin(), getPrivates().end());
2517  }
2518  privates_const_range privates() const {
2519    return privates_const_range(getPrivates().begin(), getPrivates().end());
2520  }
2521
2522  typedef MutableArrayRef<Expr *>::iterator inits_iterator;
2523  typedef ArrayRef<const Expr *>::iterator inits_const_iterator;
2524  typedef llvm::iterator_range<inits_iterator> inits_range;
2525  typedef llvm::iterator_range<inits_const_iterator> inits_const_range;
2526
2527  inits_range inits() {
2528    return inits_range(getInits().begin(), getInits().end());
2529  }
2530  inits_const_range inits() const {
2531    return inits_const_range(getInits().begin(), getInits().end());
2532  }
2533
2534  typedef MutableArrayRef<Expr *>::iterator updates_iterator;
2535  typedef ArrayRef<const Expr *>::iterator updates_const_iterator;
2536  typedef llvm::iterator_range<updates_iterator> updates_range;
2537  typedef llvm::iterator_range<updates_const_iterator> updates_const_range;
2538
2539  updates_range updates() {
2540    return updates_range(getUpdates().begin(), getUpdates().end());
2541  }
2542  updates_const_range updates() const {
2543    return updates_const_range(getUpdates().begin(), getUpdates().end());
2544  }
2545
2546  typedef MutableArrayRef<Expr *>::iterator finals_iterator;
2547  typedef ArrayRef<const Expr *>::iterator finals_const_iterator;
2548  typedef llvm::iterator_range<finals_iterator> finals_range;
2549  typedef llvm::iterator_range<finals_const_iterator> finals_const_range;
2550
2551  finals_range finals() {
2552    return finals_range(getFinals().begin(), getFinals().end());
2553  }
2554  finals_const_range finals() const {
2555    return finals_const_range(getFinals().begin(), getFinals().end());
2556  }
2557
2558  child_range children() {
2559    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2560                       reinterpret_cast<Stmt **>(varlist_end()));
2561  }
2562
2563  static bool classof(const OMPClause *T) {
2564    return T->getClauseKind() == OMPC_linear;
2565  }
2566};
2567
2568/// \brief This represents clause 'aligned' in the '#pragma omp ...'
2569/// directives.
2570///
2571/// \code
2572/// #pragma omp simd aligned(a,b : 8)
2573/// \endcode
2574/// In this example directive '#pragma omp simd' has clause 'aligned'
2575/// with variables 'a', 'b' and alignment '8'.
2576///
2577class OMPAlignedClause final
2578    : public OMPVarListClause<OMPAlignedClause>,
2579      private llvm::TrailingObjects<OMPAlignedClause, Expr *> {
2580  friend TrailingObjects;
2581  friend OMPVarListClause;
2582  friend class OMPClauseReader;
2583  /// \brief Location of ':'.
2584  SourceLocation ColonLoc;
2585
2586  /// \brief Sets the alignment for clause.
2587  void setAlignment(Expr *A) { *varlist_end() = A; }
2588
2589  /// \brief Build 'aligned' clause with given number of variables \a NumVars.
2590  ///
2591  /// \param StartLoc Starting location of the clause.
2592  /// \param LParenLoc Location of '('.
2593  /// \param ColonLoc Location of ':'.
2594  /// \param EndLoc Ending location of the clause.
2595  /// \param NumVars Number of variables.
2596  ///
2597  OMPAlignedClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2598                   SourceLocation ColonLoc, SourceLocation EndLoc,
2599                   unsigned NumVars)
2600      : OMPVarListClause<OMPAlignedClause>(OMPC_aligned, StartLoc, LParenLoc,
2601                                           EndLoc, NumVars),
2602        ColonLoc(ColonLoc) {}
2603
2604  /// \brief Build an empty clause.
2605  ///
2606  /// \param NumVars Number of variables.
2607  ///
2608  explicit OMPAlignedClause(unsigned NumVars)
2609      : OMPVarListClause<OMPAlignedClause>(OMPC_aligned, SourceLocation(),
2610                                           SourceLocation(), SourceLocation(),
2611                                           NumVars),
2612        ColonLoc(SourceLocation()) {}
2613
2614public:
2615  /// \brief Creates clause with a list of variables \a VL and alignment \a A.
2616  ///
2617  /// \param C AST Context.
2618  /// \param StartLoc Starting location of the clause.
2619  /// \param LParenLoc Location of '('.
2620  /// \param ColonLoc Location of ':'.
2621  /// \param EndLoc Ending location of the clause.
2622  /// \param VL List of references to the variables.
2623  /// \param A Alignment.
2624  static OMPAlignedClause *Create(const ASTContext &C, SourceLocation StartLoc,
2625                                  SourceLocation LParenLoc,
2626                                  SourceLocation ColonLoc,
2627                                  SourceLocation EndLoc, ArrayRef<Expr *> VL,
2628                                  Expr *A);
2629
2630  /// \brief Creates an empty clause with the place for \a NumVars variables.
2631  ///
2632  /// \param C AST context.
2633  /// \param NumVars Number of variables.
2634  ///
2635  static OMPAlignedClause *CreateEmpty(const ASTContext &C, unsigned NumVars);
2636
2637  /// \brief Sets the location of ':'.
2638  void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
2639  /// \brief Returns the location of ':'.
2640  SourceLocation getColonLoc() const { return ColonLoc; }
2641
2642  /// \brief Returns alignment.
2643  Expr *getAlignment() { return *varlist_end(); }
2644  /// \brief Returns alignment.
2645  const Expr *getAlignment() const { return *varlist_end(); }
2646
2647  child_range children() {
2648    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2649                       reinterpret_cast<Stmt **>(varlist_end()));
2650  }
2651
2652  static bool classof(const OMPClause *T) {
2653    return T->getClauseKind() == OMPC_aligned;
2654  }
2655};
2656
2657/// \brief This represents clause 'copyin' in the '#pragma omp ...' directives.
2658///
2659/// \code
2660/// #pragma omp parallel copyin(a,b)
2661/// \endcode
2662/// In this example directive '#pragma omp parallel' has clause 'copyin'
2663/// with the variables 'a' and 'b'.
2664///
2665class OMPCopyinClause final
2666    : public OMPVarListClause<OMPCopyinClause>,
2667      private llvm::TrailingObjects<OMPCopyinClause, Expr *> {
2668  // Class has 3 additional tail allocated arrays:
2669  // 1. List of helper expressions for proper generation of assignment operation
2670  // required for copyin clause. This list represents sources.
2671  // 2. List of helper expressions for proper generation of assignment operation
2672  // required for copyin clause. This list represents destinations.
2673  // 3. List of helper expressions that represents assignment operation:
2674  // \code
2675  // DstExprs = SrcExprs;
2676  // \endcode
2677  // Required for proper codegen of propagation of master's thread values of
2678  // threadprivate variables to local instances of that variables in other
2679  // implicit threads.
2680
2681  friend TrailingObjects;
2682  friend OMPVarListClause;
2683  friend class OMPClauseReader;
2684  /// \brief Build clause with number of variables \a N.
2685  ///
2686  /// \param StartLoc Starting location of the clause.
2687  /// \param LParenLoc Location of '('.
2688  /// \param EndLoc Ending location of the clause.
2689  /// \param N Number of the variables in the clause.
2690  ///
2691  OMPCopyinClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2692                  SourceLocation EndLoc, unsigned N)
2693      : OMPVarListClause<OMPCopyinClause>(OMPC_copyin, StartLoc, LParenLoc,
2694                                          EndLoc, N) {}
2695
2696  /// \brief Build an empty clause.
2697  ///
2698  /// \param N Number of variables.
2699  ///
2700  explicit OMPCopyinClause(unsigned N)
2701      : OMPVarListClause<OMPCopyinClause>(OMPC_copyin, SourceLocation(),
2702                                          SourceLocation(), SourceLocation(),
2703                                          N) {}
2704
2705  /// \brief Set list of helper expressions, required for proper codegen of the
2706  /// clause. These expressions represent source expression in the final
2707  /// assignment statement performed by the copyin clause.
2708  void setSourceExprs(ArrayRef<Expr *> SrcExprs);
2709
2710  /// \brief Get the list of helper source expressions.
2711  MutableArrayRef<Expr *> getSourceExprs() {
2712    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2713  }
2714  ArrayRef<const Expr *> getSourceExprs() const {
2715    return llvm::makeArrayRef(varlist_end(), varlist_size());
2716  }
2717
2718  /// \brief Set list of helper expressions, required for proper codegen of the
2719  /// clause. These expressions represent destination expression in the final
2720  /// assignment statement performed by the copyin clause.
2721  void setDestinationExprs(ArrayRef<Expr *> DstExprs);
2722
2723  /// \brief Get the list of helper destination expressions.
2724  MutableArrayRef<Expr *> getDestinationExprs() {
2725    return MutableArrayRef<Expr *>(getSourceExprs().end(), varlist_size());
2726  }
2727  ArrayRef<const Expr *> getDestinationExprs() const {
2728    return llvm::makeArrayRef(getSourceExprs().end(), varlist_size());
2729  }
2730
2731  /// \brief Set list of helper assignment expressions, required for proper
2732  /// codegen of the clause. These expressions are assignment expressions that
2733  /// assign source helper expressions to destination helper expressions
2734  /// correspondingly.
2735  void setAssignmentOps(ArrayRef<Expr *> AssignmentOps);
2736
2737  /// \brief Get the list of helper assignment expressions.
2738  MutableArrayRef<Expr *> getAssignmentOps() {
2739    return MutableArrayRef<Expr *>(getDestinationExprs().end(), varlist_size());
2740  }
2741  ArrayRef<const Expr *> getAssignmentOps() const {
2742    return llvm::makeArrayRef(getDestinationExprs().end(), varlist_size());
2743  }
2744
2745public:
2746  /// \brief Creates clause with a list of variables \a VL.
2747  ///
2748  /// \param C AST context.
2749  /// \param StartLoc Starting location of the clause.
2750  /// \param LParenLoc Location of '('.
2751  /// \param EndLoc Ending location of the clause.
2752  /// \param VL List of references to the variables.
2753  /// \param SrcExprs List of helper expressions for proper generation of
2754  /// assignment operation required for copyin clause. This list represents
2755  /// sources.
2756  /// \param DstExprs List of helper expressions for proper generation of
2757  /// assignment operation required for copyin clause. This list represents
2758  /// destinations.
2759  /// \param AssignmentOps List of helper expressions that represents assignment
2760  /// operation:
2761  /// \code
2762  /// DstExprs = SrcExprs;
2763  /// \endcode
2764  /// Required for proper codegen of propagation of master's thread values of
2765  /// threadprivate variables to local instances of that variables in other
2766  /// implicit threads.
2767  ///
2768  static OMPCopyinClause *
2769  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2770         SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
2771         ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps);
2772  /// \brief Creates an empty clause with \a N variables.
2773  ///
2774  /// \param C AST context.
2775  /// \param N The number of variables.
2776  ///
2777  static OMPCopyinClause *CreateEmpty(const ASTContext &C, unsigned N);
2778
2779  typedef MutableArrayRef<Expr *>::iterator helper_expr_iterator;
2780  typedef ArrayRef<const Expr *>::iterator helper_expr_const_iterator;
2781  typedef llvm::iterator_range<helper_expr_iterator> helper_expr_range;
2782  typedef llvm::iterator_range<helper_expr_const_iterator>
2783      helper_expr_const_range;
2784
2785  helper_expr_const_range source_exprs() const {
2786    return helper_expr_const_range(getSourceExprs().begin(),
2787                                   getSourceExprs().end());
2788  }
2789  helper_expr_range source_exprs() {
2790    return helper_expr_range(getSourceExprs().begin(), getSourceExprs().end());
2791  }
2792  helper_expr_const_range destination_exprs() const {
2793    return helper_expr_const_range(getDestinationExprs().begin(),
2794                                   getDestinationExprs().end());
2795  }
2796  helper_expr_range destination_exprs() {
2797    return helper_expr_range(getDestinationExprs().begin(),
2798                             getDestinationExprs().end());
2799  }
2800  helper_expr_const_range assignment_ops() const {
2801    return helper_expr_const_range(getAssignmentOps().begin(),
2802                                   getAssignmentOps().end());
2803  }
2804  helper_expr_range assignment_ops() {
2805    return helper_expr_range(getAssignmentOps().begin(),
2806                             getAssignmentOps().end());
2807  }
2808
2809  child_range children() {
2810    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2811                       reinterpret_cast<Stmt **>(varlist_end()));
2812  }
2813
2814  static bool classof(const OMPClause *T) {
2815    return T->getClauseKind() == OMPC_copyin;
2816  }
2817};
2818
2819/// \brief This represents clause 'copyprivate' in the '#pragma omp ...'
2820/// directives.
2821///
2822/// \code
2823/// #pragma omp single copyprivate(a,b)
2824/// \endcode
2825/// In this example directive '#pragma omp single' has clause 'copyprivate'
2826/// with the variables 'a' and 'b'.
2827///
2828class OMPCopyprivateClause final
2829    : public OMPVarListClause<OMPCopyprivateClause>,
2830      private llvm::TrailingObjects<OMPCopyprivateClause, Expr *> {
2831  friend TrailingObjects;
2832  friend OMPVarListClause;
2833  friend class OMPClauseReader;
2834  /// \brief Build clause with number of variables \a N.
2835  ///
2836  /// \param StartLoc Starting location of the clause.
2837  /// \param LParenLoc Location of '('.
2838  /// \param EndLoc Ending location of the clause.
2839  /// \param N Number of the variables in the clause.
2840  ///
2841  OMPCopyprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2842                       SourceLocation EndLoc, unsigned N)
2843      : OMPVarListClause<OMPCopyprivateClause>(OMPC_copyprivate, StartLoc,
2844                                               LParenLoc, EndLoc, N) {}
2845
2846  /// \brief Build an empty clause.
2847  ///
2848  /// \param N Number of variables.
2849  ///
2850  explicit OMPCopyprivateClause(unsigned N)
2851      : OMPVarListClause<OMPCopyprivateClause>(
2852            OMPC_copyprivate, SourceLocation(), SourceLocation(),
2853            SourceLocation(), N) {}
2854
2855  /// \brief Set list of helper expressions, required for proper codegen of the
2856  /// clause. These expressions represent source expression in the final
2857  /// assignment statement performed by the copyprivate clause.
2858  void setSourceExprs(ArrayRef<Expr *> SrcExprs);
2859
2860  /// \brief Get the list of helper source expressions.
2861  MutableArrayRef<Expr *> getSourceExprs() {
2862    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
2863  }
2864  ArrayRef<const Expr *> getSourceExprs() const {
2865    return llvm::makeArrayRef(varlist_end(), varlist_size());
2866  }
2867
2868  /// \brief Set list of helper expressions, required for proper codegen of the
2869  /// clause. These expressions represent destination expression in the final
2870  /// assignment statement performed by the copyprivate clause.
2871  void setDestinationExprs(ArrayRef<Expr *> DstExprs);
2872
2873  /// \brief Get the list of helper destination expressions.
2874  MutableArrayRef<Expr *> getDestinationExprs() {
2875    return MutableArrayRef<Expr *>(getSourceExprs().end(), varlist_size());
2876  }
2877  ArrayRef<const Expr *> getDestinationExprs() const {
2878    return llvm::makeArrayRef(getSourceExprs().end(), varlist_size());
2879  }
2880
2881  /// \brief Set list of helper assignment expressions, required for proper
2882  /// codegen of the clause. These expressions are assignment expressions that
2883  /// assign source helper expressions to destination helper expressions
2884  /// correspondingly.
2885  void setAssignmentOps(ArrayRef<Expr *> AssignmentOps);
2886
2887  /// \brief Get the list of helper assignment expressions.
2888  MutableArrayRef<Expr *> getAssignmentOps() {
2889    return MutableArrayRef<Expr *>(getDestinationExprs().end(), varlist_size());
2890  }
2891  ArrayRef<const Expr *> getAssignmentOps() const {
2892    return llvm::makeArrayRef(getDestinationExprs().end(), varlist_size());
2893  }
2894
2895public:
2896  /// \brief Creates clause with a list of variables \a VL.
2897  ///
2898  /// \param C AST context.
2899  /// \param StartLoc Starting location of the clause.
2900  /// \param LParenLoc Location of '('.
2901  /// \param EndLoc Ending location of the clause.
2902  /// \param VL List of references to the variables.
2903  /// \param SrcExprs List of helper expressions for proper generation of
2904  /// assignment operation required for copyprivate clause. This list represents
2905  /// sources.
2906  /// \param DstExprs List of helper expressions for proper generation of
2907  /// assignment operation required for copyprivate clause. This list represents
2908  /// destinations.
2909  /// \param AssignmentOps List of helper expressions that represents assignment
2910  /// operation:
2911  /// \code
2912  /// DstExprs = SrcExprs;
2913  /// \endcode
2914  /// Required for proper codegen of final assignment performed by the
2915  /// copyprivate clause.
2916  ///
2917  static OMPCopyprivateClause *
2918  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
2919         SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
2920         ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps);
2921  /// \brief Creates an empty clause with \a N variables.
2922  ///
2923  /// \param C AST context.
2924  /// \param N The number of variables.
2925  ///
2926  static OMPCopyprivateClause *CreateEmpty(const ASTContext &C, unsigned N);
2927
2928  typedef MutableArrayRef<Expr *>::iterator helper_expr_iterator;
2929  typedef ArrayRef<const Expr *>::iterator helper_expr_const_iterator;
2930  typedef llvm::iterator_range<helper_expr_iterator> helper_expr_range;
2931  typedef llvm::iterator_range<helper_expr_const_iterator>
2932      helper_expr_const_range;
2933
2934  helper_expr_const_range source_exprs() const {
2935    return helper_expr_const_range(getSourceExprs().begin(),
2936                                   getSourceExprs().end());
2937  }
2938  helper_expr_range source_exprs() {
2939    return helper_expr_range(getSourceExprs().begin(), getSourceExprs().end());
2940  }
2941  helper_expr_const_range destination_exprs() const {
2942    return helper_expr_const_range(getDestinationExprs().begin(),
2943                                   getDestinationExprs().end());
2944  }
2945  helper_expr_range destination_exprs() {
2946    return helper_expr_range(getDestinationExprs().begin(),
2947                             getDestinationExprs().end());
2948  }
2949  helper_expr_const_range assignment_ops() const {
2950    return helper_expr_const_range(getAssignmentOps().begin(),
2951                                   getAssignmentOps().end());
2952  }
2953  helper_expr_range assignment_ops() {
2954    return helper_expr_range(getAssignmentOps().begin(),
2955                             getAssignmentOps().end());
2956  }
2957
2958  child_range children() {
2959    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
2960                       reinterpret_cast<Stmt **>(varlist_end()));
2961  }
2962
2963  static bool classof(const OMPClause *T) {
2964    return T->getClauseKind() == OMPC_copyprivate;
2965  }
2966};
2967
2968/// \brief This represents implicit clause 'flush' for the '#pragma omp flush'
2969/// directive.
2970/// This clause does not exist by itself, it can be only as a part of 'omp
2971/// flush' directive. This clause is introduced to keep the original structure
2972/// of \a OMPExecutableDirective class and its derivatives and to use the
2973/// existing infrastructure of clauses with the list of variables.
2974///
2975/// \code
2976/// #pragma omp flush(a,b)
2977/// \endcode
2978/// In this example directive '#pragma omp flush' has implicit clause 'flush'
2979/// with the variables 'a' and 'b'.
2980///
2981class OMPFlushClause final
2982    : public OMPVarListClause<OMPFlushClause>,
2983      private llvm::TrailingObjects<OMPFlushClause, Expr *> {
2984  friend TrailingObjects;
2985  friend OMPVarListClause;
2986  /// \brief Build clause with number of variables \a N.
2987  ///
2988  /// \param StartLoc Starting location of the clause.
2989  /// \param LParenLoc Location of '('.
2990  /// \param EndLoc Ending location of the clause.
2991  /// \param N Number of the variables in the clause.
2992  ///
2993  OMPFlushClause(SourceLocation StartLoc, SourceLocation LParenLoc,
2994                 SourceLocation EndLoc, unsigned N)
2995      : OMPVarListClause<OMPFlushClause>(OMPC_flush, StartLoc, LParenLoc,
2996                                         EndLoc, N) {}
2997
2998  /// \brief Build an empty clause.
2999  ///
3000  /// \param N Number of variables.
3001  ///
3002  explicit OMPFlushClause(unsigned N)
3003      : OMPVarListClause<OMPFlushClause>(OMPC_flush, SourceLocation(),
3004                                         SourceLocation(), SourceLocation(),
3005                                         N) {}
3006
3007public:
3008  /// \brief Creates clause with a list of variables \a VL.
3009  ///
3010  /// \param C AST context.
3011  /// \param StartLoc Starting location of the clause.
3012  /// \param LParenLoc Location of '('.
3013  /// \param EndLoc Ending location of the clause.
3014  /// \param VL List of references to the variables.
3015  ///
3016  static OMPFlushClause *Create(const ASTContext &C, SourceLocation StartLoc,
3017                                SourceLocation LParenLoc, SourceLocation EndLoc,
3018                                ArrayRef<Expr *> VL);
3019  /// \brief Creates an empty clause with \a N variables.
3020  ///
3021  /// \param C AST context.
3022  /// \param N The number of variables.
3023  ///
3024  static OMPFlushClause *CreateEmpty(const ASTContext &C, unsigned N);
3025
3026  child_range children() {
3027    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3028                       reinterpret_cast<Stmt **>(varlist_end()));
3029  }
3030
3031  static bool classof(const OMPClause *T) {
3032    return T->getClauseKind() == OMPC_flush;
3033  }
3034};
3035
3036/// \brief This represents implicit clause 'depend' for the '#pragma omp task'
3037/// directive.
3038///
3039/// \code
3040/// #pragma omp task depend(in:a,b)
3041/// \endcode
3042/// In this example directive '#pragma omp task' with clause 'depend' with the
3043/// variables 'a' and 'b' with dependency 'in'.
3044///
3045class OMPDependClause final
3046    : public OMPVarListClause<OMPDependClause>,
3047      private llvm::TrailingObjects<OMPDependClause, Expr *> {
3048  friend TrailingObjects;
3049  friend OMPVarListClause;
3050  friend class OMPClauseReader;
3051  /// \brief Dependency type (one of in, out, inout).
3052  OpenMPDependClauseKind DepKind;
3053  /// \brief Dependency type location.
3054  SourceLocation DepLoc;
3055  /// \brief Colon location.
3056  SourceLocation ColonLoc;
3057  /// \brief Build clause with number of variables \a N.
3058  ///
3059  /// \param StartLoc Starting location of the clause.
3060  /// \param LParenLoc Location of '('.
3061  /// \param EndLoc Ending location of the clause.
3062  /// \param N Number of the variables in the clause.
3063  ///
3064  OMPDependClause(SourceLocation StartLoc, SourceLocation LParenLoc,
3065                  SourceLocation EndLoc, unsigned N)
3066      : OMPVarListClause<OMPDependClause>(OMPC_depend, StartLoc, LParenLoc,
3067                                          EndLoc, N),
3068        DepKind(OMPC_DEPEND_unknown) {}
3069
3070  /// \brief Build an empty clause.
3071  ///
3072  /// \param N Number of variables.
3073  ///
3074  explicit OMPDependClause(unsigned N)
3075      : OMPVarListClause<OMPDependClause>(OMPC_depend, SourceLocation(),
3076                                          SourceLocation(), SourceLocation(),
3077                                          N),
3078        DepKind(OMPC_DEPEND_unknown) {}
3079  /// \brief Set dependency kind.
3080  void setDependencyKind(OpenMPDependClauseKind K) { DepKind = K; }
3081
3082  /// \brief Set dependency kind and its location.
3083  void setDependencyLoc(SourceLocation Loc) { DepLoc = Loc; }
3084
3085  /// \brief Set colon location.
3086  void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
3087
3088public:
3089  /// \brief Creates clause with a list of variables \a VL.
3090  ///
3091  /// \param C AST context.
3092  /// \param StartLoc Starting location of the clause.
3093  /// \param LParenLoc Location of '('.
3094  /// \param EndLoc Ending location of the clause.
3095  /// \param DepKind Dependency type.
3096  /// \param DepLoc Location of the dependency type.
3097  /// \param ColonLoc Colon location.
3098  /// \param VL List of references to the variables.
3099  static OMPDependClause *
3100  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
3101         SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
3102         SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL);
3103  /// \brief Creates an empty clause with \a N variables.
3104  ///
3105  /// \param C AST context.
3106  /// \param N The number of variables.
3107  ///
3108  static OMPDependClause *CreateEmpty(const ASTContext &C, unsigned N);
3109
3110  /// \brief Get dependency type.
3111  OpenMPDependClauseKind getDependencyKind() const { return DepKind; }
3112  /// \brief Get dependency type location.
3113  SourceLocation getDependencyLoc() const { return DepLoc; }
3114  /// \brief Get colon location.
3115  SourceLocation getColonLoc() const { return ColonLoc; }
3116
3117  /// Set the loop counter value for the depend clauses with 'sink|source' kind
3118  /// of dependency. Required for codegen.
3119  void setCounterValue(Expr *V);
3120  /// Get the loop counter value.
3121  Expr *getCounterValue();
3122  /// Get the loop counter value.
3123  const Expr *getCounterValue() const;
3124
3125  child_range children() {
3126    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
3127                       reinterpret_cast<Stmt **>(varlist_end()));
3128  }
3129
3130  static bool classof(const OMPClause *T) {
3131    return T->getClauseKind() == OMPC_depend;
3132  }
3133};
3134
3135/// \brief This represents 'device' clause in the '#pragma omp ...'
3136/// directive.
3137///
3138/// \code
3139/// #pragma omp target device(a)
3140/// \endcode
3141/// In this example directive '#pragma omp target' has clause 'device'
3142/// with single expression 'a'.
3143///
3144class OMPDeviceClause : public OMPClause, public OMPClauseWithPreInit {
3145  friend class OMPClauseReader;
3146  /// \brief Location of '('.
3147  SourceLocation LParenLoc;
3148  /// \brief Device number.
3149  Stmt *Device;
3150  /// \brief Set the device number.
3151  ///
3152  /// \param E Device number.
3153  ///
3154  void setDevice(Expr *E) { Device = E; }
3155
3156public:
3157  /// \brief Build 'device' clause.
3158  ///
3159  /// \param E Expression associated with this clause.
3160  /// \param StartLoc Starting location of the clause.
3161  /// \param LParenLoc Location of '('.
3162  /// \param EndLoc Ending location of the clause.
3163  ///
3164  OMPDeviceClause(Expr *E, Stmt *HelperE, SourceLocation StartLoc,
3165                  SourceLocation LParenLoc, SourceLocation EndLoc)
3166      : OMPClause(OMPC_device, StartLoc, EndLoc), OMPClauseWithPreInit(this),
3167        LParenLoc(LParenLoc), Device(E) {
3168    setPreInitStmt(HelperE);
3169  }
3170
3171  /// \brief Build an empty clause.
3172  ///
3173  OMPDeviceClause()
3174      : OMPClause(OMPC_device, SourceLocation(), SourceLocation()),
3175        OMPClauseWithPreInit(this), LParenLoc(SourceLocation()),
3176        Device(nullptr) {}
3177  /// \brief Sets the location of '('.
3178  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
3179  /// \brief Returns the location of '('.
3180  SourceLocation getLParenLoc() const { return LParenLoc; }
3181  /// \brief Return device number.
3182  Expr *getDevice() { return cast<Expr>(Device); }
3183  /// \brief Return device number.
3184  Expr *getDevice() const { return cast<Expr>(Device); }
3185
3186  static bool classof(const OMPClause *T) {
3187    return T->getClauseKind() == OMPC_device;
3188  }
3189
3190  child_range children() { return child_range(&Device, &Device + 1); }
3191};
3192
3193/// \brief This represents 'threads' clause in the '#pragma omp ...' directive.
3194///
3195/// \code
3196/// #pragma omp ordered threads
3197/// \endcode
3198/// In this example directive '#pragma omp ordered' has simple 'threads' clause.
3199///
3200class OMPThreadsClause : public OMPClause {
3201public:
3202  /// \brief Build 'threads' clause.
3203  ///
3204  /// \param StartLoc Starting location of the clause.
3205  /// \param EndLoc Ending location of the clause.
3206  ///
3207  OMPThreadsClause(SourceLocation StartLoc, SourceLocation EndLoc)
3208      : OMPClause(OMPC_threads, StartLoc, EndLoc) {}
3209
3210  /// \brief Build an empty clause.
3211  ///
3212  OMPThreadsClause()
3213      : OMPClause(OMPC_threads, SourceLocation(), SourceLocation()) {}
3214
3215  static bool classof(const OMPClause *T) {
3216    return T->getClauseKind() == OMPC_threads;
3217  }
3218
3219  child_range children() {
3220    return child_range(child_iterator(), child_iterator());
3221  }
3222};
3223
3224/// \brief This represents 'simd' clause in the '#pragma omp ...' directive.
3225///
3226/// \code
3227/// #pragma omp ordered simd
3228/// \endcode
3229/// In this example directive '#pragma omp ordered' has simple 'simd' clause.
3230///
3231class OMPSIMDClause : public OMPClause {
3232public:
3233  /// \brief Build 'simd' clause.
3234  ///
3235  /// \param StartLoc Starting location of the clause.
3236  /// \param EndLoc Ending location of the clause.
3237  ///
3238  OMPSIMDClause(SourceLocation StartLoc, SourceLocation EndLoc)
3239      : OMPClause(OMPC_simd, StartLoc, EndLoc) {}
3240
3241  /// \brief Build an empty clause.
3242  ///
3243  OMPSIMDClause() : OMPClause(OMPC_simd, SourceLocation(), SourceLocation()) {}
3244
3245  static bool classof(const OMPClause *T) {
3246    return T->getClauseKind() == OMPC_simd;
3247  }
3248
3249  child_range children() {
3250    return child_range(child_iterator(), child_iterator());
3251  }
3252};
3253
3254/// \brief Struct that defines common infrastructure to handle mappable
3255/// expressions used in OpenMP clauses.
3256class OMPClauseMappableExprCommon {
3257public:
3258  // \brief Class that represents a component of a mappable expression. E.g.
3259  // for an expression S.a, the first component is a declaration reference
3260  // expression associated with 'S' and the second is a member expression
3261  // associated with the field declaration 'a'. If the expression is an array
3262  // subscript it may not have any associated declaration. In that case the
3263  // associated declaration is set to nullptr.
3264  class MappableComponent {
3265    // \brief Expression associated with the component.
3266    Expr *AssociatedExpression = nullptr;
3267    // \brief Declaration associated with the declaration. If the component does
3268    // not have a declaration (e.g. array subscripts or section), this is set to
3269    // nullptr.
3270    ValueDecl *AssociatedDeclaration = nullptr;
3271
3272  public:
3273    explicit MappableComponent() {}
3274    explicit MappableComponent(Expr *AssociatedExpression,
3275                               ValueDecl *AssociatedDeclaration)
3276        : AssociatedExpression(AssociatedExpression),
3277          AssociatedDeclaration(
3278              AssociatedDeclaration
3279                  ? cast<ValueDecl>(AssociatedDeclaration->getCanonicalDecl())
3280                  : nullptr) {}
3281
3282    Expr *getAssociatedExpression() const { return AssociatedExpression; }
3283    ValueDecl *getAssociatedDeclaration() const {
3284      return AssociatedDeclaration;
3285    }
3286  };
3287
3288  // \brief List of components of an expression. This first one is the whole
3289  // expression and the last one is the base expression.
3290  typedef SmallVector<MappableComponent, 8> MappableExprComponentList;
3291  typedef ArrayRef<MappableComponent> MappableExprComponentListRef;
3292
3293  // \brief List of all component lists associated to the same base declaration.
3294  // E.g. if both 'S.a' and 'S.b' are a mappable expressions, each will have
3295  // their component list but the same base declaration 'S'.
3296  typedef SmallVector<MappableExprComponentList, 8> MappableExprComponentLists;
3297  typedef ArrayRef<MappableExprComponentList> MappableExprComponentListsRef;
3298
3299protected:
3300  // \brief Return the total number of elements in a list of component lists.
3301  static unsigned
3302  getComponentsTotalNumber(MappableExprComponentListsRef ComponentLists);
3303
3304  // \brief Return the total number of elements in a list of declarations. All
3305  // declarations are expected to be canonical.
3306  static unsigned
3307  getUniqueDeclarationsTotalNumber(ArrayRef<ValueDecl *> Declarations);
3308};
3309
3310/// \brief This represents clauses with a list of expressions that are mappable.
3311/// Examples of these clauses are 'map' in
3312/// '#pragma omp target [enter|exit] [data]...' directives, and  'to' and 'from
3313/// in '#pragma omp target update...' directives.
3314template <class T>
3315class OMPMappableExprListClause : public OMPVarListClause<T>,
3316                                  public OMPClauseMappableExprCommon {
3317  friend class OMPClauseReader;
3318
3319  /// \brief Number of unique declarations in this clause.
3320  unsigned NumUniqueDeclarations;
3321
3322  /// \brief Number of component lists in this clause.
3323  unsigned NumComponentLists;
3324
3325  /// \brief Total number of components in this clause.
3326  unsigned NumComponents;
3327
3328protected:
3329  /// \brief Get the unique declarations that are in the trailing objects of the
3330  /// class.
3331  MutableArrayRef<ValueDecl *> getUniqueDeclsRef() {
3332    return MutableArrayRef<ValueDecl *>(
3333        static_cast<T *>(this)->template getTrailingObjects<ValueDecl *>(),
3334        NumUniqueDeclarations);
3335  }
3336
3337  /// \brief Get the unique declarations that are in the trailing objects of the
3338  /// class.
3339  ArrayRef<ValueDecl *> getUniqueDeclsRef() const {
3340    return ArrayRef<ValueDecl *>(
3341        static_cast<const T *>(this)
3342            ->template getTrailingObjects<ValueDecl *>(),
3343        NumUniqueDeclarations);
3344  }
3345
3346  /// \brief Set the unique declarations that are in the trailing objects of the
3347  /// class.
3348  void setUniqueDecls(ArrayRef<ValueDecl *> UDs) {
3349    assert(UDs.size() == NumUniqueDeclarations &&
3350           "Unexpected amount of unique declarations.");
3351    std::copy(UDs.begin(), UDs.end(), getUniqueDeclsRef().begin());
3352  }
3353
3354  /// \brief Get the number of lists per declaration that are in the trailing
3355  /// objects of the class.
3356  MutableArrayRef<unsigned> getDeclNumListsRef() {
3357    return MutableArrayRef<unsigned>(
3358        static_cast<T *>(this)->template getTrailingObjects<unsigned>(),
3359        NumUniqueDeclarations);
3360  }
3361
3362  /// \brief Get the number of lists per declaration that are in the trailing
3363  /// objects of the class.
3364  ArrayRef<unsigned> getDeclNumListsRef() const {
3365    return ArrayRef<unsigned>(
3366        static_cast<const T *>(this)->template getTrailingObjects<unsigned>(),
3367        NumUniqueDeclarations);
3368  }
3369
3370  /// \brief Set the number of lists per declaration that are in the trailing
3371  /// objects of the class.
3372  void setDeclNumLists(ArrayRef<unsigned> DNLs) {
3373    assert(DNLs.size() == NumUniqueDeclarations &&
3374           "Unexpected amount of list numbers.");
3375    std::copy(DNLs.begin(), DNLs.end(), getDeclNumListsRef().begin());
3376  }
3377
3378  /// \brief Get the cumulative component lists sizes that are in the trailing
3379  /// objects of the class. They are appended after the number of lists.
3380  MutableArrayRef<unsigned> getComponentListSizesRef() {
3381    return MutableArrayRef<unsigned>(
3382        static_cast<T *>(this)->template getTrailingObjects<unsigned>() +
3383            NumUniqueDeclarations,
3384        NumComponentLists);
3385  }
3386
3387  /// \brief Get the cumulative component lists sizes that are in the trailing
3388  /// objects of the class. They are appended after the number of lists.
3389  ArrayRef<unsigned> getComponentListSizesRef() const {
3390    return ArrayRef<unsigned>(
3391        static_cast<const T *>(this)->template getTrailingObjects<unsigned>() +
3392            NumUniqueDeclarations,
3393        NumComponentLists);
3394  }
3395
3396  /// \brief Set the cumulative component lists sizes that are in the trailing
3397  /// objects of the class.
3398  void setComponentListSizes(ArrayRef<unsigned> CLSs) {
3399    assert(CLSs.size() == NumComponentLists &&
3400           "Unexpected amount of component lists.");
3401    std::copy(CLSs.begin(), CLSs.end(), getComponentListSizesRef().begin());
3402  }
3403
3404  /// \brief Get the components that are in the trailing objects of the class.
3405  MutableArrayRef<MappableComponent> getComponentsRef() {
3406    return MutableArrayRef<MappableComponent>(
3407        static_cast<T *>(this)
3408            ->template getTrailingObjects<MappableComponent>(),
3409        NumComponents);
3410  }
3411
3412  /// \brief Get the components that are in the trailing objects of the class.
3413  ArrayRef<MappableComponent> getComponentsRef() const {
3414    return ArrayRef<MappableComponent>(
3415        static_cast<const T *>(this)
3416            ->template getTrailingObjects<MappableComponent>(),
3417        NumComponents);
3418  }
3419
3420  /// \brief Set the components that are in the trailing objects of the class.
3421  /// This requires the list sizes so that it can also fill the original
3422  /// expressions, which are the first component of each list.
3423  void setComponents(ArrayRef<MappableComponent> Components,
3424                     ArrayRef<unsigned> CLSs) {
3425    assert(Components.size() == NumComponents &&
3426           "Unexpected amount of component lists.");
3427    assert(CLSs.size() == NumComponentLists &&
3428           "Unexpected amount of list sizes.");
3429    std::copy(Components.begin(), Components.end(), getComponentsRef().begin());
3430  }
3431
3432  /// \brief Fill the clause information from the list of declarations and
3433  /// associated component lists.
3434  void setClauseInfo(ArrayRef<ValueDecl *> Declarations,
3435                     MappableExprComponentListsRef ComponentLists) {
3436    // Perform some checks to make sure the data sizes are consistent with the
3437    // information available when the clause was created.
3438    assert(getUniqueDeclarationsTotalNumber(Declarations) ==
3439               NumUniqueDeclarations &&
3440           "Unexpected number of mappable expression info entries!");
3441    assert(getComponentsTotalNumber(ComponentLists) == NumComponents &&
3442           "Unexpected total number of components!");
3443    assert(Declarations.size() == ComponentLists.size() &&
3444           "Declaration and component lists size is not consistent!");
3445    assert(Declarations.size() == NumComponentLists &&
3446           "Unexpected declaration and component lists size!");
3447
3448    // Organize the components by declaration and retrieve the original
3449    // expression. Original expressions are always the first component of the
3450    // mappable component list.
3451    llvm::MapVector<ValueDecl *, SmallVector<MappableExprComponentListRef, 8>>
3452        ComponentListMap;
3453    {
3454      auto CI = ComponentLists.begin();
3455      for (auto DI = Declarations.begin(), DE = Declarations.end(); DI != DE;
3456           ++DI, ++CI) {
3457        assert(!CI->empty() && "Invalid component list!");
3458        ComponentListMap[*DI].push_back(*CI);
3459      }
3460    }
3461
3462    // Iterators of the target storage.
3463    auto UniqueDeclarations = getUniqueDeclsRef();
3464    auto UDI = UniqueDeclarations.begin();
3465
3466    auto DeclNumLists = getDeclNumListsRef();
3467    auto DNLI = DeclNumLists.begin();
3468
3469    auto ComponentListSizes = getComponentListSizesRef();
3470    auto CLSI = ComponentListSizes.begin();
3471
3472    auto Components = getComponentsRef();
3473    auto CI = Components.begin();
3474
3475    // Variable to compute the accumulation of the number of components.
3476    unsigned PrevSize = 0u;
3477
3478    // Scan all the declarations and associated component lists.
3479    for (auto &M : ComponentListMap) {
3480      // The declaration.
3481      auto *D = M.first;
3482      // The component lists.
3483      auto CL = M.second;
3484
3485      // Initialize the entry.
3486      *UDI = D;
3487      ++UDI;
3488
3489      *DNLI = CL.size();
3490      ++DNLI;
3491
3492      // Obtain the cumulative sizes and concatenate all the components in the
3493      // reserved storage.
3494      for (auto C : CL) {
3495        // Accumulate with the previous size.
3496        PrevSize += C.size();
3497
3498        // Save the size.
3499        *CLSI = PrevSize;
3500        ++CLSI;
3501
3502        // Append components after the current components iterator.
3503        CI = std::copy(C.begin(), C.end(), CI);
3504      }
3505    }
3506  }
3507
3508  /// \brief Build a clause for \a NumUniqueDeclarations declarations, \a
3509  /// NumComponentLists total component lists, and \a NumComponents total
3510  /// components.
3511  ///
3512  /// \param K Kind of the clause.
3513  /// \param StartLoc Starting location of the clause (the clause keyword).
3514  /// \param LParenLoc Location of '('.
3515  /// \param EndLoc Ending location of the clause.
3516  /// \param NumVars Number of expressions listed in the clause.
3517  /// \param NumUniqueDeclarations Number of unique base declarations in this
3518  /// clause.
3519  /// \param NumComponentLists Number of component lists in this clause - one
3520  /// list for each expression in the clause.
3521  /// \param NumComponents Total number of expression components in the clause.
3522  ///
3523  OMPMappableExprListClause(OpenMPClauseKind K, SourceLocation StartLoc,
3524                            SourceLocation LParenLoc, SourceLocation EndLoc,
3525                            unsigned NumVars, unsigned NumUniqueDeclarations,
3526                            unsigned NumComponentLists, unsigned NumComponents)
3527      : OMPVarListClause<T>(K, StartLoc, LParenLoc, EndLoc, NumVars),
3528        NumUniqueDeclarations(NumUniqueDeclarations),
3529        NumComponentLists(NumComponentLists), NumComponents(NumComponents) {}
3530
3531public:
3532  /// \brief Return the number of unique base declarations in this clause.
3533  unsigned getUniqueDeclarationsNum() const { return NumUniqueDeclarations; }
3534  /// \brief Return the number of lists derived from the clause expressions.
3535  unsigned getTotalComponentListNum() const { return NumComponentLists; }
3536  /// \brief Return the total number of components in all lists derived from the
3537  /// clause.
3538  unsigned getTotalComponentsNum() const { return NumComponents; }
3539
3540  /// \brief Iterator that browse the components by lists. It also allows
3541  /// browsing components of a single declaration.
3542  class const_component_lists_iterator
3543      : public llvm::iterator_adaptor_base<
3544            const_component_lists_iterator,
3545            MappableExprComponentListRef::const_iterator,
3546            std::forward_iterator_tag, MappableComponent, ptrdiff_t,
3547            MappableComponent, MappableComponent> {
3548    // The declaration the iterator currently refers to.
3549    ArrayRef<ValueDecl *>::iterator DeclCur;
3550
3551    // The list number associated with the current declaration.
3552    ArrayRef<unsigned>::iterator NumListsCur;
3553
3554    // Remaining lists for the current declaration.
3555    unsigned RemainingLists;
3556
3557    // The cumulative size of the previous list, or zero if there is no previous
3558    // list.
3559    unsigned PrevListSize;
3560
3561    // The cumulative sizes of the current list - it will delimit the remaining
3562    // range of interest.
3563    ArrayRef<unsigned>::const_iterator ListSizeCur;
3564    ArrayRef<unsigned>::const_iterator ListSizeEnd;
3565
3566    // Iterator to the end of the components storage.
3567    MappableExprComponentListRef::const_iterator End;
3568
3569  public:
3570    /// \brief Construct an iterator that scans all lists.
3571    explicit const_component_lists_iterator(
3572        ArrayRef<ValueDecl *> UniqueDecls, ArrayRef<unsigned> DeclsListNum,
3573        ArrayRef<unsigned> CumulativeListSizes,
3574        MappableExprComponentListRef Components)
3575        : const_component_lists_iterator::iterator_adaptor_base(
3576              Components.begin()),
3577          DeclCur(UniqueDecls.begin()), NumListsCur(DeclsListNum.begin()),
3578          RemainingLists(0u), PrevListSize(0u),
3579          ListSizeCur(CumulativeListSizes.begin()),
3580          ListSizeEnd(CumulativeListSizes.end()), End(Components.end()) {
3581      assert(UniqueDecls.size() == DeclsListNum.size() &&
3582             "Inconsistent number of declarations and list sizes!");
3583      if (!DeclsListNum.empty())
3584        RemainingLists = *NumListsCur;
3585    }
3586
3587    /// \brief Construct an iterator that scan lists for a given declaration \a
3588    /// Declaration.
3589    explicit const_component_lists_iterator(
3590        const ValueDecl *Declaration, ArrayRef<ValueDecl *> UniqueDecls,
3591        ArrayRef<unsigned> DeclsListNum, ArrayRef<unsigned> CumulativeListSizes,
3592        MappableExprComponentListRef Components)
3593        : const_component_lists_iterator(UniqueDecls, DeclsListNum,
3594                                         CumulativeListSizes, Components) {
3595
3596      // Look for the desired declaration. While we are looking for it, we
3597      // update the state so that we know the component where a given list
3598      // starts.
3599      for (; DeclCur != UniqueDecls.end(); ++DeclCur, ++NumListsCur) {
3600        if (*DeclCur == Declaration)
3601          break;
3602
3603        assert(*NumListsCur > 0 && "No lists associated with declaration??");
3604
3605        // Skip the lists associated with the current declaration, but save the
3606        // last list size that was skipped.
3607        std::advance(ListSizeCur, *NumListsCur - 1);
3608        PrevListSize = *ListSizeCur;
3609        ++ListSizeCur;
3610      }
3611
3612      // If we didn't find any declaration, advance the iterator to after the
3613      // last component and set remaining lists to zero.
3614      if (ListSizeCur == CumulativeListSizes.end()) {
3615        this->I = End;
3616        RemainingLists = 0u;
3617        return;
3618      }
3619
3620      // Set the remaining lists with the total number of lists of the current
3621      // declaration.
3622      RemainingLists = *NumListsCur;
3623
3624      // Adjust the list size end iterator to the end of the relevant range.
3625      ListSizeEnd = ListSizeCur;
3626      std::advance(ListSizeEnd, RemainingLists);
3627
3628      // Given that the list sizes are cumulative, the index of the component
3629      // that start the list is the size of the previous list.
3630      std::advance(this->I, PrevListSize);
3631    }
3632
3633    // Return the array with the current list. The sizes are cumulative, so the
3634    // array size is the difference between the current size and previous one.
3635    std::pair<const ValueDecl *, MappableExprComponentListRef>
3636    operator*() const {
3637      assert(ListSizeCur != ListSizeEnd && "Invalid iterator!");
3638      return std::make_pair(
3639          *DeclCur,
3640          MappableExprComponentListRef(&*this->I, *ListSizeCur - PrevListSize));
3641    }
3642    std::pair<const ValueDecl *, MappableExprComponentListRef>
3643    operator->() const {
3644      return **this;
3645    }
3646
3647    // Skip the components of the current list.
3648    const_component_lists_iterator &operator++() {
3649      assert(ListSizeCur != ListSizeEnd && RemainingLists &&
3650             "Invalid iterator!");
3651
3652      // If we don't have more lists just skip all the components. Otherwise,
3653      // advance the iterator by the number of components in the current list.
3654      if (std::next(ListSizeCur) == ListSizeEnd) {
3655        this->I = End;
3656        RemainingLists = 0;
3657      } else {
3658        std::advance(this->I, *ListSizeCur - PrevListSize);
3659        PrevListSize = *ListSizeCur;
3660
3661        // We are done with a declaration, move to the next one.
3662        if (!(--RemainingLists)) {
3663          ++DeclCur;
3664          ++NumListsCur;
3665          RemainingLists = *NumListsCur;
3666          assert(RemainingLists && "No lists in the following declaration??");
3667        }
3668      }
3669
3670      ++ListSizeCur;
3671      return *this;
3672    }
3673  };
3674
3675  typedef llvm::iterator_range<const_component_lists_iterator>
3676      const_component_lists_range;
3677
3678  /// \brief Iterators for all component lists.
3679  const_component_lists_iterator component_lists_begin() const {
3680    return const_component_lists_iterator(
3681        getUniqueDeclsRef(), getDeclNumListsRef(), getComponentListSizesRef(),
3682        getComponentsRef());
3683  }
3684  const_component_lists_iterator component_lists_end() const {
3685    return const_component_lists_iterator(
3686        ArrayRef<ValueDecl *>(), ArrayRef<unsigned>(), ArrayRef<unsigned>(),
3687        MappableExprComponentListRef(getComponentsRef().end(),
3688                                     getComponentsRef().end()));
3689  }
3690  const_component_lists_range component_lists() const {
3691    return {component_lists_begin(), component_lists_end()};
3692  }
3693
3694  /// \brief Iterators for component lists associated with the provided
3695  /// declaration.
3696  const_component_lists_iterator
3697  decl_component_lists_begin(const ValueDecl *VD) const {
3698    return const_component_lists_iterator(
3699        VD, getUniqueDeclsRef(), getDeclNumListsRef(),
3700        getComponentListSizesRef(), getComponentsRef());
3701  }
3702  const_component_lists_iterator decl_component_lists_end() const {
3703    return component_lists_end();
3704  }
3705  const_component_lists_range decl_component_lists(const ValueDecl *VD) const {
3706    return {decl_component_lists_begin(VD), decl_component_lists_end()};
3707  }
3708
3709  /// Iterators to access all the declarations, number of lists, list sizes, and
3710  /// components.
3711  typedef ArrayRef<ValueDecl *>::iterator const_all_decls_iterator;
3712  typedef llvm::iterator_range<const_all_decls_iterator> const_all_decls_range;
3713  const_all_decls_range all_decls() const {
3714    auto A = getUniqueDeclsRef();
3715    return const_all_decls_range(A.begin(), A.end());
3716  }
3717
3718  typedef ArrayRef<unsigned>::iterator const_all_num_lists_iterator;
3719  typedef llvm::iterator_range<const_all_num_lists_iterator>
3720      const_all_num_lists_range;
3721  const_all_num_lists_range all_num_lists() const {
3722    auto A = getDeclNumListsRef();
3723    return const_all_num_lists_range(A.begin(), A.end());
3724  }
3725
3726  typedef ArrayRef<unsigned>::iterator const_all_lists_sizes_iterator;
3727  typedef llvm::iterator_range<const_all_lists_sizes_iterator>
3728      const_all_lists_sizes_range;
3729  const_all_lists_sizes_range all_lists_sizes() const {
3730    auto A = getComponentListSizesRef();
3731    return const_all_lists_sizes_range(A.begin(), A.end());
3732  }
3733
3734  typedef ArrayRef<MappableComponent>::iterator const_all_components_iterator;
3735  typedef llvm::iterator_range<const_all_components_iterator>
3736      const_all_components_range;
3737  const_all_components_range all_components() const {
3738    auto A = getComponentsRef();
3739    return const_all_components_range(A.begin(), A.end());
3740  }
3741};
3742
3743/// \brief This represents clause 'map' in the '#pragma omp ...'
3744/// directives.
3745///
3746/// \code
3747/// #pragma omp target map(a,b)
3748/// \endcode
3749/// In this example directive '#pragma omp target' has clause 'map'
3750/// with the variables 'a' and 'b'.
3751///
3752class OMPMapClause final : public OMPMappableExprListClause<OMPMapClause>,
3753                           private llvm::TrailingObjects<
3754                               OMPMapClause, Expr *, ValueDecl *, unsigned,
3755                               OMPClauseMappableExprCommon::MappableComponent> {
3756  friend TrailingObjects;
3757  friend OMPVarListClause;
3758  friend OMPMappableExprListClause;
3759  friend class OMPClauseReader;
3760
3761  /// Define the sizes of each trailing object array except the last one. This
3762  /// is required for TrailingObjects to work properly.
3763  size_t numTrailingObjects(OverloadToken<Expr *>) const {
3764    return varlist_size();
3765  }
3766  size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
3767    return getUniqueDeclarationsNum();
3768  }
3769  size_t numTrailingObjects(OverloadToken<unsigned>) const {
3770    return getUniqueDeclarationsNum() + getTotalComponentListNum();
3771  }
3772
3773  /// \brief Map type modifier for the 'map' clause.
3774  OpenMPMapClauseKind MapTypeModifier;
3775  /// \brief Map type for the 'map' clause.
3776  OpenMPMapClauseKind MapType;
3777  /// \brief Is this an implicit map type or not.
3778  bool MapTypeIsImplicit;
3779  /// \brief Location of the map type.
3780  SourceLocation MapLoc;
3781  /// \brief Colon location.
3782  SourceLocation ColonLoc;
3783
3784  /// \brief Set type modifier for the clause.
3785  ///
3786  /// \param T Type Modifier for the clause.
3787  ///
3788  void setMapTypeModifier(OpenMPMapClauseKind T) { MapTypeModifier = T; }
3789
3790  /// \brief Set type for the clause.
3791  ///
3792  /// \param T Type for the clause.
3793  ///
3794  void setMapType(OpenMPMapClauseKind T) { MapType = T; }
3795
3796  /// \brief Set type location.
3797  ///
3798  /// \param TLoc Type location.
3799  ///
3800  void setMapLoc(SourceLocation TLoc) { MapLoc = TLoc; }
3801
3802  /// \brief Set colon location.
3803  void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
3804
3805  /// \brief Build a clause for \a NumVars listed expressions, \a
3806  /// NumUniqueDeclarations declarations, \a NumComponentLists total component
3807  /// lists, and \a NumComponents total expression components.
3808  ///
3809  /// \param MapTypeModifier Map type modifier.
3810  /// \param MapType Map type.
3811  /// \param MapTypeIsImplicit Map type is inferred implicitly.
3812  /// \param MapLoc Location of the map type.
3813  /// \param StartLoc Starting location of the clause.
3814  /// \param EndLoc Ending location of the clause.
3815  /// \param NumVars Number of expressions listed in this clause.
3816  /// \param NumUniqueDeclarations Number of unique base declarations in this
3817  /// clause.
3818  /// \param NumComponentLists Number of component lists in this clause.
3819  /// \param NumComponents Total number of expression components in the clause.
3820  ///
3821  explicit OMPMapClause(OpenMPMapClauseKind MapTypeModifier,
3822                        OpenMPMapClauseKind MapType, bool MapTypeIsImplicit,
3823                        SourceLocation MapLoc, SourceLocation StartLoc,
3824                        SourceLocation LParenLoc, SourceLocation EndLoc,
3825                        unsigned NumVars, unsigned NumUniqueDeclarations,
3826                        unsigned NumComponentLists, unsigned NumComponents)
3827      : OMPMappableExprListClause(OMPC_map, StartLoc, LParenLoc, EndLoc,
3828                                  NumVars, NumUniqueDeclarations,
3829                                  NumComponentLists, NumComponents),
3830        MapTypeModifier(MapTypeModifier), MapType(MapType),
3831        MapTypeIsImplicit(MapTypeIsImplicit), MapLoc(MapLoc) {}
3832
3833  /// \brief Build an empty clause.
3834  ///
3835  /// \param NumVars Number of expressions listed in this clause.
3836  /// \param NumUniqueDeclarations Number of unique base declarations in this
3837  /// clause.
3838  /// \param NumComponentLists Number of component lists in this clause.
3839  /// \param NumComponents Total number of expression components in the clause.
3840  ///
3841  explicit OMPMapClause(unsigned NumVars, unsigned NumUniqueDeclarations,
3842                        unsigned NumComponentLists, unsigned NumComponents)
3843      : OMPMappableExprListClause(
3844            OMPC_map, SourceLocation(), SourceLocation(), SourceLocation(),
3845            NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents),
3846        MapTypeModifier(OMPC_MAP_unknown), MapType(OMPC_MAP_unknown),
3847        MapTypeIsImplicit(false), MapLoc() {}
3848
3849public:
3850  /// \brief Creates clause with a list of variables \a VL.
3851  ///
3852  /// \param C AST context.
3853  /// \param StartLoc Starting location of the clause.
3854  /// \param EndLoc Ending location of the clause.
3855  /// \param Vars The original expression used in the clause.
3856  /// \param Declarations Declarations used in the clause.
3857  /// \param ComponentLists Component lists used in the clause.
3858  /// \param TypeModifier Map type modifier.
3859  /// \param Type Map type.
3860  /// \param TypeIsImplicit Map type is inferred implicitly.
3861  /// \param TypeLoc Location of the map type.
3862  ///
3863  static OMPMapClause *Create(const ASTContext &C, SourceLocation StartLoc,
3864                              SourceLocation LParenLoc, SourceLocation EndLoc,
3865                              ArrayRef<Expr *> Vars,
3866                              ArrayRef<ValueDecl *> Declarations,
3867                              MappableExprComponentListsRef ComponentLists,
3868                              OpenMPMapClauseKind TypeModifier,
3869                              OpenMPMapClauseKind Type, bool TypeIsImplicit,
3870                              SourceLocation TypeLoc);
3871  /// \brief Creates an empty clause with the place for for \a NumVars original
3872  /// expressions, \a NumUniqueDeclarations declarations, \NumComponentLists
3873  /// lists, and \a NumComponents expression components.
3874  ///
3875  /// \param C AST context.
3876  /// \param NumVars Number of expressions listed in the clause.
3877  /// \param NumUniqueDeclarations Number of unique base declarations in this
3878  /// clause.
3879  /// \param NumComponentLists Number of unique base declarations in this
3880  /// clause.
3881  /// \param NumComponents Total number of expression components in the clause.
3882  ///
3883  static OMPMapClause *CreateEmpty(const ASTContext &C, unsigned NumVars,
3884                                   unsigned NumUniqueDeclarations,
3885                                   unsigned NumComponentLists,
3886                                   unsigned NumComponents);
3887
3888  /// \brief Fetches mapping kind for the clause.
3889  OpenMPMapClauseKind getMapType() const LLVM_READONLY { return MapType; }
3890
3891  /// \brief Is this an implicit map type?
3892  /// We have to capture 'IsMapTypeImplicit' from the parser for more
3893  /// informative error messages.  It helps distinguish map(r) from
3894  /// map(tofrom: r), which is important to print more helpful error
3895  /// messages for some target directives.
3896  bool isImplicitMapType() const LLVM_READONLY { return MapTypeIsImplicit; }
3897
3898  /// \brief Fetches the map type modifier for the clause.
3899  OpenMPMapClauseKind getMapTypeModifier() const LLVM_READONLY {
3900    return MapTypeModifier;
3901  }
3902
3903  /// \brief Fetches location of clause mapping kind.
3904  SourceLocation getMapLoc() const LLVM_READONLY { return MapLoc; }
3905
3906  /// \brief Get colon location.
3907  SourceLocation getColonLoc() const { return ColonLoc; }
3908
3909  static bool classof(const OMPClause *T) {
3910    return T->getClauseKind() == OMPC_map;
3911  }
3912
3913  child_range children() {
3914    return child_range(
3915        reinterpret_cast<Stmt **>(varlist_begin()),
3916        reinterpret_cast<Stmt **>(varlist_end()));
3917  }
3918};
3919
3920/// \brief This represents 'num_teams' clause in the '#pragma omp ...'
3921/// directive.
3922///
3923/// \code
3924/// #pragma omp teams num_teams(n)
3925/// \endcode
3926/// In this example directive '#pragma omp teams' has clause 'num_teams'
3927/// with single expression 'n'.
3928///
3929class OMPNumTeamsClause : public OMPClause, public OMPClauseWithPreInit {
3930  friend class OMPClauseReader;
3931  /// \brief Location of '('.
3932  SourceLocation LParenLoc;
3933  /// \brief NumTeams number.
3934  Stmt *NumTeams;
3935  /// \brief Set the NumTeams number.
3936  ///
3937  /// \param E NumTeams number.
3938  ///
3939  void setNumTeams(Expr *E) { NumTeams = E; }
3940
3941public:
3942  /// \brief Build 'num_teams' clause.
3943  ///
3944  /// \param E Expression associated with this clause.
3945  /// \param HelperE Helper Expression associated with this clause.
3946  /// \param CaptureRegion Innermost OpenMP region where expressions in this
3947  /// clause must be captured.
3948  /// \param StartLoc Starting location of the clause.
3949  /// \param LParenLoc Location of '('.
3950  /// \param EndLoc Ending location of the clause.
3951  ///
3952  OMPNumTeamsClause(Expr *E, Stmt *HelperE, OpenMPDirectiveKind CaptureRegion,
3953                    SourceLocation StartLoc, SourceLocation LParenLoc,
3954                    SourceLocation EndLoc)
3955      : OMPClause(OMPC_num_teams, StartLoc, EndLoc), OMPClauseWithPreInit(this),
3956        LParenLoc(LParenLoc), NumTeams(E) {
3957    setPreInitStmt(HelperE, CaptureRegion);
3958  }
3959
3960  /// \brief Build an empty clause.
3961  ///
3962  OMPNumTeamsClause()
3963      : OMPClause(OMPC_num_teams, SourceLocation(), SourceLocation()),
3964        OMPClauseWithPreInit(this), LParenLoc(SourceLocation()),
3965        NumTeams(nullptr) {}
3966  /// \brief Sets the location of '('.
3967  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
3968  /// \brief Returns the location of '('.
3969  SourceLocation getLParenLoc() const { return LParenLoc; }
3970  /// \brief Return NumTeams number.
3971  Expr *getNumTeams() { return cast<Expr>(NumTeams); }
3972  /// \brief Return NumTeams number.
3973  Expr *getNumTeams() const { return cast<Expr>(NumTeams); }
3974
3975  static bool classof(const OMPClause *T) {
3976    return T->getClauseKind() == OMPC_num_teams;
3977  }
3978
3979  child_range children() { return child_range(&NumTeams, &NumTeams + 1); }
3980};
3981
3982/// \brief This represents 'thread_limit' clause in the '#pragma omp ...'
3983/// directive.
3984///
3985/// \code
3986/// #pragma omp teams thread_limit(n)
3987/// \endcode
3988/// In this example directive '#pragma omp teams' has clause 'thread_limit'
3989/// with single expression 'n'.
3990///
3991class OMPThreadLimitClause : public OMPClause, public OMPClauseWithPreInit {
3992  friend class OMPClauseReader;
3993  /// \brief Location of '('.
3994  SourceLocation LParenLoc;
3995  /// \brief ThreadLimit number.
3996  Stmt *ThreadLimit;
3997  /// \brief Set the ThreadLimit number.
3998  ///
3999  /// \param E ThreadLimit number.
4000  ///
4001  void setThreadLimit(Expr *E) { ThreadLimit = E; }
4002
4003public:
4004  /// \brief Build 'thread_limit' clause.
4005  ///
4006  /// \param E Expression associated with this clause.
4007  /// \param HelperE Helper Expression associated with this clause.
4008  /// \param CaptureRegion Innermost OpenMP region where expressions in this
4009  /// clause must be captured.
4010  /// \param StartLoc Starting location of the clause.
4011  /// \param LParenLoc Location of '('.
4012  /// \param EndLoc Ending location of the clause.
4013  ///
4014  OMPThreadLimitClause(Expr *E, Stmt *HelperE,
4015                       OpenMPDirectiveKind CaptureRegion,
4016                       SourceLocation StartLoc, SourceLocation LParenLoc,
4017                       SourceLocation EndLoc)
4018      : OMPClause(OMPC_thread_limit, StartLoc, EndLoc),
4019        OMPClauseWithPreInit(this), LParenLoc(LParenLoc), ThreadLimit(E) {
4020    setPreInitStmt(HelperE, CaptureRegion);
4021  }
4022
4023  /// \brief Build an empty clause.
4024  ///
4025  OMPThreadLimitClause()
4026      : OMPClause(OMPC_thread_limit, SourceLocation(), SourceLocation()),
4027        OMPClauseWithPreInit(this), LParenLoc(SourceLocation()),
4028        ThreadLimit(nullptr) {}
4029  /// \brief Sets the location of '('.
4030  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4031  /// \brief Returns the location of '('.
4032  SourceLocation getLParenLoc() const { return LParenLoc; }
4033  /// \brief Return ThreadLimit number.
4034  Expr *getThreadLimit() { return cast<Expr>(ThreadLimit); }
4035  /// \brief Return ThreadLimit number.
4036  Expr *getThreadLimit() const { return cast<Expr>(ThreadLimit); }
4037
4038  static bool classof(const OMPClause *T) {
4039    return T->getClauseKind() == OMPC_thread_limit;
4040  }
4041
4042  child_range children() { return child_range(&ThreadLimit, &ThreadLimit + 1); }
4043};
4044
4045/// \brief This represents 'priority' clause in the '#pragma omp ...'
4046/// directive.
4047///
4048/// \code
4049/// #pragma omp task priority(n)
4050/// \endcode
4051/// In this example directive '#pragma omp teams' has clause 'priority' with
4052/// single expression 'n'.
4053///
4054class OMPPriorityClause : public OMPClause {
4055  friend class OMPClauseReader;
4056  /// \brief Location of '('.
4057  SourceLocation LParenLoc;
4058  /// \brief Priority number.
4059  Stmt *Priority;
4060  /// \brief Set the Priority number.
4061  ///
4062  /// \param E Priority number.
4063  ///
4064  void setPriority(Expr *E) { Priority = E; }
4065
4066public:
4067  /// \brief Build 'priority' clause.
4068  ///
4069  /// \param E Expression associated with this clause.
4070  /// \param StartLoc Starting location of the clause.
4071  /// \param LParenLoc Location of '('.
4072  /// \param EndLoc Ending location of the clause.
4073  ///
4074  OMPPriorityClause(Expr *E, SourceLocation StartLoc, SourceLocation LParenLoc,
4075                    SourceLocation EndLoc)
4076      : OMPClause(OMPC_priority, StartLoc, EndLoc), LParenLoc(LParenLoc),
4077        Priority(E) {}
4078
4079  /// \brief Build an empty clause.
4080  ///
4081  OMPPriorityClause()
4082      : OMPClause(OMPC_priority, SourceLocation(), SourceLocation()),
4083        LParenLoc(SourceLocation()), Priority(nullptr) {}
4084  /// \brief Sets the location of '('.
4085  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4086  /// \brief Returns the location of '('.
4087  SourceLocation getLParenLoc() const { return LParenLoc; }
4088  /// \brief Return Priority number.
4089  Expr *getPriority() { return cast<Expr>(Priority); }
4090  /// \brief Return Priority number.
4091  Expr *getPriority() const { return cast<Expr>(Priority); }
4092
4093  static bool classof(const OMPClause *T) {
4094    return T->getClauseKind() == OMPC_priority;
4095  }
4096
4097  child_range children() { return child_range(&Priority, &Priority + 1); }
4098};
4099
4100/// \brief This represents 'grainsize' clause in the '#pragma omp ...'
4101/// directive.
4102///
4103/// \code
4104/// #pragma omp taskloop grainsize(4)
4105/// \endcode
4106/// In this example directive '#pragma omp taskloop' has clause 'grainsize'
4107/// with single expression '4'.
4108///
4109class OMPGrainsizeClause : public OMPClause {
4110  friend class OMPClauseReader;
4111  /// \brief Location of '('.
4112  SourceLocation LParenLoc;
4113  /// \brief Safe iteration space distance.
4114  Stmt *Grainsize;
4115
4116  /// \brief Set safelen.
4117  void setGrainsize(Expr *Size) { Grainsize = Size; }
4118
4119public:
4120  /// \brief Build 'grainsize' clause.
4121  ///
4122  /// \param Size Expression associated with this clause.
4123  /// \param StartLoc Starting location of the clause.
4124  /// \param EndLoc Ending location of the clause.
4125  ///
4126  OMPGrainsizeClause(Expr *Size, SourceLocation StartLoc,
4127                     SourceLocation LParenLoc, SourceLocation EndLoc)
4128      : OMPClause(OMPC_grainsize, StartLoc, EndLoc), LParenLoc(LParenLoc),
4129        Grainsize(Size) {}
4130
4131  /// \brief Build an empty clause.
4132  ///
4133  explicit OMPGrainsizeClause()
4134      : OMPClause(OMPC_grainsize, SourceLocation(), SourceLocation()),
4135        LParenLoc(SourceLocation()), Grainsize(nullptr) {}
4136
4137  /// \brief Sets the location of '('.
4138  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4139  /// \brief Returns the location of '('.
4140  SourceLocation getLParenLoc() const { return LParenLoc; }
4141
4142  /// \brief Return safe iteration space distance.
4143  Expr *getGrainsize() const { return cast_or_null<Expr>(Grainsize); }
4144
4145  static bool classof(const OMPClause *T) {
4146    return T->getClauseKind() == OMPC_grainsize;
4147  }
4148
4149  child_range children() { return child_range(&Grainsize, &Grainsize + 1); }
4150};
4151
4152/// \brief This represents 'nogroup' clause in the '#pragma omp ...' directive.
4153///
4154/// \code
4155/// #pragma omp taskloop nogroup
4156/// \endcode
4157/// In this example directive '#pragma omp taskloop' has 'nogroup' clause.
4158///
4159class OMPNogroupClause : public OMPClause {
4160public:
4161  /// \brief Build 'nogroup' clause.
4162  ///
4163  /// \param StartLoc Starting location of the clause.
4164  /// \param EndLoc Ending location of the clause.
4165  ///
4166  OMPNogroupClause(SourceLocation StartLoc, SourceLocation EndLoc)
4167      : OMPClause(OMPC_nogroup, StartLoc, EndLoc) {}
4168
4169  /// \brief Build an empty clause.
4170  ///
4171  OMPNogroupClause()
4172      : OMPClause(OMPC_nogroup, SourceLocation(), SourceLocation()) {}
4173
4174  static bool classof(const OMPClause *T) {
4175    return T->getClauseKind() == OMPC_nogroup;
4176  }
4177
4178  child_range children() {
4179    return child_range(child_iterator(), child_iterator());
4180  }
4181};
4182
4183/// \brief This represents 'num_tasks' clause in the '#pragma omp ...'
4184/// directive.
4185///
4186/// \code
4187/// #pragma omp taskloop num_tasks(4)
4188/// \endcode
4189/// In this example directive '#pragma omp taskloop' has clause 'num_tasks'
4190/// with single expression '4'.
4191///
4192class OMPNumTasksClause : public OMPClause {
4193  friend class OMPClauseReader;
4194  /// \brief Location of '('.
4195  SourceLocation LParenLoc;
4196  /// \brief Safe iteration space distance.
4197  Stmt *NumTasks;
4198
4199  /// \brief Set safelen.
4200  void setNumTasks(Expr *Size) { NumTasks = Size; }
4201
4202public:
4203  /// \brief Build 'num_tasks' clause.
4204  ///
4205  /// \param Size Expression associated with this clause.
4206  /// \param StartLoc Starting location of the clause.
4207  /// \param EndLoc Ending location of the clause.
4208  ///
4209  OMPNumTasksClause(Expr *Size, SourceLocation StartLoc,
4210                    SourceLocation LParenLoc, SourceLocation EndLoc)
4211      : OMPClause(OMPC_num_tasks, StartLoc, EndLoc), LParenLoc(LParenLoc),
4212        NumTasks(Size) {}
4213
4214  /// \brief Build an empty clause.
4215  ///
4216  explicit OMPNumTasksClause()
4217      : OMPClause(OMPC_num_tasks, SourceLocation(), SourceLocation()),
4218        LParenLoc(SourceLocation()), NumTasks(nullptr) {}
4219
4220  /// \brief Sets the location of '('.
4221  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4222  /// \brief Returns the location of '('.
4223  SourceLocation getLParenLoc() const { return LParenLoc; }
4224
4225  /// \brief Return safe iteration space distance.
4226  Expr *getNumTasks() const { return cast_or_null<Expr>(NumTasks); }
4227
4228  static bool classof(const OMPClause *T) {
4229    return T->getClauseKind() == OMPC_num_tasks;
4230  }
4231
4232  child_range children() { return child_range(&NumTasks, &NumTasks + 1); }
4233};
4234
4235/// \brief This represents 'hint' clause in the '#pragma omp ...' directive.
4236///
4237/// \code
4238/// #pragma omp critical (name) hint(6)
4239/// \endcode
4240/// In this example directive '#pragma omp critical' has name 'name' and clause
4241/// 'hint' with argument '6'.
4242///
4243class OMPHintClause : public OMPClause {
4244  friend class OMPClauseReader;
4245  /// \brief Location of '('.
4246  SourceLocation LParenLoc;
4247  /// \brief Hint expression of the 'hint' clause.
4248  Stmt *Hint;
4249
4250  /// \brief Set hint expression.
4251  ///
4252  void setHint(Expr *H) { Hint = H; }
4253
4254public:
4255  /// \brief Build 'hint' clause with expression \a Hint.
4256  ///
4257  /// \param Hint Hint expression.
4258  /// \param StartLoc Starting location of the clause.
4259  /// \param LParenLoc Location of '('.
4260  /// \param EndLoc Ending location of the clause.
4261  ///
4262  OMPHintClause(Expr *Hint, SourceLocation StartLoc, SourceLocation LParenLoc,
4263                SourceLocation EndLoc)
4264      : OMPClause(OMPC_hint, StartLoc, EndLoc), LParenLoc(LParenLoc),
4265        Hint(Hint) {}
4266
4267  /// \brief Build an empty clause.
4268  ///
4269  OMPHintClause()
4270      : OMPClause(OMPC_hint, SourceLocation(), SourceLocation()),
4271        LParenLoc(SourceLocation()), Hint(nullptr) {}
4272
4273  /// \brief Sets the location of '('.
4274  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4275  /// \brief Returns the location of '('.
4276  SourceLocation getLParenLoc() const { return LParenLoc; }
4277
4278  /// \brief Returns number of threads.
4279  Expr *getHint() const { return cast_or_null<Expr>(Hint); }
4280
4281  static bool classof(const OMPClause *T) {
4282    return T->getClauseKind() == OMPC_hint;
4283  }
4284
4285  child_range children() { return child_range(&Hint, &Hint + 1); }
4286};
4287
4288/// \brief This represents 'dist_schedule' clause in the '#pragma omp ...'
4289/// directive.
4290///
4291/// \code
4292/// #pragma omp distribute dist_schedule(static, 3)
4293/// \endcode
4294/// In this example directive '#pragma omp distribute' has 'dist_schedule'
4295/// clause with arguments 'static' and '3'.
4296///
4297class OMPDistScheduleClause : public OMPClause, public OMPClauseWithPreInit {
4298  friend class OMPClauseReader;
4299  /// \brief Location of '('.
4300  SourceLocation LParenLoc;
4301  /// \brief A kind of the 'schedule' clause.
4302  OpenMPDistScheduleClauseKind Kind;
4303  /// \brief Start location of the schedule kind in source code.
4304  SourceLocation KindLoc;
4305  /// \brief Location of ',' (if any).
4306  SourceLocation CommaLoc;
4307  /// \brief Chunk size.
4308  Expr *ChunkSize;
4309
4310  /// \brief Set schedule kind.
4311  ///
4312  /// \param K Schedule kind.
4313  ///
4314  void setDistScheduleKind(OpenMPDistScheduleClauseKind K) { Kind = K; }
4315  /// \brief Sets the location of '('.
4316  ///
4317  /// \param Loc Location of '('.
4318  ///
4319  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4320  /// \brief Set schedule kind start location.
4321  ///
4322  /// \param KLoc Schedule kind location.
4323  ///
4324  void setDistScheduleKindLoc(SourceLocation KLoc) { KindLoc = KLoc; }
4325  /// \brief Set location of ','.
4326  ///
4327  /// \param Loc Location of ','.
4328  ///
4329  void setCommaLoc(SourceLocation Loc) { CommaLoc = Loc; }
4330  /// \brief Set chunk size.
4331  ///
4332  /// \param E Chunk size.
4333  ///
4334  void setChunkSize(Expr *E) { ChunkSize = E; }
4335
4336public:
4337  /// \brief Build 'dist_schedule' clause with schedule kind \a Kind and chunk
4338  /// size expression \a ChunkSize.
4339  ///
4340  /// \param StartLoc Starting location of the clause.
4341  /// \param LParenLoc Location of '('.
4342  /// \param KLoc Starting location of the argument.
4343  /// \param CommaLoc Location of ','.
4344  /// \param EndLoc Ending location of the clause.
4345  /// \param Kind DistSchedule kind.
4346  /// \param ChunkSize Chunk size.
4347  /// \param HelperChunkSize Helper chunk size for combined directives.
4348  ///
4349  OMPDistScheduleClause(SourceLocation StartLoc, SourceLocation LParenLoc,
4350                        SourceLocation KLoc, SourceLocation CommaLoc,
4351                        SourceLocation EndLoc,
4352                        OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize,
4353                        Stmt *HelperChunkSize)
4354      : OMPClause(OMPC_dist_schedule, StartLoc, EndLoc),
4355        OMPClauseWithPreInit(this), LParenLoc(LParenLoc), Kind(Kind),
4356        KindLoc(KLoc), CommaLoc(CommaLoc), ChunkSize(ChunkSize) {
4357    setPreInitStmt(HelperChunkSize);
4358  }
4359
4360  /// \brief Build an empty clause.
4361  ///
4362  explicit OMPDistScheduleClause()
4363      : OMPClause(OMPC_dist_schedule, SourceLocation(), SourceLocation()),
4364        OMPClauseWithPreInit(this), Kind(OMPC_DIST_SCHEDULE_unknown),
4365        ChunkSize(nullptr) {}
4366
4367  /// \brief Get kind of the clause.
4368  ///
4369  OpenMPDistScheduleClauseKind getDistScheduleKind() const { return Kind; }
4370  /// \brief Get location of '('.
4371  ///
4372  SourceLocation getLParenLoc() { return LParenLoc; }
4373  /// \brief Get kind location.
4374  ///
4375  SourceLocation getDistScheduleKindLoc() { return KindLoc; }
4376  /// \brief Get location of ','.
4377  ///
4378  SourceLocation getCommaLoc() { return CommaLoc; }
4379  /// \brief Get chunk size.
4380  ///
4381  Expr *getChunkSize() { return ChunkSize; }
4382  /// \brief Get chunk size.
4383  ///
4384  const Expr *getChunkSize() const { return ChunkSize; }
4385
4386  static bool classof(const OMPClause *T) {
4387    return T->getClauseKind() == OMPC_dist_schedule;
4388  }
4389
4390  child_range children() {
4391    return child_range(reinterpret_cast<Stmt **>(&ChunkSize),
4392                       reinterpret_cast<Stmt **>(&ChunkSize) + 1);
4393  }
4394};
4395
4396/// \brief This represents 'defaultmap' clause in the '#pragma omp ...' directive.
4397///
4398/// \code
4399/// #pragma omp target defaultmap(tofrom: scalar)
4400/// \endcode
4401/// In this example directive '#pragma omp target' has 'defaultmap' clause of kind
4402/// 'scalar' with modifier 'tofrom'.
4403///
4404class OMPDefaultmapClause : public OMPClause {
4405  friend class OMPClauseReader;
4406  /// \brief Location of '('.
4407  SourceLocation LParenLoc;
4408  /// \brief Modifiers for 'defaultmap' clause.
4409  OpenMPDefaultmapClauseModifier Modifier;
4410  /// \brief Locations of modifiers.
4411  SourceLocation ModifierLoc;
4412  /// \brief A kind of the 'defaultmap' clause.
4413  OpenMPDefaultmapClauseKind Kind;
4414  /// \brief Start location of the defaultmap kind in source code.
4415  SourceLocation KindLoc;
4416
4417  /// \brief Set defaultmap kind.
4418  ///
4419  /// \param K Defaultmap kind.
4420  ///
4421  void setDefaultmapKind(OpenMPDefaultmapClauseKind K) { Kind = K; }
4422  /// \brief Set the defaultmap modifier.
4423  ///
4424  /// \param M Defaultmap modifier.
4425  ///
4426  void setDefaultmapModifier(OpenMPDefaultmapClauseModifier M) {
4427    Modifier = M;
4428  }
4429  /// \brief Set location of the defaultmap modifier.
4430  ///
4431  void setDefaultmapModifierLoc(SourceLocation Loc) {
4432    ModifierLoc = Loc;
4433  }
4434  /// \brief Sets the location of '('.
4435  ///
4436  /// \param Loc Location of '('.
4437  ///
4438  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
4439  /// \brief Set defaultmap kind start location.
4440  ///
4441  /// \param KLoc Defaultmap kind location.
4442  ///
4443  void setDefaultmapKindLoc(SourceLocation KLoc) { KindLoc = KLoc; }
4444
4445public:
4446  /// \brief Build 'defaultmap' clause with defaultmap kind \a Kind
4447  ///
4448  /// \param StartLoc Starting location of the clause.
4449  /// \param LParenLoc Location of '('.
4450  /// \param KLoc Starting location of the argument.
4451  /// \param EndLoc Ending location of the clause.
4452  /// \param Kind Defaultmap kind.
4453  /// \param M The modifier applied to 'defaultmap' clause.
4454  /// \param MLoc Location of the modifier
4455  ///
4456  OMPDefaultmapClause(SourceLocation StartLoc, SourceLocation LParenLoc,
4457                      SourceLocation MLoc, SourceLocation KLoc,
4458                      SourceLocation EndLoc, OpenMPDefaultmapClauseKind Kind,
4459                      OpenMPDefaultmapClauseModifier M)
4460      : OMPClause(OMPC_defaultmap, StartLoc, EndLoc), LParenLoc(LParenLoc),
4461        Modifier(M), ModifierLoc(MLoc), Kind(Kind), KindLoc(KLoc) {}
4462
4463  /// \brief Build an empty clause.
4464  ///
4465  explicit OMPDefaultmapClause()
4466      : OMPClause(OMPC_defaultmap, SourceLocation(), SourceLocation()),
4467        Modifier(OMPC_DEFAULTMAP_MODIFIER_unknown),
4468        Kind(OMPC_DEFAULTMAP_unknown) {}
4469
4470  /// \brief Get kind of the clause.
4471  ///
4472  OpenMPDefaultmapClauseKind getDefaultmapKind() const { return Kind; }
4473  /// \brief Get the modifier of the clause.
4474  ///
4475  OpenMPDefaultmapClauseModifier getDefaultmapModifier() const {
4476    return Modifier;
4477  }
4478  /// \brief Get location of '('.
4479  ///
4480  SourceLocation getLParenLoc() { return LParenLoc; }
4481  /// \brief Get kind location.
4482  ///
4483  SourceLocation getDefaultmapKindLoc() { return KindLoc; }
4484  /// \brief Get the modifier location.
4485  ///
4486  SourceLocation getDefaultmapModifierLoc() const {
4487    return ModifierLoc;
4488  }
4489
4490  static bool classof(const OMPClause *T) {
4491    return T->getClauseKind() == OMPC_defaultmap;
4492  }
4493
4494  child_range children() {
4495    return child_range(child_iterator(), child_iterator());
4496  }
4497};
4498
4499/// \brief This represents clause 'to' in the '#pragma omp ...'
4500/// directives.
4501///
4502/// \code
4503/// #pragma omp target update to(a,b)
4504/// \endcode
4505/// In this example directive '#pragma omp target update' has clause 'to'
4506/// with the variables 'a' and 'b'.
4507///
4508class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
4509                          private llvm::TrailingObjects<
4510                              OMPToClause, Expr *, ValueDecl *, unsigned,
4511                              OMPClauseMappableExprCommon::MappableComponent> {
4512  friend TrailingObjects;
4513  friend OMPVarListClause;
4514  friend OMPMappableExprListClause;
4515  friend class OMPClauseReader;
4516
4517  /// Define the sizes of each trailing object array except the last one. This
4518  /// is required for TrailingObjects to work properly.
4519  size_t numTrailingObjects(OverloadToken<Expr *>) const {
4520    return varlist_size();
4521  }
4522  size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
4523    return getUniqueDeclarationsNum();
4524  }
4525  size_t numTrailingObjects(OverloadToken<unsigned>) const {
4526    return getUniqueDeclarationsNum() + getTotalComponentListNum();
4527  }
4528
4529  /// \brief Build clause with number of variables \a NumVars.
4530  ///
4531  /// \param StartLoc Starting location of the clause.
4532  /// \param EndLoc Ending location of the clause.
4533  /// \param NumVars Number of expressions listed in this clause.
4534  /// \param NumUniqueDeclarations Number of unique base declarations in this
4535  /// clause.
4536  /// \param NumComponentLists Number of component lists in this clause.
4537  /// \param NumComponents Total number of expression components in the clause.
4538  ///
4539  explicit OMPToClause(SourceLocation StartLoc, SourceLocation LParenLoc,
4540                       SourceLocation EndLoc, unsigned NumVars,
4541                       unsigned NumUniqueDeclarations,
4542                       unsigned NumComponentLists, unsigned NumComponents)
4543      : OMPMappableExprListClause(OMPC_to, StartLoc, LParenLoc, EndLoc, NumVars,
4544                                  NumUniqueDeclarations, NumComponentLists,
4545                                  NumComponents) {}
4546
4547  /// \brief Build an empty clause.
4548  ///
4549  /// \param NumVars Number of expressions listed in this clause.
4550  /// \param NumUniqueDeclarations Number of unique base declarations in this
4551  /// clause.
4552  /// \param NumComponentLists Number of component lists in this clause.
4553  /// \param NumComponents Total number of expression components in the clause.
4554  ///
4555  explicit OMPToClause(unsigned NumVars, unsigned NumUniqueDeclarations,
4556                       unsigned NumComponentLists, unsigned NumComponents)
4557      : OMPMappableExprListClause(
4558            OMPC_to, SourceLocation(), SourceLocation(), SourceLocation(),
4559            NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents) {}
4560
4561public:
4562  /// \brief Creates clause with a list of variables \a Vars.
4563  ///
4564  /// \param C AST context.
4565  /// \param StartLoc Starting location of the clause.
4566  /// \param EndLoc Ending location of the clause.
4567  /// \param Vars The original expression used in the clause.
4568  /// \param Declarations Declarations used in the clause.
4569  /// \param ComponentLists Component lists used in the clause.
4570  ///
4571  static OMPToClause *Create(const ASTContext &C, SourceLocation StartLoc,
4572                             SourceLocation LParenLoc, SourceLocation EndLoc,
4573                             ArrayRef<Expr *> Vars,
4574                             ArrayRef<ValueDecl *> Declarations,
4575                             MappableExprComponentListsRef ComponentLists);
4576
4577  /// \brief Creates an empty clause with the place for \a NumVars variables.
4578  ///
4579  /// \param C AST context.
4580  /// \param NumVars Number of expressions listed in the clause.
4581  /// \param NumUniqueDeclarations Number of unique base declarations in this
4582  /// clause.
4583  /// \param NumComponentLists Number of unique base declarations in this
4584  /// clause.
4585  /// \param NumComponents Total number of expression components in the clause.
4586  ///
4587  static OMPToClause *CreateEmpty(const ASTContext &C, unsigned NumVars,
4588                                  unsigned NumUniqueDeclarations,
4589                                  unsigned NumComponentLists,
4590                                  unsigned NumComponents);
4591
4592  static bool classof(const OMPClause *T) {
4593    return T->getClauseKind() == OMPC_to;
4594  }
4595
4596  child_range children() {
4597    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
4598                       reinterpret_cast<Stmt **>(varlist_end()));
4599  }
4600};
4601
4602/// \brief This represents clause 'from' in the '#pragma omp ...'
4603/// directives.
4604///
4605/// \code
4606/// #pragma omp target update from(a,b)
4607/// \endcode
4608/// In this example directive '#pragma omp target update' has clause 'from'
4609/// with the variables 'a' and 'b'.
4610///
4611class OMPFromClause final
4612    : public OMPMappableExprListClause<OMPFromClause>,
4613      private llvm::TrailingObjects<
4614          OMPFromClause, Expr *, ValueDecl *, unsigned,
4615          OMPClauseMappableExprCommon::MappableComponent> {
4616  friend TrailingObjects;
4617  friend OMPVarListClause;
4618  friend OMPMappableExprListClause;
4619  friend class OMPClauseReader;
4620
4621  /// Define the sizes of each trailing object array except the last one. This
4622  /// is required for TrailingObjects to work properly.
4623  size_t numTrailingObjects(OverloadToken<Expr *>) const {
4624    return varlist_size();
4625  }
4626  size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
4627    return getUniqueDeclarationsNum();
4628  }
4629  size_t numTrailingObjects(OverloadToken<unsigned>) const {
4630    return getUniqueDeclarationsNum() + getTotalComponentListNum();
4631  }
4632
4633  /// \brief Build clause with number of variables \a NumVars.
4634  ///
4635  /// \param StartLoc Starting location of the clause.
4636  /// \param EndLoc Ending location of the clause.
4637  /// \param NumVars Number of expressions listed in this clause.
4638  /// \param NumUniqueDeclarations Number of unique base declarations in this
4639  /// clause.
4640  /// \param NumComponentLists Number of component lists in this clause.
4641  /// \param NumComponents Total number of expression components in the clause.
4642  ///
4643  explicit OMPFromClause(SourceLocation StartLoc, SourceLocation LParenLoc,
4644                         SourceLocation EndLoc, unsigned NumVars,
4645                         unsigned NumUniqueDeclarations,
4646                         unsigned NumComponentLists, unsigned NumComponents)
4647      : OMPMappableExprListClause(OMPC_from, StartLoc, LParenLoc, EndLoc,
4648                                  NumVars, NumUniqueDeclarations,
4649                                  NumComponentLists, NumComponents) {}
4650
4651  /// \brief Build an empty clause.
4652  ///
4653  /// \param NumVars Number of expressions listed in this clause.
4654  /// \param NumUniqueDeclarations Number of unique base declarations in this
4655  /// clause.
4656  /// \param NumComponentLists Number of component lists in this clause.
4657  /// \param NumComponents Total number of expression components in the clause.
4658  ///
4659  explicit OMPFromClause(unsigned NumVars, unsigned NumUniqueDeclarations,
4660                         unsigned NumComponentLists, unsigned NumComponents)
4661      : OMPMappableExprListClause(
4662            OMPC_from, SourceLocation(), SourceLocation(), SourceLocation(),
4663            NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents) {}
4664
4665public:
4666  /// \brief Creates clause with a list of variables \a Vars.
4667  ///
4668  /// \param C AST context.
4669  /// \param StartLoc Starting location of the clause.
4670  /// \param EndLoc Ending location of the clause.
4671  /// \param Vars The original expression used in the clause.
4672  /// \param Declarations Declarations used in the clause.
4673  /// \param ComponentLists Component lists used in the clause.
4674  ///
4675  static OMPFromClause *Create(const ASTContext &C, SourceLocation StartLoc,
4676                               SourceLocation LParenLoc, SourceLocation EndLoc,
4677                               ArrayRef<Expr *> Vars,
4678                               ArrayRef<ValueDecl *> Declarations,
4679                               MappableExprComponentListsRef ComponentLists);
4680
4681  /// \brief Creates an empty clause with the place for \a NumVars variables.
4682  ///
4683  /// \param C AST context.
4684  /// \param NumVars Number of expressions listed in the clause.
4685  /// \param NumUniqueDeclarations Number of unique base declarations in this
4686  /// clause.
4687  /// \param NumComponentLists Number of unique base declarations in this
4688  /// clause.
4689  /// \param NumComponents Total number of expression components in the clause.
4690  ///
4691  static OMPFromClause *CreateEmpty(const ASTContext &C, unsigned NumVars,
4692                                    unsigned NumUniqueDeclarations,
4693                                    unsigned NumComponentLists,
4694                                    unsigned NumComponents);
4695
4696  static bool classof(const OMPClause *T) {
4697    return T->getClauseKind() == OMPC_from;
4698  }
4699
4700  child_range children() {
4701    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
4702                       reinterpret_cast<Stmt **>(varlist_end()));
4703  }
4704};
4705
4706/// This represents clause 'use_device_ptr' in the '#pragma omp ...'
4707/// directives.
4708///
4709/// \code
4710/// #pragma omp target data use_device_ptr(a,b)
4711/// \endcode
4712/// In this example directive '#pragma omp target data' has clause
4713/// 'use_device_ptr' with the variables 'a' and 'b'.
4714///
4715class OMPUseDevicePtrClause final
4716    : public OMPMappableExprListClause<OMPUseDevicePtrClause>,
4717      private llvm::TrailingObjects<
4718          OMPUseDevicePtrClause, Expr *, ValueDecl *, unsigned,
4719          OMPClauseMappableExprCommon::MappableComponent> {
4720  friend TrailingObjects;
4721  friend OMPVarListClause;
4722  friend OMPMappableExprListClause;
4723  friend class OMPClauseReader;
4724
4725  /// Define the sizes of each trailing object array except the last one. This
4726  /// is required for TrailingObjects to work properly.
4727  size_t numTrailingObjects(OverloadToken<Expr *>) const {
4728    return 3 * varlist_size();
4729  }
4730  size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
4731    return getUniqueDeclarationsNum();
4732  }
4733  size_t numTrailingObjects(OverloadToken<unsigned>) const {
4734    return getUniqueDeclarationsNum() + getTotalComponentListNum();
4735  }
4736
4737  /// Build clause with number of variables \a NumVars.
4738  ///
4739  /// \param StartLoc Starting location of the clause.
4740  /// \param EndLoc Ending location of the clause.
4741  /// \param NumVars Number of expressions listed in this clause.
4742  /// \param NumUniqueDeclarations Number of unique base declarations in this
4743  /// clause.
4744  /// \param NumComponentLists Number of component lists in this clause.
4745  /// \param NumComponents Total number of expression components in the clause.
4746  ///
4747  explicit OMPUseDevicePtrClause(SourceLocation StartLoc,
4748                                 SourceLocation LParenLoc,
4749                                 SourceLocation EndLoc, unsigned NumVars,
4750                                 unsigned NumUniqueDeclarations,
4751                                 unsigned NumComponentLists,
4752                                 unsigned NumComponents)
4753      : OMPMappableExprListClause(OMPC_use_device_ptr, StartLoc, LParenLoc,
4754                                  EndLoc, NumVars, NumUniqueDeclarations,
4755                                  NumComponentLists, NumComponents) {}
4756
4757  /// Build an empty clause.
4758  ///
4759  /// \param NumVars Number of expressions listed in this clause.
4760  /// \param NumUniqueDeclarations Number of unique base declarations in this
4761  /// clause.
4762  /// \param NumComponentLists Number of component lists in this clause.
4763  /// \param NumComponents Total number of expression components in the clause.
4764  ///
4765  explicit OMPUseDevicePtrClause(unsigned NumVars,
4766                                 unsigned NumUniqueDeclarations,
4767                                 unsigned NumComponentLists,
4768                                 unsigned NumComponents)
4769      : OMPMappableExprListClause(OMPC_use_device_ptr, SourceLocation(),
4770                                  SourceLocation(), SourceLocation(), NumVars,
4771                                  NumUniqueDeclarations, NumComponentLists,
4772                                  NumComponents) {}
4773
4774  /// Sets the list of references to private copies with initializers for new
4775  /// private variables.
4776  /// \param VL List of references.
4777  void setPrivateCopies(ArrayRef<Expr *> VL);
4778
4779  /// Gets the list of references to private copies with initializers for new
4780  /// private variables.
4781  MutableArrayRef<Expr *> getPrivateCopies() {
4782    return MutableArrayRef<Expr *>(varlist_end(), varlist_size());
4783  }
4784  ArrayRef<const Expr *> getPrivateCopies() const {
4785    return llvm::makeArrayRef(varlist_end(), varlist_size());
4786  }
4787
4788  /// Sets the list of references to initializer variables for new private
4789  /// variables.
4790  /// \param VL List of references.
4791  void setInits(ArrayRef<Expr *> VL);
4792
4793  /// Gets the list of references to initializer variables for new private
4794  /// variables.
4795  MutableArrayRef<Expr *> getInits() {
4796    return MutableArrayRef<Expr *>(getPrivateCopies().end(), varlist_size());
4797  }
4798  ArrayRef<const Expr *> getInits() const {
4799    return llvm::makeArrayRef(getPrivateCopies().end(), varlist_size());
4800  }
4801
4802public:
4803  /// Creates clause with a list of variables \a Vars.
4804  ///
4805  /// \param C AST context.
4806  /// \param StartLoc Starting location of the clause.
4807  /// \param EndLoc Ending location of the clause.
4808  /// \param Vars The original expression used in the clause.
4809  /// \param PrivateVars Expressions referring to private copies.
4810  /// \param Inits Expressions referring to private copy initializers.
4811  /// \param Declarations Declarations used in the clause.
4812  /// \param ComponentLists Component lists used in the clause.
4813  ///
4814  static OMPUseDevicePtrClause *
4815  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
4816         SourceLocation EndLoc, ArrayRef<Expr *> Vars,
4817         ArrayRef<Expr *> PrivateVars, ArrayRef<Expr *> Inits,
4818         ArrayRef<ValueDecl *> Declarations,
4819         MappableExprComponentListsRef ComponentLists);
4820
4821  /// Creates an empty clause with the place for \a NumVars variables.
4822  ///
4823  /// \param C AST context.
4824  /// \param NumVars Number of expressions listed in the clause.
4825  /// \param NumUniqueDeclarations Number of unique base declarations in this
4826  /// clause.
4827  /// \param NumComponentLists Number of unique base declarations in this
4828  /// clause.
4829  /// \param NumComponents Total number of expression components in the clause.
4830  ///
4831  static OMPUseDevicePtrClause *CreateEmpty(const ASTContext &C,
4832                                            unsigned NumVars,
4833                                            unsigned NumUniqueDeclarations,
4834                                            unsigned NumComponentLists,
4835                                            unsigned NumComponents);
4836
4837  typedef MutableArrayRef<Expr *>::iterator private_copies_iterator;
4838  typedef ArrayRef<const Expr *>::iterator private_copies_const_iterator;
4839  typedef llvm::iterator_range<private_copies_iterator> private_copies_range;
4840  typedef llvm::iterator_range<private_copies_const_iterator>
4841      private_copies_const_range;
4842
4843  private_copies_range private_copies() {
4844    return private_copies_range(getPrivateCopies().begin(),
4845                                getPrivateCopies().end());
4846  }
4847  private_copies_const_range private_copies() const {
4848    return private_copies_const_range(getPrivateCopies().begin(),
4849                                      getPrivateCopies().end());
4850  }
4851
4852  typedef MutableArrayRef<Expr *>::iterator inits_iterator;
4853  typedef ArrayRef<const Expr *>::iterator inits_const_iterator;
4854  typedef llvm::iterator_range<inits_iterator> inits_range;
4855  typedef llvm::iterator_range<inits_const_iterator> inits_const_range;
4856
4857  inits_range inits() {
4858    return inits_range(getInits().begin(), getInits().end());
4859  }
4860  inits_const_range inits() const {
4861    return inits_const_range(getInits().begin(), getInits().end());
4862  }
4863
4864  child_range children() {
4865    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
4866                       reinterpret_cast<Stmt **>(varlist_end()));
4867  }
4868
4869  static bool classof(const OMPClause *T) {
4870    return T->getClauseKind() == OMPC_use_device_ptr;
4871  }
4872};
4873
4874/// This represents clause 'is_device_ptr' in the '#pragma omp ...'
4875/// directives.
4876///
4877/// \code
4878/// #pragma omp target is_device_ptr(a,b)
4879/// \endcode
4880/// In this example directive '#pragma omp target' has clause
4881/// 'is_device_ptr' with the variables 'a' and 'b'.
4882///
4883class OMPIsDevicePtrClause final
4884    : public OMPMappableExprListClause<OMPIsDevicePtrClause>,
4885      private llvm::TrailingObjects<
4886          OMPIsDevicePtrClause, Expr *, ValueDecl *, unsigned,
4887          OMPClauseMappableExprCommon::MappableComponent> {
4888  friend TrailingObjects;
4889  friend OMPVarListClause;
4890  friend OMPMappableExprListClause;
4891  friend class OMPClauseReader;
4892
4893  /// Define the sizes of each trailing object array except the last one. This
4894  /// is required for TrailingObjects to work properly.
4895  size_t numTrailingObjects(OverloadToken<Expr *>) const {
4896    return varlist_size();
4897  }
4898  size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
4899    return getUniqueDeclarationsNum();
4900  }
4901  size_t numTrailingObjects(OverloadToken<unsigned>) const {
4902    return getUniqueDeclarationsNum() + getTotalComponentListNum();
4903  }
4904  /// Build clause with number of variables \a NumVars.
4905  ///
4906  /// \param StartLoc Starting location of the clause.
4907  /// \param EndLoc Ending location of the clause.
4908  /// \param NumVars Number of expressions listed in this clause.
4909  /// \param NumUniqueDeclarations Number of unique base declarations in this
4910  /// clause.
4911  /// \param NumComponentLists Number of component lists in this clause.
4912  /// \param NumComponents Total number of expression components in the clause.
4913  ///
4914  explicit OMPIsDevicePtrClause(SourceLocation StartLoc,
4915                                SourceLocation LParenLoc, SourceLocation EndLoc,
4916                                unsigned NumVars,
4917                                unsigned NumUniqueDeclarations,
4918                                unsigned NumComponentLists,
4919                                unsigned NumComponents)
4920      : OMPMappableExprListClause(OMPC_is_device_ptr, StartLoc, LParenLoc,
4921                                  EndLoc, NumVars, NumUniqueDeclarations,
4922                                  NumComponentLists, NumComponents) {}
4923
4924  /// Build an empty clause.
4925  ///
4926  /// \param NumVars Number of expressions listed in this clause.
4927  /// \param NumUniqueDeclarations Number of unique base declarations in this
4928  /// clause.
4929  /// \param NumComponentLists Number of component lists in this clause.
4930  /// \param NumComponents Total number of expression components in the clause.
4931  ///
4932  explicit OMPIsDevicePtrClause(unsigned NumVars,
4933                                unsigned NumUniqueDeclarations,
4934                                unsigned NumComponentLists,
4935                                unsigned NumComponents)
4936      : OMPMappableExprListClause(OMPC_is_device_ptr, SourceLocation(),
4937                                  SourceLocation(), SourceLocation(), NumVars,
4938                                  NumUniqueDeclarations, NumComponentLists,
4939                                  NumComponents) {}
4940
4941public:
4942  /// Creates clause with a list of variables \a Vars.
4943  ///
4944  /// \param C AST context.
4945  /// \param StartLoc Starting location of the clause.
4946  /// \param EndLoc Ending location of the clause.
4947  /// \param Vars The original expression used in the clause.
4948  /// \param Declarations Declarations used in the clause.
4949  /// \param ComponentLists Component lists used in the clause.
4950  ///
4951  static OMPIsDevicePtrClause *
4952  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
4953         SourceLocation EndLoc, ArrayRef<Expr *> Vars,
4954         ArrayRef<ValueDecl *> Declarations,
4955         MappableExprComponentListsRef ComponentLists);
4956
4957  /// Creates an empty clause with the place for \a NumVars variables.
4958  ///
4959  /// \param C AST context.
4960  /// \param NumVars Number of expressions listed in the clause.
4961  /// \param NumUniqueDeclarations Number of unique base declarations in this
4962  /// clause.
4963  /// \param NumComponentLists Number of unique base declarations in this
4964  /// clause.
4965  /// \param NumComponents Total number of expression components in the clause.
4966  ///
4967  static OMPIsDevicePtrClause *CreateEmpty(const ASTContext &C,
4968                                           unsigned NumVars,
4969                                           unsigned NumUniqueDeclarations,
4970                                           unsigned NumComponentLists,
4971                                           unsigned NumComponents);
4972
4973  child_range children() {
4974    return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
4975                       reinterpret_cast<Stmt **>(varlist_end()));
4976  }
4977
4978  static bool classof(const OMPClause *T) {
4979    return T->getClauseKind() == OMPC_is_device_ptr;
4980  }
4981};
4982} // end namespace clang
4983
4984#endif // LLVM_CLANG_AST_OPENMPCLAUSE_H
4985