blob: f5827b3ecc7d4bad4df1aa396227fddf23a974b6 [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 =
Michael Wong65f367f2015-07-21 13:44:28 +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:
Alexey Bataeva769e072013-03-22 06:34:35 +0000139 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000140 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000141 break;
142 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000143 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000144 return DeclGroupPtrTy();
145}
146
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000147/// \brief Parsing of declarative or executable OpenMP directives.
148///
149/// threadprivate-directive:
150/// annot_pragma_openmp 'threadprivate' simple-variable-list
151/// annot_pragma_openmp_end
152///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000153/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000154/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000155/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
156/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000157/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000158/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
159/// 'taskgroup' | 'teams' {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000160/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000161///
Alexey Bataev68446b72014-07-18 07:47:19 +0000162StmtResult
163Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000164 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000165 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000166 SmallVector<Expr *, 5> Identifiers;
167 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000168 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000169 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000170 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000171 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000172 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000173 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000174 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175 // Name of critical directive.
176 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000177 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000178 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000179 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000180
181 switch (DKind) {
182 case OMPD_threadprivate:
183 ConsumeToken();
184 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
185 // The last seen token is annot_pragma_openmp_end - need to check for
186 // extra tokens.
187 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
188 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000189 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000190 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000191 }
192 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000193 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000194 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
195 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000196 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000197 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000198 case OMPD_flush:
199 if (PP.LookAhead(0).is(tok::l_paren)) {
200 FlushHasClause = true;
201 // Push copy of the current token back to stream to properly parse
202 // pseudo-clause OMPFlushClause.
203 PP.EnterToken(Tok);
204 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000205 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000206 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000207 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000208 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000209 case OMPD_cancel:
Alexey Bataev68446b72014-07-18 07:47:19 +0000210 if (!StandAloneAllowed) {
211 Diag(Tok, diag::err_omp_immediate_directive)
212 << getOpenMPDirectiveName(DKind);
213 }
214 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000215 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000216 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000217 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000218 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000219 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000220 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000221 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000222 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000223 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000224 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000225 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000226 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000227 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000228 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000229 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000230 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000231 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000232 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000233 case OMPD_taskgroup:
234 case OMPD_target_data: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000235 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000236 // Parse directive name of the 'critical' directive if any.
237 if (DKind == OMPD_critical) {
238 BalancedDelimiterTracker T(*this, tok::l_paren,
239 tok::annot_pragma_openmp_end);
240 if (!T.consumeOpen()) {
241 if (Tok.isAnyIdentifier()) {
242 DirName =
243 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
244 ConsumeAnyToken();
245 } else {
246 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
247 }
248 T.consumeClose();
249 }
Alexey Bataev80909872015-07-02 11:25:17 +0000250 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000251 CancelRegion = ParseOpenMPDirectiveKind(*this);
252 if (Tok.isNot(tok::annot_pragma_openmp_end))
253 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000254 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000255
Alexey Bataevf29276e2014-06-18 04:14:57 +0000256 if (isOpenMPLoopDirective(DKind))
257 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
258 if (isOpenMPSimdDirective(DKind))
259 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
260 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000261 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000262
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000263 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000264 OpenMPClauseKind CKind =
265 Tok.isAnnotation()
266 ? OMPC_unknown
267 : FlushHasClause ? OMPC_flush
268 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000269 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000270 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000271 OMPClause *Clause =
272 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000273 FirstClauses[CKind].setInt(true);
274 if (Clause) {
275 FirstClauses[CKind].setPointer(Clause);
276 Clauses.push_back(Clause);
277 }
278
279 // Skip ',' if any.
280 if (Tok.is(tok::comma))
281 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000282 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000283 }
284 // End location of the directive.
285 EndLoc = Tok.getLocation();
286 // Consume final annot_pragma_openmp_end.
287 ConsumeToken();
288
289 StmtResult AssociatedStmt;
290 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000291 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000292 // The body is a block scope like in Lambdas and Blocks.
293 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000294 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000295 Actions.ActOnStartOfCompoundStmt();
296 // Parse statement
297 AssociatedStmt = ParseStatement();
298 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000299 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
300 CreateDirective = AssociatedStmt.isUsable();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000301 }
302 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000303 Directive = Actions.ActOnOpenMPExecutableDirective(
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000304 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 |
Michael Wong65f367f2015-07-21 13:44:28 +0000398/// update-clause | capture-clause | seq_cst-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000399///
400OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
401 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000402 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000403 bool ErrorFound = false;
404 // Check if clause is allowed for the given directive.
405 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000406 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
407 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000408 ErrorFound = true;
409 }
410
411 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000412 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000413 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000414 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000415 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000416 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000417 // OpenMP [2.5, Restrictions]
418 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000419 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000420 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000421 // Only one safelen clause can appear on a simd directive.
422 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000423 // OpenMP [2.11.1, task Construct, Restrictions]
424 // At most one if clause can appear on the directive.
425 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000426 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000427 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
428 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000429 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000430 }
431
432 Clause = ParseOpenMPSingleExprClause(CKind);
433 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000434 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000435 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000436 // OpenMP [2.14.3.1, Restrictions]
437 // Only a single default clause may be specified on a parallel, task or
438 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000439 // OpenMP [2.5, parallel Construct, Restrictions]
440 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000441 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000442 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
443 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000444 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000445 }
446
447 Clause = ParseOpenMPSimpleClause(CKind);
448 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000449 case OMPC_schedule:
450 // OpenMP [2.7.1, Restrictions, p. 3]
451 // Only one schedule clause can appear on a loop directive.
452 if (!FirstClause) {
453 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
454 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000455 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000456 }
457
458 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
459 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000460 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000461 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000462 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000463 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000464 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000465 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000466 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000467 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000468 case OMPC_seq_cst:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000469 // OpenMP [2.7.1, Restrictions, p. 9]
470 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000471 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
472 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000473 if (!FirstClause) {
474 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
475 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000476 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000477 }
478
479 Clause = ParseOpenMPClause(CKind);
480 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000481 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000482 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000483 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000484 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000485 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000486 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000487 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000488 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000489 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000490 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000491 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000492 Clause = ParseOpenMPVarListClause(CKind);
493 break;
494 case OMPC_unknown:
495 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000496 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000497 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000498 break;
499 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000500 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
501 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000502 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000503 break;
504 }
Craig Topper161e4db2014-05-21 06:02:52 +0000505 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000506}
507
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000508/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000509/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000510/// 'thread_limit'.
511///
512/// if-clause:
513/// 'if' '(' expression ')'
514///
Alexey Bataev3778b602014-07-17 07:32:53 +0000515/// final-clause:
516/// 'final' '(' expression ')'
517///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000518/// num_threads-clause:
519/// 'num_threads' '(' expression ')'
520///
521/// safelen-clause:
522/// 'safelen' '(' expression ')'
523///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000524/// collapse-clause:
525/// 'collapse' '(' expression ')'
526///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000527OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
528 SourceLocation Loc = ConsumeToken();
529
530 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
531 if (T.expectAndConsume(diag::err_expected_lparen_after,
532 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000533 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000534
535 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
536 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
537
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000538 // Parse ')'.
539 T.consumeClose();
540
541 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000542 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000543
Alexey Bataeva55ed262014-05-28 06:15:33 +0000544 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000545 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000546}
547
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000548/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000549///
550/// default-clause:
551/// 'default' '(' 'none' | 'shared' ')
552///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000553/// proc_bind-clause:
554/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
555///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000556OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
557 SourceLocation Loc = Tok.getLocation();
558 SourceLocation LOpen = ConsumeToken();
559 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000560 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000561 if (T.expectAndConsume(diag::err_expected_lparen_after,
562 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000563 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000564
Alexey Bataeva55ed262014-05-28 06:15:33 +0000565 unsigned Type = getOpenMPSimpleClauseType(
566 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000567 SourceLocation TypeLoc = Tok.getLocation();
568 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
569 Tok.isNot(tok::annot_pragma_openmp_end))
570 ConsumeAnyToken();
571
572 // Parse ')'.
573 T.consumeClose();
574
575 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
576 Tok.getLocation());
577}
578
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000579/// \brief Parsing of OpenMP clauses like 'ordered'.
580///
581/// ordered-clause:
582/// 'ordered'
583///
Alexey Bataev236070f2014-06-20 11:19:47 +0000584/// nowait-clause:
585/// 'nowait'
586///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000587/// untied-clause:
588/// 'untied'
589///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000590/// mergeable-clause:
591/// 'mergeable'
592///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000593/// read-clause:
594/// 'read'
595///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000596OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
597 SourceLocation Loc = Tok.getLocation();
598 ConsumeAnyToken();
599
600 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
601}
602
603
Alexey Bataev56dafe82014-06-20 07:16:17 +0000604/// \brief Parsing of OpenMP clauses with single expressions and some additional
605/// argument like 'schedule' or 'dist_schedule'.
606///
607/// schedule-clause:
608/// 'schedule' '(' kind [',' expression ] ')'
609///
610OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
611 SourceLocation Loc = ConsumeToken();
612 SourceLocation CommaLoc;
613 // Parse '('.
614 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
615 if (T.expectAndConsume(diag::err_expected_lparen_after,
616 getOpenMPClauseName(Kind)))
617 return nullptr;
618
619 ExprResult Val;
620 unsigned Type = getOpenMPSimpleClauseType(
621 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
622 SourceLocation KLoc = Tok.getLocation();
623 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
624 Tok.isNot(tok::annot_pragma_openmp_end))
625 ConsumeAnyToken();
626
627 if (Kind == OMPC_schedule &&
628 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
629 Type == OMPC_SCHEDULE_guided) &&
630 Tok.is(tok::comma)) {
631 CommaLoc = ConsumeAnyToken();
632 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
633 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
634 if (Val.isInvalid())
635 return nullptr;
636 }
637
638 // Parse ')'.
639 T.consumeClose();
640
641 return Actions.ActOnOpenMPSingleExprWithArgClause(
642 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
643 T.getCloseLocation());
644}
645
Alexey Bataevc5e02582014-06-16 07:08:35 +0000646static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
647 UnqualifiedId &ReductionId) {
648 SourceLocation TemplateKWLoc;
649 if (ReductionIdScopeSpec.isEmpty()) {
650 auto OOK = OO_None;
651 switch (P.getCurToken().getKind()) {
652 case tok::plus:
653 OOK = OO_Plus;
654 break;
655 case tok::minus:
656 OOK = OO_Minus;
657 break;
658 case tok::star:
659 OOK = OO_Star;
660 break;
661 case tok::amp:
662 OOK = OO_Amp;
663 break;
664 case tok::pipe:
665 OOK = OO_Pipe;
666 break;
667 case tok::caret:
668 OOK = OO_Caret;
669 break;
670 case tok::ampamp:
671 OOK = OO_AmpAmp;
672 break;
673 case tok::pipepipe:
674 OOK = OO_PipePipe;
675 break;
676 default:
677 break;
678 }
679 if (OOK != OO_None) {
680 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000681 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000682 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
683 return false;
684 }
685 }
686 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
687 /*AllowDestructorName*/ false,
688 /*AllowConstructorName*/ false, ParsedType(),
689 TemplateKWLoc, ReductionId);
690}
691
Alexander Musman1bb328c2014-06-04 13:06:39 +0000692/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000693/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000694///
695/// private-clause:
696/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000697/// firstprivate-clause:
698/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000699/// lastprivate-clause:
700/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701/// shared-clause:
702/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000703/// linear-clause:
704/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000705/// aligned-clause:
706/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000707/// reduction-clause:
708/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000709/// copyprivate-clause:
710/// 'copyprivate' '(' list ')'
711/// flush-clause:
712/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000713/// depend-clause:
714/// 'depend' '(' in | out | inout : list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000715///
716OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
717 SourceLocation Loc = Tok.getLocation();
718 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000719 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000720 // Optional scope specifier and unqualified id for reduction identifier.
721 CXXScopeSpec ReductionIdScopeSpec;
722 UnqualifiedId ReductionId;
723 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000724 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
725 SourceLocation DepLoc;
726
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000727 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000728 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000729 if (T.expectAndConsume(diag::err_expected_lparen_after,
730 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000731 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000732
Alexey Bataevc5e02582014-06-16 07:08:35 +0000733 // Handle reduction-identifier for reduction clause.
734 if (Kind == OMPC_reduction) {
735 ColonProtectionRAIIObject ColonRAII(*this);
736 if (getLangOpts().CPlusPlus) {
737 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
738 }
739 InvalidReductionId =
740 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
741 if (InvalidReductionId) {
742 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
743 StopBeforeMatch);
744 }
745 if (Tok.is(tok::colon)) {
746 ColonLoc = ConsumeToken();
747 } else {
748 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
749 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000750 } else if (Kind == OMPC_depend) {
751 // Handle dependency type for depend clause.
752 ColonProtectionRAIIObject ColonRAII(*this);
753 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
754 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
755 DepLoc = Tok.getLocation();
756
757 if (DepKind == OMPC_DEPEND_unknown) {
758 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
759 StopBeforeMatch);
760 } else {
761 ConsumeToken();
762 }
763 if (Tok.is(tok::colon)) {
764 ColonLoc = ConsumeToken();
765 } else {
766 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
767 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000768 }
769
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000770 SmallVector<Expr *, 5> Vars;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000771 bool IsComma = ((Kind != OMPC_reduction) && (Kind != OMPC_depend)) ||
772 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
773 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000774 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000775 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000776 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000777 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000778 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000779 ExprResult VarExpr =
780 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000781 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000782 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000783 } else {
784 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000785 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786 }
787 // Skip ',' if any
788 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000789 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000790 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000791 else if (Tok.isNot(tok::r_paren) &&
792 Tok.isNot(tok::annot_pragma_openmp_end) &&
793 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000794 Diag(Tok, diag::err_omp_expected_punc)
795 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
796 : getOpenMPClauseName(Kind))
797 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000798 }
799
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000800 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000801 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000802 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
803 if (MustHaveTail) {
804 ColonLoc = Tok.getLocation();
805 ConsumeToken();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000806 ExprResult Tail =
807 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexander Musman8dba6642014-04-22 13:09:42 +0000808 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000809 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000810 else
811 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
812 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000813 }
814
815 // Parse ')'.
816 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000817 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
818 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
819 InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000820 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000821
Alexey Bataevc5e02582014-06-16 07:08:35 +0000822 return Actions.ActOnOpenMPVarListClause(
823 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
824 ReductionIdScopeSpec,
825 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000826 : DeclarationNameInfo(),
827 DepKind, DepLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000828}
829