blob: 96cf37912f2ab3b84cfc76b1a8b108a58cd73fa4 [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
Dmitry Polukhin82478332016-02-13 06:53:38 +000029namespace {
30enum OpenMPDirectiveKindEx {
31 OMPD_cancellation = OMPD_unknown + 1,
32 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000033 OMPD_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000034 OMPD_enter,
35 OMPD_exit,
36 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000037 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000038 OMPD_target_enter,
39 OMPD_target_exit
40};
41} // namespace
42
43// Map token string to extended OMP token kind that are
44// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
45static unsigned getOpenMPDirectiveKindEx(StringRef S) {
46 auto DKind = getOpenMPDirectiveKind(S);
47 if (DKind != OMPD_unknown)
48 return DKind;
49
50 return llvm::StringSwitch<unsigned>(S)
51 .Case("cancellation", OMPD_cancellation)
52 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000053 .Case("declare", OMPD_declare)
Dmitry Polukhin82478332016-02-13 06:53:38 +000054 .Case("enter", OMPD_enter)
55 .Case("exit", OMPD_exit)
56 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000057 .Case("reduction", OMPD_reduction)
Dmitry Polukhin82478332016-02-13 06:53:38 +000058 .Default(OMPD_unknown);
59}
60
Alexey Bataev4acb8592014-07-07 13:01:15 +000061static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000062 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
63 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
64 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000065 static const unsigned F[][3] = {
66 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000067 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000068 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin82478332016-02-13 06:53:38 +000069 { OMPD_target, OMPD_data, OMPD_target_data },
70 { OMPD_target, OMPD_enter, OMPD_target_enter },
71 { OMPD_target, OMPD_exit, OMPD_target_exit },
72 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
73 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
74 { OMPD_for, OMPD_simd, OMPD_for_simd },
75 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
76 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
77 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
78 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
79 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
80 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
81 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000082 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +000083 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +000084 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +000085 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +000086 ? static_cast<unsigned>(OMPD_unknown)
87 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
88 if (DKind == OMPD_unknown)
89 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +000090
Alexander Musmanf82886e2014-09-18 05:12:34 +000091 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +000092 if (DKind != F[i][0])
93 continue;
Michael Wong65f367f2015-07-21 13:44:28 +000094
Dmitry Polukhin82478332016-02-13 06:53:38 +000095 Tok = P.getPreprocessor().LookAhead(0);
96 unsigned SDKind =
97 Tok.isAnnotation()
98 ? static_cast<unsigned>(OMPD_unknown)
99 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
100 if (SDKind == OMPD_unknown)
101 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000102
Dmitry Polukhin82478332016-02-13 06:53:38 +0000103 if (SDKind == F[i][1]) {
104 P.ConsumeToken();
105 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000106 }
107 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000108 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
109 : OMPD_unknown;
110}
111
112static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000113 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000114 Sema &Actions = P.getActions();
115 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000116 // Allow to use 'operator' keyword for C++ operators
117 bool WithOperator = false;
118 if (Tok.is(tok::kw_operator)) {
119 P.ConsumeToken();
120 Tok = P.getCurToken();
121 WithOperator = true;
122 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000123 switch (Tok.getKind()) {
124 case tok::plus: // '+'
125 OOK = OO_Plus;
126 break;
127 case tok::minus: // '-'
128 OOK = OO_Minus;
129 break;
130 case tok::star: // '*'
131 OOK = OO_Star;
132 break;
133 case tok::amp: // '&'
134 OOK = OO_Amp;
135 break;
136 case tok::pipe: // '|'
137 OOK = OO_Pipe;
138 break;
139 case tok::caret: // '^'
140 OOK = OO_Caret;
141 break;
142 case tok::ampamp: // '&&'
143 OOK = OO_AmpAmp;
144 break;
145 case tok::pipepipe: // '||'
146 OOK = OO_PipePipe;
147 break;
148 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000149 if (!WithOperator)
150 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000151 default:
152 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
153 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
154 Parser::StopBeforeMatch);
155 return DeclarationName();
156 }
157 P.ConsumeToken();
158 auto &DeclNames = Actions.getASTContext().DeclarationNames;
159 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
160 : DeclNames.getCXXOperatorName(OOK);
161}
162
163/// \brief Parse 'omp declare reduction' construct.
164///
165/// declare-reduction-directive:
166/// annot_pragma_openmp 'declare' 'reduction'
167/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
168/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
169/// annot_pragma_openmp_end
170/// <reduction_id> is either a base language identifier or one of the following
171/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
172///
173Parser::DeclGroupPtrTy
174Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
175 // Parse '('.
176 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
177 if (T.expectAndConsume(diag::err_expected_lparen_after,
178 getOpenMPDirectiveName(OMPD_declare_reduction))) {
179 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
180 return DeclGroupPtrTy();
181 }
182
183 DeclarationName Name = parseOpenMPReductionId(*this);
184 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
185 return DeclGroupPtrTy();
186
187 // Consume ':'.
188 bool IsCorrect = !ExpectAndConsume(tok::colon);
189
190 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
191 return DeclGroupPtrTy();
192
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000193 IsCorrect = IsCorrect && !Name.isEmpty();
194
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000195 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
196 Diag(Tok.getLocation(), diag::err_expected_type);
197 IsCorrect = false;
198 }
199
200 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
201 return DeclGroupPtrTy();
202
203 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
204 // Parse list of types until ':' token.
205 do {
206 ColonProtectionRAIIObject ColonRAII(*this);
207 SourceRange Range;
208 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
209 if (TR.isUsable()) {
210 auto ReductionType =
211 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
212 if (!ReductionType.isNull()) {
213 ReductionTypes.push_back(
214 std::make_pair(ReductionType, Range.getBegin()));
215 }
216 } else {
217 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
218 StopBeforeMatch);
219 }
220
221 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
222 break;
223
224 // Consume ','.
225 if (ExpectAndConsume(tok::comma)) {
226 IsCorrect = false;
227 if (Tok.is(tok::annot_pragma_openmp_end)) {
228 Diag(Tok.getLocation(), diag::err_expected_type);
229 return DeclGroupPtrTy();
230 }
231 }
232 } while (Tok.isNot(tok::annot_pragma_openmp_end));
233
234 if (ReductionTypes.empty()) {
235 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
236 return DeclGroupPtrTy();
237 }
238
239 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
240 return DeclGroupPtrTy();
241
242 // Consume ':'.
243 if (ExpectAndConsume(tok::colon))
244 IsCorrect = false;
245
246 if (Tok.is(tok::annot_pragma_openmp_end)) {
247 Diag(Tok.getLocation(), diag::err_expected_expression);
248 return DeclGroupPtrTy();
249 }
250
251 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
252 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
253
254 // Parse <combiner> expression and then parse initializer if any for each
255 // correct type.
256 unsigned I = 0, E = ReductionTypes.size();
257 for (auto *D : DRD.get()) {
258 TentativeParsingAction TPA(*this);
259 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
260 Scope::OpenMPDirectiveScope);
261 // Parse <combiner> expression.
262 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
263 ExprResult CombinerResult =
264 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
265 D->getLocation(), /*DiscardedValue=*/true);
266 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
267
268 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
269 Tok.isNot(tok::annot_pragma_openmp_end)) {
270 TPA.Commit();
271 IsCorrect = false;
272 break;
273 }
274 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
275 ExprResult InitializerResult;
276 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
277 // Parse <initializer> expression.
278 if (Tok.is(tok::identifier) &&
279 Tok.getIdentifierInfo()->isStr("initializer"))
280 ConsumeToken();
281 else {
282 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
283 TPA.Commit();
284 IsCorrect = false;
285 break;
286 }
287 // Parse '('.
288 BalancedDelimiterTracker T(*this, tok::l_paren,
289 tok::annot_pragma_openmp_end);
290 IsCorrect =
291 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
292 IsCorrect;
293 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
294 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
295 Scope::OpenMPDirectiveScope);
296 // Parse expression.
297 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
298 InitializerResult = Actions.ActOnFinishFullExpr(
299 ParseAssignmentExpression().get(), D->getLocation(),
300 /*DiscardedValue=*/true);
301 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
302 D, InitializerResult.get());
303 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
304 Tok.isNot(tok::annot_pragma_openmp_end)) {
305 TPA.Commit();
306 IsCorrect = false;
307 break;
308 }
309 IsCorrect =
310 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
311 }
312 }
313
314 ++I;
315 // Revert parsing if not the last type, otherwise accept it, we're done with
316 // parsing.
317 if (I != E)
318 TPA.Revert();
319 else
320 TPA.Commit();
321 }
322 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
323 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000324}
325
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000326/// \brief Parsing of declarative OpenMP directives.
327///
328/// threadprivate-directive:
329/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000330/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000331///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000332/// declare-reduction-directive:
333/// annot_pragma_openmp 'declare' 'reduction' [...]
334/// annot_pragma_openmp_end
335///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000336/// declare-simd-directive:
337/// annot_pragma_openmp 'declare simd' {<clause> [,]}
338/// annot_pragma_openmp_end
339/// <function declaration/definition>
340///
341Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
342 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
343 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000344 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000345 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000346
347 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000348 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000349 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000350
351 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000352 case OMPD_threadprivate:
353 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000354 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000355 // The last seen token is annot_pragma_openmp_end - need to check for
356 // extra tokens.
357 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
358 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000359 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000360 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000361 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000362 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000363 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000364 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000365 }
366 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000367 case OMPD_declare_reduction:
368 ConsumeToken();
369 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
370 // The last seen token is annot_pragma_openmp_end - need to check for
371 // extra tokens.
372 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
373 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
374 << getOpenMPDirectiveName(OMPD_declare_reduction);
375 while (Tok.isNot(tok::annot_pragma_openmp_end))
376 ConsumeAnyToken();
377 }
378 // Skip the last annot_pragma_openmp_end.
379 ConsumeToken();
380 return Res;
381 }
382 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000383 case OMPD_declare_simd: {
384 // The syntax is:
385 // { #pragma omp declare simd }
386 // <function-declaration-or-definition>
387 //
388
389 ConsumeToken();
390 // The last seen token is annot_pragma_openmp_end - need to check for
391 // extra tokens.
392 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
393 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
394 << getOpenMPDirectiveName(OMPD_declare_simd);
395 while (Tok.isNot(tok::annot_pragma_openmp_end))
396 ConsumeAnyToken();
397 }
398 // Skip the last annot_pragma_openmp_end.
399 ConsumeToken();
400
401 DeclGroupPtrTy Ptr;
402 if (Tok.is(tok::annot_pragma_openmp)) {
403 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
404 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
405 // Here we expect to see some function declaration.
406 if (AS == AS_none) {
407 assert(TagType == DeclSpec::TST_unspecified);
408 MaybeParseCXX11Attributes(Attrs);
409 MaybeParseMicrosoftAttributes(Attrs);
410 ParsingDeclSpec PDS(*this);
411 Ptr = ParseExternalDeclaration(Attrs, &PDS);
412 } else {
413 Ptr =
414 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
415 }
416 }
417 if (!Ptr) {
418 Diag(Loc, diag::err_omp_decl_in_declare_simd);
419 return DeclGroupPtrTy();
420 }
421
422 return Actions.ActOnOpenMPDeclareSimdDirective(Ptr, Loc);
423 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000424 case OMPD_unknown:
425 Diag(Tok, diag::err_omp_unknown_directive);
426 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000427 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000428 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000429 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000430 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000431 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000432 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000433 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000434 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000435 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000436 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000437 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000438 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000439 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000440 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000441 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000442 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000443 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000444 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000445 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000446 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000447 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000448 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000449 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000450 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000451 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000452 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000453 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000454 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000455 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000456 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000457 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000458 case OMPD_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000459 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000460 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000461 break;
462 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000463 while (Tok.isNot(tok::annot_pragma_openmp_end))
464 ConsumeAnyToken();
465 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000466 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000467}
468
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000469/// \brief Parsing of declarative or executable OpenMP directives.
470///
471/// threadprivate-directive:
472/// annot_pragma_openmp 'threadprivate' simple-variable-list
473/// annot_pragma_openmp_end
474///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000475/// declare-reduction-directive:
476/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
477/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
478/// ('omp_priv' '=' <expression>|<function_call>) ')']
479/// annot_pragma_openmp_end
480///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000481/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000482/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000483/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
484/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000485/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000486/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000487/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000488/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000489/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000490/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000491///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000492StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
493 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000494 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000495 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000496 SmallVector<Expr *, 5> Identifiers;
497 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000498 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000499 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000500 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000501 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000502 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000503 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000504 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505 // Name of critical directive.
506 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000507 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000508 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000509 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000510
511 switch (DKind) {
512 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000513 if (Allowed != ACK_Any) {
514 Diag(Tok, diag::err_omp_immediate_directive)
515 << getOpenMPDirectiveName(DKind) << 0;
516 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000517 ConsumeToken();
518 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
519 // The last seen token is annot_pragma_openmp_end - need to check for
520 // extra tokens.
521 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
522 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000523 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000524 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000525 }
526 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000527 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000528 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
529 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000530 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000531 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000532 case OMPD_declare_reduction:
533 ConsumeToken();
534 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
535 // The last seen token is annot_pragma_openmp_end - need to check for
536 // extra tokens.
537 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
538 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
539 << getOpenMPDirectiveName(OMPD_declare_reduction);
540 while (Tok.isNot(tok::annot_pragma_openmp_end))
541 ConsumeAnyToken();
542 }
543 ConsumeAnyToken();
544 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
545 } else
546 SkipUntil(tok::annot_pragma_openmp_end);
547 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000548 case OMPD_flush:
549 if (PP.LookAhead(0).is(tok::l_paren)) {
550 FlushHasClause = true;
551 // Push copy of the current token back to stream to properly parse
552 // pseudo-clause OMPFlushClause.
553 PP.EnterToken(Tok);
554 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000555 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000556 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000557 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000558 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000559 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000560 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000561 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000562 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000563 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000564 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000565 }
566 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000567 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000568 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000569 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000570 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000571 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000572 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000573 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000574 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000575 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000576 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000577 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000578 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000579 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000580 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000581 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000582 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000583 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000584 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000585 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000586 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000587 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000588 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000589 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000590 case OMPD_taskloop_simd:
591 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000592 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000593 // Parse directive name of the 'critical' directive if any.
594 if (DKind == OMPD_critical) {
595 BalancedDelimiterTracker T(*this, tok::l_paren,
596 tok::annot_pragma_openmp_end);
597 if (!T.consumeOpen()) {
598 if (Tok.isAnyIdentifier()) {
599 DirName =
600 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
601 ConsumeAnyToken();
602 } else {
603 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
604 }
605 T.consumeClose();
606 }
Alexey Bataev80909872015-07-02 11:25:17 +0000607 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000608 CancelRegion = ParseOpenMPDirectiveKind(*this);
609 if (Tok.isNot(tok::annot_pragma_openmp_end))
610 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000611 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000612
Alexey Bataevf29276e2014-06-18 04:14:57 +0000613 if (isOpenMPLoopDirective(DKind))
614 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
615 if (isOpenMPSimdDirective(DKind))
616 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
617 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000618 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000619
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000620 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000621 OpenMPClauseKind CKind =
622 Tok.isAnnotation()
623 ? OMPC_unknown
624 : FlushHasClause ? OMPC_flush
625 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000626 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000627 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000628 OMPClause *Clause =
629 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000630 FirstClauses[CKind].setInt(true);
631 if (Clause) {
632 FirstClauses[CKind].setPointer(Clause);
633 Clauses.push_back(Clause);
634 }
635
636 // Skip ',' if any.
637 if (Tok.is(tok::comma))
638 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000639 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000640 }
641 // End location of the directive.
642 EndLoc = Tok.getLocation();
643 // Consume final annot_pragma_openmp_end.
644 ConsumeToken();
645
Alexey Bataeveb482352015-12-18 05:05:56 +0000646 // OpenMP [2.13.8, ordered Construct, Syntax]
647 // If the depend clause is specified, the ordered construct is a stand-alone
648 // directive.
649 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000650 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000651 Diag(Loc, diag::err_omp_immediate_directive)
652 << getOpenMPDirectiveName(DKind) << 1
653 << getOpenMPClauseName(OMPC_depend);
654 }
655 HasAssociatedStatement = false;
656 }
657
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000658 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000659 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000660 // The body is a block scope like in Lambdas and Blocks.
661 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000662 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000663 Actions.ActOnStartOfCompoundStmt();
664 // Parse statement
665 AssociatedStmt = ParseStatement();
666 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000667 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000668 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000669 Directive = Actions.ActOnOpenMPExecutableDirective(
670 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
671 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000672
673 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000675 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000676 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000677 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000678 case OMPD_declare_simd:
679 Diag(Tok, diag::err_omp_unexpected_directive)
680 << getOpenMPDirectiveName(DKind);
681 SkipUntil(tok::annot_pragma_openmp_end);
682 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000683 case OMPD_unknown:
684 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000685 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000686 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000687 }
688 return Directive;
689}
690
Alexey Bataeva769e072013-03-22 06:34:35 +0000691/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000692/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000693///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000694/// simple-variable-list:
695/// '(' id-expression {, id-expression} ')'
696///
697bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
698 SmallVectorImpl<Expr *> &VarList,
699 bool AllowScopeSpecifier) {
700 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000701 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000702 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000703 if (T.expectAndConsume(diag::err_expected_lparen_after,
704 getOpenMPDirectiveName(Kind)))
705 return true;
706 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000707 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000708
709 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000710 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000711 CXXScopeSpec SS;
712 SourceLocation TemplateKWLoc;
713 UnqualifiedId Name;
714 // Read var name.
715 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000716 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000717
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000718 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000719 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000720 IsCorrect = false;
721 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000722 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000723 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000724 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000725 IsCorrect = false;
726 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000727 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000728 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
729 Tok.isNot(tok::annot_pragma_openmp_end)) {
730 IsCorrect = false;
731 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000732 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000733 Diag(PrevTok.getLocation(), diag::err_expected)
734 << tok::identifier
735 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000736 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000737 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000738 ExprResult Res =
739 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000740 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000741 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000742 }
743 // Consume ','.
744 if (Tok.is(tok::comma)) {
745 ConsumeToken();
746 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000747 }
748
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000749 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000750 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000751 IsCorrect = false;
752 }
753
754 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000755 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756
757 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000758}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000759
760/// \brief Parsing of OpenMP clauses.
761///
762/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000763/// if-clause | final-clause | num_threads-clause | safelen-clause |
764/// default-clause | private-clause | firstprivate-clause | shared-clause
765/// | linear-clause | aligned-clause | collapse-clause |
766/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000767/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000768/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000769/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000770/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000771/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000772/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000773///
774OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
775 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000776 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000777 bool ErrorFound = false;
778 // Check if clause is allowed for the given directive.
779 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000780 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
781 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000782 ErrorFound = true;
783 }
784
785 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000786 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000787 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000788 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000789 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000790 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000791 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000792 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000793 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000794 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000795 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000796 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000797 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000798 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000799 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000800 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000801 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000802 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000803 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000804 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000805 // OpenMP [2.9.1, target data construct, Restrictions]
806 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000807 // OpenMP [2.11.1, task Construct, Restrictions]
808 // At most one if clause can appear on the directive.
809 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000810 // OpenMP [teams Construct, Restrictions]
811 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000812 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000813 // OpenMP [2.9.1, task Construct, Restrictions]
814 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000815 // OpenMP [2.9.2, taskloop Construct, Restrictions]
816 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000817 // OpenMP [2.9.2, taskloop Construct, Restrictions]
818 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000819 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000820 Diag(Tok, diag::err_omp_more_one_clause)
821 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000822 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000823 }
824
Alexey Bataev10e775f2015-07-30 11:36:16 +0000825 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
826 Clause = ParseOpenMPClause(CKind);
827 else
828 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000829 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000830 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000831 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000832 // OpenMP [2.14.3.1, Restrictions]
833 // Only a single default clause may be specified on a parallel, task or
834 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000835 // OpenMP [2.5, parallel Construct, Restrictions]
836 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000837 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000838 Diag(Tok, diag::err_omp_more_one_clause)
839 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000840 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000841 }
842
843 Clause = ParseOpenMPSimpleClause(CKind);
844 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000845 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +0000846 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000847 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000848 // OpenMP [2.7.1, Restrictions, p. 3]
849 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000850 // OpenMP [2.10.4, Restrictions, p. 106]
851 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +0000852 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000853 Diag(Tok, diag::err_omp_more_one_clause)
854 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000855 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000856 }
857
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000858 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000859 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
860 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000861 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000862 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000863 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000864 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000865 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000866 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000867 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000868 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000869 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000870 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000871 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000872 // OpenMP [2.7.1, Restrictions, p. 9]
873 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000874 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
875 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000876 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000877 Diag(Tok, diag::err_omp_more_one_clause)
878 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000879 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000880 }
881
882 Clause = ParseOpenMPClause(CKind);
883 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000884 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000885 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000886 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000887 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000888 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000889 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000890 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000891 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000892 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000893 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000894 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000895 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000896 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000897 break;
898 case OMPC_unknown:
899 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000900 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000901 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000902 break;
903 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000904 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
905 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000906 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000907 break;
908 }
Craig Topper161e4db2014-05-21 06:02:52 +0000909 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000910}
911
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000912/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000913/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000914/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000915///
Alexey Bataev3778b602014-07-17 07:32:53 +0000916/// final-clause:
917/// 'final' '(' expression ')'
918///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000919/// num_threads-clause:
920/// 'num_threads' '(' expression ')'
921///
922/// safelen-clause:
923/// 'safelen' '(' expression ')'
924///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000925/// simdlen-clause:
926/// 'simdlen' '(' expression ')'
927///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000928/// collapse-clause:
929/// 'collapse' '(' expression ')'
930///
Alexey Bataeva0569352015-12-01 10:17:31 +0000931/// priority-clause:
932/// 'priority' '(' expression ')'
933///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000934/// grainsize-clause:
935/// 'grainsize' '(' expression ')'
936///
Alexey Bataev382967a2015-12-08 12:06:20 +0000937/// num_tasks-clause:
938/// 'num_tasks' '(' expression ')'
939///
Alexey Bataev28c75412015-12-15 08:19:24 +0000940/// hint-clause:
941/// 'hint' '(' expression ')'
942///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000943OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
944 SourceLocation Loc = ConsumeToken();
945
946 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
947 if (T.expectAndConsume(diag::err_expected_lparen_after,
948 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000949 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000950
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000951 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000952 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
953 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000954 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000955
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000956 // Parse ')'.
957 T.consumeClose();
958
959 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000960 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000961
Alexey Bataeva55ed262014-05-28 06:15:33 +0000962 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000963 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000964}
965
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000966/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000967///
968/// default-clause:
969/// 'default' '(' 'none' | 'shared' ')
970///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000971/// proc_bind-clause:
972/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
973///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000974OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
975 SourceLocation Loc = Tok.getLocation();
976 SourceLocation LOpen = ConsumeToken();
977 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000978 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000979 if (T.expectAndConsume(diag::err_expected_lparen_after,
980 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000981 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982
Alexey Bataeva55ed262014-05-28 06:15:33 +0000983 unsigned Type = getOpenMPSimpleClauseType(
984 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000985 SourceLocation TypeLoc = Tok.getLocation();
986 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
987 Tok.isNot(tok::annot_pragma_openmp_end))
988 ConsumeAnyToken();
989
990 // Parse ')'.
991 T.consumeClose();
992
993 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
994 Tok.getLocation());
995}
996
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000997/// \brief Parsing of OpenMP clauses like 'ordered'.
998///
999/// ordered-clause:
1000/// 'ordered'
1001///
Alexey Bataev236070f2014-06-20 11:19:47 +00001002/// nowait-clause:
1003/// 'nowait'
1004///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001005/// untied-clause:
1006/// 'untied'
1007///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001008/// mergeable-clause:
1009/// 'mergeable'
1010///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001011/// read-clause:
1012/// 'read'
1013///
Alexey Bataev346265e2015-09-25 10:37:12 +00001014/// threads-clause:
1015/// 'threads'
1016///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001017/// simd-clause:
1018/// 'simd'
1019///
Alexey Bataevb825de12015-12-07 10:51:44 +00001020/// nogroup-clause:
1021/// 'nogroup'
1022///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001023OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1024 SourceLocation Loc = Tok.getLocation();
1025 ConsumeAnyToken();
1026
1027 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1028}
1029
1030
Alexey Bataev56dafe82014-06-20 07:16:17 +00001031/// \brief Parsing of OpenMP clauses with single expressions and some additional
1032/// argument like 'schedule' or 'dist_schedule'.
1033///
1034/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001035/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1036/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001037///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001038/// if-clause:
1039/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1040///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001041/// defaultmap:
1042/// 'defaultmap' '(' modifier ':' kind ')'
1043///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001044OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1045 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001046 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001047 // Parse '('.
1048 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1049 if (T.expectAndConsume(diag::err_expected_lparen_after,
1050 getOpenMPClauseName(Kind)))
1051 return nullptr;
1052
1053 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001054 SmallVector<unsigned, 4> Arg;
1055 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001056 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001057 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1058 Arg.resize(NumberOfElements);
1059 KLoc.resize(NumberOfElements);
1060 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1061 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1062 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1063 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001064 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001065 if (KindModifier > OMPC_SCHEDULE_unknown) {
1066 // Parse 'modifier'
1067 Arg[Modifier1] = KindModifier;
1068 KLoc[Modifier1] = Tok.getLocation();
1069 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1070 Tok.isNot(tok::annot_pragma_openmp_end))
1071 ConsumeAnyToken();
1072 if (Tok.is(tok::comma)) {
1073 // Parse ',' 'modifier'
1074 ConsumeAnyToken();
1075 KindModifier = getOpenMPSimpleClauseType(
1076 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1077 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1078 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001079 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001080 KLoc[Modifier2] = Tok.getLocation();
1081 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1082 Tok.isNot(tok::annot_pragma_openmp_end))
1083 ConsumeAnyToken();
1084 }
1085 // Parse ':'
1086 if (Tok.is(tok::colon))
1087 ConsumeAnyToken();
1088 else
1089 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1090 KindModifier = getOpenMPSimpleClauseType(
1091 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1092 }
1093 Arg[ScheduleKind] = KindModifier;
1094 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001095 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1096 Tok.isNot(tok::annot_pragma_openmp_end))
1097 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001098 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1099 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1100 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001101 Tok.is(tok::comma))
1102 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001103 } else if (Kind == OMPC_dist_schedule) {
1104 Arg.push_back(getOpenMPSimpleClauseType(
1105 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1106 KLoc.push_back(Tok.getLocation());
1107 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1108 Tok.isNot(tok::annot_pragma_openmp_end))
1109 ConsumeAnyToken();
1110 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1111 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001112 } else if (Kind == OMPC_defaultmap) {
1113 // Get a defaultmap modifier
1114 Arg.push_back(getOpenMPSimpleClauseType(
1115 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1116 KLoc.push_back(Tok.getLocation());
1117 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1118 Tok.isNot(tok::annot_pragma_openmp_end))
1119 ConsumeAnyToken();
1120 // Parse ':'
1121 if (Tok.is(tok::colon))
1122 ConsumeAnyToken();
1123 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1124 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1125 // Get a defaultmap kind
1126 Arg.push_back(getOpenMPSimpleClauseType(
1127 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1128 KLoc.push_back(Tok.getLocation());
1129 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1130 Tok.isNot(tok::annot_pragma_openmp_end))
1131 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001132 } else {
1133 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001134 KLoc.push_back(Tok.getLocation());
1135 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1136 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001137 ConsumeToken();
1138 if (Tok.is(tok::colon))
1139 DelimLoc = ConsumeToken();
1140 else
1141 Diag(Tok, diag::warn_pragma_expected_colon)
1142 << "directive name modifier";
1143 }
1144 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001145
Carlo Bertollib4adf552016-01-15 18:50:31 +00001146 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1147 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1148 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001149 if (NeedAnExpression) {
1150 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001151 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1152 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001153 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001154 }
1155
1156 // Parse ')'.
1157 T.consumeClose();
1158
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001159 if (NeedAnExpression && Val.isInvalid())
1160 return nullptr;
1161
Alexey Bataev56dafe82014-06-20 07:16:17 +00001162 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001163 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001164 T.getCloseLocation());
1165}
1166
Alexey Bataevc5e02582014-06-16 07:08:35 +00001167static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1168 UnqualifiedId &ReductionId) {
1169 SourceLocation TemplateKWLoc;
1170 if (ReductionIdScopeSpec.isEmpty()) {
1171 auto OOK = OO_None;
1172 switch (P.getCurToken().getKind()) {
1173 case tok::plus:
1174 OOK = OO_Plus;
1175 break;
1176 case tok::minus:
1177 OOK = OO_Minus;
1178 break;
1179 case tok::star:
1180 OOK = OO_Star;
1181 break;
1182 case tok::amp:
1183 OOK = OO_Amp;
1184 break;
1185 case tok::pipe:
1186 OOK = OO_Pipe;
1187 break;
1188 case tok::caret:
1189 OOK = OO_Caret;
1190 break;
1191 case tok::ampamp:
1192 OOK = OO_AmpAmp;
1193 break;
1194 case tok::pipepipe:
1195 OOK = OO_PipePipe;
1196 break;
1197 default:
1198 break;
1199 }
1200 if (OOK != OO_None) {
1201 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001202 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001203 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1204 return false;
1205 }
1206 }
1207 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1208 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001209 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001210 TemplateKWLoc, ReductionId);
1211}
1212
Alexander Musman1bb328c2014-06-04 13:06:39 +00001213/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001214/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001215///
1216/// private-clause:
1217/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001218/// firstprivate-clause:
1219/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001220/// lastprivate-clause:
1221/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001222/// shared-clause:
1223/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001224/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001225/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001226/// aligned-clause:
1227/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001228/// reduction-clause:
1229/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001230/// copyprivate-clause:
1231/// 'copyprivate' '(' list ')'
1232/// flush-clause:
1233/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001234/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001235/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001236/// map-clause:
1237/// 'map' '(' [ [ always , ]
1238/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001239///
Alexey Bataev182227b2015-08-20 10:54:39 +00001240/// For 'linear' clause linear-list may have the following forms:
1241/// list
1242/// modifier(list)
1243/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001244OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1245 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001246 SourceLocation Loc = Tok.getLocation();
1247 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001248 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001249 // Optional scope specifier and unqualified id for reduction identifier.
1250 CXXScopeSpec ReductionIdScopeSpec;
1251 UnqualifiedId ReductionId;
1252 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001253 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +00001254 // OpenMP 4.1 [2.15.3.7, linear Clause]
1255 // If no modifier is specified it is assumed to be val.
1256 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001257 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
1258 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +00001259 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001260 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001261 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001262
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001263 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001264 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001265 if (T.expectAndConsume(diag::err_expected_lparen_after,
1266 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001267 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001268
Alexey Bataev182227b2015-08-20 10:54:39 +00001269 bool NeedRParenForLinear = false;
1270 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1271 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001272 // Handle reduction-identifier for reduction clause.
1273 if (Kind == OMPC_reduction) {
1274 ColonProtectionRAIIObject ColonRAII(*this);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001275 if (getLangOpts().CPlusPlus)
1276 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec,
1277 /*ObjectType=*/nullptr,
1278 /*EnteringContext=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001279 InvalidReductionId =
1280 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
1281 if (InvalidReductionId) {
1282 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1283 StopBeforeMatch);
1284 }
1285 if (Tok.is(tok::colon)) {
1286 ColonLoc = ConsumeToken();
1287 } else {
1288 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1289 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001290 } else if (Kind == OMPC_depend) {
1291 // Handle dependency type for depend clause.
1292 ColonProtectionRAIIObject ColonRAII(*this);
1293 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1294 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001295 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001296
1297 if (DepKind == OMPC_DEPEND_unknown) {
1298 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1299 StopBeforeMatch);
1300 } else {
1301 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +00001302 // Special processing for depend(source) clause.
1303 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
1304 // Parse ')'.
1305 T.consumeClose();
1306 return Actions.ActOnOpenMPVarListClause(
1307 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
1308 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
1309 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00001310 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1311 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +00001312 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001313 }
1314 if (Tok.is(tok::colon)) {
1315 ColonLoc = ConsumeToken();
1316 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00001317 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1318 : diag::warn_pragma_expected_colon)
1319 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001320 }
Alexey Bataev182227b2015-08-20 10:54:39 +00001321 } else if (Kind == OMPC_linear) {
1322 // Try to parse modifier if any.
1323 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +00001324 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +00001325 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001326 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +00001327 LinearT.consumeOpen();
1328 NeedRParenForLinear = true;
1329 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00001330 } else if (Kind == OMPC_map) {
1331 // Handle map type for map clause.
1332 ColonProtectionRAIIObject ColonRAII(*this);
1333
Samuel Antaof91b1632016-02-27 00:01:58 +00001334 /// The map clause modifier token can be either a identifier or the C++
1335 /// delete keyword.
1336 auto IsMapClauseModifierToken = [](const Token &Tok) {
1337 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1338 };
1339
1340 // The first identifier may be a list item, a map-type or a
1341 // map-type-modifier. The map modifier can also be delete which has the same
1342 // spelling of the C++ delete keyword.
Kelvin Li0bff7af2015-11-23 05:32:03 +00001343 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001344 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001345 DepLinMapLoc = Tok.getLocation();
1346 bool ColonExpected = false;
1347
Samuel Antaof91b1632016-02-27 00:01:58 +00001348 if (IsMapClauseModifierToken(Tok)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00001349 if (PP.LookAhead(0).is(tok::colon)) {
1350 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001351 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001352 if (MapType == OMPC_MAP_unknown) {
1353 Diag(Tok, diag::err_omp_unknown_map_type);
1354 } else if (MapType == OMPC_MAP_always) {
1355 Diag(Tok, diag::err_omp_map_type_missing);
1356 }
1357 ConsumeToken();
1358 } else if (PP.LookAhead(0).is(tok::comma)) {
Samuel Antaof91b1632016-02-27 00:01:58 +00001359 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001360 PP.LookAhead(2).is(tok::colon)) {
1361 MapTypeModifier =
1362 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001363 Kind,
1364 IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001365 if (MapTypeModifier != OMPC_MAP_always) {
1366 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1367 MapTypeModifier = OMPC_MAP_unknown;
1368 } else {
1369 MapTypeModifierSpecified = true;
1370 }
1371
1372 ConsumeToken();
1373 ConsumeToken();
1374
1375 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001376 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001377 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1378 Diag(Tok, diag::err_omp_unknown_map_type);
1379 }
1380 ConsumeToken();
1381 } else {
1382 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001383 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001384 }
1385 } else {
1386 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001387 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001388 }
1389 } else {
Samuel Antao5de996e2016-01-22 20:21:36 +00001390 MapType = OMPC_MAP_tofrom;
1391 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001392 }
1393
1394 if (Tok.is(tok::colon)) {
1395 ColonLoc = ConsumeToken();
1396 } else if (ColonExpected) {
1397 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1398 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001399 }
1400
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001401 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001402 bool IsComma =
1403 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1404 (Kind != OMPC_map)) ||
1405 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001406 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001407 (!MapTypeModifierSpecified ||
1408 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1409 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001410 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001411 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001412 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001413 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001414 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001415 ExprResult VarExpr =
1416 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataevc5970622016-04-01 08:43:42 +00001417 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001418 Vars.push_back(VarExpr.get());
Alexey Bataevc5970622016-04-01 08:43:42 +00001419 } else {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001420 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001421 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001422 }
1423 // Skip ',' if any
1424 IsComma = Tok.is(tok::comma);
Alexey Bataevc5970622016-04-01 08:43:42 +00001425 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001426 ConsumeToken();
Alexey Bataevc5970622016-04-01 08:43:42 +00001427 else if (Tok.isNot(tok::r_paren) &&
Alexander Musman8dba6642014-04-22 13:09:42 +00001428 Tok.isNot(tok::annot_pragma_openmp_end) &&
1429 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001430 Diag(Tok, diag::err_omp_expected_punc)
1431 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1432 : getOpenMPClauseName(Kind))
1433 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001434 }
1435
Alexey Bataev182227b2015-08-20 10:54:39 +00001436 // Parse ')' for linear clause with modifier.
1437 if (NeedRParenForLinear)
1438 LinearT.consumeClose();
1439
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001440 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001441 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001442 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1443 if (MustHaveTail) {
1444 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001445 SourceLocation ELoc = ConsumeToken();
1446 ExprResult Tail = ParseAssignmentExpression();
1447 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001448 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001449 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001450 else
1451 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1452 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001453 }
1454
1455 // Parse ')'.
1456 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001457 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001458 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1459 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001460 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001461 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001462
Alexey Bataevc5e02582014-06-16 07:08:35 +00001463 return Actions.ActOnOpenMPVarListClause(
1464 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1465 ReductionIdScopeSpec,
1466 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001467 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001468 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1469 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001470}
1471