blob: bf190892a1d9e74d589c8c05c7596bb2168a7539 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
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 implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// OpenMP declarative directives.
27//===----------------------------------------------------------------------===//
28
Alexey Bataev4acb8592014-07-07 13:01:15 +000029static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000030 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
31 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
32 // TODO: add other combined directives in topological order.
33 const OpenMPDirectiveKind F[][3] = {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000034 {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/,
35 OMPD_cancellation_point},
Michael Wong65f367f2015-07-21 13:44:28 +000036 {OMPD_target, OMPD_unknown /*data*/, OMPD_target_data},
Alexey Bataev6d4ed052015-07-01 06:57:41 +000037 {OMPD_for, OMPD_simd, OMPD_for_simd},
38 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
39 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
Alexey Bataev0a6ed842015-12-03 09:40:15 +000040 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
41 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000042 auto Tok = P.getCurToken();
43 auto DKind =
44 Tok.isAnnotation()
45 ? OMPD_unknown
46 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000047
Alexey Bataev6d4ed052015-07-01 06:57:41 +000048 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000049 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000050 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
51 TokenMatched =
52 (i == 0) &&
53 !P.getPreprocessor().getSpelling(Tok).compare("cancellation");
54 } else {
55 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
56 }
Michael Wong65f367f2015-07-21 13:44:28 +000057
Alexey Bataev6d4ed052015-07-01 06:57:41 +000058 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000059 Tok = P.getPreprocessor().LookAhead(0);
Michael Wong65f367f2015-07-21 13:44:28 +000060 auto TokenIsAnnotation = Tok.isAnnotation();
Alexander Musmanf82886e2014-09-18 05:12:34 +000061 auto SDKind =
Michael Wong65f367f2015-07-21 13:44:28 +000062 TokenIsAnnotation
Alexander Musmanf82886e2014-09-18 05:12:34 +000063 ? OMPD_unknown
64 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000065
66 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000067 TokenMatched =
Daniel Jasper9aea8602015-07-21 16:18:51 +000068 ((i == 0) &&
69 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
70 ((i == 1) && !P.getPreprocessor().getSpelling(Tok).compare("data"));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000071 } else {
72 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
73 }
Michael Wong65f367f2015-07-21 13:44:28 +000074
Alexey Bataev6d4ed052015-07-01 06:57:41 +000075 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000076 P.ConsumeToken();
77 DKind = F[i][2];
78 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000079 }
80 }
81 return DKind;
82}
83
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000084/// \brief Parsing of declarative OpenMP directives.
85///
86/// threadprivate-directive:
87/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000088///
89Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
90 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000091 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000092
93 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000094 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000095 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000096
97 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000098 case OMPD_threadprivate:
99 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000100 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000101 // The last seen token is annot_pragma_openmp_end - need to check for
102 // extra tokens.
103 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
104 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000105 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000106 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000107 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000108 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000109 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000110 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000111 }
112 break;
113 case OMPD_unknown:
114 Diag(Tok, diag::err_omp_unknown_directive);
115 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000116 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000117 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000118 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000119 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000120 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000121 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000122 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000123 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000124 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000125 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000126 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000127 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000128 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000129 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000130 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000131 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000132 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000133 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000134 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000135 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000136 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000137 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000138 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000139 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000140 case OMPD_target_data:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000141 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000142 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000143 case OMPD_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000144 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000145 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000146 break;
147 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000148 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000149 return DeclGroupPtrTy();
150}
151
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000152/// \brief Parsing of declarative or executable OpenMP directives.
153///
154/// threadprivate-directive:
155/// annot_pragma_openmp 'threadprivate' simple-variable-list
156/// annot_pragma_openmp_end
157///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000158/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000159/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000160/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
161/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000162/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000163/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000164/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' {clause} |
165/// 'distribute'
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000166/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167///
Alexey Bataev68446b72014-07-18 07:47:19 +0000168StmtResult
169Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000170 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000171 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000172 SmallVector<Expr *, 5> Identifiers;
173 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000174 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000175 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000176 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000177 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000178 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000179 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000180 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000181 // Name of critical directive.
182 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000183 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000184 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000185 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000186
187 switch (DKind) {
188 case OMPD_threadprivate:
189 ConsumeToken();
190 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
191 // The last seen token is annot_pragma_openmp_end - need to check for
192 // extra tokens.
193 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
194 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000195 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000196 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000197 }
198 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000199 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000200 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
201 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000202 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000203 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000204 case OMPD_flush:
205 if (PP.LookAhead(0).is(tok::l_paren)) {
206 FlushHasClause = true;
207 // Push copy of the current token back to stream to properly parse
208 // pseudo-clause OMPFlushClause.
209 PP.EnterToken(Tok);
210 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000211 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000212 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000213 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000214 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000215 case OMPD_cancel:
Alexey Bataev68446b72014-07-18 07:47:19 +0000216 if (!StandAloneAllowed) {
217 Diag(Tok, diag::err_omp_immediate_directive)
218 << getOpenMPDirectiveName(DKind);
219 }
220 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000221 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000222 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000223 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000224 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000225 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000226 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000227 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000228 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000229 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000230 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000231 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000232 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000233 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000234 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000235 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000236 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000237 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000238 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000239 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000240 case OMPD_target_data:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000241 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000242 case OMPD_taskloop_simd:
243 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000244 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000245 // Parse directive name of the 'critical' directive if any.
246 if (DKind == OMPD_critical) {
247 BalancedDelimiterTracker T(*this, tok::l_paren,
248 tok::annot_pragma_openmp_end);
249 if (!T.consumeOpen()) {
250 if (Tok.isAnyIdentifier()) {
251 DirName =
252 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
253 ConsumeAnyToken();
254 } else {
255 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
256 }
257 T.consumeClose();
258 }
Alexey Bataev80909872015-07-02 11:25:17 +0000259 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000260 CancelRegion = ParseOpenMPDirectiveKind(*this);
261 if (Tok.isNot(tok::annot_pragma_openmp_end))
262 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000263 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264
Alexey Bataevf29276e2014-06-18 04:14:57 +0000265 if (isOpenMPLoopDirective(DKind))
266 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
267 if (isOpenMPSimdDirective(DKind))
268 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
269 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000270 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000271
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000272 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000273 OpenMPClauseKind CKind =
274 Tok.isAnnotation()
275 ? OMPC_unknown
276 : FlushHasClause ? OMPC_flush
277 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000278 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000279 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000280 OMPClause *Clause =
281 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000282 FirstClauses[CKind].setInt(true);
283 if (Clause) {
284 FirstClauses[CKind].setPointer(Clause);
285 Clauses.push_back(Clause);
286 }
287
288 // Skip ',' if any.
289 if (Tok.is(tok::comma))
290 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000291 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000292 }
293 // End location of the directive.
294 EndLoc = Tok.getLocation();
295 // Consume final annot_pragma_openmp_end.
296 ConsumeToken();
297
298 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000299 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 // The body is a block scope like in Lambdas and Blocks.
301 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000302 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000303 Actions.ActOnStartOfCompoundStmt();
304 // Parse statement
305 AssociatedStmt = ParseStatement();
306 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000307 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000308 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000309 Directive = Actions.ActOnOpenMPExecutableDirective(
310 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
311 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000312
313 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000314 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000315 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000316 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000317 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000318 case OMPD_unknown:
319 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000320 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000321 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000322 }
323 return Directive;
324}
325
Alexey Bataeva769e072013-03-22 06:34:35 +0000326/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000327/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000328///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000329/// simple-variable-list:
330/// '(' id-expression {, id-expression} ')'
331///
332bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
333 SmallVectorImpl<Expr *> &VarList,
334 bool AllowScopeSpecifier) {
335 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000336 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000337 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000338 if (T.expectAndConsume(diag::err_expected_lparen_after,
339 getOpenMPDirectiveName(Kind)))
340 return true;
341 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000342 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000343
344 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000345 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000346 CXXScopeSpec SS;
347 SourceLocation TemplateKWLoc;
348 UnqualifiedId Name;
349 // Read var name.
350 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000351 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000352
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000353 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
354 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000355 IsCorrect = false;
356 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000357 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000358 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
359 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000360 IsCorrect = false;
361 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000362 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000363 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
364 Tok.isNot(tok::annot_pragma_openmp_end)) {
365 IsCorrect = false;
366 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000367 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000368 Diag(PrevTok.getLocation(), diag::err_expected)
369 << tok::identifier
370 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000371 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000372 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000373 ExprResult Res =
374 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000375 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000376 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000377 }
378 // Consume ','.
379 if (Tok.is(tok::comma)) {
380 ConsumeToken();
381 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000382 }
383
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000384 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000385 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000386 IsCorrect = false;
387 }
388
389 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000390 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000391
392 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000393}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000394
395/// \brief Parsing of OpenMP clauses.
396///
397/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000398/// if-clause | final-clause | num_threads-clause | safelen-clause |
399/// default-clause | private-clause | firstprivate-clause | shared-clause
400/// | linear-clause | aligned-clause | collapse-clause |
401/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000402/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000403/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000404/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000405/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000406/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000407/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000408///
409OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
410 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000411 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000412 bool ErrorFound = false;
413 // Check if clause is allowed for the given directive.
414 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000415 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
416 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000417 ErrorFound = true;
418 }
419
420 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000421 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000422 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000423 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000424 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000425 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000426 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000427 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000428 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000429 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000430 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000431 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000432 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000433 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000434 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000435 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000436 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000437 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000438 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000439 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000440 // OpenMP [2.9.1, target data construct, Restrictions]
441 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000442 // OpenMP [2.11.1, task Construct, Restrictions]
443 // At most one if clause can appear on the directive.
444 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000445 // OpenMP [teams Construct, Restrictions]
446 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000447 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000448 // OpenMP [2.9.1, task Construct, Restrictions]
449 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000450 // OpenMP [2.9.2, taskloop Construct, Restrictions]
451 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000452 // OpenMP [2.9.2, taskloop Construct, Restrictions]
453 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000454 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000455 Diag(Tok, diag::err_omp_more_one_clause)
456 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000457 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000458 }
459
Alexey Bataev10e775f2015-07-30 11:36:16 +0000460 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
461 Clause = ParseOpenMPClause(CKind);
462 else
463 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000464 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000465 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000466 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000467 // OpenMP [2.14.3.1, Restrictions]
468 // Only a single default clause may be specified on a parallel, task or
469 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000470 // OpenMP [2.5, parallel Construct, Restrictions]
471 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000472 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000473 Diag(Tok, diag::err_omp_more_one_clause)
474 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000475 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000476 }
477
478 Clause = ParseOpenMPSimpleClause(CKind);
479 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000480 case OMPC_schedule:
481 // OpenMP [2.7.1, Restrictions, p. 3]
482 // Only one schedule clause can appear on a loop directive.
483 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000484 Diag(Tok, diag::err_omp_more_one_clause)
485 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000486 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000487 }
488
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000489 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000490 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
491 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000492 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000493 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000494 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000495 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000496 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000497 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000498 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000499 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000500 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000501 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000502 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000503 // OpenMP [2.7.1, Restrictions, p. 9]
504 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000505 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
506 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000507 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000508 Diag(Tok, diag::err_omp_more_one_clause)
509 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000510 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000511 }
512
513 Clause = ParseOpenMPClause(CKind);
514 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000515 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000516 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000517 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000518 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000519 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000520 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000521 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000522 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000523 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000524 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000525 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000526 case OMPC_map:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000527 Clause = ParseOpenMPVarListClause(CKind);
528 break;
529 case OMPC_unknown:
530 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000531 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000532 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000533 break;
534 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000535 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
536 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000537 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000538 break;
539 }
Craig Topper161e4db2014-05-21 06:02:52 +0000540 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000541}
542
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000543/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000544/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000545/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000546///
Alexey Bataev3778b602014-07-17 07:32:53 +0000547/// final-clause:
548/// 'final' '(' expression ')'
549///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000550/// num_threads-clause:
551/// 'num_threads' '(' expression ')'
552///
553/// safelen-clause:
554/// 'safelen' '(' expression ')'
555///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000556/// simdlen-clause:
557/// 'simdlen' '(' expression ')'
558///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000559/// collapse-clause:
560/// 'collapse' '(' expression ')'
561///
Alexey Bataeva0569352015-12-01 10:17:31 +0000562/// priority-clause:
563/// 'priority' '(' expression ')'
564///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000565/// grainsize-clause:
566/// 'grainsize' '(' expression ')'
567///
Alexey Bataev382967a2015-12-08 12:06:20 +0000568/// num_tasks-clause:
569/// 'num_tasks' '(' expression ')'
570///
Alexey Bataev28c75412015-12-15 08:19:24 +0000571/// hint-clause:
572/// 'hint' '(' expression ')'
573///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000574OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
575 SourceLocation Loc = ConsumeToken();
576
577 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
578 if (T.expectAndConsume(diag::err_expected_lparen_after,
579 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000580 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000581
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000582 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000583 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
584 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000585 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000586
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000587 // Parse ')'.
588 T.consumeClose();
589
590 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000591 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000592
Alexey Bataeva55ed262014-05-28 06:15:33 +0000593 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000594 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000595}
596
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000597/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000598///
599/// default-clause:
600/// 'default' '(' 'none' | 'shared' ')
601///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000602/// proc_bind-clause:
603/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
604///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000605OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
606 SourceLocation Loc = Tok.getLocation();
607 SourceLocation LOpen = ConsumeToken();
608 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000609 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000610 if (T.expectAndConsume(diag::err_expected_lparen_after,
611 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000612 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000613
Alexey Bataeva55ed262014-05-28 06:15:33 +0000614 unsigned Type = getOpenMPSimpleClauseType(
615 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000616 SourceLocation TypeLoc = Tok.getLocation();
617 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
618 Tok.isNot(tok::annot_pragma_openmp_end))
619 ConsumeAnyToken();
620
621 // Parse ')'.
622 T.consumeClose();
623
624 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
625 Tok.getLocation());
626}
627
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000628/// \brief Parsing of OpenMP clauses like 'ordered'.
629///
630/// ordered-clause:
631/// 'ordered'
632///
Alexey Bataev236070f2014-06-20 11:19:47 +0000633/// nowait-clause:
634/// 'nowait'
635///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000636/// untied-clause:
637/// 'untied'
638///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000639/// mergeable-clause:
640/// 'mergeable'
641///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000642/// read-clause:
643/// 'read'
644///
Alexey Bataev346265e2015-09-25 10:37:12 +0000645/// threads-clause:
646/// 'threads'
647///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000648/// simd-clause:
649/// 'simd'
650///
Alexey Bataevb825de12015-12-07 10:51:44 +0000651/// nogroup-clause:
652/// 'nogroup'
653///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000654OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
655 SourceLocation Loc = Tok.getLocation();
656 ConsumeAnyToken();
657
658 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
659}
660
661
Alexey Bataev56dafe82014-06-20 07:16:17 +0000662/// \brief Parsing of OpenMP clauses with single expressions and some additional
663/// argument like 'schedule' or 'dist_schedule'.
664///
665/// schedule-clause:
666/// 'schedule' '(' kind [',' expression ] ')'
667///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000668/// if-clause:
669/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
670///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000671OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
672 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000673 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000674 // Parse '('.
675 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
676 if (T.expectAndConsume(diag::err_expected_lparen_after,
677 getOpenMPClauseName(Kind)))
678 return nullptr;
679
680 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000681 unsigned Arg;
682 SourceLocation KLoc;
683 if (Kind == OMPC_schedule) {
684 Arg = getOpenMPSimpleClauseType(
685 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
686 KLoc = Tok.getLocation();
687 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
688 Tok.isNot(tok::annot_pragma_openmp_end))
689 ConsumeAnyToken();
690 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
691 Arg == OMPC_SCHEDULE_guided) &&
692 Tok.is(tok::comma))
693 DelimLoc = ConsumeAnyToken();
694 } else {
695 assert(Kind == OMPC_if);
696 KLoc = Tok.getLocation();
697 Arg = ParseOpenMPDirectiveKind(*this);
698 if (Arg != OMPD_unknown) {
699 ConsumeToken();
700 if (Tok.is(tok::colon))
701 DelimLoc = ConsumeToken();
702 else
703 Diag(Tok, diag::warn_pragma_expected_colon)
704 << "directive name modifier";
705 }
706 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000707
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000708 bool NeedAnExpression =
709 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
710 if (NeedAnExpression) {
711 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000712 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
713 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000714 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000715 }
716
717 // Parse ')'.
718 T.consumeClose();
719
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000720 if (NeedAnExpression && Val.isInvalid())
721 return nullptr;
722
Alexey Bataev56dafe82014-06-20 07:16:17 +0000723 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000724 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000725 T.getCloseLocation());
726}
727
Alexey Bataevc5e02582014-06-16 07:08:35 +0000728static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
729 UnqualifiedId &ReductionId) {
730 SourceLocation TemplateKWLoc;
731 if (ReductionIdScopeSpec.isEmpty()) {
732 auto OOK = OO_None;
733 switch (P.getCurToken().getKind()) {
734 case tok::plus:
735 OOK = OO_Plus;
736 break;
737 case tok::minus:
738 OOK = OO_Minus;
739 break;
740 case tok::star:
741 OOK = OO_Star;
742 break;
743 case tok::amp:
744 OOK = OO_Amp;
745 break;
746 case tok::pipe:
747 OOK = OO_Pipe;
748 break;
749 case tok::caret:
750 OOK = OO_Caret;
751 break;
752 case tok::ampamp:
753 OOK = OO_AmpAmp;
754 break;
755 case tok::pipepipe:
756 OOK = OO_PipePipe;
757 break;
758 default:
759 break;
760 }
761 if (OOK != OO_None) {
762 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000763 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000764 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
765 return false;
766 }
767 }
768 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
769 /*AllowDestructorName*/ false,
770 /*AllowConstructorName*/ false, ParsedType(),
771 TemplateKWLoc, ReductionId);
772}
773
Alexander Musman1bb328c2014-06-04 13:06:39 +0000774/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000775/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000776///
777/// private-clause:
778/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000779/// firstprivate-clause:
780/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000781/// lastprivate-clause:
782/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000783/// shared-clause:
784/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000785/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000786/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000787/// aligned-clause:
788/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000789/// reduction-clause:
790/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000791/// copyprivate-clause:
792/// 'copyprivate' '(' list ')'
793/// flush-clause:
794/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000795/// depend-clause:
796/// 'depend' '(' in | out | inout : list ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000797/// map-clause:
798/// 'map' '(' [ [ always , ]
799/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000800///
Alexey Bataev182227b2015-08-20 10:54:39 +0000801/// For 'linear' clause linear-list may have the following forms:
802/// list
803/// modifier(list)
804/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000805OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
806 SourceLocation Loc = Tok.getLocation();
807 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000808 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000809 // Optional scope specifier and unqualified id for reduction identifier.
810 CXXScopeSpec ReductionIdScopeSpec;
811 UnqualifiedId ReductionId;
812 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000813 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000814 // OpenMP 4.1 [2.15.3.7, linear Clause]
815 // If no modifier is specified it is assumed to be val.
816 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000817 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
818 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
819 bool MapTypeModifierSpecified = false;
820 bool UnexpectedId = false;
821 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000822
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000823 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000824 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000825 if (T.expectAndConsume(diag::err_expected_lparen_after,
826 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000827 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000828
Alexey Bataev182227b2015-08-20 10:54:39 +0000829 bool NeedRParenForLinear = false;
830 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
831 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000832 // Handle reduction-identifier for reduction clause.
833 if (Kind == OMPC_reduction) {
834 ColonProtectionRAIIObject ColonRAII(*this);
835 if (getLangOpts().CPlusPlus) {
836 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
837 }
838 InvalidReductionId =
839 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
840 if (InvalidReductionId) {
841 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
842 StopBeforeMatch);
843 }
844 if (Tok.is(tok::colon)) {
845 ColonLoc = ConsumeToken();
846 } else {
847 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
848 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000849 } else if (Kind == OMPC_depend) {
850 // Handle dependency type for depend clause.
851 ColonProtectionRAIIObject ColonRAII(*this);
852 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
853 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000854 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000855
856 if (DepKind == OMPC_DEPEND_unknown) {
857 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
858 StopBeforeMatch);
859 } else {
860 ConsumeToken();
861 }
862 if (Tok.is(tok::colon)) {
863 ColonLoc = ConsumeToken();
864 } else {
865 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
866 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000867 } else if (Kind == OMPC_linear) {
868 // Try to parse modifier if any.
869 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000870 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000871 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000872 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000873 LinearT.consumeOpen();
874 NeedRParenForLinear = true;
875 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000876 } else if (Kind == OMPC_map) {
877 // Handle map type for map clause.
878 ColonProtectionRAIIObject ColonRAII(*this);
879
880 // the first identifier may be a list item, a map-type or
881 // a map-type-modifier
882 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
883 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
884 DepLinMapLoc = Tok.getLocation();
885 bool ColonExpected = false;
886
887 if (Tok.is(tok::identifier)) {
888 if (PP.LookAhead(0).is(tok::colon)) {
889 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
890 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
891 if (MapType == OMPC_MAP_unknown) {
892 Diag(Tok, diag::err_omp_unknown_map_type);
893 } else if (MapType == OMPC_MAP_always) {
894 Diag(Tok, diag::err_omp_map_type_missing);
895 }
896 ConsumeToken();
897 } else if (PP.LookAhead(0).is(tok::comma)) {
898 if (PP.LookAhead(1).is(tok::identifier) &&
899 PP.LookAhead(2).is(tok::colon)) {
900 MapTypeModifier =
901 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
902 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
903 if (MapTypeModifier != OMPC_MAP_always) {
904 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
905 MapTypeModifier = OMPC_MAP_unknown;
906 } else {
907 MapTypeModifierSpecified = true;
908 }
909
910 ConsumeToken();
911 ConsumeToken();
912
913 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
914 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
915 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
916 Diag(Tok, diag::err_omp_unknown_map_type);
917 }
918 ConsumeToken();
919 } else {
920 MapType = OMPC_MAP_tofrom;
921 }
922 } else {
923 MapType = OMPC_MAP_tofrom;
924 }
925 } else {
926 UnexpectedId = true;
927 }
928
929 if (Tok.is(tok::colon)) {
930 ColonLoc = ConsumeToken();
931 } else if (ColonExpected) {
932 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
933 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000934 }
935
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000936 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000937 bool IsComma =
938 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
939 (Kind != OMPC_map)) ||
940 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
941 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
942 (!MapTypeModifierSpecified ||
943 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
944 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000945 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000946 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000947 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000948 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000950 ExprResult VarExpr =
951 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000952 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000953 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000954 } else {
955 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000956 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000957 }
958 // Skip ',' if any
959 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000960 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000961 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000962 else if (Tok.isNot(tok::r_paren) &&
963 Tok.isNot(tok::annot_pragma_openmp_end) &&
964 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000965 Diag(Tok, diag::err_omp_expected_punc)
966 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
967 : getOpenMPClauseName(Kind))
968 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000969 }
970
Alexey Bataev182227b2015-08-20 10:54:39 +0000971 // Parse ')' for linear clause with modifier.
972 if (NeedRParenForLinear)
973 LinearT.consumeClose();
974
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000975 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000976 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000977 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
978 if (MustHaveTail) {
979 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000980 SourceLocation ELoc = ConsumeToken();
981 ExprResult Tail = ParseAssignmentExpression();
982 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000983 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000984 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000985 else
986 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
987 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000988 }
989
990 // Parse ')'.
991 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000992 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
993 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
Kelvin Li0bff7af2015-11-23 05:32:03 +0000994 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
995 InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +0000996 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000997 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000998
Alexey Bataevc5e02582014-06-16 07:08:35 +0000999 return Actions.ActOnOpenMPVarListClause(
1000 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1001 ReductionIdScopeSpec,
1002 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001003 : DeclarationNameInfo(),
Kelvin Li0bff7af2015-11-23 05:32:03 +00001004 DepKind, LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001005}
1006