blob: 8f24f5d0f303872d7abd4d83939134bd44d67096 [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:
Alexey Bataeva769e072013-03-22 06:34:35 +0000143 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000144 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000145 break;
146 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000147 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000148 return DeclGroupPtrTy();
149}
150
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000151/// \brief Parsing of declarative or executable OpenMP directives.
152///
153/// threadprivate-directive:
154/// annot_pragma_openmp 'threadprivate' simple-variable-list
155/// annot_pragma_openmp_end
156///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000157/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000158/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000159/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
160/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000161/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000162/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000163/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000164/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000165///
Alexey Bataev68446b72014-07-18 07:47:19 +0000166StmtResult
167Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000169 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000170 SmallVector<Expr *, 5> Identifiers;
171 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000172 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000173 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000174 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000175 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000176 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000177 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000178 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000179 // Name of critical directive.
180 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000181 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000182 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000183 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000184
185 switch (DKind) {
186 case OMPD_threadprivate:
187 ConsumeToken();
188 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
189 // The last seen token is annot_pragma_openmp_end - need to check for
190 // extra tokens.
191 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
192 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000193 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000194 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000195 }
196 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000197 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000198 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
199 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000200 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000201 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000202 case OMPD_flush:
203 if (PP.LookAhead(0).is(tok::l_paren)) {
204 FlushHasClause = true;
205 // Push copy of the current token back to stream to properly parse
206 // pseudo-clause OMPFlushClause.
207 PP.EnterToken(Tok);
208 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000209 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000210 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000211 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000212 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000213 case OMPD_cancel:
Alexey Bataev68446b72014-07-18 07:47:19 +0000214 if (!StandAloneAllowed) {
215 Diag(Tok, diag::err_omp_immediate_directive)
216 << getOpenMPDirectiveName(DKind);
217 }
218 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000219 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000220 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000221 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000222 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000223 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000224 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000225 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000226 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000227 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000228 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000229 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000230 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000231 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000232 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000233 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000234 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000235 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000236 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000237 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000238 case OMPD_target_data:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000239 case OMPD_taskloop:
240 case OMPD_taskloop_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000241 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000242 // Parse directive name of the 'critical' directive if any.
243 if (DKind == OMPD_critical) {
244 BalancedDelimiterTracker T(*this, tok::l_paren,
245 tok::annot_pragma_openmp_end);
246 if (!T.consumeOpen()) {
247 if (Tok.isAnyIdentifier()) {
248 DirName =
249 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
250 ConsumeAnyToken();
251 } else {
252 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
253 }
254 T.consumeClose();
255 }
Alexey Bataev80909872015-07-02 11:25:17 +0000256 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000257 CancelRegion = ParseOpenMPDirectiveKind(*this);
258 if (Tok.isNot(tok::annot_pragma_openmp_end))
259 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000260 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000261
Alexey Bataevf29276e2014-06-18 04:14:57 +0000262 if (isOpenMPLoopDirective(DKind))
263 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
264 if (isOpenMPSimdDirective(DKind))
265 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
266 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000267 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000268
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000269 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000270 OpenMPClauseKind CKind =
271 Tok.isAnnotation()
272 ? OMPC_unknown
273 : FlushHasClause ? OMPC_flush
274 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000275 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000276 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000277 OMPClause *Clause =
278 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000279 FirstClauses[CKind].setInt(true);
280 if (Clause) {
281 FirstClauses[CKind].setPointer(Clause);
282 Clauses.push_back(Clause);
283 }
284
285 // Skip ',' if any.
286 if (Tok.is(tok::comma))
287 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000288 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000289 }
290 // End location of the directive.
291 EndLoc = Tok.getLocation();
292 // Consume final annot_pragma_openmp_end.
293 ConsumeToken();
294
295 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000296 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297 // The body is a block scope like in Lambdas and Blocks.
298 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000299 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 Actions.ActOnStartOfCompoundStmt();
301 // Parse statement
302 AssociatedStmt = ParseStatement();
303 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000304 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000305 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000306 Directive = Actions.ActOnOpenMPExecutableDirective(
307 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
308 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000309
310 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000311 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000312 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000313 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000314 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000315 case OMPD_unknown:
316 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000317 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000318 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000319 }
320 return Directive;
321}
322
Alexey Bataeva769e072013-03-22 06:34:35 +0000323/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000324/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000325///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000326/// simple-variable-list:
327/// '(' id-expression {, id-expression} ')'
328///
329bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
330 SmallVectorImpl<Expr *> &VarList,
331 bool AllowScopeSpecifier) {
332 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000333 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000334 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000335 if (T.expectAndConsume(diag::err_expected_lparen_after,
336 getOpenMPDirectiveName(Kind)))
337 return true;
338 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000339 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000340
341 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000342 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000343 CXXScopeSpec SS;
344 SourceLocation TemplateKWLoc;
345 UnqualifiedId Name;
346 // Read var name.
347 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000348 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000349
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000350 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
351 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000352 IsCorrect = false;
353 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000354 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000355 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
356 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000357 IsCorrect = false;
358 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000359 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000360 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
361 Tok.isNot(tok::annot_pragma_openmp_end)) {
362 IsCorrect = false;
363 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000364 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000365 Diag(PrevTok.getLocation(), diag::err_expected)
366 << tok::identifier
367 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000368 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000369 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000370 ExprResult Res =
371 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000372 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000373 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000374 }
375 // Consume ','.
376 if (Tok.is(tok::comma)) {
377 ConsumeToken();
378 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000379 }
380
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000381 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000382 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000383 IsCorrect = false;
384 }
385
386 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000387 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000388
389 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000390}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000391
392/// \brief Parsing of OpenMP clauses.
393///
394/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000395/// if-clause | final-clause | num_threads-clause | safelen-clause |
396/// default-clause | private-clause | firstprivate-clause | shared-clause
397/// | linear-clause | aligned-clause | collapse-clause |
398/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000399/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000400/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000401/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000402/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000403/// thread_limit-clause | priority-clause | grainsize-clause |
404/// nogroup-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000405///
406OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
407 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000408 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000409 bool ErrorFound = false;
410 // Check if clause is allowed for the given directive.
411 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000412 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
413 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000414 ErrorFound = true;
415 }
416
417 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000418 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000419 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000420 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000421 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000422 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000423 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000424 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000425 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000426 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000427 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000428 case OMPC_grainsize:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000429 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000430 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000431 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000432 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000433 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000434 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000435 // OpenMP [2.9.1, target data construct, Restrictions]
436 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000437 // OpenMP [2.11.1, task Construct, Restrictions]
438 // At most one if clause can appear on the directive.
439 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000440 // OpenMP [teams Construct, Restrictions]
441 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000442 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000443 // OpenMP [2.9.1, task Construct, Restrictions]
444 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000445 // OpenMP [2.9.2, taskloop Construct, Restrictions]
446 // At most one grainsize clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000447 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000448 Diag(Tok, diag::err_omp_more_one_clause)
449 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000450 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000451 }
452
Alexey Bataev10e775f2015-07-30 11:36:16 +0000453 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
454 Clause = ParseOpenMPClause(CKind);
455 else
456 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000457 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000458 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000459 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000460 // OpenMP [2.14.3.1, Restrictions]
461 // Only a single default clause may be specified on a parallel, task or
462 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000463 // OpenMP [2.5, parallel Construct, Restrictions]
464 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000465 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000466 Diag(Tok, diag::err_omp_more_one_clause)
467 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000468 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000469 }
470
471 Clause = ParseOpenMPSimpleClause(CKind);
472 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000473 case OMPC_schedule:
474 // OpenMP [2.7.1, Restrictions, p. 3]
475 // Only one schedule clause can appear on a loop directive.
476 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000477 Diag(Tok, diag::err_omp_more_one_clause)
478 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000479 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000480 }
481
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000482 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000483 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
484 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000485 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000486 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000487 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000488 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000489 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000490 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000491 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000492 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000493 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000494 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000495 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000496 // OpenMP [2.7.1, Restrictions, p. 9]
497 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000498 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
499 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000500 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000501 Diag(Tok, diag::err_omp_more_one_clause)
502 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000503 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000504 }
505
506 Clause = ParseOpenMPClause(CKind);
507 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000508 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000509 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000510 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000511 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000512 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000513 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000514 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000515 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000516 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000517 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000518 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000519 case OMPC_map:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000520 Clause = ParseOpenMPVarListClause(CKind);
521 break;
522 case OMPC_unknown:
523 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000524 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000525 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000526 break;
527 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000528 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
529 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000530 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000531 break;
532 }
Craig Topper161e4db2014-05-21 06:02:52 +0000533 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000534}
535
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000536/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000537/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000538/// 'thread_limit', 'simdlen', 'priority' or 'grainsize'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000539///
Alexey Bataev3778b602014-07-17 07:32:53 +0000540/// final-clause:
541/// 'final' '(' expression ')'
542///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000543/// num_threads-clause:
544/// 'num_threads' '(' expression ')'
545///
546/// safelen-clause:
547/// 'safelen' '(' expression ')'
548///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000549/// simdlen-clause:
550/// 'simdlen' '(' expression ')'
551///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000552/// collapse-clause:
553/// 'collapse' '(' expression ')'
554///
Alexey Bataeva0569352015-12-01 10:17:31 +0000555/// priority-clause:
556/// 'priority' '(' expression ')'
557///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000558/// grainsize-clause:
559/// 'grainsize' '(' expression ')'
560///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000561OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
562 SourceLocation Loc = ConsumeToken();
563
564 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
565 if (T.expectAndConsume(diag::err_expected_lparen_after,
566 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000567 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000568
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000569 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000570 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
571 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000572 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000573
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000574 // Parse ')'.
575 T.consumeClose();
576
577 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000578 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000579
Alexey Bataeva55ed262014-05-28 06:15:33 +0000580 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000581 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000582}
583
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000584/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000585///
586/// default-clause:
587/// 'default' '(' 'none' | 'shared' ')
588///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000589/// proc_bind-clause:
590/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
591///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000592OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
593 SourceLocation Loc = Tok.getLocation();
594 SourceLocation LOpen = ConsumeToken();
595 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000596 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000597 if (T.expectAndConsume(diag::err_expected_lparen_after,
598 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000599 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000600
Alexey Bataeva55ed262014-05-28 06:15:33 +0000601 unsigned Type = getOpenMPSimpleClauseType(
602 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000603 SourceLocation TypeLoc = Tok.getLocation();
604 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
605 Tok.isNot(tok::annot_pragma_openmp_end))
606 ConsumeAnyToken();
607
608 // Parse ')'.
609 T.consumeClose();
610
611 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
612 Tok.getLocation());
613}
614
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000615/// \brief Parsing of OpenMP clauses like 'ordered'.
616///
617/// ordered-clause:
618/// 'ordered'
619///
Alexey Bataev236070f2014-06-20 11:19:47 +0000620/// nowait-clause:
621/// 'nowait'
622///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000623/// untied-clause:
624/// 'untied'
625///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000626/// mergeable-clause:
627/// 'mergeable'
628///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000629/// read-clause:
630/// 'read'
631///
Alexey Bataev346265e2015-09-25 10:37:12 +0000632/// threads-clause:
633/// 'threads'
634///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000635/// simd-clause:
636/// 'simd'
637///
Alexey Bataevb825de12015-12-07 10:51:44 +0000638/// nogroup-clause:
639/// 'nogroup'
640///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000641OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
642 SourceLocation Loc = Tok.getLocation();
643 ConsumeAnyToken();
644
645 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
646}
647
648
Alexey Bataev56dafe82014-06-20 07:16:17 +0000649/// \brief Parsing of OpenMP clauses with single expressions and some additional
650/// argument like 'schedule' or 'dist_schedule'.
651///
652/// schedule-clause:
653/// 'schedule' '(' kind [',' expression ] ')'
654///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000655/// if-clause:
656/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
657///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000658OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
659 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000660 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000661 // Parse '('.
662 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
663 if (T.expectAndConsume(diag::err_expected_lparen_after,
664 getOpenMPClauseName(Kind)))
665 return nullptr;
666
667 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000668 unsigned Arg;
669 SourceLocation KLoc;
670 if (Kind == OMPC_schedule) {
671 Arg = getOpenMPSimpleClauseType(
672 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
673 KLoc = Tok.getLocation();
674 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
675 Tok.isNot(tok::annot_pragma_openmp_end))
676 ConsumeAnyToken();
677 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
678 Arg == OMPC_SCHEDULE_guided) &&
679 Tok.is(tok::comma))
680 DelimLoc = ConsumeAnyToken();
681 } else {
682 assert(Kind == OMPC_if);
683 KLoc = Tok.getLocation();
684 Arg = ParseOpenMPDirectiveKind(*this);
685 if (Arg != OMPD_unknown) {
686 ConsumeToken();
687 if (Tok.is(tok::colon))
688 DelimLoc = ConsumeToken();
689 else
690 Diag(Tok, diag::warn_pragma_expected_colon)
691 << "directive name modifier";
692 }
693 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000694
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000695 bool NeedAnExpression =
696 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
697 if (NeedAnExpression) {
698 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000699 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
700 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000701 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000702 }
703
704 // Parse ')'.
705 T.consumeClose();
706
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000707 if (NeedAnExpression && Val.isInvalid())
708 return nullptr;
709
Alexey Bataev56dafe82014-06-20 07:16:17 +0000710 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000711 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000712 T.getCloseLocation());
713}
714
Alexey Bataevc5e02582014-06-16 07:08:35 +0000715static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
716 UnqualifiedId &ReductionId) {
717 SourceLocation TemplateKWLoc;
718 if (ReductionIdScopeSpec.isEmpty()) {
719 auto OOK = OO_None;
720 switch (P.getCurToken().getKind()) {
721 case tok::plus:
722 OOK = OO_Plus;
723 break;
724 case tok::minus:
725 OOK = OO_Minus;
726 break;
727 case tok::star:
728 OOK = OO_Star;
729 break;
730 case tok::amp:
731 OOK = OO_Amp;
732 break;
733 case tok::pipe:
734 OOK = OO_Pipe;
735 break;
736 case tok::caret:
737 OOK = OO_Caret;
738 break;
739 case tok::ampamp:
740 OOK = OO_AmpAmp;
741 break;
742 case tok::pipepipe:
743 OOK = OO_PipePipe;
744 break;
745 default:
746 break;
747 }
748 if (OOK != OO_None) {
749 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000750 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000751 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
752 return false;
753 }
754 }
755 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
756 /*AllowDestructorName*/ false,
757 /*AllowConstructorName*/ false, ParsedType(),
758 TemplateKWLoc, ReductionId);
759}
760
Alexander Musman1bb328c2014-06-04 13:06:39 +0000761/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000762/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000763///
764/// private-clause:
765/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000766/// firstprivate-clause:
767/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000768/// lastprivate-clause:
769/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000770/// shared-clause:
771/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000772/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000773/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000774/// aligned-clause:
775/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000776/// reduction-clause:
777/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000778/// copyprivate-clause:
779/// 'copyprivate' '(' list ')'
780/// flush-clause:
781/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000782/// depend-clause:
783/// 'depend' '(' in | out | inout : list ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000784/// map-clause:
785/// 'map' '(' [ [ always , ]
786/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000787///
Alexey Bataev182227b2015-08-20 10:54:39 +0000788/// For 'linear' clause linear-list may have the following forms:
789/// list
790/// modifier(list)
791/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000792OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
793 SourceLocation Loc = Tok.getLocation();
794 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000795 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000796 // Optional scope specifier and unqualified id for reduction identifier.
797 CXXScopeSpec ReductionIdScopeSpec;
798 UnqualifiedId ReductionId;
799 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000800 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000801 // OpenMP 4.1 [2.15.3.7, linear Clause]
802 // If no modifier is specified it is assumed to be val.
803 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000804 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
805 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
806 bool MapTypeModifierSpecified = false;
807 bool UnexpectedId = false;
808 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000809
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000810 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000811 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000812 if (T.expectAndConsume(diag::err_expected_lparen_after,
813 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000814 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000815
Alexey Bataev182227b2015-08-20 10:54:39 +0000816 bool NeedRParenForLinear = false;
817 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
818 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000819 // Handle reduction-identifier for reduction clause.
820 if (Kind == OMPC_reduction) {
821 ColonProtectionRAIIObject ColonRAII(*this);
822 if (getLangOpts().CPlusPlus) {
823 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
824 }
825 InvalidReductionId =
826 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
827 if (InvalidReductionId) {
828 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
829 StopBeforeMatch);
830 }
831 if (Tok.is(tok::colon)) {
832 ColonLoc = ConsumeToken();
833 } else {
834 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
835 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000836 } else if (Kind == OMPC_depend) {
837 // Handle dependency type for depend clause.
838 ColonProtectionRAIIObject ColonRAII(*this);
839 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
840 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000841 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000842
843 if (DepKind == OMPC_DEPEND_unknown) {
844 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
845 StopBeforeMatch);
846 } else {
847 ConsumeToken();
848 }
849 if (Tok.is(tok::colon)) {
850 ColonLoc = ConsumeToken();
851 } else {
852 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
853 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000854 } else if (Kind == OMPC_linear) {
855 // Try to parse modifier if any.
856 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000857 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000858 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000859 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000860 LinearT.consumeOpen();
861 NeedRParenForLinear = true;
862 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000863 } else if (Kind == OMPC_map) {
864 // Handle map type for map clause.
865 ColonProtectionRAIIObject ColonRAII(*this);
866
867 // the first identifier may be a list item, a map-type or
868 // a map-type-modifier
869 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
870 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
871 DepLinMapLoc = Tok.getLocation();
872 bool ColonExpected = false;
873
874 if (Tok.is(tok::identifier)) {
875 if (PP.LookAhead(0).is(tok::colon)) {
876 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
877 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
878 if (MapType == OMPC_MAP_unknown) {
879 Diag(Tok, diag::err_omp_unknown_map_type);
880 } else if (MapType == OMPC_MAP_always) {
881 Diag(Tok, diag::err_omp_map_type_missing);
882 }
883 ConsumeToken();
884 } else if (PP.LookAhead(0).is(tok::comma)) {
885 if (PP.LookAhead(1).is(tok::identifier) &&
886 PP.LookAhead(2).is(tok::colon)) {
887 MapTypeModifier =
888 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
889 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
890 if (MapTypeModifier != OMPC_MAP_always) {
891 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
892 MapTypeModifier = OMPC_MAP_unknown;
893 } else {
894 MapTypeModifierSpecified = true;
895 }
896
897 ConsumeToken();
898 ConsumeToken();
899
900 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
901 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
902 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
903 Diag(Tok, diag::err_omp_unknown_map_type);
904 }
905 ConsumeToken();
906 } else {
907 MapType = OMPC_MAP_tofrom;
908 }
909 } else {
910 MapType = OMPC_MAP_tofrom;
911 }
912 } else {
913 UnexpectedId = true;
914 }
915
916 if (Tok.is(tok::colon)) {
917 ColonLoc = ConsumeToken();
918 } else if (ColonExpected) {
919 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
920 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000921 }
922
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000923 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000924 bool IsComma =
925 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
926 (Kind != OMPC_map)) ||
927 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
928 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
929 (!MapTypeModifierSpecified ||
930 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
931 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000932 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000933 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000934 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000935 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000936 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000937 ExprResult VarExpr =
938 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000939 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000940 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941 } else {
942 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000943 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000944 }
945 // Skip ',' if any
946 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000947 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000948 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000949 else if (Tok.isNot(tok::r_paren) &&
950 Tok.isNot(tok::annot_pragma_openmp_end) &&
951 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000952 Diag(Tok, diag::err_omp_expected_punc)
953 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
954 : getOpenMPClauseName(Kind))
955 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000956 }
957
Alexey Bataev182227b2015-08-20 10:54:39 +0000958 // Parse ')' for linear clause with modifier.
959 if (NeedRParenForLinear)
960 LinearT.consumeClose();
961
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000962 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000963 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000964 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
965 if (MustHaveTail) {
966 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000967 SourceLocation ELoc = ConsumeToken();
968 ExprResult Tail = ParseAssignmentExpression();
969 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000970 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000971 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000972 else
973 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
974 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000975 }
976
977 // Parse ')'.
978 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000979 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
980 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
Kelvin Li0bff7af2015-11-23 05:32:03 +0000981 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
982 InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +0000983 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000984 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000985
Alexey Bataevc5e02582014-06-16 07:08:35 +0000986 return Actions.ActOnOpenMPVarListClause(
987 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
988 ReductionIdScopeSpec,
989 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000990 : DeclarationNameInfo(),
Kelvin Li0bff7af2015-11-23 05:32:03 +0000991 DepKind, LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000992}
993