blob: 5991a28d0c0469e61c25643dac7e462b60f9c5ac [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 |
Alexey Bataeva0569352015-12-01 10:17:31 +0000400/// thread_limit-clause | priority-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 Bataeva0569352015-12-01 10:17:31 +0000423 case OMPC_priority:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000424 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000425 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000426 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000427 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000428 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000429 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000430 // OpenMP [2.9.1, target data construct, Restrictions]
431 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000432 // OpenMP [2.11.1, task Construct, Restrictions]
433 // At most one if clause can appear on the directive.
434 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000435 // OpenMP [teams Construct, Restrictions]
436 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000437 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000438 // OpenMP [2.9.1, task Construct, Restrictions]
439 // At most one priority clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000440 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000441 Diag(Tok, diag::err_omp_more_one_clause)
442 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000443 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000444 }
445
Alexey Bataev10e775f2015-07-30 11:36:16 +0000446 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
447 Clause = ParseOpenMPClause(CKind);
448 else
449 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000450 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000451 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000452 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000453 // OpenMP [2.14.3.1, Restrictions]
454 // Only a single default clause may be specified on a parallel, task or
455 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000456 // OpenMP [2.5, parallel Construct, Restrictions]
457 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000458 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000459 Diag(Tok, diag::err_omp_more_one_clause)
460 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000461 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000462 }
463
464 Clause = ParseOpenMPSimpleClause(CKind);
465 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000466 case OMPC_schedule:
467 // OpenMP [2.7.1, Restrictions, p. 3]
468 // Only one schedule clause can appear on a loop directive.
469 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000470 Diag(Tok, diag::err_omp_more_one_clause)
471 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000472 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000473 }
474
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000475 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000476 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
477 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000478 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000479 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000480 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000481 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000482 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000483 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000484 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000485 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000486 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000487 case OMPC_simd:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000488 // OpenMP [2.7.1, Restrictions, p. 9]
489 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000490 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
491 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000492 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000493 Diag(Tok, diag::err_omp_more_one_clause)
494 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000495 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000496 }
497
498 Clause = ParseOpenMPClause(CKind);
499 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000500 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000501 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000502 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000504 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000505 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000506 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000507 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000508 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000509 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000510 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000511 case OMPC_map:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000512 Clause = ParseOpenMPVarListClause(CKind);
513 break;
514 case OMPC_unknown:
515 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000516 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000517 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000518 break;
519 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000520 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
521 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000522 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000523 break;
524 }
Craig Topper161e4db2014-05-21 06:02:52 +0000525 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000526}
527
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000528/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000529/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
530/// 'thread_limit', 'simdlen' or 'priority'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000531///
Alexey Bataev3778b602014-07-17 07:32:53 +0000532/// final-clause:
533/// 'final' '(' expression ')'
534///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000535/// num_threads-clause:
536/// 'num_threads' '(' expression ')'
537///
538/// safelen-clause:
539/// 'safelen' '(' expression ')'
540///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000541/// simdlen-clause:
542/// 'simdlen' '(' expression ')'
543///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000544/// collapse-clause:
545/// 'collapse' '(' expression ')'
546///
Alexey Bataeva0569352015-12-01 10:17:31 +0000547/// priority-clause:
548/// 'priority' '(' expression ')'
549///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000550OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
551 SourceLocation Loc = ConsumeToken();
552
553 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
554 if (T.expectAndConsume(diag::err_expected_lparen_after,
555 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000556 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000557
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000558 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000559 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
560 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000561 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000562
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000563 // Parse ')'.
564 T.consumeClose();
565
566 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000567 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000568
Alexey Bataeva55ed262014-05-28 06:15:33 +0000569 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000570 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000571}
572
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000573/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000574///
575/// default-clause:
576/// 'default' '(' 'none' | 'shared' ')
577///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000578/// proc_bind-clause:
579/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
580///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000581OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
582 SourceLocation Loc = Tok.getLocation();
583 SourceLocation LOpen = ConsumeToken();
584 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000585 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000586 if (T.expectAndConsume(diag::err_expected_lparen_after,
587 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000588 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000589
Alexey Bataeva55ed262014-05-28 06:15:33 +0000590 unsigned Type = getOpenMPSimpleClauseType(
591 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000592 SourceLocation TypeLoc = Tok.getLocation();
593 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
594 Tok.isNot(tok::annot_pragma_openmp_end))
595 ConsumeAnyToken();
596
597 // Parse ')'.
598 T.consumeClose();
599
600 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
601 Tok.getLocation());
602}
603
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000604/// \brief Parsing of OpenMP clauses like 'ordered'.
605///
606/// ordered-clause:
607/// 'ordered'
608///
Alexey Bataev236070f2014-06-20 11:19:47 +0000609/// nowait-clause:
610/// 'nowait'
611///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000612/// untied-clause:
613/// 'untied'
614///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000615/// mergeable-clause:
616/// 'mergeable'
617///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000618/// read-clause:
619/// 'read'
620///
Alexey Bataev346265e2015-09-25 10:37:12 +0000621/// threads-clause:
622/// 'threads'
623///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000624/// simd-clause:
625/// 'simd'
626///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000627OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
628 SourceLocation Loc = Tok.getLocation();
629 ConsumeAnyToken();
630
631 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
632}
633
634
Alexey Bataev56dafe82014-06-20 07:16:17 +0000635/// \brief Parsing of OpenMP clauses with single expressions and some additional
636/// argument like 'schedule' or 'dist_schedule'.
637///
638/// schedule-clause:
639/// 'schedule' '(' kind [',' expression ] ')'
640///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000641/// if-clause:
642/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
643///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000644OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
645 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000646 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000647 // Parse '('.
648 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
649 if (T.expectAndConsume(diag::err_expected_lparen_after,
650 getOpenMPClauseName(Kind)))
651 return nullptr;
652
653 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000654 unsigned Arg;
655 SourceLocation KLoc;
656 if (Kind == OMPC_schedule) {
657 Arg = getOpenMPSimpleClauseType(
658 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
659 KLoc = Tok.getLocation();
660 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
661 Tok.isNot(tok::annot_pragma_openmp_end))
662 ConsumeAnyToken();
663 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
664 Arg == OMPC_SCHEDULE_guided) &&
665 Tok.is(tok::comma))
666 DelimLoc = ConsumeAnyToken();
667 } else {
668 assert(Kind == OMPC_if);
669 KLoc = Tok.getLocation();
670 Arg = ParseOpenMPDirectiveKind(*this);
671 if (Arg != OMPD_unknown) {
672 ConsumeToken();
673 if (Tok.is(tok::colon))
674 DelimLoc = ConsumeToken();
675 else
676 Diag(Tok, diag::warn_pragma_expected_colon)
677 << "directive name modifier";
678 }
679 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000680
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000681 bool NeedAnExpression =
682 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
683 if (NeedAnExpression) {
684 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000685 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
686 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000687 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000688 }
689
690 // Parse ')'.
691 T.consumeClose();
692
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000693 if (NeedAnExpression && Val.isInvalid())
694 return nullptr;
695
Alexey Bataev56dafe82014-06-20 07:16:17 +0000696 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000697 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000698 T.getCloseLocation());
699}
700
Alexey Bataevc5e02582014-06-16 07:08:35 +0000701static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
702 UnqualifiedId &ReductionId) {
703 SourceLocation TemplateKWLoc;
704 if (ReductionIdScopeSpec.isEmpty()) {
705 auto OOK = OO_None;
706 switch (P.getCurToken().getKind()) {
707 case tok::plus:
708 OOK = OO_Plus;
709 break;
710 case tok::minus:
711 OOK = OO_Minus;
712 break;
713 case tok::star:
714 OOK = OO_Star;
715 break;
716 case tok::amp:
717 OOK = OO_Amp;
718 break;
719 case tok::pipe:
720 OOK = OO_Pipe;
721 break;
722 case tok::caret:
723 OOK = OO_Caret;
724 break;
725 case tok::ampamp:
726 OOK = OO_AmpAmp;
727 break;
728 case tok::pipepipe:
729 OOK = OO_PipePipe;
730 break;
731 default:
732 break;
733 }
734 if (OOK != OO_None) {
735 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000736 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000737 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
738 return false;
739 }
740 }
741 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
742 /*AllowDestructorName*/ false,
743 /*AllowConstructorName*/ false, ParsedType(),
744 TemplateKWLoc, ReductionId);
745}
746
Alexander Musman1bb328c2014-06-04 13:06:39 +0000747/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000748/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000749///
750/// private-clause:
751/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000752/// firstprivate-clause:
753/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000754/// lastprivate-clause:
755/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000756/// shared-clause:
757/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000758/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000759/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000760/// aligned-clause:
761/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000762/// reduction-clause:
763/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000764/// copyprivate-clause:
765/// 'copyprivate' '(' list ')'
766/// flush-clause:
767/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000768/// depend-clause:
769/// 'depend' '(' in | out | inout : list ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000770/// map-clause:
771/// 'map' '(' [ [ always , ]
772/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000773///
Alexey Bataev182227b2015-08-20 10:54:39 +0000774/// For 'linear' clause linear-list may have the following forms:
775/// list
776/// modifier(list)
777/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000778OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
779 SourceLocation Loc = Tok.getLocation();
780 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000781 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000782 // Optional scope specifier and unqualified id for reduction identifier.
783 CXXScopeSpec ReductionIdScopeSpec;
784 UnqualifiedId ReductionId;
785 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000786 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000787 // OpenMP 4.1 [2.15.3.7, linear Clause]
788 // If no modifier is specified it is assumed to be val.
789 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000790 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
791 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
792 bool MapTypeModifierSpecified = false;
793 bool UnexpectedId = false;
794 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000795
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000796 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000797 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 if (T.expectAndConsume(diag::err_expected_lparen_after,
799 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000800 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801
Alexey Bataev182227b2015-08-20 10:54:39 +0000802 bool NeedRParenForLinear = false;
803 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
804 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000805 // Handle reduction-identifier for reduction clause.
806 if (Kind == OMPC_reduction) {
807 ColonProtectionRAIIObject ColonRAII(*this);
808 if (getLangOpts().CPlusPlus) {
809 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
810 }
811 InvalidReductionId =
812 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
813 if (InvalidReductionId) {
814 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
815 StopBeforeMatch);
816 }
817 if (Tok.is(tok::colon)) {
818 ColonLoc = ConsumeToken();
819 } else {
820 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
821 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000822 } else if (Kind == OMPC_depend) {
823 // Handle dependency type for depend clause.
824 ColonProtectionRAIIObject ColonRAII(*this);
825 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
826 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000827 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000828
829 if (DepKind == OMPC_DEPEND_unknown) {
830 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
831 StopBeforeMatch);
832 } else {
833 ConsumeToken();
834 }
835 if (Tok.is(tok::colon)) {
836 ColonLoc = ConsumeToken();
837 } else {
838 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
839 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000840 } else if (Kind == OMPC_linear) {
841 // Try to parse modifier if any.
842 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000843 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000844 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000845 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000846 LinearT.consumeOpen();
847 NeedRParenForLinear = true;
848 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000849 } else if (Kind == OMPC_map) {
850 // Handle map type for map clause.
851 ColonProtectionRAIIObject ColonRAII(*this);
852
853 // the first identifier may be a list item, a map-type or
854 // a map-type-modifier
855 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
856 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
857 DepLinMapLoc = Tok.getLocation();
858 bool ColonExpected = false;
859
860 if (Tok.is(tok::identifier)) {
861 if (PP.LookAhead(0).is(tok::colon)) {
862 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
863 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
864 if (MapType == OMPC_MAP_unknown) {
865 Diag(Tok, diag::err_omp_unknown_map_type);
866 } else if (MapType == OMPC_MAP_always) {
867 Diag(Tok, diag::err_omp_map_type_missing);
868 }
869 ConsumeToken();
870 } else if (PP.LookAhead(0).is(tok::comma)) {
871 if (PP.LookAhead(1).is(tok::identifier) &&
872 PP.LookAhead(2).is(tok::colon)) {
873 MapTypeModifier =
874 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
875 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
876 if (MapTypeModifier != OMPC_MAP_always) {
877 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
878 MapTypeModifier = OMPC_MAP_unknown;
879 } else {
880 MapTypeModifierSpecified = true;
881 }
882
883 ConsumeToken();
884 ConsumeToken();
885
886 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
887 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
888 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
889 Diag(Tok, diag::err_omp_unknown_map_type);
890 }
891 ConsumeToken();
892 } else {
893 MapType = OMPC_MAP_tofrom;
894 }
895 } else {
896 MapType = OMPC_MAP_tofrom;
897 }
898 } else {
899 UnexpectedId = true;
900 }
901
902 if (Tok.is(tok::colon)) {
903 ColonLoc = ConsumeToken();
904 } else if (ColonExpected) {
905 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
906 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000907 }
908
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000909 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000910 bool IsComma =
911 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
912 (Kind != OMPC_map)) ||
913 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
914 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
915 (!MapTypeModifierSpecified ||
916 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
917 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000918 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000919 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000921 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000922 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000923 ExprResult VarExpr =
924 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000925 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000926 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000927 } else {
928 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000929 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000930 }
931 // Skip ',' if any
932 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000933 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000934 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000935 else if (Tok.isNot(tok::r_paren) &&
936 Tok.isNot(tok::annot_pragma_openmp_end) &&
937 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000938 Diag(Tok, diag::err_omp_expected_punc)
939 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
940 : getOpenMPClauseName(Kind))
941 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000942 }
943
Alexey Bataev182227b2015-08-20 10:54:39 +0000944 // Parse ')' for linear clause with modifier.
945 if (NeedRParenForLinear)
946 LinearT.consumeClose();
947
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000948 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000949 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000950 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
951 if (MustHaveTail) {
952 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000953 SourceLocation ELoc = ConsumeToken();
954 ExprResult Tail = ParseAssignmentExpression();
955 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000956 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000957 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000958 else
959 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
960 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000961 }
962
963 // Parse ')'.
964 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000965 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
966 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
Kelvin Li0bff7af2015-11-23 05:32:03 +0000967 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
968 InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +0000969 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000970 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000971
Alexey Bataevc5e02582014-06-16 07:08:35 +0000972 return Actions.ActOnOpenMPVarListClause(
973 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
974 ReductionIdScopeSpec,
975 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000976 : DeclarationNameInfo(),
Kelvin Li0bff7af2015-11-23 05:32:03 +0000977 DepKind, LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000978}
979