blob: 22b1d08e697ee7fcdf697c3e612c465b52009266 [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},
40 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000041 auto Tok = P.getCurToken();
42 auto DKind =
43 Tok.isAnnotation()
44 ? OMPD_unknown
45 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000046
Alexey Bataev6d4ed052015-07-01 06:57:41 +000047 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000048 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000049 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
50 TokenMatched =
51 (i == 0) &&
52 !P.getPreprocessor().getSpelling(Tok).compare("cancellation");
53 } else {
54 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
55 }
Michael Wong65f367f2015-07-21 13:44:28 +000056
Alexey Bataev6d4ed052015-07-01 06:57:41 +000057 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000058 Tok = P.getPreprocessor().LookAhead(0);
Michael Wong65f367f2015-07-21 13:44:28 +000059 auto TokenIsAnnotation = Tok.isAnnotation();
Alexander Musmanf82886e2014-09-18 05:12:34 +000060 auto SDKind =
Michael Wong65f367f2015-07-21 13:44:28 +000061 TokenIsAnnotation
Alexander Musmanf82886e2014-09-18 05:12:34 +000062 ? OMPD_unknown
63 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000064
65 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000066 TokenMatched =
Daniel Jasper9aea8602015-07-21 16:18:51 +000067 ((i == 0) &&
68 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
69 ((i == 1) && !P.getPreprocessor().getSpelling(Tok).compare("data"));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000070 } else {
71 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
72 }
Michael Wong65f367f2015-07-21 13:44:28 +000073
Alexey Bataev6d4ed052015-07-01 06:57:41 +000074 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000075 P.ConsumeToken();
76 DKind = F[i][2];
77 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000078 }
79 }
80 return DKind;
81}
82
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000083/// \brief Parsing of declarative OpenMP directives.
84///
85/// threadprivate-directive:
86/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000087///
88Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
89 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000090 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000091
92 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000093 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000094 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000095
96 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000097 case OMPD_threadprivate:
98 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000099 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000100 // The last seen token is annot_pragma_openmp_end - need to check for
101 // extra tokens.
102 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
103 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000104 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000105 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000106 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000107 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000108 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000109 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000110 }
111 break;
112 case OMPD_unknown:
113 Diag(Tok, diag::err_omp_unknown_directive);
114 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000116 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000117 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000118 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000119 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000120 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000121 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000122 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000123 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000124 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000125 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000126 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000127 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000128 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000129 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000130 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000131 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000132 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000133 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000134 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000135 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000136 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000137 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000138 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000139 case OMPD_target_data:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000140 case OMPD_taskloop:
Alexey Bataeva769e072013-03-22 06:34:35 +0000141 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000142 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000143 break;
144 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000145 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000146 return DeclGroupPtrTy();
147}
148
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000149/// \brief Parsing of declarative or executable OpenMP directives.
150///
151/// threadprivate-directive:
152/// annot_pragma_openmp 'threadprivate' simple-variable-list
153/// annot_pragma_openmp_end
154///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000155/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000156/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000157/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
158/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000159/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000160/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
161/// 'taskgroup' | 'teams' {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000162/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000163///
Alexey Bataev68446b72014-07-18 07:47:19 +0000164StmtResult
165Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000166 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000167 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168 SmallVector<Expr *, 5> Identifiers;
169 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000170 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000171 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000172 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000173 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000174 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000175 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000176 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177 // Name of critical directive.
178 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000179 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000180 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000181 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000182
183 switch (DKind) {
184 case OMPD_threadprivate:
185 ConsumeToken();
186 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
187 // The last seen token is annot_pragma_openmp_end - need to check for
188 // extra tokens.
189 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
190 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000191 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000192 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000193 }
194 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000195 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000196 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
197 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000198 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000199 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000200 case OMPD_flush:
201 if (PP.LookAhead(0).is(tok::l_paren)) {
202 FlushHasClause = true;
203 // Push copy of the current token back to stream to properly parse
204 // pseudo-clause OMPFlushClause.
205 PP.EnterToken(Tok);
206 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000207 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000208 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000209 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000210 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000211 case OMPD_cancel:
Alexey Bataev68446b72014-07-18 07:47:19 +0000212 if (!StandAloneAllowed) {
213 Diag(Tok, diag::err_omp_immediate_directive)
214 << getOpenMPDirectiveName(DKind);
215 }
216 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000217 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000218 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000219 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000220 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000221 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000222 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000223 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000224 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000225 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000226 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000227 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000228 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000229 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000230 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000231 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000232 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000233 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000234 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000235 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000236 case OMPD_target_data:
237 case OMPD_taskloop: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000238 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000239 // Parse directive name of the 'critical' directive if any.
240 if (DKind == OMPD_critical) {
241 BalancedDelimiterTracker T(*this, tok::l_paren,
242 tok::annot_pragma_openmp_end);
243 if (!T.consumeOpen()) {
244 if (Tok.isAnyIdentifier()) {
245 DirName =
246 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
247 ConsumeAnyToken();
248 } else {
249 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
250 }
251 T.consumeClose();
252 }
Alexey Bataev80909872015-07-02 11:25:17 +0000253 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000254 CancelRegion = ParseOpenMPDirectiveKind(*this);
255 if (Tok.isNot(tok::annot_pragma_openmp_end))
256 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000257 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258
Alexey Bataevf29276e2014-06-18 04:14:57 +0000259 if (isOpenMPLoopDirective(DKind))
260 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
261 if (isOpenMPSimdDirective(DKind))
262 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
263 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000264 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000265
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000266 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000267 OpenMPClauseKind CKind =
268 Tok.isAnnotation()
269 ? OMPC_unknown
270 : FlushHasClause ? OMPC_flush
271 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000272 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000273 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000274 OMPClause *Clause =
275 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000276 FirstClauses[CKind].setInt(true);
277 if (Clause) {
278 FirstClauses[CKind].setPointer(Clause);
279 Clauses.push_back(Clause);
280 }
281
282 // Skip ',' if any.
283 if (Tok.is(tok::comma))
284 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000285 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000286 }
287 // End location of the directive.
288 EndLoc = Tok.getLocation();
289 // Consume final annot_pragma_openmp_end.
290 ConsumeToken();
291
292 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000293 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000294 // The body is a block scope like in Lambdas and Blocks.
295 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000296 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297 Actions.ActOnStartOfCompoundStmt();
298 // Parse statement
299 AssociatedStmt = ParseStatement();
300 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000301 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000302 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000303 Directive = Actions.ActOnOpenMPExecutableDirective(
304 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
305 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000306
307 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000308 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000309 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000310 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000311 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000312 case OMPD_unknown:
313 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000314 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000315 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000316 }
317 return Directive;
318}
319
Alexey Bataeva769e072013-03-22 06:34:35 +0000320/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000321/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000322///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000323/// simple-variable-list:
324/// '(' id-expression {, id-expression} ')'
325///
326bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
327 SmallVectorImpl<Expr *> &VarList,
328 bool AllowScopeSpecifier) {
329 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000330 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000331 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000332 if (T.expectAndConsume(diag::err_expected_lparen_after,
333 getOpenMPDirectiveName(Kind)))
334 return true;
335 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000336 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000337
338 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000339 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000340 CXXScopeSpec SS;
341 SourceLocation TemplateKWLoc;
342 UnqualifiedId Name;
343 // Read var name.
344 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000345 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000346
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000347 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
348 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000349 IsCorrect = false;
350 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000351 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000352 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
353 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000354 IsCorrect = false;
355 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000356 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000357 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
358 Tok.isNot(tok::annot_pragma_openmp_end)) {
359 IsCorrect = false;
360 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000361 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000362 Diag(PrevTok.getLocation(), diag::err_expected)
363 << tok::identifier
364 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000365 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000366 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000367 ExprResult Res =
368 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000369 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000370 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000371 }
372 // Consume ','.
373 if (Tok.is(tok::comma)) {
374 ConsumeToken();
375 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000376 }
377
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000378 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000379 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000380 IsCorrect = false;
381 }
382
383 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000384 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000385
386 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000387}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000388
389/// \brief Parsing of OpenMP clauses.
390///
391/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000392/// if-clause | final-clause | num_threads-clause | safelen-clause |
393/// default-clause | private-clause | firstprivate-clause | shared-clause
394/// | linear-clause | aligned-clause | collapse-clause |
395/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000396/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000397/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000398/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000399/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
400/// thread_limit-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000401///
402OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
403 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000404 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000405 bool ErrorFound = false;
406 // Check if clause is allowed for the given directive.
407 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000408 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
409 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000410 ErrorFound = true;
411 }
412
413 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000414 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000415 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000416 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000417 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000418 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000419 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000420 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000421 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000422 case OMPC_thread_limit:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000423 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000424 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000425 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000426 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000427 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000428 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000429 // OpenMP [2.9.1, target data construct, Restrictions]
430 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000431 // OpenMP [2.11.1, task Construct, Restrictions]
432 // At most one if clause can appear on the directive.
433 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000434 // OpenMP [teams Construct, Restrictions]
435 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000436 // At most one thread_limit clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000437 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000438 Diag(Tok, diag::err_omp_more_one_clause)
439 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000440 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000441 }
442
Alexey Bataev10e775f2015-07-30 11:36:16 +0000443 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
444 Clause = ParseOpenMPClause(CKind);
445 else
446 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000447 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000448 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000449 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000450 // OpenMP [2.14.3.1, Restrictions]
451 // Only a single default clause may be specified on a parallel, task or
452 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000453 // OpenMP [2.5, parallel Construct, Restrictions]
454 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000455 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000456 Diag(Tok, diag::err_omp_more_one_clause)
457 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000458 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000459 }
460
461 Clause = ParseOpenMPSimpleClause(CKind);
462 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000463 case OMPC_schedule:
464 // OpenMP [2.7.1, Restrictions, p. 3]
465 // Only one schedule clause can appear on a loop directive.
466 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000467 Diag(Tok, diag::err_omp_more_one_clause)
468 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000469 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000470 }
471
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000472 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000473 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
474 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000475 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000476 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000477 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000478 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000479 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000480 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000481 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000482 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000483 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000484 case OMPC_simd:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000485 // OpenMP [2.7.1, Restrictions, p. 9]
486 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000487 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
488 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000489 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000490 Diag(Tok, diag::err_omp_more_one_clause)
491 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000492 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000493 }
494
495 Clause = ParseOpenMPClause(CKind);
496 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000497 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000498 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000499 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000500 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000501 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000502 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000503 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000504 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000505 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000506 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000507 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000508 case OMPC_map:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000509 Clause = ParseOpenMPVarListClause(CKind);
510 break;
511 case OMPC_unknown:
512 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000513 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000514 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000515 break;
516 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000517 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
518 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000519 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000520 break;
521 }
Craig Topper161e4db2014-05-21 06:02:52 +0000522 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000523}
524
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000525/// \brief Parsing of OpenMP clauses with single expressions like 'final',
526/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 'thread_limit'
527/// or 'simdlen'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000528///
Alexey Bataev3778b602014-07-17 07:32:53 +0000529/// final-clause:
530/// 'final' '(' expression ')'
531///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000532/// num_threads-clause:
533/// 'num_threads' '(' expression ')'
534///
535/// safelen-clause:
536/// 'safelen' '(' expression ')'
537///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000538/// simdlen-clause:
539/// 'simdlen' '(' expression ')'
540///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000541/// collapse-clause:
542/// 'collapse' '(' expression ')'
543///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000544OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
545 SourceLocation Loc = ConsumeToken();
546
547 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
548 if (T.expectAndConsume(diag::err_expected_lparen_after,
549 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000550 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000551
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000552 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000553 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
554 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000555 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000556
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000557 // Parse ')'.
558 T.consumeClose();
559
560 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000561 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000562
Alexey Bataeva55ed262014-05-28 06:15:33 +0000563 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000564 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000565}
566
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000567/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000568///
569/// default-clause:
570/// 'default' '(' 'none' | 'shared' ')
571///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000572/// proc_bind-clause:
573/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
574///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000575OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
576 SourceLocation Loc = Tok.getLocation();
577 SourceLocation LOpen = ConsumeToken();
578 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000579 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000580 if (T.expectAndConsume(diag::err_expected_lparen_after,
581 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000582 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000583
Alexey Bataeva55ed262014-05-28 06:15:33 +0000584 unsigned Type = getOpenMPSimpleClauseType(
585 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000586 SourceLocation TypeLoc = Tok.getLocation();
587 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
588 Tok.isNot(tok::annot_pragma_openmp_end))
589 ConsumeAnyToken();
590
591 // Parse ')'.
592 T.consumeClose();
593
594 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
595 Tok.getLocation());
596}
597
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000598/// \brief Parsing of OpenMP clauses like 'ordered'.
599///
600/// ordered-clause:
601/// 'ordered'
602///
Alexey Bataev236070f2014-06-20 11:19:47 +0000603/// nowait-clause:
604/// 'nowait'
605///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000606/// untied-clause:
607/// 'untied'
608///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000609/// mergeable-clause:
610/// 'mergeable'
611///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000612/// read-clause:
613/// 'read'
614///
Alexey Bataev346265e2015-09-25 10:37:12 +0000615/// threads-clause:
616/// 'threads'
617///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000618/// simd-clause:
619/// 'simd'
620///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000621OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
622 SourceLocation Loc = Tok.getLocation();
623 ConsumeAnyToken();
624
625 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
626}
627
628
Alexey Bataev56dafe82014-06-20 07:16:17 +0000629/// \brief Parsing of OpenMP clauses with single expressions and some additional
630/// argument like 'schedule' or 'dist_schedule'.
631///
632/// schedule-clause:
633/// 'schedule' '(' kind [',' expression ] ')'
634///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000635/// if-clause:
636/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
637///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000638OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
639 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000640 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000641 // Parse '('.
642 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
643 if (T.expectAndConsume(diag::err_expected_lparen_after,
644 getOpenMPClauseName(Kind)))
645 return nullptr;
646
647 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000648 unsigned Arg;
649 SourceLocation KLoc;
650 if (Kind == OMPC_schedule) {
651 Arg = getOpenMPSimpleClauseType(
652 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
653 KLoc = Tok.getLocation();
654 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
655 Tok.isNot(tok::annot_pragma_openmp_end))
656 ConsumeAnyToken();
657 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
658 Arg == OMPC_SCHEDULE_guided) &&
659 Tok.is(tok::comma))
660 DelimLoc = ConsumeAnyToken();
661 } else {
662 assert(Kind == OMPC_if);
663 KLoc = Tok.getLocation();
664 Arg = ParseOpenMPDirectiveKind(*this);
665 if (Arg != OMPD_unknown) {
666 ConsumeToken();
667 if (Tok.is(tok::colon))
668 DelimLoc = ConsumeToken();
669 else
670 Diag(Tok, diag::warn_pragma_expected_colon)
671 << "directive name modifier";
672 }
673 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000674
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000675 bool NeedAnExpression =
676 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
677 if (NeedAnExpression) {
678 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000679 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
680 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000681 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000682 }
683
684 // Parse ')'.
685 T.consumeClose();
686
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000687 if (NeedAnExpression && Val.isInvalid())
688 return nullptr;
689
Alexey Bataev56dafe82014-06-20 07:16:17 +0000690 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000691 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000692 T.getCloseLocation());
693}
694
Alexey Bataevc5e02582014-06-16 07:08:35 +0000695static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
696 UnqualifiedId &ReductionId) {
697 SourceLocation TemplateKWLoc;
698 if (ReductionIdScopeSpec.isEmpty()) {
699 auto OOK = OO_None;
700 switch (P.getCurToken().getKind()) {
701 case tok::plus:
702 OOK = OO_Plus;
703 break;
704 case tok::minus:
705 OOK = OO_Minus;
706 break;
707 case tok::star:
708 OOK = OO_Star;
709 break;
710 case tok::amp:
711 OOK = OO_Amp;
712 break;
713 case tok::pipe:
714 OOK = OO_Pipe;
715 break;
716 case tok::caret:
717 OOK = OO_Caret;
718 break;
719 case tok::ampamp:
720 OOK = OO_AmpAmp;
721 break;
722 case tok::pipepipe:
723 OOK = OO_PipePipe;
724 break;
725 default:
726 break;
727 }
728 if (OOK != OO_None) {
729 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000730 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000731 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
732 return false;
733 }
734 }
735 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
736 /*AllowDestructorName*/ false,
737 /*AllowConstructorName*/ false, ParsedType(),
738 TemplateKWLoc, ReductionId);
739}
740
Alexander Musman1bb328c2014-06-04 13:06:39 +0000741/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000742/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000743///
744/// private-clause:
745/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000746/// firstprivate-clause:
747/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000748/// lastprivate-clause:
749/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000750/// shared-clause:
751/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000752/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000753/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000754/// aligned-clause:
755/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000756/// reduction-clause:
757/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000758/// copyprivate-clause:
759/// 'copyprivate' '(' list ')'
760/// flush-clause:
761/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000762/// depend-clause:
763/// 'depend' '(' in | out | inout : list ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000764/// map-clause:
765/// 'map' '(' [ [ always , ]
766/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000767///
Alexey Bataev182227b2015-08-20 10:54:39 +0000768/// For 'linear' clause linear-list may have the following forms:
769/// list
770/// modifier(list)
771/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000772OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
773 SourceLocation Loc = Tok.getLocation();
774 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000775 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000776 // Optional scope specifier and unqualified id for reduction identifier.
777 CXXScopeSpec ReductionIdScopeSpec;
778 UnqualifiedId ReductionId;
779 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000780 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000781 // OpenMP 4.1 [2.15.3.7, linear Clause]
782 // If no modifier is specified it is assumed to be val.
783 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000784 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
785 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
786 bool MapTypeModifierSpecified = false;
787 bool UnexpectedId = false;
788 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000789
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000790 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000791 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000792 if (T.expectAndConsume(diag::err_expected_lparen_after,
793 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000794 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000795
Alexey Bataev182227b2015-08-20 10:54:39 +0000796 bool NeedRParenForLinear = false;
797 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
798 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000799 // Handle reduction-identifier for reduction clause.
800 if (Kind == OMPC_reduction) {
801 ColonProtectionRAIIObject ColonRAII(*this);
802 if (getLangOpts().CPlusPlus) {
803 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
804 }
805 InvalidReductionId =
806 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
807 if (InvalidReductionId) {
808 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
809 StopBeforeMatch);
810 }
811 if (Tok.is(tok::colon)) {
812 ColonLoc = ConsumeToken();
813 } else {
814 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
815 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000816 } else if (Kind == OMPC_depend) {
817 // Handle dependency type for depend clause.
818 ColonProtectionRAIIObject ColonRAII(*this);
819 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
820 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000821 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000822
823 if (DepKind == OMPC_DEPEND_unknown) {
824 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
825 StopBeforeMatch);
826 } else {
827 ConsumeToken();
828 }
829 if (Tok.is(tok::colon)) {
830 ColonLoc = ConsumeToken();
831 } else {
832 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
833 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000834 } else if (Kind == OMPC_linear) {
835 // Try to parse modifier if any.
836 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000837 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000838 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000839 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000840 LinearT.consumeOpen();
841 NeedRParenForLinear = true;
842 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000843 } else if (Kind == OMPC_map) {
844 // Handle map type for map clause.
845 ColonProtectionRAIIObject ColonRAII(*this);
846
847 // the first identifier may be a list item, a map-type or
848 // a map-type-modifier
849 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
850 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
851 DepLinMapLoc = Tok.getLocation();
852 bool ColonExpected = false;
853
854 if (Tok.is(tok::identifier)) {
855 if (PP.LookAhead(0).is(tok::colon)) {
856 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
857 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
858 if (MapType == OMPC_MAP_unknown) {
859 Diag(Tok, diag::err_omp_unknown_map_type);
860 } else if (MapType == OMPC_MAP_always) {
861 Diag(Tok, diag::err_omp_map_type_missing);
862 }
863 ConsumeToken();
864 } else if (PP.LookAhead(0).is(tok::comma)) {
865 if (PP.LookAhead(1).is(tok::identifier) &&
866 PP.LookAhead(2).is(tok::colon)) {
867 MapTypeModifier =
868 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
869 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
870 if (MapTypeModifier != OMPC_MAP_always) {
871 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
872 MapTypeModifier = OMPC_MAP_unknown;
873 } else {
874 MapTypeModifierSpecified = true;
875 }
876
877 ConsumeToken();
878 ConsumeToken();
879
880 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
881 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
882 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
883 Diag(Tok, diag::err_omp_unknown_map_type);
884 }
885 ConsumeToken();
886 } else {
887 MapType = OMPC_MAP_tofrom;
888 }
889 } else {
890 MapType = OMPC_MAP_tofrom;
891 }
892 } else {
893 UnexpectedId = true;
894 }
895
896 if (Tok.is(tok::colon)) {
897 ColonLoc = ConsumeToken();
898 } else if (ColonExpected) {
899 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
900 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000901 }
902
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000903 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000904 bool IsComma =
905 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
906 (Kind != OMPC_map)) ||
907 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
908 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
909 (!MapTypeModifierSpecified ||
910 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
911 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000912 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000913 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000915 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000917 ExprResult VarExpr =
918 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000919 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000920 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000921 } else {
922 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000923 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000924 }
925 // Skip ',' if any
926 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000927 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000928 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000929 else if (Tok.isNot(tok::r_paren) &&
930 Tok.isNot(tok::annot_pragma_openmp_end) &&
931 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000932 Diag(Tok, diag::err_omp_expected_punc)
933 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
934 : getOpenMPClauseName(Kind))
935 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000936 }
937
Alexey Bataev182227b2015-08-20 10:54:39 +0000938 // Parse ')' for linear clause with modifier.
939 if (NeedRParenForLinear)
940 LinearT.consumeClose();
941
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000942 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000943 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000944 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
945 if (MustHaveTail) {
946 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000947 SourceLocation ELoc = ConsumeToken();
948 ExprResult Tail = ParseAssignmentExpression();
949 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000950 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000951 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000952 else
953 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
954 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000955 }
956
957 // Parse ')'.
958 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000959 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
960 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
Kelvin Li0bff7af2015-11-23 05:32:03 +0000961 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
962 InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +0000963 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000964 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000965
Alexey Bataevc5e02582014-06-16 07:08:35 +0000966 return Actions.ActOnOpenMPVarListClause(
967 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
968 ReductionIdScopeSpec,
969 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000970 : DeclarationNameInfo(),
Kelvin Li0bff7af2015-11-23 05:32:03 +0000971 DepKind, LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000972}
973