blob: b1d8f2fd319e280ec14e20f556a3f81cfbd6bb9c [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"
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Alexey Bataev4acb8592014-07-07 13:01:15 +000028static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000029 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
30 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
31 // TODO: add other combined directives in topological order.
32 const OpenMPDirectiveKind F[][3] = {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000033 {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/,
34 OMPD_cancellation_point},
35 {OMPD_for, OMPD_simd, OMPD_for_simd},
36 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
37 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
38 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000039 auto Tok = P.getCurToken();
40 auto DKind =
41 Tok.isAnnotation()
42 ? OMPD_unknown
43 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000044 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000045 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000046 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
47 TokenMatched =
48 (i == 0) &&
49 !P.getPreprocessor().getSpelling(Tok).compare("cancellation");
50 } else {
51 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
52 }
53 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000054 Tok = P.getPreprocessor().LookAhead(0);
55 auto SDKind =
56 Tok.isAnnotation()
57 ? OMPD_unknown
58 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000059 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
60 TokenMatched =
61 (i == 0) && !P.getPreprocessor().getSpelling(Tok).compare("point");
62 } else {
63 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
64 }
65 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000066 P.ConsumeToken();
67 DKind = F[i][2];
68 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000069 }
70 }
71 return DKind;
72}
73
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000074/// \brief Parsing of declarative OpenMP directives.
75///
76/// threadprivate-directive:
77/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000078///
79Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
80 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000081 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000082
83 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000084 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000085 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000086
87 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000088 case OMPD_threadprivate:
89 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000090 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000091 // The last seen token is annot_pragma_openmp_end - need to check for
92 // extra tokens.
93 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
94 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000095 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000096 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000097 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000098 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000099 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000100 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000101 }
102 break;
103 case OMPD_unknown:
104 Diag(Tok, diag::err_omp_unknown_directive);
105 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000106 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000107 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000108 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000109 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000110 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000111 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000112 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000113 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000114 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000115 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000116 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000117 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000118 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000119 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000120 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000121 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000122 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000123 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000124 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000125 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000126 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000127 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000128 case OMPD_cancellation_point:
Alexey Bataeva769e072013-03-22 06:34:35 +0000129 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000130 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000131 break;
132 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000133 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000134 return DeclGroupPtrTy();
135}
136
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000137/// \brief Parsing of declarative or executable OpenMP directives.
138///
139/// threadprivate-directive:
140/// annot_pragma_openmp 'threadprivate' simple-variable-list
141/// annot_pragma_openmp_end
142///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000143/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000144/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000145/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
146/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000147/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000148/// 'for simd' | 'parallel for simd' | 'target' | 'teams' | 'taskgroup'
149/// {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000150/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000151///
Alexey Bataev68446b72014-07-18 07:47:19 +0000152StmtResult
153Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000154 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000155 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000156 SmallVector<Expr *, 5> Identifiers;
157 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000158 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000159 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000160 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000161 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000162 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000163 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000164 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000165 // Name of critical directive.
166 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000168 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000169 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000170
171 switch (DKind) {
172 case OMPD_threadprivate:
173 ConsumeToken();
174 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
175 // The last seen token is annot_pragma_openmp_end - need to check for
176 // extra tokens.
177 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
178 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000179 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000180 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000181 }
182 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000183 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000184 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
185 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000186 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000187 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000188 case OMPD_flush:
189 if (PP.LookAhead(0).is(tok::l_paren)) {
190 FlushHasClause = true;
191 // Push copy of the current token back to stream to properly parse
192 // pseudo-clause OMPFlushClause.
193 PP.EnterToken(Tok);
194 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000195 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000196 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000197 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000198 case OMPD_cancellation_point:
Alexey Bataev68446b72014-07-18 07:47:19 +0000199 if (!StandAloneAllowed) {
200 Diag(Tok, diag::err_omp_immediate_directive)
201 << getOpenMPDirectiveName(DKind);
202 }
203 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000204 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000205 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000206 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000207 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000208 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000209 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000210 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000211 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000212 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000213 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000214 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000215 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000216 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000217 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000218 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000219 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000220 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000221 case OMPD_teams:
222 case OMPD_taskgroup: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000223 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000224 // Parse directive name of the 'critical' directive if any.
225 if (DKind == OMPD_critical) {
226 BalancedDelimiterTracker T(*this, tok::l_paren,
227 tok::annot_pragma_openmp_end);
228 if (!T.consumeOpen()) {
229 if (Tok.isAnyIdentifier()) {
230 DirName =
231 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
232 ConsumeAnyToken();
233 } else {
234 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
235 }
236 T.consumeClose();
237 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000238 } else if (DKind == OMPD_cancellation_point) {
239 CancelRegion = ParseOpenMPDirectiveKind(*this);
240 if (Tok.isNot(tok::annot_pragma_openmp_end))
241 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000242 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000243
Alexey Bataevf29276e2014-06-18 04:14:57 +0000244 if (isOpenMPLoopDirective(DKind))
245 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
246 if (isOpenMPSimdDirective(DKind))
247 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
248 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000249 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000250
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000251 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000252 OpenMPClauseKind CKind =
253 Tok.isAnnotation()
254 ? OMPC_unknown
255 : FlushHasClause ? OMPC_flush
256 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000257 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000258 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000259 OMPClause *Clause =
260 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000261 FirstClauses[CKind].setInt(true);
262 if (Clause) {
263 FirstClauses[CKind].setPointer(Clause);
264 Clauses.push_back(Clause);
265 }
266
267 // Skip ',' if any.
268 if (Tok.is(tok::comma))
269 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000270 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000271 }
272 // End location of the directive.
273 EndLoc = Tok.getLocation();
274 // Consume final annot_pragma_openmp_end.
275 ConsumeToken();
276
277 StmtResult AssociatedStmt;
278 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000279 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000280 // The body is a block scope like in Lambdas and Blocks.
281 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000282 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000283 Actions.ActOnStartOfCompoundStmt();
284 // Parse statement
285 AssociatedStmt = ParseStatement();
286 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000287 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
288 CreateDirective = AssociatedStmt.isUsable();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000289 }
290 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000291 Directive = Actions.ActOnOpenMPExecutableDirective(
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000292 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
293 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000294
295 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000296 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000298 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000299 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 case OMPD_unknown:
301 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000302 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000303 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000304 }
305 return Directive;
306}
307
Alexey Bataeva769e072013-03-22 06:34:35 +0000308/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000309/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000310///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000311/// simple-variable-list:
312/// '(' id-expression {, id-expression} ')'
313///
314bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
315 SmallVectorImpl<Expr *> &VarList,
316 bool AllowScopeSpecifier) {
317 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000318 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000319 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000320 if (T.expectAndConsume(diag::err_expected_lparen_after,
321 getOpenMPDirectiveName(Kind)))
322 return true;
323 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000324 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000325
326 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000327 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000328 CXXScopeSpec SS;
329 SourceLocation TemplateKWLoc;
330 UnqualifiedId Name;
331 // Read var name.
332 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000333 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000334
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000335 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
336 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000337 IsCorrect = false;
338 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000339 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000340 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
341 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000342 IsCorrect = false;
343 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000344 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000345 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
346 Tok.isNot(tok::annot_pragma_openmp_end)) {
347 IsCorrect = false;
348 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000349 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000350 Diag(PrevTok.getLocation(), diag::err_expected)
351 << tok::identifier
352 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000353 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000354 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000355 ExprResult Res =
356 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000357 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000358 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000359 }
360 // Consume ','.
361 if (Tok.is(tok::comma)) {
362 ConsumeToken();
363 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000364 }
365
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000366 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000367 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000368 IsCorrect = false;
369 }
370
371 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000372 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000373
374 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000375}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000376
377/// \brief Parsing of OpenMP clauses.
378///
379/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000380/// if-clause | final-clause | num_threads-clause | safelen-clause |
381/// default-clause | private-clause | firstprivate-clause | shared-clause
382/// | linear-clause | aligned-clause | collapse-clause |
383/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000384/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000385/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000386/// update-clause | capture-clause | seq_cst-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000387///
388OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
389 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000390 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000391 bool ErrorFound = false;
392 // Check if clause is allowed for the given directive.
393 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000394 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
395 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000396 ErrorFound = true;
397 }
398
399 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000400 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000401 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000402 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000403 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000404 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000405 // OpenMP [2.5, Restrictions]
406 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000407 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000408 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000409 // Only one safelen clause can appear on a simd directive.
410 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000411 // OpenMP [2.11.1, task Construct, Restrictions]
412 // At most one if clause can appear on the directive.
413 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000414 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000415 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
416 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000417 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000418 }
419
420 Clause = ParseOpenMPSingleExprClause(CKind);
421 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000422 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000423 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000424 // OpenMP [2.14.3.1, Restrictions]
425 // Only a single default clause may be specified on a parallel, task or
426 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000427 // OpenMP [2.5, parallel Construct, Restrictions]
428 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000429 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000430 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
431 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000432 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000433 }
434
435 Clause = ParseOpenMPSimpleClause(CKind);
436 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000437 case OMPC_schedule:
438 // OpenMP [2.7.1, Restrictions, p. 3]
439 // Only one schedule clause can appear on a loop directive.
440 if (!FirstClause) {
441 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
442 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000443 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000444 }
445
446 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
447 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000448 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000449 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000450 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000451 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000452 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000453 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000454 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000455 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000456 case OMPC_seq_cst:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000457 // OpenMP [2.7.1, Restrictions, p. 9]
458 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000459 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
460 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000461 if (!FirstClause) {
462 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
463 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000464 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000465 }
466
467 Clause = ParseOpenMPClause(CKind);
468 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000469 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000470 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000471 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000472 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000473 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000474 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000475 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000476 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000477 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000478 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000479 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000480 Clause = ParseOpenMPVarListClause(CKind);
481 break;
482 case OMPC_unknown:
483 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000484 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000485 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000486 break;
487 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000488 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
489 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000490 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000491 break;
492 }
Craig Topper161e4db2014-05-21 06:02:52 +0000493 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000494}
495
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000496/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000497/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000498/// 'thread_limit'.
499///
500/// if-clause:
501/// 'if' '(' expression ')'
502///
Alexey Bataev3778b602014-07-17 07:32:53 +0000503/// final-clause:
504/// 'final' '(' expression ')'
505///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000506/// num_threads-clause:
507/// 'num_threads' '(' expression ')'
508///
509/// safelen-clause:
510/// 'safelen' '(' expression ')'
511///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000512/// collapse-clause:
513/// 'collapse' '(' expression ')'
514///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000515OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
516 SourceLocation Loc = ConsumeToken();
517
518 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
519 if (T.expectAndConsume(diag::err_expected_lparen_after,
520 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000521 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000522
523 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
524 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
525
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000526 // Parse ')'.
527 T.consumeClose();
528
529 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000530 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000531
Alexey Bataeva55ed262014-05-28 06:15:33 +0000532 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000533 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000534}
535
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000536/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000537///
538/// default-clause:
539/// 'default' '(' 'none' | 'shared' ')
540///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000541/// proc_bind-clause:
542/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
543///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000544OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
545 SourceLocation Loc = Tok.getLocation();
546 SourceLocation LOpen = ConsumeToken();
547 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000548 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000549 if (T.expectAndConsume(diag::err_expected_lparen_after,
550 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000551 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000552
Alexey Bataeva55ed262014-05-28 06:15:33 +0000553 unsigned Type = getOpenMPSimpleClauseType(
554 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000555 SourceLocation TypeLoc = Tok.getLocation();
556 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
557 Tok.isNot(tok::annot_pragma_openmp_end))
558 ConsumeAnyToken();
559
560 // Parse ')'.
561 T.consumeClose();
562
563 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
564 Tok.getLocation());
565}
566
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000567/// \brief Parsing of OpenMP clauses like 'ordered'.
568///
569/// ordered-clause:
570/// 'ordered'
571///
Alexey Bataev236070f2014-06-20 11:19:47 +0000572/// nowait-clause:
573/// 'nowait'
574///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000575/// untied-clause:
576/// 'untied'
577///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000578/// mergeable-clause:
579/// 'mergeable'
580///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000581/// read-clause:
582/// 'read'
583///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000584OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
585 SourceLocation Loc = Tok.getLocation();
586 ConsumeAnyToken();
587
588 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
589}
590
591
Alexey Bataev56dafe82014-06-20 07:16:17 +0000592/// \brief Parsing of OpenMP clauses with single expressions and some additional
593/// argument like 'schedule' or 'dist_schedule'.
594///
595/// schedule-clause:
596/// 'schedule' '(' kind [',' expression ] ')'
597///
598OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
599 SourceLocation Loc = ConsumeToken();
600 SourceLocation CommaLoc;
601 // Parse '('.
602 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
603 if (T.expectAndConsume(diag::err_expected_lparen_after,
604 getOpenMPClauseName(Kind)))
605 return nullptr;
606
607 ExprResult Val;
608 unsigned Type = getOpenMPSimpleClauseType(
609 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
610 SourceLocation KLoc = Tok.getLocation();
611 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
612 Tok.isNot(tok::annot_pragma_openmp_end))
613 ConsumeAnyToken();
614
615 if (Kind == OMPC_schedule &&
616 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
617 Type == OMPC_SCHEDULE_guided) &&
618 Tok.is(tok::comma)) {
619 CommaLoc = ConsumeAnyToken();
620 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
621 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
622 if (Val.isInvalid())
623 return nullptr;
624 }
625
626 // Parse ')'.
627 T.consumeClose();
628
629 return Actions.ActOnOpenMPSingleExprWithArgClause(
630 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
631 T.getCloseLocation());
632}
633
Alexey Bataevc5e02582014-06-16 07:08:35 +0000634static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
635 UnqualifiedId &ReductionId) {
636 SourceLocation TemplateKWLoc;
637 if (ReductionIdScopeSpec.isEmpty()) {
638 auto OOK = OO_None;
639 switch (P.getCurToken().getKind()) {
640 case tok::plus:
641 OOK = OO_Plus;
642 break;
643 case tok::minus:
644 OOK = OO_Minus;
645 break;
646 case tok::star:
647 OOK = OO_Star;
648 break;
649 case tok::amp:
650 OOK = OO_Amp;
651 break;
652 case tok::pipe:
653 OOK = OO_Pipe;
654 break;
655 case tok::caret:
656 OOK = OO_Caret;
657 break;
658 case tok::ampamp:
659 OOK = OO_AmpAmp;
660 break;
661 case tok::pipepipe:
662 OOK = OO_PipePipe;
663 break;
664 default:
665 break;
666 }
667 if (OOK != OO_None) {
668 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000669 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000670 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
671 return false;
672 }
673 }
674 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
675 /*AllowDestructorName*/ false,
676 /*AllowConstructorName*/ false, ParsedType(),
677 TemplateKWLoc, ReductionId);
678}
679
Alexander Musman1bb328c2014-06-04 13:06:39 +0000680/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000681/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000682///
683/// private-clause:
684/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000685/// firstprivate-clause:
686/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000687/// lastprivate-clause:
688/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000689/// shared-clause:
690/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000691/// linear-clause:
692/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000693/// aligned-clause:
694/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000695/// reduction-clause:
696/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000697/// copyprivate-clause:
698/// 'copyprivate' '(' list ')'
699/// flush-clause:
700/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000701/// depend-clause:
702/// 'depend' '(' in | out | inout : list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000703///
704OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
705 SourceLocation Loc = Tok.getLocation();
706 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000707 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000708 // Optional scope specifier and unqualified id for reduction identifier.
709 CXXScopeSpec ReductionIdScopeSpec;
710 UnqualifiedId ReductionId;
711 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000712 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
713 SourceLocation DepLoc;
714
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000715 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000716 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 if (T.expectAndConsume(diag::err_expected_lparen_after,
718 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000719 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000720
Alexey Bataevc5e02582014-06-16 07:08:35 +0000721 // Handle reduction-identifier for reduction clause.
722 if (Kind == OMPC_reduction) {
723 ColonProtectionRAIIObject ColonRAII(*this);
724 if (getLangOpts().CPlusPlus) {
725 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
726 }
727 InvalidReductionId =
728 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
729 if (InvalidReductionId) {
730 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
731 StopBeforeMatch);
732 }
733 if (Tok.is(tok::colon)) {
734 ColonLoc = ConsumeToken();
735 } else {
736 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
737 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000738 } else if (Kind == OMPC_depend) {
739 // Handle dependency type for depend clause.
740 ColonProtectionRAIIObject ColonRAII(*this);
741 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
742 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
743 DepLoc = Tok.getLocation();
744
745 if (DepKind == OMPC_DEPEND_unknown) {
746 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
747 StopBeforeMatch);
748 } else {
749 ConsumeToken();
750 }
751 if (Tok.is(tok::colon)) {
752 ColonLoc = ConsumeToken();
753 } else {
754 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
755 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000756 }
757
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000758 SmallVector<Expr *, 5> Vars;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000759 bool IsComma = ((Kind != OMPC_reduction) && (Kind != OMPC_depend)) ||
760 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
761 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000762 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000763 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000764 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000765 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000766 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000767 ExprResult VarExpr =
768 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000769 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000770 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000771 } else {
772 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000773 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000774 }
775 // Skip ',' if any
776 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000777 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000778 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000779 else if (Tok.isNot(tok::r_paren) &&
780 Tok.isNot(tok::annot_pragma_openmp_end) &&
781 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000782 Diag(Tok, diag::err_omp_expected_punc)
783 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
784 : getOpenMPClauseName(Kind))
785 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000786 }
787
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000788 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000789 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000790 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
791 if (MustHaveTail) {
792 ColonLoc = Tok.getLocation();
793 ConsumeToken();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000794 ExprResult Tail =
795 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexander Musman8dba6642014-04-22 13:09:42 +0000796 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000797 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000798 else
799 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
800 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801 }
802
803 // Parse ')'.
804 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000805 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
806 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
807 InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000808 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000809
Alexey Bataevc5e02582014-06-16 07:08:35 +0000810 return Actions.ActOnOpenMPVarListClause(
811 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
812 ReductionIdScopeSpec,
813 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000814 : DeclarationNameInfo(),
815 DepKind, DepLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000816}
817