OpenMPClause.h revision 651f13cea278ec967336033dd032faef0e9fc2ec
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
24namespace clang {
25
26//===----------------------------------------------------------------------===//
27// AST classes for clauses.
28//===----------------------------------------------------------------------===//
29
30/// \brief This is a basic class for representing single OpenMP clause.
31///
32class OMPClause {
33  /// \brief Starting location of the clause (the clause keyword).
34  SourceLocation StartLoc;
35  /// \brief Ending location of the clause.
36  SourceLocation EndLoc;
37  /// \brief Kind of the clause.
38  OpenMPClauseKind Kind;
39
40protected:
41  OMPClause(OpenMPClauseKind K, SourceLocation StartLoc, SourceLocation EndLoc)
42      : StartLoc(StartLoc), EndLoc(EndLoc), Kind(K) {}
43
44public:
45  /// \brief Returns the starting location of the clause.
46  SourceLocation getLocStart() const { return StartLoc; }
47  /// \brief Returns the ending location of the clause.
48  SourceLocation getLocEnd() const { return EndLoc; }
49
50  /// \brief Sets the starting location of the clause.
51  void setLocStart(SourceLocation Loc) { StartLoc = Loc; }
52  /// \brief Sets the ending location of the clause.
53  void setLocEnd(SourceLocation Loc) { EndLoc = Loc; }
54
55  /// \brief Returns kind of OpenMP clause (private, shared, reduction, etc.).
56  OpenMPClauseKind getClauseKind() const { return Kind; }
57
58  bool isImplicit() const { return StartLoc.isInvalid(); }
59
60  StmtRange children();
61  ConstStmtRange children() const {
62    return const_cast<OMPClause *>(this)->children();
63  }
64  static bool classof(const OMPClause *T) { return true; }
65};
66
67/// \brief This represents clauses with the list of variables like 'private',
68/// 'firstprivate', 'copyin', 'shared', or 'reduction' clauses in the
69/// '#pragma omp ...' directives.
70template <class T> class OMPVarListClause : public OMPClause {
71  friend class OMPClauseReader;
72  /// \brief Location of '('.
73  SourceLocation LParenLoc;
74  /// \brief Number of variables in the list.
75  unsigned NumVars;
76
77protected:
78  /// \brief Fetches list of variables associated with this clause.
79  llvm::MutableArrayRef<Expr *> getVarRefs() {
80    return llvm::MutableArrayRef<Expr *>(
81        reinterpret_cast<Expr **>(
82            reinterpret_cast<char *>(this) +
83            llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf<Expr *>())),
84        NumVars);
85  }
86
87  /// \brief Sets the list of variables for this clause.
88  void setVarRefs(ArrayRef<Expr *> VL) {
89    assert(VL.size() == NumVars &&
90           "Number of variables is not the same as the preallocated buffer");
91    std::copy(
92        VL.begin(), VL.end(),
93        reinterpret_cast<Expr **>(
94            reinterpret_cast<char *>(this) +
95            llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf<Expr *>())));
96  }
97
98  /// \brief Build a clause with \a N variables
99  ///
100  /// \param K Kind of the clause.
101  /// \param StartLoc Starting location of the clause (the clause keyword).
102  /// \param LParenLoc Location of '('.
103  /// \param EndLoc Ending location of the clause.
104  /// \param N Number of the variables in the clause.
105  ///
106  OMPVarListClause(OpenMPClauseKind K, SourceLocation StartLoc,
107                   SourceLocation LParenLoc, SourceLocation EndLoc, unsigned N)
108      : OMPClause(K, StartLoc, EndLoc), LParenLoc(LParenLoc), NumVars(N) {}
109
110public:
111  typedef llvm::MutableArrayRef<Expr *>::iterator varlist_iterator;
112  typedef ArrayRef<const Expr *>::iterator varlist_const_iterator;
113  typedef llvm::iterator_range<varlist_iterator> varlist_range;
114  typedef llvm::iterator_range<varlist_const_iterator> varlist_const_range;
115
116  unsigned varlist_size() const { return NumVars; }
117  bool varlist_empty() const { return NumVars == 0; }
118
119  varlist_range varlists() {
120    return varlist_range(varlist_begin(), varlist_end());
121  }
122  varlist_const_range varlists() const {
123    return varlist_const_range(varlist_begin(), varlist_end());
124  }
125
126  varlist_iterator varlist_begin() { return getVarRefs().begin(); }
127  varlist_iterator varlist_end() { return getVarRefs().end(); }
128  varlist_const_iterator varlist_begin() const { return getVarRefs().begin(); }
129  varlist_const_iterator varlist_end() const { return getVarRefs().end(); }
130
131  /// \brief Sets the location of '('.
132  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
133  /// \brief Returns the location of '('.
134  SourceLocation getLParenLoc() const { return LParenLoc; }
135
136  /// \brief Fetches list of all variables in the clause.
137  ArrayRef<const Expr *> getVarRefs() const {
138    return ArrayRef<const Expr *>(
139        reinterpret_cast<const Expr *const *>(
140            reinterpret_cast<const char *>(this) +
141            llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf<Expr *>())),
142        NumVars);
143  }
144};
145
146/// \brief This represents 'if' clause in the '#pragma omp ...' directive.
147///
148/// \code
149/// #pragma omp parallel if(a > 5)
150/// \endcode
151/// In this example directive '#pragma omp parallel' has simple 'if'
152/// clause with condition 'a > 5'.
153///
154class OMPIfClause : public OMPClause {
155  friend class OMPClauseReader;
156  /// \brief Location of '('.
157  SourceLocation LParenLoc;
158  /// \brief Condition of the 'if' clause.
159  Stmt *Condition;
160
161  /// \brief Set condition.
162  ///
163  void setCondition(Expr *Cond) { Condition = Cond; }
164
165public:
166  /// \brief Build 'if' clause with condition \a Cond.
167  ///
168  /// \param StartLoc Starting location of the clause.
169  /// \param LParenLoc Location of '('.
170  /// \param Cond Condition of the clause.
171  /// \param EndLoc Ending location of the clause.
172  ///
173  OMPIfClause(Expr *Cond, SourceLocation StartLoc, SourceLocation LParenLoc,
174              SourceLocation EndLoc)
175      : OMPClause(OMPC_if, StartLoc, EndLoc), LParenLoc(LParenLoc),
176        Condition(Cond) {}
177
178  /// \brief Build an empty clause.
179  ///
180  OMPIfClause()
181      : OMPClause(OMPC_if, SourceLocation(), SourceLocation()),
182        LParenLoc(SourceLocation()), Condition(0) {}
183
184  /// \brief Sets the location of '('.
185  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
186  /// \brief Returns the location of '('.
187  SourceLocation getLParenLoc() const { return LParenLoc; }
188
189  /// \brief Returns condition.
190  Expr *getCondition() const { return cast_or_null<Expr>(Condition); }
191
192  static bool classof(const OMPClause *T) {
193    return T->getClauseKind() == OMPC_if;
194  }
195
196  StmtRange children() { return StmtRange(&Condition, &Condition + 1); }
197};
198
199/// \brief This represents 'num_threads' clause in the '#pragma omp ...'
200/// directive.
201///
202/// \code
203/// #pragma omp parallel num_threads(6)
204/// \endcode
205/// In this example directive '#pragma omp parallel' has simple 'num_threads'
206/// clause with number of threads '6'.
207///
208class OMPNumThreadsClause : public OMPClause {
209  friend class OMPClauseReader;
210  /// \brief Location of '('.
211  SourceLocation LParenLoc;
212  /// \brief Condition of the 'num_threads' clause.
213  Stmt *NumThreads;
214
215  /// \brief Set condition.
216  ///
217  void setNumThreads(Expr *NThreads) { NumThreads = NThreads; }
218
219public:
220  /// \brief Build 'num_threads' clause with condition \a NumThreads.
221  ///
222  /// \param NumThreads Number of threads for the construct.
223  /// \param StartLoc Starting location of the clause.
224  /// \param LParenLoc Location of '('.
225  /// \param EndLoc Ending location of the clause.
226  ///
227  OMPNumThreadsClause(Expr *NumThreads, SourceLocation StartLoc,
228                      SourceLocation LParenLoc, SourceLocation EndLoc)
229      : OMPClause(OMPC_num_threads, StartLoc, EndLoc), LParenLoc(LParenLoc),
230        NumThreads(NumThreads) {}
231
232  /// \brief Build an empty clause.
233  ///
234  OMPNumThreadsClause()
235      : OMPClause(OMPC_num_threads, SourceLocation(), SourceLocation()),
236        LParenLoc(SourceLocation()), NumThreads(0) {}
237
238  /// \brief Sets the location of '('.
239  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
240  /// \brief Returns the location of '('.
241  SourceLocation getLParenLoc() const { return LParenLoc; }
242
243  /// \brief Returns number of threads.
244  Expr *getNumThreads() const { return cast_or_null<Expr>(NumThreads); }
245
246  static bool classof(const OMPClause *T) {
247    return T->getClauseKind() == OMPC_num_threads;
248  }
249
250  StmtRange children() { return StmtRange(&NumThreads, &NumThreads + 1); }
251};
252
253/// \brief This represents 'safelen' clause in the '#pragma omp ...'
254/// directive.
255///
256/// \code
257/// #pragma omp simd safelen(4)
258/// \endcode
259/// In this example directive '#pragma omp simd' has clause 'safelen'
260/// with single expression '4'.
261/// If the safelen clause is used then no two iterations executed
262/// concurrently with SIMD instructions can have a greater distance
263/// in the logical iteration space than its value. The parameter of
264/// the safelen clause must be a constant positive integer expression.
265///
266class OMPSafelenClause : public OMPClause {
267  friend class OMPClauseReader;
268  /// \brief Location of '('.
269  SourceLocation LParenLoc;
270  /// \brief Safe iteration space distance.
271  Stmt *Safelen;
272
273  /// \brief Set safelen.
274  void setSafelen(Expr *Len) { Safelen = Len; }
275
276public:
277  /// \brief Build 'safelen' clause.
278  ///
279  /// \param Len Expression associated with this clause.
280  /// \param StartLoc Starting location of the clause.
281  /// \param EndLoc Ending location of the clause.
282  ///
283  OMPSafelenClause(Expr *Len, SourceLocation StartLoc, SourceLocation LParenLoc,
284                   SourceLocation EndLoc)
285      : OMPClause(OMPC_safelen, StartLoc, EndLoc), LParenLoc(LParenLoc),
286        Safelen(Len) {}
287
288  /// \brief Build an empty clause.
289  ///
290  explicit OMPSafelenClause()
291      : OMPClause(OMPC_safelen, SourceLocation(), SourceLocation()),
292        LParenLoc(SourceLocation()), Safelen(0) {}
293
294  /// \brief Sets the location of '('.
295  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
296  /// \brief Returns the location of '('.
297  SourceLocation getLParenLoc() const { return LParenLoc; }
298
299  /// \brief Return safe iteration space distance.
300  Expr *getSafelen() const { return cast_or_null<Expr>(Safelen); }
301
302  static bool classof(const OMPClause *T) {
303    return T->getClauseKind() == OMPC_safelen;
304  }
305
306  StmtRange children() { return StmtRange(&Safelen, &Safelen + 1); }
307};
308
309/// \brief This represents 'default' clause in the '#pragma omp ...' directive.
310///
311/// \code
312/// #pragma omp parallel default(shared)
313/// \endcode
314/// In this example directive '#pragma omp parallel' has simple 'default'
315/// clause with kind 'shared'.
316///
317class OMPDefaultClause : public OMPClause {
318  friend class OMPClauseReader;
319  /// \brief Location of '('.
320  SourceLocation LParenLoc;
321  /// \brief A kind of the 'default' clause.
322  OpenMPDefaultClauseKind Kind;
323  /// \brief Start location of the kind in source code.
324  SourceLocation KindKwLoc;
325
326  /// \brief Set kind of the clauses.
327  ///
328  /// \param K Argument of clause.
329  ///
330  void setDefaultKind(OpenMPDefaultClauseKind K) { Kind = K; }
331
332  /// \brief Set argument location.
333  ///
334  /// \param KLoc Argument location.
335  ///
336  void setDefaultKindKwLoc(SourceLocation KLoc) { KindKwLoc = KLoc; }
337
338public:
339  /// \brief Build 'default' clause with argument \a A ('none' or 'shared').
340  ///
341  /// \param A Argument of the clause ('none' or 'shared').
342  /// \param ALoc Starting location of the argument.
343  /// \param StartLoc Starting location of the clause.
344  /// \param LParenLoc Location of '('.
345  /// \param EndLoc Ending location of the clause.
346  ///
347  OMPDefaultClause(OpenMPDefaultClauseKind A, SourceLocation ALoc,
348                   SourceLocation StartLoc, SourceLocation LParenLoc,
349                   SourceLocation EndLoc)
350      : OMPClause(OMPC_default, StartLoc, EndLoc), LParenLoc(LParenLoc),
351        Kind(A), KindKwLoc(ALoc) {}
352
353  /// \brief Build an empty clause.
354  ///
355  OMPDefaultClause()
356      : OMPClause(OMPC_default, SourceLocation(), SourceLocation()),
357        LParenLoc(SourceLocation()), Kind(OMPC_DEFAULT_unknown),
358        KindKwLoc(SourceLocation()) {}
359
360  /// \brief Sets the location of '('.
361  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
362  /// \brief Returns the location of '('.
363  SourceLocation getLParenLoc() const { return LParenLoc; }
364
365  /// \brief Returns kind of the clause.
366  OpenMPDefaultClauseKind getDefaultKind() const { return Kind; }
367
368  /// \brief Returns location of clause kind.
369  SourceLocation getDefaultKindKwLoc() const { return KindKwLoc; }
370
371  static bool classof(const OMPClause *T) {
372    return T->getClauseKind() == OMPC_default;
373  }
374
375  StmtRange children() { return StmtRange(); }
376};
377
378/// \brief This represents clause 'private' in the '#pragma omp ...' directives.
379///
380/// \code
381/// #pragma omp parallel private(a,b)
382/// \endcode
383/// In this example directive '#pragma omp parallel' has clause 'private'
384/// with the variables 'a' and 'b'.
385///
386class OMPPrivateClause : public OMPVarListClause<OMPPrivateClause> {
387  /// \brief Build clause with number of variables \a N.
388  ///
389  /// \param StartLoc Starting location of the clause.
390  /// \param LParenLoc Location of '('.
391  /// \param EndLoc Ending location of the clause.
392  /// \param N Number of the variables in the clause.
393  ///
394  OMPPrivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
395                   SourceLocation EndLoc, unsigned N)
396      : OMPVarListClause<OMPPrivateClause>(OMPC_private, StartLoc, LParenLoc,
397                                           EndLoc, N) {}
398
399  /// \brief Build an empty clause.
400  ///
401  /// \param N Number of variables.
402  ///
403  explicit OMPPrivateClause(unsigned N)
404      : OMPVarListClause<OMPPrivateClause>(OMPC_private, SourceLocation(),
405                                           SourceLocation(), SourceLocation(),
406                                           N) {}
407
408public:
409  /// \brief Creates clause with a list of variables \a VL.
410  ///
411  /// \param C AST context.
412  /// \param StartLoc Starting location of the clause.
413  /// \param LParenLoc Location of '('.
414  /// \param EndLoc Ending location of the clause.
415  /// \param VL List of references to the variables.
416  ///
417  static OMPPrivateClause *Create(const ASTContext &C, SourceLocation StartLoc,
418                                  SourceLocation LParenLoc,
419                                  SourceLocation EndLoc, ArrayRef<Expr *> VL);
420  /// \brief Creates an empty clause with the place for \a N variables.
421  ///
422  /// \param C AST context.
423  /// \param N The number of variables.
424  ///
425  static OMPPrivateClause *CreateEmpty(const ASTContext &C, unsigned N);
426
427  StmtRange children() {
428    return StmtRange(reinterpret_cast<Stmt **>(varlist_begin()),
429                     reinterpret_cast<Stmt **>(varlist_end()));
430  }
431
432  static bool classof(const OMPClause *T) {
433    return T->getClauseKind() == OMPC_private;
434  }
435};
436
437/// \brief This represents clause 'firstprivate' in the '#pragma omp ...'
438/// directives.
439///
440/// \code
441/// #pragma omp parallel firstprivate(a,b)
442/// \endcode
443/// In this example directive '#pragma omp parallel' has clause 'firstprivate'
444/// with the variables 'a' and 'b'.
445///
446class OMPFirstprivateClause : public OMPVarListClause<OMPFirstprivateClause> {
447  /// \brief Build clause with number of variables \a N.
448  ///
449  /// \param StartLoc Starting location of the clause.
450  /// \param LParenLoc Location of '('.
451  /// \param EndLoc Ending location of the clause.
452  /// \param N Number of the variables in the clause.
453  ///
454  OMPFirstprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
455                        SourceLocation EndLoc, unsigned N)
456      : OMPVarListClause<OMPFirstprivateClause>(OMPC_firstprivate, StartLoc,
457                                                LParenLoc, EndLoc, N) {}
458
459  /// \brief Build an empty clause.
460  ///
461  /// \param N Number of variables.
462  ///
463  explicit OMPFirstprivateClause(unsigned N)
464      : OMPVarListClause<OMPFirstprivateClause>(
465            OMPC_firstprivate, SourceLocation(), SourceLocation(),
466            SourceLocation(), N) {}
467
468public:
469  /// \brief Creates clause with a list of variables \a VL.
470  ///
471  /// \param C AST context.
472  /// \param StartLoc Starting location of the clause.
473  /// \param LParenLoc Location of '('.
474  /// \param EndLoc Ending location of the clause.
475  /// \param VL List of references to the variables.
476  ///
477  static OMPFirstprivateClause *
478  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
479         SourceLocation EndLoc, ArrayRef<Expr *> VL);
480  /// \brief Creates an empty clause with the place for \a N variables.
481  ///
482  /// \param C AST context.
483  /// \param N The number of variables.
484  ///
485  static OMPFirstprivateClause *CreateEmpty(const ASTContext &C, unsigned N);
486
487  StmtRange children() {
488    return StmtRange(reinterpret_cast<Stmt **>(varlist_begin()),
489                     reinterpret_cast<Stmt **>(varlist_end()));
490  }
491
492  static bool classof(const OMPClause *T) {
493    return T->getClauseKind() == OMPC_firstprivate;
494  }
495};
496
497/// \brief This represents clause 'shared' in the '#pragma omp ...' directives.
498///
499/// \code
500/// #pragma omp parallel shared(a,b)
501/// \endcode
502/// In this example directive '#pragma omp parallel' has clause 'shared'
503/// with the variables 'a' and 'b'.
504///
505class OMPSharedClause : public OMPVarListClause<OMPSharedClause> {
506  /// \brief Build clause with number of variables \a N.
507  ///
508  /// \param StartLoc Starting location of the clause.
509  /// \param LParenLoc Location of '('.
510  /// \param EndLoc Ending location of the clause.
511  /// \param N Number of the variables in the clause.
512  ///
513  OMPSharedClause(SourceLocation StartLoc, SourceLocation LParenLoc,
514                  SourceLocation EndLoc, unsigned N)
515      : OMPVarListClause<OMPSharedClause>(OMPC_shared, StartLoc, LParenLoc,
516                                          EndLoc, N) {}
517
518  /// \brief Build an empty clause.
519  ///
520  /// \param N Number of variables.
521  ///
522  explicit OMPSharedClause(unsigned N)
523      : OMPVarListClause<OMPSharedClause>(OMPC_shared, SourceLocation(),
524                                          SourceLocation(), SourceLocation(),
525                                          N) {}
526
527public:
528  /// \brief Creates clause with a list of variables \a VL.
529  ///
530  /// \param C AST context.
531  /// \param StartLoc Starting location of the clause.
532  /// \param LParenLoc Location of '('.
533  /// \param EndLoc Ending location of the clause.
534  /// \param VL List of references to the variables.
535  ///
536  static OMPSharedClause *Create(const ASTContext &C, SourceLocation StartLoc,
537                                 SourceLocation LParenLoc,
538                                 SourceLocation EndLoc, ArrayRef<Expr *> VL);
539  /// \brief Creates an empty clause with \a N variables.
540  ///
541  /// \param C AST context.
542  /// \param N The number of variables.
543  ///
544  static OMPSharedClause *CreateEmpty(const ASTContext &C, unsigned N);
545
546  StmtRange children() {
547    return StmtRange(reinterpret_cast<Stmt **>(varlist_begin()),
548                     reinterpret_cast<Stmt **>(varlist_end()));
549  }
550
551  static bool classof(const OMPClause *T) {
552    return T->getClauseKind() == OMPC_shared;
553  }
554};
555
556/// \brief This represents clause 'copyin' in the '#pragma omp ...' directives.
557///
558/// \code
559/// #pragma omp parallel copyin(a,b)
560/// \endcode
561/// In this example directive '#pragma omp parallel' has clause 'copyin'
562/// with the variables 'a' and 'b'.
563///
564class OMPCopyinClause : public OMPVarListClause<OMPCopyinClause> {
565  /// \brief Build clause with number of variables \a N.
566  ///
567  /// \param StartLoc Starting location of the clause.
568  /// \param LParenLoc Location of '('.
569  /// \param EndLoc Ending location of the clause.
570  /// \param N Number of the variables in the clause.
571  ///
572  OMPCopyinClause(SourceLocation StartLoc, SourceLocation LParenLoc,
573                  SourceLocation EndLoc, unsigned N)
574      : OMPVarListClause<OMPCopyinClause>(OMPC_copyin, StartLoc, LParenLoc,
575                                          EndLoc, N) {}
576
577  /// \brief Build an empty clause.
578  ///
579  /// \param N Number of variables.
580  ///
581  explicit OMPCopyinClause(unsigned N)
582      : OMPVarListClause<OMPCopyinClause>(OMPC_copyin, SourceLocation(),
583                                          SourceLocation(), SourceLocation(),
584                                          N) {}
585
586public:
587  /// \brief Creates clause with a list of variables \a VL.
588  ///
589  /// \param C AST context.
590  /// \param StartLoc Starting location of the clause.
591  /// \param LParenLoc Location of '('.
592  /// \param EndLoc Ending location of the clause.
593  /// \param VL List of references to the variables.
594  ///
595  static OMPCopyinClause *Create(const ASTContext &C, SourceLocation StartLoc,
596                                 SourceLocation LParenLoc,
597                                 SourceLocation EndLoc, ArrayRef<Expr *> VL);
598  /// \brief Creates an empty clause with \a N variables.
599  ///
600  /// \param C AST context.
601  /// \param N The number of variables.
602  ///
603  static OMPCopyinClause *CreateEmpty(const ASTContext &C, unsigned N);
604
605  StmtRange children() {
606    return StmtRange(reinterpret_cast<Stmt **>(varlist_begin()),
607                     reinterpret_cast<Stmt **>(varlist_end()));
608  }
609
610  static bool classof(const OMPClause *T) {
611    return T->getClauseKind() == OMPC_copyin;
612  }
613};
614
615} // end namespace clang
616
617#endif
618